diff --git a/src/components/Common/KilnInfo/Kiln.jsx b/src/components/Common/KilnInfo/Kiln.jsx index fdb94cc..9910be9 100644 --- a/src/components/Common/KilnInfo/Kiln.jsx +++ b/src/components/Common/KilnInfo/Kiln.jsx @@ -6,30 +6,30 @@ import { stateNameMap } from "../../../store/features/kilnSlice"; export default function Kiln() { const kilnInfo = useSelector((state) => state.kiln); - const dispatch = useDispatch(); + // const dispatch = useDispatch(); const infos = Object.keys(kilnInfo).map((key) => ({ label: stateNameMap[key], value: kilnInfo[key], })); - useEffect(() => { - setInterval(() => { - dispatch({ - type: "kiln/setKilnInfo", - payload: { - kilnPressure: Math.ceil(Math.random() * 100) + "Pa", - waterTemp: Math.ceil(Math.random() * 100) + "℃", - waterFlow: Math.ceil(Math.random() * 100) + "m³/h", - waterPressure: Math.ceil(Math.random() * 100) + "Pa", - combustionAirPressure: Math.ceil(Math.random() * 100) + "Pa", - topTemp: Math.ceil(Math.random() * 100) + "℃", - compressedAirPressure: Math.ceil(Math.random() * 100) + "Pa", - meltTemp: Math.ceil(Math.random() * 100) + "℃", - }, - }); - }, 30000); - }, [dispatch]); + // useEffect(() => { + // setInterval(() => { + // dispatch({ + // type: "kiln/setKilnInfo", + // payload: { + // kilnPressure: Math.ceil(Math.random() * 100) + "Pa", + // waterTemp: Math.ceil(Math.random() * 100) + "℃", + // waterFlow: Math.ceil(Math.random() * 100) + "m³/h", + // waterPressure: Math.ceil(Math.random() * 100) + "Pa", + // combustionAirPressure: Math.ceil(Math.random() * 100) + "Pa", + // topTemp: Math.ceil(Math.random() * 100) + "℃", + // compressedAirPressure: Math.ceil(Math.random() * 100) + "Pa", + // meltTemp: Math.ceil(Math.random() * 100) + "℃", + // }, + // }); + // }, 30000); + // }, [dispatch]); // const infos = [ // { label: "窑炉压力", value: ctx?.runState?.kilnPressure || "0Pa" }, diff --git a/src/index.js b/src/index.js index c4c6fc2..365bbbe 100644 --- a/src/index.js +++ b/src/index.js @@ -4,9 +4,11 @@ import { Provider } from "react-redux"; import { store } from "./store"; import App from "./App"; import "./index.css"; +import { WsClient } from "./utils"; const container = document.getElementById("root"); const root = createRoot(container); +const wsc = new WsClient(); root.render( diff --git a/src/utils/index.js b/src/utils/index.js index c9a86e7..858223f 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,37 +1,56 @@ +import { store } from "../store"; + export function randomInt(min, max, includeMax = false) { - let Fn = includeMax ? Math.ceil : Math.floor; - let num = Fn(Math.random() * max); - while (num < min) { - num = Fn(Math.random() * max); - } - return num; + let Fn = includeMax ? Math.ceil : Math.floor; + let num = Fn(Math.random() * max); + while (num < min) { + num = Fn(Math.random() * max); + } + return num; } export class WsClient { - static wsServer = 'ws://192.168.1.12:8081/xc-screen/websocket/1'; - static socket = null; - static tryCount = 0; + static wsServer = "ws://192.168.1.12:8081/xc-screen/websocket/info"; + static socket = null; + static tryCount = 0; - init() { - if (WsClient.socket) return; - WsClient.socket = new WebSocket(WsClient.wsServer); - WsClient.socket.onopen = () => { - console.log('[*] websocket connected!'); - }; - WsClient.socket.onmessage = (e) => { - console.log('[*] websocket message!', e, e.data); - }; - WsClient.socket.onerror = (e) => { - console.log('[*] websocket erro!', e, e.data); - }; - WsClient.socket.onclose = (e) => { - let timer = setTimeout(() => { - if (WsClient.tryCount < 3) { - const wsc = new WsClient(); - wsc.init(); - WsClient.tryCount += 1; - } else clearTimeout(timer); - }, 30000); - }; - } + constructor() { + if (WsClient.socket) return; + WsClient.socket = new WebSocket(WsClient.wsServer); + WsClient.socket.onopen = () => { + console.log("[*] websocket connected!"); + }; + WsClient.socket.onmessage = (e) => { + let serializedData = null; + try { + serializedData = JSON.parse(e.data); + } catch (error) { + console.log("[*] websocket: [unable to serialize] ---> ", e); + } + switch (serializedData?.type) { + case "KilnInfo": { + store.dispatch({ + type: "kiln/setKilnInfo", + payload: serializedData.data, + }); + break; + } + default: { + console.log("websocket message: [unknown] ---> ", e.data); + } + } + }; + WsClient.socket.onerror = (e) => { + console.log("[*] websocket error!", e, e.data); + }; + WsClient.socket.onclose = (e) => { + let timer = setTimeout(() => { + if (WsClient.tryCount < 3) { + const wsc = new WsClient(); + wsc.init(); + WsClient.tryCount += 1; + } else clearTimeout(timer); + }, 30000); + }; + } }