Exemplo n.º 1
0
        public static bool IsUserOnSameChannel(IChannel tuningDetail, TvBusinessLayer layer, DVBBaseChannel userDVBchannel)
        {
            bool isUserOnSameChannel = false;

            if (userDVBchannel != null)
            {
                var currentDVBchannel = tuningDetail as DVBBaseChannel;
                if (currentDVBchannel != null)
                {
                    TuningDetail currentDVBtuningDetail = layer.GetTuningDetail(currentDVBchannel);
                    TuningDetail userDVBtuningDetail    = layer.GetTuningDetail(userDVBchannel);
                    isUserOnSameChannel = (currentDVBtuningDetail != null && currentDVBtuningDetail.IdChannel == userDVBtuningDetail.IdChannel);
                }
            }
            return(isUserOnSameChannel);
        }
        private Channel IsInsertAllowed(EpgChannel epgChannel)
        {
            DVBBaseChannel dvbChannel = (DVBBaseChannel)epgChannel.Channel;

            //are there any epg infos for this channel?
            if (epgChannel.Programs.Count == 0)
            {
                Log.Epg("{0}: no epg infos found for channel networkid:0x{1:X} transportid:0x{2:X} serviceid:0x{3:X}",
                        _grabberName, dvbChannel.NetworkId, dvbChannel.TransportId, dvbChannel.ServiceId);
                return(null);
            }
            //do we know a channel with these tuning details?
            Channel      dbChannel    = null;
            TuningDetail tuningDetail = _layer.GetTuningDetail(dvbChannel);

            if (tuningDetail != null)
            {
                dbChannel = tuningDetail.ReferencedChannel();
            }

            if (dbChannel == null)
            {
                Log.Epg("{0}: no channel found for networkid:0x{1:X} transportid:0x{2:X} serviceid:0x{3:X}", _grabberName,
                        dvbChannel.NetworkId, dvbChannel.TransportId, dvbChannel.ServiceId);

                /*foreach (EpgProgram ei in epgChannel.Programs)
                 * {
                 * string title = "";
                 * if (ei.Text.Count > 0)
                 * {
                 *  title = ei.Text[0].Title;
                 * }
                 * Log.Epg("                   -> {0}-{1}  {2}", ei.StartTime, ei.EndTime, title);
                 * }*/
                return(null);
            }
            //should we store epg for this channel?
            if ((dbChannel.IsRadio && _storeOnlySelectedChannelsRadio) || (!dbChannel.IsRadio && _storeOnlySelectedChannels))
            {
                if (!dbChannel.GrabEpg)
                {
                    Log.Epg("{0}: channel {1} is not configured to grab epg.", _grabberName, dbChannel.DisplayName);
                    return(null);
                }
            }
            if (_checkForLastUpdate)
            {
                //is the regrab time reached?
                TimeSpan ts = DateTime.Now - dbChannel.LastGrabTime;
                if (ts.TotalMinutes < _epgReGrabAfter)
                {
                    Log.Epg("{0}: {1} not needed lastUpdate:{2}", _grabberName, dbChannel.DisplayName, dbChannel.LastGrabTime);
                    return(null);
                }
            }
            return(dbChannel);
        }
