diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java index 4e719bb5a..2293d3ccc 100644 --- a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java @@ -64,7 +64,7 @@ import org.slf4j.LoggerFactory; * @author Victor Belov - Initial contribution * @author Kai Kreuzer - migrated code to new Jetty client and ESH APIs */ -@Component(service = { EventSubscriber.class, +@Component(service = { CloudService.class, EventSubscriber.class, ActionService.class }, configurationPid = "org.openhab.openhabcloud", property = Constants.SERVICE_PID + "=org.openhab.openhabcloud") @ConfigurableService(category = "io", label = "openHAB Cloud", description_uri = "io:openhabcloud") diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java new file mode 100644 index 000000000..af3eaafa8 --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/BaseNotificationActionHandler.java @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Action; +import org.openhab.core.automation.handler.BaseActionModuleHandler; +import org.openhab.core.automation.handler.ModuleHandler; +import org.openhab.io.openhabcloud.internal.CloudService; + +/** + * This is a base {@link ModuleHandler} implementation for {@link Action}s to send a notifications via openHAB Cloud. + * + * @author Christoph Weitkamp - Initial contribution + */ +@NonNullByDefault +public abstract class BaseNotificationActionHandler extends BaseActionModuleHandler { + + public static final String PARAM_MESSAGE = "message"; + public static final String PARAM_ICON = "icon"; + public static final String PARAM_SEVERITY = "severity"; + + protected final CloudService cloudService; + + protected final String message; + protected final @Nullable String icon; + protected final @Nullable String severity; + + public BaseNotificationActionHandler(Action module, CloudService cloudService) { + super(module); + this.cloudService = cloudService; + + Object messageParam = module.getConfiguration().get(PARAM_MESSAGE); + if (messageParam instanceof String) { + this.message = messageParam.toString(); + } else { + throw new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_MESSAGE)); + } + + Object iconParam = module.getConfiguration().get(PARAM_ICON); + this.icon = iconParam instanceof String ? iconParam.toString() : null; + + Object severityParam = module.getConfiguration().get(PARAM_SEVERITY); + this.severity = severityParam instanceof String ? severityParam.toString() : null; + } +} diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java new file mode 100644 index 000000000..9af6d4cf6 --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationActionTypeProvider.java @@ -0,0 +1,148 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Locale; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Visibility; +import org.openhab.core.automation.type.ActionType; +import org.openhab.core.automation.type.ModuleType; +import org.openhab.core.automation.type.ModuleTypeProvider; +import org.openhab.core.common.registry.ProviderChangeListener; +import org.openhab.core.config.core.ConfigDescriptionParameter; +import org.openhab.core.config.core.ConfigDescriptionParameter.Type; +import org.openhab.core.config.core.ConfigDescriptionParameterBuilder; +import org.osgi.service.component.annotations.Component; + +/** + * This class provides a {@link ModuleTypeProvider} implementation to provide actions to send notifications via + * openHAB Cloud. + * + * @author Christoph Weitkamp - Initial contribution + */ +@NonNullByDefault +@Component(service = ModuleTypeProvider.class) +public class NotificationActionTypeProvider implements ModuleTypeProvider { + + private static final ModuleType SEND_NOTIFICATION_ACTION = new ActionType(SendNotificationActionHandler.TYPE_ID, + getSendNotificationConfig(false, null), "send a notificaton", + "Sends a notification to a specific cloud user.", null, Visibility.VISIBLE, null, null); + private static final ModuleType SEND_EXTENDED_NOTIFICATION_ACTION = new ActionType( + SendNotificationActionHandler.EXTENDED_TYPE_ID, getSendNotificationConfig(true, null), + "send a notificaton with icon and severity", + "Sends a notification to a specific cloud user. Optionally add an icon or the severity.", null, + Visibility.VISIBLE, null, null); + private static final ModuleType SEND_BROADCAST_NOTIFICATION_ACTION = new ActionType( + SendBroadcastNotificationActionHandler.TYPE_ID, getNotificationConfig(false, null), + "broadcast a notificaton", "Sends a notification to all devices of all users.", null, Visibility.VISIBLE, + null, null); + private static final ModuleType SEND_EXRENDED_BROADCAST_NOTIFICATION_ACTION = new ActionType( + SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(true, null), + "broadcast a notificaton with icon and severity", + "Sends a notification to all devices of all users. Optionally add an icon or the severity.", null, + Visibility.VISIBLE, null, null); + private static final ModuleType SEND_LOG_NOTIFICATION_ACTION = new ActionType( + SendLogNotificationActionHandler.TYPE_ID, getNotificationConfig(false, null), "send a log message", + "Sends a log notification to the openHAB Cloud instance. Notifications are NOT sent to any registered devices.", + null, Visibility.VISIBLE, null, null); + private static final ModuleType SEND_EXTENDED_LOG_NOTIFICATION_ACTION = new ActionType( + SendLogNotificationActionHandler.EXTENDED_TYPE_ID, getNotificationConfig(true, null), + "send a log message with icon and severity", + "Sends a log notification to the openHAB Cloud instance. Optionally add an icon or the severity. Notifications are NOT sent to any registered devices.", + null, Visibility.VISIBLE, null, null); + private static final List MODULE_TYPES = List.of(SEND_NOTIFICATION_ACTION, + SEND_EXTENDED_NOTIFICATION_ACTION, SEND_BROADCAST_NOTIFICATION_ACTION, + SEND_EXRENDED_BROADCAST_NOTIFICATION_ACTION, SEND_LOG_NOTIFICATION_ACTION, + SEND_EXTENDED_LOG_NOTIFICATION_ACTION); + + @SuppressWarnings("unchecked") + @Override + public @Nullable ModuleType getModuleType(String UID, @Nullable Locale locale) { + switch (UID) { + case SendNotificationActionHandler.TYPE_ID: + return SEND_NOTIFICATION_ACTION; + case SendNotificationActionHandler.EXTENDED_TYPE_ID: + return SEND_EXTENDED_NOTIFICATION_ACTION; + case SendBroadcastNotificationActionHandler.TYPE_ID: + return SEND_BROADCAST_NOTIFICATION_ACTION; + case SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID: + return SEND_EXRENDED_BROADCAST_NOTIFICATION_ACTION; + case SendLogNotificationActionHandler.TYPE_ID: + return SEND_LOG_NOTIFICATION_ACTION; + case SendLogNotificationActionHandler.EXTENDED_TYPE_ID: + return SEND_EXTENDED_LOG_NOTIFICATION_ACTION; + default: + return null; + } + } + + @Override + public Collection getAll() { + return MODULE_TYPES; + } + + @Override + public Collection getModuleTypes(@Nullable Locale locale) { + return MODULE_TYPES; + } + + private static List getSendNotificationConfig(boolean isExtended, + @Nullable Locale locale) { + List params = new ArrayList<>(); + params.add(ConfigDescriptionParameterBuilder.create(SendNotificationActionHandler.PARAM_USER, Type.TEXT) + .withRequired(true).withLabel("User Id").withDescription("The cloud user id of the recipient.") + .build()); + params.addAll(getNotificationConfig(isExtended, locale)); + return params; + } + + private static List getNotificationConfig(boolean isExtended, @Nullable Locale locale) { + List params = new ArrayList<>(); + params.add(getMessageConfigParameter(locale)); + if (isExtended) { + params.add(getIconConfigParameter(locale)); + params.add(getSeverityConfigParameter(locale)); + } + return params; + } + + private static ConfigDescriptionParameter getMessageConfigParameter(@Nullable Locale locale) { + return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_MESSAGE, Type.TEXT) + .withRequired(true).withLabel("Message").withDescription("The body of the notification.").build(); + } + + private static ConfigDescriptionParameter getIconConfigParameter(@Nullable Locale locale) { + return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_ICON, Type.TEXT) + .withLabel("Icon").withDescription("The icon of the notification.").build(); + } + + private static ConfigDescriptionParameter getSeverityConfigParameter(@Nullable Locale locale) { + return ConfigDescriptionParameterBuilder.create(BaseNotificationActionHandler.PARAM_SEVERITY, Type.TEXT) + .withLabel("Severity").withDescription("The severity of the notification.").build(); + } + + @Override + public void addProviderChangeListener(ProviderChangeListener listener) { + // does nothing because this provider does not change + } + + @Override + public void removeProviderChangeListener(ProviderChangeListener listener) { + // does nothing because this provider does not change + } +} diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java new file mode 100644 index 000000000..f88a5a47d --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/NotificationModuleHandlerFactory.java @@ -0,0 +1,82 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import java.util.Collection; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Action; +import org.openhab.core.automation.Module; +import org.openhab.core.automation.handler.BaseModuleHandlerFactory; +import org.openhab.core.automation.handler.ModuleHandler; +import org.openhab.core.automation.handler.ModuleHandlerFactory; +import org.openhab.io.openhabcloud.internal.CloudService; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; + +/** + * This class provides a {@link ModuleHandlerFactory} implementation to provide actions to send notifications via + * openHAB Cloud. + * + * @author Christoph Weitkamp - Initial contribution + */ +@NonNullByDefault +@Component(service = ModuleHandlerFactory.class) +public class NotificationModuleHandlerFactory extends BaseModuleHandlerFactory { + + private static final Collection TYPES = List.of(SendNotificationActionHandler.TYPE_ID, + SendNotificationActionHandler.EXTENDED_TYPE_ID, SendBroadcastNotificationActionHandler.TYPE_ID, + SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID, SendLogNotificationActionHandler.TYPE_ID, + SendLogNotificationActionHandler.EXTENDED_TYPE_ID); + private final CloudService cloudService; + + @Activate + public NotificationModuleHandlerFactory(final @Reference CloudService cloudService) { + this.cloudService = cloudService; + } + + @Override + @Deactivate + protected void deactivate() { + super.deactivate(); + } + + @Override + public Collection getTypes() { + return TYPES; + } + + @Override + protected @Nullable ModuleHandler internalCreate(Module module, String ruleUID) { + if (module instanceof Action) { + switch (module.getTypeUID()) { + case SendNotificationActionHandler.TYPE_ID: + case SendNotificationActionHandler.EXTENDED_TYPE_ID: + return new SendNotificationActionHandler((Action) module, cloudService); + case SendBroadcastNotificationActionHandler.TYPE_ID: + case SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID: + return new SendBroadcastNotificationActionHandler((Action) module, cloudService); + case SendLogNotificationActionHandler.TYPE_ID: + case SendLogNotificationActionHandler.EXTENDED_TYPE_ID: + return new SendLogNotificationActionHandler((Action) module, cloudService); + default: + break; + } + } + return null; + } +} diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java new file mode 100644 index 000000000..3380e046b --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendBroadcastNotificationActionHandler.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Action; +import org.openhab.core.automation.handler.ModuleHandler; +import org.openhab.io.openhabcloud.internal.CloudService; + +/** + * This is a {@link ModuleHandler} implementation for {@link Action}s to send a notification to all devices of all + * cloud users. + * + * @author Christoph Weitkamp - Initial contribution + */ +@NonNullByDefault +public class SendBroadcastNotificationActionHandler extends BaseNotificationActionHandler { + + public static final String TYPE_ID = "notification.SendBroadcastNotification"; + public static final String EXTENDED_TYPE_ID = "notification.SendExtendedBroadcastNotification"; + + public SendBroadcastNotificationActionHandler(Action module, CloudService cloudService) { + super(module, cloudService); + } + + @Override + public @Nullable Map execute(Map context) { + cloudService.sendBroadcastNotification(message, icon, severity); + return null; + } +} diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendLogNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendLogNotificationActionHandler.java new file mode 100644 index 000000000..fb77fc4dd --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendLogNotificationActionHandler.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Action; +import org.openhab.core.automation.handler.ModuleHandler; +import org.openhab.io.openhabcloud.internal.CloudService; + +/** + * This is a {@link ModuleHandler} implementation for {@link Action}s to send a log notification to the cloud. + * + * @author Christoph Weitkamp - Initial contribution + */ +@NonNullByDefault +public class SendLogNotificationActionHandler extends BaseNotificationActionHandler { + + public static final String TYPE_ID = "notification.SendLogNotification"; + public static final String EXTENDED_TYPE_ID = "notification.SendExtendedLogNotification"; + + public SendLogNotificationActionHandler(Action module, CloudService cloudService) { + super(module, cloudService); + } + + @Override + public @Nullable Map execute(Map context) { + cloudService.sendLogNotification(message, icon, severity); + return null; + } +} diff --git a/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java new file mode 100644 index 000000000..e5b67195a --- /dev/null +++ b/bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/actions/SendNotificationActionHandler.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.io.openhabcloud.internal.actions; + +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.Action; +import org.openhab.core.automation.handler.ModuleHandler; +import org.openhab.io.openhabcloud.internal.CloudService; + +/** + * This is a {@link ModuleHandler} implementation for {@link Action}s to send a notification to a specific cloud user. + * + * @author Christoph Weitkamp - Initial contribution + */ +@NonNullByDefault +public class SendNotificationActionHandler extends BaseNotificationActionHandler { + + public static final String TYPE_ID = "notification.SendNotification"; + public static final String EXTENDED_TYPE_ID = "notification.SendExtendedNotification"; + public static final String PARAM_USER = "userId"; + + private final String userId; + + public SendNotificationActionHandler(Action module, CloudService cloudService) { + super(module, cloudService); + + Object userIdParam = module.getConfiguration().get(PARAM_USER); + if (userIdParam instanceof String) { + this.userId = userIdParam.toString(); + } else { + throw new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_USER)); + } + } + + @Override + public @Nullable Map execute(Map context) { + cloudService.sendNotification(userId, message, icon, severity); + return null; + } +}