]> git.basschouten.com Git - openhab-addons.git/blob
a5921976a5eeebc0c0ec57c39319fd93cd237f39
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Collection;
16 import java.util.LinkedHashMap;
17 import java.util.List;
18 import java.util.Map;
19
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;
33
34 /**
35  * Service to query the gateway device.
36  *
37  * @author Andreas Berger - Initial contribution
38  */
39 @NonNullByDefault
40 public class FineOffsetGatewayQueryService extends GatewayQueryService {
41     private final Logger logger = LoggerFactory.getLogger(FineOffsetGatewayQueryService.class);
42
43     private final FineOffsetDataParser fineOffsetDataParser;
44
45     private final ConversionContext conversionContext;
46
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;
52     }
53
54     @Override
55     public @Nullable String getFirmwareVersion() {
56         var data = executeCommand(Command.CMD_READ_FIRMWARE_VERSION);
57         if (null != data) {
58             return fineOffsetDataParser.getFirmwareVersion(data);
59         }
60         return null;
61     }
62
63     @Override
64     public Map<SensorGatewayBinding, SensorDevice> getRegisteredSensors() {
65         var data = executeCommand(Command.CMD_READ_SENSOR_ID_NEW);
66         if (null == data) {
67             return Map.of();
68         }
69         return fineOffsetDataParser.getRegisteredSensors(data, () -> {
70             @Nullable
71             SystemInfo systemInfo = fetchSystemInfo();
72             if (systemInfo != null) {
73                 return systemInfo.isUseWh24();
74             }
75             return null;
76         });
77     }
78
79     @Override
80     public @Nullable SystemInfo fetchSystemInfo() {
81         var data = executeCommand(Command.CMD_READ_SSSS);
82         if (data == null) {
83             logger.debug("Unexpected response to System Info!");
84             return null;
85         }
86         return fineOffsetDataParser.fetchSystemInfo(data);
87     }
88
89     @Override
90     public Collection<MeasuredValue> getMeasuredValues() {
91         Map<String, MeasuredValue> valuePerChannel = new LinkedHashMap<>();
92
93         byte[] data = executeCommand(Command.CMD_GW1000_LIVEDATA);
94         if (data != null) {
95             List<MeasuredValue> measuredValues = fineOffsetDataParser.getMeasuredValues(data, conversionContext);
96             for (MeasuredValue measuredValue : measuredValues) {
97                 valuePerChannel.put(measuredValue.getChannelId(), measuredValue);
98             }
99         }
100
101         data = executeCommand(Command.CMD_READ_RAIN);
102         if (data != null) {
103             List<MeasuredValue> measuredRainValues = fineOffsetDataParser.getRainData(data, conversionContext);
104             for (MeasuredValue measuredValue : measuredRainValues) {
105                 valuePerChannel.put(measuredValue.getChannelId(), measuredValue);
106             }
107         }
108
109         return valuePerChannel.values();
110     }
111
112     protected byte @Nullable [] executeCommand(Command command) {
113         return executeCommand(command.name(), command.getPayload(), command::isResponseValid);
114     }
115 }