node.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import datetime
  2. import time
  3. import async_communication as CM
  4. import message_pb2 as message
  5. import google.protobuf.text_format as tf
  6. import CheckCommand as CHE
  7. from config import MQ_CONFIG as mq_config
  8. def check_park_command(body):
  9. print("接收停车指令 %s message:%s" % (datetime.datetime.now(), body))
  10. cmd = checker.entrance_isOK(body)
  11. response_port = "user_park_command_%d_response_port" % cmd.terminal_id
  12. dispatch_key = ""
  13. if cmd.statu.execute_statu == message.eNormal:
  14. # 指令检查正常
  15. print("停车指令 %s OK: 车牌号:%s 检查成功!" % (datetime.datetime.now(), cmd.car_number))
  16. dispatch_key = "park_command_request_port"
  17. g_rabbitmq.publish(mq_config.mq_command_exchange_name, dispatch_key, tf.MessageToString(cmd, as_utf8=True))
  18. else:
  19. print("停车指令 %s ERROR: 车牌号:%s 检查失败!" % (datetime.datetime.now(), cmd.car_number))
  20. g_rabbitmq.publish(mq_config.mq_command_exchange_name, response_port, tf.MessageToString(cmd, as_utf8=True))
  21. print("交换机:%s 发送反馈端口:%s %s 反馈表单:%s" % (mq_config.mq_command_exchange_name,response_port, dispatch_key, str(cmd)))
  22. def check_pick_command(body):
  23. print("接收取车指令 %s message:%s" % (datetime.datetime.now(), body))
  24. cmd = checker.exit_isOK(body)
  25. response_port = "user_pick_command_%d_response_port" % cmd.terminal_id
  26. dispatch_key = ""
  27. if cmd.statu.execute_statu == message.eNormal:
  28. # 指令检查正常
  29. print("取车指令 %s OK : 唯一码:%s 检查成功!" % (datetime.datetime.now(), cmd.primary_key))
  30. dispatch_key = "pick_command_request_port"
  31. g_rabbitmq.publish(mq_config.mq_command_exchange_name, dispatch_key, tf.MessageToString(cmd, as_utf8=True))
  32. else:
  33. print("取车指令 %s ERROR : 唯一码:%s 检查失败!" % (datetime.datetime.now(), cmd.primary_key))
  34. if cmd.terminal_id != 0:
  35. g_rabbitmq.publish(mq_config.mq_command_exchange_name, response_port, tf.MessageToString(cmd, as_utf8=True))
  36. print("交换机:%s 发送反馈端口:%s %s 反馈表单:%s" % (mq_config.mq_command_exchange_name,response_port, dispatch_key, str(cmd)))
  37. if __name__ == '__main__':
  38. # 消费指令消息
  39. cmd_callbacks = [["user_park_command_request_queue", check_park_command],
  40. ["user_pick_command_request_queue", check_pick_command]]
  41. g_rabbitmq = CM.RabbitAsyncCommunicator(mq_config.mq_ip, mq_config.mq_port, mq_config.mq_user, mq_config.mq_password)
  42. g_rabbitmq.Init(cmd_callbacks, mq_config.mq_statu_exchange_keys,"checker")
  43. checker = CHE.CommandChecker()
  44. for ex,key in mq_config.mq_statu_exchange_keys:
  45. g_rabbitmq.bind_statu_callback(ex,key,checker.receive_dispatch_statu)
  46. g_rabbitmq.start()