]> git.basschouten.com Git - openhab-addons.git/blob
e6008e4f9e57642572bcc7a6fb4dec053cb8220c
[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     public static final int STATE_LEARNING_LAWN = 516;
33
34     private final static String STATE_PREFIX = "indego.state.";
35     private final static String STATE_UNKNOWN = "unknown";
36
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)));
68
69     private String textKey;
70
71     private boolean isReadyToMow;
72
73     private @Nullable DeviceCommand associatedCommand;
74
75     private DeviceStatus(String textKey, boolean isReadyToMow, @Nullable DeviceCommand associatedCommand) {
76         this.textKey = textKey;
77         this.isReadyToMow = isReadyToMow;
78         this.associatedCommand = associatedCommand;
79     }
80
81     /**
82      * Returns a {@link DeviceStatus} instance describing the status code.
83      * 
84      * @param code the status code
85      * @return the {@link DeviceStatus} providing additional context for the code
86      */
87     public static DeviceStatus fromCode(int code) {
88         DeviceStatus status = STATUS_MAP.get(code);
89         if (status != null) {
90             return status;
91         }
92
93         DeviceCommand command = null;
94         switch (code & 0xff00) {
95             case 0x100:
96                 command = DeviceCommand.RETURN;
97                 break;
98             case 0x200:
99                 command = DeviceCommand.MOW;
100                 break;
101             case 0x300:
102                 command = DeviceCommand.RETURN;
103                 break;
104         }
105
106         return new DeviceStatus(String.valueOf(code), false, command);
107     }
108
109     /**
110      * Returns a localized description for this {@link DeviceStatus}.
111      * 
112      * @param translationProvider
113      * @return localized status description
114      */
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);
119         }
120         return textualState;
121     }
122
123     public boolean isReadyToMow() {
124         return isReadyToMow;
125     }
126
127     public @Nullable DeviceCommand getAssociatedCommand() {
128         return associatedCommand;
129     }
130 }