/
Commands.java
78 lines (66 loc) · 2.73 KB
/
Commands.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.openremote.controller.command;
import org.openremote.controller.event.EventProcessor;
import org.openremote.controller.deploy.CommandDefinition;
import org.openremote.controller.deploy.Deployment;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Can be used directly and/or by {@link EventProcessor}s to trigger
* {@link ExecutableCommand}s (e.g. in rules or from simple client call).
*/
public class Commands {
private static final Logger LOG = Logger.getLogger(Commands.class.getName());
final protected Deployment deployment;
public Commands(Deployment deployment) {
this.deployment = deployment;
}
public void execute(String commandName) {
execute(commandName, null);
}
public void execute(String commandName, int arg) {
execute(commandName, Integer.toString(arg));
}
public void execute(String commandName, String arg) {
CommandDefinition commandDefinition = deployment.getCommandDefinition(commandName);
if (commandDefinition == null) {
LOG.warning("Command definition not found, ignoring execution: " + commandName);
return;
}
execute(commandDefinition, arg);
}
/**
* Executes a command. This normally means a device protocol is used to communicate with
* some physical device.
*/
public void execute(CommandDefinition commandDefinition) {
execute(commandDefinition, null);
}
/**
* Build a {@link Command} from this definition and, if possible, execute it with the given argument.
*
* @param arg command parameter value
*/
public void execute(CommandDefinition commandDefinition, String arg) {
Command command;
try {
command = deployment.getCommandBuilder().build(commandDefinition);
if (command == null) {
LOG.log(Level.WARNING,
"No command was produced (does the protocol have an ExecutableCommand?): " + commandDefinition
);
} else if (command instanceof ExecutableCommand) {
LOG.fine("Executing '" + commandDefinition.getName() + "' with: " + arg);
ExecutableCommand executableCommand = (ExecutableCommand) command;
try {
executableCommand.send(arg);
} catch (Exception ex) {
LOG.log(Level.SEVERE, "Error executing '" + commandDefinition.getName() + "' with: " + arg, ex);
}
} else {
LOG.log(Level.WARNING, "Ignoring, not an ExecutableCommand: " + command);
}
} catch (Throwable t) {
LOG.log(Level.SEVERE, "Error building: " + commandDefinition, t);
}
}
}