Ring buffer

Ring buffer subflow

This flow receives a series of msg.payloads and builds a ring biffer with a specific buffer size. The buffer size is configurable through subflows environment variables.

This subflow was created by Smart-Tech as part of the ST-One project.

Input

The msg.payloadwill be added to the ring buffer array, except if msg.topic == list

If msg.topic == list the complete buffer will be sent at the secont output.

Outputs

Output 1

Always sends the original message

Output 2

Always return the complete buffer.

Status

Always shows the queue size.

Environment Variables

Buffer_Size defines the size of the queue. If a new item arrives and the queue is full, the oldest item will be removed.

[{"id":"6c259dd5.72f0d4","type":"subflow","name":"Ring Buffer","info":"# Ring buffer\n## Last in First Out subflow\nThis flow receives a series of msg.payloads and builds a ring bifferwith a specific buffer size. The buffer size is configurabe through subflows enviroment variables.\n\n### Input\nThe `msg.payload`will be added to the ring buffer array, except if `msg.topic == list`\n\nIf `msg.topic == list` the complete buffer will be sent at the secont output.\n\n\n### Output 1\nAlways sends the original message\n\n### Output 2\nAlways return the complete buffer.\n\n### Status\nAlways shows the queue size.\n\n### Enviroment Variables\n`Buffer_Size` defines the size of the queue. If a new item arrives and the queue is full, the oldest item will be removed.","category":"","in":[{"x":180,"y":100,"wires":[{"id":"b4286eb1.847af"}]}],"out":[{"x":280,"y":40,"wires":[{"id":"6c259dd5.72f0d4","port":0}]},{"x":800,"y":140,"wires":[{"id":"6c6b3594.fac22c","port":0}]}],"env":[{"name":"Buffer_Size","type":"num","value":"5"}],"inputLabels":["payload"],"outputLabels":["Original message","Buffer"],"icon":"font-awesome/fa-circle-o-notch","status":{"x":1020,"y":100,"wires":[{"id":"36df5ea7.231082","port":0}]}},{"id":"b4286eb1.847af","type":"switch","z":"6c259dd5.72f0d4","name":"Topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"list","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":100,"wires":[["6c6b3594.fac22c"],["afc05e2d.59f36"]]},{"id":"afc05e2d.59f36","type":"function","z":"6c259dd5.72f0d4","name":"put","func":"var queue = flow.get(\"queue\")\n\n\nif (!Array.isArray(queue)){\n    queue = [];\n}else if ( queue.length >= env.get(\"Buffer_Size\") ){\n    queue.pop(1);\n}\n\nqueue.unshift(msg.payload);\n\nflow.set(\"queue\", queue);\nmsg.payload = queue;\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":140,"wires":[["6c6b3594.fac22c"]]},{"id":"6c6b3594.fac22c","type":"change","z":"6c259dd5.72f0d4","name":"list","rules":[{"t":"set","p":"payload","pt":"msg","to":"queue","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":100,"wires":[["36df5ea7.231082"]]},{"id":"36df5ea7.231082","type":"change","z":"6c259dd5.72f0d4","name":"Count","rules":[{"t":"set","p":"payload","pt":"msg","to":"$count(msg.payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":100,"wires":[[]]},{"id":"17a6b873.473678","type":"subflow:6c259dd5.72f0d4","z":"ab14b933.6f7428","name":"","env":[],"x":510,"y":700,"wires":[["d9637269.8c3ab"],["6084378e.1a3b48"]]}]

Flow Info

Created 6 years, 5 months ago
Updated 6 years ago
Rating: 5 3

Owner

Actions

Rate:

Node Types

Core
  • change (x2)
  • function (x1)
  • switch (x1)
Other
  • subflow (x1)
  • subflow:6c259dd5.72f0d4 (x1)

Tags

  • ring
  • buffer
  • queue
  • ST-One
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option