void DoSendStartList(object sender, System.Timers.ElapsedEventArgs e)
        {
            string output = null;

            Application.Current.Dispatcher.Invoke(() =>
            {
                List <object> remaingStarters = new List <object>();
                int c = 0;
                foreach (var item in _rslVP.GetView())
                {
                    if (_limit >= 0 && c >= _limit)
                    {
                        break;
                    }

                    remaingStarters.Add(item);
                    c++;
                }

                output = JsonConversion.ConvertOnStartList(remaingStarters);
            });

            OnNewDataToSend(this, new NewDataEventArgs {
                Data = output
            });
        }
        string getRemainingStartersList(RaceRun raceRun, string grouping, int limit)
        {
            string output = "";

            Application.Current.Dispatcher.Invoke(() =>
            {
                ViewConfigurator viewConfigurator = new ViewConfigurator(raceRun.GetRace());
                var vp = viewConfigurator.GetRemainingStartersViewProvider(raceRun);
                if (grouping != null)
                {
                    vp.ChangeGrouping(grouping);
                }

                List <object> remaingStarters = new List <object>();
                int c = 0;
                foreach (var item in vp.GetView())
                {
                    if (limit >= 0 && c >= limit)
                    {
                        break;
                    }

                    remaingStarters.Add(item);
                    c++;
                }

                output = JsonConversion.ConvertOnStartList(remaingStarters);
            });

            return(output);
        }
        void DoSendResultList(object sender, System.Timers.ElapsedEventArgs e)
        {
            string output = null;

            Application.Current.Dispatcher.Invoke(() =>
            {
                output = JsonConversion.ConvertRunResults(_dm.GetCurrentRaceRun().GetResultView());
            });

            OnNewDataToSend(this, new NewDataEventArgs {
                Data = output
            });
        }
        void SendResultList()
        {
            string output = null;

            Application.Current.Dispatcher.Invoke(() =>
            {
                output = JsonConversion.ConvertOnTrack(_dm.GetCurrentRaceRun().GetOnTrackList());
            });

            OnNewDataToSend(this, new NewDataEventArgs {
                Data = output
            });
        }
        string getResultList(Race race, string grouping)
        {
            string output = "";

            Application.Current.Dispatcher.Invoke(() =>
            {
                ViewConfigurator viewConfigurator = new ViewConfigurator(race);
                RaceResultViewProvider vp         = viewConfigurator.GetRaceResultViewProvider(race);
                if (grouping != null)
                {
                    vp.ChangeGrouping(grouping);
                }

                output = JsonConversion.ConvertRaceResults(vp.GetView(), (uint)race.GetMaxRun());
            });

            return(output);
        }
        string getStartList(RaceRun raceRun, string grouping)
        {
            string output = "";

            Application.Current.Dispatcher.Invoke(() =>
            {
                ViewConfigurator viewConfigurator = new ViewConfigurator(raceRun.GetRace());
                StartListViewProvider vp          = viewConfigurator.GetStartlistViewProvider(raceRun);
                if (grouping != null)
                {
                    vp.ChangeGrouping(grouping);
                }

                output = JsonConversion.ConvertStartList(vp.GetView());
            });

            return(output);
        }
        void SendCurrentRaceData()
        {
            Race    r  = _dm.GetCurrentRace();
            RaceRun rr = _dm.GetCurrentRaceRun();

            Dictionary <string, string> data = new Dictionary <string, string>();

            data["type"] = r?.RaceType.ToString();
            data["run"]  = rr?.Run.ToString();

            string output = null;

            Application.Current.Dispatcher.Invoke(() =>
            {
                output = JsonConversion.ConvertCurrrentRaceRun(data);
            });

            OnNewDataToSend(this, new NewDataEventArgs {
                Data = output
            });
        }
        private void OnSomethingChanged(object sender, RaceParticipant participantEnteredTrack, RaceParticipant participantLeftTrack, RunResult currentRunResult)
        {
            RaceParticipant particpant = null;

            string eventType = null;

            if (participantEnteredTrack != null)
            {
                eventType  = "Started";
                particpant = participantEnteredTrack;
            }
            if (participantLeftTrack != null)
            {
                eventType  = "Finished";
                particpant = participantLeftTrack;
            }

            string output = JsonConversion.ConvertEvent(particpant, eventType, currentRunResult);

            OnNewDataToSend(this, new NewDataEventArgs {
                Data = output
            });
        }
        protected void HandleAPIRequest(HttpRequestEventArgs e)
        {
            var req = e.Request;
            var res = e.Response;

            string apiVersion = null, listName = null;
            int    raceNo = -1, runNo = -1;

            parseUrlPath(req.RawUrl, out apiVersion, out listName, out raceNo, out runNo);


            var r = req.QueryString;


            Race race = null;

            if (raceNo < 0)
            {
                race = _dataModel?.GetCurrentRace();
            }
            else
            {
                race = _dataModel?.GetRace(raceNo);
            }

            RaceRun raceRun = null;

            if (runNo < 0)
            {
                raceRun = _dataModel?.GetCurrentRaceRun();
            }
            else
            {
                raceRun = race?.GetRun(runNo);
            }

            string output = "";

            if (listName == "startlist")
            {
                if (raceRun != null)
                {
                    output = getStartList(raceRun, getGrouping(req.QueryString));
                }
            }
            else if (listName == "nextstarters")
            {
                if (raceRun != null)
                {
                    output = getRemainingStartersList(raceRun, getGrouping(req.QueryString), getLimit(req.QueryString));
                }
            }
            else if (listName == "resultlist")
            {
                if (raceRun != null)
                {
                    output = getResultList(raceRun, getGrouping(req.QueryString));
                }
                else if (race != null)
                {
                    output = getResultList(race, getGrouping(req.QueryString));
                }
            }
            else if (listName == "metadata")
            {
                if (race != null)
                {
                    var classes  = _dataModel.GetParticipantClasses().ToArray();
                    var groups   = _dataModel.GetParticipantGroups().ToArray();
                    var sex      = new string[] { "M", "W" };
                    var grouping = new string[] { "Class", "Group", "Sex" };
                    var runs     = race.GetMaxRun();

                    output = JsonConversion.ConvertMetaData(classes, groups, sex, grouping, runs);
                }
            }


            res.ContentType     = "application/vnd.api+json";
            res.ContentEncoding = Encoding.UTF8;
            res.WriteContent(Encoding.UTF8.GetBytes(output));
        }