-
Notifications
You must be signed in to change notification settings - Fork 5
/
Epson Projector.groovy
367 lines (318 loc) · 8.62 KB
/
Epson Projector.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* Epson Projector Device Handler
*
* Copyright 2021 Amos Yuen
*/
import groovy.transform.Field
import hubitat.helper.HexUtils
private def textVersion() {
return "1.0.0 (2021-04-05)"
}
private def textCopyright() {
return "Copyright ©2021\nAmos Yuen"
}
@Field final Map STATUS_MAP = [
"off": "off",
0: "standby (network off)",
1: "on",
2: "warmup",
3: "cooldown",
4: "standby (network on)",
5: "abnormal standby",
9: "A/V standby",
]
@Field final List STATUSES = STATUS_MAP.collect { it.value }
metadata {
definition (name: "Epson Projector", namespace: "amosyuen", author: "Amos Yuen") {
capability "Actuator"
capability "Polling"
capability "Refresh"
capability "Sensor"
capability "Switch"
attribute "status", "enum", STATUSES
attribute "lampHours", "number"
attribute "pollIntervalSeconds", "number"
attribute "source", "string"
command "setPollIntervalSeconds", [[
name: "Seconds",
type: "NUMBER",
description: "Interval in seconds to poll the projector when it is off."
+ " A value of 0 disables polling."
+ " Values greater than 60 will be rounded to the nearest minute."]]
command "setSource", [[
name: "Source",
type: "STRING"]]
}
preferences {
input("hubIP", "text", title: "Hub IP", required: true, displayDuringSetup: true)
input("debugLogging", "bool", title: "Enable debug logging", required: true, defaultValue: false)
input("traceLogging", "bool", title: "Enable trace logging", required: true, defaultValue: false)
}
}
//
// Life Cycle
//
def installed() {
init()
}
def updated() {
init()
}
def init() {
unschedule()
state.version = textVersion()
state.copyright = textCopyright()
if (device.currentSwitch == null) {
offline()
}
setPollIntervalSeconds(device.currentPollIntervalSeconds == null ? 300 : device.currentPollIntervalSeconds)
}
//
// Actions
//
def on() {
logMsg("debug", "on")
sendCommand("PWR ON")
}
def off() {
logMsg("debug", "off")
sendCommand("PWR OFF")
}
def poll() {
refresh()
}
def refreshIfOffline() {
if (device.currentSwitch == "offline") {
refresh()
}
}
def refresh() {
logMsg("debug", "refresh")
if (state.refreshing) {
return
}
state.refreshing = true
runIn(1, refreshDelayed)
runIn(30, refreshFinished)
}
def refreshDelayed() {
sendCommand("PWR?")
// Other values will be refreshed sequentially
}
def refreshFinished() {
unschedule(refreshFinished)
state.refreshing = false
}
def setPollIntervalSeconds(seconds) {
logMsg("debug", "setPollIntervalSeconds: seconds=${seconds}")
unschedule(refreshIfOffline)
if (seconds == null) {
seconds = "null"
} else {
if (seconds < 0) {
throw new Exception("Poll interval seconds ${seconds} must be greater than or equal to 0")
}
if (seconds > 2419200) {
throw new Exception("Poll interval seconds ${seconds} must be less than or equal to 2419200")
}
if (seconds > 0) {
if (seconds >= 86400) {
def days = Math.round(seconds / 86400) as Integer
seconds = days * 86400
schedule("0 0 0 */${days} * ?", refreshIfOffline)
} else if (seconds >= 3600) {
def hours = Math.round(seconds / 3600) as Integer
seconds = hours * 3600
schedule("0 0 */${hours} * * ?", refreshIfOffline)
} else if (seconds >= 60) {
def minutes = Math.round(seconds / 60) as Integer
seconds = minutes * 60
schedule("0 */${minutes} * * * ?", refreshIfOffline)
} else {
schedule("*/${seconds} * * * * ?", refreshIfOffline)
}
}
}
sendEvent(name: "pollIntervalSeconds", value: seconds)
}
def setSource(source) {
logMsg("debug", "setSource: source=${source}")
if (source.size() <= 0) {
throw new Exception("Invalid source ${source}")
}
sendCommand("SOURCE ${source}")
}
//
// Helpers
//
def connect() {
logMsg("debug", "connect")
try {
interfaces.rawSocket.connect(hubIP, 3629, byteInterface: true)
// message format https://support.atlona.com/hc/en-us/articles/360048888054-IP-Control-of-Epson-Projectors
sendMessage("${HexUtils.byteArrayToHexString("ESC/VP.net".bytes)}100300000000")
} catch (Exception e) {
offline()
if (e instanceof java.net.NoRouteToHostException) {
// Projector is off
return
}
throw e
}
}
def sendCommand(command) {
try {
if (device.currentSwitch == "offline") {
state.lastCommand = command
connect()
return
}
sendCommandRaw(command)
} catch (Exception e) {
refreshFinished()
throw e
}
}
def sendCommandRaw(command) {
// All commands should end with 0d (carriage return)
def cmd = "${HexUtils.byteArrayToHexString(command.bytes)}0d"
logMsg("debug", "sendCommand: cmd=${cmd}")
sendMessage(cmd)
}
def sendMessage(msg) {
interfaces.rawSocket.sendMessage(msg)
runIn(10, messageTimeout)
}
def messageTimeout() {
logMsg("debug", "messageTimeout")
offline()
refreshFinished()
}
def offline() {
sendEvent(name: "switch", value: "offline")
sendEvent(name: "status", value: "off")
}
def parse(String message) {
logMsg("trace", "parse: hex=$message")
def value = new String(HexUtils.hexStringToByteArray(message))
logMsg("trace", "parse: value=$value")
unschedule(messageTimeout)
value.split(":").each {
if (it.size() > 0) {
parseResponse(it)
}
}
if (value.endsWith(":")) {
logMsg("debug", "parse: Confirmation of command")
// Confirmation of set command.
runIn(10, refresh)
}
}
def parseResponse(response) {
logMsg("trace", "parseResponse: response=${response}")
//
// Command Responses
//
if (response.startsWith("ESC/VP.net")) {
log.info("response size ${response.size()}")
if (response.size() >= 16) {
// message format https://support.atlona.com/hc/en-us/articles/360048888054-IP-Control-of-Epson-Projectors
def version = response.bytes[10] as int
def type = response.bytes[11] as int
def seqNo = (response.bytes[12] * 256 + response.bytes[13]) as int
def status = response.bytes[14] as int
def headerCount = response.bytes[15] as int
logMsg("debug", "parseResponse: version=${version} type=${type} seqNo=${seqNo} status=${status} headerCount=${headerCount}")
if (type != 3) {
throw new Exception("Not connected. type=${type}")
}
}
if (state.lastCommand) {
def cmd = state.lastCommand
state.remove("lastCommand")
sendCommandRaw(cmd)
} else {
sendCommandRaw("PWR?")
}
return
}
if (response.startsWith("PWR=")) {
def status = response[4..-2] as int
if (updateStatus(status)) {
if (state.refreshing) {
sendCommandRaw("SOURCE?")
}
}
return
}
if (response.startsWith("SOURCE=")) {
def source = response[7..-2]
logMsg("debug", "parseResponse: source=${source}")
sendEvent(name: "source", value: source)
if (state.refreshing) {
sendCommandRaw("LAMP?")
}
return
}
if (response.startsWith("LAMP=")) {
def lampHours = response[5..-2] as int
logMsg("debug", "parseResponse: lampHours=${on}")
sendEvent(name: "lampHours", value: lampHours)
refreshFinished()
return
}
//
// Events
//
if (response.startsWith("IMEVENT=")) {
def eventCode = response[8..11] as int
switch (eventCode) {
case 1:
if (!state.refreshing) {
runIn(5, refresh)
}
return
}
logMsg("debug", "parseResponse: Unhandled IMEVENT eventCode=${eventCode} response=${response}")
return
}
logMsg("debug", "parseResponse: Unhandled response=${response}")
}
def updateStatus(int status) {
def on = status != 0 && status != 4
logMsg("debug", "updateStatus: status=${STATUS_MAP[status]} value=${status} on=${on}")
sendEvent(name: "status", value: STATUS_MAP[status])
sendEvent(name: "switch", value: on ? "on" : "off")
return on
}
def socketStatus(String status) {
logMsg("info", "socketStatus: status=${status}")
if (status.contains("disconnect")) {
offline()
}
}
def logMsg(level, message) {
switch(level) {
case "trace":
if (traceLogging) {
log.trace(message)
}
break
case "debug":
if (debugLogging) {
log.debug(message)
}
break
case "info":
log.info(message)
break
case "warn":
log.warn(message)
break
case "error":
log.error(message)
break
default:
throw new Exception("Unsupported log level ${level}")
}
}