Commit #3
[smartthings-infrastructure.git] / main.groovy
1 //Infrastructure for SmartThings Application
2 //Importing Libraries
3 import groovy.transform.Field
4
5 //Importing Classes
6 import ContactSensor.contacting
7 import ContactSensor.contacts
8 import Lock.locking
9 import Lock.locks
10 import Switch.switching
11 import Switch.switches
12 import Event.Event
13 import Logger.Logger
14 import Location.locationVar
15 import Location.Phrase
16 import appTouch.Touch
17
18 //GlobalVariables
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 = []
39
40
41
42 //ExtractedObjects
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
59
60 //Methods
61 /////////////////////////////////////////////////////////////////////
62 def definition(LinkedHashMap LHM) {
63         println("IGNORE -- JUST SOME DEFINITION")
64 }
65
66 /////////////////////////////////////////////////////////////////////
67 def preferences(Closure Input) {
68         println("IGNORE -- JUST SOME DEFINITION")
69 }
70 /////////////////////////////////////////////////////////////////////
71 def setLocationMode(String mode) {
72         location.mode = mode
73 }
74
75 /////////////////////////////////////////////////////////////////////
76 ////subscribe(app, func)
77 def subscribe(Object Obj, Closure Input) {
78         ObjectList.add(Obj)
79         EventList.add("Touched")
80         FunctionList.add(Input)
81 }
82 ////subscribe(obj, string, func)
83 def subscribe(Object Obj, String S, Closure Input) {
84         ObjectList.add(Obj)
85         EventList.add(S)
86         FunctionList.add(Input)
87 }
88 ////subscribe(obj, string, func, hashmap)
89 def subscribe(Object Obj, String S, Closure Input, LinkedHashMap LHM) {
90         ObjectList.add(Obj)     
91         EventList.add(S)
92         FunctionList.add(Input)
93 }
94 /////////////////////////////////////////////////////////////////////
95 def EventHandler() {
96         while(true) {
97                 List evt = []
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")
104                                 evt.add(new Event())
105                                 switch(SepLine[0]) { 
106                                         case "Touched":
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"
112                                                 break
113                                         case "lock":
114                                                 if (SepLine[1] == "0") {
115                                                         ObjectList[i][0].lock()
116                                                         evt[-1].deviceId = 0
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()
124                                                         evt[-1].deviceId = 1
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()
132                                                         evt[-1].deviceId = 2
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"
138                                                 }
139                                                 break
140                                         case "unlock":
141                                                 if (SepLine[1] == "0") {
142                                                         ObjectList[i][0].unlock()
143                                                         evt[-1].deviceId = 0
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()
151                                                         evt[-1].deviceId = 0
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()
159                                                         evt[-1].deviceId = 2
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"
165                                                 }
166                                                 break
167                                         case "contact.open":
168                                                 if (SepLine[1] == "0") {
169                                                         ObjectList[i][0].contactLatestValue = ObjectList[i].currentContact
170                                                         ObjectList[i][0].currentContact = "open"
171                                                         evt[-1].deviceId = 0
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"
180                                                         evt[-1].deviceId = 1
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"
189                                                         evt[-1].deviceId = 2
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"
195                                                 }
196                                                 break
197                                         case "contact.closed":
198                                                 if (SepLine[1] == "0") {
199                                                         ObjectList[i][0].contactLatestValue = ObjectList[i].currentContact
200                                                         ObjectList[i][0].currentContact = "closed"
201                                                         evt[-1].deviceId = 0
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"
210                                                         evt[-1].deviceId = 1
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"
219                                                         evt[-1].deviceId = 2
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"
225                                                 }
226                                                 break
227                                         default:
228                                                 break
229                                 }
230                                 FunctionList[i](evt[-1])
231                         }
232                 }
233         }
234 }
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)
241 }
242 /////////////////////////////////////////////////////////////////////
243 ////unschedule(func)
244 def unschedule(Closure Input) {
245         for (int i = 0;i < ListofTimersFunc.size();i++) {
246                 if (ListofTimersFunc[i] == Input) {
247                         ListofTimers[i].cancel()
248                 }
249         }
250 }
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())
258                         }
259                 }
260         }
261 }
262 /////////////////////////////////////////////////////////////////////
263 ////sendNotificationToContacts(text, recipients)
264 def sendSms(long Phone, String S) {
265         println("Sending \""+S+"\" to "+Phone.toString())
266 }
267
268 /**
269  *  Good Night House
270  *
271  *  Copyright 2014 Joseph Charette
272  *
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:
275  *
276  *      http://www.apache.org/licenses/LICENSE-2.0
277  *
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.
281  *
282  */
283 definition(
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"
291 /**
292 *  Borrowed code from
293 *  Walk Gentle Into That Good Night
294 *
295 *  Author: oneaccttorulethehouse@gmail.com
296 *  Date: 2014-02-01
297  */
298  )
299 preferences {
300         section("When I touch the app turn these lights off…"){
301                 input "switchesoff", "capability.switch", multiple: true, required:true
302         }
303     section("When I touch the app turn these lights on…"){
304                 input "switcheson", "capability.switch", multiple: true, required:false
305         }
306     section("Lock theses locks...") {
307                 input "lock1","capability.lock", multiple: true
308     }
309         section("And change to this mode...") {
310                 input "newMode", "mode", title: "Mode?"
311         }
312    section("After so many seconds (optional)"){
313                 input "waitfor", "number", title: "Off after (default 120)", required: true
314         }
315 }
316
317
318 def installed()
319 {
320         log.debug "Installed with settings: ${settings}"
321         log.debug "Current mode = ${location.mode}"
322         subscribe(app, appTouch)
323 }
324
325
326 def updated()
327 {
328         log.debug "Updated with settings: ${settings}"
329         log.debug "Current mode = ${location.mode}"
330         unsubscribe()
331         subscribe(app, appTouch)
332 }
333
334 def appTouch(evt) {
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}'"
339     }   else {
340         log.debug "New mode is the same as the old mode, leaving it be"
341         }
342     log.debug "appTouch: $evt"
343     lock1.lock()
344     switcheson.on()
345     def delay = (waitfor != null && waitfor != "") ? waitfor * 1000 : 120000
346         switchesoff.off(delay: delay)
347 }
348
349 installed()
350 EventHandler()