Ejemplo n.º 1
0
 def count_in_hour(self):
     """
     二十四小时前计数,
     :return: 每小时对应的数量
     """
     res = {}
     cur_time = datetime.now()
     cur_hour = cur_time.hour
     today_key = self.key + cur_time.strftime('%Y-%m-%d')
     yesterday_key = self.key + (cur_time -
                                 timedelta(days=1)).strftime('%Y-%m-%d')
     for x in range(0, 24):
         hour = cur_hour - 23 + x
         if hour < 0:
             data = db0.hgetall(yesterday_key + ':' + str(hour + 24))
         else:
             data = db0.hgetall(today_key + ':' + str(hour))
         if data:
             for freq, value in data.items():
                 freq = float(freq)
                 if freq not in res.keys():
                     res[freq] = [
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0
                     ]
                 res[freq][x] = int(value)
     new_res = []
     for freq, num_list in res.items():
         new_res.append({'name': freq, 'data': num_list})
     return new_res
Ejemplo n.º 2
0
 def count_in_daily(self):
     """
     一天为单位计数
     :return:返回30天前的上行数据
     """
     res = {}
     cur_time = datetime.now()
     for x in range(0, 30):
         date = cur_time - timedelta(29 - x)
         date_str = date.strftime("%Y-%m-%d")
         keys = db0.keys(self.key + date_str + ':*')
         for key in keys:
             data = db0.hgetall(key)
             for freq, value in data.items():
                 freq = float(freq)
                 if freq not in res.keys():
                     res[freq] = [
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                     ]
                 res[freq][x] += int(value)
     new_res = []
     for freq, num_list in res.items():
         new_res.append({'name': freq, 'data': num_list})
     return new_res
Ejemplo n.º 3
0
 def query(self):
     if self.order_by in ('fcnt', 'port', 'freq', 'rssi', 'lsnr'):
         list = db0.sort(ConstDB.mset + self.dev_eui, by=ConstDB.msg_up + self.app_eui + ':' + self.dev_eui + ':*->' + self.order_by, desc=self.desc)
     elif self.order_by in ('datr', 'data'):
         list = db0.sort(ConstDB.mset + self.dev_eui, by=ConstDB.msg_up + self.app_eui + ':' + self.dev_eui + ':*->' + self.order_by, alpha=True, desc=self.desc)
     else:
         list = db0.sort(ConstDB.mset + self.dev_eui, desc=self.desc)
     msg_list = []
     index = self.start
     while len(msg_list) < self.length:
         try:
             ts = int(list[index])
         except IndexError:
             break
         info = db0.hgetall(ConstDB.msg_up + self.app_eui + ':' + self.dev_eui + ':' + str(ts))
         if len(info) > 0:
             msg = {'ts': ts}
             for key, value in info.items():
                 key = key.decode()
                 if key == 'data':
                     msg[key] = hexlify(value).decode()
                 elif key in ['fcnt', 'port']:
                     msg[key] = int(value)
                 elif key in ['freq', 'rssi', 'lsnr']:
                     msg[key] = float(value)
                 else:
                     msg[key] = value.decode()
             msg_list.append(msg)
             index += 1
         else:
             db0.srem(ConstDB.mset + self.dev_eui, ts)
             del list[index]
     return {"draw": self.draw, "recordsTotal": len(list), "recordsFiltered": len(list), "data": msg_list}
Ejemplo n.º 4
0
 def get(id):
     """
     :param app_eui: 8 bytes
     :param id: bytes
     :return:
     """
     info = db0.hgetall(ConstDB.group + hexlify(id).decode())
     if len(info) > 0:
         app_eui = info[b'app_eui']
         name = info[b'name'].decode()
         addr = info[b'addr']
         nwkskey = info[b'nwkskey']
         appskey = info.get(b'appskey')
         fcnt = int(info[b'fcnt'])
         periodicity = int(info.get(b'periodicity', 0))
         datr = int(info.get(b'datr', 0))
         return Group(app_eui,
                      name,
                      addr,
                      nwkskey,
                      appskey,
                      fcnt,
                      periodicity=periodicity,
                      datr=datr,
                      id=id,
                      new_flag=False)
Ejemplo n.º 5
0
 def check(self):
     list = db0.sort(ConstDB.mset + self.dev_eui)
     i = 0
     for ts in list:
         info = db0.hgetall(ConstDB.msg_up + self.app_eui + ':' + self.dev_eui + ':' + ts.decode())
         if len(info) == 0:
             db0.srem(ConstDB.mset + self.dev_eui, ts)
             i += 1
         else:
             print('%s messages deleted' % i)
             return
Ejemplo n.º 6
0
 def count_in_daily(key):
     res = []
     cur_time = datetime.now()
     for x in range(0, 30):
         date = cur_time - timedelta(29 - x)
         name = key + date.strftime("%Y-%m-%d")
         sum = 0
         values = db0.hgetall(name).values()
         for v in values:
             sum += int(v)
         res.append(sum)
     return res
