static async System.Threading.Tasks.Task Main(string[] args)
 {
     await Parser.Default.ParseArguments <Options>(args)
     .WithParsedAsync <Options>(async o =>
     {
         if (o.Mode == Mode.FindDevices)
         {
             var devices = await Discovery.DiscoverDevices();
             foreach (var device in devices)
             {
                 System.Console.WriteLine("DEVICE FOUND : ");
                 System.Console.WriteLine($"Name : { device.Name}");
                 System.Console.WriteLine($"Id : { device.Id}");
                 System.Console.WriteLine($"Properties: { string.Join(",", device.Properties.Select(p => $"{p.Key}={p.Value}"))}");
                 System.Console.WriteLine();
             }
         }
         else if (o.Mode == Mode.FindDevicesAndGetDatas)
         {
             var devices = await Discovery.DiscoverDevices();
             foreach (var device in devices)
             {
                 var datas = await Discovery.GetData(device);
                 System.Console.WriteLine("DEVICE FOUND : ");
                 System.Console.WriteLine($"Name : { device.Name}");
                 System.Console.WriteLine($"Id : { device.Id}");
                 System.Console.WriteLine(datas);
                 System.Console.WriteLine();
             }
         }
         else if (o.Mode == Mode.FindDevicesAndWatchDatas)
         {
             var devices = await Discovery.DiscoverDevices();
             foreach (var device in devices)
             {
                 await Discovery.Subscribe(device, new Progress <MiDeviceData>(d =>
                 {
                     System.Console.WriteLine($"Name : { device.Name}");
                     System.Console.WriteLine($"Id : { device.Id}");
                     System.Console.WriteLine(d);
                     System.Console.WriteLine();
                 }));
             }
         }
         else if (o.Mode == Mode.WatchDevicesAndDatas)
         {
             //todo : write watchdevice method
         }
         else if (o.Mode == Mode.GetDatasForDevice && !string.IsNullOrWhiteSpace(o.DeviceId))
         {
             var datas = await Discovery.GetData(o.DeviceId);
             System.Console.WriteLine(datas);
             System.Console.WriteLine();
         }
         else if (o.Mode == Mode.WatchDatasForDevice && !string.IsNullOrWhiteSpace(o.DeviceId))
         {
             //todo : write watchdevice method
         }
     });
 }
Пример #2
0
        public bool Initialize()
        {
            var conn  = new Connection();
            var disco = new Discovery();
            //_log.Trace("Discovering...");
            var devices = disco.DiscoverDevices();

            if (!devices.Any())
            {
                //_log.Error("No BT devices discovered");
                throw new NoDiscoveredDevicesException("No Bluetooth devices found");
            }

            var disc       = default(BluetoothDeviceInfo);
            var targetName = ConfigurationManager.AppSettings["TargetName"];

            if (!devices.Where(d => d.DeviceName.Equals(targetName)).Any())
            {
                foreach (var device in devices)
                {
                    if (disco.GetDeviceName(device).Equals(targetName))
                    {
                        disc = device;
                    }
                }
            }
            else
            {
                disc = devices.Where(d => d.DeviceName.Equals(targetName)).First();
            }
            if (disc == default(BluetoothDeviceInfo))
            {
                throw new NoTargetFoundException("No device found");
            }

            // _log.Trace("Connecting");
            try
            {
                conn.Connect(disc);
            }
            catch (Exception e)
            {
                //_log.ErrorException("Can't connect to disc", e);
                throw new ConnectionFailedException("Failed to connect" + e);
            }

            return(true);
        }
Пример #3
0
        TestStepResult ITestStep.Execute(ref ConnectionInfo connectionInfo)
        {
            var result  = new TestStepResult();
            var disco   = new Discovery();
            var devices = disco.DiscoverDevices();

            if (!devices.Any())
            {
                _log.Error("No BT devices discovered");
                result.Passed = false;
                //throw new NoDiscoveredDevicesException("No Bluetooth devices found");
            }
            connectionInfo.DiscoveredDevices = devices;
            result.Passed = true;
            return(result);
        }