Skip to content

Commit

Permalink
Implemented NetworkStateSerialization #34
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Markus committed Feb 13, 2019
2 parents 04ba3cb + 8c83099 commit c87999c
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 115 deletions.
84 changes: 74 additions & 10 deletions Samples/core2.1/ZigBeeNet.PlayGround/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Serilog;
using ZigBeeNet.App;
using ZigBeeNet.App.Discovery;
using ZigBeeNet.DAO;
using ZigBeeNet.Hardware.TI.CC2531;
using ZigBeeNet.Serial;
using ZigBeeNet.Transaction;
Expand Down Expand Up @@ -40,6 +44,9 @@ static void Main(string[] args)

ZigBeeNetworkManager networkManager = new ZigBeeNetworkManager(dongle);

JsonNetworkSerializer deviceSerializer = new JsonNetworkSerializer("devices.json");
networkManager.NetworkStateSerializer = deviceSerializer;

ZigBeeDiscoveryExtension discoveryExtension = new ZigBeeDiscoveryExtension();
discoveryExtension.SetUpdatePeriod(60);
networkManager.AddExtension(discoveryExtension);
Expand All @@ -52,12 +59,14 @@ static void Main(string[] args)
networkManager.AddCommandListener(new ZigBeeTransaction(networkManager));
networkManager.AddCommandListener(new ConsoleCommandListener());
networkManager.AddNetworkNodeListener(new ConsoleNetworkNodeListener());



networkManager.AddSupportedCluster(0x06);
networkManager.AddSupportedCluster(0x08);
networkManager.AddSupportedCluster(0x0300);

((ZigBeeDongleTiCc2531)dongle).SetLedMode(1, false); // green led
((ZigBeeDongleTiCc2531)dongle).SetLedMode(2, false); // red led

ZigBeeStatus startupSucceded = networkManager.Startup(false);

if (startupSucceded == ZigBeeStatus.SUCCESS)
Expand All @@ -80,7 +89,7 @@ static void Main(string[] args)

while (cmd != "exit")
{
Console.WriteLine(networkManager.GetNodes().Count + " node(s)");
Console.WriteLine(networkManager.Nodes.Count + " node(s)");

if (!string.IsNullOrEmpty(cmd))
{
Expand All @@ -93,7 +102,18 @@ static void Main(string[] args)

if (node != null)
{
var endpointAddress = node.Endpoints.FirstOrDefault().Value.GetEndpointAddress();
ZigBeeEndpointAddress endpointAddress = null;
var endpoint = node.Endpoints.Values.FirstOrDefault();

if (endpoint != null)
endpointAddress = endpoint.GetEndpointAddress();

if (endpointAddress == null)
{
Console.WriteLine("No endpoint found");

continue;
}

try
{
Expand Down Expand Up @@ -156,12 +176,6 @@ static void Main(string[] args)
}
else if (cmd == "color")
{
//Console.WriteLine("ColorX between 0 and 65535: ");
//string x = Console.ReadLine();

//Console.WriteLine("ColorY between 0 and 65535: ");
//string y = Console.ReadLine();

Console.WriteLine("Red between 0 and 255: ");
string r = Console.ReadLine();

Expand Down Expand Up @@ -274,4 +288,54 @@ public void NodeUpdated(ZigBeeNode node)
Console.WriteLine("Node updated " + node);
}
}

public class JsonNetworkSerializer : IZigBeeNetworkStateSerializer
{
private string _filename;

public JsonNetworkSerializer(string filename)
{
_filename = filename;
}

public void Deserialize(ZigBeeNetworkManager networkManager)
{
if (File.Exists(_filename) == false)
return;

List<ZigBeeNodeDao> nodes = JsonConvert.DeserializeObject<List<ZigBeeNodeDao>>(File.ReadAllText(_filename));

if (nodes == null)
return;

foreach (var nodeDao in nodes)
{
ZigBeeNode node = new ZigBeeNode(networkManager, new IeeeAddress(nodeDao.IeeeAddress));
node.SetDao(nodeDao);

networkManager.AddNode(node);
}
}

public void Serialize(ZigBeeNetworkManager networkManager)
{

List<ZigBeeNodeDao> nodes = new List<ZigBeeNodeDao>();

foreach (var node in networkManager.Nodes)
{
ZigBeeNodeDao nodeDao = node.GetDao();
nodes.Add(nodeDao);
}

var settings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};

string json = JsonConvert.SerializeObject(nodes, Formatting.Indented, settings);

File.WriteAllText(_filename, json);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Serilog" Version="2.7.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions ZigBeeNet.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.329
# Visual Studio Version 16
VisualStudioVersion = 16.0.28531.58
MinimumVisualStudioVersion = 10.0.40219.1
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ZigBeeNet", "src\ZigBeeNet\ZigBeeNet.shproj", "{6BCA5435-C030-4C96-A3E2-406815ABE131}"
EndProject
Expand All @@ -24,7 +24,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{ABCECF51-6
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "netstandard2.0", "netstandard2.0", "{3F0FEB1F-792D-49A5-AD31-7725B2C68BE6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core2.1", "core2.1", "{40BEC49D-5BDE-452F-8A54-59E7B95AC1D0}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core2.2", "core2.2", "{40BEC49D-5BDE-452F-8A54-59E7B95AC1D0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZigBeeNet.PlayGround", "Samples\core2.1\ZigBeeNet.PlayGround\ZigBeeNet.PlayGround.csproj", "{8D086719-5BFD-4AC3-812A-93543AF2CE34}"
EndProject
Expand Down
17 changes: 7 additions & 10 deletions src/ZigBeeNet/IeeeAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public ulong Value
{
return BitConverter.ToUInt64(_address, 0);
}
set
{
SetAddress(value);
}
}

/**
Expand Down Expand Up @@ -45,7 +49,7 @@ public IeeeAddress(string address) : this()
{
try
{
SetAddress((ulong)BigInteger.Parse(address, System.Globalization.NumberStyles.HexNumber));
SetAddress(ulong.Parse(address));
}
catch (FormatException e)
{
Expand Down Expand Up @@ -97,15 +101,8 @@ public override bool Equals(object obj)
}

public override string ToString()
{
StringBuilder builder = new StringBuilder();

for (int cnt = 7; cnt >= 0; cnt--)
{
builder.Append(string.Format("{0}", _address[cnt]));
}

return builder.ToString();
{
return Value.ToString();
}

public int CompareTo(IeeeAddress other)
Expand Down
Loading

0 comments on commit c87999c

Please sign in to comment.