]> git.basschouten.com Git - openhab-addons.git/blob
02e076e78706b54b6420c12f322d218ab72b7e47
[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.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.DebugDetails;
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
32 /**
33  * Service to query an ELV gateway device.
34  *
35  * @author Andreas Berger - Initial contribution
36  */
37 @NonNullByDefault
38 public class ELVGatewayQueryService extends GatewayQueryService {
39
40     private final FineOffsetDataParser fineOffsetDataParser;
41
42     private final ConversionContext conversionContext;
43
44     public ELVGatewayQueryService(FineOffsetGatewayConfiguration config,
45             @Nullable ThingStatusListener thingStatusListener, ConversionContext conversionContext) {
46         super(config, thingStatusListener);
47         this.fineOffsetDataParser = new FineOffsetDataParser(Protocol.ELV);
48         this.conversionContext = conversionContext;
49     }
50
51     @Override
52     public @Nullable String getFirmwareVersion() {
53         Command command = Command.CMD_READ_FIRMWARE_VERSION;
54         var data = executeCommand(command.name(), command.getPayloadAlternative(), bytes -> true);
55         if (null != data) {
56             return fineOffsetDataParser.getFirmwareVersion(data);
57         }
58         return null;
59     }
60
61     @Override
62     public Map<SensorGatewayBinding, SensorDevice> getRegisteredSensors() {
63         // not supported by ELV device
64         return Collections.emptyMap();
65     }
66
67     @Override
68     public @Nullable SystemInfo fetchSystemInfo() {
69         // not supported by ELV device
70         return null;
71     }
72
73     @Override
74     public List<MeasuredValue> getMeasuredValues() {
75         Command command = Command.CMD_WS980_LIVEDATA;
76         // since this request has 2 checksums we shortcut it here and provide the concrete payload directly
77         byte[] payload = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0x0b, (byte) 0x00, (byte) 0x06, (byte) 0x04,
78                 (byte) 0x04, (byte) 0x19 };
79         byte[] data = executeCommand(command.name(), payload, command::isResponseValid);
80         if (data == null) {
81             return Collections.emptyList();
82         }
83         DebugDetails debugDetails = new DebugDetails(data, Command.CMD_WS980_LIVEDATA, Protocol.ELV);
84         List<MeasuredValue> measuredValues = fineOffsetDataParser.getMeasuredValues(data, conversionContext,
85                 debugDetails);
86         logger.trace("{}", debugDetails);
87         return measuredValues;
88     }
89 }