def app
//Extracted objects for App1
- //Object for class switch!
- def switches
- //Object for class thermostat!
- def thermostats
+ //Global variable for time!
+ def time = "15:00"
//Object for class lock!
- def locks
+ def lock
+ //Object for class contactSensor!
+ def contact
+ //Global variable for enum!
+ def sendPushMessage = "Yes"
+ //Global variable for phone!
+ def phone = 9495379373
//Extracted objects for functions for App1
//Global Object for functions in subscribe method!
//Global Object for functions in subscribe method!
def updated = this.&updated
//Global Object for functions in subscribe method!
- def appTouch = this.&appTouch
- //Global Object for functions in subscribe method!
- def changedLocationMode = this.&changedLocationMode
- //Global Object for functions in subscribe method!
- def restoreState = this.&restoreState
+ def setTimeCallback = this.&setTimeCallback
//Global Object for functions in subscribe method!
- def saveState = this.&saveState
+ def doorOpenCheck = this.&doorOpenCheck
//Global Object for functions in subscribe method!
- def getCurrentMode = this.&getCurrentMode
+ def lockMessage = this.&lockMessage
App1(Object obj) {
reference = obj
location = obj.locationObject
app = obj.appObject
- switches = obj.switchObject
- thermostats = obj.thermostatObject
- locks = obj.lockObject
+ lock = obj.lockObject
+ contact = obj.contactObject
//Global variable for settings!
- settings = [app:app, switches:switches, thermostats:thermostats, locks:locks]
+ settings = [app:app, time:time, lock:lock, contact:contact, sendPushMessage:sendPushMessage, phone:phone]
}
//Global variables for each app
//Global variable for state[mode]
}
def installed() {
- subscribe(location, changedLocationMode)
- subscribe(app, appTouch)
- saveState()
- }
+ schedule(time, "setTimeCallback")
- def updated() {
- unsubscribe()
- subscribe(location, changedLocationMode)
- subscribe(app, appTouch)
- saveState()
}
- def appTouch(evt)
- {
- restoreState(currentMode)
- }
-
- def changedLocationMode(evt)
- {
- restoreState(evt.value)
- }
-
- private restoreState(mode)
- {
- log.info "restoring state for mode '$mode'"
- def map = state[mode] ?: [:]
- switches?.each {
- def value = map[it.id]
- if (value?.switch == "on") {
- def level = value.level
- if (level) {
- log.debug "setting $it.label level to $level"
- it.setLevel(level)
- }
- else {
- log.debug "turning $it.label on"
- it.on()
- }
- }
- else if (value?.switch == "off") {
- log.debug "turning $it.label off"
- it.off()
- }
- }
-
- thermostats?.each {
- def value = map[it.id]
- if (value?.coolingSetpoint) {
- log.debug "coolingSetpoint = $value.coolingSetpoint"
- it.setCoolingSetpoint(value.coolingSetpoint)
- }
- if (value?.heatingSetpoint) {
- log.debug "heatingSetpoint = $value.heatingSetpoint"
- it.setHeatingSetpoint(value.heatingSetpoint)
- }
- }
-
- locks?.each {
- def value = map[it.id]
- if (value) {
- if (value?.locked) {
- it.lock()
- }
- else {
- it.unlock()
- }
- }
- }
+ def updated(settings) {
+ unschedule()
+ schedule(time, "setTimeCallback")
}
-
- private saveState()
- {
- def mode = currentMode
- def map = state[mode] ?: [:]
-
- switches?.each {
- map[it.id] = [switch: it.currentSwitch, level: it.currentLevel]
- }
-
- thermostats?.each {
- map[it.id] = [coolingSetpoint: it.currentCoolingSetpoint, heatingSetpoint: it.currentHeatingSetpoint]
- }
-
- locks?.each {
- map[it.id] = [locked: it.currentLock == "locked"]
- }
-
- state[mode] = map
- log.debug "saved state for mode ${mode}: ${state[mode]}"
- log.debug "state: $state"
+ def setTimeCallback() {
+ if (contact) {
+ doorOpenCheck()
+ } else {
+ lockMessage()
+ lock.lock()
+ }
}
-
- private getCurrentMode()
- {
- location.mode ?: "_none_"
+ def doorOpenCheck() {
+ def currentState = contact.contactState
+ if (currentState?.value == "open") {
+ def msg = "${contact.displayName} is open. Scheduled lock failed."
+ log.info msg
+ if (sendPushMessage) {
+ sendPush msg
+ }
+ if (phone) {
+ sendSms phone, msg
+ }
+ } else {
+ lockMessage()
+ lock.lock()
+ }
+ }
+
+ def lockMessage() {
+ def msg = "Locking ${lock.displayName} due to scheduled lock."
+ log.info msg
+ if (sendPushMessage) {
+ sendPush msg
+ }
+ if (phone) {
+ sendSms phone, msg
+ }
}
}
//Global variable for enum!
def masterLock = "Yes"
//Global variable for enum!
- def masterDoor = "No"
+ def masterDoor = "Yes"
//Extracted objects for functions for App2
//Global Object for functions in subscribe method!
app1.installed()
app2.installed()
-appObject.setValue([name: "Touched", value: "touched", deviceId: "touchedSensorID0", descriptionText: "",
-displayed: true, linkText: "", isStateChange: false, unit: "", data: [info: "info"]])
-appObject.setValue([name: "nfcTouch", value: "touched", deviceId: "nfcSensorID0", descriptionText: "",
-displayed: true, linkText: "", isStateChange: false, unit: "", data: [info: "info"]])
-locationObject.setValue([name: "Location", value: "away", deviceId: "locationID0", descriptionText: "",
-displayed: true, linkText: "", isStateChange: false, unit: "", data: [info: "info"]])
-locationObject.setValue([name: "Location", value: "home", deviceId: "locationID0", descriptionText: "",
-displayed: true, linkText: "", isStateChange: false, unit: "", data: [info: "info"]])
-locationObject.setValue([name: "Location", value: "night", deviceId: "locationID0", descriptionText: "",
-displayed: true, linkText: "", isStateChange: false, unit: "", data: [info: "info"]])