SafeDispatch/CPlus_GW/MainForm.cs

275 lines
10 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using SafeMobileLib;
using System.Threading;
using Telerik.WinControls.Primitives;
using Telerik.WinControls.UI;
using System.Collections;
using SharedUI;
using System.Diagnostics;
namespace CPlus_GW
{
public partial class MainForm : Telerik.WinControls.UI.RadForm
{
CPlusGW GW;
Thread GatewayThreadobj = null;
Thread StatusThreadobj = null;
private TallysmanReceiveThread TallysmanReceiveConnection = null;
private Thread TallysmanReceiveThreadObj = null;
private bool connecting = false;
private BindingList<String> eventsList = new BindingList<String>();
public static DBvehiclesManager vehiclesManager;
public static List<Vehicles> vehicles = new List<Vehicles>();
public MainForm()
{
InitializeComponent();
// set datasource for events
rlvEvents.DataSource = null;
rlvEvents.DataSource = eventsList;
Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
SafeMobileLib.Utils.WriteLine(lbVersion.Text = "Version: " + v.ToString(), ConsoleColor.Cyan);
//this.Text += " - v." + v.ToString();
SM.Debug("Starting Connect plus GW ",false );
CPlusGW.TextQueue.PostItem("Starting Connect plus GW ");
CPlusGW.TextQueue.PostItem("Version: " + v.ToString());
GW = new CPlusGW();
GUIinitSettings();
StartTallysmanService();
}
private void btnSave_Click(object sender, EventArgs e)
{
if ((tbCntrlIP.Text == "") ||
(tbARSport.Text == "") ||
(tbGPSport.Text == "") ||
(tbSMSport.Text == "") ||
(tbTallysmanport.Text == "") ||
(tbGPS_R_port.Text == "") ||
(tbSMS_R_port.Text == "") ||
(tbMultiIP.Text == "") ||
(tbMultiPort.Text == "") ||
(tbGWid.Text == "") ||
(tbReportInterval.Text == "")
)
{
RadMessageBox.Show("Please make sure that all textboxes are compleated with coresponding values.", "Invalid input", MessageBoxButtons.OK, RadMessageIcon.Error);
return;
}
Config.SaveConfig(tbCntrlIP.Text, tbARSport.Text, tbGPSport.Text, tbSMSport.Text, tbTallysmanport.Text,
tbGPS_R_port.Text, tbSMS_R_port.Text, tbMultiIP.Text, tbMultiPort.Text, tbGWid.Text,
tbReportInterval.Text);
RadMessageBox.Show("Configs saved to file.\r\nPlease restart the application for this settings to take place.", "Saved", MessageBoxButtons.OK, RadMessageIcon.Info);
}
private void GUIinitSettings()
{
tbGWid.Text = Program.cfg.gatewayID;
tbCntrlID.Text = "10000";
tbReportInterval.Text = Program.cfg.report;
tbCntrlIP.Text = Program.cfg.ctrlIP;
tbARSport.Text = Program.cfg.arsPort.ToString();
tbGPSport.Text = Program.cfg.locPort.ToString();
tbGPS_R_port.Text = Program.cfg.locPort_r.ToString();
tbSMSport.Text = Program.cfg.smsPort.ToString();
tbSMS_R_port.Text = Program.cfg.smsPort_r.ToString();
tbTallysmanport.Text = Program.cfg.tallysmanPort.ToString();
tbMultiIP.Text = Program.cfg.multi_IP;
tbMultiPort.Text = Program.cfg.multi_port.ToString();
CPlusGW.TextQueue.PostItem(DateTime.Now.ToString());
CPlusGW.TextQueue.PostItem("Welcome to Safemobile Connect plus Gateway.");
CPlusGW.TextQueue.PostItem("Controller status:"+((GW.status)?"Online":"Offline"));
}
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
if (!GW.status && !connecting)
{
CPlusGW.TextQueue.PostItem("Connecting to " + tbCntrlIP.Text);
GatewayThreadobj = new Thread(new ThreadStart(GW.Start));
GatewayThreadobj.IsBackground = true;
GatewayThreadobj.Start();
btnConnect.Text = "Disconnect";
//btnConnect.Enabled = false;
connecting = true;
}
else
{
CPlusGW.TextQueue.PostItem("Disconnecting from " + tbCntrlIP.Text);
GW.Stop();
GatewayThreadobj.Abort();
btnConnect.Text = "Connect";
connecting = false;
}
}
catch (Exception ex)
{
SM.Debug(ex.ToString());
CPlusGW.TextQueue.PostItem("Error connecting/diconnecting from controller.");
}
}
void StartTallysmanService()
{
bool writeGatewayTallysmanStatus = false;
//create thread for receiving Tallysman notifications from the radios
TallysmanReceiveConnection = new TallysmanReceiveThread((ushort)Program.cfg.tallysmanPort);
#region TALLYSMAN Received Events
TallysmanReceiveConnection.OnLocationReceived += delegate (LocationEventArgs e)
{
SafeMobileLib.Utils.WriteLine("LOCATION RECEIVED ON TALLYSMAN from radio " + e.RadioID, ConsoleColor.DarkGreen);
};
TallysmanReceiveConnection.OnTelemetryReceived += delegate (TelemetryReceivedEventArgs e)
{
SafeMobileLib.Utils.WriteLine("TELEMETRY RECEIVED ON TALLYSMAN from radio " + e.RadioID, ConsoleColor.DarkGreen);
};
TallysmanReceiveConnection.OnTallysmanEventReceived += delegate (TallysmanEventArgs e)
{
SafeMobileLib.Utils.WriteLine("EVENT RECEIVED ON TALLYSMAN from radio " + e.RadioID, ConsoleColor.DarkGreen);
};
#endregion
TallysmanReceiveThreadObj = new Thread(new ThreadStart(TallysmanReceiveConnection.handleConnection));
TallysmanReceiveThreadObj.IsBackground = true;
TallysmanReceiveThreadObj.Start();
}
//while(true) - this method is threaded with StatusThreadobj
private void CheckNewStatusEntry()
{
while (StatusThreadobj.IsAlive)
{
string text = CPlusGW.TextQueue.GetItem(-1);
if (text != null && text.Trim().Length > 0)
AddEvent(text);
}
}
private void AddEvent(String msg)
{
this.Invoke((MethodInvoker)delegate ()
{
rlvEvents.BeginEdit();
eventsList.Add($"{DateTime.Now:HH:mm:ss} | {msg}");
// remove first item if more thatn 200
if (eventsList.Count > 200)
eventsList.RemoveAt(0);
rlvEvents.EndEdit();
});
}
#region UPDATE
/// <summary>
/// Check for new version release on SafeMobile Portal
/// The check is done by comparing Current Assembly Version with the one
/// written in a xml file on the portal.
/// </summary>
private void CheckForUpdate()
{
App appType = App.GW_CPLUS;
AutoUpdate au = new AutoUpdate(appType) { IsDevelop = Program.cfg.isDevelop };
au.OnNewVersionAvailable += delegate (string version)
{
notifyIcon1.Text = "New version available";
notifyIcon1.ShowBalloonTip(2500, $"{App.GetAppName(appType)} version {version} available",
$"Press to download and install the latest version of {App.GetAppName(appType)}.", ToolTipIcon.Info);
notifyIcon1.BalloonTipClicked += delegate (object sender, EventArgs e)
{
if (notifyIcon1.Text.Equals("New version available"))
{
UpdateForm uf = new UpdateForm(appType, true) { IsDevelop = Program.cfg.isDevelop };
uf.Show();
}
};
};
// call method to check for new updated
au.CheckUpdate();
}
#endregion
private void MainForm_Load(object sender, EventArgs e)
{
vehiclesManager = new DBvehiclesManager(Program.cfg.DB_IP, Program.cfg.DB_schema, Program.cfg.DB_user, Program.cfg.DB_passwd, Program.cfg.DB_port);
Boolean hasOnlyIntegerValues = false;
vehicles = vehiclesManager.getAllVehicles(out hasOnlyIntegerValues, false);
StatusThreadobj = new Thread(new ThreadStart(CheckNewStatusEntry));
StatusThreadobj.IsBackground = true;
StatusThreadobj.Start();
ContextMenu contextMenu = new ContextMenu();
contextMenu.MenuItems.Add("Check for updates...", (s, e2) => {
UpdateForm uf = new UpdateForm(App.GW_CPLUS, true) { IsDevelop = Program.cfg.isDevelop };
uf.Show();
});
contextMenu.MenuItems.Add("Exit", (s, e2) => CloseApp());
notifyIcon1.ContextMenu = contextMenu;
// check for a new version if available
if (Program.cfg.autoupdate)
CheckForUpdate();
// restore previous value
rcbAutoReconnect.Checked = Program.cfg.autoReconnect;
rcbAutoReconnect.ToggleStateChanged += rcbAutoReconnect_ToggleStateChanged;
// auto reconnect to the controller if this option was selected
if (Program.cfg.autoReconnect)
btnConnect.PerformClick();
}
private void CloseApp()
{
Process oldProcess = Process.GetCurrentProcess();
oldProcess.Kill();
Application.Exit();
}
private void rcbAutoReconnect_ToggleStateChanged(object sender, StateChangedEventArgs args)
{
Program.cfg.SaveConfigOption("GATEWAY", "autoReconnect", rcbAutoReconnect.Checked ? "true" : "false");
Program.cfg.autoReconnect = rcbAutoReconnect.Checked;
}
}
}