2 * Copyright (c) 2010-2022 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.boschindego.internal;
15 import static java.util.Map.entry;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.boschindego.internal.dto.DeviceCommand;
24 * {@link DeviceStatus} describes status codes from the device with corresponding
25 * ready state and associated command.
27 * @author Jacob Laursen - Initial contribution
30 public class DeviceStatus {
32 public static final int STATE_LEARNING_LAWN = 516;
34 private final static String STATE_PREFIX = "indego.state.";
35 private final static String STATE_UNKNOWN = "unknown";
37 private static final Map<Integer, DeviceStatus> STATUS_MAP = Map.ofEntries(
38 entry(0, new DeviceStatus("reading-status", false, DeviceCommand.RETURN)),
39 entry(257, new DeviceStatus("charging", false, DeviceCommand.RETURN)),
40 entry(258, new DeviceStatus("docked", true, DeviceCommand.RETURN)),
41 entry(259, new DeviceStatus("docked-software-update", false, DeviceCommand.RETURN)),
42 entry(260, new DeviceStatus("docked", true, DeviceCommand.RETURN)),
43 entry(261, new DeviceStatus("docked", true, DeviceCommand.RETURN)),
44 entry(262, new DeviceStatus("docked-loading-map", false, DeviceCommand.MOW)),
45 entry(263, new DeviceStatus("docked-saving-map", false, DeviceCommand.RETURN)),
46 entry(266, new DeviceStatus("leaving-dock", false, DeviceCommand.MOW)),
47 entry(513, new DeviceStatus("mowing", false, DeviceCommand.MOW)),
48 entry(514, new DeviceStatus("relocalising", false, DeviceCommand.MOW)),
49 entry(515, new DeviceStatus("loading-map", false, DeviceCommand.MOW)),
50 entry(STATE_LEARNING_LAWN, new DeviceStatus("learning-lawn", false, DeviceCommand.MOW)),
51 entry(517, new DeviceStatus("paused", true, DeviceCommand.PAUSE)),
52 entry(518, new DeviceStatus("border-cut", false, DeviceCommand.MOW)),
53 entry(519, new DeviceStatus("idle-in-lawn", true, DeviceCommand.MOW)),
54 entry(523, new DeviceStatus("spotmow", false, DeviceCommand.MOW)),
55 entry(769, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
56 entry(770, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
57 entry(771, new DeviceStatus("returning-to-dock-battery-low", false, DeviceCommand.RETURN)),
58 entry(772, new DeviceStatus("returning-to-dock-calendar-timeslot-ended", false, DeviceCommand.RETURN)),
59 entry(773, new DeviceStatus("returning-to-dock-battery-temp-range", false, DeviceCommand.RETURN)),
60 entry(774, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
61 entry(775, new DeviceStatus("returning-to-dock-lawn-complete", false, DeviceCommand.RETURN)),
62 entry(776, new DeviceStatus("returning-to-dock-relocalising", false, DeviceCommand.RETURN)),
63 entry(1025, new DeviceStatus("diagnostic-mode", false, null)),
64 entry(1026, new DeviceStatus("end-of-life", false, null)),
65 entry(1281, new DeviceStatus("software-update", false, null)),
66 entry(1537, new DeviceStatus("energy-save-mode", true, DeviceCommand.RETURN)),
67 entry(64513, new DeviceStatus("docked", true, DeviceCommand.RETURN)));
69 private String textKey;
71 private boolean isReadyToMow;
73 private @Nullable DeviceCommand associatedCommand;
75 private DeviceStatus(String textKey, boolean isReadyToMow, @Nullable DeviceCommand associatedCommand) {
76 this.textKey = textKey;
77 this.isReadyToMow = isReadyToMow;
78 this.associatedCommand = associatedCommand;
82 * Returns a {@link DeviceStatus} instance describing the status code.
84 * @param code the status code
85 * @return the {@link DeviceStatus} providing additional context for the code
87 public static DeviceStatus fromCode(int code) {
88 DeviceStatus status = STATUS_MAP.get(code);
93 DeviceCommand command = null;
94 switch (code & 0xff00) {
96 command = DeviceCommand.RETURN;
99 command = DeviceCommand.MOW;
102 command = DeviceCommand.RETURN;
106 return new DeviceStatus(String.valueOf(code), false, command);
110 * Returns a localized description for this {@link DeviceStatus}.
112 * @param translationProvider
113 * @return localized status description
115 public String getMessage(BoschIndegoTranslationProvider translationProvider) {
116 String textualState = translationProvider.getText(STATE_PREFIX + textKey);
117 if (textualState == null) {
118 textualState = String.format(translationProvider.getText(STATE_PREFIX + STATE_UNKNOWN, textKey), textKey);
123 public boolean isReadyToMow() {
127 public @Nullable DeviceCommand getAssociatedCommand() {
128 return associatedCommand;