]> git.basschouten.com Git - openhab-addons.git/blob
37875759ab366e1f7079774d9cc596d25b5fbc9f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.epsonprojector.internal;
14
15 import java.time.Duration;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.epsonprojector.internal.configuration.EpsonProjectorConfiguration;
23 import org.openhab.binding.epsonprojector.internal.connector.EpsonProjectorConnector;
24 import org.openhab.binding.epsonprojector.internal.connector.EpsonProjectorSerialConnector;
25 import org.openhab.binding.epsonprojector.internal.connector.EpsonProjectorTcpConnector;
26 import org.openhab.binding.epsonprojector.internal.enums.AspectRatio;
27 import org.openhab.binding.epsonprojector.internal.enums.Background;
28 import org.openhab.binding.epsonprojector.internal.enums.ColorMode;
29 import org.openhab.binding.epsonprojector.internal.enums.ErrorMessage;
30 import org.openhab.binding.epsonprojector.internal.enums.Gamma;
31 import org.openhab.binding.epsonprojector.internal.enums.Luminance;
32 import org.openhab.binding.epsonprojector.internal.enums.PowerStatus;
33 import org.openhab.binding.epsonprojector.internal.enums.Switch;
34 import org.openhab.core.cache.ExpiringCache;
35 import org.openhab.core.io.transport.serial.SerialPortManager;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Provide high level interface to Epson projector.
41  *
42  * @author Pauli Anttila - Initial contribution
43  * @author Yannick Schaus - Refactoring
44  * @author Michael Lobstein - Improvements for OH3
45  */
46 @NonNullByDefault
47 public class EpsonProjectorDevice {
48     private static final int[] MAP64 = new int[] { 0, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63, 66,
49             70, 74, 78, 82, 86, 90, 94, 98, 102, 106, 110, 114, 118, 122, 126, 129, 133, 137, 141, 145, 149, 153, 157,
50             161, 165, 169, 173, 177, 181, 185, 189, 192, 196, 200, 204, 208, 212, 216, 220, 224, 228, 232, 236, 240,
51             244, 248, 252 };
52
53     private static final int[] MAP60 = new int[] { 0, 4, 8, 12, 16, 20, 25, 29, 33, 37, 41, 46, 50, 54, 58, 62, 67, 71,
54             75, 79, 83, 88, 92, 96, 100, 104, 109, 113, 117, 121, 125, 130, 134, 138, 142, 146, 151, 155, 159, 163, 167,
55             172, 176, 180, 184, 188, 193, 197, 201, 205, 209, 214, 218, 222, 226, 230, 235, 239, 243, 247, 251 };
56
57     private static final int[] MAP49 = new int[] { 0, 5, 10, 15, 20, 25, 30, 35, 40, 46, 51, 56, 61, 66, 71, 76, 81, 87,
58             92, 97, 102, 107, 112, 117, 122, 128, 133, 138, 143, 148, 153, 158, 163, 168, 174, 179, 184, 189, 194, 199,
59             204, 209, 215, 220, 225, 230, 235, 240, 245, 250 };
60
61     private static final int[] MAP48 = new int[] { 0, 5, 10, 15, 20, 26, 31, 36, 41, 47, 52, 57, 62, 67, 73, 78, 83, 88,
62             94, 99, 104, 109, 114, 120, 125, 130, 135, 141, 146, 151, 156, 161, 167, 172, 177, 182, 188, 193, 198, 203,
63             208, 214, 219, 224, 229, 235, 240, 245, 250 };
64
65     private static final int[] MAP20 = new int[] { 0, 12, 24, 36, 48, 60, 73, 85, 97, 109, 121, 134, 146, 158, 170, 182,
66             195, 207, 219, 231, 243 };
67
68     private static final int[] MAP18 = new int[] { 0, 13, 26, 40, 53, 67, 80, 94, 107, 121, 134, 148, 161, 175, 188,
69             202, 215, 229, 242 };
70
71     private static final int[] MAP_COLOR_TEMP = new int[] { 0, 25, 51, 76, 102, 128, 153, 179, 204, 230 };
72     private static final int[] MAP_FLESH_COLOR = new int[] { 0, 36, 73, 109, 146, 182, 219 };
73
74     private static final int DEFAULT_TIMEOUT = 5 * 1000;
75     private static final int POWER_ON_TIMEOUT = 100 * 1000;
76     private static final int POWER_OFF_TIMEOUT = 130 * 1000;
77     private static final int LAMP_REFRESH_WAIT_MINUTES = 5;
78
79     private static final String ON = "ON";
80     private static final String ERR = "ERR";
81
82     private final Logger logger = LoggerFactory.getLogger(EpsonProjectorDevice.class);
83
84     private @Nullable ScheduledExecutorService scheduler = null;
85     private @Nullable ScheduledFuture<?> timeoutJob;
86
87     private EpsonProjectorConnector connection;
88     private ExpiringCache<Integer> cachedLampHours = new ExpiringCache<>(Duration.ofMinutes(LAMP_REFRESH_WAIT_MINUTES),
89             this::queryLamp);
90     private boolean connected = false;
91     private boolean ready = true;
92
93     public EpsonProjectorDevice(SerialPortManager serialPortManager, EpsonProjectorConfiguration config) {
94         connection = new EpsonProjectorSerialConnector(serialPortManager, config.serialPort);
95     }
96
97     public EpsonProjectorDevice(EpsonProjectorConfiguration config) {
98         connection = new EpsonProjectorTcpConnector(config.host, config.port);
99     }
100
101     public boolean isReady() {
102         return ready;
103     }
104
105     public void setScheduler(ScheduledExecutorService scheduler) {
106         this.scheduler = scheduler;
107     }
108
109     private synchronized @Nullable String sendQuery(String query, int timeout)
110             throws EpsonProjectorCommandException, EpsonProjectorException {
111         logger.debug("Query: '{}'", query);
112         String response = connection.sendMessage(query, timeout);
113
114         if (response.length() == 0) {
115             throw new EpsonProjectorException("No response received");
116         }
117
118         response = response.replace("\r:", "");
119         logger.debug("Response: '{}'", response);
120
121         if (ERR.equals(response)) {
122             throw new EpsonProjectorCommandException("Error response received for command: " + query);
123         }
124
125         if ("PWR OFF".equals(query) && ":".equals(response)) {
126             // When PWR OFF command is sent, next command can be sent 10 seconds after the colon is received
127             logger.debug("Refusing further commands for 10 seconds to power OFF completion");
128             ready = false;
129             ScheduledExecutorService scheduler = this.scheduler;
130             if (scheduler != null) {
131                 timeoutJob = scheduler.schedule(() -> {
132                     ready = true;
133                 }, 10, TimeUnit.SECONDS);
134             }
135         }
136
137         return response;
138     }
139
140     private String splitResponse(@Nullable String response) throws EpsonProjectorException {
141         if (response != null && !"".equals(response)) {
142             String[] pieces = response.split("=");
143
144             if (pieces.length < 2) {
145                 throw new EpsonProjectorException("Invalid response from projector: " + response);
146             }
147
148             return pieces[1].trim();
149         } else {
150             throw new EpsonProjectorException("No response received");
151         }
152     }
153
154     protected void sendCommand(String command, int timeout)
155             throws EpsonProjectorCommandException, EpsonProjectorException {
156         sendQuery(command, timeout);
157     }
158
159     protected void sendCommand(String command) throws EpsonProjectorCommandException, EpsonProjectorException {
160         sendCommand(command, DEFAULT_TIMEOUT);
161     }
162
163     protected int queryInt(String query, int timeout, int radix)
164             throws EpsonProjectorCommandException, EpsonProjectorException {
165         String response = sendQuery(query, timeout);
166
167         String str = splitResponse(response);
168
169         // if the response has two number groups, get the first one (Aspect Ratio does this)
170         if (str.contains(" ")) {
171             String[] subStr = str.split(" ");
172             str = subStr[0];
173         }
174
175         return Integer.parseInt(str, radix);
176     }
177
178     protected int queryInt(String query, int timeout) throws EpsonProjectorCommandException, EpsonProjectorException {
179         return queryInt(query, timeout, 10);
180     }
181
182     protected int queryInt(String query) throws EpsonProjectorCommandException, EpsonProjectorException {
183         return queryInt(query, DEFAULT_TIMEOUT, 10);
184     }
185
186     protected int queryHexInt(String query, int timeout)
187             throws EpsonProjectorCommandException, EpsonProjectorException {
188         return queryInt(query, timeout, 16);
189     }
190
191     protected int queryHexInt(String query) throws EpsonProjectorCommandException, EpsonProjectorException {
192         return queryInt(query, DEFAULT_TIMEOUT, 16);
193     }
194
195     protected String queryString(String query) throws EpsonProjectorCommandException, EpsonProjectorException {
196         String response = sendQuery(query, DEFAULT_TIMEOUT);
197         return splitResponse(response);
198     }
199
200     public void connect() throws EpsonProjectorException {
201         connection.connect();
202         connected = true;
203     }
204
205     public void disconnect() throws EpsonProjectorException {
206         connection.disconnect();
207         connected = false;
208         ScheduledFuture<?> timeoutJob = this.timeoutJob;
209         if (timeoutJob != null) {
210             timeoutJob.cancel(true);
211             this.timeoutJob = null;
212         }
213     }
214
215     public boolean isConnected() {
216         return connected;
217     }
218
219     /*
220      * Power
221      */
222     public PowerStatus getPowerStatus() throws EpsonProjectorCommandException, EpsonProjectorException {
223         int val = queryInt("PWR?");
224         return PowerStatus.forValue(val);
225     }
226
227     public void setPower(Switch value) throws EpsonProjectorCommandException, EpsonProjectorException {
228         sendCommand(String.format("PWR %s", value.name()), value == Switch.ON ? POWER_ON_TIMEOUT : POWER_OFF_TIMEOUT);
229     }
230
231     /*
232      * Key code
233      */
234     public void sendKeyCode(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
235         sendCommand(String.format("KEY %02X", value));
236     }
237
238     /*
239      * Vertical Keystone
240      */
241     public int getVerticalKeystone() throws EpsonProjectorCommandException, EpsonProjectorException {
242         int vkey = queryInt("VKEYSTONE?");
243         for (int i = 0; i < MAP60.length; i++) {
244             if (vkey == MAP60[i]) {
245                 return i - 30;
246             }
247         }
248         return 0;
249     }
250
251     public void setVerticalKeystone(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
252         value = value + 30;
253         if (value >= 0 && value <= 60) {
254             sendCommand(String.format("VKEYSTONE %d", MAP60[value]));
255         }
256     }
257
258     /*
259      * Horizontal Keystone
260      */
261     public int getHorizontalKeystone() throws EpsonProjectorCommandException, EpsonProjectorException {
262         int hkey = queryInt("HKEYSTONE?");
263         for (int i = 0; i < MAP60.length; i++) {
264             if (hkey == MAP60[i]) {
265                 return i - 30;
266             }
267         }
268         return 0;
269     }
270
271     public void setHorizontalKeystone(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
272         value = value + 30;
273         if (value >= 0 && value <= 60) {
274             sendCommand(String.format("HKEYSTONE %d", MAP60[value]));
275         }
276     }
277
278     /*
279      * Auto Keystone
280      */
281
282     public Switch getAutoKeystone() throws EpsonProjectorCommandException, EpsonProjectorException {
283         String val = queryString("AUTOKEYSTONE?");
284         return val.equals(ON) ? Switch.ON : Switch.OFF;
285     }
286
287     public void setAutoKeystone(Switch value) throws EpsonProjectorCommandException, EpsonProjectorException {
288         sendCommand(String.format("AUTOKEYSTONE %s", value.name()), DEFAULT_TIMEOUT);
289     }
290
291     /*
292      * Freeze
293      */
294     public Switch getFreeze() throws EpsonProjectorCommandException, EpsonProjectorException {
295         String val = queryString("FREEZE?");
296         return val.equals(ON) ? Switch.ON : Switch.OFF;
297     }
298
299     public void setFreeze(Switch value) throws EpsonProjectorCommandException, EpsonProjectorException {
300         sendCommand(String.format("FREEZE %s", value.name()), DEFAULT_TIMEOUT);
301     }
302
303     /*
304      * Aspect Ratio
305      */
306     public AspectRatio getAspectRatio() throws EpsonProjectorCommandException, EpsonProjectorException {
307         int val = queryHexInt("ASPECT?");
308         return AspectRatio.forValue(val);
309     }
310
311     public void setAspectRatio(AspectRatio value) throws EpsonProjectorCommandException, EpsonProjectorException {
312         sendCommand(String.format("ASPECT %02X", value.toInt()));
313     }
314
315     /*
316      * Luminance
317      */
318     public Luminance getLuminance() throws EpsonProjectorCommandException, EpsonProjectorException {
319         int val = queryHexInt("LUMINANCE?");
320         return Luminance.forValue(val);
321     }
322
323     public void setLuminance(Luminance value) throws EpsonProjectorCommandException, EpsonProjectorException {
324         sendCommand(String.format("LUMINANCE %02X", value.toInt()));
325     }
326
327     /*
328      * Source
329      */
330     public String getSource() throws EpsonProjectorCommandException, EpsonProjectorException {
331         return queryString("SOURCE?");
332     }
333
334     public void setSource(String value) throws EpsonProjectorCommandException, EpsonProjectorException {
335         sendCommand(String.format("SOURCE %s", value));
336     }
337
338     /*
339      * Brightness
340      */
341     public int getBrightness() throws EpsonProjectorCommandException, EpsonProjectorException {
342         int brt = queryInt("BRIGHT?");
343         for (int i = 0; i < MAP48.length; i++) {
344             if (brt == MAP48[i]) {
345                 return i - 24;
346             }
347         }
348         return 0;
349     }
350
351     public void setBrightness(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
352         value = value + 24;
353         if (value >= 0 && value <= 48) {
354             sendCommand(String.format("BRIGHT %d", MAP48[value]));
355         }
356     }
357
358     /*
359      * Contrast
360      */
361     public int getContrast() throws EpsonProjectorCommandException, EpsonProjectorException {
362         int con = queryInt("CONTRAST?");
363         for (int i = 0; i < MAP48.length; i++) {
364             if (con == MAP48[i]) {
365                 return i - 24;
366             }
367         }
368         return 0;
369     }
370
371     public void setContrast(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
372         value = value + 24;
373         if (value >= 0 && value <= 48) {
374             sendCommand(String.format("CONTRAST %d", MAP48[value]));
375         }
376     }
377
378     /*
379      * Density
380      */
381     public int getDensity() throws EpsonProjectorCommandException, EpsonProjectorException {
382         int den = queryInt("DENSITY?");
383         for (int i = 0; i < MAP64.length; i++) {
384             if (den == MAP64[i]) {
385                 return i - 32;
386             }
387         }
388         return 0;
389     }
390
391     public void setDensity(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
392         value = value + 32;
393         if (value >= 0 && value <= 64) {
394             sendCommand(String.format("DENSITY %d", MAP64[value]));
395         }
396     }
397
398     /*
399      * Tint
400      */
401     public int getTint() throws EpsonProjectorCommandException, EpsonProjectorException {
402         int tint = queryInt("TINT?");
403         for (int i = 0; i < MAP64.length; i++) {
404             if (tint == MAP64[i]) {
405                 return i - 32;
406             }
407         }
408         return 0;
409     }
410
411     public void setTint(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
412         value = value + 32;
413         if (value >= 0 && value <= 64) {
414             sendCommand(String.format("TINT %d", MAP64[value]));
415         }
416     }
417
418     /*
419      * Color Temperature
420      */
421     public int getColorTemperature() throws EpsonProjectorCommandException, EpsonProjectorException {
422         int ctemp = queryInt("CTEMP?");
423         for (int i = 0; i < MAP_COLOR_TEMP.length; i++) {
424             if (ctemp == MAP_COLOR_TEMP[i]) {
425                 return i;
426             }
427         }
428         return 0;
429     }
430
431     public void setColorTemperature(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
432         if (value >= 0 && value <= 9) {
433             sendCommand(String.format("CTEMP %d", MAP_COLOR_TEMP[value]));
434         }
435     }
436
437     /*
438      * Flesh Color
439      */
440     public int getFleshColor() throws EpsonProjectorCommandException, EpsonProjectorException {
441         int fclr = queryInt("FCOLOR?");
442         for (int i = 0; i < MAP_FLESH_COLOR.length; i++) {
443             if (fclr == MAP_FLESH_COLOR[i]) {
444                 return i;
445             }
446         }
447         return 0;
448     }
449
450     public void setFleshColor(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
451         if (value >= 0 && value <= 6) {
452             sendCommand(String.format("FCOLOR %d", MAP_FLESH_COLOR[value]));
453         }
454     }
455
456     /*
457      * Color Mode
458      */
459     public ColorMode getColorMode() throws EpsonProjectorCommandException, EpsonProjectorException {
460         int val = queryHexInt("CMODE?");
461         return ColorMode.forValue(val);
462     }
463
464     public void setColorMode(ColorMode value) throws EpsonProjectorCommandException, EpsonProjectorException {
465         sendCommand(String.format("CMODE %02X", value.toInt()));
466     }
467
468     /*
469      * Horizontal Position
470      */
471     public int getHorizontalPosition() throws EpsonProjectorCommandException, EpsonProjectorException {
472         int hpos = queryInt("HPOS?");
473         for (int i = 0; i < MAP49.length; i++) {
474             if (hpos == MAP49[i]) {
475                 return i - 23;
476             }
477         }
478         return 0;
479     }
480
481     public void setHorizontalPosition(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
482         value = value + 23;
483         if (value >= 0 && value <= 49) {
484             sendCommand(String.format("HPOS %d", MAP49[value]));
485         }
486     }
487
488     /*
489      * Vertical Position
490      */
491     public int getVerticalPosition() throws EpsonProjectorCommandException, EpsonProjectorException {
492         int vpos = queryInt("VPOS?");
493         for (int i = 0; i < MAP18.length; i++) {
494             if (vpos == MAP18[i]) {
495                 return i - 8;
496             }
497         }
498         return 0;
499     }
500
501     public void setVerticalPosition(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
502         value = value + 8;
503         if (value >= 0 && value <= 18) {
504             sendCommand(String.format("VPOS %d", MAP18[value]));
505         }
506     }
507
508     /*
509      * Gamma
510      */
511     public Gamma getGamma() throws EpsonProjectorCommandException, EpsonProjectorException {
512         int val = queryHexInt("GAMMA?");
513         return Gamma.forValue(val);
514     }
515
516     public void setGamma(Gamma value) throws EpsonProjectorCommandException, EpsonProjectorException {
517         sendCommand(String.format("GAMMA %02X", value.toInt()));
518     }
519
520     /*
521      * Volume
522      */
523     public int getVolume() throws EpsonProjectorCommandException, EpsonProjectorException {
524         int vol = queryInt("VOL?");
525         for (int i = 0; i < MAP20.length; i++) {
526             if (vol == MAP20[i]) {
527                 return i;
528             }
529         }
530         return 0;
531     }
532
533     public void setVolume(int value) throws EpsonProjectorCommandException, EpsonProjectorException {
534         if (value >= 0 && value <= 20) {
535             sendCommand(String.format("VOL %d", MAP20[value]));
536         }
537     }
538
539     /*
540      * AV Mute
541      */
542     public Switch getMute() throws EpsonProjectorCommandException, EpsonProjectorException {
543         String val = queryString("MUTE?");
544         return val.equals(ON) ? Switch.ON : Switch.OFF;
545     }
546
547     public void setMute(Switch value) throws EpsonProjectorCommandException, EpsonProjectorException {
548         sendCommand(String.format("MUTE %s", value.name()));
549     }
550
551     /*
552      * Horizontal Reverse
553      */
554     public Switch getHorizontalReverse() throws EpsonProjectorCommandException, EpsonProjectorException {
555         String val = queryString("HREVERSE?");
556         return val.equals(ON) ? Switch.ON : Switch.OFF;
557     }
558
559     public void setHorizontalReverse(Switch value) throws EpsonProjectorCommandException, EpsonProjectorException {
560         sendCommand(String.format("HREVERSE %s", value.name()));
561     }
562
563     /*
564      * Vertical Reverse
565      */
566     public Switch getVerticalReverse() throws EpsonProjectorCommandException, EpsonProjectorException {
567         String val = queryString("VREVERSE?");
568         return val.equals(ON) ? Switch.ON : Switch.OFF;
569     }
570
571     public void setVerticalReverse(Switch value) throws EpsonProjectorCommandException, EpsonProjectorException {
572         sendCommand(String.format("VREVERSE %s", value.name()));
573     }
574
575     /*
576      * Background Select for AV Mute
577      */
578     public Background getBackground() throws EpsonProjectorCommandException, EpsonProjectorException {
579         int val = queryHexInt("MSEL?");
580         return Background.forValue(val);
581     }
582
583     public void setBackground(Background value) throws EpsonProjectorCommandException, EpsonProjectorException {
584         sendCommand(String.format("MSEL %02X", value.toInt()));
585     }
586
587     /*
588      * Lamp Time (hours) - get from cache
589      */
590     public int getLampTime() throws EpsonProjectorCommandException, EpsonProjectorException {
591         Integer lampHours = cachedLampHours.getValue();
592
593         if (lampHours != null) {
594             return lampHours.intValue();
595         } else {
596             throw new EpsonProjectorCommandException("cachedLampHours returned null");
597         }
598     }
599
600     /*
601      * Get Lamp Time
602      */
603     private @Nullable Integer queryLamp() {
604         try {
605             return Integer.valueOf(queryInt("LAMP?"));
606         } catch (EpsonProjectorCommandException | EpsonProjectorException e) {
607             logger.debug("Error executing command LAMP?", e);
608             return null;
609         }
610     }
611
612     /*
613      * Error Code
614      */
615     public int getError() throws EpsonProjectorCommandException, EpsonProjectorException {
616         return queryHexInt("ERR?");
617     }
618
619     /*
620      * Error Code Description
621      */
622     public String getErrorString() throws EpsonProjectorCommandException, EpsonProjectorException {
623         int err = queryInt("ERR?");
624         return ErrorMessage.forCode(err);
625     }
626 }