import SDK.ZX.async_communication as cm
import SDK.ZX.async_communication_etc_pb2 as rce
import SDK.ZX.tool as zx_tool
import os
from rabbitmq_ui import *


if __name__ == '__main__':
    # 加载配置
    rabbitmq_etc = zx_tool.getProtobufJsonConfig(os.path.dirname(os.path.abspath(__file__)) + "/etc/rabbitmq.json",
                                                 rce.RabbitmqEtc())
    print(rabbitmq_etc)
    g_rabbitmq = cm.RabbitAsyncCommunicator(rabbitmq_etc.ip, rabbitmq_etc.port,
                                            rabbitmq_etc.user, rabbitmq_etc.password)
    statu_ex_keys = []
    for bind in rabbitmq_etc.binds:
        key = [bind.ex, bind.route_key]
        statu_ex_keys.append(key)

    # 初始化
    g_rabbitmq.Init(None, statu_ex_keys)
    g_rabbitmq.start()

    # rabbit_ui = RabbitmqUI()
    # rabbit_ui.run()
    while True:
        message = input("Input message: ")
        if message == "Q" or message == "q":
            break
        else:
            print(message)
        g_rabbitmq.publish(statu_ex_keys[0][0], statu_ex_keys[0][1], message)

    # 强制结束进程
    pid = os.getpid()
    os.kill(pid, 1)
