Commit #5
[smartthings-infrastructure.git] / main.groovy
index e406b84248a0ca0013e123ccc59bf4f78b510c4e..2e249bd4c0d6c64d10f650e9b3455795e653d410 100644 (file)
@@ -3,53 +3,56 @@
 import groovy.transform.Field
 
 //Importing Classes
-import ContactSensor.contacting
-import ContactSensor.contacts
-import Lock.locking
-import Lock.locks
-import Switch.switching
-import Switch.switches
-import Event.Event
+import ContactSensor.Contacting
+import ContactSensor.Contacts
+import Lock.Locking
+import Lock.Locks
+import Switch.Switching
+import Switch.Switches
 import Logger.Logger
-import Location.locationVar
+import Location.LocationVar
 import Location.Phrase
-import appTouch.Touch
+import appTouch.Touched
+import Event.Event
 
 //GlobalVariables
+//Create a global variable for send event
+@Field def sendEvent = {eventDataMap -> eventHandler(eventDataMap)}
 //create a location object to change the variable inside the class
-@Field def location = new locationVar()
+@Field def location = new LocationVar()
 //Settings variable defined to settings on purpose
 @Field def settings = "Settings"
 //Global variable for state[mode]
 @Field def state = [home:[],away:[],night:[]]
+//Global object for touch
+@Field def app = new Touched(sendEvent, 0)
 //Create a global logger object for methods
 @Field def log = new Logger()
-//Create a global object for app
-@Field def app = new Touch(1)
-//Create a global list for objects for events on subscribe methods
-@Field def ObjectList = []
-//Create a global list for events
-@Field def EventList = []
-//Create a global list for function calls based on corresponding events
-@Field def FunctionList = []
+//Create a global variable for Functions in Subscribe method
+@Field def functionList = []
+//Create a global variable for Objects in Subscribe method
+@Field def objectList = []
+//Create a global variable for Events in Subscribe method
+@Field def eventList = []
 //Create a global list for function schedulers
-@Field def ListofTimersFunc = []
+@Field def timersFuncList = []
 //Create a global list for timer schedulers
-@Field def ListofTimers = []
-
+@Field def timersList = []
+//Create a global list for events
+@Field def evt = []
 
 
-//ExtractedObjects
+//extractedObjects
 //Global Object for class switch!
-@Field def switchesoff = new switching(1)
+@Field def switchesoff = new Switching(sendEvent, 1)
 //Global Object for class switch!
-@Field def switcheson = new switching(1)
+@Field def switcheson = new Switching(sendEvent, 1)
 //Global Object for class lock!
-@Field def lock1 = new locking(1)
+@Field def lock1 = new Locking(sendEvent, 1)
 //Global variable for mode!
 @Field def newMode = "away"
 //Global variable for number!
-@Field def waitfor = 5
+@Field def waitfor = 10
 //Global Object for functions in subscribe method!
 @Field def installed = this.&installed
 //Global Object for functions in subscribe method!
@@ -59,12 +62,12 @@ import appTouch.Touch
 
 //Methods
 /////////////////////////////////////////////////////////////////////
