Example #1
0
        private async Task <IEnumerable <Channel> > ScanChannel(int channelId, ControlClient client)
        {
            await Task.Yield();

            var frequency = ChannelMapper.GetFrequency(channelId);

            if (frequency == null)
            {
                return(Enumerable.Empty <Channel>());
            }

            Console.WriteLine($"Scanning {client.Host}:{client.Port} tuner {client.Tuner} for channel {channelId} ({frequency:#.00}Mhz)...");

            await client.SetFrequency(frequency.Value);

            var token = new CancellationTokenSource(TimeSpan.FromSeconds(5));

            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    return(Enumerable.Empty <Channel>());
                }

                var status = await client.GetStatus();

                if (status.Modulation == CommandValues.None)
                {
                    continue;
                }

                break;
            }

            token = new CancellationTokenSource(TimeSpan.FromSeconds(10));

            while (true)
            {
                var info = await client.GetStreamInfo();

                if (!token.IsCancellationRequested && (info.Channels.Count == 0 || info.IsWaitingForData))
                {
                    continue;
                }

                var results = info.Channels
                              .Where(c => !c.IsEncrypted && !c.IsControl && !c.IsNoData && !string.IsNullOrEmpty(c.DisplayChannel))
                              .Select(c => new Channel(channelId, frequency.Value, c.Program, c.DisplayChannel, c.Name))
                              .ToList();

                if (results.Any())
                {
                    Console.WriteLine($"Found {results.Count} virtual channels on {client.Host}:{client.Port} tuner {client.Tuner} for channel {channelId} ({frequency:#.00}Mhz).");
                }

                return(results);
            }
        }