trphoenix
2025-12-11 a7eb97fc18f24c13b3c79350b1941f429f41150b
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
[
    {
        "id": "200c026180f22a70",
        "type": "tab",
        "label": "新安国际集中供液",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "ha_integration_tab",
        "type": "tab",
        "label": "HomeAssistant集成",
        "disabled": false,
        "info": "集中供液数据发送到HomeAssistant",
        "env": []
    },
    {
        "id": "086467396a2eed2c",
        "type": "modbus-client",
        "name": "modbus-slave-新安集中供液",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "failureLogEnabled": true,
        "tcpHost": "192.168.50.91",
        "tcpPort": "5020",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": 9600,
        "serialDatabits": 8,
        "serialStopbits": 1,
        "serialParity": "none",
        "serialConnectionDelay": 100,
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": 1,
        "commandDelay": 1,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 2000,
        "parallelUnitIdsAllowed": true,
        "showErrors": false,
        "showWarnings": true,
        "showLogs": true
    },
    {
        "id": "mqtt_broker_ha",
        "type": "mqtt-broker",
        "name": "HomeAssistant MQTT",
        "broker": "192.168.50.83",
        "port": "1883",
        "clientid": "nodered_central_liquid",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "homeassistant/status",
        "birthQos": "0",
        "birthPayload": "online",
        "birthMsg": {},
        "closeTopic": "homeassistant/status",
        "closeQos": "0",
        "closePayload": "offline",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "72a90d52a45530d0",
        "type": "function",
        "z": "200c026180f22a70",
        "name": "通用解析节点",
        "func": "\nconst registers = msg.payload;\n// node.warn(`解析${registers}`);\n\n// 检查是否为状态数据(布尔类型或布尔数组)\nif (typeof registers === 'boolean' ||\n    (Array.isArray(registers) && registers.length === 1 && typeof registers[0] === 'boolean') ||\n    (Array.isArray(registers) && registers.length === 1 && typeof registers[0] === 'number' && (registers[0] === 0 || registers[0] === 1)) ||\n    (Array.isArray(registers) && registers.length > 1 && typeof registers[0] === 'boolean')) {\n\n    // 处理布尔状态数据\n    let boolValue;\n    if (typeof registers === 'boolean') {\n        boolValue = registers;\n    } else if (Array.isArray(registers) && registers.length === 1) {\n        boolValue = typeof registers[0] === 'boolean' ? registers[0] : (registers[0] === 1);\n    } else if (Array.isArray(registers) && registers.length > 1 && typeof registers[0] === 'boolean') {\n        // 对于多个布尔值的数组,取第一个值作为主要状态\n        boolValue = registers[0];\n        // node.warn(`布尔数组长度: ${registers.length}, 取第一个值: ${boolValue}`);\n    }\n\n    // 根据 topic 确定状态类型\n    let dataType = \"未知状态\";\n    let statusValue = boolValue;\n\n    if (msg.topic) {\n        if (msg.topic.includes(\"工作状态\") || msg.topic.includes(\"work\") || msg.topic.includes(\"Q1.6\")) {\n            dataType = \"工作状态\";\n            statusValue = boolValue ? \"运行\" : \"停止\";\n            msg.workStatus = statusValue;\n        } else if (msg.topic.includes(\"报警状态\") || msg.topic.includes(\"alarm\") || msg.topic.includes(\"Q1.5\")) {\n            dataType = \"报警状态\";\n            statusValue = boolValue ? \"报警\" : \"正常\";\n            msg.alarmStatus = statusValue;\n        } else if (msg.topic.includes(\"状态\") || msg.topic.includes(\"status\")) {\n            dataType = \"设备状态\";\n            statusValue = boolValue ? \"激活\" : \"关闭\";\n            msg.status = statusValue;\n        }\n    } else {\n        // 如果没有 topic,使用通用状态字段\n        msg.status = boolValue;\n        dataType = \"状态\";\n    }\n\n    // 打印解析结果到调试控制台\n    // node.warn(`解析${dataType} ---> ${statusValue}`);\n\n    // 保留原始的 payload,添加解析后的值\n    msg.originalPayload = msg.payload;\n    msg.payload = statusValue;\n\n    return msg;\n}\n\n// 检查寄存器长度(浮点数解析)\nif (!Array.isArray(registers) || registers.length !== 2) {\n    node.warn(\"寄存器数量不足,无法解析浮点数!\");\n    return null;\n}\n\n// 处理 Modbus 寄存器数据\n// 寄存器数据按 Big Endian 字序存储,但浮点数需要按 Little Endian 读取\nconst highRegister = registers[0]; // 高16位寄存器\nconst lowRegister = registers[1];  // 低16位寄存器\n\n// 创建一个 4 字节缓冲区\nconst buffer = Buffer.alloc(4);\n\n// 按照 Big Endian 方式写入寄存器数据\nbuffer.writeUInt16BE(highRegister, 0); // 写入高16位到前两个字节\nbuffer.writeUInt16BE(lowRegister, 2);  // 写入低16位到后两个字节\n\n// 按照 Little Endian 方式读取 IEEE 754 浮点数\nconst rawValue = buffer.readFloatLE(0);\n\n// 保留两位小数\nconst value = Math.round(rawValue * 100) / 100;\n\n// 根据输入消息的 topic 或其他标识来确定数据类型\nlet dataType = \"未知数据\";\nif (msg.topic) {\n    if (msg.topic.includes(\"温度\") || msg.topic.includes(\"temperature\")) {\n        dataType = \"温度\";\n        msg.temperature = value;\n    } else if (msg.topic.includes(\"PH\") || msg.topic.includes(\"ph\")) {\n        dataType = \"PH值\";\n        msg.ph = value;\n    } else if (msg.topic.includes(\"液位\") || msg.topic.includes(\"level\")) {\n        dataType = \"液位\";\n        msg.level = value;\n    } else if (msg.topic.includes(\"电导率\") || msg.topic.includes(\"conductivity\")) {\n        dataType = \"电导率\";\n        msg.conductivity = value;\n    } else if (msg.topic.includes(\"压力\") || msg.topic.includes(\"pressure\")) {\n        dataType = \"压力\";\n        msg.pressure = value;\n    }\n} else {\n    // 如果没有 topic,使用通用字段名\n    msg.value = value;\n    dataType = \"数值\";\n}\n\n// 打印解析结果到调试控制台\n// node.warn(`解析${dataType} ---> ${value}`);\n\n// 保留原始的 payload,添加解析后的值\nmsg.originalPayload = msg.payload;\nmsg.payload = value;\n\n// 返回 msg 对象,供后续节点使用\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 420,
        "wires": [
            [
                "af0930278623437b"
            ]
        ]
    },
    {
        "id": "af0930278623437b",
        "type": "switch",
        "z": "200c026180f22a70",
        "name": "节点分流",
        "property": "topic",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "温度",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "PH",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "储液当前液位",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "电导率",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "供液一压力",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "供液二压力",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "报警状态",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "工作状态",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 8,
        "x": 700,
        "y": 420,
        "wires": [
            [
                "fa5417377a61b71e",
                "link_temperature_out"
            ],
            [
                "1490bcab1bec54c1",
                "link_ph_out"
            ],
            [
                "84475a694e701126",
                "link_level_out"
            ],
            [
                "7c6dae0c8ca8c7aa",
                "link_conductivity_out"
            ],
            [
                "095d8a5fee72b512",
                "link_pressure1_out"
            ],
            [
                "f82ed17f4277b2e2",
                "link_pressure2_out"
            ],
            [
                "aff2063ee6398034",
                "link_alarm_out"
            ],
            [
                "c44e177352a6f455",
                "link_work_status_out"
            ]
        ]
    },
    {
        "id": "7bec428b86e8c8bc",
        "type": "modbus-read",
        "z": "200c026180f22a70",
        "name": "中央供液-slave-温度",
        "topic": "温度",
        "showStatusActivities": true,
        "logIOActivities": false,
        "showErrors": true,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "23",
        "quantity": "2",
        "rate": "5",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "086467396a2eed2c",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": true,
        "x": 150,
        "y": 240,
        "wires": [
            [
                "72a90d52a45530d0"
            ],
            []
        ]
    },
    {
        "id": "fa5417377a61b71e",
        "type": "debug",
        "z": "200c026180f22a70",
        "name": "debug-温度",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1110,
        "y": 60,
        "wires": []
    },
    {
        "id": "fb1f1d999f707aa2",
        "type": "modbus-read",
        "z": "200c026180f22a70",
        "name": "中央供液-slave-PH值",
        "topic": "PH值",
        "showStatusActivities": true,
        "logIOActivities": false,
        "showErrors": true,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "160",
        "quantity": "2",
        "rate": "5",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "086467396a2eed2c",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": true,
        "x": 160,
        "y": 300,
        "wires": [
            [
                "72a90d52a45530d0"
            ],
            []
        ]
    },
    {
        "id": "1490bcab1bec54c1",
        "type": "debug",
        "z": "200c026180f22a70",
        "name": "debug-PH值",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1110,
        "y": 100,
        "wires": []
    },
    {
        "id": "bb17984ec96509da",
        "type": "modbus-read",
        "z": "200c026180f22a70",
        "name": "中央供液-slave-储液当前液位",
        "topic": "储液当前液位",
        "showStatusActivities": true,
        "logIOActivities": false,
        "showErrors": true,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "128",
        "quantity": "2",
        "rate": "5",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "086467396a2eed2c",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": true,
        "x": 180,
        "y": 360,
        "wires": [
            [
                "72a90d52a45530d0"
            ],
            []
        ]
    },
    {
        "id": "84475a694e701126",
        "type": "debug",
        "z": "200c026180f22a70",
        "name": "debug-储液当前液位",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1120,
        "y": 140,
        "wires": []
    },
    {
        "id": "805bb39037771c2d",
        "type": "modbus-read",
        "z": "200c026180f22a70",
        "name": "中央供液-slave-电导率",
        "topic": "电导率",
        "showStatusActivities": true,
        "logIOActivities": false,
        "showErrors": true,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "84",
        "quantity": "2",
        "rate": "5",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "086467396a2eed2c",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": true,
        "x": 160,
        "y": 420,
        "wires": [
            [
                "72a90d52a45530d0"
            ],
            []
        ]
    },
    {
        "id": "7c6dae0c8ca8c7aa",
        "type": "debug",
        "z": "200c026180f22a70",
        "name": "debug-电导率",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1100,
        "y": 340,
        "wires": []
    },
    {
        "id": "56e20ad9b8718468",
        "type": "modbus-read",
        "z": "200c026180f22a70",
        "name": "中央供液-slave-供液一压力",
        "topic": "供液一压力",
        "showStatusActivities": true,
        "logIOActivities": false,
        "showErrors": true,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "112",
        "quantity": "2",
        "rate": "5",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "086467396a2eed2c",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": true,
        "x": 170,
        "y": 480,
        "wires": [
            [
                "72a90d52a45530d0"
            ],
            []
        ]
    },
    {
        "id": "095d8a5fee72b512",
        "type": "debug",
        "z": "200c026180f22a70",
        "name": "debug-供液一压力",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1110,
        "y": 380,
        "wires": []
    },
    {
        "id": "2923878ddaa66ef8",
        "type": "modbus-read",
        "z": "200c026180f22a70",
        "name": "中央供液-slave-供液二压力",
        "topic": "供液二压力",
        "showStatusActivities": true,
        "logIOActivities": false,
        "showErrors": true,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "116",
        "quantity": "2",
        "rate": "5",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "086467396a2eed2c",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": true,
        "x": 170,
        "y": 540,
        "wires": [
            [
                "72a90d52a45530d0"
            ],
            []
        ]
    },
    {
        "id": "f82ed17f4277b2e2",
        "type": "debug",
        "z": "200c026180f22a70",
        "name": "debug-供液二压力",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1110,
        "y": 420,
        "wires": []
    },
    {
        "id": "fe307fc992aa444d",
        "type": "modbus-read",
        "z": "200c026180f22a70",
        "name": "中央供液-slave-报警状态",
        "topic": "报警状态",
        "showStatusActivities": true,
        "logIOActivities": false,
        "showErrors": true,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "Coil",
        "adr": "13",
        "quantity": "1",
        "rate": "5",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "086467396a2eed2c",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": true,
        "x": 170,
        "y": 600,
        "wires": [
            [
                "72a90d52a45530d0"
            ],
            []
        ]
    },
    {
        "id": "aff2063ee6398034",
        "type": "debug",
        "z": "200c026180f22a70",
        "name": "debug-报警状态",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1100,
        "y": 460,
        "wires": []
    },
    {
        "id": "5ab73337424b189a",
        "type": "modbus-read",
        "z": "200c026180f22a70",
        "name": "中央供液-slave-工作状态",
        "topic": "工作状态",
        "showStatusActivities": true,
        "logIOActivities": false,
        "showErrors": true,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "Coil",
        "adr": "14",
        "quantity": "1",
        "rate": "5",
        "rateUnit": "s",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "086467396a2eed2c",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": true,
        "x": 170,
        "y": 660,
        "wires": [
            [
                "72a90d52a45530d0"
            ],
            []
        ]
    },
    {
        "id": "c44e177352a6f455",
        "type": "debug",
        "z": "200c026180f22a70",
        "name": "debug-工作状态",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1100,
        "y": 500,
        "wires": []
    },
    {
        "id": "link_temperature_out",
        "type": "link out",
        "z": "200c026180f22a70",
        "name": "发送温度到HA",
        "mode": "link",
        "links": [
            "link_ha_temperature_in"
        ],
        "x": 885,
        "y": 60,
        "wires": []
    },
    {
        "id": "link_ph_out",
        "type": "link out",
        "z": "200c026180f22a70",
        "name": "发送PH值到HA",
        "mode": "link",
        "links": [
            "link_ha_ph_in"
        ],
        "x": 885,
        "y": 100,
        "wires": []
    },
    {
        "id": "link_level_out",
        "type": "link out",
        "z": "200c026180f22a70",
        "name": "发送液位到HA",
        "mode": "link",
        "links": [
            "link_ha_level_in"
        ],
        "x": 885,
        "y": 140,
        "wires": []
    },
    {
        "id": "link_conductivity_out",
        "type": "link out",
        "z": "200c026180f22a70",
        "name": "发送电导率到HA",
        "mode": "link",
        "links": [
            "link_ha_conductivity_in"
        ],
        "x": 965,
        "y": 340,
        "wires": []
    },
    {
        "id": "link_pressure1_out",
        "type": "link out",
        "z": "200c026180f22a70",
        "name": "发送供液一压力到HA",
        "mode": "link",
        "links": [
            "link_ha_pressure1_in"
        ],
        "x": 965,
        "y": 380,
        "wires": []
    },
    {
        "id": "link_pressure2_out",
        "type": "link out",
        "z": "200c026180f22a70",
        "name": "发送供液二压力到HA",
        "mode": "link",
        "links": [
            "link_ha_pressure2_in"
        ],
        "x": 965,
        "y": 420,
        "wires": []
    },
    {
        "id": "link_alarm_out",
        "type": "link out",
        "z": "200c026180f22a70",
        "name": "发送报警状态到HA",
        "mode": "link",
        "links": [
            "link_ha_alarm_in"
        ],
        "x": 965,
        "y": 460,
        "wires": []
    },
    {
        "id": "link_work_status_out",
        "type": "link out",
        "z": "200c026180f22a70",
        "name": "发送工作状态到HA",
        "mode": "link",
        "links": [
            "link_ha_work_in"
        ],
        "x": 965,
        "y": 500,
        "wires": []
    },
    {
        "id": "ha_discovery_function",
        "type": "function",
        "z": "ha_integration_tab",
        "name": "HomeAssistant自动发现配置",
        "func": "// HomeAssistant MQTT 自动发现配置 - 无分割版本\n// 每个传感器单独发送,避免分割节点问题\n\nconst deviceId = \"central_liquid_supply\";\nconst deviceName = \"新安国际集中供液系统\";\nconst baseTopic = \"homeassistant\";\n\n// 设备基本信息\nconst device = {\n    identifiers: [deviceId],\n    name: deviceName,\n    manufacturer: \"新安国际\",\n    model: \"集中供液系统\",\n    sw_version: \"1.0\"\n};\n\n// 所有传感器配置\nconst allSensors = [\n    { type: \"sensor\", name: \"温度\", entity_id: \"temperature\", unit: \"°C\", device_class: \"temperature\" },\n    { type: \"sensor\", name: \"PH值\", entity_id: \"ph_value\", unit: \"pH\" },\n    { type: \"sensor\", name: \"电导率\", entity_id: \"conductivity\", unit: \"μS/cm\" },\n    { type: \"sensor\", name: \"储液当前液位\", entity_id: \"storage_level\", unit: \"L\" },\n    { type: \"sensor\", name: \"供液一压力\", entity_id: \"supply_pressure_1\", unit: \"kPa\", device_class: \"pressure\" },\n    { type: \"sensor\", name: \"供液二压力\", entity_id: \"supply_pressure_2\", unit: \"kPa\", device_class: \"pressure\" },\n    { type: \"binary_sensor\", name: \"工作状态\", entity_id: \"work_status\", device_class: \"running\" },\n    { type: \"binary_sensor\", name: \"报警状态\", entity_id: \"alarm_status\", device_class: \"problem\" }\n];\n\nnode.warn(`开始处理 ${allSensors.length} 个传感器配置`);\n\n// 逐个发送传感器配置\nallSensors.forEach((sensor, index) => {\n    const config = {\n        name: sensor.name,\n        unique_id: `${deviceId}_${sensor.entity_id}`,\n        state_topic: `${deviceId}/${sensor.type}/${sensor.entity_id}/state`,\n        device: device\n    };\n\n    // 根据传感器类型添加特定配置\n    if (sensor.type === \"sensor\") {\n        config.state_class = \"measurement\";\n        if (sensor.unit) config.unit_of_measurement = sensor.unit;\n        if (sensor.device_class) config.device_class = sensor.device_class;\n    } else if (sensor.type === \"binary_sensor\") {\n        config.device_class = sensor.device_class;\n        config.payload_on = sensor.entity_id === \"work_status\" ? \"运行\" : \"报警\";\n        config.payload_off = sensor.entity_id === \"work_status\" ? \"停止\" : \"正常\";\n    }\n\n    const mqttTopic = `${baseTopic}/${sensor.type}/${deviceId}_${sensor.entity_id}/config`;\n    const mqttPayload = JSON.stringify(config);\n\n    node.warn(`发送配置 ${index + 1}/${allSensors.length}: ${sensor.name}`);\n    node.warn(`  主题: ${mqttTopic}`);\n\n    // 立即发送MQTT消息\n    node.send({\n        topic: mqttTopic,\n        payload: mqttPayload,\n        retain: true\n    });\n\n    // 添加小延迟,确保消息按顺序发送\n    setTimeout(() => {\n        if (index === allSensors.length - 1) {\n            // 最后发送设备状态\n            node.send({\n                topic: `${deviceId}/status`,\n                payload: \"online\",\n                retain: true\n            });\n            node.warn(\"所有自动发现配置已发送完成\");\n        }\n    }, index * 100);\n});\n\nreturn null; // 不通过return发送,直接通过node.send发送\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 240,
        "y": 140,
        "wires": [
            [
                "25565ecbf48f0308",
                "ha_discovery_mqtt"
            ]
        ]
    },
    {
        "id": "ha_discovery_mqtt",
        "type": "mqtt out",
        "z": "ha_integration_tab",
        "name": "发送自动发现配置",
        "topic": "",
        "qos": "0",
        "retain": "true",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "mqtt_broker_ha",
        "x": 610,
        "y": 120,
        "wires": []
    },
    {
        "id": "ha_discovery_trigger",
        "type": "inject",
        "z": "ha_integration_tab",
        "name": "启动自动发现",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": "5",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 80,
        "wires": [
            [
                "ha_discovery_function"
            ]
        ]
    },
    {
        "id": "ha_data_processor",
        "type": "function",
        "z": "ha_integration_tab",
        "name": "数据处理器",
        "func": "// 处理来自集中供液系统的数据\nconst deviceId = \"central_liquid_supply\";\nconst topic = msg.topic;\nconst value = msg.payload;\n\n// 根据 topic 确定实体类型和 ID\nlet entityType = \"sensor\";\nlet entityId = \"\";\nlet stateTopic = \"\";\n\nif (topic.includes(\"温度\")) {\n    entityId = \"temperature\";\n    stateTopic = `${deviceId}/sensor/${entityId}/state`;\n} else if (topic.includes(\"PH\")) {\n    entityId = \"ph_value\";\n    stateTopic = `${deviceId}/sensor/${entityId}/state`;\n} else if (topic.includes(\"电导率\")) {\n    entityId = \"conductivity\";\n    stateTopic = `${deviceId}/sensor/${entityId}/state`;\n} else if (topic.includes(\"配液当前液位\")) {\n    entityId = \"mixing_level\";\n    stateTopic = `${deviceId}/sensor/${entityId}/state`;\n} else if (topic.includes(\"储液当前液位\")) {\n    entityId = \"storage_level\";\n    stateTopic = `${deviceId}/sensor/${entityId}/state`;\n} else if (topic.includes(\"供液一压力\")) {\n    entityId = \"supply_pressure_1\";\n    stateTopic = `${deviceId}/sensor/${entityId}/state`;\n} else if (topic.includes(\"供液二压力\")) {\n    entityId = \"supply_pressure_2\";\n    stateTopic = `${deviceId}/sensor/${entityId}/state`;\n} else if (topic.includes(\"工作状态\")) {\n    entityType = \"binary_sensor\";\n    entityId = \"work_status\";\n    stateTopic = `${deviceId}/binary_sensor/${entityId}/state`;\n} else if (topic.includes(\"报警状态\")) {\n    entityType = \"binary_sensor\";\n    entityId = \"alarm_status\";\n    stateTopic = `${deviceId}/binary_sensor/${entityId}/state`;\n} else {\n    node.warn(`未知的数据类型: ${topic}`);\n    return null;\n}\n\n// 记录数据处理信息\nnode.warn(`处理数据: ${topic} = ${value} -> ${stateTopic}`);\n\n// 创建输出消息\nconst outputMsg = {\n    topic: stateTopic,\n    payload: String(value),\n    retain: false\n};\n\nreturn outputMsg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 300,
        "y": 300,
        "wires": [
            [
                "ha_data_mqtt",
                "ha_data_debug"
            ]
        ]
    },
    {
        "id": "ha_data_mqtt",
        "type": "mqtt out",
        "z": "ha_integration_tab",
        "name": "发送数据到HA",
        "topic": "",
        "qos": "0",
        "retain": "false",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "mqtt_broker_ha",
        "x": 540,
        "y": 300,
        "wires": []
    },
    {
        "id": "ha_data_debug",
        "type": "debug",
        "z": "ha_integration_tab",
        "name": "HA数据调试",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 550,
        "y": 400,
        "wires": []
    },
    {
        "id": "link_ha_temperature_in",
        "type": "link in",
        "z": "ha_integration_tab",
        "name": "接收温度数据",
        "links": [
            "link_temperature_out"
        ],
        "x": 105,
        "y": 180,
        "wires": [
            [
                "ha_data_processor"
            ]
        ]
    },
    {
        "id": "link_ha_ph_in",
        "type": "link in",
        "z": "ha_integration_tab",
        "name": "接收PH值数据",
        "links": [
            "link_ph_out"
        ],
        "x": 105,
        "y": 240,
        "wires": [
            [
                "ha_data_processor"
            ]
        ]
    },
    {
        "id": "link_ha_level_in",
        "type": "link in",
        "z": "ha_integration_tab",
        "name": "接收液位数据",
        "links": [
            "link_level_out"
        ],
        "x": 105,
        "y": 280,
        "wires": [
            [
                "ha_data_processor",
                "0b8146b6074f206f"
            ]
        ]
    },
    {
        "id": "link_ha_conductivity_in",
        "type": "link in",
        "z": "ha_integration_tab",
        "name": "接收电导率数据",
        "links": [
            "link_conductivity_out"
        ],
        "x": 115,
        "y": 360,
        "wires": [
            [
                "ha_data_processor"
            ]
        ]
    },
    {
        "id": "link_ha_pressure1_in",
        "type": "link in",
        "z": "ha_integration_tab",
        "name": "接收供液一压力数据",
        "links": [
            "link_pressure1_out"
        ],
        "x": 105,
        "y": 320,
        "wires": [
            [
                "ha_data_processor"
            ]
        ]
    },
    {
        "id": "link_ha_pressure2_in",
        "type": "link in",
        "z": "ha_integration_tab",
        "name": "接收供液二压力数据",
        "links": [
            "link_pressure2_out"
        ],
        "x": 115,
        "y": 420,
        "wires": [
            [
                "ha_data_processor"
            ]
        ]
    },
    {
        "id": "link_ha_alarm_in",
        "type": "link in",
        "z": "ha_integration_tab",
        "name": "接收报警状态数据",
        "links": [
            "link_alarm_out"
        ],
        "x": 115,
        "y": 460,
        "wires": [
            [
                "ha_data_processor"
            ]
        ]
    },
    {
        "id": "link_ha_work_in",
        "type": "link in",
        "z": "ha_integration_tab",
        "name": "接收工作状态数据",
        "links": [
            "link_work_status_out"
        ],
        "x": 115,
        "y": 500,
        "wires": [
            [
                "ha_data_processor"
            ]
        ]
    },
    {
        "id": "25565ecbf48f0308",
        "type": "debug",
        "z": "ha_integration_tab",
        "name": "debug 2",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 490,
        "y": 60,
        "wires": []
    },
    {
        "id": "0b8146b6074f206f",
        "type": "debug",
        "z": "ha_integration_tab",
        "name": "debug 1",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 310,
        "y": 240,
        "wires": []
    }
]