led_control_XmSgj.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. if self.unit == 31 or self.unit == 32:
  74. led_show_string = "剩高车位:%d\\n剩大车位:%d" % (big_space_count, small_space_count)
  75. else:
  76. led_show_string = "剩大车位:%d\\n剩小车位:%d" % (big_space_count, small_space_count)
  77. return led_show_string
  78. def get_entrance_statu_led_string(self, small_space_count, big_space_count, pick_command_count):
  79. key = "dispatch_%d_statu_port" % self.unit
  80. led_show_string = ""
  81. if (key in self._dispatch_statu) is False or self._dispatch_statu[key].timeout():
  82. print('ERROR --- 调度节点未连接----key:%s---------time:%s' % (key, datetime.now()))
  83. led_show_string = "故障!!"
  84. else:
  85. dispatch_node_statu = message.dispatch_node_statu()
  86. try:
  87. tf.Parse(self._dispatch_statu[key].statu, dispatch_node_statu)
  88. if dispatch_node_statu.plc_carrier_status == 0:
  89. led_show_string = "故 障!"
  90. elif dispatch_node_statu.plc_carrier_status == 4:
  91. led_show_string = "维护中!"
  92. else:
  93. if big_space_count <= 0 and small_space_count <= 0:
  94. led_show_string = "无位 不可进\\n取车候车:%d" % pick_command_count
  95. elif big_space_count > 0 and (dispatch_node_statu.dispatch_plc_passway_status_vector[
  96. self.channel - 1].plc_passway_enable == 0 or
  97. dispatch_node_statu.dispatch_plc_passway_status_vector[
  98. self.channel - 1].plc_passway_enable == 2):
  99. led_show_string = "占用 不可进\\n取车候车:%d" % pick_command_count
  100. elif big_space_count > 0 and (dispatch_node_statu.dispatch_plc_passway_status_vector[
  101. self.channel - 1].plc_passway_enable == 1 or
  102. dispatch_node_statu.dispatch_plc_passway_status_vector[
  103. self.channel - 1].plc_passway_enable == 3):
  104. led_show_string = "空闲 可进入\\n取车候车:%d" % pick_command_count
  105. elif small_space_count > 0 and (dispatch_node_statu.dispatch_plc_passway_status_vector[
  106. self.channel - 1].plc_passway_enable == 0 or
  107. dispatch_node_statu.dispatch_plc_passway_status_vector[
  108. self.channel - 1].plc_passway_enable == 2):
  109. led_show_string = "占用 不可进\\n取车候车:%d" % pick_command_count
  110. elif small_space_count > 0 and (dispatch_node_statu.dispatch_plc_passway_status_vector[
  111. self.channel - 1].plc_passway_enable == 1 or
  112. dispatch_node_statu.dispatch_plc_passway_status_vector[
  113. self.channel - 1].plc_passway_enable == 3):
  114. led_show_string = "空闲 可进入\\n取车候车:%d" % pick_command_count
  115. except Exception:
  116. print('ERROR --- 调度状态消息解析错误 key:%s time:%s message:\n%s' % (
  117. key, datetime.now(), self._dispatch_statu[key].statu))
  118. led_show_string = "故 障!"
  119. return led_show_string
  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. def run(self):
  134. i = 0
  135. while True:
  136. time.sleep(1)
  137. if i == 20:
  138. self.led_control_init()
  139. time.sleep(0.1)
  140. i=0
  141. i = i + 1
  142. if self.unit == 31 or self.unit == 32:
  143. small_space_res = self.db.query_space_in_height_unit_and_empty_2(1.8, 2, self.unit)
  144. else:
  145. small_space_res = self.db.query_space_in_height_unit_and_empty_2(1.48, 1.5, self.unit)
  146. big_space_res = self.db.query_space_in_height_unit_and_empty(2, self.unit)
  147. # command_small_res = self.db.query_command_in_height_minmax_unit_and_statu(1.48, 1.5, self.unit)
  148. # command_big_res = self.db.query_command_in_height_unit_and_statu(2, self.unit)
  149. pick_command_res = self.db.query_sort_pick_command(self.unit)
  150. # 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:
  151. # continue
  152. if small_space_res is None or big_space_res is None or pick_command_res is None:
  153. continue
  154. # small_space_count = len(small_space_res) - len(command_small_res)
  155. # big_space_count = len(big_space_res) - len(command_big_res)
  156. # pick_commandcount = len(pick_command_res)
  157. small_space_count = len(small_space_res)
  158. big_space_count = len(big_space_res)
  159. pick_commandcount = len(pick_command_res)
  160. statu_string = self.get_entrance_statu_led_string(small_space_count, big_space_count, pick_commandcount)
  161. parking_string = self.get_parking_string(small_space_count, big_space_count)
  162. if self.area_1_show_str_timer.statu != statu_string or self.area_1_show_str_timer.timeout():
  163. self.area_1_show_str_timer = TimeStatu(statu_string, 20)
  164. self._pool.submit(self.send_led_msg, 1, statu_string,self.en_font,self.cn_font)
  165. # test
  166. # self._pool.submit(self.send_led_msg, 2, str(i),self.en_font,self.cn_font)
  167. # i = i + 1
  168. if self.area_2_show_str_timer.statu != parking_string or self.area_2_show_str_timer.timeout():
  169. self.area_2_show_str_timer = TimeStatu(parking_string,20)
  170. self._pool.submit(self.send_led_msg,2, parking_string,self.en_font,self.cn_font)