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.mihome.internal.handler;
15 import static org.openhab.binding.mihome.internal.XiaomiGatewayBindingConstants.*;
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;
23 import com.google.gson.JsonObject;
26 * Handles the Xiaomi magic controller cube
28 * @author Patrick Boos - Initial contribution
29 * @author Dieter Schmidt - Refactor
31 public class XiaomiSensorCubeHandler extends XiaomiSensorBaseHandler {
33 private final Logger logger = LoggerFactory.getLogger(XiaomiSensorCubeHandler.class);
34 private static final String STATUS = "status";
35 private static final String ROTATE = "rotate";
37 public XiaomiSensorCubeHandler(Thing thing) {
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)) {
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);
57 time = Integer.parseInt((data.get(ROTATE).getAsString().split(",")[1]));
58 } catch (NumberFormatException e) {
59 logger.error("Could not parse rotation time", e);
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");