static async Task Main(string[] args) { // Configure Serilog Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .CreateLogger(); bool showHelp = false; ZigBeeDongle zigBeeDongle = ZigBeeDongle.TiCc2531; OptionSet options = new OptionSet { { "h|help", "show this message and exit", h => showHelp = h != null }, { "zbd|zigbeeDongle=", "the zigbee dongle to use. 0 = TiCc2531 | 1 = DigiXBee", (ZigBeeDongle zbd) => zigBeeDongle = zbd } }; try { IList <string> extraArgs = options.Parse(args); foreach (string extraArg in extraArgs) { Console.WriteLine($"Error: Unknown option: {extraArg}"); showHelp = true; } Console.Write("Enter COM Port: "); string port = Console.ReadLine(); ZigBeeSerialPort zigbeePort = new ZigBeeSerialPort(port); IZigBeeTransportTransmit dongle; switch (zigBeeDongle) { case ZigBeeDongle.TiCc2531: { dongle = new ZigBeeDongleTiCc2531(zigbeePort); } break; case ZigBeeDongle.DigiXbee: { dongle = new ZigBeeDongleXBee(zigbeePort); } break; default: { dongle = new ZigBeeDongleTiCc2531(zigbeePort); } break; } ZigBeeNetworkManager networkManager = new ZigBeeNetworkManager(dongle); JsonNetworkSerializer deviceSerializer = new JsonNetworkSerializer("devices.json"); //networkManager.NetworkStateSerializer = deviceSerializer; ZigBeeDiscoveryExtension discoveryExtension = new ZigBeeDiscoveryExtension(); discoveryExtension.SetUpdatePeriod(60); networkManager.AddExtension(discoveryExtension); // Initialise the network networkManager.Initialize(); /* Network (de)serialization */ //networkManager.AddCommandListener(new ZigBeeNetworkDiscoverer(networkManager)); //networkManager.AddCommandListener(new ZigBeeNodeServiceDiscoverer(networkManager)); networkManager.AddCommandListener(new ZigBeeTransaction(networkManager)); networkManager.AddCommandListener(new ConsoleCommandListener()); networkManager.AddNetworkNodeListener(new ConsoleNetworkNodeListener()); networkManager.AddSupportedCluster(ZclOnOffCluster.CLUSTER_ID); networkManager.AddSupportedCluster(ZclColorControlCluster.CLUSTER_ID); networkManager.AddSupportedCluster(ZclTouchlinkCluster.CLUSTER_ID); networkManager.AddExtension(new ZigBeeBasicServerExtension()); if (zigBeeDongle == ZigBeeDongle.TiCc2531) { ((ZigBeeDongleTiCc2531)dongle).SetLedMode(1, false); // green led ((ZigBeeDongleTiCc2531)dongle).SetLedMode(2, false); // red led } ZigBeeStatus startupSucceded = networkManager.Startup(false); if (startupSucceded == ZigBeeStatus.SUCCESS) { Log.Logger.Information("ZigBee console starting up ... [OK]"); } else { Log.Logger.Information("ZigBee console starting up ... [FAIL]"); Log.Logger.Information("Press any key to exit..."); Console.ReadKey(); return; } ZigBeeNode coord = networkManager.GetNode(0); Console.WriteLine("Joining enabled..."); string cmd = string.Empty; while (cmd != "exit") { Console.WriteLine(networkManager.Nodes.Count + " node(s)" + Environment.NewLine); if (cmd == "join") { coord.PermitJoin(true); } else if (cmd == "unjoin") { coord.PermitJoin(false); } else if (!string.IsNullOrEmpty(cmd)) { var tmp = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkGreen; Console.Write("Destination Address: "); Console.ForegroundColor = tmp; string nwkAddr = Console.ReadLine(); if (ushort.TryParse(nwkAddr, out ushort addr)) { var node = networkManager.GetNode(addr); if (node != null) { 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 { if (cmd == "toggle") { await networkManager.Send(endpointAddress, new ToggleCommand()); } else if (cmd == "level") { Console.WriteLine("Level between 0 and 255: "); string level = Console.ReadLine(); Console.WriteLine("time between 0 and 65535: "); string time = Console.ReadLine(); var command = new MoveToLevelWithOnOffCommand() { Level = byte.Parse(level), TransitionTime = ushort.Parse(time) }; await networkManager.Send(endpointAddress, command); } else if (cmd == "move") { await networkManager.Send(endpointAddress, new MoveCommand() { MoveMode = 1, Rate = 100 }); } else if (cmd == "on") { await networkManager.Send(endpointAddress, new OnCommand()); } else if (cmd == "off") { await networkManager.Send(endpointAddress, new OffCommand()); } else if (cmd == "effect") { await networkManager.Send(endpointAddress, new OffCommand()); bool state = false; for (int i = 0; i < 10; i++) { if (state) { await networkManager.Send(endpointAddress, new OffCommand()); } else { await networkManager.Send(endpointAddress, new OnCommand()); } state = !state; await Task.Delay(1000); } } else if (cmd == "stress") { await networkManager.Send(endpointAddress, new OffCommand()); bool state = false; for (int i = 0; i < 100; i++) { if (state) { await networkManager.Send(endpointAddress, new OffCommand()); } else { await networkManager.Send(endpointAddress, new OnCommand()); } state = !state; await Task.Delay(1); } } else if (cmd == "desc") { NodeDescriptorRequest nodeDescriptorRequest = new NodeDescriptorRequest() { Destination = endpointAddress, NwkAddrOfInterest = addr }; networkManager.SendTransaction(nodeDescriptorRequest); } else if (cmd == "color") { Console.WriteLine("Red between 0 and 255: "); string r = Console.ReadLine(); Console.WriteLine("Green between 0 and 255: "); string g = Console.ReadLine(); Console.WriteLine("Blue between 0 and 255: "); string b = Console.ReadLine(); if (int.TryParse(r, out int _r) && int.TryParse(g, out int _g) && int.TryParse(b, out int _b)) { CieColor xyY = ColorConverter.RgbToCie(_r, _g, _b); MoveToColorCommand command = new MoveToColorCommand() { ColorX = xyY.X, ColorY = xyY.Y, TransitionTime = 10 }; await networkManager.Send(endpointAddress, command); } } else if (cmd == "hue") { Console.WriteLine("Red between 0 and 255: "); string hue = Console.ReadLine(); if (byte.TryParse(hue, out byte _hue)) { MoveToHueCommand command = new MoveToHueCommand() { Hue = _hue, Direction = 0, TransitionTime = 10 }; await networkManager.Send(endpointAddress, command); } } else if (cmd == "read") { var result = await((ZclElectricalMeasurementCluster)endpoint.GetInputCluster(ZclElectricalMeasurementCluster.CLUSTER_ID)).Read(ZclElectricalMeasurementCluster.ATTR_MEASUREMENTTYPE); if (result.IsSuccess()) { ReadAttributesResponse response = result.GetResponse <ReadAttributesResponse>(); if (response.Records.Count == 0) { Console.WriteLine("No records returned"); continue; } ZclStatus statusCode = response.Records[0].Status; if (statusCode == ZclStatus.SUCCESS) { Console.WriteLine("Cluster " + response + ", Attribute " + response.Records[0].AttributeIdentifier + ", type " + response.Records[0].AttributeDataType + ", value: " + response.Records[0].AttributeValue); } else { Console.WriteLine("Attribute value read error: " + statusCode); } } } } catch (Exception ex) { Log.Logger.Error(ex, "{Error}"); } } else { Console.WriteLine($"Node {addr} not found"); } } } var currentForeGroundColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkGreen; Console.Write("cmd> "); Console.ForegroundColor = currentForeGroundColor; cmd = Console.ReadLine(); } } catch (OptionException e) { Console.WriteLine(e.Message); showHelp = true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } if (showHelp) { Console.WriteLine("Options:"); options.WriteOptionDescriptions(Console.Out); return; } }
static void Main(string[] args) { // Configure Serilog Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .CreateLogger(); try { Console.Write("Enter COM Port: "); string port = Console.ReadLine(); ZigBeeSerialPort zigbeePort = new ZigBeeSerialPort(port); IZigBeeTransportTransmit dongle = new ZigBeeDongleTiCc2531(zigbeePort); ZigBeeNetworkManager networkManager = new ZigBeeNetworkManager(dongle); JsonNetworkSerializer deviceSerializer = new JsonNetworkSerializer("devices.json"); networkManager.NetworkStateSerializer = deviceSerializer; ZigBeeDiscoveryExtension discoveryExtension = new ZigBeeDiscoveryExtension(); discoveryExtension.SetUpdatePeriod(60); networkManager.AddExtension(discoveryExtension); // Initialise the network networkManager.Initialize(); networkManager.AddCommandListener(new ZigBeeNetworkDiscoverer(networkManager)); //networkManager.AddCommandListener(new ZigBeeNodeServiceDiscoverer(networkManager)); 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) { Log.Logger.Information("ZigBee console starting up ... [OK]"); } else { Log.Logger.Information("ZigBee console starting up ... [FAIL]"); return; } ZigBeeNode coord = networkManager.GetNode(0); Console.WriteLine("Joining enabled..."); string cmd = Console.ReadLine(); while (cmd != "exit") { Console.WriteLine(networkManager.Nodes.Count + " node(s)"); if (cmd == "join") { coord.PermitJoin(true); } else if (cmd == "unjoin") { coord.PermitJoin(false); } else if (!string.IsNullOrEmpty(cmd)) { Console.WriteLine("Destination Address: "); string nwkAddr = Console.ReadLine(); if (ushort.TryParse(nwkAddr, out ushort addr)) { var node = networkManager.GetNode(addr); if (node != null) { 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 { if (cmd == "toggle") { networkManager.Send(endpointAddress, new ToggleCommand()).GetAwaiter().GetResult(); } else if (cmd == "level") { Console.WriteLine("Level between 0 and 255: "); string level = Console.ReadLine(); Console.WriteLine("time between 0 and 65535: "); string time = Console.ReadLine(); var command = new MoveToLevelWithOnOffCommand() { Level = byte.Parse(level), TransitionTime = ushort.Parse(time) }; networkManager.Send(endpointAddress, command).GetAwaiter().GetResult(); } else if (cmd == "move") { networkManager.Send(endpointAddress, new MoveCommand() { MoveMode = 1, Rate = 100 }).GetAwaiter().GetResult(); } else if (cmd == "on") { networkManager.Send(endpointAddress, new OnCommand()).GetAwaiter().GetResult(); } else if (cmd == "off") { networkManager.Send(endpointAddress, new OffCommand()).GetAwaiter().GetResult(); } else if (cmd == "effect") { networkManager.Send(endpointAddress, new OffCommand()).GetAwaiter().GetResult(); bool state = false; for (int i = 0; i < 10; i++) { if (state) { networkManager.Send(endpointAddress, new OffCommand()).GetAwaiter().GetResult(); } else { networkManager.Send(endpointAddress, new OnCommand()).GetAwaiter().GetResult(); } state = !state; System.Threading.Thread.Sleep(1000); } } else if (cmd == "desc") { NodeDescriptorRequest nodeDescriptorRequest = new NodeDescriptorRequest() { DestinationAddress = endpointAddress, NwkAddrOfInterest = addr }; networkManager.SendTransaction(nodeDescriptorRequest); } else if (cmd == "color") { Console.WriteLine("Red between 0 and 255: "); string r = Console.ReadLine(); Console.WriteLine("Green between 0 and 255: "); string g = Console.ReadLine(); Console.WriteLine("Blue between 0 and 255: "); string b = Console.ReadLine(); if (int.TryParse(r, out int _r) && int.TryParse(g, out int _g) && int.TryParse(b, out int _b)) { CieColor xyY = ColorConverter.RgbToCie(_r, _g, _b); MoveToColorCommand command = new MoveToColorCommand() { ColorX = xyY.X, ColorY = xyY.Y, TransitionTime = 10 }; networkManager.Send(endpointAddress, command).GetAwaiter().GetResult(); } } else if (cmd == "hue") { Console.WriteLine("Red between 0 and 255: "); string hue = Console.ReadLine(); if (byte.TryParse(hue, out byte _hue)) { MoveToHueCommand command = new MoveToHueCommand() { Hue = _hue, Direction = 0, TransitionTime = 10 }; networkManager.Send(endpointAddress, command).GetAwaiter().GetResult(); } } else if (cmd == "read") { //var value = ((ZclOnOffCluster)endpoint.GetInputCluster(6)).GetAttribute(ZclOnOffCluster.ATTR_ONOFF); var result = ((ZclColorControlCluster)endpoint.GetInputCluster(ZclColorControlCluster.CLUSTER_ID)).Read(ZclColorControlCluster.ATTR_CURRENTY).Result; if (result.IsSuccess()) { ReadAttributesResponse response = result.GetResponse <ReadAttributesResponse>(); if (response.Records.Count == 0) { Console.WriteLine("No records returned"); continue; } ZclStatus statusCode = response.Records[0].Status; if (statusCode == ZclStatus.SUCCESS) { Console.WriteLine("Cluster " + string.Format("%04X", response.ClusterId) + ", Attribute " + response.Records[0].AttributeIdentifier + ", type " + response.Records[0].AttributeDataType + ", value: " + response.Records[0].AttributeValue); } else { Console.WriteLine("Attribute value read error: " + statusCode); } } } } catch (Exception ex) { Log.Logger.Error(ex, "{Error}"); } } else { Console.WriteLine($"Node {addr} not found"); } } } cmd = Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }