use of org.openhab.binding.maxcube.internal.message.S_Command in project openhab1-addons by openhab.
the class MaxCubeBinding method internalReceiveCommand.
/**
* {@inheritDoc}
*/
@Override
public void internalReceiveCommand(String itemName, Command command) {
logger.debug("Received command from {}", itemName);
// resolve serial number for item
String serialNumber = null;
for (MaxCubeBindingProvider provider : providers) {
serialNumber = provider.getSerialNumber(itemName);
if (StringUtils.isBlank(serialNumber)) {
continue;
}
// send command to MAX!Cube LAN Gateway
Device device = findDevice(serialNumber, devices);
if (device == null) {
logger.debug("Cannot send command to device with serial number {}, device not listed.", serialNumber);
continue;
}
String rfAddress = device.getRFAddress();
String commandString = null;
if (command instanceof DecimalType || command instanceof OnOffType) {
DecimalType decimalType = DEFAULT_OFF_TEMPERATURE;
if (command instanceof DecimalType) {
decimalType = (DecimalType) command;
} else if (command instanceof OnOffType) {
decimalType = OnOffType.ON.equals(command) ? DEFAULT_ON_TEMPERATURE : DEFAULT_OFF_TEMPERATURE;
}
S_Command cmd = new S_Command(rfAddress, device.getRoomId(), ((HeatingThermostat) device).getMode(), decimalType.doubleValue());
commandString = cmd.getCommandString();
} else if (command instanceof StringType) {
String commandContent = command.toString().trim().toUpperCase();
S_Command cmd = null;
ThermostatModeType commandThermoType = null;
if (commandContent.contentEquals(ThermostatModeType.AUTOMATIC.toString())) {
commandThermoType = ThermostatModeType.AUTOMATIC;
cmd = new S_Command(rfAddress, device.getRoomId(), commandThermoType);
} else if (commandContent.contentEquals(ThermostatModeType.BOOST.toString())) {
commandThermoType = ThermostatModeType.BOOST;
Double setTemp = Double.parseDouble(((HeatingThermostat) device).getTemperatureSetpoint().toString());
cmd = new S_Command(rfAddress, device.getRoomId(), commandThermoType, setTemp);
} else if (commandContent.contentEquals(ThermostatModeType.MANUAL.toString())) {
commandThermoType = ThermostatModeType.MANUAL;
Double setTemp = Double.parseDouble(((HeatingThermostat) device).getTemperatureSetpoint().toString());
cmd = new S_Command(rfAddress, device.getRoomId(), commandThermoType, setTemp);
logger.debug("updates to MANUAL mode with temperature '{}'", setTemp);
} else {
logger.debug("Only updates to AUTOMATIC, MANUAL & BOOST supported, received value ;'{}'", commandContent);
continue;
}
commandString = cmd.getCommandString();
}
if (commandString != null) {
try {
if (socket == null) {
this.socketConnect();
}
writer.write(commandString);
logger.debug(commandString);
writer.flush();
Message message = null;
String raw = reader.readLine();
try {
while (!this.messageProcessor.isMessageAvailable()) {
this.messageProcessor.addReceivedLine(raw);
raw = reader.readLine();
}
message = this.messageProcessor.pull();
} catch (Exception e) {
logger.info("Error while handling response from MAX! Cube lan gateway!");
logger.debug(Utils.getStackTrace(e));
this.messageProcessor.reset();
}
if (message != null) {
if (message.getType() == MessageType.S) {
sMessageProcessing((S_Message) message);
}
}
if (!exclusive) {
socket.close();
socket = null;
}
} catch (UnknownHostException e) {
logger.info("Host error occurred while connecting to MAX! Cube lan gateway '{}': {}", ip, e.getMessage());
socketClose();
} catch (IOException e) {
logger.info("IO error occurred while writing to MAX! Cube lan gateway '{}': {}", ip, e.getMessage());
// reconnect on next execution
socketClose();
} catch (Exception e) {
logger.info("Error occurred while writing to MAX! Cube lan gateway '{}': {}", ip, e.getMessage());
logger.info(Utils.getStackTrace(e));
// reconnect on next execution
socketClose();
}
logger.debug("Command Sent to {}", ip);
} else {
logger.debug("Null Command not sent to {}", ip);
}
}
}
Aggregations