/// <inheritdoc/> public async Task <IList <ElgatoLight> > StartDiscoverAsync(int timeout = 30, CancellationToken cancellationToken = default) { var lights = new List <ElgatoLight>(); if (NetworkDiscoveryHelper.IsNetworkAvailable() == false) { return(lights); } // discover the set of available lights ResolveOptions elgatoKeyOptions = new ResolveOptions(BonjourDiscoveryElgatoKeyLightService) { //ScanTime = TimeSpan.FromSeconds(timeout) }; /* * ResolveOptions eveKeyOptions = new ResolveOptions(ElgatoEveKeyLightService) * { * //ScanTime = TimeSpan.FromSeconds(timeout) * }; */ IReadOnlyList <IZeroconfHost> elgatoDiscoveredServices = await ZeroconfResolver.ResolveAsync(elgatoKeyOptions, callback : null, cancellationToken); // IReadOnlyList<IZeroconfHost> eveDiscoveredServices = await ZeroconfResolver.ResolveAsync(eveKeyOptions, callback: null, cancellationToken); if (elgatoDiscoveredServices != null) { // for each light that is discovered, retrieve details about the keylight by calling the device directly foreach (var resolvedElgatoLightService in elgatoDiscoveredServices) { var light = await RetrieveKeyLightInfoAsync(resolvedElgatoLightService.IPAddress, resolvedElgatoLightService.DisplayName, resolvedElgatoLightService.Services.FirstOrDefault().Value.Port); if (light != null) { await light.UpdateStatusAsync(); await light.UpdateSettings(); lights.Add(light); } } } /* if (eveDiscoveredServices != null) * { * // for each light that is discovered, retrieve details about the keylight by calling the device directly * foreach (var resolvedEveLightService in eveDiscoveredServices) * { * var light = await RetrieveKeyLightInfoAsync(resolvedEveLightService.IPAddress, resolvedEveLightService.DisplayName, resolvedEveLightService.Services.FirstOrDefault().Value.Port); * * //lights.Add(light); * } * } * */ return(lights); }
private async Task <string> UpdateDevice(string endPoint, string lightStatusJson) { if (string.IsNullOrWhiteSpace(endPoint)) { throw new ArgumentException($"'{nameof(endPoint)}' cannot be null or whitespace", nameof(endPoint)); } if (string.IsNullOrWhiteSpace(lightStatusJson)) { throw new ArgumentException($"'{nameof(lightStatusJson)}' cannot be null or whitespace", nameof(lightStatusJson)); } if (NetworkDiscoveryHelper.IsNetworkAvailable() == false) { return(string.Empty); } using (var client = new HttpClient()) { using (var content = new StringContent(lightStatusJson, Encoding.UTF8)) { content.Headers.Remove("Content-Type"); content.Headers.Add("Content-Type", "application/json"); using (var req = new HttpRequestMessage(HttpMethod.Put, endPoint)) { req.Content = content; // Ignore Certificate validation failures (aka untrusted certificate + certificate chains) ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; using (HttpResponseMessage resp = await client.SendAsync(req)) { resp.EnsureSuccessStatusCode(); return(await resp.Content.ReadAsStringAsync()); } } } } }
internal async Task UpdateStatusAsync() { var statusEndPoint = string.Format(StatusInfoEndPointTemplate, Address, Port); if (NetworkDiscoveryHelper.IsNetworkAvailable() == false) { return; } using (var client = new HttpClient()) { var streamTask = client.GetStreamAsync(statusEndPoint); LightsStatus status = await JsonSerializer.DeserializeAsync <LightsStatus>(await streamTask); if (status != null && status.Lights != null && status.Lights.Count > 0) { On = status.Lights[0].On; Brightness = status.Lights[0].Brightness; Temperature = status.Lights[0].Temperature; } } }