Пример #1
0
        private static async Task <string> SetUpIP()
        {
            string lipAddress = ipAddress;

            if (lipAddress == null)
            {
                Q42.HueApi.HttpBridgeLocator b = new Q42.HueApi.HttpBridgeLocator();
                var result = await b.LocateBridgesAsync(TimeSpan.FromSeconds(10));

                if (result != null)
                {
                    lipAddress = result.FirstOrDefault();
                }
            }

            if (lipAddress == null)
            {
                try
                {
                    Q42.HueApi.Interfaces.IBridgeLocator locator = new Q42.HueApi.WinRT.SSDPBridgeLocator();
                    var result = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(15));

                    if (result != null)
                    {
                        lipAddress = result.FirstOrDefault();
                    }
                }
                catch { }
            }

            return(lipAddress);
        }
Пример #2
0
        public override int Run(string[] remainingArguments)
        {
            if (_scan)
            {
                //TODO: Cant really assume /24 subnet...
                if (_ip != null && _ip.Split('.').Count() != 4)
                {
                    throw new ConsoleHelpAsException("Please provide valid IP address");
                }

                var initialIp = string.IsNullOrEmpty(_ip) ? GetIpAddresss() : _ip;
                var ip = String.Join(".",initialIp.Split('.').Take(3));
                Parallel.ForEach(Enumerable.Range(0, 254), (lastByte, state) =>
                    {
                        try
                        {
                            var testHost = string.Format("{0}.{1}", ip, lastByte);
                            var hostResource = String.Format("http://{0}/debug/clip.html", testHost);
                            using (var client = new WebClient())
                            {
                                client.DownloadString(hostResource);
                                Console.WriteLine("Found host at: {0}", testHost);
                                state.Break();
                            }
                        }
                        catch (WebException)
                        {
                        }
                    });
                Console.WriteLine("Failed to find any bridges via scan.  Ensure bridge is switched on and discoverable on your network segment.");
                return 1;
            }

            try
            {
                var locator = new HttpBridgeLocator();
                var result = locator.LocateBridgesAsync(new TimeSpan(0, 0, GetTimeoutValue())).Result.ToList();
                if (result.Any())
                {
                    Console.WriteLine("Found {0} bridges:", result.Count);
                    Console.WriteLine();
                    foreach (var bridge in result)
                    {
                        Console.WriteLine("\t{0}", bridge);
                    }
                    return 0;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not locate bridges due to error: {0} : {1}", e.Message, e.InnerException != null ? e.InnerException.Message : "");
                throw;
            }
            Console.WriteLine("Failed to find any bridges.  Ensure bridge Has internet access to register.");
            return 1;
        }
Пример #3
0
    public static async Task<string> Init()
    {
      var task = new Q42.HueApi.HttpBridgeLocator().LocateBridgesAsync(TimeSpan.FromMinutes(1));

      var bridges = await task;

      _ip = bridges.Single();

      return _ip;
    }
Пример #4
0
        private async Task<string> GetBridgeIP()
        {
            while (true)
            {
                var locator = new HttpBridgeLocator();
                IEnumerable<string> bridgeIPs = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(5));

                if (bridgeIPs.Count() > 0)
                {
                    return bridgeIPs.First();
                }

                var md = new MessageDialog("Make sure the Hue bridge is on and on the same WiFi network as your phone", "Bridge not found");
                md.Commands.Add(new UICommand("Retry"));
                await md.ShowAsync();
            }
        }
Пример #5
0
        private static string FindIp()
        {
            var locator = new HttpBridgeLocator();
            var ips = locator.LocateBridgesAsync(TimeSpan.FromSeconds(5)).Result.ToArray();

            if (ips.Length == 0)
            {
                throw new HomeAutomationException("No Hue bridge was found");
            }

            if (ips.Length > 1)
            {
                throw new HomeAutomationException("Multiple Hue bridges found.  One must be explicitly specified.");
            }

            var result = ips.First();

            return result;
        }
Пример #6
0
		protected override async void OnStart ()
		{

            try
            {
                var locator = new HttpBridgeLocator();
                var bridgeIPs = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(5));

                // Handle when your app starts
                ILocalHueClient client = new LocalHueClient("ip");
                var appKey = await client.RegisterAsync("mypersonalappname", "mydevicename");
                client.Initialize("mypersonalappkey");
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }


        }
Пример #7
0
		private async Task<string> LocateBridgeIP()
		{
			IBridgeLocator bridgeLocator = new HttpBridgeLocator();
			var bridgeIPs = await bridgeLocator.LocateBridgesAsync(TimeSpan.FromSeconds(5));
			return bridgeIPs.FirstOrDefault();
		}
		async void TryToConnectToBridge ()
		{
			IBridgeLocator locator = new HttpBridgeLocator ();
			IEnumerable<string> bridgeIPs = await locator.LocateBridgesAsync (TimeSpan.FromSeconds (5));

			var db = new SQLite.SQLiteConnection (_pathToDatabase);
			try {
				var bridgeIp = db.Table<HueBridge> ().ToArray ();
				var appKey = db.Table<HueAppKey> ().ToArray ();

				client = new LocalHueClient (bridgeIp [0].HueBridgeIpAddress);
				client.Initialize (appKey [0].AppId);
			} catch {
				var alert = new UIAlertView ("Hangon!", "First, press the button on the Hue bridge. Then tap 'Connect' in the app.", null, "OK");		
				alert.Show ();		
				Console.WriteLine ("First, connect to bridge");
			}
		}
		async void ConnectBridgeClicked (object sender, EventArgs e)
		{
			IBridgeLocator locator = new HttpBridgeLocator ();
			IEnumerable<string> bridgeIPs = await locator.LocateBridgesAsync (TimeSpan.FromSeconds (5));

			IpAddress = bridgeIPs.FirstOrDefault ();

			client = new LocalHueClient (IpAddress);

			var appKey = new HueAppKey{ AppId = await client.RegisterAsync ("pooberry", "iphone") };
			var bridgeIp = new HueBridge { HueBridgeIpAddress = IpAddress };

			var conn = new SQLite.SQLiteConnection (_pathToDatabase);
			//set up bridge table
			conn.CreateTable<HueBridge> ();
			conn.DeleteAll<HueBridge> ();

			//set up app key table
			conn.CreateTable<HueAppKey> ();
			conn.DeleteAll<HueAppKey> ();

			//insert app key and bridge ip into database
			var db = new SQLite.SQLiteConnection (_pathToDatabase);
			db.Insert (bridgeIp);
			db.Insert (appKey);

			var alert = new UIAlertView ("Success!", "Feel free to play with your lights!", null, "OK");		
			alert.Show ();	
		}