code3000.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. # --- fill d1 ---
  35. """
  36. UserWorkRecordList: 用户操作记录
  37. UserWorkRecordList.uuid: 记录标识
  38. UserWorkRecordList.user_uuid: 用户标识
  39. UserWorkRecordList.start_at: 操作开始时间
  40. UserWorkRecordList.end_at: 操作结束时间
  41. UserWorkRecordList.cockpit_id: 舱端id
  42. UserWorkRecordList.vehicle_id: 车端id
  43. """
  44. d1 = list()
  45. page = sources.get('page', 1)
  46. size = sources.get('size', 10)
  47. for item in Global.mdb.get_all('UserWorkRecordList', sort_field=[('start_at', -1)]):
  48. # --- check ---
  49. uuid = str(item.get('_id'))
  50. if uuid not in log_file_uuid_list:
  51. continue
  52. # --- update ---
  53. item['uuid'] = uuid
  54. item['start_time_at'] = item.get('start_at')
  55. item['end_time_at'] = item.get('end_at')
  56. item['cockpit_name'] = item.get('cockpit_id')
  57. # --- check ---
  58. item['driver_name'] = item.get('user_uuid')
  59. if len('67174cf9cd36d7a6def99cae') == len(item.get('user_uuid')):
  60. item['driver_name'] = Global.mdb.get_one_by_id('UserInfo', item.get('user_uuid')).get('name')
  61. # --- check ---
  62. item['pid'] = item.get('vehicle_id')
  63. if int(item.get('vehicle_id')) in d2:
  64. item['pid'] = d2.get(int(item.get('vehicle_id')))
  65. # --- update --
  66. del item['_id']
  67. del item['user_uuid']
  68. del item['start_at']
  69. del item['end_at']
  70. del item['cockpit_id']
  71. del item['vehicle_id']
  72. d1.append(item)
  73. # d1.append(item)
  74. # d1 = [
  75. # {
  76. # 'uuid': '65dbe96949fbe311a3a01d30',
  77. # 'pid': 'AA112233', # 车牌号
  78. # 'start_time_at': methods.now_ts(),
  79. # 'end_time_at': methods.now_ts() + (3600 * 8),
  80. # 'driver_name': '张三',
  81. # 'cockpit_name': '1号舱',
  82. # }, {
  83. # 'uuid': '65dbe96949fbe311a3a01d30',
  84. # 'pid': 'AA112233',
  85. # 'start_time_at': methods.now_ts(),
  86. # 'end_time_at': methods.now_ts() + (3600 * 8),
  87. # 'driver_name': '李四',
  88. # 'cockpit_name': '1号舱',
  89. # }, {
  90. # 'uuid': '65dbe96949fbe311a3a01d30',
  91. # 'pid': 'AA112233',
  92. # 'start_time_at': methods.now_ts(),
  93. # 'end_time_at': methods.now_ts() + (3600 * 8),
  94. # 'driver_name': '李四',
  95. # 'cockpit_name': '1号舱',
  96. # }, {
  97. # 'uuid': '65dbe96949fbe311a3a01d30',
  98. # 'pid': 'AA112233',
  99. # 'start_time_at': methods.now_ts(),
  100. # 'end_time_at': methods.now_ts() + (3600 * 8),
  101. # 'driver_name': '李四',
  102. # 'cockpit_name': '1号舱',
  103. # }, {
  104. # 'uuid': '65dbe96949fbe311a3a01d30',
  105. # 'pid': 'AA112233',
  106. # 'start_time_at': methods.now_ts(),
  107. # 'end_time_at': methods.now_ts() + (3600 * 8),
  108. # 'driver_name': '李四',
  109. # 'cockpit_name': '1号舱',
  110. # }, {
  111. # 'uuid': '65dbe96949fbe311a3a01d30',
  112. # 'pid': 'AA112233',
  113. # 'start_time_at': methods.now_ts(),
  114. # 'end_time_at': methods.now_ts() + (3600 * 8),
  115. # 'driver_name': '李四',
  116. # 'cockpit_name': '1号舱',
  117. # }, {
  118. # 'uuid': '65dbe96949fbe311a3a01d30',
  119. # 'pid': 'AA112233',
  120. # 'start_time_at': methods.now_ts(),
  121. # 'end_time_at': methods.now_ts() + (3600 * 8),
  122. # 'driver_name': '李四',
  123. # 'cockpit_name': '1号舱',
  124. # }, {
  125. # 'uuid': '65dbe96949fbe311a3a01d30',
  126. # 'pid': 'AA112233',
  127. # 'start_time_at': methods.now_ts(),
  128. # 'end_time_at': methods.now_ts() + (3600 * 8),
  129. # 'driver_name': '李四',
  130. # 'cockpit_name': '1号舱',
  131. # }, {
  132. # 'uuid': '65dbe96949fbe311a3a01d30',
  133. # 'pid': 'AA112233',
  134. # 'start_time_at': methods.now_ts(),
  135. # 'end_time_at': methods.now_ts() + (3600 * 8),
  136. # 'driver_name': '李四',
  137. # 'cockpit_name': '1号舱',
  138. # }, {
  139. # 'uuid': '65dbe96949fbe311a3a01d30',
  140. # 'pid': 'AA112233',
  141. # 'start_time_at': methods.now_ts(),
  142. # 'end_time_at': methods.now_ts() + (3600 * 8),
  143. # 'driver_name': '李四',
  144. # 'cockpit_name': '1号舱',
  145. # }
  146. # ]
  147. return dict(code=0, data=d1[(page - 1) * size: page * size], total=len(d1), page=page, size=size)
  148. async def code3002(**sources):
  149. """
  150. 下载指定驾驶人员操作日志
  151. """
  152. # --- check ---
  153. if not sources.get('uuid'):
  154. return dict(code=1, details=f"not found uuid.[url中缺少uuid参数]")
  155. # --- get file_path ---
  156. # log_uuid = '671754598302fc6fd344819f'
  157. log_uuid = sources.get('uuid')
  158. file_path = os.path.join(save_dir, f"{log_uuid}.log")
  159. # --- get file_name ---
  160. methods.debug_log('code3000.code3002|145:', f"#log_uuid: {log_uuid}")
  161. item = Global.mdb.get_one_by_id('UserWorkRecordList', log_uuid)
  162. start_at = methods.ts_to_string(item.get('start_at'), '%Y%m%d%H%M%S')
  163. end_at = methods.ts_to_string(item.get('end_at'), '%Y%m%d%H%M%S')
  164. file_name = f"{start_at}-{end_at}.log"
  165. return {'code': 0, 'file_path': file_path, 'file_name': file_name}