SafeDispatch/SubscriberAndUserManager/Main.cs

541 lines
22 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 Nini.Config;
using SafeMobileLib;
using Telerik.WinControls;
using RegLibDLL;
using System.Net.Sockets;
using System.Net;
using System.Resources;
using System.Globalization;
namespace SubscriberAndUserManager
{
public partial class Main : Telerik.WinControls.UI.RadRibbonForm
{
private IConfigSource source = null;
public static string CFG_FILE = "SubsConfig.ini";
public static Boolean Hytera =false;
public static Color HyteraColor = Color.Moccasin;
public string DBServer, DBSchema, DBUser, DBPass, DBPort;
private bool DBcleanUp = false;
private DBuserManager db4users;
private DBvehiclesManager db4vehs;
private DBsettingsManager db4settings;
private DBalarmManager db4alarmTypes;
private DBtelemetryManager db4telemetry;
private DBalarmManager db4Alarm;
private DBgatewaysManager db4gateway;
private DBgroupsManager db4groups;
private DBregistrationManager db4Registration;
private DBemailServiceManager db4emailService;
private controlUser usrControl;
private controlUnit unitControl;
private controlSettings setControl;
private controlSystem systemControl;
private controlGateways gatewayControl;
private controlRegistration registrationControl;
private TcpClient client;
private Int32 registrationPort = 5680;
String serverIP = "";
Int32 totalGateways = 0;
Int32 totalGPS = 0;
Int32 totalSMS = 0;
Int32 totalReports = 0;
Int32 totalVoice = 0;
Int32 totalZones = 0;
Int32 totalTelemetry = 0;
Int32 totalGoogle = 0;
Int32 totalUS = 0;
Int32 totalEurope = 0;
Int32 totalArcgis = 0;
public static ResourceManager RManager = new ResourceManager("SubscriberAndUserManager.Language.Language", typeof(Main).Assembly);
public static CultureInfo culture = CultureInfo.CreateSpecificCulture("");
public static Int32 Langidx=0;
public static string returnLNGString(string name)
{
string returnSTR = "";
string temp = RManager.GetString(name, culture);
returnSTR = temp;
if (temp != null)
if (temp.Contains("\\r\\n"))
{
returnSTR = temp.Replace("\\r\\n", "\r\n");
}
return returnSTR;
}
private void setLanguage()
{
tabUnits.Text = returnLNGString("units");
tabUser.Text = returnLNGString("users");
tabSettings.Text = returnLNGString("sett");
tabSystem.Text = returnLNGString("telem");
tabRegistration.Text = returnLNGString("regs");
tabGateways.Text = returnLNGString("gate");
btnAddUnit.Text = returnLNGString("add");
btnGroupAdd.Text = returnLNGString("add");
btnUserAdd.Text = returnLNGString("add");
btnUnitDelete.Text = returnLNGString("del");
btnGroupDelete.Text = returnLNGString("del");
btnUserDelete.Text = returnLNGString("del");
btnUnitEdit.Text = returnLNGString("edit");
btnGroupEdit.Text = returnLNGString("edit");
btnUserEdit.Text = returnLNGString("edit");
btnSaveSettings.Text = returnLNGString("saveset");
btnSaveSettings4User.Text = returnLNGString("saveset");
btnTelemetrySave.Text = returnLNGString("save");
radRibbonBarGroup1.Text = returnLNGString("sett");
radRibbonBarGroup2.Text = returnLNGString("units");
radRibbonBarGroup6.Text = returnLNGString("group");
radRibbonBarGroup4.Text = returnLNGString("sett");
radRibbonBarGroup3.Text = returnLNGString("sett");
radRibbonBarGroup5.Text = returnLNGString("sdset");
}
#region Constructor
public Main()
{
SM debug = new SM();
LoadConfig();
db4Registration = new DBregistrationManager(DBServer, DBSchema, DBUser, DBPass, DBPort);
if (DBServer.Equals("localhost"))
{
serverIP = "127.0.0.1";
}
else
{
serverIP = DBServer;
}
getRegistration();
db4users = new DBuserManager(DBServer, DBSchema, DBUser, DBPass, DBPort);
db4vehs = new DBvehiclesManager(DBServer, DBSchema, DBUser, DBPass, DBPort);
db4settings = new DBsettingsManager(DBServer, DBSchema, DBUser, DBPass, DBPort);
db4telemetry = new DBtelemetryManager(DBServer, DBSchema, DBUser, DBPass, DBPort);
db4Alarm = new DBalarmManager(DBServer, DBSchema, DBUser, DBPass, DBPort);
db4gateway = new DBgatewaysManager(DBServer, DBSchema, DBUser, DBPass, DBPort);
db4groups = new DBgroupsManager(DBServer, DBSchema, DBUser, DBPass, DBPort);
db4emailService = new DBemailServiceManager(DBServer, DBSchema, DBUser, DBPass, DBPort);
//cleanup DB from old IMEIS
if (DBcleanUp) DB_Cleanup();
if (Hytera)
{
ThemeResolutionService.ApplicationThemeName = "Desert";
this.AllowAero = false;
this.Icon = global::SubscriberAndUserManager.Properties.Resources.SYTADMIN24x;
}
// ThemeResolutionService.ApplicationThemeName = "Breeze";
//show login form
Login lgForm;
if (totalGateways > 0)
{
lgForm = new Login(db4users, true);
}
else
{
lgForm = new Login(db4users, false);
}
DialogResult dr = lgForm.ShowDialog();
Console.WriteLine("dr=" + dr);
//if login form was closed without entering a correct password exit
if (dr != DialogResult.OK) System.Environment.Exit(0);
else
{
switch (Main.Langidx)
{
case 0:
culture = CultureInfo.CreateSpecificCulture("en");
break;
case 1:
culture = CultureInfo.CreateSpecificCulture("zh-CN");
break;
case 2:
culture = CultureInfo.CreateSpecificCulture("cs");
break;
case 3:
culture = CultureInfo.CreateSpecificCulture("fr");
break;
case 4:
culture = CultureInfo.CreateSpecificCulture("de");
break;
case 5:
culture = CultureInfo.CreateSpecificCulture("it");
break;
case 6:
culture = CultureInfo.CreateSpecificCulture("pl");
break;
case 7:
culture = CultureInfo.CreateSpecificCulture("pt");
break;
case 8:
culture = CultureInfo.CreateSpecificCulture("ro");
break;
case 9:
culture = CultureInfo.CreateSpecificCulture("ru");
break;
case 10:
culture = CultureInfo.CreateSpecificCulture("sr");
break;
case 11:
culture = CultureInfo.CreateSpecificCulture("es");
break;
case 12:
culture = CultureInfo.CreateSpecificCulture("sv");
break;
case 13:
culture = CultureInfo.CreateSpecificCulture("tr");
break;
}
InitializeComponent();
setLanguage();
tabUnits.IsSelected = true;
radRibbonBar1.RibbonBarElement.QuickAccessToolBar.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
if ((totalGateways == 0) || (totalGPS == 0 && totalSMS == 0 && totalReports == 0 && totalTelemetry == 0
&& totalVoice == 0 && totalZones == 0))
{
radRibbonBar1.Enabled = false;
MainPanel.Enabled = false;
}
}
}
private Boolean getRegistration()
{
Boolean toReturn = false;
try
{
client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), registrationPort);
client.Connect(serverEndPoint);
NetworkStream clientStream = client.GetStream();
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] buffer = encoding.GetBytes("300");
clientStream.Write(buffer, 0, buffer.Length);
Console.WriteLine("Sent registration request to server, waiting...");
byte[] message = new byte[128];
clientStream.ReadTimeout = 10000;
int result = clientStream.Read(message, 0, message.Length);
Console.WriteLine("Received registration from server");
String decodedString = encoding.GetString(message).Trim('\0');
String[] options = decodedString.Split(';');
if (options[0].Equals("301"))
{
totalGateways = Int32.Parse(options[1]);
totalGPS = Int32.Parse(options[2]);
totalSMS = Int32.Parse(options[3]);
totalReports = Int32.Parse(options[4]);
totalVoice = Int32.Parse(options[5]);
totalZones = Int32.Parse(options[6]);
totalTelemetry = Int32.Parse(options[7]);
totalGoogle = Int32.Parse(options[8]);
totalUS = Int32.Parse(options[9]);
totalEurope = Int32.Parse(options[10]);
totalArcgis = Int32.Parse(options[11]);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception in gateway:getRegistration():" + ex.ToString());
RadMessageBox.Show(Main.returnLNGString("cantapp"), Main.returnLNGString("err") ,MessageBoxButtons.OK, RadMessageIcon.Error);
System.Environment.Exit(0);
}
return toReturn;
}
private void LoadConfig()
{
try
{
source = new IniConfigSource(CFG_FILE);
registrationPort = Convert.ToInt32(source.Configs["Registration"].Get("port"));
DBServer = source.Configs["Database"].Get("IP");
DBSchema = source.Configs["Database"].Get("Schema");
DBUser = source.Configs["Database"].Get("User");
DBPort = source.Configs["Database"].Get("Port");
DBPass = source.Configs["Database"].Get("Passwd");
DBcleanUp = Convert.ToBoolean(source.Configs["Database"].Get("deleteOrphanIMEI"));
Hytera = Convert.ToBoolean(source.Configs["Type"].Get("Hytera"));
Langidx = Convert.ToInt32(source.Configs["Language"].Get("Language"));
}
catch (Exception e)
{
Console.WriteLine(e.Message + " " + e.StackTrace + " " + e.Source);
}
}
#endregion
#region tabs load functions
private void radRibbonBar1_CommandTabSelected(object sender, Telerik.WinControls.UI.CommandTabEventArgs args)
{
if (args.CommandTab == tabUser) LoadUserTab();
if (args.CommandTab == tabUnits) LoadUnitsTab();
if (args.CommandTab == tabSystem) LoadSystemTab();
if (args.CommandTab == tabSettings) LoadSettingsTab();
if (args.CommandTab == tabGateways) LoadGatewayTab();
if (args.CommandTab == tabRegistration) LoadRegistrationTab();
}
private void LoadRegistrationTab()
{
registrationControl = new controlRegistration(db4Registration, totalGateways, totalGPS, totalSMS, totalReports, totalVoice, totalZones, totalTelemetry,totalGoogle,totalUS,totalEurope,totalArcgis);
registrationControl.Dock = System.Windows.Forms.DockStyle.Fill;
MainPanel.Controls.Clear();
MainPanel.Controls.Add(registrationControl);
}
private void LoadUserTab()
{
usrControl = new controlUser(db4users, db4vehs,db4settings);
usrControl.Dock = System.Windows.Forms.DockStyle.Fill;
MainPanel.Controls.Clear();
MainPanel.Controls.Add(usrControl);
}
private void LoadUnitsTab()
{
MainPanel.Controls.Clear();
unitControl = new controlUnit(db4users, db4vehs, db4groups);
unitControl.Dock = System.Windows.Forms.DockStyle.Fill;
MainPanel.Controls.Add(unitControl);
}
private void LoadSystemTab()
{
MainPanel.Controls.Clear();
systemControl = new controlSystem(db4vehs,db4telemetry,db4Alarm);
systemControl.Dock = System.Windows.Forms.DockStyle.Fill;
MainPanel.Controls.Add(systemControl);
}
private void LoadSettingsTab()
{
MainPanel.Controls.Clear();
setControl = new controlSettings(DBUser, DBSchema,DBServer,db4emailService,db4Alarm);
setControl.Dock = System.Windows.Forms.DockStyle.Fill;
MainPanel.Controls.Add(setControl);
}
private void LoadGatewayTab()
{
MainPanel.Controls.Clear();
gatewayControl = new controlGateways(db4gateway);
gatewayControl.Dock = System.Windows.Forms.DockStyle.Fill;
MainPanel.Controls.Add(gatewayControl);
}
#endregion
#region users
private void btnUserAdd_Click(object sender, EventArgs e)
{
Console.WriteLine("User form ; mode = ADD");
UsersForm uf = new UsersForm(db4users, db4settings);
uf.FormClosed += new FormClosedEventHandler(uf_FormClosed);
uf.Show();
}
void uf_FormClosed(object sender, FormClosedEventArgs e)
{
LoadUserTab();
}
private void btnUserEdit_Click(object sender, EventArgs e)
{
Console.WriteLine("User form ; mode = EDIT");
if (usrControl != null && usrControl.selectedUser != null)
{
UsersForm uf = new UsersForm(db4users, usrControl.selectedUser);
uf.FormClosed += new FormClosedEventHandler(uf_FormClosed);
uf.Show();
}
else
{
//RadMessageBox.Show("Select an user first","error!", MessageBoxButtons.OK, RadMessageIcon.Error);
CustomMessageBox.Show(CustomMessageBoxType.exclamation, Main.returnLNGString("selfirst"), Main.returnLNGString("err"));
}
}
private void btnUserDelete_Click(object sender, EventArgs e)
{
if (usrControl != null && usrControl.selectedUser != null)
{
if (usrControl.selectedUser.UType == USERTYPE.Admin)
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, Main.returnLNGString("adminUSR"), Main.returnLNGString("err"));
return;
}
DialogResult dr = CustomMessageBox.Show(CustomMessageBoxType.exclamation, Main.returnLNGString("deluser"), Main.returnLNGString("areyou"), true);
if (dr == DialogResult.Yes)
{
db4users.deleteUser(usrControl.selectedUser.UserName);
db4settings.deleteSettingForUser(usrControl.selectedUser.Id);
LoadUserTab();
}
}
else
{
//RadMessageBox.Show("Select an user first", "error!", MessageBoxButtons.OK, RadMessageIcon.Error);
CustomMessageBox.Show(CustomMessageBoxType.exclamation, Main.returnLNGString("selfirst"), Main.returnLNGString("err"));
}
}
#endregion
#region vehicles
private void btnAddUnit_Click(object sender, EventArgs e)
{
List<User> usrList = db4users.getAllUsers();
VehiclesForm vf = new VehiclesForm(db4vehs,db4groups,db4telemetry, usrList);
vf.FormClosed += new FormClosedEventHandler(vf_FormClosed);
vf.Show();
}
void vf_FormClosed(object sender, FormClosedEventArgs e)
{
LoadUnitsTab();
}
private void btnUnitEdit_Click(object sender, EventArgs e)
{
if (unitControl != null && unitControl.selectedVeh != null)
{
List<User> usrList = db4users.getAllUsers();
VehiclesForm vf = new VehiclesForm(db4vehs, db4groups, usrList, unitControl.selectedVeh);
vf.FormClosed += new FormClosedEventHandler(vf_FormClosed);
vf.Show();
}
}
private void btnUnitDelete_Click(object sender, EventArgs e)
{
if (unitControl != null && unitControl.selectedVeh != null)
{
DialogResult dr = CustomMessageBox.Show(CustomMessageBoxType.exclamation, Main.returnLNGString("aredel"), Main.returnLNGString("areyou"), true);
if (dr == DialogResult.Yes)
{
db4vehs.deleteVehicle(unitControl.selectedVeh.Imei);
LoadUnitsTab();
}
}
}
#endregion
private void btnTelemetrySave_Click(object sender, EventArgs e)
{
systemControl.Save();
}
private void btnSaveSettings4User_Click(object sender, EventArgs e)
{
usrControl.SaveSettings();
}
#region groups
private void btnGroupAdd_Click(object sender, EventArgs e)
{
//unitControl.AddNewGroup();
GroupsForm gf = new GroupsForm(db4groups, "add");
gf.Show();
}
private void btnGroupEdit_Click(object sender, EventArgs e)
{
GroupsForm gf = new GroupsForm(db4groups, "edit");
gf.Show();
}
private void btnGroupDelete_Click(object sender, EventArgs e)
{
GroupsForm gf = new GroupsForm(db4groups, "delete");
gf.Show();
}
#endregion
private void btnAddNewGateway_Click(object sender, EventArgs e)
{
//todo: add new gateway in the database
}
private void btnAddNewSafeDispatch_Click(object sender, EventArgs e)
{
//todo: add new SafeDispatch in the database
}
private void btnSaveSettings_Click(object sender, EventArgs e)
{
setControl.SaveEmailServiceSettings();
Console.WriteLine("saving settings for email service");
}
//DB cleanup
private void DB_Cleanup()
{
SM.Debug("DB cleanup function!!!!!!!");
List<SUID_AND_IMEI> list = new List<SUID_AND_IMEI>();
list = db4vehs.getOrphanImeis();
foreach (SUID_AND_IMEI obj in list)
{
SM.Debug("Imei:" + obj.Imei +" found in DB without a vehicle attached!!!",false);
vehResponse resp = db4vehs.CleanupSC_ID(obj.Sc_id);
if (resp == vehResponse.done)
{
SM.Debug("Imei:" + obj.Imei + " removed from DB");
}
else
{
SM.Debug("Imei:" + obj.Imei + " failed to cleanup from DB");
}
}
}
private void Main_Shown(object sender, EventArgs e)
{
if (Hytera)
{
Theme currentTheme = Telerik.WinControls.ThemeResolutionService.GetTheme("Desert");
currentTheme.AddColorBlend("BaseColor", HslColor.FromColor(Color.FromArgb(153, 204, 255)));
Color c1 = Color.Orange;
Color c2 = Color.FromArgb(c1.A, (int)(c1.R * 0.9), (int)(c1.G * 0.9), (int)(c1.B * 0.9));
currentTheme.ThemeProperties["BaseColor"] = HslColor.FromColor(c2);
this.Icon = global::SubscriberAndUserManager.Properties.Resources.SYTADMIN64x;
radRibbonBar1.StartButtonImage = global::SubscriberAndUserManager.Properties.Resources.SYT_admin_mod_2_small;
this.BackColor = HyteraColor;
MainPanel.BackColor = HyteraColor;
btnUserAdd.Image = global::SubscriberAndUserManager.Properties.Resources.addNewOR;
btnAddUnit.Image = global::SubscriberAndUserManager.Properties.Resources.addNewOR;
btnGroupAdd.Image = global::SubscriberAndUserManager.Properties.Resources.addNewOR;
btnUserEdit.Image = global::SubscriberAndUserManager.Properties.Resources.editNewOR;
btnUnitEdit.Image = global::SubscriberAndUserManager.Properties.Resources.editNewOR;
btnGroupEdit.Image = global::SubscriberAndUserManager.Properties.Resources.editNewOR;
btnUserDelete.Image = global::SubscriberAndUserManager.Properties.Resources.deleteNewOR;
btnUnitDelete.Image = global::SubscriberAndUserManager.Properties.Resources.deleteNewOR;
btnGroupDelete.Image = global::SubscriberAndUserManager.Properties.Resources.deleteNewOR;
btnSaveSettings.Image = global::SubscriberAndUserManager.Properties.Resources.write1NewOR;
btnSaveSettings4User.Image = global::SubscriberAndUserManager.Properties.Resources.write1NewOR;
btnTelemetrySave.Image = global::SubscriberAndUserManager.Properties.Resources.write1NewOR;
}
}
}
}