Example #1
0
 def publish(self):
     data = json.dumps(self.__obj_to_dict())
     # db1.publish(Channel1.join_req_alarm, data)
     db0.publish(Channel1.join_accept_alarm +
                 hexlify(self.app_eui).decode(), data)
     Logger.info(action=Action.otaa, msg='Publish Join Accept alram %s, %s' % (
         Channel1.join_req_alarm, data))
Example #2
0
 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)
Example #3
0
 def _run(self):
     ts = round(time.time())
     data = self.data
     device = Device.objects.get(self.dev_eui)
     fcnt = unpack('<H', data[6:8])[0]
     device.fcnt_up = fcnt
     dev_gateway = DevGateway(device.dev_eui, self.gateway.mac_addr, int(self.trans_params['rssi']), float(self.trans_params['lsnr']))
     if start_device_block(self.dev_eui):
         dev_gateway.set()
         Logger.info(action=Action.uplink, type=IDType.device, id=device.dev_eui, msg='Fcnt: %s; New Message from gateway: %s' % (device.fcnt_up, hexlify(self.gateway.mac_addr).decode()))
         trans_params = TransParams(device.dev_eui, gateway_mac_addr=self.gateway.mac_addr, trans_params=self.trans_params, ts=ts)
         trans_params.save()
     else:
         Logger.info(action=Action.uplink, type=IDType.device, id=device.dev_eui, msg='Fcnt: %s; Send by other gateway: %s' % (device.fcnt_up, hexlify(self.gateway.mac_addr).decode()))
         if dev_gateway.add():
             trans_params = TransParams(device.dev_eui, gateway_mac_addr=self.gateway.mac_addr, trans_params=self.trans_params, ts=ts)
             trans_params.save()
         return
     if fcnt == device.fcnt_up:
         retransmission = 1
     else:
         retransmission = 0
         device.update()
     fctrl = data[5]
     fport = 0
     app_data = b''
     if len(data) > 8:
         fport = data[8]
         app_data = data[8: -1]
     sleep(0.1)
     gateway_mac_addr = Gateway.objects.best_mac_addr(device.dev_eui)
     trans_params = TransParams.objects.get(device.dev_eui, gateway_mac_addr)
     msg = MsgUp(device.dev_eui, ts=ts, fcnt=device.fcnt_up, port=fport, data=app_data,
                 mtype=self.mtype, retrans=retransmission, trans_params=trans_params, cipher=False, )
     msg.save()
     Logger.info(action=Action.uplink, type=IDType.device, id=device.dev_eui,
                 msg='fcnt:%s, mtype: %s; retransmission: %s; fport: %s; app_data: %s'
                     % (device.fcnt_up, MType(self.mtype).name, retransmission, fport, app_data))
     db0.publish(Channel0.rx1_alarm, device.dev_eui)
