2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.fineoffsetweatherstation.internal.service;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.Socket;
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.List;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetGatewayConfiguration;
26 import org.openhab.binding.fineoffsetweatherstation.internal.Utils;
27 import org.openhab.binding.fineoffsetweatherstation.internal.domain.Command;
28 import org.openhab.binding.fineoffsetweatherstation.internal.domain.ConversionContext;
29 import org.openhab.binding.fineoffsetweatherstation.internal.domain.SensorGatewayBinding;
30 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.MeasuredValue;
31 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.SensorDevice;
32 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.SystemInfo;
33 import org.openhab.binding.fineoffsetweatherstation.internal.handler.ThingStatusListener;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Service to query the gateway device.
42 * @author Andreas Berger - Initial contribution
45 public class FineOffsetGatewayQueryService implements AutoCloseable {
46 private final Logger logger = LoggerFactory.getLogger(FineOffsetGatewayQueryService.class);
48 private @Nullable Socket socket;
49 private final FineOffsetGatewayConfiguration config;
50 private final ThingStatusListener thingStatusListener;
51 private final FineOffsetDataParser fineOffsetDataParser;
53 private final ConversionContext conversionContext;
55 public FineOffsetGatewayQueryService(FineOffsetGatewayConfiguration config, ThingStatusListener thingStatusListener,
56 ConversionContext conversionContext) {
58 this.thingStatusListener = thingStatusListener;
59 this.fineOffsetDataParser = new FineOffsetDataParser();
60 this.conversionContext = conversionContext;
63 public @Nullable String getFirmwareVersion() {
64 var data = executeCommand(Command.CMD_READ_FIRMWARE_VERSION);
66 return fineOffsetDataParser.getFirmwareVersion(data);
71 public Map<SensorGatewayBinding, SensorDevice> getRegisteredSensors() {
72 var data = executeCommand(Command.CMD_READ_SENSOR_ID_NEW);
76 return fineOffsetDataParser.getRegisteredSensors(data, () -> {
78 SystemInfo systemInfo = fetchSystemInfo();
79 if (systemInfo != null) {
80 return systemInfo.isUseWh24();
86 public @Nullable SystemInfo fetchSystemInfo() {
87 var data = executeCommand(Command.CMD_READ_SSSS);
89 logger.debug("Unexpected response to System Info!");
92 return fineOffsetDataParser.fetchSystemInfo(data);
95 public List<MeasuredValue> getLiveData() {
96 byte[] data = executeCommand(Command.CMD_GW1000_LIVEDATA);
98 return Collections.emptyList();
100 return fineOffsetDataParser.getLiveData(data, conversionContext);
103 private synchronized byte @Nullable [] executeCommand(Command command) {
104 byte[] buffer = new byte[2028];
106 byte[] request = command.getPayload();
109 Socket socket = getConnection();
110 if (socket == null) {
113 InputStream in = socket.getInputStream();
114 socket.getOutputStream().write(request);
115 if ((bytesRead = in.read(buffer)) == -1) {
118 if (!command.isResponseValid(buffer)) {
120 logger.debug("executeCommand({}), invalid response: {}", command,
121 Utils.toHexString(buffer, bytesRead, ""));
123 logger.debug("executeCommand({}): no response", command);
128 } catch (IOException ex) {
129 thingStatusListener.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
133 } catch (IOException e) {
137 } catch (Exception ex) {
138 logger.warn("executeCommand({})", command, ex);
142 var data = Arrays.copyOfRange(buffer, 0, bytesRead);
143 logger.trace("executeCommand({}): received: {}", command, Utils.toHexString(data, data.length, ""));
147 private synchronized @Nullable Socket getConnection() {
148 Socket socket = this.socket;
149 if (socket == null) {
151 socket = new Socket(config.ip, config.port);
152 socket.setSoTimeout(5000);
153 this.socket = socket;
154 thingStatusListener.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
155 } catch (IOException e) {
156 thingStatusListener.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
164 public void close() throws IOException {
165 Socket socket = this.socket;
167 if (socket != null) {