2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lametrictime.internal.api.impl;
15 import static org.openhab.binding.lametrictime.internal.api.dto.ApiValue.raw;
17 import java.util.Arrays;
19 import javax.ws.rs.client.ClientBuilder;
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;
55 * Implementation class for LaMetricTime interface.
57 * @author Gregory Moyer - Initial contribution
60 public class LaMetricTimeImpl implements LaMetricTime {
62 private final Logger logger = LoggerFactory.getLogger(LaMetricTimeImpl.class);
64 private final LaMetricTimeLocal local;
65 private final LaMetricTimeCloud cloud;
67 private final Object muteLock = new Object();
69 private Integer volumeSaveState;
71 public LaMetricTimeImpl(Configuration config) {
72 this(config.getLocalConfig(), config.getCloudConfig());
75 public LaMetricTimeImpl(Configuration config, ClientBuilder clientBuilder) {
76 this(config.getLocalConfig(), config.getCloudConfig(), clientBuilder);
79 public LaMetricTimeImpl(LocalConfiguration localConfig, CloudConfiguration cloudConfig) {
80 this.local = LaMetricTimeLocal.create(localConfig);
81 this.cloud = LaMetricTimeCloud.create(cloudConfig);
84 public LaMetricTimeImpl(LocalConfiguration localConfig, CloudConfiguration cloudConfig,
85 ClientBuilder clientBuilder) {
86 this.local = LaMetricTimeLocal.create(localConfig, clientBuilder);
87 this.cloud = LaMetricTimeCloud.create(cloudConfig, clientBuilder);
91 public String getVersion() {
92 return local.getApi().getApiVersion();
96 public String notifyInfo(@Nullable String message) throws NotificationCreationException {
97 return notify(message, Priority.INFO, Icons.key("i1248"), Sound.NOTIFICATION, 1, 1);
101 public String notifyWarning(@Nullable String message) throws NotificationCreationException {
102 return notify(message, Priority.WARNING, Icons.key("a2098"), Sound.NOTIFICATION2, 2, 2);
106 public String notifyCritical(@Nullable String message) throws NotificationCreationException {
107 return notify(message, Priority.CRITICAL, Icons.key("a4787"), Sound.ALARM1, 0, 0);
111 public String notify(@Nullable String message, @Nullable Priority priority, @Nullable Icon icon,
112 @Nullable Sound sound, int messageRepeat, int soundRepeat) throws NotificationCreationException {
114 NotificationModel model = new NotificationModel()
115 .withCycles(messageRepeat)
116 .withFrames(Arrays.asList(new Frame().withText(message)
117 .withIcon(raw(icon))));
120 model.setSound(new org.openhab.binding.lametrictime.internal.api.local.dto.Sound()
121 .withCategory(raw(sound.getCategory()))
123 .withRepeat(soundRepeat));
127 Notification notification = new Notification().withPriority(raw(priority)).withModel(model);
128 return local.createNotification(notification);
132 public @Nullable Application getClock() {
133 return getApplication(CoreApps.clock());
137 public @Nullable Application getCountdown() {
138 return getApplication(CoreApps.countdown());
142 public @Nullable Application getRadio() {
143 return getApplication(CoreApps.radio());
147 public @Nullable Application getStopwatch() {
148 return getApplication(CoreApps.stopwatch());
152 public @Nullable Application getWeather() {
153 return getApplication(CoreApps.weather());
157 public @Nullable Application getApplication(@Nullable CoreApplication coreApp) {
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);
168 public @Nullable Application getApplication(@Nullable String name) throws ApplicationNotFoundException {
170 return getLocalApi().getApplication(name);
177 public void activateApplication(@Nullable CoreApplication coreApp) {
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);
187 public void activateApplication(@Nullable Application app) throws ApplicationActivationException {
188 getLocalApi().activateApplication(app.getPackageName(), getFirstWidgetId(app));
192 public void activateWidget(@Nullable Widget widget) throws ApplicationActivationException {
193 getLocalApi().activateApplication(widget.getPackageName(), widget.getId());
197 public void doAction(@Nullable CoreAction coreAction) {
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);
207 public void doAction(@Nullable Application app, @Nullable UpdateAction action) throws ApplicationActionException {
208 getLocalApi().doAction(app.getPackageName(), getFirstWidgetId(app), action);
212 public void doAction(@Nullable Widget widget, @Nullable CoreAction coreAction) throws ApplicationActionException {
213 doAction(widget, coreAction.getAction());
217 public void doAction(@Nullable Widget widget, @Nullable UpdateAction action) throws ApplicationActionException {
218 getLocalApi().doAction(widget.getPackageName(), widget.getId(), action);
221 protected String getFirstWidgetId(Application app) {
222 return app.getWidgets().firstKey();
226 public Display setBrightness(int brightness) throws UpdateException {
228 .updateDisplay(new Display().withBrightness(brightness).withBrightnessMode(raw(BrightnessMode.MANUAL)));
232 public Display setBrightnessMode(@Nullable BrightnessMode mode) throws UpdateException {
233 return local.updateDisplay(new Display().withBrightnessMode(raw(mode)));
237 public Audio setVolume(int volume) throws UpdateException {
238 return local.updateAudio(new Audio().withVolume(volume));
242 public Audio mute() throws UpdateException {
243 synchronized (muteLock) {
244 Audio audio = local.getAudio();
245 if (audio.getVolume() == 0) {
249 volumeSaveState = audio.getVolume();
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);
266 Audio audio = setVolume(volumeSaveState);
267 volumeSaveState = null;
273 public Bluetooth setBluetoothActive(boolean active) throws UpdateException {
274 return local.updateBluetooth(new Bluetooth().withActive(active));
278 public Bluetooth setBluetoothName(@Nullable String name) throws UpdateException {
279 return local.updateBluetooth(new Bluetooth().withName(name));
283 public LaMetricTimeLocal getLocalApi() {
288 public LaMetricTimeCloud getCloudApi() {