diff --git a/src/utils/index.js b/src/utils/index.js index dd4fbd2..fa44b63 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -9,140 +9,167 @@ export function randomInt(min, max, includeMax = false) { return num; } -export class WsClient { - static wsServer = "ws://192.168.1.12:8081/xc-screen/websocket/xc001"; - static socket = null; - static tryCount = 0; - - constructor() { - if (WsClient.socket) return; - WsClient.socket = new WebSocket(WsClient.wsServer); - WsClient.socket.onopen = () => { - console.log("[*] websocket connected!"); +/** + * new XClient('ws://192.168.1.12:8081/xc-screen/websocket/xc001', 'DCS') + */ +class XClient { + constructor(url, name, onmessage) { + this.url = url; + this.name = name; + this.ws = new WebSocket(url); + this.ws.onopen = () => { + console.log(`[*] ${this.name} ws 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; - } - case "CombustionAirInfo": { - // 助燃风流量 实时 - store.dispatch({ - type: "combustionAir/setRuntime", - payload: serializedData.data.combustionAirNow, // [] - }); - // 助燃风流量 历史 - store.dispatch({ - type: "combustionAir/setHistory", - payload: serializedData.data.combustionAirHis, // {} - }); - break; - } - case "GasInfo": { - // 天然气流量 1 实时 - store.dispatch({ - type: "natGas/setGasIRuntime", - payload: serializedData.data.gas1Now, - }); - // 天然气流量 1 历史 - store.dispatch({ - type: "natGas/setGasIHistory", - payload: serializedData.data.hisGas1, - }); - // 天然气流量 2 实时 - store.dispatch({ - type: "natGas/setGasIIRuntime", - payload: serializedData.data.gas2Now, - }); - // 天然气流量 2 历史 - store.dispatch({ - type: "natGas/setGasIIHistory", - payload: serializedData.data.hisGas2, - }); - break; - } - case "FanFrequencyInfo": { - // 风机运行频率 暂时只有实时数据 - store.dispatch({ - type: "fanFrequence/setRuntime", - payload: serializedData.data.FanFrequencyInfo, - }); - // 风机运行频率 历史 暂无 - break; - } - case "TopTempInfo": { - // 碹顶温度列表 - store.dispatch({ - type: "temperature/setTopTemp", - payload: serializedData.data.topTempInfo, - }); - break; - } - case "BottomTempInfo": { - // 碹底温度列表 - store.dispatch({ - type: "temperature/setBottomTemp", - payload: serializedData.data.bottomTempInfo, - }); - break; - } - case "FeederInfo": { - // 投料机信息 - store.dispatch({ - type: "feeder/setFeederInfo", - payload: serializedData.data, - }); - break; - } - case "FireInfo": { - // 火向时间信息 - store.dispatch({ - type: "fireInfo/setFireInfo", - payload: serializedData.data, - }); - break; - } - case "FanInfo": { - // 风机信息 - store.dispatch({ - type: "fanInfo/setInfo", - payload: serializedData.data, - }); - break; - } - case "energyInfo": { - // 能耗数据 - store.dispatch({ - type: "energy/setInfo", - payload: serializedData.data, - }); - break; - } - default: { - console.log("websocket message: [unknown] ---> ", e.data); - } - } + this.ws.onmessage = onmessage; + this.ws.onerror = (err) => { + console.log("[*] websocket error!", err, err.data); }; - WsClient.socket.onerror = (e) => { - console.log("[*] websocket error!", e, e.data); - }; - WsClient.socket.onclose = (e) => { - let timer = setTimeout(() => { - if (WsClient.tryCount < 3) { - new WsClient(); - WsClient.tryCount += 1; - } else clearTimeout(timer); - }, 30000); + this.ws.onclose = (e) => { + console.log(`[*] ${this.name} ws closed`); }; } } + +new XClient( + "ws://192.168.1.12:8081/xc-screen/websocket/xc001", + "DCS_DATA", + (msg) => { + let serializedData = null; + try { + serializedData = JSON.parse(msg.data); + } catch (error) { + console.log("[*] websocket: [unable to serialize] ---> ", msg); + } + switch (serializedData?.type) { + case "KilnInfo": { + store.dispatch({ + type: "kiln/setKilnInfo", + payload: serializedData.data, + }); + break; + } + case "CombustionAirInfo": { + // 助燃风流量 实时 + store.dispatch({ + type: "combustionAir/setRuntime", + payload: serializedData.data.combustionAirNow, // [] + }); + // 助燃风流量 历史 + store.dispatch({ + type: "combustionAir/setHistory", + payload: serializedData.data.combustionAirHis, // {} + }); + break; + } + case "GasInfo": { + // 天然气流量 1 实时 + store.dispatch({ + type: "natGas/setGasIRuntime", + payload: serializedData.data.gas1Now, + }); + // 天然气流量 1 历史 + store.dispatch({ + type: "natGas/setGasIHistory", + payload: serializedData.data.hisGas1, + }); + // 天然气流量 2 实时 + store.dispatch({ + type: "natGas/setGasIIRuntime", + payload: serializedData.data.gas2Now, + }); + // 天然气流量 2 历史 + store.dispatch({ + type: "natGas/setGasIIHistory", + payload: serializedData.data.hisGas2, + }); + break; + } + case "FanFrequencyInfo": { + // 风机运行频率 暂时只有实时数据 + store.dispatch({ + type: "fanFrequence/setRuntime", + payload: serializedData.data.FanFrequencyInfo, + }); + // 风机运行频率 历史 暂无 + break; + } + case "TopTempInfo": { + // 碹顶温度列表 + store.dispatch({ + type: "temperature/setTopTemp", + payload: serializedData.data.topTempInfo, + }); + break; + } + case "BottomTempInfo": { + // 碹底温度列表 + store.dispatch({ + type: "temperature/setBottomTemp", + payload: serializedData.data.bottomTempInfo, + }); + break; + } + case "FeederInfo": { + // 投料机信息 + store.dispatch({ + type: "feeder/setFeederInfo", + payload: serializedData.data, + }); + break; + } + case "FireInfo": { + // 火向时间信息 + store.dispatch({ + type: "fireInfo/setFireInfo", + payload: serializedData.data, + }); + break; + } + case "FanInfo": { + // 风机信息 + store.dispatch({ + type: "fanInfo/setInfo", + payload: serializedData.data, + }); + break; + } + case "energyInfo": { + // 能耗数据 + store.dispatch({ + type: "energy/setInfo", + payload: serializedData.data, + }); + break; + } + default: { + console.log("websocket message: [unknown] ---> ", msg.data); + } + } + } +); + +new XClient( + "ws://192.168.1.74:48080/websocket/message?userId=ENERGY111", + "MES_DATA", + (msg) => { + let serializedData = null; + try { + serializedData = JSON.parse(msg.data); + } catch (error) { + console.log("[*] websocket: [unable to serialize] ---> ", msg); + } + switch (serializedData?.type) { + case "EnergyInfo": { + // 能耗数据 + store.dispatch({ + type: "energy/setInfo", + payload: serializedData.data, + }); + break; + } + default: { + console.log("websocket message: [unknown] ---> ", msg.data); + } + } + } +);