/** * Updates {@link ZigBeeNode} and adds it to the {@link ZigBeeNetworkManager} * * @param ieeeAddress the {@link IeeeAddress} of the newly announced node * @param networkAddress the network address of the newly announced node */ private void AddNode(IeeeAddress ieeeAddress, ushort networkAddress) { ZigBeeNode node = _networkManager.GetNode(ieeeAddress); if (node != null) { if (node.NetworkAddress != networkAddress) { _logger.Debug("{IeeeAddress}: Network address updated to {NetworkAddress}", ieeeAddress, networkAddress); } node.NetworkAddress = networkAddress; _networkManager.UpdateNode(node); return; } node = new ZigBeeNode(_networkManager, ieeeAddress); node.NetworkAddress = networkAddress; // Add the node to the network... _networkManager.AddNode(node); }
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); } }
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 (cmd == "add") { 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)) { Console.Write("IeeeAddress: "); Console.ForegroundColor = tmp; string ieeeAddr = Console.ReadLine(); networkManager.AddNode(new ZigBeeNode() { NetworkAddress = addr, IeeeAddress = new IeeeAddress(ieeeAddr) }); } } 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 cluster = endpoint.GetInputCluster(ZclBasicCluster.CLUSTER_ID); if (cluster != null) { var result = await((ZclBasicCluster)cluster).GetManufacturerNameAsync(); 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; } }