-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuxa.js
307 lines (265 loc) · 11.4 KB
/
fuxa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const { t } = require('i18next');
const WebSocket = require('ws'); // 添加 ws 模块引用
module.exports = function (RED) {
// FUXA服务器配置节点
function FuxaServerNode(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
this.serverUrl = config.serverUrl;
}
function FuxaConnect(config, node) {
// 重连相关配置
const reconnectConfig = {
attempts: 0,
maxAttempts: 5,
delay: 5000, // 5秒
timer: null
};
// 重连函数
function reconnect() {
if (reconnectConfig.attempts >= reconnectConfig.maxAttempts) {
node.error('重连次数超过最大限制,停止重连');
node.status({fill:"red", shape:"dot", text:`重连失败(${reconnectConfig.attempts}次)`});
return;
}
reconnectConfig.attempts++;
node.status({fill:"yellow", shape:"ring", text:`正在重连(${reconnectConfig.attempts}次)`});
node.log(`尝试第 ${reconnectConfig.attempts} 次重连...`);
// 清除之前的定时器
if (reconnectConfig.timer) {
clearTimeout(reconnectConfig.timer);
}
// 延迟重连
reconnectConfig.timer = setTimeout(() => {
initConnection();
}, reconnectConfig.delay);
}
// 初始化连接
function initConnection() {
node.log('正在连接到FUXA服务器...');
// 构建socket.io连接URL
const socketUrl = `${config.server.serverUrl}/socket.io/?token=null&EIO=3&transport=polling&t=PNLV_ls`;
fetch(socketUrl)
.then(response => {
if (!response.ok) throw new Error('连接失败');
return response.text();
})
.then(data => {
node.log('连接成功,服务器响应: ' + data);
// 解析响应数据
try {
// 提取JSON部分
const match = data.match(/\d+:\d+({.*})\d*:\d*/);
if (!match || !match[1]) {
throw new Error('无效的响应格式');
}
const jsonStr = match[1];
node.log('提取的JSON数据: ' + jsonStr);
const jsonData = JSON.parse(jsonStr);
const sid = jsonData.sid;
// 构建WebSocket URL
const wsUrl = config.server.serverUrl.replace('http', 'ws') +
`/socket.io/?token=null&EIO=3&transport=websocket&sid=${sid}`;
node.log('正在建立WebSocket连接: ' + wsUrl);
// 创建WebSocket连接
const ws = new WebSocket(wsUrl);
ws.on('open', function () {
node.log('WebSocket连接已建立');
node.status({ fill: "green", shape: "dot", text: "WebSocket已连接" });
// 保存WebSocket实例以便后续使用
node.server.ws = ws;
ws.send("2probe");
// 连接成功,重置重连计数
reconnectConfig.attempts = 0;
// 添加心跳定时器
node.heartbeatTimer = setInterval(function() {
try {
if (ws.readyState === WebSocket.OPEN) {
ws.send("2");
} else {
clearInterval(node.heartbeatTimer);
reconnect(); // 心跳失败时触发重连
}
} catch (error) {
clearInterval(node.heartbeatTimer);
reconnect(); // 发送心跳异常时触发重连
}
}, 20000); // 20秒发送一次
});
ws.on('message', function (msg) {
//node.log('收到WebSocket消息: ' + msg.toString());
// 如果是心跳包,不处理
var data = msg.toString();
if (data == ("3probe")) {
ws.send("5");
}
if (data === "2" || data === "3probe" || data === "3") {
return;
}
node.fuxa_cb(data);
});
ws.on('error', function (error) {
node.error('WebSocket错误: ' + error.message);
node.status({ fill: "red", shape: "ring", text: "WebSocket错误" });
});
ws.on('close', function () {
node.log('WebSocket连接已关闭');
node.status({ fill: "yellow", shape: "ring", text: "WebSocket已断开" });
// 清除心跳定时器
if (node.heartbeatTimer) {
clearInterval(node.heartbeatTimer);
delete node.heartbeatTimer;
}
delete node.server.ws;
// 触发重连
reconnect();
});
} catch (error) {
node.error('解析响应数据失败: ' + error.message);
node.status({ fill: "red", shape: "ring", text: "连接失败" });
reconnect(); // 解析失败时触发重连
}
// 记录配置信息
node.log('已连接到服务器: ' + config.server.serverUrl);
if (config.device) {
node.log('已选择设备: ' + config.device);
}
if (config.variable) {
node.log('已选择变量: ' + config.variable);
}
})
.catch(error => {
node.error('连接FUXA服务器失败: ' + error.message);
node.status({ fill: "red", shape: "ring", text: "连接失败" });
reconnect(); // 连接失败时触发重连
});
}
// 开始首次连接
initConnection();
}
// FUXA主节点
function FuxaNode(config) {
RED.nodes.createNode(this, config);
var node = this;
// 初始化日志
node.log('FUXA节点初始化中...');
node.status({ fill: "yellow", shape: "ring", text: "初始化中" });
// 获取服务器配置节点
this.server = RED.nodes.getNode(config.server);
this.device = config.device;
this.variable = config.variable;
//DataCallBack
this.fuxa_cb = function (param) {
try {
// 移除消息类型前缀(如果存在)
const data = param.replace(/^\d+/, '');
if (data) {
try {
// 尝试解析JSON数据
const jsonData = JSON.parse(data);
if (jsonData[0] = "device-values") {
if (jsonData[1].id === node.device) {
jsonData[1].values.forEach(e => {
if (e.id === node.variable) {
// 发送解析后的JSON对象
node.send({
topic: "fuxa",
payload: e.tagref,
raw: param
});
}
});
}
}
} catch (e) {
// 如果不是JSON,发送原始数据
node.send({
topic: "fuxa",
payload: data,
raw: param
});
}
}
} catch (error) {
node.error("处理FUXA数据失败: " + error.message);
node.send({
topic: "fuxa_error",
payload: error.message,
raw: param
});
}
};
if (this.server) {
FuxaConnect(this, node);
} else {
node.status({ fill: "grey", shape: "dot", text: "FUXA未配置服务器" });
}
// 处理输入消息
this.on('input', function (msg) {
if (msg.topic != "fuxa" || msg.payload == null || msg.payload.value == undefined) {
return;
}
node.log(JSON.stringify());
var data = ["device-values", {
cmd: "set",
fnc: [null, msg.payload.value],
var: {
id: this.variable,
source: this.device,
value: msg.payload.value,
timestamp: new Date().getTime()
}
}];
if (this.server) {
msg.serverUrl = this.server.serverUrl;
msg.device = this.device;
msg.variable = this.variable;
node.log(JSON.stringify(data));
node.server.ws.send("42" + JSON.stringify(data));
// node.send(msg);
}
});
// 节点关闭时的清理
this.on('close', function () {
// 清除心跳定时器
if (node.heartbeatTimer) {
clearInterval(node.heartbeatTimer);
delete node.heartbeatTimer;
}
// 关闭WebSocket连接
if (node.ws) {
node.ws.close();
delete node.ws;
}
node.log('FUXA节点关闭');
node.status({});
});
}
// 注册服务器配置节点
RED.nodes.registerType("fuxa-server", FuxaServerNode, {
defaults: {
name: { value: "" },
serverUrl: { value: "", required: true }
},
label: function () {
return this.name || this.serverUrl;
}
});
// 注册FUXA主节点
RED.nodes.registerType("fuxa", FuxaNode, {
defaults: {
name: { value: "" },
server: { type: "fuxa-server", required: true },
device: { value: "" },
variable: { value: "" }
},
category: 'FUXA',
color: '#a6bbcf',
inputs: 1,
outputs: 1,
icon: "file.png",
label: function () {
return this.name || "FUXA";
}
});
}