​ Node-RED是构建物联网应用程序的一个强大工具,其重点是简化代码块的”连接”以执行任务。它使用可视化编程方法,允许开发人员将预定义的代码块(也叫做”节点”)连接起来执行任务。连接的节点,通常是输入节点、处理节点和输出节点的组合,当它们连接在一起时,构成一个”流”。最初是IBM在2013年末开发的一个开源项目,以满足他们快速连接硬件和设备到Web服务和其他软件的需求–作为物联网的一种粘合剂–它很快发展成为一种通用的物联网编程工具。重要的是,Node-RED已经迅速开发出一个重要的、不断增长的用户基础和一个活跃的开发人员社区,他们正在开发新的节点,允许程序员重用Node-RED色代码来完成各种各样的任务。
​ 虽然Node-RED最初是用来处理物联网的,也就是说,它是与现实世界交互和控制的设备,但随着它的发展,它已经成为一系列应用的有用工具。

基础使用(node-red版本-v2.1.4)

注解:

  • node-red将预定义的代码块叫做”节点“,以下也会用节点来代指。以下内容主要是对每一个节点的功能和使用进行说明
  • 标题中带”*”表示该(分类下)节点是扩展节点,是通过“节点管理”功能后安装的节点。扩展模块的具体名称将在标题后用“()”标记出来。
共通
  • inject

    手动或定期得将消息注入流中。消息的有效荷载可以为多种类型,包括字符串,JavaScript对象或当前时间。

    输出

    • payloadvarious

      指定的消息的有效荷载。

    • topic字符串

      可以在节点中配置的可选属性。

    详细

    通过使用特定的有效荷载,注入节点可以启动流。默认有效荷载是当前时间的时间戳(以毫秒为单位,自1970年1月1日起)。

    该节点还支持注入字符串,数字,布尔值,JavaScript对象或流/全局上下文值。

    默认情况下,节点可以通过在编辑器中单击节点按钮来手动触发。同时也可以被设置为定期或按计划注入。

    另一个可选的设置是在每次启动流时注入一次。

    可以指定的最大间隔约为596小时/24天。 但是,如果对于间隔超过一天的那些间隔,建议您使用scheduler节点来应对断电或重启。

    注意:选项“时间间隔”“特定时间” 使用了标准cron系统。这意味着因此“20分钟”并不表示在此之后20分钟,而是每小时的20分钟,40分钟。如果您希望设定为从现在开始的每20分钟,那么请使用“间隔” 选项。

    注意: 如果您想在字符串中包含换行符,必须使用“功能”节点创建有效荷载。

  • debug

    在“调试”侧边栏选项卡和运行时日志中显示选定的消息属性。 默认情况下,它会显示msg.payload的值,但您也可以将其设置成显示任意属性,完整消息或JSONata表达式的结果。

    详细
    调试侧边栏会提供已发消息的结构化视图,方便您查询消息的结构。

    JavaScript对象和数组可以根据需要来折叠或扩展。缓冲区对象可以显示为原始数据,也可以显示为字符串。

    对任意条消息,调试侧边栏还会显示接收消息的时间,发送消息的节点以及消息类型等信息。单击源节点ID将在工作区中显示该节点。

    节点上的按钮可用于启用或禁用其输出。建议禁用或删除所有未使用的调试节点。

    还可以通过配置节点,将所有消息发送到运行时的日志,或将简短的数据(32个字符内)在调试节点下的状态文本上显示。

  • complete

    当另一个节点完成对消息的处理时触发流。

    详细

    如果一个节点通知运行时它已完成消息的处理,该节点可用于触发第二个流。

    这个节点可以与没有输出端口的节点一起使用,例如在使用电子邮件发送节点来发送邮件后触发一个流。

    此节点只能被设置为处理流中某个所选节点的事件。与Catch节点不同,您不能指定“所有节点”模式并以流中的所有节点为目标。

    并非所有节点都会触发此事件。这取决于它们是否支持于Node-RED 1.0中引入的此功能。

  • catch

    捕获由同一标签页上的节点引发的错误。

    输出

    • error.message字符串

      错误消息。

    • error.source.id字符串

      引发错误的节点的ID。

    • error.source.type字符串

      引发错误的节点的类型。

    • error.source.name字符串

      引发错误的节点的名称。(如果已设置)

    详细

    如果节点在处理消息时抛出错误,则流程通常会停止。该节点可用于捕获那些错误并通过专用流程进行处理。

    默认情况下,该节点将捕获同一标签页上任何节点抛出的错误。或者,它可以针对特定节点,或配置为仅捕获另一个“目标”捕获节点尚未捕获的错误。

    当错误发生时,所有匹配的catch节点都会收到错误消息。

    如果在子流中发送了错误,则该错误将由子流中的任意捕获节点处理。如果子流中不存在捕获节点,则那错误将被传播到子流实例所在的标签页。

    如果消息已经具有error属性,则将该error复制为_error

  • status

    获取在同一标签页上的其他节点的状态消息。

    输出

    • status.text字符串

      状态文本。

    • status.source.type字符串

      报告状态的节点的类型。

    • status.source.id字符串

      报告状态的节点的ID。

    • status.source.name字符串

      报告状态的节点的名称(如果已设置)。

    详细

    该节点不包含有效荷载

    默认情况下,节点会获取同一工作空间标签页上报告所有节点的状态。可以通过配置来设定目标节点。

  • link in

    在流之间创建虚拟连线。

    详细

    该节点可以连接到任何标签页上存在的任何link out节点。连接后,它们的行为就像连接在一起。

    仅当选择链接节点时,才会显示链接节点之间的链接。如果有指向另一个选项卡的链接,则显示一个虚拟节点。单击该虚拟节点将带您到相应的选项卡。

    注意:无法创建进入或离开子流的链接。

  • link call

  • link out

    在流之间创建虚拟连线。

    详细

    该节点可以连接到任何标签页上存在的任何link in节点。连接后,它们的行为就像连接在一起。

    仅当选择链接节点时,才会显示链接节点之间的链接。如果有指向另一个选项卡的链接,则显示一个虚拟节点。单击该虚拟节点将带您到相应的选项卡。

    注意:无法创建进入或离开子流的链接。

  • comment

    可用于向流添加注释的节点。

    详细

    编辑面板接受Markdown语法。输入的文本将在信息侧面板中显示。

