Hikvision Event

Hi

Does someone know if it is possible to capture a Hikvision motion event in openhab?

/mike

Have find this documentation:

This function is used to get an event notification alert stream from the media device via
HTTP or HTTPS. This function does not require that a client/VMS system be added as an
HTTP(S) destination on the media device. Instead, the client/VMS system can call this
API to initialize a stream of event information from the device. In other words, a
connection is established with the device when this function is called, and stays open to
constantly receive event notifications.
This API uses HTTP server-push with the MIME type multipart/mixed defined in RFC
2046.

Is it posible to open this kind of stream from OpenHAB and capture the events.

/Mike

Hi

I solved it by following this description and redirected the PIR alert to the alert output.

Then i maked a bash script the read the alert output.

#!/bin/bash

RESP=$(curl -s http://admin:XXXXXX@192.168.192.83/IO/outputs/1/status/)
echo "$RESP" | xpath -q -e '*/ioState/text()' | sed 's/inactive/CLOSED/g;s/active/OPEN/g'

Then i bind an Item to that bash script:

Contact Motion_GF_Kitchen	"Kök PIR [MAP(motion_se.map):%s]"	<shield>	       (GF_Kitchen,motion, security)	{ exec="<[/home/openhab/hikalert.sh:1000:REGEX((.*?))]" }`

Hi @tnemrap - Thanks for posting. I would like to try this soon, so thank you.

Question. what is content of motion_se.map file?

Hi @greg.

I also have some scripts to enable/disable motion detection if you are interested.

/mike

@tnemrap,

I have a couple of LTS(rebranded Hikvision) cameras around the house. Could you send me a copy of your scripts? I am hoping to do something similar.

Thanks.

Mike,
I have 2 Hikvision IP cameras. I would like to get them working in OpenHAB. I have a number of questions.

  1. I have my OpenHAB system running on RPI2. Where does the bash script go? Do you put it into a certain directory?
  2. Do I need to use any additional bindings?
  3. What is the motion_se.map file?
    Any help would in setting this up would be greatly appreciated. Thanks!

Unfortunately my cameras are newer and cheaper, so they don’t have the alert output or PIR sensor.

It’s a bit of a kludge, but I was able to get Hikvision motion events into a contact using an external script and the rest api. I was able to separate out the VMD (basic motion event) from the linecrossing (smart motion event). I seem to have fewer false-positives with the linecrossing detection.

2 Likes

Hello, friends.

I’ve done several changes to the picture on my Hikvision cameras and set it to change Day/Night mode manually.
Any ideas how can I change Day/Night mode from OpenHAB? Hope there is an http get or post command.
That will make my life so much easier :wink:

I’ve found a documentation on API with XML requests, but I don’t think I can put it all together.
It’s here - http://www.hikvisioneurope.com/portal/portal/Integration%20and%20Development%20Materials/03--Protocol/04--ISAPI%20protocol/00--Baseline%20ISAPI%20Protocol/HIKVISION%20ISAPI_2.5-Image%20Service.pdf

Thank you!
Sasha

In case somebody is interested in receiving alerts from a DVR on OpenHab I’m posting this solution. My Hikvision DVR did not have the alert stream API so I ended up writing a small SMTP server in NodeJS that parses emails and changes the state of openHab items (using SMTPServer and simpleParser). Here is the code

// Start the SMTP service to collect the DVR events
const server = new SMTPServer({
    logger: false,
    secure: false,
    authOptional: true,
    authMethods: ['PLAIN'],
    onData(stream, session, callback){
      // Assemble the whole string (there is some bug on the parser's logic)
      var buffers = [];
      stream.on('data', function(buffer) {
        buffers.push(buffer);
      });
      stream.on('end', function() {
        var buffer = Buffer.concat(buffers);
            simpleParser(buffer).then(mail=>{
              try{
                // console.log(JSON.stringify(mail));
                if(mail.from.value[0].name=="DVR"){
                  if(mail.subject.match(/Motion Detected On/)){
                    var match=mail.text.match(/CAMERA NAME\(NUM\):\s+([^\(]+)\(/);
                    var cameraName=match[1];
                    logger.warn("SMTP: "+mail.subject+" ("+cameraName+")");
                    if(cameraName=="Timbre"){ 
                      const noPics=5;
                      arduino.saveAndRotateImage(cameraName,noPics,mail.attachments[0].content);
                    }
                  }else{
                    //We got some notification from the DVR
                    arduino.setOpenHabItemState('CallbackEvent', (c.eventDVRNotification*100)+c.HS_Z_Raspi);
                  }
                }
              }catch(e){
                logger.error(e);
              }
          }).catch(err=>{});
          callback();
      });
    },
});
logger.info("Started SMTP server on port 2525");
server.listen(2525);

Cheers

2 Likes

I can not get this to work on my Raspberry.
Have someone been successful to get this to work on Raspberry?

/Mike

I’m assuming this is broken for several reasons… It could be that it doesn’t run on a newer version of node yet. I’m still running node 0.10.25 :grimacing:. I’m still rocking Ubuntu server 14.04 :grimacing:, so it uses upstart instead of systemd like debian jessie to start the service.

My changes to node-hikvision-api were upstream after that post, so you should be able to install it with

npm i node-hikvision-api

If that succeeds, try running it manually

nodejs /opt/cameramotion/hikmotion.js

TBH, I suck at JS, so “error handling” is mostly just hoping it all works. Looking at my logs for the service. It throws a lot of errors, but still mostly works :grimacing:.

I’m hoping to find some time this week to take another crack at OpenHAB 2 and OpenHABian… or at least get OpenHAB 1.8.3 moved over to a pi 3 & Raspbian Jessie. I’ll update the blog post if I get it working.

2 Likes

I had some issues trying to get the node-hikvision-api to work. I found the hicknotify repository by ccontavalli on github and was able to modify his code to get the results I was looking for. I don’t really know how to use github properly, so this repository is a mess, but here it is: OpenHAB-ONVIFAlerts

This lets you define an array of camera objects in the config.json file and then build the golang executable to monitor the specified cameras for motion alerts and, when motion is detected, set an OpenHAB contact to OPEN. It will be up to you to close it. Take a look at the original project’s readme for more information on how it works.

You shouldn’t have to modify the hicknotify.go file, just build it and configure the settings in config.json.

Hopefully one of y’all find this useful.

-James

1 Like

I have taken a blind stab at adding motion detection into my binding and if someone with a camera helps with posting the output of their debug logs (if it does not work already) I should be able to get it going and add some more features. I do not own a hikvision camera hence why I need help testing.

Binding found here.

I can give i a try i am now using hikmotion to capture hikvision events.

/Mike

Receiving this when trying to add Hikvision.

2018-05-07 23:51:10.227 [INFO ] [ing.ipcamera.handler.IpCameraHandler] - About to connect to IP Camera at IP:192.168.192.85:80
2018-05-07 23:51:10.276 [ERROR] [l.internal.messaging.saaj.client.p2p] - SAAJ0008: Bad Response; Not Found
2018-05-07 23:51:10.280 [ERROR] [ing.ipcamera.handler.IpCameraHandler] - com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404Not Found

Thanks for trying it.

My first thought was that the ONVIF port may not be 80 as the binding uses both ONVIF and also http to talk to the camera, then I found this.

ONVIF is disabled by default in hikvision and this PDF shows how to enable it. Whilst in there take note of any port numbers. I like the fact the hik range can be changed to basic or digest auth as well, my binding should work with either method but I have not heard back from anyone using basic yet as my cameras use digest authorisation.

Have now enabled ONVIF that was disabled last upgrade.

New error

2018-05-08 10:22:50.559 [INFO ] [ing.ipcamera.handler.IpCameraHandler] - About to connect to IP Camera at IP:192.168.192.85:80
2018-05-08 10:23:22.384 [INFO ] [ing.ipcamera.handler.IpCameraHandler] - About to fetch the Media Profile list from the camera
2018-05-08 10:23:22.446 [ERROR] [l.internal.messaging.saaj.client.p2p] - SAAJ0008: Bad Response; Bad Request

From looking at the error code it seems to be an issue with onvif password, however I suspect it is because the user name and password also need to be setup on HIKVISION to work. There is an excellent video that covers this in the first 3 minutes and is direct to the point (link below). Because it may be possible to have a different user/pass for the API it would be a good idea to make sure the onvif user and password is identical to the HTTP api details as my binding does not allow for different user and passes. Lastly best to use a admin user until it is working to rule out that as another cause of it not connecting. The software the video mentions is good for testing if the camera is setup and if this is my binding or the camera…

Now i have created a separate ONVIF user and get Online but now i receive a lot of this:

2018-05-08 18:17:39.464 [WARN ] [ing.ipcamera.handler.IpCameraHandler] - Camera at 192.168.192.85:80 is not closing the connection quick enough. Check for digest stale=?
2018-05-08 18:17:47.159 [WARN ] [ing.ipcamera.handler.IpCameraHandler] - Camera at 192.168.192.85:80 is not closing the connection quick enough. Check for digest stale=?
2018-05-08 18:17:58.671 [WARN ] [ing.ipcamera.handler.IpCameraHandler] - Camera at 192.168.192.85:80 is not closing the connection quick enough. Check for digest stale=?
2018-05-08 18:18:06.554 [WARN ] [ing.ipcamera.handler.IpCameraHandler] - Camera at 192.168.192.85:80 is not closing the connection quick enough. Check for digest stale=?
2018-05-08 18:18:13.938 [WARN ] [ing.ipcamera.handler.IpCameraHandler] - Camera at 192.168.192.85:80 is not closing the connection quick enough. Check for digest stale=?
2018-05-08 18:18:21.778 [WARN ] [ing.ipcamera.handler.IpCameraHandler] - Camera at 192.168.192.85:80 is not closing the connection quick enough. Check for digest stale=?
2018-05-08 18:18:29.181 [WARN ] [ing.ipcamera.handler.IpCameraHandler] - Camera at 192.168.192.85:80 is not closing the connection quick enough. Check for digest stale=?