[openhabcloud] Added Actions for sending notifications (#9145)

* Added ThingActions for sending notifications

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2020-11-28 21:25:20 +01:00 committed by GitHub
parent 936a4dc8d8
commit be61a70030
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 428 additions and 1 deletions

View File

@ -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")

View File

@ -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;
}
}

View File

@ -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<ModuleType> 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<ModuleType> getAll() {
return MODULE_TYPES;
}
@Override
public Collection<ModuleType> getModuleTypes(@Nullable Locale locale) {
return MODULE_TYPES;
}
private static List<ConfigDescriptionParameter> getSendNotificationConfig(boolean isExtended,
@Nullable Locale locale) {
List<ConfigDescriptionParameter> 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<ConfigDescriptionParameter> getNotificationConfig(boolean isExtended, @Nullable Locale locale) {
List<ConfigDescriptionParameter> 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<ModuleType> listener) {
// does nothing because this provider does not change
}
@Override
public void removeProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
// does nothing because this provider does not change
}
}

View File

@ -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<String> 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<String> 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;
}
}

View File

@ -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<String, Object> execute(Map<String, Object> context) {
cloudService.sendBroadcastNotification(message, icon, severity);
return null;
}
}

View File

@ -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<String, Object> execute(Map<String, Object> context) {
cloudService.sendLogNotification(message, icon, severity);
return null;
}
}

View File

@ -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<String, Object> execute(Map<String, Object> context) {
cloudService.sendNotification(userId, message, icon, severity);
return null;
}
}