2 * Copyright (c) 2010-2023 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.util.Collection;
16 import java.util.LinkedHashMap;
17 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetGatewayConfiguration;
23 import org.openhab.binding.fineoffsetweatherstation.internal.domain.Command;
24 import org.openhab.binding.fineoffsetweatherstation.internal.domain.ConversionContext;
25 import org.openhab.binding.fineoffsetweatherstation.internal.domain.DebugDetails;
26 import org.openhab.binding.fineoffsetweatherstation.internal.domain.Protocol;
27 import org.openhab.binding.fineoffsetweatherstation.internal.domain.SensorGatewayBinding;
28 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.MeasuredValue;
29 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.SensorDevice;
30 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.SystemInfo;
31 import org.openhab.binding.fineoffsetweatherstation.internal.handler.ThingStatusListener;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Service to query the gateway device.
38 * @author Andreas Berger - Initial contribution
41 public class FineOffsetGatewayQueryService extends GatewayQueryService {
42 private static final Protocol PROTOCOL = Protocol.DEFAULT;
43 private final Logger logger = LoggerFactory.getLogger(FineOffsetGatewayQueryService.class);
45 private final FineOffsetDataParser fineOffsetDataParser;
47 private final ConversionContext conversionContext;
49 public FineOffsetGatewayQueryService(FineOffsetGatewayConfiguration config,
50 @Nullable ThingStatusListener thingStatusListener, ConversionContext conversionContext) {
51 super(config, thingStatusListener);
52 this.fineOffsetDataParser = new FineOffsetDataParser(PROTOCOL);
53 this.conversionContext = conversionContext;
57 public @Nullable String getFirmwareVersion() {
58 var data = executeCommand(Command.CMD_READ_FIRMWARE_VERSION);
60 return fineOffsetDataParser.getFirmwareVersion(data);
66 public Map<SensorGatewayBinding, SensorDevice> getRegisteredSensors() {
67 var data = executeCommand(Command.CMD_READ_SENSOR_ID_NEW);
71 return fineOffsetDataParser.getRegisteredSensors(data, () -> {
73 SystemInfo systemInfo = fetchSystemInfo();
74 if (systemInfo != null) {
75 return systemInfo.isUseWh24();
82 public @Nullable SystemInfo fetchSystemInfo() {
83 var data = executeCommand(Command.CMD_READ_SSSS);
85 logger.debug("Unexpected response to System Info!");
88 return fineOffsetDataParser.fetchSystemInfo(data);
92 public Collection<MeasuredValue> getMeasuredValues() {
93 Map<String, MeasuredValue> valuePerChannel = new LinkedHashMap<>();
95 byte[] data = executeCommand(Command.CMD_GW1000_LIVEDATA);
97 DebugDetails debugDetails = new DebugDetails(data, Command.CMD_GW1000_LIVEDATA, PROTOCOL);
98 List<MeasuredValue> measuredValues = fineOffsetDataParser.getMeasuredValues(data, conversionContext,
100 for (MeasuredValue measuredValue : measuredValues) {
101 valuePerChannel.put(measuredValue.getChannelId(), measuredValue);
103 logger.trace("{}", debugDetails);
106 data = executeCommand(Command.CMD_READ_RAIN);
108 DebugDetails debugDetails = new DebugDetails(data, Command.CMD_READ_RAIN, PROTOCOL);
109 List<MeasuredValue> measuredRainValues = fineOffsetDataParser.getRainData(data, conversionContext,
111 for (MeasuredValue measuredValue : measuredRainValues) {
112 valuePerChannel.put(measuredValue.getChannelId(), measuredValue);
114 logger.trace("{}", debugDetails);
117 return valuePerChannel.values();
120 protected byte @Nullable [] executeCommand(Command command) {
121 return executeCommand(command.name(), command.getPayload(), command::isResponseValid);