def query_gps(user_id: (str, ObjectId), begin: datetime.datetime, end: datetime.datetime) -> list: """ 查询指定用户,指定时间区间的数据 :param user_id: :param begin: :param end: :return: """ u = User.find_by_id(user_id) f = {"user_id": u.get_dbref(), "time": {"$gte": begin, "$lte": end}} s = {"time": -1} r = GPS.find_plus(filter_dict=f, sort_dict=s, to_dict=True) return r
def process_device_info(user_id, key): """处理手机(及其附带的传感器的)信息的增删改查""" message = pack_message(error_code=3012, key=key) if key == "add": """添加一个移动设备的信息,原则上说,如果型号和model相同就认为是同一款手机,但是也不排除同一型号的手机,由于发布时间不同 厂家私自更改配置的情况""" args = get_args(request) print(args) try: args.pop("token") except KeyError as e: print(e) finally: if args is None or len(args) == 0: message = pack_message() else: message = {"message": "success"} dbref = None try: obj = item_module.PhoneDevice(**args) dbref = obj.save_self_and_return_dbref() except error_module.RepeatError as e: print(e) message['inserted_id'] = '' except Exception as e: logger.exception("process_device_info exception:") print(e) message = pack_message(error_code=5000, **args) finally: if dbref is None: pass else: """更新用户信息""" res = User.add_phone_device(user_id=user_id, dbref_obj=dbref) if res: pass else: message = pack_message(error_code=5000, user_id=user_id, **args) else: """没法识别的key类型""" pass return json.dumps(message)
def rebuild() -> None: """ 重建关系员工和公司 :return: """ us = read_excel() p_map = { x['phone_num']: { "real_name": x['real_name'], "employee_number": x['employee_number'], "line": x['line'], } for x in us } phones = list(p_map.keys()) f = {"phone_num": {"$in": phones}} users = User.find_plus(filter_dict=f, to_dict=False) ids = list() for e in users: phone = e.get_attr('phone_num') temp = p_map[phone] e.set_attr('real_name', temp['real_name']) e.set_attr('employee_number', temp['employee_number']) e.set_attr('line', temp['line']) r = e.save_plus() if r is None: print("保存失败") else: ids.append(r) company_id = get_obj_id("59a671a0de713e3db34de8bf") # 顺丰公司 dept_id = get_obj_id("5abcac4b4660d3599207fe18") # 顺丰华新分部 post_id = get_obj_id("59a67348de713e3f43dcf0d7") # 司机岗位 Company.rebuild_relation(company_id=company_id, dept_id=dept_id, post_id=post_id, employee_list=ids)
def page(cls, driver_id: ObjectId = None, writer: (str, ObjectId, DBRef) = None, status: int = None, plate_number: str = None, city: str = None, begin_date: datetime.datetime = None, end_date: datetime.datetime = None, index: int = 1, num: int = 20, can_json: bool = True, reverse: bool = True) -> dict: """ 分页查询行车记录 :param driver_id: 司机id,为空表示所有司机,注意这里为了和违章记录保持一致,是ObjectId对象而不是常见的DBRef :param writer: 录入者id,默认指向user_info的_id :param status: int 处理状态 0未处理,1已处理, -1和None表示不做筛选 :param plate_number: 车牌 :param city: 城市 :param begin_date: 开始时间 :param end_date: 截至时间 :param index: 页码 :param can_json: 是否进行can json转换 :param num: 每页多少条记录 :param reverse: 是否倒序排列? :return: 事件记录的列表和统计组成的dict """ filter_dict = dict() if driver_id is not None: filter_dict['driver_id'] = driver_id if writer is not None: if isinstance(writer, DBRef): filter_dict['writer'] = writer elif isinstance(writer, ObjectId): writer = DBRef(database=mongo_db.db_name, collection=User.get_table_name(), id=writer) filter_dict['writer'] = writer elif isinstance(writer, str) and len(writer) == 24: writer = DBRef(database=mongo_db.db_name, collection=User.get_table_name(), id=ObjectId(writer)) filter_dict['writer'] = writer else: ms = "身份验证失败" raise ValueError(ms) if status is not None and status != -1: filter_dict['status'] = status if plate_number is not None: filter_dict['plate_number'] = plate_number if city is not None: filter_dict['city'] = city if isinstance(begin_date, datetime.datetime) and isinstance( end_date, datetime.datetime): filter_dict['time'] = {"$lte": end_date, "$gte": begin_date} elif isinstance(begin_date, datetime.datetime): filter_dict['time'] = {"$gte": begin_date} elif isinstance(end_date, datetime.datetime): filter_dict['time'] = {"$lte": end_date} else: pass skip = (index - 1) * num sort_dict = {"time": -1 if reverse else 1} count = cls.count(filter_dict=filter_dict) res = cls.find_plus(filter_dict=filter_dict, sort_dict=sort_dict, skip=skip, limit=num, to_dict=True) if can_json: res = [mongo_db.to_flat_dict(x) for x in res] data = {"count": count, "data": res} return data
def files_to_mongodb(args_dict: dict) -> None: """ 把一个zip文件的内容写入数据库 :param args_dict: zip文件内部的文件的绝对路径和内容对象数组组成的字典 :return: None """ for file_path, obj_list in args_dict.items(): file_name = os.path.split(file_path)[-1] # 获取文件名 print(file_name) if file_name == "device_info.txt": """设备信息""" obj_dict = obj_list[0] user_id = obj_dict.pop("user_id") obj = PhoneDevice(**obj_dict) device_id = PhoneDevice.find_by_id(obj.save()).get_dbref() User.add_phone_device(user_id, device_id) elif file_name == "rec_gps.txt": """GPS传感器""" obj_list = [ x for x in obj_list if not (x.get("time") == "0" or x.get('ts') == "0") ] if len(obj_list) > 0: inserted_obj_list = GPS.insert_many( obj_list) # 插入成功的GPS实例的doc的数组 """生产track数据,并返回最后的位置点""" last_position = Track.batch_create_item_from_gps( inserted_obj_list) else: print("empty file: {}".format(file_path)) else: """其他传感器""" sensor_type = file_name.split(".", 1)[0] """转换传感器类型的字典""" type_transform_dict = { "rec_acce": { "sensor_type": "accelerate", "description": "加速度传感器" }, "rec_grav": { "sensor_type": "gravitation", "description": "重力传感器(GV-sensor)" }, "rec_gyro": { "sensor_type": "gyroscope", "description": "陀螺仪传感器(Gyro-sensor)" }, "rec_rota": { "sensor_type": "rotation vector", "description": "旋转矢量传感器(RV-sensor)" }, "rec_magn": { "sensor_type": "magnetism", "description": "磁力传感器(M-sensor)" }, "rec_bpm": { "sensor_type": "beat per minute", "description": "心率传感器" } } obj_list = [ join_item(x, type_transform_dict.get(sensor_type)) for x in obj_list if not (x.get("time") == "0" or x.get('ts') == "0") ] if len(obj_list) > 0: Sensor.insert_many(obj_list) else: print("empty file: {}".format(file_path))
def read_zip(file_path: str) -> dict: """ 读取一个zip文件,返回内部的文件名和对象的数组组成的字典, :param file_path: zip文件绝对路径 :return: {"file_01":[dict_01, dict_02..., dict_0n], "file_02":[dict_01, dict_02..., dict_0n], ..... "file_0n":[dict_01, dict_02..., dict_0n]] """ zf = None try: if os.path.exists(file_path): zf = zipfile.ZipFile(file_path) else: pass # raise FileNotFoundError("{} not found".format(file_path)) except FileNotFoundError as e: print(e) recode("Error read_zip, file not found: filename={}".format(file_path)) logger.exception("Error read_zip:") except Exception as e1: logger.exception("Error read_zip:") print(e1) finally: if zf is None: result_dict = dict() else: name_list = zf.namelist() # 压缩文件内部的文件list result_dict = dict() # 存放结果集的字典 user_id = os.path.split(os.path.split(file_path)[0])[-1] user = User.find_by_id(user_id) user.set_attr("last_update", datetime.datetime.now()) # 更新上传时间 user.save() if not isinstance(user, User): # 这个文件夹的名字不合法 """删除文件""" delete_file(file_path) else: user_id = user.get_dbref() # 用户id app_version = None # app版本信息 phone_model = None # 当前手机型号 os_version = None # 当前手机操作系统 """检查是否有设备信息文件,如果有,就把设备信息文件中的app版本信息写入各种传感器/gps记录中""" device_file_name = 'device_info.txt' if device_file_name in name_list: """如果设备信息文件在压缩文件中,那就先读取这个文件""" name_list.remove(device_file_name) print("inner file name is {}".format(device_file_name)) obj = zf.read(device_file_name) file_content = obj.decode() try: dev_list = parse_content(file_content, user_id) _version = None """检查是否有app的版本信息?""" for dev in dev_list: app_version = dev.get('AppVersion') phone_model = dev.get('model') os_version = dev.get('os_version') if app_version is not None and phone_model is not None and os_version is not None: break result_dict[device_file_name] = dev_list except json.decoder.JSONDecodeError as e: print(e) msg = "文件{} {} json序列化失败".format( file_path, device_file_name) print(msg) logger.exception(msg) else: """如果没有设备信息文件,n那就忽略,保持app版本号为None""" pass """检查用户版本信息/ 手机设备是否一致?""" print(user) if app_version is not None: user_app_version = user.get_attr("app_version") if user_app_version != app_version: """更新app版本信息""" user.set_attr("app_version", app_version) if phone_model is not None: user_phone_model = user.get_attr('phone_model') if user_phone_model != phone_model: user.set_attr("phone_model", phone_model) if os_version is not None: user_os_version = user.get_attr('os_version') if user_os_version != os_version: user.set_attr("os_version", os_version) user.set_attr("last_update", datetime.datetime.now()) # 更新最后接收数据的时间 user.save_plus() """向zip文件读取结果集中加入版本信息""" for name in name_list: print("inner file name is {}".format(name)) obj = zf.read(name) file_content = obj.decode() try: dict_list = parse_content(file_content, user_id, app_version) result_dict[name] = dict_list except json.decoder.JSONDecodeError as e: print(e) msg = "文件{} {} json序列化失败".format(file_path, name) print(msg) logger.exception(msg) zf.close() return result_dict