communication.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import time
  2. import lib.ZX.async_communication as cm
  3. import lib.ZX.async_communication_etc_pb2 as rce
  4. import lib.ZX.tool as zx_tool
  5. import os, logging
  6. import detect_people_pb2 as detect_people_info
  7. import google.protobuf.text_format as tf
  8. default_rabbitmq_etc_file = os.path.dirname(os.path.abspath(__file__)) + "/etc/rabbitmq.json"
  9. class rabbitmq_client:
  10. def rabbitmq_client_exex(self, message):
  11. info = detect_people_info.DetectPeopleResults()
  12. tf.Parse(message.statu, info)
  13. # print("-------------------\n", info)
  14. def __init__(self, file=default_rabbitmq_etc_file):
  15. # 加载配置
  16. rabbitmq_etc = zx_tool.getProtobufJsonConfig(file, rce.RabbitmqEtc())
  17. print(rabbitmq_etc)
  18. self.g_rabbitmq = cm.RabbitAsyncCommunicator(rabbitmq_etc.ip, rabbitmq_etc.port,
  19. rabbitmq_etc.user, rabbitmq_etc.password)
  20. self.statu_ex_keys = []
  21. for bind in rabbitmq_etc.binds:
  22. key = [bind.ex, bind.route_key]
  23. self.statu_ex_keys.append(key)
  24. self.g_rabbitmq.Init(None, self.statu_ex_keys)
  25. self.g_rabbitmq.start()
  26. self.g_rabbitmq.bind_statu_callback(self.statu_ex_keys[0][0], self.statu_ex_keys[0][1], self.rabbitmq_client_exex)
  27. def sendPredict(self, message):
  28. return self.g_rabbitmq.publish(self.statu_ex_keys[0][0], self.statu_ex_keys[0][1], message, timeout=1)
  29. if __name__ == '__main__':
  30. client = rabbitmq_client()
  31. while True:
  32. client.sendPredict("test_msg")
  33. time.sleep(1)