import Vue from 'vue'
import App from './App.vue'
import router from './router'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
import Plugin from 'v-fit-columns';
Vue.use(Plugin);
import * as echarts from 'echarts'
import 'echarts-gl'
Vue.prototype.$echarts = echarts



import moment from 'moment'
//定义一个全局过滤器实现日期格式化
Vue.filter('datefmt', function (input, fmtstring) {//当input为时间戳时，需转为Number类型
  // 使用momentjs这个日期格式化类库实现日期的格式化功能
  return moment(input).format(fmtstring);
});
Vue.prototype.$moment = moment

import 'amfe-flexible'; //引入amfe-flexible做rem适配
import './assets/index.css';
import mqttConfig from './utils/mqttConfig.js';
Vue.config.productionTip = false
//mqtt订阅
var currentTopics = null;
var client = null;  //mqtt客户端连接
var callback = null; //回调
let mqtt = new mqttConfig(currentTopics); // 初始化mqtt
//订阅mqtt
window.PubScribe = function (topic, _callback,) {
  callback = _callback;
  if (currentTopics != null) {
    //取消currentTopics主题订阅
    if (client != null) {
      client.unsubscribe(currentTopics);
      currentTopics = null;
    }
  }
  if (client == null) {
    client = mqtt.createConnect(() => {
      //客户端订阅主题
      client.subscribe(['FirstLevel','SecondLevel','ThirdLevel'], {
        qos: 0
      }, (err) => {
        if (!err) {
          //console.log("订阅成功0", ['FirstLevel','SecondLevel','ThirdLevel']);
        } else {
          //console.log('订阅失败');
        }
      });

      client.on("message", (topic, message) => {
        //数据分类
        try {
          callback(topic, message);
        } catch (error) {

        }
      });
    });
  } else {
    // if (topic != null) {
    //   console.log("topic", topic)
    //   currentTopics = topic;
    //   client.subscribe(currentTopics, {
    //     qos: 0
    //   }, (err) => {
    //     if (!err) {
    //       console.log("订阅成功1");
    //     } else {
    //       console.log('消息订阅失败！')
    //     }
    //   });
    // }
  }
}
window.PubScribe(null,realInfo);
window.alarmAllLevel = []
window.alarmFirstLevel = []
window.alarmSecondLevel = []
window.alarmThirdLevel = []
function realInfo(topic, message) {
      switch (topic) {
        // 接收托片
        case "FirstLevel":
          try {
            // window.alarmFirstLevel= []
            const utf8decoder = new TextDecoder();
            const u8arr = new Uint8Array(message);
            const temp = utf8decoder.decode(u8arr); // 将二进制数据转为字符串
            const msg = JSON.parse(temp); //这一步报错则返回的是二进制流图片，不报错则返回的是JSON的错误提示数据
            
            for (let i = 0; i < msg.length; i++) {
                window.alarmFirstLevel.splice(0,window.alarmFirstLevel.length);
                window.alarmFirstLevel.push(msg[i])
            }
            // window.alarmFirstLevel= msg
            // alarmAllLevel
        //消防，电梯、动环系统
          } catch (error) {}
          break;
           case "SecondLevel":
          try {
            const utf8decoder = new TextDecoder();
            const u8arr = new Uint8Array(message);
            const temp = utf8decoder.decode(u8arr); // 将二进制数据转为字符串
            const msg = JSON.parse(temp); //这一步报错则返回的是二进制流图片，不报错则返回的是JSON的错误提示数据
            for (let i = 0; i < msg.length; i++) {
                window.alarmSecondLevel.splice(0,window.alarmSecondLevel.length);
                window.alarmSecondLevel.push(msg[i])
            }
          } catch (error) {}
          break;
          case "ThirdLevel":
          try {
            const utf8decoder = new TextDecoder();
            const u8arr = new Uint8Array(message);
            const temp = utf8decoder.decode(u8arr); // 将二进制数据转为字符串
            const msg = JSON.parse(temp); //这一步报错则返回的是二进制流图片，不报错则返回的是JSON的错误提示数据

            for (let i = 0; i < msg.length; i++) {
                window.alarmThirdLevel.splice(0,window.alarmThirdLevel.length);
                window.alarmThirdLevel.push(msg[i])
            }
          } catch (error) {}
          break;
      }
    }
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
