import Stomp from "stompjs";
import store from '../store'
// ============================一般使用的变量============================
let number = 0;
export const dataAdapterInit = initialize;
// ============================  大函数体   ============================
function initialize() {
    let adpClient = null;
    var url = "ws://192.168.2.3:8075/wisdom";
    adpClient = Stomp.client(url);
    adpClient.debug = null;
    adpClient.connect({}, adpClient => connectCallBackSubscribe(adpClient), error => reconnect(error, adpClient));
}
// 断线重连
function reconnect(error, adpClient) {
    //连接失败时再次调用函数
    number++;
    adpClient.connected = false;
    clearTimeout(setTimeout(initialize(), 1000 * 5));
    debugX("DataAdapter reconnect:" + number + " 次");
    return;
}
// ============================  订阅函数体  ============================
function connectCallBackSubscribe(adpClient) {
    console.warn(adpClient);
    console.warn('订阅函数体');
    number = 0;
    adpClient.connected = true;
    adpClient.subscribe("/topic/one", stompMessage => reflexWindturbineBasicInformation(stompMessage));
}
// ============================  解析函数体  ============================
function reflexWindturbineBasicInformation(stompMessage) {
    var windturbineInformationArray = JSON.parse(stompMessage.body);
    store.dispatch('updateWindturbineArray', windturbineInformationArray);
}
// ============================  其他  ============================
function debugX(text) {
    console.log(text);
}