]> git.basschouten.com Git - openhab-addons.git/blob
cdf7f5ce7f33b9652699a81b37aad47c1a796430
[openhab-addons.git] /
1 /**
2  * Copyright 2017-2018 Gregory Moyer and contributors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openhab.binding.lametrictime.api.impl;
17
18 import static org.openhab.binding.lametrictime.api.model.ApiValue.raw;
19
20 import java.util.Arrays;
21
22 import javax.ws.rs.client.ClientBuilder;
23
24 import org.openhab.binding.lametrictime.api.Configuration;
25 import org.openhab.binding.lametrictime.api.LaMetricTime;
26 import org.openhab.binding.lametrictime.api.cloud.CloudConfiguration;
27 import org.openhab.binding.lametrictime.api.cloud.LaMetricTimeCloud;
28 import org.openhab.binding.lametrictime.api.local.ApplicationActionException;
29 import org.openhab.binding.lametrictime.api.local.ApplicationActivationException;
30 import org.openhab.binding.lametrictime.api.local.ApplicationNotFoundException;
31 import org.openhab.binding.lametrictime.api.local.LaMetricTimeLocal;
32 import org.openhab.binding.lametrictime.api.local.LocalConfiguration;
33 import org.openhab.binding.lametrictime.api.local.NotificationCreationException;
34 import org.openhab.binding.lametrictime.api.local.UpdateException;
35 import org.openhab.binding.lametrictime.api.local.model.Application;
36 import org.openhab.binding.lametrictime.api.local.model.Audio;
37 import org.openhab.binding.lametrictime.api.local.model.Bluetooth;
38 import org.openhab.binding.lametrictime.api.local.model.Display;
39 import org.openhab.binding.lametrictime.api.local.model.Frame;
40 import org.openhab.binding.lametrictime.api.local.model.Notification;
41 import org.openhab.binding.lametrictime.api.local.model.NotificationModel;
42 import org.openhab.binding.lametrictime.api.local.model.UpdateAction;
43 import org.openhab.binding.lametrictime.api.local.model.Widget;
44 import org.openhab.binding.lametrictime.api.model.CoreAction;
45 import org.openhab.binding.lametrictime.api.model.CoreApplication;
46 import org.openhab.binding.lametrictime.api.model.CoreApps;
47 import org.openhab.binding.lametrictime.api.model.Icon;
48 import org.openhab.binding.lametrictime.api.model.Icons;
49 import org.openhab.binding.lametrictime.api.model.enums.BrightnessMode;
50 import org.openhab.binding.lametrictime.api.model.enums.Priority;
51 import org.openhab.binding.lametrictime.api.model.enums.Sound;
52
53 public class LaMetricTimeImpl implements LaMetricTime
54 {
55     private final LaMetricTimeLocal local;
56     private final LaMetricTimeCloud cloud;
57
58     private final Object muteLock = new Object();
59     private Integer volumeSaveState;
60
61     public LaMetricTimeImpl(Configuration config)
62     {
63         this(config.getLocalConfig(), config.getCloudConfig());
64     }
65
66     public LaMetricTimeImpl(Configuration config, ClientBuilder clientBuilder)
67     {
68         this(config.getLocalConfig(), config.getCloudConfig(), clientBuilder);
69     }
70
71     public LaMetricTimeImpl(LocalConfiguration localConfig, CloudConfiguration cloudConfig)
72     {
73         this.local = LaMetricTimeLocal.create(localConfig);
74         this.cloud = LaMetricTimeCloud.create(cloudConfig);
75     }
76
77     public LaMetricTimeImpl(LocalConfiguration localConfig,
78                             CloudConfiguration cloudConfig,
79                             ClientBuilder clientBuilder)
80     {
81         this.local = LaMetricTimeLocal.create(localConfig, clientBuilder);
82         this.cloud = LaMetricTimeCloud.create(cloudConfig, clientBuilder);
83     }
84
85     @Override
86     public String getVersion()
87     {
88         return local.getApi().getApiVersion();
89     }
90
91     @Override
92     public String notifyInfo(String message) throws NotificationCreationException
93     {
94         return notify(message, Priority.INFO, Icons.key("i1248"), Sound.NOTIFICATION, 1, 1);
95     }
96
97     @Override
98     public String notifyWarning(String message) throws NotificationCreationException
99     {
100         return notify(message, Priority.WARNING, Icons.key("a2098"), Sound.NOTIFICATION2, 2, 2);
101     }
102
103     @Override
104     public String notifyCritical(String message) throws NotificationCreationException
105     {
106         return notify(message, Priority.CRITICAL, Icons.key("a4787"), Sound.ALARM1, 0, 0);
107     }
108
109     @Override
110     public String notify(String message,
111                          Priority priority,
112                          Icon icon,
113                          Sound sound,
114                          int messageRepeat,
115                          int soundRepeat) throws NotificationCreationException
116     {
117         // @formatter:off
118         NotificationModel model = new NotificationModel()
119                                       .withCycles(messageRepeat)
120                                       .withFrames(Arrays.asList(new Frame().withText(message)
121                                                                            .withIcon(raw(icon))));
122         if (sound != null)
123         {
124             model.setSound(new org.openhab.binding.lametrictime.api.local.model.Sound()
125                                .withCategory(raw(sound.getCategory()))
126                                .withId(raw(sound))
127                                .withRepeat(soundRepeat));
128         }
129         // @formatter:on
130
131         Notification notification = new Notification().withPriority(raw(priority)).withModel(model);
132         return local.createNotification(notification);
133     }
134
135     @Override
136     public Application getClock()
137     {
138         return getApplication(CoreApps.clock());
139     }
140
141     @Override
142     public Application getCountdown()
143     {
144         return getApplication(CoreApps.countdown());
145     }
146
147     @Override
148     public Application getRadio()
149     {
150         return getApplication(CoreApps.radio());
151     }
152
153     @Override
154     public Application getStopwatch()
155     {
156         return getApplication(CoreApps.stopwatch());
157     }
158
159     @Override
160     public Application getWeather()
161     {
162         return getApplication(CoreApps.weather());
163     }
164
165     @Override
166     public Application getApplication(CoreApplication coreApp)
167     {
168         try
169         {
170             return getLocalApi().getApplication(coreApp.getPackageName());
171         }
172         catch (ApplicationNotFoundException e)
173         {
174             // core apps should never throw errors
175             throw new RuntimeException("Failed to retrieve core application: "
176                                        + coreApp.getPackageName(),
177                                        e);
178         }
179     }
180
181     @Override
182     public Application getApplication(String name) throws ApplicationNotFoundException
183     {
184         return getLocalApi().getApplication(name);
185     }
186
187     @Override
188     public void activateApplication(CoreApplication coreApp)
189     {
190         try
191         {
192             activateApplication(getApplication(coreApp));
193         }
194         catch (ApplicationActivationException e)
195         {
196             // core apps should never throw errors
197             throw new RuntimeException("Failed to activate core application: "
198                                        + coreApp.getPackageName(),
199                                        e);
200         }
201     }
202
203     @Override
204     public void activateApplication(Application app) throws ApplicationActivationException
205     {
206         getLocalApi().activateApplication(app.getPackageName(), getFirstWidgetId(app));
207     }
208
209     @Override
210     public void activateWidget(Widget widget) throws ApplicationActivationException
211     {
212         getLocalApi().activateApplication(widget.getPackageName(), widget.getId());
213     }
214
215     @Override
216     public void doAction(CoreAction coreAction)
217     {
218         try
219         {
220             doAction(getApplication(coreAction.getApp()), coreAction.getAction());
221         }
222         catch (ApplicationActionException e)
223         {
224             // core apps should never throw errors
225             throw new RuntimeException("Failed to execute weather forecast action", e);
226         }
227     }
228
229     @Override
230     public void doAction(Application app, UpdateAction action) throws ApplicationActionException
231     {
232         getLocalApi().doAction(app.getPackageName(), getFirstWidgetId(app), action);
233     }
234
235     @Override
236     public void doAction(Widget widget, CoreAction coreAction) throws ApplicationActionException
237     {
238         doAction(widget, coreAction.getAction());
239     }
240
241     @Override
242     public void doAction(Widget widget, UpdateAction action) throws ApplicationActionException
243     {
244         getLocalApi().doAction(widget.getPackageName(), widget.getId(), action);
245     }
246
247     protected String getFirstWidgetId(Application app)
248     {
249         return app.getWidgets().firstKey();
250     }
251
252     @Override
253     public Display setBrightness(int brightness) throws UpdateException
254     {
255         return local.updateDisplay(new Display().withBrightness(brightness)
256                                                 .withBrightnessMode(raw(BrightnessMode.MANUAL)));
257     }
258
259     @Override
260     public Display setBrightnessMode(BrightnessMode mode) throws UpdateException
261     {
262         return local.updateDisplay(new Display().withBrightnessMode(raw(mode)));
263     }
264
265     @Override
266     public Audio setVolume(int volume) throws UpdateException
267     {
268         return local.updateAudio(new Audio().withVolume(volume));
269     }
270
271     @Override
272     public Audio mute() throws UpdateException
273     {
274         synchronized (muteLock)
275         {
276             Audio audio = local.getAudio();
277             if (audio.getVolume() == 0)
278             {
279                 return audio;
280             }
281
282             volumeSaveState = audio.getVolume();
283             return setVolume(0);
284         }
285     }
286
287     @Override
288     public Audio unmute() throws UpdateException
289     {
290         synchronized (muteLock)
291         {
292             if (volumeSaveState == null)
293             {
294                 Audio audio = local.getAudio();
295                 if (audio.getVolume() == 0)
296                 {
297                     return setVolume(50);
298                 }
299                 else
300                 {
301                     return audio;
302                 }
303             }
304
305             Audio audio = setVolume(volumeSaveState);
306             volumeSaveState = null;
307             return audio;
308         }
309     }
310
311     @Override
312     public Bluetooth setBluetoothActive(boolean active) throws UpdateException
313     {
314         return local.updateBluetooth(new Bluetooth().withActive(active));
315     }
316
317     @Override
318     public Bluetooth setBluetoothName(String name) throws UpdateException
319     {
320         return local.updateBluetooth(new Bluetooth().withName(name));
321     }
322
323     @Override
324     public LaMetricTimeLocal getLocalApi()
325     {
326         return local;
327     }
328
329     @Override
330     public LaMetricTimeCloud getCloudApi()
331     {
332         return cloud;
333     }
334 }