henry-spanka / node-red-contrib-hikvision

Hikvision API for Node Red
MIT License
10 stars 1 forks source link

succesfull working with hikvision DVR #32

Open ozett opened 4 years ago

ozett commented 4 years ago

GREAT! i tried and had success. the index-nr is the camera-channel on the hikvision-dvr. credentials are the hikvision-dvr credentials. i had to activate the notifications per channel in the dvr (not in the camera)

image

JulianCaruso commented 4 years ago

Hi @ozett ! Can you share how you make it work? I keep getting the following error when an alarm input is triggered in my DVR Hikvision 7208:

TypeError: Cannot read property '0' of undefined at /home/julian/.node-red/node_modules/node-hikvision-api/Hikvision.js:191:85 at Parser. (/home/julian/.node-red/node_modules/xml2js/lib/parser.js:304:18) at Parser.emit (events.js:210:5) at SAXParser.onclosetag (/home/julian/.node-red/node_modules/xml2js/lib/parser.js:262:26) at emit (/home/julian/.node-red/node_modules/sax/lib/sax.js:624:35) at emitNode (/home/julian/.node-red/node_modules/sax/lib/sax.js:629:5) at closeTag (/home/julian/.node-red/node_modules/sax/lib/sax.js:889:7) at SAXParser.write (/home/julian/.node-red/node_modules/sax/lib/sax.js:1436:13) at Parser.exports.Parser.Parser.parseString (/home/julian/.node-red/node_modules/xml2js/lib/parser.js:323:31) at Parser.parseString (/home/julian/.node-red/node_modules/xml2js/lib/parser.js:5:59) at Hikvision.handleData (/home/julian/.node-red/node_modules/node-hikvision-api/Hikvision.js:187:16) at Socket.emit (events.js:210:5) at addChunk (_stream_readable.js:308:12) at readableAddChunk (_stream_readable.js:289:11) at Socket.Readable.push (_stream_readable.js:223:10) at TCP.onStreamRead (internal/stream_base_commons.js:182:23)

Thank you!

ozett commented 4 years ago

check firmware (latest for me, i guess) image

check alarm settings per camera...must be a hikvision, i guess image

that should send the alarm-events to nodered debug.log

maybe another check with curl on command-line? see -> https://github.com/henry-spanka/node-red-contrib-hikvision/issues/10#issuecomment-645567127

DarwinsDen commented 3 years ago

Hi @ozett ! Can you share how you make it work? I keep getting the following error when an alarm input is triggered in my DVR Hikvision 7208:

TypeError: Cannot read property '0' of undefined at /home/julian/.node-red/node_modules/node-hikvision-api/Hikvision.js:191:85 at Parser. (/home/julian/.node-red/node_modules/xml2js/lib/parser.js:304:18) ...

I'm getting this same error as well when an alarm is triggered on my Hikvision NVR. This crashes my node red instance each time.

What I found was that the NVR changes the html tag from 'channelID' to 'dynChannelID' when an alarm is sent on one of the channels.

To get around this issue for now, I changed line 191 in the Hikvision.js file from this:

            const index = parseInt(result['EventNotificationAlert']['channelID'][0]);

to this:

    var index = 0;
    if ('channelID' in result['EventNotificationAlert']) {
              index = parseInt(result['EventNotificationAlert']['channelID'][0]);
    }
    if ('dynChannelID' in result['EventNotificationAlert']) {
              index = parseInt(result['EventNotificationAlert']['dynChannelID'][0]);
        }

There's cleaner ways of handling it, but this got me going again for now.

ozett commented 3 years ago

maybe the developer henry can update the source here?

henry-spanka commented 3 years ago

Added support for dynChannelId in https://github.com/henry-spanka/node-hikvision-api/commit/c346e89f81e31482676a21a1f80295581396ab62

DarwinsDen commented 3 years ago

Added support for dynChannelId in henry-spanka/node-hikvision-api@c346e89

That looks like it should handle the issue. Thank you!

JulianCaruso commented 3 years ago

