123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- import sys
- sys.path.append("..")
- # 导入pymysql
- import socket
- import sys
- import threading
- import time
- from datetime import datetime
- from config import MQ_CONFIG as mq_config
- import google.protobuf.text_format as tf
- import pymysql as psql
- import async_communication as CM
- from db_helper.db_operation import DBOperation
- import message_pb2 as message
- from config import LED_CONFIG as led_config
- from led_protocol import LedProtocol
- from led_protocol import font_library
- class led_control(threading.Thread):
- def __init__(self, unit, communication, area_list):
- threading.Thread.__init__(self)
- self._dispatch_statu = {}
- self.led_communication = communication
- self.area_list = area_list
- self.wait_command_dict = {}
- self.pick_command_dict = {}
- self.db = DBOperation()
- self.unit = unit
- self.led_protocol = LedProtocol(area_list)
- def led_control_init(self):
- unit_char = ""
- if self.unit == 11:
- unit_char = 'A1'
- elif self.unit == 12:
- unit_char = 'A2'
- elif self.unit == 13:
- unit_char = 'A3'
- elif self.unit == 14:
- unit_char = 'A4'
- elif self.unit == 21:
- unit_char = 'BA1'
- elif self.unit == 22:
- unit_char = 'B2'
- elif self.unit == 23:
- unit_char = 'B3'
- elif self.unit == 24:
- unit_char = 'B4'
- elif self.unit == 25:
- unit_char = 'B5'
- elif self.unit == 31:
- unit_char = 'C1'
- elif self.unit == 32:
- unit_char = 'C2'
- for area in self.area_list.keys():
- if area == 0:
- self.send_led_msg(area, unit_char,en_font=font_library["EN32"])
- def receive_dispatch_statu(self, statu, ex, key):
- self._dispatch_statu[key] = statu
- def get_wait_led_string(self,cmd_queue):
- if len(cmd_queue) == 0:
- return ""
- led_show_string = ""
- for num, cmd in zip(range(0, len(cmd_queue)), cmd_queue):
- led_show_string = led_show_string + "%d#%s " % (num + 1, cmd['car_number'])
- led_show_string = "候车:"+led_show_string
- return led_show_string
- def get_pick_led_string(self, cmd_queue):
- if len(cmd_queue) == 0:
- return "等待分配!"
- led_show_string = cmd_queue[0]['car_number']+" 取车中,请稍候!"
- return led_show_string
- def send_led_msg(self, area_index, input, en_font=font_library["EN16"], cn_font=font_library["CN16"]):
- t_time = datetime.now()
- while (datetime.now() - t_time).microseconds < 30000:
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.sock.settimeout(1)
- try:
- self.sock.connect((self.led_communication['ip'], self.led_communication['port']))
- self.sock.send(self.led_protocol.string2bytes(area_index, input, en_font, cn_font))
- print("connected led \033[0:32mSUCCEND\033[m ip:%s port:%d\nsend msg area_index:%d msg:%s" % (
- self.led_communication['ip'], self.led_communication['port'], area_index, input))
- break
- except Exception as e:
- print("connected led \033[0:31mERROR\033[m: %s time:%s msg=%s" % (str(e.args),datetime.now(),input))
- time.sleep(0.01)
- self.sock.close()
- time.sleep(0.1)
- def run(self):
- self.led_control_init()
- while True:
- wait_cmd_dict = self.db.query_pick_command_in_unit_and_statu(self.unit, 0)
- pick_cmd_dict = self.db.query_pick_command_in_unit_and_statu(self.unit, 1)
- if self.wait_command_dict != wait_cmd_dict:
- self.wait_command_dict = wait_cmd_dict
- led_show_string = self.get_wait_led_string(wait_cmd_dict)
- self.send_led_msg(1,led_show_string)
- key = "dispatch_%d_statu_port" % self.unit
- if (key in self._dispatch_statu) is False or self._dispatch_statu[key].timeout():
- print('ERROR --- 调度节点未连接----key:%s---------time:%s'%(key,datetime.now()))
- else:
- dispatch_node_statu = message.dispatch_node_statu()
- try:
- tf.Parse(self._dispatch_statu[key].statu, dispatch_node_statu)
- if dispatch_node_statu.plc_carrier_status == 0:
- self.send_led_msg(2, "故障!!!")
- elif dispatch_node_statu.plc_carrier_status == 4:
- self.send_led_msg(2, "维护中!")
- break
- except Exception:
- print('ERROR --- 调度状态消息解析错误 key:%s time:%s message:\n%s'% (key,datetime.now(),self._dispatch_statu[key].statu))
- if self.pick_command_dict != pick_cmd_dict:
- self.pick_command_dict = pick_cmd_dict
- led_show_string = self.get_pick_led_string(pick_cmd_dict)
- self.send_led_msg(2,led_show_string)
- time.sleep(1)
- if __name__ == "__main__":
- p_unit_id = led_config.unit
- p_channel = led_config.channel
- if len(sys.argv) == 3:
- p_unit_id = int(sys.argv[1])
- p_channel = int(sys.argv[2])
- g_rabbitmq = CM.RabbitAsyncCommunicator(mq_config.mq_ip, mq_config.mq_port, mq_config.mq_user, mq_config.mq_password)
- for list in mq_config.mq_statu_exchange_keys:
- if list[1].find(str(p_unit_id)) >= 0:
- g_rabbitmq.Init(None, [list],"export_%d_led"%p_channel)
- 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])
- for ex,key in mq_config.mq_statu_exchange_keys:
- g_rabbitmq.bind_statu_callback(ex,key,led.receive_dispatch_statu)
- g_rabbitmq.start()
- time.sleep(0.05)
- led.start()
- g_rabbitmq.join()
- led.join()
|