# -*- coding:utf-8 -*-
"""
@Author  : xuxingchen
@Contact : xuxingchen@sinochem.com
@Desc    : mqtt后端服务程序启动入口
"""
import time

from config import BROKER_HOST, BROKER_PORT, BROKER_USERNAME, BROKER_PASSWD
from utils.misc import create_mqtt_client, UserData, on_connect, on_disconnect, on_publish
from device.services import on_message


def main_mqtt():
    client_dict = {}
    userdata = UserData()
    userdata.set_topics([
        "/jmlink/+/comm/register",
        "/jmlink/+/comm/sub/register",
        "/jmlink/+/comm/online",
        "/jmlink/+/comm/offline",
        "/jmlink/+/comm/post",
        "/jmlink/+/tml/event/post",
        "/jmlink/+/tml/property/post"
    ])
    userdata.set_clients(client_dict)
    client = create_mqtt_client(BROKER_HOST, BROKER_PORT,
                                userdata, on_message, on_publish, on_connect, on_disconnect,
                                "backend@python", username=BROKER_USERNAME, password=BROKER_PASSWD)
    client.loop_start()
    client_dict["center"] = [client, userdata]

    while True:
        time.sleep(86400)


if __name__ == "__main__":
    main_mqtt()
