Using variable-triggered action lists and HTTP commands

Posted on
Thu Nov 01, 2018 5:22 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Using variable-triggered action lists and HTTP commands

I have a couple of scripts posted below. Both use lists of commands. Could I ask for some hints on converting them? It's really just how to set up the action list and then assemble a URL (I hope!)

Here is my digital loggers switch AppleScript:

Code: Select all
using terms from application "IndigoServer"
   tell application "IndigoServer"
      
      set actionOrdered to the value of variable "Digital_Loggers_Office"
      
      set actionListAll to {"1=ON", "1=OFF", "1=CCL", "2=ON", "2=OFF", "2=CCL", "3=ON", "3=OFF", "3=CCL", "4=ON", "4=OFF", "4=CCL", "5=ON", "5=OFF", "5=CCL", "6=ON", "6=OFF", "6=CCL", "7=ON", "7=OFF", "7=CCL", "8=ON", "8=OFF", "8=CCL"}
      
      if actionListAll contains actionOrdered is true then
         log actionOrdered & " the list works"
         
         set theIP to "http://username:password@10.0.1.XX:XXXX/outlet?" --change your ip and command string here
         
         set theURL to theIP & actionOrdered
         --log theURL --comment this out once it is working
         set curlURL to "curl -k" & " " & theURL
         
         set shellscript to curlURL
         --log shellscript -- commment this out once it is working
         do shell script shellscript
         set value of variable "Digital_Loggers_Office" to "x"
      end if
   end tell
end using terms from



This is for a FosCam camera:


Code: Select all
using terms from application "IndigoServer"
   tell application "IndigoServer"
      
      set actionOrdered to value of variable "Cam_Pool_Equipment" as string
      log actionOrdered
      
      
      --Cardinal Directions. Automatically stops the directional movement via delayed command at the bottom.
      set actionDirections to {"Left", "Right", "Up", "Down"}
      if actionDirections contains actionOrdered is true then
         set theAction to "ptzMove" & actionOrdered & ""
         --log theAction
         --set value of variable "Cam_Pool_Equipment" to "x"
      end if
      
      
      
      --zoom in, out. Automatically stops the zoom via delayed command at the bottom.
      set actionZoom to {"zoomIn", "zoomOut", "zoomStop"}
      if actionZoom contains actionOrdered is true then
         set theAction to actionOrdered & ""
         --log theAction
         --set value of variable "Cam_Pool_Equipment" to "x"
      end if
      
      
      --Go to Preset.
      set actionPreSet to {"TopMost", "RightMost", "LeftMost", "BottomMost", "Preset_1", "Preset_2", "Preset_3"} --I added WideView and Center. Modify the list to add your custom preset
      if actionPreSet contains actionOrdered is true then
         set theAction to "ptzGotoPresetPoint&name=" & actionOrdered & ""
         --log theAction
         --set value of variable "Cam_Pool_Equipment" to "x"
         log actionOrdered & "the list works"
         
      end if
      
      
      
      
      
      --Need to place all non-IR commands in this list so they will execute with proper timing.
      set actionListAll to {"Left", "Right", "Up", "Down", "zoomIn", "zoomOut", "zoomStop", "TopMost", "RightMost", "LeftMost", "BottomMost", "Preset_1", "Preset_2", "Preset_3"}
      if actionListAll contains actionOrdered is true then
         log actionOrdered & "the list works"
         
         set theIP to "'http://10.0.1.20:80/cgi-bin/CGIProxy.fcgi?" --change your ip and command string here. Note the ' at the beginning. Need one at the end.
         set thePW to the value of variable "Camera_Password"
         set theUser to the value of variable "Camera_User"
         --set theAction to the value of variable "Cam_Pool_Equipment"
         
         set theURL to theIP & "cmd=" & theAction & "&" & "usr=" & theUser & "&" & "pwd=" & thePW & "'" --rearrange the order of the username/password and command string if necessary for your camera.
         --log theURL --comment this out once it is working
         set curlURL to "curl -f" & " " & theURL
         
         set shellscript to curlURL
         --log shellscript -- commment this out once it is working
         do shell script shellscript
         set value of variable "Cam_Pool_Equipment" to "x"
      end if
      
      
      --http://10.0.1.20:80/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=[user]&pwd=[password]
      
      --This is a distinct URL send for IR LED open and close. Otherwise it does not work correctly when run as an external script.       
      set actionIRonOff to {"openInfraLed", "closeInfraLed"}
      if actionIRonOff contains actionOrdered is true then
         set theAction_IR_Only to actionOrdered
         --log theAction
         --delay 2
         --set value of variable "Cam_Pool_Equipment" to "x"
         
         
         set theIP to "'http://10.0.1.20:80/cgi-bin/CGIProxy.fcgi?" --change your ip and command string here
         set thePW to the value of variable "Camera_Password"
         set theUser to the value of variable "Camera_User"
         
         set theURL to theIP & "&cmd=" & theAction_IR_Only & "&" & "usr=" & theUser & "&" & "pwd=" & thePW & "'" --rearrange the order of the username/password and command string if necessary for your camera.
         --log theURL --comment this out once it is working
         set curlURL to "curl -f" & " " & theURL
         
         set shellscript to curlURL
         --log shellscript -- commment this out once it is working
         do shell script shellscript
         --delay 2
         set value of variable "Cam_Pool_Equipment" to "x"
      end if
      
      
      
      --Send "stop" URL function for directions and zoom
      delay 0.3
      if actionDirections contains actionOrdered is true then
         
         set theAction to "ptz" & "StopRun"
         set theIP to "'http://10.0.1.20:80/cgi-bin/CGIProxy.fcgi?" --change your ip and command string here
         set thePW to the value of variable "Camera_Password"
         set theUser to the value of variable "Camera_User"
         
         set theURL to theIP & "&cmd=" & theAction & "&" & "usr=" & theUser & "&" & "pwd=" & thePW & "'" --rearrange the order of the username/password and command string if necessary for your camera.
         --log theURL --comment this out once it is working
         set curlURL to "curl -f" & " " & theURL
         
         set shellscript to curlURL
         set theAction to "ptzMove" & "Stop"
         do shell script shellscript
         set value of variable "Cam_Pool_Equipment" to "x"
         
      end if
      
      
      (* --not used on non-zoom cameras
      if actionZoom contains actionOrdered is true then
         
         set theAction to "zoomStop" & "'"
         set theURL to theIP & "&cmd=" & theAction & "usr=" & theUser & "&" & "pwd=" & thePW --rearrange the order of the username/password and command string if necessary for your camera.
         --log theURL --comment this out once it is working
         set curlURL to "curl -f" & " " & theURL
         
         set shellscript to curlURL
         set theAction to "ptzMove" & "Stop" & "'"
         do shell script shellscript
         set value of variable "Cam_Pool_Equipment" to "x"
         
      end if
      
      --Send "stop" URL function after several seconds for longer direction duration and zoom
      
      *)
      
   end tell
