led_control_GyBhhy.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. import google.protobuf.text_format as tf
  10. import message_pb2 as message
  11. from led_protocol import LedProtocol
  12. from led_protocol import font_library
  13. class led_control(threading.Thread):
  14. def __init__(self, unit, channel,communication, area_list):
  15. threading.Thread.__init__(self)
  16. self._dispatch_statu = {}
  17. self.led_communication = communication
  18. self.area_list = area_list
  19. self.statu_string = ""
  20. self.parking_string = ""
  21. self.db =db
  22. self.unit = unit
  23. self.channel = channel
  24. self.led_protocol = LedProtocol(area_list)
  25. self._pool = ThreadPoolExecutor(max_workers=11)
  26. def led_control_init(self):
  27. unit_char = ""
  28. if self.unit == 1:
  29. unit_char = 'A'
  30. elif self.unit == 2:
  31. unit_char = 'B'
  32. elif self.unit == 11:
  33. unit_char = 'A1'
  34. elif self.unit == 12:
  35. unit_char = 'A2'
  36. elif self.unit == 13:
  37. unit_char = 'A3'
  38. elif self.unit == 14:
  39. unit_char = 'A4'
  40. elif self.unit == 21:
  41. unit_char = 'BA1'
  42. elif self.unit == 22:
  43. unit_char = 'B2'
  44. elif self.unit == 23:
  45. unit_char = 'B3'
  46. elif self.unit == 24:
  47. unit_char = 'B4'
  48. elif self.unit == 25:
  49. unit_char = 'B5'
  50. elif self.unit == 31:
  51. unit_char = 'C1'
  52. elif self.unit == 32:
  53. unit_char = 'C2'
  54. self.send_led_msg(0, unit_char,en_font=font_library["EN32"])
  55. def receive_dispatch_statu(self, statu, ex, key):
  56. self._dispatch_statu[key] = statu
  57. def get_parking_string(self,small_space_res,big_space_res):
  58. led_show_string = "剩大车位:%d\\n剩小车位:%d" % ( len(big_space_res), len(small_space_res))
  59. return led_show_string
  60. def get_entrance_statu_led_string(self,key,small_space_res,big_space_res):
  61. led_show_string = ""
  62. if (key in self._dispatch_statu) is False or self._dispatch_statu[key].timeout():
  63. print('ERROR --- 调度节点未连接----key:%s---------time:%s' % (key, datetime.now()))
  64. led_show_string = "故障!!"
  65. else:
  66. dispatch_node_statu = message.dispatch_node_statu()
  67. try:
  68. tf.Parse(self._dispatch_statu[key].statu, dispatch_node_statu)
  69. if dispatch_node_statu.plc_carrier_status == 0:
  70. led_show_string = "故障!!"
  71. elif dispatch_node_statu.plc_carrier_status == 4:
  72. led_show_string = "维护中!"
  73. else:
  74. if len(big_space_res)<=0 and len(small_space_res)<=0:
  75. led_show_string = "无位 不可进\\n限高: 0.00"
  76. elif len(big_space_res)>0 and dispatch_node_statu.dispatch_plc_passway_status_vector[self.channel-1].plc_passway_enable == 0:
  77. led_show_string = "占用 不可进\\n限高: 2.00"
  78. elif len(big_space_res)>0 and dispatch_node_statu.dispatch_plc_passway_status_vector[self.channel-1].plc_passway_enable == 1:
  79. led_show_string = "空闲 可进入\\n限高: 2.00"
  80. elif len(small_space_res)>0 and dispatch_node_statu.dispatch_plc_passway_status_vector[self.channel-1].plc_passway_enable == 0:
  81. led_show_string = "占用 不可进\\n限高: 1.50"
  82. elif len(small_space_res)>0 and dispatch_node_statu.dispatch_plc_passway_status_vector[self.channel-1].plc_passway_enable == 1:
  83. led_show_string = "空闲 可进入\\n限高: 1.50"
  84. except Exception:
  85. print('ERROR --- 调度状态消息解析错误 key:%s time:%s message:\n%s' % (
  86. key, datetime.now(), self._dispatch_statu[key].statu))
  87. led_show_string = "故障!!"
  88. return led_show_string
  89. def send_led_msg(self, area_index, input, en_font=font_library["EN16"], cn_font=font_library["CN16"]):
  90. t_time = datetime.now()
  91. while (datetime.now() - t_time).microseconds < 30000:
  92. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  93. sock.settimeout(1)
  94. try:
  95. sock.connect((self.led_communication['ip'], self.led_communication['port']))
  96. sock.send(self.led_protocol.string2bytes(area_index, input, en_font, cn_font))
  97. print("connected led \033[0:32mSUCCEND\033[m ip:%s port:%d\nsend msg area_index:%d msg:%s" % (
  98. self.led_communication['ip'], self.led_communication['port'], area_index, input))
  99. break
  100. except Exception as e:
  101. print("connected led \033[0:31mERROR\033[m: %s time:%s msg=%s" % (str(e.args),datetime.now(),input))
  102. time.sleep(0.01)
  103. sock.close()
  104. time.sleep(0.1)
  105. def run(self):
  106. self.led_control_init()
  107. key = "dispatch_%d_statu_port" % self.unit
  108. while True:
  109. small_space_res = self.db.query_space_in_height_unit_and_empty(1.48,self.unit)
  110. big_space_res = self.db.query_space_in_height_unit_and_empty(1.50,self.unit)
  111. statu_string = self.get_entrance_statu_led_string(key,small_space_res,big_space_res)
  112. if self.statu_string != statu_string:
  113. self.statu_string = statu_string
  114. self._pool.submit(self.send_led_msg,1, self.statu_string)
  115. parking_string = self.get_parking_string(small_space_res,big_space_res)
  116. if self.parking_string != parking_string:
  117. self.parking_string = parking_string
  118. # self.send_led_msg(2, self.parking_string)
  119. self._pool.submit(self.send_led_msg,2, self.parking_string)
  120. time.sleep(1)