import sys
from PyQt5.QtWidgets import QWidget, QApplication,QMainWindow,QLabel
from PyQt5.QtCore import Qt
import window_screen_pyqt as ws
import async_communication as CM
import led
import DatabaseSearch as DB
import json
import google.protobuf.text_format as tf
import message_pb2 as message

with open("./ui_config.json",'r',encoding='utf-8') as fp:
    config=json.load(fp)
led_cfg=config["led"]
db_cfg=config["db"]
rabbitmq_cfg=config["rabbitmq"]
node_id=config["entrance_id"]
unit=config["unit"]
prj_parameter=config.get("prj")


#启动入口单片机,向rabbitmq发送数据，并接收关门请求
#[消息服务器ip,port,用户，密码，状态交换机名称,状态消息key]
statu_ex_keys=[["statu_ex","in_mcpu_%d_statu_port"%node_id],
               ["statu_ex","measure_%d_statu_port"%node_id],
               ["statu_ex","terminal_node_%d_statu_port"%node_id]
]
cpu_ex,cpu_key=statu_ex_keys[0]
measure_ex,measure_key=statu_ex_keys[1]
terminal_ex,terminal_key=statu_ex_keys[2]

g_rabbitmq=CM.RabbitAsyncCommunicator(rabbitmq_cfg["ip"],rabbitmq_cfg["port"],
                         rabbitmq_cfg["user"],rabbitmq_cfg["password"])
g_rabbitmq.Init(None,statu_ex_keys)
g_rabbitmq.start()

#启动led
led=led.EntranceLED(led_cfg["ip"],led_cfg['port'])
def db_callback(suv,small):
    idle = False
    time_statu = g_rabbitmq._status.get(cpu_ex+":"+cpu_key)
    if not time_statu==None:
        if not time_statu.statu==None:
            icpu_statu = message.in_mcpu_statu()
            tf.Parse(time_statu.statu, icpu_statu)
            idle = icpu_statu.is_occupy == 2
    led.fresh_led(suv,small,idle)
#启动数据库，绑定led与数据库
DBSearcher=DB.DBSeacher(db_cfg["ip"],db_cfg["port"],db_cfg["database"],
                        db_cfg["user"],db_cfg["password"],unit)
DBSearcher.set_ledcallback(db_callback)
DBSearcher.start()

#启动界面
#绑定单片机与界面
images={"传感器超时":"./resource/arrows/传感器超时.png",
          "测绘超时":"./resource/arrows/测绘超时.png",
          "正确文字":"./resource/arrows/正确文字.png",
          "正确图片":"./resource/arrows/正确图片.png",
        "超宽车辆":"./resource/arrows/超宽车辆.png",
        "轴距超差":"./resource/arrows/轴距超差.png",
        "请调整":"./resource/arrows/请调整.png",
        "向前调整":"./resource/arrows/向前调整.png",
        "向后调整":"./resource/arrows/向后调整.png",
        "向左调整":"./resource/arrows/向左调整.png",
        "向右调整":"./resource/arrows/向右调整.png",
        "左前调整":"./resource/arrows/左前调整.png",
        "左后调整":"./resource/arrows/左后调整.png",
        "右前调整":"./resource/arrows/右前调整.png",
        "右后调整":"./resource/arrows/右后调整.png",
        "向左旋转":"./resource/arrows/向左旋转.png",
        "向右旋转":"./resource/arrows/向右旋转.png",
        "空闲":"./resource/arrows/空闲.png",
        "回正方向盘":"./resource/arrows/回正方向盘.png",
        "检查杂物":"./resource/arrows/检查杂物.png"}

app = QApplication(sys.argv)
frame = ws.Frame(images,prj_parameter,True)
frame.resize(600, 900)
frame.setWindowTitle("楚天大厦入口 %d 引导"%node_id)
frame.show()
frame.setWindowFlags(Qt.WindowMaximizeButtonHint|Qt.WindowCloseButtonHint)
frame.showMaximized()
g_rabbitmq.bind_statu_callback(cpu_ex,cpu_key,frame.receive_icpu)
g_rabbitmq.bind_statu_callback(measure_ex,measure_key,frame.receive_measureInfo)
g_rabbitmq.bind_statu_callback(terminal_ex,terminal_key,frame.receive_terminal)

app.exec()
DBSearcher.close()
g_rabbitmq.close()
DBSearcher.join()
g_rabbitmq.join()
