]> git.basschouten.com Git - openhab-addons.git/blob
2000ae8b51b63dcb140bf0c253b113e60d3cea64
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.boschindego.internal;
14
15 import static java.util.Map.entry;
16
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.boschindego.internal.dto.DeviceCommand;
22
23 /**
24  * {@link DeviceStatus} describes status codes from the device with corresponding
25  * ready state and associated command.
26  * 
27  * @author Jacob Laursen - Initial contribution
28  */
29 @NonNullByDefault
30 public class DeviceStatus {
31
32     private final static String STATE_PREFIX = "indego.state.";
33     private final static String STATE_UNKNOWN = "unknown";
34
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(513, new DeviceStatus("mowing", false, DeviceCommand.MOW)),
45             entry(514, new DeviceStatus("relocalising", false, DeviceCommand.MOW)),
46             entry(515, new DeviceStatus("loading-map", false, DeviceCommand.MOW)),
47             entry(516, new DeviceStatus("learning-lawn", false, DeviceCommand.MOW)),
48             entry(517, new DeviceStatus("paused", true, DeviceCommand.PAUSE)),
49             entry(518, new DeviceStatus("border-cut", false, DeviceCommand.MOW)),
50             entry(519, new DeviceStatus("idle-in-lawn", true, DeviceCommand.MOW)),
51             entry(769, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
52             entry(770, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
53             entry(771, new DeviceStatus("returning-to-dock-battery-low", false, DeviceCommand.RETURN)),
54             entry(772, new DeviceStatus("returning-to-dock-calendar-timeslot-ended", false, DeviceCommand.RETURN)),
55             entry(773, new DeviceStatus("returning-to-dock-battery-temp-range", false, DeviceCommand.RETURN)),
56             entry(774, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
57             entry(775, new DeviceStatus("returning-to-dock-lawn-complete", false, DeviceCommand.RETURN)),
58             entry(776, new DeviceStatus("returning-to-dock-relocalising", false, DeviceCommand.RETURN)),
59             entry(1025, new DeviceStatus("diagnostic-mode", false, null)),
60             entry(1026, new DeviceStatus("end-of-life", false, null)),
61             entry(1281, new DeviceStatus("software-update", false, null)),
62             entry(64513, new DeviceStatus("docked", true, DeviceCommand.RETURN)));
63
64     private String textKey;
65
66     private boolean isReadyToMow;
67
68     private @Nullable DeviceCommand associatedCommand;
69
70     private DeviceStatus(String textKey, boolean isReadyToMow, @Nullable DeviceCommand associatedCommand) {
71         this.textKey = textKey;
72         this.isReadyToMow = isReadyToMow;
73         this.associatedCommand = associatedCommand;
74     }
75
76     /**
77      * Returns a {@link DeviceStatus} instance describing the status code.
78      * 
79      * @param code the status code
80      * @return the {@link DeviceStatus} providing additional context for the code
81      */
82     public static DeviceStatus fromCode(int code) {
83         DeviceStatus status = STATUS_MAP.get(code);
84         if (status != null) {
85             return status;
86         }
87
88         DeviceCommand command = null;
89         switch (code & 0xff00) {
90             case 0x100:
91                 command = DeviceCommand.RETURN;
92                 break;
93             case 0x200:
94                 command = DeviceCommand.MOW;
95                 break;
96             case 0x300:
97                 command = DeviceCommand.RETURN;
98                 break;
99         }
100
101         return new DeviceStatus(String.valueOf(code), false, command);
102     }
103
104     /**
105      * Returns a localized description for this {@link DeviceStatus}.
106      * 
107      * @param translationProvider
108      * @return localized status description
109      */
110     public String getMessage(BoschIndegoTranslationProvider translationProvider) {
111         String textualState = translationProvider.getText(STATE_PREFIX + textKey);
112         if (textualState == null) {
113             textualState = String.format(translationProvider.getText(STATE_PREFIX + STATE_UNKNOWN, textKey), textKey);
114         }
115         return textualState;
116     }
117
118     public boolean isReadyToMow() {
119         return isReadyToMow;
120     }
121
122     public @Nullable DeviceCommand getAssociatedCommand() {
123         return associatedCommand;
124     }
125 }