Python code to trigger BI camera

tai4de2

Young grasshopper
Joined
Aug 25, 2019
Messages
41
Reaction score
15
Location
Near Seattle
I'm posting this in case someone else might find it useful. It can be used to trigger a camera in BI5 from the command line. To use, you
  • Change "user" and "password" in the line starting with "s =" (note: do not change the colons, just the user and password parts) to match the credentials of one of your configured BI users.
  • Change the url. In my case I'm running this script on the same machine where BI is running, but this can work across your LAN also.
  • Supply the shortname of the camera on the command line, e.g. "python bi-trigger.py somecamera".
Python:
import hashlib
import json
import sys
import time
from urllib.parse import urlencode
from urllib.request import Request, urlopen

#print(time.monotonic())

url = 'http://127.0.0.1:1768/json'

def Send(request):
    jsonRequest = json.dumps(request).encode('ascii')
    request = Request(url, jsonRequest)
    response = urlopen(request).read().decode()
    jsonResponse = json.loads(response)
    return jsonResponse

request = { "cmd": "login" }
response = Send(request)
#print(time.monotonic())
#print(response)
session = response["session"]

s = "user:" + session + ":password"
hash = hashlib.md5(s.encode("utf-8")).hexdigest()
response = Send({ "cmd": "login", "session": session, "response": hash })
#print(time.monotonic())
#print(response)

request = { "cmd": "trigger", "session": session, "camera": sys.argv[1] }
response = Send(request)
#print(time.monotonic())
#print(response)
 
Top