add examples for function category nodes

This commit is contained in:
Hiroyasu Nishiyama
2020-06-01 14:44:18 +09:00
parent cbf1afc9fe
commit b67f2d874b
48 changed files with 48 additions and 0 deletions

View File

@@ -0,0 +1 @@
[{"id":"ec5a531b.68b65","type":"inject","z":"90acd374.2feda","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"World","payloadType":"str","x":150,"y":100,"wires":[["961abba6.04a028"]]},{"id":"1b0f8c3e.1fd7e4","type":"debug","z":"90acd374.2feda","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":100,"wires":[]},{"id":"4e5bf6b2.b4dd58","type":"comment","z":"90acd374.2feda","name":"send a message to output port","info":"Function node can be used to write JavaScript code to handle messages.\nThe input message can be referrenced by `msg` variable. \nA message returned from body of the function is sent to output port.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).","x":170,"y":40,"wires":[]},{"id":"961abba6.04a028","type":"function","z":"90acd374.2feda","name":"return a message","func":"// returning message send it to output port\nmsg.payload = \"Hello, \"+msg.payload +\"!\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":100,"wires":[["1b0f8c3e.1fd7e4"]]}]

View File

@@ -0,0 +1 @@
[{"id":"c2b3b0f1.62189","type":"inject","z":"b4c03214.fa42e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":100,"wires":[["7241db1e.8946c4"]]},{"id":"c6191361.0f3c","type":"debug","z":"b4c03214.fa42e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":80,"wires":[]},{"id":"3aa5727c.e5019e","type":"comment","z":"b4c03214.fa42e","name":"send multiple message","info":"Function node can send multiple messages to output ports by returning an array of messages. \n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).","x":140,"y":40,"wires":[]},{"id":"7241db1e.8946c4","type":"function","z":"b4c03214.fa42e","name":"return array of messages","func":"// returning array of message send elements to output ports\nvar msg1 = { payload:\"first out of output 1\" };\nvar msg2 = { payload:\"second out of output 1\" };\nvar msg3 = { payload:\"third out of output 1\" };\nvar msg4 = { payload:\"only message from output 2\" };\nreturn [ [ msg1, msg2, msg3 ], msg4 ];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":370,"y":100,"wires":[["c6191361.0f3c"],["23a53d00.c89b74"]]},{"id":"23a53d00.c89b74","type":"debug","z":"b4c03214.fa42e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":120,"wires":[]}]

View File

@@ -0,0 +1 @@
[{"id":"cc74476e.cbdf68","type":"inject","z":"e099b273.21259","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Hello, World!","payloadType":"str","x":190,"y":100,"wires":[["7d90286.706e9d8"]]},{"id":"909906c3.ea9f58","type":"debug","z":"e099b273.21259","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":100,"wires":[]},{"id":"d5fc0512.4cd9c8","type":"comment","z":"e099b273.21259","name":"sending messages asynchronously","info":"Function node can asynchronously send a messages to output ports by calling `node.send` function.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).","x":200,"y":40,"wires":[]},{"id":"7d90286.706e9d8","type":"function","z":"e099b273.21259","name":"wait 2s then send message","func":"// setTimeout calls calls specified callback function asynchronously after a specified time\nsetTimeout(function () {\n node.send(msg);\n}, 2*1000);\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":100,"wires":[["909906c3.ea9f58"]]}]

View File

@@ -0,0 +1 @@
[{"id":"64470964.a291a8","type":"inject","z":"d9fbb64b.577e08","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":120,"wires":[["ee4f5964.5cdae8"]]},{"id":"ed028c2f.924b","type":"debug","z":"d9fbb64b.577e08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":120,"wires":[]},{"id":"b1686e70.a9f6d","type":"comment","z":"d9fbb64b.577e08","name":"logging events","info":"In body of function node code, following logging functions can be used to log events:\n- `node.log`\n- `node.warn`\n- `node.error`\n- `node.trace`\n- `node.debug`\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).","x":140,"y":60,"wires":[]},{"id":"ee4f5964.5cdae8","type":"function","z":"d9fbb64b.577e08","name":"log events","func":"// In function node, node.log, node.warn, and node.error functions can be used for logging\n// See debug sidebar and console output\nnode.log(\"Something happened\");\nnode.warn(\"Something happened you should know about\");\nnode.error(\"Oh no, something bad happened\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":120,"wires":[["ed028c2f.924b"]]}]

View File

@@ -0,0 +1 @@
[{"id":"89c17d21.15da2","type":"inject","z":"dca895d.18be468","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":120,"wires":[["1bcca7af.619428"]]},{"id":"3b9cd70e.8e66e8","type":"debug","z":"dca895d.18be468","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":120,"wires":[]},{"id":"f76ea68a.6f3c58","type":"comment","z":"dca895d.18be468","name":"handling errors","info":"Calling `node.error` function with the original message as a second argument, function node can trigger a catch node in the same tab.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).","x":140,"y":60,"wires":[]},{"id":"1bcca7af.619428","type":"function","z":"dca895d.18be468","name":"report error","func":"// In function node, calling node.error functions with the original input message as its second argument triggers catch node\n// See debug sidebar and console output\nnode.error(\"Oh no, something bad happened\", msg);\n// execution should stops here","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":120,"wires":[["3b9cd70e.8e66e8"]]},{"id":"74854950.d99558","type":"catch","z":"dca895d.18be468","name":"","scope":["1bcca7af.619428"],"uncaught":false,"x":330,"y":180,"wires":[["32743f74.e718a"]]},{"id":"32743f74.e718a","type":"debug","z":"dca895d.18be468","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":510,"y":180,"wires":[]},{"id":"fb884166.e42f3","type":"comment","z":"dca895d.18be468","name":"↑ error information can be found in msg.error","info":"","x":630,"y":220,"wires":[]}]

View File

@@ -0,0 +1 @@
[{"id":"a3a635eb.191188","type":"inject","z":"97cd77e0.37c268","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":100,"wires":[["77f57d92.06fa14"]]},{"id":"1a218bea.61ae04","type":"debug","z":"97cd77e0.37c268","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":100,"wires":[]},{"id":"a56bde3a.330e1","type":"comment","z":"97cd77e0.37c268","name":"storing data in context","info":"The function node code can store data in the context store.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions) and [context store](https://nodered.org/docs/user-guide/context).","x":160,"y":40,"wires":[]},{"id":"77f57d92.06fa14","type":"function","z":"97cd77e0.37c268","name":"counter","func":"// initialise the counter to 0 if it doesn't exist already\nvar count = context.get('count')||0;\ncount += 1;\n// store the value back\ncontext.set('count',count);\n// make it part of the outgoing msg object\nmsg.payload = count;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":100,"wires":[["1a218bea.61ae04"]]}]

View File

@@ -0,0 +1 @@
[{"id":"ce0b7cca.34817","type":"inject","z":"b8ceafb9.9c135","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"red","payloadType":"str","x":150,"y":100,"wires":[["a4a6c205.8afd4"]]},{"id":"c98c52d0.ab51f","type":"comment","z":"b8ceafb9.9c135","name":"showing status information","info":"The function node code can provide status decoration by calling `node.status` function.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).","x":150,"y":40,"wires":[]},{"id":"a4a6c205.8afd4","type":"function","z":"b8ceafb9.9c135","name":"show status","func":"// calling node.status show status information below the function node\nswitch (msg.payload) {\n case \"red\":\n node.status({fill:\"red\",shape:\"ring\",text:\"disconnected\"});\n break;\n case \"green\":\n node.status({fill:\"green\",shape:\"dot\",text:\"connected\"});\n break;\n case \"text\":\n node.status({text:\"Just text status\"});\n break;\n case \"clear\":\n node.status({}); // to clear the status \n break;\n}\n\n","outputs":0,"noerr":0,"initialize":"","finalize":"","x":330,"y":100,"wires":[]},{"id":"9f29ae74.8dd11","type":"inject","z":"b8ceafb9.9c135","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"green","payloadType":"str","x":150,"y":140,"wires":[["a4a6c205.8afd4"]]},{"id":"83fc1404.ec0b98","type":"inject","z":"b8ceafb9.9c135","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"text","payloadType":"str","x":150,"y":180,"wires":[["a4a6c205.8afd4"]]},{"id":"517a869c.0ceab8","type":"inject","z":"b8ceafb9.9c135","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"clear","payloadType":"str","x":150,"y":220,"wires":[["a4a6c205.8afd4"]]}]

View File

@@ -0,0 +1 @@
[{"id":"3ece1efd.306c22","type":"inject","z":"1b8d1c9a.2dae03","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":120,"wires":[["c9100743.673598"]]},{"id":"ed3da95d.62ee98","type":"debug","z":"1b8d1c9a.2dae03","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":120,"wires":[]},{"id":"85b5b8ad.0717d8","type":"comment","z":"1b8d1c9a.2dae03","name":"using external modules\\n(needs change in settings.js - see details on node description of this node) ","info":"The function node can use external node modules using global context value.\n\nAdditional node modules must be loaded in `settings.js` file and added to the `functionGlobalContext` property. \n\nFor example, following definition in `settings.js` load `os` module on start up of Node-RED. \n\n```\nfunctionGlobalContext: {\n os: require(\"os\")\n}\n```\n\nThe `os` module can be accessed in a function blocks as:\n```\nglobal.get(\"os\")\n```\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions) and [context store](https://nodered.org/docs/user-guide/context).","x":320,"y":60,"wires":[]},{"id":"c9100743.673598","type":"function","z":"1b8d1c9a.2dae03","name":"use os module","func":"// use external os module to access OS information\nvar os = global.get(\"os\");\nmsg.payload = { \n hostname: os.hostname(),\n arch: os.arch(),\n platform: os.platform(),\n release: os.release(),\n free: os.freemem()\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":120,"wires":[["ed3da95d.62ee98"]]}]

View File

@@ -0,0 +1 @@
[{"id":"e397736b.a96af","type":"inject","z":"4829bfa2.a23c5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":120,"wires":[["900ad01c.561f"]]},{"id":"1e2a78d6.33f617","type":"debug","z":"4829bfa2.a23c5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":120,"wires":[]},{"id":"83d3f2fd.593b4","type":"comment","z":"4829bfa2.a23c5","name":"setup and close","info":"The function node code provide setup and closecode. \nThe `Setup` tab contains code that will be run whenever the node is started. The `Close` tab contains code that will be run when the node is stopped.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).","x":140,"y":60,"wires":[]},{"id":"900ad01c.561f","type":"function","z":"4829bfa2.a23c5","name":"counter","func":"// get value of global counter\nvar count = global.get('count');\ncount += 1;\n// store the value back\nglobal.set('count',count);\n// make it part of the outgoing msg object\nmsg.payload = count;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"// initialize global counter\nglobal.set('count', 0);","finalize":"// report current counter to console\nvar count = global.get('count');\nconsole.log(\"count:\", count);","x":340,"y":120,"wires":[["1e2a78d6.33f617"]]}]

View File

@@ -0,0 +1 @@
[{"id":"89904b80.4fcd08","type":"inject","z":"d44bc7c8.48eca8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":100,"wires":[["22c235a0.acb41a"]]},{"id":"20fa70dc.7107f","type":"debug","z":"d44bc7c8.48eca8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":100,"wires":[]},{"id":"cddf88c9.9df018","type":"comment","z":"d44bc7c8.48eca8","name":"asynchronous setup","info":"The `Setup` code that performs asynchronous work can return a promise object of the work. The completion of the promise is waited before starting function body.\n\nSee Node-RED user guide about [functions](https://nodered.org/docs/user-guide/writing-functions).","x":150,"y":40,"wires":[]},{"id":"22c235a0.acb41a","type":"function","z":"d44bc7c8.48eca8","name":"async. setup","func":"// retrieve message value\nmsg.payload = global.get('message');\nreturn msg;","outputs":1,"noerr":0,"initialize":"// set initial value of message\nglobal.set(\"message\", \"Initializing, World!\");\n// create promise for async work\nvar promise = new Promise((resolve, reject) => {\n // async work: wait 1s, then set message\n setTimeout(() => {\n global.set(\"message\", \"Hello, World!\");\n // report this work successfuly ended\n resolve();\n }, 1000);\n});\n// return the promise that should be executed before function code\nreturn promise;","finalize":"","x":370,"y":100,"wires":[["20fa70dc.7107f"]]}]