SafeDispatch/SubscriberAndUserManager/VehiclesMobileForm.cs

406 lines
18 KiB
C#
Raw 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 Telerik.WinControls.UI;
using SafeMobileLib;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
using System.Net.Mail;
using System.IO;
using System.Net.Mime;
using System.Threading;
using Telerik.WinControls.Layouts;
using Telerik.WinControls.Enumerations;
namespace SubscriberAndUserManager
{
public partial class VehiclesMobileForm : Telerik.WinControls.UI.RadForm
{
int SC_id = -1;
public VehiclesMobileForm(string RadioIMEI, string unitName)
{
InitializeComponent();
LoadUnit(RadioIMEI, unitName);
// event on email message complete
emailServerSSL.OnMailSendComplete += delegate(bool result)
{
this.Invoke((MethodInvoker)delegate
{
if (!result)
// RadMessageBox.Show("Email settings are invalid", "Send Email Failed", MessageBoxButtons.OK, RadMessageIcon.Error);
//else
// RadMessageBox.Show("Email has been sent.", "Send Email Success", MessageBoxButtons.OK, RadMessageIcon.Info);
CustomMessageBox.Show(CustomMessageBoxType.exclamation, "Email settings are invalid", "Send Email Failed");
else
CustomMessageBox.Show(CustomMessageBoxType.check, "Email has been sent.", "Send Email Success");
});
};
RadButtonElement button = new RadButtonElement();
button.Click += delegate(object sender, EventArgs e)
{
if (tbSipPswd.PasswordChar.Equals('*'))
{
tbSipPswd.PasswordChar = tbSipLogin.PasswordChar;
}
else
{
tbSipPswd.PasswordChar = '*';
}
};
button.Padding = new Padding(0, 0, 0, 0);
button.Margin = new Padding(0, 0, 0, 0);
button.Image = SubscriberAndUserManager.Properties.Resources.eye2;
button.Size = new System.Drawing.Size(18, 18);
RadTextBoxItem tbItem = tbSipPswd.TextBoxElement.TextBoxItem;
tbSipPswd.TextBoxElement.Children.Remove(tbItem);
StackLayoutElement stackPanel = new StackLayoutElement();
stackPanel.Orientation = Orientation.Horizontal;
stackPanel.Margin = new Padding(0, 0, 0, 0);
stackPanel.Children.Add(button);
DockLayoutPanel dockPanel = new DockLayoutPanel();
dockPanel.Children.Add(stackPanel);
dockPanel.Children.Add(tbItem);
DockLayoutPanel.SetDock(tbItem, Telerik.WinControls.Layouts.Dock.Left);
DockLayoutPanel.SetDock(stackPanel, Telerik.WinControls.Layouts.Dock.Right);
tbSipPswd.TextBoxElement.Children.Add(dockPanel);
tbSipPswd.TextBoxElement.Padding = new Padding(2, 0, 0, 0);
tbItem.Margin = new Padding(0, 0, 0, 0);
}
public VehiclesMobileForm()
{
// TODO: Complete member initialization
}
private string sip_server_ip = "";
private string web_service_ip = "";
private string LanIP = "";
private string NatIP = "";
private string LanSipPort = "";
private string NatSipPort = "";
private string LanSigPort = "";
private string NatSigPort = "";
private DBsettingsManager DBSettings;
private DBvehiclesManager DBvehicle;
private EmailService eService;
private EmailServerSSL emailServerSSL = new EmailServerSSL();
private DBemailServiceManager DBEmailService;
private void LoadUnit(string RadioIMEI, string unitName)
{
tbRadioIMEI.Text = RadioIMEI;
tbName.Text = unitName;
tbFirstName.Text = VehiclesForm.mobile.FirstName;
tbLastName.Text = VehiclesForm.mobile.LastName;
tbPhoneType.Text = VehiclesForm.mobile.PhoneType;
tbSipPswd.Text = VehiclesForm.mobile.SipPswd;
tbEmail.Text = VehiclesForm.mobile.Email;
tbSipLogin.Text = VehiclesForm.mobile.SipLogin;
chbGPS.Checked = VehiclesForm.mobile.HasGPS;
chbSMS.Checked = VehiclesForm.mobile.HasText;
chbVoice.Checked = VehiclesForm.mobile.HasVoice;
chbTicketing.Checked = VehiclesForm.mobile.HasTicketing;
chbLoneWorker.Checked = VehiclesForm.mobile.IsLoneWorker;
cbManDown.Checked = VehiclesForm.mobile.HasManDown;
cbEmergency.Checked = VehiclesForm.mobile.HasEmergency;
tbLoneworkerInterval.Value = VehiclesForm.mobile.LoneWorkerInterval;
SC_id = VehiclesForm.mobile.Id;
DBSettings = new DBsettingsManager(MainForm.DBServer, MainForm.DBSchema, MainForm.DBUser, MainForm.DBPass, MainForm.DBPort);
DBvehicle = new DBvehiclesManager(MainForm.DBServer, MainForm.DBSchema, MainForm.DBUser, MainForm.DBPass, MainForm.DBPort);
DBEmailService = new DBemailServiceManager(MainForm.DBServer, MainForm.DBSchema, MainForm.DBUser, MainForm.DBPass, MainForm.DBPort);
if(VehiclesForm.isNEW == true)
{
chbGPS.Checked = chbSMS.Checked = chbVoice.Checked = /*chbLoneWorker.Checked = cbManDown.Checked =*/ true;
chbTicketing.Checked = false;
}
sip_server_ip = DBSettings.getSettingValue(0, "lanLinxServer") + ":" + DBSettings.getSettingValue(0, "lanSipPort");
web_service_ip = DBSettings.getSettingValue(0, "lanLinxServer") + ":" + DBSettings.getSettingValue(0, "lanSigPort");
LanIP = DBSettings.getSettingValue(0, "lanLinxServer");
NatIP = DBSettings.getSettingValue(0, "natLinxServer");
LanSipPort = DBSettings.getSettingValue(0, "lanSipPort");
NatSipPort = DBSettings.getSettingValue(0, "natSipPort");
LanSigPort = DBSettings.getSettingValue(0, "lanSigPort");
NatSigPort = DBSettings.getSettingValue(0, "natSigPort");
eService = DBEmailService.GetEmailServiceSettings();
if (LanIP != "" && NatIP != "" && LanSigPort != "" && LanSipPort != "" && NatSipPort != "" && NatSigPort != ""
&& tbSipLogin.Text!= "" && tbSipPswd.Text !="")
btnEncode.PerformClick();
gbQRSettings.Enabled = web_service_ip != "";
if(RadioIMEI.Contains(","))
{
PrepareControlsForMultiEdit(this);
}
}
private void PrepareControlsForMultiEdit(Control container)
{
foreach (Control c in container.Controls)
{
c.Enabled = false;
}
gbQRSettings.Enabled = true;
foreach (Control x in gbQRSettings.Controls)
{
x.Enabled = x is RadCheckBox;
if (x is RadCheckBox)
{
((RadCheckBox)x).IsThreeState = true;
((RadCheckBox)x).ToggleState = ToggleState.Indeterminate;
}
}
btnAdd.Enabled = true;
}
private void SaveUnit()
{
VehiclesForm.mobile.FirstName = tbFirstName.Text;
VehiclesForm.mobile.LastName = tbLastName.Text;
VehiclesForm.mobile.PhoneType = tbPhoneType.Text;
VehiclesForm.mobile.SipPswd = tbSipPswd.Text;
VehiclesForm.mobile.CanUpdate = true;
VehiclesForm.mobile.Email = tbEmail.Text;
VehiclesForm.mobile.SipLogin = tbSipLogin.Text;
VehiclesForm.mobile.HasGPS = chbGPS.Checked;
VehiclesForm.mobile.HasText = chbSMS.Checked;
VehiclesForm.mobile.HasVoice = chbVoice.Checked;
VehiclesForm.mobile.IsLoneWorker = chbLoneWorker.Checked;
VehiclesForm.mobile.HasManDown = cbManDown.Checked;
VehiclesForm.mobile.HasEmergency = cbEmergency.Checked;
VehiclesForm.mobile.LoneWorkerInterval = (int)tbLoneworkerInterval.Value;
VehiclesForm.mobile.IMEI = VehiclesForm.mobile.Company = VehiclesForm.mobile.OS = "";
VehiclesForm.mobile.ToggleStateDictionary = new Dictionary<string, bool>();
VehiclesForm.mobile.AddToHashTableThreeState("HasGPS", chbGPS.ToggleState == ToggleState.Indeterminate);
VehiclesForm.mobile.AddToHashTableThreeState("HasText", chbSMS.ToggleState == ToggleState.Indeterminate);
VehiclesForm.mobile.AddToHashTableThreeState("HasVoice", chbVoice.ToggleState == ToggleState.Indeterminate);
VehiclesForm.mobile.AddToHashTableThreeState("IsLoneWorker", chbLoneWorker.ToggleState == ToggleState.Indeterminate);
VehiclesForm.mobile.AddToHashTableThreeState("HasManDown", cbManDown.ToggleState == ToggleState.Indeterminate);
VehiclesForm.mobile.AddToHashTableThreeState("HasEmergency", cbEmergency.ToggleState == ToggleState.Indeterminate);
//VehiclesForm.mobile.HasTicketing = chbTicketing.Checked;
VehiclesForm.mobile.HasTicketing = false;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (!DBvehicle.isUniqueLogin(tbSipLogin.Text, (SC_id == 0 ? -1 : SC_id)))
{
if (tbSipLogin.Text != "")
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, "Duplicate SIP Login!", MainForm.returnLNGString("err"));
return;
}
}
if (!Utils.IsValidEmail(tbEmail.Text) && tbEmail.Text != "")
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, "Invalid Email!", MainForm.returnLNGString("err"));
return;
}
foreach(Control x in this.Controls)
{
if(x is RadTextBox)
{
x.Text = FixComma(x.Text);
}
}
SaveUnit();
VehiclesForm.hasModifiedMobile = true;
//this.Close();
}
public static String FixComma(String s)
{
if (s == null)
return "";
else
{
String tmp = s;
tmp = tmp.Replace(',', ' ');
tmp = tmp.Replace('\'', '`');
return tmp;
}
}
private void btnEncode_Click(object sender, EventArgs e)
{
if( sip_server_ip == "")
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, "SIP Server IP missing!", MainForm.returnLNGString("err"));
return;
}
if (web_service_ip == "")
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, "WEB Service IP missing!", MainForm.returnLNGString("err"));
return;
}
if (tbSipPswd.Text == "")
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, "SIP Password missing!", MainForm.returnLNGString("err"));
return;
}
if (tbSipPswd.Text.ToString().Length < 4)
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, MainForm.returnLNGString("linxPswsLength"), MainForm.returnLNGString("err"));
return;
}
if (tbSipLogin.Text == "")
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, "SIP Login missing!", MainForm.returnLNGString("err"));
return;
}
else
{
if (!DBvehicle.isUniqueLogin(tbSipLogin.Text, (SC_id == 0 ? -1 : SC_id)))
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, "Duplicate SIP Login!", MainForm.returnLNGString("err"));
return;
}
}
//String data = string.Format("{{ \"sip_server_ip\": \"{0}\", \"web_service_ip\" : \"{1}\", \"sip_login\" : \"{2}\", \"sip_pswd\" : \"{3}\" }}"
// , sip_server_ip, web_service_ip, tbSipLogin.Text, tbSipPswd.Text);
string data = $"{{\"lan\":\"{LanIP}\", \"nat\":\"{NatIP}\",\"sip\":\"{LanSipPort}\",\"lsig\":\"{LanSigPort}\",\"nsig\":\"{NatSigPort}\",\"login\" : \"{tbSipLogin.Text}\", \"pswd\" : \"{tbSipPswd.Text}\"}}";
TripleDESEncryption tes = new TripleDESEncryption();
String encrypted = tes.Encrypt(data);
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = 2;
qrCodeEncoder.QRCodeVersion = 12;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
Image image;
image = qrCodeEncoder.Encode(encrypted);
picEncode.Image = image;
}
private void btnSave_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png";
saveFileDialog1.Title = "Save";
saveFileDialog1.FileName = string.Empty;
saveFileDialog1.ShowDialog();
try
{
// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (saveFileDialog1.FilterIndex)
{
case 1:
this.picEncode.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
this.picEncode.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3:
this.picEncode.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
case 4:
this.picEncode.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Png);
break;
}
fs.Close();
}
}
catch(Exception ex)
{
RadMessageBox.Show(ex.ToString());
}
}
private void btnEmailQR_Click(object sender, EventArgs e)
{
if (!Utils.IsValidEmail(tbEmail.Text))
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, "Invalid Email!", MainForm.returnLNGString("err"));
return;
}
sendMail(tbEmail.Text);
}
private void sendMail(string mailAdr)
{
if (tbRadioIMEI.Text == "")
{
CustomMessageBox.Show(CustomMessageBoxType.exclamation, "Empty Radio ID!", MainForm.returnLNGString("err"));
return;
}
SM.Debug("Sending mail to " + mailAdr);
try
{
string filePath = Environment.CurrentDirectory + @"\qr.png";
MailMessage message = new MailMessage();
message.From = new MailAddress(eService.EmailAddress);
message.To.Add(mailAdr);
message.Body = message.Subject = "QR For Unit " + tbRadioIMEI.Text;
this.picEncode.Image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
string attachmentPath = filePath;
//Attachment inline = new Attachment(attachmentPath);
using (Attachment inline = new Attachment(attachmentPath))
{
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
//inline.ContentId = contentID;
inline.ContentType.MediaType = "image/png";
inline.ContentType.Name = Path.GetFileName(attachmentPath);
message.Attachments.Add(inline);
message.IsBodyHtml = true;
emailServerSSL.sendEmailWithFeedback(eService.SmtpServer, eService.SmtpPort, eService.EmailAddress, eService.Pass, message, eService.IsSmtpSSL);
}
File.Delete(filePath);
}
catch (Exception ex)
{
Console.WriteLine("Exception in sendMail QR Code ", ex.ToString());
//Console.WriteLine(e.ToString());
}
}
private void VehiclesMobileForm_Load(object sender, EventArgs e)
{
}
private void VehiclesMobileForm_Leave(object sender, EventArgs e)
{
btnAdd.PerformClick();
}
private void chbLoneWorker_ToggleStateChanged(object sender, StateChangedEventArgs args)
{
tbLoneworkerInterval.Enabled = chbLoneWorker.Checked;
}
}
}