From 0d358af15b5edb830b482cd10ba1683ff4202e9a Mon Sep 17 00:00:00 2001 From: Tom Pittenger Date: Tue, 25 Apr 2023 11:20:35 -0700 Subject: [PATCH 1/2] Settings: add GetDecimal() to Settings.Instance storage --- ExtLibs/Utilities/Settings.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ExtLibs/Utilities/Settings.cs b/ExtLibs/Utilities/Settings.cs index 1600bd8ea3..54c20f97e2 100644 --- a/ExtLibs/Utilities/Settings.cs +++ b/ExtLibs/Utilities/Settings.cs @@ -243,6 +243,17 @@ public double GetDouble(string key, double defaultd = 0) return result; } + public decimal GetDecimal(string key, decimal defaultd = 0) + { + decimal result = defaultd; + string value = null; + if (config.TryGetValue(key, out value)) + { + decimal.TryParse(value, out result); + } + return result; + } + public byte GetByte(string key, byte defaultb = 0) { byte result = defaultb; From e883cf5fca119f57d018c94c31be7167ff942c40 Mon Sep 17 00:00:00 2001 From: Tom Pittenger Date: Tue, 25 Apr 2023 12:01:00 -0700 Subject: [PATCH 2/2] CoT: save/load all user config settings on load/close plus misc cleanup --- Controls/SerialOutputCoT.Designer.cs | 90 +++++++++++++++++----------- Controls/SerialOutputCoT.cs | 75 +++++++++++------------ Controls/SerialOutputCoT.resx | 18 ++++++ 3 files changed, 110 insertions(+), 73 deletions(-) diff --git a/Controls/SerialOutputCoT.Designer.cs b/Controls/SerialOutputCoT.Designer.cs index 5727654e60..fe63c46c93 100644 --- a/Controls/SerialOutputCoT.Designer.cs +++ b/Controls/SerialOutputCoT.Designer.cs @@ -42,16 +42,18 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.CMB_updaterate = new System.Windows.Forms.ComboBox(); this.CMB_baudrate = new System.Windows.Forms.ComboBox(); this.CMB_serialport = new System.Windows.Forms.ComboBox(); this.TB_output = new System.Windows.Forms.TextBox(); this.BTN_clear_TB = new System.Windows.Forms.Button(); this.GB_connection = new System.Windows.Forms.GroupBox(); + this.BUT_connect = new MissionPlanner.Controls.MyButton(); this.label_type = new System.Windows.Forms.Label(); this.TB_xml_type = new System.Windows.Forms.TextBox(); this.CB_advancedMode = new System.Windows.Forms.CheckBox(); this.chk_indent = new System.Windows.Forms.CheckBox(); + this.updateRate_numericUpDown = new System.Windows.Forms.NumericUpDown(); + this.label2 = new System.Windows.Forms.Label(); this.myDataGridView1 = new MissionPlanner.Controls.MyDataGridView(); this.sysid = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.UID = new System.Windows.Forms.DataGridViewTextBoxColumn(); @@ -59,29 +61,11 @@ private void InitializeComponent() this.ContactCallsign = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ContactEndPointIP = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.VMF = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.BUT_connect = new MissionPlanner.Controls.MyButton(); this.GB_connection.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.updateRate_numericUpDown)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.myDataGridView1)).BeginInit(); this.SuspendLayout(); // - // CMB_updaterate - // - this.CMB_updaterate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.CMB_updaterate.FormattingEnabled = true; - this.CMB_updaterate.Items.AddRange(new object[] { - "10hz", - "5hz", - "2hz", - "1hz", - "0.5hz", - "0.2hz", - "0.1hz"}); - this.CMB_updaterate.Location = new System.Drawing.Point(136, 46); - this.CMB_updaterate.Name = "CMB_updaterate"; - this.CMB_updaterate.Size = new System.Drawing.Size(75, 21); - this.CMB_updaterate.TabIndex = 11; - this.CMB_updaterate.SelectedIndexChanged += new System.EventHandler(this.CMB_updaterate_SelectedIndexChanged); - // // CMB_baudrate // this.CMB_baudrate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; @@ -126,7 +110,7 @@ private void InitializeComponent() // // BTN_clear_TB // - this.BTN_clear_TB.Location = new System.Drawing.Point(264, 70); + this.BTN_clear_TB.Location = new System.Drawing.Point(370, 70); this.BTN_clear_TB.Name = "BTN_clear_TB"; this.BTN_clear_TB.Size = new System.Drawing.Size(88, 23); this.BTN_clear_TB.TabIndex = 16; @@ -138,7 +122,6 @@ private void InitializeComponent() // this.GB_connection.Controls.Add(this.CMB_serialport); this.GB_connection.Controls.Add(this.BUT_connect); - this.GB_connection.Controls.Add(this.CMB_updaterate); this.GB_connection.Controls.Add(this.CMB_baudrate); this.GB_connection.Location = new System.Drawing.Point(12, 12); this.GB_connection.Name = "GB_connection"; @@ -147,6 +130,18 @@ private void InitializeComponent() this.GB_connection.TabStop = false; this.GB_connection.Text = "Connection"; // + // BUT_connect + // + this.BUT_connect.ImeMode = System.Windows.Forms.ImeMode.NoControl; + this.BUT_connect.Location = new System.Drawing.Point(135, 19); + this.BUT_connect.Name = "BUT_connect"; + this.BUT_connect.Size = new System.Drawing.Size(75, 23); + this.BUT_connect.TabIndex = 9; + this.BUT_connect.Text = "Connect"; + this.BUT_connect.TextColorNotEnabled = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(87)))), ((int)(((byte)(4))))); + this.BUT_connect.UseVisualStyleBackColor = true; + this.BUT_connect.Click += new System.EventHandler(this.BUT_connect_Click); + // // label_type // this.label_type.AutoSize = true; @@ -192,6 +187,38 @@ private void InitializeComponent() this.chk_indent.UseVisualStyleBackColor = true; this.chk_indent.CheckedChanged += new System.EventHandler(this.chk_indent_CheckedChanged); // + // updateRate_numericUpDown + // + this.updateRate_numericUpDown.Location = new System.Drawing.Point(307, 73); + this.updateRate_numericUpDown.Maximum = new decimal(new int[] { + 9999, + 0, + 0, + 0}); + this.updateRate_numericUpDown.Minimum = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.updateRate_numericUpDown.Name = "updateRate_numericUpDown"; + this.updateRate_numericUpDown.Size = new System.Drawing.Size(46, 20); + this.updateRate_numericUpDown.TabIndex = 25; + this.updateRate_numericUpDown.Value = new decimal(new int[] { + 10, + 0, + 0, + 0}); + this.updateRate_numericUpDown.ValueChanged += new System.EventHandler(this.updateRate_numericUpDown_ValueChanged); + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(235, 75); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(68, 13); + this.label2.TabIndex = 26; + this.label2.Text = "Interval (sec)"; + // // myDataGridView1 // this.myDataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) @@ -255,24 +282,14 @@ private void InitializeComponent() this.VMF.Name = "VMF"; this.VMF.ToolTipText = "If you don\'t know what this is then leave it blank"; // - // BUT_connect - // - this.BUT_connect.ImeMode = System.Windows.Forms.ImeMode.NoControl; - this.BUT_connect.Location = new System.Drawing.Point(135, 19); - this.BUT_connect.Name = "BUT_connect"; - this.BUT_connect.Size = new System.Drawing.Size(75, 23); - this.BUT_connect.TabIndex = 9; - this.BUT_connect.Text = "Connect"; - this.BUT_connect.TextColorNotEnabled = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(87)))), ((int)(((byte)(4))))); - this.BUT_connect.UseVisualStyleBackColor = true; - this.BUT_connect.Click += new System.EventHandler(this.BUT_connect_Click); - // // SerialOutputCoT // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(583, 575); + this.Controls.Add(this.label2); this.Controls.Add(this.chk_indent); + this.Controls.Add(this.updateRate_numericUpDown); this.Controls.Add(this.CB_advancedMode); this.Controls.Add(this.myDataGridView1); this.Controls.Add(this.label_type); @@ -285,6 +302,7 @@ private void InitializeComponent() this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.SerialOutputCoT_FormClosing); this.Load += new System.EventHandler(this.SerialOutputCoT_Load); this.GB_connection.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.updateRate_numericUpDown)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.myDataGridView1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -292,8 +310,6 @@ private void InitializeComponent() } #endregion - - private System.Windows.Forms.ComboBox CMB_updaterate; private System.Windows.Forms.ComboBox CMB_baudrate; private MyButton BUT_connect; private System.Windows.Forms.ComboBox CMB_serialport; @@ -311,5 +327,7 @@ private void InitializeComponent() private System.Windows.Forms.DataGridViewTextBoxColumn ContactCallsign; private System.Windows.Forms.DataGridViewTextBoxColumn ContactEndPointIP; private System.Windows.Forms.DataGridViewTextBoxColumn VMF; + private System.Windows.Forms.NumericUpDown updateRate_numericUpDown; + private System.Windows.Forms.Label label2; } } \ No newline at end of file diff --git a/Controls/SerialOutputCoT.cs b/Controls/SerialOutputCoT.cs index 30c81ffd45..9fca6428bb 100644 --- a/Controls/SerialOutputCoT.cs +++ b/Controls/SerialOutputCoT.cs @@ -2,6 +2,7 @@ using MissionPlanner.Comms; using MissionPlanner.Utilities; using MissionPlanner.Utilities.CoT; +using Newtonsoft.Json.Linq; using System; using System.Globalization; using System.IO; @@ -18,49 +19,24 @@ public partial class SerialOutputCoT : Form { static TcpListener listener; static ICommsSerial CoTStream = new SerialPort(); - static double updaterate = 1; + static int updaterate_ms = 10*1000; System.Threading.Thread t12; static bool threadrun = false; - static internal PointLatLngAlt HomeLoc = new PointLatLngAlt(0, 0, 0, "Home"); - static object[,] tabledata; private bool indent; public SerialOutputCoT() { InitializeComponent(); - CMB_serialport.Items.AddRange(SerialPort.GetPortNames()); + CMB_serialport.Items.Add("TAK Multicast"); CMB_serialport.Items.Add("TCP Host - 14551"); CMB_serialport.Items.Add("TCP Client"); CMB_serialport.Items.Add("UDP Host - 14551"); CMB_serialport.Items.Add("UDP Client"); - - - CMB_serialport.Items.Add("ATAK MC"); - CMB_serialport.SelectedIndex = CMB_serialport.Items.Count - 1; // select ATAK MC as default - - CMB_updaterate.Text = updaterate + "Hz"; - - if (threadrun) - { - BUT_connect.Text = Strings.Stop; - } - + CMB_serialport.Items.AddRange(SerialPort.GetPortNames()); MissionPlanner.Utilities.Tracking.AddPage(this.GetType().ToString(), this.Text); } - private void CMB_updaterate_SelectedIndexChanged(object sender, EventArgs e) - { - try - { - updaterate = float.Parse(CMB_updaterate.Text.Replace("hz", "")); - } - catch - { - CustomMessageBox.Show(Strings.InvalidUpdateRate, Strings.ERROR); - } - } - private void BUT_connect_Click(object sender, EventArgs e) { @@ -106,8 +82,8 @@ private void BUT_connect_Click(object sender, EventArgs e) CoTStream = new UdpSerialConnect(); CMB_baudrate.SelectedIndex = 0; break; - case "ATAK MC": - CoTStream = new UdpSerialConnect() { ConfigRef = "ATAK" }; + case "TAK Multicast": + CoTStream = new UdpSerialConnect() { ConfigRef = "TAK_Multicast" }; ((UdpSerialConnect)CoTStream).Open("239.2.3.1", "6969"); CMB_baudrate.SelectedIndex = 0; break; @@ -206,16 +182,12 @@ void mainloop() TB_output.Text = view; }); - var nextsend = DateTime.Now.AddMilliseconds(1000 / updaterate); - var sleepfor = Math.Min((int)Math.Abs((nextsend - DateTime.Now).TotalMilliseconds), 4000); - Thread.Sleep(sleepfor); + Thread.Sleep(updaterate_ms); counter++; - - } catch { - Thread.Sleep(10); + Thread.Sleep(1000); } } } @@ -399,13 +371,37 @@ private void SerialOutputCoT_Load(object sender, EventArgs e) // this can crash if you're connecting as you're loading the screen so the row count may change as you're populating it. myDataGridView1.Deserialize(Settings.Instance["CoTUID"]); } catch { } + + + updateRate_numericUpDown.Value = Settings.Instance.GetDecimal("CoT_updateRate", 10); + CMB_serialport.SelectedIndex = Settings.Instance.GetInt32("CoT_CMB_serialport", 0); + CMB_baudrate.SelectedIndex = Settings.Instance.GetInt32("CoT_CMB_baudrate", 0); + CB_advancedMode.Checked = Settings.Instance.GetBoolean("CoT_CB_advancedMode", true); + chk_indent.Checked = Settings.Instance.GetBoolean("CoT_chk_indent", true); + CB_advancedMode_CheckedChanged(null, null); chk_indent_CheckedChanged(null, null); + updateRate_numericUpDown_ValueChanged(null, null); + CMB_serialport_SelectedIndexChanged(null, null); + + if (threadrun) + { + // stop + BUT_connect_Click(null, null); + + // restart + BUT_connect_Click(null, null); + } + } private void SerialOutputCoT_FormClosing(object sender, FormClosingEventArgs e) { - + Settings.Instance["CoT_updateRate"] = updateRate_numericUpDown.Value.ToString(); + Settings.Instance["CoT_CMB_serialport"] = CMB_serialport.SelectedIndex.ToString(); + Settings.Instance["CoT_CMB_baudrate"] = CMB_baudrate.SelectedIndex.ToString(); + Settings.Instance["CoT_CB_advancedMode"] = CB_advancedMode.Checked.ToString(); + Settings.Instance["CoT_chk_indent"] = chk_indent.Checked.ToString(); } private void myDataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e) @@ -452,6 +448,11 @@ private void myDataGridView1_CellLeave(object sender, DataGridViewCellEventArgs { myDataGridView1.EndEdit(); } + + private void updateRate_numericUpDown_ValueChanged(object sender, EventArgs e) + { + updaterate_ms = (int)(updateRate_numericUpDown.Value * 1000); + } } public sealed class Utf8StringWriter : StringWriter diff --git a/Controls/SerialOutputCoT.resx b/Controls/SerialOutputCoT.resx index c7cd7d05cf..af0786e4b4 100644 --- a/Controls/SerialOutputCoT.resx +++ b/Controls/SerialOutputCoT.resx @@ -135,4 +135,22 @@ True + + True + + + True + + + True + + + True + + + True + + + True + \ No newline at end of file