-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTTP-Ping.groovy
184 lines (149 loc) · 4.95 KB
/
HTTP-Ping.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
/**
* HTTP Ping
*
* Copyright 2014 Kristopher Kubicki
*
* 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.
*
*/
preferences {
input("dest_ip", "text", title: "IP", description: "The device IP you wish to ping")
input("dest_port", "number", title: "Port", description: "The port you wish to connect to to emulate a ping")
}
metadata {
definition (name: "HTTP Ping", namespace: "castlecole", author: "Kristopher Kubicki") {
capability "Polling"
capability "Contact Sensor"
capability "Refresh"
attribute "ttl", "string"
attribute "last_request", "number"
attribute "last_live", "number"
}
simulator {
// TODO: define status and reply messages here
}
tiles {
standardTile("contact", "device.contact", width: 2, height: 2, canChangeIcon: false, canChangeBackground: true) {
//
// color scheme is inverse of a normal sensor.
//
state "open", label: '${name}', backgroundColor: "#ffa81e", icon:"st.contact.contact.open"
state "closed", label: '${name}', backgroundColor: "#79b821", icon:"st.contact.contact.closed"
}
standardTile("refresh", "device.ttl", inactiveLabel: false, decoration: "flat") {
state "default", action:"polling.poll", icon:"st.secondary.refresh"
}
standardTile("ttl", "device.ttl", inactiveLabel: false, decoration: "flat") {
state "ttl", label:'${ttl}'
}
main "contact"
details(["contact", "refresh", "ttl"])
}
// TODO: define your main and details tiles here
}
def parse(String description) {
log.debug "Parsing '${description}'"
def map = stringToMap(description)
// def headerString = new String(map.headers.decodeBase64())
// def bodyString = new String(map.body.decodeBase64())
// log.debug "Parsing '${headerString}'"
// log.debug "Parsing '${bodyString}'"
def c = new GregorianCalendar()
sendEvent(name: 'contact', value: "open")
sendEvent(name: 'last_live', value: c.time.time)
def ping = ttl()
sendEvent(name: 'ttl', value: ping)
log.debug "Pinging ${device.deviceNetworkId}: ${ping}"
}
private ttl() {
def last_request = device.latestValue("last_request")
if(!last_request) {
last_request = 0
}
def last_alive = device.latestValue("last_live")
if(!last_alive) {
last_alive = 0
}
def last_status = device.latestValue("status")
def c = new GregorianCalendar()
def ttl = c.time.time - last_request
if(ttl > 10000 || last_status == "Down") {
ttl = c.time.time - last_alive
}
def units = "ms"
if(ttl > 10*52*7*24*60*60*1000) {
return "Never"
}
else if(ttl > 52*7*24*60*60*1000) {
ttl = ttl / (52*7*24*60*60*1000)
units = "y"
}
else if(ttl > 7*24*60*60*1000) {
ttl = ttl / (7*24*60*60*1000)
units = "w"
}
else if(ttl > 24*60*60*1000) {
ttl = ttl / (24*60*60*1000)
units = "d"
}
else if(ttl > 60*60*1000) {
ttl = ttl / (60*60*1000)
units = "h"
}
else if(ttl > 60*1000) {
ttl = ttl / (60*1000)
units = "m"
}
else if(ttl > 1000) {
ttl = ttl / 1000
units = "s"
}
def ttl_int = ttl.intValue()
"${ttl_int} ${units}"
}
// handle commands
def poll() {
def hosthex = convertIPToHex(dest_ip)
def porthex = Long.toHexString(Long.parseLong(dest_port))
if (porthex.length() < 4) { porthex = "00" + porthex }
device.deviceNetworkId = "$hosthex:$porthex"
// log.debug "The DNI configured is $device.deviceNetworkId"
def hubAction = new physicalgraph.device.HubAction(
method: "GET",
path: "/"
)
def last_request = device.latestValue("last_request")
def last_live = device.latestValue("last_live")
if(!last_request) {
last_request = 0
}
if(!last_live) {
last_live = 0
}
def c = new GregorianCalendar()
if(last_live < last_request) {
sendEvent(name: 'contact', value: "closed")
sendEvent(name: 'ttl', value: ttl())
}
sendEvent(name: 'last_request', value: c.time.time)
// log.debug hubAction
hubAction
}
private Long convertIntToLong(ipAddress) {
long result = 0
def parts = ipAddress.split("\\.")
for (int i = 3; i >= 0; i--) {
result |= (Long.parseLong(parts[3 - i]) << (i * 8));
}
return result & 0xFFFFFFFF;
}
private String convertIPToHex(ipAddress) {
return Long.toHexString(convertIntToLong(ipAddress));
}