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