/// <summary> /// If we would find any difference between any section it would already return /// something. so that mean that both IPs are the same. /// </summary> /// <param name="item1">The item1.</param> /// <param name="item2">The item2.</param> /// <returns></returns> public static int FoundDeviceExtension(FoundDevice item1, FoundDevice item2) { var ipOne = item1.IpAddress; var ipTwo = item2.IpAddress; string ip1 = ipOne + '.', ip2 = ipTwo + '.'; string xSection = "", ySection = ""; for (int i = 0; i < ip1.Length && i < ip2.Length; i++) { if (ip1[i] == '.' && ip2[i] == '.') { if (xSection != ySection) return int.Parse(xSection) - int.Parse(ySection); xSection = ""; // Start compare the next section ySection = ""; } else if (ip1[i] == '.') { return -1; } // The first section is smaller because it's length is smaller else if (ip2[i] == '.') { return 1; } else { xSection += ip1[i]; ySection += ip2[i]; } } return 0; }
public override TOutput BuildObject <TOutput, TInput>(TInput raw) { //try { var objs = raw as List <object>; var device = objs.FirstOrDefault() as DiscoveredSsdpDevice; objs.RemoveAt(0); var info = objs.FirstOrDefault() as SsdpDevice; var Uuid = (info.CustomProperties?.Count > 0 ? info.CustomProperties.FirstOrDefault(x => x.Name.Equals("X_hardwareId") || x.Name.Equals("deviceID"))?.Value : info.Uuid) ?? ""; var foundDevice = new FoundDevice { IpAddress = device.DescriptionLocation.DnsSafeHost, DeviceName = info.FriendlyName, DeviceId = Uuid.Replace("-", "") ?? Utils.Common, FoundUsing = "SSdp", FoundAt = device.AsAt.LocalDateTime }; return((TOutput)Convert.ChangeType(foundDevice, foundDevice.GetType())); //}catch(NullReferenceException ex){Console.WriteLine(ex.Message);throw ex;} }
/// <summary> /// Searches the specified IP Address. /// //string IP, string term) /// </summary> public async Task Search() { try { var foundDevices = await deviceLocator.SearchAsync().ConfigureAwait(false); foreach (DiscoveredSsdpDevice device in foundDevices.ToList()) { SsdpDevice info = await device.GetDeviceInfo().ConfigureAwait(false); var fd = new FoundDevice { IpAddress = device.DescriptionLocation.DnsSafeHost, DeviceName = info.FriendlyName, DeviceId = info.Uuid.Replace("-", ""), FoundUsing = "SSdp", FoundAt = device.AsAt.DateTime }; FoundDeviceCollection.Add(fd); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"\t{ex.Message}"); Console.ResetColor(); } }
public Result FindValue(FoundDevice device, string Property) { try { List <Task <Result> > tasks = new List <Task <Result> >(); foreach (var factory in Factories) { try { #region Switch cases switch (factory.ServiceName) { case ServiceEnum.DEVICENAME: break; case ServiceEnum.WMI: break; case ServiceEnum.SNMP: break; case ServiceEnum.SSH: factory.SetCommands(new List <object> { "cat /proc/cpuinfo" }); break; case ServiceEnum.WizBulb: break; } #endregion tasks.Add(factory.FindValue(device.IpAddress, Property)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } Task.WaitAll(tasks.ToArray()); var deviceResults = Task.WhenAll(tasks.ToList()).GetAwaiter().GetResult().Where(x => x.Value != null); var found = deviceResults.Where(x => !x.Value.Equals(Utils.Common)).ToList(); return((found.Count.Equals(0)) ? new Result() { Value = Utils.Common } : found.FirstOrDefault()); } catch (Exception) { return(new Result() { Value = Utils.Common }); } }
public virtual T BuildObject <T>(string ip) { var fd = new FoundDevice { IpAddress = ip, DeviceId = Utils.Common, DeviceName = Utils.Common, FoundAt = DateTime.Now, FoundUsing = Utils.Common }; return((T)Convert.ChangeType(fd, fd.GetType())); }
public override TOutput BuildObject <TOutput, TInput>(TInput raw) { var itm = raw as List <object>; var device = itm.FirstOrDefault() as DiscoveredSsdpDevice; var info = itm.LastOrDefault() as SsdpDevice; var foundDevice = new FoundDevice { DeviceId = info.Uuid.Replace("-", ""), DeviceName = info.FriendlyName, FoundAt = device.AsAt.LocalDateTime, IpAddress = device.DescriptionLocation.DnsSafeHost, FoundUsing = "UPNP" }; return((TOutput)Convert.ChangeType(foundDevice, foundDevice.GetType())); }
/// <summary> /// Lookup Device ID /// </summary> /// <param name="device"></param> /// <returns></returns> public override Result FindDeviceId(FoundDevice device) { const string Property = "Serial"; Factories = new List <Factory.ARpcFactory>() { Factory.LoadFactory(ServiceEnum.SSH), Factory.LoadFactory(ServiceEnum.WMI), }; try { foreach (var factory in Factories) { if (factory.ServiceName.Equals(ServiceEnum.SSH)) { factory.SetCommands(new List <object> { "cat /proc/cpuinfo" }); } var result = factory.FindValue(device.IpAddress, Property) .GetAwaiter().GetResult(); if (result.Value != Utils.Common) { Console.WriteLine($"{Property} :: {factory.ServiceName} | {device.IpAddress} | {result.Value}"); return(new Result { Value = result.Value, FoundAt = factory.ServiceName.ToString() }); } } Console.WriteLine($"{Property} :: Factory Found no value. | {device.IpAddress} | {Utils.Common}"); return(new Result { Value = Utils.Common }); } catch (Exception) { return(new Result { Value = Utils.Common, FoundAt = Utils.Common }); } }
/// <summary> /// Runs the ping. /// </summary> public void RunPing() { //nFound = 0; List <Task <PingReply> > tasks = new List <Task <PingReply> >(); stopWatch.Start(); foreach (var ip in ipAddresses) { tasks.Add(PingAndUpdateAsync(new Ping(), ip)); } Task.WaitAll(tasks.ToArray()); var reachable = tasks.Where(x => x.Result?.Status == IPStatus.Success).ToList(); var cnt = 1; foreach (var result in reachable) { var reply = result.Result; var fd = new FoundDevice { IpAddress = reply.Address.ToString(), DeviceId = Utils.Common, DeviceName = Utils.Common, Id = cnt, FoundAt = DateTime.Now, FoundUsing = "Ping" }; var args = new DeviceFoundEventArgs { Found = cnt, TimeReached = DateTime.Now, Item = fd }; OnDeviceReached(args); cnt++; } }
public override void OnReceive(Context context, Intent intent) { var action = intent.Action; // Found a device switch (action) { case BluetoothDevice.ActionFound: // Get the device var device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice); // Only update the adapter with items which are not bonded if (device.BondState != Bond.Bonded) { FoundDevice?.Invoke(new DeviceInfo(device.Name, device.Address)); } break; default: break; } }
/// <summary> /// Lookup device names based on provided object. /// </summary> /// <param name="device"></param> /// <returns></returns> public override Result FindDeviceName(FoundDevice device) { const string Property = "SYSTEMNAME"; Factories = new List <Factory.ARpcFactory> { Factory.LoadFactory(ServiceEnum.DEVICENAME), Factory.LoadFactory(ServiceEnum.SNMP) }; try { foreach (var f in Factories) { var result = f.FindValue(device.IpAddress, Property).GetAwaiter().GetResult(); if (result.Value != Utils.Common) { Console.WriteLine($"{Property} :: {f.ServiceName} | {device.IpAddress} | {result.Value}"); return(new Result { Value = result.Value, FoundAt = f.ServiceName.ToString() }); } } Console.WriteLine($"{Property} :: Factory Found no value. | {device.IpAddress} | {Utils.Common}"); return(new Result { Value = Utils.Common }); } catch (Exception ex) { Console.WriteLine(ex.Message); return(new Result { Value = Utils.Common, FoundAt = Utils.Common }); } }
public virtual Result FindDeviceId(FoundDevice device) => null;