Esempio n. 1
0
 def _loadVehicles(self):
     vehicles_file = os.path.join(instance.getDataPath(), self.cfgs.get('vehicle_list'))
     fp = open(vehicles_file)
     for line in fp.readlines():
         line = line.strip()
         if not line:
             break
         self.vehicles.append(line.decode('utf-8'))
Esempio n. 2
0
 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
Esempio n. 3
0
    def saveCacheData(self):
        """写入所有车辆状态数据到本地数据库"""

        path = os.path.join(instance.getDataPath(),
                            self.cfgs.get('cache_file', 'location.db'))
        os.remove(path)

        conn = sqlite3.connect(path)
        conn.executescript(SQL_MO_TABLE)

        for k, v in self.mos.items():
            conn.execute("insert into movable_object values(?,0,0,?)",
                         (k, v.marshall()))
        conn = None
Esempio n. 4
0
    def getCacheFileName(self, mo):

        span = self.cfgs.get('data_span', 5) * 60
        loc = mo.getLocation()
        print mo.getId(), loc.time, timestamp_to_str(loc.time,
                                                     fmt='%Y%m%d_%H%M%M')
        start = (loc.time // span) * span
        name = timestamp_to_str(start, fmt='%Y%m%d_%H%M')
        app_data_path = instance.getDataPath()

        path = os.path.join(app_data_path, self.cfgs.get('data_path'), name)
        if not os.path.exists(path):
            os.makedirs(path)
        filename = mo.getId().encode('utf-8') + '.txt'
        return os.path.join(path, filename)
Esempio n. 5
0
    def _thread_persist(self):
        """
        将缓存数据持久化

        1.数据校验,保证缓存的记录没有超出时间边界,并保证按时间排序
        2.扫描时间从 1天前 - 距今两个缓存时间跨度
        :return:
        """
        seconds_oneday = 3600 * 24
        span = self.cfgs.get('data_span') * 60
        distance = self.cfgs.get('distance') * span
        app_data_path = instance.getDataPath()
        cache_path = os.path.join(app_data_path, self.cfgs.get('data_path'))
        back_path = os.path.join(app_data_path, self.cfgs.get('backup_path'))
        if not os.path.exists(back_path):
            os.makedirs(back_path)
        while True:
            now = time.time()
            end = (now // span) * span - distance
            start = end - seconds_oneday
            while start <= end:
                name = timestamp_to_str(start, fmt='%Y%m%d_%H%M')
                path = os.path.join(cache_path, name)
                # print path
                if not os.path.exists(path):
                    start += span
                    continue

                for _ in os.listdir(path):
                    if _.find('.txt') == -1:
                        continue
                    self.processFile(start, span, os.path.join(path, _))
                # 将数据目录移到备份目录
                src = path
                dest = os.path.join(back_path, name)
                print 'src:', src
                print 'dest:', dest
                if os.path.exists(dest):
                    shutil.rmtree(dest)

                shutil.move(src, dest)

                start += span

            gevent.sleep(5)
Esempio n. 6
0
 def getVehicles(self, user):
     """
     根据用户名获取车辆列表
     :param user:
     :return:
     """
     data_path = instance.getDataPath()
     ids = []
     for _ in self.cfgs.get('users', []):
         if _.get('name') == user:
             files = _.get('vehicle', [])
             for f in files:
                 path = os.path.join(data_path, f)
                 lines = open(path).readlines()
                 lines = map(string.strip, lines)
                 for line in lines:
                     if not line: continue
                     ids.append(line)
     return ids