end using terms from

Posted on
Fri Nov 02, 2018 8:47 am
jay (support) offline
Site Admin
User avatar
Posts: 18216
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Using variable-triggered action lists and HTTP commands

Here's the first script:

hamw wrote:
Code: Select all
using terms from application "IndigoServer"
   tell application "IndigoServer"
      
      set actionOrdered to the value of variable "Digital_Loggers_Office"
      
      set actionListAll to {"1=ON", "1=OFF", "1=CCL", "2=ON", "2=OFF", "2=CCL", "3=ON", "3=OFF", "3=CCL", "4=ON", "4=OFF", "4=CCL", "5=ON", "5=OFF", "5=CCL", "6=ON", "6=OFF", "6=CCL", "7=ON", "7=OFF", "7=CCL", "8=ON", "8=OFF", "8=CCL"}
      
      if actionListAll contains actionOrdered is true then
         log actionOrdered & " the list works"
         
         set theIP to "http://username:password@10.0.1.XX:XXXX/outlet?" --change your ip and command string here
         
         set theURL to theIP & actionOrdered
         --log theURL --comment this out once it is working
         set curlURL to "curl -k" & " " & theURL
         
         set shellscript to curlURL
         --log shellscript -- commment this out once it is working
         do shell script shellscript
         set value of variable "Digital_Loggers_Office" to "x"
      end if
   end tell
end using terms from


Here's the equivalent Python script:

Code: Select all
import requests

