def get(self, skey, dtfrom, dtto): hours = BinGPS.gethours(skey, int(dtfrom), int(dtto)) # data = [d for d in BinGPS.gethours(skey, int(dtfrom), int(dtto))] self.writeasjson({ "skey": skey, "from": dtfrom, "to": dtto, "hours": hours })
def get(self, skey, dtfrom, dtto): # System(skey).change_desc(self.payload["desc"], domain=self.domain) self.set_header('Content-Type', 'application/octet-stream') # def join(blist): # j = "" # for b in blist: # j += b # return j data = BinGPS.getraw(skey, int(dtfrom), int(dtto)) # logging.info("data = %s" % repr(data)) # result = ({"hour": d["hour"], "data": b64encode(join(d["data"]))} for d in data) # for d in data: # self.write(join(d["data"])) self.write(data)
def get_old(self, skey, dtfrom, dtto): # System(skey).change_desc(self.payload["desc"], domain=self.domain) def join(blist): j = "" for b in blist: j += b return j data = BinGPS.getraw(skey, int(dtfrom), int(dtto)) logging.info("data = %s" % repr(data)) result = ({"hour": d["hour"], "data": b64encode(join(d["data"]))} for d in data) self.writeasjson({ "skey": skey, "from": dtfrom, "to": dtto, "data": [r for r in result] })
def onpost(self): dataid = int(self.get_argument('dataid', '0'), 16) pdata = self.request.body _log = '\n\n====\nLOGS:' _log += "\n pdata len=%s" % len(pdata) _log += "\n data id=%s" % dataid #_log += "\n os.environ: %s" % repr(os.environ) _log += "\n headers: %s" % repr(self.request.headers) _log += "\n arguments: %s" % self.request.arguments #_log += "\n body: %s" % len(self.request.body) _log += "\n pbody: %s" % len(pdata) _log += "\n Remote IP: %s" % str(self.request.remote_ip) #_log += "Request info: %s\n" % str(self.request.content_type) #_log += "\n self=%s" % dir(self) _log += "\n IMEI=%s" % self.imei _log += "\n skey=%s" % self.skey #skey = DBSystem.key_by_imei(imei) if USE_BACKUP: _log += '\n Saving to backup (TBD)' _log += '\n Data (HEX):' for data in pdata: _log += ' %02X' % ord(data) pass if len(pdata) < 3: logging.error('Data packet is too small or miss.') self.write("BINGPS: CRCERROR\r\n") return crc = ord(pdata[-1]) * 256 + ord(pdata[-2]) pdata = pdata[:-2] crc2 = 0 for byte in pdata: crc2 = CRC16(crc2, ord(byte)) if crc != crc2: _log += '\n Warning! Calculated CRC: 0x%04X but system say CRC: 0x%04X. (Now error ignored.)' % (crc2, crc) _log += '\n Original data (HEX):' odata = self.request.body for data in odata: _log += ' %02X' % ord(data) logging.info(_log) _log = '\n Encoded data (HEX):' for data in pdata: _log += ' %02X' % ord(data) logging.info(_log) self.write('BINGPS: CRCERROR\r\n') return else: _log += '\n CRC OK %04X' % crc logging.info(_log) plen = len(pdata) #packer = Packer() packer = BinGPS.packer(self.skey) offset = 0 lastdt = 0 lastpoint = None while offset < plen: if pdata[offset] != '\xFF': offset += 1 continue if pdata[offset + 1] == '\xF2': point = UpdatePoint(pdata, offset) offset += 32 if point is not None: dt = unpack_from("<I", point, 4)[0] # TODO! Не самое элегантное решение logging.info("packet F2 datetime = %d => %s" % (dt, datetime.fromtimestamp(dt).strftime('%Y-%m-%d %H:%M:%S'))) packer.add_point_to_packer(point, dt // 3600) lastpoint = point lastdt = dt # logging.info('=== Point=%s' % repr(point)) elif pdata[offset + 1] == '\xF4': point = pdata[offset:offset+32] dt = unpack_from("<I", pdata, offset + 3)[0] # TODO! Не самое элегантное решение logging.info("packet F4 datetime = %d" % dt) packer.add_point_to_packer(point, dt // 3600) offset += 32 lastpoint = point lastdt = dt elif pdata[offset + 1] == '\xF5': point = pdata[offset:offset+32] dt = unpack_from("<I", pdata, offset + 4)[0] # TODO! Не самое элегантное решение logging.info("packet F5 datetime = %d => %s" % (dt, datetime.fromtimestamp(dt).strftime('%Y-%m-%d %H:%M:%S'))) if lastdt >= dt: logging.error("Datetime must be grow (%d -> %d) at %d offset" % (lastdt, dt, offset)) packer.add_point_to_packer(point, dt // 3600) offset += 32 lastpoint = point lastdt = dt else: logging.error("Wrong packet at %d offset" % offset) packer.save_packer() if lastpoint is not None: # asdict = point_to_dict(lastpoint) self.dynamic.update(point_to_dict(lastpoint)) #system.update_dynamic(lastlat = asdict['latitude'], lastlon = asdict['longitude'], sats = asdict['sats']) # self.system.update_dynamic(**asdict) # msg = { # "id": 0, # "message": "update_dynamic", # "skey": self.skey, # "dynamic": asdict # } # self.application.publisher.send(msg) # msg = { # "id": 0, # "message": "last_update", # "skey": self.skey, # "point": asdict # } # self.application.publisher.send(msg) for l in sinform.sinform_getall(self.skey): self.write("%s\r\n" % str(l)) self.write("ADDLOG: OK\r\n") self.write("BINGPS: OK\r\n")