My last post detailed how to integrate a cheap IP cam with SmartThings. I briefly mentioned a SmartApp that took a picture when the door opened. This was pretty straightforward. I wanted to take it a step further and trigger recording when my door was opened (but no one is home). Beyond the obvious, I had the following requirements:
- SmartThings needs to be able toggle recording. The SmartApp should notify my household when it begins recording.
- Tampering with the camera shouldn’t destroy already captured footage.
- Footage should start uploading somewhere offsite as soon as possible.
- Control over uploaded footage.
I already covered (1) in my last post. My integration with the IP camera enables a scheduled recording feature, and configures this feature to always record. Switching off recording clears the schedule.
Uploading footage to S3
The camera I’m using already has builtin support to upload footage to an FTP server, which leaves everything but uploading to offsite storage.
Since it’s easy and cheap, I decided to upload footage to Amazon S3. I then needed a tool that:
- Watched for newly created files to appear in the directory my internal FTP server is pointed at. When a new file is detected:
- Immediately begin a streaming upload to S3. Since the file size isn’t known ahead of time, I made use of the multipart upload API, which allows for the breaking down of uploads into smaller (5MB) chunks.
- When the file is done being written to, complete the upload (i.e., tell Amazon the file is done being uploaded). This makes the file available on S3.
This seemed like a good fit for inotify, which allows for the monitoring of filesystem events. It’s possible to set up notifications that are triggered when, for example, a new file within a directory is opened, closed, or modified.
I didn’t find a tool that did exactly what I wanted, so I made a ruby library that did. I called it “s3reamer“. My sincerest apologies for being an awful portmanteau-ist. I run this on my home server:
s3reamer /path/to/cam_data org-sidoh-mybucket --log-file "log/s3reamer.log"
This will automatically begin uploading to S3 when recording on the device is triggered. Here’s a snippet from the log file:
I, [2016-03-20T13:47:26.761571 #13905] INFO -- : Setting up watch on: /path/to/cam/data
I, [2016-03-20T13:47:34.664618 #13905] INFO -- : File opened: /path/to/cam/data/my/foscam/record/schedule_20160320_124834.mkv
I, [2016-03-20T13:47:46.218999 #13905] INFO -- : File closed. Completing S3 upload: /path/to/cam/data/my/foscam/record/record/schedule_20160320_124834.mkv
Uploading starts within a few seconds of recording being switched on. Most of that delay is waiting for the camera to begin uploading.
SmartApp to trigger recording when door opens
This part was pretty straightforward. Code below. This also sends a push notification when recording is switched on so my household knows and can react accordingly.