Rework ThingActions (#8732)

* Remove proxy workarounds
* Move ThingActions and a few other classes into the internal package
* Use more consistent action labels/descriptions

Related to:

* openhab/openhab-core#1714
* openhab/openhab-core#1639

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2020-10-14 19:01:12 +02:00
committed by GitHub
parent 4adc214c69
commit bef1046258
86 changed files with 708 additions and 2096 deletions

View File

@@ -1,48 +0,0 @@
/**
* 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.binding.telegram.bot;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* Provides the actions for the Telegram API.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface ITelegramActions {
public boolean sendTelegramAnswer(@Nullable Long chatId, @Nullable String replyId, @Nullable String message);
public boolean sendTelegramAnswer(@Nullable String replyId, @Nullable String message);
public boolean sendTelegram(@Nullable Long chatId, @Nullable String message);
public boolean sendTelegram(@Nullable String message);
public boolean sendTelegramQuery(@Nullable Long chatId, @Nullable String message, @Nullable String replyId,
@Nullable String... buttons);
public boolean sendTelegramQuery(@Nullable String message, @Nullable String replyId, @Nullable String... buttons);
public boolean sendTelegram(@Nullable Long chatId, @Nullable String message, @Nullable Object... args);
public boolean sendTelegram(@Nullable String message, @Nullable Object... args);
public boolean sendTelegramPhoto(@Nullable Long chatId, @Nullable String photoURL, @Nullable String caption,
@Nullable String username, @Nullable String password);
public boolean sendTelegramPhoto(@Nullable String photoURL, @Nullable String caption, @Nullable String username,
@Nullable String password);
}

View File

@@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.telegram.bot.TelegramActions;
import org.openhab.binding.telegram.internal.action.TelegramActions;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.ChannelUID;

View File

@@ -10,13 +10,11 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.telegram.bot;
package org.openhab.binding.telegram.internal.action;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
@@ -58,18 +56,12 @@ import com.pengrad.telegrambot.response.SendResponse;
/**
* Provides the actions for the Telegram API.
* <p>
* <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
* the test <i>actions instanceof TelegramActions</i> fails. This test can fail
* due to an issue in openHAB core v2.5.0 where the {@link TelegramActions} class
* can be loaded by a different classloader than the <i>actions</i> instance.
*
* @author Alexander Krasnogolowy - Initial contribution
*
*/
@ThingActionsScope(name = "telegram")
@NonNullByDefault
public class TelegramActions implements ThingActions, ITelegramActions {
public class TelegramActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(TelegramActions.class);
private @Nullable TelegramHandler handler;
@@ -81,7 +73,6 @@ public class TelegramActions implements ThingActions, ITelegramActions {
return true;
}
@NonNullByDefault
private static class BasicResult implements Authentication.Result {
private final HttpHeader header;
@@ -112,8 +103,7 @@ public class TelegramActions implements ThingActions, ITelegramActions {
}
}
@Override
@RuleAction(label = "Telegram answer", description = "Sends a Telegram answer via Telegram API")
@RuleAction(label = "send an answer", description = "Send a Telegram answer using the Telegram API.")
public boolean sendTelegramAnswer(@ActionInput(name = "chatId") @Nullable Long chatId,
@ActionInput(name = "replyId") @Nullable String replyId,
@ActionInput(name = "message") @Nullable String message) {
@@ -155,8 +145,7 @@ public class TelegramActions implements ThingActions, ITelegramActions {
return false;
}
@Override
@RuleAction(label = "Telegram answer", description = "Sends a Telegram answer via Telegram API")
@RuleAction(label = "send an answer", description = "Send a Telegram answer using the Telegram API.")
public boolean sendTelegramAnswer(@ActionInput(name = "replyId") @Nullable String replyId,
@ActionInput(name = "message") @Nullable String message) {
TelegramHandler localHandler = handler;
@@ -170,15 +159,13 @@ public class TelegramActions implements ThingActions, ITelegramActions {
return true;
}
@Override
@RuleAction(label = "Telegram message", description = "Sends a Telegram via Telegram API")
@RuleAction(label = "send a message", description = "Send a Telegram message using the Telegram API.")
public boolean sendTelegram(@ActionInput(name = "chatId") @Nullable Long chatId,
@ActionInput(name = "message") @Nullable String message) {
return sendTelegramGeneral(chatId, message, (String) null);
}
@Override
@RuleAction(label = "Telegram message", description = "Sends a Telegram via Telegram API")
@RuleAction(label = "send a message", description = "Send a Telegram message using the Telegram API.")
public boolean sendTelegram(@ActionInput(name = "message") @Nullable String message) {
TelegramHandler localHandler = handler;
if (localHandler != null) {
@@ -191,8 +178,7 @@ public class TelegramActions implements ThingActions, ITelegramActions {
return true;
}
@Override
@RuleAction(label = "Telegram message", description = "Sends a Telegram via Telegram API")
@RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
public boolean sendTelegramQuery(@ActionInput(name = "chatId") @Nullable Long chatId,
@ActionInput(name = "message") @Nullable String message,
@ActionInput(name = "replyId") @Nullable String replyId,
@@ -200,8 +186,7 @@ public class TelegramActions implements ThingActions, ITelegramActions {
return sendTelegramGeneral(chatId, message, replyId, buttons);
}
@Override
@RuleAction(label = "Telegram message", description = "Sends a Telegram via Telegram API")
@RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
public boolean sendTelegramQuery(@ActionInput(name = "message") @Nullable String message,
@ActionInput(name = "replyId") @Nullable String replyId,
@ActionInput(name = "buttons") @Nullable String... buttons) {
@@ -266,16 +251,14 @@ public class TelegramActions implements ThingActions, ITelegramActions {
return false;
}
@Override
@RuleAction(label = "Telegram message", description = "Sends a Telegram via Telegram API")
@RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
public boolean sendTelegram(@ActionInput(name = "chatId") @Nullable Long chatId,
@ActionInput(name = "message") @Nullable String message,
@ActionInput(name = "args") @Nullable Object... args) {
return sendTelegram(chatId, String.format(message, args));
}
@Override
@RuleAction(label = "Telegram message", description = "Sends a Telegram via Telegram API")
@RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
public boolean sendTelegram(@ActionInput(name = "message") @Nullable String message,
@ActionInput(name = "args") @Nullable Object... args) {
TelegramHandler localHandler = handler;
@@ -289,15 +272,14 @@ public class TelegramActions implements ThingActions, ITelegramActions {
return true;
}
@RuleAction(label = "Telegram photo", description = "Sends a Picture via Telegram API")
@RuleAction(label = "send a photo", description = "Send a picture using the Telegram API.")
public boolean sendTelegramPhoto(@ActionInput(name = "chatId") @Nullable Long chatId,
@ActionInput(name = "photoURL") @Nullable String photoURL,
@ActionInput(name = "caption") @Nullable String caption) {
return sendTelegramPhoto(chatId, photoURL, caption, null, null);
}
@Override
@RuleAction(label = "Telegram photo", description = "Sends a Picture via Telegram API")
@RuleAction(label = "send a photo", description = "Send a picture using the Telegram API.")
public boolean sendTelegramPhoto(@ActionInput(name = "chatId") @Nullable Long chatId,
@ActionInput(name = "photoURL") @Nullable String photoURL,
@ActionInput(name = "caption") @Nullable String caption,
@@ -387,8 +369,7 @@ public class TelegramActions implements ThingActions, ITelegramActions {
return false;
}
@Override
@RuleAction(label = "Telegram photo", description = "Sends a Picture via Telegram API")
@RuleAction(label = "send a photo", description = "Send a Picture using the Telegram API.")
public boolean sendTelegramPhoto(@ActionInput(name = "photoURL") @Nullable String photoURL,
@ActionInput(name = "caption") @Nullable String caption,
@ActionInput(name = "username") @Nullable String username,
@@ -404,7 +385,7 @@ public class TelegramActions implements ThingActions, ITelegramActions {
return true;
}
@RuleAction(label = "Telegram photo", description = "Sends a Picture via Telegram API")
@RuleAction(label = "send a photo", description = "Send a Picture using the Telegram API.")
public boolean sendTelegramPhoto(@ActionInput(name = "photoURL") @Nullable String photoURL,
@ActionInput(name = "caption") @Nullable String caption) {
return sendTelegramPhoto(photoURL, caption, null, null);
@@ -414,78 +395,103 @@ public class TelegramActions implements ThingActions, ITelegramActions {
/* APIs without chatId parameter */
public static boolean sendTelegram(@Nullable ThingActions actions, @Nullable String format,
@Nullable Object... args) {
return invokeMethodOf(actions).sendTelegram(format, args);
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegram(format, args);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
}
public static boolean sendTelegramQuery(@Nullable ThingActions actions, @Nullable String message,
@Nullable String replyId, @Nullable String... buttons) {
return invokeMethodOf(actions).sendTelegramQuery(message, replyId, buttons);
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegramQuery(message, replyId, buttons);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
}
public static boolean sendTelegramPhoto(@Nullable ThingActions actions, @Nullable String photoURL,
@Nullable String caption) {
return invokeMethodOf(actions).sendTelegramPhoto(photoURL, caption, null, null);
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegramPhoto(photoURL, caption, null, null);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
}
public static boolean sendTelegramPhoto(@Nullable ThingActions actions, @Nullable String photoURL,
@Nullable String caption, @Nullable String username, @Nullable String password) {
return invokeMethodOf(actions).sendTelegramPhoto(photoURL, caption, username, password);
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegramPhoto(photoURL, caption, username, password);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
}
public static boolean sendTelegramAnswer(@Nullable ThingActions actions, @Nullable String replyId,
@Nullable String message) {
return invokeMethodOf(actions).sendTelegramAnswer(replyId, message);
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegramAnswer(replyId, message);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
}
/* APIs with chatId parameter */
public static boolean sendTelegram(@Nullable ThingActions actions, @Nullable Long chatId, @Nullable String format,
@Nullable Object... args) {
return invokeMethodOf(actions).sendTelegram(chatId, format, args);
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegram(chatId, format, args);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
}
public static boolean sendTelegramQuery(@Nullable ThingActions actions, @Nullable Long chatId,
@Nullable String message, @Nullable String replyId, @Nullable String... buttons) {
return invokeMethodOf(actions).sendTelegramQuery(chatId, message, replyId, buttons);
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegramQuery(chatId, message, replyId, buttons);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
}
public static boolean sendTelegramPhoto(@Nullable ThingActions actions, @Nullable Long chatId,
@Nullable String photoURL, @Nullable String caption) {
return invokeMethodOf(actions).sendTelegramPhoto(chatId, photoURL, caption, null, null);
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegramPhoto(chatId, photoURL, caption, null, null);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
}
public static boolean sendTelegramPhoto(@Nullable ThingActions actions, @Nullable Long chatId,
@Nullable String photoURL, @Nullable String caption, @Nullable String username, @Nullable String password) {
return invokeMethodOf(actions).sendTelegramPhoto(chatId, photoURL, caption, username, password);
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegramPhoto(chatId, photoURL, caption, username, password);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
}
public static boolean sendTelegramAnswer(@Nullable ThingActions actions, @Nullable Long chatId,
@Nullable String replyId, @Nullable String message) {
return invokeMethodOf(actions).sendTelegramAnswer(chatId, replyId, message);
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegramAnswer(chatId, replyId, message);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
}
public static boolean sendTelegramAnswer(@Nullable ThingActions actions, @Nullable String chatId,
@Nullable String replyId, @Nullable String message) {
return invokeMethodOf(actions).sendTelegramAnswer(Long.valueOf(chatId), replyId, message);
}
private static ITelegramActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
if (actions instanceof TelegramActions) {
return ((TelegramActions) actions).sendTelegramAnswer(Long.valueOf(chatId), replyId, message);
} else {
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
if (actions.getClass().getName().equals(TelegramActions.class.getName())) {
if (actions instanceof ITelegramActions) {
return (ITelegramActions) actions;
} else {
return (ITelegramActions) Proxy.newProxyInstance(ITelegramActions.class.getClassLoader(),
new Class[] { ITelegramActions.class }, (Object proxy, Method method, Object[] args) -> {
Method m = actions.getClass().getDeclaredMethod(method.getName(),
method.getParameterTypes());
return m.invoke(actions, args);
});
}
}
throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
}
@Override