Update influxdb-logger.groovy
[smartapps.git] / official / beaconthings-manager.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   // def children = getChildDevices()
94   // def beaconDevice = children.find{ d -> d.deviceNetworkId == "${params.id}" }
95   if (!beaconDevice) {
96     log.debug "Beacon not found directly"
97     def children = getChildDevices()
98     beaconDevice = children.find{ d -> d.deviceNetworkId == "${params.id}" }
99     if (!beaconDevice) {
100       log.debug "Beacon not found in list either"
101       return
102     }
103   }
104
105   // This could be just updating the presence
106   def presence = request.JSON?.presence
107   if (presence) {
108     log.debug "Setting ${beaconDevice.label} to $presence"
109     beaconDevice.setPresence(presence)
110   }
111
112   // It could be someone arriving
113   def arrived = request.JSON?.arrived
114   if (arrived) {
115     log.debug "$arrived arrived at ${beaconDevice.label}"
116     beaconDevice.arrived(arrived)
117   }
118
119   // It could be someone left
120   def left = request.JSON?.left
121   if (left) {
122     log.debug "$left left ${beaconDevice.label}"
123     beaconDevice.left(left)
124   }
125
126   // or it could be updating the name
127   def beacon = request.JSON?.beacon
128   if (beacon) {
129     beaconDevice.label = beacon.name
130   }
131 }
132
133 void deleteBeacon() {
134   log.debug "deleting beacon ${params.id}"
135   deleteChildDevice(params.id)
136   // def children = getChildDevices()
137   // def beaconDevice = children.find{ d -> d.deviceNetworkId == "${params.id}" }
138   // if (beaconDevice) {
139   //   deleteChildDevice(beaconDevice.deviceNetworkId)
140   // }
141 }
142
143 private removeChildDevices(delete) {
144   delete.each {
145     deleteChildDevice(it.deviceNetworkId)
146   }
147 }