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.util.Collections;
16 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetGatewayConfiguration;
22 import org.openhab.binding.fineoffsetweatherstation.internal.domain.Command;
23 import org.openhab.binding.fineoffsetweatherstation.internal.domain.ConversionContext;
24 import org.openhab.binding.fineoffsetweatherstation.internal.domain.Protocol;
25 import org.openhab.binding.fineoffsetweatherstation.internal.domain.SensorGatewayBinding;
26 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.MeasuredValue;
27 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.SensorDevice;
28 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.SystemInfo;
29 import org.openhab.binding.fineoffsetweatherstation.internal.handler.ThingStatusListener;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Service to query the gateway device.
36 * @author Andreas Berger - Initial contribution
39 public class FineOffsetGatewayQueryService extends GatewayQueryService {
40 private final Logger logger = LoggerFactory.getLogger(FineOffsetGatewayQueryService.class);
42 private final FineOffsetDataParser fineOffsetDataParser;
44 private final ConversionContext conversionContext;
46 public FineOffsetGatewayQueryService(FineOffsetGatewayConfiguration config,
47 @Nullable ThingStatusListener thingStatusListener, ConversionContext conversionContext) {
48 super(config, thingStatusListener);
49 this.fineOffsetDataParser = new FineOffsetDataParser(Protocol.DEFAULT);
50 this.conversionContext = conversionContext;
54 public @Nullable String getFirmwareVersion() {
55 var data = executeCommand(Command.CMD_READ_FIRMWARE_VERSION);
57 return fineOffsetDataParser.getFirmwareVersion(data);
63 public Map<SensorGatewayBinding, SensorDevice> getRegisteredSensors() {
64 var data = executeCommand(Command.CMD_READ_SENSOR_ID_NEW);
68 return fineOffsetDataParser.getRegisteredSensors(data, () -> {
70 SystemInfo systemInfo = fetchSystemInfo();
71 if (systemInfo != null) {
72 return systemInfo.isUseWh24();
79 public @Nullable SystemInfo fetchSystemInfo() {
80 var data = executeCommand(Command.CMD_READ_SSSS);
82 logger.debug("Unexpected response to System Info!");
85 return fineOffsetDataParser.fetchSystemInfo(data);
89 public List<MeasuredValue> getMeasuredValues() {
90 byte[] data = executeCommand(Command.CMD_GW1000_LIVEDATA);
92 return Collections.emptyList();
94 return fineOffsetDataParser.getMeasuredValues(data, conversionContext);
97 protected byte @Nullable [] executeCommand(Command command) {
98 return executeCommand(command.name(), command.getPayload(), command::isResponseValid);