Search in sources :

Example 1 with DigitalMessage

use of org.openhab.binding.tacmi.internal.message.DigitalMessage 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)

Example 2 with DigitalMessage

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

Aggregations

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