功能
  • function
  • switch
  • change
  • range
  • template
  • delay
  • trigger
  • exec
  • filter
网络
  • mqtt in
  • mqtt out
  • http in
  • http response
  • http request
  • websocket in
  • websocket out
  • tcp in
  • tcp out
  • tcp request
  • udp in
  • udp out
序列
  • split
  • join
  • sort
  • batch
解析
  • csv
  • html
  • json
  • xml
  • yaml
存储
  • write file
  • read file
  • watch
  • *mysql(node-red-node-mysql)
*dashboard(node-red-dashboard)
  • button

  • dropdown

  • switch

  • slider

  • numeric

  • text input

  • date picker

  • color picker

  • form

  • text

  • gauge

  • chart

    折线图示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    //chart节点前的 function节点内的代码
    let msg = {
    payload: [{
    "series": ["A", "B", "C"],
    "data": [
    [{ "x": 1504029632890, "y": 5 },
    { "x": 1504029636001, "y": 4 },
    { "x": 1504029638656, "y": 2 }
    ],
    [{ "x": 1504029633514, "y": 6 },
    { "x": 1504029636622, "y": 7 },
    { "x": 1504029639539, "y": 6 }
    ],
    [{ "x": 1504029634400, "y": 7 },
    { "x": 1504029637959, "y": 7 },
    { "x": 1504029640317, "y": 7 }
    ]
    ],
    "labels": [""]
    }]
    }

    return msg

    如果x轴是非时间序列,不应该配置x和y,而是按照以下格式操作:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    let msg = {
    payload: [{
    "series": ["X", "Y", "Z" ],
    "data": [ [5,6,9,10], [3,8,5,10], [6,7,2,10] ],
    "labels": [ "Jan", "Feb", "Mar", "Apr" ]
    }]
    }

    return msg
  • audio out

  • notification

  • ui control

  • template

实际案例