-def definition(LinkedHashMap LHM) {
+def definition(LinkedHashMap metaData) {
        println("IGNORE -- JUST SOME DEFINITION")
 }
 
 /////////////////////////////////////////////////////////////////////
-def preferences(Closure Input) {
+def preferences(Closure metaData) {
        println("IGNORE -- JUST SOME DEFINITION")
 }
 /////////////////////////////////////////////////////////////////////
@@ -73,196 +76,84 @@ def setLocationMode(String mode) {
 }
 
 /////////////////////////////////////////////////////////////////////
-////subscribe(app, func)
-def subscribe(Object Obj, Closure Input) {
-       ObjectList.add(Obj)
-       EventList.add("Touched")
-       FunctionList.add(Input)
+////subscribe(obj, func)
+def subscribe(Object obj, Closure FunctionToCall) {
+       objectList.add(obj)
+       eventList.add("Touched")
+       functionList.add(FunctionToCall)
 }
-////subscribe(obj, string, func)
-def subscribe(Object Obj, String S, Closure Input) {
-       ObjectList.add(Obj)
-       EventList.add(S)
-       FunctionList.add(Input)
+////subscribe(obj, event, func)
+def subscribe(Object obj, String event, Closure FunctionToCall) {
+       objectList.add(obj)
+       eventList.add(event)
+       functionList.add(FunctionToCall)
 }
-////subscribe(obj, string, func, hashmap)
-def subscribe(Object Obj, String S, Closure Input, LinkedHashMap LHM) {
-       ObjectList.add(Obj)     
-       EventList.add(S)
-       FunctionList.add(Input)
-}
-/////////////////////////////////////////////////////////////////////
-def EventHandler() {
-       while(true) {
-               List evt = []
-               print "Waiting for an event...\n"
-               def EVENT = System.in.newReader().readLine()
-               SepLine = EVENT.split()
-               for (int i = 0; i < EventList.size(); i++) {
-                       if (EventList[i] == SepLine[0]) {
-                               println("The following effect: \n")
-                               evt.add(new Event())
-                               switch(SepLine[0]) { 
-                                       case "Touched":
-                                               ObjectList[i].touched = 1
-                                               evt[-1].value = "Touched"
-                                               evt[-1].linkText = "touched by user"
-                                               evt[-1].name = "TouchSensor"
-                                               evt[-1].descriptionText = "Touching"
-                                               break
-                                       case "lock":
-                                               if (SepLine[1] == "0") {
-                                                       ObjectList[i][0].lock()
-                                                       evt[-1].deviceId = 0
-                                                       evt[-1].value = "locked"
-                                                       evt[-1].linkText = "lock0"
-                                                       evt[-1].displayName = "lock0"
-                                                       evt[-1].name = "lock"
-                                                       evt[-1].descriptionText = "locking"
-                                               } else if (SepLine[1] == "1") {
-                                                       ObjectList[i][1].lock()
-                                                       evt[-1].deviceId = 1
-                                                       evt[-1].value = "locked"
-                                                       evt[-1].linkText = "lock1"
-                                                       evt[-1].displayName = "lock1"
-                                                       evt[-1].name = "lock"
-                                                       evt[-1].descriptionText = "locking"
-                                               } else if (SepLine[1] == "2") {
-                                                       ObjectList[i][2].lock()
-                                                       evt[-1].deviceId = 2
-                                                       evt[-1].value = "locked"
-                                                       evt[-1].linkText = "lock2"
-                                                       evt[-1].displayName = "lock2"
-                                                       evt[-1].name = "lock"
-                                                       evt[-1].descriptionText = "locking"
-                                               }
-                                               break
-                                       case "unlock":
-                                               if (SepLine[1] == "0") {
-                                                       ObjectList[i][0].unlock()
-                                                       evt[-1].deviceId = 0
-                                                       evt[-1].value = "unlocked"
-                                                       evt[-1].linkText = "lock0"
-                                                       evt[-1].displayName = "lock0"
-                                                       evt[-1].name = "lock"
-                                                       evt[-1].descriptionText = "unlocking"   
-                                               } else if (SepLine[1] == "1") {
-                                                       ObjectList[i][1].unlock()
-                                                       evt[-1].deviceId = 0
-                                                       evt[-1].value = "unlocked"
-                                                       evt[-1].linkText = "lock1"
-                                                       evt[-1].displayName = "lock1"
-                                                       evt[-1].name = "lock"
-                                                       evt[-1].descriptionText = "unlocking"
-                                               } else if (SepLine[1] == "2") {
-                                                       ObjectList[i][2].unlock()
-                                                       evt[-1].deviceId = 2
-                                                       evt[-1].value = "unlocked"
-                                                       evt[-1].linkText = "lock2"
-                                                       evt[-1].displayName = "lock2"
-                                                       evt[-1].name = "lock"
-                                                       evt[-1].descriptionText = "unlocking"
-                                               }
-                                               break
-                                       case "contact.open":
-                                               if (SepLine[1] == "0") {
-                                                       ObjectList[i][0].contactLatestValue = ObjectList[i].currentContact
-                                                       ObjectList[i][0].currentContact = "open"
-                                                       evt[-1].deviceId = 0
-                                                       evt[-1].value = "contact.open"
-                                                       evt[-1].linkText = "contact0"
-                                                       evt[-1].displayName = "contact0"
-                                                       evt[-1].name = "ContactSensor"
-                                                       evt[-1].descriptionText = "opening"     
-                                               } else if (SepLine[1] == "1") {
-                                                       ObjectList[i][1].contactLatestValue = ObjectList[i].currentContact
-                                                       ObjectList[i][1].currentContact = "open"
-                                                       evt[-1].deviceId = 1
-                                                       evt[-1].value = "contact.open"
-                                                       evt[-1].linkText = "contact1"
-                                                       evt[-1].displayName = "contact1"
-                                                       evt[-1].name = "ContactSensor"
-                                                       evt[-1].descriptionText = "opening"
-                                               } else if (SepLine[1] == "2") {
-                                                       ObjectList[i][2].contactLatestValue = ObjectList[i].currentContact
-                                                       ObjectList[i][2].currentContact = "open"
-                                                       evt[-1].deviceId = 2
-                                                       evt[-1].value = "contact.open"
-                                                       evt[-1].linkText = "contact2"
-                                                       evt[-1].displayName = "contact2"
-                                                       evt[-1].name = "ContactSensor"
-                                                       evt[-1].descriptionText = "opening"
-                                               }
-                                               break
-                                       case "contact.closed":
-                                               if (SepLine[1] == "0") {
-                                                       ObjectList[i][0].contactLatestValue = ObjectList[i].currentContact
-                                                       ObjectList[i][0].currentContact = "closed"
-                                                       evt[-1].deviceId = 0
-                                                       evt[-1].value = "contact.closed"
-                                                       evt[-1].linkText = "contact0"
-                                                       evt[-1].displayName = "contact0"
-                                                       evt[-1].name = "ContactSensor"
-                                                       evt[-1].descriptionText = "closing"
-                                               } else if (SepLine[1] == "1") {
-                                                       ObjectList[i][1].contactLatestValue = ObjectList[i].currentContact
-                                                       ObjectList[i][1].currentContact = "closed"
-                                                       evt[-1].deviceId = 1
-                                                       evt[-1].value = "contact.closed"
-                                                       evt[-1].linkText = "contact1"
-                                                       evt[-1].displayName = "contact1"
-                                                       evt[-1].name = "ContactSensor"
-                                                       evt[-1].descriptionText = "closing"
-                                               } else if (SepLine[1] == "2") {
-                                                       ObjectList[i][2].contactLatestValue = ObjectList[i].currentContact
-                                                       ObjectList[i][2].currentContact = "closed"
-                                                       evt[-1].deviceId = 2
-                                                       evt[-1].value = "contact.closed"
-                                                       evt[-1].linkText = "contact2"
-                                                       evt[-1].displayName = "contact2"
-                                                       evt[-1].name = "ContactSensor"
-                                                       evt[-1].descriptionText = "closing"
-                                               }
-                                               break
-                                       default:
-                                               break
-                               }
-                               FunctionList[i](evt[-1])
-                       }
-               }
-       }
+////subscribe(obj, event, func, data)
+def subscribe(Object obj, String event, Closure FunctionToCall, LinkedHashMap metaData) {
+       objectList.add(obj)     
+       eventList.add(event)
+       functionList.add(FunctionToCall)
 }
 /////////////////////////////////////////////////////////////////////
 ////runIn(time, func)
-def runIn(int seconds, Closure Input) {
-       ListofTimersFunc.add(Input)
-       ListofTimers.add(new Timer())
-       def task = ListofTimers[-1].runAfter(1000*seconds, Input)
+def runIn(int seconds, Closure functionToCall) {
+       timersFuncList.add(functionToCall)
+       timersList.add(new Timer())
+       def task = timersList[-1].runAfter(1000*seconds, functionToCall)
 }
 /////////////////////////////////////////////////////////////////////
 ////unschedule(func)
-def unschedule(Closure Input) {
-       for (int i = 0;i < ListofTimersFunc.size();i++) {
-               if (ListofTimersFunc[i] == Input) {
-                       ListofTimers[i].cancel()
+def unschedule(Closure functionToUnschedule) {
+       for (int i = 0;i < timersFuncList.size();i++) {
+               if (timersFuncList[i] == functionToUnschedule) {
+                       timersList[i].cancel()
                }
        }
 }
 /////////////////////////////////////////////////////////////////////
 ////sendNotificationToContacts(text, recipients)
-def sendNotificationToContacts(String S, List recipients) {
+def sendNotificationToContacts(String text, List recipients) {
        for (int i = 0;i < recipients.size();i++) {
-               for (int j = 0;j < location.CONTACTS.size();j++) {
-                       if (recipients[i] == location.CONTACTS[j]) {
-                               println("Sending \""+S+"\" to "+location.PhoneNums[j].toString())
+               for (int j = 0;j < location.contacts.size();j++) {
+                       if (recipients[i] == location.contacts[j]) {
+                               println("Sending \""+text+"\" to "+location.phoneNumbers[j].toString())
                        }
                }
        }
 }
 /////////////////////////////////////////////////////////////////////
-////sendNotificationToContacts(text, recipients)
-def sendSms(long Phone, String S) {
-       println("Sending \""+S+"\" to "+Phone.toString())
+////sendSms(phone, text)
+def sendSms(long phoneNumber, String text) {
+       println("Sending \""+text+"\" to "+phoneNumber.toString())
+}
+/////////////////////////////////////////////////////////////////////
+def eventHandler(LinkedHashMap eventDataMap) {
+       def value = eventDataMap["value"]
+       def name = eventDataMap["name"]
+       def deviceId = eventDataMap["deviceId"]
+       def descriptionText = eventDataMap["descriptionText"]
+       def displayed = eventDataMap["displayed"]
+       def linkText = eventDataMap["linkText"]
+       def isStateChange = eventDataMap["isStateChange"]
+       def unit = eventDataMap["unit"]
+       def data = eventDataMap["data"]
+       
+       for (int i = 0;i < eventList.size();i++) {
+               if (eventList[i] == value) {
+                       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
+                       functionList[i](evt[-1])
+               }
+       }
 }
 
 /**
@@ -347,4 +238,3 @@ def appTouch(evt) {
 }
 
 installed()
-EventHandler()