Exemple #1
0
        public async Task RefreshAsync()
        {
            if (needsLoad)
            {
                needsLoad = false;
                await LoadOldDataAsync();
            }
            var httpClient = new HttpClient();

            using var ssdpClient = new Discovery.SSDP.Agents.ClientAgent();
            var services = await Task.Run(() => ssdpClient.Discover(serviceType));

            if (services == null)
            {
                return;
            }
            var lightServices =
                (from s in services
                 where s.ServiceType == serviceType
                 select s).ToArray();
            var discoveredDevices = new List <DeviceInfo>();

            foreach (var s in lightServices)
            {
                var descriptorUrl = new Uri(s.Location);
                var apiUrl        = $"http://{descriptorUrl.Host}:{descriptorUrl.Port}/api?lights=0";
                var apiRawResult  = await httpClient.GetStringAsync(apiUrl);

                var apiResult = JsonConvert.DeserializeObject <Dictionary <string, LightInfo> >(apiRawResult);
                foreach (var kv in apiResult)
                {
                    discoveredDevices.Add(new DeviceInfo
                    {
                        FriendlyName = kv.Value.Name,
                        LightId      = kv.Key,
                        Host         = descriptorUrl.Host,
                        Port         = descriptorUrl.Port,
                        Enabled      = true,
                    });
                }
            }

            discoveredDevices.Sort((a, b) => a.UniqueKey.CompareTo(b.UniqueKey));

            lock (devicesLock)
            {
                // var diff = Devices.Diff(discoveredDevices);
                Devices.MergeInto(discoveredDevices,
                                  match: (a, b) => a.UniqueKey == b.UniqueKey,
                                  create: (b) => b,
                                  update: (source, destination) => destination.SyncFrom(source),
                                  delete: (a) => { });
            }

            RefreshTime = DateTime.Now;
            SetNeedsSave();

            CollectionChanged?.Invoke(this, EventArgs.Empty);
        }
Exemple #2
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            _Client = new Discovery.SSDP.Agents.ClientAgent();

            _Client.AnnounceReceived  += _Client_AnnounceReceived;
            _Client.ByeReceived       += _Client_ByeReceived;
            _Client.DiscoveryReceived += _Client_DiscoveryReceived;
        }
Exemple #3
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            _Client = new Discovery.SSDP.Agents.ClientAgent();

            _Client.AnnounceReceived += _Client_AnnounceReceived;
            _Client.ByeReceived += _Client_ByeReceived;
            _Client.DiscoveryReceived += _Client_DiscoveryReceived;
        }