On my learning voyage, I have tried to solve the same problem in different ways and I thought, I share my result for those who are on a similar journey... ... from screen scraping to apple script to py ...
1. problem: the Dlink 930 is a very cheap camera, but does not adopt to changing brightness well. It has a web page where the sensitivity can be changed.
2. three solutions - all of them work.
(A) using safari and automating commands in applescript change the parameters. see code (A)
- pros if other tools not available ...
- cons, not stable, sensitive to changes, opens and closes safari, must be run in foreground and indigo waits until its finished, that creates timeout issues.
use as embedded script or as file
(B) applescript calling curl, launched as .app in background and curl is used with '&' to make it a background job. see code (B)
- pros: works well, easier to understand, good start for beginner.
- cons: have to understand the curl command .. used wireshark(in linux Vbox on MAC) fiddler(in vbox Windows7) .. to understand the communication
code should be in the ..../indigo 6 /scripts/attachment directory
In applescript editor do export and save as app, then do plugins/ reload attachments in indigo
(C) Phyton solution using urllib. see code (C)
- pros: goes with indigo philosophy... good translation project to go from applescript to PY
- cons:a little bit steeper learning curve
- launch py from within py and use & to send to background
(could also be call from applescript in embedded script with:
do shell script " '/Library/Application Support/Perceptive Automation/Indigo 6/Scripts/Attachments/setCameraLevels.py' 1 1 3 1 0 '192.168.1.71' >/dev/null 2>&1 & )
code should be in the ...../indigo 6 /scripts/attachment directory as referenced in the call statement.
comments welcome, especially for the PY section..
Karl
ps userids and passwords have been 'xxx'ed
- Code: Select all
(*
----- CODE (A) ----
set brightness level of Dlink 930 camera through safari session
get ip number and brightness level from Indigo.
keep userid and password in code, does not change between cameras.
Karl Wachs
v 1.0
10/1/13
*)
set theUserID to "xxx"
set thePassword to "xxx"
try
tell application "IndigoServer"
set ipNumber to (get value of variable "cameraBrightnessLevelPNumber")
set brightnessLevel to (get value of variable "cameraBrightnessLevel")
end tell
on error
return
end try
-- first login if needed
tell application "Safari"
activate
delay 0.5
make new document
set URL of document 1 to "http://" & ipNumber & "/image.htm"
end tell
set returnValue to SafariWindowIsLoading("Password")
if returnValue = "false" or returnValue = "true" then -- can we skip userid and password page?
delay 4
tell application "System Events"
tell process "Safari"
delay 0.1
keystroke theUserID
key code 48
keystroke thePassword
delay 0.1
keystroke return
delay 0.1
end tell
end tell
-- close window
tell application "Safari" to close every window
delay 0.1
end if
--now start without /after login:
if returnValue is not equal to "skip" then -- is safari already on right page? if yes skip
tell application "Safari"
activate
make new document
set URL of document 1 to "http://" & ipNumber & "/image.htm"
end tell
SafariWindowIsLoading("Brightness") -- wait for "brighness" on the page
delay 4
end if
tell application "System Events"
tell process "Safari"
-- tab to brightneslevel set field
key code 48
key code 48
key code 48
key code 48
key code 48
key code 48
-- set brightness to top level no matter what, move up 10 times
set ii to 0
repeat until ii = 10
delay 0.1
key code 126
set ii to ii + 1
end repeat
-- we are now at brightness +5, decrement to right value
set ii to 0
repeat until ii = 5 - brightnessLevel
delay 0.1
key code 125
set ii to ii + 1
end repeat
-- now save settings
delay 0.1
keystroke return
-- tabe to save button
key code 48
key code 48
key code 48
keystroke return -- hit save button
delay 3
end tell
end tell
tell application "Safari" to close every window
tell application "IndigoServer"
set value of variable "cameraBrightnessLevelRunning" to "false"
end tell
-----------------
on SafariWindowIsLoading(theText)
delay 0.2
repeat with i from 1 to 15
tell application "Safari"
if name of current tab of window 1 is not "Loading" then exit repeat
end tell
delay 1
end repeat
if i is 15 then return false
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
delay 0.5
end repeat
end tell
tell application "Safari" to set PageText to the text of document 1
if PageText contains "Brightness" then return "skip"
if PageText contains "theText" then return "true"
return "false"
end SafariWindowIsLoading
- Code: Select all
(*
----- CODE (B) ------
set level parameters in DLINK 930 camera
-- example call in indigo scheduled action in embedded applescript
setCameraLevels(0, 0, 0, 1, 0, "192.168.1.75")
setCameraLevels(0, 0, 0, 1, 0, "192.168.1.77")
setCameraLevels(0, 0, 0, 1, 0, "192.168.1.78")
Karl Wachs
v1
10/1/13
*)
on setCameraLevels(brightnessLevel525, contrastControl525, SaturationControl525, AntiFlickerEnable01, mirrorOnOff01, theIPNumber)
set theUserID to "xxx"
set thePassword to "xxxx"
-- map input(-5 ..+5) -5--> 1 , .. , +5 --> 128 levels on webpage
set mapping to {1, 14, 26, 39, 51, 64, 77, 90, 103, 115, 128}
if brightnessLevel525 > 5 then brightnessLevel525 = 5
if brightnessLevel525 < -5 then brightnessLevel525 = -5
set setbrightnessLevel to item (brightnessLevel525 + 6) of mapping
if contrastControl525 > 5 then contrastControl525 = 5
if contrastControl525 < -5 then contrastControl525 = -5
set setcontrastControl to item (contrastControl525 + 6) of mapping
if SaturationControl525 > 5 then SaturationControl525 = 5
if SaturationControl525 < -5 then SaturationControl525 = -5
set setSaturationControl to item (SaturationControl525 + 6) of mapping
if AntiFlickerEnable01 > 0 then set setAntiFlickerEnable to "1"
if AntiFlickerEnable01 < 1 then set setAntiFlickerEnable to "0"
if mirrorOnOff01 > 0 then set setMirror to "1"
if mirrorOnOff01 < 1 then set setMirror to "0"
-- compile cmd string
set cmd to " curl -u " & theUserID & ":" & thePassword -- userid and password
-- set cmd to cmd & " -m 20 " -- max wait 20 secs
-- data
set cmd to cmd & " -d \"" & "ReplySuccessPage=image.htm&ReplyErrorPage=errrimg.htm"
set cmd to cmd & "&BrightnessControl=" & setbrightnessLevel
set cmd to cmd & "&ContrastControl=" & setcontrastControl
set cmd to cmd & "&SaturationControl=" & setSaturationControl
set cmd to cmd & "&AntiFlickerEnable=" & setAntiFlickerEnable
set cmd to cmd & "&Mirror=" & setMirror
set cmd to cmd & "&ConfigSystemStream=Save\"" -- save it
-- end data
set cmd to cmd & " http://" & theIPNumber & "/setSystemStream" -- this is the page we are addressing
set cmd to cmd & " >/dev/null 2>&1 &" -- dump response, dont wait = backgrund job,
do shell script cmd
return
end setCameraLevels
- Code: Select all
#!/usr/bin/env python
## above needed to be executable without calling py
## ----- CODE (C) ------
import urllib2
import sys
## call eg: /path to this file/setCameraLevels.py 1 1 1 1 0 '192.168.1.71'
## --> how to call in indigo scheduled action: (without the ##)
## call executable python scipt from phyton, send output to nirwana (>..) + do not wait until is finished (&) otherwise indigo times-out
## this is day time setting, change 0 0 0 (bright. Contr. Satur.) to more sensitve numbers for night times
##os.system ("'/Library/Application Support/Perceptive Automation/Indigo 6/Scripts/Attachments/setCameraLevels.py' 0 0 0 1 0 '192.168.1.71' >/dev/null 2>&1 & ")
##os.system ("'/Library/Application Support/Perceptive Automation/Indigo 6/Scripts/Attachments/setCameraLevels.py' 0 0 0 1 0 '192.168.1.73' >/dev/null 2>&1 & ")
##os.system ("'/Library/Application Support/Perceptive Automation/Indigo 6/Scripts/Attachments/setCameraLevels.py' 0 0 0 1 0 '192.168.1.74' >/dev/null 2>&1 & ")
##os.system ("'/Library/Application Support/Perceptive Automation/Indigo 6/Scripts/Attachments/setCameraLevels.py' 0 0 0 1 0 '192.168.1.75' >/dev/null 2>&1 & ")
Brightness525= int(sys.argv[1])
Contrast525= int(sys.argv[2])
Saturation525= int(sys.argv[3])
Antiflicker01= int(sys.argv[4])
MirrorOnOff01= int(sys.argv[5])
theIPnumber = sys.argv[6]
theUserid="xxx"
thePassword="xxx"
## map input(-5 ..+5) -5--> 1 , .. , +5 --> 128 levels on webpage
map = ["1", "14", "26", "39", "51", "64", "77", "90", "103", "115", "128"]
## check input ranges
if Brightness525 >= 5: Brightness525 = 5
if Brightness525 <= -5: Brightness525 = -5
setBrightness = map[(Brightness525 +5)]
if Contrast525 >= 5: Contrast525 = 5
if Contrast525 <= -5: Contrast525 = -5
setContrast = map[(Contrast525 +5)]
if Saturation525 >= 5: Saturation525 = 5
if Saturation525 <= -5: Saturation525 = -5
setSaturation = map[(Saturation525 +5)]
if Antiflicker01 >= 1: setAntiflickerEnable = '1'
if Antiflicker01 <= 0: setAntiflickerEnable = '0'
if MirrorOnOff01 >= 1: setMirrorOnOff = '1'
if MirrorOnOff01 <= 0: setMirrorOnOff = '0'
## compile data string to be send to URL
theData = "&ReplySuccessPage=image.htm"
theData = theData + "&ReplyErrorPage=errrimg.htm"
theData = theData +"&BrightnessControl="+ setBrightness
theData = theData +"&ContrastControl="+setContrast
theData = theData +"&SaturationControl="+setSaturation
theData = theData +"&AntiFlickerEnable="+setAntiflickerEnable
theData = theData +"&Mirror="+setMirrorOnOff
theData = theData +"&ConfigSystemStream=Save"
theURL = 'http://'+theIPnumber+'/setSystemStream'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theURL, theUserid, thePassword)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
response = urllib2.urlopen(theURL,theData)
exit()