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,37 +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.doorbird.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* The {@link IDoorbirdActions} defines the interface for all thing actions supported by the binding.
* These methods, parameters, and return types are explained in {@link DoorbirdActions}.
*
* @author Mark Hilbush - Initial contribution
*/
@NonNullByDefault
public interface IDoorbirdActions {
public void restart();
public void sipHangup();
public String getRingTimeLimit();
public String getCallTimeLimit();
public String getLastErrorCode();
public String getLastErrorText();
}

View File

@@ -10,10 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.doorbird.action;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
package org.openhab.binding.doorbird.internal.action;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -34,12 +31,12 @@ import org.slf4j.LoggerFactory;
@ThingActionsScope(name = "doorbird")
@NonNullByDefault
public class DoorbirdActions implements ThingActions {
private static final Logger LOGGER = LoggerFactory.getLogger(DoorbirdActions.class);
private final Logger logger = LoggerFactory.getLogger(DoorbirdActions.class);
private @Nullable DoorbellHandler handler;
public DoorbirdActions() {
LOGGER.debug("DoorbirdActions service created");
logger.debug("DoorbirdActions service created");
}
@Override
@@ -51,113 +48,118 @@ public class DoorbirdActions implements ThingActions {
@Override
public @Nullable ThingHandler getThingHandler() {
return this.handler;
return handler;
}
private static IDoorbirdActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
}
if (actions.getClass().getName().equals(DoorbirdActions.class.getName())) {
if (actions instanceof IDoorbirdActions) {
return (IDoorbirdActions) actions;
} else {
return (IDoorbirdActions) Proxy.newProxyInstance(IDoorbirdActions.class.getClassLoader(),
new Class[] { IDoorbirdActions.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 DoorbirdActions");
}
@RuleAction(label = "Restart Doorbird", description = "Restarts the Doorbird device")
@RuleAction(label = "restart the Doorbird", description = "Restarts the Doorbird device.")
public void restart() {
LOGGER.debug("Doorbird action 'restart' called");
logger.debug("Doorbird action 'restart' called");
if (handler != null) {
handler.actionRestart();
} else {
LOGGER.info("Doorbird Action service ThingHandler is null!");
logger.info("Doorbird Action service ThingHandler is null!");
}
}
public static void restart(@Nullable ThingActions actions) {
invokeMethodOf(actions).restart();
if (actions instanceof DoorbirdActions) {
((DoorbirdActions) actions).restart();
} else {
throw new IllegalArgumentException("Actions is not an instance of DoorbirdActions");
}
}
@RuleAction(label = "SIP Hangup", description = "Hangup SIP call")
@RuleAction(label = "hangup a SIP call", description = "Hangup SIP call.")
public void sipHangup() {
LOGGER.debug("Doorbird action 'sipHangup' called");
logger.debug("Doorbird action 'sipHangup' called");
if (handler != null) {
handler.actionSIPHangup();
} else {
LOGGER.info("Doorbird Action service ThingHandler is null!");
logger.info("Doorbird Action service ThingHandler is null!");
}
}
public static void sipHangup(@Nullable ThingActions actions) {
invokeMethodOf(actions).sipHangup();
if (actions instanceof DoorbirdActions) {
((DoorbirdActions) actions).sipHangup();
} else {
throw new IllegalArgumentException("Actions is not an instance of DoorbirdActions");
}
}
@RuleAction(label = "Get Ring Time Limit", description = "Get the value of RING_TIME_LIMIT")
@RuleAction(label = "get the ring time limit", description = "Get the value of RING_TIME_LIMIT.")
public @ActionOutput(name = "getRingTimeLimit", type = "java.lang.String") String getRingTimeLimit() {
LOGGER.debug("Doorbird action 'getRingTimeLimit' called");
logger.debug("Doorbird action 'getRingTimeLimit' called");
if (handler != null) {
return handler.actionGetRingTimeLimit();
} else {
LOGGER.info("Doorbird Action service ThingHandler is null!");
logger.info("Doorbird Action service ThingHandler is null!");
return "";
}
}
public static String getRingTimeLimit(@Nullable ThingActions actions) {
return invokeMethodOf(actions).getRingTimeLimit();
if (actions instanceof DoorbirdActions) {
return ((DoorbirdActions) actions).getRingTimeLimit();
} else {
throw new IllegalArgumentException("Actions is not an instance of DoorbirdActions");
}
}
@RuleAction(label = "Get Call Time Limit", description = "Get the value of CALL_TIME_LIMIT")
@RuleAction(label = "get the call time limit", description = "Get the value of CALL_TIME_LIMIT.")
public @ActionOutput(name = "getCallTimeLimit", type = "java.lang.String") String getCallTimeLimit() {
LOGGER.debug("Doorbird action 'getCallTimeLimit' called");
logger.debug("Doorbird action 'getCallTimeLimit' called");
if (handler != null) {
return handler.actionGetCallTimeLimit();
} else {
LOGGER.info("Doorbird Action service ThingHandler is null!");
logger.info("Doorbird Action service ThingHandler is null!");
return "";
}
}
public static String getCallTimeLimit(@Nullable ThingActions actions) {
return invokeMethodOf(actions).getCallTimeLimit();
if (actions instanceof DoorbirdActions) {
return ((DoorbirdActions) actions).getCallTimeLimit();
} else {
throw new IllegalArgumentException("Actions is not an instance of DoorbirdActions");
}
}
@RuleAction(label = "Get Last Error Code", description = "Get the value of LASTERRORCODE")
@RuleAction(label = "get the last error code", description = "Get the value of LASTERRORCODE.")
public @ActionOutput(name = "getLastErrorCode", type = "java.lang.String") String getLastErrorCode() {
LOGGER.debug("Doorbird action 'getLastErrorCode' called");
logger.debug("Doorbird action 'getLastErrorCode' called");
if (handler != null) {
return handler.actionGetLastErrorCode();
} else {
LOGGER.info("Doorbird Action service ThingHandler is null!");
logger.info("Doorbird Action service ThingHandler is null!");
return "";
}
}
public static String getLastErrorCode(@Nullable ThingActions actions) {
return invokeMethodOf(actions).getLastErrorCode();
if (actions instanceof DoorbirdActions) {
return ((DoorbirdActions) actions).getLastErrorCode();
} else {
throw new IllegalArgumentException("Actions is not an instance of DoorbirdActions");
}
}
@RuleAction(label = "Get Last Error Text", description = "Get the value of LASTERRORTEXT")
@RuleAction(label = "get the last error text", description = "Get the value of LASTERRORTEXT.")
public @ActionOutput(name = "getLastErrorText", type = "java.lang.String") String getLastErrorText() {
LOGGER.debug("Doorbird action 'getLastErrorText' called");
logger.debug("Doorbird action 'getLastErrorText' called");
if (handler != null) {
return handler.actionGetLastErrorText();
} else {
LOGGER.info("Doorbird Action service ThingHandler is null!");
logger.info("Doorbird Action service ThingHandler is null!");
return "";
}
}
public static String getLastErrorText(@Nullable ThingActions actions) {
return invokeMethodOf(actions).getLastErrorText();
if (actions instanceof DoorbirdActions) {
return ((DoorbirdActions) actions).getLastErrorText();
} else {
throw new IllegalArgumentException("Actions is not an instance of DoorbirdActions");
}
}
}

View File

@@ -33,7 +33,7 @@ import javax.imageio.ImageIO;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.doorbird.action.DoorbirdActions;
import org.openhab.binding.doorbird.internal.action.DoorbirdActions;
import org.openhab.binding.doorbird.internal.api.DoorbirdAPI;
import org.openhab.binding.doorbird.internal.api.DoorbirdImage;
import org.openhab.binding.doorbird.internal.api.SipStatus;