2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.verisure.internal.handler;
15 import static org.openhab.binding.verisure.internal.VerisureBindingConstants.*;
17 import java.util.List;
20 import javax.measure.quantity.Temperature;
21 import javax.measure.quantity.Time;
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;
40 * Handler for the Mice Detection thing type that Verisure provides.
42 * @author Jan Gustafsson - Initial contribution
46 public class VerisureMiceDetectionThingHandler extends VerisureThingHandler<VerisureMiceDetectionDTO> {
48 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_MICE_DETECTION);
50 public VerisureMiceDetectionThingHandler(Thing thing) {
55 public Class<VerisureMiceDetectionDTO> getVerisureThingClass() {
56 return VerisureMiceDetectionDTO.class;
60 public synchronized void update(VerisureMiceDetectionDTO thing) {
61 updateMiceDetectionState(thing);
62 updateStatus(ThingStatus.ONLINE);
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);
75 if (!mouse.getDetections().isEmpty()) {
76 updateTimeStamp(mouse.getDetections().get(0).getNodeTime());
78 updateTimeStamp(miceDetectionJSON.getTemperatureTime(), CHANNEL_TEMPERATURE_TIMESTAMP);
79 updateInstallationChannels(miceDetectionJSON);
81 logger.debug("MiceList is empty!");
85 public State getValue(String channelId, VerisureMiceDetectionDTO miceDetectionJSON, Mouse mouse) {
87 case CHANNEL_COUNT_LATEST_DETECTION:
88 if (mouse.getDetections().isEmpty()) {
89 return new DecimalType(0);
91 return new DecimalType(mouse.getDetections().get(0).getCount());
93 case CHANNEL_COUNT_LAST_24_HOURS:
94 if (mouse.getDetections().isEmpty()) {
95 return new DecimalType(0);
97 return new DecimalType(mouse.getDetections().stream().mapToInt(Detection::getCount).sum());
99 case CHANNEL_DURATION_LATEST_DETECTION:
100 if (mouse.getDetections().isEmpty()) {
101 return new QuantityType<Time>(0, Units.SECOND);
103 return new QuantityType<Time>(mouse.getDetections().get(0).getDuration(), Units.SECOND);
105 case CHANNEL_DURATION_LAST_24_HOURS:
106 if (mouse.getDetections().isEmpty()) {
107 return new QuantityType<Time>(0, Units.SECOND);
109 return new QuantityType<Time>(mouse.getDetections().stream().mapToInt(Detection::getDuration).sum(),
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)
121 return UnDefType.UNDEF;
125 public void updateTriggerChannel(String event) {
126 logger.debug("MiceThingHandler trigger event {}", event);
127 triggerChannel(CHANNEL_MICE_DETECTION_TRIGGER_CHANNEL, event);