import axios from "axios" import { WebSocket } from "ws" const axiosInstance = axios.create({ baseURL: "http://47.98.225.28/api", headers: { Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InpvdW1hQWRtaW4iLCJzdWIiOjEsInJvbGVzIjpbImFkbWluIl0sImlhdCI6MTcyNjU4MTk1OH0.dQiHFYaDDa1qp4OpEHaH5SCZ9dafJ8uO9fAAc8HZgAo" } }) const { data: { items: devices } } = await axiosInstance.post("/device", { page: { page: 1, limit: 1000 }, search: { where: { online: true }, order: { name: "ASC" } } }) const ws = new WebSocket("ws://121.40.132.44:6001") let xwDevices = [] ws.onopen = () => { console.log("Connected to WebSocket") ws.send( JSON.stringify({ action: "list" }) ) console.log("Requesting device list") setTimeout(() => { console.log("Updating device names") if (xwDevices.length > 0) { let i = 101 for (let device of devices) { const xwDevice = xwDevices.find( xwDevice => xwDevice.onlySerial === device.id ) if (xwDevice) { console.log(device.name) ws.send( JSON.stringify({ action: "updateDevices", devices: xwDevice.serial, data: { sort: i++, name: device.name } }) ) } } } else { console.log("No device found") } }, 2000) } ws.onmessage = e => { const data = JSON.parse(e.data) if (data.code === 10000 && data.data instanceof Array) { xwDevices = data.data console.log("found " + xwDevices.length + " devices") } }