]> git.basschouten.com Git - openhab-addons.git/blob
7f6e6a9ca39e31ffbf5c8d1a83a58a03e87bcc6d
[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.onewire.internal.handler;
14
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
16
17 import java.nio.charset.StandardCharsets;
18 import java.util.Collections;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider;
26 import org.openhab.binding.onewire.internal.OwException;
27 import org.openhab.binding.onewire.internal.OwPageBuffer;
28 import org.openhab.binding.onewire.internal.device.EDS006x;
29 import org.openhab.binding.onewire.internal.device.OwSensorType;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32
33 /**
34  * The {@link EDSSensorThingHandler} is responsible for handling EDS multisensors
35  *
36  * @author Jan N. Klug - Initial contribution
37  */
38 @NonNullByDefault
39 public class EDSSensorThingHandler extends OwBaseThingHandler {
40     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_EDS_ENV);
41     public static final Set<OwSensorType> SUPPORTED_SENSOR_TYPES = Collections
42             .unmodifiableSet(Stream.of(OwSensorType.EDS0064, OwSensorType.EDS0065, OwSensorType.EDS0066,
43                     OwSensorType.EDS0067, OwSensorType.EDS0068).collect(Collectors.toSet()));
44     private static final Set<String> REQUIRED_PROPERTIES = Collections.singleton(PROPERTY_HW_REVISION);
45
46     public EDSSensorThingHandler(Thing thing, OwDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
47         super(thing, dynamicStateDescriptionProvider, SUPPORTED_SENSOR_TYPES, REQUIRED_PROPERTIES);
48     }
49
50     @Override
51     public void initialize() {
52         if (!super.configureThingHandler()) {
53             return;
54         }
55
56         // add sensors
57         sensors.add(new EDS006x(sensorId, sensorType, this));
58
59         scheduler.execute(() -> {
60             configureThingChannels();
61         });
62     }
63
64     @Override
65     public void updateSensorProperties(OwserverBridgeHandler bridgeHandler) throws OwException {
66         Map<String, String> properties = editProperties();
67
68         OwPageBuffer pages = bridgeHandler.readPages(sensorId);
69
70         OwSensorType sensorType = OwSensorType.UNKNOWN;
71         try {
72             sensorType = OwSensorType.valueOf(new String(pages.getPage(0), 0, 7, StandardCharsets.US_ASCII));
73         } catch (IllegalArgumentException e) {
74         }
75
76         if (!SUPPORTED_SENSOR_TYPES.contains(sensorType)) {
77             throw new OwException("sensorType not supported for EDSSensorThing");
78         }
79
80         int fwRevisionLow = pages.getByte(3, 3);
81         int fwRevisionHigh = pages.getByte(3, 4);
82         String fwRevision = String.format("%d.%d", fwRevisionHigh, fwRevisionLow);
83
84         properties.put(PROPERTY_MODELID, sensorType.name());
85         properties.put(PROPERTY_VENDOR, "Embedded Data Systems");
86         properties.put(PROPERTY_HW_REVISION, String.valueOf(fwRevision));
87
88         updateProperties(properties);
89     }
90 }