led_control_XmSgj.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import sys
  2. from async_communication import TimeStatu
  3. sys.path.append("..")
  4. # 导入pymysql
  5. import socket
  6. import threading
  7. import time
  8. from datetime import datetime
  9. from concurrent.futures import ThreadPoolExecutor
  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. import mytool.db_helper.db_operation as spmng
  15. class led_control(threading.Thread):
  16. def __init__(self, unit, channel, communication, area_list, db_config):
  17. threading.Thread.__init__(self)
  18. self._dispatch_statu = {}
  19. self.led_communication = communication
  20. self.area_list = area_list
  21. self.area_1_show_str_timer = TimeStatu('', 0.1)
  22. self.area_2_show_str_timer = TimeStatu('', 0.1)
  23. self.db = spmng.DBOperation(db_config['db_ip'], db_config['db_port'], db_config['db_name'],
  24. db_config['db_user'], db_config['db_password'])
  25. self.unit = unit
  26. # if self.unit == 31 or self.unit == 32:
  27. # self.en_font = font_library["EN32"]
  28. # self.cn_font = font_library["CN32"]
  29. # else:
  30. # self.en_font = font_library["EN16"]
  31. # self.cn_font = font_library["CN16"]
  32. self.en_font = font_library["EN16"]
  33. self.cn_font = font_library["CN16"]
  34. self.channel = channel
  35. self.led_protocol = LedProtocol(area_list)
  36. self._pool = ThreadPoolExecutor()
  37. self.led_control_init()
  38. def led_control_init(self):
  39. font = font_library["EN32"]
  40. unit_char = ""
  41. if self.unit == 1:
  42. unit_char = 'A'
  43. elif self.unit == 2:
  44. unit_char = 'B'
  45. elif self.unit == 11:
  46. unit_char = 'A1'
  47. elif self.unit == 12:
  48. unit_char = 'A2'
  49. elif self.unit == 13:
  50. unit_char = 'A3'
  51. elif self.unit == 14:
  52. unit_char = 'A4'
  53. elif self.unit == 21:
  54. unit_char = 'B1'
  55. elif self.unit == 22:
  56. unit_char = 'B2'
  57. elif self.unit == 23:
  58. unit_char = 'B3'
  59. elif self.unit == 24:
  60. unit_char = 'B4'
  61. elif self.unit == 25:
  62. unit_char = 'B5'
  63. elif self.unit == 31:
  64. unit_char = 'C1'
  65. # font = font_library["EN48"]
  66. elif self.unit == 32:
  67. unit_char = 'C2'
  68. # font = font_library["EN48"]
  69. self.send_led_msg(0, unit_char, en_font=font, cn_font=self.cn_font)
  70. def receive_dispatch_statu(self, statu, ex, key):
  71. self._dispatch_statu[key] = statu
  72. def get_parking_string(self, small_space_count, big_space_count):
  73. led_show_string = "剩大车位:%d\\n剩小车位:%d" % (big_space_count, small_space_count)
  74. return led_show_string
  75. def get_entrance_statu_led_string(self, small_space_count, big_space_count, pick_command_count):
  76. key = "dispatch_%d_statu_port" % self.unit
  77. led_show_string = ""
  78. if (key in self._dispatch_statu) is False or self._dispatch_statu[key].timeout():
  79. print('ERROR --- 调度节点未连接----key:%s---------time:%s' % (key, datetime.now()))
  80. led_show_string = "故障!!"
  81. else:
  82. dispatch_node_statu = message.dispatch_node_statu()
  83. try:
  84. tf.Parse(self._dispatch_statu[key].statu, dispatch_node_statu)
  85. if dispatch_node_statu.plc_carrier_status == 0:
  86. led_show_string = "故 障!"
  87. elif dispatch_node_statu.plc_carrier_status == 4:
  88. led_show_string = "维护中!"
  89. else:
  90. if big_space_count <= 0 and small_space_count <= 0:
  91. led_show_string = "无位 不可进\\n取车候车:%d" % pick_command_count
  92. elif big_space_count > 0 and (dispatch_node_statu.dispatch_plc_passway_status_vector[
  93. self.channel - 1].plc_passway_enable == 0 or
  94. dispatch_node_statu.dispatch_plc_passway_status_vector[
  95. self.channel - 1].plc_passway_enable == 2):
  96. led_show_string = "占用 不可进\\n取车候车:%d" % pick_command_count
  97. elif big_space_count > 0 and (dispatch_node_statu.dispatch_plc_passway_status_vector[
  98. self.channel - 1].plc_passway_enable == 1 or
  99. dispatch_node_statu.dispatch_plc_passway_status_vector[
  100. self.channel - 1].plc_passway_enable == 3):
  101. led_show_string = "空闲 可进入\\n取车候车:%d" % pick_command_count
  102. elif small_space_count > 0 and (dispatch_node_statu.dispatch_plc_passway_status_vector[
  103. self.channel - 1].plc_passway_enable == 0 or
  104. dispatch_node_statu.dispatch_plc_passway_status_vector[
  105. self.channel - 1].plc_passway_enable == 2):
  106. led_show_string = "占用 不可进\\n取车候车:%d" % pick_command_count
  107. elif small_space_count > 0 and (dispatch_node_statu.dispatch_plc_passway_status_vector[
  108. self.channel - 1].plc_passway_enable == 1 or
  109. dispatch_node_statu.dispatch_plc_passway_status_vector[
  110. self.channel - 1].plc_passway_enable == 3):
  111. led_show_string = "空闲 可进入\\n取车候车:%d" % pick_command_count
  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. return led_show_string
  117. def send_led_msg(self, area_index, input, en_font=font_library["EN32"], cn_font=font_library["CN32"]):
  118. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  119. sock.settimeout(1)
  120. try:
  121. sock.connect((self.led_communication['ip'], self.led_communication['port']))
  122. time.sleep(0.1)
  123. sock.send(self.led_protocol.string2bytes(area_index, input, en_font=en_font, cn_font=cn_font))
  124. print("connected led \033[0:32mSUCCEND\033[m ip:%s port:%d\nsend msg area_index:%d msg:%s" % (
  125. self.led_communication['ip'], self.led_communication['port'], area_index, input))
  126. except Exception as e:
  127. print("connected led \033[0:31mERROR\033[m: %s time:%s msg=%s" % (str(e.args), datetime.now(), input))
  128. time.sleep(0.1)
  129. sock.close()
  130. def run(self):
  131. i = 0
  132. while True:
  133. time.sleep(1)
  134. if i == 20:
  135. self.led_control_init()
  136. time.sleep(0.1)
  137. i=0
  138. i = i + 1
  139. small_space_res = self.db.query_space_in_height_unit_and_empty_2(1.48, 1.5, self.unit)
  140. big_space_res = self.db.query_space_in_height_unit_and_empty(2, self.unit)
  141. command_small_res = self.db.query_command_in_height_minmax_unit_and_statu(1.48, 1.5, self.unit)
  142. command_big_res = self.db.query_command_in_height_unit_and_statu(2, self.unit)
  143. pick_command_res = self.db.query_sort_pick_command(self.unit)
  144. if small_space_res is None or big_space_res is None or command_small_res is None or command_big_res is None or pick_command_res is None:
  145. continue
  146. small_space_count = len(small_space_res) - len(command_small_res)
  147. big_space_count = len(big_space_res) - len(command_big_res)
  148. pick_commandcount = len(pick_command_res)
  149. statu_string = self.get_entrance_statu_led_string(small_space_count, big_space_count, pick_commandcount)
  150. parking_string = self.get_parking_string(small_space_count, big_space_count)
  151. if self.area_1_show_str_timer.statu != statu_string or self.area_1_show_str_timer.timeout():
  152. self.area_1_show_str_timer = TimeStatu(statu_string, 20)
  153. self._pool.submit(self.send_led_msg, 1, statu_string,self.en_font,self.cn_font)
  154. # test
  155. # self._pool.submit(self.send_led_msg, 2, str(i),self.en_font,self.cn_font)
  156. # i = i + 1
  157. if self.area_2_show_str_timer.statu != parking_string or self.area_2_show_str_timer.timeout():
  158. self.area_2_show_str_timer = TimeStatu(parking_string,20)
  159. self._pool.submit(self.send_led_msg,2, parking_string,self.en_font,self.cn_font)