use of org.openhab.binding.tacmi.internal.message.AnalogMessage in project openhab1-addons by openhab.
the class TACmiBinding method execute.
/**
* @{inheritDoc
*/
@Override
protected void execute() {
logger.trace("execute() method is called!");
try {
clientSocket.setBroadcast(true);
clientSocket.setSoTimeout(120000);
byte[] receiveData = new byte[14];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
byte[] data = receivePacket.getData();
Message message;
if (data[1] > 0) {
logger.debug("Processing analog message");
message = new AnalogMessage(data);
} else if (data[1] == 0) {
logger.debug("Processing digital message");
message = new DigitalMessage(data);
} else {
logger.debug("Invalid message received");
return;
}
logger.debug(message.toString());
for (TACmiBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
logger.debug("Processing item: " + itemName);
int portNumber = provider.getPortNumber(itemName);
if (provider.getCanNode(itemName) == message.canNode && provider.getPortType(itemName).equals(message.getType().toString().toLowerCase())) {
if (message.hasPortnumber(portNumber)) {
if (message.getType() == MessageType.A) {
AnalogValue value = ((AnalogMessage) message).getAnalogValue(portNumber);
if (value.value != null) {
logger.debug("Updating item: " + itemName + " with value: " + value.value);
eventPublisher.postUpdate(itemName, new DecimalType(value.value));
}
} else {
OnOffType state = ((DigitalMessage) message).getPortStateAsOnOffType(portNumber);
logger.debug("Updating item {} with state {}", itemName, state);
eventPublisher.postUpdate(itemName, state);
}
} else {
logger.debug("Portnumber {} not included in message", portNumber);
}
} else {
logger.debug("CAN Node does not match");
}
}
}
} catch (SocketTimeoutException te) {
logger.info("Receive timeout on CoE socket, retrying ...");
} catch (Exception e) {
logger.error("Error in execute: ", e);
}
logger.trace("TACmi execute() finished");
}
use of org.openhab.binding.tacmi.internal.message.AnalogMessage in project openhab1-addons by openhab.
the class TACmiBinding method internalReceiveCommand.
/**
* @throws UnknownHostException
* @{inheritDoc
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
logger.debug("internalReceiveCommand({},{}) is called!", itemName, command);
for (TACmiBindingProvider provider : providers) {
int canNode = provider.getCanNode(itemName);
String portType = provider.getPortType(itemName);
int portNumber = provider.getPortNumber(itemName);
logger.trace("Type: {}, portNumber: {}, command: {}", portType, portNumber, command.toString());
byte[] messageBytes;
if (portType.equals("d") && portNumber == 1 && command instanceof OnOffType) {
boolean state = OnOffType.ON.equals(command) ? true : false;
DigitalMessage message = new DigitalMessage((byte) canNode, state);
messageBytes = message.getRaw();
} else if (portType.equals("a") && (portNumber - 1) % 4 == 0 && command instanceof DecimalType) {
TACmiMeasureType measureType = provider.getMeasureType(itemName);
AnalogMessage message = new AnalogMessage((byte) canNode, 1, (DecimalType) command, measureType);
messageBytes = message.getRaw();
} else {
logger.info("Not sending command: portType: {}, portNumber: {}, command: {}", portType, portNumber, command.toString());
return;
}
DatagramPacket packet = new DatagramPacket(messageBytes, messageBytes.length, cmiAddress, TACmiBinding.cmiPort);
try {
clientSocket.send(packet);
} catch (IOException e) {
logger.warn("Error sending message: {}, {}", e.getClass().getName(), e.getMessage());
}
}
}
Aggregations