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.Protocol;
26 import org.openhab.binding.fineoffsetweatherstation.internal.domain.SensorGatewayBinding;
27 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.MeasuredValue;
28 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.SensorDevice;
29 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.SystemInfo;
30 import org.openhab.binding.fineoffsetweatherstation.internal.handler.ThingStatusListener;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Service to query the gateway device.
37 * @author Andreas Berger - Initial contribution
40 public class FineOffsetGatewayQueryService extends GatewayQueryService {
41 private final Logger logger = LoggerFactory.getLogger(FineOffsetGatewayQueryService.class);
43 private final FineOffsetDataParser fineOffsetDataParser;
45 private final ConversionContext conversionContext;
47 public FineOffsetGatewayQueryService(FineOffsetGatewayConfiguration config,
48 @Nullable ThingStatusListener thingStatusListener, ConversionContext conversionContext) {
49 super(config, thingStatusListener);
50 this.fineOffsetDataParser = new FineOffsetDataParser(Protocol.DEFAULT);
51 this.conversionContext = conversionContext;
55 public @Nullable String getFirmwareVersion() {
56 var data = executeCommand(Command.CMD_READ_FIRMWARE_VERSION);
58 return fineOffsetDataParser.getFirmwareVersion(data);
64 public Map<SensorGatewayBinding, SensorDevice> getRegisteredSensors() {
65 var data = executeCommand(Command.CMD_READ_SENSOR_ID_NEW);
69 return fineOffsetDataParser.getRegisteredSensors(data, () -> {
71 SystemInfo systemInfo = fetchSystemInfo();
72 if (systemInfo != null) {
73 return systemInfo.isUseWh24();
80 public @Nullable SystemInfo fetchSystemInfo() {
81 var data = executeCommand(Command.CMD_READ_SSSS);
83 logger.debug("Unexpected response to System Info!");
86 return fineOffsetDataParser.fetchSystemInfo(data);
90 public Collection<MeasuredValue> getMeasuredValues() {
91 Map<String, MeasuredValue> valuePerChannel = new LinkedHashMap<>();
93 byte[] data = executeCommand(Command.CMD_GW1000_LIVEDATA);
95 List<MeasuredValue> measuredValues = fineOffsetDataParser.getMeasuredValues(data, conversionContext);
96 for (MeasuredValue measuredValue : measuredValues) {
97 valuePerChannel.put(measuredValue.getChannelId(), measuredValue);
101 data = executeCommand(Command.CMD_READ_RAIN);
103 List<MeasuredValue> measuredRainValues = fineOffsetDataParser.getRainData(data, conversionContext);
104 for (MeasuredValue measuredValue : measuredRainValues) {
105 valuePerChannel.put(measuredValue.getChannelId(), measuredValue);
109 return valuePerChannel.values();
112 protected byte @Nullable [] executeCommand(Command command) {
113 return executeCommand(command.name(), command.getPayload(), command::isResponseValid);