Binding to integrate with imagery generated by motion detection CCTV

Tags: #<Tag:0x00007fc3f215a118> #<Tag:0x00007fc3f215a000> #<Tag:0x00007fc3f2159df8>

Hi All

I have been migrating my house across from OpenWRT and a bunch of custom lua scripts to OpenHab running on a Raspberry Pi 2 and I’m loving it!

This is my first post on the forum and I wanted to get your opinions on an idea I had for a binding - maybe there’s something out there already that does something similar?

One thing I’ve recently set up is a Hikvision camera outside the house which monitors my front drive. I’ve configured motion detection so that when there’s movement across my perimeter the camera FTPs across images to the raspberry Pi.

I was thinking about creating a custom binding which could do the following:

  • manage the images - i.e. delete them when there are too many / too old / using too much disk space
  • publish an event to the bus when motion is detected and new images are created
  • offer a servlet which renders the images for inspection

If I had a binding like that I could then integrate the imagery into the GUI and put some rules about firing alarms/notifications when there’s movement etc…

What do you think?

1 Like

I’ve had a very similar hope for a clean solution to integrate my surveillance DVRs as motion detecting alarm inputs to openHAB, and a good way to manage snapshot images that come in. The two avenues I’ve thought about for this integration are

  1. Develop a binding that speaks ONVIF directly to the DVR. This is a challenging path because the ONVIF protocol is hard to work with and not well documented, but it does hold the potential for tight integration.
  2. Wait for openHAB 2 to be running on the Karaf OSGi container, and integrate between Apache Camel and ESH/OH2 so that when new files arrive via FTP from the DVR, post update events to openHAB and perform other actions like putting a snapshot image into a well known name and location for client access and other file management steps. Apache Camel already supports a very flexible and powerful approach to handling all sorts of files through “routes,” much of which can be defined in XML with Spring or Blueprint.

Please let us know how you address this integration, as I suspect we’re not alone in wanting a good solution!

ok in which case I will have a play and try and create a binding for openhab 1.7. Will post back if/when I can get something sensible working.

I am attempting to code a binding but given that I have no knowledge of OSGi and haven’t written much java for the last few years my progress is painfully slow.

Would anybody be able to review what I’ve done so far and point me in the right direction please?

If so please let me know and I’ll put the code online somewhere…

Thanks.

Ben

I managed to complete the binding on my own and it works nicely. Now I can just configure items for each camera like this:

DateTime camera1 "Date [%1$td.%1$tm.%1$tY %1$tT]" <calendar> { snapshot="path=/path/to/snapshots;maxFiles=50" }
DateTime camera2 "Date [%1$td.%1$tm.%1$tY %1$tT]" <calendar> { snapshot="path=/another/path/here;maxDays=30" }

When the binding starts, it begins monitoring the configured directories. I have my Hikvision cameras set up to send jpegs based on motion detection to those directories via FTP. When a new snapshot appears then the binding does the following:

  • updates the corresponding Item on the event bus
  • if the option maxDays is configured, it deletes any snapshots older than the value configured
  • if the option maxFiles is configured, it sorts the snapshots by creation date, then deletes the oldest files so that the file count for the given folder does not exceed maxFiles

The binding has a webservlet with the following features:

  • renders a webpage to summarise the folders and their contents
  • groups the snapshots based on timestamp (if snapshots are less than a minute apart they are presumed to relate to the same event and so are grouped together). This means it’s easy to browse recent ‘events’. The snapshot shown in the summary view is the middle snapshot.
  • renders a webpage to provide a list of snapshots in a specific group
  • uses tokens and template files (I took the concept from the excellent weather binding) so that you can fully customise the webpages
  • can optionally resize snapshots to create thumbs/previews in the webpages
  • can render the original snapshot

I was planning to do a pull request and contribute the binding back, but according to the ‘How to Contribute’ page, it needs to be tested on OH 2.0 which it’s not as I am on OH 1.7.

So if anbody’s interested in this binding or sees value in it, please let me know, in which case I can either send the code or if there’s enough enthusiasm, I’ll upgrade to OH 2 and contribute.

Hey just to let you know, this would be something I would be interested in using. I’m probably a long time away from using it though. I haven’t even got my server back up and running after moving house.

Josh

Hi watou.

What dvr do you use that has a more open configuration go support this. I’ve got some analogue cameras and a budget dvr but can’t get to inter grate it into openhab

Thanks

I’m in the same situation, where my “budget” DVR cannot be integrated well into other things like openHAB. It doesn’t support web callbacks on motion, it doesn’t have “snapshot” URLs to feed into a sitemap. It will email or FTP snapshots on motion, so the trick is to find a way to have openHAB field either one of those. I think @PolishBen’s work might be the best possible if you have one of these closed DVRs.