Ejemplo n.º 7
0
 def get(dev_eui, gateway_id):
     """
     :param dev_eui: bytes
     :param gateway_id: bytes
     :return: TransParams
     """
     info = db0.hgetall(ConstDB.trans_params +
                        hexlify(dev_eui).decode() + ':' +
                        hexlify(gateway_id).decode())
     info = {
         key.decode(): value.decode()
         for key, value in info.items()
     }
     return TransStatus(dev_eui, gateway_id, info)
Ejemplo n.º 8
0
 def init_on_load(self):
     self.mac_addr = eui_64_to_48(self.id)
     info = db0.hgetall(ConstDB.gateway + hexlify(self.id).decode())
     self.freq_plan = FrequencyPlan(info[b'freq_plan'].decode())
     self.public = bool(int(info[b'public']))
     self.disable = bool(int(info[b'disable']))
     self.platform = Platform[info[b'platform'].decode()]
     self.model = Model[info[b'model'].decode()]
     location = info.get(b'location')
     if location is not None:
         self.location = Location.objects.str_to_obj(location.decode())
     else:
         self.location = Location(0.0, 0.0, 0)
     time = db3.get(ConstDB3.T_GATEWAY + hexlify(self.id).decode())
     if time is not None:
         self.time = int(time)
Ejemplo n.º 9
0
 def get(msg_key):
     """
     :param msg_key: str
     :return:
     """
     info = db0.hgetall(msg_key)
     if len(info) > 0:
         msg_key = msg_key.split(':')
         type = msg_key[1]
         if type == 'DEV':
             eui = msg_key[2]
             ts = msg_key[3]
         elif type == 'GROUP':
             eui = msg_key[3]
             ts = msg_key[4]
         fcnt = int(info[b'fcnt'])
         data = info[b'data']
         return MsgDn(type=type, eui=eui, ts=ts, fcnt=fcnt, data=data)
Ejemplo n.º 10
0
 def get(msg_key):
     """
     :param msg_key: str
     :return:
     """
     info = db0.hgetall(msg_key)
     try:
         msg_key = msg_key.split(':')
         dev_eui = msg_key[2]
         ts = msg_key[3]
         fcnt = int(info[b'fcnt'])
         port = int(info[b'port'])
         freq = info[b'freq'].decode()
         datr = info.get(b'dr')
         if not datr:
             datr = info.get(b'datr')
         datr = datr.decode()
         rssi = info[b'rssi'].decode()
         snr = info.get(b'snr')
         if not snr:
             snr = info.get(b'lsnr')
         snr = snr.decode()
         cipher = info.get(b'cipher')
         if cipher is not None:
             cipher = bool(int(cipher))
         data = info.get(b'encdata')
         if data is not None:
             cipher = True
         else:
             data = info.get(b'data')
             cipher = cipher if cipher is not None else False
         return MsgUp(dev_eui=dev_eui,
                      ts=ts,
                      fcnt=fcnt,
                      port=port,
                      freq=freq,
                      datr=datr,
                      rssi=rssi,
                      snr=snr,
                      cipher=cipher,
                      data=data)
     except KeyError:
         pass
Ejemplo n.º 11
0
 def init_on_load(self):
     if self.active_at:
         info = db0.hgetall(ConstDB.dev + hexlify(self.dev_eui).decode())
         try:
             self.addr = info[b'addr']
             self.nwkskey = info[b'nwkskey']
             self.appskey = info.get(b'appskey', b'')
             self.fcnt_up = int(info[b'fcnt_up'])
             self.fcnt_down = int(info[b'fcnt_down'])
             self.dev_class = ClassType(info[b'dev_class'].decode())
             self.adr = bool(int(info[b'adr']))
             self.check_fcnt = bool(int(info[b'check_fcnt']))
             self.que_down = QueDownDev(self.dev_eui)
             self.trans_status = TransStatus.objects.best(self.dev_eui)
             self.__get_more_info()
             if self.dev_class == ClassType.b:
                 self.get_b_info()
         except (KeyError, TypeError) as error:
             Logger.error(action=Action.get,
                          resource=Resource.device,
                          id=self.dev_eui,
                          msg=str(error))
Ejemplo n.º 12
0
 def get_pull_info(self):
     key = ConstDB.gateway_pull + hexlify(self.id).decode()
     info = db0.hgetall(key)
     if info:
         self.ip_addr = info[b'ip_addr'].decode()
         self.prot_ver = int(info[b'prot_ver'])
Ejemplo n.º 13
0
 def get_b_info(self):
     info = db0.hgetall(ConstDB.b + hexlify(self.dev_eui).decode())
     self.periodicity = info['periodicity'] if info else 0
     self.datr = info['datr'] if info else 0