How to watch a directory and its subdirectories for new files?

2,367 views
Skip to first unread message

andreas....@softhouse.se

unread,
Feb 6, 2017, 1:15:05 AM2/6/17
to Node-RED
Hi,
Found the wonderful Node-red a couple of months ago and have managed to automate my house with a Raspberry Pi, Telldus duo and a couple of cameras.
The cameras FTPs their files to the Raspberry where Node-Red is running.
One D-link camera creates subdirectories for each day and hour before storing. I've googled a lot and that seems to be the only way for it to store files.
I want Node-red to e-mail and send pushbullet-notifications when a file is stored but since the included core watch-node does not work recursively I'm stuck.
I thought of one approach to build the input parameter to the watch-node as a flow-variable depending on the time being but it doesn't look like you can have the argument to a node as a variable.
My next try was to use the node-watch package which defaults to recursive mode, https://www.npmjs.com/package/node-watch
I tired it first as a stand alone like this:
var watch = require('/usr/local/lib/node_modules/node-watch');
watch('/home/pi/FTP/Cameras/inhouse1/20170202/15', function(filename) {
    if( /\.mp4$/.test(filename)) {
        node.log(filename, ' changed.');
    }
});

Works perfectly.


So here is what I did.
1. cd to .node-red
2.npm install node-watch
That command seems to added the a directory named node-watch in the node_modules directory and also added the a row in the settings.js filelooking like
watch:require('node-watch') which I found out should make it possible to use it in a function.
3. Then I added a function node with the following code connected to a debug-node and triggered by a inject node every 3rd second



Code in the function-node looks like:

var watch = global.get('watch');

watch('/home/pi/FTP/Cameras/inhouse1/20170202/15', function(filename) {
        msg.payload=filename+' changed.';
        return msg;
});


It never triggers whatever I do in the directory....any suggestions on how to proceed?
One more question from a Javascript rookie,
Isn't this code going to create a new watch-object every 3rd second which will result in a lot of them being created after a couple of days resulting in memory problems?
brgds
Andreas
Auto Generated Inline Image 1

Mike Biddell

unread,
Feb 6, 2017, 3:00:33 AM2/6/17
to Node-RED
Andreas

I do that. I use node-red-contrib-ftp-server. I do not bother to store the image locally (but you could). I just email them. After all, if you have the image in your email to review I personally dont see the point of a local store. So, although that is not a specific answer to your query, in the broadest sense, it is a way of achieving what you want.

mike

Julian Knight

unread,
Feb 7, 2017, 2:03:22 AM2/7/17
to Node-RED
Is there a problem with the watch note?


On Monday, 6 February 2017 06:15:05 UTC, andreas....@softhouse.se wrote:

andreas....@softhouse.se

unread,
Feb 7, 2017, 2:41:40 AM2/7/17
to Node-RED
Aha, that was another approach to solve the problem. I'll look into it.
Thanks for the hint.
/Andreas

andreas....@softhouse.se

unread,
Feb 7, 2017, 2:48:52 AM2/7/17
to Node-RED
Hi,
Well if I could wish I would like it to watch into subdirectories as well. Today it is limited to the folder you tell it to look into.
Maybe a parameter "Recursive mode" or not...
Another way would be if it took the folder to watch as a parameter, then you could wire another node building the path in runtime to it.
/Andreas

Dave C-J

unread,
Feb 7, 2017, 6:47:57 AM2/7/17
to node...@googlegroups.com
Adding recursion to the watch node is a sensible thought... will add to to-do list.

andreas....@softhouse.se

unread,
Feb 8, 2017, 3:15:26 PM2/8/17
to Node-RED
Mike,
Could you please show how you use that node?
I set set the IP and port to the existing ftp-server on the PI and the user/pwd to an existing user as well.
When I deploy node-red stop responding.
I then manually have to open flows_raspberrypi.json in a text editor and change the port to something else and restarting node-red.
Using a different port and user doesn't hang node-red but I cannot connect with a ftp-client nor the cameras...

/Andreas


On Monday, February 6, 2017 at 9:00:33 AM UTC+1, Mike Biddell wrote:

andreas....@softhouse.se

unread,
Feb 25, 2017, 3:23:44 PM2/25/17
to Node-RED
Hi Dave,
Found out that there already is a commit to add recursion. wow! :-)
https://github.com/node-red/node-red/commit/7029541b4f4ab749b6c0cebb449440d419c3d412

What is the best way to get hold of that change to run it in my Raspberry PI?
I'm running version  0.16.2 of node-red today.
I tried git clone but didn't get the https-url right to clone branch 0.17.
Can I just manually add the changes to my local files? What is needed to rebuild the sources then?
How long may it take before it is included into the master-branch?
Best regards
Andreas

Dave C-J

unread,
Feb 25, 2017, 5:43:50 PM2/25/17
to node...@googlegroups.com
hmmm - at this point in time it would probably be quickest/safest to just hand edit those files in the 0.16.2 version you have. 
Reply all
Reply to author
Forward
0 new messages