Update SmartPresence.groovy
[smartapps.git] / official / gideon.groovy
1 /**
2  *  Gideon
3  *
4  *  Copyright 2016 Nicola Russo
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16 definition(
17     name: "Gideon",
18     namespace: "gideon.api",
19     author: "Braindrain Solutions",
20     description: "Gideon AI Smart app allows you to connect and control all of your SmartThings devices through the Gideon AI app, making your SmartThings devices even smarter.",
21     category: "Family",
22     iconUrl: "http://s33.postimg.org/t77u7y7v3/logo.png",
23     iconX2Url: "http://s33.postimg.org/t77u7y7v3/logo.png",
24     iconX3Url: "http://s33.postimg.org/t77u7y7v3/logo.png",
25     oauth: [displayName: "Gideon AI API", displayLink: "gideon.ai"])
26
27
28 preferences {
29         section("Control these switches...") {
30         input "switches", "capability.switch", multiple:true
31     }
32     section("Control these motion sensors...") {
33         input "motions", "capability.motionSensor", multiple:true
34     }
35     section("Control these presence sensors...") {
36         input "presence_sensors", "capability.presenceSensor", multiple:true
37     }
38     section("Control these outlets...") {
39         input "outlets", "capability.switch", multiple:true
40     }
41     section("Control these locks...") {
42         input "locks", "capability.lock", multiple:true
43     }
44     section("Control these locks...") {
45             input "temperature_sensors", "capability.temperatureMeasurement"
46     }
47 }
48
49 def installed() {
50         log.debug "Installed with settings: ${settings}"
51
52         initialize()
53 }
54
55 def updated() {
56         log.debug "Updated with settings: ${settings}"
57
58         unsubscribe()
59         initialize()
60 }
61
62 def initialize() {
63         // TODO: subscribe to attributes, devices, locations, etc.
64     subscribe(outlet, "energy", outletHandler)
65         subscribe(outlet, "switch", outletHandler)
66 }
67
68 // TODO: implement event handlers
69 def outletHandler(evt) {
70         log.debug "$outlet.currentEnergy"
71         //TODO call G API  
72 }
73
74
75 private device(it, type) {
76         it ? [id: it.id, label: it.label, type: type] : null
77 }
78
79 //API Mapping
80 mappings {
81         path("/getalldevices") {
82     action: [
83                         GET: "getAllDevices"
84                 ]
85         }
86         path("/doorlocks/:id/:command") {
87     action: [
88       GET: "updateDoorLock"
89     ]
90   }
91         path("/doorlocks/:id") {
92     action: [
93                         GET: "getDoorLockStatus"
94                 ]
95         }
96         path("/tempsensors/:id") {
97     action: [
98       GET: "getTempSensorsStatus"
99     ]
100   }
101         path("/presences/:id") {
102     action: [
103       GET: "getPresenceStatus"
104     ]
105   }
106         path("/motions/:id") {
107     action: [
108       GET: "getMotionStatus"
109     ]
110   }
111         path("/outlets/:id") {
112     action: [
113       GET: "getOutletStatus"
114     ]
115   }
116         path("/outlets/:id/:command") {
117     action: [
118       GET: "updateOutlet"
119     ]
120   }
121         path("/switches/:command") {
122     action: [
123       PUT: "updateSwitch"
124     ]
125   }
126 }
127
128 //API Methods
129 def getAllDevices() {
130         def locks_list = locks.collect{device(it,"Lock")}
131     def presences_list = presence_sensors.collect{device(it,"Presence")}
132     def motions_list = motions.collect{device(it,"Motion")}
133     def outlets_list = outlets.collect{device(it,"Outlet")}
134     def switches_list = switches.collect{device(it,"Switch")}
135     def temp_list = temperature_sensors.collect{device(it,"Temperature")}
136     return [Locks: locks_list, Presences: presences_list, Motions: motions_list, Outlets: outlets_list, Switches: switches_list, Temperatures: temp_list]
137 }
138
139 //LOCKS
140 def getDoorLockStatus() {
141         def device = locks.find { it.id == params.id }
142     if (!device) {
143             httpError(404, "Device not found")
144         } else {
145                 return [Device_state: device.currentValue('lock')]
146         }
147 }
148
149 def updateDoorLock() {
150         def command = params.command
151     def device = locks.find { it.id == params.id }
152     if (command){
153         if (!device) {
154             httpError(404, "Device not found")
155         } else {
156             if(command == "toggle")
157             {
158                 if(device.currentValue('lock') == "locked")
159                   device.unlock();
160                 else
161                   device.lock();
162                   
163                 return [Device_id: params.id, result_action: "200"]
164             }
165         }
166     }
167 }
168
169 //PRESENCE
170 def getPresenceStatus() {
171
172         def device = presence_sensors.find { it.id == params.id }
173     if (!device) {
174             httpError(404, "Device not found")
175         } else {
176                 return [Device_state: device.currentValue('presence')]
177    }
178 }
179
180 //MOTION
181 def getMotionStatus() {
182
183         def device = motions.find { it.id == params.id }
184     if (!device) {
185             httpError(404, "Device not found")
186         } else {
187                 return [Device_state: device.currentValue('motion')]
188    }
189 }
190
191 //OUTLET
192 def getOutletStatus() {
193         
194     def device = outlets.find { it.id == params.id }
195         if (!device) {
196             httpError(404, "Device not found")
197         } else {
198                 return [Device_state: device.currentSwitch, Current_watt: device.currentValue("energy")]
199   }
200 }
201
202 def updateOutlet() {
203         
204     def command = params.command
205     def device = outlets.find { it.id == params.id }
206     if (command){
207         if (!device) {
208             httpError(404, "Device not found")
209         } else {
210             if(command == "toggle")
211             {
212                 if(device.currentSwitch == "on")
213                   device.off();
214                 else
215                   device.on();
216                   
217                 return [Device_id: params.id, result_action: "200"]
218             }
219         }
220     }
221 }                
222
223 //SWITCH
224 def updateSwitch() {
225     def command = params.command
226     def device = switches.find { it.id == params.id }
227     if (command){
228         if (!device) {
229             httpError(404, "Device not found")
230         } else {
231             if(command == "toggle")
232             {
233                 if(device.currentSwitch == "on")
234                   device.off();
235                 else
236                   device.on();
237                   
238                 return [Device_id: params.id, result_action: "200"]
239             }
240         }
241     }
242 }
243
244 //TEMPERATURE
245 def getTempSensorsStatus() {
246         
247     def device = temperature_sensors.find { it.id == params.id }
248     if (!device) {
249             httpError(404, "Device not found")
250         } else {
251                 return [Device_state: device.currentValue('temperature')]
252    }
253 }