import { defineStore } from 'pinia';
import io from 'socket.io-client'
import { getToken } from '/@/utils/auth';

interface SocketState {
  robotInfo?: any;
}

export const useThingsBoard = defineStore({
  id: 'app-thingsboard',
  state: (): SocketState => ({
    robotInfo: null,
  }),
  getters: {
    getRobotInfo(): any {
      return this.robotInfo || {};
    },
  },
  actions: {
    setRobotInfo(info: any) {
      this.robotInfo = info;
    },
    startThingsBoardSocket() {
      const socket = new WebSocket(`ws://222.190.143.210:8088/api/ws/plugins/telemetry?token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ4dzA4MTBAbGl2ZS5jb20iLCJzY29wZXMiOlsiVEVOQU5UX0FETUlOIl0sInVzZXJJZCI6IjU4ZjFkN2YwLWZjZDktMTFlYy1hMzMwLTJmMWU5ZTYxMzMxNyIsImZpcnN0TmFtZSI6IndlaSIsImxhc3ROYW1lIjoieGlvbmciLCJlbmFibGVkIjp0cnVlLCJpc1B1YmxpYyI6ZmFsc2UsInRlbmFudElkIjoiMzQ2N2JmODAtZmNkOS0xMWVjLWEzMzAtMmYxZTllNjEzMzE3IiwiY3VzdG9tZXJJZCI6IjEzODE0MDAwLTFkZDItMTFiMi04MDgwLTgwODA4MDgwODA4MCIsImlzcyI6InRoaW5nc2JvYXJkLmlvIiwiaWF0IjoxNjU4NzE1Mjg3LCJleHAiOjE2NTg3MjQyODd9._Bdau5ai5LhKepWF93XaqFTKFBkKjUikcMVXpgpj7IYZjeksP48j66pR4w-3rZHelaeCHd5yIcfUw6h6ciDpxg`);
      socket.onopen = () => {
        console.log('连接成功');
      };
      socket.onmessage = (event) => {
        console.log('Message from server ', event.data);
      };
      socket.onclose = () => {
        console.log("WebSocket is closed now.");
      };

      // const socket = io(`wss://101.34.171.164:8088/websocket?token=${getToken()}`);
      // //监听与服务器端的连接成功事件
      // socket.on('connect',function(){
      //   console.log('连接成功');
      //   socket.send('welcome');
      // });
      // //客户端收到服务器发过来的消息后触发
      // socket.on('message',function(message){
      //   console.log(message);
      // });
      // //监听与服务器端断开连接事件
      // socket.on('disconnect',function(){
      //   console.log('断开连接');
      // });
    },
  },
});