SafeNet/.svn/pristine/71/712cd900a2c1d2beacfb4f51497f7ce327e003d3.svn-base
2021-02-24 13:50:23 +02:00

268 lines
8.6 KiB
Plaintext

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace Simulator
{
class Unit
{
private string imei;
private int gpsInterval;
private NetworkStream ns;
private bool arsValue = false; //on/off
public UdpClient udp;
public double lat = 42.061306;
public double lng = -88.025755;
public Int64 unitID_Int64;
Thread t_ARS;
Thread t_GPS;
bool arsON = false;
bool gpsON = false;
public Unit(string _imei, int _gpsInterval, NetworkStream _ns)
{
imei = _imei;
gpsInterval = _gpsInterval;
ns = _ns;
arsValue = true;//temp all unit are ON
unitID_Int64 = Convert.ToInt64(imei);
}
public void Start()
{
Console.WriteLine("Starting ARS thread for unit:"+imei);
if (t_ARS != null)
{
arsON = false;
Thread.Sleep(3000);
t_ARS.Abort();
t_ARS = null;
}
arsON = true;
t_ARS = new Thread(new ThreadStart(SendARS));
t_ARS.IsBackground = false;
t_ARS.Start();
Console.WriteLine("Starting GPS thread for unit:" + imei);
if (t_GPS != null)
{
gpsON = false;
Thread.Sleep(3000);
t_GPS.Abort();
t_GPS = null;
}
gpsON = true;
t_GPS = new Thread(new ThreadStart(SendGPS));
t_GPS.IsBackground = false;
t_GPS.Start();
}
public void Stop()
{
if (t_ARS != null)
{
arsON = false;
Thread.Sleep(3000);
t_ARS.Abort();
t_ARS = null;
}
if (t_GPS != null)
{
gpsON = false;
Thread.Sleep(3000);
t_GPS.Abort();
t_GPS = null;
}
}
private void SendARS()
{
while (arsON)
{
try
{
Console.WriteLine("Send ARS for unit:" + imei + "arsValue:" + arsValue);
byte[] idtmp = Encoding.Unicode.GetBytes(imei);
byte[] idtmpX = (arsValue) ? Encoding.Unicode.GetBytes("1") : Encoding.Unicode.GetBytes("0");
int totalLeN = 7 + idtmp.Length + 2 + idtmpX.Length;
byte[] msg = new byte[totalLeN];
//length
msg[0] = 0x00;
msg[1] = 0x13;
//decodeID
msg[2] = 0x04;
//dialogID
msg[3] = 0x12;
msg[4] = 0x34;
// body length
msg[5] = 0x0A;
msg[6] = (byte)idtmp.Length;
for (int i = 0; i < idtmp.Length; i++)
{
msg[i + 7] = idtmp[i];
}
msg[7 + idtmp.Length] = 0x0E;
msg[7 + idtmp.Length + 1] = (byte)idtmpX.Length;
for (int i = 0; i < idtmpX.Length; i++)
{
msg[7 + idtmp.Length + 2 + i] = idtmpX[i];
}
//Console.WriteLine("Length:" + totalLeN.ToString() + " msg Lenght:" + msg.Length.ToString());
TCPServer.Send(msg, msg.Length, ns);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Thread.Sleep(1000 * 5);
}
}
private void SendGPS()
{
while (gpsON)
{
try
{
//Console.WriteLine("Send Mess ON UDP unit:"+unitID+"unit State:"+unitState);
Byte[] sendBytes = { 0x0D, 0x1F, 0x22, 0x04, 0x24, 0x68, 0xAC, 0xE0, 0x26, 0x0C, 0x00, 0x03, 0xEA, 0x34, 0x1F, 0x4D, 0xBC, 0x77, 0x80, 0x51, 0x11, 0x8E, 0xCD, 0x8D, 0x11, 0x8A, 0xD4, 0x7B, 0x00, 0x63, 0x6C, 0x00, 0x06 };
sendBytes[12] = (byte)(unitID_Int64 % 256);
sendBytes[11] = (byte)((Int32)(unitID_Int64 / 256) % 256);
sendBytes[10] = (byte)(unitID_Int64 / 256 * 256);
DateTime timeNow = DateTime.Now;
Int64 timeInt = timeNow.Second + 64 * timeNow.Minute + 64 * 64 * timeNow.Hour + 64 * 64 * 32 * timeNow.Day + 64 * 64 * 32 * 32 * timeNow.Month + 64 * 64 * 32 * 32 * 16 * (timeNow.Year % 64);
//settime
sendBytes[18] = (byte)(timeInt % 256);
sendBytes[17] = (byte)((Int64)(timeInt / 256) % 256);
sendBytes[16] = (byte)((Int64)(timeInt / (256 * 256)) % 256);
sendBytes[15] = (byte)((Int64)(timeInt / (256 * 256 * 256)));
sendBytes[14] = (byte)(timeNow.Year / 64);
//Int64 latInt = sendBytes[20] * 256 * 256 * 256 + sendBytes[21] * 256 * 256 + sendBytes[22] * 256 + sendBytes[23];
//Console.WriteLine("latINT for 22.3344 ="+latInt);
//setLAT
Byte[] lat_arr = Invers_Lat(lat);
//setLAT
sendBytes[23] = lat_arr[0];
sendBytes[22] = lat_arr[1];
sendBytes[21] = lat_arr[2];
sendBytes[20] = lat_arr[3];
//setLNG
Byte[] lng_arr = Invers_LNG(lng);
sendBytes[27] = lng_arr[0];
sendBytes[26] = lng_arr[1];
sendBytes[25] = lng_arr[2];
sendBytes[24] = lng_arr[3]; ;
Random random = new Random();
int numSpeed1 = random.Next(16);
int numSpeed2 = random.Next(16);
//setSpeed
sendBytes[32] = (byte)numSpeed1;//0x0F;
sendBytes[31] = (byte)numSpeed2;//0x0F;
//incriment LAT LNG
double numInc = random.NextDouble();
double d_num = numInc/10000;
lat = lat - d_num;
lng = lng + d_num;
if (arsValue)
{
udp = new UdpClient();
udp.Send(sendBytes, sendBytes.Length, new IPEndPoint(IPAddress.Parse(Program.UDP_IP), Convert.ToInt32(Program.UDP_PORT)));
Console.WriteLine("GPS for:" + imei + " sent date:" + DateTime.Now.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine("Error SendGPS !!! \n" + ex.ToString());
}
Thread.Sleep(1000 * gpsInterval);
}//end while
}
public static Byte[] Invers_Lat(Double l)
{
Byte[] ret = new Byte[4];
Double lat = l;
bool sign = false;
if (lat < 0)
{
sign = true;
lat *= -1;
}
lat *= 2;
lat *= 1024;
lat *= 1024;
lat *= 1024;
lat /= 90;
ulong ul = (ulong)lat;
ret = BitConverter.GetBytes(ul);
if (sign)
ret[3] |= 0x80;
return ret;
}
public static Byte[] Invers_LNG(Double l)
{
Byte[] ret = new Byte[4];
Double lng = l;
bool sign = false;
if (lng < 0)
{
sign = true;
lng *= -1;
lng = 180 - lng;
}
lng *= 4;
lng *= 1024;
lng *= 1024;
lng *= 1024;
lng /= 360;
ulong ul = (ulong)lng;
ret = BitConverter.GetBytes(ul);
if (sign)
ret[3] |= 0x80;
return ret;
}
//String DataToSend = "#"+GetRandomNumber(41.708804, 42.07784)+"#"+GetRandomNumber(-88.092499, -87.686005)+"#";
public static double GetRandomNumber(double minimum, double maximum)
{
Random random = new Random();
double dbl = random.NextDouble() * (maximum - minimum) + minimum;
int a = (int)(dbl * 100000);
double dbl1 = (double)((double)a / 100000);
return dbl1;
}
}
}