天气预报页面:主要是通过dropdown节点进行地区选择,然后使用http request节点,访问 高德-天气查询 的api,然后通过各个ui组件展示出来。

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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
[
{
"id": "93431d81a8ddb5f8",
"type": "tab",
"label": "获取天气信息",
"disabled": false,
"info": "",
"env": []
},
{
"id": "8e91ffaf1056ff9d",
"type": "link out",
"z": "93431d81a8ddb5f8",
"name": "",
"mode": "link",
"links": [
"d23b7fcbd6eac6c6"
],
"x": 2815,
"y": 580,
"wires": []
},
{
"id": "d23b7fcbd6eac6c6",
"type": "link in",
"z": "93431d81a8ddb5f8",
"name": "",
"links": [
"8e91ffaf1056ff9d"
],
"x": 2915,
"y": 580,
"wires": [
[
"9695bcb124067020"
]
]
},
{
"id": "77afba0c01392262",
"type": "switch",
"z": "93431d81a8ddb5f8",
"name": "判断请求类型",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "hask",
"v": "forecasts",
"vt": "str"
},
{
"t": "hask",
"v": "lives",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 3320,
"y": 580,
"wires": [
[
"46f54fe47d2668b2"
],
[
"01a29a8ee6e9973e"
]
]
},
{
"id": "46f54fe47d2668b2",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取未来几天的天气信息",
"func": "let result = {\n payload: msg.payload.forecasts[0]\n}\nreturn result;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 3550,
"y": 520,
"wires": [
[
"a76cf5bdc5d8582d",
"5f97c00d04379107",
"65ab3b77263e35cb"
]
]
},
{
"id": "400e447cece05b1a",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取未来几天天气信息",
"func": "let data = msg.payload.casts\n\nlet res = {\n payload: [{\n series: [\"最低温度\", \"最高温度\"],\n data: [data.map(e=>{\n return e.nighttemp\n }),data.map(e=>{\n return e.daytemp\n })],\n labels: data.map(e=>{\n return e.date + ['']\n }),\n }]\n}\n\n\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 4100,
"y": 540,
"wires": [
[
"fe36c4bb2dcf9423"
]
]
},
{
"id": "fe36c4bb2dcf9423",
"type": "ui_chart",
"z": "93431d81a8ddb5f8",
"name": "",
"group": "0273a65c57aae0c4",
"order": 18,
"width": 0,
"height": 0,
"label": "近三日温度",
"chartType": "line",
"legend": "true",
"xformat": "HH:mm:ss",
"interpolate": "linear",
"nodata": "",
"dot": false,
"ymin": "",
"ymax": "",
"removeOlder": "1",
"removeOlderPoints": "1111",
"removeOlderUnit": "604800",
"cutout": 0,
"useOneColor": false,
"useUTC": false,
"colors": [
"#1f77b4",
"#e33d2b",
"#ff7f0e",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5"
],
"outputs": 1,
"useDifferentColor": false,
"className": "",
"x": 4310,
"y": 540,
"wires": [
[]
]
},
{
"id": "e78e9980959f8193",
"type": "http request",
"z": "93431d81a8ddb5f8",
"name": "获取未来几天的天气信息",
"method": "GET",
"ret": "obj",
"paytoqs": "query",
"url": "https://restapi.amap.com/v3/weather/weatherInfo?key=0fcc0a97a51a9f8c912dc799779fff76&extensions=all",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"senderr": false,
"x": 2570,
"y": 580,
"wires": [
[
"8e91ffaf1056ff9d"
]
]
},
{
"id": "8057b904a6e8565f",
"type": "e-mail",
"z": "93431d81a8ddb5f8",
"server": "smtp.gmail.com",
"port": "465",
"secure": true,
"tls": true,
"name": "",
"dname": "",
"x": 3750,
"y": 1080,
"wires": []
},
{
"id": "68735a5bbf8c4b94",
"type": "switch",
"z": "93431d81a8ddb5f8",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 3690,
"y": 1020,
"wires": [
[
"8057b904a6e8565f"
]
]
},
{
"id": "01a29a8ee6e9973e",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取实时天气信息",
"func": "let result = {\n payload: msg.payload.lives[0]\n}\nreturn result;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 3550,
"y": 820,
"wires": [
[
"68735a5bbf8c4b94",
"2c38bf74933d4bf3"
]
]
},
{
"id": "8d7678fb61468ec4",
"type": "http request",
"z": "93431d81a8ddb5f8",
"name": "获取实时天气信息",
"method": "GET",
"ret": "obj",
"paytoqs": "query",
"url": "https://restapi.amap.com/v3/weather/weatherInfo?key=0fcc0a97a51a9f8c912dc799779fff76&extensions=base",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"senderr": false,
"x": 2550,
"y": 640,
"wires": [
[
"8e91ffaf1056ff9d"
]
]
},
{
"id": "9695bcb124067020",
"type": "switch",
"z": "93431d81a8ddb5f8",
"name": "判断请求是否成功",
"property": "payload.status",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "1",
"vt": "str"
},
{
"t": "neq",
"v": "1",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 3050,
"y": 580,
"wires": [
[
"77afba0c01392262"
],
[
"e3d08351c3fba68b"
]
]
},
{
"id": "1f0c9379d9cf99ea",
"type": "inject",
"z": "93431d81a8ddb5f8",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "{\"city\":\"110115\"}",
"payloadType": "json",
"x": 780,
"y": 460,
"wires": [
[
"0334158d305569e4"
]
]
},
{
"id": "99cb170d1eb29648",
"type": "link in",
"z": "93431d81a8ddb5f8",
"name": "",
"links": [
"0334158d305569e4"
],
"x": 1115,
"y": 540,
"wires": [
[
"101d5be84a9aedd4",
"997b812c57a1b210",
"8d7678fb61468ec4"
]
]
},
{
"id": "0334158d305569e4",
"type": "link out",
"z": "93431d81a8ddb5f8",
"name": "",
"mode": "link",
"links": [
"99cb170d1eb29648"
],
"x": 1075,
"y": 540,
"wires": []
},
{
"id": "3f43a8f6106d9e56",
"type": "ui_dropdown",
"z": "93431d81a8ddb5f8",
"name": "地区选择",
"label": "",
"tooltip": "",
"place": "请选择地区",
"group": "0273a65c57aae0c4",
"order": 2,
"width": 5,
"height": 1,
"passthru": true,
"multiple": false,
"options": [
{
"label": "东城区",
"value": "110101",
"type": "str"
},
{
"label": "西城区",
"value": "110102",
"type": "str"
},
{
"label": "丰台区",
"value": "110106",
"type": "str"
},
{
"label": "海淀区",
"value": "110108",
"type": "str"
},
{
"label": "朝阳区",
"value": "110105",
"type": "str"
},
{
"label": "石景山区",
"value": "110107",
"type": "str"
},
{
"label": "门头沟区",
"value": "110109",
"type": "str"
},
{
"label": "房山区",
"value": "110111",
"type": "str"
},
{
"label": "通州区",
"value": "110112",
"type": "str"
},
{
"label": "顺义区",
"value": "110113",
"type": "str"
},
{
"label": "昌平区",
"value": "110114",
"type": "str"
},
{
"label": "大兴区",
"value": "110115",
"type": "str"
},
{
"label": "怀柔区",
"value": "110116",
"type": "str"
},
{
"label": "平谷区",
"value": "110117",
"type": "str"
},
{
"label": "密云区",
"value": "110118",
"type": "str"
},
{
"label": "延庆区",
"value": "110119",
"type": "str"
}
],
"payload": "",
"topic": "topic",
"topicType": "msg",
"className": "",
"x": 660,
"y": 540,
"wires": [
[
"c96d82e44a98b8c1"
]
]
},
{
"id": "c96d82e44a98b8c1",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "格式化地区选择",
"func": "let res = {\n payload: {\n city: msg.payload\n }\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 840,
"y": 540,
"wires": [
[
"0334158d305569e4"
]
]
},
{
"id": "ffbf4b6f4095f633",
"type": "file",
"z": "93431d81a8ddb5f8",
"name": "forecasts",
"filename": "forecasts.txt",
"appendNewline": false,
"createDir": true,
"overwriteFile": "true",
"encoding": "none",
"x": 440,
"y": 660,
"wires": [
[]
]
},
{
"id": "101d5be84a9aedd4",
"type": "file in",
"z": "93431d81a8ddb5f8",
"name": "",
"filename": "forecasts.txt",
"format": "utf8",
"chunk": false,
"sendError": false,
"encoding": "none",
"allProps": false,
"x": 1250,
"y": 540,
"wires": [
[
"3a451e17d0b2321c"
]
]
},
{
"id": "3a451e17d0b2321c",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "string2object",
"func": "let res = {\n payload: JSON.parse(msg.payload || '{\"data\":{}}')\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1430,
"y": 540,
"wires": [
[
"997b812c57a1b210"
]
]
},
{
"id": "997b812c57a1b210",
"type": "join",
"z": "93431d81a8ddb5f8",
"name": "",
"mode": "custom",
"build": "merged",
"property": "payload",
"propertyType": "msg",
"key": "payload",
"joiner": "\\n",
"joinerType": "str",
"accumulate": false,
"timeout": "",
"count": "2",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "",
"reduceFixup": "",
"x": 1610,
"y": 540,
"wires": [
[
"cdd36146dca21ac6"
]
]
},
{
"id": "cdd36146dca21ac6",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "判断当前天气信息是否有效",
"func": "let {payload} = msg\n\nlet city = payload.city\n\nlet data = payload.data[city]\n//判断文件内是否存了这个地区的天气信息\nif(data) {\n //判断天气信息是否过期,过期时间算4小时\n if(new Date()-new Date(data.reporttime) < 4 * 60 *60 * 1000) {\n return {\n payload: {\n code: 1,\n data: data\n }\n }\n } else {\n return {\n payload: {\n code: 0,\n city: city,\n data: payload.data\n }\n };\n }\n \n} else {\n return {\n payload: {\n code: 0,\n city: city,\n data: payload.data\n }\n };\n}\n\n\nlet res = {\n payload: payload\n}\n\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1880,
"y": 540,
"wires": [
[
"c3b575766d5fd07f"
]
]
},
{
"id": "c3b575766d5fd07f",
"type": "switch",
"z": "93431d81a8ddb5f8",
"name": "",
"property": "payload.code",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "1",
"vt": "str"
},
{
"t": "neq",
"v": "1",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 2090,
"y": 540,
"wires": [
[
"b118ab6778e6d9b4"
],
[
"37f02e7c2ebd2027"
]
]
},
{
"id": "37f02e7c2ebd2027",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "格式化地区选择",
"func": "let res = {\n payload: {\n city: msg.payload.city\n }\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 2320,
"y": 580,
"wires": [
[
"e78e9980959f8193"
]
]
},
{
"id": "b118ab6778e6d9b4",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "格式化本地数据",
"func": "let res = {\n payload: msg.payload.data\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 2320,
"y": 480,
"wires": [
[
"a76cf5bdc5d8582d"
]
]
},
{
"id": "a76cf5bdc5d8582d",
"type": "link out",
"z": "93431d81a8ddb5f8",
"name": "",
"mode": "link",
"links": [
"552b298f10f7cf4b"
],
"x": 3755,
"y": 480,
"wires": []
},
{
"id": "552b298f10f7cf4b",
"type": "link in",
"z": "93431d81a8ddb5f8",
"name": "",
"links": [
"a76cf5bdc5d8582d"
],
"x": 3935,
"y": 480,
"wires": [
[
"400e447cece05b1a"
]
]
},
{
"id": "5f97c00d04379107",
"type": "file in",
"z": "93431d81a8ddb5f8",
"name": "",
"filename": "forecasts.txt",
"format": "utf8",
"chunk": false,
"sendError": false,
"encoding": "none",
"allProps": false,
"x": 3770,
"y": 680,
"wires": [
[
"fd399620160e6e04"
]
]
},
{
"id": "fd399620160e6e04",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "格式化存储消息",
"func": "let res = {\n payload: JSON.parse(msg.payload || '{\"data\":{}}')\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 3960,
"y": 680,
"wires": [
[
"65ab3b77263e35cb"
]
]
},
{
"id": "ba1ed720f85507cc",
"type": "inject",
"z": "93431d81a8ddb5f8",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payloadType": "str",
"x": 290,
"y": 660,
"wires": [
[
"ffbf4b6f4095f633"
]
]
},
{
"id": "3ba6c11f7bb8c9ee",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "",
"func": "let {city,adcode,province,reporttime,casts,data } = msg.payload\nObject.assign(data,{\n [adcode]:{\n city:city,\n adcode:adcode,\n province:province,\n reporttime: reporttime,\n casts:casts\n }\n})\nreturn {\n payload:{\n data: data\n }\n}",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 4370,
"y": 660,
"wires": [
[
"1c32e9cc321eb3b0"
]
]
},
{
"id": "1c32e9cc321eb3b0",
"type": "file",
"z": "93431d81a8ddb5f8",
"name": "forecasts",
"filename": "forecasts.txt",
"appendNewline": false,
"createDir": true,
"overwriteFile": "true",
"encoding": "none",
"x": 4540,
"y": 660,
"wires": [
[]
]
},
{
"id": "65ab3b77263e35cb",
"type": "join",
"z": "93431d81a8ddb5f8",
"name": "",
"mode": "custom",
"build": "merged",
"property": "payload",
"propertyType": "msg",
"key": "payload",
"joiner": "\\n",
"joinerType": "str",
"accumulate": false,
"timeout": "",
"count": "6",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "",
"reduceFixup": "",
"x": 4190,
"y": 660,
"wires": [
[
"3ba6c11f7bb8c9ee"
]
]
},
{
"id": "a25d38cb1b412ab9",
"type": "comment",
"z": "93431d81a8ddb5f8",
"name": "初始化文件",
"info": "",
"x": 280,
"y": 700,
"wires": []
},
{
"id": "b756bfb837b9d8f8",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取城市",
"func": "let res = {\n payload: msg.payload.city\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 4000,
"y": 780,
"wires": [
[
"75d727507e0dad59"
]
]
},
{
"id": "6d393852d8765650",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取省份",
"func": "let res = {\n payload: msg.payload.province\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 4000,
"y": 820,
"wires": [
[
"c599c051b70a4004"
]
]
},
{
"id": "1a47aa2f61014a98",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取更新时间",
"func": "let res = {\n payload: msg.payload.reporttime\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 4020,
"y": 860,
"wires": [
[
"2c1de88cf13d17a6"
]
]
},
{
"id": "75d727507e0dad59",
"type": "ui_text",
"z": "93431d81a8ddb5f8",
"group": "0273a65c57aae0c4",
"order": 5,
"width": 4,
"height": 1,
"name": "",
"label": "城市",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 4210,
"y": 780,
"wires": []
},
{
"id": "c599c051b70a4004",
"type": "ui_text",
"z": "93431d81a8ddb5f8",
"group": "0273a65c57aae0c4",
"order": 3,
"width": 4,
"height": 1,
"name": "",
"label": "省份",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 4210,
"y": 820,
"wires": []
},
{
"id": "2c1de88cf13d17a6",
"type": "ui_text",
"z": "93431d81a8ddb5f8",
"group": "0273a65c57aae0c4",
"order": 7,
"width": 5,
"height": 1,
"name": "",
"label": "时间",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 4210,
"y": 860,
"wires": []
},
{
"id": "2c38bf74933d4bf3",
"type": "link out",
"z": "93431d81a8ddb5f8",
"name": "",
"mode": "link",
"links": [
"36fa90b5a0c0102c"
],
"x": 3755,
"y": 820,
"wires": []
},
{
"id": "36fa90b5a0c0102c",
"type": "link in",
"z": "93431d81a8ddb5f8",
"name": "",
"links": [
"2c38bf74933d4bf3"
],
"x": 3815,
"y": 820,
"wires": [
[
"b756bfb837b9d8f8",
"6d393852d8765650",
"1a47aa2f61014a98",
"84e714b6f4e9b3c6",
"6627257a317b84bc",
"145854514113326a",
"7e6cfe96c879a814",
"7badc7d124da7d46"
]
]
},
{
"id": "84e714b6f4e9b3c6",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取天气",
"func": "let res = {\n payload: msg.payload.weather\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 4000,
"y": 900,
"wires": [
[
"daaafd845910e3f3"
]
]
},
{
"id": "daaafd845910e3f3",
"type": "ui_text",
"z": "93431d81a8ddb5f8",
"group": "0273a65c57aae0c4",
"order": 8,
"width": 4,
"height": 1,
"name": "",
"label": "天气",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 4210,
"y": 900,
"wires": []
},
{
"id": "6627257a317b84bc",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取温度",
"func": "let res = {\n payload: msg.payload.temperature+'°'\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 4000,
"y": 940,
"wires": [
[
"54454b4f7b20cd6c"
]
]
},
{
"id": "54454b4f7b20cd6c",
"type": "ui_text",
"z": "93431d81a8ddb5f8",
"group": "0273a65c57aae0c4",
"order": 10,
"width": 4,
"height": 1,
"name": "",
"label": "温度",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 4210,
"y": 940,
"wires": []
},
{
"id": "145854514113326a",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取风向",
"func": "let res = {\n payload: msg.payload.winddirection\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 4000,
"y": 980,
"wires": [
[
"8dd7389318dbd418"
]
]
},
{
"id": "8dd7389318dbd418",
"type": "ui_text",
"z": "93431d81a8ddb5f8",
"group": "0273a65c57aae0c4",
"order": 14,
"width": 4,
"height": 1,
"name": "",
"label": "风向",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 4210,
"y": 980,
"wires": []
},
{
"id": "7e6cfe96c879a814",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取风力",
"func": "let res = {\n payload: msg.payload.windpower + '级'\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 4000,
"y": 1020,
"wires": [
[
"cf006e3d202cb629"
]
]
},
{
"id": "cf006e3d202cb629",
"type": "ui_text",
"z": "93431d81a8ddb5f8",
"group": "0273a65c57aae0c4",
"order": 16,
"width": 4,
"height": 1,
"name": "",
"label": "风力",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 4210,
"y": 1020,
"wires": []
},
{
"id": "7badc7d124da7d46",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "获取湿度",
"func": "let res = {\n payload: msg.payload.humidity + '%'\n}\nreturn res;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 4000,
"y": 1060,
"wires": [
[
"d4481e360aeedbef"
]
]
},
{
"id": "d4481e360aeedbef",
"type": "ui_text",
"z": "93431d81a8ddb5f8",
"group": "0273a65c57aae0c4",
"order": 12,
"width": 4,
"height": 1,
"name": "",
"label": "湿度",
"format": "{{msg.payload}}",
"layout": "row-spread",
"className": "",
"x": 4210,
"y": 1060,
"wires": []
},
{
"id": "8d89f5d347fd4cf0",
"type": "ui_ui_control",
"z": "93431d81a8ddb5f8",
"name": "",
"events": "connect",
"x": 280,
"y": 540,
"wires": [
[
"589f22432e35ca6a"
]
]
},
{
"id": "589f22432e35ca6a",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "",
"func": "return {\n payload: '110108'\n};",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 540,
"wires": [
[
"3f43a8f6106d9e56"
]
]
},
{
"id": "e3d08351c3fba68b",
"type": "function",
"z": "93431d81a8ddb5f8",
"name": "",
"func": "\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 2990,
"y": 840,
"wires": [
[]
]
},
{
"id": "2abc219f7bcbef2e",
"type": "ui_spacer",
"z": "93431d81a8ddb5f8",
"name": "spacer",
"group": "0273a65c57aae0c4",
"order": 1,
"width": 10,
"height": 1
},
{
"id": "880529f04ce11886",
"type": "ui_spacer",
"z": "93431d81a8ddb5f8",
"name": "spacer",
"group": "0273a65c57aae0c4",
"order": 4,
"width": 1,
"height": 1
},
{
"id": "b5a9dcc679da1227",
"type": "ui_spacer",
"z": "93431d81a8ddb5f8",
"name": "spacer",
"group": "0273a65c57aae0c4",
"order": 6,
"width": 1,
"height": 1
},
{
"id": "33eb97474de459ab",
"type": "ui_spacer",
"z": "93431d81a8ddb5f8",
"name": "spacer",
"group": "0273a65c57aae0c4",
"order": 9,
"width": 1,
"height": 1
},
{
"id": "9b1e39dc1e1f64fe",
"type": "ui_spacer",
"z": "93431d81a8ddb5f8",
"name": "spacer",
"group": "0273a65c57aae0c4",
"order": 11,
"width": 1,
"height": 1
},
{
"id": "2d1b1fea5b63129c",
"type": "ui_spacer",
"z": "93431d81a8ddb5f8",
"name": "spacer",
"group": "0273a65c57aae0c4",
"order": 13,
"width": 1,
"height": 1
},
{
"id": "1e79c2642674d113",
"type": "ui_spacer",
"z": "93431d81a8ddb5f8",
"name": "spacer",
"group": "0273a65c57aae0c4",
"order": 15,
"width": 1,
"height": 1
},
{
"id": "14eaf56393830744",
"type": "ui_spacer",
"z": "93431d81a8ddb5f8",
"name": "spacer",
"group": "0273a65c57aae0c4",
"order": 17,
"width": 6,
"height": 1
},
{
"id": "0273a65c57aae0c4",
"type": "ui_group",
"name": "HOME",
"tab": "9382e8206c7b0f0a",
"order": 1,
"disp": true,
"width": 15,
"collapse": false,
"className": ""
},
{
"id": "9382e8206c7b0f0a",
"type": "ui_tab",
"name": "Home",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]