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