def save(self): try: app_eui = hexlify( db0.hget(self.category + self.eui, ConstDB0.app_eui)).decode() except TypeError as e: Logger.warning(action='MsgDn', type=IDType.app, id=self.category + self.eui, msg=str(e)) app_eui = self.eui.split(':')[0] key = ConstDB0.msg_down + self.category + \ self.eui + ':' + str(round(self.ts)) pipe = db0.pipeline() pipe.hmset(key, self.__obj_to_dict()) pipe.expire(key, ConstMsg.EXPIRE_MSG) pipe.publish(Channel0.msg_alarm + app_eui, key) pipe.execute() self.__count_statistics_down() key = ConstDB2.dn_m + self.category + \ self.eui + ':' + str(round(self.ts)) pipe = db2.pipeline() info = {ConstMsg.fcnt: self.fcnt} if hasattr(self, ConstMsg.gateways): if isinstance(self.gateways, bytes): info[ConstMsg.gateways] = hexlify(self.gateways).decode() elif isinstance(self.gateways, list): info[ConstMsg.gateways] = ';'.join( map(bytes_to_hexstr, self.gateways)) pipe.hmset(key, info) pipe.expire(key, ConstMsg.EXPIRE_STATIS) pipe.publish(Channel0.dn_alarm + app_eui, key) pipe.execute()
def save(self): """ :param msg: MsgUp :return: None """ dev_eui = hexlify(self.dev_eui).decode() app_eui = hexlify(db0.hget(ConstDB0.dev + dev_eui, FieldDevice.app_eui)).decode() key = ConstDB0.msg_up + app_eui + ':' + dev_eui + ':' + str(self.ts) pipe = db0.pipeline() pipe.hmset(key, self.__obj_to_dict()) pipe.sadd(ConstDB0.mset + dev_eui, self.ts) pipe.expire(key, ConstMsg.EXPIRE_MSG) pipe.publish(Channel0.msg_alarm + app_eui, key) pipe.execute() self._count_statistics_up() pipe = db2.pipeline() if self.restart: cur_ts = self.ts pipe.rpush(ConstDB2.up_l_l + dev_eui, str(self.ts)) else: cur_ts = db2.lindex(ConstDB2.up_l_l + dev_eui, -1) if cur_ts: cur_ts = int(cur_ts) else: cur_ts = self.ts pipe.rpush(ConstDB2.up_l_l + dev_eui, str(self.ts)) pipe.rpush(ConstDB2.up_l + dev_eui + ':%s' % cur_ts, self.ts) key = ConstDB2.up_m + dev_eui + ':%s' % self.ts pipe.hmset(key, self.obj_to_dict()) pipe.expire(key, ConstMsg.EXPIRE_STATIS) pipe.publish(Channel0.up_alarm + app_eui, key) pipe.execute()
def get(self): ack = db0.hget(ConstDB0.dev_ack + hexlify(self.dev_eui).decode(), self.ack_name) if ack is not None: return int(ack) else: return 0
def get_dev_class(dev_eui): """ :param dev_eui: bytes :return: """ dev_class = db0.hget(ConstDB0.dev + hexlify(dev_eui).decode(), FieldDevice.dev_class) if dev_class is not None: dev_class = ClassType(dev_class.decode()) return dev_class
def push_into_que(self, cid, payload=None): dev_eui = hexlify(self.device.dev_eui).decode() db0.set(ConstDB0.mac_cmd + dev_eui + ':' + hexlify(cid).decode(), self.__return_data(cid, payload)) que_key = ConstDB0.mac_cmd_que + dev_eui db0.lrem(que_key, count=0, value=cid) db0.rpush(que_key, cid) dev_class = ClassType( db0.hget(ConstDB0.dev + dev_eui, FieldDevice.dev_class).decode()) if dev_class == ClassType.c or dev_class == ClassType.b: db0.publish(Channel0.que_down_alarm_c, ConstDB0.dev + dev_eui)
def pop_restart(self): restart = db0.hget(ConstDB0.gateway + hexlify(self.mac_addr).decode(), 'restart') if restart == b'1': db0.hdel(ConstDB0.gateway + hexlify(self.mac_addr).decode(), 'restart') Logger.info(action=Action.object, type=IDType.gateway, id=hexlify(self.mac_addr).decode(), msg='Restart') return True else: return False
def get(dev_eui, gateway_mac_addr, name=None): key = ConstDB0.trans_params + hexlify( dev_eui).decode() + ':' + hexlify(gateway_mac_addr).decode() if name is None: trans_params = db0.hgetall(key) return { key.decode(): value.decode() for key, value in trans_params.items() } else: trans_param = db0.hget( ConstDB0.trans_params + hexlify(dev_eui).decode() + ':' + hexlify(gateway_mac_addr).decode(), name) return trans_param.decode() if trans_param else trans_param
def p_to_c(self, *args): pipe = db0.pipeline() for arg in args: value = db0.hget(self.key, 'p_' + arg) if value is not None: pipe.hset(self.key, arg, value) pipe.hdel(self.key, 'p_' + arg) else: Logger.error(action=Action.mac_cmd_send, type=IDType.dev_info, id=hexlify(self.dev_eui).decode(), msg='No P value can be set to C: %s' % arg) return pipe.execute()
def __init__(self, request_msg, trans_params, gateway): """ :param request_msg:str :param trans_params:dict :param gateway_mac_addr: :return: """ Greenlet.__init__(self) app_eui = int.to_bytes(int.from_bytes(request_msg[1:9], byteorder='little'), byteorder='big', length=8) dev_eui = int.to_bytes(int.from_bytes(request_msg[9:17], byteorder='little'), byteorder='big', length=8) dev_nonce = int.to_bytes(int.from_bytes(request_msg[17:19], byteorder='little'), byteorder='big', length=2) mic = request_msg[19:23] hex_dev_eui = hexlify(dev_eui).decode() hex_app_eui = hexlify(app_eui).decode() Logger.info(action=Action.otaa, type=IDType.device, id=dev_eui, msg="JOIN REQ: APP:%s, dev_nonce:%s" % (hex_app_eui, dev_nonce)) real_app_eui = db0.hget(ConstDB0.dev + hex_dev_eui, 'app_eui') if real_app_eui is not None and real_app_eui != app_eui: raise Exception('Device %s belong to other app %s, not app %s' % (dev_eui, real_app_eui, app_eui)) else: app = Application.objects.get(app_eui) if app is None: raise KeyError('APP:%s does not exist' % hex_app_eui) elif gateway.public is not True and app.user_id != gateway.user_id: raise AccessDeny( hexlify(gateway.mac_addr).decode(), ConstDB0.app + hex_app_eui) self.app = app self.dev_eui = dev_eui self.request_msg = request_msg self.trans_params = trans_params self.gateway = gateway self.dev_nonce = dev_nonce self.mic = mic
def get_tx_params(self, name=None): FREQ_PLAN = frequency_plan[self.app.freq_plan] if name is None: info = db0.hmget( ConstDB0.dev_info + hexlify(self.dev_eui).decode(), self.__tx_params) tx_params = {} tx_params[TXParams.RX1DRoffset] = int( info[0]) if info and info[0] else FREQ_PLAN.RX1DRoffset tx_params[TXParams.RX2DataRate] = int( info[1]) if info and info[1] else FREQ_PLAN.RX2DataRate tx_params[TXParams.RX2Frequency] = float( info[2]) if info and info[2] else FREQ_PLAN.RX2Frequency tx_params[TXParams.RxDelay] = int( info[3]) if info and info[3] else FREQ_PLAN.RxDelay return tx_params elif name == TXParams.RxDelay: rxdelay = db0.hget( ConstDB0.dev_info + hexlify(self.dev_eui).decode(), TXParams.RxDelay) rxdelay = int(rxdelay) if rxdelay else FREQ_PLAN.RxDelay return rxdelay