| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- # web服务
- from microdot import Microdot
- import machine
- from config import getConfig,saveConfig
- import utils
- import _thread
- app = Microdot()
- @app.before_request
- # 请求时暂停主循环检查任务
- def before_request():
- getConfig().runTask = False
- @app.after_request
- def after_request():
- getConfig().runTask = True
- @app.route('/ping')
- async def ping(req):
- return 'esp'
- @app.post('/addWifi')
- async def addWifi(req):
- wifiName = req.form.get('wifiName')
- wifiPwd = req.form.get('wifiPwd')
- getConfig().wifi[wifiName] = wifiPwd
- saveConfig()
- return '1'
- @app.post('/rmWifi')
- async def rmWifi(req):
- wifiName = req.form.get('wifiName')
- try:
- del getConfig().wifi[wifiName]
- except:
- print('')
- saveConfig()
- return '1'
- @app.post('/connectWifi')
- async def connectWifi(req):
- ssid = req.form.get('wifiName')
- pwd = req.form.get('wifiPwd')
- if utils.connectWifi(ssid, pwd):
- getConfig().wifi[ssid] = pwd
- saveConfig()
- getConfig().runTask = True
- return '1'
- return '2'
- @app.post('/updateAp')
- async def updateAp(req):
- apName = req.form.get('apName')
- apPwd = req.form.get('apPwd')
- getConfig().ap.ssid = apName
- getConfig().ap.pwd = apPwd
- saveConfig()
- return '1'
- @app.get('/scanWifi')
- async def scanWifi(request):
- return utils.scanWifi()
- @app.get('/getInfo')
- async def index(request):
- return getConfig().toJson()
- @app.get('/disconnectWifi')
- async def disconnectWifi(request):
- utils.disconnectWifi()
- @app.route('/restart')
- def index(req):
- machine.reset()
- @app.post('/bind')
- async def index(req):
- uid = req.form.get('uid')
- getConfig().mqtt.clientId = uid
- saveConfig()
- getConfig().mqttStatus = False
- return '1'
- def openHttpServer():
- if not getConfig().httpServer:
- getConfig().httpServer = True
- app.run(host='0.0.0.0', port=80, debug=True)
- def startHttpServer():
- if not getConfig().httpServer:
- _thread.start_new_thread(openHttpServer, ())
- def closeHttpServer():
- if getConfig().httpServer:
- getConfig().httpServer = False
- app.shutdown()
- print('http server 关闭')
|