connect KilnInfo

This commit is contained in:
lb 2023-11-30 16:07:41 +08:00
parent bc03a717bb
commit af531456f4
3 changed files with 70 additions and 49 deletions

View File

@ -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" },

View File

@ -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(
<React.StrictMode>

View File

@ -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);
};
}
}