Guest User

Untitled

a guest
Mar 11th, 2018
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *  Foscam Universal Device V2
  3.  *
  4.     V1
  5.    
  6.     Copyright 2014 skp19
  7.    
  8.     Licensed under the Apache License, Version 2.0 (the "License");
  9.     you may not use this file except in compliance with the License.
  10.     You may obtain a copy of the License at
  11.    
  12.         http://www.apache.org/licenses/LICENSE-2.0
  13.    
  14.     Unless required by applicable law or agreed to in writing, software
  15.     distributed under the License is distributed on an "AS IS" BASIS,
  16.     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.     See the License for the specific language governing permissions and
  18.     limitations under the License.
  19.    
  20.     V2
  21.    
  22.     Copyright 2015 Jody Albritton
  23.    
  24.     Code is licensed for personal use only. Not for resale.
  25.    
  26.     V3
  27.  
  28.     Copyright 2015 Ashish Agrawal
  29.     Added code for retaining alarm settings
  30.    
  31.  *  
  32.  *
  33.  */
  34. metadata {
  35.     definition (name: "Foscam Universal Device V3", namespace: "ashishagrawal", author: "ashishagrawal") {
  36.         capability "Polling"
  37.         capability "Image Capture"
  38.        
  39.         attribute "alarmStatus", "string"
  40.         attribute "ledStatus",   "string"
  41.         attribute "hubactionMode", "string"
  42.    
  43.         command "alarmOn"
  44.         command "alarmOff"
  45.         command "toggleAlarm"
  46.         command "toggleLED"
  47.        
  48.         command "ledOn"
  49.         command "ledOff"
  50.         command "ledAuto"
  51.        
  52.         command "left"
  53.         command "right"
  54.         command "up"
  55.         command "down"
  56.        
  57.         command "cruisemap1"
  58.         command "cruisemap2"
  59.         command "stopCruise"
  60.        
  61.         command "preset1"
  62.         command "preset2"
  63.         command "preset3"
  64.     }
  65.    
  66.     preferences {
  67.         input("ip", "string", title:"Camera IP Address", description: "Camera IP Address", required: true, displayDuringSetup: true)
  68.         input("port", "string", title:"Camera Port", description: "Camera Port", defaultValue: 80 , required: true, displayDuringSetup: true)
  69.         input("username", "string", title:"Camera Username", description: "Camera Username", required: true, displayDuringSetup: true)
  70.         input("password", "password", title:"Camera Password", description: "Camera Password", required: true, displayDuringSetup: true)
  71.         input("hdcamera", "bool", title:"HD Foscam Camera? (9xxx Series)", description: "Type of Foscam Camera", required: true, displayDuringSetup: true)
  72.         input("mirror", "bool", title:"Mirror? (Not required for HD cameras)", description: "Camera Mirrored?")
  73.         input("flip", "bool", title:"Flip? (Not required for HD cameras)", description: "Camera Flipped?")
  74.         input("preset1", "text", title: "Preset 1 (For HD cameras only)", description: "Name of your first preset position")
  75.         input("preset2", "text", title: "Preset 2 (For HD cameras only)", description: "Name of your second preset position")
  76.         input("preset3", "text", title: "Preset 3 (For HD cameras only)", description: "Name of your third preset position")
  77.         input("cruisemap1", "text", title: "Cruise Map 1 (For HD cameras only. Non-HD cameras will default to Horizontal.)", description: "Name of your first cruise map", defaultValue: "Horizontal")
  78.         input("cruisemap2", "text", title: "Cruise Map 2 (For HD cameras only. Non-HD cameras will default to Vertical.)", description: "Name of your second cruise map", defaultValue: "Vertical")
  79.         input("snapInterval", "string", title: "Snap Interval for Alarm Motion Detection (For HD Cameras only)", description: "1 for 1s, 2 for 2s... 5 for 5s (max 5)", defaultValue: "5")
  80.         input("sensitivity", "string", title: "Sensitivity for Alarm Motion Detection (For HD Cameras only)", description: "-2:Lowest, -1:Lower, 0:Low, 1:Medium, 2:High", defaultValue: "1")
  81.         input("linkage", "string", title: "Action Setup for Alarm Motion Detection (For HD Cameras only)", description: "Camera Sound:1, Send Email:2, Take Snapashot:4, Recording:8 (Sum digits for multiple selections)", defaultValue: "15")
  82.         input("triggerInterval", "string", title: "Triggered Interval for Alarm Motion Detection (For HD Cameras only)", description: "0 for 5s, 1 for 6s... 10 for 15s (max 10)", defaultValue: "10")
  83.         input("schedule0", "string", title: "Alarm Schedule for Monday", description: "281474976710655 for entire day", defaultValue: "281474976710655")
  84.         input("schedule1", "string", title: "Alarm Schedule for Tuesday", description: "281474976710655 for entire day", defaultValue: "281474976710655")
  85.         input("schedule2", "string", title: "Alarm Schedule for Wednesday", description: "281474976710655 for entire day", defaultValue: "281474976710655")
  86.         input("schedule3", "string", title: "Alarm Schedule for Thursday", description: "281474976710655 for entire day", defaultValue: "281474976710655")
  87.         input("schedule4", "string", title: "Alarm Schedule for Friday", description: "281474976710655 for entire day", defaultValue: "281474976710655")
  88.         input("schedule5", "string", title: "Alarm Schedule for Saturday", description: "281474976710655 for entire day", defaultValue: "281474976710655")
  89.         input("schedule6", "string", title: "Alarm Schedule for Sunday", description: "281474976710655 for entire day", defaultValue: "281474976710655")
  90.         input("area0", "string", title: "Tiles for Detection Area", description: "Each tile in 2^tile number; use 1023 for all", defaultValue: "1023")
  91.         input("area1", "string", title: "Tiles for Detection Area", description: "Each tile in 2^tile number; use 1023 for all", defaultValue: "1023")
  92.         input("area2", "string", title: "Tiles for Detection Area", description: "Each tile in 2^tile number; use 1023 for all", defaultValue: "1023")
  93.         input("area3", "string", title: "Tiles for Detection Area", description: "Each tile in 2^tile number; use 1023 for all", defaultValue: "1023")
  94.         input("area4", "string", title: "Tiles for Detection Area", description: "Each tile in 2^tile number; use 1023 for all", defaultValue: "1023")
  95.         input("area5", "string", title: "Tiles for Detection Area", description: "Each tile in 2^tile number; use 1023 for all", defaultValue: "1023")
  96.         input("area6", "string", title: "Tiles for Detection Area", description: "Each tile in 2^tile number; use 1023 for all", defaultValue: "1023")
  97.         input("area7", "string", title: "Tiles for Detection Area", description: "Each tile in 2^tile number; use 1023 for all", defaultValue: "1023")
  98.         input("area8", "string", title: "Tiles for Detection Area", description: "Each tile in 2^tile number; use 1023 for all", defaultValue: "1023")
  99.         input("area9", "string", title: "Tiles for Detection Area", description: "Each tile in 2^tile number; use 1023 for all", defaultValue: "1023")
  100.  
  101. }
  102.  
  103.     tiles {
  104.        
  105.  
  106.         standardTile("camera", "device.alarmStatus", width: 1, height: 1, canChangeIcon: true, inactiveLabel: true, canChangeBackground: true) {
  107.           state "off", label: "off", action: "toggleAlarm", icon: "st.camera.dropcam-centered", backgroundColor: "#FFFFFF"
  108.           state "on", label: "on", action: "toggleAlarm", icon: "st.camera.dropcam-centered",  backgroundColor: "#53A7C0"
  109.         }
  110.  
  111.         standardTile("take", "device.image", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
  112.             state "take", label: "Take", action: "Image Capture.take", icon: "st.camera.camera", backgroundColor: "#FFFFFF", nextState:"taking"
  113.             state "taking", label:'Taking', action: "", icon: "st.camera.take-photo", backgroundColor: "#53a7c0"
  114.             state "image", label: "Take", action: "Image Capture.take", icon: "st.camera.camera", backgroundColor: "#FFFFFF", nextState:"taking"
  115.         }
  116.  
  117.         standardTile("alarmStatus", "device.alarmStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
  118.           state "off", label: "off", action: "toggleAlarm", icon: "st.quirky.spotter.quirky-spotter-sound-off", backgroundColor: "#FFFFFF"
  119.           state "on", label: "on", action: "toggleAlarm", icon: "st.quirky.spotter.quirky-spotter-sound-on",  backgroundColor: "#53A7C0"
  120.         }
  121.        
  122.         standardTile("ledStatus", "device.ledStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
  123.           state "auto", label: "auto", action: "toggleLED", icon: "st.Lighting.light13", backgroundColor: "#53A7C0"
  124.           state "off", label: "off", action: "toggleLED", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
  125.           state "on", label: "on", action: "toggleLED", icon: "st.Lighting.light11", backgroundColor: "#FFFF00"
  126.           state "manual", label: "manual", action: "toggleLED", icon: "st.Lighting.light13", backgroundColor: "#FFFF00"
  127.         }
  128.        
  129.         standardTile("ledAuto", "device.ledStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
  130.           state "auto", label: "auto", action: "ledAuto", icon: "st.Lighting.light11", backgroundColor: "#53A7C0"
  131.           state "off", label: "auto", action: "ledAuto", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
  132.           state "on", label: "auto", action: "ledAuto", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
  133.           state "manual", label: "auto", action: "ledAuto", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
  134.         }
  135.  
  136.         standardTile("ledOn", "device.ledStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
  137.           state "auto", label: "on", action: "ledOn", icon: "st.Lighting.light11", backgroundColor: "#FFFFFF"
  138.           state "off", label: "on", action: "ledOn", icon: "st.Lighting.light11", backgroundColor: "#FFFFFF"
  139.           state "on", label: "on", action: "ledOn", icon: "st.Lighting.light11", backgroundColor: "#FFFF00"
  140.           state "manual", label: "on", action: "ledOn", icon: "st.Lighting.light11", backgroundColor: "#00FF00"
  141.         }
  142.        
  143.         standardTile("ledOff", "device.ledStatus", width: 1, height: 1, canChangeIcon: false, inactiveLabel: true, canChangeBackground: false) {
  144.           state "auto", label: "off", action: "ledOff", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
  145.           state "off", label: "off", action: "ledOff", icon: "st.Lighting.light13", backgroundColor: "#53A7C0"
  146.           state "on", label: "off", action: "ledOff", icon: "st.Lighting.light13", backgroundColor: "#FFFFFF"
  147.           state "manual", label: "off", action: "ledOff", icon: "st.Lighting.light13", backgroundColor: "#00FF00"
  148.         }
  149.        
  150.         standardTile("preset1", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
  151.             state "preset1", label: "preset 1", action: "preset1", icon: ""
  152.         }
  153.  
  154.         standardTile("preset2", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
  155.             state "preset2", label: "preset 2", action: "preset2", icon: ""
  156.         }
  157.  
  158.         standardTile("preset3", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
  159.             state "preset3", label: "preset 3", action: "preset3", icon: ""
  160.         }
  161.        
  162.         standardTile("cruisemap1", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
  163.             state "cruisemap1", label: "Cruise Map 1", action: "cruisemap1", icon: ""
  164.         }
  165.  
  166.         standardTile("cruisemap2", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
  167.             state "cruisemap2", label: "Cruise Map 2", action: "cruisemap2", icon: ""
  168.         }
  169.  
  170.         standardTile("stopcruise", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
  171.             state "stopcruise", label: "Stop Cruise", action: "stopCruise", icon: ""
  172.         }
  173.  
  174.         standardTile("left", "device.image", width: 1, height: 1, canChangeIcon: false,  canChangeBackground: false, decoration: "flat") {
  175.             state "left", label: "left", action: "left", icon: ""
  176.         }
  177.  
  178.         standardTile("right", "device.image", width: 1, height: 1, canChangeIcon: false,  canChangeBackground: false, decoration: "flat") {
  179.             state "right", label: "right", action: "right", icon: ""
  180.         }
  181.  
  182.         standardTile("up", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
  183.             state "up", label: "up", action: "up", icon: "st.thermostat.thermostat-up"
  184.         }
  185.  
  186.         standardTile("down", "device.image", width: 1, height: 1, canChangeIcon: false, canChangeBackground: false, decoration: "flat") {
  187.             state "down", label: "down", action: "down", icon: "st.thermostat.thermostat-down"
  188.         }
  189.  
  190.         standardTile("stop", "device.image", width: 1, height: 1, canChangeIcon: false,  canChangeBackground: false, decoration: "flat") {
  191.             state "stop", label: "", action: "stopCruise", icon: "st.sonos.stop-btn"
  192.         }
  193.  
  194.  
  195.         standardTile("refresh", "device.alarmStatus", inactiveLabel: false, decoration: "flat") {
  196.           state "refresh", action:"polling.poll", icon:"st.secondary.refresh"
  197.         }
  198.        
  199.         standardTile("blank", "device.image", width: 1, height: 1, canChangeIcon: false,  canChangeBackground: false, decoration: "flat") {
  200.           state "blank", label: "", action: "", icon: "", backgroundColor: "#FFFFFF"
  201.         }
  202.        
  203.         carouselTile("cameraDetails", "device.image", width: 3, height: 2) { }
  204.         main "camera"
  205.             details(["cameraDetails", "take", "refresh", "alarmStatus", "ledAuto", "ledOn", "ledOff"]) //**Uncomment this line and comment out the next line to hide the PTZ controls
  206.             //details([ "take", "blank", "alarmStatus", "ledAuto", "ledOn", "ledOff", "preset1", "preset2", "preset3", "cruisemap1", "cruisemap2", "stopcruise", "blank", "up", "blank", "left", "stop", "right", "blank", "down", "blank", "refresh", "cameraDetails"])
  207.     }
  208. }
  209.  
  210. //TAKE PICTURE
  211. def take() {
  212.     //log.debug("1 Taking Photo")
  213.      sendEvent(name: "hubactionMode", value: "s3");
  214.     if(hdcamera == true) {
  215.             //log.debug "HD is true snapPicture2"
  216.             hubGet("cmd=snapPicture2")
  217.             //log.debug "Hubget is done "    
  218.     }
  219.     else {
  220.         hubGet("/snapshot.cgi?")
  221.     }
  222.    
  223. }
  224. //END TAKE PICTURE
  225.  
  226. //ALARM ACTIONS
  227. def toggleAlarm() {
  228.     log.debug "Toggling Alarm"
  229.     if(device.currentValue("alarmStatus") == "on") {
  230.         alarmOff()
  231.     }
  232.     else {
  233.         alarmOn()
  234.     }
  235. }
  236.  
  237. def alarmOn() {
  238.     log.debug "Enabling Alarm"
  239.     sendEvent(name: "alarmStatus", value: "on");
  240.     if(hdcamera == true) {
  241.         hubGet("cmd=setMotionDetectConfig&isEnable=1&sensitivity=${sensitivity}&snapInterval=${snapInterval}&linkage=${linkage}&triggerInterval=${triggerInterval}&schedule0=${schedule0}&schedule1=${schedule1}&schedule2=${schedule2}&schedule3=${schedule3}&schedule4=${schedule4}&schedule5=${schedule5}&schedule6=${schedule6}&area0=${area0}&area1=${area1}&area2=${area2}&area3=${area3}&area4=${area4}&area5=${area5}&area6=${area6}&area7=${area7}&area8=${area8}&area9=${area9}")
  242.     }
  243.     else {
  244.         hubGet("/set_alarm.cgi?motion_armed=1&")
  245.     }
  246. }
  247.  
  248. def alarmOff() {
  249.     log.debug "Disabling Alarm"
  250.     sendEvent(name: "alarmStatus", value: "off");
  251.     if(hdcamera == true) {
  252.         hubGet("cmd=setMotionDetectConfig&isEnable=0")
  253.     }
  254.     else {
  255.         hubGet("/set_alarm.cgi?motion_armed=0&")
  256.     }
  257. }
  258. //END ALARM ACTIONS
  259.  
  260. //LED ACTIONS
  261. //Toggle LED's
  262. def toggleLED() {
  263.   log.debug("Toggle LED")
  264.  
  265.   if(device.currentValue("ledStatus") == "auto") {
  266.     ledOn()
  267.   }
  268.  
  269.   else if(device.currentValue("ledStatus") == "on") {
  270.     ledOff()
  271.   }
  272.  
  273.   else {
  274.     ledAuto()
  275.   }
  276. }
  277.  
  278. def ledOn() {
  279.     log.debug("LED changed to: on")
  280.     sendEvent(name: "ledStatus", value: "on");
  281.     if(hdcamera == true) {
  282.         delayBetween([hubGet("cmd=setInfraLedConfig&mode=1"), hubGet("cmd=openInfraLed")])
  283.     }
  284.     else {
  285.         hubGet("/decoder_control.cgi?command=95&")
  286.     }
  287. }
  288.  
  289. def ledOff() {
  290.     log.debug("LED changed to: off")
  291.     sendEvent(name: "ledStatus", value: "off");
  292.     if(hdcamera == true) {
  293.         delayBetween([hubGet("cmd=setInfraLedConfig&mode=1"), hubGet("cmd=closeInfraLed")])
  294.     }
  295.     else {
  296.         hubGet("/decoder_control.cgi?command=94&")
  297.     }
  298. }
  299.  
  300. def ledAuto() {
  301.     log.debug("LED changed to: auto")
  302.     sendEvent(name: "ledStatus", value: "auto");
  303.     if(hdcamera == true) {
  304.         hubGet("cmd=setInfraLedConfig&mode=0")
  305.     }
  306.     else {
  307.         hubGet("/decoder_control.cgi?command=95&")
  308.     }
  309. }
  310. //END LED ACTIONS
  311.  
  312. //PRESET ACTIONS
  313. def preset1() {
  314.     log.debug("Preset 1 Selected - ${preset1}")
  315.     if(hdcamera == true) {
  316.         hubGet("cmd=ptzGotoPresetPoint&name=${preset1}")
  317.     }
  318.     else {
  319.         hubGet("/decoder_control.cgi?command=31&")
  320.     }
  321. }
  322.  
  323. def preset2() {
  324.     log.debug("Preset 2 Selected - ${preset2}")
  325.     if(hdcamera == true) {
  326.         hubGet("cmd=ptzGotoPresetPoint&name=${preset2}")
  327.     }
  328.     else {
  329.         hubGet("/decoder_control.cgi?command=33&")
  330.     }
  331. }
  332.  
  333. def preset3() {
  334.     log.debug("Preset 3 Selected - ${preset3}")
  335.     if(hdcamera == true) {
  336.         hubGet("cmd=ptzGotoPresetPoint&name=${preset3}")
  337.     }
  338.     else {
  339.         hubGet("/decoder_control.cgi?command=35&")
  340.     }
  341. }
  342. //END PRESET ACTIONS
  343.  
  344. //CRUISE ACTIONS
  345. def cruisemap1() {
  346.     log.debug("Cruise Map 1 Selected - ${cruisemap1}")
  347.     if(hdcamera == true) {
  348.         hubGet("cmd=ptzStartCruise&mapName=${cruisemap1}")
  349.     }
  350.     else {
  351.         hubGet("/decoder_control.cgi?command=28&")
  352.     }
  353. }
  354.  
  355. def cruisemap2() {
  356.     log.debug("Cruise Map 2 Selected - ${cruisemap2}")
  357.     if(hdcamera == true) {
  358.         hubGet("cmd=ptzStartCruise&mapName=${cruisemap2}")
  359.     }
  360.     else {
  361.         hubGet("/decoder_control.cgi?command=26&")
  362.     }
  363. }
  364.  
  365. def stopCruise() {
  366.     log.debug("Stop Cruise")
  367.     if(hdcamera == true) {
  368.         hubGet("cmd=ptzStopRun")
  369.     }
  370.     else {
  371.         delayBetween([hubGet("/decoder_control.cgi?command=29&"), hubGet("/decoder_control.cgi?command=27&")])
  372.     }
  373. }
  374. //END CRUISE ACTIONS
  375.  
  376. //PTZ CONTROLS
  377. def left() {
  378.     if(hdcamera == true) {
  379.         delayBetween([hubGet("cmd=ptzMoveLeft"), hubGet("cmd=ptzStopRun")])
  380.     }
  381.     else {
  382.         if(mirror == "true") {
  383.             hubGet("/decoder_control.cgi?command=4&onestep=1&")
  384.         }
  385.         else {
  386.             hubGet("/decoder_control.cgi?command=6&onestep=1&")
  387.         }
  388.     }
  389. }
  390.  
  391. def right() {
  392.     if(hdcamera == "true") {
  393.         delayBetween([hubGet("cmd=ptzMoveRight"), hubGet("cmd=ptzStopRun")])
  394.     }
  395.     else {
  396.         if(mirror == "true") {
  397.             hubGet("/decoder_control.cgi?command=6&onestep=1&")
  398.         }
  399.         else {
  400.             hubGet("/decoder_control.cgi?command=4&onestep=1&")
  401.         }
  402.     }
  403. }
  404.  
  405. def up() {
  406.     if(hdcamera == "true") {
  407.         delayBetween([hubGet("cmd=ptzMoveUp"), hubGet("cmd=ptzStopRun")])
  408.     }
  409.     else {
  410.         if(flip == "true") {
  411.             hubGet("/decoder_control.cgi?command=2&onestep=1&")
  412.         }
  413.         else {
  414.             hubGet("/decoder_control.cgi?command=0&onestep=1&")
  415.         }
  416.     }
  417. }
  418.  
  419. def down() {
  420.     if(hdcamera == "true") {
  421.         delayBetween([hubGet("cmd=ptzMoveDown"), hubGet("cmd=ptzStopRun")])
  422.     }
  423.     else {
  424.         if(flip == "true") {
  425.             hubGet("/decoder_control.cgi?command=0&onestep=1&")
  426.         }
  427.         else {
  428.             hubGet("/decoder_control.cgi?command=2&onestep=1&")
  429.         }
  430.     }
  431. }
  432. //END PTZ CONTROLS
  433.  
  434. def poll() {
  435.  
  436.     sendEvent(name: "hubactionMode", value: "local");
  437.     //Poll Motion Alarm Status and IR LED Mode
  438.     if(hdcamera == true) {
  439.         delayBetween([hubGet("cmd=getMotionDetectConfig"), hubGet("cmd=getInfraLedConfig")])
  440.     }
  441.     else {
  442.         hubGet("/get_params.cgi?")
  443.     }
  444. }
  445.  
  446. private getLogin() {
  447.     if(hdcamera == true) {
  448.         return "usr=${username}&pwd=${password}&"
  449.     }
  450.     else {
  451.         return "user=${username}&pwd=${password}"
  452.     }
  453. }
  454.  
  455. private hubGet(def apiCommand) {
  456.     //Setting Network Device Id
  457.     def iphex = convertIPtoHex(ip)
  458.     def porthex = convertPortToHex(port)
  459.     device.deviceNetworkId = "$iphex:$porthex"
  460.     //log.debug "Device Network Id set to ${iphex}:${porthex}"
  461.  
  462.     //log.debug("Executing hubaction on " + getHostAddress())
  463.     def uri = ""
  464.     if(hdcamera == true) {
  465.         uri = "/cgi-bin/CGIProxy.fcgi?" + getLogin() + apiCommand
  466.     }
  467.     else {
  468.         uri = apiCommand + getLogin()
  469.     }
  470.    // log.debug uri
  471.     def hubAction = new physicalgraph.device.HubAction(
  472.         method: "GET",
  473.         path: uri,
  474.         headers: [HOST:getHostAddress()]
  475.     )
  476.     if(device.currentValue("hubactionMode") == "s3") {
  477.         hubAction.options = [outputMsgToS3:true]
  478.         sendEvent(name: "hubactionMode", value: "local");
  479.     }
  480.     return hubAction
  481. }
  482.  
  483. def parse(String description) {
  484. log.debug "Parsing '${description}'"
  485.  
  486. def map = [:]
  487. def retResult = []
  488. def descMap = parseDescriptionAsMap(description)
  489.    
  490. //Image
  491. if (descMap.tempImageKey) {
  492.     try {
  493.     log.debug ("store Image")
  494.         storeTemporaryImage(descMap.tempImageKey, getPictureName())
  495.     } catch (Exception e) {
  496.         log.error e
  497.     }
  498. } else if (descMap.error) {
  499.     log.error "Error: ${descMap.error}"
  500. }
  501.  
  502. //Status Polling
  503. else if (descMap["headers"] && descMap["body"]) {
  504.     def body = new String(descMap["body"].decodeBase64())
  505.     if(hdcamera == true) {
  506.         def langs = new XmlSlurper().parseText(body)
  507.  
  508.         def motionAlarm = "$langs.isEnable"
  509.         def ledMode = "$langs.mode"
  510.  
  511.         //Get Motion Alarm Status
  512.         if(motionAlarm == "0") {
  513.             log.info("Polled: Alarm Off")
  514.             sendEvent(name: "alarmStatus", value: "off");
  515.         }
  516.         else if(motionAlarm == "1") {
  517.             log.info("Polled: Alarm On")
  518.             sendEvent(name: "alarmStatus", value: "on");
  519.         }
  520.  
  521.         //Get IR LED Mode
  522.         if(ledMode == "0") {
  523.             log.info("Polled: LED Mode Auto")
  524.             sendEvent(name: "ledStatus", value: "auto")
  525.         }
  526.         else if(ledMode == "1") {
  527.             log.info("Polled: LED Mode Manual")
  528.             sendEvent(name: "ledStatus", value: "manual")
  529.         }
  530.     }
  531.     else {
  532.         if(body.find("alarm_motion_armed=0")) {
  533.             log.info("Polled: Alarm Off")
  534.             sendEvent(name: "alarmStatus", value: "off")
  535.         }
  536.         else if(body.find("alarm_motion_armed=1")) {
  537.             log.info("Polled: Alarm On")
  538.             sendEvent(name: "alarmStatus", value: "on")
  539.         }
  540.         //The API does not provide a way to poll for LED status on 8xxx series at the moment
  541.     }
  542. }
  543. }
  544.  
  545. def parseDescriptionAsMap(description) {
  546.     description.split(",").inject([:]) { map, param ->
  547.         def nameAndValue = param.split(":")
  548.         map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
  549.     }
  550. }
  551.  
  552. private getPictureName() {
  553.   def pictureUuid = java.util.UUID.randomUUID().toString().replaceAll('-', '')
  554.   "image" + "_$pictureUuid" + ".jpg"
  555. }
  556.  
  557. private getHostAddress() {
  558.     return "${ip}:${port}"
  559. }
  560.  
  561. private String convertIPtoHex(ipAddress) {
  562.     String hex = ipAddress.tokenize( '.' ).collect {  String.format( '%02x', it.toInteger() ) }.join()
  563.     return hex
  564.  
  565. }
  566.  
  567. private String convertPortToHex(port) {
  568.     String hexport = port.toString().format( '%04x', port.toInteger() )
  569.     return hexport
  570. }
Add Comment
Please, Sign In to add comment