Update double-tap.groovy
[smartapps.git] / third-party / BeaconThingsManager.groovy
1 /**
2  *  BeaconThing Manager
3  *
4  *  Copyright 2015 obycode
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: "BeaconThings Manager",
18     namespace: "com.obycode",
19     author: "obycode",
20     description: "SmartApp to interact with the BeaconThings iOS app. Use this app to integrate iBeacons into your smart home.",
21     category: "Convenience",
22     iconUrl: "http://beaconthingsapp.com/images/Icon-60.png",
23     iconX2Url: "http://beaconthingsapp.com/images/Icon-60@2x.png",
24     iconX3Url: "http://beaconthingsapp.com/images/Icon-60@3x.png",
25     oauth: true)
26
27
28 preferences {
29         section("Allow BeaconThings to talk to your home") {
30
31         }
32 }
33
34 def installed() {
35   log.debug "Installed with settings: ${settings}"
36
37   initialize()
38 }
39
40 def initialize() {
41 }
42
43 def uninstalled() {
44   removeChildDevices(getChildDevices())
45 }
46
47 mappings {
48   path("/beacons") {
49     action: [
50     DELETE: "clearBeacons",
51     POST:   "addBeacon"
52     ]
53   }
54
55   path("/beacons/:id") {
56     action: [
57     PUT: "updateBeacon",
58     DELETE: "deleteBeacon"
59     ]
60   }
61 }
62
63 void clearBeacons() {
64   removeChildDevices(getChildDevices())
65 }
66
67 void addBeacon() {
68   def beacon = request.JSON?.beacon
69   if (beacon) {
70     def beaconId = "BeaconThings"
71     if (beacon.major) {
72       beaconId = "$beaconId-${beacon.major}"
73       if (beacon.minor) {
74         beaconId = "$beaconId-${beacon.minor}"
75       }
76     }
77     log.debug "adding beacon $beaconId"
78     def d = addChildDevice("com.obycode", "BeaconThing", beaconId,  null, [label:beacon.name, name:"BeaconThing", completedSetup: true])
79     log.debug "addChildDevice returned $d"
80
81     if (beacon.present) {
82       d.arrive(beacon.present)
83     }
84     else if (beacon.presence) {
85       d.setPresence(beacon.presence)
86     }
87   }
88 }
89
90 void updateBeacon() {
91   log.debug "updating beacon ${params.id}"
92   def beaconDevice = getChildDevice(params.id)
93   if (!beaconDevice) {
94     log.debug "Beacon not found"
95     return
96   }
97
98   // This could be just updating the presence
99   def presence = request.JSON?.presence
100   if (presence) {
101     log.debug "Setting ${beaconDevice.label} to $presence"
102     beaconDevice.setPresence(presence)
103   }
104
105   // It could be someone arriving
106   def arrived = request.JSON?.arrived
107   if (arrived) {
108     log.debug "$arrived arrived at ${beaconDevice.label}"
109     beaconDevice.arrived(arrived)
110   }
111
112   // It could be someone left
113   def left = request.JSON?.left
114   if (left) {
115     log.debug "$left left ${beaconDevice.label}"
116     beaconDevice.left(left)
117   }
118
119   // or it could be updating the name
120   def beacon = request.JSON?.beacon
121   if (beacon) {
122     beaconDevice.label = beacon.name
123   }
124 }
125
126 void deleteBeacon() {
127   log.debug "deleting beacon ${params.id}"
128   deleteChildDevice(params.id)
129 }
130
131 private removeChildDevices(delete) {
132   delete.each {
133     deleteChildDevice(it.deviceNetworkId)
134   }
135 }