]> git.basschouten.com Git - openhab-addons.git/blob
b4a85e58d1c8c157fcbc6ed92013100b3df3b5b8
[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 static final Map<Integer, DeviceStatus> STATUS_MAP = Map.ofEntries(
33             entry(0, new DeviceStatus("Reading status", false, DeviceCommand.RETURN)),
34             entry(257, new DeviceStatus("Charging", false, DeviceCommand.RETURN)),
35             entry(258, new DeviceStatus("Docked", true, DeviceCommand.RETURN)),
36             entry(259, new DeviceStatus("Docked - Software update", false, DeviceCommand.RETURN)),
37             entry(260, new DeviceStatus("Docked", true, DeviceCommand.RETURN)),
38             entry(261, new DeviceStatus("Docked", true, DeviceCommand.RETURN)),
39             entry(262, new DeviceStatus("Docked - Loading map", false, DeviceCommand.MOW)),
40             entry(263, new DeviceStatus("Docked - Saving map", false, DeviceCommand.RETURN)),
41             entry(513, new DeviceStatus("Mowing", false, DeviceCommand.MOW)),
42             entry(514, new DeviceStatus("Relocalising", false, DeviceCommand.MOW)),
43             entry(515, new DeviceStatus("Loading map", false, DeviceCommand.MOW)),
44             entry(516, new DeviceStatus("Learning lawn", false, DeviceCommand.MOW)),
45             entry(517, new DeviceStatus("Paused", true, DeviceCommand.PAUSE)),
46             entry(518, new DeviceStatus("Border cut", false, DeviceCommand.MOW)),
47             entry(519, new DeviceStatus("Idle in lawn", true, DeviceCommand.MOW)),
48             entry(769, new DeviceStatus("Returning to dock", false, DeviceCommand.RETURN)),
49             entry(770, new DeviceStatus("Returning to dock", false, DeviceCommand.RETURN)),
50             entry(771, new DeviceStatus("Returning to dock - Battery low", false, DeviceCommand.RETURN)),
51             entry(772, new DeviceStatus("Returning to dock - Calendar timeslot ended", false, DeviceCommand.RETURN)),
52             entry(773, new DeviceStatus("Returning to dock - Battery temp range", false, DeviceCommand.RETURN)),
53             entry(774, new DeviceStatus("Returning to dock", false, DeviceCommand.RETURN)),
54             entry(775, new DeviceStatus("Returning to dock - Lawn complete", false, DeviceCommand.RETURN)),
55             entry(776, new DeviceStatus("Returning to dock - Relocalising", false, DeviceCommand.RETURN)),
56             entry(1025, new DeviceStatus("Diagnostic mode", false, null)),
57             entry(1026, new DeviceStatus("End of life", false, null)),
58             entry(1281, new DeviceStatus("Software update", false, null)),
59             entry(64513, new DeviceStatus("Docked", true, DeviceCommand.RETURN)));
60
61     private String message;
62
63     private boolean isReadyToMow;
64
65     private @Nullable DeviceCommand associatedCommand;
66
67     private DeviceStatus(String message, boolean isReadyToMow, @Nullable DeviceCommand associatedCommand) {
68         this.message = message;
69         this.isReadyToMow = isReadyToMow;
70         this.associatedCommand = associatedCommand;
71     }
72
73     /**
74      * Returns a {@link DeviceStatus} instance describing the status code.
75      * 
76      * @param code the status code
77      * @return the {@link DeviceStatus} providing additional context for the code
78      */
79     public static DeviceStatus fromCode(int code) {
80         DeviceStatus status = STATUS_MAP.get(code);
81         if (status != null) {
82             return status;
83         }
84
85         DeviceCommand command = null;
86         switch (code & 0xff00) {
87             case 0x100:
88                 command = DeviceCommand.RETURN;
89                 break;
90             case 0x200:
91                 command = DeviceCommand.MOW;
92                 break;
93             case 0x300:
94                 command = DeviceCommand.RETURN;
95                 break;
96         }
97
98         return new DeviceStatus(String.format("Unknown status code %d", code), false, command);
99     }
100
101     public String getMessage() {
102         return message;
103     }
104
105     public boolean isReadyToMow() {
106         return isReadyToMow;
107     }
108
109     public @Nullable DeviceCommand getAssociatedCommand() {
110         return associatedCommand;
111     }
112 }