]> git.basschouten.com Git - openhab-addons.git/blob
2da7a014620e34280688429f308036438b4712a2
[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.tellstick.internal.local;
14
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeoutException;
19
20 import org.eclipse.jetty.client.HttpClient;
21 import org.eclipse.jetty.client.api.ContentResponse;
22 import org.eclipse.jetty.client.api.Request;
23 import org.eclipse.jetty.http.HttpMethod;
24 import org.openhab.binding.tellstick.internal.TelldusBindingException;
25 import org.openhab.binding.tellstick.internal.conf.TelldusLocalConfiguration;
26 import org.openhab.binding.tellstick.internal.handler.TelldusDeviceController;
27 import org.openhab.binding.tellstick.internal.local.dto.TelldusLocalResponseDTO;
28 import org.openhab.binding.tellstick.internal.local.dto.TellstickLocalDeviceDTO;
29 import org.openhab.core.library.types.IncreaseDecreaseType;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.PercentType;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.tellstick.JNA;
37 import org.tellstick.device.TellstickDevice;
38 import org.tellstick.device.TellstickDeviceEvent;
39 import org.tellstick.device.TellstickException;
40 import org.tellstick.device.TellstickSensorEvent;
41 import org.tellstick.device.iface.Device;
42 import org.tellstick.device.iface.DeviceChangeListener;
43 import org.tellstick.device.iface.SensorListener;
44 import org.tellstick.device.iface.SwitchableDevice;
45
46 import com.google.gson.Gson;
47 import com.google.gson.JsonSyntaxException;
48
49 /**
50  * {@link TelldusLocalDeviceController} handles the communication with Telldus Local API (Tellstick ZNET v1/v2)
51  * This controller uses JSON based Rest API to communicate with Telldus Local API.
52  *
53  * @author Jan Gustafsson - Initial contribution
54  */
55 public class TelldusLocalDeviceController implements DeviceChangeListener, SensorListener, TelldusDeviceController {
56     private final Logger logger = LoggerFactory.getLogger(TelldusLocalDeviceController.class);
57     private long lastSend = 0;
58     public static final long DEFAULT_INTERVAL_BETWEEN_SEND_SEC = 250;
59     private final HttpClient httpClient;
60     private final Gson gson = new Gson();
61     private String localApiUrl;
62     private String authorizationHeader = "Bearer ";
63     static final String HTTP_LOCAL_API = "api/";
64     static final String HTTP_LOCAL_API_DEVICES = HTTP_LOCAL_API + "devices/list?supportedMethods=19&includeIgnored=0";
65     static final String HTTP_LOCAL_API_SENSORS = HTTP_LOCAL_API
66             + "sensors/list?includeValues=1&includeScale=1&includeUnit=1&includeIgnored=0";
67     static final String HTTP_LOCAL_API_SENSOR_INFO = HTTP_LOCAL_API + "sensor/info";
68     static final String HTTP_LOCAL_API_DEVICE_DIM = HTTP_LOCAL_API + "device/dim?id=%d&level=%d";
69     static final String HTTP_LOCAL_API_DEVICE_TURNOFF = HTTP_LOCAL_API + "device/turnOff?id=%d";
70     static final String HTTP_LOCAL_DEVICE_TURNON = HTTP_LOCAL_API + "device/turnOn?id=%d";
71     private static final int MAX_RETRIES = 3;
72
73     public TelldusLocalDeviceController(TelldusLocalConfiguration configuration, HttpClient httpClient) {
74         this.httpClient = httpClient;
75         localApiUrl = "http://" + configuration.ipAddress + "/";
76         authorizationHeader = authorizationHeader + configuration.accessToken;
77     }
78
79     @Override
80     public void dispose() {
81     }
82
83     @Override
84     public void handleSendEvent(Device device, int resendCount, boolean isdimmer, Command command)
85             throws TellstickException {
86         logger.debug("Send {} to {}", command, device);
87         try {
88             if (device instanceof TellstickLocalDeviceDTO) {
89                 if (command == OnOffType.ON) {
90                     turnOn(device);
91                 } else if (command == OnOffType.OFF) {
92                     turnOff(device);
93                 } else if (command instanceof PercentType percentCommand) {
94                     dim(device, percentCommand);
95                 } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
96                     increaseDecrease(device, increaseDecreaseCommand);
97                 }
98             } else if (device instanceof SwitchableDevice) {
99                 if (command == OnOffType.ON) {
100                     if (isdimmer) {
101                         logger.trace("Turn off first in case it is allready on");
102                         turnOff(device);
103                     }
104                     turnOn(device);
105                 } else if (command == OnOffType.OFF) {
106                     turnOff(device);
107                 }
108             } else {
109                 logger.warn("Cannot send to {}", device);
110             }
111         } catch (InterruptedException e) {
112             logger.debug("OH is shut-down.");
113         }
114     }
115
116     private void increaseDecrease(Device dev, IncreaseDecreaseType increaseDecreaseType)
117             throws TellstickException, InterruptedException {
118         String strValue = ((TellstickDevice) dev).getData();
119         double value = 0;
120         if (strValue != null) {
121             value = Double.valueOf(strValue);
122         }
123         int percent = (int) Math.round((value / 255) * 100);
124         if (IncreaseDecreaseType.INCREASE == increaseDecreaseType) {
125             percent = Math.min(percent + 10, 100);
126         } else if (IncreaseDecreaseType.DECREASE == increaseDecreaseType) {
127             percent = Math.max(percent - 10, 0);
128         }
129         dim(dev, new PercentType(percent));
130     }
131
132     private void dim(Device dev, PercentType command) throws TellstickException, InterruptedException {
133         double value = command.doubleValue();
134
135         // 0 means OFF and 100 means ON
136         if (value == 0 && dev instanceof TellstickLocalDeviceDTO) {
137             turnOff(dev);
138         } else if (value == 100 && dev instanceof TellstickLocalDeviceDTO) {
139             turnOn(dev);
140         } else if (dev instanceof TellstickLocalDeviceDTO device
141                 && (device.getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
142             long tdVal = Math.round((value / 100) * 255);
143             TelldusLocalResponseDTO response = callRestMethod(
144                     String.format(HTTP_LOCAL_API_DEVICE_DIM, dev.getId(), tdVal), TelldusLocalResponseDTO.class);
145             handleResponse(device, response);
146         } else {
147             throw new TelldusBindingException("Cannot send DIM to " + dev);
148         }
149     }
150
151     private void turnOff(Device dev) throws TellstickException, InterruptedException {
152         if (dev instanceof TellstickLocalDeviceDTO device) {
153             TelldusLocalResponseDTO response = callRestMethod(String.format(HTTP_LOCAL_API_DEVICE_TURNOFF, dev.getId()),
154                     TelldusLocalResponseDTO.class);
155             handleResponse(device, response);
156         } else {
157             throw new TelldusBindingException("Cannot send OFF to " + dev);
158         }
159     }
160
161     private void handleResponse(TellstickLocalDeviceDTO device, TelldusLocalResponseDTO response)
162             throws TellstickException {
163         if (response == null || (response.getStatus() == null && response.getError() == null)) {
164             throw new TelldusBindingException("No response " + response);
165         } else if (response.getError() != null) {
166             device.setUpdated(true);
167             throw new TelldusBindingException("Error " + response.getError());
168         } else if (!"success".equals(response.getStatus().trim())) {
169             throw new TelldusBindingException("Response " + response.getStatus());
170         }
171     }
172
173     private void turnOn(Device dev) throws TellstickException, InterruptedException {
174         if (dev instanceof TellstickLocalDeviceDTO device) {
175             TelldusLocalResponseDTO response = callRestMethod(String.format(HTTP_LOCAL_DEVICE_TURNON, dev.getId()),
176                     TelldusLocalResponseDTO.class);
177             handleResponse(device, response);
178         } else {
179             throw new TelldusBindingException("Cannot send ON to " + dev);
180         }
181     }
182
183     @Override
184     public State calcState(Device dev) {
185         TellstickLocalDeviceDTO device = (TellstickLocalDeviceDTO) dev;
186         State st = null;
187
188         switch (device.getState()) {
189             case JNA.CLibrary.TELLSTICK_TURNON:
190                 st = OnOffType.ON;
191                 break;
192             case JNA.CLibrary.TELLSTICK_TURNOFF:
193                 st = OnOffType.OFF;
194                 break;
195             case JNA.CLibrary.TELLSTICK_DIM:
196                 BigDecimal dimValue = new BigDecimal(device.getStatevalue());
197                 if (dimValue.intValue() == 0) {
198                     st = OnOffType.OFF;
199                 } else if (dimValue.intValue() >= 255) {
200                     st = OnOffType.ON;
201                 } else {
202                     st = OnOffType.ON;
203                 }
204                 break;
205             default:
206                 logger.warn("Could not handle {} for {}", device.getState(), device);
207         }
208
209         return st;
210     }
211
212     @Override
213     public BigDecimal calcDimValue(Device device) {
214         BigDecimal dimValue = BigDecimal.ZERO;
215         switch (((TellstickLocalDeviceDTO) device).getState()) {
216             case JNA.CLibrary.TELLSTICK_TURNON:
217                 dimValue = new BigDecimal(100);
218                 break;
219             case JNA.CLibrary.TELLSTICK_TURNOFF:
220                 break;
221             case JNA.CLibrary.TELLSTICK_DIM:
222                 dimValue = new BigDecimal(((TellstickLocalDeviceDTO) device).getStatevalue());
223                 dimValue = dimValue.multiply(new BigDecimal(100));
224                 dimValue = dimValue.divide(new BigDecimal(255), 0, RoundingMode.HALF_UP);
225                 break;
226             default:
227                 logger.warn("Could not handle {} for {}", (((TellstickLocalDeviceDTO) device).getState()), device);
228         }
229         return dimValue;
230     }
231
232     public long getLastSend() {
233         return lastSend;
234     }
235
236     public void setLastSend(long currentTimeMillis) {
237         lastSend = currentTimeMillis;
238     }
239
240     @Override
241     public void onRequest(TellstickSensorEvent newDevices) {
242         setLastSend(newDevices.getTimestamp());
243     }
244
245     @Override
246     public void onRequest(TellstickDeviceEvent newDevices) {
247         setLastSend(newDevices.getTimestamp());
248     }
249
250     <T> T callRestMethod(String uri, Class<T> response) throws TelldusLocalException, InterruptedException {
251         T resultObj = null;
252         try {
253             for (int i = 0; i < MAX_RETRIES; i++) {
254                 try {
255                     resultObj = innerCallRest(localApiUrl + uri, response);
256                     break;
257                 } catch (TimeoutException e) {
258                     logger.warn("TimeoutException error in get");
259                 }
260             }
261         } catch (JsonSyntaxException e) {
262             throw new TelldusLocalException(e);
263         } catch (ExecutionException e) {
264             throw new TelldusLocalException(e);
265         }
266         return resultObj;
267     }
268
269     private <T> T innerCallRest(String uri, Class<T> json)
270             throws ExecutionException, InterruptedException, TimeoutException, JsonSyntaxException {
271         logger.trace("HTTP GET: {}", uri);
272
273         Request request = httpClient.newRequest(uri).method(HttpMethod.GET);
274         request.header("Authorization", authorizationHeader);
275
276         ContentResponse response = request.send();
277         String content = response.getContentAsString();
278         logger.trace("API response: {}", content);
279
280         return gson.fromJson(content, json);
281     }
282 }