private void ProcessFixture(IResource fixture, string sport)
        {
            if (!_activeFixtures.ContainsKey(fixture.Id) && !_listeners.ContainsKey(fixture.Id))
            {
                try
                {
                    _activeFixtures.TryAdd(fixture.Id, true);

                    var matchStatus = 0;
                    var matchSequence = 0;
                    if (fixture.Content != null)
                    {
                        matchStatus = fixture.Content.MatchStatus;
                        //Get the sequence number, if you store this to file you can check if you need to process a snapshot between restarts
                        //this can save pushing unnesasary snapshots
                        matchSequence = fixture.Content.Sequence;
                    }

                    //if not match over
                    if (matchStatus != (int)SSMatchStatus.MatchOver)
                    {
                        _logger.InfoFormat("Get UDAPI Snapshot for {0} id {1}", fixture.Name, fixture.Id);
                        var snapshotString = fixture.GetSnapshot();
                        _logger.InfoFormat("Successfully retrieved UDAPI Snapshot for {0} id {1}", fixture.Name, fixture.Id);

                        var fixtureSnapshot =
                           (Fixture)
                           JsonConvert.DeserializeObject(snapshotString, typeof(Fixture),
                                                           new JsonSerializerSettings
                                                           {
                                                               Converters =
                                                                   new List<JsonConverter> { new IsoDateTimeConverter() },
                                                               NullValueHandling = NullValueHandling.Ignore
                                                           });

                        var epoch = fixtureSnapshot.Epoch;

                        //process the snapshot here and push it into the client system

                        var streamListener = new StreamListener(fixture, epoch, sport);
                        _listeners.TryAdd(fixture.Id, streamListener);
                    }
                    else
                    {
                        _logger.InfoFormat("Fixture {0} id {1} has finished. Will not process", fixture.Name, fixture.Id);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Fixture {0} id {1} There is a problem processing this fixture", fixture.Name, fixture.Id), ex);
                }

                bool y;
                _activeFixtures.TryRemove(fixture.Id, out y);
            }
            else
            {
                _logger.InfoFormat("Fixture {0} id {1} is currently being processed", fixture.Name, fixture.Id);
                if (_listeners.ContainsKey(fixture.Id))
                {
                    if (_listeners[fixture.Id].FixtureEnded)
                    {
                        StreamListener theListener;
                        if (_listeners.TryRemove(fixture.Id, out theListener))
                        {
                            _logger.InfoFormat("Fixture {0} id {1} is over.", fixture.Name, fixture.Id);
                        }
                        bool activeFixture;
                        _activeFixtures.TryRemove(fixture.Id, out activeFixture);
                    }
                }
            }
        }
예제 #2
0
        private void ProcessFixture(IResource fixture, string sport)
        {
            if (!_activeFixtures.ContainsKey(fixture.Id) && !_listeners.ContainsKey(fixture.Id))
            {
                try
                {
                    _activeFixtures.TryAdd(fixture.Id, true);

                    var matchStatus   = 0;
                    var matchSequence = 0;
                    if (fixture.Content != null)
                    {
                        matchStatus = fixture.Content.MatchStatus;
                        //Get the sequence number, if you store this to file you can check if you need to process a snapshot between restarts
                        //this can save pushing unnesasary snapshots
                        matchSequence = fixture.Content.Sequence;
                    }

                    //if not match over
                    if (matchStatus != (int)SSMatchStatus.MatchOver)
                    {
                        _logger.InfoFormat("Get UDAPI Snapshot for {0} id {1}", fixture.Name, fixture.Id);
                        var snapshotString = fixture.GetSnapshot();
                        _logger.InfoFormat("Successfully retrieved UDAPI Snapshot for {0} id {1}", fixture.Name, fixture.Id);

                        var fixtureSnapshot =
                            (Fixture)
                            JsonConvert.DeserializeObject(snapshotString, typeof(Fixture),
                                                          new JsonSerializerSettings
                        {
                            Converters =
                                new List <JsonConverter> {
                                new IsoDateTimeConverter()
                            },
                            NullValueHandling = NullValueHandling.Ignore
                        });

                        var epoch = fixtureSnapshot.Epoch;

                        //process the snapshot here and push it into the client system

                        var streamListener = new StreamListener(fixture, epoch, sport);
                        _listeners.TryAdd(fixture.Id, streamListener);
                    }
                    else
                    {
                        _logger.InfoFormat("Fixture {0} id {1} has finished. Will not process", fixture.Name, fixture.Id);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Fixture {0} id {1} There is a problem processing this fixture", fixture.Name, fixture.Id), ex);
                }

                bool y;
                _activeFixtures.TryRemove(fixture.Id, out y);
            }
            else
            {
                _logger.InfoFormat("Fixture {0} id {1} is currently being processed", fixture.Name, fixture.Id);
                if (_listeners.ContainsKey(fixture.Id))
                {
                    if (_listeners[fixture.Id].FixtureEnded)
                    {
                        StreamListener theListener;
                        if (_listeners.TryRemove(fixture.Id, out theListener))
                        {
                            _logger.InfoFormat("Fixture {0} id {1} is over.", fixture.Name, fixture.Id);
                        }
                        bool activeFixture;
                        _activeFixtures.TryRemove(fixture.Id, out activeFixture);
                    }
                }
            }
        }