code3000.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = [
  74. # {
  75. # 'uuid': '65dbe96949fbe311a3a01d30',
  76. # 'pid': 'AA112233', # 车牌号
  77. # 'start_time_at': methods.now_ts(),
  78. # 'end_time_at': methods.now_ts() + (3600 * 8),
  79. # 'driver_name': '张三',
  80. # 'cockpit_name': '1号舱',
  81. # }, {
  82. # 'uuid': '65dbe96949fbe311a3a01d30',
  83. # 'pid': 'AA112233',
  84. # 'start_time_at': methods.now_ts(),
  85. # 'end_time_at': methods.now_ts() + (3600 * 8),
  86. # 'driver_name': '李四',
  87. # 'cockpit_name': '1号舱',
  88. # }, {
  89. # 'uuid': '65dbe96949fbe311a3a01d30',
  90. # 'pid': 'AA112233',
  91. # 'start_time_at': methods.now_ts(),
  92. # 'end_time_at': methods.now_ts() + (3600 * 8),
  93. # 'driver_name': '李四',
  94. # 'cockpit_name': '1号舱',
  95. # }, {
  96. # 'uuid': '65dbe96949fbe311a3a01d30',
  97. # 'pid': 'AA112233',
  98. # 'start_time_at': methods.now_ts(),
  99. # 'end_time_at': methods.now_ts() + (3600 * 8),
  100. # 'driver_name': '李四',
  101. # 'cockpit_name': '1号舱',
  102. # }, {
  103. # 'uuid': '65dbe96949fbe311a3a01d30',
  104. # 'pid': 'AA112233',
  105. # 'start_time_at': methods.now_ts(),
  106. # 'end_time_at': methods.now_ts() + (3600 * 8),
  107. # 'driver_name': '李四',
  108. # 'cockpit_name': '1号舱',
  109. # }, {
  110. # 'uuid': '65dbe96949fbe311a3a01d30',
  111. # 'pid': 'AA112233',
  112. # 'start_time_at': methods.now_ts(),
  113. # 'end_time_at': methods.now_ts() + (3600 * 8),
  114. # 'driver_name': '李四',
  115. # 'cockpit_name': '1号舱',
  116. # }, {
  117. # 'uuid': '65dbe96949fbe311a3a01d30',
  118. # 'pid': 'AA112233',
  119. # 'start_time_at': methods.now_ts(),
  120. # 'end_time_at': methods.now_ts() + (3600 * 8),
  121. # 'driver_name': '李四',
  122. # 'cockpit_name': '1号舱',
  123. # }, {
  124. # 'uuid': '65dbe96949fbe311a3a01d30',
  125. # 'pid': 'AA112233',
  126. # 'start_time_at': methods.now_ts(),
  127. # 'end_time_at': methods.now_ts() + (3600 * 8),
  128. # 'driver_name': '李四',
  129. # 'cockpit_name': '1号舱',
  130. # }, {
  131. # 'uuid': '65dbe96949fbe311a3a01d30',
  132. # 'pid': 'AA112233',
  133. # 'start_time_at': methods.now_ts(),
  134. # 'end_time_at': methods.now_ts() + (3600 * 8),
  135. # 'driver_name': '李四',
  136. # 'cockpit_name': '1号舱',
  137. # }, {
  138. # 'uuid': '65dbe96949fbe311a3a01d30',
  139. # 'pid': 'AA112233',
  140. # 'start_time_at': methods.now_ts(),
  141. # 'end_time_at': methods.now_ts() + (3600 * 8),
  142. # 'driver_name': '李四',
  143. # 'cockpit_name': '1号舱',
  144. # }
  145. # ]
  146. return dict(code=0, data=d1[(page - 1) * size: page * size], total=len(d1), page=page, size=size)
  147. async def code3002(**sources):
  148. """
  149. 下载指定驾驶人员操作日志
  150. """
  151. if not sources.get('uuid'):
  152. return dict(code=1, details=f"not found uuid.")
  153. # log_uuid = '671754598302fc6fd344819f'
  154. # if sources.get('uuid'):
  155. # log_uuid = sources.get('uuid')
  156. methods.debug_log('code3000.code3002|145:', f"#log_uuid: {sources.get('uuid')}")
  157. file_path = os.path.join(save_dir, f"{sources.get('uuid')}.log")
  158. # --- get file_name ---
  159. item = Global.mdb.get_one_by_id('UserWorkRecordList', sources.get('uuid'))
  160. start_at = methods.ts_to_string(item.get('start_at'), '%Y%m%d%H%M%S')
  161. end_at = methods.ts_to_string(item.get('end_at'), '%Y%m%d%H%M%S')
  162. file_name = f"{start_at}-{end_at}.log"
  163. return {'file_path': file_path, 'file_name': file_name}