node-red-contrib-symi-mesh 1.6.6 → 1.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +233 -429
- package/examples/knx-sync-example.json +48 -410
- package/lib/device-manager.js +11 -3
- package/lib/tcp-client.js +6 -2
- package/nodes/symi-485-bridge.js +26 -27
- package/nodes/symi-485-config.js +38 -3
- package/nodes/symi-gateway.js +27 -20
- package/nodes/symi-knx-bridge.html +368 -0
- package/nodes/symi-knx-bridge.js +1110 -0
- package/nodes/symi-mqtt.js +52 -10
- package/package.json +4 -3
|
@@ -0,0 +1,1110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Symi KNX Bridge Node - Mesh与KNX设备双向同步桥接
|
|
3
|
+
* 支持开关、窗帘等设备的双向状态同步
|
|
4
|
+
* 事件驱动架构,命令队列顺序处理,防死循环机制
|
|
5
|
+
*
|
|
6
|
+
* 版本: 1.6.8
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
module.exports = function(RED) {
|
|
10
|
+
|
|
11
|
+
// 设备类型配置
|
|
12
|
+
const DEVICE_TYPES = {
|
|
13
|
+
'switch': { dpt: '1.001', name: '开关', hasChannel: true },
|
|
14
|
+
'light_mono': { dpt: '5.001', name: '单色调光', hasChannel: false },
|
|
15
|
+
'light_cct': { dpt: '5.001', name: '双色调光', hasChannel: false },
|
|
16
|
+
'light_rgb': { dpt: '232.600', name: 'RGB调光', hasChannel: false },
|
|
17
|
+
'light_rgbcw': { dpt: '251.600', name: 'RGBCW调光', hasChannel: false },
|
|
18
|
+
'cover': { dpt: '1.008', name: '窗帘', hasChannel: false },
|
|
19
|
+
'climate': { dpt: '9.001', name: '空调', hasChannel: false },
|
|
20
|
+
'fresh_air': { dpt: '1.001', name: '新风', hasChannel: false },
|
|
21
|
+
'floor_heating': { dpt: '9.001', name: '地暖', hasChannel: false }
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function SymiKNXBridgeNode(config) {
|
|
25
|
+
RED.nodes.createNode(this, config);
|
|
26
|
+
const node = this;
|
|
27
|
+
|
|
28
|
+
// 基本配置
|
|
29
|
+
node.name = config.name || 'KNX Bridge';
|
|
30
|
+
node.gateway = RED.nodes.getNode(config.gateway);
|
|
31
|
+
|
|
32
|
+
// 解析KNX实体库
|
|
33
|
+
let knxEntities = [];
|
|
34
|
+
try {
|
|
35
|
+
knxEntities = JSON.parse(config.knxEntities || '[]');
|
|
36
|
+
} catch (e) {
|
|
37
|
+
knxEntities = [];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 解析实体映射
|
|
41
|
+
try {
|
|
42
|
+
const rawMappings = JSON.parse(config.mappings || '[]');
|
|
43
|
+
node.mappings = rawMappings.map(m => {
|
|
44
|
+
// 查找对应的KNX实体
|
|
45
|
+
const knxEntity = knxEntities.find(e => e.id === m.knxEntityId) || {};
|
|
46
|
+
const deviceType = knxEntity.type || 'switch';
|
|
47
|
+
|
|
48
|
+
// 基础映射
|
|
49
|
+
const mapping = {
|
|
50
|
+
meshMac: m.meshMac,
|
|
51
|
+
meshChannel: parseInt(m.meshChannel) || 1,
|
|
52
|
+
knxEntityId: m.knxEntityId,
|
|
53
|
+
knxAddrCmd: knxEntity.cmdAddr || '',
|
|
54
|
+
knxAddrStatus: knxEntity.statusAddr || '',
|
|
55
|
+
deviceType: deviceType,
|
|
56
|
+
invertPosition: knxEntity.invert || false,
|
|
57
|
+
name: knxEntity.name || ''
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// 根据设备类型映射扩展地址到具体功能
|
|
61
|
+
// 参考HTML模板中的typeFields定义
|
|
62
|
+
switch (deviceType) {
|
|
63
|
+
case 'light_mono':
|
|
64
|
+
// 单色调光: 开关, 状态, 亮度
|
|
65
|
+
mapping.knxAddrBrightness = knxEntity.ext1 || '';
|
|
66
|
+
break;
|
|
67
|
+
case 'light_cct':
|
|
68
|
+
// 双色调光: 开关, 状态, 亮度, 色温
|
|
69
|
+
mapping.knxAddrBrightness = knxEntity.ext1 || '';
|
|
70
|
+
mapping.knxAddrColorTemp = knxEntity.ext2 || '';
|
|
71
|
+
break;
|
|
72
|
+
case 'light_rgb':
|
|
73
|
+
// RGB调光: 开关, 状态, 亮度, RGB
|
|
74
|
+
mapping.knxAddrBrightness = knxEntity.ext1 || '';
|
|
75
|
+
mapping.knxAddrRGB = knxEntity.ext2 || '';
|
|
76
|
+
break;
|
|
77
|
+
case 'light_rgbcw':
|
|
78
|
+
// RGBCW: 开关, 状态, 亮度, 色温, RGB
|
|
79
|
+
mapping.knxAddrBrightness = knxEntity.ext1 || '';
|
|
80
|
+
mapping.knxAddrColorTemp = knxEntity.ext2 || '';
|
|
81
|
+
mapping.knxAddrRGB = knxEntity.ext3 || '';
|
|
82
|
+
break;
|
|
83
|
+
case 'cover':
|
|
84
|
+
// 窗帘: 上下, 位置, 停止
|
|
85
|
+
mapping.knxAddrPosition = knxEntity.statusAddr || '';
|
|
86
|
+
mapping.knxAddrStop = knxEntity.ext1 || '';
|
|
87
|
+
break;
|
|
88
|
+
case 'climate':
|
|
89
|
+
// 空调: 开关, 温度, 模式, 风速, 当前温度
|
|
90
|
+
mapping.knxAddrTemp = knxEntity.statusAddr || '';
|
|
91
|
+
mapping.knxAddrMode = knxEntity.ext1 || '';
|
|
92
|
+
mapping.knxAddrFanSpeed = knxEntity.ext2 || '';
|
|
93
|
+
mapping.knxAddrCurrentTemp = knxEntity.ext3 || '';
|
|
94
|
+
break;
|
|
95
|
+
case 'fresh_air':
|
|
96
|
+
// 新风: 开关, 风速
|
|
97
|
+
mapping.knxAddrFanSpeed = knxEntity.statusAddr || '';
|
|
98
|
+
break;
|
|
99
|
+
case 'floor_heating':
|
|
100
|
+
// 地暖: 开关, 温度, 当前温度
|
|
101
|
+
mapping.knxAddrTemp = knxEntity.statusAddr || '';
|
|
102
|
+
mapping.knxAddrCurrentTemp = knxEntity.ext1 || '';
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 收集所有KNX地址用于快速查找
|
|
107
|
+
mapping.allKnxAddrs = [
|
|
108
|
+
mapping.knxAddrCmd,
|
|
109
|
+
mapping.knxAddrStatus,
|
|
110
|
+
mapping.knxAddrBrightness,
|
|
111
|
+
mapping.knxAddrColorTemp,
|
|
112
|
+
mapping.knxAddrRGB,
|
|
113
|
+
mapping.knxAddrPosition,
|
|
114
|
+
mapping.knxAddrStop,
|
|
115
|
+
mapping.knxAddrTemp,
|
|
116
|
+
mapping.knxAddrMode,
|
|
117
|
+
mapping.knxAddrFanSpeed,
|
|
118
|
+
mapping.knxAddrCurrentTemp
|
|
119
|
+
].filter(addr => addr && addr.length > 0);
|
|
120
|
+
|
|
121
|
+
return mapping;
|
|
122
|
+
}).filter(m => m.meshMac && m.knxAddrCmd);
|
|
123
|
+
|
|
124
|
+
// 打印映射配置
|
|
125
|
+
node.mappings.forEach((m, i) => {
|
|
126
|
+
const typeConfig = DEVICE_TYPES[m.deviceType] || DEVICE_TYPES['switch'];
|
|
127
|
+
if (typeConfig.hasChannel) {
|
|
128
|
+
node.log(`[映射${i+1}] ${m.name}: Mesh ${m.meshMac} CH${m.meshChannel} <-> KNX ${m.knxAddrCmd} (${typeConfig.name})`);
|
|
129
|
+
} else {
|
|
130
|
+
node.log(`[映射${i+1}] ${m.name}: Mesh ${m.meshMac} <-> KNX ${m.knxAddrCmd} (${typeConfig.name})`);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
} catch (e) {
|
|
134
|
+
node.mappings = [];
|
|
135
|
+
node.error(`映射配置解析失败: ${e.message}`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (!node.gateway) {
|
|
139
|
+
node.status({ fill: 'red', shape: 'ring', text: '未配置Mesh网关' });
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 状态管理
|
|
144
|
+
node.commandQueue = [];
|
|
145
|
+
node.processing = false;
|
|
146
|
+
node.syncLock = false;
|
|
147
|
+
node.lastSyncTime = 0;
|
|
148
|
+
node.stateCache = {}; // Mesh设备状态缓存
|
|
149
|
+
node.knxStateCache = {}; // KNX设备状态缓存
|
|
150
|
+
node.lastMeshToKnx = {}; // 记录Mesh->KNX发送时间,防止回环
|
|
151
|
+
node.lastKnxToMesh = {}; // 记录KNX->Mesh发送时间,防止回环
|
|
152
|
+
|
|
153
|
+
// 防死循环参数
|
|
154
|
+
const LOOP_PREVENTION_MS = 800; // 800ms内不处理反向同步,防止回环
|
|
155
|
+
const DEBOUNCE_MS = 100; // 100ms防抖
|
|
156
|
+
const MAX_QUEUE_SIZE = 100; // 最大队列大小
|
|
157
|
+
|
|
158
|
+
// 初始化标记
|
|
159
|
+
node.initializing = true;
|
|
160
|
+
node.initTimer = setTimeout(() => {
|
|
161
|
+
node.initializing = false;
|
|
162
|
+
node.log('[KNX Bridge] 初始化完成,开始同步');
|
|
163
|
+
}, 20000); // 20秒初始化延迟
|
|
164
|
+
|
|
165
|
+
if (node.mappings.length === 0) {
|
|
166
|
+
node.status({ fill: 'grey', shape: 'ring', text: '请添加实体映射' });
|
|
167
|
+
} else {
|
|
168
|
+
node.status({ fill: 'yellow', shape: 'ring', text: `${node.mappings.length}个映射等待连接...` });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ========== 辅助函数 ==========
|
|
172
|
+
|
|
173
|
+
// 查找Mesh设备的映射
|
|
174
|
+
node.findMeshMapping = function(mac, channel) {
|
|
175
|
+
const macNormalized = (mac || '').toLowerCase().replace(/:/g, '');
|
|
176
|
+
return node.mappings.find(m => {
|
|
177
|
+
const mappingMac = (m.meshMac || '').toLowerCase().replace(/:/g, '');
|
|
178
|
+
return mappingMac === macNormalized &&
|
|
179
|
+
(m.deviceType === 'cover' || m.meshChannel === channel);
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// 查找所有匹配MAC的映射
|
|
184
|
+
node.findAllMeshMappings = function(mac) {
|
|
185
|
+
const macNormalized = (mac || '').toLowerCase().replace(/:/g, '');
|
|
186
|
+
return node.mappings.filter(m => {
|
|
187
|
+
const mappingMac = (m.meshMac || '').toLowerCase().replace(/:/g, '');
|
|
188
|
+
return mappingMac === macNormalized;
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// 查找KNX组地址的映射(检查所有相关地址)
|
|
193
|
+
node.findKnxMapping = function(groupAddr) {
|
|
194
|
+
return node.mappings.find(m => m.allKnxAddrs.includes(groupAddr));
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// 判断KNX地址的功能类型
|
|
198
|
+
node.getKnxAddrFunction = function(mapping, groupAddr) {
|
|
199
|
+
if (groupAddr === mapping.knxAddrCmd) return 'cmd';
|
|
200
|
+
if (groupAddr === mapping.knxAddrStatus) return 'status';
|
|
201
|
+
if (groupAddr === mapping.knxAddrBrightness) return 'brightness';
|
|
202
|
+
if (groupAddr === mapping.knxAddrColorTemp) return 'colorTemp';
|
|
203
|
+
if (groupAddr === mapping.knxAddrRGB) return 'rgb';
|
|
204
|
+
if (groupAddr === mapping.knxAddrPosition) return 'position';
|
|
205
|
+
if (groupAddr === mapping.knxAddrStop) return 'stop';
|
|
206
|
+
if (groupAddr === mapping.knxAddrTemp) return 'temp';
|
|
207
|
+
if (groupAddr === mapping.knxAddrMode) return 'mode';
|
|
208
|
+
if (groupAddr === mapping.knxAddrFanSpeed) return 'fanSpeed';
|
|
209
|
+
if (groupAddr === mapping.knxAddrCurrentTemp) return 'currentTemp';
|
|
210
|
+
return 'unknown';
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// 标准化DPT格式 (处理 "DPT1.001" 和 "1.001" 两种格式)
|
|
214
|
+
node.normalizeDpt = function(dpt) {
|
|
215
|
+
if (!dpt) return '';
|
|
216
|
+
const str = String(dpt).toUpperCase();
|
|
217
|
+
if (str.startsWith('DPT')) {
|
|
218
|
+
return str.substring(3);
|
|
219
|
+
}
|
|
220
|
+
return str;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// 检查是否应该阻止同步(防死循环)
|
|
224
|
+
node.shouldPreventSync = function(direction, key) {
|
|
225
|
+
const now = Date.now();
|
|
226
|
+
if (direction === 'mesh-to-knx') {
|
|
227
|
+
const lastKnxTime = node.lastKnxToMesh[key] || 0;
|
|
228
|
+
return (now - lastKnxTime) < LOOP_PREVENTION_MS;
|
|
229
|
+
} else {
|
|
230
|
+
const lastMeshTime = node.lastMeshToKnx[key] || 0;
|
|
231
|
+
return (now - lastMeshTime) < LOOP_PREVENTION_MS;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
// 记录同步时间(用于防死循环)
|
|
236
|
+
node.recordSyncTime = function(direction, key) {
|
|
237
|
+
const now = Date.now();
|
|
238
|
+
if (direction === 'mesh-to-knx') {
|
|
239
|
+
node.lastMeshToKnx[key] = now;
|
|
240
|
+
} else {
|
|
241
|
+
node.lastKnxToMesh[key] = now;
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// 延时函数
|
|
246
|
+
node.sleep = function(ms) {
|
|
247
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
// ========== Mesh设备状态变化处理 ==========
|
|
251
|
+
const handleMeshStateChange = (eventData) => {
|
|
252
|
+
// 只检查initializing,不检查syncLock
|
|
253
|
+
// syncLock会导致队列处理期间丢失事件,改用per-device时间戳防回环
|
|
254
|
+
if (node.initializing) return;
|
|
255
|
+
|
|
256
|
+
const mac = eventData.device.macAddress;
|
|
257
|
+
const state = eventData.state || {};
|
|
258
|
+
|
|
259
|
+
// 状态缓存比较
|
|
260
|
+
if (!node.stateCache[mac]) node.stateCache[mac] = {};
|
|
261
|
+
const cached = node.stateCache[mac];
|
|
262
|
+
const changed = {};
|
|
263
|
+
const isFirstState = Object.keys(cached).length === 0;
|
|
264
|
+
|
|
265
|
+
for (const [key, value] of Object.entries(state)) {
|
|
266
|
+
if (cached[key] !== value) {
|
|
267
|
+
changed[key] = value;
|
|
268
|
+
cached[key] = value;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (Object.keys(changed).length === 0) return;
|
|
273
|
+
|
|
274
|
+
// 首次收到状态时只缓存,不触发同步(避免启动时批量发码)
|
|
275
|
+
// 窗帘控制命令除外
|
|
276
|
+
const hasCurtainControl = changed.curtainStatus !== undefined;
|
|
277
|
+
if (isFirstState && !hasCurtainControl) {
|
|
278
|
+
node.debug(`[Mesh事件] MAC=${mac} 首次状态,仅缓存: ${JSON.stringify(changed)}`);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const macNormalized = mac.toLowerCase().replace(/:/g, '');
|
|
283
|
+
|
|
284
|
+
// 遍历所有匹配的映射
|
|
285
|
+
const matchedMappings = node.findAllMeshMappings(mac);
|
|
286
|
+
if (matchedMappings.length === 0) {
|
|
287
|
+
node.debug(`[Mesh事件] MAC=${macNormalized} 无映射配置`);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
node.log(`[Mesh事件] MAC=${macNormalized}, 找到${matchedMappings.length}个映射, 变化: ${JSON.stringify(changed)}`);
|
|
291
|
+
|
|
292
|
+
for (const mapping of matchedMappings) {
|
|
293
|
+
const loopKey = `${mac}_${mapping.meshChannel}`;
|
|
294
|
+
|
|
295
|
+
// 防死循环检查
|
|
296
|
+
if (node.shouldPreventSync('mesh-to-knx', loopKey)) {
|
|
297
|
+
node.log(`[Mesh->KNX] 跳过(防死循环): ${loopKey}`);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// 开关设备
|
|
302
|
+
if (mapping.deviceType === 'switch') {
|
|
303
|
+
const switchKey = `switch_${mapping.meshChannel}`;
|
|
304
|
+
if (changed[switchKey] !== undefined) {
|
|
305
|
+
node.log(`[Mesh->KNX] 开关变化: ${mapping.name || eventData.device.name} CH${mapping.meshChannel} = ${changed[switchKey]}`);
|
|
306
|
+
node.queueCommand({
|
|
307
|
+
direction: 'mesh-to-knx',
|
|
308
|
+
mapping: mapping,
|
|
309
|
+
type: 'switch',
|
|
310
|
+
value: changed[switchKey],
|
|
311
|
+
key: loopKey
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
// 调光灯设备(单色、双色、RGB、RGBCW)
|
|
316
|
+
else if (mapping.deviceType.startsWith('light_')) {
|
|
317
|
+
// 开关状态
|
|
318
|
+
if (changed.switch !== undefined) {
|
|
319
|
+
node.log(`[Mesh->KNX] 调光灯开关: ${mapping.name || eventData.device.name} = ${changed.switch}`);
|
|
320
|
+
node.queueCommand({
|
|
321
|
+
direction: 'mesh-to-knx',
|
|
322
|
+
mapping: mapping,
|
|
323
|
+
type: 'light_switch',
|
|
324
|
+
value: changed.switch,
|
|
325
|
+
key: loopKey
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
// 亮度
|
|
329
|
+
if (changed.brightness !== undefined) {
|
|
330
|
+
node.log(`[Mesh->KNX] 调光灯亮度: ${mapping.name || eventData.device.name} = ${changed.brightness}%`);
|
|
331
|
+
node.queueCommand({
|
|
332
|
+
direction: 'mesh-to-knx',
|
|
333
|
+
mapping: mapping,
|
|
334
|
+
type: 'light_brightness',
|
|
335
|
+
value: changed.brightness,
|
|
336
|
+
key: loopKey
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
// 色温
|
|
340
|
+
if (changed.colorTemp !== undefined && (mapping.deviceType === 'light_cct' || mapping.deviceType === 'light_rgbcw')) {
|
|
341
|
+
node.log(`[Mesh->KNX] 调光灯色温: ${mapping.name || eventData.device.name} = ${changed.colorTemp}`);
|
|
342
|
+
node.queueCommand({
|
|
343
|
+
direction: 'mesh-to-knx',
|
|
344
|
+
mapping: mapping,
|
|
345
|
+
type: 'light_color_temp',
|
|
346
|
+
value: changed.colorTemp,
|
|
347
|
+
key: loopKey
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
// 窗帘设备
|
|
352
|
+
else if (mapping.deviceType === 'cover') {
|
|
353
|
+
if (!eventData.isUserControl) continue;
|
|
354
|
+
|
|
355
|
+
if (changed.curtainAction !== undefined || changed.curtainStatus !== undefined) {
|
|
356
|
+
const action = changed.curtainAction || state.curtainAction;
|
|
357
|
+
node.log(`[Mesh->KNX] 窗帘动作: ${mapping.name || eventData.device.name} action=${action}`);
|
|
358
|
+
node.queueCommand({
|
|
359
|
+
direction: 'mesh-to-knx',
|
|
360
|
+
mapping: mapping,
|
|
361
|
+
type: 'cover_action',
|
|
362
|
+
value: action,
|
|
363
|
+
key: loopKey
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
if (changed.curtainPosition !== undefined) {
|
|
367
|
+
node.log(`[Mesh->KNX] 窗帘位置: ${mapping.name || eventData.device.name} pos=${changed.curtainPosition}%`);
|
|
368
|
+
node.queueCommand({
|
|
369
|
+
direction: 'mesh-to-knx',
|
|
370
|
+
mapping: mapping,
|
|
371
|
+
type: 'cover_position',
|
|
372
|
+
value: changed.curtainPosition,
|
|
373
|
+
key: loopKey
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
// 空调设备
|
|
378
|
+
else if (mapping.deviceType === 'climate') {
|
|
379
|
+
if (changed.acSwitch !== undefined || changed.climateSwitch !== undefined) {
|
|
380
|
+
const sw = changed.acSwitch !== undefined ? changed.acSwitch : changed.climateSwitch;
|
|
381
|
+
node.log(`[Mesh->KNX] 空调开关: ${mapping.name || eventData.device.name} = ${sw}`);
|
|
382
|
+
node.queueCommand({
|
|
383
|
+
direction: 'mesh-to-knx',
|
|
384
|
+
mapping: mapping,
|
|
385
|
+
type: 'climate_switch',
|
|
386
|
+
value: sw,
|
|
387
|
+
key: loopKey
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
if (changed.targetTemp !== undefined || changed.acTargetTemp !== undefined) {
|
|
391
|
+
const temp = changed.targetTemp !== undefined ? changed.targetTemp : changed.acTargetTemp;
|
|
392
|
+
node.log(`[Mesh->KNX] 空调温度: ${mapping.name || eventData.device.name} = ${temp}°C`);
|
|
393
|
+
node.queueCommand({
|
|
394
|
+
direction: 'mesh-to-knx',
|
|
395
|
+
mapping: mapping,
|
|
396
|
+
type: 'climate_temp',
|
|
397
|
+
value: temp,
|
|
398
|
+
key: loopKey
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
if (changed.acMode !== undefined || changed.climateMode !== undefined) {
|
|
402
|
+
const mode = changed.acMode !== undefined ? changed.acMode : changed.climateMode;
|
|
403
|
+
node.log(`[Mesh->KNX] 空调模式: ${mapping.name || eventData.device.name} = ${mode}`);
|
|
404
|
+
node.queueCommand({
|
|
405
|
+
direction: 'mesh-to-knx',
|
|
406
|
+
mapping: mapping,
|
|
407
|
+
type: 'climate_mode',
|
|
408
|
+
value: mode,
|
|
409
|
+
key: loopKey
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
// 新风设备
|
|
414
|
+
else if (mapping.deviceType === 'fresh_air') {
|
|
415
|
+
if (changed.freshAirSwitch !== undefined) {
|
|
416
|
+
node.log(`[Mesh->KNX] 新风开关: ${mapping.name || eventData.device.name} = ${changed.freshAirSwitch}`);
|
|
417
|
+
node.queueCommand({
|
|
418
|
+
direction: 'mesh-to-knx',
|
|
419
|
+
mapping: mapping,
|
|
420
|
+
type: 'fresh_air_switch',
|
|
421
|
+
value: changed.freshAirSwitch,
|
|
422
|
+
key: loopKey
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
if (changed.freshAirSpeed !== undefined) {
|
|
426
|
+
node.log(`[Mesh->KNX] 新风风速: ${mapping.name || eventData.device.name} = ${changed.freshAirSpeed}`);
|
|
427
|
+
node.queueCommand({
|
|
428
|
+
direction: 'mesh-to-knx',
|
|
429
|
+
mapping: mapping,
|
|
430
|
+
type: 'fresh_air_speed',
|
|
431
|
+
value: changed.freshAirSpeed,
|
|
432
|
+
key: loopKey
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
// 地暖设备
|
|
437
|
+
else if (mapping.deviceType === 'floor_heating') {
|
|
438
|
+
if (changed.floorHeatingSwitch !== undefined) {
|
|
439
|
+
node.log(`[Mesh->KNX] 地暖开关: ${mapping.name || eventData.device.name} = ${changed.floorHeatingSwitch}`);
|
|
440
|
+
node.queueCommand({
|
|
441
|
+
direction: 'mesh-to-knx',
|
|
442
|
+
mapping: mapping,
|
|
443
|
+
type: 'floor_heating_switch',
|
|
444
|
+
value: changed.floorHeatingSwitch,
|
|
445
|
+
key: loopKey
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
if (changed.floorHeatingTemp !== undefined) {
|
|
449
|
+
node.log(`[Mesh->KNX] 地暖温度: ${mapping.name || eventData.device.name} = ${changed.floorHeatingTemp}°C`);
|
|
450
|
+
node.queueCommand({
|
|
451
|
+
direction: 'mesh-to-knx',
|
|
452
|
+
mapping: mapping,
|
|
453
|
+
type: 'floor_heating_temp',
|
|
454
|
+
value: changed.floorHeatingTemp,
|
|
455
|
+
key: loopKey
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
// ========== 命令队列处理 ==========
|
|
463
|
+
node.queueCommand = function(cmd) {
|
|
464
|
+
// 队列大小限制
|
|
465
|
+
if (node.commandQueue.length >= MAX_QUEUE_SIZE) {
|
|
466
|
+
node.warn(`[KNX Bridge] 命令队列已满(${MAX_QUEUE_SIZE}),丢弃最旧命令`);
|
|
467
|
+
node.commandQueue.shift();
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// 检查队列中是否有相同映射的命令(防抖)
|
|
471
|
+
const existing = node.commandQueue.find(c =>
|
|
472
|
+
c.direction === cmd.direction &&
|
|
473
|
+
c.mapping.meshMac === cmd.mapping.meshMac &&
|
|
474
|
+
c.mapping.meshChannel === cmd.mapping.meshChannel &&
|
|
475
|
+
c.type === cmd.type &&
|
|
476
|
+
Date.now() - (c.timestamp || 0) < DEBOUNCE_MS
|
|
477
|
+
);
|
|
478
|
+
|
|
479
|
+
if (existing) {
|
|
480
|
+
existing.value = cmd.value;
|
|
481
|
+
node.debug(`[队列] 合并命令: ${cmd.key}`);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
cmd.timestamp = Date.now();
|
|
486
|
+
node.commandQueue.push(cmd);
|
|
487
|
+
node.processQueue();
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
node.processQueue = async function() {
|
|
491
|
+
if (node.processing || node.commandQueue.length === 0) return;
|
|
492
|
+
|
|
493
|
+
node.processing = true;
|
|
494
|
+
|
|
495
|
+
try {
|
|
496
|
+
while (node.commandQueue.length > 0) {
|
|
497
|
+
const cmd = node.commandQueue.shift();
|
|
498
|
+
try {
|
|
499
|
+
if (cmd.direction === 'mesh-to-knx') {
|
|
500
|
+
await node.syncMeshToKnx(cmd);
|
|
501
|
+
} else if (cmd.direction === 'knx-to-mesh') {
|
|
502
|
+
await node.syncKnxToMesh(cmd);
|
|
503
|
+
}
|
|
504
|
+
await node.sleep(50); // 命令间隔50ms
|
|
505
|
+
} catch (err) {
|
|
506
|
+
node.error(`同步失败: ${err.message}`);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
} finally {
|
|
510
|
+
node.processing = false;
|
|
511
|
+
node.lastSyncTime = Date.now();
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
// ========== Mesh -> KNX 同步 ==========
|
|
516
|
+
node.syncMeshToKnx = async function(cmd) {
|
|
517
|
+
const { mapping, type, value, key } = cmd;
|
|
518
|
+
|
|
519
|
+
// 记录发送时间
|
|
520
|
+
node.recordSyncTime('mesh-to-knx', key);
|
|
521
|
+
|
|
522
|
+
// 构建KNX消息并通过输出端口发送
|
|
523
|
+
let knxMsg = null;
|
|
524
|
+
|
|
525
|
+
if (type === 'switch') {
|
|
526
|
+
// 开关: DPT 1.001, 使用boolean类型
|
|
527
|
+
knxMsg = {
|
|
528
|
+
topic: mapping.knxAddrCmd,
|
|
529
|
+
payload: value === true || value === 1,
|
|
530
|
+
dpt: '1.001',
|
|
531
|
+
knx: {
|
|
532
|
+
destination: mapping.knxAddrCmd,
|
|
533
|
+
dpt: '1.001',
|
|
534
|
+
action: 'write'
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
node.log(`[Mesh->KNX] 发送开关: ${mapping.knxAddrCmd} = ${value ? 'ON' : 'OFF'}`);
|
|
538
|
+
}
|
|
539
|
+
else if (type === 'cover_action') {
|
|
540
|
+
// 窗帘动作: opening->false(上), closing->true(下), stopped->true
|
|
541
|
+
// DPT 1.008: false=上(开), true=下(关)
|
|
542
|
+
if (value === 'opening') {
|
|
543
|
+
knxMsg = {
|
|
544
|
+
topic: mapping.knxAddrCmd,
|
|
545
|
+
payload: false,
|
|
546
|
+
dpt: '1.008',
|
|
547
|
+
knx: {
|
|
548
|
+
destination: mapping.knxAddrCmd,
|
|
549
|
+
dpt: '1.008',
|
|
550
|
+
action: 'write'
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
node.log(`[Mesh->KNX] 发送窗帘: ${mapping.knxAddrCmd} = 上(开)`);
|
|
554
|
+
} else if (value === 'closing') {
|
|
555
|
+
knxMsg = {
|
|
556
|
+
topic: mapping.knxAddrCmd,
|
|
557
|
+
payload: true,
|
|
558
|
+
dpt: '1.008',
|
|
559
|
+
knx: {
|
|
560
|
+
destination: mapping.knxAddrCmd,
|
|
561
|
+
dpt: '1.008',
|
|
562
|
+
action: 'write'
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
node.log(`[Mesh->KNX] 发送窗帘: ${mapping.knxAddrCmd} = 下(关)`);
|
|
566
|
+
} else if (value === 'stopped') {
|
|
567
|
+
// 停止使用单独的停止地址
|
|
568
|
+
const stopAddr = mapping.knxAddrStop || mapping.knxAddrCmd;
|
|
569
|
+
knxMsg = {
|
|
570
|
+
topic: stopAddr,
|
|
571
|
+
payload: true,
|
|
572
|
+
dpt: '1.010',
|
|
573
|
+
knx: {
|
|
574
|
+
destination: stopAddr,
|
|
575
|
+
dpt: '1.010',
|
|
576
|
+
action: 'write'
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
node.log(`[Mesh->KNX] 发送窗帘: ${stopAddr} = 停止`);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
else if (type === 'cover_position') {
|
|
583
|
+
// 窗帘位置: 0-100%
|
|
584
|
+
const pos = mapping.invertPosition ? (100 - value) : value;
|
|
585
|
+
const posAddr = mapping.knxAddrPosition || mapping.knxAddrStatus;
|
|
586
|
+
if (posAddr) {
|
|
587
|
+
knxMsg = {
|
|
588
|
+
topic: posAddr,
|
|
589
|
+
payload: pos,
|
|
590
|
+
knx: { destination: posAddr, dpt: '5.001', action: 'write' }
|
|
591
|
+
};
|
|
592
|
+
node.log(`[Mesh->KNX] 发送窗帘位置: ${posAddr} = ${pos}%`);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
// 调光灯开关
|
|
596
|
+
else if (type === 'light_switch') {
|
|
597
|
+
knxMsg = {
|
|
598
|
+
topic: mapping.knxAddrCmd,
|
|
599
|
+
payload: value === true || value === 1,
|
|
600
|
+
dpt: '1.001',
|
|
601
|
+
knx: { destination: mapping.knxAddrCmd, dpt: '1.001', action: 'write' }
|
|
602
|
+
};
|
|
603
|
+
node.log(`[Mesh->KNX] 发送调光灯开关: ${mapping.knxAddrCmd} = ${value ? 'ON' : 'OFF'}`);
|
|
604
|
+
}
|
|
605
|
+
// 调光灯亮度
|
|
606
|
+
else if (type === 'light_brightness') {
|
|
607
|
+
const brightnessAddr = mapping.knxAddrBrightness || mapping.knxAddrStatus || mapping.knxAddrCmd;
|
|
608
|
+
if (brightnessAddr) {
|
|
609
|
+
knxMsg = {
|
|
610
|
+
topic: brightnessAddr,
|
|
611
|
+
payload: Math.round(value * 255 / 100),
|
|
612
|
+
knx: { destination: brightnessAddr, dpt: '5.001', action: 'write' }
|
|
613
|
+
};
|
|
614
|
+
node.log(`[Mesh->KNX] 发送调光灯亮度: ${brightnessAddr} = ${value}%`);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
// 调光灯色温
|
|
618
|
+
else if (type === 'light_color_temp') {
|
|
619
|
+
const ctAddr = mapping.knxAddrColorTemp || mapping.knxAddrStatus;
|
|
620
|
+
if (ctAddr) {
|
|
621
|
+
knxMsg = {
|
|
622
|
+
topic: ctAddr,
|
|
623
|
+
payload: value,
|
|
624
|
+
knx: { destination: ctAddr, dpt: '5.001', action: 'write' }
|
|
625
|
+
};
|
|
626
|
+
node.log(`[Mesh->KNX] 发送调光灯色温: ${ctAddr} = ${value}`);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
// 空调开关
|
|
630
|
+
else if (type === 'climate_switch') {
|
|
631
|
+
knxMsg = {
|
|
632
|
+
topic: mapping.knxAddrCmd,
|
|
633
|
+
payload: value === true || value === 1,
|
|
634
|
+
dpt: '1.001',
|
|
635
|
+
knx: { destination: mapping.knxAddrCmd, dpt: '1.001', action: 'write' }
|
|
636
|
+
};
|
|
637
|
+
node.log(`[Mesh->KNX] 发送空调开关: ${mapping.knxAddrCmd} = ${value ? 'ON' : 'OFF'}`);
|
|
638
|
+
}
|
|
639
|
+
// 空调温度
|
|
640
|
+
else if (type === 'climate_temp') {
|
|
641
|
+
const tempAddr = mapping.knxAddrTemp || mapping.knxAddrStatus || mapping.knxAddrCmd;
|
|
642
|
+
if (tempAddr) {
|
|
643
|
+
knxMsg = {
|
|
644
|
+
topic: tempAddr,
|
|
645
|
+
payload: value,
|
|
646
|
+
knx: { destination: tempAddr, dpt: '9.001', action: 'write' }
|
|
647
|
+
};
|
|
648
|
+
node.log(`[Mesh->KNX] 发送空调温度: ${tempAddr} = ${value}°C`);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
// 空调模式
|
|
652
|
+
else if (type === 'climate_mode') {
|
|
653
|
+
const modeAddr = mapping.knxAddrMode || mapping.knxAddrStatus;
|
|
654
|
+
if (modeAddr) {
|
|
655
|
+
// Mesh模式: 1=制冷, 2=制热, 3=送风, 4=除湿
|
|
656
|
+
knxMsg = {
|
|
657
|
+
topic: modeAddr,
|
|
658
|
+
payload: value,
|
|
659
|
+
knx: { destination: modeAddr, dpt: '20.102', action: 'write' }
|
|
660
|
+
};
|
|
661
|
+
node.log(`[Mesh->KNX] 发送空调模式: ${modeAddr} = ${value}`);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
// 新风开关
|
|
665
|
+
else if (type === 'fresh_air_switch') {
|
|
666
|
+
knxMsg = {
|
|
667
|
+
topic: mapping.knxAddrCmd,
|
|
668
|
+
payload: value === true || value === 1,
|
|
669
|
+
dpt: '1.001',
|
|
670
|
+
knx: { destination: mapping.knxAddrCmd, dpt: '1.001', action: 'write' }
|
|
671
|
+
};
|
|
672
|
+
node.log(`[Mesh->KNX] 发送新风开关: ${mapping.knxAddrCmd} = ${value ? 'ON' : 'OFF'}`);
|
|
673
|
+
}
|
|
674
|
+
// 新风风速
|
|
675
|
+
else if (type === 'fresh_air_speed') {
|
|
676
|
+
const speedAddr = mapping.knxAddrFanSpeed || mapping.knxAddrStatus || mapping.knxAddrCmd;
|
|
677
|
+
if (speedAddr) {
|
|
678
|
+
// Mesh风速: 1=高, 2=中, 3=低, 4=自动 -> 百分比
|
|
679
|
+
const speedMap = { 1: 100, 2: 66, 3: 33, 4: 50 };
|
|
680
|
+
knxMsg = {
|
|
681
|
+
topic: speedAddr,
|
|
682
|
+
payload: speedMap[value] || 50,
|
|
683
|
+
knx: { destination: speedAddr, dpt: '5.001', action: 'write' }
|
|
684
|
+
};
|
|
685
|
+
node.log(`[Mesh->KNX] 发送新风风速: ${speedAddr} = ${speedMap[value] || 50}%`);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
// 地暖开关
|
|
689
|
+
else if (type === 'floor_heating_switch') {
|
|
690
|
+
knxMsg = {
|
|
691
|
+
topic: mapping.knxAddrCmd,
|
|
692
|
+
payload: value === true || value === 1,
|
|
693
|
+
dpt: '1.001',
|
|
694
|
+
knx: { destination: mapping.knxAddrCmd, dpt: '1.001', action: 'write' }
|
|
695
|
+
};
|
|
696
|
+
node.log(`[Mesh->KNX] 发送地暖开关: ${mapping.knxAddrCmd} = ${value ? 'ON' : 'OFF'}`);
|
|
697
|
+
}
|
|
698
|
+
// 地暖温度
|
|
699
|
+
else if (type === 'floor_heating_temp') {
|
|
700
|
+
const tempAddr = mapping.knxAddrTemp || mapping.knxAddrStatus || mapping.knxAddrCmd;
|
|
701
|
+
if (tempAddr) {
|
|
702
|
+
knxMsg = {
|
|
703
|
+
topic: tempAddr,
|
|
704
|
+
payload: value,
|
|
705
|
+
knx: { destination: tempAddr, dpt: '9.001', action: 'write' }
|
|
706
|
+
};
|
|
707
|
+
node.log(`[Mesh->KNX] 发送地暖温度: ${tempAddr} = ${value}°C`);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
if (knxMsg) {
|
|
712
|
+
// knxUltimate官方格式: https://supergiovane.github.io/node-red-contrib-knx-ultimate/wiki/Device
|
|
713
|
+
// 必须包含: destination, payload, dpt, event
|
|
714
|
+
const sendMsg = {
|
|
715
|
+
destination: knxMsg.knx.destination,
|
|
716
|
+
payload: knxMsg.payload,
|
|
717
|
+
dpt: knxMsg.dpt || knxMsg.knx.dpt,
|
|
718
|
+
event: "GroupValue_Write"
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
// 调试信息
|
|
722
|
+
const debugMsg = {
|
|
723
|
+
topic: 'mesh-to-knx',
|
|
724
|
+
payload: {
|
|
725
|
+
direction: 'Mesh->KNX',
|
|
726
|
+
knxAddr: knxMsg.knx.destination,
|
|
727
|
+
value: knxMsg.payload,
|
|
728
|
+
dpt: knxMsg.dpt || knxMsg.knx.dpt,
|
|
729
|
+
meshMac: mapping.meshMac,
|
|
730
|
+
channel: mapping.meshChannel,
|
|
731
|
+
type: type
|
|
732
|
+
},
|
|
733
|
+
timestamp: new Date().toISOString()
|
|
734
|
+
};
|
|
735
|
+
|
|
736
|
+
node.log(`[Mesh->KNX] 发送: destination=${sendMsg.destination}, payload=${sendMsg.payload}, dpt=${sendMsg.dpt}, event=${sendMsg.event}`);
|
|
737
|
+
|
|
738
|
+
// 同时发送到两个输出端口(一次send调用)
|
|
739
|
+
node.send([sendMsg, debugMsg]);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
node.status({ fill: 'green', shape: 'dot', text: `Mesh→KNX ${node.mappings.length}个映射` });
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
// ========== KNX -> Mesh 同步 ==========
|
|
746
|
+
node.syncKnxToMesh = async function(cmd) {
|
|
747
|
+
const { mapping, type, value, key } = cmd;
|
|
748
|
+
|
|
749
|
+
// 记录发送时间
|
|
750
|
+
node.recordSyncTime('knx-to-mesh', key);
|
|
751
|
+
|
|
752
|
+
// 查找Mesh设备
|
|
753
|
+
const meshMac = mapping.meshMac || '';
|
|
754
|
+
const macNormalized = meshMac.toLowerCase().replace(/:/g, '');
|
|
755
|
+
let meshDevice = node.gateway.getDevice(meshMac);
|
|
756
|
+
|
|
757
|
+
if (!meshDevice) {
|
|
758
|
+
meshDevice = node.gateway.getDevice(macNormalized);
|
|
759
|
+
}
|
|
760
|
+
if (!meshDevice) {
|
|
761
|
+
const allDevices = node.gateway.deviceManager?.getAllDevices() || [];
|
|
762
|
+
meshDevice = allDevices.find(d => {
|
|
763
|
+
const devMac = (d.macAddress || '').toLowerCase().replace(/:/g, '');
|
|
764
|
+
return devMac === macNormalized;
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
if (!meshDevice) {
|
|
769
|
+
node.warn(`[KNX->Mesh] 未找到Mesh设备: ${meshMac}`);
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
try {
|
|
774
|
+
if (type === 'switch') {
|
|
775
|
+
const channel = mapping.meshChannel || 1;
|
|
776
|
+
const totalChannels = meshDevice.channels || 1;
|
|
777
|
+
const param = Buffer.from([totalChannels, channel, value ? 1 : 0]);
|
|
778
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x02, param);
|
|
779
|
+
node.log(`[KNX->Mesh] 发送开关: ${meshDevice.name} CH${channel} = ${value ? 'ON' : 'OFF'}`);
|
|
780
|
+
}
|
|
781
|
+
else if (type === 'cover_action') {
|
|
782
|
+
const action = value === 'open' ? 1 : value === 'close' ? 2 : 3;
|
|
783
|
+
const param = Buffer.from([action]);
|
|
784
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x05, param);
|
|
785
|
+
node.log(`[KNX->Mesh] 发送窗帘: ${meshDevice.name} = ${value}`);
|
|
786
|
+
}
|
|
787
|
+
else if (type === 'cover_position') {
|
|
788
|
+
const pos = mapping.invertPosition ? (100 - value) : value;
|
|
789
|
+
const param = Buffer.from([pos]);
|
|
790
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x06, param);
|
|
791
|
+
node.log(`[KNX->Mesh] 发送窗帘位置: ${meshDevice.name} = ${pos}%`);
|
|
792
|
+
}
|
|
793
|
+
// 调光灯开关
|
|
794
|
+
else if (type === 'light_switch') {
|
|
795
|
+
const param = Buffer.from([value ? 0x02 : 0x01]);
|
|
796
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x02, param);
|
|
797
|
+
node.log(`[KNX->Mesh] 发送调光灯开关: ${meshDevice.name} = ${value ? 'ON' : 'OFF'}`);
|
|
798
|
+
}
|
|
799
|
+
// 调光灯亮度
|
|
800
|
+
else if (type === 'light_brightness') {
|
|
801
|
+
const param = Buffer.from([value]);
|
|
802
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x03, param);
|
|
803
|
+
node.log(`[KNX->Mesh] 发送调光灯亮度: ${meshDevice.name} = ${value}%`);
|
|
804
|
+
}
|
|
805
|
+
// 调光灯色温
|
|
806
|
+
else if (type === 'light_color_temp') {
|
|
807
|
+
const param = Buffer.from([value]);
|
|
808
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x04, param);
|
|
809
|
+
node.log(`[KNX->Mesh] 发送调光灯色温: ${meshDevice.name} = ${value}`);
|
|
810
|
+
}
|
|
811
|
+
// 空调开关
|
|
812
|
+
else if (type === 'climate_switch') {
|
|
813
|
+
const param = Buffer.from([value ? 0x01 : 0x00]);
|
|
814
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x1E, param);
|
|
815
|
+
node.log(`[KNX->Mesh] 发送空调开关: ${meshDevice.name} = ${value ? 'ON' : 'OFF'}`);
|
|
816
|
+
}
|
|
817
|
+
// 空调温度
|
|
818
|
+
else if (type === 'climate_temp') {
|
|
819
|
+
const param = Buffer.from([Math.round(value)]);
|
|
820
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x1B, param);
|
|
821
|
+
node.log(`[KNX->Mesh] 发送空调温度: ${meshDevice.name} = ${value}°C`);
|
|
822
|
+
}
|
|
823
|
+
// 空调模式
|
|
824
|
+
else if (type === 'climate_mode') {
|
|
825
|
+
const param = Buffer.from([value]);
|
|
826
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x1D, param);
|
|
827
|
+
node.log(`[KNX->Mesh] 发送空调模式: ${meshDevice.name} = ${value}`);
|
|
828
|
+
}
|
|
829
|
+
// 空调风速
|
|
830
|
+
else if (type === 'climate_fan') {
|
|
831
|
+
const param = Buffer.from([value]);
|
|
832
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x1C, param);
|
|
833
|
+
node.log(`[KNX->Mesh] 发送空调风速: ${meshDevice.name} = ${value}`);
|
|
834
|
+
}
|
|
835
|
+
// 新风开关
|
|
836
|
+
else if (type === 'fresh_air_switch') {
|
|
837
|
+
const param = Buffer.from([value ? 0x01 : 0x00]);
|
|
838
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x68, param);
|
|
839
|
+
node.log(`[KNX->Mesh] 发送新风开关: ${meshDevice.name} = ${value ? 'ON' : 'OFF'}`);
|
|
840
|
+
}
|
|
841
|
+
// 新风风速
|
|
842
|
+
else if (type === 'fresh_air_speed') {
|
|
843
|
+
const param = Buffer.from([value]);
|
|
844
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x68, param);
|
|
845
|
+
node.log(`[KNX->Mesh] 发送新风风速: ${meshDevice.name} = ${value}`);
|
|
846
|
+
}
|
|
847
|
+
// 地暖开关
|
|
848
|
+
else if (type === 'floor_heating_switch') {
|
|
849
|
+
const param = Buffer.from([value ? 0x01 : 0x00]);
|
|
850
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x6B, param);
|
|
851
|
+
node.log(`[KNX->Mesh] 发送地暖开关: ${meshDevice.name} = ${value ? 'ON' : 'OFF'}`);
|
|
852
|
+
}
|
|
853
|
+
// 地暖温度
|
|
854
|
+
else if (type === 'floor_heating_temp') {
|
|
855
|
+
const param = Buffer.from([Math.round(value)]);
|
|
856
|
+
await node.gateway.sendControl(meshDevice.networkAddress, 0x6B, param);
|
|
857
|
+
node.log(`[KNX->Mesh] 发送地暖温度: ${meshDevice.name} = ${value}°C`);
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// 输出调试信息
|
|
861
|
+
node.send([null, {
|
|
862
|
+
topic: 'knx-to-mesh',
|
|
863
|
+
payload: {
|
|
864
|
+
direction: 'KNX→Mesh',
|
|
865
|
+
mapping: {
|
|
866
|
+
meshMac: mapping.meshMac,
|
|
867
|
+
channel: mapping.meshChannel,
|
|
868
|
+
knxAddr: cmd.sourceAddr
|
|
869
|
+
},
|
|
870
|
+
type: type,
|
|
871
|
+
value: value
|
|
872
|
+
},
|
|
873
|
+
timestamp: new Date().toISOString()
|
|
874
|
+
}]);
|
|
875
|
+
|
|
876
|
+
} catch (err) {
|
|
877
|
+
node.error(`[KNX->Mesh] 发送失败: ${err.message}`);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
node.status({ fill: 'blue', shape: 'dot', text: `KNX→Mesh ${node.mappings.length}个映射` });
|
|
881
|
+
};
|
|
882
|
+
|
|
883
|
+
// ========== 处理输入消息(来自KNX节点) ==========
|
|
884
|
+
node.on('input', function(msg, send, done) {
|
|
885
|
+
if (node.initializing) {
|
|
886
|
+
done && done();
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// 从消息中提取KNX组地址
|
|
891
|
+
const groupAddr = msg.knx?.destination || msg.topic || '';
|
|
892
|
+
const value = msg.payload;
|
|
893
|
+
const dpt = node.normalizeDpt(msg.knx?.dpt || msg.dpt || '');
|
|
894
|
+
|
|
895
|
+
if (!groupAddr) {
|
|
896
|
+
done && done();
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// 查找映射
|
|
901
|
+
const mapping = node.findKnxMapping(groupAddr);
|
|
902
|
+
if (!mapping) {
|
|
903
|
+
node.debug(`[KNX输入] 未找到映射: ${groupAddr}`);
|
|
904
|
+
done && done();
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
// 确定地址功能(优先使用地址匹配,比DPT更可靠)
|
|
909
|
+
const addrFunc = node.getKnxAddrFunction(mapping, groupAddr);
|
|
910
|
+
const loopKey = `${mapping.meshMac}_${mapping.meshChannel}`;
|
|
911
|
+
|
|
912
|
+
// 防死循环检查
|
|
913
|
+
if (node.shouldPreventSync('knx-to-mesh', loopKey)) {
|
|
914
|
+
node.debug(`[KNX->Mesh] 跳过同步(防死循环): ${loopKey}, addr=${groupAddr}`);
|
|
915
|
+
done && done();
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
node.log(`[KNX输入] 收到: ${groupAddr} = ${value}, DPT=${dpt}, 功能=${addrFunc}, 设备=${mapping.deviceType}`);
|
|
920
|
+
|
|
921
|
+
// 根据设备类型和地址功能处理
|
|
922
|
+
if (mapping.deviceType === 'switch') {
|
|
923
|
+
// 开关命令(只处理cmd地址)
|
|
924
|
+
if (addrFunc === 'cmd' || addrFunc === 'status') {
|
|
925
|
+
const switchValue = (value === 1 || value === true || value === 'on' || value === 'ON');
|
|
926
|
+
node.queueCommand({
|
|
927
|
+
direction: 'knx-to-mesh',
|
|
928
|
+
mapping: mapping,
|
|
929
|
+
type: 'switch',
|
|
930
|
+
value: switchValue,
|
|
931
|
+
key: loopKey,
|
|
932
|
+
sourceAddr: groupAddr
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
// 窗帘设备
|
|
937
|
+
else if (mapping.deviceType === 'cover') {
|
|
938
|
+
if (addrFunc === 'cmd') {
|
|
939
|
+
// 上下命令: 0=上(开), 1=下(关)
|
|
940
|
+
const action = (value === 0 || value === false) ? 'open' : 'close';
|
|
941
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'cover_action', value: action, key: loopKey, sourceAddr: groupAddr });
|
|
942
|
+
}
|
|
943
|
+
else if (addrFunc === 'stop') {
|
|
944
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'cover_action', value: 'stop', key: loopKey, sourceAddr: groupAddr });
|
|
945
|
+
}
|
|
946
|
+
else if (addrFunc === 'position' || addrFunc === 'status') {
|
|
947
|
+
const pos = mapping.invertPosition ? (100 - (parseInt(value) || 0)) : (parseInt(value) || 0);
|
|
948
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'cover_position', value: pos, key: loopKey, sourceAddr: groupAddr });
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
// 调光灯设备
|
|
952
|
+
else if (mapping.deviceType.startsWith('light_')) {
|
|
953
|
+
if (addrFunc === 'cmd' || addrFunc === 'status') {
|
|
954
|
+
// 开关
|
|
955
|
+
const sw = (value === 1 || value === true);
|
|
956
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'light_switch', value: sw, key: loopKey, sourceAddr: groupAddr });
|
|
957
|
+
}
|
|
958
|
+
else if (addrFunc === 'brightness') {
|
|
959
|
+
// 亮度 (KNX 0-255 -> Mesh 0-100)
|
|
960
|
+
const brightness = Math.round((parseInt(value) || 0) * 100 / 255);
|
|
961
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'light_brightness', value: brightness, key: loopKey, sourceAddr: groupAddr });
|
|
962
|
+
}
|
|
963
|
+
else if (addrFunc === 'colorTemp') {
|
|
964
|
+
// 色温
|
|
965
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'light_color_temp', value: parseInt(value) || 50, key: loopKey, sourceAddr: groupAddr });
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
// 空调设备
|
|
969
|
+
else if (mapping.deviceType === 'climate') {
|
|
970
|
+
if (addrFunc === 'cmd') {
|
|
971
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'climate_switch', value: value === 1 || value === true, key: loopKey, sourceAddr: groupAddr });
|
|
972
|
+
}
|
|
973
|
+
else if (addrFunc === 'temp' || addrFunc === 'status') {
|
|
974
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'climate_temp', value: parseFloat(value) || 24, key: loopKey, sourceAddr: groupAddr });
|
|
975
|
+
}
|
|
976
|
+
else if (addrFunc === 'mode') {
|
|
977
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'climate_mode', value: parseInt(value) || 1, key: loopKey, sourceAddr: groupAddr });
|
|
978
|
+
}
|
|
979
|
+
else if (addrFunc === 'fanSpeed') {
|
|
980
|
+
// 风速
|
|
981
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'climate_fan', value: parseInt(value) || 1, key: loopKey, sourceAddr: groupAddr });
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
// 新风设备
|
|
985
|
+
else if (mapping.deviceType === 'fresh_air') {
|
|
986
|
+
if (addrFunc === 'cmd') {
|
|
987
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'fresh_air_switch', value: value === 1 || value === true, key: loopKey, sourceAddr: groupAddr });
|
|
988
|
+
}
|
|
989
|
+
else if (addrFunc === 'fanSpeed' || addrFunc === 'status') {
|
|
990
|
+
// 百分比转风速: >66=高(1), >33=中(2), <=33=低(3)
|
|
991
|
+
const pct = parseInt(value) || 0;
|
|
992
|
+
const speed = pct > 66 ? 1 : pct > 33 ? 2 : 3;
|
|
993
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'fresh_air_speed', value: speed, key: loopKey, sourceAddr: groupAddr });
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
// 地暖设备
|
|
997
|
+
else if (mapping.deviceType === 'floor_heating') {
|
|
998
|
+
if (addrFunc === 'cmd') {
|
|
999
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'floor_heating_switch', value: value === 1 || value === true, key: loopKey, sourceAddr: groupAddr });
|
|
1000
|
+
}
|
|
1001
|
+
else if (addrFunc === 'temp' || addrFunc === 'status') {
|
|
1002
|
+
node.queueCommand({ direction: 'knx-to-mesh', mapping, type: 'floor_heating_temp', value: parseFloat(value) || 24, key: loopKey, sourceAddr: groupAddr });
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
done && done();
|
|
1007
|
+
});
|
|
1008
|
+
|
|
1009
|
+
// ========== 事件监听 ==========
|
|
1010
|
+
|
|
1011
|
+
// 监听网关连接状态
|
|
1012
|
+
node.gateway.on('gateway-connected', () => {
|
|
1013
|
+
node.status({ fill: 'green', shape: 'ring', text: `网关已连接 ${node.mappings.length}个映射` });
|
|
1014
|
+
});
|
|
1015
|
+
|
|
1016
|
+
node.gateway.on('gateway-disconnected', () => {
|
|
1017
|
+
node.status({ fill: 'red', shape: 'ring', text: '网关断开' });
|
|
1018
|
+
});
|
|
1019
|
+
|
|
1020
|
+
// 监听Mesh设备状态变化
|
|
1021
|
+
node.gateway.on('device-state-changed', handleMeshStateChange);
|
|
1022
|
+
|
|
1023
|
+
// ========== 场景执行事件处理 ==========
|
|
1024
|
+
// 当收到场景执行通知时,查询所有已映射设备的状态
|
|
1025
|
+
const handleSceneExecuted = (eventData) => {
|
|
1026
|
+
if (node.initializing) return;
|
|
1027
|
+
|
|
1028
|
+
node.log(`[场景执行] 检测到场景${eventData.sceneId}执行,延迟查询设备状态`);
|
|
1029
|
+
|
|
1030
|
+
// 延迟300ms后查询设备状态,给设备执行时间
|
|
1031
|
+
setTimeout(async () => {
|
|
1032
|
+
// 收集所有已映射的唯一设备地址
|
|
1033
|
+
const mappedAddresses = new Set();
|
|
1034
|
+
for (const mapping of node.mappings) {
|
|
1035
|
+
const mac = node.normalizeMac(mapping.meshMac);
|
|
1036
|
+
const device = node.gateway.deviceManager.getDeviceByMac(mac);
|
|
1037
|
+
if (device && device.networkAddress) {
|
|
1038
|
+
mappedAddresses.add(device.networkAddress);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
if (mappedAddresses.size === 0) {
|
|
1043
|
+
node.debug(`[场景执行] 没有已映射的设备需要查询`);
|
|
1044
|
+
return;
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
node.log(`[场景执行] 查询${mappedAddresses.size}个设备的状态`);
|
|
1048
|
+
|
|
1049
|
+
// 逐个查询设备状态
|
|
1050
|
+
for (const addr of mappedAddresses) {
|
|
1051
|
+
try {
|
|
1052
|
+
// 查询开关状态 (0x02)
|
|
1053
|
+
const queryFrame = node.gateway.protocolHandler.buildDeviceStatusQueryFrame(addr, 0x02);
|
|
1054
|
+
await node.gateway.client.sendFrame(queryFrame, 2);
|
|
1055
|
+
await node.sleep(100);
|
|
1056
|
+
} catch (err) {
|
|
1057
|
+
node.debug(`[场景执行] 查询设备0x${addr.toString(16)}状态失败: ${err.message}`);
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
}, 300);
|
|
1061
|
+
};
|
|
1062
|
+
|
|
1063
|
+
node.gateway.on('scene-executed', handleSceneExecuted);
|
|
1064
|
+
|
|
1065
|
+
// ========== 清理 ==========
|
|
1066
|
+
node.on('close', function(done) {
|
|
1067
|
+
// 清除初始化定时器
|
|
1068
|
+
if (node.initTimer) {
|
|
1069
|
+
clearTimeout(node.initTimer);
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
// 移除事件监听
|
|
1073
|
+
if (node.gateway) {
|
|
1074
|
+
node.gateway.removeListener('device-state-changed', handleMeshStateChange);
|
|
1075
|
+
node.gateway.removeListener('scene-executed', handleSceneExecuted);
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
// 清空队列和缓存
|
|
1079
|
+
node.commandQueue = [];
|
|
1080
|
+
node.stateCache = {};
|
|
1081
|
+
node.knxStateCache = {};
|
|
1082
|
+
node.lastMeshToKnx = {};
|
|
1083
|
+
node.lastKnxToMesh = {};
|
|
1084
|
+
|
|
1085
|
+
node.status({});
|
|
1086
|
+
done();
|
|
1087
|
+
});
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
RED.nodes.registerType('symi-knx-bridge', SymiKNXBridgeNode);
|
|
1091
|
+
|
|
1092
|
+
// ========== HTTP API:获取Mesh设备列表 ==========
|
|
1093
|
+
RED.httpAdmin.get('/symi-knx-bridge/devices/:gatewayId', function(req, res) {
|
|
1094
|
+
const gatewayNode = RED.nodes.getNode(req.params.gatewayId);
|
|
1095
|
+
if (!gatewayNode || !gatewayNode.deviceManager) {
|
|
1096
|
+
return res.json([]);
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
const devices = gatewayNode.deviceManager.getAllDevices();
|
|
1100
|
+
const result = devices.map(d => ({
|
|
1101
|
+
mac: d.macAddress,
|
|
1102
|
+
name: d.name,
|
|
1103
|
+
type: d.getEntityType(),
|
|
1104
|
+
channels: d.channels,
|
|
1105
|
+
online: d.online
|
|
1106
|
+
}));
|
|
1107
|
+
|
|
1108
|
+
res.json(result);
|
|
1109
|
+
});
|
|
1110
|
+
};
|