async def __set(self, c): self.__config = c if self.__check_config(): if self.__save(): return await self.__reload_config() else: return result(500, "Save %s failed" % self.__fn) else: return result(400, "Invalid config, not saved")
async def index(request, response): try: t, p, m, d, _ = await parse_request(request) r = await opc.op(t, p, m, d) except BaseException as e: #NOSONAR log.debug("Request error: %r", e) r = result(500, str(e)) code = r[CODE] if 200 <= code <= 299: v = None if VALUE not in r else r[VALUE] if v is None: s = "" else: s = dumps(enc(v)) else: if VALUE in r: r.pop(VALUE) s = dumps(enc(r)) response.code = r[CODE] # Start HTTP response with content-type text/html response.add_header('Content-Type', 'application/json') response.add_header('Content-Length', str(len(s))) await response._send_headers() #pylint: disable=protected-access await response.send(s)
async def __op(self, p): asyncio.sleep(0) try: mop = p[OP].lower() if mop == 'on': self.__relay.on() elif mop == 'off': self.__relay.off() elif mop == 'flip': self.flip() elif mop == 'blink': onms = p[ON_MS] offms = p[OFF_MS] if type(onms) == int and type(offms) == int: asyncio.create_task(self.async_blink(int(onms), int(offms))) else: return INVALID_ARGS elif mop == 'stop-blink': self.stop_async_blink() else: return INVALID_ARGS except BaseException as e: log.debug("Op error: %r", e) return INVALID_ARGS return result(200, None, self.get_state())
async def _call(self, data): try: r = await self._opc.op_request(data, False) self.send(r) except BaseException as e: #NOSONAR msg = "Call failed %r" % e log.debug(msg) self.send(result(500, msg))
async def __cleanup_onetime(self, _): log.debug("Clean up one time job") if self.__onetime is not None and len(self.__onetime) > 0: for t in self.__onetime: cancel_task(t) self.__onetime.clear() self.__onetime = [] await asyncio.sleep(0) return result()
def __sub_callback(self, topic, pay): s = pay.decode("utf-8") log.info('Received %s: %s' , topic.decode("utf-8"), s) try: json = loads(s) if ENCRYPTED_INPUT: # 处理加密 st = Sec() json = st.dec_payload(json) create_task(self.__opc.op_request(json)) except BaseException as e: m = "Process message failed: %r" % e log.error(m) self.publish_op_log(None, None, result(500, m))
async def __add_one_time_job(self, c): log.debug("Add one time job: %s", c) self.__create_scheduler(c, True) return result()
async def __reconnect(self, _): delayed_task(5000, self.connect, (True)) return result(200, None, RECONNECT_MSG)
async def __reload_config(self): # NOSONAR delayed_task(5000, self.connect, (True, True)) return result(200, None, RECONNECT_MSG)
async def __get_info(self, _): v = self.get_info() await sleep(0) return result(200, None, v)
async def __get(self, _): await asyncio.sleep(0) v = self.get_state() return result(200, None, v)
async def __get(self, _): v = self.load(True) if v is None: return result(500, "Load %s failed" % self.__fn) return result(200, None, v)
async def __reload_config(self): await sleep(0) return result()
async def __reconnect(self, _): delayed_task(5000, self.async_connect, (True), True) return result(200, None, RECONNECT_WIFI)
from sec import Sec from op import request, result import dev st = Sec("", "12345678") req = request('sys/info', 'get', {'a': 'a', 'b': 1, 'c': True}) req['t'] = 'get-tm1' req2 = request('sys/echo', 'set', 'Hi2') ret = result(200, 'test', v={'alert': 1, 'n': 'pir'}) req2['t'] = 'get-tm1' print("Test request") print("1: %r" % req) p = st.enc_paylaod(req) print("2: %r" % p) d = st.dec_payload(p) print("3: %r" % d) print("Test request2") print("1: %r" % req2) p = st.enc_paylaod(req2) print("2: %r" % p) d = st.dec_payload(p) print("3: %r" % d) print("Test result") print("1: %r" % ret) p = st.enc_paylaod(ret) print("2: %r" % p) d = st.dec_payload(p)
async def __reload_config(self): # NOSONAR try: self.setup() return result() except: #NOSONAR #pylint: disable=bare-except return result(500, "Reload config failed")
async def __cleanup_cron(self, _): await self.cleanup_cron(True) return result()
async def __get(self, _): await sleep(0) return result(200, None, self.__sensors)