LogicMachine Forum
HIKVision support - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: HIKVision support (/showthread.php?tid=1630)



HIKVision support - buuuudzik - 05.10.2018

My client has on his house HIKVision cameras and I am so impressed about its quality and also modern webserver and API. But it is fresh software so I must update my scripts to support such cameras. I found that I cannot download an image in same way as before in BCS cameras. I have right path:
http://username:password@ip_address:port/ISAPI/Streaming/channels/102/picture

and it's work perfectly on webbrowser but unfortunately not in script.

This is my script:

Code:
require('socket.http') local image1 = socket.http.request('http://username:password@ip_address:port/ISAPI/Streaming/channels/102/picture') log(#image1)


And it logs that length of image1 is nil.


I've found this specification:
http://down.dipol.com.pl/Cctv/-Hikvision-/isapi/HIKVISION%20ISAPI_2.5-Image%20Service.pdf

I've had similar differences when I've tried get image from old BCS camera and new HIK by Postman app and I've figured out that on HIK cameras autentification must be set to Digest Auth. How can I do it in Lua?


RE: HIKVision support - buuuudzik - 05.10.2018

Also I see that there is possibility to download uptodate list or have connection with camera and get notified when new event occured but it is not so easy to me. In node js all would be easy but how use this function in Lua?

http://www.ragingcomputer.com/2016/06/hikvision-motion-detection-in-openhab-using-node-js

Perfect would if admin could give some short start guide how get the data(without parsing) like in nodejs:
https://github.com/ragingcomputer/node-hikvision-api/blob/master/hikvision.js


RE: HIKVision support - admin - 06.10.2018

Try code for digest auth in this post: https://forum.logicmachine.net/showthread.php?tid=1061&pid=9364#pid9364


RE: HIKVision support - buuuudzik - 06.10.2018

(06.10.2018, 05:01)admin Wrote: Try code for digest auth in this post: https://forum.logicmachine.net/showthread.php?tid=1061&pid=9364#pid9364

It works in local with http. I see that the library is not a 4 line codeWink ThanksWink And what do you think admin about this alertStream?

I think this could be a very good option to integrate e.g. linedetection or facedetection from so sharp camera.


RE: HIKVision support - admin - 09.10.2018

Try this for alert stream:
Code:
socket = require('socket') mime = require('mime') host = '192.168.1.2' port = 80 auth = 'user:password' init = 'GET /ISAPI/Event/notification/alertStream HTTP/1.1\r\n' .. 'Host: ' .. host .. ':' .. port .. '\r\n' .. 'Authorization: Basic ' .. mime.b64(auth) .. '\r\n' .. 'Accept: multipart/x-mixed-replace\r\n\r\n' sock = socket.tcp() sock:settimeout(60) res, err = sock:connect(host, port) if res then   alert('connection ok')      sock:send(init)   line, err = sock:receive('*l')   log(line, err) else   alert('connection failed: ' .. tostring(err)) end



RE: HIKVision support - buuuudzik - 09.10.2018

Today I will check for sure. Very thanks and this is really nice featureWink


RE: HIKVision support - buuuudzik - 09.10.2018

I have the info that module socket.mime not found


RE: HIKVision support - admin - 09.10.2018

Replace it with require('mime')


RE: HIKVision support - buuuudzik - 09.10.2018

This script should be in resident 0s? Now I have it in resident 10s and updated version returns in Alerts "connection failed: nil"


RE: HIKVision support - admin - 09.10.2018

I've updated the script, try again. Use a resident script with sleep time more than zero for testing, otherwise you might get too many alerts/logs.


RE: HIKVision support - buuuudzik - 09.10.2018

Now is unauthorized message:

Code:
* arg: 1  * string: HTTP/1.1 401 Unauthorized * arg: 2  * nil


I'm sure I have right credentialsWink


RE: HIKVision support - admin - 09.10.2018

Replace *l with *a and post the whole output.


RE: HIKVision support - buuuudzik - 09.10.2018

Below whole result:

Code:
* arg: 1  * string: HTTP/1.1 401 Unauthorized Date: Tue, 09 Oct 2018 19:29:04 GMT Server: App-webs/ Content-Length: 178 Content-Type: text/html Connection: close WWW-Authenticate: Digest qop="auth", realm="IP Camera(13656)", nonce="4e3245794d444a684f6a56684e47526b5a575179", stale="FALSE" <!DOCTYPE html> <html><head><title>Document Error: Unauthorized</title></head> <body><h2>Access Error: 401 -- Unauthorized</h2> <p>Authentication Error</p> </body> </html> * arg: 2  * nil



RE: HIKVision support - admin - 10.10.2018

This means that you need to use digest auth here as well. Considering the code complexity, writing any examples without access to the real device is like shooting in the dark, sorry.


RE: HIKVision support - buuuudzik - 10.10.2018

(10.10.2018, 10:22)admin Wrote: This means that you need to use digest auth here as well. Considering the code complexity, writing any examples without access to the real device is like shooting in the dark, sorry.

Thanks for your efforts. I can give you an access to the real device via private message.


RE: HIKVision support - admin - 10.10.2018

Try this, change IP, port, user and pass as needed. If this works correctly, you should get xml payloads in Logs tab, otherwise errors will go to Alerts tab.

Code:
require('socket') md5sum = require('encdec').md5 host = '192.168.1.2' port = 80 user = 'username' pass = 'password' uri = '/ISAPI/Event/notification/alertStream' function getheader(resp, header)   local st, en   header = header .. '="'   st = resp:find(header)   if st then     en = resp:find('"', st + #header)     if en then       return resp:sub(st + #header, en - 1)     end   end end function makedigestheader(headers)   local digest = {}   for _, header in ipairs(headers) do     if not header.unquote then       header[ 2 ] = '"' .. header[ 2 ] .. '"'     end     digest[ #digest + 1 ] = header[ 1 ] .. '=' .. header[ 2 ]   end   return 'Digest ' .. table.concat(digest, ', ') end function hash(...)   return md5sum(table.concat({...}, ':')) end init = {   'GET ' .. uri .. ' HTTP/1.1',   'Host: ' .. host,   'Accept: multipart/x-mixed-replace', } sock = socket.tcp() sock:settimeout(60) res, err = sock:connect(host, port) if not res then   alert('init connection failed: ' .. tostring(err))   return end sock:send(table.concat(init, '\r\n') .. '\r\n\r\n') resp, err = sock:receive('*a') sock:close() if not resp then   alert('no response: ' .. tostring(err))   return end realm = getheader(resp, 'realm') nonce = getheader(resp, 'nonce') if not realm or not nonce then   alert('no realm/nonce')   return end nc = '00000001' cnonce = string.format('%08x', os.time()) method = 'GET' response = hash(   hash(user, realm, pass),   nonce,   nc,   cnonce,   'auth',   hash(method, uri) ) auth = {   { 'username', user },   { 'realm', realm },   { 'nonce', nonce },   { 'uri', uri },   { 'cnonce', cnonce },   { 'nc', nc, unquote = true },   { 'qop', 'auth' },   { 'algorithm', 'MD5' },   { 'response', response }, } table.insert(init, 2, 'Authorization: ' .. makedigestheader(auth)) sock = socket.tcp() sock:settimeout(60) res, err = sock:connect(host, port) if not res then   alert('data connection failed: ' .. tostring(err))   return end sock:send(table.concat(init, '\r\n') .. '\r\n\r\n') cl = 'Content-Length:' while true do   line, err = sock:receive('*l')   if line then     if line:find(cl, 1, true) then       len = tonumber(line:sub(#cl + 1))       if len then         sock:receive('*l') -- skip empty line         data, err = sock:receive(len)         if data then           log(data)         else           alert('data receive failed: ' .. tostring(err))         end       end     end   else     alert('line receive failed: ' .. tostring(err))     break   end end sock:close()



RE: HIKVision support - buuuudzik - 10.10.2018

It works I see such output:

Code:
* string: <EventNotificationAlert version="2.0" xmlns="http://www.hikvision.com/ver20/XMLSchema"> <ipAddress>192.168.101.201</ipAddress> <portNo>80</portNo> <protocol>HTTP</protocol> <macAddress>aa:bb:cc:dd:ee:ff</macAddress> <channelID>1</channelID> <dateTime>2018-10-10T14:31:43+01:00</dateTime> <activePostCount>0</activePostCount> <eventType>videoloss</eventType> <eventState>inactive</eventState> <eventDescription>videoloss alarm</eventDescription> <channelName>K03 Podjazd</channelName> </EventNotificationAlert>


I will find how to parse it. Thank you very much, you are very wiseWink


RE: HIKVision support - Alberto.ricof - 28.05.2020

Hello, 

I rescue this thread to see if anyone can help me,

How can I find the values in the XML data that the string returns? specifically what I need are the ones marked in yellow in the attached screenshot. These records are only sent if: <statisticalMethods> realTime </statisticalMethods>

Thank you very much in advance !!

Best regards


RE: HIKVision support - Erwin van der Zwart - 28.05.2020

Hi,

You need to parse the XML data to get values from it, you can use the build in LuaExpat lib for that.

See this post for a sample how to use the lpx parser: https://forum.logicmachine.net/showthread.php?tid=1836&pid=11472#pid11472

BR,

Erwin


RE: HIKVision support - Alberto.ricof - 29.05.2020

(28.05.2020, 21:16)Erwin van der Zwart Wrote: Hi,

You need to parse the XML data to get values from it, you can use the build in LuaExpat lib for that.

See this post for a sample how to use the lpx parser: https://forum.logicmachine.net/showthread.php?tid=1836&pid=11472#pid11472

BR,

Erwin

Thank you very much Erwin, it's just what i needed.

BR, 

Alberto R.