Search in sources :

Example 1 with ZigBeeNodeDao

use of com.zsmartsystems.zigbee.dao.ZigBeeNodeDao in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZigBeeNetworkStateSerializerImpl method deserialize.

/**
 * Deserializes the network state.
 *
 * @param networkState the network state
 */
@Override
public void deserialize(final ZigBeeNetworkManager networkState) {
    final File file = new File(networkId);
    boolean networkStateExists = file.exists();
    if (networkStateExists == false) {
        return;
    }
    logger.info("Loading network state...");
    try {
        XStream stream = openStream();
        BufferedReader reader;
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        final List<Object> objects = (List<Object>) stream.fromXML(reader);
        for (final Object object : objects) {
            if (object instanceof ZigBeeNodeDao) {
                ZigBeeNodeDao nodeDao = (ZigBeeNodeDao) object;
                ZigBeeNode node = new ZigBeeNode(networkState, new IeeeAddress(nodeDao.getIeeeAddress()));
                node.setDao(nodeDao);
                networkState.addNode(node);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    logger.info("Loading network state complete.");
}
Also used : InputStreamReader(java.io.InputStreamReader) XStream(com.thoughtworks.xstream.XStream) ZigBeeNode(com.zsmartsystems.zigbee.ZigBeeNode) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) IeeeAddress(com.zsmartsystems.zigbee.IeeeAddress) ZigBeeNodeDao(com.zsmartsystems.zigbee.dao.ZigBeeNodeDao) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 2 with ZigBeeNodeDao

use of com.zsmartsystems.zigbee.dao.ZigBeeNodeDao in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZigBeeNode method getDao.

/**
 * Gets a {@link ZigBeeNodeDao} representing the node
 *
 * @return the {@link ZigBeeNodeDao}
 */
public ZigBeeNodeDao getDao() {
    ZigBeeNodeDao dao = new ZigBeeNodeDao();
    dao.setIeeeAddress(ieeeAddress.toString());
    dao.setNetworkAddress(networkAddress);
    dao.setNodeDescriptor(nodeDescriptor);
    dao.setPowerDescriptor(powerDescriptor);
    dao.setBindingTable(bindingTable);
    List<ZigBeeEndpointDao> endpointDaoList = new ArrayList<ZigBeeEndpointDao>();
    for (ZigBeeEndpoint endpoint : endpoints.values()) {
        endpointDaoList.add(endpoint.getDao());
    }
    dao.setEndpoints(endpointDaoList);
    return dao;
}
Also used : ZigBeeNodeDao(com.zsmartsystems.zigbee.dao.ZigBeeNodeDao) ArrayList(java.util.ArrayList) ZigBeeEndpointDao(com.zsmartsystems.zigbee.dao.ZigBeeEndpointDao)

Example 3 with ZigBeeNodeDao

use of com.zsmartsystems.zigbee.dao.ZigBeeNodeDao in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZigBeeNetworkStateSerializerImpl method serialize.

/**
 * Serializes the network state.
 *
 * @param networkState the network state
 */
@Override
public void serialize(final ZigBeeNetworkManager networkState) {
    XStream stream = openStream();
    final List<ZigBeeNodeDao> destinations = new ArrayList<ZigBeeNodeDao>();
    for (ZigBeeNode node : networkState.getNodes()) {
        ZigBeeNodeDao nodeDao = node.getDao();
        destinations.add(nodeDao);
    }
    final File file = new File(networkId);
    try {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        stream.marshal(destinations, new PrettyPrintWriter(writer));
        writer.flush();
        writer.close();
    } catch (IOException e) {
        logger.error("Error writing network state", e);
    }
    logger.info("ZigBee saving network state complete.");
}
Also used : ZigBeeNodeDao(com.zsmartsystems.zigbee.dao.ZigBeeNodeDao) XStream(com.thoughtworks.xstream.XStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) ZigBeeNode(com.zsmartsystems.zigbee.ZigBeeNode) OutputStreamWriter(java.io.OutputStreamWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Aggregations

ZigBeeNodeDao (com.zsmartsystems.zigbee.dao.ZigBeeNodeDao)3 ArrayList (java.util.ArrayList)3 XStream (com.thoughtworks.xstream.XStream)2 ZigBeeNode (com.zsmartsystems.zigbee.ZigBeeNode)2 File (java.io.File)2 IOException (java.io.IOException)2 PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)1 IeeeAddress (com.zsmartsystems.zigbee.IeeeAddress)1 ZigBeeEndpointDao (com.zsmartsystems.zigbee.dao.ZigBeeEndpointDao)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 List (java.util.List)1