]> git.basschouten.com Git - openhab-addons.git/blob
7258110643c448ce16c4bec4d736211e8906eda8
[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.bticinosmarther.internal.api.dto;
14
15 import java.util.List;
16
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.openhab.binding.bticinosmarther.internal.api.exception.SmartherIllegalPropertyValueException;
19 import org.openhab.core.types.State;
20 import org.openhab.core.types.UnDefType;
21
22 /**
23  * The {@code Sensor} class defines the dto for Smarther API sensor object.
24  *
25  * @author Fabio Possieri - Initial contribution
26  */
27 public class Sensor {
28
29     private List<Measure> measures;
30
31     /**
32      * Returns the list of measures this sensor takes.
33      *
34      * @return the measures this sensor takes, may be {@code null}
35      */
36     public @Nullable List<Measure> getMeasures() {
37         return measures;
38     }
39
40     /**
41      * Returns the measure taken by this sensor at the given index.
42      *
43      * @param index
44      *            the index to get the measure for
45      *
46      * @return the requested measure, or {@code null} in case of no measure found at given index
47      */
48     public @Nullable Measure getMeasure(int index) {
49         return (measures != null && measures.size() > index) ? measures.get(index) : null;
50     }
51
52     /**
53      * Returns the overall state of the sensor.
54      *
55      * @return a {@link State} object representing the overall state of the sensor
56      *
57      * @throws SmartherIllegalPropertyValueException if the sensor internal raw state cannot be mapped to any valid
58      *             value
59      */
60     public State toState() throws SmartherIllegalPropertyValueException {
61         final Measure measure = getMeasure(0);
62         return (measure != null) ? measure.toState() : UnDefType.UNDEF;
63     }
64
65     @Override
66     public String toString() {
67         return String.format("measures=%s", measures);
68     }
69 }