async def double_press(): gwlogging.sendLog(gwlogging.INFO, "Double Press Detected", MOD) if wifimgr.check_connection() == False or CLOUD_CONFIG == None: gwlogging.sendLog( gwlogging.ERR, "GW must be configured before Web Server can be disabled", MOD) pass elif GW_CONFIG == None: temp = {} temp['web'] = 0 config_manager.write_gw(temp) sleep(5) reset() elif GW_CONFIG['web'] == 1: temp = GW_CONFIG temp['web'] = 0 config_manager.write_gw(temp) sleep(5) reset() else: temp = GW_CONFIG temp['web'] = 1 config_manager.write_gw(temp) sleep(5) reset()
def RequestTestPost(microWebSrv2, request): gc.collect() data = request.GetPostedURLEncodedForm() try: if data['restart'] == '0': content = head1 + restart_selected + head2 + """ <div class="header"> <h1> Restart Cancelled</h1> </div> """ + end request.Response.ReturnOk(content) elif data['restart'] == '1': gwlogging.sendLog(gwlogging.DEBUG, "Restarting. Inititated via web.", MOD) content = head1 + restart_selected + head2 + """ <div class="header"> <h1> Ruuvi GW Restarting</h1> </div> """ + end request.Response.ReturnOk(content) import _thread _thread.start_new_thread(restart, ()) except: request.Response.ReturnBadRequest() return
def log(self, in_reconnect, e): if self.DEBUG: if in_reconnect: gwlogging.sendLog(gwlogging.DEBUG, ("mqtt reconnect: %r" % e), "MQTT") #print("mqtt reconnect: %r" % e) else: gwlogging.sendLog(gwlogging.DEBUG, ("mqtt: %r" % e), "MQTT")
async def long_press(): gwlogging.sendLog(gwlogging.INFO, "Long Press Detected", MOD) fast_pulse(5) await asyncio.sleep_ms(2000) import os try: os.remove('wifi.json') except: #catches if file doesn't exist reset() await asyncio.sleep_ms(2000) reset()
def Log(self, msg, msgType) : if self._onLogging : try : self._onLogging(self, str(msg), msgType) return except Exception as ex : msgType = MicroWebSrv2.ERROR msg = 'Exception raised from "OnLoggin" handler: %s' % ex t = MicroWebSrv2.MSG_TYPE_STR.get(msgType, None) if t and t == 'ERROR': gwlogging.sendLog(gwlogging.ERR, msg, 'MWS2' ) elif t: print('MWS2-%s> %s' % (t, msg))
def start(config): global active active = 0 gwlogging.sendLog(gwlogging.INFO, "Starting MQTT", MOD) epoch = 0 global TOPIC TOPIC = config['topic1'] + "/" + MAC + "/" global TOPICble TOPICble = TOPIC + config['topic2'] + "/" if int(config['epoch']) == 1: epoch = 1 mqttH(config) _thread.start_new_thread(heartbeat, (epoch,)) #Start HeartBeat loop mqttB(config) active = 1 uart(epoch, config['dble'])
def mqttB(config):#HeartBeat client state = DISCONNECTED global clientB if config['username'] != "" and config['password'] != "": clientB = MQTTClient( MAC, config['host'] ,user=config['username'], password=config['password'], port=int(config['port']), keepalive=30, ssl=False) else: clientB = MQTTClient( MAC, config['host'] ,port=int(config['port']), keepalive=30, ssl=False) while state != CONNECTED: try: state = CONNECTING clientB.connect() state = CONNECTED except: gwlogging.sendLog(gwlogging.INFO, "Could not establish MQTT-BLE connection", MOD) time.sleep(1) if state == CONNECTED: gwlogging.sendLog(gwlogging.INFO, "MQTT-BLE Connected", MOD)
def RequestTestPost(microWebSrv2, request): gc.collect() data = request.GetPostedURLEncodedForm() try: if wifimgr.write_wifi(data): print("WiFi Config Saved") if wifimgr.do_connect(data): print("WiFi Connected") except: request.Response.ReturnBadRequest() return content = head1 + wifi_selected + head2 + """ <div class="header"> <h1> WiFi Configured</h1> </div> """ + end request.Response.ReturnOk(content) if wifimgr.check_connection(): sleep(2) settime() gwlogging.sendLog(gwlogging.INFO, "Time Set", MOD)
def start(config): global active active = 0 gwlogging.sendLog(gwlogging.INFO, "Starting HTTP", MOD) global tags tags = [] epoch = False if int(config['epoch']) == 1: epoch = True _thread.start_new_thread(uart, (epoch, )) active = 1 global Messages Messages = 0 while True: slow_pulse(3) time.sleep(10) tempT = tags tags = [] flT = time_stamp(time.localtime()) m = {'ts': flT, 'mac': MAC, 'tags': tempT} msgJson = ujson.dumps(m) urequests.post(config['host'], data=msgJson) Messages = Messages + 1
def mqttH(config):#HeartBeat client state = DISCONNECTED global clientH if config['username'] != "" and config['password'] != "": clientH = MQTTClient( MAC + "H", config['host'] ,user=config['username'], password=config['password'], port=int(config['port']), keepalive=30, ssl=False) else: clientH = MQTTClient( MAC + "H", config['host'], port=int(config['port']), keepalive=30, ssl=False) clientH.set_callback(sub_cb) while state != CONNECTED: try: state = CONNECTING clientH.connect() state = CONNECTED except: gwlogging.sendLog(gwlogging.INFO, "Could not establish MQTT-H connection", MOD) time.sleep(1) if state == CONNECTED: gwlogging.sendLog(gwlogging.INFO, "MQTT-H Connected", MOD) subTopic = "%s/config/#" % MAC clientH.subscribe(subTopic) gwlogging.sendLog(gwlogging.INFO, "MQTT-H Subscribed to: %s" % subTopic, MOD)
def RequestTestPost(microWebSrv2, request): gc.collect() data = request.GetPostedURLEncodedForm() try: if data['resetM'] == '0': gwlogging.sendLog(gwlogging.DEBUG, "Removing Wifi Configuration", MOD) gwlogging.sendLog(gwlogging.DEBUG, "Gateway will restart", MOD) import os os.remove('wifi.json') sleep(2) reset() elif data['resetM'] == '1': gwlogging.sendLog(gwlogging.DEBUG, "Performing WiFi scan", MOD) global resetVal resetVal = 1 request.Response.ReturnRedirect('/wifi-rescan') elif data['resetM'] == '2': gwlogging.sendLog(gwlogging.DEBUG, "Resetting Cloud Configuration", MOD) global resetVal resetVal = 1 request.Response.ReturnRedirect('/cloud-reconfigure') elif data['resetM'] == '3': gwlogging.sendLog(gwlogging.DEBUG, "Resetting Syslog Server", MOD) global resetVal resetVal = 1 request.Response.ReturnRedirect('/syslog-reconfigure') elif data['resetM'] == '4': gwlogging.sendLog(gwlogging.DEBUG, "Performing Factory Reset", MOD) gwlogging.sendLog(gwlogging.DEBUG, "Gateway will restart", MOD) import os os.remove('wifi.json') os.remove('gw.json') os.remove('wifi.json') os.remove('cloud.json') sleep(2) reset() except: request.Response.ReturnBadRequest() return
if int(CLOUD_CONFIG['mode']) == 0: import gwhttp pyhtmlMod.SetGlobalVar('GwMode', 0) gwhttp.start(CLOUD_CONFIG) elif int(CLOUD_CONFIG['mode']) == 1: import gwmqtt pyhtmlMod.SetGlobalVar('GwMode', 1) gwmqtt.start(CLOUD_CONFIG) else: sleep(1) pass except: fast_pulse(1) sleep(1) except KeyboardInterrupt: gwlogging.sendLog(gwlogging.NOTICE, "Exited from KeyboardInterrupt", MOD) # End, mws2.Stop() else: # Main program loop until keyboard interrupt, try: while True: try: if int(CLOUD_CONFIG['mode']) == 0: import gwhttp gwhttp.start(CLOUD_CONFIG) elif int(CLOUD_CONFIG['mode']) == 1: import gwmqtt gwmqtt.start(CLOUD_CONFIG) else: sleep(1)