Commit #5
[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 Logger.Logger
13 import Location.LocationVar
14 import Location.Phrase
15 import appTouch.Touched
16 import Event.Event
17
18 //GlobalVariables
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
42 @Field def evt = []
43
44
45 //extractedObjects
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
62
63 //Methods
64 /////////////////////////////////////////////////////////////////////
65 def definition(LinkedHashMap metaData) {
66         println("IGNORE -- JUST SOME DEFINITION")
67 }
68
69 /////////////////////////////////////////////////////////////////////
70 def preferences(Closure metaData) {
71         println("IGNORE -- JUST SOME DEFINITION")
72 }
73 /////////////////////////////////////////////////////////////////////
74 def setLocationMode(String mode) {
75         location.mode = mode
76 }
77
78 /////////////////////////////////////////////////////////////////////
79 ////subscribe(obj, func)
80 def subscribe(Object obj, Closure FunctionToCall) {
81         objectList.add(obj)
82         eventList.add("Touched")
83         functionList.add(FunctionToCall)
84 }
85 ////subscribe(obj, event, func)
86 def subscribe(Object obj, String event, Closure FunctionToCall) {
87         objectList.add(obj)
88         eventList.add(event)
89         functionList.add(FunctionToCall)
90 }
91 ////subscribe(obj, event, func, data)
92 def subscribe(Object obj, String event, Closure FunctionToCall, LinkedHashMap metaData) {
93         objectList.add(obj)     
94         eventList.add(event)
95         functionList.add(FunctionToCall)
96 }
97 /////////////////////////////////////////////////////////////////////
98 ////runIn(time, func)
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)
103 }
104 /////////////////////////////////////////////////////////////////////
105 ////unschedule(func)
106 def unschedule(Closure functionToUnschedule) {
107         for (int i = 0;i < timersFuncList.size();i++) {
108                 if (timersFuncList[i] == functionToUnschedule) {
109                         timersList[i].cancel()
110                 }
111         }
112 }
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())
120                         }
121                 }
122         }
123 }
124 /////////////////////////////////////////////////////////////////////
125 ////sendSms(phone, text)
126 def sendSms(long phoneNumber, String text) {
127         println("Sending \""+text+"\" to "+phoneNumber.toString())
128 }
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"]
140         
141         for (int i = 0;i < eventList.size();i++) {
142                 if (eventList[i] == value) {
143                         evt.add(new Event())
144                         evt[-1].value = value
145                         evt[-1].name = name
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
152                         evt[-1].unit = unit
153                         evt[-1].data = data
154                         functionList[i](evt[-1])
155                 }
156         }
157 }
158
159 /**
160  *  Good Night House
161  *
162  *  Copyright 2014 Joseph Charette
163  *
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:
166  *
167  *      http://www.apache.org/licenses/LICENSE-2.0
168  *
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.
172  *
173  */
174 definition(
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"
182 /**
183 *  Borrowed code from
184 *  Walk Gentle Into That Good Night
185 *
186 *  Author: oneaccttorulethehouse@gmail.com
187 *  Date: 2014-02-01
188  */
189  )
190 preferences {
191         section("When I touch the app turn these lights off…"){
192                 input "switchesoff", "capability.switch", multiple: true, required:true
193         }
194     section("When I touch the app turn these lights on…"){
195                 input "switcheson", "capability.switch", multiple: true, required:false
196         }
197     section("Lock theses locks...") {
198                 input "lock1","capability.lock", multiple: true
199     }
200         section("And change to this mode...") {
201                 input "newMode", "mode", title: "Mode?"
202         }
203    section("After so many seconds (optional)"){
204                 input "waitfor", "number", title: "Off after (default 120)", required: true
205         }
206 }
207
208
209 def installed()
210 {
211         log.debug "Installed with settings: ${settings}"
212         log.debug "Current mode = ${location.mode}"
213         subscribe(app, appTouch)
214 }
215
216
217 def updated()
218 {
219         log.debug "Updated with settings: ${settings}"
220         log.debug "Current mode = ${location.mode}"
221         unsubscribe()
222         subscribe(app, appTouch)
223 }
224
225 def appTouch(evt) {
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}'"
230     }   else {
231         log.debug "New mode is the same as the old mode, leaving it be"
232         }
233     log.debug "appTouch: $evt"
234     lock1.lock()
235     switcheson.on()
236     def delay = (waitfor != null && waitfor != "") ? waitfor * 1000 : 120000
237         switchesoff.off(delay: delay)
238 }
239
240 installed()