SafeNet/MotoTRBO_SOC/SettingsForm.cs
2021-02-24 13:50:23 +02:00

201 lines
7.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net;
using SafeNetLib;
namespace MotoTRBO_SOC
{
public partial class SettingsForm : Form
{
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
private int seqID = 1;
public SettingsForm()
{
InitializeComponent();
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
private void pictureBoxClose_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// Intercept mouse move on all the items of the form
/// </summary>
/// <param name="sender">UI object which raised the event</param>
/// <param name="e">Mouse event parameters</param>
private void SettingsForm_MouseMove(object sender, MouseEventArgs e)
{
// move form window if mouse moved with mouse pressed
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
/// <summary>
/// Event handler for when the from is loaded. It will update the UI
/// elements to the value stored in the config file
/// </summary>
private void SettingsForm_Load(object sender, EventArgs e)
{
// set if gateway must be capacity plus or no
cbCapacityPlus.Checked = MotoTRBO_GW.cfg.capPlus;
// set radio IP
tbRadioIP.Text = MotoTRBO_GW.cfg.masterRadioIP;
// set if gateway should send triggered location stop or not
cbLocationStop.Checked = MotoTRBO_GW.cfg.locationStop;
// set gateway code
Int32 gwCode = 666666;
Int32.TryParse(MotoTRBO_GW.cfg.gw_id, out gwCode);
npGwCode.Value = gwCode > 0 ? gwCode : 1;
// set radio ID
npRadioID.Value = MotoTRBO_GW.cfg.masterRadioID > 0 ? MotoTRBO_GW.cfg.masterRadioID : 1;
}
private void btSave_Click(object sender, EventArgs e)
{
MotoTRBO_GW.cfg.UpdateConfigParameter("GATEWAY", "id", npGwCode.Value.ToString());
MotoTRBO_GW.cfg.UpdateConfigParameter("GATEWAY", "MasterRadioIP", tbRadioIP.Text);
MotoTRBO_GW.cfg.UpdateConfigParameter("GATEWAY", "MasterRadioID", npRadioID.Value.ToString());
MotoTRBO_GW.cfg.UpdateConfigParameter("GATEWAY", "capPlus", cbCapacityPlus.Checked ? "true" : "false");
MotoTRBO_GW.cfg.UpdateConfigParameter("GATEWAY", "locationStop", cbLocationStop.Checked ? "true" : "false");
DialogResult dr = MessageBox.Show("Settings saved succesfully. You need to restart gateway " +
"for the changes to take effect." + System.Environment.NewLine +
System.Environment.NewLine + " Do you wish to restart now?", "Settings saved",
MessageBoxButtons.YesNo);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
// restart gateway
if (OnRestartSelected != null)
OnRestartSelected();
this.Close();
}
else
{
// user decided to do nothing
}
}
/// <summary>
/// Event handler for when Capacity Plus checkbox changes it's status
/// </summary>
private void cbCapacityPlus_CheckedChanged(object sender, EventArgs e)
{
if (cbCapacityPlus.Checked)
lbRadioIP.Text = "MASTER RADIO IP";
else
lbRadioIP.Text = "RADIO IP";
}
private void tbRadioIP_TextChanged(object sender, EventArgs e)
{
IPAddress ip;
bool b = IPAddress.TryParse(tbRadioIP.Text, out ip);
btSave.Enabled = b;
}
public delegate void RestartSelected();
public event RestartSelected OnRestartSelected;
private void buttonTriggered_Click(object sender, EventArgs e)
{
try
{
if (!MotoTRBO_GW.unitsLoaded)
return;
buttonTriggered.Enabled = false;
LocationThread.SendTriggeredLocationSTOP(numericRadioID.Value.ToString(), LocationThread.REQ_ID);
if (OnCommandRequested != null)
OnCommandRequested("Manual triggered location stop request for " + numericRadioID.Value.ToString());
Timer p = new Timer();
p.Interval = 1500;
p.Start();
p.Tick += delegate(object sender2, EventArgs e2)
{
Utils.WriteLine("««« Debug Triggered location request", ConsoleColor.Cyan);
LocationThread.SendTriggeredLocationRequest(numericRadioID.Value.ToString(), (int)numericInterval.Value);
if (OnCommandRequested != null)
OnCommandRequested("Manual triggered location request [" + numericInterval.Value + " sec] for " + numericRadioID.Value.ToString());
this.Invoke((MethodInvoker)delegate()
{
buttonTriggered.Enabled = true;
p.Stop();
p.Dispose();
});
};
}
catch (Exception ex) { }
}
private void buttonImmediate_Click(object sender, EventArgs e)
{
try
{
if (!MotoTRBO_GW.unitsLoaded)
return;
buttonImmediate.Enabled = false;
Utils.WriteLine("««« Debug POLL location request", ConsoleColor.Cyan);
LocationThread.SendPollRequest(numericRadioID.Value.ToString(), (byte)seqID++);
if (OnCommandRequested != null)
OnCommandRequested("Manual poll location request for " + numericRadioID.Value.ToString());
Timer p = new Timer();
p.Interval = 3000;
p.Start();
p.Tick += delegate(object sender2, EventArgs e2)
{
this.Invoke((MethodInvoker)delegate()
{
buttonImmediate.Enabled = true;
p.Stop();
p.Dispose();
});
};
}
catch (Exception ex) { }
}
public delegate void commandRequested(string UIMessage);
public event commandRequested OnCommandRequested;
}
}