node.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import datetime
  2. import threading
  3. import time
  4. import async_communication as CM
  5. import message_pb2 as message
  6. import google.protobuf.text_format as tf
  7. import CheckCommand as CHE
  8. from config import MQ_CONFIG as mq_config
  9. import sys
  10. sys.path.append("..")
  11. from log_helper.logger import HandleLog
  12. def check_park_command(body):
  13. # print("接收停车指令 %s message:%s" % (datetime.datetime.now(), body))
  14. log.info("接收停车指令 %s message:%s" % (datetime.datetime.now(),body[0:120]))
  15. cmd = checker.entrance_isOK(body)
  16. response_port = "user_park_command_%d_response_port" % cmd.terminal_id
  17. dispatch_key = ""
  18. if cmd.statu.execute_statu == message.eNormal:
  19. # 指令检查正常
  20. log.info("停车指令 %s OK: 车牌号:%s 检查成功!\n" % (datetime.datetime.now(), cmd.car_number))
  21. dispatch_key = "park_command_request_port"
  22. g_rabbitmq.publish(mq_config.mq_command_exchange_name, dispatch_key, tf.MessageToString(cmd, as_utf8=True))
  23. else:
  24. if cmd.statu.execute_statu == message.eWarning:
  25. log.warning("停车指令 %s 车牌号:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % (
  26. datetime.datetime.now(), cmd.car_number,
  27. mq_config.mq_command_exchange_name, response_port + "," + dispatch_key,
  28. tf.MessageToString(cmd, as_utf8=True)))
  29. elif cmd.statu.execute_statu == message.eError:
  30. log.error("停车指令 %s 车牌号:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % (
  31. datetime.datetime.now(), cmd.car_number,
  32. mq_config.mq_command_exchange_name, response_port + "," + dispatch_key,
  33. tf.MessageToString(cmd, as_utf8=True)))
  34. else:
  35. log.critical("停车指令 %s 车牌号:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % (
  36. datetime.datetime.now(), cmd.car_number,
  37. mq_config.mq_command_exchange_name, response_port + "," + dispatch_key,
  38. tf.MessageToString(cmd, as_utf8=True)))
  39. g_rabbitmq.publish(mq_config.mq_command_exchange_name, response_port, tf.MessageToString(cmd, as_utf8=True))
  40. def check_pick_command(body):
  41. log.info("接收取车指令 %s message:%s" % (datetime.datetime.now(), body))
  42. cmd = checker.exit_isOK(body)
  43. response_port = "user_pick_command_%d_response_port" % cmd.terminal_id
  44. dispatch_key = ""
  45. if cmd.statu.execute_statu == message.eNormal:
  46. # 指令检查正常
  47. log.info("取车指令 %s OK : 唯一码:%s 检查成功!\n" % (datetime.datetime.now(), cmd.primary_key))
  48. dispatch_key = "pick_command_request_port"
  49. g_rabbitmq.publish(mq_config.mq_command_exchange_name, dispatch_key, tf.MessageToString(cmd, as_utf8=True))
  50. else:
  51. if cmd.statu.execute_statu == message.eWarning:
  52. log.warning("取车指令 %s 唯一码:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % (
  53. datetime.datetime.now(), cmd.primary_key,
  54. mq_config.mq_command_exchange_name, response_port + "," + dispatch_key,
  55. tf.MessageToString(cmd, as_utf8=True)))
  56. elif cmd.statu.execute_statu == message.eError:
  57. log.error("取车指令 %s 唯一码:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % (
  58. datetime.datetime.now(), cmd.primary_key,
  59. mq_config.mq_command_exchange_name, response_port + "," + dispatch_key,
  60. tf.MessageToString(cmd, as_utf8=True)))
  61. else:
  62. log.critical("取车指令 %s 唯一码:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % (
  63. datetime.datetime.now(), cmd.primary_key,
  64. mq_config.mq_command_exchange_name, response_port + "," + dispatch_key,
  65. tf.MessageToString(cmd, as_utf8=True)))
  66. if cmd.terminal_id != 0:
  67. g_rabbitmq.publish(mq_config.mq_command_exchange_name, response_port, tf.MessageToString(cmd, as_utf8=True))
  68. if __name__ == '__main__':
  69. # 日志记录
  70. log = HandleLog.get_logger('.\\指令检查节点\\logs')
  71. # 消费指令消息
  72. cmd_callbacks = [["user_park_command_request_queue", check_park_command],
  73. ["user_pick_command_request_queue", check_pick_command]]
  74. print("系统初始化中...... %s " % (datetime.datetime.now()))
  75. checker = CHE.CommandChecker()
  76. print("系统初始化完成! %s " % (datetime.datetime.now()))
  77. g_rabbitmq = CM.RabbitAsyncCommunicator(mq_config.mq_ip, mq_config.mq_port, mq_config.mq_user, mq_config.mq_password)
  78. g_rabbitmq.Init(cmd_callbacks, mq_config.mq_statu_exchange_keys,"checker")
  79. for ex,key in mq_config.mq_statu_exchange_keys:
  80. g_rabbitmq.bind_statu_callback(ex,key,checker.receive_dispatch_statu)
  81. g_rabbitmq.start()
  82. g_rabbitmq.join()