This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

cURL to LUA to set Dahua Camera parameters
#1
Hello! I am trying to send a curl command to a Dahua camera from LM. I have successfully tested the direct cURL command and it works as expected and we avoided extra parameter for 'globoff'. But we have difficulties in translating that into LUA language. I am trying to achieve video enable/disable by simple on-off button in the visualization so thought of using if-else statement. Would something like this work?

Here is the cURL link 
Code:
curl --digest -u "admin:xxxxxx" "http://10.0.100.10/cgi-bin/configManager.cgi?action=setConfig&Encode%5b0%5d.MainFormat%5b0%5d.VideoEnable=true&Encode%5b0%5d.ExtraFormat%5b0%5d.VideoEnable=true"

Here is a code adapted from Hikvision relay thread. Not sure if I can use PUT method in our case.
Code:
url = 'http://admin:xxxxxx@10.0.100.10/cgi-bin/' local skthttp = require('socket.http') local skturl = require('socket.url') local ltn12 = require('ltn12') local md5sum = require('encdec').md5 local value = event.getvalue() local hash = function(...)   return md5sum(table.concat({...}, ':')) end local parse_header = function(header)   local result = {}   for key, value in (header .. ','):gmatch('(%w+)=(.-),') do     if value:sub(1, 1) == '"' then -- strip quotes       result[ key:lower() ] = value:sub(2, -2)     else       result[ key:lower() ] = value     end   end   return result end local make_digest_header = function(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 local _request = function(req)   if not req.url then     return nil, 'missing url'   end   local url = skturl.parse(req.url)   local user, password = url.user, url.password   if not user or not password then     return nil, 'missing credentials in url'   end   url.user, url.password, url.authority, url.userinfo = nil, nil, nil, nil   req.url = skturl.build(url)   local source   if req.source then     local chunks = {}     local capture = function(chunk)       if chunk then         chunks[ #chunks + 1 ] = chunk       end       return chunk     end     local chunk_id = 0     source = function()       chunk_id = chunk_id + 1       return chunks[ chunk_id ]     end     req.source = ltn12.source.chain(req.source, capture)   end   local body, code, hdrs = skthttp.request(req)   if code == 401 and hdrs['www-authenticate'] then     local ht = parse_header(hdrs['www-authenticate'])     if not ht.realm or not ht.nonce then       return nil, 'missing realm/nonce from response'     end     if ht.qop ~= 'auth' then       return nil, 'unsupported qop ' .. tostring(ht.qop)     end     if ht.algorithm and ht.algorithm:lower() ~= 'md5' then       return nil, 'unsupported algo ' .. tostring(ht.algorithm)     end     local nc = '00000001'     local cnonce = string.format('%08x', os.time())     local uri = skturl.build({ path = url.path, query = url.query })     local method = req.method or 'GET'     local response = hash(       hash(user, ht.realm, password),       ht.nonce,       nc,       cnonce,       'auth',       hash(method, uri)     )     req.headers = req.headers or {}     local auth = {       { 'username', user },       { 'realm', ht.realm },       { 'nonce', ht.nonce },       { 'uri', uri },       { 'cnonce', cnonce },       { 'nc', nc, unquote = true },       { 'qop', 'auth' },       { 'algorithm', 'MD5' },       { 'response', response },     }     if ht.opaque then       table.insert(auth, { 'opaque', ht.opaque })     end     req.headers.authorization = make_digest_header(auth)     if not req.headers.cookie and hdrs['set-cookie'] then       -- not really correct but enough for httpbin       local cookie = (hdrs['set-cookie'] .. ';'):match('(.-=.-)[;,]')       if cookie then         req.headers.cookie = '$Version: 0; ' .. cookie .. ';'       end     end     if req.source then       req.source = source     end     body, code, hdrs = skthttp.request(req)   end   return body, code, hdrs end local request = function(url)   local t = type(url)   if t == 'table' then     return _request(table.clone(url))   elseif t == 'string' then     local req = {}     local _, code, headers = _request({ url = url, sink = ltn12.sink.table(req) })     return table.concat(req), code, headers   end end if (value == true) then res, err, hdrs = request({   url = url,   method = 'PUT',   body = 'configManager.cgi?action=setConfig&Encode%5b0%5d.MainFormat%5b0%5d.VideoEnable=true&Encode%5b0%5d.ExtraFormat%5b0%5d.VideoEnable=true' }) else res, err, hdrs = request({   url = url,   method = 'PUT',   body = 'configManager.cgi?action=setConfig&Encode%5b0%5d.MainFormat%5b0%5d.VideoEnable=false&Encode%5b0%5d.ExtraFormat%5b0%5d.VideoEnable=false' }) end log(res, err, hdrs)
Reply
#2
Method should be set to GET. What do you get in Logs after sending the request?
Reply
#3
(10 hours ago)admin Wrote: Method should be set to GET. What do you get in Logs after sending the request?

With the method set to GET it would log:
Code:
* arg: 1   * string: Error Bad Request! * arg: 2   * number: 400 * arg: 3   * table:    ["content-length"]     * string: 21    ["content-type"]     * string: text/plain;charset=utf-8    ["connection"]     * string: close
Which is simply bad request. Can I force the full url from beginning and specify url_on and url_off arguments instead of adding the body inside the function?

UPDATED while testing GET:
- by specifying the full size url_on and url_off it seems to be working with an else-if statement.
Code:
url_on = 'http://admin:xxxxxx@10.0.100.10/cgi-bin/configManager.cgi?action=setConfig&Encode%5b0%5d.MainFormat%5b0%5d.VideoEnable=true&Encode%5b0%5d.ExtraFormat%5b0%5d.VideoEnable=true' --skipped the middle part if (value == true) then res, err, hdrs = request({   url = url_on, }) else res, err, hdrs = request({   url = url_off, }) end
Reply


Forum Jump: