]> git.basschouten.com Git - openhab-addons.git/blob
a1e0933cfe7878feedf9304e78b8e632e198e6e8
[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.QuantityType;
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 magic controller cube
27  *
28  * @author Patrick Boos - Initial contribution
29  * @author Dieter Schmidt - Refactor
30  */
31 public class XiaomiSensorCubeHandler extends XiaomiSensorBaseHandler {
32
33     private final Logger logger = LoggerFactory.getLogger(XiaomiSensorCubeHandler.class);
34     private static final String STATUS = "status";
35     private static final String ROTATE = "rotate";
36
37     public XiaomiSensorCubeHandler(Thing thing) {
38         super(thing);
39     }
40
41     @Override
42     void parseReport(JsonObject data) {
43         if (data.has(STATUS)) {
44             triggerChannel(CHANNEL_ACTION, data.get(STATUS).getAsString().toUpperCase());
45             updateState(CHANNEL_LAST_ACTION, new DateTimeType());
46         } else if (data.has(ROTATE)) {
47             Integer rot = 0;
48             Integer time = 0;
49             try {
50                 rot = Integer.parseInt((data.get(ROTATE).getAsString().split(",")[0]));
51                 // convert from percent to angle degrees
52                 rot = (int) (rot * 3.6);
53             } catch (NumberFormatException e) {
54                 logger.error("Could not parse rotation angle", e);
55             }
56             try {
57                 time = Integer.parseInt((data.get(ROTATE).getAsString().split(",")[1]));
58             } catch (NumberFormatException e) {
59                 logger.error("Could not parse rotation time", e);
60             }
61             updateState(CHANNEL_CUBE_ROTATION_ANGLE, new QuantityType<>(rot, ANGLE_UNIT));
62             updateState(CHANNEL_CUBE_ROTATION_TIME, new QuantityType<>(time, TIME_UNIT));
63             triggerChannel(CHANNEL_ACTION, rot < 0 ? "ROTATE_LEFT" : "ROTATE_RIGHT");
64         }
65     }
66 }