]> git.basschouten.com Git - openhab-addons.git/blob
0c8088bdd096cb19f35be7cb8ef10dfadb9f0b65
[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.lametrictime.internal.api.impl;
14
15 import static org.openhab.binding.lametrictime.internal.api.dto.ApiValue.raw;
16
17 import java.util.Arrays;
18
19 import javax.ws.rs.client.ClientBuilder;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.lametrictime.internal.api.Configuration;
24 import org.openhab.binding.lametrictime.internal.api.LaMetricTime;
25 import org.openhab.binding.lametrictime.internal.api.cloud.CloudConfiguration;
26 import org.openhab.binding.lametrictime.internal.api.cloud.LaMetricTimeCloud;
27 import org.openhab.binding.lametrictime.internal.api.dto.CoreAction;
28 import org.openhab.binding.lametrictime.internal.api.dto.CoreApplication;
29 import org.openhab.binding.lametrictime.internal.api.dto.CoreApps;
30 import org.openhab.binding.lametrictime.internal.api.dto.Icon;
31 import org.openhab.binding.lametrictime.internal.api.dto.Icons;
32 import org.openhab.binding.lametrictime.internal.api.dto.enums.BrightnessMode;
33 import org.openhab.binding.lametrictime.internal.api.dto.enums.Priority;
34 import org.openhab.binding.lametrictime.internal.api.dto.enums.Sound;
35 import org.openhab.binding.lametrictime.internal.api.local.ApplicationActionException;
36 import org.openhab.binding.lametrictime.internal.api.local.ApplicationActivationException;
37 import org.openhab.binding.lametrictime.internal.api.local.ApplicationNotFoundException;
38 import org.openhab.binding.lametrictime.internal.api.local.LaMetricTimeLocal;
39 import org.openhab.binding.lametrictime.internal.api.local.LocalConfiguration;
40 import org.openhab.binding.lametrictime.internal.api.local.NotificationCreationException;
41 import org.openhab.binding.lametrictime.internal.api.local.UpdateException;
42 import org.openhab.binding.lametrictime.internal.api.local.dto.Application;
43 import org.openhab.binding.lametrictime.internal.api.local.dto.Audio;
44 import org.openhab.binding.lametrictime.internal.api.local.dto.Bluetooth;
45 import org.openhab.binding.lametrictime.internal.api.local.dto.Display;
46 import org.openhab.binding.lametrictime.internal.api.local.dto.Frame;
47 import org.openhab.binding.lametrictime.internal.api.local.dto.Notification;
48 import org.openhab.binding.lametrictime.internal.api.local.dto.NotificationModel;
49 import org.openhab.binding.lametrictime.internal.api.local.dto.UpdateAction;
50 import org.openhab.binding.lametrictime.internal.api.local.dto.Widget;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * Implementation class for LaMetricTime interface.
56  *
57  * @author Gregory Moyer - Initial contribution
58  */
59 @NonNullByDefault
60 public class LaMetricTimeImpl implements LaMetricTime {
61
62     private final Logger logger = LoggerFactory.getLogger(LaMetricTimeImpl.class);
63
64     private final LaMetricTimeLocal local;
65     private final LaMetricTimeCloud cloud;
66
67     private final Object muteLock = new Object();
68     @Nullable
69     private Integer volumeSaveState;
70
71     public LaMetricTimeImpl(Configuration config) {
72         this(config.getLocalConfig(), config.getCloudConfig());
73     }
74
75     public LaMetricTimeImpl(Configuration config, ClientBuilder clientBuilder) {
76         this(config.getLocalConfig(), config.getCloudConfig(), clientBuilder);
77     }
78
79     public LaMetricTimeImpl(LocalConfiguration localConfig, CloudConfiguration cloudConfig) {
80         this.local = LaMetricTimeLocal.create(localConfig);
81         this.cloud = LaMetricTimeCloud.create(cloudConfig);
82     }
83
84     public LaMetricTimeImpl(LocalConfiguration localConfig, CloudConfiguration cloudConfig,
85             ClientBuilder clientBuilder) {
86         this.local = LaMetricTimeLocal.create(localConfig, clientBuilder);
87         this.cloud = LaMetricTimeCloud.create(cloudConfig, clientBuilder);
88     }
89
90     @Override
91     public String getVersion() {
92         return local.getApi().getApiVersion();
93     }
94
95     @Override
96     public String notifyInfo(@Nullable String message) throws NotificationCreationException {
97         return notify(message, Priority.INFO, Icons.key("i1248"), Sound.NOTIFICATION, 1, 1);
98     }
99
100     @Override
101     public String notifyWarning(@Nullable String message) throws NotificationCreationException {
102         return notify(message, Priority.WARNING, Icons.key("a2098"), Sound.NOTIFICATION2, 2, 2);
103     }
104
105     @Override
106     public String notifyCritical(@Nullable String message) throws NotificationCreationException {
107         return notify(message, Priority.CRITICAL, Icons.key("a4787"), Sound.ALARM1, 0, 0);
108     }
109
110     @Override
111     public String notify(@Nullable String message, @Nullable Priority priority, @Nullable Icon icon,
112             @Nullable Sound sound, int messageRepeat, int soundRepeat) throws NotificationCreationException {
113         // @formatter:off
114         NotificationModel model = new NotificationModel()
115                                       .withCycles(messageRepeat)
116                                       .withFrames(Arrays.asList(new Frame().withText(message)
117                                                                            .withIcon(raw(icon))));
118         if (sound != null)
119         {
120             model.setSound(new org.openhab.binding.lametrictime.internal.api.local.dto.Sound()
121                                .withCategory(raw(sound.getCategory()))
122                                .withId(raw(sound))
123                                .withRepeat(soundRepeat));
124         }
125         // @formatter:on
126
127         Notification notification = new Notification().withPriority(raw(priority)).withModel(model);
128         return local.createNotification(notification);
129     }
130
131     @Override
132     public @Nullable Application getClock() {
133         return getApplication(CoreApps.clock());
134     }
135
136     @Override
137     public @Nullable Application getCountdown() {
138         return getApplication(CoreApps.countdown());
139     }
140
141     @Override
142     public @Nullable Application getRadio() {
143         return getApplication(CoreApps.radio());
144     }
145
146     @Override
147     public @Nullable Application getStopwatch() {
148         return getApplication(CoreApps.stopwatch());
149     }
150
151     @Override
152     public @Nullable Application getWeather() {
153         return getApplication(CoreApps.weather());
154     }
155
156     @Override
157     public @Nullable Application getApplication(@Nullable CoreApplication coreApp) {
158         try {
159             return getLocalApi().getApplication(coreApp.getPackageName());
160         } catch (ApplicationNotFoundException e) {
161             // core apps should never throw errors
162             logger.error("Failed to retrieve core application: {}", coreApp.getPackageName(), e);
163             return null;
164         }
165     }
166
167     @Override
168     public @Nullable Application getApplication(@Nullable String name) throws ApplicationNotFoundException {
169         if (name != null) {
170             return getLocalApi().getApplication(name);
171         } else {
172             return null;
173         }
174     }
175
176     @Override
177     public void activateApplication(@Nullable CoreApplication coreApp) {
178         try {
179             activateApplication(getApplication(coreApp));
180         } catch (ApplicationActivationException e) {
181             // core apps should never throw errors
182             logger.error("Failed to activate core application: {}", coreApp.getPackageName(), e);
183         }
184     }
185
186     @Override
187     public void activateApplication(@Nullable Application app) throws ApplicationActivationException {
188         getLocalApi().activateApplication(app.getPackageName(), getFirstWidgetId(app));
189     }
190
191     @Override
192     public void activateWidget(@Nullable Widget widget) throws ApplicationActivationException {
193         getLocalApi().activateApplication(widget.getPackageName(), widget.getId());
194     }
195
196     @Override
197     public void doAction(@Nullable CoreAction coreAction) {
198         try {
199             doAction(getApplication(coreAction.getApp()), coreAction.getAction());
200         } catch (ApplicationActionException e) {
201             // core apps should never throw errors
202             logger.error("Failed to execute weather forecast action", e);
203         }
204     }
205
206     @Override
207     public void doAction(@Nullable Application app, @Nullable UpdateAction action) throws ApplicationActionException {
208         getLocalApi().doAction(app.getPackageName(), getFirstWidgetId(app), action);
209     }
210
211     @Override
212     public void doAction(@Nullable Widget widget, @Nullable CoreAction coreAction) throws ApplicationActionException {
213         doAction(widget, coreAction.getAction());
214     }
215
216     @Override
217     public void doAction(@Nullable Widget widget, @Nullable UpdateAction action) throws ApplicationActionException {
218         getLocalApi().doAction(widget.getPackageName(), widget.getId(), action);
219     }
220
221     protected String getFirstWidgetId(Application app) {
222         return app.getWidgets().firstKey();
223     }
224
225     @Override
226     public Display setBrightness(int brightness) throws UpdateException {
227         return local
228                 .updateDisplay(new Display().withBrightness(brightness).withBrightnessMode(raw(BrightnessMode.MANUAL)));
229     }
230
231     @Override
232     public Display setBrightnessMode(@Nullable BrightnessMode mode) throws UpdateException {
233         return local.updateDisplay(new Display().withBrightnessMode(raw(mode)));
234     }
235
236     @Override
237     public Audio setVolume(int volume) throws UpdateException {
238         return local.updateAudio(new Audio().withVolume(volume));
239     }
240
241     @Override
242     public Audio mute() throws UpdateException {
243         synchronized (muteLock) {
244             Audio audio = local.getAudio();
245             if (audio.getVolume() == 0) {
246                 return audio;
247             }
248
249             volumeSaveState = audio.getVolume();
250             return setVolume(0);
251         }
252     }
253
254     @Override
255     public Audio unmute() throws UpdateException {
256         synchronized (muteLock) {
257             if (volumeSaveState == null) {
258                 Audio audio = local.getAudio();
259                 if (audio.getVolume() == 0) {
260                     return setVolume(50);
261                 } else {
262                     return audio;
263                 }
264             }
265
266             Audio audio = setVolume(volumeSaveState);
267             volumeSaveState = null;
268             return audio;
269         }
270     }
271
272     @Override
273     public Bluetooth setBluetoothActive(boolean active) throws UpdateException {
274         return local.updateBluetooth(new Bluetooth().withActive(active));
275     }
276
277     @Override
278     public Bluetooth setBluetoothName(@Nullable String name) throws UpdateException {
279         return local.updateBluetooth(new Bluetooth().withName(name));
280     }
281
282     @Override
283     public LaMetricTimeLocal getLocalApi() {
284         return local;
285     }
286
287     @Override
288     public LaMetricTimeCloud getCloudApi() {
289         return cloud;
290     }
291 }