]> git.basschouten.com Git - openhab-addons.git/blob
30ab3722ef30dccb05659c4fc50cb6e91406de76
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.irobot.internal.dto;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
19 import com.google.gson.JsonElement;
20
21 /**
22  * iRobot MQTT protocol messages
23  *
24  * @author Pavel Fedin - Initial contribution
25  * @author Florian Binder - Added CleanRoomsRequest
26  *
27  */
28 public class MQTTProtocol {
29     public interface Request {
30         public String getTopic();
31     }
32
33     public static class CleanRoomsRequest extends CommandRequest {
34         public int ordered;
35         public String pmap_id;
36         public List<Region> regions;
37
38         public CleanRoomsRequest(String cmd, String mapId, String[] regions) {
39             super(cmd);
40             ordered = 1;
41             pmap_id = mapId;
42             this.regions = Arrays.stream(regions).map(i -> new Region(i)).collect(Collectors.toList());
43         }
44
45         public static class Region {
46             public String region_id;
47             public String type;
48
49             public Region(String id) {
50                 this.region_id = id;
51                 this.type = "rid";
52             }
53         }
54     }
55
56     public static class CommandRequest implements Request {
57         public String command;
58         public long time;
59         public String initiator;
60
61         public CommandRequest(String cmd) {
62             command = cmd;
63             time = System.currentTimeMillis() / 1000;
64             initiator = "openhab";
65         }
66
67         @Override
68         public String getTopic() {
69             return "cmd";
70         }
71     }
72
73     public static class DeltaRequest implements Request {
74         public StateValue state;
75
76         public DeltaRequest(StateValue state) {
77             this.state = state;
78         }
79
80         @Override
81         public String getTopic() {
82             return "delta";
83         }
84     }
85
86     public static class CleanMissionStatus {
87         public String cycle;
88         public String phase;
89         public String initiator;
90         public int error;
91     }
92
93     public static class BinStatus {
94         public boolean present;
95         public boolean full;
96     }
97
98     public static class SignalStrength {
99         public int rssi;
100         public int snr;
101     }
102
103     public static class Schedule {
104         public String[] cycle;
105         public int[] h;
106         public int[] m;
107
108         public static final int NUM_WEEK_DAYS = 7;
109
110         public Schedule(int cycles_bitmask) {
111             cycle = new String[NUM_WEEK_DAYS];
112             for (int i = 0; i < NUM_WEEK_DAYS; i++) {
113                 enableCycle(i, (cycles_bitmask & (1 << i)) != 0);
114             }
115         }
116
117         public Schedule(String[] cycle) {
118             this.cycle = cycle;
119         }
120
121         public boolean cycleEnabled(int i) {
122             return cycle[i].equals("start");
123         }
124
125         public void enableCycle(int i, boolean enable) {
126             cycle[i] = enable ? "start" : "none";
127         }
128     }
129
130     public static class StateValue {
131         // Just some common type, nothing to do here
132         protected StateValue() {
133         }
134     }
135
136     public static class OpenOnly extends StateValue {
137         public boolean openOnly;
138
139         public OpenOnly(boolean openOnly) {
140             this.openOnly = openOnly;
141         }
142     }
143
144     public static class BinPause extends StateValue {
145         public boolean binPause;
146
147         public BinPause(boolean binPause) {
148             this.binPause = binPause;
149         }
150     }
151
152     public static class PowerBoost extends StateValue {
153         public boolean carpetBoost;
154         public boolean vacHigh;
155
156         public PowerBoost(boolean carpetBoost, boolean vacHigh) {
157             this.carpetBoost = carpetBoost;
158             this.vacHigh = vacHigh;
159         }
160     }
161
162     public static class CleanPasses extends StateValue {
163         public boolean noAutoPasses;
164         public boolean twoPass;
165
166         public CleanPasses(boolean noAutoPasses, boolean twoPass) {
167             this.noAutoPasses = noAutoPasses;
168             this.twoPass = twoPass;
169         }
170     }
171
172     public static class CleanSchedule extends StateValue {
173         public Schedule cleanSchedule;
174
175         public CleanSchedule(Schedule schedule) {
176             cleanSchedule = schedule;
177         }
178     }
179
180     // "reported" messages never contain the full state, only a part.
181     // Therefore all the fields in this class are nullable
182     public static class GenericState extends StateValue {
183         // "cleanMissionStatus":{"cycle":"clean","phase":"hmUsrDock","expireM":0,"rechrgM":0,"error":0,"notReady":0,"mssnM":1,"sqft":7,"initiator":"rmtApp","nMssn":39}
184         public CleanMissionStatus cleanMissionStatus;
185         // "batPct":100
186         public Integer batPct;
187         // "bin":{"present":true,"full":false}
188         public BinStatus bin;
189         // "signal":{"rssi":-55,"snr":33}
190         public SignalStrength signal;
191         // "cleanSchedule":{"cycle":["none","start","start","start","start","none","none"],"h":[9,12,12,12,12,12,9],"m":[0,0,0,0,0,0,0]}
192         public Schedule cleanSchedule;
193         // "openOnly":false
194         public Boolean openOnly;
195         // "binPause":true
196         public Boolean binPause;
197         // "carpetBoost":true
198         public Boolean carpetBoost;
199         // "vacHigh":false
200         public Boolean vacHigh;
201         // "noAutoPasses":true
202         public Boolean noAutoPasses;
203         // "twoPass":true
204         public Boolean twoPass;
205         // "softwareVer":"v2.4.6-3"
206         public String softwareVer;
207         // "navSwVer":"01.12.01#1"
208         public String navSwVer;
209         // "wifiSwVer":"20992"
210         public String wifiSwVer;
211         // "mobilityVer":"5806"
212         public String mobilityVer;
213         // "bootloaderVer":"4042"
214         public String bootloaderVer;
215         // "umiVer":"6",
216         public String umiVer;
217         // "lastCommand":
218         // {"command":"start","initiator":"localApp","time":1610283995,"ordered":1,"pmap_id":"AAABBBCCCSDDDEEEFFF","regions":[{"region_id":"6","type":"rid"}]}
219         public JsonElement lastCommand;
220     }
221
222     // Data comes as JSON string: {"state":{"reported":<Actual content here>}}
223     // or: {"state":{"desired":<Some content here>}}
224     // Of the second form i've so far observed only: {"state":{"desired":{"echo":null}}}
225     // I don't know what it is, so let's ignore it.
226     public static class ReportedState {
227         public GenericState reported;
228     }
229
230     public static class StateMessage {
231         public ReportedState state;
232     }
233 };