Search in sources :

Example 1 with AnalogValue

use of org.openhab.binding.tacmi.internal.message.AnalogValue 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");
}
Also used : AnalogMessage(org.openhab.binding.tacmi.internal.message.AnalogMessage) Message(org.openhab.binding.tacmi.internal.message.Message) DigitalMessage(org.openhab.binding.tacmi.internal.message.DigitalMessage) DigitalMessage(org.openhab.binding.tacmi.internal.message.DigitalMessage) AnalogValue(org.openhab.binding.tacmi.internal.message.AnalogValue) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) AnalogMessage(org.openhab.binding.tacmi.internal.message.AnalogMessage) SocketTimeoutException(java.net.SocketTimeoutException) TACmiBindingProvider(org.openhab.binding.tacmi.TACmiBindingProvider) OnOffType(org.openhab.core.library.types.OnOffType) DatagramPacket(java.net.DatagramPacket) DecimalType(org.openhab.core.library.types.DecimalType)

Aggregations

IOException (java.io.IOException)1 DatagramPacket (java.net.DatagramPacket)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 TACmiBindingProvider (org.openhab.binding.tacmi.TACmiBindingProvider)1 AnalogMessage (org.openhab.binding.tacmi.internal.message.AnalogMessage)1 AnalogValue (org.openhab.binding.tacmi.internal.message.AnalogValue)1 DigitalMessage (org.openhab.binding.tacmi.internal.message.DigitalMessage)1 Message (org.openhab.binding.tacmi.internal.message.Message)1 DecimalType (org.openhab.core.library.types.DecimalType)1 OnOffType (org.openhab.core.library.types.OnOffType)1