forked from davidsulpy/initialstate-smartapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initial-state-event-sender.groovy
376 lines (329 loc) · 12.3 KB
/
initial-state-event-sender.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* Initial State Event Streamer
*
* Copyright 2016 David Sulpy
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
* SmartThings data is sent from this SmartApp to Initial State. This is event data only for
* devices for which the user has authorized. Likewise, Initial State's services call this
* SmartApp on the user's behalf to configure Initial State specific parameters. The ToS and
* Privacy Policy for Initial State can be found here: https://www.initialstate.com/terms
*/
definition(
name: "Initial State Event Streamer",
namespace: "initialstate.events",
author: "David Sulpy",
description: "A SmartThings SmartApp to allow SmartThings events to be viewable inside an Initial State Event Bucket in your https://www.initialstate.com account.",
category: "SmartThings Labs",
iconUrl: "https://s3.amazonaws.com/initialstate-web-cdn/IS-wordmark-vertica_small.png",
iconX2Url: "https://s3.amazonaws.com/initialstate-web-cdn/IS-wordmark-vertical.png",
iconX3Url: "https://s3.amazonaws.com/initialstate-web-cdn/IS-wordmark-vertical.png",
oauth: [displayName: "Initial State", displayLink: "https://www.initialstate.com"])
import groovy.json.JsonSlurper
preferences {
section("Choose which devices to monitor...") {
input "accelerometers", "capability.accelerationSensor", title: "Accelerometers", multiple: true, required: false
input "alarms", "capability.alarm", title: "Alarms", multiple: true, required: false
input "batteries", "capability.battery", title: "Batteries", multiple: true, required: false
input "beacons", "capability.beacon", title: "Beacons", multiple: true, required: false
input "cos", "capability.carbonMonoxideDetector", title: "Carbon Monoxide Detectors", multiple: true, required: false
input "colors", "capability.colorControl", title: "Color Controllers", multiple: true, required: false
input "contacts", "capability.contactSensor", title: "Contact Sensors", multiple: true, required: false
input "doorsControllers", "capability.doorControl", title: "Door Controllers", multiple: true, required: false
input "energyMeters", "capability.energyMeter", title: "Energy Meters", multiple: true, required: false
input "illuminances", "capability.illuminanceMeasurement", title: "Illuminance Meters", multiple: true, required: false
input "locks", "capability.lock", title: "Locks", multiple: true, required: false
input "motions", "capability.motionSensor", title: "Motion Sensors", multiple: true, required: false
input "musicPlayers", "capability.musicPlayer", title: "Music Players", multiple: true, required: false
input "powerMeters", "capability.powerMeter", title: "Power Meters", multiple: true, required: false
input "presences", "capability.presenceSensor", title: "Presence Sensors", multiple: true, required: false
input "humidities", "capability.relativeHumidityMeasurement", title: "Humidity Meters", multiple: true, required: false
input "relaySwitches", "capability.relaySwitch", title: "Relay Switches", multiple: true, required: false
input "sleepSensors", "capability.sleepSensor", title: "Sleep Sensors", multiple: true, required: false
input "smokeDetectors", "capability.smokeDetector", title: "Smoke Detectors", multiple: true, required: false
input "peds", "capability.stepSensor", title: "Pedometers", multiple: true, required: false
input "switches", "capability.switch", title: "Switches", multiple: true, required: false
input "switchLevels", "capability.switchLevel", title: "Switch Levels", multiple: true, required: false
input "temperatures", "capability.temperatureMeasurement", title: "Temperature Sensors", multiple: true, required: false
input "thermostats", "capability.thermostat", title: "Thermostats", multiple: true, required: false
input "valves", "capability.valve", title: "Valves", multiple: true, required: false
input "waterSensors", "capability.waterSensor", title: "Water Sensors", multiple: true, required: false
}
}
mappings {
path("/access_key") {
action: [
GET: "getAccessKey",
PUT: "setAccessKey"
]
}
path("/bucket") {
action: [
GET: "getBucketKey",
PUT: "setBucketKey"
]
}
}
def getAccessKey() {
log.trace "get access key"
if (atomicState.accessKey == null) {
httpError(404, "Access Key Not Found")
} else {
[
accessKey: atomicState.accessKey
]
}
}
def getBucketKey() {
log.trace "get bucket key"
if (atomicState.bucketKey == null) {
httpError(404, "Bucket key Not Found")
} else {
[
bucketKey: atomicState.bucketKey,
bucketName: atomicState.bucketName
]
}
}
def setBucketKey() {
log.trace "set bucket key"
def newBucketKey = request.JSON?.bucketKey
def newBucketName = request.JSON?.bucketName
log.debug "bucket name: $newBucketName"
log.debug "bucket key: $newBucketKey"
if (newBucketKey && (newBucketKey != atomicState.bucketKey || newBucketName != atomicState.bucketName)) {
atomicState.bucketKey = "$newBucketKey"
atomicState.bucketName = "$newBucketName"
atomicState.isBucketCreated = false
}
tryCreateBucket()
}
def setAccessKey() {
log.trace "set access key"
def newAccessKey = request.JSON?.accessKey
def newGrokerSubdomain = request.JSON?.grokerSubdomain
if (newGrokerSubdomain && newGrokerSubdomain != "" && newGrokerSubdomain != atomicState.grokerSubdomain) {
atomicState.grokerSubdomain = "$newGrokerSubdomain"
atomicState.isBucketCreated = false
}
if (newAccessKey && newAccessKey != atomicState.accessKey) {
atomicState.accessKey = "$newAccessKey"
atomicState.isBucketCreated = false
}
}
def subscribeToEvents() {
if (accelerometers != null) {
subscribe(accelerometers, "acceleration", genericHandler)
}
if (alarms != null) {
subscribe(alarms, "alarm", genericHandler)
}
if (batteries != null) {
subscribe(batteries, "battery", genericHandler)
}
if (beacons != null) {
subscribe(beacons, "presence", genericHandler)
}
if (cos != null) {
subscribe(cos, "carbonMonoxide", genericHandler)
}
if (colors != null) {
subscribe(colors, "hue", genericHandler)
subscribe(colors, "saturation", genericHandler)
subscribe(colors, "color", genericHandler)
}
if (contacts != null) {
subscribe(contacts, "contact", genericHandler)
}
if (energyMeters != null) {
subscribe(energyMeters, "energy", genericHandler)
}
if (illuminances != null) {
subscribe(illuminances, "illuminance", genericHandler)
}
if (locks != null) {
subscribe(locks, "lock", genericHandler)
}
if (motions != null) {
subscribe(motions, "motion", genericHandler)
}
if (musicPlayers != null) {
subscribe(musicPlayers, "status", genericHandler)
subscribe(musicPlayers, "level", genericHandler)
subscribe(musicPlayers, "trackDescription", genericHandler)
subscribe(musicPlayers, "trackData", genericHandler)
subscribe(musicPlayers, "mute", genericHandler)
}
if (powerMeters != null) {
subscribe(powerMeters, "power", genericHandler)
}
if (presences != null) {
subscribe(presences, "presence", genericHandler)
}
if (humidities != null) {
subscribe(humidities, "humidity", genericHandler)
}
if (relaySwitches != null) {
subscribe(relaySwitches, "switch", genericHandler)
}
if (sleepSensors != null) {
subscribe(sleepSensors, "sleeping", genericHandler)
}
if (smokeDetectors != null) {
subscribe(smokeDetectors, "smoke", genericHandler)
}
if (peds != null) {
subscribe(peds, "steps", genericHandler)
subscribe(peds, "goal", genericHandler)
}
if (switches != null) {
subscribe(switches, "switch", genericHandler)
}
if (switchLevels != null) {
subscribe(switchLevels, "level", genericHandler)
}
if (temperatures != null) {
subscribe(temperatures, "temperature", genericHandler)
}
if (thermostats != null) {
subscribe(thermostats, "temperature", genericHandler)
subscribe(thermostats, "heatingSetpoint", genericHandler)
subscribe(thermostats, "coolingSetpoint", genericHandler)
subscribe(thermostats, "thermostatSetpoint", genericHandler)
subscribe(thermostats, "thermostatMode", genericHandler)
subscribe(thermostats, "thermostatFanMode", genericHandler)
subscribe(thermostats, "thermostatOperatingState", genericHandler)
}
if (valves != null) {
subscribe(valves, "contact", genericHandler)
}
if (waterSensors != null) {
subscribe(waterSensors, "water", genericHandler)
}
}
def installed() {
atomicState.version = "1.1.0"
atomicState.isBucketCreated = false
atomicState.grokerSubdomain = "groker"
subscribeToEvents()
atomicState.isBucketCreated = false
atomicState.grokerSubdomain = "groker"
log.debug "installed (version $atomicState.version)"
}
def updated() {
atomicState.version = "1.1.0"
unsubscribe()
if (atomicState.bucketKey != null && atomicState.accessKey != null) {
atomicState.isBucketCreated = false
}
if (atomicState.grokerSubdomain == null || atomicState.grokerSubdomain == "") {
atomicState.grokerSubdomain = "groker"
}
subscribeToEvents()
log.debug "updated (version $atomicState.version)"
}
def uninstalled() {
log.debug "uninstalled (version $atomicState.version)"
}
def tryCreateBucket() {
// can't ship events if there is no grokerSubdomain
if (atomicState.grokerSubdomain == null || atomicState.grokerSubdomain == "") {
log.error "streaming url is currently null"
return
}
// if the bucket has already been created, no need to continue
if (atomicState.isBucketCreated) {
return
}
if (!atomicState.bucketName) {
atomicState.bucketName = atomicState.bucketKey
}
if (!atomicState.accessKey) {
return
}
def bucketName = "${atomicState.bucketName}"
def bucketKey = "${atomicState.bucketKey}"
def accessKey = "${atomicState.accessKey}"
def bucketCreateBody = new JsonSlurper().parseText("{\"bucketKey\": \"$bucketKey\", \"bucketName\": \"$bucketName\"}")
def bucketCreatePost = [
uri: "https://${atomicState.grokerSubdomain}.initialstate.com/api/buckets",
headers: [
"Content-Type": "application/json",
"X-IS-AccessKey": accessKey
],
body: bucketCreateBody
]
log.debug bucketCreatePost
try {
// Create a bucket on Initial State so the data has a logical grouping
httpPostJson(bucketCreatePost) { resp ->
log.debug "bucket posted"
if (resp.status >= 400) {
log.error "bucket not created successfully"
} else {
atomicState.isBucketCreated = true
}
}
} catch (e) {
log.error "bucket creation error: $e"
}
}
def genericHandler(evt) {
log.trace "$evt.displayName($evt.name:$evt.unit) $evt.value"
def key = "$evt.displayName($evt.name)"
if (evt.unit != null) {
key = "$evt.displayName(${evt.name}_$evt.unit)"
}
def value = "$evt.value"
tryCreateBucket()
eventHandler(key, value)
}
def eventHandler(name, value) {
def epoch = now() / 1000
def event = new JsonSlurper().parseText("{\"key\": \"$name\", \"value\": \"$value\", \"epoch\": \"$epoch\"}")
tryShipEvents(event)
log.debug "Shipped Event: " + event
}
def tryShipEvents(event) {
def grokerSubdomain = atomicState.grokerSubdomain
// can't ship events if there is no grokerSubdomain
if (grokerSubdomain == null || grokerSubdomain == "") {
log.error "streaming url is currently null"
return
}
def accessKey = atomicState.accessKey
def bucketKey = atomicState.bucketKey
// can't ship if access key and bucket key are null, so finish trying
if (accessKey == null || bucketKey == null) {
return
}
def eventPost = [
uri: "https://${grokerSubdomain}.initialstate.com/api/events",
headers: [
"Content-Type": "application/json",
"X-IS-BucketKey": "${bucketKey}",
"X-IS-AccessKey": "${accessKey}",
"Accept-Version": "0.0.2"
],
body: event
]
try {
// post the events to initial state
httpPostJson(eventPost) { resp ->
log.debug "shipped events and got ${resp.status}"
if (resp.status >= 400) {
log.error "shipping failed... ${resp.data}"
}
}
} catch (e) {
log.error "shipping events failed: $e"
}
}