import sys
sys.path.append("..")
import sys
import time
import mytool.json_helper.parse_json as parse_json
import  async_communication as CM
import led_control_XmSgj as led_XmSgj
import led_control_GyBhhy as led_GyBhhy

if __name__ == "__main__":
    # 读取文件
    config = parse_json.parse_json_with_comments('./config.json')
    # 数据库配置
    db_config = config['db_config']
    # rabbitmq配置
    mq_config = config['mq_config']
    # LED配置
    led_config = config['led_config']
    led_list_all_config = led_config['led_list_all']
    area_list_all_config = led_config['area_list_all']

    p_unit_id = led_config['unit']
    p_channel = led_config['channel']
    if len(sys.argv) == 3:
        p_unit_id = int(sys.argv[1])
        p_channel = int(sys.argv[2])

    g_rabbitmq = CM.RabbitAsyncCommunicator(mq_config['mq_ip'], mq_config['mq_port'], mq_config['mq_user'], mq_config['mq_password'])
    for list in mq_config['mq_statu_exchange_keys']:
        if list[1].find(str(p_unit_id)) >= 0:
            g_rabbitmq.Init(None, [list],"export_%d_led"%p_channel)


    if config['project_name'] == 'xm_sgj':
        led = led_XmSgj.led_control(p_unit_id, p_channel,led_list_all_config[str(p_unit_id)][p_channel - 1],
                          area_list_all_config[str(p_unit_id)][p_channel - 1], db_config)
    elif config['project_name'] == 'gy_bhhy':
        led = led_GyBhhy.led_control(p_unit_id,p_channel, led_list_all_config[str(p_unit_id)][p_channel - 1],
                          area_list_all_config[str(p_unit_id)][p_channel - 1], db_config)
    led.start()
    for ex,key in mq_config['mq_statu_exchange_keys']:
        g_rabbitmq.bind_statu_callback(ex,key,led.receive_dispatch_statu)
    g_rabbitmq.start()

    g_rabbitmq.join()
    led.join()
