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.Collections;
18 import java.util.List;
21 import javax.measure.quantity.Temperature;
22 import javax.measure.quantity.Time;
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;
41 * Handler for the Mice Detection thing type that Verisure provides.
43 * @author Jan Gustafsson - Initial contribution
47 public class VerisureMiceDetectionThingHandler extends VerisureThingHandler<VerisureMiceDetectionDTO> {
49 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_MICE_DETECTION);
51 public VerisureMiceDetectionThingHandler(Thing thing) {
56 public Class<VerisureMiceDetectionDTO> getVerisureThingClass() {
57 return VerisureMiceDetectionDTO.class;
61 public synchronized void update(VerisureMiceDetectionDTO thing) {
62 updateMiceDetectionState(thing);
63 updateStatus(ThingStatus.ONLINE);
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);
76 if (mouse.getDetections().size() != 0) {
77 updateTimeStamp(mouse.getDetections().get(0).getNodeTime());
79 updateTimeStamp(miceDetectionJSON.getTemperatureTime(), CHANNEL_TEMPERATURE_TIMESTAMP);
80 updateInstallationChannels(miceDetectionJSON);
82 logger.debug("MiceList is empty!");
86 public State getValue(String channelId, VerisureMiceDetectionDTO miceDetectionJSON, Mouse mouse) {
88 case CHANNEL_COUNT_LATEST_DETECTION:
89 if (mouse.getDetections().size() == 0) {
90 return new DecimalType(0);
92 return new DecimalType(mouse.getDetections().get(0).getCount());
94 case CHANNEL_COUNT_LAST_24_HOURS:
95 if (mouse.getDetections().size() == 0) {
96 return new DecimalType(0);
98 return new DecimalType(mouse.getDetections().stream().mapToInt(Detection::getCount).sum());
100 case CHANNEL_DURATION_LATEST_DETECTION:
101 if (mouse.getDetections().size() == 0) {
102 return new QuantityType<Time>(0, Units.SECOND);
104 return new QuantityType<Time>(mouse.getDetections().get(0).getDuration(), Units.SECOND);
106 case CHANNEL_DURATION_LAST_24_HOURS:
107 if (mouse.getDetections().size() == 0) {
108 return new QuantityType<Time>(0, Units.SECOND);
110 return new QuantityType<Time>(mouse.getDetections().stream().mapToInt(Detection::getDuration).sum(),
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)
122 return UnDefType.UNDEF;
126 public void updateTriggerChannel(String event) {
127 logger.debug("MiceThingHandler trigger event {}", event);
128 triggerChannel(CHANNEL_MICE_DETECTION_TRIGGER_CHANNEL, event);