Esempio n. 1
0
        private async Task UpdateHud(CancellationToken cancellationToken)
        {
            try
            {
                while (true)
                {
                    var text = "";
                    var obj  = await Telemetry.GetFlightData();

                    int delay = 1000;

                    if (obj != null && obj["type"] != "dummy_plane")
                    {
                        if (!prevDataValid)
                        {
                            currentCraftName = obj["type"];

                            prevDataValid                 = true;
                            CurrentCraftNameLbl.Text      = currentCraftName;
                            CurrentCraftNameLbl.ForeColor = System.Drawing.Color.DarkGreen;
                            ReloadBtn.Enabled             = true;
                            LoadBtn.Enabled               = true;

                            LogEntriesLbl.Text  = "0";
                            LogFileSizeLbl.Text = "0 kb";

                            await ReloadParams();

                            LoadSavedConfig();

                            if (LoggingEnableChkBox.Checked)
                            {
                                StartLogging();
                            }
                            else
                            {
                                LogFileNameLbl.Text = "Logging not active";
                            }
                        }

                        if (LoggingEnableChkBox.Checked)
                        {
                            var loggingDict = new Dictionary <byte, float>();

                            byte id = 0;
                            foreach (var item in paramIdToName)
                            {
                                if ((activeParamsBs.Contains(new ParamDescription(item)) && LogShownRB.Checked) || LogAllRB.Checked)
                                {
                                    if (float.TryParse(obj[item], NumberStyles.Any, CultureInfo.InvariantCulture, out float value))
                                    {
                                        loggingDict.Add(id, value);
                                    }
                                }
                                id++;
                            }

                            LogWriter.AddRecord(ref loggingDict);

                            LogEntriesLbl.Text  = LogWriter.NumEntries.ToString();
                            LogFileSizeLbl.Text = $"{LogWriter.FileSize / 1024} kb";
                        }

                        foreach (ParamDescription item in activeParamsBs)
                        {
                            if (!obj.ContainsKey(item.Name))
                            {
                                continue;
                            }

                            try
                            {
                                var formatString = $"{{0,{item.Format}}}";

                                var temp = $"{item.Description,-6}";
                                temp += String.Format(CultureInfo.InvariantCulture, formatString, double.Parse(obj[item.Name], CultureInfo.InvariantCulture));
                                temp += " " + item.Unit + "\n";

                                text += temp;
                            }
                            catch (FormatException)
                            {
                                text += $"{item.Description,-6} Bad format string\n";
                            }
                        }

                        delay = Properties.Settings.Default.RefreshRate;
                    }
                    else
                    {
                        if (prevDataValid)
                        {
                            LogShownRB.Enabled = true;
                            LogAllRB.Enabled   = true;

                            prevDataValid                 = false;
                            ReloadBtn.Enabled             = false;
                            LoadBtn.Enabled               = false;
                            CurrentCraftNameLbl.ForeColor = System.Drawing.Color.DarkRed;

                            LogWriter.FinalizeLog();
                        }
                    }

                    hudForm.HUDLabel.Text = text;

                    Task waitTask = Task.Delay(delay, cancellationToken);
                    try
                    {
                        await waitTask;
                    }
                    catch (TaskCanceledException)
                    {
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Hud update task exited!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }