Hey all,
I'm trying to create a simple flow to download the latest .csv file generated to the user's machine.
Context: When the user turns logging on, some other flows log external measurements to a local .csv file stored in the folder "/home/pi/log/". The file name varies based on the time they started measuring, but the full path is stored in global variables 'global.a-filename' and 'global.b-filename'.
I've tried integrating a version of this flow but can't get the flow to work with my variables, as they include the full path and using them without a default path seems to cause issues.
If i remove the path in the variable, leaving just the file name, and insert the path into the base path, I can get it to download. Perhaps I can just edit the string I'm inputting to remove the file path.
Here's an example of my variable:
global.a-filename="/home/pi/log/A_Power_Log20220427-11:42:32.csv"
And here's what I'm using currently in place of it:
"/A_Power_Log20220427-11:42:32.csv"
Any suggestions? I am by no means a node-red pro, so struggling a bit!
Here's what I've come up with...
[{"id":"1c47c3c0.9e4ecc","type":"function","z":"af4bb936.890328","name":"Set base path","func":"// using basepath = \"\" doesn't work as I'd hoped\n// example global.a-filename = /home/pi/log/A_Power_Log20220427-11:42:32.csv\n\nvar basePath = \"/home/pi/log/\";\nvar filename = msg.req.params.fn;\n\nmsg.filename = basePath + filename;\nreturn [msg, null];\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1280,"y":1640,"wires":[["ad804a0d.4310e8"],["ea82bad4.101d98"]]},{"id":"ea82bad4.101d98","type":"http response","z":"af4bb936.890328","name":"","statusCode":"","headers":{},"x":1650,"y":1680,"wires":[]},{"id":"7d2d8318.53d4bc","type":"catch","z":"af4bb936.890328","name":"","scope":null,"uncaught":false,"x":1120,"y":1720,"wires":[["bfbc1050.6708a","747edb6e.69d3b4"]]},{"id":"bfbc1050.6708a","type":"function","z":"af4bb936.890328","name":"Set 404","func":"msg.payload = msg.error;\nmsg.statusCode = 404;//resource not found\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1500,"y":1720,"wires":[["ea82bad4.101d98"]]},{"id":"747edb6e.69d3b4","type":"debug","z":"af4bb936.890328","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1150,"y":1760,"wires":[]},{"id":"a32f0ed2.38111","type":"ui_template","z":"af4bb936.890328","group":"dfb4a60f.d788f8","name":"ui_temlplate - present download links on dashboard","order":0,"width":0,"height":0,"format":"<div >\n <a href=\"/A_Power_Log20220427-11:42:32.csv\"> Download File A</a>\n <a href=\"/B_Power_Log20220427-11:42:32.csv\">Download File B</a>\n</div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":1490,"y":1800,"wires":[[]]},{"id":"8d759c0.04f7a68","type":"http in","z":"af4bb936.890328","name":"","url":"/:fn","method":"get","upload":false,"swaggerDoc":"","x":1080,"y":1640,"wires":[["1c47c3c0.9e4ecc"]]},{"id":"ad804a0d.4310e8","type":"file in","z":"af4bb936.890328","name":"","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":1490,"y":1620,"wires":[["ea82bad4.101d98"]]},{"id":"dfb4a60f.d788f8","type":"ui_group","name":"Data Export","tab":"48418b79.0f5834","order":1,"disp":true,"width":"12"},{"id":"48418b79.0f5834","type":"ui_tab","name":"Dashboard","icon":"dashboard","order":1}]