Exemplo n.º 3
0
        private void DoScan()
        {
            int tvChannelsNew        = 0;
            int radioChannelsNew     = 0;
            int tvChannelsUpdated    = 0;
            int radioChannelsUpdated = 0;

            IUser user = new User();

            user.CardId = _cardNumber;
            try
            {
                // First lock the card, because so that other parts of a hybrid card can't be used at the same time
                RemoteControl.Instance.EpgGrabberEnabled = false;

                TvBusinessLayer layer = new TvBusinessLayer();
                Card            card  = layer.GetCardByDevicePath(RemoteControl.Instance.CardDevice(_cardNumber));

                int index = -1;
                IEnumerator <PlayListItem> enumerator = playlist.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    string url  = enumerator.Current.FileName.Substring(enumerator.Current.FileName.LastIndexOf('\\') + 1);
                    string name = enumerator.Current.Description;

                    DVBIPChannel tuneChannel = new DVBIPChannel();
                    tuneChannel.Url  = url;
                    tuneChannel.Name = name;
                    string line = String.Format("{0}- {1} - {2}", 1 + index, tuneChannel.Name, tuneChannel.Url);
                    Log.Debug(line);

                    RemoteControl.Instance.Tune(ref user, tuneChannel, -1);
                    IChannel[] channels;
                    channels = RemoteControl.Instance.Scan(_cardNumber, tuneChannel);

                    if (channels == null || channels.Length == 0)
                    {
                        if (RemoteControl.Instance.TunerLocked(_cardNumber) == false)
                        {
                            line = String.Format("{0}- {1} - {2} :No Signal", 1 + index, tuneChannel.Url, tuneChannel.Name);
                            Log.Debug(line);
                            continue;
                        }
                        else
                        {
                            line = String.Format("{0}- {1} - {2} :Nothing found", 1 + index, tuneChannel.Url, tuneChannel.Name);
                            Log.Debug(line);
                            continue;
                        }
                    }

                    int newChannels     = 0;
                    int updatedChannels = 0;

                    for (int i = 0; i < channels.Length; ++i)
                    {
                        Channel      dbChannel;
                        DVBIPChannel channel = (DVBIPChannel)channels[i];
                        if (channels.Length > 1)
                        {
                            if (channel.Name.IndexOf("Unknown") == 0)
                            {
                                channel.Name = name + (i + 1);
                            }
                        }
                        else
                        {
                            channel.Name = name;
                        }
                        bool         exists;
                        TuningDetail currentDetail;
                        //Check if we already have this tuningdetail. According to DVB-IP specifications there are two ways to identify DVB-IP
                        //services: one ONID + SID based, the other domain/URL based. At this time we don't fully and properly implement the DVB-IP
                        //specifications, so the safest method for service identification is the URL. The user has the option to enable the use of
                        //ONID + SID identification and channel move detection...
                        if (true)
                        {
                            currentDetail = layer.GetTuningDetail(channel.NetworkId, channel.ServiceId,
                                                                  TvBusinessLayer.GetChannelType(channel));
                        }
                        else
                        {
                            currentDetail = layer.GetTuningDetail(channel.Url, TvBusinessLayer.GetChannelType(channel));
                        }

                        if (currentDetail == null)
                        {
                            //add new channel
                            exists              = false;
                            dbChannel           = layer.AddNewChannel(channel.Name, channel.LogicalChannelNumber);
                            dbChannel.SortOrder = 10000;
                            if (channel.LogicalChannelNumber >= 1)
                            {
                                dbChannel.SortOrder = channel.LogicalChannelNumber;
                            }
                            dbChannel.IsTv    = channel.IsTv;
                            dbChannel.IsRadio = channel.IsRadio;
                            dbChannel.Persist();
                        }
                        else
                        {
                            exists    = true;
                            dbChannel = currentDetail.ReferencedChannel();
                        }

                        layer.AddChannelToGroup(dbChannel, TvConstants.TvGroupNames.AllChannels);

                        if (_defaultTVGroup != "")
                        {
                            layer.AddChannelToGroup(dbChannel, _defaultTVGroup);
                        }
                        if (currentDetail == null)
                        {
                            layer.AddTuningDetails(dbChannel, channel);
                        }
                        else
                        {
                            //update tuning details...
                            TuningDetail td = layer.UpdateTuningDetails(dbChannel, channel, currentDetail);
                            td.Persist();
                        }

                        if (channel.IsTv)
                        {
                            if (exists)
                            {
                                tvChannelsUpdated++;
                                updatedChannels++;
                            }
                            else
                            {
                                tvChannelsNew++;
                                newChannels++;
                            }
                        }
                        if (channel.IsRadio)
                        {
                            if (exists)
                            {
                                radioChannelsUpdated++;
                                updatedChannels++;
                            }
                            else
                            {
                                radioChannelsNew++;
                                newChannels++;
                            }
                        }
                        layer.MapChannelToCard(card, dbChannel, false);
                        line = String.Format("{0}- {1} :New:{2} Updated:{3}", 1 + index, tuneChannel.Name, newChannels,
                                             updatedChannels);
                        Log.Debug(line);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
            finally
            {
                RemoteControl.Instance.StopCard(user);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Scan Thread
        /// </summary>
        private void DoScan()
        {
            suminfo tv    = new suminfo();
            suminfo radio = new suminfo();
            IUser   user  = new User();

            user.CardId = _cardNumber;
            try
            {
                scanState = ScanState.Scanning;
                if (_dvbcChannels.Count == 0)
                {
                    return;
                }

                RemoteControl.Instance.EpgGrabberEnabled = false;

                SetButtonState();
                TvBusinessLayer layer = new TvBusinessLayer();
                Card            card  = layer.GetCardByDevicePath(RemoteControl.Instance.CardDevice(_cardNumber));

                for (int index = 0; index < _dvbcChannels.Count; ++index)
                {
                    if (scanState == ScanState.Cancel)
                    {
                        return;
                    }

                    float percent = ((float)(index)) / _dvbcChannels.Count;
                    percent *= 100f;
                    if (percent > 100f)
                    {
                        percent = 100f;
                    }
                    progressBar1.Value = (int)percent;

                    Application.DoEvents();

                    DVBCChannel  tuneChannel = new DVBCChannel(_dvbcChannels[index]); // new DVBCChannel();
                    string       line        = String.Format("{0}tp- {1}", 1 + index, tuneChannel.TuningInfo.ToString());
                    ListViewItem item        = listViewStatus.Items.Add(new ListViewItem(line));
                    item.EnsureVisible();

                    if (index == 0)
                    {
                        RemoteControl.Instance.Scan(ref user, tuneChannel, -1);
                    }

                    IChannel[] channels = RemoteControl.Instance.Scan(_cardNumber, tuneChannel);
                    UpdateStatus();

                    if (channels == null || channels.Length == 0)
                    {
                        if (RemoteControl.Instance.TunerLocked(_cardNumber) == false)
                        {
                            line = String.Format("{0}tp- {1} {2} {3}:No signal", 1 + index, tuneChannel.Frequency,
                                                 tuneChannel.ModulationType, tuneChannel.SymbolRate);
                            item.Text      = line;
                            item.ForeColor = Color.Red;
                            continue;
                        }
                        line = String.Format("{0}tp- {1} {2} {3}:Nothing found", 1 + index, tuneChannel.Frequency,
                                             tuneChannel.ModulationType, tuneChannel.SymbolRate);
                        item.Text      = line;
                        item.ForeColor = Color.Red;
                        continue;
                    }

                    radio.newChannel = 0;
                    radio.updChannel = 0;
                    tv.newChannel    = 0;
                    tv.updChannel    = 0;
                    for (int i = 0; i < channels.Length; ++i)
                    {
                        Channel      dbChannel;
                        DVBCChannel  channel = (DVBCChannel)channels[i];
                        bool         exists;
                        TuningDetail currentDetail;
                        //Check if we already have this tuningdetail. The user has the option to enable channel move detection...
                        if (checkBoxEnableChannelMoveDetection.Checked)
                        {
                            //According to the DVB specs ONID + SID is unique, therefore we do not need to use the TSID to identify a service.
                            //The DVB spec recommends that the SID should not change if a service moves. This theoretically allows us to
                            //track channel movements.
                            currentDetail = layer.GetTuningDetail(channel.NetworkId, channel.ServiceId,
                                                                  TvBusinessLayer.GetChannelType(channel));
                        }
                        else
                        {
                            //There are certain providers that do not maintain unique ONID + SID combinations.
                            //In those cases, ONID + TSID + SID is generally unique. The consequence of using the TSID to identify
                            //a service is that channel movement tracking won't work (each transponder/mux should have its own TSID).
                            currentDetail = layer.GetTuningDetail(channel.NetworkId, channel.TransportId, channel.ServiceId,
                                                                  TvBusinessLayer.GetChannelType(channel));
                        }

                        if (currentDetail == null)
                        {
                            //add new channel
                            exists              = false;
                            dbChannel           = layer.AddNewChannel(channel.Name, channel.LogicalChannelNumber);
                            dbChannel.SortOrder = 10000;
                            if (channel.LogicalChannelNumber >= 1)
                            {
                                dbChannel.SortOrder = channel.LogicalChannelNumber;
                            }
                            dbChannel.IsTv    = channel.IsTv;
                            dbChannel.IsRadio = channel.IsRadio;
                            dbChannel.Persist();
                        }
                        else
                        {
                            exists    = true;
                            dbChannel = currentDetail.ReferencedChannel();
                        }

                        if (dbChannel.IsTv)
                        {
                            layer.AddChannelToGroup(dbChannel, TvConstants.TvGroupNames.AllChannels);
                            if (checkBoxCreateSignalGroup.Checked)
                            {
                                layer.AddChannelToGroup(dbChannel, TvConstants.TvGroupNames.DVBC);
                            }
                            if (checkBoxCreateGroups.Checked)
                            {
                                layer.AddChannelToGroup(dbChannel, channel.Provider);
                            }
                        }
                        if (dbChannel.IsRadio)
                        {
                            layer.AddChannelToRadioGroup(dbChannel, TvConstants.RadioGroupNames.AllChannels);
                            if (checkBoxCreateSignalGroup.Checked)
                            {
                                layer.AddChannelToRadioGroup(dbChannel, TvConstants.RadioGroupNames.DVBC);
                            }
                            if (checkBoxCreateGroups.Checked)
                            {
                                layer.AddChannelToRadioGroup(dbChannel, channel.Provider);
                            }
                        }

                        if (currentDetail == null)
                        {
                            layer.AddTuningDetails(dbChannel, channel);
                        }
                        else
                        {
                            //update tuning details...
                            TuningDetail td = layer.UpdateTuningDetails(dbChannel, channel, currentDetail);
                            td.Persist();
                        }

                        if (channel.IsTv)
                        {
                            if (exists)
                            {
                                tv.updChannel++;
                            }
                            else
                            {
                                tv.newChannel++;
                                tv.newChannels.Add(channel);
                            }
                        }
                        if (channel.IsRadio)
                        {
                            if (exists)
                            {
                                radio.updChannel++;
                            }
                            else
                            {
                                radio.newChannel++;
                                radio.newChannels.Add(channel);
                            }
                        }
                        layer.MapChannelToCard(card, dbChannel, false);
                        line = String.Format("{0}tp- {1} {2} {3}:New TV/Radio:{4}/{5} Updated TV/Radio:{6}/{7}", 1 + index,
                                             tuneChannel.Frequency, tuneChannel.ModulationType, tuneChannel.SymbolRate,
                                             tv.newChannel, radio.newChannel, tv.updChannel, radio.updChannel);
                        item.Text = line;
                    }
                    tv.updChannelSum    += tv.updChannel;
                    radio.updChannelSum += radio.updChannel;
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
            finally
            {
                RemoteControl.Instance.StopCard(user);
                RemoteControl.Instance.EpgGrabberEnabled = true;
                progressBar1.Value = 100;

                scanState = ScanState.Done;
                SetButtonState();
            }
            listViewStatus.Items.Add(
                new ListViewItem(String.Format("Total radio channels updated:{0}, new:{1}", radio.updChannelSum,
                                               radio.newChannelSum)));
            foreach (IChannel newChannel in radio.newChannels)
            {
                listViewStatus.Items.Add(new ListViewItem(String.Format("  -> new channel: {0}", newChannel.Name)));
            }

            listViewStatus.Items.Add(
                new ListViewItem(String.Format("Total tv channels updated:{0}, new:{1}", tv.updChannelSum, tv.newChannelSum)));
            foreach (IChannel newChannel in tv.newChannels)
            {
                listViewStatus.Items.Add(new ListViewItem(String.Format("  -> new channel: {0}", newChannel.Name)));
            }
            ListViewItem lastItem = listViewStatus.Items.Add(new ListViewItem("Scan done..."));

            lastItem.EnsureVisible();
        }
Exemplo n.º 5
0
        private void DoScan()
        {
            int tvChannelsNew        = 0;
            int radioChannelsNew     = 0;
            int tvChannelsUpdated    = 0;
            int radioChannelsUpdated = 0;

            string buttonText = mpButtonScanTv.Text;
            IUser  user       = new User();

            user.CardId = _cardNumber;
            try
            {
                // First lock the card, because so that other parts of a hybrid card can't be used at the same time
                _isScanning         = true;
                _stopScanning       = false;
                mpButtonScanTv.Text = "Cancel...";
                RemoteControl.Instance.EpgGrabberEnabled = false;
                listViewStatus.Items.Clear();

                PlayList playlist = new PlayList();
                if (mpComboBoxService.SelectedIndex == 0)
                {
                    //TODO read SAP announcements
                }
                else
                {
                    IPlayListIO playlistIO =
                        PlayListFactory.CreateIO(String.Format(@"{0}\TuningParameters\dvbip\{1}.m3u", PathManager.GetDataPath,
                                                               mpComboBoxService.SelectedItem));
                    playlistIO.Load(playlist,
                                    String.Format(@"{0}\TuningParameters\dvbip\{1}.m3u", PathManager.GetDataPath,
                                                  mpComboBoxService.SelectedItem));
                }
                if (playlist.Count == 0)
                {
                    return;
                }

                mpComboBoxService.Enabled    = false;
                checkBoxCreateGroups.Enabled = false;
                checkBoxEnableChannelMoveDetection.Enabled = false;
                TvBusinessLayer layer = new TvBusinessLayer();
                Card            card  = layer.GetCardByDevicePath(RemoteControl.Instance.CardDevice(_cardNumber));

                int index = -1;
                IEnumerator <PlayListItem> enumerator = playlist.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    if (_stopScanning)
                    {
                        return;
                    }
                    index++;
                    float percent = ((float)(index)) / playlist.Count;
                    percent *= 100f;
                    if (percent > 100f)
                    {
                        percent = 100f;
                    }
                    progressBar1.Value = (int)percent;

                    string url  = enumerator.Current.FileName.Substring(enumerator.Current.FileName.LastIndexOf('\\') + 1);
                    string name = enumerator.Current.Description;

                    DVBIPChannel tuneChannel = new DVBIPChannel();
                    tuneChannel.Url  = url;
                    tuneChannel.Name = name;
                    string       line = String.Format("{0}- {1} - {2}", 1 + index, tuneChannel.Name, tuneChannel.Url);
                    ListViewItem item = listViewStatus.Items.Add(new ListViewItem(line));
                    item.EnsureVisible();
                    RemoteControl.Instance.Tune(ref user, tuneChannel, -1);
                    IChannel[] channels;
                    channels = RemoteControl.Instance.Scan(_cardNumber, tuneChannel);
                    UpdateStatus();
                    if (channels == null || channels.Length == 0)
                    {
                        if (RemoteControl.Instance.TunerLocked(_cardNumber) == false)
                        {
                            line           = String.Format("{0}- {1} :No Signal", 1 + index, tuneChannel.Url);
                            item.Text      = line;
                            item.ForeColor = Color.Red;
                            continue;
                        }
                        else
                        {
                            line           = String.Format("{0}- {1} :Nothing found", 1 + index, tuneChannel.Url);
                            item.Text      = line;
                            item.ForeColor = Color.Red;
                            continue;
                        }
                    }

                    int newChannels     = 0;
                    int updatedChannels = 0;

                    for (int i = 0; i < channels.Length; ++i)
                    {
                        Channel      dbChannel;
                        DVBIPChannel channel = (DVBIPChannel)channels[i];
                        if (channels.Length > 1)
                        {
                            if (channel.Name.IndexOf("Unknown") == 0)
                            {
                                channel.Name = name + (i + 1);
                            }
                        }
                        else
                        {
                            channel.Name = name;
                        }
                        bool         exists;
                        TuningDetail currentDetail;
                        //Check if we already have this tuningdetail. According to DVB-IP specifications there are two ways to identify DVB-IP
                        //services: one ONID + SID based, the other domain/URL based. At this time we don't fully and properly implement the DVB-IP
                        //specifications, so the safest method for service identification is the URL. The user has the option to enable the use of
                        //ONID + SID identification and channel move detection...
                        if (checkBoxEnableChannelMoveDetection.Checked)
                        {
                            currentDetail = layer.GetTuningDetail(channel.NetworkId, channel.ServiceId,
                                                                  TvBusinessLayer.GetChannelType(channel));
                        }
                        else
                        {
                            currentDetail = layer.GetTuningDetail(channel.Url, TvBusinessLayer.GetChannelType(channel));
                        }

                        if (currentDetail == null)
                        {
                            //add new channel
                            exists              = false;
                            dbChannel           = layer.AddNewChannel(channel.Name, channel.LogicalChannelNumber);
                            dbChannel.SortOrder = 10000;
                            if (channel.LogicalChannelNumber >= 1)
                            {
                                dbChannel.SortOrder = channel.LogicalChannelNumber;
                            }
                            dbChannel.IsTv    = channel.IsTv;
                            dbChannel.IsRadio = channel.IsRadio;
                            dbChannel.Persist();
                        }
                        else
                        {
                            exists    = true;
                            dbChannel = currentDetail.ReferencedChannel();
                        }

                        layer.AddChannelToGroup(dbChannel, TvConstants.TvGroupNames.AllChannels);

                        if (checkBoxCreateGroups.Checked)
                        {
                            layer.AddChannelToGroup(dbChannel, channel.Provider);
                        }
                        if (currentDetail == null)
                        {
                            layer.AddTuningDetails(dbChannel, channel);
                        }
                        else
                        {
                            //update tuning details...
                            TuningDetail td = layer.UpdateTuningDetails(dbChannel, channel, currentDetail);
                            td.Persist();
                        }

                        if (channel.IsTv)
                        {
                            if (exists)
                            {
                                tvChannelsUpdated++;
                                updatedChannels++;
                            }
                            else
                            {
                                tvChannelsNew++;
                                newChannels++;
                            }
                        }
                        if (channel.IsRadio)
                        {
                            if (exists)
                            {
                                radioChannelsUpdated++;
                                updatedChannels++;
                            }
                            else
                            {
                                radioChannelsNew++;
                                newChannels++;
                            }
                        }
                        layer.MapChannelToCard(card, dbChannel, false);
                        line = String.Format("{0}- {1} :New:{2} Updated:{3}", 1 + index, tuneChannel.Name, newChannels,
                                             updatedChannels);
                        item.Text = line;
                    }
                }
                //DatabaseManager.Instance.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
            finally
            {
                RemoteControl.Instance.StopCard(user);
                RemoteControl.Instance.EpgGrabberEnabled = true;
                progressBar1.Value           = 100;
                mpComboBoxService.Enabled    = true;
                checkBoxCreateGroups.Enabled = true;
                checkBoxEnableChannelMoveDetection.Enabled = true;
                mpButtonScanTv.Text = buttonText;
                _isScanning         = false;
            }
            ListViewItem lastItem = listViewStatus.Items.Add(new ListViewItem("Scan done..."));

            lastItem =
                listViewStatus.Items.Add(
                    new ListViewItem(String.Format("Total radio channels new:{0} updated:{1}", radioChannelsNew,
                                                   radioChannelsUpdated)));
            lastItem =
                listViewStatus.Items.Add(
                    new ListViewItem(String.Format("Total tv channels new:{0} updated:{1}", tvChannelsNew, tvChannelsUpdated)));
            lastItem.EnsureVisible();
        }
Exemplo n.º 6
0
        private void DoScan()
        {
            int    tvChannelsNew        = 0;
            int    radioChannelsNew     = 0;
            int    tvChannelsUpdated    = 0;
            int    radioChannelsUpdated = 0;
            string buttonText           = mpButtonScanTv.Text;

            try
            {
                _isScanning         = true;
                _stopScanning       = false;
                mpButtonScanTv.Text = "Cancel...";
                RemoteControl.Instance.EpgGrabberEnabled = false;
                if (_atscChannels.Count == 0)
                {
                    return;
                }
                mpComboBoxFrequencies.Enabled = false;
                listViewStatus.Items.Clear();
                TvBusinessLayer layer = new TvBusinessLayer();
                Card            card  = layer.GetCardByDevicePath(RemoteControl.Instance.CardDevice(_cardNumber));
                IUser           user  = new User();
                user.CardId = _cardNumber;
                int minchan = 2;
                int maxchan = 69 + 1;
                if ((string)mpComboBoxTuningMode.SelectedItem == "Clear QAM Cable")
                {
                    minchan = 1;
                    maxchan = _atscChannels.Count + 1;
                }
                else if ((string)mpComboBoxTuningMode.SelectedItem == "Digital Cable")
                {
                    minchan = 0;
                    maxchan = 1;
                }
                for (int index = minchan; index < maxchan; ++index)
                {
                    if (_stopScanning)
                    {
                        return;
                    }
                    float percent = ((float)(index)) / (maxchan - minchan);
                    percent *= 100f;
                    if (percent > 100f)
                    {
                        percent = 100f;
                    }
                    progressBar1.Value = (int)percent;
                    ATSCChannel tuneChannel = new ATSCChannel();
                    tuneChannel.NetworkId    = -1;
                    tuneChannel.TransportId  = -1;
                    tuneChannel.ServiceId    = -1;
                    tuneChannel.MinorChannel = -1;
                    tuneChannel.MajorChannel = -1;
                    string line;
                    if ((string)mpComboBoxTuningMode.SelectedItem == "Clear QAM Cable")
                    {
                        tuneChannel.PhysicalChannel = index;
                        tuneChannel.Frequency       = _atscChannels[index - 1].frequency;
                        if (tuneChannel.Frequency < 10000)
                        {
                            continue;
                        }
                        tuneChannel.ModulationType = ModulationType.Mod256Qam;
                        line = string.Format("physical channel = {0}, frequency = {1} kHz, modulation = 256 QAM", tuneChannel.PhysicalChannel, tuneChannel.Frequency);
                        Log.Info("ATSC: scanning clear QAM cable, {0}, frequency plan = {1}", line, mpComboBoxFrequencies.SelectedItem);
                    }
                    else if ((string)mpComboBoxTuningMode.SelectedItem == "ATSC Digital Terrestrial")
                    {
                        tuneChannel.PhysicalChannel = index;
                        tuneChannel.Frequency       = -1;
                        tuneChannel.ModulationType  = ModulationType.Mod8Vsb;
                        line = string.Format("physical channel = {0}, modulation = 8 VSB", tuneChannel.PhysicalChannel);
                        Log.Info("ATSC: scanning ATSC over-the-air, {0}", line);
                    }
                    else
                    {
                        tuneChannel.PhysicalChannel = 0;
                        tuneChannel.ModulationType  = ModulationType.Mod256Qam;
                        line = "out-of-band service information";
                        Log.Info("ATSC: scanning digital cable, {0}", line);
                    }
                    line += "... ";
                    ListViewItem item = listViewStatus.Items.Add(new ListViewItem(line));
                    item.EnsureVisible();
                    if (index == minchan)
                    {
                        RemoteControl.Instance.Scan(ref user, tuneChannel, -1);
                    }
                    IChannel[] channels = RemoteControl.Instance.Scan(_cardNumber, tuneChannel);
                    UpdateStatus();
                    if (channels == null || channels.Length == 0)
                    {
                        if (tuneChannel.PhysicalChannel > 0 && !RemoteControl.Instance.TunerLocked(_cardNumber))
                        {
                            line += "no signal";
                        }
                        else
                        {
                            line += "signal locked, no channels found";
                        }
                        item.Text      = line;
                        item.ForeColor = Color.Red;
                        continue;
                    }
                    int newChannels     = 0;
                    int updatedChannels = 0;
                    for (int i = 0; i < channels.Length; ++i)
                    {
                        Channel     dbChannel;
                        ATSCChannel channel = (ATSCChannel)channels[i];
                        //No support for channel moving, or merging with existing channels here.
                        //We do not know how ATSC works to correctly implement this.
                        TuningDetail currentDetail = layer.GetTuningDetail(channel);
                        if (currentDetail != null)
                        {
                            if (channel.IsDifferentTransponder(layer.GetTuningChannel(currentDetail)))
                            {
                                currentDetail = null;
                            }
                        }
                        bool exists;
                        if (currentDetail == null)
                        {
                            //add new channel
                            exists              = false;
                            dbChannel           = layer.AddNewChannel(channel.Name, channel.LogicalChannelNumber);
                            dbChannel.SortOrder = 10000;
                            if (channel.LogicalChannelNumber >= 1)
                            {
                                dbChannel.SortOrder = channel.LogicalChannelNumber;
                            }
                        }
                        else
                        {
                            exists    = true;
                            dbChannel = currentDetail.ReferencedChannel();
                        }
                        dbChannel.IsTv    = channel.IsTv;
                        dbChannel.IsRadio = channel.IsRadio;
                        dbChannel.Persist();
                        if (dbChannel.IsTv)
                        {
                            layer.AddChannelToGroup(dbChannel, TvConstants.TvGroupNames.AllChannels);
                        }
                        if (dbChannel.IsRadio)
                        {
                            layer.AddChannelToRadioGroup(dbChannel, TvConstants.RadioGroupNames.AllChannels);
                        }
                        if (currentDetail == null)
                        {
                            layer.AddTuningDetails(dbChannel, channel);
                        }
                        else
                        {
                            //update tuning details...
                            TuningDetail td = layer.UpdateTuningDetails(dbChannel, channel, currentDetail);
                            td.Persist();
                        }
                        if (channel.IsTv)
                        {
                            if (exists)
                            {
                                tvChannelsUpdated++;
                                updatedChannels++;
                            }
                            else
                            {
                                tvChannelsNew++;
                                newChannels++;
                            }
                        }
                        if (channel.IsRadio)
                        {
                            if (exists)
                            {
                                radioChannelsUpdated++;
                                updatedChannels++;
                            }
                            else
                            {
                                radioChannelsNew++;
                                newChannels++;
                            }
                        }
                        layer.MapChannelToCard(card, dbChannel, false);
                    }
                    line     += string.Format("new = {0}, updated = {1}", newChannels, updatedChannels);
                    item.Text = line;
                    Log.Info("ATSC: scan result, new = {0}, updated = {1}", newChannels, updatedChannels);
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
            finally
            {
                IUser user = new User();
                user.CardId = _cardNumber;
                RemoteControl.Instance.StopCard(user);
                RemoteControl.Instance.EpgGrabberEnabled = true;
                progressBar1.Value           = 100;
                mpComboBoxTuningMode.Enabled = true;
                UpdateQamFrequencyFieldAvailability();
                mpButtonScanTv.Text = buttonText;
                _isScanning         = false;
            }
            listViewStatus.Items.Add(
                new ListViewItem(String.Format("Total radio channels, new = {0}, updated = {1}", radioChannelsNew,
                                               radioChannelsUpdated)));
            listViewStatus.Items.Add(
                new ListViewItem(String.Format("Total TV channels, new = {0} updated = {1}", tvChannelsNew, tvChannelsUpdated)));
            ListViewItem lastItem = listViewStatus.Items.Add(new ListViewItem("Scan done!"));

            lastItem.EnsureVisible();
            Log.Info("ATSC: scan summary, new TV = {0}, updated TV = {1}, new radio = {2}, updated radio = {3}", tvChannelsNew, tvChannelsUpdated, radioChannelsNew, radioChannelsUpdated);
        }
Exemplo n.º 7
0
        private void DoScan()
        {
            suminfo tv    = new suminfo();
            suminfo radio = new suminfo();
            IUser   user  = new User();

            user.CardId = _cardNumber;

            try
            {
                if (_DVBCChannels.Count == 0)
                {
                    return;
                }

                TvBusinessLayer layer = new TvBusinessLayer();
                Card            card  = layer.GetCardByDevicePath(RemoteControl.Instance.CardDevice(_cardNumber));

                for (int index = 0; index < _DVBCChannels.Count; ++index)
                {
                    DVBCTuning  curTuning   = _DVBCChannels[index];
                    DVBCChannel tuneChannel = new DVBCChannel(curTuning);
                    string      line        = String.Format("{0}tp- {1}", 1 + index, tuneChannel.TuningInfo.ToString());
                    Log.Debug(line);

                    if (index == 0)
                    {
                        RemoteControl.Instance.Scan(ref user, tuneChannel, -1);
                    }

                    IChannel[] channels = RemoteControl.Instance.Scan(_cardNumber, tuneChannel);

                    if (channels == null || channels.Length == 0)
                    {
                        if (RemoteControl.Instance.TunerLocked(_cardNumber) == false)
                        {
                            line = String.Format("{0}tp- {1} {2} {3}:No signal", 1 + index, tuneChannel.Frequency,
                                                 tuneChannel.ModulationType, tuneChannel.SymbolRate);

                            Log.Error(line);
                            continue;
                        }
                        line = String.Format("{0}tp- {1} {2} {3}:Nothing found", 1 + index, tuneChannel.Frequency,
                                             tuneChannel.ModulationType, tuneChannel.SymbolRate);

                        Log.Error(line);
                        continue;
                    }

                    radio.newChannel = 0;
                    radio.updChannel = 0;
                    tv.newChannel    = 0;
                    tv.updChannel    = 0;

                    for (int i = 0; i < channels.Length; ++i)
                    {
                        Channel      dbChannel;
                        DVBCChannel  channel = (DVBCChannel)channels[i];
                        bool         exists;
                        TuningDetail currentDetail;
                        //Check if we already have this tuningdetail. The user has the option to enable channel move detection...
                        if (true)
                        {
                            //According to the DVB specs ONID + SID is unique, therefore we do not need to use the TSID to identify a service.
                            //The DVB spec recommends that the SID should not change if a service moves. This theoretically allows us to
                            //track channel movements.
                            currentDetail = layer.GetTuningDetail(channel.NetworkId, channel.ServiceId,
                                                                  TvBusinessLayer.GetChannelType(channel));
                        }
                        else
                        {
                            //There are certain providers that do not maintain unique ONID + SID combinations.
                            //In those cases, ONID + TSID + SID is generally unique. The consequence of using the TSID to identify
                            //a service is that channel movement tracking won't work (each transponder/mux should have its own TSID).
                            currentDetail = layer.GetTuningDetail(channel.NetworkId, channel.TransportId, channel.ServiceId,
                                                                  TvBusinessLayer.GetChannelType(channel));
                        }

                        if (currentDetail == null)
                        {
                            //add new channel
                            exists              = false;
                            dbChannel           = layer.AddNewChannel(channel.Name, channel.LogicalChannelNumber);
                            dbChannel.SortOrder = 10000;
                            if (channel.LogicalChannelNumber >= 1)
                            {
                                dbChannel.SortOrder = channel.LogicalChannelNumber;
                            }
                            dbChannel.IsTv    = channel.IsTv;
                            dbChannel.IsRadio = channel.IsRadio;
                            dbChannel.GrabEpg = true;
                            dbChannel.Persist();

                            if (dbChannel.IsTv)
                            {
                                layer.AddChannelToGroup(dbChannel, TvConstants.TvGroupNames.AllChannels);

                                if (_defaultTVGroup != "")
                                {
                                    layer.AddChannelToGroup(dbChannel, _defaultTVGroup);
                                }
                            }
                            if (dbChannel.IsRadio)
                            {
                                layer.AddChannelToRadioGroup(dbChannel, TvConstants.RadioGroupNames.AllChannels);

                                if (_defaultTVGroup != "")
                                {
                                    layer.AddChannelToRadioGroup(dbChannel, _defaultTVGroup);
                                }
                            }
                        }
                        else
                        {
                            exists    = true;
                            dbChannel = currentDetail.ReferencedChannel();
                        }

                        if (currentDetail == null)
                        {
                            layer.AddTuningDetails(dbChannel, channel);
                        }
                        else
                        {
                            //update tuning details...
                            TuningDetail td = layer.UpdateTuningDetails(dbChannel, channel, currentDetail);
                            td.Persist();
                        }

                        if (channel.IsTv)
                        {
                            if (exists)
                            {
                                tv.updChannel++;
                            }
                            else
                            {
                                tv.newChannel++;
                                tv.newChannels.Add(channel);
                            }
                        }
                        if (channel.IsRadio)
                        {
                            if (exists)
                            {
                                radio.updChannel++;
                            }
                            else
                            {
                                radio.newChannel++;
                                radio.newChannels.Add(channel);
                            }
                        }
                        layer.MapChannelToCard(card, dbChannel, false);
                        line = String.Format("{0}tp- {1} {2} {3}:New TV/Radio:{4}/{5} Updated TV/Radio:{6}/{7}", 1 + index,
                                             tuneChannel.Frequency, tuneChannel.ModulationType, tuneChannel.SymbolRate,
                                             tv.newChannel, radio.newChannel, tv.updChannel, radio.updChannel);
                        Log.Debug(line);
                    }
                    tv.updChannelSum    += tv.updChannel;
                    radio.updChannelSum += radio.updChannel;
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
            finally
            {
                RemoteControl.Instance.StopCard(user);
            }

            if (radio.newChannels.Count == 0)
            {
                Log.Debug("No new radio channels");
            }
            else
            {
                foreach (IChannel newChannel in radio.newChannels)
                {
                    String line = String.Format("Radio  -> new channel: {0}", newChannel.Name);
                    Log.Debug(line);
                }
            }

            if (tv.newChannels.Count == 0)
            {
                Log.Debug("No new TV channels");
            }
            else
            {
                foreach (IChannel newChannel in tv.newChannels)
                {
                    String line = String.Format("TV  -> new channel: {0}", newChannel.Name);
                    Log.Debug(line);
                }
            }
        }
Exemplo n.º 8
0
        public ICardTuneReservationTicket RequestCardTuneReservation(ITvCardHandler tvcard, IChannel tuningDetail, IUser user, int idChannel)
        {
            ICardTuneReservationTicket cardTuneReservationTicket = null;
            var layer = new TvBusinessLayer();

            CardTuneState cardTuneState;
            int           ticketId = 0;
            bool          isCardAvail;
            bool          hasUserHigherPriorityThanBlockingUser = false;

            lock (tvcard.Tuner.CardReservationsLock)
            {
                isCardAvail = IsCardAvail(tvcard);
                if (!isCardAvail)
                {
                    if (tvcard.Tuner.CardTuneState != CardTuneState.TuneCancelled)
                    {
                        IUser blockingUser = GetBlockingUser(tvcard);
                        hasUserHigherPriorityThanBlockingUser = (HasUserHigherPriorityThanBlockingUser(user, blockingUser));
                        if (hasUserHigherPriorityThanBlockingUser)
                        {
                            tvcard.Tuner.CardTuneState = CardTuneState.TuneCancelled;
                        }
                    }
                }
            }
            if (!isCardAvail)
            {
                if (hasUserHigherPriorityThanBlockingUser)
                {
                    tvcard.Tuner.CancelTune(tvcard.Tuner.ActiveCardTuneReservationTicket.PendingSubchannel);
                    lock (tvcard.Tuner.CardReservationsLock)
                    {
                        isCardAvail = IsCardAvail(tvcard);
                    }
                }
            }

            lock (tvcard.Tuner.CardReservationsLock)
            {
                if (isCardAvail)
                {
                    tvcard.Tuner.CardTuneState = CardTuneState.TunePending;
                    bool isTunedToTransponder = IsTunedToTransponder(tvcard, tuningDetail);

                    int ownerSubchannel = -1;
                    int numberOfUsersOnSameCurrentChannel = 0;
                    int numberOfOtherUsersOnSameChannel   = 0;
                    int numberOfOtherUsersOnCurrentCard   = 0;

                    bool hasUserHighestPriority       = false;
                    bool hasUserEqualOrHigherPriority = false;
                    bool isCamAlreadyDecodingChannel  = false;
                    bool conflictingSubchannelFound   = false;
                    bool isRecordingAnyUser           = false;
                    bool isAnySubChannelTimeshifting  = tvcard.TimeShifter.IsAnySubChannelTimeshifting;
                    bool isOwner       = tvcard.Users.IsOwner(user);
                    var  users         = new List <IUser>(tvcard.Users.GetUsers());
                    var  inactiveUsers = new List <IUser>();
                    var  activeUsers   = new List <IUser>();
                    var  recUsers      = new List <IUser>();
                    var  tsUsers       = new List <IUser>();

                    var context = tvcard.Card.Context as ITvCardContext;
                    if (context != null)
                    {
                        context.GetUser(ref user);
                        hasUserHighestPriority       = context.HasUserHighestPriority(user);
                        hasUserEqualOrHigherPriority = context.HasUserEqualOrHigherPriority(user);
                    }

                    int currentChannelId = tvcard.CurrentDbChannel(ref user);

                    for (int i = users.Count - 1; i > -1; i--)
                    {
                        IUser actualUser = users[i];
                        CardReservationHelper.AddUserIfRecording(tvcard, ref actualUser, recUsers);
                        CardReservationHelper.AddUserIfTimeshifting(tvcard, ref actualUser, tsUsers);

                        bool isCurrentUser = user.Name.Equals(actualUser.Name);

                        IChannel userChannel    = tvcard.CurrentChannel(ref actualUser);
                        var      userDVBchannel = userChannel as DVBBaseChannel;

                        if (!isCurrentUser)
                        {
                            if (!isRecordingAnyUser)
                            {
                                isRecordingAnyUser = CardReservationHelper.IsRecordingUser(tvcard, user, ref actualUser);
                            }

                            if (actualUser.SubChannel == user.SubChannel && user.IdChannel > 0)
                            {
                                conflictingSubchannelFound = true;
                            }
                            numberOfOtherUsersOnCurrentCard = CardReservationHelper.GetNumberOfOtherUsersOnCurrentCard(user,
                                                                                                                       numberOfOtherUsersOnCurrentCard);


                            if (userChannel == null)
                            {
                                inactiveUsers.Add(actualUser);
                            }
                            else
                            {
                                if (userDVBchannel != null)
                                {
                                    actualUser.IdChannel = layer.GetTuningDetail(userDVBchannel).IdChannel;
                                }

                                bool isDiffTS = tuningDetail.IsDifferentTransponder(userChannel);

                                if (isDiffTS)
                                {
                                    activeUsers.Add(actualUser);
                                }
                                else
                                {
                                    if (!isOwner)
                                    {
                                        bool isUserOnSameChannel = CardReservationHelper.IsUserOnSameChannel(tuningDetail, layer,
                                                                                                             userDVBchannel);
                                        if (isUserOnSameChannel)
                                        {
                                            numberOfOtherUsersOnSameChannel++;
                                            //we do not want to hook up on schedulers existing subchannel
                                            if (!actualUser.IsAdmin)
                                            {
                                                ownerSubchannel = actualUser.SubChannel;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        bool isUserOnSameCurrentChannel = CardReservationHelper.IsUserOnSameCurrentChannel(currentChannelId, actualUser);
                        if (isUserOnSameCurrentChannel)
                        {
                            numberOfUsersOnSameCurrentChannel++;
                        }

                        if (!isCamAlreadyDecodingChannel)
                        {
                            isCamAlreadyDecodingChannel = IsCamAlreadyDecodingChannel(tuningDetail, userChannel);
                        }
                    }

                    bool isFreeToAir = CardReservationHelper.IsFreeToAir(tvcard, user);

                    cardTuneReservationTicket = new CardTuneReservationTicket
                                                (
                        user,
                        tuningDetail,
                        isTunedToTransponder,
                        numberOfOtherUsersOnSameChannel,
                        isAnySubChannelTimeshifting,
                        inactiveUsers,
                        activeUsers,
                        users,
                        ownerSubchannel,
                        isOwner,
                        tvcard.DataBaseCard.IdCard,
                        tvcard.NumberOfChannelsDecrypting,
                        isFreeToAir,
                        numberOfOtherUsersOnCurrentCard,
                        recUsers,
                        tsUsers,
                        conflictingSubchannelFound,
                        numberOfUsersOnSameCurrentChannel,
                        isCamAlreadyDecodingChannel,
                        hasUserHighestPriority,
                        hasUserEqualOrHigherPriority);
                    tvcard.Tuner.ActiveCardTuneReservationTicket = cardTuneReservationTicket;
                    tvcard.Tuner.ReservationsForTune.Add(cardTuneReservationTicket);
                }

                cardTuneState = tvcard.Tuner.CardTuneState;
                if (tvcard.Tuner.ActiveCardTuneReservationTicket != null)
                {
                    ticketId = tvcard.Tuner.ActiveCardTuneReservationTicket.Id;
                }
            }


            if (cardTuneReservationTicket != null)
            {
                Log.Debug("RequestCardTuneReservation: placed reservation with id={0}, tuningdetails={1}", cardTuneReservationTicket.Id, cardTuneReservationTicket.TuningDetail);
            }
            else
            {
                if (ticketId > 0)
                {
                    Log.Debug("RequestCardTuneReservation: failed reservation tuningdetails={0}, res id blocking={1}, state={2}", tuningDetail, ticketId, cardTuneState);
                }
                else
                {
                    Log.Debug("RequestCardTuneReservation: failed reservation tuningdetails={0}, res id blocking={1}, state={2}", tuningDetail, "n/a", cardTuneState);
                }
            }
            return(cardTuneReservationTicket);
        }
Exemplo n.º 9
0
        private void DoScan()
        {
            int    tvChannelsNew        = 0;
            int    radioChannelsNew     = 0;
            int    tvChannelsUpdated    = 0;
            int    radioChannelsUpdated = 0;
            string buttonText           = mpButtonScanTv.Text;

            try
            {
                _isScanning         = true;
                _stopScanning       = false;
                mpButtonScanTv.Text = "Cancel...";
                RemoteControl.Instance.EpgGrabberEnabled = false;
                if (_atscChannels.Count == 0)
                {
                    return;
                }
                mpComboBoxFrequencies.Enabled = false;
                listViewStatus.Items.Clear();
                TvBusinessLayer layer = new TvBusinessLayer();
                Card            card  = layer.GetCardByDevicePath(RemoteControl.Instance.CardDevice(_cardNumber));
                IUser           user  = new User();
                user.CardId = _cardNumber;
                int minchan = 2;
                int maxchan = 69;
                //Check if QAM if so then the number of channels varies
                if (checkBoxQAM.Checked)
                {
                    minchan = 0;
                    maxchan = _atscChannels.Count;
                }
                for (int index = minchan; index < maxchan; ++index)
                {
                    if (_stopScanning)
                    {
                        return;
                    }
                    float percent = ((float)(index)) / (maxchan - minchan);
                    percent *= 100f;
                    if (percent > 100f)
                    {
                        percent = 100f;
                    }
                    progressBar1.Value = (int)percent;
                    ATSCChannel tuneChannel = new ATSCChannel();
                    tuneChannel.NetworkId    = -1;
                    tuneChannel.TransportId  = -1;
                    tuneChannel.ServiceId    = -1;
                    tuneChannel.MinorChannel = -1;
                    tuneChannel.MajorChannel = -1;
                    if (checkBoxQAM.Checked)
                    {
                        Log.WriteFile("ATSC tune: QAM checkbox selected... using Modulation 256Qam");
                        tuneChannel.PhysicalChannel = index + 1;
                        tuneChannel.Frequency       = _atscChannels[index].frequency;
                        tuneChannel.ModulationType  = ModulationType.Mod256Qam;
                    }
                    else
                    {
                        Log.WriteFile("ATSC tune: QAM checkbox not selected... using Modulation 8Vsb");
                        tuneChannel.PhysicalChannel = index;
                        tuneChannel.Frequency       = -1;
                        tuneChannel.ModulationType  = ModulationType.Mod8Vsb;
                    }
                    Log.WriteFile("ATSC tune: PhysicalChannel: {0} Frequency: {1} Modulation: {2}", tuneChannel.PhysicalChannel,
                                  tuneChannel.Frequency, tuneChannel.ModulationType);
                    string line = String.Format("physical channel:{0} frequency:{1} modulation:{2}", tuneChannel.PhysicalChannel,
                                                tuneChannel.Frequency, tuneChannel.ModulationType);
                    ListViewItem item = listViewStatus.Items.Add(new ListViewItem(line));
                    item.EnsureVisible();
                    if (index == minchan)
                    {
                        RemoteControl.Instance.Scan(ref user, tuneChannel, -1);
                    }
                    IChannel[] channels = RemoteControl.Instance.Scan(_cardNumber, tuneChannel);
                    UpdateStatus();

                    /*if (channels == null || channels.Length == 0)
                     * {
                     * if (checkBoxQAM.Checked)
                     * {
                     *  //try Modulation 64Qam now
                     *  tuneChannel.PhysicalChannel = index + 1;
                     *  tuneChannel.Frequency = _atscChannels[index].frequency;
                     *  tuneChannel.ModulationType = ModulationType.Mod64Qam;
                     *  line = String.Format("physical channel:{0} frequency:{1} modulation:{2}: No signal", tuneChannel.PhysicalChannel, tuneChannel.Frequency, tuneChannel.ModulationType);
                     *  item.Text = line;
                     *  channels = RemoteControl.Instance.Scan(_cardNumber, tuneChannel);
                     * }
                     * }*/
                    UpdateStatus();
                    if (channels == null || channels.Length == 0)
                    {
                        if (RemoteControl.Instance.TunerLocked(_cardNumber) == false)
                        {
                            line = String.Format("physical channel:{0} frequency:{1} modulation:{2}: No signal",
                                                 tuneChannel.PhysicalChannel, tuneChannel.Frequency, tuneChannel.ModulationType);
                            item.Text      = line;
                            item.ForeColor = Color.Red;
                            continue;
                        }
                        line = String.Format("physical channel:{0} frequency:{1} modulation:{2}: Nothing found",
                                             tuneChannel.PhysicalChannel, tuneChannel.Frequency, tuneChannel.ModulationType);
                        item.Text      = line;
                        item.ForeColor = Color.Red;
                        continue;
                    }
                    int newChannels     = 0;
                    int updatedChannels = 0;
                    for (int i = 0; i < channels.Length; ++i)
                    {
                        Channel     dbChannel;
                        ATSCChannel channel = (ATSCChannel)channels[i];
                        //No support for channel moving, or merging with existing channels here.
                        //We do not know how ATSC works to correctly implement this.
                        TuningDetail currentDetail = layer.GetTuningDetail(channel);
                        if (currentDetail != null)
                        {
                            if (channel.Frequency != currentDetail.Frequency)
                            {
                                currentDetail = null;
                            }
                        }
                        bool exists;
                        if (currentDetail == null)
                        {
                            //add new channel
                            exists              = false;
                            dbChannel           = layer.AddNewChannel(channel.Name);
                            dbChannel.SortOrder = 10000;
                            if (channel.LogicalChannelNumber >= 1)
                            {
                                dbChannel.SortOrder = channel.LogicalChannelNumber;
                            }
                        }
                        else
                        {
                            exists    = true;
                            dbChannel = currentDetail.ReferencedChannel();
                        }
                        dbChannel.IsTv    = channel.IsTv;
                        dbChannel.IsRadio = channel.IsRadio;
                        dbChannel.Persist();
                        if (dbChannel.IsTv)
                        {
                            layer.AddChannelToGroup(dbChannel, TvConstants.TvGroupNames.AllChannels);
                        }
                        if (dbChannel.IsRadio)
                        {
                            layer.AddChannelToRadioGroup(dbChannel, TvConstants.RadioGroupNames.AllChannels);
                        }
                        if (currentDetail == null)
                        {
                            layer.AddTuningDetails(dbChannel, channel);
                        }
                        else
                        {
                            //update tuning details...
                            TuningDetail td = layer.UpdateTuningDetails(dbChannel, channel, currentDetail);
                            td.Persist();
                        }
                        if (channel.IsTv)
                        {
                            if (exists)
                            {
                                tvChannelsUpdated++;
                                updatedChannels++;
                            }
                            else
                            {
                                tvChannelsNew++;
                                newChannels++;
                            }
                        }
                        if (channel.IsRadio)
                        {
                            if (exists)
                            {
                                radioChannelsUpdated++;
                                updatedChannels++;
                            }
                            else
                            {
                                radioChannelsNew++;
                                newChannels++;
                            }
                        }
                        layer.MapChannelToCard(card, dbChannel, false);
                        line = String.Format("physical channel:{0} frequency:{1} modulation:{2} New:{3} Updated:{4}",
                                             tuneChannel.PhysicalChannel, tuneChannel.Frequency, tuneChannel.ModulationType,
                                             newChannels, updatedChannels);
                        item.Text = line;
                    }
                }
                //DatabaseManager.Instance.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
            finally
            {
                IUser user = new User();
                user.CardId = _cardNumber;
                RemoteControl.Instance.StopCard(user);
                RemoteControl.Instance.EpgGrabberEnabled = true;
                progressBar1.Value            = 100;
                checkBoxQAM.Enabled           = true;
                mpComboBoxFrequencies.Enabled = true;
                mpButtonScanTv.Text           = buttonText;
                _isScanning = false;
            }
            listViewStatus.Items.Add(
                new ListViewItem(String.Format("Total radio channels new:{0} updated:{1}", radioChannelsNew,
                                               radioChannelsUpdated)));
            listViewStatus.Items.Add(
                new ListViewItem(String.Format("Total tv channels new:{0} updated:{1}", tvChannelsNew, tvChannelsUpdated)));
            ListViewItem lastItem = listViewStatus.Items.Add(new ListViewItem("Scan done..."));

            lastItem.EnsureVisible();
        }