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 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"]
terminal_id=config["terminal_id"]
import_id=config["import_id"]
unit=config["unit"]
prj_parameter=config.get("prj")
#启动入口单片机,向rabbitmq发送数据，并接收关门请求
#[消息服务器ip,port,用户，密码，状态交换机名称,状态消息key]
statu_ex_keys=[["statu_ex","dispatch_%d_statu_port"%unit],
               ["statu_ex","measure_%d_statu_port"%terminal_id],
               ["statu_ex","terminal_node_%d_statu_port"%terminal_id],
               ["statu_ex","entrance_update_%d_port"%terminal_id]

]
dispatch_ex,dispatch_key=statu_ex_keys[0]
measure_ex,measure_key=statu_ex_keys[1]
terminal_ex,terminal_key=statu_ex_keys[2]
entrance_ex,entrance_key=statu_ex_keys[3]

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()

#启动界面
#绑定单片机与界面
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",
        "检查杂物":"./Resource/arrows/检查杂物.png"}

app = QApplication(sys.argv)
desktop = QApplication.desktop()
frame = ws.Frame(import_id,images,prj_parameter,True)

frame.resize(600, 900)


frame.setWindowTitle("贵阳观山湖智能停车库 %d 引导"%terminal_id)
frame.setWindowFlags(Qt.WindowMaximizeButtonHint|Qt.WindowCloseButtonHint)
screen_count = desktop.screenCount()
screen = desktop.screen(screen_count-1)
frame.move(screen.geometry().x(), screen.geometry().y())
frame.showMaximized()
g_rabbitmq.bind_statu_callback(dispatch_ex,dispatch_key,frame.receive_dispatch)
g_rabbitmq.bind_statu_callback(measure_ex,measure_key,frame.receive_measureInfo)
g_rabbitmq.bind_statu_callback(terminal_ex,terminal_key,frame.receive_terminal)
g_rabbitmq.bind_statu_callback(entrance_ex,entrance_key,frame.receive_entrance_update)

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