From e967e66f8e5a414c24bc87c5146bade90e1759cc Mon Sep 17 00:00:00 2001 From: amiraj Date: Tue, 2 Jul 2019 14:39:33 -0700 Subject: [PATCH] Commit #4 --- ContactSensor/Contacting.groovy | 45 +++ ContactSensor/Contacts.groovy | 35 +++ ContactSensor/contacting.groovy | 34 -- ContactSensor/contacts.groovy | 29 -- Event/Event.groovy | 8 + Extractor/ExtractorScript.py | 14 +- ...Objects.groovy => extractedObjects.groovy} | 8 +- GlobalVariables/GlobalVariables.groovy | 27 +- Location/LocationVar.groovy | 21 ++ Location/Phrase.groovy | 6 +- Location/locationVar.groovy | 21 -- Lock/Locking.groovy | 67 ++++ Lock/Locks.groovy | 77 +++++ Lock/locking.groovy | 55 ---- Lock/locks.groovy | 40 --- Methods/EventHandler.groovy | 141 --------- Methods/definition.groovy | 2 +- Methods/eventHandler.groovy | 29 ++ Methods/preferences.groovy | 2 +- Methods/runIn.groovy | 8 +- Methods/sendNotificationToContacts.groovy | 8 +- Methods/sendSms.groovy | 6 +- Methods/subscribe.groovy | 30 +- Methods/unschedule.groovy | 8 +- Runner.py | 31 +- Switch/Switches.groovy | 77 +++++ Switch/Switching.groovy | 67 ++++ Switch/switches.groovy | 58 ---- Switch/switching.groovy | 80 ----- appTouch/Touch.groovy | 10 - appTouch/Touched.groovy | 19 ++ main.groovy | 290 ++++++------------ 32 files changed, 611 insertions(+), 742 deletions(-) create mode 100644 ContactSensor/Contacting.groovy create mode 100644 ContactSensor/Contacts.groovy delete mode 100644 ContactSensor/contacting.groovy delete mode 100644 ContactSensor/contacts.groovy rename Extractor/{ExtractedObjects.groovy => extractedObjects.groovy} (71%) create mode 100644 Location/LocationVar.groovy delete mode 100644 Location/locationVar.groovy create mode 100644 Lock/Locking.groovy create mode 100644 Lock/Locks.groovy delete mode 100644 Lock/locking.groovy delete mode 100644 Lock/locks.groovy delete mode 100644 Methods/EventHandler.groovy create mode 100644 Methods/eventHandler.groovy create mode 100644 Switch/Switches.groovy create mode 100644 Switch/Switching.groovy delete mode 100644 Switch/switches.groovy delete mode 100644 Switch/switching.groovy delete mode 100644 appTouch/Touch.groovy create mode 100644 appTouch/Touched.groovy diff --git a/ContactSensor/Contacting.groovy b/ContactSensor/Contacting.groovy new file mode 100644 index 0000000..1fa6c47 --- /dev/null +++ b/ContactSensor/Contacting.groovy @@ -0,0 +1,45 @@ +//Create a class for contact sensor +package ContactSensor + +public class Contacting{ + private int deviceNumbers + private List contacts + def sendEvent + + + + Contacting(Closure sendEvent, int deviceNumbers) { + this.sendEvent = sendEvent + this.deviceNumbers = deviceNumbers + this.contacts = [] + int id = 0 + for (int i = 0;i < deviceNumbers;i++) { + contacts.add(new Contacts(id, "contact"+id.toString(), "closed", "closed"))//By default closed + id = id+1 + } + } + + //By Model Checker + def setValue(LinkedHashMap eventDataMap) { + contacts[eventDataMap["deviceId"]].setValue(eventDataMap["value"]) + sendEvent(eventDataMap) + } + + def currentValue(String deviceFeature) { + if (deviceNumbers == 1) + contacts[0].currentValue(deviceFeature)//It is called if we have only one device + else + contacts*.currentValue(deviceFeature) + } + + def latestValue(String deviceFeature) { + if (deviceNumbers == 1) + contacts[0].latestValue(deviceFeature)//It is called if we have only one device + else + contacts*.latestValue(deviceFeature) + } + + def getAt(int ix) { + contacts[ix] + } +} diff --git a/ContactSensor/Contacts.groovy b/ContactSensor/Contacts.groovy new file mode 100644 index 0000000..c922a9e --- /dev/null +++ b/ContactSensor/Contacts.groovy @@ -0,0 +1,35 @@ +//Create a class for contact sensor +package ContactSensor + +public class Contacts { + private int id + private String displayName + private String contactCurrentValue + private String contactLatestValue + + Contacts(int id, String displayName, String contactCurrentValue, String contactLatestValue) { + this.id = id + this.displayName = displayName + this.contactCurrentValue = contactCurrentValue + this.contactLatestValue = contactLatestValue + } + + def setValue(String value) { + this.contactLatestValue = contactCurrentValue + println("the contact sensor with id:$id is triggered to $value!") + this.contactCurrentValue = value + } + + def currentValue(String deviceFeature) { + if (deviceFeature == "contact") { + return contactCurrentValue + } + } + + def latestValue(String deviceFeature) { + if (deviceFeature == "contact") { + return contactLatestValue + } + } +} + diff --git a/ContactSensor/contacting.groovy b/ContactSensor/contacting.groovy deleted file mode 100644 index e6d8848..0000000 --- a/ContactSensor/contacting.groovy +++ /dev/null @@ -1,34 +0,0 @@ -//Create a class for contact sensor -package ContactSensor - -public class contacting{ - List contacts - int count - - contacting(int count) { - this.count = count - if (count == 1) { - contacts = [new contacts(0, "contact0", "closed", "closed")] - } else if (count == 2) { - contacts = [new contacts(0, "contact0", "closed", "closed"), new contacts(1, "contact1", "closed", "closed")] - } else if (count == 3) { - contacts = [new contacts(0, "contact0", "closed", "closed"), new contacts(1, "contact1", "closed", "closed"), new contacts(2,"contact2", "closed", "closed")] - } - } - - def currentValue(String S) { - if (count == 1) { - contacts[0].currentValue(S) - } else { - contacts*.currentValue(S) - } - } - - def latestValue(String S) { - if (count == 1) { - contacts[0].latestValue(S) - } else { - contacts*.latestValue(S) - } - } -} diff --git a/ContactSensor/contacts.groovy b/ContactSensor/contacts.groovy deleted file mode 100644 index 09f64bf..0000000 --- a/ContactSensor/contacts.groovy +++ /dev/null @@ -1,29 +0,0 @@ -//Create a class for contact sensor -package ContactSensor - -public class contacts { - private int id = 0 - private String displayName - private String currentContact - private String contactLatestValue - - contacts(int id, String displayName, String currentContact, String contactLatestValue) { - this.id = id - this.displayName = displayName - this.currentContact = currentContact - this.contactLatestValue = contactLatestValue - } - - def currentValue(String S) { - if (S == "contact") { - return currentContact - } - } - - def latestValue(String S) { - if (S == "contact") { - return contactLatestValue - } - } -} - diff --git a/Event/Event.groovy b/Event/Event.groovy index fb738c7..a1d5e71 100644 --- a/Event/Event.groovy +++ b/Event/Event.groovy @@ -6,8 +6,12 @@ public class Event { private String value private String linkText private String displayName + private boolean displayed private String name private String descriptionText + private boolean isStateChange + private String unit + private LinkedHashMap data Event() { this.deviceId = 0 @@ -16,5 +20,9 @@ public class Event { this.displayName = "" this.name = "" this.descriptionText = "" + this.isStateChange = false + this.unit = "" + this.data = [] + this.displayed = false } } diff --git a/Extractor/ExtractorScript.py b/Extractor/ExtractorScript.py index b9654b4..399651b 100644 --- a/Extractor/ExtractorScript.py +++ b/Extractor/ExtractorScript.py @@ -37,7 +37,7 @@ def GetToken(f): F = open("Extractor/App.groovy", "r") -Out = open("Extractor/ExtractedObjects.groovy", "w+") +Out = open("Extractor/extractedObjects.groovy", "w+") Temp = GetToken(F) Objects = [] @@ -83,11 +83,11 @@ while (Temp != "EOF"): if (Multiple != "" and Multiple == "true"): g = raw_input("Enter the number of locks to control: (1, 2, or 3)\n") Out.write("//Global Object for class lock!\n") - Out.write("@Field def %s = new locking(" % Object) + Out.write("@Field def %s = new Locking(sendEvent, " % Object) Out.write("%s)\n" % g) elif (Multiple == "" or Multiple == "false"): Out.write("//Global Object for class lock!\n") - Out.write("@Field def %s = new locking(1)\n" % Object) + Out.write("@Field def %s = new Locking(sendEvent, 1)\n" % Object) #elif (Type == "capability.alarm"): #elif (Type == "capability.battery"): @@ -104,11 +104,11 @@ while (Temp != "EOF"): if (Multiple != "" and Multiple == "true"): g = raw_input("Enter the number of contactSensors to monitor: (1, 2, or 3)\n") Out.write("//Global Object for class contactSensor!\n") - Out.write("@Field def %s = new contacting(" % Object) + Out.write("@Field def %s = new Contacting(sendEvent, " % Object) Out.write("%s)\n" % g) elif (Multiple == "" or Multiple == "false"): Out.write("//Global Object for class contactSensor!\n") - Out.write("@Field def %s = new contacting(1)\n" % Object) + Out.write("@Field def %s = new Contacting(sendEvent, 1)\n" % Object) #elif (Type == "capability.doorControl"): #elif (Type == "capability.energyMeter"): @@ -141,11 +141,11 @@ while (Temp != "EOF"): if (Multiple != "" and Multiple == "true"): g = raw_input("Enter the number of switches to control: (1, 2, or 3)\n") Out.write("//Global Object for class switch!\n") - Out.write("@Field def %s = new switching(" % Object) + Out.write("@Field def %s = new Switching(sendEvent, " % Object) Out.write("%s)\n" % g) elif (Multiple == "" or Multiple == "false"): Out.write("//Global Object for class switch!\n") - Out.write("@Field def %s = new switching(1)\n" % Object) + Out.write("@Field def %s = new Switching(sendEvent, 1)\n" % Object) #elif (Type == "capability.switchLevel"): #elif (Type == "capability.temperatureMeasurement"): diff --git a/Extractor/ExtractedObjects.groovy b/Extractor/extractedObjects.groovy similarity index 71% rename from Extractor/ExtractedObjects.groovy rename to Extractor/extractedObjects.groovy index 67d3b0f..ccd3d3a 100644 --- a/Extractor/ExtractedObjects.groovy +++ b/Extractor/extractedObjects.groovy @@ -1,11 +1,11 @@ //Global Object for class switch! -@Field def switchesoff = new switching(3) +@Field def switchesoff = new Switching(sendEvent, 1) //Global Object for class switch! -@Field def switcheson = new switching(3) +@Field def switcheson = new Switching(sendEvent, 1) //Global Object for class lock! -@Field def lock1 = new locking(2) +@Field def lock1 = new Locking(sendEvent, 2) //Global variable for mode! -@Field def newMode = "home" +@Field def newMode = "away" //Global variable for number! @Field def waitfor = 10 //Global Object for functions in subscribe method! diff --git a/GlobalVariables/GlobalVariables.groovy b/GlobalVariables/GlobalVariables.groovy index 92ce8b9..5207e4f 100644 --- a/GlobalVariables/GlobalVariables.groovy +++ b/GlobalVariables/GlobalVariables.groovy @@ -1,22 +1,25 @@ +//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 = [] diff --git a/Location/LocationVar.groovy b/Location/LocationVar.groovy new file mode 100644 index 0000000..cfaa308 --- /dev/null +++ b/Location/LocationVar.groovy @@ -0,0 +1,21 @@ +//Create a class for location variable +package Location + +class LocationVar { + private int contactBookEnabled + private String modes + private String mode + private List contacts + private List phoneNumbers + + private Phrase helloHome + + LocationVar() { + this.modes = "'home', 'away', 'night'" + this.mode = "home" + this.helloHome = new Phrase() + this.contactBookEnabled = 1 + this.contacts = ['AJ'] + this.phoneNumbers = [9495379373] + } +} diff --git a/Location/Phrase.groovy b/Location/Phrase.groovy index f9f3081..c70b934 100644 --- a/Location/Phrase.groovy +++ b/Location/Phrase.groovy @@ -2,13 +2,13 @@ package Location class Phrase { - private LinkedHashMap Phrases + private LinkedHashMap phrases Phrase() { - this.Phrases = [id:0, label:"Good Morning!"] + this.phrases = [id:0, label:"Good Morning!"] } def getPhrases() { - return Phrases + return this.phrases } } diff --git a/Location/locationVar.groovy b/Location/locationVar.groovy deleted file mode 100644 index 42e1f56..0000000 --- a/Location/locationVar.groovy +++ /dev/null @@ -1,21 +0,0 @@ -//Create a class for location variable -package Location - -class locationVar { - private String modes - private String mode - private int contactBookEnabled - private List CONTACTS - private List PhoneNums - - private Phrase helloHome - - locationVar() { - this.modes = "'home', 'away', 'night'" - this.mode = "home" - this.helloHome = new Phrase() - this.contactBookEnabled = 1 - this.CONTACTS = ['AJ'] - this.PhoneNums = [9495379373] - } -} diff --git a/Lock/Locking.groovy b/Lock/Locking.groovy new file mode 100644 index 0000000..20b0a11 --- /dev/null +++ b/Lock/Locking.groovy @@ -0,0 +1,67 @@ +//Create a class for lock device +package Lock + +public class Locking{ + int deviceNumbers + List locks + def sendEvent + def timers + + Locking(Closure sendEvent, int deviceNumbers) { + this.sendEvent = sendEvent + this.timers = new Timer() + this.deviceNumbers = deviceNumbers + this.locks = [] + int id = 0 + for (int i = 0;i < deviceNumbers;i++) { + locks.add(new Locks(sendEvent, id, "lock"+id, "locked", "locked"))//By default locked + id = id+1 + } + } + + //By Apps + def lock() { + locks*.lock() + } + + def lock(LinkedHashMap metaData) { + def task = timers.runAfter(metaData["delay"]) { + locks*.lock() + } + } + + def unlock() { + locks*.unlock() + } + + def unlock(LinkedHashMap metaData) { + def task = timers.runAfter(metaData["delay"]) { + locks*.unlock() + } + } + + //By Model Checker + def setValue(LinkedHashMap eventDataMap) { + locks[eventDataMap["deviceId"]].setValue(eventDataMap["value"]) + sendEvent(eventDataMap) + } + + def currentValue(String deviceFeature) { + if (deviceNumbers == 1) + locks[0].currentValue(deviceFeature) + else + locks*.currentValue(deviceFeature) + } + + def latestValue(String deviceFeature) { + if (deviceNumbers == 1) + locks[0].latestValue(deviceFeature) + else + locks*.latestValue(deviceFeature) + } + + def getAt(int ix) { + locks[ix] + } +} + diff --git a/Lock/Locks.groovy b/Lock/Locks.groovy new file mode 100644 index 0000000..752f509 --- /dev/null +++ b/Lock/Locks.groovy @@ -0,0 +1,77 @@ +//Create a class for lock device +package Lock + +public class Locks { + private int id + private String displayName + private String lockCurrentValue + private String lockLatestValue + def sendEvent + def timers + + + Locks(Closure sendEvent, int id, String displayName, String lockCurrentValue, String lockLatestValue) { + this.id = id + this.sendEvent = sendEvent + this.displayName = displayName + this.lockCurrentValue = lockCurrentValue + this.lockLatestValue = lockLatestValue + this.timers = new Timer() + } + + //By Apps + def lock() { + println("the door with id:$id is locked!") + this.lockLatestValue = this.lockCurrentValue + this.lockCurrentValue = "locked" + sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + } + + def lock(LinkedHashMap metaData) { + def task = timers.runAfter(metaData["delay"]) { + println("the door with id:$id is locked!") + this.lockLatestValue = this.lockCurrentValue + this.lockCurrentValue = "locked" + sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + } + } + + def unlock() { + println("the door with id:$id is unlocked!") + this.lockLatestValue = this.lockCurrentValue + this.lockCurrentValue = "unlocked" + sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + } + + def unlock(LinkedHashMap metaData) { + def task = timers.runAfter(metaData["delay"]) { + println("the door with id:$id is locked!") + this.lockLatestValue = this.lockCurrentValue + this.lockCurrentValue = "locked" + sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + } + } + + //By Model Checker + def setValue(String value) { + println("the door with id:$id is $value!") + this.lockLatestValue = this.lockCurrentValue + this.lockCurrentValue = value + } + + def currentValue(String deviceFeature) { + if (deviceFeature == "lock") { + return lockCurrentValue + } + } + + def latestValue(String deviceFeature) { + if (deviceFeature == "lock") { + return lockLatestValue + } + } +} diff --git a/Lock/locking.groovy b/Lock/locking.groovy deleted file mode 100644 index e8ce9dd..0000000 --- a/Lock/locking.groovy +++ /dev/null @@ -1,55 +0,0 @@ -//Create a class for lock device -package Lock - -public class locking{ - List locks - int count - - locking(int count) { - this.count = count - if (count == 1) { - locks = [new locks(0, "lock0", "locked", "locked")] - } else if (count == 2) { - locks = [new locks(0, "lock0", "locked", "locked"),new locks(1, "lock1", "locked", "locked")] - } else if (count == 3) { - locks = [new locks(0, "lock0", "locked", "locked"),new locks(1, "lock1", "locked", "locked"),new locks(2, "lock2", "locked", "locked")] - } - } - - def lock() { - if (count == 1) { - locks[0].lock() - } else { - locks*.lock() - } - } - - def unlock() { - if (count == 1) { - locks[0].unlock() - } else { - locks*.unlock() - } - } - - def currentValue(String S) { - if (count == 1) { - locks[0].currentValue(S) - } else { - locks*.currentValue(S) - } - } - - def latestValue(String S) { - if (count == 1) { - locks[0].latestValue(S) - } else { - locks*.latestValue(S) - } - } - - def getAt(int ix) { - locks[ix] - } -} - diff --git a/Lock/locks.groovy b/Lock/locks.groovy deleted file mode 100644 index 87671da..0000000 --- a/Lock/locks.groovy +++ /dev/null @@ -1,40 +0,0 @@ -//Create a class for lock device -package Lock - -public class locks { - private int id = 0 - private String displayName - private String currentLock - private String lockLatestValue - - locks(int id, String displayName, String currentLock, String lockLatestValue) { - this.id = id - this.displayName = displayName - this.currentLock = currentLock - this.lockLatestValue = lockLatestValue - } - - def lock() { - println("the door with id:$id is locked!") - this.lockLatestValue = this.currentLock - this.currentLock = "locked" - } - - def unlock() { - println("the door with id:$id is unlocked!") - this.lockLatestValue = this.currentLock - this.currentLock = "unlocked" - } - - def currentValue(String S) { - if (S == "lock") { - return currentLock - } - } - - def latestValue(String S) { - if (S == "lock") { - return lockLatestValue - } - } -} diff --git a/Methods/EventHandler.groovy b/Methods/EventHandler.groovy deleted file mode 100644 index a216725..0000000 --- a/Methods/EventHandler.groovy +++ /dev/null @@ -1,141 +0,0 @@ -///////////////////////////////////////////////////////////////////// -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]) - } - } - } -} diff --git a/Methods/definition.groovy b/Methods/definition.groovy index a5cb5dd..aac9c53 100644 --- a/Methods/definition.groovy +++ b/Methods/definition.groovy @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////// -def definition(LinkedHashMap LHM) { +def definition(LinkedHashMap metaData) { println("IGNORE -- JUST SOME DEFINITION") } diff --git a/Methods/eventHandler.groovy b/Methods/eventHandler.groovy new file mode 100644 index 0000000..f16bdce --- /dev/null +++ b/Methods/eventHandler.groovy @@ -0,0 +1,29 @@ +///////////////////////////////////////////////////////////////////// +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]) + } + } +} diff --git a/Methods/preferences.groovy b/Methods/preferences.groovy index 8c98821..ac81c8e 100644 --- a/Methods/preferences.groovy +++ b/Methods/preferences.groovy @@ -1,4 +1,4 @@ ///////////////////////////////////////////////////////////////////// -def preferences(Closure Input) { +def preferences(Closure metaData) { println("IGNORE -- JUST SOME DEFINITION") } diff --git a/Methods/runIn.groovy b/Methods/runIn.groovy index ce056e3..3816599 100644 --- a/Methods/runIn.groovy +++ b/Methods/runIn.groovy @@ -1,7 +1,7 @@ ///////////////////////////////////////////////////////////////////// ////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) } diff --git a/Methods/sendNotificationToContacts.groovy b/Methods/sendNotificationToContacts.groovy index cab016b..b5afd49 100644 --- a/Methods/sendNotificationToContacts.groovy +++ b/Methods/sendNotificationToContacts.groovy @@ -1,10 +1,10 @@ ///////////////////////////////////////////////////////////////////// ////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()) } } } diff --git a/Methods/sendSms.groovy b/Methods/sendSms.groovy index f60a0a1..2d71fa9 100644 --- a/Methods/sendSms.groovy +++ b/Methods/sendSms.groovy @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////// -////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()) } diff --git a/Methods/subscribe.groovy b/Methods/subscribe.groovy index c7104fc..1726fc5 100644 --- a/Methods/subscribe.groovy +++ b/Methods/subscribe.groovy @@ -1,19 +1,19 @@ ///////////////////////////////////////////////////////////////////// -////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) +////subscribe(obj, event, func, data) +def subscribe(Object obj, String event, Closure FunctionToCall, LinkedHashMap metaData) { + objectList.add(obj) + eventList.add(event) + functionList.add(FunctionToCall) } diff --git a/Methods/unschedule.groovy b/Methods/unschedule.groovy index 8033606..1689621 100644 --- a/Methods/unschedule.groovy +++ b/Methods/unschedule.groovy @@ -1,9 +1,9 @@ ///////////////////////////////////////////////////////////////////// ////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() } } } diff --git a/Runner.py b/Runner.py index 86a4251..9aab628 100644 --- a/Runner.py +++ b/Runner.py @@ -7,13 +7,13 @@ definition = open("Methods/"+"definition.groovy", "r") preferences = open("Methods/"+"preferences.groovy", "r") setLocationMode = open("Methods/"+"setLocationMode.groovy", "r") subscribe = open("Methods/"+"subscribe.groovy", "r") -EventHandler = open("Methods/"+"EventHandler.groovy", "r") runIn = open("Methods/"+"runIn.groovy", "r") unschedule = open("Methods/"+"unschedule.groovy", "r") sendNotificationToContacts = open("Methods/"+"sendNotificationToContacts.groovy", "r") sendSms = open("Methods/"+"sendSms.groovy", "r") +eventHandler = open("Methods/"+"eventHandler.groovy", "r") App = open("Extractor/"+"App.groovy", "r") -ExtractedObjects = open("Extractor/"+"ExtractedObjects.groovy", "r") +extractedObjects = open("Extractor/"+"extractedObjects.groovy", "r") #Extract information from preferences and subscribe method to create required objects @@ -24,24 +24,24 @@ Out.write("//Importing Libraries\n") Out.write("import groovy.transform.Field\n") Out.write("\n") Out.write("//Importing Classes\n") -Out.write("import ContactSensor.contacting\n") -Out.write("import ContactSensor.contacts\n") -Out.write("import Lock.locking\n") -Out.write("import Lock.locks\n") -Out.write("import Switch.switching\n") -Out.write("import Switch.switches\n") -Out.write("import Event.Event\n") +Out.write("import ContactSensor.Contacting\n") +Out.write("import ContactSensor.Contacts\n") +Out.write("import Lock.Locking\n") +Out.write("import Lock.Locks\n") +Out.write("import Switch.Switching\n") +Out.write("import Switch.Switches\n") Out.write("import Logger.Logger\n") -Out.write("import Location.locationVar\n") +Out.write("import Location.LocationVar\n") Out.write("import Location.Phrase\n") -Out.write("import appTouch.Touch\n") +Out.write("import appTouch.Touched\n") +Out.write("import Event.Event\n") Out.write("\n") Out.write("//GlobalVariables\n") for line in GlobalVariables: Out.write(line) Out.write("\n") -Out.write("//ExtractedObjects\n") -for line in ExtractedObjects: +Out.write("//extractedObjects\n") +for line in extractedObjects: Out.write(line) Out.write("\n") Out.write("//Methods\n") @@ -53,8 +53,6 @@ for line in setLocationMode: Out.write(line) for line in subscribe: Out.write(line) -for line in EventHandler: - Out.write(line) for line in runIn: Out.write(line) for line in unschedule: @@ -63,12 +61,13 @@ for line in sendNotificationToContacts: Out.write(line) for line in sendSms: Out.write(line) +for line in eventHandler: + Out.write(line) Out.write("\n") for line in App: Out.write(line) Out.write("\n") Out.write("installed()\n") -Out.write("EventHandler()\n") Out.close() diff --git a/Switch/Switches.groovy b/Switch/Switches.groovy new file mode 100644 index 0000000..dcf981f --- /dev/null +++ b/Switch/Switches.groovy @@ -0,0 +1,77 @@ +//Create a class for switch device +package Switch + +public class Switches { + private int id = 0 + private String displayName + private String switchCurrentValue + private String switchLatestValue + def sendEvent + def timers + + + Switches(Closure sendEvent, int id, String displayName, String switchCurrentValue, String switchLatestValue) { + this.sendEvent = sendEvent + this.timers = new Timer() + this.id = id + this.displayName = displayName + this.switchCurrentValue = switchCurrentValue + this.switchLatestValue = switchLatestValue + } + + //By Apps + def on() { + println("the switch with id:$id is on!") + this.switchLatestValue = this.switchCurrentValue + this.switchCurrentValue = "on" + sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + } + + def on(LinkedHashMap metaData) { + def task = timers.runAfter(metaData["delay"]) { + println("the switch with id:$id is on!") + this.switchLatestValue = this.switchCurrentValue + this.switchCurrentValue = "on" + sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + } + } + + def off() { + println("the switch with id:$id is off!") + this.switchLatestValue = this.switchCurrentValue + this.switchCurrentValue = "off" + sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + } + + def off(LinkedHashMap metaData) { + def task = timers.runAfter(metaData["delay"]) { + println("the switch with id:$id is off!") + this.switchLatestValue = this.switchCurrentValue + this.switchCurrentValue = "off" + sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + } + } + + //By Model Checker + def setValue(String value) { + println("the switch with id:$id is $value!") + this.switchLatestValue = this.switchCurrentValue + this.switchCurrentValue = value + } + + def currentValue(String deviceFeature) { + if (deviceFeature == "switch") { + return switchCurrentValue + } + } + + def latestValue(String deviceFeature) { + if (deviceFeature == "switch") { + return switchLatestValue + } + } +} diff --git a/Switch/Switching.groovy b/Switch/Switching.groovy new file mode 100644 index 0000000..1364d41 --- /dev/null +++ b/Switch/Switching.groovy @@ -0,0 +1,67 @@ +//Create a class for switch device +package Switch + +public class Switching{ + int deviceNumbers + List switches + def timers + def sendEvent + + Switching(Closure sendEvent, int deviceNumbers) { + this.sendEvent = sendEvent + this.timers = new Timer() + this.deviceNumbers = deviceNumbers + this.switches = [] + int id = 0 + for (int i = 0;i < deviceNumbers;i++) { + switches.add(new Switches(sendEvent, id, "switch"+id.toString(), "off", "off")) + id = id+1 + } + } + + //By Apps + def on() { + switches*.on() + } + + def on(LinkedHashMap metaData) { + def task = timers.runAfter(metaData["delay"]) { + switches*.on() + } + } + + def off() { + switches*.off() + } + + def off(LinkedHashMap metaData) { + def task = timers.runAfter(metaData["delay"]) { + switches*.off() + } + } + + //By Model Checker + def setValue(LinkedHashMap eventDataMap) { + switches[eventDataMap["deviceId"]].setValue(eventDataMap["value"]) + sendEvent(eventDataMap) + } + + + def currentValue(String deviceFeature) { + if (deviceNumbers == 1) + switches[0].currentValue(deviceFeature) + else + switches*.currentValue(deviceFeature) + } + + def latestValue(String deviceFeature) { + if (deviceNumbers == 1) + switches[0].latestValue(deviceFeature) + else + switches*.latestValue(deviceFeature) + } + + def getAt(int ix) { + switches[ix] + } +} diff --git a/Switch/switches.groovy b/Switch/switches.groovy deleted file mode 100644 index 34ebe8e..0000000 --- a/Switch/switches.groovy +++ /dev/null @@ -1,58 +0,0 @@ -//Create a class for switch device -package Switch - -public class switches { - private int id = 0 - private String displayName - private String currentSwitch - private String switchLatestValue - def Timers - - switches(int id, String displayName, String currentSwitch, String switchLatestValue) { - this.Timers = new Timer() - this.id = id - this.displayName = displayName - this.currentSwitch = currentSwitch - this.switchLatestValue = switchLatestValue - } - - def on() { - println("the switch with id:$id is on!") - this.switchLatestValue = this.currentSwitch - this.currentSwitch = "on" - } - - def on(LinkedHashMap LHM) { - def task = Timers.runAfter(LHM["delay"]) { - println("the switch with id:$id is on!") - this.switchLatestValue = this.currentSwitch - this.currentSwitch = "on" - } - } - - def off() { - println("the switch with id:$id is off!") - this.switchLatestValue = this.currentSwitch - this.currentSwitch = "off" - } - - def off(LinkedHashMap LHM) { - def task = Timers.runAfter(LHM["delay"]) { - println("the switch with id:$id is off!") - this.switchLatestValue = this.currentSwitch - this.currentSwitch = "off" - } - } - - def currentValue(String S) { - if (S == "switch") { - return currentSwitch - } - } - - def latestValue(String S) { - if (S == "switch") { - return switchLatestValue - } - } -} diff --git a/Switch/switching.groovy b/Switch/switching.groovy deleted file mode 100644 index bc74af6..0000000 --- a/Switch/switching.groovy +++ /dev/null @@ -1,80 +0,0 @@ -//Create a class for switch device -package Switch - -public class switching{ - List switches - int count - def Timers - - switching(int count) { - this.Timers = new Timer() - this.count = count - if (count == 1) { - switches = [new switches(0, "switch0", "off", "off")] - } else if (count == 2) { - switches = [new switches(0, "switch0", "off", "off"),new switches(1, "switch1", "off", "off")] - } else if (count == 3) { - switches = [new switches(0, "switch0", "off", "off"),new switches(1, "switch1", "off", "off"),new switches(2, "switch2", "off", "off")] - } - } - - def on() { - if (count == 1) { - switches[0].on() - } else { - switches*.on() - } - } - - def on(LinkedHashMap LHM) { - if (count == 1) { - def task = Timers.runAfter(LHM["delay"]) { - switches[0].on() - } - } else { - def task = Timers.runAfter(LHM["delay"]) { - switches*.on() - } - } - } - - def off() { - if (count == 1) { - switches[0].off() - } else { - switches*.off() - } - } - - def off(LinkedHashMap LHM) { - if (count == 1) { - def task = Timers.runAfter(LHM["delay"]) { - switches[0].off() - } - } else { - def task = Timers.runAfter(LHM["delay"]) { - switches*.off() - } - } - } - - def currentValue(String S) { - if (count == 1) { - switches[0].currentValue(S) - } else { - switches*.currentValue(S) - } - } - - def latestValue(String S) { - if (count == 1) { - switches[0].latestValue(S) - } else { - switches*.latestValue(S) - } - } - - def getAt(int ix) { - switches[ix] - } -} diff --git a/appTouch/Touch.groovy b/appTouch/Touch.groovy deleted file mode 100644 index 19e6816..0000000 --- a/appTouch/Touch.groovy +++ /dev/null @@ -1,10 +0,0 @@ -//Create a class for touch -package appTouch - -public class Touch { - private int touched = 0 - - Touch(int touched) { - this.touched = touched - } -} diff --git a/appTouch/Touched.groovy b/appTouch/Touched.groovy new file mode 100644 index 0000000..e2172cc --- /dev/null +++ b/appTouch/Touched.groovy @@ -0,0 +1,19 @@ +//Create a class for Touch sensor +package appTouch + +public class Touched{ + def fun + private int isTouched + + Touched(Closure sendEvent, int isTouched) { + fun = sendEvent + this.isTouched = isTouched + } + + //By Model Checker + def setValue(LinkedHashMap eventDataMap) { + println("The application is Touched!") + this.isTouched = 1 //Do we need this? + fun(eventDataMap) + } +} diff --git a/main.groovy b/main.groovy index fb7fadd..ac7aa3b 100644 --- a/main.groovy +++ b/main.groovy @@ -3,51 +3,54 @@ 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(3) +@Field def switchesoff = new Switching(sendEvent, 1) //Global Object for class switch! -@Field def switcheson = new switching(3) +@Field def switcheson = new Switching(sendEvent, 1) //Global Object for class lock! -@Field def lock1 = new locking(2) +@Field def lock1 = new Locking(sendEvent, 2) //Global variable for mode! -@Field def newMode = "home" +@Field def newMode = "away" //Global variable for number! @Field def waitfor = 10 //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() -- 2.34.1