import datetime import os import threading import time import async_communication as CM import message_pb2 as message import google.protobuf.text_format as tf import check_command_XmSgj as CHE_XmSgj import check_command_GyBhhy as CHE_GyBhhy import tool.db_helper.db_operation as spmng import tool.json_helper.parse_json as parse_json import sys sys.path.append("..") from tool.log_helper.logger import HandleLog def check_park_command(body): # print("接收停车指令 %s message:%s" % (datetime.datetime.now(), body)) log.info("接收停车指令 %s message:%s" % (datetime.datetime.now(),body[0:120])) cmd = checker.entrance_isOK(body) response_port = "user_park_command_%d_response_port" % cmd.terminal_id dispatch_key = "" if cmd.statu.execute_statu == message.eNormal: # 指令检查正常 log.info("停车指令 %s OK: 车牌号:%s 检查成功!\n" % (datetime.datetime.now(), cmd.car_number)) dispatch_key = "park_command_request_port" g_rabbitmq.publish(mq_config['mq_command_exchange_name'], dispatch_key, tf.MessageToString(cmd, as_utf8=True)) else: if cmd.statu.execute_statu == message.eWarning: log.warning("停车指令 %s 车牌号:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % ( datetime.datetime.now(), cmd.car_number, mq_config['mq_command_exchange_name'], response_port + "," + dispatch_key, tf.MessageToString(cmd, as_utf8=True))) elif cmd.statu.execute_statu == message.eError: log.error("停车指令 %s 车牌号:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % ( datetime.datetime.now(), cmd.car_number, mq_config['mq_command_exchange_name'], response_port + "," + dispatch_key, tf.MessageToString(cmd, as_utf8=True))) else: log.critical("停车指令 %s 车牌号:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % ( datetime.datetime.now(), cmd.car_number, mq_config['mq_command_exchange_name'], response_port + "," + dispatch_key, tf.MessageToString(cmd, as_utf8=True))) g_rabbitmq.publish(mq_config['mq_command_exchange_name'], response_port, tf.MessageToString(cmd, as_utf8=True)) def check_pick_command(body): log.info("接收取车指令 %s message:%s" % (datetime.datetime.now(), body)) cmd = checker.exit_isOK(body) response_port = "user_pick_command_%d_response_port" % cmd.terminal_id dispatch_key = "" if cmd.statu.execute_statu == message.eNormal: # 指令检查正常 log.info("取车指令 %s OK : 唯一码:%s 检查成功!\n" % (datetime.datetime.now(), cmd.primary_key)) dispatch_key = "pick_command_request_port" g_rabbitmq.publish(mq_config['mq_command_exchange_name'], dispatch_key, tf.MessageToString(cmd, as_utf8=True)) else: if cmd.statu.execute_statu == message.eWarning: log.warning("取车指令 %s 唯一码:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % ( datetime.datetime.now(), cmd.primary_key, mq_config['mq_command_exchange_name'], response_port + "," + dispatch_key, tf.MessageToString(cmd, as_utf8=True))) elif cmd.statu.execute_statu == message.eError: log.error("取车指令 %s 唯一码:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % ( datetime.datetime.now(), cmd.primary_key, mq_config['mq_command_exchange_name'], response_port + "," + dispatch_key, tf.MessageToString(cmd, as_utf8=True))) else: log.critical("取车指令 %s 唯一码:%s 检查失败!\n 发送反馈 交换机:%s 发送反馈端口:%s 反馈表单:%s" % ( datetime.datetime.now(), cmd.primary_key, mq_config['mq_command_exchange_name'], response_port + "," + dispatch_key, tf.MessageToString(cmd, as_utf8=True))) if cmd.terminal_id != 0: g_rabbitmq.publish(mq_config['mq_command_exchange_name'], response_port, tf.MessageToString(cmd, as_utf8=True)) if __name__ == '__main__': config = parse_json.parse_json_with_comments('./config.json') # 数据库配置 db_config = config['db_config'] # rabbitmq配置 mq_config = config['mq_config'] # 日志记录 log = HandleLog.get_logger(os.getcwd()+'\\logs') g_space = spmng.DBOperation(db_config['db_ip'],db_config['db_port'],db_config['db_name'],db_config['db_user'],db_config['db_password']) # 消费指令消息 cmd_callbacks = [["user_park_command_request_queue", check_park_command], ["user_pick_command_request_queue", check_pick_command]] print("系统初始化中...... %s " % (datetime.datetime.now())) if config['project_name'] == 'xm_sgj': checker = CHE_XmSgj.CommandChecker(g_space) elif config['project_name'] == 'gy_bhhy': checker = CHE_GyBhhy.CommandChecker(g_space) print("系统初始化完成! %s " % (datetime.datetime.now())) g_rabbitmq = CM.RabbitAsyncCommunicator(mq_config['mq_ip'], mq_config['mq_port'], mq_config['mq_user'], mq_config['mq_password']) g_rabbitmq.Init(cmd_callbacks, mq_config['mq_statu_exchange_keys'],"checker") for ex,key in mq_config['mq_statu_exchange_keys']: if key.find("dispatch") >= 0: g_rabbitmq.bind_statu_callback(ex,key,checker.receive_dispatch_statu) if key.find("measure") >= 0: g_rabbitmq.bind_statu_callback(ex,key,checker.receive_measureInfo) g_rabbitmq.start() g_rabbitmq.join()