Storing data into node-red

@akhtar321h this is the subflow node I made for buffering data.

Built in help...

Additional notes...

Can be persisted to file by specifying a different "Store Name" (needs setup in node-red settings.js - see context-stores)

In action...

The sample flow...

[{"id":"83c74899.222798","type":"subflow","name":"fifo buffer","info":"# About\nThis node acts like a FIFO. \nMessages added first are returned first.\nThis node performs 5 distinct operations, depending on the value of `topic`\n\n# node properties\n* **Buffer Name** - The name of the `flow` variable to store the buffer in.\n* **Store Name** - The name of store (e.g. \"file\" or \"default\"). NOTE: The storage must be setup in settings.js for this installation of node-red is using this option.\n* **Auto Status** - This flag sets the node to auto update the buffer cound status. This is needed to update all instances of the same buffer. Additionally, nodes with this option set `true` will output status info on output2 automatically when a change occurs.\n* **Topic** - The operation to perform (see Operations below).\n\n# Operations\nDepending on the value of node property `topic` (or `msg.topic` if `topic` is blank)\nThis node performs the following as below...\n* **put**\n  * adds `msg.payload` to the buffer\n  * returns the added value in `msg.added`\n* **get**\n  * simply returns the next item in the buffer \n  * `msg.payload` will be null if buffer is empty\n* **shift**\n  * removes the last item from the buffer\n  * `msg.payload` will be the value of the next item or null if buffer is empty\n* **clear**\n  * empties the buffer\n* **status**\n  * returns the count of items in `msg.payload`\n\n# Output\n1. All successful operations will return a `msg`\n  * When an item is successfully added, a copy is returned in `msg.added`\n2. Status info will be sent from this output. useful to get the status at any time.\n","category":"","in":[{"x":60,"y":120,"wires":[{"id":"33c0ee20.33ae32"}]}],"out":[{"x":900,"y":100,"wires":[{"id":"f0ff1b50.004c18","port":0}]},{"x":900,"y":260,"wires":[{"id":"fa449f35.b95a2","port":0}]}],"env":[{"name":"bufferName","type":"str","value":"buffer","ui":{"label":{"en-US":"Buffer Name"},"type":"input","opts":{"types":["str","env"]}}},{"name":"storeName","type":"str","value":"file","ui":{"label":{"en-US":"Store Name"}}},{"name":"autoUpdateStatus","type":"bool","value":"false","ui":{"label":{"en-US":"Auto Status"}}},{"name":"topic","type":"str","value":"","ui":{"label":{"en-US":"Topic"}}}],"color":"#C0DEED","outputLabels":["msg","status"],"icon":"font-awesome/fa-database","status":{"x":900,"y":340,"wires":[{"id":"6572aae8.238064","port":0}]}},{"id":"f0ff1b50.004c18","type":"function","z":"83c74899.222798","name":"get next in buffer","func":"var getBuffer = msg._util.getBuffer;\nvar setBuffer = msg._util.setBuffer;\n\nvar buffer = getBuffer();\n\nvar entry = buffer.length ? buffer[buffer.length-1] : null;\nnode.status({text:`Buffer: ${buffer.length}`});\nmsg.payload = entry;\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":100,"wires":[[]]},{"id":"bb7ccfd5.61128","type":"function","z":"83c74899.222798","name":"add to buffer","func":"var getBuffer = msg._util.getBuffer;\nvar setBuffer = msg._util.setBuffer;\n\nvar buffer = getBuffer();\nbuffer.unshift(msg.payload);\nsetBuffer(buffer);\nmsg.buffer = buffer;\nmsg.added = msg.payload;\nnode.status({text:`Buffer: ${buffer.length}`});\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":220,"wires":[["f0ff1b50.004c18"]]},{"id":"adca17cc.e60358","type":"switch","z":"83c74899.222798","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"get","vt":"str"},{"t":"eq","v":"shift","vt":"str"},{"t":"eq","v":"clear","vt":"str"},{"t":"eq","v":"put","vt":"str"},{"t":"eq","v":"status","vt":"str"},{"t":"eq","v":"autostatus","vt":"str"}],"checkall":"true","repair":false,"outputs":6,"x":330,"y":220,"wires":[["f0ff1b50.004c18"],["8f13088f.c890b8"],["8f13088f.c890b8"],["bb7ccfd5.61128"],["88c020b4.c5b49"],["88c020b4.c5b49"]]},{"id":"6572aae8.238064","type":"status","z":"83c74899.222798","name":"","scope":null,"x":100,"y":340,"wires":[[]]},{"id":"8f13088f.c890b8","type":"function","z":"83c74899.222798","name":"shift / clear","func":"var getBuffer = msg._util.getBuffer;\nvar setBuffer = msg._util.setBuffer;\n\nvar buffer = getBuffer();\n\nif(msg.topic == \"clear\"){\n    buffer = [];\n} else if(buffer.length){\n    msg.item = buffer.pop();\n}\n\nsetBuffer(buffer);\n\n\nnode.status({text:`Buffer: ${buffer.length}`});\nmsg.payload = buffer.length;\n\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":180,"wires":[["f0ff1b50.004c18"]]},{"id":"33c0ee20.33ae32","type":"function","z":"83c74899.222798","name":"init msg.utils","func":"\nif(msg.topic !== \"autostatus\"){\n    msg.topic = env.get(\"topic\") || msg.topic;\n}\n\nvar _util = {\n    bufferName: env.get(\"bufferName\"),\n    storeName : env.get(\"storeName\")\n};\n\n_util.getBuffer = function(){\n    return flow.get(\"$parent.\" + _util.bufferName, _util.storeName) || [];\n}\n\n_util.setBuffer = function(buffer){\n    return flow.set(\"$parent.\" + _util.bufferName, buffer, _util.storeName);\n}\n\n_util.showError = function(errText, msg){\n    let e = new Error(errText);\n    node.error(e,msg);\n    node.status({text:e})\n}\n\nmsg._util = _util;\nreturn msg;","outputs":1,"noerr":0,"x":210,"y":120,"wires":[["adca17cc.e60358"]]},{"id":"b9e97c41.4dc98","type":"inject","z":"83c74899.222798","name":"status updater","topic":"autostatus","payload":"autoUpdateStatus","payloadType":"env","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":40,"wires":[["6b0e0fc2.e4a33"]]},{"id":"6b0e0fc2.e4a33","type":"switch","z":"83c74899.222798","name":"is autoUpdateStatus set?","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":40,"wires":[["33c0ee20.33ae32"]]},{"id":"88c020b4.c5b49","type":"function","z":"83c74899.222798","name":"update status","func":"var buffer = msg._util.getBuffer();\nnode.status({text:`Buffer: ${buffer.length}`});\nmsg.payload = buffer.length;\nreturn msg;\n","outputs":1,"noerr":0,"x":560,"y":260,"wires":[["fa449f35.b95a2"]]},{"id":"fa449f35.b95a2","type":"rbe","z":"83c74899.222798","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":730,"y":260,"wires":[[]]},{"id":"aeac549b.1567f8","type":"subflow:83c74899.222798","z":"66a6a259.d08c0c","name":"FIFO1","env":[{"name":"bufferName","value":"FIFO1","type":"str"},{"name":"storeName","value":"default","type":"str"},{"name":"autoUpdateStatus","value":"true","type":"bool"}],"x":730,"y":660,"wires":[["9d9fde76.c3531"],["1c948287.5c03cd"]]},{"id":"1599929c.288ccd","type":"inject","z":"66a6a259.d08c0c","name":"","topic":"put","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":570,"y":580,"wires":[["aeac549b.1567f8"]]},{"id":"331c7cc7.065234","type":"inject","z":"66a6a259.d08c0c","name":"","topic":"get","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":550,"y":620,"wires":[["aeac549b.1567f8"]]},{"id":"8706e3f1.5552a","type":"inject","z":"66a6a259.d08c0c","name":"","topic":"shift","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":560,"y":660,"wires":[["aeac549b.1567f8"]]},{"id":"4bb4d489.e97c9c","type":"inject","z":"66a6a259.d08c0c","name":"","topic":"clear","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":560,"y":700,"wires":[["aeac549b.1567f8"]]},{"id":"141c35f5.8d55ea","type":"debug","z":"66a6a259.d08c0c","name":"get only","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1020,"y":580,"wires":[]},{"id":"1c948287.5c03cd","type":"debug","z":"66a6a259.d08c0c","name":"status","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":680,"wires":[]},{"id":"9d9fde76.c3531","type":"switch","z":"66a6a259.d08c0c","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"get","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":870,"y":600,"wires":[["141c35f5.8d55ea"],["d113bfaa.152ca"]]},{"id":"d113bfaa.152ca","type":"debug","z":"66a6a259.d08c0c","name":"others","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1010,"y":620,"wires":[]},{"id":"831bd60d.beee58","type":"subflow:83c74899.222798","z":"66a6a259.d08c0c","name":"FIFO2","env":[{"name":"bufferName","value":"FIFO2","type":"str"},{"name":"storeName","value":"default","type":"str"}],"x":730,"y":880,"wires":[["bac89208.68dfd"],["c69f318a.68f69"]]},{"id":"86e29396.b5c16","type":"inject","z":"66a6a259.d08c0c","name":"","topic":"put","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":570,"y":800,"wires":[["831bd60d.beee58"]]},{"id":"21f65048.fc37c","type":"inject","z":"66a6a259.d08c0c","name":"","topic":"get","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":550,"y":840,"wires":[["831bd60d.beee58"]]},{"id":"76a7cba3.f01024","type":"inject","z":"66a6a259.d08c0c","name":"","topic":"shift","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":560,"y":880,"wires":[["831bd60d.beee58"]]},{"id":"7bced259.c2f52c","type":"inject","z":"66a6a259.d08c0c","name":"","topic":"clear","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":560,"y":920,"wires":[["831bd60d.beee58"]]},{"id":"784628ea.e62948","type":"debug","z":"66a6a259.d08c0c","name":"get only","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1020,"y":800,"wires":[]},{"id":"c69f318a.68f69","type":"debug","z":"66a6a259.d08c0c","name":"status","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":900,"wires":[]},{"id":"bac89208.68dfd","type":"switch","z":"66a6a259.d08c0c","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"get","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":870,"y":820,"wires":[["784628ea.e62948"],["1a4c70cd.1d8e1f"]]},{"id":"1a4c70cd.1d8e1f","type":"debug","z":"66a6a259.d08c0c","name":"others","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1010,"y":840,"wires":[]},{"id":"b2f11c36.64f7e","type":"inject","z":"66a6a259.d08c0c","name":"","topic":"status","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":560,"y":960,"wires":[["831bd60d.beee58"]]}]
1 Like