node.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import datetime
  2. import time
  3. from async_communication import rabbit_async_communicator as g_rabbitmq
  4. import message_pb2 as message
  5. import google.protobuf.text_format as tf
  6. from CheckCommand import check_command as g_check
  7. import threading
  8. # 状态消息接收器参数
  9. statu_ex_keys = [
  10. ["statu_ex","in_mcpu_1_statu_port"],
  11. ["statu_ex","in_mcpu_2_statu_port"],
  12. ["statu_ex","in_mcpu_3_statu_port"],
  13. ["statu_ex","in_mcpu_4_statu_port"],
  14. ["statu_ex","in_mcpu_5_statu_port"],
  15. ["statu_ex","in_mcpu_6_statu_port"],
  16. ["statu_ex","measure_1_statu_port"],
  17. ["statu_ex","measure_2_statu_port"],
  18. ["statu_ex","measure_3_statu_port"],
  19. ["statu_ex","measure_4_statu_port"],
  20. ["statu_ex","measure_5_statu_port"],
  21. ["statu_ex","measure_6_statu_port"],
  22. ["statu_ex","dispatch_1_statu_port"],
  23. ["statu_ex","dispatch_2_statu_port"],
  24. ["statu_ex","dispatch_3_statu_port"]
  25. ]
  26. # mq参数
  27. mq_ip = "192.168.1.233"
  28. mq_port = 5672
  29. mq_user = "zx"
  30. mq_password = "zx123456"
  31. ex_name = "command_ex"
  32. def user_command_callback(body):
  33. if body.find("car_number")>=0:
  34. check_park_command(body)
  35. elif body.find("primary_key")>=0:
  36. check_pick_command(body)
  37. else:
  38. print("指令类型错误!\n%s" % body)
  39. def check_park_command(body):
  40. print("recieve park_command_queue message:%s" % body)
  41. table = g_check.check_park_command(body)
  42. cmd = message.park_table()
  43. tf.Parse(table, cmd)
  44. if cmd.statu.execute_statu == message.eNormal:
  45. # 指令检查正常
  46. print("停车指令 %s OK: 车牌号:%s 检查成功!" % (
  47. datetime.datetime.now(), cmd.car_number))
  48. response_port = "command_enqueue_port"
  49. else:
  50. print("停车指令 %s ERROR: 车牌号:%s 检查失败!" % (
  51. datetime.datetime.now(), cmd.car_number))
  52. response_port = "park_response_%d_port" % cmd.terminal_id
  53. g_rabbitmq.publish( ex_name, response_port,table)
  54. print("发送反馈端口:%s 反馈表单:%s"%(response_port,table))
  55. def check_pick_command(body):
  56. print("recv pick_command_queue message:%s" % body)
  57. table = g_check.check_pick_command(body)
  58. cmd = message.pick_table()
  59. tf.Parse(table, cmd)
  60. if cmd.statu.execute_statu == message.eNormal:
  61. # 指令检查正常
  62. print("取车指令 %s OK : 唯一码:%s 检查成功!" % (
  63. datetime.datetime.now(), cmd.primary_key))
  64. response_port = "command_enqueue_port"
  65. else:
  66. print("取车指令 %s ERROR : 唯一码:%s 检查失败!" % (
  67. datetime.datetime.now(), cmd.primary_key))
  68. response_port = "pick_response_port"
  69. g_rabbitmq.publish( ex_name, response_port,table)
  70. print("发送反馈端口:%s 反馈表单:%s"%(response_port,table))
  71. def statu_msg_thread():
  72. statu = message.table_statu()
  73. statu.execute_statu = message.eNormal
  74. body = tf.MessageToString(statu, as_utf8=True)
  75. while True:
  76. g_rabbitmq.publish("statu_ex", "command_check_statu_port",body)
  77. time.sleep(1)
  78. if __name__ == '__main__':
  79. # 消费指令消息
  80. cmd_callbacks=[["user_command_queue",user_command_callback]]
  81. g_rabbitmq.InitRabbitmq(mq_ip,mq_port,mq_user,mq_password)
  82. g_rabbitmq.InitConsumer(cmd_callbacks,statu_ex_keys)
  83. g_rabbitmq.start()
  84. statu = message.table_statu()
  85. statu.execute_statu = message.eNormal
  86. body = tf.MessageToString(statu, as_utf8=True)
  87. while True:
  88. g_rabbitmq.publish("statu_ex", "command_check_statu_port",body)
  89. time.sleep(1)
  90. g_rabbitmq.join()