Hikvision Event

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.

https://www.hikvision.com/ueditor/net/upload/2017-11-13/c81ae947-b819-4859-b298-fb1ce1f463ac.pdf

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=?

Disabled modbus binding and now it seems to work.

/Mike

Thanks Mike I’ll take a look at Modbus to look at why, can you answer these please as to what does and does not work?

  1. Does the picture update in the image channel in paperUI’s control tab?
  2. The motion alarm channel changes correctly when you have movement?
  3. Which Modbus binding are you using as I believe there is a new V2 as well as an older openhab 1 binding?

The enable/disable motion control does not work yet that will be next to look at if the motion detection is working correctly. Also the extra options for only updating on motion alarm does not work in your build, but I have done the work for the next build.

Thanks,

Matt

When i connected an item to image i started to receive this

2018-05-09 07:58:13.820 [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-09 07:58:24.837 [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-09 07:58:32.305 [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-09 07:58:39.767 [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-09 07:58:47.428 [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-09 07:58:47.586 [WARN ] [ore.internal.events.OSGiEventManager] - Dispatching event to subscriber 'org.eclipse.smarthome.core.internal.items.ItemUpdater@bd521c' takes more than 5000ms.
2018-05-09 07:58:54.686 [WARN ] [ore.internal.events.OSGiEventManager] - Dispatching event to subscriber 'org.eclipse.smarthome.core.internal.items.ItemUpdater@bd521c' takes more than 5000ms.
2018-05-09 07:58:58.632 [ERROR] [ing.ipcamera.handler.IpCameraHandler] - Can not connect to the camera at 192.168.192.85:80 check your network for issues or change cameras settings.
2018-05-09 07:59:09.510 [INFO ] [ing.ipcamera.handler.IpCameraHandler] - About to connect to IP Camera at IP:192.168.192.85:80
2018-05-09 07:59:20.478 [WARN ] [ore.internal.events.OSGiEventManager] - Dispatching event to subscriber 'org.eclipse.smarthome.core.internal.items.ItemUpdater@bd521c' takes more than 5000ms.
2018-05-09 07:59:46.024 [WARN ] [ore.internal.events.OSGiEventManager] - Dispatching event to subscriber 'org.eclipse.smarthome.core.internal.items.ItemUpdater@bd521c' takes more than 5000ms.
2018-05-09 08:00:07.924 [WARN ] [ore.internal.events.OSGiEventManager] - Dispatching event to subscriber 'org.eclipse.smarthome.core.internal.items.ItemUpdater@bd521c' takes more than 5000ms.
2018-05-09 08:00:57.669 [WARN ] [ore.internal.events.OSGiEventManager] - Dispatching event to subscriber 'org.eclipse.smarthome.core.internal.items.ItemUpdater@bd521c' takes more than 5000ms.
2018-05-09 08:00:57.659 [ERROR] [nal.common.AbstractInvocationHandler] - An error occurred while calling method 'EventSubscriber.receive()' on 'org.eclipse.smarthome.core.internal.items.ItemUpdater@bd521c': Java heap space
java.lang.OutOfMemoryError: Java heap space

Now i have tried a couple of times but it always give OutOfMemoryError: Java heap space on Raspberry Pi 3 B+

/Mike

Can you enable debug output in the log please? How to do this is in the first post of the IpCamera thread.

The warning you are getting is the camera not closing the connection after it replies and is not an error or something to be concerned about, but this may be causing the OOME and be a clue as to where to look in my code. It does not occur for me as my camera always closes the connection. I will look into this over the next few days and make a new build. There are a number of other bindings using the Netty library and I also need to look into this as well as it may be a conflict with another binding that also uses Netty. If you can post some of the debug output from this build it will tell me more info and how much data is being sent to the camera as well as what the reply from the camera looks like. I can use that to make some adjustments before the next build.

Thanks for doing the testing and if you could confirm that the binding does react to motion detection and updates the channel that would be great.

Sorry i cant do that.
When i have it enabled it breaks my Openhab Z-Wave and other bindings stop working.
I will se if i can put up a test system.

/Mike

I do have the same issue, openhab is slowing down very much and I can’t disable modbus since it’s essential to me.

Hey @Matt3 , but I have 5 Foscam cameras. Openhab is getting not useable and I’m guessing this is because of the binding:-(