private static async System.Threading.Tasks.Task<HueClient> SaveApiKey(HueDataContext ctx) { HueClient client; var secret = GenerateApiKey(); client = HueHelper.GetClient(); var success = await client.RegisterAsync("Huemanatee", secret); if (!success) { throw new InvalidOperationException("Registering API key not successful."); } HueHelper.SetApiKey(secret); var setting = new Setting { Key = "HueApiKey", Value = secret }; ctx.Settings.Add(setting); ctx.SaveChanges(); client.Initialize(secret); return client; }
protected async override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines) { log4net.Config.XmlConfigurator.Configure(); await HueHelper.Init(); using (var ctx = new HueDataContext()) { var apiKey = ctx.Settings.SingleOrDefault(s => s.Key == "HueApiKey"); HueClient client; if (apiKey == null) { client = await SaveApiKey(ctx); } else { HueHelper.SetApiKey(apiKey.Value); client = HueHelper.GetClient(); } var lights = await client.GetLightsAsync(); var currentLights = ctx.Lights.ToList(); foreach (var light in lights) { var existingLight = currentLights.SingleOrDefault(c => c.HueId == light.Id); if (existingLight == null) { existingLight = new Data.Light { HueId = light.Id, Name = light.Name, }; ctx.Lights.Add(existingLight); } } ctx.SaveChanges(); } }
public IndexModule() { Get["/"] = parameters => { using (var ctx = new HueDataContext()) { var lights = ctx.Lights.ToList(); var model = new IndexModel { Lights = lights }; return View["index", model]; } }; Get["/webgl"] = parameters => { return View["webgl"]; }; }