def index(): devices = SerialManager.getDevices() devices.sort() return render_template('gui.html', sg_v=read_sg_version(), ui_v=read_ui_version(), cc_v=VERSION)
def setAutoStart(d_id): try: controller = SerialManager.getController(d_id) if controller: req = request.get_json() controller.setAutoStart(req['state']) return Response(status=200, response=json.dumps(req)) except Exception as ex: logger.error(ex) return Response(status=500)
def consoleOutput(d_id): try: controller = SerialManager.getController(d_id) if controller: WebsocketConsole.setSource(controller.log_file) time.sleep(0.5) return Response(status=200) except Exception as ex: logger.error(ex) return Response(status=500)
def getPlot(d_id): try: controller = SerialManager.getController(d_id) if controller: data = controller.getPlotData() if data: return Response(status=200, response=json.dumps({'res': data})) return Response(status=404) except Exception as ex: logger.error(ex) return Response(status=500)
def getBoundaries(d_id): try: controller = SerialManager.getController(d_id) if controller: event = DeviceEvent() controller.findBoundaries( functools.partial(__class__.callbk, event)) event.wait(timeout=15) return Response(status=event.status, response=json.dumps(event.message)) except Exception as ex: logger.error(ex) return Response(status=500)
def device(d_id): try: controller = SerialManager.getController(d_id) if controller: if request.method == 'POST': sett = request.get_json() controller.setSettings(sett['rpkwh'], sett['tkwh'], sett['name']) if request.method == 'GET': sett = controller.getSettings() return Response(status=200, response=json.dumps(sett)) except Exception as ex: logger.error(ex) return Response(status=500)
def getHistogram(d_id): try: controller = SerialManager.getController(d_id) if controller: event = DeviceEvent() conf = request.get_json() controller.buildHistogram( functools.partial(__class__.callbk, event), conf['lb'], conf['rb'], conf['res']) event.wait(timeout=15) return Response(status=event.status, response=json.dumps(event.message)) except Exception as ex: logger.error(ex) return Response(status=500)
def pushReadings(): while True: for controller in SerialManager.getControllers(): try: Client.event( controller._extended_id, 'detection', json.dumps({ 'value': float(controller._kwh), 'unit': 'kWh', 'time': '{}Z'.format(datetime.datetime.utcnow().isoformat()) }), block=False ) time.sleep(0.1) except Exception as ex: logger.error(ex) time.sleep(20)
def conf(d_id): try: controller = SerialManager.getController(d_id) if controller: event = DeviceEvent() if request.method == 'POST': conf = request.get_json() controller.setConf( functools.partial(__class__.callbk, event), conf['mode'], conf['conf_a'], conf['conf_b'], conf['dt'], conf['ndt']) if request.method == 'GET': conf = controller.getConf() event.set() event.wait(timeout=15) return Response(status=event.status, response=json.dumps(conf)) except Exception as ex: logger.error(ex) return Response(status=500)
def devices(): devices = SerialManager.getDevices() devices.sort() return Response(status=200, response=json.dumps(devices))
for controller in SerialManager.getControllers(): try: Client.event( controller._extended_id, 'detection', json.dumps({ 'value': float(controller._kwh), 'unit': 'kWh', 'time': '{}Z'.format(datetime.datetime.utcnow().isoformat()) }), block=False ) time.sleep(0.1) except Exception as ex: logger.error(ex) time.sleep(20) readings_scraper = Thread(target=pushReadings, name="Scraper") loop = asyncio.get_event_loop() cw = asyncio.get_child_watcher() if __name__ == '__main__': connector_client = Client(device_manager=DevicePool) WebsocketConsole(loop) WebUI() SerialManager() readings_scraper.start() loop.run_forever()