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
14 import Location.locationVar
15 import Location.Phrase
19 //create a location object to change the variable inside the class
20 @Field def location = new locationVar()
21 //Settings variable defined to settings on purpose
22 @Field def settings = "Settings"
23 //Global variable for state[mode]
24 @Field def state = [home:[],away:[],night:[]]
25 //Create a global logger object for methods
26 @Field def log = new Logger()
27 //Create a global object for app
28 @Field def app = new Touch(1)
29 //Create a global list for objects for events on subscribe methods
30 @Field def ObjectList = []
31 //Create a global list for events
32 @Field def EventList = []
33 //Create a global list for function calls based on corresponding events
34 @Field def FunctionList = []
35 //Create a global list for function schedulers
36 @Field def ListofTimersFunc = []
37 //Create a global list for timer schedulers
38 @Field def ListofTimers = []
43 //Global Object for class switch!
44 @Field def switchesoff = new switching(3)
45 //Global Object for class switch!
46 @Field def switcheson = new switching(3)
47 //Global Object for class lock!
48 @Field def lock1 = new locking(2)
49 //Global variable for mode!
50 @Field def newMode = "home"
51 //Global variable for number!
52 @Field def waitfor = 10
53 //Global Object for functions in subscribe method!
54 @Field def installed = this.&installed
55 //Global Object for functions in subscribe method!
56 @Field def updated = this.&updated
57 //Global Object for functions in subscribe method!
58 @Field def appTouch = this.&appTouch
61 /////////////////////////////////////////////////////////////////////
62 def definition(LinkedHashMap LHM) {
63 println("IGNORE -- JUST SOME DEFINITION")
66 /////////////////////////////////////////////////////////////////////
67 def preferences(Closure Input) {
68 println("IGNORE -- JUST SOME DEFINITION")
70 /////////////////////////////////////////////////////////////////////
71 def setLocationMode(String mode) {
75 /////////////////////////////////////////////////////////////////////
76 ////subscribe(app, func)
77 def subscribe(Object Obj, Closure Input) {
79 EventList.add("Touched")
80 FunctionList.add(Input)
82 ////subscribe(obj, string, func)
83 def subscribe(Object Obj, String S, Closure Input) {
86 FunctionList.add(Input)
88 ////subscribe(obj, string, func, hashmap)
89 def subscribe(Object Obj, String S, Closure Input, LinkedHashMap LHM) {
92 FunctionList.add(Input)
94 /////////////////////////////////////////////////////////////////////
98 print "Waiting for an event...\n"
99 def EVENT = System.in.newReader().readLine()
100 SepLine = EVENT.split()
101 for (int i = 0; i < EventList.size(); i++) {
102 if (EventList[i] == SepLine[0]) {
103 println("The following effect: \n")
107 ObjectList[i].touched = 1
108 evt[-1].value = "Touched"
109 evt[-1].linkText = "touched by user"
110 evt[-1].name = "TouchSensor"
111 evt[-1].descriptionText = "Touching"
114 if (SepLine[1] == "0") {
115 ObjectList[i][0].lock()
117 evt[-1].value = "locked"
118 evt[-1].linkText = "lock0"
119 evt[-1].displayName = "lock0"
120 evt[-1].name = "lock"
121 evt[-1].descriptionText = "locking"
122 } else if (SepLine[1] == "1") {
123 ObjectList[i][1].lock()
125 evt[-1].value = "locked"
126 evt[-1].linkText = "lock1"
127 evt[-1].displayName = "lock1"
128 evt[-1].name = "lock"
129 evt[-1].descriptionText = "locking"
130 } else if (SepLine[1] == "2") {
131 ObjectList[i][2].lock()
133 evt[-1].value = "locked"
134 evt[-1].linkText = "lock2"
135 evt[-1].displayName = "lock2"
136 evt[-1].name = "lock"
137 evt[-1].descriptionText = "locking"
141 if (SepLine[1] == "0") {
142 ObjectList[i][0].unlock()
144 evt[-1].value = "unlocked"
145 evt[-1].linkText = "lock0"
146 evt[-1].displayName = "lock0"
147 evt[-1].name = "lock"
148 evt[-1].descriptionText = "unlocking"
149 } else if (SepLine[1] == "1") {
150 ObjectList[i][1].unlock()
152 evt[-1].value = "unlocked"
153 evt[-1].linkText = "lock1"
154 evt[-1].displayName = "lock1"
155 evt[-1].name = "lock"
156 evt[-1].descriptionText = "unlocking"
157 } else if (SepLine[1] == "2") {
158 ObjectList[i][2].unlock()
160 evt[-1].value = "unlocked"
161 evt[-1].linkText = "lock2"
162 evt[-1].displayName = "lock2"
163 evt[-1].name = "lock"
164 evt[-1].descriptionText = "unlocking"
168 if (SepLine[1] == "0") {
169 ObjectList[i][0].contactLatestValue = ObjectList[i].currentContact
170 ObjectList[i][0].currentContact = "open"
172 evt[-1].value = "contact.open"
173 evt[-1].linkText = "contact0"
174 evt[-1].displayName = "contact0"
175 evt[-1].name = "ContactSensor"
176 evt[-1].descriptionText = "opening"
177 } else if (SepLine[1] == "1") {
178 ObjectList[i][1].contactLatestValue = ObjectList[i].currentContact
179 ObjectList[i][1].currentContact = "open"
181 evt[-1].value = "contact.open"
182 evt[-1].linkText = "contact1"
183 evt[-1].displayName = "contact1"
184 evt[-1].name = "ContactSensor"
185 evt[-1].descriptionText = "opening"
186 } else if (SepLine[1] == "2") {
187 ObjectList[i][2].contactLatestValue = ObjectList[i].currentContact
188 ObjectList[i][2].currentContact = "open"
190 evt[-1].value = "contact.open"
191 evt[-1].linkText = "contact2"
192 evt[-1].displayName = "contact2"
193 evt[-1].name = "ContactSensor"
194 evt[-1].descriptionText = "opening"
197 case "contact.closed":
198 if (SepLine[1] == "0") {
199 ObjectList[i][0].contactLatestValue = ObjectList[i].currentContact
200 ObjectList[i][0].currentContact = "closed"
202 evt[-1].value = "contact.closed"
203 evt[-1].linkText = "contact0"
204 evt[-1].displayName = "contact0"
205 evt[-1].name = "ContactSensor"
206 evt[-1].descriptionText = "closing"
207 } else if (SepLine[1] == "1") {
208 ObjectList[i][1].contactLatestValue = ObjectList[i].currentContact
209 ObjectList[i][1].currentContact = "closed"
211 evt[-1].value = "contact.closed"
212 evt[-1].linkText = "contact1"
213 evt[-1].displayName = "contact1"
214 evt[-1].name = "ContactSensor"
215 evt[-1].descriptionText = "closing"
216 } else if (SepLine[1] == "2") {
217 ObjectList[i][2].contactLatestValue = ObjectList[i].currentContact
218 ObjectList[i][2].currentContact = "closed"
220 evt[-1].value = "contact.closed"
221 evt[-1].linkText = "contact2"
222 evt[-1].displayName = "contact2"
223 evt[-1].name = "ContactSensor"
224 evt[-1].descriptionText = "closing"
230 FunctionList[i](evt[-1])
235 /////////////////////////////////////////////////////////////////////
236 ////runIn(time, func)
237 def runIn(int seconds, Closure Input) {
238 ListofTimersFunc.add(Input)
239 ListofTimers.add(new Timer())
240 def task = ListofTimers[-1].runAfter(1000*seconds, Input)
242 /////////////////////////////////////////////////////////////////////
244 def unschedule(Closure Input) {
245 for (int i = 0;i < ListofTimersFunc.size();i++) {
246 if (ListofTimersFunc[i] == Input) {
247 ListofTimers[i].cancel()
251 /////////////////////////////////////////////////////////////////////
252 ////sendNotificationToContacts(text, recipients)
253 def sendNotificationToContacts(String S, List recipients) {
254 for (int i = 0;i < recipients.size();i++) {
255 for (int j = 0;j < location.CONTACTS.size();j++) {
256 if (recipients[i] == location.CONTACTS[j]) {
257 println("Sending \""+S+"\" to "+location.PhoneNums[j].toString())
262 /////////////////////////////////////////////////////////////////////
263 ////sendNotificationToContacts(text, recipients)
264 def sendSms(long Phone, String S) {
265 println("Sending \""+S+"\" to "+Phone.toString())
271 * Copyright 2014 Joseph Charette
273 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
274 * in compliance with the License. You may obtain a copy of the License at:
276 * http://www.apache.org/licenses/LICENSE-2.0
278 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
279 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
280 * for the specific language governing permissions and limitations under the License.
284 name: "Good Night House",
285 namespace: "charette.joseph@gmail.com",
286 author: "Joseph Charette",
287 description: "Some on, some off with delay for bedtime, Lock The Doors",
288 category: "Convenience",
289 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
290 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png"
293 * Walk Gentle Into That Good Night
295 * Author: oneaccttorulethehouse@gmail.com
300 section("When I touch the app turn these lights off…"){
301 input "switchesoff", "capability.switch", multiple: true, required:true
303 section("When I touch the app turn these lights on…"){
304 input "switcheson", "capability.switch", multiple: true, required:false
306 section("Lock theses locks...") {
307 input "lock1","capability.lock", multiple: true
309 section("And change to this mode...") {
310 input "newMode", "mode", title: "Mode?"
312 section("After so many seconds (optional)"){
313 input "waitfor", "number", title: "Off after (default 120)", required: true
320 log.debug "Installed with settings: ${settings}"
321 log.debug "Current mode = ${location.mode}"
322 subscribe(app, appTouch)
328 log.debug "Updated with settings: ${settings}"
329 log.debug "Current mode = ${location.mode}"
331 subscribe(app, appTouch)
335 log.debug "changeMode, location.mode = $location.mode, newMode = $newMode, location.modes = $location.modes"
336 if (location.mode != newMode) {
337 setLocationMode(newMode)
338 log.debug "Changed the mode to '${newMode}'"
340 log.debug "New mode is the same as the old mode, leaving it be"
342 log.debug "appTouch: $evt"
345 def delay = (waitfor != null && waitfor != "") ? waitfor * 1000 : 120000
346 switchesoff.off(delay: delay)