node.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import sys
  2. sys.path.append("..")
  3. # 导入pymysql
  4. import socket
  5. import sys
  6. import threading
  7. import time
  8. from datetime import datetime
  9. from config import MQ_CONFIG as mq_config
  10. import google.protobuf.text_format as tf
  11. import pymysql as psql
  12. import async_communication as CM
  13. from db_helper.db_operation import DBOperation
  14. import message_pb2 as message
  15. from config import LED_CONFIG as led_config
  16. from led_protocol import LedProtocol
  17. from led_protocol import font_library
  18. class led_control(threading.Thread):
  19. def __init__(self, unit, communication, area_list):
  20. threading.Thread.__init__(self)
  21. self._dispatch_statu = {}
  22. self.led_communication = communication
  23. self.area_list = area_list
  24. self.wait_command_dict = {}
  25. self.pick_command_dict = {}
  26. self.db = DBOperation()
  27. self.unit = unit
  28. self.led_protocol = LedProtocol(area_list)
  29. def led_control_init(self):
  30. unit_char = ""
  31. if self.unit == 11:
  32. unit_char = 'A1'
  33. elif self.unit == 12:
  34. unit_char = 'A2'
  35. elif self.unit == 13:
  36. unit_char = 'A3'
  37. elif self.unit == 14:
  38. unit_char = 'A4'
  39. elif self.unit == 21:
  40. unit_char = 'BA1'
  41. elif self.unit == 22:
  42. unit_char = 'B2'
  43. elif self.unit == 23:
  44. unit_char = 'B3'
  45. elif self.unit == 24:
  46. unit_char = 'B4'
  47. elif self.unit == 25:
  48. unit_char = 'B5'
  49. elif self.unit == 31:
  50. unit_char = 'C1'
  51. elif self.unit == 32:
  52. unit_char = 'C2'
  53. for area in self.area_list.keys():
  54. if area == 0:
  55. self.send_led_msg(area, unit_char,en_font=font_library["EN32"])
  56. def receive_dispatch_statu(self, statu, ex, key):
  57. self._dispatch_statu[key] = statu
  58. def get_wait_led_string(self,cmd_queue):
  59. if len(cmd_queue) == 0:
  60. return ""
  61. led_show_string = ""
  62. for num, cmd in zip(range(0, len(cmd_queue)), cmd_queue):
  63. led_show_string = led_show_string + "%d#%s " % (num + 1, cmd['car_number'])
  64. led_show_string = "候车:"+led_show_string
  65. return led_show_string
  66. def get_pick_led_string(self, cmd_queue):
  67. if len(cmd_queue) == 0:
  68. return "等待分配!"
  69. led_show_string = cmd_queue[0]['car_number']+" 取车中,请稍候!"
  70. return led_show_string
  71. def send_led_msg(self, area_index, input, en_font=font_library["EN16"], cn_font=font_library["CN16"]):
  72. t_time = datetime.now()
  73. while (datetime.now() - t_time).microseconds < 30000:
  74. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  75. self.sock.settimeout(1)
  76. try:
  77. self.sock.connect((self.led_communication['ip'], self.led_communication['port']))
  78. self.sock.send(self.led_protocol.string2bytes(area_index, input, en_font, cn_font))
  79. print("connected led \033[0:32mSUCCEND\033[m ip:%s port:%d\nsend msg area_index:%d msg:%s" % (
  80. self.led_communication['ip'], self.led_communication['port'], area_index, input))
  81. break
  82. except Exception as e:
  83. print("connected led \033[0:31mERROR\033[m: %s time:%s msg=%s" % (str(e.args),datetime.now(),input))
  84. time.sleep(0.01)
  85. self.sock.close()
  86. time.sleep(0.1)
  87. def run(self):
  88. self.led_control_init()
  89. while True:
  90. wait_cmd_dict = self.db.query_pick_command_in_unit_and_statu(self.unit, 0)
  91. pick_cmd_dict = self.db.query_pick_command_in_unit_and_statu(self.unit, 1)
  92. if self.wait_command_dict != wait_cmd_dict:
  93. self.wait_command_dict = wait_cmd_dict
  94. led_show_string = self.get_wait_led_string(wait_cmd_dict)
  95. self.send_led_msg(1,led_show_string)
  96. key = "dispatch_%d_statu_port" % self.unit
  97. if (key in self._dispatch_statu) is False or self._dispatch_statu[key].timeout():
  98. print('ERROR --- 调度节点未连接----key:%s---------time:%s'%(key,datetime.now()))
  99. else:
  100. dispatch_node_statu = message.dispatch_node_statu()
  101. try:
  102. tf.Parse(self._dispatch_statu[key].statu, dispatch_node_statu)
  103. if dispatch_node_statu.plc_carrier_status == 0:
  104. self.send_led_msg(2, "故障!!!")
  105. elif dispatch_node_statu.plc_carrier_status == 4:
  106. self.send_led_msg(2, "维护中!")
  107. break
  108. except Exception:
  109. print('ERROR --- 调度状态消息解析错误 key:%s time:%s message:\n%s'% (key,datetime.now(),self._dispatch_statu[key].statu))
  110. if self.pick_command_dict != pick_cmd_dict:
  111. self.pick_command_dict = pick_cmd_dict
  112. led_show_string = self.get_pick_led_string(pick_cmd_dict)
  113. self.send_led_msg(2,led_show_string)
  114. time.sleep(1)
  115. if __name__ == "__main__":
  116. p_unit_id = led_config.unit
  117. p_channel = led_config.channel
  118. if len(sys.argv) == 3:
  119. p_unit_id = int(sys.argv[1])
  120. p_channel = int(sys.argv[2])
  121. g_rabbitmq = CM.RabbitAsyncCommunicator(mq_config.mq_ip, mq_config.mq_port, mq_config.mq_user, mq_config.mq_password)
  122. for list in mq_config.mq_statu_exchange_keys:
  123. if list[1].find(str(p_unit_id)) >= 0:
  124. g_rabbitmq.Init(None, [list],"export_%d_led"%p_channel)
  125. led = led_control(p_unit_id, led_config.led_list_all[p_unit_id][p_channel-1], led_config.area_list_all[p_unit_id][p_channel-1])
  126. for ex,key in mq_config.mq_statu_exchange_keys:
  127. g_rabbitmq.bind_statu_callback(ex,key,led.receive_dispatch_statu)
  128. g_rabbitmq.start()
  129. time.sleep(0.05)
  130. led.start()
  131. g_rabbitmq.join()
  132. led.join()