From: amiraj Date: Fri, 12 Jul 2019 00:24:02 +0000 (-0700) Subject: Commit #7: Events thread-based + new easier Extractor.py + our own Timer class X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=d6c9bed26d6482e0565de85fda57281899546ebb;p=smartthings-infrastructure.git Commit #7: Events thread-based + new easier Extractor.py + our own Timer class --- diff --git a/ContactSensor/Contacting.groovy b/ContactSensor/Contacting.groovy index 9b293e8..a744889 100644 --- a/ContactSensor/Contacting.groovy +++ b/ContactSensor/Contacting.groovy @@ -1,17 +1,27 @@ //Create a class for contact sensor package ContactSensor +import Timer.SimulatedTimer -public class Contacting{ +public class Contacting { private int deviceNumbers private List contacts def sendEvent + + //For one device(We cannot have obj.id)-> We should have obj[0].id + private int id = 0 + private String displayName = "contact0" + private String contactState = "closed" + private String contactLatestValue = "closed" + Contacting(Closure sendEvent, int deviceNumbers) { this.sendEvent = sendEvent this.deviceNumbers = deviceNumbers this.contacts = [] + + if (deviceNumbers == 1) { - contacts = [new Contacts(0, "contact0", "closed", "closed")] + contacts = [new Contacts(this.id, this.displayName, this.contactState, this.contactLatestValue)] } else if (deviceNumbers == 2) { contacts = [new Contacts(0, "contact0", "closed", "closed"), new Contacts(1, "contact1", "closed", "closed")] } else if (deviceNumbers == 3) { @@ -23,6 +33,9 @@ public class Contacting{ //By Model Checker def setValue(LinkedHashMap eventDataMap) { contacts[eventDataMap["deviceId"]].setValue(eventDataMap["value"]) + if (deviceNumbers == 1) + this.contactLatestValue = contacts[eventDataMap["deviceId"]].contactLatestValue + this.contactState = contacts[eventDataMap["deviceId"]].contactState sendEvent(eventDataMap) } diff --git a/ContactSensor/Contacts.groovy b/ContactSensor/Contacts.groovy index 90bd008..3796b60 100644 --- a/ContactSensor/Contacts.groovy +++ b/ContactSensor/Contacts.groovy @@ -1,28 +1,29 @@ //Create a class for contact sensor package ContactSensor +import Timer.SimulatedTimer public class Contacts { private int id private String displayName - private String contactCurrentValue + private String contactState private String contactLatestValue - Contacts(int id, String displayName, String contactCurrentValue, String contactLatestValue) { + Contacts(int id, String displayName, String contactState, String contactLatestValue) { this.id = id this.displayName = displayName - this.contactCurrentValue = contactCurrentValue + this.contactState = contactState this.contactLatestValue = contactLatestValue } def setValue(String value) { - this.contactLatestValue = contactCurrentValue + this.contactLatestValue = contactState println("the contact sensor with id:$id is triggered to $value!") - this.contactCurrentValue = value + this.contactState = value } def currentValue(String deviceFeature) { if (deviceFeature == "contact") { - return contactCurrentValue + return contactState } } diff --git a/Event/Event.groovy b/Event/Event.groovy index a1d5e71..794dbca 100644 --- a/Event/Event.groovy +++ b/Event/Event.groovy @@ -11,18 +11,18 @@ public class Event { private String descriptionText private boolean isStateChange private String unit - private LinkedHashMap data + private ArrayList data - Event() { - this.deviceId = 0 - this.linkText = "" - this.value = "" - this.displayName = "" - this.name = "" - this.descriptionText = "" - this.isStateChange = false - this.unit = "" - this.data = [] - this.displayed = false + Event(String value, String name, int deviceId, String descriptionText, boolean displayed, String linkText, String displayName, boolean isStateChange, String unit, ArrayList data) { + this.deviceId = deviceId + this.linkText = linkText + this.value = value + this.displayName = displayName + this.name = name + this.descriptionText = descriptionText + this.isStateChange = isStateChange + this.unit = unit + this.data = data + this.displayed = displayed } } diff --git a/Extractor/App1.groovy b/Extractor/App1.groovy deleted file mode 100644 index 1c2c55f..0000000 --- a/Extractor/App1.groovy +++ /dev/null @@ -1,116 +0,0 @@ -////////// -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/App1/App1.groovy b/Extractor/App1/App1.groovy new file mode 100644 index 0000000..1c2c55f --- /dev/null +++ b/Extractor/App1/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/App1/extractedFunctionsApp1.groovy b/Extractor/App1/extractedFunctionsApp1.groovy new file mode 100644 index 0000000..b7e30e6 --- /dev/null +++ b/Extractor/App1/extractedFunctionsApp1.groovy @@ -0,0 +1,12 @@ +//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/App1/extractedObjectsApp1.groovy b/Extractor/App1/extractedObjectsApp1.groovy new file mode 100644 index 0000000..be7bc9f --- /dev/null +++ b/Extractor/App1/extractedObjectsApp1.groovy @@ -0,0 +1,12 @@ +//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 = 10 +//Global variable for contact! +def recipients = "AJ" +//Global variable for phone! +def phoneNumber = 9495379373 diff --git a/Extractor/App1/extractedObjectsConstructorApp1.groovy b/Extractor/App1/extractedObjectsConstructorApp1.groovy new file mode 100644 index 0000000..2e3aa8e --- /dev/null +++ b/Extractor/App1/extractedObjectsConstructorApp1.groovy @@ -0,0 +1,2 @@ +lock1 = obj.lockObject +contact = obj.contactObject diff --git a/Extractor/App1/inputMethodsFile1.groovy b/Extractor/App1/inputMethodsFile1.groovy new file mode 100644 index 0000000..0310df4 --- /dev/null +++ b/Extractor/App1/inputMethodsFile1.groovy @@ -0,0 +1,6 @@ +input"lock1","capability.lock",required:true,"App1" +input"contact","capability.contactSensor",required:true,"App1" +input"minutesLater","number",title:"Delay(inminutes):",required:true,"App1" +input"secondsLater","number",title:"Delay(inseconds):",required:true,"App1" +input("recipients","contact",title:"Sendnotificationsto",required:false,"App1") +input"phoneNumber","phone",title:"Warnwithtextmessage(optional)",description:"PhoneNumber",required:false,"App1" diff --git a/Extractor/App2.groovy b/Extractor/App2.groovy deleted file mode 100644 index 176a336..0000000 --- a/Extractor/App2.groovy +++ /dev/null @@ -1,80 +0,0 @@ -/** - * 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() -{ - 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) -} diff --git a/Extractor/App2/App2.groovy b/Extractor/App2/App2.groovy new file mode 100644 index 0000000..0af812f --- /dev/null +++ b/Extractor/App2/App2.groovy @@ -0,0 +1,66 @@ +///// +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() +{ + 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) +} diff --git a/Extractor/App2/extractedFunctionsApp2.groovy b/Extractor/App2/extractedFunctionsApp2.groovy new file mode 100644 index 0000000..6d9060e --- /dev/null +++ b/Extractor/App2/extractedFunctionsApp2.groovy @@ -0,0 +1,6 @@ +//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/App2/extractedObjectsApp2.groovy b/Extractor/App2/extractedObjectsApp2.groovy new file mode 100644 index 0000000..7089b9b --- /dev/null +++ b/Extractor/App2/extractedObjectsApp2.groovy @@ -0,0 +1,10 @@ +//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 = 4 diff --git a/Extractor/App2/extractedObjectsConstructorApp2.groovy b/Extractor/App2/extractedObjectsConstructorApp2.groovy new file mode 100644 index 0000000..709a386 --- /dev/null +++ b/Extractor/App2/extractedObjectsConstructorApp2.groovy @@ -0,0 +1,3 @@ +switchesoff = obj.switchObject +switcheson = obj.switchObject +lock1 = obj.lockObject diff --git a/Extractor/App2/inputMethodsFile2.groovy b/Extractor/App2/inputMethodsFile2.groovy new file mode 100644 index 0000000..a5d4302 --- /dev/null +++ b/Extractor/App2/inputMethodsFile2.groovy @@ -0,0 +1,5 @@ +input"switchesoff","capability.switch",multiple:true,required:true,"App2" +input"switcheson","capability.switch",multiple:true,required:false,"App2" +input"lock1","capability.lock",multiple:true,"App2" +input"newMode","mode",title:"Mode?","App2" +input"waitfor","number",title:"Offafter(default120)",required:true,"App2" diff --git a/Extractor/ExtractorScript.py b/Extractor/ExtractorScript.py index e925f5f..2d4493c 100644 --- a/Extractor/ExtractorScript.py +++ b/Extractor/ExtractorScript.py @@ -1,10 +1,12 @@ +import os readyToReturn = 0 ToReturn = "" def GetToken(f): global readyToReturn global ToReturn - Skip = ["(", "\"", ":", ",", "{", "}", ")", '\n', '\t', ' ', "/"] + Skip = ['\n', '\t', ' '] + Special = ["(", "\"", ":", ",", "{", "}", ")", "/", "*"] S = "" if (readyToReturn): readyToReturn = 0 @@ -12,242 +14,104 @@ def GetToken(f): ToReturn = "" c = f.read(1) while(True): - if (c in Skip): + if (c in Special): if (S != ""): - if (c == "{" or c == "}"): - readyToReturn = 1 - ToReturn = c - return S - else: - return S - + readyToReturn = 1 + ToReturn = c + return S else: - if (c == "{" or c == "}"): - return c - else: - c = f.read(1) - continue + return c + elif (c in Skip): + if (S != ""): + return S + else: + c = f.read(1) + continue S += c c = f.read(1) 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 +def ExtractMethods(F, inputMethodsFile, appName): + Temp = GetToken(F) + inputMethod = "" while (Temp != "EOF"): - #Extract the global objects for input + #Extract the input methods 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"): + inputMethod += Temp + Temp = GetToken(F) #",(,input + #input "","",linkedHashMap + if (Temp == "\""): + while (Temp!="input" and Temp!="}"): + inputMethod += Temp Temp = GetToken(F) - Title = Temp - elif (Temp == "required"): + inputMethod += ",\""+appName+"\"" + #input () + elif (Temp == "("): + while (Temp!=")"): + inputMethod += Temp Temp = GetToken(F) - Required = Temp - elif (Temp == "multiple"): + inputMethod += ",\""+appName+"\")" + #input linkedHashMap + elif (Temp == "input"): + while (Temp!="input" and Temp!="}"): + inputMethod += Temp 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"): - - #elif (Type == "capability.waterSensor"): - - #elif (Type == "capability.touchSensor"): - - #elif (Type == "capability.imageCapture"): - - #elif (Type == "device.mobilePresence"): - - #elif (Type == "device.aeonKeyFob"): - - 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 == "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 + inputMethod += ",\""+appName+"\"" 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) - -ExtractorFunc(F, outApp, outConstructorApp, Temp) - -#For app2 -F = open("Extractor/App2.groovy", "r") -outApp = open("Extractor/extractedObjectsApp2.groovy", "w+") -outConstructorApp = open("Extractor/extractedObjectsConstructorApp2.groovy", "w+") -Temp = GetToken(F) + if (appName == "App1"): + extractedFunctionsApp1.write("//Global Object for functions in subscribe method!\n") + extractedFunctionsApp1.write("def %s = this.&" % NameofFunc) + extractedFunctionsApp1.write("%s\n" % NameofFunc) + else: + extractedFunctionsApp2.write("//Global Object for functions in subscribe method!\n") + extractedFunctionsApp2.write("def %s = this.&" % NameofFunc) + extractedFunctionsApp2.write("%s\n" % NameofFunc) + if (Temp!="input"): + Temp=GetToken(F) + if (inputMethod != ""): + inputMethodsFile.write(inputMethod+"\n") + inputMethod = "" + -ExtractorFunc(F, outApp, outConstructorApp, Temp) -outGlobal.close() +#Extract the methods from App1 and store in inputMethodsFile +F1 = open("Extractor/App1/App1.groovy", "r") +inputMethodsFile1 = open("Extractor/App1/inputMethodsFile1.groovy", "w+") +extractedFunctionsApp1 = open("Extractor/App1/extractedFunctionsApp1.groovy", "w+") +ExtractMethods(F1, inputMethodsFile1, "App1") +inputMethodsFile1.close() +F1.close() + +#Extract the methods from App2 and store in inputMethodsFile +F2 = open("Extractor/App2/App2.groovy", "r") +inputMethodsFile2 = open("Extractor/App2/inputMethodsFile2.groovy", "w+") +extractedFunctionsApp2 = open("Extractor/App2/extractedFunctionsApp2.groovy", "w+") +ExtractMethods(F2, inputMethodsFile2, "App2") +inputMethodsFile2.close() +F2.close() + + +#Save the extracted methods and methods functions in a same file +extractorFile = open("Extractor/extractorFile.groovy", "w+") +inputMethodsFile1 = open("Extractor/App1/inputMethodsFile1.groovy", "r") +inputMethodsFile2 = open("Extractor/App2/inputMethodsFile2.groovy", "r") +inputMethods = open("Extractor/inputMethods.groovy", "r") +for line in inputMethods: + extractorFile.write(line) +extractorFile.write("\n\n") +for line in inputMethodsFile1: + extractorFile.write(line) +for line in inputMethodsFile2: + extractorFile.write(line) +extractorFile.close() +inputMethodsFile1.close() +inputMethodsFile2.close() +inputMethods.close() + + +#Run the file to extract the objects +os.system("groovy Extractor/extractorFile.groovy") diff --git a/Extractor/extractedObjectsApp1.groovy b/Extractor/extractedObjectsApp1.groovy deleted file mode 100644 index d205217..0000000 --- a/Extractor/extractedObjectsApp1.groovy +++ /dev/null @@ -1,24 +0,0 @@ -//Object for class lock! -def lock1 -//Object for class contactSensor! -def contact -//Global variable for number! -def minutesLater = 1 -//Global variable for number! -def secondsLater = 1 -//Global variable for recipients! -def recipients = ['AJ'] -//Global variable for phone number! -def phoneNumber = 9495379373 -//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 deleted file mode 100644 index 77277fe..0000000 --- a/Extractor/extractedObjectsApp2.groovy +++ /dev/null @@ -1,16 +0,0 @@ -//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 = 1 -//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 deleted file mode 100644 index 2e3aa8e..0000000 --- a/Extractor/extractedObjectsConstructorApp1.groovy +++ /dev/null @@ -1,2 +0,0 @@ -lock1 = obj.lockObject -contact = obj.contactObject diff --git a/Extractor/extractedObjectsConstructorApp2.groovy b/Extractor/extractedObjectsConstructorApp2.groovy deleted file mode 100644 index 709a386..0000000 --- a/Extractor/extractedObjectsConstructorApp2.groovy +++ /dev/null @@ -1,3 +0,0 @@ -switchesoff = obj.switchObject -switcheson = obj.switchObject -lock1 = obj.lockObject diff --git a/Extractor/extractorFile.groovy b/Extractor/extractorFile.groovy new file mode 100644 index 0000000..612e6ac --- /dev/null +++ b/Extractor/extractorFile.groovy @@ -0,0 +1,748 @@ +//////////////////////////////////////// +import groovy.transform.Field +@Field File outGlobal = new File("Extractor/outGlobal.groovy") +@Field File extractedObjectsApp1 = new File("Extractor/App1/extractedObjectsApp1.groovy") +@Field File extractedObjectsApp2 = new File("Extractor/App2/extractedObjectsApp2.groovy") +@Field File extractedObjectsConstructorApp1 = new File("Extractor/App1/extractedObjectsConstructorApp1.groovy") +@Field File extractedObjectsConstructorApp2 = new File("Extractor/App2/extractedObjectsConstructorApp2.groovy") + +//Empty the files +outGlobal.write("") +extractedObjectsApp1.write("") +extractedObjectsApp2.write("") +extractedObjectsConstructorApp1.write("") +extractedObjectsConstructorApp2.write("") + + +@Field lockIsSet = 0 +@Field contactIsSet = 0 +@Field switchIsSet = 0 + +//input "","" +def input(String name, String type, String App) { + switch(type) { + case "capability.lock": + if (lockIsSet != 1) { + lockIsSet = 1 + outGlobal.append("//Global Object for class lock!\n") + outGlobal.append("@Field def lockObject = new Locking(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class lock!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.lockObject\n") + } else { + extractedObjectsApp2.append("//Object for class lock!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.lockObject\n") + } + break + case "capability.alarm": + break + case "capability.battery": + break + case "capability.beacon": + break + case "capability.carbonMonoxideDetector": + break + case "capability.colorControl": + break + case "capability.contactSensor": + if (contactIsSet != 1) { + contactIsSet = 1 + outGlobal.append("//Global Object for class contactSensor!\n") + outGlobal.append("@Field def contactObject = new Contacting(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class contactSensor!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.contactObject\n") + } else { + extractedObjectsApp2.append("//Object for class contactSensor!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.contactObject\n") + } + break + case "capability.doorControl": + break + case "capability.energyMeter": + break + case "capability.illuminanceMeasurement": + break + case "capability.accelerationSensor": + break + case "capability.motionSensor": + break + case "capability.musicPlayer": + break + case "capability.powerMeter": + break + case "capability.presenceSensor": + break + case "capability.relativeHumidityMeasurement": + break + case "capability.relaySwitch": + break + case "capability.sleepSensor": + break + case "capability.smokeDetector": + break + case "capability.stepSensor": + break + case "capability.switch": + if (switchIsSet != 1) { + switchIsSet = 1 + outGlobal.append("//Global Object for class switch!\n") + outGlobal.append("@Field def switchObject = new Switching(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class switch!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.switchObject\n") + } else { + extractedObjectsApp2.append("//Object for class switch!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.switchObject\n") + } + break + case "capability.switchLevel": + break + case "capability.temperatureMeasurement": + break + case "capability.thermostat": + break + case "capability.valve": + break + case "capability.waterSensor": + break + case "capability.touchSensor": + break + case "capability.imageCapture": + break + case "device.mobilePresence": + break + case "device.aeonKeyFob": + break + case "mode": + def userInput = System.console().readLine 'Enter the mode:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for mode!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for mode!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "decimal": + break + case "text": + break + case "number": + def userInput = System.console().readLine 'Enter the number:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for number!\n") + extractedObjectsApp1.append("def "+name+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for number!\n") + extractedObjectsApp2.append("def "+name+" = "+userInput+"\n") + } + break + case "time": + def userInput = System.console().readLine 'Enter the time:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for time!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for time!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "enum": + def userInput = System.console().readLine 'Enter the enum:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for enum!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for enum!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "bool": + break + case "phone": + def userInput = System.console().readLine 'Enter the phone:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for phone!\n") + extractedObjectsApp1.append("def "+name+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for phone!\n") + extractedObjectsApp2.append("def "+name+" = "+userInput+"\n") + } + break + case "contact": + def userInput = System.console().readLine 'Enter the name of the contact:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for contact!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for contact!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + default: + break + } +} + +//input "","",linkedHashMap +def input(LinkedHashMap metaData, String name, String type, String App) { + switch(type) { + case "capability.lock": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (lockIsSet != 1) { + lockIsSet = 1 + def count = System.console().readLine 'Enter the number of locks to control?(1,2, or 3)' + outGlobal.append("//Global Object for class lock!\n") + outGlobal.append("@Field def lockObject = new Locking(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (lockIsSet != 1) { + lockIsSet = 1 + outGlobal.append("//Global Object for class lock!\n") + outGlobal.append("@Field def lockObject = new Locking(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class lock!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.lockObject\n") + } else { + extractedObjectsApp2.append("//Object for class lock!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.lockObject\n") + } + break + case "capability.alarm": + break + case "capability.battery": + break + case "capability.beacon": + break + case "capability.carbonMonoxideDetector": + break + case "capability.colorControl": + break + case "capability.contactSensor": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (contactIsSet != 1) { + contactIsSet = 1 + def count = System.console().readLine 'Enter the number of contact sensors to control?(1,2, or 3)' + outGlobal.append("//Global Object for class contactSensor!\n") + outGlobal.append("@Field def contactObject = new Contacting(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (contactIsSet != 1) { + contactIsSet = 1 + outGlobal.append("//Global Object for class contactSensor!\n") + outGlobal.append("@Field def contactObject = new Contacting(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class contactSensor!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.contactObject\n") + } else { + extractedObjectsApp2.append("//Object for class contactSensor!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.contactObject\n") + } + break + case "capability.doorControl": + break + case "capability.energyMeter": + break + case "capability.illuminanceMeasurement": + break + case "capability.accelerationSensor": + break + case "capability.motionSensor": + break + case "capability.musicPlayer": + break + case "capability.powerMeter": + break + case "capability.presenceSensor": + break + case "capability.relativeHumidityMeasurement": + break + case "capability.relaySwitch": + break + case "capability.sleepSensor": + break + case "capability.smokeDetector": + break + case "capability.stepSensor": + break + case "capability.switch": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (switchIsSet != 1) { + switchIsSet = 1 + def count = System.console().readLine 'Enter the number of switches to control?(1,2, or 3)' + outGlobal.append("//Global Object for class switch!\n") + outGlobal.append("@Field def switchObject = new Switching(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (switchIsSet != 1) { + switchIsSet = 1 + outGlobal.append("//Global Object for class switch!\n") + outGlobal.append("@Field def switchObject = new Switching(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class switch!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.switchObject\n") + } else { + extractedObjectsApp2.append("//Object for class switch!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.switchObject\n") + } + break + case "capability.switchLevel": + break + case "capability.temperatureMeasurement": + break + case "capability.thermostat": + break + case "capability.valve": + break + case "capability.waterSensor": + break + case "capability.touchSensor": + break + case "capability.imageCapture": + break + case "device.mobilePresence": + break + case "device.aeonKeyFob": + break + case "mode": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + def userInput = System.console().readLine 'Enter the mode:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for mode!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for mode!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "decimal": + break + case "text": + break + case "number": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the number:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for number!\n") + extractedObjectsApp1.append("def "+name+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for number!\n") + extractedObjectsApp2.append("def "+name+" = "+userInput+"\n") + } + break + case "time": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the time:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for time!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for time!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "enum": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the enum:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for enum!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for enum!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "bool": + break + case "phone": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the phone:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for phone!\n") + extractedObjectsApp1.append("def "+name+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for phone!\n") + extractedObjectsApp2.append("def "+name+" = "+userInput+"\n") + } + break + case "contact": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the name of the contact:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for contact!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for contact!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + default: + break + } +} +//input linkedHashMap +def input(LinkedHashMap metaData, String App) { + switch(metaData['type']) { + case "capability.lock": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (lockIsSet != 1) { + lockIsSet = 1 + def count = System.console().readLine 'Enter the number of locks to control?(1,2, or 3)' + outGlobal.append("//Global Object for class lock!\n") + outGlobal.append("@Field def lockObject = new Locking(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (lockIsSet != 1) { + lockIsSet = 1 + outGlobal.append("//Global Object for class lock!\n") + outGlobal.append("@Field def lockObject = new Locking(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class lock!\n") + extractedObjectsApp1.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp1.append(metaData['input']+" = obj.lockObject\n") + } else { + extractedObjectsApp2.append("//Object for class lock!\n") + extractedObjectsApp2.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp2.append(metaData['input']+" = obj.lockObject\n") + } + break + case "capability.alarm": + break + case "capability.battery": + break + case "capability.beacon": + break + case "capability.carbonMonoxideDetector": + break + case "capability.colorControl": + break + case "capability.contactSensor": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (contactIsSet != 1) { + contactIsSet = 1 + def count = System.console().readLine 'Enter the number of contact sensors to control?(1,2, or 3)' + outGlobal.append("//Global Object for class contactSensor!\n") + outGlobal.append("@Field def contactObject = new Contacting(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (contactIsSet != 1) { + contactIsSet = 1 + outGlobal.append("//Global Object for class contactSensor!\n") + outGlobal.append("@Field def contactObject = new Contacting(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class contactSensor!\n") + extractedObjectsApp1.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp1.append(metaData['input']+" = obj.contactObject\n") + } else { + extractedObjectsApp2.append("//Object for class contactSensor!\n") + extractedObjectsApp2.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp2.append(metaData['input']+" = obj.contactObject\n") + } + break + case "capability.doorControl": + break + case "capability.energyMeter": + break + case "capability.illuminanceMeasurement": + break + case "capability.accelerationSensor": + break + case "capability.motionSensor": + break + case "capability.musicPlayer": + break + case "capability.powerMeter": + break + case "capability.presenceSensor": + break + case "capability.relativeHumidityMeasurement": + break + case "capability.relaySwitch": + break + case "capability.sleepSensor": + break + case "capability.smokeDetector": + break + case "capability.stepSensor": + break + case "capability.switch": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (switchIsSet != 1) { + switchIsSet = 1 + def count = System.console().readLine 'Enter the number of switches to control?(1,2, or 3)' + outGlobal.append("//Global Object for class switch!\n") + outGlobal.append("@Field def switchObject = new Switching(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (switchIsSet != 1) { + switchIsSet = 1 + outGlobal.append("//Global Object for class switch!\n") + outGlobal.append("@Field def switchObject = new Switching(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class switch!\n") + extractedObjectsApp1.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp1.append(metaData['input']+" = obj.switchObject\n") + } else { + extractedObjectsApp2.append("//Object for class switch!\n") + extractedObjectsApp2.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp2.append(metaData['input']+" = obj.switchObject\n") + } + break + case "capability.switchLevel": + break + case "capability.temperatureMeasurement": + break + case "capability.thermostat": + break + case "capability.valve": + break + case "capability.waterSensor": + break + case "capability.touchSensor": + break + case "capability.imageCapture": + break + case "device.mobilePresence": + break + case "device.aeonKeyFob": + break + case "mode": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the mode:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for mode!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for mode!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } + break + case "decimal": + break + case "text": + break + case "number": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the number:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for number!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for number!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = "+userInput+"\n") + } + break + case "time": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the time:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for time!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for time!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } + break + case "enum": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the enum:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for enum!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for enum!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } + break + case "bool": + break + case "phone": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the phone:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for phone!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for phone!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = "+userInput+"\n") + } + break + case "contact": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the name of the contact:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for contact!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for contact!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } + break + default: + break + } +} + + +input"lock1","capability.lock",required:true,"App1" +input"contact","capability.contactSensor",required:true,"App1" +input"minutesLater","number",title:"Delay(inminutes):",required:true,"App1" +input"secondsLater","number",title:"Delay(inseconds):",required:true,"App1" +input("recipients","contact",title:"Sendnotificationsto",required:false,"App1") +input"phoneNumber","phone",title:"Warnwithtextmessage(optional)",description:"PhoneNumber",required:false,"App1" +input"switchesoff","capability.switch",multiple:true,required:true,"App2" +input"switcheson","capability.switch",multiple:true,required:false,"App2" +input"lock1","capability.lock",multiple:true,"App2" +input"newMode","mode",title:"Mode?","App2" +input"waitfor","number",title:"Offafter(default120)",required:true,"App2" diff --git a/Extractor/inputMethods.groovy b/Extractor/inputMethods.groovy new file mode 100644 index 0000000..43223d5 --- /dev/null +++ b/Extractor/inputMethods.groovy @@ -0,0 +1,735 @@ +//////////////////////////////////////// +import groovy.transform.Field +@Field File outGlobal = new File("Extractor/outGlobal.groovy") +@Field File extractedObjectsApp1 = new File("Extractor/App1/extractedObjectsApp1.groovy") +@Field File extractedObjectsApp2 = new File("Extractor/App2/extractedObjectsApp2.groovy") +@Field File extractedObjectsConstructorApp1 = new File("Extractor/App1/extractedObjectsConstructorApp1.groovy") +@Field File extractedObjectsConstructorApp2 = new File("Extractor/App2/extractedObjectsConstructorApp2.groovy") + +//Empty the files +outGlobal.write("") +extractedObjectsApp1.write("") +extractedObjectsApp2.write("") +extractedObjectsConstructorApp1.write("") +extractedObjectsConstructorApp2.write("") + + +@Field lockIsSet = 0 +@Field contactIsSet = 0 +@Field switchIsSet = 0 + +//input "","" +def input(String name, String type, String App) { + switch(type) { + case "capability.lock": + if (lockIsSet != 1) { + lockIsSet = 1 + outGlobal.append("//Global Object for class lock!\n") + outGlobal.append("@Field def lockObject = new Locking(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class lock!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.lockObject\n") + } else { + extractedObjectsApp2.append("//Object for class lock!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.lockObject\n") + } + break + case "capability.alarm": + break + case "capability.battery": + break + case "capability.beacon": + break + case "capability.carbonMonoxideDetector": + break + case "capability.colorControl": + break + case "capability.contactSensor": + if (contactIsSet != 1) { + contactIsSet = 1 + outGlobal.append("//Global Object for class contactSensor!\n") + outGlobal.append("@Field def contactObject = new Contacting(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class contactSensor!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.contactObject\n") + } else { + extractedObjectsApp2.append("//Object for class contactSensor!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.contactObject\n") + } + break + case "capability.doorControl": + break + case "capability.energyMeter": + break + case "capability.illuminanceMeasurement": + break + case "capability.accelerationSensor": + break + case "capability.motionSensor": + break + case "capability.musicPlayer": + break + case "capability.powerMeter": + break + case "capability.presenceSensor": + break + case "capability.relativeHumidityMeasurement": + break + case "capability.relaySwitch": + break + case "capability.sleepSensor": + break + case "capability.smokeDetector": + break + case "capability.stepSensor": + break + case "capability.switch": + if (switchIsSet != 1) { + switchIsSet = 1 + outGlobal.append("//Global Object for class switch!\n") + outGlobal.append("@Field def switchObject = new Switching(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class switch!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.switchObject\n") + } else { + extractedObjectsApp2.append("//Object for class switch!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.switchObject\n") + } + break + case "capability.switchLevel": + break + case "capability.temperatureMeasurement": + break + case "capability.thermostat": + break + case "capability.valve": + break + case "capability.waterSensor": + break + case "capability.touchSensor": + break + case "capability.imageCapture": + break + case "device.mobilePresence": + break + case "device.aeonKeyFob": + break + case "mode": + def userInput = System.console().readLine 'Enter the mode:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for mode!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for mode!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "decimal": + break + case "text": + break + case "number": + def userInput = System.console().readLine 'Enter the number:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for number!\n") + extractedObjectsApp1.append("def "+name+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for number!\n") + extractedObjectsApp2.append("def "+name+" = "+userInput+"\n") + } + break + case "time": + def userInput = System.console().readLine 'Enter the time:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for time!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for time!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "enum": + def userInput = System.console().readLine 'Enter the enum:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for enum!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for enum!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "bool": + break + case "phone": + def userInput = System.console().readLine 'Enter the phone:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for phone!\n") + extractedObjectsApp1.append("def "+name+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for phone!\n") + extractedObjectsApp2.append("def "+name+" = "+userInput+"\n") + } + break + case "contact": + def userInput = System.console().readLine 'Enter the name of the contact:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for contact!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for contact!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + default: + break + } +} + +//input "","",linkedHashMap +def input(LinkedHashMap metaData, String name, String type, String App) { + switch(type) { + case "capability.lock": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (lockIsSet != 1) { + lockIsSet = 1 + def count = System.console().readLine 'Enter the number of locks to control?(1,2, or 3)' + outGlobal.append("//Global Object for class lock!\n") + outGlobal.append("@Field def lockObject = new Locking(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (lockIsSet != 1) { + lockIsSet = 1 + outGlobal.append("//Global Object for class lock!\n") + outGlobal.append("@Field def lockObject = new Locking(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class lock!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.lockObject\n") + } else { + extractedObjectsApp2.append("//Object for class lock!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.lockObject\n") + } + break + case "capability.alarm": + break + case "capability.battery": + break + case "capability.beacon": + break + case "capability.carbonMonoxideDetector": + break + case "capability.colorControl": + break + case "capability.contactSensor": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (contactIsSet != 1) { + contactIsSet = 1 + def count = System.console().readLine 'Enter the number of contact sensors to control?(1,2, or 3)' + outGlobal.append("//Global Object for class contactSensor!\n") + outGlobal.append("@Field def contactObject = new Contacting(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (contactIsSet != 1) { + contactIsSet = 1 + outGlobal.append("//Global Object for class contactSensor!\n") + outGlobal.append("@Field def contactObject = new Contacting(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class contactSensor!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.contactObject\n") + } else { + extractedObjectsApp2.append("//Object for class contactSensor!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.contactObject\n") + } + break + case "capability.doorControl": + break + case "capability.energyMeter": + break + case "capability.illuminanceMeasurement": + break + case "capability.accelerationSensor": + break + case "capability.motionSensor": + break + case "capability.musicPlayer": + break + case "capability.powerMeter": + break + case "capability.presenceSensor": + break + case "capability.relativeHumidityMeasurement": + break + case "capability.relaySwitch": + break + case "capability.sleepSensor": + break + case "capability.smokeDetector": + break + case "capability.stepSensor": + break + case "capability.switch": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (switchIsSet != 1) { + switchIsSet = 1 + def count = System.console().readLine 'Enter the number of switches to control?(1,2, or 3)' + outGlobal.append("//Global Object for class switch!\n") + outGlobal.append("@Field def switchObject = new Switching(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (switchIsSet != 1) { + switchIsSet = 1 + outGlobal.append("//Global Object for class switch!\n") + outGlobal.append("@Field def switchObject = new Switching(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class switch!\n") + extractedObjectsApp1.append("def "+name+"\n") + extractedObjectsConstructorApp1.append(name+" = obj.switchObject\n") + } else { + extractedObjectsApp2.append("//Object for class switch!\n") + extractedObjectsApp2.append("def "+name+"\n") + extractedObjectsConstructorApp2.append(name+" = obj.switchObject\n") + } + break + case "capability.switchLevel": + break + case "capability.temperatureMeasurement": + break + case "capability.thermostat": + break + case "capability.valve": + break + case "capability.waterSensor": + break + case "capability.touchSensor": + break + case "capability.imageCapture": + break + case "device.mobilePresence": + break + case "device.aeonKeyFob": + break + case "mode": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + def userInput = System.console().readLine 'Enter the mode:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for mode!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for mode!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "decimal": + break + case "text": + break + case "number": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the number:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for number!\n") + extractedObjectsApp1.append("def "+name+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for number!\n") + extractedObjectsApp2.append("def "+name+" = "+userInput+"\n") + } + break + case "time": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the time:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for time!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for time!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "enum": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the enum:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for enum!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for enum!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + case "bool": + break + case "phone": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the phone:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for phone!\n") + extractedObjectsApp1.append("def "+name+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for phone!\n") + extractedObjectsApp2.append("def "+name+" = "+userInput+"\n") + } + break + case "contact": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the name of the contact:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for contact!\n") + extractedObjectsApp1.append("def "+name+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for contact!\n") + extractedObjectsApp2.append("def "+name+" = \""+userInput+"\"\n") + } + break + default: + break + } +} +//input linkedHashMap +def input(LinkedHashMap metaData, String App) { + switch(metaData['type']) { + case "capability.lock": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (lockIsSet != 1) { + lockIsSet = 1 + def count = System.console().readLine 'Enter the number of locks to control?(1,2, or 3)' + outGlobal.append("//Global Object for class lock!\n") + outGlobal.append("@Field def lockObject = new Locking(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (lockIsSet != 1) { + lockIsSet = 1 + outGlobal.append("//Global Object for class lock!\n") + outGlobal.append("@Field def lockObject = new Locking(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class lock!\n") + extractedObjectsApp1.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp1.append(metaData['input']+" = obj.lockObject\n") + } else { + extractedObjectsApp2.append("//Object for class lock!\n") + extractedObjectsApp2.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp2.append(metaData['input']+" = obj.lockObject\n") + } + break + case "capability.alarm": + break + case "capability.battery": + break + case "capability.beacon": + break + case "capability.carbonMonoxideDetector": + break + case "capability.colorControl": + break + case "capability.contactSensor": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (contactIsSet != 1) { + contactIsSet = 1 + def count = System.console().readLine 'Enter the number of contact sensors to control?(1,2, or 3)' + outGlobal.append("//Global Object for class contactSensor!\n") + outGlobal.append("@Field def contactObject = new Contacting(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (contactIsSet != 1) { + contactIsSet = 1 + outGlobal.append("//Global Object for class contactSensor!\n") + outGlobal.append("@Field def contactObject = new Contacting(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class contactSensor!\n") + extractedObjectsApp1.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp1.append(metaData['input']+" = obj.contactObject\n") + } else { + extractedObjectsApp2.append("//Object for class contactSensor!\n") + extractedObjectsApp2.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp2.append(metaData['input']+" = obj.contactObject\n") + } + break + case "capability.doorControl": + break + case "capability.energyMeter": + break + case "capability.illuminanceMeasurement": + break + case "capability.accelerationSensor": + break + case "capability.motionSensor": + break + case "capability.musicPlayer": + break + case "capability.powerMeter": + break + case "capability.presenceSensor": + break + case "capability.relativeHumidityMeasurement": + break + case "capability.relaySwitch": + break + case "capability.sleepSensor": + break + case "capability.smokeDetector": + break + case "capability.stepSensor": + break + case "capability.switch": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('multiple')) { + if (metaData['multiple'] == true) { + if (switchIsSet != 1) { + switchIsSet = 1 + def count = System.console().readLine 'Enter the number of switches to control?(1,2, or 3)' + outGlobal.append("//Global Object for class switch!\n") + outGlobal.append("@Field def switchObject = new Switching(sendEvent, ") + outGlobal.append(count+")\n") + } + } + } + if (switchIsSet != 1) { + switchIsSet = 1 + outGlobal.append("//Global Object for class switch!\n") + outGlobal.append("@Field def switchObject = new Switching(sendEvent,1)\n") + } + if (App == "App1") { + extractedObjectsApp1.append("//Object for class switch!\n") + extractedObjectsApp1.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp1.append(metaData['input']+" = obj.switchObject\n") + } else { + extractedObjectsApp2.append("//Object for class switch!\n") + extractedObjectsApp2.append("def "+metaData['input']+"\n") + extractedObjectsConstructorApp2.append(metaData['input']+" = obj.switchObject\n") + } + break + case "capability.switchLevel": + break + case "capability.temperatureMeasurement": + break + case "capability.thermostat": + break + case "capability.valve": + break + case "capability.waterSensor": + break + case "capability.touchSensor": + break + case "capability.imageCapture": + break + case "device.mobilePresence": + break + case "device.aeonKeyFob": + break + case "mode": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the mode:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for mode!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for mode!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } + break + case "decimal": + break + case "text": + break + case "number": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the number:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for number!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for number!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = "+userInput+"\n") + } + break + case "time": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the time:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for time!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for time!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } + break + case "enum": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the enum:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for enum!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for enum!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } + break + case "bool": + break + case "phone": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the phone:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for phone!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = "+userInput+"\n") + } else { + extractedObjectsApp2.append("//Global variable for phone!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = "+userInput+"\n") + } + break + case "contact": + if (metaData.containsKey('title')) { + println metaData['title'] + } + if (metaData.containsKey('options')) { + println "Options: "+metaData['options'] + } + if (metaData.containsKey('metadata')) { + println "metaData: "+metaData['metadata'] + } + def userInput = System.console().readLine 'Enter the name of the contact:' + if (App == "App1") { + extractedObjectsApp1.append("//Global variable for contact!\n") + extractedObjectsApp1.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } else { + extractedObjectsApp2.append("//Global variable for contact!\n") + extractedObjectsApp2.append("def "+metaData['input']+" = \""+userInput+"\"\n") + } + break + default: + break + } +} diff --git a/Extractor/outGlobal.groovy b/Extractor/outGlobal.groovy index c1c0918..35f7e8f 100644 --- a/Extractor/outGlobal.groovy +++ b/Extractor/outGlobal.groovy @@ -1,6 +1,6 @@ //Global Object for class lock! -@Field def lockObject = new Locking(sendEvent, 1) +@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 contactObject = new Contacting(sendEvent,1) +//Global Object for class switch! @Field def switchObject = new Switching(sendEvent, 1) diff --git a/Lock/Locking.groovy b/Lock/Locking.groovy index f55a239..23c4e2e 100644 --- a/Lock/Locking.groovy +++ b/Lock/Locking.groovy @@ -1,5 +1,6 @@ //Create a class for lock device package Lock +import Timer.SimulatedTimer public class Locking{ int deviceNumbers @@ -7,13 +8,19 @@ public class Locking{ def sendEvent def timers + //When we have only one device + private int id = 0 + private String displayName = "lock0" + private String lockState = "locked" + private String lockLatestValue = "locked" + Locking(Closure sendEvent, int deviceNumbers) { this.sendEvent = sendEvent - this.timers = new Timer() + this.timers = new SimulatedTimer() this.deviceNumbers = deviceNumbers this.locks = [] if (deviceNumbers == 1) { - locks = [new Locks(sendEvent, 0, "lock0", "locked", "locked")] + locks = [new Locks(sendEvent, this.id, this.displayName, this.lockState, this.lockLatestValue)] } else if (deviceNumbers == 2) { locks = [new Locks(sendEvent, 0, "lock0", "locked", "locked"), new Locks(sendEvent, 1, "lock1", "locked", "locked")] } else if (deviceNumbers == 3) { @@ -46,6 +53,9 @@ public class Locking{ //By Model Checker def setValue(LinkedHashMap eventDataMap) { locks[eventDataMap["deviceId"]].setValue(eventDataMap["value"]) + if (deviceNumbers == 1) + this.lockState = locks[eventDataMap["deviceId"]].lockState + this.lockLatestValue = locks[eventDataMap["deviceId"]].lockLatestValue sendEvent(eventDataMap) } diff --git a/Lock/Locks.groovy b/Lock/Locks.groovy index d5f97ae..0f1244b 100644 --- a/Lock/Locks.groovy +++ b/Lock/Locks.groovy @@ -1,29 +1,30 @@ //Create a class for lock device package Lock +import Timer.SimulatedTimer public class Locks { private int id private String displayName - private String lockCurrentValue + private String lockState private String lockLatestValue def sendEvent def timers - Locks(Closure sendEvent, int id, String displayName, String lockCurrentValue, String lockLatestValue) { + Locks(Closure sendEvent, int id, String displayName, String lockState, String lockLatestValue) { this.id = id this.sendEvent = sendEvent this.displayName = displayName - this.lockCurrentValue = lockCurrentValue + this.lockState = lockState this.lockLatestValue = lockLatestValue - this.timers = new Timer() + this.timers = new SimulatedTimer() } //By Apps def lock() { println("the door with id:$id is locked!") - this.lockLatestValue = this.lockCurrentValue - this.lockCurrentValue = "locked" + this.lockLatestValue = this.lockState + this.lockState = "locked" sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } @@ -31,8 +32,8 @@ public class Locks { 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" + this.lockLatestValue = this.lockState + this.lockState = "locked" sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } @@ -40,8 +41,8 @@ public class Locks { def unlock() { println("the door with id:$id is unlocked!") - this.lockLatestValue = this.lockCurrentValue - this.lockCurrentValue = "unlocked" + this.lockLatestValue = this.lockState + this.lockState = "unlocked" sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } @@ -49,8 +50,8 @@ public class Locks { 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" + this.lockLatestValue = this.lockState + this.lockState = "locked" sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } @@ -59,13 +60,13 @@ public class Locks { //By Model Checker def setValue(String value) { println("the door with id:$id is $value!") - this.lockLatestValue = this.lockCurrentValue - this.lockCurrentValue = value + this.lockLatestValue = this.lockState + this.lockState = value } def currentValue(String deviceFeature) { if (deviceFeature == "lock") { - return lockCurrentValue + return lockState } } diff --git a/Methods/eventHandler.groovy b/Methods/eventHandler.groovy index 5c8fc2d..4df7675 100644 --- a/Methods/eventHandler.groovy +++ b/Methods/eventHandler.groovy @@ -9,85 +9,20 @@ def eventHandler(LinkedHashMap eventDataMap) { def isStateChange = eventDataMap["isStateChange"] def unit = eventDataMap["unit"] def data = eventDataMap["data"] - def minSize - def smallest - //make search efficient - if (app1.eventList.size() == app2.eventList.size()) { - minSize = app1.eventList.size() - smallest = "equal" - } else if (app1.eventList.size() < app2.eventList.size()) { - minSize = app1.eventList.size() - smallest = "app1" - } else { - minSize = app2.eventList.size() - smallest = "app2" - } - - for (int i = 0;i < minSize;i++) { - if (app1.eventList[i] == name) { - evt.add(new Event()) - evt[-1].value = value - evt[-1].name = name - evt[-1].deviceId = deviceId - evt[-1].descriptionText = descriptionText - evt[-1].displayed = displayed - evt[-1].linkText = linkText - evt[-1].displayName = linkText - evt[-1].isStateChange = isStateChange - evt[-1].unit = unit - evt[-1].data = data - app1.functionList[i](evt[-1]) - } + for (int i = 0;i < app2.eventList.size();i++) { if (app2.eventList[i] == name) { - evt.add(new Event()) - evt[-1].value = value - evt[-1].name = name - evt[-1].deviceId = deviceId - evt[-1].descriptionText = descriptionText - evt[-1].displayed = displayed - evt[-1].linkText = linkText - evt[-1].displayName = linkText - evt[-1].isStateChange = isStateChange - evt[-1].unit = unit - evt[-1].data = data - app2.functionList[i](evt[-1]) + def event = new Event(value, name, deviceId, descriptionText, displayed, linkText, linkText, isStateChange, unit, data) + evt.add(event) + app2.functionList[i](event) } } - if (smallest == "app1") { - for (int i = minSize;i < app2.eventList.size();i++) { - if (app2.eventList[i] == name) { - evt.add(new Event()) - evt[-1].value = value - evt[-1].name = name - evt[-1].deviceId = deviceId - evt[-1].descriptionText = descriptionText - evt[-1].displayed = displayed - evt[-1].linkText = linkText - evt[-1].displayName = linkText - evt[-1].isStateChange = isStateChange - evt[-1].unit = unit - evt[-1].data = data - app2.functionList[i](evt[-1]) - } - } - } else if (smallest == "app2") { - for (int i = minSize;i < app1.eventList.size();i++) { - if (app1.eventList[i] == name) { - evt.add(new Event()) - evt[-1].value = value - evt[-1].name = name - evt[-1].deviceId = deviceId - evt[-1].descriptionText = descriptionText - evt[-1].displayed = displayed - evt[-1].linkText = linkText - evt[-1].displayName = linkText - evt[-1].isStateChange = isStateChange - evt[-1].unit = unit - evt[-1].data = data - app1.functionList[i](evt[-1]) - } + for (int i = 0;i < app1.eventList.size();i++) { + if (app1.eventList[i] == name) { + def event = new Event(value, name, deviceId, descriptionText, displayed, linkText, linkText, isStateChange, unit, data) + evt.add(event) + app1.functionList[i](event) } - } + } } diff --git a/Methods/runIn.groovy b/Methods/runIn.groovy index 3816599..178e16d 100644 --- a/Methods/runIn.groovy +++ b/Methods/runIn.groovy @@ -1,7 +1,12 @@ ///////////////////////////////////////////////////////////////////// ////runIn(time, func) def runIn(int seconds, Closure functionToCall) { - timersFuncList.add(functionToCall) - timersList.add(new Timer()) - def task = timersList[-1].runAfter(1000*seconds, functionToCall) + if (timersFuncList.contains(functionToCall)) { + timersList[timersFuncList.indexOf(functionToCall)].cancel() + def task = timersList[timersFuncList.indexOf(functionToCall)].runAfter(1000*seconds, functionToCall) + } else { + timersFuncList.add(functionToCall) + timersList.add(new SimulatedTimer()) + def task = timersList[timersFuncList.indexOf(functionToCall)].runAfter(1000*seconds, functionToCall) + } } diff --git a/Methods/schedule.groovy b/Methods/schedule.groovy new file mode 100644 index 0000000..be935b8 --- /dev/null +++ b/Methods/schedule.groovy @@ -0,0 +1,50 @@ +///////////////////////////////////////////////////////////////////// +////schedule(time, nameOfFunction as String) +def schedule(String time, String nameOfFunction) { + def _inputTime = time.split(':') + Date date = new Date() + def _currentTime = date.format("HH:mm:ss").split(':') + + //Convert input time and current time to minutes + def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60 + def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2]) + def delay + + if (inputTime < currentTime) { + delay = 24*60*60-inputTime+currentTime + } else { + delay = inputTime-currentTime + } + + timersFuncList.add(nameOfFunction) + timersList.add(new SimulatedTimer()) + def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*1000) { + "$nameOfFunction"() + } +} +////schedule(time, nameOfFunction as Closure) +def schedule(String time, Closure nameOfFunction) { + def _inputTime = time.split(':') + Date date = new Date() + def _currentTime = date.format("HH:mm:ss").split(':') + + //Convert input time and current time to minutes + def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60 + def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2]) + def delay + + if (inputTime < currentTime) { + delay = 24*60*60-inputTime+currentTime + } else { + delay = inputTime-currentTime + } + + if (timersFuncList.contains(nameOfFunction)) { + timersList[timersFuncList.indexOf(nameOfFunction)].cancel() + def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction) + } else { + timersFuncList.add(nameOfFunction) + timersList.add(new SimulatedTimer()) + def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction) + } +} diff --git a/Methods/sendNotificationToContacts.groovy b/Methods/sendNotificationToContacts.groovy index b5afd49..165d0a4 100644 --- a/Methods/sendNotificationToContacts.groovy +++ b/Methods/sendNotificationToContacts.groovy @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////// ////sendNotificationToContacts(text, recipients) -def sendNotificationToContacts(String text, List recipients) { +def sendNotificationToContacts(String text, String recipients) { for (int i = 0;i < recipients.size();i++) { for (int j = 0;j < location.contacts.size();j++) { if (recipients[i] == location.contacts[j]) { diff --git a/Methods/sendPush.groovy b/Methods/sendPush.groovy new file mode 100644 index 0000000..723ce8f --- /dev/null +++ b/Methods/sendPush.groovy @@ -0,0 +1,5 @@ +///////////////////////////////////////////////////////////////////// +////sendPush(text) +def sendPush(String text) { + println(text) +} diff --git a/Methods/unschedule.groovy b/Methods/unschedule.groovy index 1689621..322d805 100644 --- a/Methods/unschedule.groovy +++ b/Methods/unschedule.groovy @@ -3,7 +3,16 @@ def unschedule(Closure functionToUnschedule) { for (int i = 0;i < timersFuncList.size();i++) { if (timersFuncList[i] == functionToUnschedule) { - timersList[i].cancel() + if (timersList != null) + timersList[i].cancel() } } } + + +def unschedule() { + for (int i = 0;i < timersFuncList.size();i++) { + if (timersList != null) + timersList[i].cancel() + } +} diff --git a/Runner.py b/Runner.py index 0514c39..440899b 100644 --- a/Runner.py +++ b/Runner.py @@ -2,11 +2,14 @@ import os #Create directory for files to append in the main file +#Extract information from preferences and subscribe method to create required objects +os.system("python Extractor/ExtractorScript.py") + #Files for both Apps Out = open("main.groovy", "w+") GlobalVariablesBothApps = open("GlobalVariables/"+"GlobalVariablesBothApps.groovy", "r") outGlobal = open("Extractor/"+"outGlobal.groovy", "r") - +eventSimulator = open("eventSimulator/"+"eventSimulator.groovy", "r") #For App1 GlobalVariablesEachApp = open("GlobalVariables/"+"GlobalVariablesEachApp.groovy", "r") @@ -16,14 +19,16 @@ runIn = open("Methods/"+"runIn.groovy", "r") unschedule = open("Methods/"+"unschedule.groovy", "r") sendNotificationToContacts = open("Methods/"+"sendNotificationToContacts.groovy", "r") sendSms = open("Methods/"+"sendSms.groovy", "r") +sendPush = open("Methods/"+"sendPush.groovy", "r") eventHandler = open("Methods/"+"eventHandler.groovy", "r") -App1 = open("Extractor/"+"App1.groovy", "r") -extractedObjectsApp1 = open("Extractor/"+"extractedObjectsApp1.groovy", "r") -extractedObjectsConstructorApp1 = open("Extractor/"+"extractedObjectsConstructorApp1.groovy", "r") +schedule = open("Methods/"+"schedule.groovy", "r") +App1 = open("Extractor/"+"App1/App1.groovy", "r") +extractedObjectsApp1 = open("Extractor/"+"App1/extractedObjectsApp1.groovy", "r") +extractedObjectsConstructorApp1 = open("Extractor/"+"App1/extractedObjectsConstructorApp1.groovy", "r") +extractedFunctionsApp1 = open("Extractor/"+"App1/extractedFunctionsApp1.groovy", "r") + -#Extract information from preferences and subscribe method to create required objects -os.system("python Extractor/ExtractorScript.py") Out.write("//Infrastructure for SmartThings Application\n") Out.write("//Importing Libraries\n") @@ -41,6 +46,7 @@ Out.write("import Location.LocationVar\n") Out.write("import Location.Phrase\n") Out.write("import appTouch.Touched\n") Out.write("import Event.Event\n") +Out.write("import Timer.SimulatedTimer\n") Out.write("\n") Out.write("//Global eventHandler\n") for line in eventHandler: @@ -64,6 +70,10 @@ Out.write("\t//Extracted objects for App1\n") for line in extractedObjectsApp1: Out.write("\t"+line) Out.write("\n") +Out.write("\t//Extracted objects for functions for App1\n") +for line in extractedFunctionsApp1: + 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") @@ -88,6 +98,10 @@ for line in sendNotificationToContacts: Out.write("\t"+line) for line in sendSms: Out.write("\t"+line) +for line in sendPush: + Out.write("\t"+line) +for line in schedule: + Out.write("\t"+line) Out.write("\n") Start = 0 for line in App1: @@ -109,9 +123,11 @@ 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") +schedule = open("Methods/"+"schedule.groovy", "r") +App2 = open("Extractor/"+"App2/App2.groovy", "r") +extractedObjectsApp2 = open("Extractor/"+"App2/extractedObjectsApp2.groovy", "r") +extractedObjectsConstructorApp2 = open("Extractor/"+"App2/extractedObjectsConstructorApp2.groovy", "r") +extractedFunctionsApp2 = open("Extractor/"+"App2/extractedFunctionsApp2.groovy", "r") Out.write("//Application #2\n") Out.write("class App2 {\n") @@ -123,6 +139,10 @@ Out.write("\t//Extracted objects for App2\n") for line in extractedObjectsApp2: Out.write("\t"+line) Out.write("\n") +Out.write("\t//Extracted objects for functions for App2\n") +for line in extractedFunctionsApp2: + 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") @@ -147,6 +167,10 @@ for line in sendNotificationToContacts: Out.write("\t"+line) for line in sendSms: Out.write("\t"+line) +for line in sendPush: + Out.write("\t"+line) +for line in schedule: + Out.write("\t"+line) Out.write("\n") Start = 0 for line in App2: @@ -160,6 +184,9 @@ 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.write("\n") +for line in eventSimulator: + Out.write("\t"+line) Out.close() diff --git a/Switch/Switches.groovy b/Switch/Switches.groovy index f5e59e5..4071de5 100644 --- a/Switch/Switches.groovy +++ b/Switch/Switches.groovy @@ -1,30 +1,31 @@ //Create a class for switch device package Switch +import Timer.SimulatedTimer public class Switches { private int id = 0 private String displayName - private String switchCurrentValue + private String switchState private String switchLatestValue def sendEvent def timers - Switches(Closure sendEvent, int id, String displayName, String switchCurrentValue, String switchLatestValue) { + Switches(Closure sendEvent, int id, String displayName, String switchState, String switchLatestValue) { this.sendEvent = sendEvent - this.timers = new Timer() + this.timers = new SimulatedTimer() this.id = id this.displayName = displayName - this.switchCurrentValue = switchCurrentValue + this.switchState = switchState this.switchLatestValue = switchLatestValue } //By Apps def on() { println("the switch with id:$id is on!") - this.switchLatestValue = this.switchCurrentValue - this.switchCurrentValue = "on" + this.switchLatestValue = this.switchState + this.switchState = "on" sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } @@ -32,8 +33,8 @@ public class Switches { 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" + this.switchLatestValue = this.switchState + this.switchState = "on" sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } @@ -41,8 +42,8 @@ public class Switches { def off() { println("the switch with id:$id is off!") - this.switchLatestValue = this.switchCurrentValue - this.switchCurrentValue = "off" + this.switchLatestValue = this.switchState + this.switchState = "off" sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } @@ -50,8 +51,8 @@ public class Switches { 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" + this.switchLatestValue = this.switchState + this.switchState = "off" sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } @@ -60,13 +61,13 @@ public class Switches { //By Model Checker def setValue(String value) { println("the switch with id:$id is $value!") - this.switchLatestValue = this.switchCurrentValue - this.switchCurrentValue = value + this.switchLatestValue = this.switchState + this.switchState = value } def currentValue(String deviceFeature) { if (deviceFeature == "switch") { - return switchCurrentValue + return switchState } } diff --git a/Switch/Switching.groovy b/Switch/Switching.groovy index f65bd41..34b58ef 100644 --- a/Switch/Switching.groovy +++ b/Switch/Switching.groovy @@ -1,5 +1,6 @@ //Create a class for switch device package Switch +import Timer.SimulatedTimer public class Switching{ int deviceNumbers @@ -7,13 +8,19 @@ public class Switching{ def timers def sendEvent + //If we have only one device + private int id = 0 + private String displayName = "switch0" + private String switchState = "off" + private String switchLatestValue = "off" + Switching(Closure sendEvent, int deviceNumbers) { this.sendEvent = sendEvent - this.timers = new Timer() + this.timers = new SimulatedTimer() this.deviceNumbers = deviceNumbers this.switches = [] if (deviceNumbers == 1) { - switches = [new Switches(sendEvent, 0, "switch0", "off", "off")] + switches = [new Switches(sendEvent, this.id, this.displayName, this.switchState, this.switchLatestValue)] } else if (deviceNumbers == 2) { switches = [new Switches(sendEvent, 0, "switch0", "off", "off"), new Switches(sendEvent, 1, "switch1", "off", "off")] } else if (deviceNumbers == 3) { @@ -46,6 +53,9 @@ public class Switching{ //By Model Checker def setValue(LinkedHashMap eventDataMap) { switches[eventDataMap["deviceId"]].setValue(eventDataMap["value"]) + if (deviceNumbers == 1) + this.switchState = switches[eventDataMap["deviceId"]].switchState + this.switchLatestValue = switches[eventDataMap["deviceId"]].switchLatestValue sendEvent(eventDataMap) } diff --git a/Timer/SimulatedTimer.groovy b/Timer/SimulatedTimer.groovy new file mode 100644 index 0000000..a133766 --- /dev/null +++ b/Timer/SimulatedTimer.groovy @@ -0,0 +1,28 @@ +//Create a class for timer +package Timer + +public class SimulatedTimer { + private Thread thread; + + SimulatedTimer() { + thread = null; + } + + //By Apps + def runAfter(int delay, Closure closure) { + thread = new Thread() { + + @Override + public void run() { + Thread.sleep(delay) + closure() + } + }.start() + return thread + } + + def cancel() { + if (thread != null) + thread.stop() + } +} diff --git a/bin/main/App1.class b/bin/main/App1.class deleted file mode 100644 index 796d062..0000000 Binary files a/bin/main/App1.class and /dev/null differ diff --git a/bin/main/App2.class b/bin/main/App2.class deleted file mode 100644 index 22dbf9b..0000000 Binary files a/bin/main/App2.class and /dev/null differ diff --git a/bin/main/ContactSensor/Contacting.class b/bin/main/ContactSensor/Contacting.class deleted file mode 100644 index 5f5e0b1..0000000 Binary files a/bin/main/ContactSensor/Contacting.class and /dev/null differ diff --git a/bin/main/ContactSensor/Contacts.class b/bin/main/ContactSensor/Contacts.class deleted file mode 100644 index 51bed71..0000000 Binary files a/bin/main/ContactSensor/Contacts.class and /dev/null differ diff --git a/bin/main/Event/Event.class b/bin/main/Event/Event.class deleted file mode 100644 index 2f7f0e2..0000000 Binary files a/bin/main/Event/Event.class and /dev/null differ diff --git a/bin/main/Location/LocationVar.class b/bin/main/Location/LocationVar.class deleted file mode 100644 index 6b600fd..0000000 Binary files a/bin/main/Location/LocationVar.class and /dev/null differ diff --git a/bin/main/Location/Phrase.class b/bin/main/Location/Phrase.class deleted file mode 100644 index 9c29d56..0000000 Binary files a/bin/main/Location/Phrase.class and /dev/null differ diff --git a/bin/main/Lock/Locking$_lock_closure1.class b/bin/main/Lock/Locking$_lock_closure1.class deleted file mode 100644 index 516079e..0000000 Binary files a/bin/main/Lock/Locking$_lock_closure1.class and /dev/null differ diff --git a/bin/main/Lock/Locking$_unlock_closure2.class b/bin/main/Lock/Locking$_unlock_closure2.class deleted file mode 100644 index 90197ad..0000000 Binary files a/bin/main/Lock/Locking$_unlock_closure2.class and /dev/null differ diff --git a/bin/main/Lock/Locking.class b/bin/main/Lock/Locking.class deleted file mode 100644 index 9e966cf..0000000 Binary files a/bin/main/Lock/Locking.class and /dev/null differ diff --git a/bin/main/Lock/Locks$_lock_closure1.class b/bin/main/Lock/Locks$_lock_closure1.class deleted file mode 100644 index ca70ed1..0000000 Binary files a/bin/main/Lock/Locks$_lock_closure1.class and /dev/null differ diff --git a/bin/main/Lock/Locks$_unlock_closure2.class b/bin/main/Lock/Locks$_unlock_closure2.class deleted file mode 100644 index 6a77013..0000000 Binary files a/bin/main/Lock/Locks$_unlock_closure2.class and /dev/null differ diff --git a/bin/main/Lock/Locks.class b/bin/main/Lock/Locks.class deleted file mode 100644 index 93aa14a..0000000 Binary files a/bin/main/Lock/Locks.class and /dev/null differ diff --git a/bin/main/Logger/Logger.class b/bin/main/Logger/Logger.class deleted file mode 100644 index 492d200..0000000 Binary files a/bin/main/Logger/Logger.class and /dev/null differ diff --git a/bin/main/Switch/Switches$_off_closure2.class b/bin/main/Switch/Switches$_off_closure2.class deleted file mode 100644 index 973337d..0000000 Binary files a/bin/main/Switch/Switches$_off_closure2.class and /dev/null differ diff --git a/bin/main/Switch/Switches$_on_closure1.class b/bin/main/Switch/Switches$_on_closure1.class deleted file mode 100644 index cd4028c..0000000 Binary files a/bin/main/Switch/Switches$_on_closure1.class and /dev/null differ diff --git a/bin/main/Switch/Switches.class b/bin/main/Switch/Switches.class deleted file mode 100644 index d246380..0000000 Binary files a/bin/main/Switch/Switches.class and /dev/null differ diff --git a/bin/main/Switch/Switching$_off_closure2.class b/bin/main/Switch/Switching$_off_closure2.class deleted file mode 100644 index 7fdefd3..0000000 Binary files a/bin/main/Switch/Switching$_off_closure2.class and /dev/null differ diff --git a/bin/main/Switch/Switching$_on_closure1.class b/bin/main/Switch/Switching$_on_closure1.class deleted file mode 100644 index 29e4146..0000000 Binary files a/bin/main/Switch/Switching$_on_closure1.class and /dev/null differ diff --git a/bin/main/Switch/Switching.class b/bin/main/Switch/Switching.class deleted file mode 100644 index 60f69f8..0000000 Binary files a/bin/main/Switch/Switching.class and /dev/null differ diff --git a/bin/main/appTouch/Touched.class b/bin/main/appTouch/Touched.class deleted file mode 100644 index 563a7e4..0000000 Binary files a/bin/main/appTouch/Touched.class and /dev/null differ diff --git a/bin/main/main$_closure1.class b/bin/main/main$_closure1.class deleted file mode 100644 index b5c9a14..0000000 Binary files a/bin/main/main$_closure1.class and /dev/null differ diff --git a/bin/main/main.class b/bin/main/main.class deleted file mode 100644 index c1a6055..0000000 Binary files a/bin/main/main.class and /dev/null differ diff --git a/eventSimulator/eventSimulator.groovy b/eventSimulator/eventSimulator.groovy new file mode 100644 index 0000000..c414b3a --- /dev/null +++ b/eventSimulator/eventSimulator.groovy @@ -0,0 +1,68 @@ +// Generate a random variable +Random random = new Random(1131) +int nextRandom = 10 + +// Touch events +new Thread() { + @Override + public void run() { + while(true) { + appObject.setValue([name: "Touched", value: "Touched", deviceId: 0, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + Thread.sleep(random.nextInt(nextRandom)); + + } + } +}.start() + +// Lock events +new Thread() { + + @Override + public void run() { + while(true) { + lockObject.setValue([name: "lock", value: "locked", deviceId: 0, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + Thread.sleep(random.nextInt(nextRandom)); + } + } +}.start() + +new Thread() { + + @Override + public void run() { + while(true) { + lockObject.setValue([name: "unlock", value: "unlocked", deviceId: 0, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + Thread.sleep(random.nextInt(nextRandom)); + } + } +}.start() + +// Contact sensor events +new Thread() { + + @Override + public void run() { + while(true) { + contactObject.setValue([name: "contact.open", value: "open", deviceId: 0, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + Thread.sleep(random.nextInt(nextRandom)); + + } + } +}.start() + +new Thread() { + + @Override + public void run() { + while(true) { + contactObject.setValue([name: "contact.closed", value: "closed", deviceId: 0, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + Thread.sleep(random.nextInt(nextRandom)); + + } + } +}.start() diff --git a/main.groovy b/main.groovy index b08146d..d0d5796 100644 --- a/main.groovy +++ b/main.groovy @@ -14,6 +14,7 @@ import Location.LocationVar import Location.Phrase import appTouch.Touched import Event.Event +import Timer.SimulatedTimer //Global eventHandler ///////////////////////////////////////////////////////////////////// @@ -27,87 +28,22 @@ def eventHandler(LinkedHashMap eventDataMap) { def isStateChange = eventDataMap["isStateChange"] def unit = eventDataMap["unit"] def data = eventDataMap["data"] - def minSize - def smallest - //make search efficient - if (app1.eventList.size() == app2.eventList.size()) { - minSize = app1.eventList.size() - smallest = "equal" - } else if (app1.eventList.size() < app2.eventList.size()) { - minSize = app1.eventList.size() - smallest = "app1" - } else { - minSize = app2.eventList.size() - smallest = "app2" - } - - for (int i = 0;i < minSize;i++) { - if (app1.eventList[i] == name) { - evt.add(new Event()) - evt[-1].value = value - evt[-1].name = name - evt[-1].deviceId = deviceId - evt[-1].descriptionText = descriptionText - evt[-1].displayed = displayed - evt[-1].linkText = linkText - evt[-1].displayName = linkText - evt[-1].isStateChange = isStateChange - evt[-1].unit = unit - evt[-1].data = data - app1.functionList[i](evt[-1]) - } + for (int i = 0;i < app2.eventList.size();i++) { if (app2.eventList[i] == name) { - evt.add(new Event()) - evt[-1].value = value - evt[-1].name = name - evt[-1].deviceId = deviceId - evt[-1].descriptionText = descriptionText - evt[-1].displayed = displayed - evt[-1].linkText = linkText - evt[-1].displayName = linkText - evt[-1].isStateChange = isStateChange - evt[-1].unit = unit - evt[-1].data = data - app2.functionList[i](evt[-1]) + def event = new Event(value, name, deviceId, descriptionText, displayed, linkText, linkText, isStateChange, unit, data) + evt.add(event) + app2.functionList[i](event) } } - if (smallest == "app1") { - for (int i = minSize;i < app2.eventList.size();i++) { - if (app2.eventList[i] == name) { - evt.add(new Event()) - evt[-1].value = value - evt[-1].name = name - evt[-1].deviceId = deviceId - evt[-1].descriptionText = descriptionText - evt[-1].displayed = displayed - evt[-1].linkText = linkText - evt[-1].displayName = linkText - evt[-1].isStateChange = isStateChange - evt[-1].unit = unit - evt[-1].data = data - app2.functionList[i](evt[-1]) - } - } - } else if (smallest == "app2") { - for (int i = minSize;i < app1.eventList.size();i++) { - if (app1.eventList[i] == name) { - evt.add(new Event()) - evt[-1].value = value - evt[-1].name = name - evt[-1].deviceId = deviceId - evt[-1].descriptionText = descriptionText - evt[-1].displayed = displayed - evt[-1].linkText = linkText - evt[-1].displayName = linkText - evt[-1].isStateChange = isStateChange - evt[-1].unit = unit - evt[-1].data = data - app1.functionList[i](evt[-1]) - } + for (int i = 0;i < app1.eventList.size();i++) { + if (app1.eventList[i] == name) { + def event = new Event(value, name, deviceId, descriptionText, displayed, linkText, linkText, isStateChange, unit, data) + evt.add(event) + app1.functionList[i](event) } - } + } } //GlobalVariables for both Apps @@ -124,10 +60,10 @@ def eventHandler(LinkedHashMap eventDataMap) { //Extracted global objects for both Apps //Global Object for class lock! -@Field def lockObject = new Locking(sendEvent, 1) +@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 contactObject = new Contacting(sendEvent,1) +//Global Object for class switch! @Field def switchObject = new Switching(sendEvent, 1) //Application #1 @@ -144,11 +80,13 @@ class App1 { //Global variable for number! def minutesLater = 1 //Global variable for number! - def secondsLater = 1 - //Global variable for recipients! - def recipients = ['AJ'] - //Global variable for phone number! + def secondsLater = 10 + //Global variable for contact! + def recipients = "AJ" + //Global variable for phone! def phoneNumber = 9495379373 + + //Extracted objects for functions for App1 //Global Object for functions in subscribe method! def installed = this.&installed //Global Object for functions in subscribe method! @@ -215,22 +153,36 @@ class App1 { ///////////////////////////////////////////////////////////////////// ////runIn(time, func) def runIn(int seconds, Closure functionToCall) { - timersFuncList.add(functionToCall) - timersList.add(new Timer()) - def task = timersList[-1].runAfter(1000*seconds, functionToCall) + if (timersFuncList.contains(functionToCall)) { + timersList[timersFuncList.indexOf(functionToCall)].cancel() + def task = timersList[timersFuncList.indexOf(functionToCall)].runAfter(1000*seconds, functionToCall) + } else { + timersFuncList.add(functionToCall) + timersList.add(new SimulatedTimer()) + def task = timersList[timersFuncList.indexOf(functionToCall)].runAfter(1000*seconds, functionToCall) + } } ///////////////////////////////////////////////////////////////////// ////unschedule(func) def unschedule(Closure functionToUnschedule) { for (int i = 0;i < timersFuncList.size();i++) { if (timersFuncList[i] == functionToUnschedule) { - timersList[i].cancel() + if (timersList != null) + timersList[i].cancel() } } } + + + def unschedule() { + for (int i = 0;i < timersFuncList.size();i++) { + if (timersList != null) + timersList[i].cancel() + } + } ///////////////////////////////////////////////////////////////////// ////sendNotificationToContacts(text, recipients) - def sendNotificationToContacts(String text, List recipients) { + def sendNotificationToContacts(String text, String recipients) { for (int i = 0;i < recipients.size();i++) { for (int j = 0;j < location.contacts.size();j++) { if (recipients[i] == location.contacts[j]) { @@ -244,6 +196,61 @@ class App1 { def sendSms(long phoneNumber, String text) { println("Sending \""+text+"\" to "+phoneNumber.toString()) } + ///////////////////////////////////////////////////////////////////// + ////sendPush(text) + def sendPush(String text) { + println(text) + } + ///////////////////////////////////////////////////////////////////// + ////schedule(time, nameOfFunction as String) + def schedule(String time, String nameOfFunction) { + def _inputTime = time.split(':') + Date date = new Date() + def _currentTime = date.format("HH:mm:ss").split(':') + + //Convert input time and current time to minutes + def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60 + def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2]) + def delay + + if (inputTime < currentTime) { + delay = 24*60*60-inputTime+currentTime + } else { + delay = inputTime-currentTime + } + + timersFuncList.add(nameOfFunction) + timersList.add(new SimulatedTimer()) + def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*1000) { + "$nameOfFunction"() + } + } + ////schedule(time, nameOfFunction as Closure) + def schedule(String time, Closure nameOfFunction) { + def _inputTime = time.split(':') + Date date = new Date() + def _currentTime = date.format("HH:mm:ss").split(':') + + //Convert input time and current time to minutes + def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60 + def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2]) + def delay + + if (inputTime < currentTime) { + delay = 24*60*60-inputTime+currentTime + } else { + delay = inputTime-currentTime + } + + if (timersFuncList.contains(nameOfFunction)) { + timersList[timersFuncList.indexOf(nameOfFunction)].cancel() + def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction) + } else { + timersFuncList.add(nameOfFunction) + timersList.add(new SimulatedTimer()) + def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction) + } + } def installed(){ initialize() @@ -340,16 +347,18 @@ class App2 { def app //Extracted objects for App2 - //Object for class Switch! + //Object for class switch! def switchesoff - //Object for class Switch! + //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 = 1 + def waitfor = 4 + + //Extracted objects for functions for App2 //Global Object for functions in subscribe method! def installed = this.&installed //Global Object for functions in subscribe method! @@ -411,22 +420,36 @@ class App2 { ///////////////////////////////////////////////////////////////////// ////runIn(time, func) def runIn(int seconds, Closure functionToCall) { - timersFuncList.add(functionToCall) - timersList.add(new Timer()) - def task = timersList[-1].runAfter(1000*seconds, functionToCall) + if (timersFuncList.contains(functionToCall)) { + timersList[timersFuncList.indexOf(functionToCall)].cancel() + def task = timersList[timersFuncList.indexOf(functionToCall)].runAfter(1000*seconds, functionToCall) + } else { + timersFuncList.add(functionToCall) + timersList.add(new SimulatedTimer()) + def task = timersList[timersFuncList.indexOf(functionToCall)].runAfter(1000*seconds, functionToCall) + } } ///////////////////////////////////////////////////////////////////// ////unschedule(func) def unschedule(Closure functionToUnschedule) { for (int i = 0;i < timersFuncList.size();i++) { if (timersFuncList[i] == functionToUnschedule) { - timersList[i].cancel() + if (timersList != null) + timersList[i].cancel() } } } + + + def unschedule() { + for (int i = 0;i < timersFuncList.size();i++) { + if (timersList != null) + timersList[i].cancel() + } + } ///////////////////////////////////////////////////////////////////// ////sendNotificationToContacts(text, recipients) - def sendNotificationToContacts(String text, List recipients) { + def sendNotificationToContacts(String text, String recipients) { for (int i = 0;i < recipients.size();i++) { for (int j = 0;j < location.contacts.size();j++) { if (recipients[i] == location.contacts[j]) { @@ -440,6 +463,56 @@ class App2 { def sendSms(long phoneNumber, String text) { println("Sending \""+text+"\" to "+phoneNumber.toString()) } + ///////////////////////////////////////////////////////////////////// + ////schedule(time, nameOfFunction as String) + def schedule(String time, String nameOfFunction) { + def _inputTime = time.split(':') + Date date = new Date() + def _currentTime = date.format("HH:mm:ss").split(':') + + //Convert input time and current time to minutes + def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60 + def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2]) + def delay + + if (inputTime < currentTime) { + delay = 24*60*60-inputTime+currentTime + } else { + delay = inputTime-currentTime + } + + timersFuncList.add(nameOfFunction) + timersList.add(new SimulatedTimer()) + def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*1000) { + "$nameOfFunction"() + } + } + ////schedule(time, nameOfFunction as Closure) + def schedule(String time, Closure nameOfFunction) { + def _inputTime = time.split(':') + Date date = new Date() + def _currentTime = date.format("HH:mm:ss").split(':') + + //Convert input time and current time to minutes + def inputTime = Integer.parseInt(_inputTime[0])*3600+Integer.parseInt(_inputTime[1])*60 + def currentTime = Integer.parseInt(_currentTime[0])*3600+Integer.parseInt(_currentTime[1])*60+Integer.parseInt(_currentTime[2]) + def delay + + if (inputTime < currentTime) { + delay = 24*60*60-inputTime+currentTime + } else { + delay = inputTime-currentTime + } + + if (timersFuncList.contains(nameOfFunction)) { + timersList[timersFuncList.indexOf(nameOfFunction)].cancel() + def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction) + } else { + timersFuncList.add(nameOfFunction) + timersList.add(new SimulatedTimer()) + def task = timersList[timersFuncList.indexOf(nameOfFunction)].runAfter(delay*seconds, nameOfFunction) + } + } def installed() { @@ -477,6 +550,72 @@ class App2 { @Field def app2 = new App2(this) app1.installed() app2.installed() -appObject.setValue([name: "Touched", value: "Touched", deviceId: 0, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + // Generate a random variable + Random random = new Random(1131) + int nextRandom = 10 + + // Touch events + new Thread() { + @Override + public void run() { + while(true) { + appObject.setValue([name: "Touched", value: "Touched", deviceId: 0, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + Thread.sleep(random.nextInt(nextRandom)); + + } + } + }.start() + + // Lock events + new Thread() { + + @Override + public void run() { + while(true) { + lockObject.setValue([name: "lock", value: "locked", deviceId: 0, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + Thread.sleep(random.nextInt(nextRandom)); + } + } + }.start() + + new Thread() { + + @Override + public void run() { + while(true) { + lockObject.setValue([name: "unlock", value: "unlocked", deviceId: 0, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + Thread.sleep(random.nextInt(nextRandom)); + } + } + }.start() + + // Contact sensor events + new Thread() { + + @Override + public void run() { + while(true) { + contactObject.setValue([name: "contact.open", value: "open", deviceId: 0, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + Thread.sleep(random.nextInt(nextRandom)); + + } + } + }.start() + + new Thread() { + + @Override + public void run() { + while(true) { + contactObject.setValue([name: "contact.closed", value: "closed", deviceId: 0, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) + Thread.sleep(random.nextInt(nextRandom)); + + } + } + }.start()