]> git.basschouten.com Git - openhab-addons.git/blob
43c3ba8a52d1f54092a1bde2bf9bf13bc6302e29
[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.dlinksmarthome.internal.handler;
14
15 import static org.openhab.binding.dlinksmarthome.internal.DLinkSmartHomeBindingConstants.MOTION;
16
17 import org.openhab.binding.dlinksmarthome.internal.motionsensor.DLinkMotionSensorCommunication;
18 import org.openhab.binding.dlinksmarthome.internal.motionsensor.DLinkMotionSensorCommunication.DeviceStatus;
19 import org.openhab.binding.dlinksmarthome.internal.motionsensor.DLinkMotionSensorConfig;
20 import org.openhab.binding.dlinksmarthome.internal.motionsensor.DLinkMotionSensorListener;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.openhab.core.types.Command;
27
28 /**
29  * The {@link DLinkMotionSensorHandler} is responsible for handling commands, which are
30  * sent to one of the channels.
31  *
32  * @author Mike Major - Initial contribution
33  */
34 public class DLinkMotionSensorHandler extends BaseThingHandler implements DLinkMotionSensorListener {
35
36     private DLinkMotionSensorCommunication motionSensor;
37
38     private final ChannelUID motionChannel;
39
40     public DLinkMotionSensorHandler(final Thing thing) {
41         super(thing);
42         motionChannel = new ChannelUID(getThing().getUID(), MOTION);
43     }
44
45     @Override
46     public void handleCommand(final ChannelUID channelUID, final Command command) {
47         // Does not support commands
48     }
49
50     @Override
51     public void initialize() {
52         final DLinkMotionSensorConfig config = getConfigAs(DLinkMotionSensorConfig.class);
53         motionSensor = new DLinkMotionSensorCommunication(config, this, scheduler);
54     }
55
56     @Override
57     public void motionDetected() {
58         triggerChannel(motionChannel);
59     }
60
61     @Override
62     public void sensorStatus(final DeviceStatus status) {
63         switch (status) {
64             case ONLINE:
65                 updateStatus(ThingStatus.ONLINE);
66                 break;
67             case REBOOTING:
68                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.DUTY_CYCLE, "Device rebooting");
69                 break;
70             case COMMUNICATION_ERROR:
71                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
72                 break;
73             case INVALID_PIN:
74                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Invalid pin code");
75                 break;
76             case INTERNAL_ERROR:
77                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "System error");
78                 break;
79             case UNSUPPORTED_FIRMWARE:
80                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Unsupported firmware");
81                 break;
82             default:
83                 break;
84         }
85     }
86
87     @Override
88     public void dispose() {
89         if (motionSensor != null) {
90             motionSensor.dispose();
91         }
92         super.dispose();
93     }
94 }