-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDAI.py
180 lines (142 loc) · 4.55 KB
/
DAI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#todo
#溫溼度 濕度只要低於多少啟動 溫度是高於多少啟動
#時間到了 判斷濕度有沒有低於控制值
#todo
#溫溼度 濕度只要低於多少啟動 溫度是高於多少啟動
#時間到了 判斷濕度有沒有低於控制值
import time, random, requests, threading, json, os, datetime
import DAN
import atexit
from flask import Flask, render_template, request, jsonify, url_for, redirect, abort
def Auto_pull():
global odf_list, odf_data, data
while True:
time.sleep(5)
if DAN.state == 'RESUME':
for odf in odf_list:
d = DAN.pull(odf)
if d != None:
#print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %p')-data[odf+'_time'])
data[odf+'_time'] = datetime.datetime.now().strftime('%H:%M:%S %p')
odf_data[odf] = round(d[0], 2)
IOT_ServerURL = 'http://140.113.111.72:9999' #with SSL connection
Reg_addr = None #if None, Reg_addr = MAC address
WEB_HOST = '0.0.0.0'
WEB_PORT = 9487
config_name = 'config.json'
idf_list = ['Switch1']
odf_list = ['Humidity1-O', 'Temperature1-O']
odf_data = {'Humidity1-O':'GG', 'Temperature1-O':'GG'}
data = {
"Manual_mode":0,
"end_hour":17,
"end_min":20,
"max_Temperature":0,
"min_Humidity":-6,
"start_hour":17,
"start_min":0,
"switch":0,
"Humidity1-O_time": datetime.datetime.now().strftime('%H:%M:%S %p'),
"Temperature1-O_time":datetime.datetime.now().strftime('%H:%M:%S %p')
}
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
global data
return render_template('index.html', profile=DAN.profile
, state=DAN.state=='SUSPEND'
, data=data
, IOT_ServerURL=IOT_ServerURL)
@app.route('/pull', methods=['GET'])
def get_pull():
global odf_data, data
if DAN.state == 'SUSPEND':
abort(404)
return json.dumps(dict( **odf_data, **data))
@app.route('/con_check', methods=['GET'])
def get_con():
return json.dumps(DAN.state == 'RESUME')
@app.route('/update', methods=['POST'])
def update():
global data
datas = request.form.to_dict()
print(datas)
for d in datas:
data[d] = int(datas[d])
save_config(config_name)
return json.dumps(data)
def killport(port):
command= f'lsof -nti:{port} | xargs kill -9'
os.system(command)
def on_exit():
DAN.deregister()
killport(WEB_PORT)
save_config(config_name)
def open_config(filename):
global data
if os.path.isfile(filename):
with open(filename, 'r') as f:
if f != None:
data = json.load(f)
def save_config(filename):
global data
with open(filename, 'w') as f:
t = json.dumps(data, sort_keys=True, indent=4, separators=(',', ':'))
f.write(t)
def Switch_control(flag, con):
global data
print(con)
if flag == '1' or flag == True or flag == 'true':
flag = 1
else:
flag = 0
data['switch'] = flag
def check_time():
global data
now = datetime.datetime.now()
sh = data['start_hour']
sm = data['start_min']
eh = data['end_hour']
em = data['end_min']
if datetime.time(sh,sm) <= now.time() <= datetime.time(eh,em):
return True
else:
return False
def check_control():
global data, odf_data
if odf_data['Humidity1-O'] != 'GG':
return data['min_Humidity'] > odf_data['Humidity1-O']
else:
return True
def auto_push_switch():
global data
while True:
if data['Manual_mode'] == 0:
if check_time() and check_control():
Switch_control(1, 'control')
else:
Switch_control(0, 'control')
DAN.push('Switch1', data['switch'])
time.sleep(5)
def main():
print(type(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %p')))
DAN.profile['dm_name'] = 'SwitchAIO'
DAN.profile['d_name'] = 'TEST_SwitchAIO'
DAN.profile['df_list'] = idf_list + odf_list
open_config(config_name)
#DAN.profile['d_name']= 'Assign a Device Name'
DAN.device_registration_with_retry(IOT_ServerURL, Reg_addr)
t_auto_pull = threading.Thread(target=Auto_pull)
t_auto_pull.start()
t_time_control = threading.Thread(target=auto_push_switch)
t_time_control.start()
atexit.register(on_exit)
app.run(
host=WEB_HOST,
port=WEB_PORT,
threaded = True,
debug=False
)
#killport(WEB_PORT)
if '__main__' == __name__:
main()