def postget(self): #logging.error('/addlog') slat = self.get_argument('lat', '0000.0000E') slon = self.get_argument('lon', '00000.0000N') lat = float(slat[:2]) + float(slat[2:9]) / 60.0 lon = float(slon[:3]) + float(slon[3:10]) / 60.0 if slat[-1] == 'S': lat = -lat if slon[-1] == 'W': lon = -lon pure_text = self.request.arguments.get('text', [''])[0] reencode = eval(repr(pure_text.decode('utf-8')).replace('u', '')) #.decode('utf-8') # logging.info("Pure text = %s" % repr(pure_text)) # logging.info("Reencode text = %s" % repr(reencode)) text = self.get_argument('text', '') if reencode.decode('utf-8') == reencode: pass else: text = reencode # logging.info("Log text on hex: %s" % text.encode('hex')) # try: #text = text.decode('utf-8') # except: # pass try: swid = restartlog.findall(text)[0] logging.info("parced SWID: %s" % repr(swid)) self.system.update({"$set": {"swid": swid}}) except: logging.info("skip parce") pass log = { 'imei': self.imei, 'skey': self.skey, 'akey': self.get_argument('akey', None), 'text': text, 'label': int(self.get_argument('label', '0'), 10), 'mtype': self.get_argument('mtype', None), 'lat': lat, 'lon': lon, 'fid': int(self.get_argument('fid', '0'), 10), 'ceng': self.get_argument('ceng', ''), 'dt': int(time()) } Logs().add(log) del log["_id"] msg = { "message": "add_log", "skey": self.skey, "log": log } self.application.publisher.send(msg) # Возможно это нужно перенести в общий блок. А может и нет. #sinform for l in sinform.sinform_getall(self.skey): self.write("%s\r\n" % str(l)) self.write("ADDLOG: OK\r\n")
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")