| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import axios from "axios"
- import { WebSocket } from "ws"
- // const {
- // data: { access_token }
- // } = await axios.post("http://47.98.225.28/api/auth/admin/login", {
- // username: "zoumaAdmin",
- // password: "WQ%B6j9uXKPvYf",
- // code: "123123",
- // bindingCode: ""
- // })
- const {
- data: { access_token }
- } = await axios.post("http://8.149.128.251/api/auth/admin/login", {
- username: "admin",
- password: "r&po^a9$Nc2AP9",
- code: "123123",
- bindingCode: ""
- })
- const axiosInstance = axios.create({
- // baseURL: "http://47.98.225.28/api",
- // headers: {
- // Authorization: `Bearer ${access_token}`
- // }
- baseURL: "http://8.149.128.251/api",
- headers: {
- Authorization: `Bearer ${access_token}`
- }
- })
- 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://192.168.6.165:22222")
- 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")
- console.log(xwDevices)
- if (xwDevices.length > 0) {
- let i = 1
- for (let device of devices) {
- const xwDevice = xwDevices.find(
- xwDevice => xwDevice.onlySerial === device.id
- )
- if (xwDevice) {
- console.log(device.name)
- if (device.name.startsWith("NJ-01")) {
- } else if (device.name.startsWith("NJ-02")) {
- } else if (device.name.startsWith("SZ-03")) {
- }
- 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")
- }
- }
|