如何将布尔值 true 注入 Node-RED 中的有效负载

How can I inject a boolean of value true into a payload in Node-RED

我设置了 3 个传感器,我为每个传感器添加了一个时间戳,然后我想将一个布尔值注入到它们的有效负载中。

我已成功将布尔值注入到我的 sensordata 的有效负载中,但我无法将布尔值设置为 true,只要激活注入节点就应该如此。

我的代码如下:

[{"id":"bca1865d.435e78","type":"websocket-listener","z":"","path":"/ws/iot","wholemsg":"false"},{"id":"981eb056.67e15","type":"ibmiot in","z":"c645a0e3.39ba6","authentication":"quickstart","apiKey":"","inputType":"evt","deviceId":"e197b6bd8a80","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"Sensor #1","service":"quickstart","allDevices":false,"allApplications":false,"allDeviceTypes":true,"allEvents":true,"allCommands":false,"allFormats":false,"x":220,"y":120,"wires":[["e4c932e3.1b36d","58a6047e.a759fc"]]},{"id":"e4c932e3.1b36d","type":"debug","z":"c645a0e3.39ba6","name":"Sensor data","active":false,"console":"false","complete":"true","x":482.5,"y":83,"wires":[]},{"id":"13ce88e9.ec3177","type":"websocket out","z":"c645a0e3.39ba6","name":"WebSocket","server":"bca1865d.435e78","client":"","x":903,"y":174,"wires":[]},{"id":"d0db918a.2f247","type":"cloudant out","z":"c645a0e3.39ba6","service":"gwens-iottrial2-cloudantNoSQLDB","cloudant":"","name":"","database":"iotsensortdata","payonly":true,"operation":"insert","x":912.5,"y":126,"wires":[]},{"id":"6979b2fb.96864c","type":"debug","z":"c645a0e3.39ba6","name":"Sensor JSON","active":false,"console":"false","complete":"true","x":913,"y":230,"wires":[]},{"id":"58a6047e.a759fc","type":"function","z":"c645a0e3.39ba6","name":"Add timestamp","func":"msg.topic = \"Sensor Data\";\nmsg.payload.time = Date.now();\nreturn msg;","outputs":1,"noerr":0,"x":483,"y":174,"wires":[["d24cfce6.fed3a"]]},{"id":"4c4be7e7.b3b418","type":"ibmiot in","z":"c645a0e3.39ba6","authentication":"quickstart","apiKey":"","inputType":"evt","deviceId":"64833708ebbf","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"Sensor #2","service":"quickstart","allDevices":false,"allApplications":false,"allDeviceTypes":true,"allEvents":true,"allCommands":false,"allFormats":false,"x":220,"y":174,"wires":[["58a6047e.a759fc"]]},{"id":"e5570623.1aa8f8","type":"ibmiot in","z":"c645a0e3.39ba6","authentication":"quickstart","apiKey":"","inputType":"evt","deviceId":"00933ded85c4","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"Sensor #3","service":"quickstart","allDevices":false,"allApplications":false,"allDeviceTypes":true,"allEvents":true,"allCommands":false,"allFormats":false,"x":221,"y":227,"wires":[["58a6047e.a759fc"]]},{"id":"fd41e3f7.d77f48","type":"inject","z":"c645a0e3.39ba6","name":"Occupancy","topic":"OccupancyBool","payload":"true","payloadType":"bool","repeat":"1","crontab":"","once":true,"x":485,"y":126.5,"wires":[["d24cfce6.fed3a"]]},{"id":"d24cfce6.fed3a","type":"function","z":"c645a0e3.39ba6","name":"Occupancy Injection","func":"var Occupancy = false;\n\nconsole.log(\"hi \" + msg);\n\nif(msg.topic == \"OccupancyBool\") {\n    if(msg.payload === true) {\n        Occupancy = true;\n    }\n}\n\nelse if(msg.topic == \"Sensor Data\") {\n    msg.payload.occupancy = Occupancy;\n    return msg;\n}\n/*\nswitch(msg.topic) {\n    case \"OccupancyBool\":\n        if(msg.payload === true) {\n            Occupancy = msg.payload;\n        }\n        console.log(\"occupancy bool\");\n        break;\n    case \"Sensor Data\":\n        msg.payload.occupancy = Occupancy;\n        console.log(\"sensor data\");\n        return msg;\n        break;\n    default:\n        console.log(\"default\");\n        break;\n}*/","outputs":1,"noerr":0,"x":691.5,"y":174.5,"wires":[["d0db918a.2f247","13ce88e9.ec3177","6979b2fb.96864c","76b1bbbe.8f57fc"]]},{"id":"76b1bbbe.8f57fc","type":"debug","z":"c645a0e3.39ba6","name":"","active":true,"console":"false","complete":"payload","x":844,"y":68,"wires":[]}]

如何将布尔值 true 注入我的传感器数据的有效负载?

您将 Occupancy 的值作为局部变量存储在 Occupancy Injection 函数中。这意味着它的范围仅限于函数的每个单独调用。当来自 Inject 节点的消息到来时,它将值设置为 true,然后函数存在并且值丢失。当传感器读数进入时,该值重新初始化为 false。

要在调用之间存储值,您需要使用上下文对象:

// Retrieve the last known value from context, default to false
var Occupancy = context.get('occupancy')||false;

if(msg.topic == "OccupancyBool") {
    if(msg.payload === true) {
        Occupancy = true;
    } else {
        Occupancy = false;
    }
    // store the value for next time
    context.set('occupancy',Occupancy);
}