main.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import asyncio
  2. import datetime
  3. import os
  4. import websockets
  5. import uuid
  6. import config
  7. from loguru import logger
  8. import json
  9. from algo.car_cf import car_classification
  10. from algo.car_direction import car_dircetion
  11. from algo.class_direction import class_direction
  12. logger.add(config.log_path, rotation="50 MB", encoding='utf-8')
  13. XLS_TYPE = 'xls'
  14. XLSX_TYPE = 'xlsx'
  15. TXT_TYPE ='txt'
  16. WAV_TYPE = 'wav'
  17. JSON_TYPE ='json'
  18. NPY_TYPE = 'npy'
  19. handlers = {
  20. XLS_TYPE : 'xls',
  21. XLSX_TYPE : 'xlsx',
  22. TXT_TYPE : 'txt',
  23. WAV_TYPE : 'wav',
  24. JSON_TYPE :'json',
  25. NPY_TYPE : 'npy'
  26. }
  27. CLOSE_TYPE = 'close'
  28. ERROR_TYPE = 'error'
  29. CONNECT_TYPE = 'connect'
  30. CAR_CLASS = 'car_classification'
  31. CAR_DIREC = 'car_direction'
  32. CLASS_DIREC = 'class_direc'
  33. class MessageOperation:
  34. def __init__(self, uid: str):
  35. self._uid = uid
  36. async def handle(self, msg: str) -> str:
  37. try:
  38. ask = json.loads(msg)
  39. self.msg_type = ask['msg_type']
  40. self.positive_dir = ask['positive_dir']
  41. self.negativa_dir = ask['negativa_dir']
  42. self.task_type = ask['task_type']
  43. self.record_id = ask['recordId']
  44. self.msg_content = ask['content']
  45. if self.task_type == CAR_CLASS:
  46. if self.msg_type in handlers:
  47. self.response_status,self.response_car_content,self.response_direction_content = car_classification(
  48. self.msg_content)
  49. elif self.msg_type == CLOSE_TYPE:
  50. self.response_status = True
  51. self.response_car_content = "close success"
  52. self.response_direction_content = 'close success'
  53. else:
  54. self.response_status = False
  55. self.response_car_content = "无法识别"
  56. self.response_direction_content = '无法识别'
  57. # self.record_id = "null"
  58. elif self.task_type == CAR_DIREC:
  59. if self.msg_type in handlers:
  60. self.response_status, self.response_direction_content, self.response_car_content = car_dircetion(
  61. self.msg_content,self.positive_dir,self.negativa_dir)
  62. elif self.msg_type == CLOSE_TYPE:
  63. self.response_status = True
  64. self.response_direction_content = "close success"
  65. self.response_car_content = "close success"
  66. else:
  67. self.response_status = False
  68. self.response_direction_content = "无法识别"
  69. self.response_car_content = "无法识别"
  70. elif self.task_type == CLASS_DIREC:
  71. if self.msg_type in handlers:
  72. self.response_status, self.response_car_content,self.response_direction_content = class_direction(
  73. self.msg_content,self.positive_dir,self.negativa_dir)
  74. elif self.msg_type == CLOSE_TYPE:
  75. self.response_status = True
  76. self.response_car_content = "close success"
  77. # self.response_direction_content = "close success"
  78. else:
  79. self.response_status = False
  80. self.response_car_content = "无法识别"
  81. # self.response_direction_content = "无法识别"
  82. except Exception as e:
  83. self.msg_type = ERROR_TYPE
  84. self.response_status = False
  85. self.response_content = str(e)
  86. def pack(self) -> str:
  87. return json.dumps({
  88. 'Sender': self._uid,
  89. 'Recipient': 'server',
  90. 'recordId': self.record_id,
  91. 'status': self.response_status,
  92. 'msg_type': self.msg_type,
  93. 'car_content': self.response_car_content,
  94. 'direction_content': self.response_direction_content,
  95. 'time': str(datetime.datetime.now())
  96. })
  97. async def main():
  98. uid = str(uuid.uuid4())
  99. async with websockets.connect(
  100. f'{config.WS_URI}?uid={uid}&to_uid=server') as websocket:
  101. pid = os.getpid()
  102. logger.info(f'uid: {uid}')
  103. logger.info(f'pid: {pid}')
  104. greeting = json.dumps({
  105. 'status': True,
  106. 'msg_type': CONNECT_TYPE,
  107. 'pid': pid,
  108. 'car_content': '',
  109. 'direction_content': '',
  110. 'time': str(datetime.datetime.now())
  111. })
  112. logger.debug(f">>> {greeting}")
  113. await websocket.send(greeting)
  114. await websocket.recv()
  115. while True:
  116. ask = await websocket.recv()
  117. logger.debug(f"<<< {ask}")
  118. msg_operation = MessageOperation(uid)
  119. await msg_operation.handle(ask)
  120. await websocket.send(msg_operation.pack())
  121. if msg_operation.msg_type == CLOSE_TYPE:
  122. break
  123. if __name__ == "__main__":
  124. logger.info('Starting...')
  125. asyncio.run(main())
  126. logger.info('End')