Example #4
0
 def run(self):
     data = self.data
     device = Device.objects.get(self.dev_eui)
     ts = round(time.time())
     restart = False
     try:
         addr_int = unpack(">I", device.addr)[0]
         mac_fhdr_fcnt = data[6:8]
         mic = data[len(data)-4:]
         fcnt_d = device.fcnt_up
         fcnt_m = unpack('<H', mac_fhdr_fcnt)[0]
         if not mic_ver(data[0:len(data)-4], device.nwkskey, addr_int, Const.DIR_UP, fcnt_m, mic):
             low_16 = fcnt_d & 0xffff
             high_16 = fcnt_d >> 16
             low_16 = fcnt_m
             if low_16 < fcnt_m:
                 high_16 += 1
             fcnt_m = (high_16 << 16) + low_16
             if not mic_ver(data, device.nwkskey, addr_int, Const.DIR_UP, fcnt_m, mic):
                 raise MICError()
         sub = fcnt_m - fcnt_d
         if sub > 0:
             if sub > Const.MAX_FCNT_GAP:
                 if device.check_fcnt:
                     raise FcntError(sub)
         elif sub < 0:
             if fcnt_m > Const.MAX_FCNT_GAP:
                 if device.check_fcnt:
                     raise FcntError(sub)
             if fcnt_m < 10:
                 device.fcnt_down = 0
                 restart = True
         # new_fcnt_tuple = up_cnt(mac_fhdr_fcnt, device.fcnt_up, device.check_fcnt)
         # fcnt_new = mic_verify(data[0:len(data)-4], device.nwkskey, addr_int, new_fcnt_tuple, mic)
     except (FcntError, MICError) as error:
         Logger.error(action=Action.uplink, type=IDType.device, id=device.dev_eui, msg='ERROR: MIC or FCNT: %s' % error)
         return
     except Exception as error:
         print(error)
         Logger.error(action=Action.uplink, type=IDType.device, id=device.dev_eui, msg='Unknown ERROR: %s' % error)
         return
     dev_gateway = DevGateway(device.dev_eui, self.gateway.mac_addr, int(self.trans_params['rssi']), float(self.trans_params['lsnr']))
     if start_device_block(self.dev_eui):
         dev_gateway.set()
         Logger.info(action=Action.uplink, type=IDType.device, id=device.dev_eui, msg='Fcnt: %s; New Message from gateway: %s' % (fcnt_m, hexlify(self.gateway.mac_addr).decode()))
         trans_params = TransParams(device.dev_eui, gateway_mac_addr=self.gateway.mac_addr, trans_params=self.trans_params, ts=ts)
         trans_params.save()
     else:
         Logger.info(action=Action.uplink, type=IDType.device, id=device.dev_eui, msg='Fcnt: %s; Send by other gateway: %s' % (fcnt_m, hexlify(self.gateway.mac_addr).decode()))
         if dev_gateway.add():
             trans_params = TransParams(device.dev_eui, gateway_mac_addr=self.gateway.mac_addr, trans_params=self.trans_params, ts=ts)
             trans_params.save()
         return
     if fcnt_m == fcnt_d:
         retransmission = 1
     else:
         retransmission = 0
         device.fcnt_up = fcnt_m
         device.update()
     fctrl = data[5]
     foptslen = fctrl & 0b1111
     class_b = fctrl >> 4 & 0b1
     ack = fctrl >> 5 & 0b1
     adrackreq = fctrl >> 6 & 0b1
     adr = fctrl >> 7
     mac_cmd = data[8:8+foptslen]
     encrypt = False
     fport = 0
     app_data = b''
     if (8 + foptslen) < len(data)-4:
         fport = data[8 + foptslen]
         frampayload = data[9 + foptslen: len(data)-4]
         assert -1 < fport < 256, "AssertError: fport error"
         if fport == 0:
             mac_cmd = LoRaCrypto.payload_decrypt(frampayload, device.nwkskey, addr_int, Const.DIR_UP, fcnt_m)
         else:
             if device.appskey == b'':
                 encrypt = True
                 app_data = frampayload
             elif len(device.appskey) == 16:
                 app_data = LoRaCrypto.payload_decrypt(frampayload, device.appskey, addr_int, Const.DIR_UP, fcnt_m)
     if class_b == 1:
         if device.dev_class == ClassType.a:
             device.dev_class = ClassType.b
     else:
         if device.dev_class == ClassType.b:
             device.dev_class = ClassType.a
     device.update()
     if ack == 1:    # if ack == 1, confirm the down link which need to be confirm
         Resend(device.dev_eui).del_retrans_down()
     if adrackreq == 1:
         DeviceACK(device.dev_eui, ACKName.adr_ack_req).set(1)
     if self.mtype == MType.CONFIRMED_DATA_UP:
         DeviceACK(device.dev_eui, ACKName.ack_request).set(1)
     # analyze mac cmd
     analyze_mac_cmd_ans(device, mac_cmd)
     # ADR
     if adr and device.adr:
         adr_thread = ADR(device)
         adr_thread.start()
     # sleep(0.1)
     gateway_mac_addr = Gateway.objects.best_mac_addr(device.dev_eui)
     trans_params = TransParams.objects.get(device.dev_eui, gateway_mac_addr)
     msg = MsgUp(device.dev_eui, ts=ts, fcnt=device.fcnt_up, port=fport, data=app_data, cipher=encrypt,
                 mtype=self.mtype, retrans=retransmission, trans_params=trans_params, restart=restart)
     msg.save()
     Logger.info(action=Action.uplink, type=IDType.device, id=device.dev_eui,
                 msg='fcnt:%s, mtype: %s; retransmission: %s; ack: %s; adrackreq: %s; mac_cmd: %s; class_b: %s; fport: %s; app_data: %s; encrypt: %s '
                 % (device.fcnt_up, MType(self.mtype).name, retransmission, ack, adrackreq, mac_cmd, class_b, fport, app_data, encrypt))
     db0.publish(Channel0.rx1_alarm, device.dev_eui)
     t2 = time.time()
     g_time = iso_to_utc_ts(self.trans_params['time'])
     self.args.insert(0, g_time)
     self.args.append(t2)
     timing.info(msg='PUSH_DATA: DEV:%s, FCNT:%s, DELAY_TIME:%s, TIME:%s' % (hexlify(device.dev_eui).decode(), fcnt_m, self.args[1] - self.args[0], self.args))
Example #5
0
 def request2niot_platform(self):
     """
     The function is used to publish message to program which will send request to niot platform about the gateway.
     :return:
     """
     db0.publish(ConstDB0.niot_request, hexlify(self.mac_addr))