2 * Copyright 2017-2018 Gregory Moyer and contributors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openhab.binding.lametrictime.api.impl;
18 import static org.openhab.binding.lametrictime.api.model.ApiValue.raw;
20 import java.util.Arrays;
22 import javax.ws.rs.client.ClientBuilder;
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;
53 public class LaMetricTimeImpl implements LaMetricTime
55 private final LaMetricTimeLocal local;
56 private final LaMetricTimeCloud cloud;
58 private final Object muteLock = new Object();
59 private Integer volumeSaveState;
61 public LaMetricTimeImpl(Configuration config)
63 this(config.getLocalConfig(), config.getCloudConfig());
66 public LaMetricTimeImpl(Configuration config, ClientBuilder clientBuilder)
68 this(config.getLocalConfig(), config.getCloudConfig(), clientBuilder);
71 public LaMetricTimeImpl(LocalConfiguration localConfig, CloudConfiguration cloudConfig)
73 this.local = LaMetricTimeLocal.create(localConfig);
74 this.cloud = LaMetricTimeCloud.create(cloudConfig);
77 public LaMetricTimeImpl(LocalConfiguration localConfig,
78 CloudConfiguration cloudConfig,
79 ClientBuilder clientBuilder)
81 this.local = LaMetricTimeLocal.create(localConfig, clientBuilder);
82 this.cloud = LaMetricTimeCloud.create(cloudConfig, clientBuilder);
86 public String getVersion()
88 return local.getApi().getApiVersion();
92 public String notifyInfo(String message) throws NotificationCreationException
94 return notify(message, Priority.INFO, Icons.key("i1248"), Sound.NOTIFICATION, 1, 1);
98 public String notifyWarning(String message) throws NotificationCreationException
100 return notify(message, Priority.WARNING, Icons.key("a2098"), Sound.NOTIFICATION2, 2, 2);
104 public String notifyCritical(String message) throws NotificationCreationException
106 return notify(message, Priority.CRITICAL, Icons.key("a4787"), Sound.ALARM1, 0, 0);
110 public String notify(String message,
115 int soundRepeat) throws NotificationCreationException
118 NotificationModel model = new NotificationModel()
119 .withCycles(messageRepeat)
120 .withFrames(Arrays.asList(new Frame().withText(message)
121 .withIcon(raw(icon))));
124 model.setSound(new org.openhab.binding.lametrictime.api.local.model.Sound()
125 .withCategory(raw(sound.getCategory()))
127 .withRepeat(soundRepeat));
131 Notification notification = new Notification().withPriority(raw(priority)).withModel(model);
132 return local.createNotification(notification);
136 public Application getClock()
138 return getApplication(CoreApps.clock());
142 public Application getCountdown()
144 return getApplication(CoreApps.countdown());
148 public Application getRadio()
150 return getApplication(CoreApps.radio());
154 public Application getStopwatch()
156 return getApplication(CoreApps.stopwatch());
160 public Application getWeather()
162 return getApplication(CoreApps.weather());
166 public Application getApplication(CoreApplication coreApp)
170 return getLocalApi().getApplication(coreApp.getPackageName());
172 catch (ApplicationNotFoundException e)
174 // core apps should never throw errors
175 throw new RuntimeException("Failed to retrieve core application: "
176 + coreApp.getPackageName(),
182 public Application getApplication(String name) throws ApplicationNotFoundException
184 return getLocalApi().getApplication(name);
188 public void activateApplication(CoreApplication coreApp)
192 activateApplication(getApplication(coreApp));
194 catch (ApplicationActivationException e)
196 // core apps should never throw errors
197 throw new RuntimeException("Failed to activate core application: "
198 + coreApp.getPackageName(),
204 public void activateApplication(Application app) throws ApplicationActivationException
206 getLocalApi().activateApplication(app.getPackageName(), getFirstWidgetId(app));
210 public void activateWidget(Widget widget) throws ApplicationActivationException
212 getLocalApi().activateApplication(widget.getPackageName(), widget.getId());
216 public void doAction(CoreAction coreAction)
220 doAction(getApplication(coreAction.getApp()), coreAction.getAction());
222 catch (ApplicationActionException e)
224 // core apps should never throw errors
225 throw new RuntimeException("Failed to execute weather forecast action", e);
230 public void doAction(Application app, UpdateAction action) throws ApplicationActionException
232 getLocalApi().doAction(app.getPackageName(), getFirstWidgetId(app), action);
236 public void doAction(Widget widget, CoreAction coreAction) throws ApplicationActionException
238 doAction(widget, coreAction.getAction());
242 public void doAction(Widget widget, UpdateAction action) throws ApplicationActionException
244 getLocalApi().doAction(widget.getPackageName(), widget.getId(), action);
247 protected String getFirstWidgetId(Application app)
249 return app.getWidgets().firstKey();
253 public Display setBrightness(int brightness) throws UpdateException
255 return local.updateDisplay(new Display().withBrightness(brightness)
256 .withBrightnessMode(raw(BrightnessMode.MANUAL)));
260 public Display setBrightnessMode(BrightnessMode mode) throws UpdateException
262 return local.updateDisplay(new Display().withBrightnessMode(raw(mode)));
266 public Audio setVolume(int volume) throws UpdateException
268 return local.updateAudio(new Audio().withVolume(volume));
272 public Audio mute() throws UpdateException
274 synchronized (muteLock)
276 Audio audio = local.getAudio();
277 if (audio.getVolume() == 0)
282 volumeSaveState = audio.getVolume();
288 public Audio unmute() throws UpdateException
290 synchronized (muteLock)
292 if (volumeSaveState == null)
294 Audio audio = local.getAudio();
295 if (audio.getVolume() == 0)
297 return setVolume(50);
305 Audio audio = setVolume(volumeSaveState);
306 volumeSaveState = null;
312 public Bluetooth setBluetoothActive(boolean active) throws UpdateException
314 return local.updateBluetooth(new Bluetooth().withActive(active));
318 public Bluetooth setBluetoothName(String name) throws UpdateException
320 return local.updateBluetooth(new Bluetooth().withName(name));
324 public LaMetricTimeLocal getLocalApi()
330 public LaMetricTimeCloud getCloudApi()