4 * Copyright 2015 obycode
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 name: "BeaconThings Manager",
18 namespace: "com.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",
29 section("Allow BeaconThings to talk to your home") {
35 log.debug "Installed with settings: ${settings}"
44 removeChildDevices(getChildDevices())
50 DELETE: "clearBeacons",
55 path("/beacons/:id") {
58 DELETE: "deleteBeacon"
64 removeChildDevices(getChildDevices())
68 def beacon = request.JSON?.beacon
70 def beaconId = "BeaconThings"
72 beaconId = "$beaconId-${beacon.major}"
74 beaconId = "$beaconId-${beacon.minor}"
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"
82 d.arrive(beacon.present)
84 else if (beacon.presence) {
85 d.setPresence(beacon.presence)
91 log.debug "updating beacon ${params.id}"
92 def beaconDevice = getChildDevice(params.id)
94 log.debug "Beacon not found"
98 // This could be just updating the presence
99 def presence = request.JSON?.presence
101 log.debug "Setting ${beaconDevice.label} to $presence"
102 beaconDevice.setPresence(presence)
105 // It could be someone arriving
106 def arrived = request.JSON?.arrived
108 log.debug "$arrived arrived at ${beaconDevice.label}"
109 beaconDevice.arrived(arrived)
112 // It could be someone left
113 def left = request.JSON?.left
115 log.debug "$left left ${beaconDevice.label}"
116 beaconDevice.left(left)
119 // or it could be updating the name
120 def beacon = request.JSON?.beacon
122 beaconDevice.label = beacon.name
126 void deleteBeacon() {
127 log.debug "deleting beacon ${params.id}"
128 deleteChildDevice(params.id)
131 private removeChildDevices(delete) {
133 deleteChildDevice(it.deviceNetworkId)