-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebsrv.py
111 lines (82 loc) · 2.83 KB
/
websrv.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
import uasyncio as asyncio
from microdot_asyncio import Microdot, send_file, redirect
import os
import machine
import network
import config
from time import sleep
import _thread
_ipaddress=""
_PanelConnected=False
cfg = ""
def set_webpage_vars( ipaddress, PanelConnected):
global _ipaddress,_PanelConnected
_ipaddress = ipaddress[0]
_PanelConnected=PanelConnected
def set_panel_isconnected(panelconnected):
global _PanelConnected
_PanelConnected=panelconnected
app = Microdot()
@app.route('/')
async def index(request):
f = open('html/index.html','r')
htmlstr =f.read()
htmlstr = htmlstr.replace("[controller_name]",cfg.controller_name)
htmlstr = htmlstr.replace("[ipaddress]",_ipaddress)
htmlstr = htmlstr.replace("[PanelConnected]",str(_PanelConnected))
#response = htmlstr
return htmlstr, 200, {'Content-Type': 'text/html'}
def reset_machine():
sleep(5)
machine.reset()
def softreset_machine():
sleep(5)
machine.soft_reset()
@app.route('/reset')
async def reset(request):
treset = _thread.start_new_thread(reset_machine,())
return redirect('/')
@app.route('/jsonconfig')
async def jsonconfig(request):
return send_file('config.json',200,'application/json')
@app.route('/config')
async def config(request):
return send_file('html/config.html')
@app.route('/savejson',methods=['POST'])
async def savejson(request):
try:
cfg.wifissid=request.form['wifissid']
cfg.wifipassword=request.form['wifipassword']
cfg.mqttserver=request.form['mqttserver']
cfg.mqttusername=request.form['mqttusername']
cfg.mqttpassword=request.form['mqttpassword']
cfg.root_topicOut=request.form['root_topicOut']
cfg.root_topicStatus=request.form['root_topicStatus']
cfg.root_topicIn=request.form['root_topicIn']
cfg.root_topicHassioArm=request.form['root_topicHassioArm']
cfg.ESP_UART=int(request.form['ESP_UART'])
cfg.controller_name=request.form['controller_name']
cfg.timezone=request.form['timezone']
cfg.homekit=request.form['homekit']
cfg.homekit_user=request.form['homekit_user']
cfg.homekit_secure=request.form['homekit_secure']
f = open('config.json','w')
f.write(cfg.toJson())
f.close()
return send_file('html/saveOK.html')
except:
return "ERROR SAVING <a href='/config'>Return to config</a>",200,"text/html"
@app.route('/test')
async def testsave(request):
return send_file('html/test.html')
@app.route('/Soft_reset')
async def Soft_reset(request):
treset = _thread.start_new_thread(softreset_machine,())
return redirect('/')
async def main():
await app.start_server(debug=True,port=80)
def runsrv():
asyncio.run(main())
def stopsrv():
app.shutdown()
#runsrv()