code3000.py 3.9 KB

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