]> git.basschouten.com Git - openhab-addons.git/blob
69756ac2b193e89896db8d7d8481b1b585d2406f
[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.netatmo.internal.api.data;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16 import static org.openhab.core.library.CoreItemFactory.*;
17 import static org.openhab.core.library.unit.MetricPrefix.*;
18
19 import java.net.URI;
20 import java.util.EnumSet;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
26
27 import javax.measure.Unit;
28
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.openhab.core.library.unit.SIUnits;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.types.StateDescriptionFragment;
33 import org.openhab.core.types.StateDescriptionFragmentBuilder;
34 import org.openhab.core.types.util.UnitUtils;
35
36 import com.google.gson.annotations.SerializedName;
37
38 /**
39  * This class holds various definitions and settings provided by the Netatmo
40  * API documentation
41  *
42  * @author GaĆ«l L'hopital - Initial contribution
43  */
44 @NonNullByDefault
45 public class NetatmoConstants {
46     public static class Measure {
47         public final double minValue;
48         public final double maxValue;
49         public final int scale;
50         public final Unit<?> unit;
51
52         private Measure(double minValue, double maxValue, double precision, Unit<?> unit) {
53             this.minValue = minValue;
54             this.maxValue = maxValue;
55             this.unit = unit;
56             String[] splitter = Double.valueOf(precision).toString().split("\\.");
57             if (splitter.length > 1) {
58                 int dec = Integer.parseInt(splitter[1]);
59                 this.scale = dec > 0 ? Integer.toString(dec).length() : 0;
60             } else {
61                 this.scale = 0;
62             }
63         }
64     }
65
66     public static class MeasureChannelDetails {
67         private static final StateDescriptionFragmentBuilder BUILDER = StateDescriptionFragmentBuilder.create();
68         public final URI configURI;
69         public final String itemType;
70         public final StateDescriptionFragment stateDescriptionFragment;
71
72         private MeasureChannelDetails(String measureType, String itemType, String pattern) {
73             this.configURI = URI.create(String.join(":", BINDING_ID, measureType, "config"));
74             this.itemType = itemType;
75             this.stateDescriptionFragment = BUILDER.withReadOnly(true).withPattern(pattern).build();
76         }
77     }
78
79     public enum MeasureClass {
80         INSIDE_TEMPERATURE(0, 50, 0.3, SIUnits.CELSIUS, "temp", "measure", true),
81         OUTSIDE_TEMPERATURE(-40, 65, 0.3, SIUnits.CELSIUS, "temp", "measure", true),
82         HEAT_INDEX(-40, 65, 1, SIUnits.CELSIUS, "", "", false),
83         PRESSURE(260, 1260, 1, HECTO(SIUnits.PASCAL), "pressure", "measure", true),
84         CO2(0, 5000, 50, Units.PARTS_PER_MILLION, "co2", "measure", true),
85         NOISE(35, 120, 1, Units.DECIBEL, "noise", "measure", true),
86         RAIN_QUANTITY(0, 150, 0.1, MILLI(SIUnits.METRE), "sum_rain", "sum_rain", false),
87         RAIN_INTENSITY(0, 150, 0.1, Units.MILLIMETRE_PER_HOUR, "", "", false),
88         WIND_SPEED(0, 160, 1.8, SIUnits.KILOMETRE_PER_HOUR, "", "", false),
89         WIND_ANGLE(0, 360, 5, Units.DEGREE_ANGLE, "", "", false),
90         HUMIDITY(0, 100, 3, Units.PERCENT, "hum", "measure", true);
91
92         public static final EnumSet<MeasureClass> AS_SET = EnumSet.allOf(MeasureClass.class);
93
94         public final Measure measureDefinition;
95         public final String apiDescriptor;
96         public final Map<String, MeasureChannelDetails> channels = new HashMap<>(2);
97
98         MeasureClass(double min, double max, double precision, Unit<?> unit, String apiDescriptor, String confFragment,
99                 boolean canScale) {
100             this.measureDefinition = new Measure(min, max, precision, unit);
101             this.apiDescriptor = apiDescriptor;
102             if (!apiDescriptor.isBlank()) {
103                 String dimension = UnitUtils.getDimensionName(unit);
104
105                 channels.put(String.join("-", apiDescriptor, "measurement"),
106                         new MeasureChannelDetails(confFragment, String.join(":", NUMBER, dimension),
107                                 String.format("%%.%df %s", measureDefinition.scale, UnitUtils.UNIT_PLACEHOLDER)));
108                 if (canScale) {
109                     channels.put(String.join("-", apiDescriptor, GROUP_TIMESTAMP),
110                             new MeasureChannelDetails(GROUP_TIMESTAMP, DATETIME, "%1$tA, %1$td.%1$tm. %1$tH:%1$tM"));
111                 }
112             }
113         }
114     }
115
116     // Netatmo API urls
117     public static final String URL_API = "https://api.netatmo.com/";
118     public static final String URL_APP = "https://app.netatmo.net/";
119     public static final String PATH_OAUTH = "oauth2";
120     public static final String SUB_PATH_TOKEN = "token";
121     public static final String SUB_PATH_AUTHORIZE = "authorize";
122     public static final String PATH_API = "api";
123     public static final String PATH_COMMAND = "command";
124     public static final String PATH_STATE = "setstate";
125     public static final String SUB_PATH_PERSON_AWAY = "setpersonsaway";
126     public static final String SUB_PATH_PERSON_HOME = "setpersonshome";
127     public static final String SUB_PATH_HOMES_DATA = "homesdata";
128     public static final String SUB_PATH_ADDWEBHOOK = "addwebhook";
129     public static final String SUB_PATH_DROPWEBHOOK = "dropwebhook";
130     public static final String SUB_PATH_SETROOMTHERMPOINT = "setroomthermpoint";
131     public static final String SUB_PATH_SETTHERMMODE = "setthermmode";
132     public static final String SUB_PATH_SWITCHSCHEDULE = "switchschedule";
133     public static final String SUB_PATH_GETSTATION = "getstationsdata";
134     public static final String SUB_PATH_GETMEASURE = "getmeasure";
135     public static final String SUB_PATH_HOMESTATUS = "homestatus";
136     public static final String SUB_PATH_HOMECOACH = "gethomecoachsdata";
137     public static final String SUB_PATH_GETEVENTS = "getevents";
138     public static final String SUB_PATH_PING = "ping";
139     public static final String SUB_PATH_CHANGESTATUS = "changestatus";
140     public static final String PARAM_DEVICEID = "device_id";
141     public static final String PARAM_MODULEID = "module_id";
142     public static final String PARAM_HOMEID = "home_id";
143     public static final String PARAM_ROOMID = "room_id";
144     public static final String PARAM_PERSONID = "person_id";
145     public static final String PARAM_SCHEDULEID = "schedule_id";
146     public static final String PARAM_OFFSET = "offset";
147     public static final String PARAM_GATEWAYTYPE = "gateway_types";
148     public static final String PARAM_MODE = "mode";
149     public static final String PARAM_URL = "url";
150     public static final String PARAM_FAVORITES = "get_favorites";
151     public static final String PARAM_STATUS = "status";
152
153     // Autentication process params
154     public static final String PARAM_ERROR = "error";
155
156     // Global variables
157     public static final int THERM_MAX_SETPOINT = 30;
158
159     // Token scopes
160     public static enum Scope {
161         @SerializedName("read_station")
162         READ_STATION,
163         @SerializedName("read_thermostat")
164         READ_THERMOSTAT,
165         @SerializedName("write_thermostat")
166         WRITE_THERMOSTAT,
167         @SerializedName("read_camera")
168         READ_CAMERA,
169         @SerializedName("write_camera")
170         WRITE_CAMERA,
171         @SerializedName("access_camera")
172         ACCESS_CAMERA,
173         @SerializedName("read_presence")
174         READ_PRESENCE,
175         @SerializedName("write_presence")
176         WRITE_PRESENCE,
177         @SerializedName("access_presence")
178         ACCESS_PRESENCE,
179         @SerializedName("read_smokedetector")
180         READ_SMOKEDETECTOR,
181         @SerializedName("read_homecoach")
182         READ_HOMECOACH,
183         @SerializedName("read_doorbell")
184         READ_DOORBELL,
185         @SerializedName("write_doorbell")
186         WRITE_DOORBELL,
187         @SerializedName("access_doorbell")
188         ACCESS_DOORBELL,
189         UNKNOWN;
190     }
191
192     private static final Set<Scope> SMOKE = Set.of(Scope.READ_SMOKEDETECTOR);
193     private static final Set<Scope> WELCOME = Set.of(Scope.READ_CAMERA, Scope.WRITE_CAMERA, Scope.ACCESS_CAMERA);
194     private static final Set<Scope> DOORBELL = Set.of(Scope.READ_DOORBELL, Scope.WRITE_DOORBELL, Scope.ACCESS_DOORBELL);
195     private static final Set<Scope> PRESENCE = Set.of(Scope.READ_PRESENCE, Scope.WRITE_PRESENCE, Scope.ACCESS_PRESENCE);
196
197     // Radio signal quality thresholds
198     static final int[] WIFI_SIGNAL_LEVELS = new int[] { 99, 84, 69, 54 }; // Resp : bad, average, good, full
199     static final int[] RADIO_SIGNAL_LEVELS = new int[] { 90, 80, 70, 60 }; // Resp : low, medium, high, full
200
201     public static enum FeatureArea {
202         AIR_CARE(Scope.READ_HOMECOACH),
203         WEATHER(Scope.READ_STATION),
204         ENERGY(Scope.READ_THERMOSTAT, Scope.WRITE_THERMOSTAT),
205         SECURITY(Stream.of(WELCOME, PRESENCE, SMOKE, DOORBELL).flatMap(Set::stream).toArray(Scope[]::new)),
206         NONE();
207
208         public static final Set<FeatureArea> AS_SET = EnumSet.allOf(FeatureArea.class);
209
210         public static String toScopeString(Set<FeatureArea> featureSet) {
211             return featureSet.stream().map(fa -> fa.scopes).flatMap(Set::stream).map(s -> s.name().toLowerCase())
212                     .collect(Collectors.joining(" "));
213         }
214
215         public final Set<Scope> scopes;
216
217         FeatureArea(Scope... scopes) {
218             this.scopes = Set.of(scopes);
219         }
220     }
221
222     // Thermostat definitions
223     public static enum SetpointMode {
224         @SerializedName("program")
225         PROGRAM("program"),
226         @SerializedName("away")
227         AWAY("away"),
228         @SerializedName("hg")
229         FROST_GUARD("hg"),
230         @SerializedName("manual")
231         MANUAL("manual"),
232         @SerializedName("off")
233         OFF("off"),
234         @SerializedName("max")
235         MAX("max"),
236         @SerializedName("schedule")
237         SCHEDULE("schedule"),
238         HOME("home"),
239         UNKNOWN("");
240
241         public final String apiDescriptor;
242
243         SetpointMode(String descriptor) {
244             this.apiDescriptor = descriptor;
245         }
246     }
247
248     public static enum ThermostatZoneType {
249         @SerializedName("0")
250         DAY("0"),
251         @SerializedName("1")
252         NIGHT("1"),
253         @SerializedName("2")
254         AWAY("2"),
255         @SerializedName("3")
256         FROST_GUARD("3"),
257         @SerializedName("4")
258         CUSTOM("4"),
259         @SerializedName("5")
260         ECO("5"),
261         @SerializedName("8")
262         COMFORT("8"),
263         UNKNOWN("");
264
265         public final String zoneId;
266
267         private ThermostatZoneType(String id) {
268             zoneId = id;
269         }
270     }
271
272     public enum FloodLightMode {
273         @SerializedName("on")
274         ON,
275         @SerializedName("off")
276         OFF,
277         @SerializedName("auto")
278         AUTO,
279         UNKNOWN;
280     }
281
282     public enum EventCategory {
283         @SerializedName("human")
284         HUMAN,
285         @SerializedName("animal")
286         ANIMAL,
287         @SerializedName("vehicle")
288         VEHICLE,
289         UNKNOWN;
290     }
291
292     public enum TrendDescription {
293         @SerializedName("up")
294         UP,
295         @SerializedName("stable")
296         STABLE,
297         @SerializedName("down")
298         DOWN,
299         UNKNOWN;
300     }
301
302     public enum VideoStatus {
303         @SerializedName("recording")
304         RECORDING,
305         @SerializedName("available")
306         AVAILABLE,
307         @SerializedName("deleted")
308         DELETED,
309         UNKNOWN;
310     }
311
312     public enum SdCardStatus {
313         @SerializedName("1")
314         SD_CARD_MISSING,
315         @SerializedName("2")
316         SD_CARD_INSERTED,
317         @SerializedName("3")
318         SD_CARD_FORMATTED,
319         @SerializedName("4")
320         SD_CARD_WORKING,
321         @SerializedName("5")
322         SD_CARD_DEFECTIVE,
323         @SerializedName("6")
324         SD_CARD_INCOMPATIBLE_SPEED,
325         @SerializedName("7")
326         SD_CARD_INSUFFICIENT_SPACE,
327         UNKNOWN;
328     }
329
330     public enum AlimentationStatus {
331         @SerializedName("1")
332         ALIM_INCORRECT_POWER,
333         @SerializedName("2")
334         ALIM_CORRECT_POWER,
335         UNKNOWN;
336     }
337
338     public enum BatteryState {
339         @SerializedName("full")
340         FULL(100),
341         @SerializedName("high")
342         HIGH(80),
343         @SerializedName("medium")
344         MEDIUM(50),
345         @SerializedName("low")
346         LOW(15),
347         UNKNOWN(-1);
348
349         public final int level;
350
351         BatteryState(int i) {
352             this.level = i;
353         }
354     }
355
356     public enum ServiceError {
357         @SerializedName("99")
358         UNKNOWN,
359         @SerializedName("-2")
360         UNKNOWN_ERROR_IN_OAUTH,
361         @SerializedName("-1")
362         GRANT_IS_INVALID,
363         @SerializedName("1")
364         ACCESS_TOKEN_MISSING,
365         @SerializedName("2")
366         INVALID_TOKEN_MISSING,
367         @SerializedName("3")
368         ACCESS_TOKEN_EXPIRED,
369         @SerializedName("5")
370         APPLICATION_DEACTIVATED,
371         @SerializedName("7")
372         NOTHING_TO_MODIFY,
373         @SerializedName("9")
374         DEVICE_NOT_FOUND,
375         @SerializedName("10")
376         MISSING_ARGUMENTS,
377         @SerializedName("13")
378         OPERATION_FORBIDDEN,
379         @SerializedName("19")
380         IP_NOT_FOUND,
381         @SerializedName("21")
382         INVALID_ARGUMENT,
383         @SerializedName("22")
384         APPLICATION_NOT_FOUND,
385         @SerializedName("23")
386         USER_NOT_FOUND,
387         @SerializedName("25")
388         INVALID_DATE,
389         @SerializedName("26")
390         MAXIMUM_USAGE_REACHED,
391         @SerializedName("30")
392         INVALID_REFRESH_TOKEN,
393         @SerializedName("31")
394         METHOD_NOT_FOUND,
395         @SerializedName("35")
396         UNABLE_TO_EXECUTE,
397         @SerializedName("36")
398         PROHIBITED_STRING,
399         @SerializedName("37")
400         NO_MORE_SPACE_AVAILABLE_ON_THE_CAMERA,
401         @SerializedName("40")
402         JSON_GIVEN_HAS_AN_INVALID_ENCODING,
403         @SerializedName("41")
404         DEVICE_IS_UNREACHABLE;
405     }
406 }