def enqueue(self, mo_id, data): """ :param data: :type data: DataCategory :return: """ mo = self.mos.get(mo_id) if not mo: mo = MovableObject(mo_id) self.mos[mo_id] = mo if mo.update(data) == YES: return data return None
def processFile(self, start, span, locfile): """ 排序、时间范围校验 :param start: :param span: :param locfile: :return: """ lines = open(locfile).readlines() lines = map(string.strip, lines) list = map(json.loads, lines) mos = [] for _ in list: mo = MovableObject.unmarshall(_) if not mo: continue loc = mo.getLocation() if loc.time < start or loc.time > start + span: instance.getLogger().warning( 'location time is not in (%s,%s)' % (start, start + span)) continue mos.append(mo) #根据时间排序 mos = sorted( mos, lambda x, y: cmp(x.getLocation().time, y.getLocation().time)) PersistenceManager.instance().write(start, span, mos)
def set(self, mo_id, data): """ 1.加入新的车辆标识到内存数据库用于之后的车牌名称检索 :param mo_id: 车辆唯一标识 :type mo_id: basestring :param data: :type data: DataEnvelope or DataCategory """ mo = self.mos.get(mo_id) if not mo: mo = MovableObject(mo_id) self.mos[mo_id] = mo self.memconn.execute("insert into movable_object values(?)", (mo_id, )) if mo.update(data): self.diffs[mo.getId()] = mo #数据更新了 return mo return None
def getVehicleTrack(self, id, start, end, granule): """查询车辆轨迹 CREATE TABLE public.mo_data_20170501 ( id CHARACTER VARYING(20) NOT NULL, time INTEGER NOT NULL, data JSONB, last JSONB, PRIMARY KEY (id, time) ); CREATE INDEX mo_data_20170501_id_index ON mo_data_20170501 USING BTREE (id); CREATE INDEX mo_data_20170501_time_index ON mo_data_20170501 USING BTREE (time); CREATE INDEX mo_data_20170501_time_id_index ON mo_data_20170501 USING BTREE (id, time); 注意: 查询跨表 """ days = get_across_days(start, end) result = [] for day in days: y, m, d = day.year, day.month, day.day table = "mo_data_" + "%04d%02d%02d" % (y, m, d) # 获得全部轨迹记录,不做数据颗粒处理 sql = "select data from {table} where id=%s and (time BETWEEN %s and %s) order by time".format( table=table) conn = TraceService.instance().getDatabaseConnection() try: cur = conn.cursor() cur.execute(sql, (id, start, end)) last_time = 0 row = cur.fetchone() while row: # for data in json.loads(row[0]): # data = row[0] # if granule>5: # granule = 5 # num = len(row[0])//granule for _ in row[0]: step = VehicleObject( MovableObject.unmarshall(_)).dict() if step['time'] - granule * 60 > last_time: last_time = step['time'] result.append(step) # for _ in row[0]: # step = VehicleObject(MovableObject.unmarshall(_)).dict() # result.append(step) row = cur.fetchone() finally: TraceService.instance().freeDatabaseConnection(conn) return result
def loadCachedData(self): """启动时从本地db中加载所有的定位信息""" path = os.path.join(instance.getDataPath(), self.cfgs.get('cache_file', 'location.db')) conn = sqlite3.connect(path) cur = conn.cursor() for row in cur.execute('select * from mo_buffers'): moid, data, time = row[0], row[1], row[2] mo = MovableObject.unmarshall(data) if mo: self.mos[mo.id] = mo conn = None
def __init__(self, vehicle_id): MovableObject.__init__(self, vehicle_id)