Someday I hope to replace the DVR with a network DVR that takes analog inputs and creates H.264 streams from them, has snapshot URLs and web callbacks on motion or other events. But all I’m aware of now are closed systems that have some proprietary ONVIF protocol implementation, and I don’t have time to sniff the packets and make sense of it.

Hi @PolishBen,

sounds great.
I am struggling with my own solution so I would greatly appreciate if you could share your setup.
dziękuję

Best regards,
NCO

1 Like

just learning how to use GitHub so bear with me :smile:

Have created a ticket here https://github.com/openhab/openhab/issues/3583 where I’ve linked to a fork.

Hi @PolishBen,
your snapshot binding works very well - thanks for sharing.

I just have one issue (openhab.log).

Error attempting to delete file: /var/www/upload/abus/evt20151228_134007.log

What I already tried is to set the owner of the files to openhab, so that openhab (your binding) should be able to delete the files according to your limit parameter (days / files).
Any suggestion?

Hi

Great - glad to hear it. Have you set up the web page to go with it too?

Does your snapshot directory contain files other than JPGs - I am wondering why it’s trying to delete a log file? I recommend you have a dedicated snapshot directory because the binding will try and delete all files if maxFiles/maxDays options are set.

@PolishBen,

no, not yet - I just display the latest file like this:

Frame {
                Image url="http://localhost:8080/abus/latest.jpg" label="TeleEye Snapshot" refresh=10000
            }

Obviously the camera generates the log.
So your snapshot tool just deletes the .jpg files?

No, it simply blindly attempts to delete every file in the directory, regardless of file type. Therefore I am sure you can just ignore that warning in the log file or turn it off by increasing the log threshold in your logback.xml file.

I will do an enhancement in future so it only looks for certain file types (i.e. jpg) to provide a more elegant solution.

Thanks.
I just put a workaround in one of my scripts to delete the log files myself.
Otherwise no more jpg files can be uploaded and the actual snapshot is not updated accordingly.

Hi @PolishBen,

I still have problems with deleting files.
Now I got the message:
[.snaphsot2.SnapshotFileManager] - Error attempting to sort directory: /var/www/upload/abus/Motion20160117_214407.jpg

Does your binding delete using sudo or does it assume that i has the rights to delete files upload by a different user?

It just presumes it has the rights to delete files. I suspect it runs as whichever user you’re running OpenHab under. I am running openhab as a sudo user because my /etc/init.d/openhab script starts OpenHab using sudo.

Hi @PolishBen,

finally I tried to implement your webview stuff, but I am not sure if I understood everything completely.
When you refer to the “item” to link to the images (like cam1 in your examples) is all this covered by the DateTime item?
I my setup below correct accordingly?

item:
DateTime Abus_Motion_tmstmp "Zeitpunkt: [%1$td.%1$tm.%1$tY - %1tH:%1$tM:%1$tS]" <calendar> (G_Abus) { snapshot="path=/var/www/upload/abus;maxFiles=90" }

sitemap:
Text item=Abus_Motion_tmstmp {
Frame {
Image url=“http://localhost:8080/snapshot?action=snapshot&name=Abus_Motion_tmstmp&size=360:360
}
}
Text label=“Recent Activity” icon=“none” {
Webview url="/snapshot" height=14
}

index.html:

<html>
<body>
<div class="summary">
<h1>TeleEye</h2>
<!-- This example presumes there is an item called "Abus_Motion_tmstmp" defined in the items file: -->
<p>${Abus_Motion_tmstmp.fileCount} snapshot(s) found in ${Abus_Motion_tmstmp.groupCount} group(s)</p>
<ol>
${Abus_Motion_tmstmp.groups}
</ol>
</div>
</body>
</html>

in my iPhone App I just get:
TeleEye
0 snapshot(s) found in 0 group(s)

Hi

Your understanding is correct and your config looks fine to me.

I don’t see any obvious reason why it’s not finding any files - can you
turn on debug level in the log? Anything unusual? Any errors?

Obviously the upload from the camera messed up the directory and deleted all files :wink:
So that’s fixed.
However, now I get:

TeleEye
4 snapshot(s) found in 1 group(s)
1. ${timestamp}

I found {timestamp} in the group.html, but don’t know, what to do with it. :frowning:
Any thoughts?

By the way: the single image works:

Frame {
    Image url="http://localhost:8080/snapshot?action=snapshot&name=Abus_Motion_tmstmp&size=360:360"
    }

I assume your snapshot is running as openhab?
Because the folder with the images is owned by ftpuser, but openhab is in the group of ftp user (deleting obviously works as well).