led_control_XmSgj.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import sys
  2. from concurrent.futures import ThreadPoolExecutor
  3. sys.path.append("..")
  4. # 导入pymysql
  5. import socket
  6. import threading
  7. import time
  8. from datetime import datetime
  9. from async_communication import TimeStatu
  10. import google.protobuf.text_format as tf
  11. import message_pb2 as message
  12. from led_protocol import LedProtocol
  13. from led_protocol import font_library
  14. from led_protocol import string_color
  15. import mytool.db_helper.db_operation as spmng
  16. class led_control(threading.Thread):
  17. def __init__(self, unit, channel, communication, area_list, db_config):
  18. threading.Thread.__init__(self)
  19. self._dispatch_statu = {}
  20. self.led_communication = communication
  21. self.area_list = area_list
  22. self.area_1_show_str_timer = TimeStatu('', 0.1)
  23. self.area_2_show_str_timer = TimeStatu('', 0.1)
  24. try:
  25. self.db = spmng.DBOperation(db_config['db_ip'], db_config['db_port'], db_config['db_name'],
  26. db_config['db_user'], db_config['db_password'])
  27. except Exception as e:
  28. print(str(e.args))
  29. self.channel = channel
  30. self.unit = unit
  31. if self.unit == 31 and self.channel == 1:
  32. self.en_font = font_library["EN32"]
  33. self.cn_font = font_library["CN32"]
  34. else:
  35. self.en_font = font_library["EN16"]
  36. self.cn_font = font_library["CN16"]
  37. self.led_protocol = LedProtocol(area_list)
  38. self._pool = ThreadPoolExecutor()
  39. self.led_control_init()
  40. def led_control_init(self):
  41. font = font_library["EN32"]
  42. unit_char = ""
  43. if self.unit == 1:
  44. unit_char = 'A'
  45. elif self.unit == 2:
  46. unit_char = 'B'
  47. elif self.unit == 11:
  48. unit_char = 'A1'
  49. elif self.unit == 12:
  50. unit_char = 'A2'
  51. elif self.unit == 13:
  52. unit_char = 'A3'
  53. elif self.unit == 14:
  54. unit_char = 'A4'
  55. elif self.unit == 21:
  56. unit_char = 'B1'
  57. elif self.unit == 22:
  58. unit_char = 'B2'
  59. elif self.unit == 23:
  60. unit_char = 'B3'
  61. elif self.unit == 24:
  62. unit_char = 'B4'
  63. elif self.unit == 25:
  64. unit_char = 'B5'
  65. elif self.unit == 31:
  66. unit_char = 'C1'
  67. if self.channel == 1:
  68. font = string_color["WHITE"] + font_library["EN48"]
  69. elif self.unit == 32:
  70. unit_char = 'C2'
  71. self.send_led_msg(0, unit_char, en_font=font, cn_font=self.cn_font)
  72. def receive_dispatch_statu(self, statu, ex, key):
  73. self._dispatch_statu[key] = statu
  74. def get_wait_led_string(self, cmd_queue):
  75. if len(cmd_queue) == 0:
  76. return ""
  77. led_show_string = ""
  78. for num, cmd in zip(range(0, len(cmd_queue)), cmd_queue):
  79. led_show_string = led_show_string + "%d#%s " % (num + 1, cmd['car_number'])
  80. led_show_string = "候车:" + led_show_string
  81. if self.unit == 31 and self.channel == 1:
  82. led_show_string = string_color["YELLOW"] + led_show_string
  83. return led_show_string
  84. def get_pick_led_string(self, cmd_queue):
  85. key = "dispatch_%d_statu_port" % self.unit
  86. if (key in self._dispatch_statu) is False or self._dispatch_statu[key].timeout():
  87. print('ERROR --- 调度节点未连接----key:%s---------time:%s' % (key, datetime.now()))
  88. led_show_string = " 故 障!"
  89. if self.unit == 31 and self.channel == 1:
  90. led_show_string = string_color["RED"] + led_show_string
  91. else:
  92. dispatch_node_statu = message.dispatch_node_statu()
  93. try:
  94. tf.Parse(self._dispatch_statu[key].statu, dispatch_node_statu)
  95. if dispatch_node_statu.plc_carrier_status == 0:
  96. led_show_string = " 故 障!"
  97. if self.unit == 31 and self.channel == 1:
  98. led_show_string = string_color["RED"] + led_show_string
  99. elif dispatch_node_statu.plc_carrier_status == 4:
  100. led_show_string = " 维护中!"
  101. if self.unit == 31 and self.channel == 1:
  102. led_show_string = string_color["RED"] + led_show_string
  103. else:
  104. if len(cmd_queue) == 0:
  105. led_show_string = "等待分配!"
  106. if self.unit == 31 and self.channel == 1:
  107. led_show_string = string_color["BLUE"] + led_show_string
  108. else:
  109. led_show_string = cmd_queue[0]['car_number'] + " 取车中,请稍候!"
  110. if self.unit == 31 and self.channel == 1:
  111. led_show_string = string_color["GREEN"] + led_show_string
  112. except Exception:
  113. print('ERROR --- 调度状态消息解析错误 key:%s time:%s message:\n%s' % (
  114. key, datetime.now(), self._dispatch_statu[key].statu))
  115. led_show_string = " 故 障!"
  116. if self.unit == 31 and self.channel == 1:
  117. led_show_string = string_color["RED"] + led_show_string
  118. return led_show_string
  119. # 发送LED消息
  120. def send_led_msg(self, area_index, input, en_font=font_library["EN32"], cn_font=font_library["CN32"]):
  121. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  122. sock.settimeout(1)
  123. try:
  124. sock.connect((self.led_communication['ip'], self.led_communication['port']))
  125. time.sleep(0.1)
  126. sock.send(self.led_protocol.string2bytes(area_index, input, en_font=en_font, cn_font=cn_font))
  127. print("connected led \033[0:32mSUCCEND\033[m ip:%s port:%d\nsend msg area_index:%d msg:%s" % (
  128. self.led_communication['ip'], self.led_communication['port'], area_index, input))
  129. except Exception as e:
  130. print("connected led \033[0:31mERROR\033[m: %s time:%s msg=%s" % (str(e.args), datetime.now(), input))
  131. time.sleep(0.1)
  132. sock.close()
  133. #
  134. def run(self):
  135. i = 0
  136. while True:
  137. time.sleep(1)
  138. if i == 20:
  139. self.led_control_init()
  140. time.sleep(0.1)
  141. i = 0
  142. i = i + 1
  143. wait_cmd_dict = self.db.query_pick_command_in_unit_and_statu(self.unit, 0)
  144. pick_cmd_dict = self.db.query_pick_command_in_unit_and_statu(self.unit, 1)
  145. if wait_cmd_dict is None or pick_cmd_dict is None:
  146. continue
  147. led_wait_string = self.get_wait_led_string(wait_cmd_dict)
  148. led_pick_string = self.get_pick_led_string(pick_cmd_dict)
  149. # test
  150. # self._pool.submit(self.send_led_msg, 1, str(i),self.en_font,self.cn_font)
  151. # i = i +1
  152. if self.area_1_show_str_timer.statu != led_wait_string or self.area_1_show_str_timer.timeout():
  153. self.area_1_show_str_timer = TimeStatu(led_wait_string, 20)
  154. self._pool.submit(self.send_led_msg, 1, led_wait_string, self.en_font, self.cn_font)
  155. if self.area_2_show_str_timer.statu != led_pick_string or self.area_2_show_str_timer.timeout():
  156. self.area_2_show_str_timer = TimeStatu(led_pick_string, 20)
  157. self._pool.submit(self.send_led_msg, 2, led_pick_string, self.en_font, self.cn_font)