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 private final static String STATE_PREFIX = "indego.state.";
33 private final static String STATE_UNKNOWN = "unknown";
35 private static final Map<Integer, DeviceStatus> STATUS_MAP = Map.ofEntries(
36 entry(0, new DeviceStatus("reading-status", false, DeviceCommand.RETURN)),
37 entry(257, new DeviceStatus("charging", false, DeviceCommand.RETURN)),
38 entry(258, new DeviceStatus("docked", true, DeviceCommand.RETURN)),
39 entry(259, new DeviceStatus("docked-software-update", false, DeviceCommand.RETURN)),
40 entry(260, new DeviceStatus("docked", true, DeviceCommand.RETURN)),
41 entry(261, new DeviceStatus("docked", true, DeviceCommand.RETURN)),
42 entry(262, new DeviceStatus("docked-loading-map", false, DeviceCommand.MOW)),
43 entry(263, new DeviceStatus("docked-saving-map", false, DeviceCommand.RETURN)),
44 entry(266, new DeviceStatus("leaving-dock", false, DeviceCommand.MOW)),
45 entry(513, new DeviceStatus("mowing", false, DeviceCommand.MOW)),
46 entry(514, new DeviceStatus("relocalising", false, DeviceCommand.MOW)),
47 entry(515, new DeviceStatus("loading-map", false, DeviceCommand.MOW)),
48 entry(516, new DeviceStatus("learning-lawn", false, DeviceCommand.MOW)),
49 entry(517, new DeviceStatus("paused", true, DeviceCommand.PAUSE)),
50 entry(518, new DeviceStatus("border-cut", false, DeviceCommand.MOW)),
51 entry(519, new DeviceStatus("idle-in-lawn", true, DeviceCommand.MOW)),
52 entry(523, new DeviceStatus("spotmow", false, DeviceCommand.MOW)),
53 entry(769, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
54 entry(770, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
55 entry(771, new DeviceStatus("returning-to-dock-battery-low", false, DeviceCommand.RETURN)),
56 entry(772, new DeviceStatus("returning-to-dock-calendar-timeslot-ended", false, DeviceCommand.RETURN)),
57 entry(773, new DeviceStatus("returning-to-dock-battery-temp-range", false, DeviceCommand.RETURN)),
58 entry(774, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
59 entry(775, new DeviceStatus("returning-to-dock-lawn-complete", false, DeviceCommand.RETURN)),
60 entry(776, new DeviceStatus("returning-to-dock-relocalising", false, DeviceCommand.RETURN)),
61 entry(1025, new DeviceStatus("diagnostic-mode", false, null)),
62 entry(1026, new DeviceStatus("end-of-life", false, null)),
63 entry(1281, new DeviceStatus("software-update", false, null)),
64 entry(1537, new DeviceStatus("energy-save-mode", true, DeviceCommand.RETURN)),
65 entry(64513, new DeviceStatus("docked", true, DeviceCommand.RETURN)));
67 private String textKey;
69 private boolean isReadyToMow;
71 private @Nullable DeviceCommand associatedCommand;
73 private DeviceStatus(String textKey, boolean isReadyToMow, @Nullable DeviceCommand associatedCommand) {
74 this.textKey = textKey;
75 this.isReadyToMow = isReadyToMow;
76 this.associatedCommand = associatedCommand;
80 * Returns a {@link DeviceStatus} instance describing the status code.
82 * @param code the status code
83 * @return the {@link DeviceStatus} providing additional context for the code
85 public static DeviceStatus fromCode(int code) {
86 DeviceStatus status = STATUS_MAP.get(code);
91 DeviceCommand command = null;
92 switch (code & 0xff00) {
94 command = DeviceCommand.RETURN;
97 command = DeviceCommand.MOW;
100 command = DeviceCommand.RETURN;
104 return new DeviceStatus(String.valueOf(code), false, command);
108 * Returns a localized description for this {@link DeviceStatus}.
110 * @param translationProvider
111 * @return localized status description
113 public String getMessage(BoschIndegoTranslationProvider translationProvider) {
114 String textualState = translationProvider.getText(STATE_PREFIX + textKey);
115 if (textualState == null) {
116 textualState = String.format(translationProvider.getText(STATE_PREFIX + STATE_UNKNOWN, textKey), textKey);
121 public boolean isReadyToMow() {
125 public @Nullable DeviceCommand getAssociatedCommand() {
126 return associatedCommand;