[英]How to Parse Arrays and Extract Values with NodeRED's Switch Node
A quick NodeRed question as I am not sure I am correct in my approach. 一个快速的NodeRed问题,因为我不确定我的方法是否正确。
I'd like to visualize my laptop's CPU usage value to a gauge. 我想将笔记本电脑的CPU使用率值直观地显示出来。 My laptop is a quad-core, having 4 CPU's. 我的笔记本电脑是四核的,有4个CPU。
With that, NodeRED's CPU usage node returns the following array when debugged: 这样,NodeRED的CPU使用率节点在调试时将返回以下数组:
{"payload":29,"topic":"core_1","model":"Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz","speed":2300,"_msgid":"1a846a9b.631375"} {“有效载荷”:29,“主题”:“ core_1”,“模型”:“英特尔(R)i5-7360U CPU @ 2.30GHz”,“速度”:2300,“ _ msgid”:“ 1a846a9b”。 631375" }
{"payload":13,"topic":"core_2","model":"Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz","speed":2300,"_msgid":"1a846a9b.631375"} {“有效负载”:13,“主题”:“ core_2”,“模型”:“英特尔(R)i5-7360U CPU @ 2.30GHz”,“速度”:2300,“ _ msgid”:“ 1a846a9b”。 631375" }
{"payload":27,"topic":"core_3","model":"Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz","speed":2300,"_msgid":"1a846a9b.631375"} {“有效载荷”:27,“主题”:“ core_3”,“模型”:“ Intel(R)CoreTM i5-7360U CPU @ 2.30GHz”,“速度”:2300,“ _ msgid”:“ 1a846a9b”。 631375" }
{"payload":14,"topic":"core_4","model":"Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz","speed":2300,"_msgid":"1a846a9b.631375"} {“有效载荷”:14,“主题”:“ core_4”,“模型”:“ Intel(R)CoreTM i5-7360U CPU @ 2.30GHz”,“速度”:2300,“ _ msgid”:“ 1a846a9b”。 631375" }
Each cpu is represented, indicated by the "topic" value. 代表每个cpu,以“ topic”值表示。
With that in mind, I wired up program to extract the each payloads value via the Switch node, to then be sent to debug. 考虑到这一点,我连接了程序以通过Switch节点提取每个有效负载值,然后将其发送到调试程序。
[{"id":"bb8b8d89.ff51f","type":"cpu","z":"c5c8a205.2f99a","name":"","msgCore":true,"msgOverall":false,"x":270,"y":140,"wires":[["4acb9cc9.1dea24","d55a24f8.0811a8"]]},{"id":"a1805033.6df75","type":"inject","z":"c5c8a205.2f99a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":140,"wires":[["bb8b8d89.ff51f"]]},{"id":"c3a48e9d.2f8de","type":"debug","z":"c5c8a205.2f99a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":310,"y":200,"wires":[]},{"id":"4acb9cc9.1dea24","type":"switch","z":"c5c8a205.2f99a","name":"CPU Parse","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"core_1","vt":"msg"},{"t":"eq","v":"core_2","vt":"msg"},{"t":"eq","v":"core_3","vt":"msg"},{"t":"eq","v":"core_4","vt":"msg"}],"checkall":"true","repair":false,"outputs":4,"x":130,"y":220,"wires":[["c3a48e9d.2f8de"],["55530c03.0256b4"],["bd6f5bb7.283058"],["70265266.692dec"]]},{"id":"55530c03.0256b4","type":"debug","z":"c5c8a205.2f99a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":310,"y":240,"wires":[]},{"id":"bd6f5bb7.283058","type":"debug","z":"c5c8a205.2f99a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":310,"y":280,"wires":[]},{"id":"70265266.692dec","type":"debug","z":"c5c8a205.2f99a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":310,"y":320,"wires":[]},{"id":"d55a24f8.0811a8","type":"debug","z":"c5c8a205.2f99a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":490,"y":140,"wires":[]},{"id":"b0d4e3d4.7e538","type":"ui_gauge","z":"c5c8a205.2f99a","name":"","order":0,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00B500","#E6E600","#CA3838"],"seg1":"","seg2":"","x":490,"y":200,"wires":[]},{"id":"9c6e57f2.cdc4f8","type":"ui_gauge","z":"c5c8a205.2f99a","name":"","order":0,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00B500","#E6E600","#CA3838"],"seg1":"","seg2":"","x":490,"y":240,"wires":[]},{"id":"5fa0cda1.ad7074","type":"ui_gauge","z":"c5c8a205.2f99a","name":"","order":0,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00B500","#E6E600","#CA3838"],"seg1":"","seg2":"","x":490,"y":280,"wires":[]},{"id":"924f895e.acc578","type":"ui_gauge","z":"c5c8a205.2f99a","name":"","order":0,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00B500","#E6E600","#CA3838"],"seg1":"","seg2":"","x":490,"y":320,"wires":[]}]
This approach may be incorrect as I am not sure whether the CPU Usage node will return the desired values. 这种方法可能不正确,因为我不确定CPU使用率节点是否会返回所需的值。 But assuming that this is the correct approach, I've configured the Switch node in the following manner: 但是,假设这是正确的方法,我已经按照以下方式配置了Switch节点:
As mentioned, this may not be the correct approach. 如上所述,这可能不是正确的方法。 Any insights you can provide will be incredibly helpful. 您可以提供的任何见解都将非常有用。
Many thanks! 非常感谢!
It's not returning an array, it's returning 4 separate messages, one for each core. 它不返回数组,而是返回4条单独的消息,每个内核一条。
It's identifying each core by settings the msg.topic
to core_1
, core_2
, core_3
and core_4
respectively. 它识别每个芯由设置msg.topic
到core_1
, core_2
, core_3
和core_4
分别。 So change your switch node to route based on msg.topic
not msg.payload
and have it compare to a string not a msg
property 因此,将您的交换节点更改为基于msg.topic
而不是msg.payload
进行路由, msg.topic
其与不是msg
属性的字符串进行比较
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.