Search in sources :

Example 1 with TransformationException

use of org.openhab.core.transform.TransformationException in project openhab1-addons by openhab.

the class SerialDevice method serialEvent.

@Override
public void serialEvent(SerialPortEvent event) {
    switch(event.getEventType()) {
        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
        case SerialPortEvent.DATA_AVAILABLE:
            // we get here if data has been received
            StringBuilder sb = new StringBuilder();
            byte[] readBuffer = new byte[20];
            try {
                do {
                    // read data from serial device
                    while (inputStream.available() > 0) {
                        int bytes = inputStream.read(readBuffer);
                        sb.append(new String(readBuffer, 0, bytes));
                    }
                    try {
                        // add wait states around reading the stream, so that interrupted transmissions are merged
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    // ignore interruption
                    }
                } while (inputStream.available() > 0);
                // sent data
                String result = sb.toString();
                // send data to the bus
                logger.debug("Received message '{}' on serial port {}", new String[] { result, port });
                if (eventPublisher != null) {
                    if (configMap != null && !configMap.isEmpty()) {
                        for (Entry<String, ItemType> entry : configMap.entrySet()) {
                            // use pattern
                            if (entry.getValue().pattern != null) {
                                if (transformationService == null) {
                                    logger.error("No transformation service available!");
                                } else {
                                    try {
                                        String value = transformationService.transform(entry.getValue().pattern, result);
                                        if (entry.getValue().type.equals(NumberItem.class)) {
                                            try {
                                                eventPublisher.postUpdate(entry.getKey(), new DecimalType(value));
                                            } catch (NumberFormatException e) {
                                                logger.warn("Unable to convert regex result '{}' for item {} to number", new String[] { result, entry.getKey() });
                                            }
                                        } else {
                                            eventPublisher.postUpdate(entry.getKey(), new StringType(value));
                                        }
                                    } catch (TransformationException e) {
                                        logger.error("Unable to transform!", e);
                                    }
                                }
                            } else if (entry.getValue().type == StringItem.class) {
                                if (entry.getValue().base64) {
                                    result = Base64.encodeBase64String(result.getBytes());
                                }
                                eventPublisher.postUpdate(entry.getKey(), new StringType(result));
                            } else if (entry.getValue().type == SwitchItem.class && result.trim().isEmpty()) {
                                eventPublisher.postUpdate(entry.getKey(), OnOffType.ON);
                                eventPublisher.postUpdate(entry.getKey(), OnOffType.OFF);
                            }
                        }
                    }
                }
            } catch (IOException e) {
                logger.debug("Error receiving data on serial port {}: {}", new String[] { port, e.getMessage() });
            }
            break;
    }
}
Also used : TransformationException(org.openhab.core.transform.TransformationException) StringType(org.openhab.core.library.types.StringType) IOException(java.io.IOException) StringItem(org.openhab.core.library.items.StringItem) DecimalType(org.openhab.core.library.types.DecimalType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 2 with TransformationException

use of org.openhab.core.transform.TransformationException in project openhab1-addons by openhab.

the class SmarthomaticBinding method processTransformation.

private String processTransformation(String transformation, String response) {
    String transformedResponse = response;
    if (transformation == null) {
        return transformedResponse;
    }
    try {
        String[] parts = splitTransformationConfig(transformation);
        String transformationType = parts[0];
        String transformationFunction = parts[1];
        TransformationService transformationService = TransformationHelper.getTransformationService(SmarthomaticActivator.getContext(), transformationType);
        if (transformationService != null) {
            transformedResponse = transformationService.transform(transformationFunction, response);
        } else {
            transformedResponse = response;
            logger.warn("couldn't transform response because transformationService of type '{}' is unavailable", transformationType);
        }
    } catch (TransformationException te) {
        logger.error("transformation throws exception [transformation= {}, response= {}]", transformation, response);
        logger.error("received transformation exception", te);
        // in case of an error we return the response without any
        // transformation
        transformedResponse = response;
    }
    logger.debug("transformed response is '{}'", transformedResponse);
    return transformedResponse;
}
Also used : TransformationException(org.openhab.core.transform.TransformationException) TransformationService(org.openhab.core.transform.TransformationService)

Example 3 with TransformationException

use of org.openhab.core.transform.TransformationException in project openhab1-addons by openhab.

the class LcnGenericBindingProvider method resolveMappings.

/**
     * Resolves LCN commands (with mappings) to plain commands.
     * 
     * @param lcnTarget the target or a mapping
     * @param openHABcmd the command send by openHAB
     * @return the resolved result (can be null)
     */
private static String resolveMappings(String lcnTarget, String openHABcmd) {
    String result = null;
    Matcher matcher = PATTERN_MAPPING.matcher(lcnTarget);
    if (!matcher.matches()) {
        result = lcnTarget;
    } else {
        matcher.reset();
        matcher.find();
        String s1 = matcher.group(1);
        String s2 = matcher.group(2);
        TransformationService transformationService = TransformationHelper.getTransformationService(LcnBindingActivator.getContext(), s1);
        if (transformationService != null) {
            try {
                result = transformationService.transform(s2, openHABcmd);
            } catch (TransformationException e) {
                result = lcnTarget;
            }
        } else {
            result = lcnTarget;
        }
    }
    return result;
}
Also used : TransformationException(org.openhab.core.transform.TransformationException) Matcher(java.util.regex.Matcher) TransformationService(org.openhab.core.transform.TransformationService)

Example 4 with TransformationException

use of org.openhab.core.transform.TransformationException in project openhab1-addons by openhab.

the class FatekNumberItem method getStateWithTransformation.

private State getStateWithTransformation(BigDecimal val) {
    TransformationService transformationService = TransformationHelper.getTransformationService(FatekPLCActivator.getContext(), transType);
    String strVal = String.valueOf(val);
    String transOut;
    if (transformationService != null) {
        try {
            transOut = transformationService.transform(transFunc, strVal);
            logger.debug("transOut={}", transOut);
        } catch (TransformationException e) {
            transOut = null;
            logger.warn("Transformation error: {}", e.getMessage());
        }
    } else {
        transOut = null;
        logger.warn("No transformation service for: {}", transType);
    }
    if (transOut != null && !"null".equals(transOut)) {
        strVal = transOut;
    }
    return new DecimalType(strVal);
}
Also used : TransformationException(org.openhab.core.transform.TransformationException) DecimalType(org.openhab.core.library.types.DecimalType) TransformationService(org.openhab.core.transform.TransformationService)

Example 5 with TransformationException

use of org.openhab.core.transform.TransformationException in project openhab1-addons by openhab.

the class SnmpBinding method dispatchPdu.

private void dispatchPdu(Address address, PDU pdu) {
    if (pdu != null & address != null) {
        logger.debug("Received PDU from '{}' '{}'", address, pdu);
        for (SnmpBindingProvider provider : providers) {
            for (String itemName : provider.getItemNames()) {
                // Check the IP address
                if (!provider.getAddress(itemName).equals(address)) {
                    continue;
                }
                // Check the OID
                OID oid = provider.getOID(itemName);
                Variable variable = pdu.getVariable(oid);
                if (variable != null) {
                    Class<? extends Item> itemType = provider.getItemType(itemName);
                    // Do any transformations
                    String value = variable.toString();
                    try {
                        value = provider.doTransformation(itemName, value);
                    } catch (TransformationException e) {
                        logger.error("Transformation error with item {}: {}", itemName, e);
                    }
                    // Change to a state
                    State state = null;
                    if (itemType.isAssignableFrom(StringItem.class)) {
                        state = StringType.valueOf(value);
                    } else if (itemType.isAssignableFrom(NumberItem.class)) {
                        state = DecimalType.valueOf(value);
                    } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                        state = OnOffType.valueOf(value);
                    }
                    if (state != null) {
                        eventPublisher.postUpdate(itemName, state);
                    } else {
                        logger.debug("'{}' couldn't be parsed to a State. Valid State-Types are String and Number", variable.toString());
                    }
                } else {
                    logger.trace("PDU doesn't contain a variable with OID '{}'", oid.toString());
                }
            }
        }
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) TransformationException(org.openhab.core.transform.TransformationException) Variable(org.snmp4j.smi.Variable) SnmpBindingProvider(org.openhab.binding.snmp.SnmpBindingProvider) State(org.openhab.core.types.State) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID)

Aggregations

TransformationException (org.openhab.core.transform.TransformationException)8 TransformationService (org.openhab.core.transform.TransformationService)6 DecimalType (org.openhab.core.library.types.DecimalType)3 State (org.openhab.core.types.State)2 IOException (java.io.IOException)1 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 HttpBindingProvider (org.openhab.binding.http.HttpBindingProvider)1 SnmpBindingProvider (org.openhab.binding.snmp.SnmpBindingProvider)1 NumberItem (org.openhab.core.library.items.NumberItem)1 StringItem (org.openhab.core.library.items.StringItem)1 SwitchItem (org.openhab.core.library.items.SwitchItem)1 StringType (org.openhab.core.library.types.StringType)1 OID (org.snmp4j.smi.OID)1 OctetString (org.snmp4j.smi.OctetString)1 Variable (org.snmp4j.smi.Variable)1