code3000.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from hub import methods, Global
  2. import os
  3. save_dir = r'C:\logs' # sri内网测试环境
  4. async def code3001(**sources):
  5. """
  6. 查询驾驶人员操作记录列表
  7. """
  8. # --- check ---
  9. if not sources.get('page'):
  10. return dict(code=1, details=f"something is wrong.")
  11. elif not sources.get('size'):
  12. return dict(code=2, details=f"something is wrong.")
  13. # --- get log_file_uuid_list ---
  14. log_file_list = methods.get_file_path_list(save_dir)
  15. log_file_name_list = [i.split('\\')[-1] for i in log_file_list]
  16. log_file_uuid_list = [i.split('.')[0] for i in log_file_name_list]
  17. # --- fill d2 ---
  18. d2 = dict()
  19. """
  20. VehicleInfo: 车辆信息表
  21. VehicleInfo.pid: 车牌号
  22. VehicleInfo.type: 车型号
  23. VehicleInfo.host_address: 工控机地址
  24. VehicleInfo.rtk_address: rtk地址
  25. VehicleInfo.cpe_address: cpe地址
  26. VehicleInfo.release_at: 出厂时期
  27. VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
  28. VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
  29. VehicleInfo.update_at: 更新时间
  30. """
  31. for item in Global.mdb.get_all('VehicleInfo'):
  32. ipv4 = int(f"{item.get('host_address').replace('.', '')}")
  33. d2[ipv4] = item.get('pid')
  34. cockpit_name_dict = {
  35. '': '1号舱',
  36. '': '2号舱',
  37. }
  38. # --- fill d1 ---
  39. """
  40. UserWorkRecordList: 用户操作记录
  41. UserWorkRecordList.uuid: 记录标识
  42. UserWorkRecordList.user_uuid: 用户标识
  43. UserWorkRecordList.start_at: 操作开始时间
  44. UserWorkRecordList.end_at: 操作结束时间
  45. UserWorkRecordList.cockpit_id: 舱端id
  46. UserWorkRecordList.vehicle_id: 车端id
  47. """
  48. d1 = list()
  49. page = sources.get('page', 1)
  50. size = sources.get('size', 10)
  51. for item in Global.mdb.get_all('UserWorkRecordList', sort_field=[('start_at', -1)]):
  52. # --- check ---
  53. uuid = str(item.get('_id'))
  54. if uuid not in log_file_uuid_list:
  55. continue
  56. # --- update ---
  57. item['uuid'] = uuid
  58. item['start_time_at'] = item.get('start_at')
  59. item['end_time_at'] = item.get('end_at')
  60. # --- update driver_name ---
  61. item['driver_name'] = item.get('user_uuid')
  62. if len('67174cf9cd36d7a6def99cae') == len(item.get('user_uuid')):
  63. item['driver_name'] = Global.mdb.get_one_by_id('UserInfo', item.get('user_uuid')).get('name')
  64. # --- update pid ---
  65. item['pid'] = item.get('vehicle_id')
  66. if int(item.get('vehicle_id')) in d2:
  67. item['pid'] = d2.get(int(item.get('vehicle_id')))
  68. # --- update cockpit_name ---
  69. item['cockpit_name'] = item.get('cockpit_id')
  70. # --- update --
  71. del item['_id']
  72. del item['user_uuid']
  73. del item['start_at']
  74. del item['end_at']
  75. del item['cockpit_id']
  76. del item['vehicle_id']
  77. d1.append(item)
  78. return dict(code=0, data=d1[(page - 1) * size: page * size], total=len(d1), page=page, size=size)
  79. async def code3002(**sources):
  80. """
  81. 下载指定驾驶人员操作日志
  82. """
  83. # --- check ---
  84. if not sources.get('uuid'):
  85. return dict(code=1, details=f"not found uuid.[url中缺少uuid参数]")
  86. # --- get file_path ---
  87. # log_uuid = '671754598302fc6fd344819f'
  88. log_uuid = sources.get('uuid')
  89. file_path = os.path.join(save_dir, f"{log_uuid}.log")
  90. # --- get file_name ---
  91. methods.debug_log('code3000.code3002|145:', f"#log_uuid: {log_uuid}")
  92. item = Global.mdb.get_one_by_id('UserWorkRecordList', log_uuid)
  93. start_at = methods.ts_to_string(item.get('start_at'), '%Y%m%d%H%M%S')
  94. end_at = methods.ts_to_string(item.get('end_at'), '%Y%m%d%H%M%S')
  95. file_name = f"{start_at}-{end_at}.log"
  96. return {'code': 0, 'file_path': file_path, 'file_name': file_name}