private void Receive(IAsyncResult ar) { try { IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER); byte[] bytes = udp.EndReceive(ar, ref ip); var message = Encoding.ASCII.GetString(Messaging.SmartHomeProtocolEncoder.Decrypt(bytes)); var sys_info = ((dynamic)JObject.Parse(message)).system.get_sysinfo; TPLinkSmartDevice device = null; if (((string)sys_info.model).StartsWith("HS")) { device = new TPLinkSmartPlug(ip.Address.ToString()); } else if (((string)sys_info.model).StartsWith("LB")) { device = new TPLinkSmartBulb(ip.Address.ToString()); } if (device != null) { DiscoveredDevices.Add(device); } } catch (Exception ex) { } if (udp != null) { StartListening(); } }
private async Task Receive() { while (true) { if (discoveryComplete) //Prevent ObjectDisposedException/NullReferenceException when the Close() function is called { return; } IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER); UdpReceiveResult result = await udp.ReceiveAsync().ConfigureAwait(false); ip = result.RemoteEndPoint; var message = Encoding.ASCII.GetString(Messaging.SmartHomeProtocolEncoder.Decrypt(result.Buffer)); TPLinkSmartDevice device = null; try { dynamic sys_info = ((dynamic)JObject.Parse(message)).system.get_sysinfo; string model = (string)sys_info.model; if (model != null) { if (model.StartsWith("HS110")) { device = await TPLinkSmartMeterPlug.Create(ip.Address.ToString()).ConfigureAwait(false); } else if (model.StartsWith("HS300") || model.StartsWith("KP303") || model.StartsWith("HS107")) { device = await TPLinkSmartMultiPlug.Create(ip.Address.ToString()).ConfigureAwait(false); } else if (model.StartsWith("HS220")) { device = await TPLinkSmartDimmer.Create(ip.Address.ToString()).ConfigureAwait(false); } else if (model.StartsWith("HS")) { device = await TPLinkSmartPlug.Create(ip.Address.ToString()).ConfigureAwait(false); } else if (model.StartsWith("KL") || model.StartsWith("LB")) { device = await TPLinkSmartBulb.Create(ip.Address.ToString()).ConfigureAwait(false); } } } catch (RuntimeBinderException) { //discovered wrong device } if (device != null) { DiscoveredDevices.Add(device); OnDeviceFound(device); } } }
private void Receive(IAsyncResult ar) { if (discoveryComplete) //Prevent ObjectDisposedException/NullReferenceException when the Close() function is called { return; } IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER); byte[] bytes = udp.EndReceive(ar, ref ip); var message = Encoding.UTF8.GetString(Messaging.SmartHomeProtocolEncoder.Decrypt(bytes)); var sys_info = ((dynamic)JObject.Parse(message)).system.get_sysinfo; TPLinkSmartDevice device = null; if (((JObject)sys_info).Count != 0 && sys_info.model != null) { string model = (string)sys_info.model; if (model.StartsWith("HS110")) { device = new TPLinkSmartMeterPlug(ip.Address.ToString()); } else if (model.StartsWith("HS")) { device = new TPLinkSmartPlug(ip.Address.ToString()); } else if (model.StartsWith("LB")) { device = new TPLinkSmartBulb(ip.Address.ToString()); } else if (model.StartsWith("KP303")) { device = new TPLinkSmartStrip(ip.Address.ToString()); } } if (device != null) { DiscoveredDevices.Add(device); OnDeviceFound(device); // If the caller has specified a maximum number of devices to discover, stop waiting after we've // reached this count if (maxNumberOfDevicesToDiscover > 0 && DiscoveredDevices.Count >= maxNumberOfDevicesToDiscover) { delayCancellationToken.Cancel(); } } if (udp != null) { StartListening(); } }
static void Main(string[] args) { var smartPlug = new TPLinkSmartPlug("HS100"); var smartBulb = new TPLinkSmartBulb("LB130"); if (!smartBulb.PoweredOn) { //smartBulb.PoweredOn = false; } if (!smartPlug.OutletPowered) { //smartPlug.OutletPowered = false; } Console.WriteLine($@"Device Name: { smartBulb.Alias }"); Console.WriteLine($@"Device Status: { smartBulb.PoweredOn }"); Console.WriteLine(); Console.WriteLine($@"Device Name: { smartPlug.Alias }"); Console.WriteLine($@"Device Status: { smartPlug.OutletPowered }"); Console.WriteLine(); Console.ReadLine(); if (smartBulb.PoweredOn) { //smartBulb.PoweredOn = true; } if (smartPlug.OutletPowered) { //smartPlug.OutletPowered = true; } Console.WriteLine($@"Device Name: { smartBulb.Alias }"); Console.WriteLine($@"Device Status: { smartBulb.PoweredOn }"); Console.WriteLine(); Console.WriteLine($@"Device Name: { smartPlug.Alias }"); Console.WriteLine($@"Device Status: { smartPlug.OutletPowered }"); Console.WriteLine(); Console.ReadLine(); }
private void Receive(IAsyncResult ar) { if (discoveryComplete) //Prevent ObjectDisposedException/NullReferenceException when the Close() function is called { return; } IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER); byte[] bytes = udp.EndReceive(ar, ref ip); var message = Encoding.ASCII.GetString(Messaging.SmartHomeProtocolEncoder.Decrypt(bytes)); var sys_info = ((dynamic)JObject.Parse(message)).system.get_sysinfo; TPLinkSmartDevice device = null; string model = (string)sys_info.model; if (model.StartsWith("HS110")) { device = new TPLinkSmartMeterPlug(ip.Address.ToString()); } else if (model.StartsWith("HS")) { device = new TPLinkSmartPlug(ip.Address.ToString()); } else if (model.StartsWith("LB")) { device = new TPLinkSmartBulb(ip.Address.ToString()); } if (device != null) { DiscoveredDevices.Add(device); } if (udp != null) { StartListening(); } }