# In Python, it's standard practice to put constants in ALL_CAPS
# Also, in this script I'm going to use more standard Python
# style, including variable names with underscores, etc.
ACTION_LIST_ALL = [
    "1=ON", "1=OFF", "1=CCL",
    "2=ON", "2=OFF", "2=CCL",
    "3=ON", "3=OFF", "3=CCL",
    "4=ON", "4=OFF", "4=CCL",
    "5=ON", "5=OFF", "5=CCL",
    "6=ON", "6=OFF", "6=CCL",
    "7=ON", "7=OFF", "7=CCL",
    "8=ON", "8=OFF", "8=CCL"
]
DIGITAL_LOGGERS_OFFICE_ID = 12345  # Substitute the  ID of variable Digital_Loggers_Office

action_ordered = indigo.variables[DIGITAL_LOGGERS_OFFICE_ID].value
if action_ordered in ACTION_LIST_ALL:
    indigo.server.log("{} the list works".format(action_ordered))
    the_url = "http://username:password@10.0.1.XX:XXXX/outlet?{}".format(action_ordered)
    requests.get(the_url)
    indigo.variable.updateValue(DIGITAL_LOGGERS_OFFICE_ID, value="X")

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Nov 02, 2018 9:44 am
jay (support) offline
Site Admin
User avatar
Posts: 18216
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Using variable-triggered action lists and HTTP commands

Here's your second script. It's very similar to the first though it performs a couple of different URL calls:

Code: Select all
import requests

# In Python, it's standard practice to put constants in ALLCAPS
# Also, in this script I'm going to use more standard Python
# style, including variable names with underscores, etc.
ACTION_DIRECTIONS = ["Left", "Right", "Up", "Down"]
ACTION_ZOOM = ["zoomIn", "zoomOut", "zoomStop"]
ACTION_PRESET = ["TopMost", "RightMost", "LeftMost", "BottomMost", "Preset_1", "Preset_2", "Preset_3"]
ACTION_IR_ON_OFF = ["openInfraLed", "closeInfraLed"]

CAM_POOL_EQUIPMENT_ID = 12345  # Substitute the ID of variable Digital_Loggers_Office

CAMERA_USER = indigo.variables[98765].value # Substitute the ID of variable Camera_User
CAMERA_PASSWORD = indigo.variables[67890].value # Substitute the ID of variable Camera_Password

# Create the base URL that we'll add GET params to later
BASE_URL = "http://10.0.1.20:80/cgi-bin/CGIProxy.fcgi"

# Create the dictionary of GET parameters for the HTTP call - we'll add
# cmd later when we know what it is.
get_params = {
    "usr": CAMERA_USER,
    "pwd": CAMERA_PASSWORD
}

action_ordered = indigo.variables[CAM_POOL_EQUIPMENT_ID].value
indigo.server.log(action_ordered)

the_action = ""
if action_ordered in ACTION_DIRECTIONS:
    the_action += "ptzMove{}".format(action_ordered)
if action_ordered in ACTION_ZOOM:
    the_action += action_ordered
if action_ordered in ACTION_PRESET:
    the_action += "ptzGotoPresetPoint&name={}".format(action_ordered)

# At this point, you only need to see if there is an action to perform
if the_action:
    indigo.server.log("{}the list works".format(action_ordered))
    # Add the cmd parameter to the dict then issue the GET request
    get_params["cmd"] = the_action
    requests.get(BASE_URL, params=get_params)
    indigo.variables.updateValue(CAM_POOL_EQUIPMENT_ID, value="x")

if action_ordered in ACTION_IR_ON_OFF:
    # Add/update the cmd parameter to the dict then issue the GET request
    get_params["cmd"] = action_ordered
    requests.get(BASE_URL, params=get_params)
    indigo.variables.updateValue(CAM_POOL_EQUIPMENT_ID, value="x")

# I'm not sure why you need this, but here it is
indigo.script.sleep(.3)

if action_ordered in ACTION_DIRECTIONS:
    # Add/update the cmd parameter to the dict then issue the GET request
    get_params["cmd"] = "ptzStopRun"
    requests.get(BASE_URL, params=get_params)
    indigo.variables.updateValue(CAM_POOL_EQUIPMENT_ID, value="x")
   
    # FIXME: note, your script changes theAction to ptzMoveStop right before
    # it calls the shell script, but it never actually updates the URL so
    # I don't know if that's what you meant or not.


I didn't significantly change the logic, though this script could likely get (greatly?) simplified by revisiting the construction of the cmd arg. I'll leave that as an exercise for you...

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Nov 04, 2018 11:35 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Using variable-triggered action lists and HTTP commands

Jay,

Thanks! I'll work on these and see if I can come up with a better CMD argument.

Much appreciated!

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest