def __connect(self, force = False, reload = False): if self.is_connected() and not force and not reload: return True self.disconnect() self.load(reload) self.__check_config() if self.__valid_config: if self.config_item(ENABLED, False): if self.__with_auth: self.__client = MQTTClient( DEVICE_NAME, self.config_item(HOST), self.config_item(PORT), self.config_item(USER), self.config_item(PASSWORD), 0, SSL) else: self.__client = MQTTClient( DEVICE_NAME, self.config_item(HOST), self.config_item(PORT), None, None, 0, SSL) else: log.info("MQTT disabled") return False else: log.error("Invalid mqtt config") return False self.__client.connect() self.__client.DEBUG = DEBUG self.__client.set_callback(self.__sub_callback) self.__client.subscribe(bytes(CMD_TOPIC, 'utf-8')) self.__connected = True log.debug("Mqtt broker connected") return True
async def op(self, params): log.debug("Run scheduled job with param: %s", params) x = await self.__opc.op_request(params, False) if x[CODE] == 200: log.info("Run scheduled job done: %s", x) else: log.warning("Run scheduled job failed: %s", x)
def check_auth(self): for conn_handle in self._connections: if self._connections[conn_handle] >= 0: self._connections[conn_handle] = self._connections[conn_handle] + 1 if self._connections[conn_handle] > AUTH_FAILED_COUNT: log.info("Connect is not authenticated: %d, disconnected.", conn_handle) self.disconnect(conn_handle)
async def monitor(self): log.debug("Setup wifi monitor") while hw.WIFI: try: await sleep(hw.WIFI_CHECK_INTVAL) if not self.check_connection(): log.info("Wifi is not ready, reconnecting...") await self.async_connect(True) except: #NOSONAR # pylint: disable=W0702 pass
def dump_json(obj, file): log.info("Write json to file as string: %s", file) try: with open(file, "w") as fp: fp.write(dumps(obj)) log.info("Write json, done") return True except BaseException as e: #NOSONAR log.error("Write json failed! %s", e) return False
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))
def __set_properties(self): if self.__wlan.isconnected(): log.info('network information: %r', self.__wlan.ifconfig()) (self.ip, _, self.gw, self.dns) = self.__wlan.ifconfig() self.is_ok = True self.__led.on() else: self.ip = None self.gw = None self.dns = None self.is_ok = False self.__wlan = None self.__led.off() return self.is_ok
def load_json(json): log.info("Load json: %s", json) try: with open(json) as fp: data = loads(fp.read()) if type(data) == str: if data == "[]": return [] return {} log.info("Read json, done") return data except BaseException as e: #NOSONAR log.error("Read json failed! %s", e) return None
def __connect_init(self): self.check_wifi_config() if self.__config is None: log.error("Wifi config is None") return log.info("Connect to wifi: %s", self.__config[SSID]) self.__wlan = WLAN(STA_IF) # 创建 station 接口 if self.__wlan.isconnected(): self.__wlan.disconnect() self.__wlan.active(True) # Activate the interface self.__wlan.scan() # Scan self.__led.off() self.__wlan.config(dhcp_hostname=self.__hostname) self.__wlan.connect(self.__config[SSID], self.__config[PASSWORD]) # 连接到指定ESSID网络 if TIMEOUT in self.__config: self.__timeout = self.__config[TIMEOUT]
def read(self): #NOSONAR try: while True: (stat, tag_type, raw_uid, uid) = self.get_stat() if stat == self._rdr.OK: if self._rdr.select_tag(raw_uid) == self._rdr.OK: key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] if self._rdr.auth(self._rdr.AUTHENT1A, 8, key, raw_uid) == self._rdr.OK: data = self._rdr.read(self._addr) self._rdr.stop_crypto1() if data is not None: return stat, tag_type, raw_uid, uid, data log.error("Failed to read data to card") else: log.error("Authentication error") else: log.error("Failed to select tag") except BaseException as e: log.info("Stop reading %r", e)
def get_stat(self): try: while True: uid = "" (stat, tag_type) = self._rdr.request(self._rdr.REQIDL) if stat == self._rdr.OK: (stat, raw_uid) = self._rdr.anticoll() if stat == self._rdr.OK: uid = ( "0x%02x%02x%02x%02x" % (raw_uid[0], raw_uid[1], raw_uid[2], raw_uid[3])) log.info("New card detected") log.info(" - tag type: 0x%02x", tag_type) log.info(" - uid : %s", uid) return stat, tag_type, raw_uid, uid except BaseException as e: log.info("Stop getting stat %r", e)
def write(self, data): log.info("Writing...") try: while True: (stat, _, raw_uid, _) = self.get_stat() if stat == self._rdr.OK: if self._rdr.select_tag(raw_uid) == self._rdr.OK: key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] if self._rdr.auth(self._rdr.AUTHENT1A, 8, key, raw_uid) == self._rdr.OK: stat = self._rdr.write(self._addr, data) self._rdr.stop_crypto1() if stat == self._rdr.OK: log.info("Data written to card") return True log.error("Failed to write data to card") else: log.error("Authentication error") else: log.error("Failed to select tag") except BaseException as e: log.info("Stop writing %r", e) return False
def _advertise(self, interval_us=500000): self._ble.gap_advertise(interval_us, adv_data=self._payload) log.info("Advertised", )