Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / main.groovy
index b08146d504111bc87618f29f4504da16d3595b75..54605116eadba871d6e24cf2164acc84236ad0e4 100644 (file)
@@ -3,17 +3,31 @@
 import groovy.transform.Field
 
 //Importing Classes
-import ContactSensor.Contacting
-import ContactSensor.Contacts
-import Lock.Locking
+import ContactSensor.ContactSensor
+import ContactSensor.ContactSensors
+import DoorControl.DoorControl
+import DoorControl.DoorControls
+import Lock.Lock
 import Lock.Locks
-import Switch.Switching
+import Thermostat.Thermostat
+import Thermostat.Thermostats
+import Switch.Switch
 import Switch.Switches
+import PresenceSensor.PresenceSensor
+import PresenceSensor.PresenceSensors
 import Logger.Logger
 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
+
+import gov.nasa.jpf.vm.Verify
 
 //Global eventHandler
 /////////////////////////////////////////////////////////////////////
@@ -27,87 +41,22 @@ def eventHandler(LinkedHashMap eventDataMap) {
        def isStateChange = eventDataMap["isStateChange"]
        def unit = eventDataMap["unit"]
        def data = eventDataMap["data"]
-       def minSize
-       def smallest
-
-       //make search efficient
-       if (app1.eventList.size() == app2.eventList.size()) {
-               minSize = app1.eventList.size()
-               smallest = "equal"
-       } else if (app1.eventList.size() < app2.eventList.size()) {
-               minSize = app1.eventList.size()
-               smallest = "app1"
-       } else {
-               minSize = app2.eventList.size()
-               smallest = "app2"
-       }
 
-       for (int i = 0;i < minSize;i++) {
-               if (app1.eventList[i] == name) {
-                       evt.add(new Event())
-                       evt[-1].value = value
-                       evt[-1].name = name
-                       evt[-1].deviceId = deviceId
-                       evt[-1].descriptionText = descriptionText
-                       evt[-1].displayed = displayed
-                       evt[-1].linkText = linkText
-                       evt[-1].displayName = linkText
-                       evt[-1].isStateChange = isStateChange
-                       evt[-1].unit = unit
-                       evt[-1].data = data
-                       app1.functionList[i](evt[-1])
-               }
+       for (int i = 0;i < app2.eventList.size();i++) {
                if (app2.eventList[i] == name) {
-                       evt.add(new Event())
-                       evt[-1].value = value
-                       evt[-1].name = name
-                       evt[-1].deviceId = deviceId
-                       evt[-1].descriptionText = descriptionText
-                       evt[-1].displayed = displayed
-                       evt[-1].linkText = linkText
-                       evt[-1].displayName = linkText
-                       evt[-1].isStateChange = isStateChange
-                       evt[-1].unit = unit
-                       evt[-1].data = data
-                       app2.functionList[i](evt[-1])
+                       def event = new Event(value, name, deviceId, descriptionText, displayed, linkText, linkText, isStateChange, unit, data)
+                       evt.add(event)
+                       app2.functionList[i](event)
                }
        }
 
-       if (smallest == "app1") {
-               for (int i = minSize;i < app2.eventList.size();i++) {
-                       if (app2.eventList[i] == name) {
-                               evt.add(new Event())
-                               evt[-1].value = value
-                               evt[-1].name = name
-                               evt[-1].deviceId = deviceId
-                               evt[-1].descriptionText = descriptionText
-                               evt[-1].displayed = displayed
-                               evt[-1].linkText = linkText
-                               evt[-1].displayName = linkText
-                               evt[-1].isStateChange = isStateChange
-                               evt[-1].unit = unit
-                               evt[-1].data = data
-                               app2.functionList[i](evt[-1])
-                       }
-               }
-       } else if (smallest == "app2") {
-               for (int i = minSize;i < app1.eventList.size();i++) {
-                       if (app1.eventList[i] == name) {
-                               evt.add(new Event())
-                               evt[-1].value = value
-                               evt[-1].name = name
-                               evt[-1].deviceId = deviceId
-                               evt[-1].descriptionText = descriptionText
-                               evt[-1].displayed = displayed
-                               evt[-1].linkText = linkText
-                               evt[-1].displayName = linkText
-                               evt[-1].isStateChange = isStateChange
-                               evt[-1].unit = unit
-                               evt[-1].data = data
-                               app1.functionList[i](evt[-1])
-                       }
+       for (int i = 0;i < app1.eventList.size();i++) {
+               if (app1.eventList[i] == name) {
+                       def event = new Event(value, name, deviceId, descriptionText, displayed, linkText, linkText, isStateChange, unit, data)
+                       evt.add(event)
+                       app1.functionList[i](event)
                }
-       }       
+       }
 }
 
 //GlobalVariables for both Apps
@@ -117,18 +66,28 @@ def eventHandler(LinkedHashMap eventDataMap) {
                        }
 //Object for location
 @Field def locationObject = new LocationVar()
-//Object for touch
+//Object for touch to call function
 @Field def appObject = new Touched(sendEvent, 0)
 //Create a global list for events
 @Field def evt = []
-
-//Extracted global objects for both Apps
+//Global Object for class Touch Sensor!
+@Field def touchSensorObject = new NfcTouch(sendEvent, 1)
+//Global Object for class switch!
+@Field def switchObject = new Switches(sendEvent, 1)
 //Global Object for class lock!
-@Field def lockObject = new Locking(sendEvent, 1)
-//Global Object for class contactSensor!
-@Field def contactObject = new Contacting(sendEvent, 1)
-//Global Object for class Switch!
-@Field def switchObject = new Switching(sendEvent, 1)
+@Field def lockObject = new Locks(sendEvent, 1)
+//Global Object for class door control!
+@Field def doorControlObject = new DoorControls(sendEvent, 1)
+//Global Object for class contact sensor!
+@Field def contactObject = new ContactSensors(sendEvent, 1)
+//Global Object for class presence sensor!
+@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 {
@@ -139,16 +98,12 @@ class App1 {
        //Extracted objects for App1
        //Object for class lock!
        def lock1
-       //Object for class contactSensor!
-       def contact
        //Global variable for number!
        def minutesLater = 1
-       //Global variable for number!
-       def secondsLater = 1
-       //Global variable for recipients!
-       def recipients = ['AJ']
-       //Global variable for phone number!
-       def phoneNumber = 9495379373
+       //Object for class contactSensor!
+       def openSensor
+
+       //Extracted objects for functions for App1
        //Global Object for functions in subscribe method!
        def installed = this.&installed
        //Global Object for functions in subscribe method!
@@ -158,7 +113,9 @@ class App1 {
        //Global Object for functions in subscribe method!
        def lockDoor = this.&lockDoor
        //Global Object for functions in subscribe method!
-       def unlockDoor = this.&unlockDoor
+       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
 
@@ -167,11 +124,11 @@ class App1 {
                location = obj.locationObject
                app = obj.appObject
                lock1 = obj.lockObject
-               contact = obj.contactObject
+               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
@@ -186,6 +143,8 @@ class App1 {
        def timersFuncList = []
        //Create a global list for timer schedulers
        def timersList = []
+       //Create a global variable for settings
+       def settings
 
        //Methods
        /////////////////////////////////////////////////////////////////////
@@ -196,9 +155,15 @@ class App1 {
        /////////////////////////////////////////////////////////////////////
        ////subscribe(obj, func)
        def subscribe(Object obj, Closure FunctionToCall) {
-               objectList.add(obj)
-               eventList.add("Touched")
-               functionList.add(FunctionToCall)
+               if (obj == app) {
+                       objectList.add(obj)
+                       eventList.add("Touched")
+                       functionList.add(FunctionToCall)
+               } else if (obj == location) {
+                       objectList.add(obj)
+                       eventList.add("Location")
+                       functionList.add(FunctionToCall)
+               }
        }
        ////subscribe(obj, event, func)
        def subscribe(Object obj, String event, Closure FunctionToCall) {
@@ -215,22 +180,36 @@ class App1 {
        /////////////////////////////////////////////////////////////////////
        ////runIn(time, func)
        def runIn(int seconds, Closure functionToCall) {
-               timersFuncList.add(functionToCall)
-               timersList.add(new Timer())
-               def task = timersList[-1].runAfter(1000*seconds, functionToCall)
+               if (timersFuncList.contains(functionToCall)) {
+                       timersList[timersFuncList.indexOf(functionToCall)].cancel()
+                       def task = timersList[timersFuncList.indexOf(functionToCall)].runAfter(1000*seconds, functionToCall)
+               } else {
+                       timersFuncList.add(functionToCall)
+                       timersList.add(new SimulatedTimer())
+                       def task = timersList[timersFuncList.indexOf(functionToCall)].runAfter(1000*seconds, functionToCall)
+               }
        }
        /////////////////////////////////////////////////////////////////////
        ////unschedule(func)
        def unschedule(Closure functionToUnschedule) {
                for (int i = 0;i < timersFuncList.size();i++) {
                        if (timersFuncList[i] == functionToUnschedule) {
-                               timersList[i].cancel()
+                               if (timersList != null)
+                                       timersList[i].cancel()
                        }
                }
        }
+       
+       
+       def unschedule() {
+               for (int i = 0;i < timersFuncList.size();i++) {
+                       if (timersList != null)
+                               timersList[i].cancel()
+               }
+       }
        /////////////////////////////////////////////////////////////////////
        ////sendNotificationToContacts(text, recipients)
-       def sendNotificationToContacts(String text, List recipients) {
+       def sendNotificationToContacts(String text, String recipients) {
                for (int i = 0;i < recipients.size();i++) {
                        for (int j = 0;j < location.contacts.size();j++) {
                                if (recipients[i] == location.contacts[j]) {
@@ -244,90 +223,127 @@ class App1 {
        def sendSms(long phoneNumber, String text) {
                println("Sending \""+text+"\" to "+phoneNumber.toString())
        }
+       /////////////////////////////////////////////////////////////////////
+       ////sendPush(text)
+       def sendPush(String text) {
+               println(text)
+       }
+       /////////////////////////////////////////////////////////////////////
+       ////schedule(time, nameOfFunction as String)
+       def schedule(String time, String nameOfFunction) {
+               def _inputTime = time.split(':')
+               Date date = new Date()  
+               def _currentTime = date.format("HH:mm:ss").split(':')
+       
+               //Convert input time and current time to minutes
+               def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60
+               def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2])
+               def delay
+       
+               if (inputTime < currentTime) {
+                       delay = 24*60*60-inputTime+currentTime
+               } else {
+                       delay = inputTime-currentTime
+               }
+       
+               timersFuncList.add(nameOfFunction)
+               timersList.add(new SimulatedTimer())
+               def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*1000) {
+                       "$nameOfFunction"()
+               }
+       }
+       ////schedule(time, nameOfFunction as Closure)
+       def schedule(String time, Closure nameOfFunction) {
+               def _inputTime = time.split(':')
+               Date date = new Date()  
+               def _currentTime = date.format("HH:mm:ss").split(':')
+       
+               //Convert input time and current time to minutes
+               def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60
+               def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2])
+               def delay
+       
+               if (inputTime < currentTime) {
+                       delay = 24*60*60-inputTime+currentTime
+               } else {
+                       delay = inputTime-currentTime
+               }
+       
+               if (timersFuncList.contains(nameOfFunction)) {
+                       timersList[timersFuncList.indexOf(nameOfFunction)].cancel()
+                       def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction)
+               } else {
+                       timersFuncList.add(nameOfFunction)
+                       timersList.add(new SimulatedTimer())
+                       def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction)
+               }
+       }
+       /////////////////////////////////////////////////////////////////////
+       def now() {
+               return System.currentTimeMillis()
+       }
 
-       def installed(){
+       def installed()
+       {
+           log.debug "Auto Lock Door installed. (URL: http://www.github.com/smartthings-users/smartapp.auto-lock-door)"
            initialize()
        }
        
-       def updated(){
+       def updated()
+       {
            unsubscribe()
            unschedule()
+           log.debug "Auto Lock Door updated."
            initialize()
        }
        
-       def initialize(){
+       def initialize()
+       {
            log.debug "Settings: ${settings}"
-           subscribe(lock1, "lock", doorHandler, [filterEvents: false])
-           subscribe(lock1, "unlock", doorHandler, [filterEvents: false])  
-           subscribe(contact, "contact.open", doorHandler)
-           subscribe(contact, "contact.closed", doorHandler)
-       }
-       
-       def lockDoor(){
-           log.debug "Locking the door."
-           lock1.lock()
-           if(location.contactBookEnabled) {
-               if ( recipients ) {
-                   log.debug ( "Sending Push Notification..." ) 
-                   sendNotificationToContacts( "${lock1} locked after ${contact} was closed for ${minutesLater} minutes!", recipients)
-               }
-           }
-           if (phoneNumber) {
-               log.debug("Sending text message...")
-               sendSms( phoneNumber, "${lock1} locked after ${contact} was closed for ${minutesLater} minutes!")
-           }
+           subscribe(lock1, "lock", doorHandler)
+           subscribe(openSensor, "contact.closed", doorClosed)
+           subscribe(openSensor, "contact.open", doorOpen)
        }
        
-       def unlockDoor(){
-           log.debug "Unlocking the door."
-           lock1.unlock()
-           if(location.contactBookEnabled) {
-               if ( recipients ) {
-                   log.debug ( "Sending Push Notification..." ) 
-                   sendNotificationToContacts( "${lock1} unlocked after ${contact} was opened for ${secondsLater} seconds!", recipients)
+       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 )
                }
            }
-           if ( phoneNumber ) {
-               log.debug("Sending text message...")
-               sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened for ${secondsLater} seconds!")
-           }
        }
        
-       def doorHandler(evt){
-           if ((contact.latestValue("contact") == "open") && (evt.value == "locked")) { // If the door is open and a person locks the door then...  
-               //def delay = (secondsLater) // runIn uses seconds
-               runIn( secondsLater, unlockDoor )   // ...schedule (in minutes) to unlock...  We don't want the door to be closed while the lock is engaged. 
-           }
-           else if ((contact.latestValue("contact") == "open") && (evt.value == "unlocked")) { // If the door is open and a person unlocks it then...
-               unschedule( unlockDoor ) // ...we don't need to unlock it later.
-           }
-           else if ((contact.latestValue("contact") == "closed") && (evt.value == "locked")) { // If the door is closed and a person manually locks it then...
-               unschedule( lockDoor ) // ...we don't need to lock it later.
-           }   
-           else if ((contact.latestValue("contact") == "closed") && (evt.value == "unlocked")) { // If the door is closed and a person unlocks it then...
-              //def delay = (minutesLater * 60) // runIn uses seconds
-               runIn( (minutesLater * 60), lockDoor ) // ...schedule (in minutes) to lock.
-           }
-           else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "open")) { // If a person opens an unlocked door...
-               unschedule( lockDoor ) // ...we don't need to lock it later.
-           }
-           else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "closed")) { // If a person closes an unlocked door...
-               //def delay = (minutesLater * 60) // runIn uses seconds
-               runIn( (minutesLater * 60), lockDoor ) // ...schedule (in minutes) to lock.
+       def doorOpen(evt) {
+           log.debug "Door open reset previous lock task..."
+           unschedule( lockDoor )
+           def delay = minutesLater * 60
+           runIn( delay, lockDoor )
+       }
+       
+       def doorClosed(evt) {
+           log.debug "Door Closed"
+       }
+       
+       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.
            }
-           else { //Opening or Closing door when locked (in case you have a handle lock)
-               log.debug "Unlocking the door."
-               lock1.unlock()
-               if(location.contactBookEnabled) {
-                   if ( recipients ) {
-                       log.debug ( "Sending Push Notification..." ) 
-                       sendNotificationToContacts( "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!", recipients)
-                   }
-               }
-               if ( phoneNumber ) {
-                   log.debug("Sending text message...")
-                   sendSms( phoneNumber, "${lock1} unlocked after ${contact} was opened or closed when ${lock1} was locked!")
-               }
+           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.
            }
        }
 }
@@ -340,34 +356,47 @@ class App2 {
        def app
 
        //Extracted objects for App2
-       //Object for class Switch!
-       def switchesoff
-       //Object for class Switch!
-       def switcheson
+       //Object for class Touch Sensor!
+       def tag
+       //Object for class switch!
+       def switch1
        //Object for class lock!
-       def lock1
-       //Global variable for mode!
-       def newMode = "away"
-       //Global variable for number!
-       def waitfor = 1
+       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
@@ -382,6 +411,8 @@ class App2 {
        def timersFuncList = []
        //Create a global list for timer schedulers
        def timersList = []
+       //Create a global variable for settings
+       def settings
 
        //Methods
        /////////////////////////////////////////////////////////////////////
@@ -392,9 +423,15 @@ class App2 {
        /////////////////////////////////////////////////////////////////////
        ////subscribe(obj, func)
        def subscribe(Object obj, Closure FunctionToCall) {
-               objectList.add(obj)
-               eventList.add("Touched")
-               functionList.add(FunctionToCall)
+               if (obj == app) {
+                       objectList.add(obj)
+                       eventList.add("Touched")
+                       functionList.add(FunctionToCall)
+               } else if (obj == location) {
+                       objectList.add(obj)
+                       eventList.add("Location")
+                       functionList.add(FunctionToCall)
+               }
        }
        ////subscribe(obj, event, func)
        def subscribe(Object obj, String event, Closure FunctionToCall) {
@@ -411,22 +448,36 @@ class App2 {
        /////////////////////////////////////////////////////////////////////
        ////runIn(time, func)
        def runIn(int seconds, Closure functionToCall) {
-               timersFuncList.add(functionToCall)
-               timersList.add(new Timer())
-               def task = timersList[-1].runAfter(1000*seconds, functionToCall)
+               if (timersFuncList.contains(functionToCall)) {
+                       timersList[timersFuncList.indexOf(functionToCall)].cancel()
+                       def task = timersList[timersFuncList.indexOf(functionToCall)].runAfter(1000*seconds, functionToCall)
+               } else {
+                       timersFuncList.add(functionToCall)
+                       timersList.add(new SimulatedTimer())
+                       def task = timersList[timersFuncList.indexOf(functionToCall)].runAfter(1000*seconds, functionToCall)
+               }
        }
        /////////////////////////////////////////////////////////////////////
        ////unschedule(func)
        def unschedule(Closure functionToUnschedule) {
                for (int i = 0;i < timersFuncList.size();i++) {
                        if (timersFuncList[i] == functionToUnschedule) {
-                               timersList[i].cancel()
+                               if (timersList != null)
+                                       timersList[i].cancel()
                        }
                }
        }
+       
+       
+       def unschedule() {
+               for (int i = 0;i < timersFuncList.size();i++) {
+                       if (timersList != null)
+                               timersList[i].cancel()
+               }
+       }
        /////////////////////////////////////////////////////////////////////
        ////sendNotificationToContacts(text, recipients)
-       def sendNotificationToContacts(String text, List recipients) {
+       def sendNotificationToContacts(String text, String recipients) {
                for (int i = 0;i < recipients.size();i++) {
                        for (int j = 0;j < location.contacts.size();j++) {
                                if (recipients[i] == location.contacts[j]) {
@@ -440,36 +491,157 @@ class App2 {
        def sendSms(long phoneNumber, String text) {
                println("Sending \""+text+"\" to "+phoneNumber.toString())
        }
+       /////////////////////////////////////////////////////////////////////
+       ////schedule(time, nameOfFunction as String)
+       def schedule(String time, String nameOfFunction) {
+               def _inputTime = time.split(':')
+               Date date = new Date()  
+               def _currentTime = date.format("HH:mm:ss").split(':')
+       
+               //Convert input time and current time to minutes
+               def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60
+               def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2])
+               def delay
+       
+               if (inputTime < currentTime) {
+                       delay = 24*60*60-inputTime+currentTime
+               } else {
+                       delay = inputTime-currentTime
+               }
+       
+               timersFuncList.add(nameOfFunction)
+               timersList.add(new SimulatedTimer())
+               def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*1000) {
+                       "$nameOfFunction"()
+               }
+       }
+       ////schedule(time, nameOfFunction as Closure)
+       def schedule(String time, Closure nameOfFunction) {
+               def _inputTime = time.split(':')
+               Date date = new Date()  
+               def _currentTime = date.format("HH:mm:ss").split(':')
+       
+               //Convert input time and current time to minutes
+               def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60
+               def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2])
+               def delay
+       
+               if (inputTime < currentTime) {
+                       delay = 24*60*60-inputTime+currentTime
+               } else {
+                       delay = inputTime-currentTime
+               }
+       
+               if (timersFuncList.contains(nameOfFunction)) {
+                       timersList[timersFuncList.indexOf(nameOfFunction)].cancel()
+                       def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction)
+               } else {
+                       timersFuncList.add(nameOfFunction)
+                       timersList.add(new SimulatedTimer())
+                       def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction)
+               }
+       }
 
-       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()
+                   }
+               }
+           }
        }
 }
 
@@ -477,6 +649,49 @@ class App2 {
 @Field def app2 = new App2(this)
 app1.installed()
 app2.installed()
-appObject.setValue([name: "Touched", value: "Touched", deviceId: 0, descriptionText: "",
-           displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
 
+def events = [1,2,3,4,5,6,7]
+def list = events.permutations()
+int count = Verify.getInt(0,list.size()-1)
+println "COUNT: " + count
+
+list[count].each {
+  switch(it) {
+    case 1:
+      appObject.setValue([name: "Touched", value: "Touched", deviceId: 0, descriptionText: "",
+                                          displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+      println "1"
+      break
+    case 2:
+      lockObject.setValue([name: "lock0", value: "locked", deviceId: 0, descriptionText: "",
+                                          displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+      println "   2"
+                       break
+    case 3:
+      lockObject.setValue([name: "lock0", value: "unlocked", deviceId: 0, descriptionText: "",
+                                          displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+      println "      3"
+      break
+    case 4:
+      contactObject.setValue([name: "contact0", value: "open", deviceId: 0, descriptionText: "",
+                                          displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+      println "         4"
+      break
+    case 5:
+      contactObject.setValue([name: "contact0", value: "closed", deviceId: 0, descriptionText: "",
+                                          displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+      println "            5"
+      break
+    case 6:
+      switchObject.setValue([name: "switch0", value: "on", deviceId: 0, descriptionText: "",
+                                          displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+      println "               6"
+      break
+    case 7:
+      switchObject.setValue([name: "switch0", value: "off", deviceId: 0, descriptionText: "",
+                                          displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
+      println "                   7"
+    default:
+      break
+  }
+}