I have a bunch of cams. I'm using Python to send the same command, via URL, to all the cams at the same time: turn on all IR lights, turn 'em all off, move all cams to preset position 1, preset 2, reboot all cams, etc. Which is why I am using parameters, to tell the script which commands to send out. And then I log what happened, including errors. I built a list of IP addresses (the cams' web server), and loop through the list, sending a URL to each cam. I don't process the result of the URL, so I don't need the response from the cam's server, so I don't need to wait for it. Is there a parameter I can add to the "requests.get" line to avoid the wait for the response ?
Is the script taking x amount of time to connect, and/or x amount of time waiting for a reply, costing me more than 10 seconds? There are eight or nine cams in play, so that many URL requests, depending on the command. Or is the logging taking too much time? I'm not logging each loop, just once after the loop is done, or once if there was an error.
I could build a standalone external script for each command, to eliminate the need for the parameters, but this is so clean using one script like this. I'd rather figure out how to send parameters to the py file, or just speed up what I've got.
Worst case, the script is working, the URLs find the cams and execute. The only real issue is the errors in the Indigo log. I no likely the red text!!!
Here's the py script in an attachment file::
- Code: Select all
try:
import indigo
except ImportError:
print "Attachments can only be used from within Indigo"
raise ImportError
import requests
from requests.auth import HTTPDigestAuth
def sendCommandToCams(mCommand,mOption):
if mCommand == "all_ir":
ipAddresses = ['xxx.xxx.xxx.xx1','xxx.xxx.xxx.xx2','xxx.xxx.xxx.xx3','xxx.xxx.xxx.xx4','xxx.xxx.xxx.xx5','xxx.xxx.xxx.xx6','xxx.xxx.xxx.xx7','xxx.xxx.xxx.xx8','xxx.xxx.xxx.xx9']
cgi = 'configManager.cgi?action=setConfig&Lighting[0][0].Mode='+mOption
elif mCommand == "all_preset":
ipAddresses = ['xxx.xxx.xxx.xx1','xxx.xxx.xxx.xx2','xxx.xxx.xxx.xx3','xxx.xxx.xxx.xx4','xxx.xxx.xxx.xx5','xxx.xxx.xxx.xx6','xxx.xxx.xxx.xx7','xxx.xxx.xxx.xx8']
cgi = 'ptz.cgi?action=start&channel=0&code=GotoPreset&arg1=0&arg2='+mOption+'&arg3=0'
elif mCommand == "all_reboot":
ipAddresses = ['xxx.xxx.xxx.xx1','xxx.xxx.xxx.xx2','xxx.xxx.xxx.xx3','xxx.xxx.xxx.xx4','xxx.xxx.xxx.xx5','xxx.xxx.xxx.xx6','xxx.xxx.xxx.xx7','xxx.xxx.xxx.xx8','xxx.xxx.xxx.xx9']
cgi = 'magicBox.cgi?action=reboot'
else:
indigo.server.log("indigo_cams error: sendCommandToCams method", type="Module", isError=True)
indigo.server.log("mCommand parameter not defined", type="Module", isError=True)
try:
for ipAddress in ipAddresses:
tURL = 'http://'+ipAddress+'/cgi-bin/'+cgi
requests.get(tURL, auth=HTTPDigestAuth('username', 'super_dooper_strong_password'))
indigo.server.log("indigo_cams.py executed: "+mCommand+", "+mOption, type="Module Execution")
except:
indigo.server.log("indigo_cams error: sendCommandToCams method", type="Module", isError=True)
indigo.server.log("something went wrong with the http request", type="Module", isError=True)
And within an Action Group I use:
- Code: Select all
import indigo_cams
indigo_cams.sendCommandToCams("all_ir","Off")
Side note: my error detection isn't very robust. If the script reaches that "else" statement, where I log that the parameter was not defined, I should abort the script at that point, before the loop starts,, but I don't know how to do that. Or I could eliminate all the logging and error detection altogether, it's not necessary, I just like having it there.