How I used Node-RED.
Node-RED is a flow-based programming tool. It is used a lot in IoT projects.
I had a requirement to stitch together a number of discrete tasks into a workflow. I felt Node-RED would be a good fit to solve this problem. It makes it very easy to wire together different processes (they are represented as nodes), and pass messages between them, for example a status code or output created by one node can be processed by a different node in the workflow.
Problem Specifics:
I had a requirement to reprocess a file. Files are automatically processed using default parameter values. Sometimes when a file is processed the final output is not optimal, the default parameters used to generate it are not suitable. In this instance I would like to be able to re-run the process repeatedly, tweaking those parameters until such time as I was satisfied with the final set of output results.
There are only 2 python scripts that needed to be run. These are run in sequence, the first script generates a set of files, and the second script converts those files into a set of execution instructions.
I would need a UI to enter the parameter values that are used as the command line arguments to the first script.
Solution Specifics:
Node-RED has a UI component.
There is no coding involved in creating this UI, no UI coding at least.
I only needed 2 fields and 1 button.
The first field was a number in the range from 1 - 8.
The second field contained a list of values drop-down option. To get the values to populate the drop-down did involve a small amount of coding.
This is the complete workflow snippet to populate the drop-down values.
The "Populate LOV" node contains this snippet to generate the list of values, which in this case refer to directory names, these are used as product codes.
ls -lart /home/pi/maker/artmart/out | grep "^d" | sed 's/^.* //' | sed 's/\.*//' | awk NF
The "convert to JSON" node contains this Javascript code to convert the list into a JSON object.
let Arrayobj = [];
msg.payload.split("\n").forEach(
function(item) {
node.log(item);
let nme = item.toString();
let dir = {[nme] : nme };
Arrayobj.push(dir);
}
);
msg.options = Arrayobj;
return msg;
And that is it in terms of the UI.
When the button is pressed it runs the process. The diagram for that is:
The template node collates the 2 input arguments to the node "Process". The Process node calls the bash shell script
. ${ARTMRT}/../bin/reprocess.sh
The reprocess script is very simple, it runs the python scripts in sequence.
export ARTMART=/home/pi/maker/artmart echo "process product code" $1 " with " $2 " buckets of colour" rm $ARTMART/out/$1/*.png || true $ARTMART/bin/preprocess.py $2 $1 rm $ARTMART/gcode/$1/*.ng || true $ARTMART/bin/createRecipe.py $1
Conclusion:
The beauty of using a tool in this scenario is it abstracts the detail away from the operation of executing the work. All I have to do is pick a couple of parameter values, and click on a button, then check the outcome.
Node-red is one of the best orchestration tools available and the rich set of available packages makes it extremely convenient to use.
Another example of node-RED. I'd encourage anyone who is currently considering any sort of project that has an automation component to give this a look.