1 //Infrastructure for SmartThings Application
3 import groovy.transform.Field
6 import ContactSensor.Contacting
7 import ContactSensor.Contacts
10 import Switch.Switching
11 import Switch.Switches
13 import Location.LocationVar
14 import Location.Phrase
15 import appTouch.Touched
19 //Create a global variable for send event
20 @Field def sendEvent = {eventDataMap -> eventHandler(eventDataMap)}
21 //create a location object to change the variable inside the class
22 @Field def location = new LocationVar()
23 //Settings variable defined to settings on purpose
24 @Field def settings = "Settings"
25 //Global variable for state[mode]
26 @Field def state = [home:[],away:[],night:[]]
27 //Global object for touch
28 @Field def app = new Touched(sendEvent, 0)
29 //Create a global logger object for methods
30 @Field def log = new Logger()
31 //Create a global variable for Functions in Subscribe method
32 @Field def functionList = []
33 //Create a global variable for Objects in Subscribe method
34 @Field def objectList = []
35 //Create a global variable for Events in Subscribe method
36 @Field def eventList = []
37 //Create a global list for function schedulers
38 @Field def timersFuncList = []
39 //Create a global list for timer schedulers
40 @Field def timersList = []
41 //Create a global list for events
46 //Global Object for class switch!
47 @Field def switchesoff = new Switching(sendEvent, 1)
48 //Global Object for class switch!
49 @Field def switcheson = new Switching(sendEvent, 1)
50 //Global Object for class lock!
51 @Field def lock1 = new Locking(sendEvent, 1)
52 //Global variable for mode!
53 @Field def newMode = "away"
54 //Global variable for number!
55 @Field def waitfor = 10
56 //Global Object for functions in subscribe method!
57 @Field def installed = this.&installed
58 //Global Object for functions in subscribe method!
59 @Field def updated = this.&updated
60 //Global Object for functions in subscribe method!
61 @Field def appTouch = this.&appTouch
64 /////////////////////////////////////////////////////////////////////
65 def definition(LinkedHashMap metaData) {
66 println("IGNORE -- JUST SOME DEFINITION")
69 /////////////////////////////////////////////////////////////////////
70 def preferences(Closure metaData) {
71 println("IGNORE -- JUST SOME DEFINITION")
73 /////////////////////////////////////////////////////////////////////
74 def setLocationMode(String mode) {
78 /////////////////////////////////////////////////////////////////////
79 ////subscribe(obj, func)
80 def subscribe(Object obj, Closure FunctionToCall) {
82 eventList.add("Touched")
83 functionList.add(FunctionToCall)
85 ////subscribe(obj, event, func)
86 def subscribe(Object obj, String event, Closure FunctionToCall) {
89 functionList.add(FunctionToCall)
91 ////subscribe(obj, event, func, data)
92 def subscribe(Object obj, String event, Closure FunctionToCall, LinkedHashMap metaData) {
95 functionList.add(FunctionToCall)
97 /////////////////////////////////////////////////////////////////////
99 def runIn(int seconds, Closure functionToCall) {
100 timersFuncList.add(functionToCall)
101 timersList.add(new Timer())
102 def task = timersList[-1].runAfter(1000*seconds, functionToCall)
104 /////////////////////////////////////////////////////////////////////
106 def unschedule(Closure functionToUnschedule) {
107 for (int i = 0;i < timersFuncList.size();i++) {
108 if (timersFuncList[i] == functionToUnschedule) {
109 timersList[i].cancel()
113 /////////////////////////////////////////////////////////////////////
114 ////sendNotificationToContacts(text, recipients)
115 def sendNotificationToContacts(String text, List recipients) {
116 for (int i = 0;i < recipients.size();i++) {
117 for (int j = 0;j < location.contacts.size();j++) {
118 if (recipients[i] == location.contacts[j]) {
119 println("Sending \""+text+"\" to "+location.phoneNumbers[j].toString())
124 /////////////////////////////////////////////////////////////////////
125 ////sendSms(phone, text)
126 def sendSms(long phoneNumber, String text) {
127 println("Sending \""+text+"\" to "+phoneNumber.toString())
129 /////////////////////////////////////////////////////////////////////
130 def eventHandler(LinkedHashMap eventDataMap) {
131 def value = eventDataMap["value"]
132 def name = eventDataMap["name"]
133 def deviceId = eventDataMap["deviceId"]
134 def descriptionText = eventDataMap["descriptionText"]
135 def displayed = eventDataMap["displayed"]
136 def linkText = eventDataMap["linkText"]
137 def isStateChange = eventDataMap["isStateChange"]
138 def unit = eventDataMap["unit"]
139 def data = eventDataMap["data"]
141 for (int i = 0;i < eventList.size();i++) {
142 if (eventList[i] == value) {
144 evt[-1].value = value
146 evt[-1].deviceId = deviceId
147 evt[-1].descriptionText = descriptionText
148 evt[-1].displayed = displayed
149 evt[-1].linkText = linkText
150 evt[-1].displayName = linkText
151 evt[-1].isStateChange = isStateChange
154 functionList[i](evt[-1])
162 * Copyright 2014 Joseph Charette
164 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
165 * in compliance with the License. You may obtain a copy of the License at:
167 * http://www.apache.org/licenses/LICENSE-2.0
169 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
170 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
171 * for the specific language governing permissions and limitations under the License.
175 name: "Good Night House",
176 namespace: "charette.joseph@gmail.com",
177 author: "Joseph Charette",
178 description: "Some on, some off with delay for bedtime, Lock The Doors",
179 category: "Convenience",
180 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
181 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png"
184 * Walk Gentle Into That Good Night
186 * Author: oneaccttorulethehouse@gmail.com
191 section("When I touch the app turn these lights off…"){
192 input "switchesoff", "capability.switch", multiple: true, required:true
194 section("When I touch the app turn these lights on…"){
195 input "switcheson", "capability.switch", multiple: true, required:false
197 section("Lock theses locks...") {
198 input "lock1","capability.lock", multiple: true
200 section("And change to this mode...") {
201 input "newMode", "mode", title: "Mode?"
203 section("After so many seconds (optional)"){
204 input "waitfor", "number", title: "Off after (default 120)", required: true
211 log.debug "Installed with settings: ${settings}"
212 log.debug "Current mode = ${location.mode}"
213 subscribe(app, appTouch)
219 log.debug "Updated with settings: ${settings}"
220 log.debug "Current mode = ${location.mode}"
222 subscribe(app, appTouch)
226 log.debug "changeMode, location.mode = $location.mode, newMode = $newMode, location.modes = $location.modes"
227 if (location.mode != newMode) {
228 setLocationMode(newMode)
229 log.debug "Changed the mode to '${newMode}'"
231 log.debug "New mode is the same as the old mode, leaving it be"
233 log.debug "appTouch: $evt"
236 def delay = (waitfor != null && waitfor != "") ? waitfor * 1000 : 120000
237 switchesoff.off(delay: delay)