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