Changing remote branch to PLRG Git server.
[smartapps.git] / third-party / BeaconThing.groovy
1 /**
2 *  BeaconThing
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
17 import groovy.json.JsonSlurper
18
19 metadata {
20         definition (name: "BeaconThing", namespace: "com.obycode", author: "obycode") {
21                 capability "Beacon"
22                 capability "Presence Sensor"
23                 capability "Sensor"
24
25                 attribute "inRange", "json_object"
26                 attribute "inRangeFriendly", "string"
27
28                 command "setPresence", ["string"]
29                 command "arrived", ["string"]
30                 command "left", ["string"]
31         }
32
33         simulator {
34                 status "present": "presence: 1"
35                 status "not present": "presence: 0"
36         }
37
38         tiles {
39                 standardTile("presence", "device.presence", width: 2, height: 2, canChangeBackground: true) {
40                         state("present", labelIcon:"st.presence.tile.present", backgroundColor:"#53a7c0")
41                         state("not present", labelIcon:"st.presence.tile.not-present", backgroundColor:"#ffffff")
42                 }
43                 valueTile("inRange", "device.inRangeFriendly", inactiveLabel: true, height:1, width:3, decoration: "flat") {
44                         state "default", label:'${currentValue}', backgroundColor:"#ffffff"
45                 }
46                 main "presence"
47                 details (["presence","inRange"])
48         }
49 }
50
51 // parse events into attributes
52 def parse(String description) {
53         log.debug "Parsing '${description}'"
54 }
55
56 def installed() {
57         sendEvent(name: "presence", value: "not present")
58         def emptyList = []
59         def json = new groovy.json.JsonBuilder(emptyList)
60         sendEvent(name:"inRange", value:json.toString())
61 }
62
63 def setPresence(status) {
64         log.debug "Status is $status"
65         sendEvent(name:"presence", value:status)
66 }
67
68 def arrived(id) {
69         log.debug "$id has arrived"
70         def theList = device.latestValue("inRange")
71         def inRangeList = new JsonSlurper().parseText(theList)
72         if (inRangeList.contains(id)) {
73                 return
74         }
75         inRangeList += id
76         def json = new groovy.json.JsonBuilder(inRangeList)
77         log.debug "Now in range: ${json.toString()}"
78         sendEvent(name:"inRange", value:json.toString())
79
80         // Generate human friendly string for tile
81         def friendlyList = "Nearby: " + inRangeList.join(", ")
82         sendEvent(name:"inRangeFriendly", value:friendlyList)
83
84         if (inRangeList.size() == 1) {
85                 setPresence("present")
86         }
87 }
88
89 def left(id) {
90         log.debug "$id has left"
91         def theList = device.latestValue("inRange")
92         def inRangeList = new JsonSlurper().parseText(theList)
93         inRangeList -= id
94         def json = new groovy.json.JsonBuilder(inRangeList)
95         log.debug "Now in range: ${json.toString()}"
96         sendEvent(name:"inRange", value:json.toString())
97
98         // Generate human friendly string for tile
99         def friendlyList = "Nearby: " + inRangeList.join(", ")
100
101         if (inRangeList.empty) {
102                 setPresence("not present")
103                 friendlyList = "No one is nearby"
104         }
105
106         sendEvent(name:"inRangeFriendly", value:friendlyList)
107 }