-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver.py
322 lines (298 loc) · 12.1 KB
/
httpserver.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from microWebSrv import MicroWebSrv
import machine
import ujson
class HttpServer:
def __init__(self, process_obj, wifi_obj, rtc_obj, user_settings_dict):
self.process = process_obj
self.wifi = wifi_obj
self.rtc = rtc_obj
self.settings = user_settings_dict
self.app = None
self.gravity_sg = None
self.set_temp = None
self.chamber_temp = None
self.wort_temp = None
self.time_mark = None
def start(self):
process = self.process
wifi = self.wifi
rtc = self.rtc
settings = self.settings
this = self
@MicroWebSrv.route('/connecttest')
def test_get(httpClient, httpResponse):
"""
测试前端和后端之间的网络连接
"""
httpResponse.WriteResponseOk()
# Define the web routes and functions
@MicroWebSrv.route('/overview')
def overview_get(httpClient, httpResponse):
"""
提供首页所有数据显示,前端每1分钟请求1次
"""
brewery_name = settings['breweryName']
wifi_connected = wifi.is_connected()
real_date = rtc.get_localdate()
real_time = rtc.get_localtime()
basic_info = {
'breweryName': brewery_name,
'wifiIsConnected': wifi_connected,
'realDate': real_date,
'realTime': real_time,
}
process_info = process.get_process_info()
overview = basic_info.copy()
overview.update(process_info)
# 以下数据更新用于前端绘制echarts折线图
this.set_temp = process_info.get('setTemp')
this.chamber_temp = process_info.get('chamberTemp')
this.wort_temp = process_info.get('wortTemp')
this.gravity_sg = process_info.get('hydrometerData').get('currentGravity')
this.time_mark = real_date + ' ' + real_time
httpResponse.WriteResponseJSONOk(obj=overview, headers=None)
@MicroWebSrv.route('/fermentation', 'POST')
def fermentation_post(httpClient, httpResponse):
"""
前台向后台提交发酵步骤数据,并且开始发酵过程
"""
json = httpClient.ReadRequestContentAsJSON()
beerName = json['beerName']
fermentationSteps = json['fermentationSteps']
try:
process.set_beer_name(beerName)
process.load_steps(fermentationSteps)
process.start()
except:
# throw 500 error code
httpResponse.WriteResponseInternalServerError()
else:
httpResponse.WriteResponseOk()
@MicroWebSrv.route('/abort')
def abort_get(httpClient, httpResponse):
try:
process.abort()
except:
# throw 500 error code
httpResponse.WriteResponseInternalServerError()
else:
httpResponse.WriteResponseOk()
@MicroWebSrv.route('/settings')
def settings_get(httpClient, httpResponse):
"""
从后台读取设置参数
"""
wifi_list = wifi.scan_wifi_list()
temp_sensor_list = process.fermenter_temp_ctrl.chamber_sensor.ds_obj.get_device_list()
# open user_settings.json and read settings
with open('user_settings.json', 'r') as f:
settings_dict = ujson.load(f)
wort_sensor_dev_num = settings_dict.get('wortSensorDev')
chamber_sensor_dev_num = settings_dict.get('chamberSensorDev')
wort_sensor_rom_code = ''
chamber_sensor_rom_code = ''
for sensor_dict in temp_sensor_list:
detail_list = sensor_dict.values()
if wort_sensor_dev_num in detail_list:
wort_sensor_rom_code = sensor_dict.get('label')
elif chamber_sensor_dev_num in detail_list:
chamber_sensor_rom_code = sensor_dict.get('label')
wort_sensor_dev = {
'value': wort_sensor_dev_num,
'label': wort_sensor_rom_code
}
chamber_sensor_dev = {
'value': chamber_sensor_dev_num,
'label': chamber_sensor_rom_code
}
settings_added = {
'wifiList': wifi_list,
'wortSensorDev': wort_sensor_dev,
'chamberSensorDev': chamber_sensor_dev,
'tempSensorList': temp_sensor_list
}
settings_combined = settings_dict.copy()
settings_combined.update(settings_added)
# settings_json = ujson.dumps(settings_combined)
httpResponse.WriteResponseJSONOk(obj=settings_combined, headers=None)
# httpResponse.WriteResponseOk(
# headers=None,
# contentType="application/json",
# contentCharset="UTF-8",
# content=settings_json
# )
@MicroWebSrv.route('/settings', 'POST')
def settings_post(httpClient, httpResponse):
"""
向后台保存设置参数,并且重启ESP32
"""
# json = httpClient.ReadRequestContentAsJSON()
# settings_dict = ujson.loads(json)
settings_dict = httpClient.ReadRequestContentAsJSON()
settings_dict['wortSensorDev'] = settings_dict['wortSensorDev']['value']
settings_dict['chamberSensorDev'] = settings_dict['chamberSensorDev']['value']
try:
with open('user_settings.json', 'w') as f:
ujson.dump(settings_dict, f)
except:
httpResponse.WriteResponseInternalServerError()
else:
httpResponse.WriteResponseOk()
@MicroWebSrv.route('/reboot')
def reboot_get(httpClient, httpResponse):
tim = machine.Timer(-1)
try:
tim.init(period=3000, mode=machine.Timer.ONE_SHOT, callback=lambda t: machine.reset())
except:
httpResponse.WriteResponseInternalServerError()
else:
httpResponse.WriteResponseOk()
@MicroWebSrv.route('/wifi')
def wifi_get(httpClient, httpResponse):
"""
获取WIFI热点列表,用于刷新热点列表
"""
wifi_list = wifi.scan_wifi_list()
wifi_dict = {'wifiList': wifi_list}
httpResponse.WriteResponseJSONOk(obj=wifi_dict, headers=None)
@MicroWebSrv.route('/wifi', 'POST')
def wifi_post(httpClient, httpResponse):
"""
连接WIFI热点
"""
wifi_dict = httpClient.ReadRequestContentAsJSON()
try:
new_ip = wifi.sta_connect(wifi_dict['ssid'], wifi_dict['pass'])
except:
httpResponse.WriteResponseInternalServerError()
else:
if wifi.is_connected():
httpResponse.WriteResponseOk()
else:
httpResponse.WriteResponseInternalServerError()
@MicroWebSrv.route('/ip')
def ip_get(httpClient, httpResponse):
"""
获取IP地址
"""
ap_ip = wifi.get_ap_ip_addr()
sta_ip = wifi.get_sta_ip_addr()
sta_ssid = wifi.get_sta_ssid()
ip_dict = {
'apIp': ap_ip,
'staIp': sta_ip,
'staSsid': sta_ssid
}
httpResponse.WriteResponseJSONOk(obj=ip_dict, headers=None)
@MicroWebSrv.route('/ftp')
def ftp_get(httpClient, httpResponse):
"""
Start FTP service
"""
try:
import uftpd
except:
httpResponse.WriteResponseInternalServerError()
else:
httpResponse.WriteResponseOk()
@MicroWebSrv.route('/mqtttest', 'POST')
def mqtt_post(httpClient, httpResponse):
"""
Send test message to MQTT server
"""
settings_dict = httpClient.ReadRequestContentAsJSON()
test_msg = ujson.dumps({'test-message': 200})
from mqtt_client import MQTT
try:
test_mqtt = MQTT(settings_dict)
test_mqtt.publish(test_msg)
except:
httpResponse.WriteResponseInternalServerError()
else:
httpResponse.WriteResponseOk()
@MicroWebSrv.route('/actuator', 'POST')
def actuactor_post(httpClient, httpResponse):
"""
手动控制制冷压缩机或制热器,主要用于测试
"""
actuactor_dict = httpClient.ReadRequestContentAsJSON()
heater = process.fermenter_temp_ctrl.heater
cooler = process.fermenter_temp_ctrl.cooler
led = process.fermenter_temp_ctrl.led
try:
if actuactor_dict.get('element') == 'heater':
element = heater
else:
element = cooler
action = actuactor_dict.get('action')
if action:
element.force_on()
else:
element.force_off()
if heater.is_on() and not cooler.is_on():
led_color = 'red'
elif not heater.is_on() and cooler.is_on():
led_color = 'blue'
elif not heater.is_on() and not cooler.is_on():
led_color = 'green'
else:
led_color = 'orange'
led.set_color(led_color)
except:
httpResponse.WriteResponseInternalServerError()
else:
httpResponse.WriteResponseOk()
@MicroWebSrv.route('/tempsensors', 'POST')
def temp_post(httpClient, httpResponse):
"""
获取温度传感器读数
"""
# 获取前端发来的设备序号
sensor_dict = httpClient.ReadRequestContentAsJSON()
new_wort_romcode = sensor_dict.get('wortSensorDev').get('value')
new_chamber_romcode = sensor_dict.get('chamberSensorDev').get('value')
# 获取温感对象实例
wort_sensor = process.fermenter_temp_ctrl.wort_sensor
chamber_sensor = process.fermenter_temp_ctrl.chamber_sensor
# 更新温感设备序号
wort_sensor.update_romcode(new_wort_romcode)
chamber_sensor.update_romcode(new_chamber_romcode)
try:
# 测量温度
wort_temp = wort_sensor.read_temp()
chamber_temp = chamber_sensor.read_temp()
except:
# throw 500 error code
httpResponse.WriteResponseInternalServerError()
else:
temp_dict = {
'wortTemp': wort_temp,
'chamberTemp': chamber_temp
}
httpResponse.WriteResponseJSONOk(obj=temp_dict, headers=None)
@MicroWebSrv.route('/hydrometer', 'POST')
def gravity_post(httpClient, httpResponse):
hydrometer_dict = httpClient.ReadRequestContentAsJSON()
process.save_hydrometer_data(hydrometer_dict)
httpResponse.WriteResponseOk()
@MicroWebSrv.route('/chart')
def chart_get(httpClient, httpResponse):
data = {
'timeMark': this.time_mark,
'setTemp': this.set_temp,
'wortTemp': this.wort_temp,
'chamberTemp': this.chamber_temp,
'gravitySg': this.gravity_sg
}
httpResponse.WriteResponseJSONOk(obj=data, headers=None)
# Initialize the Web server
self.app = MicroWebSrv(webPath='/sd/www')
self.app.Start(threaded=True) # Starts the server
def stop(self):
if self.app:
self.app.Stop()
def is_started(self):
if self.app:
return self.app.IsStarted()
else:
return False