Esempio n. 1
0
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Hosted Service running.");
            System.Diagnostics.Debug.WriteLine("Hosted Service running.");
            Load();
            metaDataMusicBrainz = new MetaDataMusicBrainz(Path.Combine(discsPath, "MusicBrainz"), discsRelPath + "/MusicBrainz");
            metaDataGD3         = new MetaDataGD3(_provider, contentRootPath, Path.Combine(discsPath, "GD3"), discsRelPath + "/GD3");

            key2DiscChanger = new Dictionary <string, DiscChanger>(DiscChangers.Count);
            List <Task> connectTasks = new List <Task>(DiscChangers.Count);

            foreach (var discChanger in DiscChangers)
            {
                key2DiscChanger[discChanger.Key] = discChanger;
                foreach (var kvp in discChanger.Discs)
                {
                    Disc d = kvp.Value;
                    d.DataMusicBrainz = metaDataMusicBrainz.Get(d);
                    d.DataGD3Match    = metaDataGD3.Get(d);
                }
                try
                {
                    connectTasks.Add(discChanger.Connect(this, _hubContext, _logger));
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("Exception connecting disc changer: " + discChanger.Key + ": " + e.Message);
                    await _hubContext.Clients.All.SendAsync("StatusData",
                                                            discChanger.Key,
                                                            null,
                                                            null,
                                                            null,
                                                            "off",
                                                            null);
                }
            }
            Task.WaitAll(connectTasks.ToArray());
            _logger.LogInformation("Hosted service starting");
            TimeSpan  discDataTimeOut          = TimeSpan.FromSeconds(3);
            const int NullStatusQueryFrequency = 40;//only query null status every 40x3 seconds
            var       metaDataSet = new HashSet <string> {
                MetaDataMusicBrainz.Type, MetaDataGD3.Type_Match
            };
            var metaDataSetWithGD3Lookup = new HashSet <string> {
                MetaDataMusicBrainz.Type, MetaDataGD3.Type_Match, MetaDataGD3.Type_MetaData
            };

            await Task.Factory.StartNew(async() =>
            {
                int countDown = NullStatusQueryFrequency;
                // loop until a cancellation is requested
                while (!cancellationToken.IsCancellationRequested)
                {
                    _logger.LogInformation("Hosted service executing - {0}", DateTime.Now);
                    try
                    {
                        Disc d         = await discDataMessages.ReceiveAsync(discDataTimeOut, cancellationToken);
                        DiscChanger dc = d.DiscChanger;
                        await UpdateMetaData(d, (metaDataGD3?.AutoLookupEnabled(d) ?? false) ? metaDataSetWithGD3Lookup : metaDataSet, true);
                        d.DateTimeAdded ??= DateTime.Now;
                        dc.Discs[d.Slot] = d;
                        needsSaving      = true;
                    }
                    catch (OperationCanceledException) { }
                    catch (TimeoutException)
                    {
                        if (needsSaving)
                        {
                            Save();
                        }
                        countDown--;
                        foreach (var discChanger in DiscChangers)
                        {
                            var status = discChanger.CurrentStatus();
                            if ((status == null && countDown == 0) /*||(status!=null&&status.IsOutDated())*/) //feature to refresh outdated status prevents auto off on DVP-CX777ES
                            {
                                discChanger.ClearStatus();
                                if (discChanger.Connected())
                                {
                                    discChanger.InitiateStatusUpdate();
                                }
                            }
                        }
                        if (countDown <= 0)
                        {
                            countDown = NullStatusQueryFrequency;
                        }
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine("Hosted Service Exception: " + e.Message);
                        _logger.LogInformation(e, "Hosted Service Exception");
                    }
                }
            }, cancellationToken);
        }
Esempio n. 2
0
            public async Task <bool> RetrieveMetaData(MetaDataGD3 metaDataGD3, string path)
            {
                if (Matches == null || Matches.Length == 0 || metaData != null)
                {
                    return(false);
                }
                GD3DVD.DVDMatch match   = Matches[SelectedMatch ?? 0];
                var             dvdCode = match.DVDCode;
                var             discID  = match.DiscID;
                var             t       = Tuple.Create(dvdCode, discID);

                if (AssociateMetaData())
                {
                    return(false);
                }
                var task = metaDataGD3.SoapClientDVD.RetrieveDVDMetaByDiscIDAsync(metaDataGD3.authDVD, dvdCode, discID);

                if (await Task.WhenAny(task, Task.Delay(30000)) != task)
                {
                    throw new Exception("GD3DVD RetrieveDVDMetaByDiscIDAsync timeout: " + dvdCode);
                }
                var retrieveDVDMetaResponse = await task;
                var dvdMeta = retrieveDVDMetaResponse?.RetrieveDVDMetaByDiscIDResult;

                if (dvdMeta == null)
                {
                    return(false);
                }
                string title                   = MetaDataProvider.RemoveBlacklistedCharacters(GetTitlePlusDisc(dvdMeta) ?? "NoTitle", 80);
                string fileNameBase            = $"Meta_{match.MediaType}_{title}_{dvdCode}_{discID}";
                string frontCoverImageFileName = null;
                string backCoverImageFileName  = null;
                var    ai = dvdMeta.FrontCover;
                var    bc = dvdMeta.BackCover;

                if (ai != null)
                {
                    if (ai.Length > 0)
                    {
                        frontCoverImageFileName = await WriteImage(ai, path, $"MetaFrontCover_{title}_{dvdCode}");
                    }
                    dvdMeta.FrontCover = null;
                }
                if (bc != null)
                {
                    if (bc.Length > 0)
                    {
                        backCoverImageFileName = await WriteImage(bc, path, $"MetaBackCover_{title}_{dvdCode}");
                    }
                    dvdMeta.BackCover = null;
                }
                var m = new MetaDataDVD
                {
                    DVDCode       = dvdCode,
                    DiscID        = discID,
                    DVDMeta       = dvdMeta,
                    ImageFileName = frontCoverImageFileName ?? backCoverImageFileName
                };

                using (var f = File.Create(Path.Combine(path, Path.ChangeExtension(fileNameBase, "json"))))
                {
                    var w = new Utf8JsonWriter(f, new JsonWriterOptions {
                        Indented = true
                    });
                    JsonSerializer.Serialize(w, m);
                    f.Close();
                }
                metaDataGD3.dvdCodeDiscIDToMetaData[t] = m;
                metaData = m;
                return(true);
            }
Esempio n. 3
0
 public MatchBD(MetaDataGD3 m)
 {
     metaDataGD3 = m;
 }
Esempio n. 4
0
 public void SetMetaDataGD3(MetaDataGD3 metaDataGD3) => this.metaDataGD3 = metaDataGD3;