code2000.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. from hub import methods, Global
  2. async def code2001(**sources):
  3. """
  4. 新增车辆
  5. """
  6. # --- check ---
  7. if not sources.get('pid'):
  8. return dict(code=1, details=f"something is wrong.")
  9. elif not sources.get('type'):
  10. return dict(code=2, details=f"something is wrong.")
  11. # --- check ---
  12. count = Global.mdb.get_count('VehicleInfo', {'pid': sources.get('pid')})
  13. if count != 0:
  14. return dict(code=3, details=f"something is wrong.")
  15. # --- save ---
  16. """
  17. VehicleInfo: 车辆信息表
  18. VehicleInfo.pid: 车牌号
  19. VehicleInfo.type: 车型号
  20. VehicleInfo.host_address: 工控机地址
  21. VehicleInfo.rtk_address: rtk地址
  22. VehicleInfo.cpe_address: cpe地址
  23. VehicleInfo.release_at: 出厂时期
  24. VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
  25. VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
  26. VehicleInfo.update_at: 更新时间
  27. """
  28. data = {
  29. 'pid': sources.get('pid'),
  30. 'type': sources.get('type'),
  31. 'host_address': sources.get('host_address'),
  32. 'rtk_address': sources.get('rtk_address'),
  33. 'cpe_address': sources.get('cpe_address'),
  34. 'release_at': sources.get('release_at'),
  35. 'update_at': methods.now_ts(),
  36. 'state': 1,
  37. 'permit_state': 0,
  38. }
  39. uuid = Global.mdb.add('VehicleInfo', data)
  40. return dict(code=0, data=uuid)
  41. async def code2002(**sources):
  42. """
  43. 查询车辆列表
  44. """
  45. # --- check ---
  46. if not sources.get('page'):
  47. return dict(code=1, details=f"something is wrong.")
  48. elif not sources.get('size'):
  49. return dict(code=2, details=f"something is wrong.")
  50. # --- get VehicleStatus ---
  51. unique_dict = {'name': 'VehicleStatus'}
  52. item = Global.mdb.get_one('GlobalVariable', unique_dict)
  53. status_dict = item.get('args', {})
  54. # --- fill d1 ---
  55. d1 = list()
  56. page = sources.get('page', 1)
  57. size = sources.get('size', 10)
  58. for item in Global.mdb.get_all('VehicleInfo', sort_field=[('update_at', -1)]):
  59. # --- check ---
  60. if sources.get('pid') and sources.get('pid') not in item.get('pid'):
  61. continue
  62. data = {
  63. 'uuid': str(item.get('_id')),
  64. 'pid': item.get('pid'),
  65. 'type': item.get('type'),
  66. 'host_address': item.get('host_address'),
  67. 'rtk_address': item.get('rtk_address'),
  68. 'cpe_address': item.get('cpe_address'),
  69. 'release_at': item.get('release_at'),
  70. 'state': item.get('state', 1), # 1:离线 2:在线(空闲) 3: 现场驾驶中 4: 远程驾驶中
  71. 'permit_state': item.get('permit_state', 0),
  72. }
  73. # --- update state ---
  74. vehicle_id = str(f"{item.get('host_address').replace('.', '')}")
  75. if not status_dict.get(vehicle_id):
  76. data['state'] = 1
  77. else:
  78. data['state'] = status_dict.get(vehicle_id)
  79. d1.append(data)
  80. return dict(code=0, data=d1[(page - 1) * size: page * size], total=len(d1), page=page, size=size)
  81. async def code2003(**sources):
  82. """
  83. 修改指定车辆信息
  84. """
  85. # --- check ---
  86. uuid = sources.get('uuid')
  87. if not uuid:
  88. return dict(code=1, details=f"something is wrong.")
  89. # --- check ---
  90. item = Global.mdb.get_one_by_id('VehicleInfo', uuid)
  91. if not item:
  92. return dict(code=2, details=f"something is wrong.")
  93. # --- check ---
  94. # count = Global.mdb.get_count('VehicleInfo', {'pid': sources.get('pid')})
  95. # if count != 0:
  96. # return dict(code=3, details=f"something is wrong.")
  97. # --- update ---
  98. """
  99. VehicleInfo: 车辆信息表
  100. VehicleInfo.pid: 车牌号
  101. VehicleInfo.type: 车型号
  102. VehicleInfo.host_address: 工控机地址
  103. VehicleInfo.rtk_address: rtk地址
  104. VehicleInfo.cpe_address: cpe地址
  105. VehicleInfo.release_at: 出厂时期
  106. VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
  107. VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
  108. VehicleInfo.update_at: 更新时间
  109. """
  110. update_dict = dict()
  111. update_dict['update_at'] = methods.now_ts()
  112. if sources.get('pid'):
  113. update_dict['pid'] = sources.get('pid')
  114. if sources.get('type'):
  115. update_dict['type'] = sources.get('type')
  116. if sources.get('host_address'):
  117. update_dict['host_address'] = sources.get('host_address')
  118. if sources.get('rtk_address'):
  119. update_dict['rtk_address'] = sources.get('rtk_address')
  120. if sources.get('cpe_address'):
  121. update_dict['cpe_address'] = sources.get('cpe_address')
  122. if sources.get('release_at'):
  123. update_dict['release_at'] = sources.get('release_at')
  124. Global.mdb.update_one_by_id('VehicleInfo', uuid, update_dict)
  125. return dict(code=0, data=uuid)
  126. async def code2004(**sources):
  127. """
  128. 禁止指定车辆远程操作
  129. """
  130. # --- check ---
  131. uuid = sources.get('uuid')
  132. if not uuid:
  133. return dict(code=1, details=f"something is wrong.")
  134. # --- check ---
  135. item = Global.mdb.get_one_by_id('VehicleInfo', uuid)
  136. if not item:
  137. return dict(code=2, details=f"something is wrong.")
  138. # --- update ---
  139. """
  140. VehicleInfo: 车辆信息表
  141. VehicleInfo.pid: 车牌号
  142. VehicleInfo.type: 车型号
  143. VehicleInfo.host_address: 工控机地址
  144. VehicleInfo.rtk_address: rtk地址
  145. VehicleInfo.cpe_address: cpe地址
  146. VehicleInfo.release_at: 出厂时期
  147. VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
  148. VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
  149. VehicleInfo.update_at: 更新时间
  150. """
  151. update_dict = dict()
  152. update_dict['permit_state'] = 1
  153. item = Global.mdb.update_one_by_id('VehicleInfo', uuid, update_dict, need_back=True)
  154. return dict(code=0, data=item)
  155. async def code2005(**sources):
  156. """
  157. 允许指定车辆远程操作
  158. """
  159. # --- check ---
  160. uuid = sources.get('uuid')
  161. if not uuid:
  162. return dict(code=1, details=f"something is wrong.")
  163. # --- check ---
  164. item = Global.mdb.get_one_by_id('VehicleInfo', uuid)
  165. if not item:
  166. return dict(code=2, details=f"something is wrong.")
  167. # --- update ---
  168. """
  169. VehicleInfo: 车辆信息表
  170. VehicleInfo.pid: 车牌号
  171. VehicleInfo.type: 车型号
  172. VehicleInfo.host_address: 工控机地址
  173. VehicleInfo.rtk_address: rtk地址
  174. VehicleInfo.cpe_address: cpe地址
  175. VehicleInfo.release_at: 出厂时期
  176. VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
  177. VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
  178. VehicleInfo.update_at: 更新时间
  179. """
  180. update_dict = dict()
  181. update_dict['permit_state'] = 0
  182. item = Global.mdb.update_one_by_id('VehicleInfo', uuid, update_dict, need_back=True)
  183. return dict(code=0, data=item)
  184. async def code2006(**sources):
  185. """
  186. 删除指定车辆
  187. """
  188. # --- check ---
  189. uuid = sources.get('uuid')
  190. if not uuid:
  191. return dict(code=1, details=f"something is wrong.")
  192. # --- check ---
  193. item = Global.mdb.get_one_by_id('VehicleInfo', uuid)
  194. if not item:
  195. return dict(code=2, details=f"something is wrong.")
  196. # --- clean ---
  197. Global.mdb.remove_one_by_id('VehicleInfo', uuid)
  198. return dict(code=0, data=uuid)
  199. async def code2007(**sources):
  200. """
  201. 获取指定作业车辆详情
  202. """
  203. uuid = sources.get('uuid')
  204. if not uuid:
  205. return dict(code=1, details=f"something is wrong.")
  206. item = Global.mdb.get_one_by_id('VehicleInfo', uuid)
  207. if not item:
  208. return dict(code=2, details=f"something is wrong.")
  209. # --- fill ---
  210. """
  211. VehicleInfo: 车辆信息表
  212. VehicleInfo.pid: 车牌号
  213. VehicleInfo.type: 车型号
  214. VehicleInfo.host_address: 工控机地址
  215. VehicleInfo.rtk_address: rtk地址
  216. VehicleInfo.cpe_address: cpe地址
  217. VehicleInfo.release_at: 出厂时期
  218. VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
  219. VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
  220. VehicleInfo.update_at: 更新时间
  221. """
  222. item.pop('_id')
  223. item['uuid'] = uuid
  224. return dict(code=0, data=item)