I had forgotten about this. Your solution didn't fix my problem, but I did find mine, and it's similar. I was using the alarm input, not the motion detection from a camera. So the problem is that there is no channelID tag in that alarm, instead it is called inputIOPortID. So when wanting to parse and not find it, it failed

I modified the line from the above solution as follows, and was successful: const index = parseInt('channelID' in result['EventNotificationAlert'] ? result['EventNotificationAlert']['channelID'][0] : result['EventNotificationAlert']['inputIOPortID'][0]);

ozett commented 3 years ago

As the dev was active after a longer period may he put this also in for completion? or this is another start for some feature upgrades? 😄

DarwinsDen commented 3 years ago

@henry-spanka , I had another issue with the NVR where I was missing events. Most of the time this was not an issue since there are usually multiple chances to emit events with each alarm from the NVR, but the problem was much more noticeable for shorter alarms.

What I found was that the valid XML data sent to the Hikvision.js/handleData() method would usually be preceded by the following text:

--boundary Content-Type: application/xml; charset="UTF-8" Content-Length: 467

This would often cause parser.parseString on line 187 to fail with the error: "Error: Non-whitespace before first tag, Line: 0, Column: 1, Char: -", and the handleData() method would silently fail to process the message.

My workaround fix was to add the following code before the "parser.parseString(data, (err, result) => {" on line 187:

    let xmlStart = data.indexOf("<");
    if (xmlStart > 0) {
      data = data.toString().substring(xmlStart);
} 

With this change above, the parser no longer fails to process the alarms and I no longer appear to be missing messages. The odd part is, I would have thought the parser would always fail with this issue, but it is able to parse the message with the extra text about half the time.

Silverknight87 commented 3 years ago

I had forgotten about this. Your solution didn't fix my problem, but I did find mine, and it's similar. I was using the alarm input, not the motion detection from a camera. So the problem is that there is no channelID tag in that alarm, instead it is called inputIOPortID. So when wanting to parse and not find it, it failed

I modified the line from the above solution as follows, and was successful: const index = parseInt('channelID' in result['EventNotificationAlert'] ? result['EventNotificationAlert']['channelID'][0] : result['EventNotificationAlert']['inputIOPortID'][0]);

Hi JulianCaruso

can you guide where you made these changes. i cant find any relevant code in line 191 or anywhere else in the code. I have the same problem.

smcgann99 commented 3 years ago

@henry-spanka

Just trying this on my iDS-7208HUHI-K1 / 4S(B) DVR. If I trigger an alarm input wired directly to the back of the dvr, it doesnt report and causes this error. Seems related to problems above. Any ideas anyone ?

7 Oct 17:08:30 - [red] Uncaught Exception: 7 Oct 17:08:30 - TypeError: Cannot read property '0' of undefined at /home/pi/.node-red/node_modules/node-hikvision-api/Hikvision.js:191:189 at Parser. (/home/pi/.node-red/node_modules/node-hikvision-api/node_modules/xml2js/lib/parser.js:304:18) at Parser.emit (events.js:315:20) at SAXParser.onclosetag (/home/pi/.node-red/node_modules/node-hikvision-api/node_modules/xml2js/lib/parser.js:262:26) at emit (/home/pi/.node-red/node_modules/sax/lib/sax.js:624:35) at emitNode (/home/pi/.node-red/node_modules/sax/lib/sax.js:629:5) at closeTag (/home/pi/.node-red/node_modules/sax/lib/sax.js:889:7) at SAXParser.write (/home/pi/.node-red/node_modules/sax/lib/sax.js:1436:13) at Parser.exports.Parser.Parser.parseString (/home/pi/.node-red/node_modules/node-hikvision-api/node_modules/xml2js/lib/parser.js:323:31) at Parser.parseString (/home/pi/.node-red/node_modules/node-hikvision-api/node_modules/xml2js/lib/parser.js:5:59) nodered.service: Main process exited, code=exited, status=1/FAILURE nodered.service: Failed with result 'exit-code'.

henry-spanka commented 3 years ago

I guess we also need to check IOPortID. I am currently on vacation so if anyone likes to create a PR I would appreciate it. Otherwise I will fix it next week.