]> git.basschouten.com Git - openhab-addons.git/blob
32400ae1c4eb7d247c3f140c87477d2868896812
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.benqprojector.internal;
14
15 import java.time.Duration;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.benqprojector.internal.configuration.BenqProjectorConfiguration;
20 import org.openhab.binding.benqprojector.internal.connector.BenqProjectorConnector;
21 import org.openhab.binding.benqprojector.internal.connector.BenqProjectorSerialConnector;
22 import org.openhab.binding.benqprojector.internal.connector.BenqProjectorTcpConnector;
23 import org.openhab.binding.benqprojector.internal.enums.Switch;
24 import org.openhab.core.cache.ExpiringCache;
25 import org.openhab.core.io.transport.serial.SerialPortManager;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Provide high level interface to BenQ projector.
31  *
32  * @author Michael Lobstein - Initial contribution
33  */
34 @NonNullByDefault
35 public class BenqProjectorDevice {
36     private static final String UNSUPPORTED_ITM = "Unsupported item";
37     private static final String BLOCK_ITM = "Block item";
38     private static final String ILLEGAL_FMT = "Illegal format";
39
40     private static final int LAMP_REFRESH_WAIT_MINUTES = 5;
41
42     private ExpiringCache<Integer> cachedLampHours = new ExpiringCache<>(Duration.ofMinutes(LAMP_REFRESH_WAIT_MINUTES),
43             this::queryLamp);
44
45     private final Logger logger = LoggerFactory.getLogger(BenqProjectorDevice.class);
46
47     private BenqProjectorConnector connection;
48     private boolean connected = false;
49
50     public BenqProjectorDevice(SerialPortManager serialPortManager, BenqProjectorConfiguration config) {
51         connection = new BenqProjectorSerialConnector(serialPortManager, config.serialPort);
52     }
53
54     public BenqProjectorDevice(BenqProjectorConfiguration config) {
55         connection = new BenqProjectorTcpConnector(config.host, config.port);
56     }
57
58     private synchronized String sendQuery(String query) throws BenqProjectorCommandException, BenqProjectorException {
59         logger.debug("Query: '{}'", query);
60         String response = connection.sendMessage(query);
61
62         if (response.length() == 0) {
63             throw new BenqProjectorException("No response received");
64         }
65
66         if (response.contains(UNSUPPORTED_ITM)) {
67             return "UNSUPPORTED";
68         }
69
70         if (response.contains(BLOCK_ITM)) {
71             throw new BenqProjectorCommandException("Block Item received for command: " + query);
72         }
73
74         if (response.contains(ILLEGAL_FMT)) {
75             throw new BenqProjectorCommandException("Illegal Format response received for command: " + query);
76         }
77
78         logger.debug("Response: '{}'", response);
79
80         // example: SOUR=HDMI2
81         String[] responseParts = response.split("=");
82         if (responseParts.length != 2) {
83             throw new BenqProjectorCommandException("Invalid respose for command: " + query);
84         }
85
86         return responseParts[1].toLowerCase();
87     }
88
89     protected void sendCommand(String command) throws BenqProjectorCommandException, BenqProjectorException {
90         sendQuery(command);
91     }
92
93     protected int queryInt(String query) throws BenqProjectorCommandException, BenqProjectorException {
94         String response = sendQuery(query);
95         try {
96             return Integer.parseInt(response);
97         } catch (NumberFormatException nfe) {
98             throw new BenqProjectorCommandException(
99                     "Unable to parse response '" + response + "' as Integer for command: " + query);
100         }
101     }
102
103     protected String queryString(String query) throws BenqProjectorCommandException, BenqProjectorException {
104         return sendQuery(query);
105     }
106
107     public void connect() throws BenqProjectorException {
108         connection.connect();
109         connected = true;
110     }
111
112     public void disconnect() throws BenqProjectorException {
113         connection.disconnect();
114         connected = false;
115     }
116
117     public boolean isConnected() {
118         return connected;
119     }
120
121     /*
122      * Power
123      */
124     public Switch getPowerStatus() throws BenqProjectorCommandException, BenqProjectorException {
125         return (queryString("pow=?").contains("on") ? Switch.ON : Switch.OFF);
126     }
127
128     public void setPower(Switch value) throws BenqProjectorCommandException, BenqProjectorException {
129         sendCommand(value == Switch.ON ? "pow=on" : "pow=off");
130     }
131
132     /*
133      * Source
134      */
135     public @Nullable String getSource() throws BenqProjectorCommandException, BenqProjectorException {
136         return queryString("sour=?");
137     }
138
139     public void setSource(String value) throws BenqProjectorCommandException, BenqProjectorException {
140         sendCommand(String.format("sour=%s", value));
141     }
142
143     /*
144      * Picture Mode
145      */
146     public @Nullable String getPictureMode() throws BenqProjectorCommandException, BenqProjectorException {
147         return queryString("appmod=?");
148     }
149
150     public void setPictureMode(String value) throws BenqProjectorCommandException, BenqProjectorException {
151         sendCommand(String.format("appmod=%s", value));
152     }
153
154     /*
155      * Aspect Ratio
156      */
157     public @Nullable String getAspectRatio() throws BenqProjectorCommandException, BenqProjectorException {
158         return queryString("asp=?");
159     }
160
161     public void setAspectRatio(String value) throws BenqProjectorCommandException, BenqProjectorException {
162         sendCommand(String.format("asp=%s", value));
163     }
164
165     /*
166      * Blank Screen
167      */
168     public Switch getBlank() throws BenqProjectorCommandException, BenqProjectorException {
169         return (queryString("blank=?").contains("on") ? Switch.ON : Switch.OFF);
170     }
171
172     public void setBlank(Switch value) throws BenqProjectorCommandException, BenqProjectorException {
173         sendCommand(String.format("blank=%s", (value == Switch.ON ? "on" : "off")));
174     }
175
176     /*
177      * Freeze
178      */
179     public Switch getFreeze() throws BenqProjectorCommandException, BenqProjectorException {
180         return (queryString("freeze=?").contains("on") ? Switch.ON : Switch.OFF);
181     }
182
183     public void setFreeze(Switch value) throws BenqProjectorCommandException, BenqProjectorException {
184         sendCommand(String.format("freeze=%s", (value == Switch.ON ? "on" : "off")));
185     }
186
187     /*
188      * Direct Command
189      */
190     public void sendDirectCommand(String value) throws BenqProjectorCommandException, BenqProjectorException {
191         sendCommand(value);
192     }
193
194     /*
195      * Lamp Time (hours) - get from cache
196      */
197     public int getLampTime() throws BenqProjectorCommandException, BenqProjectorException {
198         Integer lampHours = cachedLampHours.getValue();
199
200         if (lampHours != null) {
201             return lampHours.intValue();
202         } else {
203             throw new BenqProjectorCommandException("cachedLampHours returned null");
204         }
205     }
206
207     /*
208      * Get Lamp Time
209      */
210     private @Nullable Integer queryLamp() {
211         try {
212             return Integer.valueOf(queryInt("ltim=?"));
213         } catch (BenqProjectorCommandException | BenqProjectorException e) {
214             logger.debug("Error executing command ltim=?", e);
215             return null;
216         }
217     }
218 }