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