From 285570e387d51040a4c14ac604a6cc6ea68e0f9d Mon Sep 17 00:00:00 2001 From: amiraj Date: Mon, 8 Jul 2019 16:31:56 -0700 Subject: [PATCH] Commit #6 --- ContactSensor/Contacting.groovy | 8 +- ContactSensor/Contacts.groovy | 1 - Extractor/App1.groovy | 116 ++++ Extractor/{App.groovy => App2.groovy} | 0 Extractor/ExtractorScript.py | 382 ++++++----- Extractor/extractedObjects.groovy | 16 - Extractor/extractedObjectsApp1.groovy | 24 + Extractor/extractedObjectsApp2.groovy | 16 + .../extractedObjectsConstructorApp1.groovy | 2 + .../extractedObjectsConstructorApp2.groovy | 3 + Extractor/outGlobal.groovy | 6 + GlobalVariables/GlobalVariables.groovy | 25 - .../GlobalVariablesBothApps.groovy | 9 + GlobalVariables/GlobalVariablesEachApp.groovy | 18 + Lock/Locking.groovy | 1 - Lock/Locks.groovy | 5 +- Methods/definition.groovy | 5 - Methods/eventHandler.groovy | 2 +- Methods/preferences.groovy | 4 - Runner.py | 140 +++- Switch/Switches.groovy | 2 +- Switch/Switching.groovy | 1 - appTouch/Touched.groovy | 6 +- main.groovy | 605 ++++++++++++------ 24 files changed, 934 insertions(+), 463 deletions(-) create mode 100644 Extractor/App1.groovy rename Extractor/{App.groovy => App2.groovy} (100%) delete mode 100644 Extractor/extractedObjects.groovy create mode 100644 Extractor/extractedObjectsApp1.groovy create mode 100644 Extractor/extractedObjectsApp2.groovy create mode 100644 Extractor/extractedObjectsConstructorApp1.groovy create mode 100644 Extractor/extractedObjectsConstructorApp2.groovy create mode 100644 Extractor/outGlobal.groovy delete mode 100644 GlobalVariables/GlobalVariables.groovy create mode 100644 GlobalVariables/GlobalVariablesBothApps.groovy create mode 100644 GlobalVariables/GlobalVariablesEachApp.groovy delete mode 100644 Methods/definition.groovy delete mode 100644 Methods/preferences.groovy diff --git a/ContactSensor/Contacting.groovy b/ContactSensor/Contacting.groovy index 022b44a..9b293e8 100644 --- a/ContactSensor/Contacting.groovy +++ b/ContactSensor/Contacting.groovy @@ -11,12 +11,12 @@ public class Contacting{ this.deviceNumbers = deviceNumbers this.contacts = [] if (deviceNumbers == 1) { - contacts = [new Contacts(sendEvent, 0, "contact0", "closed", "closed")] + contacts = [new Contacts(0, "contact0", "closed", "closed")] } else if (deviceNumbers == 2) { - contacts = [new Contacts(sendEvent, 0, "contact0", "closed", "closed"), new Contacts(sendEvent, 1, "contact1", "closed", "closed")] + contacts = [new Contacts(0, "contact0", "closed", "closed"), new Contacts(1, "contact1", "closed", "closed")] } else if (deviceNumbers == 3) { - contacts = [new Contacts(sendEvent, 0, "contact0", "closed", "closed"), new Contacts(sendEvent, 1, "contact1", "closed", "closed") - ,new Contacts(sendEvent, 2, "contact2", "closed", "closed")] + contacts = [new Contacts(0, "contact0", "closed", "closed"), new Contacts(1, "contact1", "closed", "closed") + ,new Contacts(2, "contact2", "closed", "closed")] } } diff --git a/ContactSensor/Contacts.groovy b/ContactSensor/Contacts.groovy index c922a9e..90bd008 100644 --- a/ContactSensor/Contacts.groovy +++ b/ContactSensor/Contacts.groovy @@ -32,4 +32,3 @@ public class Contacts { } } } - diff --git a/Extractor/App1.groovy b/Extractor/App1.groovy new file mode 100644 index 0000000..1c2c55f --- /dev/null +++ b/Extractor/App1.groovy @@ -0,0 +1,116 @@ +////////// +definition( + name: "Enhanced Auto Lock Door", + namespace: "Lock Auto Super Enhanced", + author: "Arnaud", + description: "Automatically locks a specific door after X minutes when closed and unlocks it when open after X seconds.", + category: "Safety & Security", + iconUrl: "http://www.gharexpert.com/mid/4142010105208.jpg", + iconX2Url: "http://www.gharexpert.com/mid/4142010105208.jpg" +) + +preferences{ + section("Select the door lock:") { + input "lock1", "capability.lock", required: true + } + section("Select the door contact sensor:") { + input "contact", "capability.contactSensor", required: true + } + section("Automatically lock the door when closed...") { + input "minutesLater", "number", title: "Delay (in minutes):", required: true + } + section("Automatically unlock the door when open...") { + input "secondsLater", "number", title: "Delay (in seconds):", required: true + } + section( "Notifications" ) { + input("recipients", "contact", title: "Send notifications to", required: false) { + input "phoneNumber", "phone", title: "Warn with text message (optional)", description: "Phone Number", required: false + } + } +} + +def installed(){ + initialize() +} + +def updated(){ + unsubscribe() + unschedule() + 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!") + } +} + +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) + } + } + 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. + } + 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!") + } + } +} diff --git a/Extractor/App.groovy b/Extractor/App2.groovy similarity index 100% rename from Extractor/App.groovy rename to Extractor/App2.groovy diff --git a/Extractor/ExtractorScript.py b/Extractor/ExtractorScript.py index 399651b..e925f5f 100644 --- a/Extractor/ExtractorScript.py +++ b/Extractor/ExtractorScript.py @@ -32,188 +32,222 @@ def GetToken(f): if not c: return "EOF" +#For both apps +outGlobal = open("Extractor/outGlobal.groovy", "w+") +lockIsSet = 0 +ContactIsSet = 0 +SwitchIsSet = 0 + + +def ExtractorFunc(F, outApp, outConstructorApp, Temp): + global outGlobal + global lockIsSet + global ContactIsSet + global SwitchIsSet + + while (Temp != "EOF"): + #Extract the global objects for input + if (Temp == "input"): + Object = "" + Type = "" + Temp = GetToken(F) #name or "name" + #input name: "name", type: "type",... + if (Temp == "name"): + Temp = GetToken(F) #"name" + Object = Temp + GetToken(F) #type + Temp = GetToken(F) #"type" + Type = Temp + #input "name", "type",... + else: + Object = Temp + Temp = GetToken(F) #"type" + Type = Temp + Temp = GetToken(F) + Title = "" + Required = "" + Multiple = "" + while (Temp != "input" and Temp != "}"): + if (Temp == "title"): + Temp = GetToken(F) + Title = Temp + elif (Temp == "required"): + Temp = GetToken(F) + Required = Temp + elif (Temp == "multiple"): + Temp = GetToken(F) + Multiple = Temp + Temp = GetToken(F) + if (Type == "capability.lock"): + if (Title != ""): + print(Object+", "+Title) + if (Multiple != "" and Multiple == "true" and lockIsSet != 1): + lockIsSet = 1 + g = raw_input("Enter the number of locks to control: (1, 2, or 3)\n") + outGlobal.write("//Global Object for class lock!\n") + outGlobal.write("@Field def lockObject = new Locking(sendEvent, ") + outGlobal.write("%s)\n" % g) + elif ((Multiple == "" or Multiple == "false") and lockIsSet != 1): + lockIsSet = 1 + outGlobal.write("//Global Object for class lock!\n") + outGlobal.write("@Field def lockObject = new Locking(sendEvent, 1)\n") + outApp.write("//Object for class lock!\n") + outApp.write("def %s\n" % Object) + outConstructorApp.write("%s = obj.lockObject\n" % Object) + #elif (Type == "capability.alarm"): + + #elif (Type == "capability.battery"): + + #elif (Type == "capability.beacon"): + + #elif (Type == "capability.carbonMonoxideDetector"): + + #elif (Type == "capability.colorControl"): + + elif (Type == "capability.contactSensor"): + if (Title != ""): + print(Object+", "+Title) + if (Multiple != "" and Multiple == "true" and ContactIsSet != 1): + ContactIsSet = 1 + g = raw_input("Enter the number of contact sensors to control: (1, 2, or 3)\n") + outGlobal.write("//Global Object for class contactSensor!\n") + outGlobal.write("@Field def contactObject = new Contacting(sendEvent, ") + outGlobal.write("%s)\n" % g) + elif ((Multiple == "" or Multiple == "false") and ContactIsSet != 1): + ContactIsSet = 1 + outGlobal.write("//Global Object for class contactSensor!\n") + outGlobal.write("@Field def contactObject = new Contacting(sendEvent, 1)\n") + outApp.write("//Object for class contactSensor!\n") + outApp.write("def %s\n" % Object) + outConstructorApp.write("%s = obj.contactObject\n" % Object) + #elif (Type == "capability.doorControl"): + + #elif (Type == "capability.energyMeter"): + + #elif (Type == "capability.illuminanceMeasurement"): + + #elif (Type == "capability.accelerationSensor"): + + #elif (Type == "capability.motionSensor"): + + #elif (Type == "capability.musicPlayer"): + + #elif (Type == "capability.powerMeter"): + + #elif (Type == "capability.presenceSensor"): + + #elif (Type == "capability.relativeHumidityMeasurement"): + + #elif (Type == "capability.relaySwitch"): + + #elif (Type == "capability.sleepSensor"): + + #elif (Type == "capability.smokeDetector"): + + #elif (Type == "capability.stepSensor"): + + elif (Type == "capability.switch"): + if (Title != ""): + print(Object+", "+Title) + if (Multiple != "" and Multiple == "true" and SwitchIsSet != 1): + SwitchIsSet = 1 + g = raw_input("Enter the number of switches to control: (1, 2, or 3)\n") + outGlobal.write("//Global Object for class Switch!\n") + outGlobal.write("@Field def switchObject = new Switching(sendEvent, ") + outGlobal.write("%s)\n" % g) + elif ((Multiple == "" or Multiple == "false") and SwitchIsSet != 1): + SwitchIsSet = 1 + outGlobal.write("//Global Object for class Switch!\n") + outGlobal.write("@Field def switchObject = new Switching(sendEvent, 1)\n") + outApp.write("//Object for class Switch!\n") + outApp.write("def %s\n" % Object) + outConstructorApp.write("%s = obj.switchObject\n" % Object) + #elif (Type == "capability.switchLevel"): + + #elif (Type == "capability.temperatureMeasurement"): - - + #elif (Type == "capability.thermostat"): + + #elif (Type == "capability.valve"): -F = open("Extractor/App.groovy", "r") -Out = open("Extractor/extractedObjects.groovy", "w+") -Temp = GetToken(F) + #elif (Type == "capability.waterSensor"): -Objects = [] -Functions = [] + #elif (Type == "capability.touchSensor"): - -while (Temp != "EOF"): - #Extract the global objects for input - if (Temp == "input"): - Object = "" - Type = "" - Temp = GetToken(F) #name or "name" - #input name: "name", type: "type",... - if (Temp == "name"): - Temp = GetToken(F) #"name" - Object = Temp - GetToken(F) #type - Temp = GetToken(F) #"type" - Type = Temp - #input "name", "type",... - else: - Object = Temp - Temp = GetToken(F) #"type" - Type = Temp - Temp = GetToken(F) - Title = "" - Required = "" - Multiple = "" - while (Temp != "input" and Temp != "}"): - if (Temp == "title"): - Temp = GetToken(F) - Title = Temp - elif (Temp == "required"): - Temp = GetToken(F) - Required = Temp - elif (Temp == "multiple"): - Temp = GetToken(F) - Multiple = Temp - Temp = GetToken(F) - if (Type == "capability.lock"): - if (Title != ""): - print(Object+", "+Title) - 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(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(sendEvent, 1)\n" % Object) - #elif (Type == "capability.alarm"): - - #elif (Type == "capability.battery"): - - #elif (Type == "capability.beacon"): - - #elif (Type == "capability.carbonMonoxideDetector"): - - #elif (Type == "capability.colorControl"): - - elif (Type == "capability.contactSensor"): - if (Title != ""): - print(Object+", "+Title) - 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(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(sendEvent, 1)\n" % Object) - #elif (Type == "capability.doorControl"): - - #elif (Type == "capability.energyMeter"): - - #elif (Type == "capability.illuminanceMeasurement"): - - #elif (Type == "capability.accelerationSensor"): - - #elif (Type == "capability.motionSensor"): - - #elif (Type == "capability.musicPlayer"): - - #elif (Type == "capability.powerMeter"): - - #elif (Type == "capability.presenceSensor"): - - #elif (Type == "capability.relativeHumidityMeasurement"): - - #elif (Type == "capability.relaySwitch"): - - #elif (Type == "capability.sleepSensor"): - - #elif (Type == "capability.smokeDetector"): - - #elif (Type == "capability.stepSensor"): - - elif (Type == "capability.switch"): - if (Title != ""): - print(Object+", "+Title) - 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(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(sendEvent, 1)\n" % Object) - #elif (Type == "capability.switchLevel"): - - #elif (Type == "capability.temperatureMeasurement"): - - #elif (Type == "capability.thermostat"): - - #elif (Type == "capability.valve"): + #elif (Type == "capability.imageCapture"): - #elif (Type == "capability.waterSensor"): + #elif (Type == "device.mobilePresence"): - #elif (Type == "capability.touchSensor"): + #elif (Type == "device.aeonKeyFob"): - #elif (Type == "capability.imageCapture"): + elif (Type == "mode"): + if (Title != ""): + print(Object+", "+Title) + g = raw_input("Enter the mode: ") + outApp.write("//Global variable for mode!\n") + outApp.write("def %s = " % Object) + outApp.write("\"%s\"\n" % g) + #elif (Type == "decimal"): - #elif (Type == "device.mobilePresence"): + #elif (Type == "text"): + + elif (Type == "number"): + if (Title != ""): + print(Object+", "+Title) + g = raw_input("Enter the number: ") + outApp.write("//Global variable for number!\n") + outApp.write("def %s = " % Object) + outApp.write("%s\n" % g) + #elif (Type == "time"): + + #elif (Type == "enum"): + + #elif (Type == "bool"): + + elif (Type == "phone"): + if (Title != ""): + print(Object+", "+Title) + g = raw_input("Enter the number to send notification to:\n") + outApp.write("//Global variable for phone number!\n") + outApp.write("def %s = " % Object) + outApp.write("%s\n" % g) + elif (Type == "contact"): + if (Title != ""): + print(Object+", "+Title) + g = raw_input("Enter the name of the recipients:\n") + outApp.write("//Global variable for recipients!\n") + g = g.split() + outApp.write("def %s = " % Object) + outApp.write("%s\n" % g) + #Extract the global object for functions + elif (Temp == "def"): + Temp = GetToken(F) + NameofFunc = Temp + if (GetToken(F) != "="): #We have a function to create object for + outApp.write("//Global Object for functions in subscribe method!\n") + outApp.write("def %s = this.&" % NameofFunc) + outApp.write("%s\n" % NameofFunc) + if (Temp != "input"): + Temp = GetToken(F) + + F.close() + outApp.close() + outConstructorApp.close() + +#For app1 +F = open("Extractor/App1.groovy", "r") +outApp = open("Extractor/extractedObjectsApp1.groovy", "w+") +outConstructorApp = open("Extractor/extractedObjectsConstructorApp1.groovy", "w+") +Temp = GetToken(F) - #elif (Type == "device.aeonKeyFob"): +ExtractorFunc(F, outApp, outConstructorApp, Temp) - elif (Type == "mode"): - if (Title != ""): - print(Object+", "+Title) - g = raw_input("Enter the mode: ") - Out.write("//Global variable for mode!\n") - Out.write("@Field def %s = " % Object) - Out.write("\"%s\"\n" % g) - #elif (Type == "decimal"): +#For app2 +F = open("Extractor/App2.groovy", "r") +outApp = open("Extractor/extractedObjectsApp2.groovy", "w+") +outConstructorApp = open("Extractor/extractedObjectsConstructorApp2.groovy", "w+") +Temp = GetToken(F) - #elif (Type == "text"): - - elif (Type == "number"): - if (Title != ""): - print(Object+", "+Title) - g = raw_input("Enter the number: ") - Out.write("//Global variable for number!\n") - Out.write("@Field def %s = " % Object) - Out.write("%s\n" % g) - #elif (Type == "time"): - - #elif (Type == "enum"): - - #elif (Type == "bool"): - - elif (Type == "phone"): - if (Title != ""): - print(Object+", "+Title) - g = raw_input("Enter the number to send notification to:\n") - Out.write("//Global variable for phone number!\n") - Out.write("@Field def %s = " % Object) - Out.write("%s\n" % g) - elif (Type == "contact"): - if (Title != ""): - print(Object+", "+Title) - g = raw_input("Enter the name of the recipients:\n") - Out.write("//Global variable for recipients!\n") - g = g.split() - Out.write("@Field def %s = " % Object) - Out.write("%s\n" % g) - #Extract the global object for functions - elif (Temp == "def"): - Temp = GetToken(F) - NameofFunc = Temp - if (GetToken(F) != "="): #We have a function to create object for - Out.write("//Global Object for functions in subscribe method!\n") - Out.write("@Field def %s = this.&" % NameofFunc) - Out.write("%s\n" % NameofFunc) - if (Temp != "input"): - Temp = GetToken(F) - - -F.close() -Out.close() +ExtractorFunc(F, outApp, outConstructorApp, Temp) + +outGlobal.close() diff --git a/Extractor/extractedObjects.groovy b/Extractor/extractedObjects.groovy deleted file mode 100644 index c978614..0000000 --- a/Extractor/extractedObjects.groovy +++ /dev/null @@ -1,16 +0,0 @@ -//Global Object for class switch! -@Field def switchesoff = new Switching(sendEvent, 1) -//Global Object for class switch! -@Field def switcheson = new Switching(sendEvent, 1) -//Global Object for class lock! -@Field def lock1 = new Locking(sendEvent, 1) -//Global variable for mode! -@Field def newMode = "away" -//Global variable for number! -@Field def waitfor = 10 -//Global Object for functions in subscribe method! -@Field def installed = this.&installed -//Global Object for functions in subscribe method! -@Field def updated = this.&updated -//Global Object for functions in subscribe method! -@Field def appTouch = this.&appTouch diff --git a/Extractor/extractedObjectsApp1.groovy b/Extractor/extractedObjectsApp1.groovy new file mode 100644 index 0000000..c3e09ad --- /dev/null +++ b/Extractor/extractedObjectsApp1.groovy @@ -0,0 +1,24 @@ +//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 = 2 +//Global variable for recipients! +def recipients = ['AJ'] +//Global variable for phone number! +def phoneNumber = 9495379373 +//Global Object for functions in subscribe method! +def installed = this.&installed +//Global Object for functions in subscribe method! +def updated = this.&updated +//Global Object for functions in subscribe method! +def initialize = this.&initialize +//Global Object for functions in subscribe method! +def lockDoor = this.&lockDoor +//Global Object for functions in subscribe method! +def unlockDoor = this.&unlockDoor +//Global Object for functions in subscribe method! +def doorHandler = this.&doorHandler diff --git a/Extractor/extractedObjectsApp2.groovy b/Extractor/extractedObjectsApp2.groovy new file mode 100644 index 0000000..4363a18 --- /dev/null +++ b/Extractor/extractedObjectsApp2.groovy @@ -0,0 +1,16 @@ +//Object for class Switch! +def switchesoff +//Object for class Switch! +def switcheson +//Object for class lock! +def lock1 +//Global variable for mode! +def newMode = "away" +//Global variable for number! +def waitfor = 10 +//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 diff --git a/Extractor/extractedObjectsConstructorApp1.groovy b/Extractor/extractedObjectsConstructorApp1.groovy new file mode 100644 index 0000000..2e3aa8e --- /dev/null +++ b/Extractor/extractedObjectsConstructorApp1.groovy @@ -0,0 +1,2 @@ +lock1 = obj.lockObject +contact = obj.contactObject diff --git a/Extractor/extractedObjectsConstructorApp2.groovy b/Extractor/extractedObjectsConstructorApp2.groovy new file mode 100644 index 0000000..709a386 --- /dev/null +++ b/Extractor/extractedObjectsConstructorApp2.groovy @@ -0,0 +1,3 @@ +switchesoff = obj.switchObject +switcheson = obj.switchObject +lock1 = obj.lockObject diff --git a/Extractor/outGlobal.groovy b/Extractor/outGlobal.groovy new file mode 100644 index 0000000..b22c362 --- /dev/null +++ b/Extractor/outGlobal.groovy @@ -0,0 +1,6 @@ +//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, 2) diff --git a/GlobalVariables/GlobalVariables.groovy b/GlobalVariables/GlobalVariables.groovy deleted file mode 100644 index 5207e4f..0000000 --- a/GlobalVariables/GlobalVariables.groovy +++ /dev/null @@ -1,25 +0,0 @@ -//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() -//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 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 timersFuncList = [] -//Create a global list for timer schedulers -@Field def timersList = [] -//Create a global list for events -@Field def evt = [] - diff --git a/GlobalVariables/GlobalVariablesBothApps.groovy b/GlobalVariables/GlobalVariablesBothApps.groovy new file mode 100644 index 0000000..dea126a --- /dev/null +++ b/GlobalVariables/GlobalVariablesBothApps.groovy @@ -0,0 +1,9 @@ +//Create a global variable for send event +@Field def sendEvent = {eventDataMap -> + app1.eventHandler(eventDataMap) + app2.eventHandler(eventDataMap) + } +//Object for location +@Field def locationObject = new LocationVar() +//Object for touch +@Field def appObject = new Touched(sendEvent, 0) diff --git a/GlobalVariables/GlobalVariablesEachApp.groovy b/GlobalVariables/GlobalVariablesEachApp.groovy new file mode 100644 index 0000000..7d44e0c --- /dev/null +++ b/GlobalVariables/GlobalVariablesEachApp.groovy @@ -0,0 +1,18 @@ +//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 +def log = new Logger() +//Create a global variable for Functions in Subscribe method +def functionList = [] +//Create a global variable for Objects in Subscribe method +def objectList = [] +//Create a global variable for Events in Subscribe method +def eventList = [] +//Create a global list for function schedulers +def timersFuncList = [] +//Create a global list for timer schedulers +def timersList = [] +//Create a global list for events +def evt = [] diff --git a/Lock/Locking.groovy b/Lock/Locking.groovy index 9d43ae0..f55a239 100644 --- a/Lock/Locking.groovy +++ b/Lock/Locking.groovy @@ -10,7 +10,6 @@ public class Locking{ Locking(Closure sendEvent, int deviceNumbers) { this.sendEvent = sendEvent this.timers = new Timer() - timers.cancel() //Timer is ready to use this.deviceNumbers = deviceNumbers this.locks = [] if (deviceNumbers == 1) { diff --git a/Lock/Locks.groovy b/Lock/Locks.groovy index 42e0fc4..d5f97ae 100644 --- a/Lock/Locks.groovy +++ b/Lock/Locks.groovy @@ -17,7 +17,6 @@ public class Locks { this.lockCurrentValue = lockCurrentValue this.lockLatestValue = lockLatestValue this.timers = new Timer() - timers.cancel() //Timer is ready to use } //By Apps @@ -43,7 +42,7 @@ public class Locks { println("the door with id:$id is unlocked!") this.lockLatestValue = this.lockCurrentValue this.lockCurrentValue = "unlocked" - sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "", + sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } @@ -52,7 +51,7 @@ public class Locks { println("the door with id:$id is locked!") this.lockLatestValue = this.lockCurrentValue this.lockCurrentValue = "locked" - sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "", + sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } } diff --git a/Methods/definition.groovy b/Methods/definition.groovy deleted file mode 100644 index aac9c53..0000000 --- a/Methods/definition.groovy +++ /dev/null @@ -1,5 +0,0 @@ -///////////////////////////////////////////////////////////////////// -def definition(LinkedHashMap metaData) { - println("IGNORE -- JUST SOME DEFINITION") -} - diff --git a/Methods/eventHandler.groovy b/Methods/eventHandler.groovy index f16bdce..eee7a25 100644 --- a/Methods/eventHandler.groovy +++ b/Methods/eventHandler.groovy @@ -11,7 +11,7 @@ def eventHandler(LinkedHashMap eventDataMap) { def data = eventDataMap["data"] for (int i = 0;i < eventList.size();i++) { - if (eventList[i] == value) { + if (eventList[i] == name) { evt.add(new Event()) evt[-1].value = value evt[-1].name = name diff --git a/Methods/preferences.groovy b/Methods/preferences.groovy deleted file mode 100644 index ac81c8e..0000000 --- a/Methods/preferences.groovy +++ /dev/null @@ -1,4 +0,0 @@ -///////////////////////////////////////////////////////////////////// -def preferences(Closure metaData) { - println("IGNORE -- JUST SOME DEFINITION") -} diff --git a/Runner.py b/Runner.py index 9aab628..2d32581 100644 --- a/Runner.py +++ b/Runner.py @@ -1,10 +1,15 @@ import os #Create directory for files to append in the main file + +#Files for both Apps Out = open("main.groovy", "w+") -GlobalVariables = open("GlobalVariables/"+"GlobalVariables.groovy", "r") -definition = open("Methods/"+"definition.groovy", "r") -preferences = open("Methods/"+"preferences.groovy", "r") +GlobalVariablesBothApps = open("GlobalVariables/"+"GlobalVariablesBothApps.groovy", "r") +outGlobal = open("Extractor/"+"outGlobal.groovy", "r") + + +#For App1 +GlobalVariablesEachApp = open("GlobalVariables/"+"GlobalVariablesEachApp.groovy", "r") setLocationMode = open("Methods/"+"setLocationMode.groovy", "r") subscribe = open("Methods/"+"subscribe.groovy", "r") runIn = open("Methods/"+"runIn.groovy", "r") @@ -12,8 +17,9 @@ 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") +App1 = open("Extractor/"+"App1.groovy", "r") +extractedObjectsApp1 = open("Extractor/"+"extractedObjectsApp1.groovy", "r") +extractedObjectsConstructorApp1 = open("Extractor/"+"extractedObjectsConstructorApp1.groovy", "r") #Extract information from preferences and subscribe method to create required objects @@ -36,38 +42,124 @@ Out.write("import Location.Phrase\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("//GlobalVariables for both Apps\n") +for line in GlobalVariablesBothApps: Out.write(line) Out.write("\n") -Out.write("//extractedObjects\n") -for line in extractedObjects: +Out.write("//Extracted global objects for both Apps\n") +for line in outGlobal: Out.write(line) Out.write("\n") -Out.write("//Methods\n") -for line in definition: - Out.write(line) -for line in preferences: - Out.write(line) +Out.write("//Application #1\n") +Out.write("class App1 {\n") +Out.write("\tdef reference\n") +Out.write("\tdef location\n") +Out.write("\tdef app\n") +Out.write("\n") +Out.write("\t//Extracted objects for App1\n") +for line in extractedObjectsApp1: + Out.write("\t"+line) +Out.write("\n") +Out.write("\tApp1(Object obj) {\n") +Out.write("\t\treference = obj\n") +Out.write("\t\tlocation = obj.locationObject\n") +Out.write("\t\tapp = obj.appObject\n") +for line in extractedObjectsConstructorApp1: + Out.write("\t\t"+line) +Out.write("\t}\n") +Out.write("\t//Global variables for each app\n") +for line in GlobalVariablesEachApp: + Out.write("\t"+line) +Out.write("\n") +Out.write("\t//Methods\n") for line in setLocationMode: - Out.write(line) + Out.write("\t"+line) for line in subscribe: - Out.write(line) + Out.write("\t"+line) for line in runIn: - Out.write(line) + Out.write("\t"+line) for line in unschedule: - Out.write(line) + Out.write("\t"+line) for line in sendNotificationToContacts: - Out.write(line) + Out.write("\t"+line) for line in sendSms: - Out.write(line) + Out.write("\t"+line) for line in eventHandler: - Out.write(line) + Out.write("\t"+line) Out.write("\n") -for line in App: - Out.write(line) +Start = 0 +for line in App1: + if ("def " in line): + Start = 1 + if (Start): + Out.write("\t"+line) +Out.write("}\n") +Out.write("\n") +Out.write("\n") +GlobalVariablesEachApp = open("GlobalVariables/"+"GlobalVariablesEachApp.groovy", "r") + +#For App2 +GlobalVariablesEachApp = open("GlobalVariables/"+"GlobalVariablesEachApp.groovy", "r") +setLocationMode = open("Methods/"+"setLocationMode.groovy", "r") +subscribe = open("Methods/"+"subscribe.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") +App2 = open("Extractor/"+"App2.groovy", "r") +extractedObjectsApp2 = open("Extractor/"+"extractedObjectsApp2.groovy", "r") +extractedObjectsConstructorApp2 = open("Extractor/"+"extractedObjectsConstructorApp2.groovy", "r") + +Out.write("//Application #2\n") +Out.write("class App2 {\n") +Out.write("\tdef reference\n") +Out.write("\tdef location\n") +Out.write("\tdef app\n") +Out.write("\n") +Out.write("\t//Extracted objects for App2\n") +for line in extractedObjectsApp2: + Out.write("\t"+line) +Out.write("\n") +Out.write("\tApp2(Object obj) {\n") +Out.write("\t\treference = obj\n") +Out.write("\t\tlocation = obj.locationObject\n") +Out.write("\t\tapp = obj.appObject\n") +for line in extractedObjectsConstructorApp2: + Out.write("\t\t"+line) +Out.write("\t}\n") +Out.write("\t//Global variables for each app\n") +for line in GlobalVariablesEachApp: + Out.write("\t"+line) +Out.write("\n") +Out.write("\t//Methods\n") +for line in setLocationMode: + Out.write("\t"+line) +for line in subscribe: + Out.write("\t"+line) +for line in runIn: + Out.write("\t"+line) +for line in unschedule: + Out.write("\t"+line) +for line in sendNotificationToContacts: + Out.write("\t"+line) +for line in sendSms: + Out.write("\t"+line) +for line in eventHandler: + Out.write("\t"+line) +Out.write("\n") +Start = 0 +for line in App2: + if ("def " in line): + Start = 1 + if (Start): + Out.write("\t"+line) +Out.write("}\n") Out.write("\n") -Out.write("installed()\n") +Out.write("@Field def app1 = new App1(this)\n") +Out.write("@Field def app2 = new App2(this)\n") +Out.write("app1.installed()\n") +Out.write("app2.installed()\n") Out.close() diff --git a/Switch/Switches.groovy b/Switch/Switches.groovy index 65cac12..f5e59e5 100644 --- a/Switch/Switches.groovy +++ b/Switch/Switches.groovy @@ -13,7 +13,7 @@ public class Switches { Switches(Closure sendEvent, int id, String displayName, String switchCurrentValue, String switchLatestValue) { this.sendEvent = sendEvent this.timers = new Timer() - timers.cancel() //Timer is ready to use + this.id = id this.displayName = displayName this.switchCurrentValue = switchCurrentValue diff --git a/Switch/Switching.groovy b/Switch/Switching.groovy index 1b11850..f65bd41 100644 --- a/Switch/Switching.groovy +++ b/Switch/Switching.groovy @@ -10,7 +10,6 @@ public class Switching{ Switching(Closure sendEvent, int deviceNumbers) { this.sendEvent = sendEvent this.timers = new Timer() - timers.cancel() //Timer is ready to use this.deviceNumbers = deviceNumbers this.switches = [] if (deviceNumbers == 1) { diff --git a/appTouch/Touched.groovy b/appTouch/Touched.groovy index e2172cc..530d64b 100644 --- a/appTouch/Touched.groovy +++ b/appTouch/Touched.groovy @@ -2,11 +2,11 @@ package appTouch public class Touched{ - def fun + def sendEvent private int isTouched Touched(Closure sendEvent, int isTouched) { - fun = sendEvent + this.sendEvent = sendEvent this.isTouched = isTouched } @@ -14,6 +14,6 @@ public class Touched{ def setValue(LinkedHashMap eventDataMap) { println("The application is Touched!") this.isTouched = 1 //Do we need this? - fun(eventDataMap) + sendEvent(eventDataMap) } } diff --git a/main.groovy b/main.groovy index 2e249bd..299e6e4 100644 --- a/main.groovy +++ b/main.groovy @@ -15,226 +15,431 @@ import Location.Phrase import appTouch.Touched import Event.Event -//GlobalVariables +//GlobalVariables for both Apps //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() -//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 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 timersFuncList = [] -//Create a global list for timer schedulers -@Field def timersList = [] -//Create a global list for events -@Field def evt = [] - +@Field def sendEvent = {eventDataMap -> + app1.eventHandler(eventDataMap) + app2.eventHandler(eventDataMap) + } +//Object for location +@Field def locationObject = new LocationVar() +//Object for touch +@Field def appObject = new Touched(sendEvent, 0) -//extractedObjects -//Global Object for class switch! -@Field def switchesoff = new Switching(sendEvent, 1) -//Global Object for class switch! -@Field def switcheson = new Switching(sendEvent, 1) +//Extracted global objects for both Apps //Global Object for class lock! -@Field def lock1 = new Locking(sendEvent, 1) -//Global variable for mode! -@Field def newMode = "away" -//Global variable for number! -@Field def waitfor = 10 -//Global Object for functions in subscribe method! -@Field def installed = this.&installed -//Global Object for functions in subscribe method! -@Field def updated = this.&updated -//Global Object for functions in subscribe method! -@Field def appTouch = this.&appTouch +@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, 2) -//Methods -///////////////////////////////////////////////////////////////////// -def definition(LinkedHashMap metaData) { - println("IGNORE -- JUST SOME DEFINITION") -} +//Application #1 +class App1 { + def reference + def location + def app -///////////////////////////////////////////////////////////////////// -def preferences(Closure metaData) { - println("IGNORE -- JUST SOME DEFINITION") -} -///////////////////////////////////////////////////////////////////// -def setLocationMode(String mode) { - location.mode = mode -} + //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 = 2 + //Global variable for recipients! + def recipients = ['AJ'] + //Global variable for phone number! + def phoneNumber = 9495379373 + //Global Object for functions in subscribe method! + def installed = this.&installed + //Global Object for functions in subscribe method! + def updated = this.&updated + //Global Object for functions in subscribe method! + def initialize = this.&initialize + //Global Object for functions in subscribe method! + def lockDoor = this.&lockDoor + //Global Object for functions in subscribe method! + def unlockDoor = this.&unlockDoor + //Global Object for functions in subscribe method! + def doorHandler = this.&doorHandler -///////////////////////////////////////////////////////////////////// -////subscribe(obj, func) -def subscribe(Object obj, Closure FunctionToCall) { - objectList.add(obj) - eventList.add("Touched") - functionList.add(FunctionToCall) -} -////subscribe(obj, event, func) -def subscribe(Object obj, String event, Closure FunctionToCall) { - objectList.add(obj) - eventList.add(event) - functionList.add(FunctionToCall) -} -////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 functionToCall) { - timersFuncList.add(functionToCall) - timersList.add(new Timer()) - def task = timersList[-1].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() + App1(Object obj) { + reference = obj + location = obj.locationObject + app = obj.appObject + lock1 = obj.lockObject + contact = obj.contactObject + } + //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 + def log = new Logger() + //Create a global variable for Functions in Subscribe method + def functionList = [] + //Create a global variable for Objects in Subscribe method + def objectList = [] + //Create a global variable for Events in Subscribe method + def eventList = [] + //Create a global list for function schedulers + def timersFuncList = [] + //Create a global list for timer schedulers + def timersList = [] + //Create a global list for events + def evt = [] + + //Methods + ///////////////////////////////////////////////////////////////////// + def setLocationMode(String mode) { + location.mode = mode + } + + ///////////////////////////////////////////////////////////////////// + ////subscribe(obj, func) + def subscribe(Object obj, Closure FunctionToCall) { + objectList.add(obj) + eventList.add("Touched") + functionList.add(FunctionToCall) + } + ////subscribe(obj, event, func) + def subscribe(Object obj, String event, Closure FunctionToCall) { + objectList.add(obj) + eventList.add(event) + functionList.add(FunctionToCall) + } + ////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 functionToCall) { + timersFuncList.add(functionToCall) + timersList.add(new Timer()) + def task = timersList[-1].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() + } } } -} -///////////////////////////////////////////////////////////////////// -////sendNotificationToContacts(text, 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 \""+text+"\" to "+location.phoneNumbers[j].toString()) + ///////////////////////////////////////////////////////////////////// + ////sendNotificationToContacts(text, 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 \""+text+"\" to "+location.phoneNumbers[j].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]) + ///////////////////////////////////////////////////////////////////// + ////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] == 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 + functionList[i](evt[-1]) + } } } -} -/** - * Good Night House - * - * Copyright 2014 Joseph Charette - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License - * for the specific language governing permissions and limitations under the License. - * - */ -definition( - name: "Good Night House", - namespace: "charette.joseph@gmail.com", - author: "Joseph Charette", - description: "Some on, some off with delay for bedtime, Lock The Doors", - category: "Convenience", - iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", - iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png" -/** -* Borrowed code from -* Walk Gentle Into That Good Night -* -* Author: oneaccttorulethehouse@gmail.com -* Date: 2014-02-01 - */ - ) -preferences { - section("When I touch the app turn these lights off…"){ - input "switchesoff", "capability.switch", multiple: true, required:true - } - section("When I touch the app turn these lights on…"){ - input "switcheson", "capability.switch", multiple: true, required:false - } - section("Lock theses locks...") { - input "lock1","capability.lock", multiple: true - } - section("And change to this mode...") { - input "newMode", "mode", title: "Mode?" - } - section("After so many seconds (optional)"){ - input "waitfor", "number", title: "Off after (default 120)", required: true + def installed(){ + initialize() + } + + def updated(){ + unsubscribe() + unschedule() + 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!") + } + } + + 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) + } + } + 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. + } + 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!") + } + } } } -def installed() -{ - log.debug "Installed with settings: ${settings}" - log.debug "Current mode = ${location.mode}" - subscribe(app, appTouch) -} +//Application #2 +class App2 { + def reference + def location + def app + //Extracted objects for App2 + //Object for class Switch! + def switchesoff + //Object for class Switch! + def switcheson + //Object for class lock! + def lock1 + //Global variable for mode! + def newMode = "away" + //Global variable for number! + def waitfor = 10 + //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 updated() -{ - log.debug "Updated with settings: ${settings}" - log.debug "Current mode = ${location.mode}" - unsubscribe() - subscribe(app, appTouch) -} + App2(Object obj) { + reference = obj + location = obj.locationObject + app = obj.appObject + switchesoff = obj.switchObject + switcheson = obj.switchObject + lock1 = obj.lockObject + } + //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 + def log = new Logger() + //Create a global variable for Functions in Subscribe method + def functionList = [] + //Create a global variable for Objects in Subscribe method + def objectList = [] + //Create a global variable for Events in Subscribe method + def eventList = [] + //Create a global list for function schedulers + def timersFuncList = [] + //Create a global list for timer schedulers + def timersList = [] + //Create a global list for events + def evt = [] + + //Methods + ///////////////////////////////////////////////////////////////////// + def setLocationMode(String mode) { + location.mode = mode + } + + ///////////////////////////////////////////////////////////////////// + ////subscribe(obj, func) + def subscribe(Object obj, Closure FunctionToCall) { + objectList.add(obj) + eventList.add("Touched") + functionList.add(FunctionToCall) + } + ////subscribe(obj, event, func) + def subscribe(Object obj, String event, Closure FunctionToCall) { + objectList.add(obj) + eventList.add(event) + functionList.add(FunctionToCall) + } + ////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 functionToCall) { + timersFuncList.add(functionToCall) + timersList.add(new Timer()) + def task = timersList[-1].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() + } + } + } + ///////////////////////////////////////////////////////////////////// + ////sendNotificationToContacts(text, 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 \""+text+"\" to "+location.phoneNumbers[j].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] == 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 + functionList[i](evt[-1]) + } + } + } -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) + def installed() + { + log.debug "Installed with settings: ${settings}" + log.debug "Current mode = ${location.mode}" + subscribe(app, appTouch) + } + + + 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) + } } -installed() +@Field def app1 = new App1(this) +@Field def app2 = new App2(this) +app1.installed() +app2.installed() -- 2.34.1