name.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import axios from "axios"
  2. import { WebSocket } from "ws"
  3. // const {
  4. // data: { access_token }
  5. // } = await axios.post("http://47.98.225.28/api/auth/admin/login", {
  6. // username: "zoumaAdmin",
  7. // password: "WQ%B6j9uXKPvYf",
  8. // code: "123123",
  9. // bindingCode: ""
  10. // })
  11. const {
  12. data: { access_token }
  13. } = await axios.post("http://8.149.128.251/api/auth/admin/login", {
  14. username: "admin",
  15. password: "r&po^a9$Nc2AP9",
  16. code: "123123",
  17. bindingCode: ""
  18. })
  19. const axiosInstance = axios.create({
  20. // baseURL: "http://47.98.225.28/api",
  21. // headers: {
  22. // Authorization: `Bearer ${access_token}`
  23. // }
  24. baseURL: "http://8.149.128.251/api",
  25. headers: {
  26. Authorization: `Bearer ${access_token}`
  27. }
  28. })
  29. const {
  30. data: { items: devices }
  31. } = await axiosInstance.post("/device", {
  32. page: { page: 1, limit: 1000 },
  33. search: { where: { online: true }, order: { name: "ASC" } }
  34. })
  35. const ws = new WebSocket("ws://192.168.6.165:22222")
  36. let xwDevices = []
  37. ws.onopen = () => {
  38. console.log("Connected to WebSocket")
  39. ws.send(
  40. JSON.stringify({
  41. action: "list"
  42. })
  43. )
  44. console.log("Requesting device list")
  45. setTimeout(() => {
  46. console.log("Updating device names")
  47. console.log(xwDevices)
  48. if (xwDevices.length > 0) {
  49. let i = 1
  50. for (let device of devices) {
  51. const xwDevice = xwDevices.find(
  52. xwDevice => xwDevice.onlySerial === device.id
  53. )
  54. if (xwDevice) {
  55. console.log(device.name)
  56. if (device.name.startsWith("NJ-01")) {
  57. } else if (device.name.startsWith("NJ-02")) {
  58. } else if (device.name.startsWith("SZ-03")) {
  59. }
  60. ws.send(
  61. JSON.stringify({
  62. action: "updateDevices",
  63. devices: xwDevice.serial,
  64. data: {
  65. sort: i++,
  66. name: device.name
  67. }
  68. })
  69. )
  70. }
  71. }
  72. } else {
  73. console.log("No device found")
  74. }
  75. }, 2000)
  76. }
  77. ws.onmessage = e => {
  78. const data = JSON.parse(e.data)
  79. if (data.code === 10000 && data.data instanceof Array) {
  80. xwDevices = data.data
  81. console.log("found " + xwDevices.length + " devices")
  82. }
  83. }