Commit #9: extension to the infrastructure with more devices + minor changes in extra...
[smartthings-infrastructure.git] / main.groovy
index e282ed898bdaa5ef71e779d2b0902564e7705ad1..6ba69420098bdd23777652ae8d0d70cbc0557fab 100644 (file)
@@ -20,6 +20,10 @@ import Location.LocationVar
 import Location.Phrase
 import appTouch.Touched
 import NfcTouch.NfcTouch
+import AeonKeyFob.AeonKeyFob
+import AeonKeyFob.AeonKeyFobs
+import MusicPlayer.MusicPlayer
+import MusicPlayer.MusicPlayers
 import Event.Event
 import Timer.SimulatedTimer
 
@@ -78,6 +82,10 @@ def eventHandler(LinkedHashMap eventDataMap) {
 @Field def presenceSensorObject = new PresenceSensors(sendEvent, 1)
 //Global Object for class thermostat!
 @Field def thermostatObject = new Thermostats(sendEvent, 1)
+//Global Object for class aeon key fob!
+@Field def aeonKeyFobObject = new AeonKeyFobs(sendEvent, 1)
+//Global Object for class music player!
+@Field def musicPlayerObject = new MusicPlayers(sendEvent, 1)
 
 //Application #1
 class App1 {
@@ -86,47 +94,39 @@ class App1 {
        def app
 
        //Extracted objects for App1
-       //Object for class Touch Sensor!
-       def tag
-       //Object for class switch!
-       def switch1
        //Object for class lock!
-       def lock
-       //Object for class door control!
-       def garageDoor
-       //Global variable for enum!
-       def masterSwitch = "40"
-       //Global variable for enum!
-       def masterLock = "20"
-       //Global variable for enum!
-       def masterDoor = "40"
+       def lock1
+       //Global variable for number!
+       def minutesLater = 1
+       //Object for class contactSensor!
+       def openSensor
 
        //Extracted objects for functions for App1
        //Global Object for functions in subscribe method!
-       def pageTwo = this.&pageTwo
-       //Global Object for functions in subscribe method!
        def installed = this.&installed
        //Global Object for functions in subscribe method!
        def updated = this.&updated
        //Global Object for functions in subscribe method!
        def initialize = this.&initialize
        //Global Object for functions in subscribe method!
-       def currentStatus = this.&currentStatus
+       def lockDoor = this.&lockDoor
        //Global Object for functions in subscribe method!
-       def touchHandler = this.&touchHandler
+       def doorOpen = this.&doorOpen
+       //Global Object for functions in subscribe method!
+       def doorClosed = this.&doorClosed
+       //Global Object for functions in subscribe method!
+       def doorHandler = this.&doorHandler
 
        App1(Object obj) {
                reference = obj
                location = obj.locationObject
                app = obj.appObject
-               tag = obj.touchSensorObject
-               switch1 = obj.switchObject
-               lock = obj.lockObject
-               garageDoor = obj.doorControlObject
+               lock1 = obj.lockObject
+               openSensor = obj.contactObject
+               //Global variable for settings!
+               settings = [app:app, lock1:lock1, minutesLater:minutesLater, openSensor:openSensor]
        }
        //Global variables for each app
-       //Settings variable defined to settings on purpose
-       def settings = "Settings"
        //Global variable for state[mode]
        def state = [home:[],away:[],night:[]]
        //Create a global logger object for methods
@@ -141,6 +141,8 @@ class App1 {
        def timersFuncList = []
        //Create a global list for timer schedulers
        def timersList = []
+       //Create a global variable for settings
+       def settings
 
        //Methods
        /////////////////////////////////////////////////////////////////////
@@ -274,106 +276,72 @@ class App1 {
                        def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction)
                }
        }
+       /////////////////////////////////////////////////////////////////////
+       def now() {
+               return System.currentTimeMillis()
+       }
 
-       def pageTwo() {
-               dynamicPage(name: "pageTwo") {
-               section("If set, the state of these devices will be toggled each time the tag is touched, " + 
-                       "e.g. a light that's on will be turned off and one that's off will be turned on, " +
-                       "other devices of the same type will be set to the same state as their master device. " +
-                       "If no master is designated then the majority of devices of the same type will be used " +
-                       "to determine whether to turn on or off the devices.") {
-                   
-                   if (switch1 || masterSwitch) {
-                       input "masterSwitch", "enum", title: "Master switch", options: switch1.collect{[(it.id): it.displayName]}, required: false
-                   }
-                   if (lock || masterLock) {
-                       input "masterLock", "enum", title: "Master lock", options: lock.collect{[(it.id): it.displayName]}, required: false
-                   }
-                   if (garageDoor || masterDoor) {
-                       input "masterDoor", "enum", title: "Master door", options: garageDoor.collect{[(it.id): it.displayName]}, required: false
-                   }            
-                       }
-                       section([mobileOnly:true]) {
-                               label title: "Assign a name", required: false
-                               mode title: "Set for specific mode(s)", required: false
-                       }        
-           }
+       def installed()
+       {
+           log.debug "Auto Lock Door installed. (URL: http://www.github.com/smartthings-users/smartapp.auto-lock-door)"
+           initialize()
        }
        
-       def installed() {
-               log.debug "Installed with settings: ${settings}"
-       
-               initialize()
+       def updated()
+       {
+           unsubscribe()
+           unschedule()
+           log.debug "Auto Lock Door updated."
+           initialize()
        }
        
-       def updated() {
-               log.debug "Updated with settings: ${settings}"
+       def initialize()
+       {
+           log.debug "Settings: ${settings}"
+           subscribe(lock1, "lock", doorHandler)
+           subscribe(openSensor, "contact.closed", doorClosed)
+           subscribe(openSensor, "contact.open", doorOpen)
+       }
        
-               unsubscribe()
-               initialize()
+       def lockDoor()
+       {
+           log.debug "Locking Door if Closed"
+           if((openSensor.latestValue("contact") == "closed")){
+               log.debug "Door Closed"
+               lock1.lock()
+           } else {
+               if ((openSensor.latestValue("contact") == "open")) {
+               def delay = minutesLater * 60
+               log.debug "Door open will try again in $minutesLater minutes"
+               runIn( delay, lockDoor )
+               }
+           }
        }
        
-       def initialize() {
-               subscribe tag, "nfcTouch", touchHandler
-           subscribe app, touchHandler
+       def doorOpen(evt) {
+           log.debug "Door open reset previous lock task..."
+           unschedule( lockDoor )
+           def delay = minutesLater * 60
+           runIn( delay, lockDoor )
        }
        
-       private currentStatus(devices, master, attribute) {
-               log.trace "currentStatus($devices, $master, $attribute)"
-               def result = null
-               if (master) {
-               result = devices.find{it.id == master}?.currentValue(attribute)
-           }
-           else {
-               def map = [:]
-               devices.each {
-                       def value = it.currentValue(attribute)
-                   map[value] = (map[value] ?: 0) + 1
-                   log.trace "$it.displayName: $value"
-               }
-               log.trace map
-               result = map.collect{it}.sort{it.value}[-1].key
-           }
-           log.debug "$attribute = $result"
-           result
+       def doorClosed(evt) {
+           log.debug "Door Closed"
        }
        
-       def touchHandler(evt) {
-               log.trace "touchHandler($evt.descriptionText)"
-           if (switch1) {
-               def status = currentStatus(switch1, masterSwitch, "switch")
-               switch1.each {
-                   if (status == "on") {
-                       it.off()
-                   }
-                   else {
-                       it.on()
-                   }
-               }
-           }
-           
-           if (lock) {
-               def status = currentStatus(lock, masterLock, "lock")
-               lock.each {
-                   if (status == "locked") {
-                       lock.unlock()
-                   }
-                   else {
-                       lock.lock()
-                   }
-               }
+       def doorHandler(evt)
+       {
+           log.debug "Door ${openSensor.latestValue}"
+           log.debug "Lock ${evt.name} is ${evt.value}."
+       
+           if (evt.value == "locked") {                  // If the human locks the door then...
+               log.debug "Cancelling previous lock task..."
+               unschedule( lockDoor )                  // ...we don't need to lock it later.
            }
-           
-           if (garageDoor) {
-               def status = currentStatus(garageDoor, masterDoor, "status")
-               garageDoor.each {
-                       if (status == "open") {
-                       it.close()
-                   }
-                   else {
-                       it.open()
-                   }
-               }
+           else {                                      // If the door is unlocked then...
+               def delay = minutesLater * 60          // runIn uses seconds
+               log.debug "Re-arming lock in ${minutesLater} minutes (${delay}s)."
+               runIn( delay, lockDoor )                // ...schedule to lock in x minutes.
            }
        }
 }
@@ -386,36 +354,47 @@ class App2 {
        def app
 
        //Extracted objects for App2
+       //Object for class Touch Sensor!
+       def tag
        //Object for class switch!
-       def switchesoff
-       //Object for class switch!
-       def switcheson
+       def switch1
        //Object for class lock!
-       def lock1
-       //Global variable for mode!
-       def newMode = "home"
-       //Global variable for number!
-       def waitfor = 10
+       def lock
+       //Object for class door control!
+       def garageDoor
+       //Global variable for enum!
+       def masterSwitch = "switchID0"
+       //Global variable for enum!
+       def masterLock = "lockID0"
+       //Global variable for enum!
+       def masterDoor = "DoorControlID0"
 
        //Extracted objects for functions for App2
        //Global Object for functions in subscribe method!
+       def pageTwo = this.&pageTwo
+       //Global Object for functions in subscribe method!
        def installed = this.&installed
        //Global Object for functions in subscribe method!
        def updated = this.&updated
        //Global Object for functions in subscribe method!
-       def appTouch = this.&appTouch
+       def initialize = this.&initialize
+       //Global Object for functions in subscribe method!
+       def currentStatus = this.&currentStatus
+       //Global Object for functions in subscribe method!
+       def touchHandler = this.&touchHandler
 
        App2(Object obj) {
                reference = obj
                location = obj.locationObject
                app = obj.appObject
-               switchesoff = obj.switchObject
-               switcheson = obj.switchObject
-               lock1 = obj.lockObject
+               tag = obj.touchSensorObject
+               switch1 = obj.switchObject
+               lock = obj.lockObject
+               garageDoor = obj.doorControlObject
+               //Global variable for settings!
+               settings = [app:app, tag:tag, switch1:switch1, lock:lock, garageDoor:garageDoor, masterSwitch:masterSwitch, masterLock:masterLock, masterDoor:masterDoor]
        }
        //Global variables for each app
-       //Settings variable defined to settings on purpose
-       def settings = "Settings"
        //Global variable for state[mode]
        def state = [home:[],away:[],night:[]]
        //Create a global logger object for methods
@@ -430,6 +409,8 @@ class App2 {
        def timersFuncList = []
        //Create a global list for timer schedulers
        def timersList = []
+       //Create a global variable for settings
+       def settings
 
        //Methods
        /////////////////////////////////////////////////////////////////////
@@ -559,35 +540,106 @@ class App2 {
                }
        }
 
-       def installed()
-       {
-               log.debug "Installed with settings: ${settings}"
-               log.debug "Current mode = ${location.mode}"
-               subscribe(app, appTouch)
+       def pageTwo() {
+               dynamicPage(name: "pageTwo") {
+               section("If set, the state of these devices will be toggled each time the tag is touched, " +
+                       "e.g. a light that's on will be turned off and one that's off will be turned on, " +
+                       "other devices of the same type will be set to the same state as their master device. " +
+                       "If no master is designated then the majority of devices of the same type will be used " +
+                       "to determine whether to turn on or off the devices.") {
+       
+                   if (switch1 || masterSwitch) {
+                       input "masterSwitch", "enum", title: "Master switch", options: switch1.collect{[(it.id): it.displayName]}, required: false
+                   }
+                   if (lock || masterLock) {
+                       input "masterLock", "enum", title: "Master lock", options: lock.collect{[(it.id): it.displayName]}, required: false
+                   }
+                   if (garageDoor || masterDoor) {
+                       input "masterDoor", "enum", title: "Master door", options: garageDoor.collect{[(it.id): it.displayName]}, required: false
+                   }
+                       }
+                       section([mobileOnly:true]) {
+                               label title: "Assign a name", required: false
+                               mode title: "Set for specific mode(s)", required: false
+                       }
+           }
        }
        
+       def installed() {
+               log.debug "Installed with settings: ${settings}"
        
-       def updated()
-       {
+               initialize()
+       }
+       
+       def updated() {
                log.debug "Updated with settings: ${settings}"
-               log.debug "Current mode = ${location.mode}"
+       
                unsubscribe()
-               subscribe(app, appTouch)
-       }
-       
-       def appTouch(evt) {
-               log.debug "changeMode, location.mode = $location.mode, newMode = $newMode, location.modes = $location.modes"
-           if (location.mode != newMode) {
-                               setLocationMode(newMode)
-                               log.debug "Changed the mode to '${newMode}'"
-           }   else {
-               log.debug "New mode is the same as the old mode, leaving it be"
-               }
-           log.debug "appTouch: $evt"
-           lock1.lock()
-           switcheson.on()
-           def delay = (waitfor != null && waitfor != "") ? waitfor * 1000 : 120000
-               switchesoff.off(delay: delay)
+               initialize()
+       }
+       
+       def initialize() {
+               subscribe tag, "nfcTouch", touchHandler
+           subscribe app, touchHandler
+       }
+       
+       private currentStatus(devices, master, attribute) {
+               log.trace "currentStatus($devices, $master, $attribute)"
+               def result = null
+               if (master) {
+               result = devices.find{it.id == master}?.currentValue(attribute)
+           }
+           else {
+               def map = [:]
+               devices.each {
+                       def value = it.currentValue(attribute)
+                   map[value] = (map[value] ?: 0) + 1
+                   log.trace "$it.displayName: $value"
+               }
+               log.trace map
+               result = map.collect{it}.sort{it.value}[-1].key
+           }
+           log.debug "$attribute = $result"
+           result
+       }
+       
+       def touchHandler(evt) {
+               log.trace "touchHandler($evt.descriptionText)"
+           if (switch1) {
+               def status = currentStatus(switch1, masterSwitch, "switch")
+               switch1.each {
+                   if (status == "on") {
+                       it.off()
+                   }
+                   else {
+                       it.on()
+                   }
+               }
+           }
+       
+           if (lock) {
+               def status = currentStatus(lock, masterLock, "lock")
+               lock.each {
+                   if (status == "locked") {
+                       lock.unlock()
+                   }
+                   else {
+                       lock.lock()
+                   }
+               }
+           }
+       
+           if (garageDoor) {
+               def status = currentStatus(garageDoor, masterDoor, "status")
+               garageDoor.each {
+                       if (status == "open") {
+                       it.close()
+                   }
+                   else {
+                       it.open()
+                   }
+               }
+           }
        }
 }