]> git.basschouten.com Git - openhab-addons.git/blob
c16a7a058eb49397b3cdc47f29b05cd7edee4bd8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.verisure.internal.handler;
14
15 import static org.openhab.binding.verisure.internal.VerisureBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Set;
20
21 import javax.measure.quantity.Temperature;
22 import javax.measure.quantity.Time;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.binding.verisure.internal.dto.VerisureMiceDetectionDTO;
26 import org.openhab.binding.verisure.internal.dto.VerisureMiceDetectionDTO.Detection;
27 import org.openhab.binding.verisure.internal.dto.VerisureMiceDetectionDTO.Mouse;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.library.unit.SIUnits;
32 import org.openhab.core.library.unit.Units;
33 import org.openhab.core.thing.Channel;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.types.State;
38 import org.openhab.core.types.UnDefType;
39
40 /**
41  * Handler for the Mice Detection thing type that Verisure provides.
42  *
43  * @author Jan Gustafsson - Initial contribution
44  *
45  */
46 @NonNullByDefault
47 public class VerisureMiceDetectionThingHandler extends VerisureThingHandler<VerisureMiceDetectionDTO> {
48
49     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_MICE_DETECTION);
50
51     public VerisureMiceDetectionThingHandler(Thing thing) {
52         super(thing);
53     }
54
55     @Override
56     public Class<VerisureMiceDetectionDTO> getVerisureThingClass() {
57         return VerisureMiceDetectionDTO.class;
58     }
59
60     @Override
61     public synchronized void update(VerisureMiceDetectionDTO thing) {
62         updateMiceDetectionState(thing);
63         updateStatus(ThingStatus.ONLINE);
64     }
65
66     private void updateMiceDetectionState(VerisureMiceDetectionDTO miceDetectionJSON) {
67         List<Mouse> miceList = miceDetectionJSON.getData().getInstallation().getMice();
68         if (!miceList.isEmpty()) {
69             Mouse mouse = miceList.get(0);
70             getThing().getChannels().stream().map(Channel::getUID).filter(channelUID -> isLinked(channelUID)
71                     && !channelUID.getId().equals("timestamp") && !channelUID.getId().equals("temperatureTimestamp"))
72                     .forEach(channelUID -> {
73                         State state = getValue(channelUID.getId(), miceDetectionJSON, mouse);
74                         updateState(channelUID, state);
75                     });
76             if (mouse.getDetections().size() != 0) {
77                 updateTimeStamp(mouse.getDetections().get(0).getNodeTime());
78             }
79             updateTimeStamp(miceDetectionJSON.getTemperatureTime(), CHANNEL_TEMPERATURE_TIMESTAMP);
80             updateInstallationChannels(miceDetectionJSON);
81         } else {
82             logger.debug("MiceList is empty!");
83         }
84     }
85
86     public State getValue(String channelId, VerisureMiceDetectionDTO miceDetectionJSON, Mouse mouse) {
87         switch (channelId) {
88             case CHANNEL_COUNT_LATEST_DETECTION:
89                 if (mouse.getDetections().size() == 0) {
90                     return new DecimalType(0);
91                 } else {
92                     return new DecimalType(mouse.getDetections().get(0).getCount());
93                 }
94             case CHANNEL_COUNT_LAST_24_HOURS:
95                 if (mouse.getDetections().size() == 0) {
96                     return new DecimalType(0);
97                 } else {
98                     return new DecimalType(mouse.getDetections().stream().mapToInt(Detection::getCount).sum());
99                 }
100             case CHANNEL_DURATION_LATEST_DETECTION:
101                 if (mouse.getDetections().size() == 0) {
102                     return new QuantityType<Time>(0, Units.SECOND);
103                 } else {
104                     return new QuantityType<Time>(mouse.getDetections().get(0).getDuration(), Units.SECOND);
105                 }
106             case CHANNEL_DURATION_LAST_24_HOURS:
107                 if (mouse.getDetections().size() == 0) {
108                     return new QuantityType<Time>(0, Units.SECOND);
109                 } else {
110                     return new QuantityType<Time>(mouse.getDetections().stream().mapToInt(Detection::getDuration).sum(),
111                             Units.SECOND);
112                 }
113             case CHANNEL_LOCATION:
114                 String location = mouse.getDevice().getArea();
115                 return location != null ? new StringType(location) : UnDefType.NULL;
116             case CHANNEL_TEMPERATURE:
117                 double temperature = miceDetectionJSON.getTemperatureValue();
118                 return temperature != VerisureMiceDetectionDTO.UNDEFINED
119                         ? new QuantityType<Temperature>(temperature, SIUnits.CELSIUS)
120                         : UnDefType.UNDEF;
121         }
122         return UnDefType.UNDEF;
123     }
124
125     @Override
126     public void updateTriggerChannel(String event) {
127         logger.debug("MiceThingHandler trigger event {}", event);
128         triggerChannel(CHANNEL_MICE_DETECTION_TRIGGER_CHANNEL, event);
129     }
130 }