How to define a new command and retrieve an existing one.
Defining Commands
To create a new command:
The following Java code snippet creates a new command of type Log Command and configures it. Additionally, it saves the ID of the command in the mylogcommandID variable.
HomeAutomationAdmin hamAdmin = (HomeAutomationAdmin) service;
//The type of the command that we want to create and its properties
String type = LogCommand.TYPE;
//create a command
Object mycommand = hamAdmin.createCommand(type);
LogCommand mylogcommand = (LogCommand)mycommand;
//configure its properties
mylogcommand.setMessage("Message");
mylogcommand.setPrintOnConsole(true);
mylogcommand.setPrintOnLog(false);
//apply changes
mylogcommand.save();
//save its id for later use
String mylogcommandID = mylogcommand.getId();
The following JavaScript code snippet creates a new command of type Log Command and configures it. Additionally, it saves the ID of the command in the mylogcommandID variable.
//The type of the command that we want to create and its properties
type = "LogCommand"
props = new Object()
props.message = "Message"
props.print_on_console=true
props.print_on_log=false
//create a command and configure its properties
var xhr = new XMLHttpRequest();
xhr.open("POST", "/remote/json-rpc", false);
xhr.setRequestHeader("Content-Type","application/json")
xhr.send('{"jsonrpc": "2.0", "method": "HAM/createCommand", "params": ["'+type+'", '+
JSON.stringify(props)+'], "id": 1}')
//save its id for later use
mylogcommandID = JSON.parse(xhr.responseText).result
Editing Commands
To edit a command:
The following Java code example retrieves the command, created in the previous example and changes one of its properties.
//retrieve the command
HomeAutomationAdmin hamAdmin = (HomeAutomationAdmin) service;
Object mycommand = hamAdmin.getCommand(LogCommand.TYPE, mylogcommandID);
LogCommand mylogcommand = (LogCommand)mycommand;
//change property
mylogcommand.setMessage("Different Message");
//apply changes
mylogcommand.save();
The following JavaScript code example retrieves the command, created in the previous example and changes one of its properties.
//retrieve the command
var xhr = new XMLHttpRequest();
xhr.open("POST", "/remote/json-rpc", false);
xhr.setRequestHeader("Content-Type","application/json")
xhr.send('{"jsonrpc": "2.0", "method": "HAM/getCommand", "params": ["LogCommand", "'+
mylogcommandID+'" ], "id": 1}')
mylogcommand=JSON.parse(xhr.responseText).result
//change property
mylogcommand.message = "Another Message"
//apply changes
xhr.open("POST", "/remote/json-rpc", false);
xhr.setRequestHeader("Content-Type","application/json")
xhr.send('{"jsonrpc": "2.0", "method": "HAM/updateCommand", "params": ["LogCommand", "'+
mylogcommandID+'", '+JSON.stringify(mylogcommand)+' ], "id": 1}')
If you are using the JSON API you can retrieve some of the command's properties, without retrieving the whole JSON object by calling the getCommandsSimple and getCommandSimple functions and passing an array of the properties you need.
Explicitly Starting a Command
To explicitly start a command:
Receiving Events
You can be notified when new commands and condition types are added and when commands finish their execution.
Available Command/Condition Types
The topics of the events related to changes in the set of command and condition types are:
To subscribe to both topics, use the HomeAutomationAdmin.TOPIC_PREFIX prefix followed by a wildcard (*).
Command Status Events
Status events fired by commands are under the StatusReporter.TOPIC_STATUS topic.
Command status events are disabled by default. You can enable them by populating the ham.status.event system property, either with comma-separated list of the command types you want to listen to, or with a wildcard (*) to listen to events by all command types.
You can receive events by subscribing to those topics via OSGi Event Handler, or remotely via JSON-RPC or Web Socket.