/*
 * @Descripttion:
 * @version:
 * @Author: Eugene
 * @Date: 2023-10-12 09:55:52
 * @LastEditors: Andy
 * @LastEditTime: 2024-03-11 11:07:00
 */
var MQTT_MODE = 1;

function pageName() {
    var a = location.href;
    var b = a.split("/");
    var c = b.slice(b.length - 1, b.length).toString(String).split(".");
    return c[0];
}

var json_id = 0;

//在发送数据后调用这个函数
function MQTT_send(string) {
    json_id = localStorage.getItem("json_id");
    json_id++;
    if (json_id > 255) {
        json_id = 0;
    }
    localStorage.setItem("json_id", json_id);
    let send_string = "JSON_id\":" + json_id + ",\"board_id\":102" + ",\"led_protocol\":" + string ;
    console.log("MQTT 发送:" + send_string);

    if (/xazn/.test(navigator.userAgent) || /uni-app/.test(navigator.userAgent)) {
        uni.postMessage({
            data: {
                str: send_string
            }
        });
    } else {
        //这里需要将string发送出去
        window.parent.postMessage({
            data: send_string
        }, "*"); // 替换为Vue应用的URL
    }

}
//在发送数据后调用这个函数(带协议发送)
function MQTT_json_send(string, protocol) {
    json_id = localStorage.getItem("json_id");
    json_id++;
    if (json_id > 255) {
        json_id = 0;
    }
    let send_string;
    localStorage.setItem("json_id", json_id);
    if (protocol == "sound_card") {
        send_string = "{\"JSON_id\":" + json_id + ",\"board_id\":103" + ",\"" + protocol + "\":" + string + "}";

    } else {
        send_string = "{\"JSON_id\":" + json_id + ",\"" + protocol + "\":" + string + "}";

    }
    console.log("MQTT 发送:" + send_string);

    if (/xazn/.test(navigator.userAgent) || /uni-app/.test(navigator.userAgent)) {
        uni.postMessage({
            data: {
                str: send_string
            }
        });
    } else {
        //这里需要将string发送出去
        window.parent.postMessage({
            data: send_string
        }, "*"); // 替换为Vue应用的URL
    }
}
//在收到数据后需要调用这个函数
function MQTT_recv(string) {
    console.log("MQTT 接收:" + string);
    let json = jQuery.parseJSON(string);
    let href = pageName();
    if ("error_code" in json) {
        console.log("MQTT接收到回复码" + json.error_code);
    }
    var str = JSON.stringify(json);
    string = str;
    console.log("MQTT 处理后:" + string);
    if (href == "screen_main") {
        console.log("now -> screen_main");
        cmd_reply_parse_fun(string);
    }
    else if (href == "screen_par") {
        console.log("now -> screen_par");
        screen_par_reply_parse_fun(string);
    }
    else if (href == "screen_program") {
        console.log("now -> screen_program");
        program_list_reply_parse_fun(string);
    }
    else if (href == "screen_virtual") {
        console.log("now -> screen_virtual");
        screen_virtual_reply_parse_fun(string);
    }
    else if (href == "content_pattern1") {
        console.log("now -> content_pattern1");
        content_pattern_reply_parse_fun(string);
    }
    else if (href == "voice") {
        console.log("now -> voice");
        voice_reply_parse_fun(string);
    }
}
window.addEventListener('message', function (event) {
    console.log("接受的原始数据", JSON.stringify(event.data))
    if (event.data) {
        MQTT_recv(JSON.stringify(event.data));  // 保持与原 MQTT_recv 兼容 
    }
});