Logic Machine Forum
API Control camera Vivotek - Printable Version

+- Logic Machine 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: API Control camera Vivotek (/showthread.php?tid=5638)



API Control camera Vivotek - luanpt - 29.09.2024

Dear all,
I want to connect Vivotek camera API to LM5,

https://www.vivotek.com/fd9387-fr-v2

I attach the camera API guide, please help me

Thanks you.


RE: API Control camera Vivotek - admin - 30.09.2024

What exactly do you want to control via API?


RE: API Control camera Vivotek - luanpt - 01.10.2024

(30.09.2024, 07:43)admin Wrote: What exactly do you want to control via API?

Thanks for the feedback.

I want to have it detect me and send information via the API to trigger contexts that are specific to me. Or send a welcome home notification to the ceiling speaker.


RE: API Control camera Vivotek - admin - 02.10.2024

Run this script as resident. Change host variable to your camera IP and auth variable to access credentials in username:password format. Make sure that Basic auth mode (not digest) is set on your camera. Then post what you get in LM Logs when running this script.
Code:
local json = require('json')
local socket = require('socket')
local mime = require('mime')

local host = '192.168.1.1'
local auth = 'user:password'

local sock = socket.tcp()
sock:settimeout(15)

local res, err = sock:connect(host, 80)

if not res then
  sock:close()
  log('connect failed', err)
  os.sleep(5)
  return
end

local function init()
  local crlf = '\r\n'

  sock:send(
    'GET /FR/events/live/json?heartbeat=10 HTTP/1.1' .. crlf ..
    'Host: ' .. host .. crlf ..
    'Authorization: Basic ' .. mime.b64(auth) .. crlf .. crlf
  )
end

local function parse(resp)
  resp = json.pdecode(resp)
  log(resp)
end

init()
local pat, len = nil, nil

while true do
  res, err = sock:receive(pat)
  log('rx', pat, res, err)

  if err then
    sock:close()
    log('receive error', err)
    break
  end

  if type(pat) == 'number' then
    pcall(parse, res)
    pat, len = nil, nil
  elseif #res == 0 then
    pat = len
  elseif not len then
    len = res:match('Content%-Length: (%d+)')
    len = tonumber(len)
  end
end



RE: API Control camera Vivotek - luanpt - 05.10.2024

(02.10.2024, 11:52)admin Wrote: Run this script as resident. Change host variable to your camera IP and auth variable to access credentials in username:password format. Make sure that Basic auth mode (not digest) is set on your camera. Then post what you get in LM Logs when running this script.
Code:
local json = require('json')
local socket = require('socket')
local mime = require('mime')

local host = '192.168.1.1'
local auth = 'user:password'

local sock = socket.tcp()
sock:settimeout(15)

local res, err = sock:connect(host, 80)

if not res then
  sock:close()
  log('connect failed', err)
  os.sleep(5)
  return
end

local function init()
  local crlf = '\r\n'

  sock:send(
    'GET /FR/events/live/json?heartbeat=10 HTTP/1.1' .. crlf ..
    'Host: ' .. host .. crlf ..
    'Authorization: Basic ' .. mime.b64(auth) .. crlf .. crlf
  )
end

local function parse(resp)
  resp = json.pdecode(resp)
  log(resp)
end

init()
local pat, len = nil, nil

while true do
  res, err = sock:receive(pat)
  log('rx', pat, res, err)

  if err then
    sock:close()
    log('receive error', err)
    break
  end

  if type(pat) == 'number' then
    pcall(parse, res)
    pat, len = nil, nil
  elseif #res == 0 then
    pat = len
  elseif not len then
    len = res:match('Content%-Length: (%d+)')
    len = tonumber(len)
  end
end
Thanks for the reply.
I am sending the log again as attached file.
Please help me.


RE: API Control camera Vivotek - admin - 07.10.2024

You need to trigger a face detection event and check if it's logged. You can disable the log call on line 40 to filter out debug info:
Code:
-- log('rx', pat, res, err)



RE: API Control camera Vivotek - luanpt - 10.10.2024

(07.10.2024, 06:33)admin Wrote: You need to trigger a face detection event and check if it's logged. You can disable the log call on line 40 to filter out debug info:
Code:
-- log('rx', pat, res, err)

I send the information back, admin please check it for me
I see LM has connected and recognized
Please give me the command to control
Thank you very much


RE: API Control camera Vivotek - admin - 10.10.2024

Replace parse function in the script with the following code. The script will send ON to 1/1/1 each time the camera detects you.
Code:
local function parse(resp)
  resp = json.pdecode(resp)
 
  if resp.type == 'person' then
    if resp.name == 'luan thanh' then
      gpr.write('1/1/1', true)
    end
  end
end



RE: API Control camera Vivotek - luanpt - 12.10.2024

(10.10.2024, 13:04)admin Wrote: Replace parse function in the script with the following code. The script will send ON to 1/1/1 each time the camera detects you.
Code:
local function parse(resp)
  resp = json.pdecode(resp)
 
  if resp.type == 'person' then
    if resp.name == 'luan thanh' then
      gpr.write('1/1/1', true)
    end
  end
end
I checked, tested everything works perfectly.
Thank you very much