]> git.basschouten.com Git - openhab-addons.git/blob
d1556c386fe976aab4d615318207bba8268a800d
[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.mihome.internal.handler;
14
15 import static org.openhab.binding.mihome.internal.XiaomiGatewayBindingConstants.*;
16
17 import org.openhab.core.library.types.DateTimeType;
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.thing.Thing;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.gson.JsonObject;
24
25 /**
26  * Handles the Xiaomi Aqara Smart Motion Vibration Sensor
27  *
28  * @author Dieter Schmidt - Initial contribution
29  */
30 public class XiaomiSensorVibrationHandler extends XiaomiSensorBaseHandler {
31
32     private final Logger logger = LoggerFactory.getLogger(XiaomiSensorVibrationHandler.class);
33     private static final String STATUS = "status";
34     private static final String TILT_ANGLE = "final_tilt_angle";
35     private static final String ORIENTATIONS = "coordination";
36     private static final String BED_ACTIVITY = "bed_activity";
37
38     public XiaomiSensorVibrationHandler(Thing thing) {
39         super(thing);
40     }
41
42     @Override
43     void parseReport(JsonObject data) {
44         if (data.has(STATUS)) {
45             triggerChannel(CHANNEL_ACTION, data.get(STATUS).getAsString().toUpperCase());
46             updateState(CHANNEL_LAST_ACTION, new DateTimeType());
47         } else if (data.has(TILT_ANGLE)) {
48             updateState(CHANNEL_TILT_ANGLE, new DecimalType(Integer.parseInt((data.get(TILT_ANGLE).getAsString()))));
49         } else if (data.has(ORIENTATIONS)) {
50             Integer x = 0;
51             Integer y = 0;
52             Integer z = 0;
53             try {
54                 x = Integer.parseInt((data.get(ORIENTATIONS).getAsString().split(",")[0]));
55                 y = Integer.parseInt((data.get(ORIENTATIONS).getAsString().split(",")[1]));
56                 z = Integer.parseInt((data.get(ORIENTATIONS).getAsString().split(",")[2]));
57             } catch (NumberFormatException e) {
58                 logger.error("Could not parse coordinates", e);
59             }
60             updateState(CHANNEL_ORIENTATION_X, new DecimalType(x));
61             updateState(CHANNEL_ORIENTATION_Y, new DecimalType(y));
62             updateState(CHANNEL_ORIENTATION_Z, new DecimalType(z));
63         } else if (data.has(BED_ACTIVITY)) {
64             updateState(CHANNEL_BED_ACTIVITY,
65                     new DecimalType(Integer.parseInt((data.get(BED_ACTIVITY).getAsString()))));
66         }
67     }
68 }