]> git.basschouten.com Git - openhab-addons.git/blob
7520f954087586088bab1db9ba41b1472424aa0d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.fineoffsetweatherstation.internal.service;
14
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18
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;
32
33 /**
34  * Service to query the gateway device.
35  *
36  * @author Andreas Berger - Initial contribution
37  */
38 @NonNullByDefault
39 public class FineOffsetGatewayQueryService extends GatewayQueryService {
40     private final Logger logger = LoggerFactory.getLogger(FineOffsetGatewayQueryService.class);
41
42     private final FineOffsetDataParser fineOffsetDataParser;
43
44     private final ConversionContext conversionContext;
45
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;
51     }
52
53     @Override
54     public @Nullable String getFirmwareVersion() {
55         var data = executeCommand(Command.CMD_READ_FIRMWARE_VERSION);
56         if (null != data) {
57             return fineOffsetDataParser.getFirmwareVersion(data);
58         }
59         return null;
60     }
61
62     @Override
63     public Map<SensorGatewayBinding, SensorDevice> getRegisteredSensors() {
64         var data = executeCommand(Command.CMD_READ_SENSOR_ID_NEW);
65         if (null == data) {
66             return Map.of();
67         }
68         return fineOffsetDataParser.getRegisteredSensors(data, () -> {
69             @Nullable
70             SystemInfo systemInfo = fetchSystemInfo();
71             if (systemInfo != null) {
72                 return systemInfo.isUseWh24();
73             }
74             return null;
75         });
76     }
77
78     @Override
79     public @Nullable SystemInfo fetchSystemInfo() {
80         var data = executeCommand(Command.CMD_READ_SSSS);
81         if (data == null) {
82             logger.debug("Unexpected response to System Info!");
83             return null;
84         }
85         return fineOffsetDataParser.fetchSystemInfo(data);
86     }
87
88     @Override
89     public List<MeasuredValue> getMeasuredValues() {
90         byte[] data = executeCommand(Command.CMD_GW1000_LIVEDATA);
91         if (data == null) {
92             return Collections.emptyList();
93         }
94         return fineOffsetDataParser.getMeasuredValues(data, conversionContext);
95     }
96
97     protected byte @Nullable [] executeCommand(Command command) {
98         return executeCommand(command.name(), command.getPayload(), command::isResponseValid);
99     }
100 }