2023-11-30 16:07:41 +08:00
|
|
|
import { store } from "../store";
|
|
|
|
|
2023-11-09 13:36:21 +08:00
|
|
|
export function randomInt(min, max, includeMax = false) {
|
2023-11-30 16:07:41 +08:00
|
|
|
let Fn = includeMax ? Math.ceil : Math.floor;
|
|
|
|
let num = Fn(Math.random() * max);
|
|
|
|
while (num < min) {
|
|
|
|
num = Fn(Math.random() * max);
|
|
|
|
}
|
|
|
|
return num;
|
2023-11-09 13:36:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export class WsClient {
|
2023-11-30 16:40:02 +08:00
|
|
|
static wsServer = "ws://192.168.1.12:8081/xc-screen/websocket/xc001";
|
2023-11-30 16:07:41 +08:00
|
|
|
static socket = null;
|
|
|
|
static tryCount = 0;
|
2023-11-09 13:36:21 +08:00
|
|
|
|
2023-11-30 16:07:41 +08:00
|
|
|
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;
|
|
|
|
}
|
2023-11-30 16:40:02 +08:00
|
|
|
case "CombustionAirInfo": {
|
|
|
|
// 助燃风流量 实时
|
|
|
|
store.dispatch({
|
|
|
|
type: "combustionAir/setRuntime",
|
|
|
|
payload: serializedData.data.combustionAirNow, // []
|
|
|
|
});
|
|
|
|
// 助燃风流量 历史
|
|
|
|
store.dispatch({
|
|
|
|
type: "combustionAir/setHistory",
|
|
|
|
payload: serializedData.data.combustionAirHis, // {}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2023-11-30 17:01:30 +08:00
|
|
|
case "GasInfo": {
|
|
|
|
// 天然气流量 1 实时
|
|
|
|
store.dispatch({
|
2023-12-01 09:31:44 +08:00
|
|
|
type: 'natGas/setGasIRuntime',
|
2023-11-30 17:01:30 +08:00
|
|
|
payload: serializedData.data.gas1Now
|
|
|
|
})
|
|
|
|
// 天然气流量 1 历史
|
|
|
|
store.dispatch({
|
2023-12-01 09:31:44 +08:00
|
|
|
type: 'natGas/setGasIHistory',
|
2023-11-30 17:01:30 +08:00
|
|
|
payload: serializedData.data.hisGas1
|
|
|
|
})
|
|
|
|
// 天然气流量 2 实时
|
|
|
|
store.dispatch({
|
2023-12-01 09:31:44 +08:00
|
|
|
type: 'natGas/setGasIIRuntime',
|
2023-11-30 17:01:30 +08:00
|
|
|
payload: serializedData.data.gas2Now
|
|
|
|
})
|
|
|
|
// 天然气流量 2 历史
|
|
|
|
store.dispatch({
|
2023-12-01 09:31:44 +08:00
|
|
|
type: 'natGas/setGasIIHistory',
|
2023-11-30 17:01:30 +08:00
|
|
|
payload: serializedData.data.hisGas2
|
|
|
|
})
|
|
|
|
break;
|
|
|
|
}
|
2023-11-30 16:07:41 +08:00
|
|
|
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) {
|
2023-11-30 16:40:02 +08:00
|
|
|
new WsClient();
|
2023-11-30 16:07:41 +08:00
|
|
|
WsClient.tryCount += 1;
|
|
|
|
} else clearTimeout(timer);
|
|
|
|
}, 30000);
|
|
|
|
};
|
|
|
|
}
|
2023-11-09 13:36:21 +08:00
|
|
|
}
|