This tutorial, we'll add feature in openremote to handle serial commands.
/**
*
*/
package org.openremote.controller.command.protocol.serial;
import java.util.List;
import org.jdom.Element;
import org.openremote.controller.command.Command;
import org.openremote.controller.command.CommandBuilder;
import org.openremote.controller.utils.CommandUtil;
/**
* @author Anand.Tamariya
*
*/
public class SerialCommandBuilder implements CommandBuilder {
/** The command to perform the http get request on */
private String comPort;
private SerialCommand command;
/*
* (non-Javadoc)
*
* @see
* org.openremote.controller.command.CommandBuilder#build(org.jdom.Element)
*/
@Override
public Command build(Element element) {
command.setComPort(getComPort());
List<Element> propertyElements = element.getChildren(
CommandBuilder.XML_ELEMENT_PROPERTY, element.getNamespace());
for (Element el : propertyElements) {
if ("command".equals(el
.getAttributeValue(CommandBuilder.XML_ATTRIBUTENAME_NAME))) {
command.setCommand(CommandUtil.parseStringWithParam(
element,
el.getAttributeValue(CommandBuilder.XML_ATTRIBUTENAME_VALUE)));
}
}
return command;
}
/**
* @return the command
*/
public String getComPort() {
return comPort;
}
/**
* @param command
* the command to set
*/
public void setComPort(String command) {
this.comPort = command;
}
/**
* @return the command
*/
public SerialCommand getCommand() {
return command;
}
/**
* @param command the command to set
*/
public void setCommand(SerialCommand command) {
this.command = command;
}
}
/**
*
*/
package org.openremote.controller.command.protocol.serial;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.OutputStream;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.openremote.controller.command.ExecutableCommand;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Anand.Tamariya
*
*/
public class SerialCommand implements ExecutableCommand {
/** The logger. */
private static Logger logger = Logger.getLogger(SerialCommand.class
.getName());
/** The command to perform the http get request on */
private String command;
private String comPort = "COM25";
/*
* (non-Javadoc)
*
* @see org.openremote.controller.command.ExecutableCommand#send()
*/
@Override
public void send() {
try {
byte[] dataBytes = getCommand().getBytes();
logger.info("Command: " + getCommand() + " DataBytes: "
+ Arrays.toString(dataBytes));
CommPortIdentifier id = CommPortIdentifier
.getPortIdentifier(comPort);
SerialPort serialPort = (SerialPort) id.open("ORBController", 2000);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
OutputStream outputStream = serialPort.getOutputStream();
outputStream.write(dataBytes);
outputStream.close();
serialPort.close();
logger.info("Command Execution finished");
} catch (Exception e) {
logger.error("Error sending serial command Port: " + comPort, e);
}
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
SerialCommand cmd = (SerialCommand) context.getBean("serialCommand");
cmd.setCommand("M050001FF");
cmd.send();
}
/**
* @return the command
*/
public String getCommand() {
return command;
}
/**
* @param command
* the command to set
*/
public void setCommand(String command) {
this.command = command;
}
/**
* @return the comPort
*/
public String getComPort() {
return comPort;
}
/**
* @param comPort the comPort to set
*/
public void setComPort(String comPort) {
this.comPort = comPort;
}
}
<bean id = "commandFactory" class = "org.openremote.controller.command.CommandFactory">
<constructor-arg>
<map>
...
<entry key = "serial" value-ref = "serialCommandBuilder"/>
</map>
</constructor-arg>
<bean id="serialCommandBuilder" class="org.openremote.controller.command.protocol.serial.SerialCommandBuilder">
<property name ="comPort" value="COM25"/>
<property name ="command" ref="serialCommand"/>
</bean>
<bean id="serialCommand" class="org.openremote.controller.command.protocol.serial.SerialCommand" scope="prototype">
</bean>
<command id="62" protocol="serial">
<property name="command" value="M050001FF" />
<property name="name" value="on" />
</command>
Comments/suggestions - Send a mail to tamariya@rediffmail.com.