node.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 CheckEntrance as CHE
  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. async def user_command_callback(body):
  33. if body.find("car_number")>=0:
  34. await check_park_command(body)
  35. elif body.find("primary_key")>=0:
  36. await check_pick_command(body)
  37. else:
  38. print("指令类型错误!\n%s" % body)
  39. async def check_park_command(body):
  40. print("recieve park_command_queue message:%s" % body)
  41. park = message.park_table()
  42. tf.Parse(body, park)
  43. table = await EntranceCheckers[park.terminal_id-1].entrance_isOK(park)
  44. cmd = message.park_table()
  45. tf.Parse(table, cmd)
  46. if cmd.statu.execute_statu == message.eNormal:
  47. # 指令检查正常
  48. print("停车指令 %s OK: 车牌号:%s 检查成功!" % (
  49. datetime.datetime.now(), cmd.car_number))
  50. response_port = "command_enqueue_port"
  51. else:
  52. print("停车指令 %s ERROR: 车牌号:%s 检查失败!" % (
  53. datetime.datetime.now(), cmd.car_number))
  54. response_port = "park_response_%d_port" % cmd.terminal_id
  55. g_rabbitmq.publish( ex_name, response_port,table)
  56. print("发送反馈端口:%s 反馈表单:%s"%(response_port,table))
  57. async def check_pick_command(body):
  58. print("recv pick_command_queue message:%s" % body)
  59. pick = message.pick_table()
  60. tf.Parse(body, pick)
  61. table = await EntranceCheckers[0].exit_isOK(pick)
  62. cmd = message.pick_table()
  63. tf.Parse(table, cmd)
  64. if cmd.statu.execute_statu == message.eNormal:
  65. # 指令检查正常
  66. print("取车指令 %s OK : 唯一码:%s 检查成功!" % (
  67. datetime.datetime.now(), cmd.primary_key))
  68. response_port = "command_enqueue_port"
  69. else:
  70. print("取车指令 %s ERROR : 唯一码:%s 检查失败!" % (
  71. datetime.datetime.now(), cmd.primary_key))
  72. response_port = "pick_response_%d_port" % cmd.terminal_id
  73. g_rabbitmq.publish( ex_name, response_port,table)
  74. print("发送反馈端口:%s 反馈表单:%s"%(response_port,table))
  75. def statu_msg_thread():
  76. statu = message.table_statu()
  77. statu.execute_statu = message.eNormal
  78. body = tf.MessageToString(statu, as_utf8=True)
  79. while True:
  80. g_rabbitmq.publish("statu_ex", "command_check_statu_port",body)
  81. time.sleep(1)
  82. cmd_callbacks=[["user_command_queue",user_command_callback]]
  83. g_rabbitmq=CM.RabbitAsyncCommunicator(mq_ip,mq_port,mq_user,mq_password)
  84. g_rabbitmq.Init(cmd_callbacks,statu_ex_keys)
  85. g_rabbitmq.start()
  86. EntranceCheckers=[]
  87. for i in range(6):
  88. checker=CHE.EntranceChecker()
  89. g_rabbitmq.bind_statu_callback("statu_ex", "in_mcpu_%d_statu_port"%(i+1), checker.receive_icpu)
  90. g_rabbitmq.bind_statu_callback("statu_ex", "measure_%d_statu_port"%(i+1), checker.receive_measureInfo)
  91. EntranceCheckers.append(checker)
  92. checker.start()
  93. if __name__ == '__main__':
  94. # 消费指令消息
  95. statu = message.table_statu()
  96. statu.execute_statu = message.eNormal
  97. body = tf.MessageToString(statu, as_utf8=True)
  98. while True:
  99. g_rabbitmq.publish("statu_ex", "command_check_statu_port",body)
  100. time.sleep(1)