SafeNet/Simulator_CS_GTW/Form1.cs

307 lines
14 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 SN_Server;
using SN_Server.ServerMSGS;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
namespace Simulator_CS_GTW
{
public partial class Form1 : Form
{
private System.Threading.Timer tServerConnection;
private System.Threading.Timer tPingPong;
private System.Threading.Timer tCheckCSList;
private System.Threading.Timer tCheckUnitList;
private System.Threading.Timer tCheckSMSlist;
private System.Threading.Timer tCheckPolllist;
private System.Threading.Timer tReportPollACK;
private System.Threading.Timer tReportTM;
private System.Threading.Timer tReportARS;
private System.Threading.Timer tReportPosition;
private ServerConnector _sc;
private bool isConnected = false;
private string localIP = "127.0.0.1";
//private String serial = "000B68542G52";
//private String[] serials = { "000B28063B53", "000B68542G52", "051W3213H340", "005G8544W550", "101B4410A000", "994B1085G890", "95001DSAKA01" };
//private String[] serials = { "000B28063B53", "000B68542G52", "051W3213H340", "005G8544W550", "101B4410A000", "994B1085G890", "95001DSAKA01" };
private UnitList unitList = new UnitList();
public Form1()
{
InitializeComponent();
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
//Console.WriteLine(ni.Name);
if(!ni.Name.Contains("VM"))
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIP = ip.Address.ToString();
Console.WriteLine(ip.Address.ToString());
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Random rnd = new Random();
//serial = serials[rnd.Next(1, 7)];
// serial = GeneratePassword();
}
private void ReportPositionCallBack(Object state)
{
if (cbPosRep.Checked)
{
for (int j = 0; j < unitList.Count; j++)
{
double lat = 43.65354 + j * 0.001;
double lng = -86.39612 + j * 0.001;
double speed = j % 90;
String gpsTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); ;
String msg = GenMsg(0xB011, textBoxSerial.Text, unitList.getUnitAtPosition(j).DBid + "/" + lat + "/"
+ lng + "/" + speed + "/" + gpsTime);
if (_sc != null)
{
byte[] dataMsg = System.Text.Encoding.ASCII.GetBytes(msg);
_sc.Send(dataMsg, dataMsg.Length);
}
}
}
tReportPosition.Change(new TimeSpan(0, 0, Int32.Parse(tbIntARSRep.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
}
private void ReportARSCallBack(Object state)
{
if (cbARSRep.Checked)
{
for (int j=0; j<unitList.Count; j++)
{
String msg = GenMsg(0xB010, textBoxSerial.Text, unitList.getUnitAtPosition(j).DBid + "/ON");
if (_sc != null)
{
byte[] dataMsg = System.Text.Encoding.ASCII.GetBytes(msg);
_sc.Send(dataMsg, dataMsg.Length);
}
}
}
tReportARS.Change(new TimeSpan(0, 0, Int32.Parse(tbIntARSRep.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
}
public static string GenMsg(int opcode, string serialNr, string paylaod)
{
string temp = "#" + opcode.ToString("X4") + "#" + serialNr + "#" + paylaod + "#";
int strlen = ("#" + (temp.Length).ToString() + temp).Length;
temp = "#" + strlen.ToString() + temp;
return temp;
}
private void ReportTMCallBack(Object state)
{
if (cbTMRep.Checked)
{
Console.WriteLine("#####################\nUnitList is " + unitList.Count);
for (int j = 0; j < unitList.Count; j++)
{
String msg = GenMsg(0xB012, textBoxSerial.Text, unitList.getUnitAtPosition(j).DBid + "/simulator message " + j);
//String msg = "#47#B012#95001DSAKA01#1929/simulator message 1##47#B012#95001DSAKA01#1928/simulator message 2##47#B012#95001DSAKA01#1927/simulator message 3##47#B012#95001DSAKA01#1926/simulator message 4##47#B012#95001DSAKA01#1925/simulator message 5##47#B012#95001DSAKA01#1924/simulator message 6##47#B012#95001DSAKA01#1923/simulator message 7##47#B012#95001DSAKA01#1922/simulator message 8##47#B012#95001DSAKA01#1921/simulator message 9##48#B012#95001DSAKA01#1920/simulator message 10##48#B012#95001DSAKA01#1919/simulator message 11##48#B012#95001DSAKA01#1918/simulator message 12##48#B012#95001DSAKA01#1917/simulator message 13##48#B012#95001DSAKA01#1916/simulator message 14##48#B012#95001DSAKA01#1915/simulator message 15##48#B012#95001DSAKA01#1914/simulator message 16##48#B012#95001DSAKA01#1913/simulator message 17##48#B012#95001DSAKA01#1912/simulator message 18##48#B012#95001DSAKA01#1911/simulator message 19##48#B012#95001DSAKA01#1910/simulator message 20##48#B012#95001DSAKA01#1909/simulator message 21##48#B012#95001DSAKA01#19";
if (_sc != null)
{
byte[] dataMsg = System.Text.Encoding.ASCII.GetBytes(msg);
_sc.Send(dataMsg, dataMsg.Length);
}
//String msg = "#49#B012#95001DSAKA01#91907/simulator message 23#";
//msg = "08/simulator message 22##48#B012#95001DSAKA01#1907/simulator message 23##48#B012#95001DSAKA01#1906/simulator message 24##48#B012#95001DSAKA01#1905/simulator message 25##48#B012#95001DSAKA01#1904/simulator message 26##48#B012#95001DSAKA01#1903/simulator message 27##48#B012#95001DSAKA01#1902/simulator message 28##48#B012#95001DSAKA01#1901/simulator message 29##48#B012#95001DSAKA01#1900/simulator message 30##48#B012#95001DSAKA01#1899/simulator message 31##48#B012#95001DSAKA01#1898/simulator message 32##48#B012#95001DSAKA01#1897/simulator message 33##48#B012#95001DSAKA01#1896/simulator message 34##48#B012#95001DSAKA01#1895/simulator message 35##48#B012#95001DSAKA01#1894/simulator message 36##48#B012#95001DSAKA01#1893/simulator message 37#";
}
}
tReportTM.Change(new TimeSpan(0, 0, Int32.Parse(tbIntTMRep.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
}
private void ReportPollACKCallBack(Object state)
{
/*
if (cbPollRep.Checked)
{
String msg = "#23#9912#" + serial + "##";
if (_sc != null)
{
byte[] dataMsg = System.Text.Encoding.ASCII.GetBytes(msg);
_sc.Send(dataMsg, dataMsg.Length);
}
}
tReportPollACK.Change(new TimeSpan(0, 0, Int32.Parse(tbIntPollRep.Text)), new TimeSpan(System.Threading.Timeout.Infinite));*/
}
private string GeneratePassword()
{
string strPwdchar = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string strPwd = "";
Random rnd = new Random();
for (int i = 0; i < 12; i++)
{
int iRandom = rnd.Next(0, strPwdchar.Length - 1);
strPwd += strPwdchar.Substring(iRandom, 1);
}
return strPwd;
}
private void ServerConnectionCallBack(Object state)
{
if (_sc == null)
{
_sc = new ServerConnector(textBoxSerial.Text);
_sc.OnNewDataRecv += new ServerConnector.newData4Send(_sc_OnNewDataRecv);
}
}
void _sc_OnNewDataRecv(string data)
{
try
{
data = data.Trim();
//Console.WriteLine("RX: " + data);
parseServerMsg(data);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void parseServerMsg(string data)
{
ServerMSG sm = new ServerMSG(data, data.Length);
switch (sm.OPcode)
{
case 0x7701:
{
SM_UnitList smUnit = new SM_UnitList(sm);
unitList.Add(smUnit.unit);
Console.WriteLine("Units size: " + unitList.Count);
break;
}
case 0x7702:
{
SM_UnitList smUnit = new SM_UnitList(sm);
//TODO
//send start GPS trigger
Console.WriteLine("[TODO]Must Send start GPS to :" + smUnit.unit.RadioID + " interval" + smUnit.unit.Reporting);
break;
}
}
}
private void PingCallBack(Object state)
{
if (cbPP.Checked)
{
String ping = GenMsg(0xB0B0, textBoxSerial.Text, localIP);
byte[] dataPong = System.Text.Encoding.ASCII.GetBytes(ping);
if(_sc != null)
_sc.Send(dataPong, dataPong.Length);
}
tPingPong.Change(new TimeSpan(0, 0, Int32.Parse(tbIntPP.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
}
private void CSListCallBack(Object state)
{
if (cbCSListReq.Checked)
{
byte[] dataMsg = SM_IPList.GenRequestMsg();
if(_sc != null)
_sc.Send(dataMsg, dataMsg.Length);
}
tCheckCSList.Change(new TimeSpan(0, 0, Int32.Parse(tbIntCSListReq.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
}
private void UnitListCallBack(Object state)
{
if (cbUnitReq.Checked)
{
byte[] dataMsg = SM_UnitList.GenRequestMsg();
if (_sc != null)
{
_sc.Send(dataMsg, dataMsg.Length);
}
}
tCheckUnitList.Change(new TimeSpan(0, 0, Int32.Parse(tbIntUnitReq.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
}
private void TMListCallBack(Object state)
{
if (cbTMReq.Checked)
{
String msg = GenMsg(0x0012, textBoxSerial.Text, "");
if (_sc != null)
{
byte[] dataMsg = System.Text.Encoding.ASCII.GetBytes(msg);
_sc.Send(dataMsg, dataMsg.Length);
}
}
tCheckSMSlist.Change(new TimeSpan(0, 0, Int32.Parse(tbIntTMReq.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
}
private void PollListCallBack(Object state)
{
if (cbPollReq.Checked)
{
String msg = GenMsg(0x0011, textBoxSerial.Text, "");
if (_sc != null)
{
byte[] dataMsg = System.Text.Encoding.ASCII.GetBytes(msg);
_sc.Send(dataMsg, dataMsg.Length);
}
}
tCheckPolllist.Change(new TimeSpan(0, 0, Int32.Parse(tbIntPollReq.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
}
private void button1_Click(object sender, EventArgs e)
{
if (!isConnected)
{
tServerConnection = new System.Threading.Timer(ServerConnectionCallBack, null, new TimeSpan(0, 0, 1), new TimeSpan(System.Threading.Timeout.Infinite));
tPingPong = new System.Threading.Timer(PingCallBack, null, new TimeSpan(0, 0, 2), new TimeSpan(System.Threading.Timeout.Infinite));
tCheckCSList = new System.Threading.Timer(CSListCallBack, null, new TimeSpan(0, 0, Int32.Parse(tbIntCSListReq.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
tCheckUnitList = new System.Threading.Timer(UnitListCallBack, null, new TimeSpan(0, 0, Int32.Parse(tbIntUnitReq.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
tCheckSMSlist = new System.Threading.Timer(TMListCallBack, null, new TimeSpan(0, 0, Int32.Parse(tbIntTMReq.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
tCheckPolllist = new System.Threading.Timer(PollListCallBack, null, new TimeSpan(0, 0, Int32.Parse(tbIntPollReq.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
tReportPosition = new System.Threading.Timer(ReportPositionCallBack, null, new TimeSpan(0, 0, Int32.Parse(tbIntPosRep.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
tReportTM = new System.Threading.Timer(ReportTMCallBack, null, new TimeSpan(0, 0, Int32.Parse(tbIntTMRep.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
tReportARS = new System.Threading.Timer(ReportARSCallBack, null, new TimeSpan(0, 0, Int32.Parse(tbIntARSRep.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
tReportPollACK = new System.Threading.Timer(ReportPollACKCallBack, null, new TimeSpan(0, 0, Int32.Parse(tbIntPollRep.Text)), new TimeSpan(System.Threading.Timeout.Infinite));
button1.Enabled = false;
}
}
}
}