Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VolvoOnCall] Reliable implementation of thing actions #8117

Merged
merged 3 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* 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.volvooncall.internal.action;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* The {@link IVolvoOnCallActions} defines the interface for all thing actions supported by the binding.
*
* @author Gaël L'hopital - Initial contribution
*/
@NonNullByDefault
public interface IVolvoOnCallActions {
public void honkBlinkCommand(Boolean honk, Boolean blink);

public void preclimatizationStopCommand();

public void heaterStopCommand();

public void heaterStartCommand();

public void preclimatizationStartCommand();

public void engineStartCommand(@Nullable Integer runtime);

public void openCarCommand();

public void closeCarCommand();

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
package org.openhab.binding.volvooncall.internal.action;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.thing.binding.ThingActions;
Expand All @@ -31,7 +34,7 @@
*/
@ThingActionsScope(name = "volvooncall")
@NonNullByDefault
public class VolvoOnCallActions implements ThingActions {
public class VolvoOnCallActions implements ThingActions, IVolvoOnCallActions {

private final Logger logger = LoggerFactory.getLogger(VolvoOnCallActions.class);

Expand All @@ -53,6 +56,7 @@ public void setThingHandler(@Nullable ThingHandler handler) {
return this.handler;
}

@Override
@RuleAction(label = "Volvo On Call : Close", description = "Closes the car")
public void closeCarCommand() {
logger.debug("closeCarCommand called");
Expand All @@ -65,13 +69,10 @@ public void closeCarCommand() {
}

public static void closeCarCommand(@Nullable ThingActions actions) {
if (actions instanceof VolvoOnCallActions) {
((VolvoOnCallActions) actions).closeCarCommand();
} else {
throw new IllegalArgumentException("Instance is not an VolvoThingActionsService class.");
}
invokeMethodOf(actions).closeCarCommand();
}

@Override
@RuleAction(label = "Volvo On Call : Open", description = "Opens the car")
public void openCarCommand() {
logger.debug("openCarCommand called");
Expand All @@ -84,13 +85,10 @@ public void openCarCommand() {
}

public static void openCarCommand(@Nullable ThingActions actions) {
if (actions instanceof VolvoOnCallActions) {
((VolvoOnCallActions) actions).openCarCommand();
} else {
throw new IllegalArgumentException("Instance is not an VolvoThingActionsService class.");
}
invokeMethodOf(actions).openCarCommand();
}

@Override
@RuleAction(label = "Volvo On Call : Start Engine", description = "Starts the engine")
public void engineStartCommand(@ActionInput(name = "runtime", label = "Runtime") @Nullable Integer runtime) {
logger.debug("engineStartCommand called");
Expand All @@ -103,13 +101,10 @@ public void engineStartCommand(@ActionInput(name = "runtime", label = "Runtime")
}

public static void engineStartCommand(@Nullable ThingActions actions, @Nullable Integer runtime) {
if (actions instanceof VolvoOnCallActions) {
((VolvoOnCallActions) actions).engineStartCommand(runtime);
} else {
throw new IllegalArgumentException("Instance is not an VolvoThingActionsService class.");
}
invokeMethodOf(actions).engineStartCommand(runtime);
}

@Override
@RuleAction(label = "Volvo On Call : Heater Start", description = "Starts car heater")
public void heaterStartCommand() {
logger.debug("heaterStartCommand called");
Expand All @@ -122,13 +117,10 @@ public void heaterStartCommand() {
}

public static void heaterStartCommand(@Nullable ThingActions actions) {
if (actions instanceof VolvoOnCallActions) {
((VolvoOnCallActions) actions).heaterStartCommand();
} else {
throw new IllegalArgumentException("Instance is not an VolvoThingActionsService class.");
}
invokeMethodOf(actions).heaterStartCommand();
}

@Override
@RuleAction(label = "Volvo On Call : Preclimatization Start", description = "Starts car heater")
public void preclimatizationStartCommand() {
logger.debug("preclimatizationStartCommand called");
Expand All @@ -141,13 +133,10 @@ public void preclimatizationStartCommand() {
}

public static void preclimatizationStartCommand(@Nullable ThingActions actions) {
if (actions instanceof VolvoOnCallActions) {
((VolvoOnCallActions) actions).preclimatizationStartCommand();
} else {
throw new IllegalArgumentException("Instance is not an VolvoThingActionsService class.");
}
invokeMethodOf(actions).preclimatizationStartCommand();
}

@Override
@RuleAction(label = "Volvo On Call : Heater Stop", description = "Stops car heater")
public void heaterStopCommand() {
logger.debug("heaterStopCommand called");
Expand All @@ -160,13 +149,10 @@ public void heaterStopCommand() {
}

public static void heaterStopCommand(@Nullable ThingActions actions) {
if (actions instanceof VolvoOnCallActions) {
((VolvoOnCallActions) actions).heaterStopCommand();
} else {
throw new IllegalArgumentException("Instance is not an VolvoThingActionsService class.");
}
invokeMethodOf(actions).heaterStopCommand();
}

@Override
@RuleAction(label = "Volvo On Call : Preclimatization Stop", description = "Stops car heater")
public void preclimatizationStopCommand() {
logger.debug("preclimatizationStopCommand called");
Expand All @@ -179,13 +165,10 @@ public void preclimatizationStopCommand() {
}

public static void preclimatizationStopCommand(@Nullable ThingActions actions) {
if (actions instanceof VolvoOnCallActions) {
((VolvoOnCallActions) actions).preclimatizationStopCommand();
} else {
throw new IllegalArgumentException("Instance is not an VolvoThingActionsService class.");
}
invokeMethodOf(actions).preclimatizationStopCommand();
}

@Override
@RuleAction(label = "Volvo On Call : Honk-blink", description = "Activates the horn and or lights of the car")
public void honkBlinkCommand(@ActionInput(name = "honk", label = "Honk") Boolean honk,
@ActionInput(name = "blink", label = "Blink") Boolean blink) {
Expand All @@ -199,10 +182,25 @@ public void honkBlinkCommand(@ActionInput(name = "honk", label = "Honk") Boolean
}

public static void honkBlinkCommand(@Nullable ThingActions actions, Boolean honk, Boolean blink) {
if (actions instanceof VolvoOnCallActions) {
((VolvoOnCallActions) actions).honkBlinkCommand(honk, blink);
} else {
throw new IllegalArgumentException("Instance is not an VolvoThingActionsService class.");
}
invokeMethodOf(actions).honkBlinkCommand(honk, blink);
}

private static IVolvoOnCallActions invokeMethodOf(@Nullable ThingActions actions) {
if (actions == null) {
throw new IllegalArgumentException("actions cannot be null");
}
if (actions.getClass().getName().equals(VolvoOnCallActions.class.getName())) {
if (actions instanceof IVolvoOnCallActions) {
return (IVolvoOnCallActions) actions;
} else {
return (IVolvoOnCallActions) Proxy.newProxyInstance(IVolvoOnCallActions.class.getClassLoader(),
new Class[] { IVolvoOnCallActions.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 VolvoOnCallActions");
}
}