]> git.basschouten.com Git - openhab-addons.git/blob
6e3f564fe7f1e60e63d1d5809a329e03891dd310
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.boschshc.internal.devices.motiondetector;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_LATEST_MOTION;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.boschshc.internal.devices.BoschSHCHandler;
20 import org.openhab.binding.boschshc.internal.devices.motiondetector.dto.LatestMotionState;
21 import org.openhab.core.library.types.DateTimeType;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26
27 import com.google.gson.JsonElement;
28
29 /**
30  * MotionDetectorHandler
31  *
32  * @author Stefan Kästle - Initial contribution
33  */
34 @NonNullByDefault
35 public class MotionDetectorHandler extends BoschSHCHandler {
36
37     public MotionDetectorHandler(Thing thing) {
38         super(thing);
39     }
40
41     @Override
42     public void handleCommand(ChannelUID channelUID, Command command) {
43         logger.debug("Handle command for: {} - {}", channelUID.getThingUID(), command);
44
45         if (CHANNEL_LATEST_MOTION.equals(channelUID.getId())) {
46             if (command instanceof RefreshType) {
47                 LatestMotionState state = this.getState("LatestMotion", LatestMotionState.class);
48                 if (state != null) {
49                     updateLatestMotionState(state);
50                 }
51             }
52         }
53     }
54
55     void updateLatestMotionState(LatestMotionState state) {
56         DateTimeType date = new DateTimeType(state.latestMotionDetected);
57         updateState(CHANNEL_LATEST_MOTION, date);
58     }
59
60     @Override
61     public void processUpdate(String id, JsonElement state) {
62         logger.debug("Motion detector: received update: {} {}", id, state);
63
64         @Nullable
65         LatestMotionState latestMotionState = GSON.fromJson(state, LatestMotionState.class);
66         if (latestMotionState == null) {
67             logger.warn("Received unknown update in in-wall switch: {}", state);
68             return;
69         }
70         updateLatestMotionState(latestMotionState);
71     }
72 }