Search in sources :

Example 1 with S_Command

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);
        }
    }
}
Also used : S_Command(org.openhab.binding.maxcube.internal.message.S_Command) MaxCubeBindingProvider(org.openhab.binding.maxcube.MaxCubeBindingProvider) S_Message(org.openhab.binding.maxcube.internal.message.S_Message) M_Message(org.openhab.binding.maxcube.internal.message.M_Message) C_Message(org.openhab.binding.maxcube.internal.message.C_Message) L_Message(org.openhab.binding.maxcube.internal.message.L_Message) Message(org.openhab.binding.maxcube.internal.message.Message) UnknownHostException(java.net.UnknownHostException) StringType(org.openhab.core.library.types.StringType) Device(org.openhab.binding.maxcube.internal.message.Device) HeatingThermostat(org.openhab.binding.maxcube.internal.message.HeatingThermostat) IOException(java.io.IOException) IncompleteMessageException(org.openhab.binding.maxcube.internal.exceptions.IncompleteMessageException) UnprocessableMessageException(org.openhab.binding.maxcube.internal.exceptions.UnprocessableMessageException) UnsupportedMessageTypeException(org.openhab.binding.maxcube.internal.exceptions.UnsupportedMessageTypeException) ConfigurationException(org.osgi.service.cm.ConfigurationException) MessageIsWaitingException(org.openhab.binding.maxcube.internal.exceptions.MessageIsWaitingException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NoMessageAvailableException(org.openhab.binding.maxcube.internal.exceptions.NoMessageAvailableException) IncorrectMultilineIndexException(org.openhab.binding.maxcube.internal.exceptions.IncorrectMultilineIndexException) OnOffType(org.openhab.core.library.types.OnOffType) DecimalType(org.openhab.core.library.types.DecimalType) ThermostatModeType(org.openhab.binding.maxcube.internal.message.ThermostatModeType)

Aggregations

IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 MaxCubeBindingProvider (org.openhab.binding.maxcube.MaxCubeBindingProvider)1 IncompleteMessageException (org.openhab.binding.maxcube.internal.exceptions.IncompleteMessageException)1 IncorrectMultilineIndexException (org.openhab.binding.maxcube.internal.exceptions.IncorrectMultilineIndexException)1 MessageIsWaitingException (org.openhab.binding.maxcube.internal.exceptions.MessageIsWaitingException)1 NoMessageAvailableException (org.openhab.binding.maxcube.internal.exceptions.NoMessageAvailableException)1 UnprocessableMessageException (org.openhab.binding.maxcube.internal.exceptions.UnprocessableMessageException)1 UnsupportedMessageTypeException (org.openhab.binding.maxcube.internal.exceptions.UnsupportedMessageTypeException)1 C_Message (org.openhab.binding.maxcube.internal.message.C_Message)1 Device (org.openhab.binding.maxcube.internal.message.Device)1 HeatingThermostat (org.openhab.binding.maxcube.internal.message.HeatingThermostat)1 L_Message (org.openhab.binding.maxcube.internal.message.L_Message)1 M_Message (org.openhab.binding.maxcube.internal.message.M_Message)1 Message (org.openhab.binding.maxcube.internal.message.Message)1 S_Command (org.openhab.binding.maxcube.internal.message.S_Command)1 S_Message (org.openhab.binding.maxcube.internal.message.S_Message)1 ThermostatModeType (org.openhab.binding.maxcube.internal.message.ThermostatModeType)1 DecimalType (org.openhab.core.library.types.DecimalType)1 OnOffType (org.openhab.core.library.types.OnOffType)1