This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented serialization of network state to ZigBeeApi and ZigBeeCon…
…sole. Enables instantaneous startup of console after first startup which includes initial network browsing. Network state is serialized as JSON and for this added jackson dependency. fixes #11
- Loading branch information
Showing
21 changed files
with
424 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ target/ | |
.svn | ||
*.iml | ||
*.log | ||
*.json | ||
zigbee-api/build | ||
.gradle | ||
zigbee-console/build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
zigbee-api/src/main/java/org/bubblecloud/zigbee/network/impl/NetworkStateSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.bubblecloud.zigbee.network.impl; | ||
|
||
import org.bubblecloud.zigbee.network.ZigBeeEndpoint; | ||
import org.bubblecloud.zigbee.network.ZigBeeNetworkManager; | ||
import org.bubblecloud.zigbee.network.ZigBeeNode; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
/** | ||
* NetworkStateSerializer serializes and deserializes the ZigBeeNetworkState. | ||
*/ | ||
public class NetworkStateSerializer { | ||
|
||
/** | ||
* Serializes network state. | ||
* @return the serialized network state as String. | ||
*/ | ||
public String serialize(final ZigBeeNetwork zigBeeNetwork) { | ||
final ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.enableDefaultTyping(); | ||
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | ||
try { | ||
final HashMap<ZigBeeNode, HashMap<Integer, ZigBeeEndpoint>> devices = new HashMap(zigBeeNetwork.getDevices()); | ||
|
||
final List<ZigBeeEndpoint> endpoints = new ArrayList<ZigBeeEndpoint>(); | ||
for (final ZigBeeNode node : devices.keySet()) { | ||
for (final ZigBeeEndpoint endpoint : devices.get(node).values()) { | ||
endpoints.add(endpoint); | ||
} | ||
} | ||
|
||
return objectMapper.writeValueAsString(endpoints); | ||
} catch (final IOException e) { | ||
throw new RuntimeException("Error serializing network state.", e); | ||
} | ||
} | ||
|
||
/** | ||
* Deserializes network state. | ||
* @param zigBeeNetworkManager the ZigBee network manager | ||
* @param zigBeeNetwork the ZigBee network | ||
* @param networkStateString the network state as String | ||
*/ | ||
public void deserialize(final ZigBeeNetworkManager zigBeeNetworkManager, final ZigBeeNetwork zigBeeNetwork, final String networkStateString) { | ||
final ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.enableDefaultTyping(); | ||
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | ||
final List<ZigBeeEndpoint> endpoints; | ||
try { | ||
endpoints = objectMapper.readValue(networkStateString, ArrayList.class); | ||
} catch (final IOException e) { | ||
throw new RuntimeException("Error serializing network state.", e); | ||
} | ||
for (final ZigBeeEndpoint endpoint : endpoints) { | ||
if (zigBeeNetwork.getNode(endpoint.getNode().getIeeeAddress()) == null) { | ||
zigBeeNetwork.addNode((ZigBeeNodeImpl) endpoint.getNode()); | ||
} | ||
((ZigBeeEndpointImpl) endpoint).setNetworkManager(zigBeeNetworkManager); | ||
zigBeeNetwork.addEndpoint(endpoint); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.