Search in sources :

Example 1 with KNXMLException

use of tuwien.auto.calimero.xml.KNXMLException in project openremote by openremote.

the class KNXProtocol method discoverLinkedAssetAttributes.

@Override
public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException {
    ZipInputStream zin = null;
    try {
        boolean fileFound = false;
        byte[] data = CodecUtil.decodeBase64(fileInfo.getContents());
        zin = new ZipInputStream(new ByteArrayInputStream(data));
        ZipEntry zipEntry = zin.getNextEntry();
        while (zipEntry != null) {
            if (zipEntry.getName().endsWith("/0.xml")) {
                fileFound = true;
                break;
            }
            zipEntry = zin.getNextEntry();
        }
        if (!fileFound) {
            String msg = "Failed to find '0.xml' in project file";
            LOG.info(msg);
            throw new IllegalStateException(msg);
        }
        // Create a transform factory instance.
        System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
        TransformerFactory tfactory = TransformerFactory.newInstance();
        // Create a transformer for the stylesheet.
        Transformer transformer = tfactory.newTransformer(new StreamSource(this.getClass().getResourceAsStream("/org/openremote/agent/protocol/knx/ets_calimero_group_name.xsl")));
        // Set the URIResolver
        transformer.setURIResolver(new EtsFileUriResolver(data));
        // Transform the source XML into byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        transformer.transform(new StreamSource(zin), new StreamResult(bos));
        byte[] result = bos.toByteArray();
        // we use a map of state-based datapoints and read from the transformed xml
        final DatapointMap<StateDP> datapoints = new DatapointMap<>();
        try (final XmlReader r = XmlInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(result))) {
            datapoints.load(r);
        } catch (final KNXMLException e) {
            String msg = "Error loading parsed ETS file: " + e.getMessage();
            LOG.warning(msg);
            throw new IllegalStateException(msg, e);
        }
        MetaItem agentLink = AgentLink.asAgentLinkMetaItem(protocolConfiguration.getReferenceOrThrow());
        Map<String, Asset> createdAssets = new HashMap<>();
        for (StateDP dp : datapoints.getDatapoints()) {
            if (dp.getName().endsWith("#A")) {
                createAsset(dp, false, agentLink, createdAssets);
            } else if (dp.getName().endsWith("#S")) {
                createAsset(dp, true, agentLink, createdAssets);
            } else if (dp.getName().endsWith("#SA") || dp.getName().endsWith("#AS")) {
                createAsset(dp, false, agentLink, createdAssets);
                createAsset(dp, true, agentLink, createdAssets);
            } else {
                LOG.info("Only group addresses ending on #A, #S, #AS or #SA will be imported. Ignoring: " + dp.getName());
            }
        }
        return createdAssets.values().toArray(new Asset[createdAssets.values().size()]);
    } catch (Exception e) {
        throw new IllegalStateException("ETS import error", e);
    } finally {
        if (zin != null) {
            try {
                zin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DatapointMap(tuwien.auto.calimero.datapoint.DatapointMap) StreamResult(javax.xml.transform.stream.StreamResult) ZipEntry(java.util.zip.ZipEntry) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XmlReader(tuwien.auto.calimero.xml.XmlReader) KNXMLException(tuwien.auto.calimero.xml.KNXMLException) IOException(java.io.IOException) KNXMLException(tuwien.auto.calimero.xml.KNXMLException) KNXFormatException(tuwien.auto.calimero.KNXFormatException) IOException(java.io.IOException) KNXException(tuwien.auto.calimero.KNXException) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) StateDP(tuwien.auto.calimero.datapoint.StateDP) Asset(org.openremote.model.asset.Asset)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Asset (org.openremote.model.asset.Asset)1 KNXException (tuwien.auto.calimero.KNXException)1 KNXFormatException (tuwien.auto.calimero.KNXFormatException)1 DatapointMap (tuwien.auto.calimero.datapoint.DatapointMap)1 StateDP (tuwien.auto.calimero.datapoint.StateDP)1 KNXMLException (tuwien.auto.calimero.xml.KNXMLException)1 XmlReader (tuwien.auto.calimero.xml.XmlReader)1