]> git.basschouten.com Git - openhab-addons.git/blob
7adf332e9dce86f7b1ed89ddb5371efdaa414c63
[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(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(769, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
53             entry(770, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
54             entry(771, new DeviceStatus("returning-to-dock-battery-low", false, DeviceCommand.RETURN)),
55             entry(772, new DeviceStatus("returning-to-dock-calendar-timeslot-ended", false, DeviceCommand.RETURN)),
56             entry(773, new DeviceStatus("returning-to-dock-battery-temp-range", false, DeviceCommand.RETURN)),
57             entry(774, new DeviceStatus("returning-to-dock", false, DeviceCommand.RETURN)),
58             entry(775, new DeviceStatus("returning-to-dock-lawn-complete", false, DeviceCommand.RETURN)),
59             entry(776, new DeviceStatus("returning-to-dock-relocalising", false, DeviceCommand.RETURN)),
60             entry(1025, new DeviceStatus("diagnostic-mode", false, null)),
61             entry(1026, new DeviceStatus("end-of-life", false, null)),
62             entry(1281, new DeviceStatus("software-update", false, null)),
63             entry(1537, new DeviceStatus("energy-save-mode", true, DeviceCommand.RETURN)),
64             entry(64513, new DeviceStatus("docked", true, DeviceCommand.RETURN)));
65
66     private String textKey;
67
68     private boolean isReadyToMow;
69
70     private @Nullable DeviceCommand associatedCommand;
71
72     private DeviceStatus(String textKey, boolean isReadyToMow, @Nullable DeviceCommand associatedCommand) {
73         this.textKey = textKey;
74         this.isReadyToMow = isReadyToMow;
75         this.associatedCommand = associatedCommand;
76     }
77
78     /**
79      * Returns a {@link DeviceStatus} instance describing the status code.
80      * 
81      * @param code the status code
82      * @return the {@link DeviceStatus} providing additional context for the code
83      */
84     public static DeviceStatus fromCode(int code) {
85         DeviceStatus status = STATUS_MAP.get(code);
86         if (status != null) {
87             return status;
88         }
89
90         DeviceCommand command = null;
91         switch (code & 0xff00) {
92             case 0x100:
93                 command = DeviceCommand.RETURN;
94                 break;
95             case 0x200:
96                 command = DeviceCommand.MOW;
97                 break;
98             case 0x300:
99                 command = DeviceCommand.RETURN;
100                 break;
101         }
102
103         return new DeviceStatus(String.valueOf(code), false, command);
104     }
105
106     /**
107      * Returns a localized description for this {@link DeviceStatus}.
108      * 
109      * @param translationProvider
110      * @return localized status description
111      */
112     public String getMessage(BoschIndegoTranslationProvider translationProvider) {
113         String textualState = translationProvider.getText(STATE_PREFIX + textKey);
114         if (textualState == null) {
115             textualState = String.format(translationProvider.getText(STATE_PREFIX + STATE_UNKNOWN, textKey), textKey);
116         }
117         return textualState;
118     }
119
120     public boolean isReadyToMow() {
121         return isReadyToMow;
122     }
123
124     public @Nullable DeviceCommand getAssociatedCommand() {
125         return associatedCommand;
126     }
127 }