Several AS; help needed. will buy BEER!!! #1 Foscam control

Posted on
Tue Oct 29, 2019 8:06 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Several AS; help needed. will buy BEER!!! #1 Foscam control

hoping to get some help with the following scripts. I use them every day but they are a little complicated for me to dig around.

First, this controls motion on a Foscam PTZ. . yes, there is a plugin but neither will control my cameras.

Code: Select all
using terms from application "IndigoServer"
   tell application "IndigoServer"
      
      set actionOrdered to value of variable "Cam_Garage" 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_Garage" 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_Garage" 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_Garage" 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.25: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_Garage"
         
         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_Garage" to "x"
      end if
      
      
      --http://10.0.1.25: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_Garage" to "x"
         
         
         set theIP to "'http://10.0.1.25: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_Garage" 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.25: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_Garage" to "x"
         
      end if
      

Posted on
Wed Oct 30, 2019 9:27 am
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Several AS; help needed. will buy BEER!!! #1 Foscam con

First, I don't know if the current Foscam plugin does what you need it to do, just wanted to point out that it exists.

Next, this should be pretty close, though given the script size and my inability to test, it might not be totally correct. I left in the AppleScript (commented out) so you can see how the conversion works. Some of it is line by line, other parts (notably the HTTP communication) I reworked because that is MUCH easier in Python that it was in AppleScript. If you take out all the commented-out AppleScript, it's actually pretty short (~60 lines with blanks for readability).

Code: Select all
# NOTE: Everywhere I have 'IDOF_', you must replace with the ID of the specified Indigo Object (variable, etc.)
# I've mostly done a line-by-line conversion to help you understand how that kind of conversion would work,
# but in a few cases where things are just much easier in Python I reworked a section (which I NOTE).

# First, import the requests module, which we'll use for the HTTP communication instead of using 'do shell script'
import requests
import time

# using terms from application "IndigoServer"
#    tell application "IndigoServer"
#       
#       set actionOrdered to value of variable "Cam_Garage" as string
action_ordered = indigo.variables[IDOF_Cam_Garage].value

#       log actionOrdered
indigo.server.log(action_ordered)

#       --Cardinal Directions. Automatically stops the directional movement via delayed command at the bottom.
#       set actionDirections to {}
ACTION_DIRECTIONS = ["Left", "Right", "Up", "Down"]  # It's customary in Python to name contstant variables IN_ALL_CAPS

#       if actionDirections contains actionOrdered is true then
#          set theAction to "ptzMove" & actionOrdered & ""
#          --log theAction
#          --set value of variable "Cam_Garage" to "x"
#       end if
if action_ordered in ACTION_DIRECTIONS:
    the_action = "ptzMove{}".format(action_ordered)
   
#       --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_Garage" to "x"
#       end if
if action_ordered in ["zoomIn", "zoomOut", "zoomStop"]:
    action_zoom = action_ordered
   
#       --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_Garage" to "x"
#          log actionOrdered & "the list works"
#         
#       end if
if action_ordered in ["TopMost", "RightMost", "LeftMost", "BottomMost", "Preset_1", "Preset_2", "Preset_3"]:
    the_action = "ptzGotoPresetPoint&name=".format(action_ordered)
    indigo.server.log("{} the list works".format(action_ordered))

#       --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.25: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_Garage"  #NOTE: I'm not sure if this really is supposed to be commented out or not, but it is in your script
#         
#          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_Garage" to "x"
#       end if
if action_ordered in ["Left", "Right", "Up", "Down", "zoomIn", "zoomOut", "zoomStop", "TopMost", "RightMost", "LeftMost", "BottomMost", "Preset_1", "Preset_2", "Preset_3"]:
    indigo.server.log("{} the list works".format(action_ordered))
    # NOTE: I did change this one since you had to jump through some hoops to make getting the URL work in AppleScript - it's much easier in Python
    the_URL = 'http://10.0.1.25:80/cgi-bin/CGIProxy.fcgi'
    the_args = {
        'cmd': the_action, # NOTE: again, it appears that the_action may not be defined at this time because you commented out the explicit set above
        'usr': indigo.variables[IDOF_Camera_User].value,
        'pwd': indigo.variables[IDOF_Camera_Password].value,
    }
    reply = requests.get(the_URL, params=the_args)
    # NOTE: you can look at the reply object to see if the request was successful
    indigo.variable.updateValue(IDOF_Cam_Garage, value="x")
   
#       --http://10.0.1.25: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_Garage" to "x"
#         
#         
#          set theIP to "'http://10.0.1.25: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_Garage" to "x"
#       end if
if action_ordered in ["openInfraLed", "closeInfraLed"]:
    indigo.server.log("{} the list works".format(action_ordered))
    # NOTE: I did change this one since you had to jump through some hoops to make getting the URL work in AppleScript - it's much easier in Python
    the_URL = 'http://10.0.1.25:80/cgi-bin/CGIProxy.fcgi'
    the_args = {
        'cmd': action_ordered,
        'usr': indigo.variables[IDOF_Camera_User].value,
        'pwd': indigo.variables[IDOF_Camera_Password].value,
    }
    reply = requests.get(the_URL, params=the_args)
    # NOTE: you can look at the reply object to see if the request was successful
    indigo.variable.updateValue(IDOF_Cam_Garage, value="x")

#       --Send "stop" URL function for directions and zoom
#       delay 0.3
time.sleep(3)

#       if actionDirections contains actionOrdered is true then
#         
#          set theAction to "ptz" & "StopRun"
#          set theIP to "'http://10.0.1.25: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
# NOTE: this next line doesnt' appear to do aything since you're already created the URL using 'ptzStopRun'
#          set theAction to "ptzMove" & "Stop"
#          do shell script shellscript
#          set value of variable "Cam_Garage" to "x"
#         
#       end if
#     
if action_ordered in ACTION_DIRECTIONS:
    # NOTE: I did change this one since you had to jump through some hoops to make getting the URL work in AppleScript - it's much easier in Python
    the_URL = 'http://10.0.1.25:80/cgi-bin/CGIProxy.fcgi'
    the_args = {
        'cmd': "ptzStopRun",
        'usr': indigo.variables[IDOF_Camera_User].value,
        'pwd': indigo.variables[IDOF_Camera_Password].value,
    }
    reply = requests.get(the_URL, params=the_args)
    # NOTE: you can look at the reply object to see if the request was successful
    indigo.variable.updateValue(IDOF_Cam_Garage, value="x")

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest