Code
Code
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace GPSTrackerServer
{
public partial class Service1 : ServiceBase
{
public IPAddress localAddr =
[Link]([Link]["IP"].ToString());
private Thread workerThread = null;
int DeviceCount = 1;
public Service1()
{
InitializeComponent();
}
void connectAscii()
{
//Concox gps = new
Concox([Link]["IP"].ToString(), PortConcox);
while (true)
{
TcpClient client1 = [Link]();
lock (_lock) list_clients.Add(DeviceCount, client1);
int id = (int)o;
TcpClient client;
if (byte_count == 0)
{
break;
}
//2056.3146N
string templat = [Link](24, 10);
string lat =
ConvertDegreeAngleToDouble([Link](0, 2), [Link](2, 7),
[Link](9, 1));//[Link](0, 2) + "." + [Link](2,
[Link] - 1);
//07255.9841E
string templog = [Link](34, 11);
string log =
ConvertDegreeAngleToDouble([Link](0, 3), [Link](3, 7),
[Link](10, 1));
DataSet ds = [Link](strStud,
param);
double lat1 =
[Link]([Link][0].Rows[i]["Latitude"].ToString());
double lat2 =
[Link]([Link][0].Rows[i]["Latitude2"].ToString());
double lng1 =
[Link]([Link][0].Rows[i]["Longitude"].ToString());
double lng2 =
[Link]([Link][0].Rows[i]["Longitude2"].ToString());
protocol = [Link]("$deviceid$",
deviceid);
protocol = [Link]("$geomode$",
"1");
string minlat =
ConvertDoubleDegreeAngle("lat", ((lat1 > lat2) ? lat2 : lat1).ToString());
protocol = [Link]("$lattype$",
[Link]([Link] - 1, 1));
protocol = [Link]("$minlat$",
[Link](0, [Link] - 1));
string maxlat =
ConvertDoubleDegreeAngle("lat", ((lat1 > lat2) ? lat1 : lat2).ToString());
protocol = [Link]("$maxlat$",
[Link](0, [Link] - 1));
string minlng =
ConvertDoubleDegreeAngle("lmg", ((lng1 > lng2) ? lng2 : lng1).ToString());
protocol = [Link]("$lngtype$",
[Link]([Link] - 1, 1));
protocol = [Link]("$minlng$",
[Link](0, [Link] - 1));
string maxlng =
ConvertDoubleDegreeAngle("lmg", ((lng1 > lng2) ? lng1 : lng2).ToString());
protocol = [Link]("$maxlng$",
[Link](0, [Link] - 1));
WriteToFile("protocol1:" + protocol);
byte[] SendData =
[Link](protocol);
NetworkStream Netstream =
[Link]();
[Link](SendData, 0,
[Link]);
}
}
#endregion
#region lock
if ([Link][1].[Link] > 0 && !
[Link][1].Rows[0][0].ToString().Contains("Error"))
{
string protocol = [Link][1].Rows[0]
["ProtocolData"].ToString();
string strlock = ([Link][1].Rows[0]
["lock"].ToString() == "True") ? "0" : "1";
protocol = [Link]("$deviceid$",
deviceid);
protocol = [Link]("$lock$", strlock);
WriteToFile("protocol2:" + protocol);
byte[] SendData =
[Link](protocol);
NetworkStream Netstream = [Link]();
[Link](SendData, 0, [Link]);
}
#endregion
#region overspeed
if ([Link][2].[Link] > 0 && !
[Link][2].Rows[0][0].ToString().Contains("Error"))
{
string protocol = [Link][2].Rows[0]
["ProtocolData"].ToString();
string strhigh = "0" + [Link][2].Rows[0]
["MaxSpeed"].ToString();
string strlow = "0" + [Link][2].Rows[0]
["MinSpeed"].ToString();
protocol = [Link]("$deviceid$",
deviceid);
protocol = [Link]("$high$", strhigh);
protocol = [Link]("$low$", strlow);
WriteToFile("protocol3:" + protocol);
byte[] SendData =
[Link](protocol);
NetworkStream Netstream = [Link]();
[Link](SendData, 0, [Link]);
}
#endregion
#endregion
}
catch (Exception ex)
{
WriteToFile([Link]);
}
}
if ([Link](13, 4) == "BO01")
{
string alarm = [Link](17, 1);
}
//1:Alarm when inside of Geo - fence
//4:Lowerspeed Alert
//5:Overspeed Alert
#region concox
static byte[] loginResponse = { 0x78, 0x78, 0x05, 0x01, 0x00, 0x00, 0x00,
0x00, 0x0D, 0x0A };
static byte[] alarmResponse = { 0x78, 0x78, 0x05, 0x16, 0x00, 0x00, 0x00,
0x00, 0x0D, 0x0A };
#endregion
void connectConcox()
{
while (true)
{
TcpClient client1 = [Link]();
StateObject objstate;
lock (_lock) objstate = Concoxlist_clients[id];
TcpClient client;
lock (_lock) client = [Link];
UInt16 sendCRC = 0;
byte[] serverFlagBit = null;
byte[] stringArray = null;
string stringMessage = "";
byte lengthOfCommand = 0;
while (true)
{
Global g = new Global();
NetworkStream stream = [Link]();
byte[] buffer = new byte[1024];
int byte_count = [Link](buffer, 0, [Link]);
if (byte_count == 0)
{
break;
}
//WriteToFile([Link]("yyyy-MM-dd hh:mm:ss") +
">> " + data);
switch (protocolNumber)
{
case PROTOCOL_NUMBER.LOGIN_MESSAGE:
[Link](loginResponse, 4);
sendCRC =
crc_bytes([Link](2).Take([Link] - 6).ToArray());
loginResponse[[Link] - 4] = (byte)
((sendCRC >> 8) & 0xFF);
loginResponse[[Link] - 3] = (byte)
((sendCRC) & 0xFF);
//
string IMEI =
BytesToString([Link](4).Take(messageLength - 5).ToArray());
[Link] = IMEI;
//[Link] = IMEI;
//Concoxlist_clients[id] = IMEI;
//WriteToFile([Link]("yyyy-MM-dd
hh:mm:ss") + ">> " + IMEI);
//send responce
NetworkStream Netstream = [Link]();
[Link](loginResponse, 0,
[Link]);
break;
case PROTOCOL_NUMBER.LOCATION_DATA:
//WriteToFile([Link]("yyyy-MM-dd
hh:mm:ss") + ">> Client IMEI:" + [Link]);
DateTime date;
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
year = receiveMessage[4];
month = receiveMessage[5];
day = receiveMessage[6];
hour = receiveMessage[7];
minute = receiveMessage[8];
second = receiveMessage[9];
string binarystring =
hex2bin(BytesToString(courseStatus));//[Link]([Link],
[Link](c => [Link](Convert.ToInt32([Link](), 16),
2).PadLeft(4, '0')));
uint number =
BitConverter.ToUInt32([Link]().ToArray(), 0);
double lon = number / 1800000.0;
number =
BitConverter.ToUInt32([Link]().ToArray(), 0);
double lat = number / 1800000.0;
case PROTOCOL_NUMBER.ALARM_DATA:
//first response
int alarmPacketLen = [Link] - 5;
alarmResponse[2] = (byte)(alarmPacketLen & 0xFF);
sendCRC =
crc_bytes([Link](2).Take(alarmPacketLen - 1).ToArray());
alarmResponse[alarmPacketLen + 1] = (byte)((sendCRC >>
8) & 0xFF);
alarmResponse[alarmPacketLen + 2] = (byte)((sendCRC) &
0xFF);
//second response
year = receiveMessage[4];
month = receiveMessage[5];
day = receiveMessage[6];
hour = receiveMessage[7];
minute = receiveMessage[8];
second = receiveMessage[9];
int alarmDataAddressPacketLen =
[Link] - 5;
alarmDataAddressResponse[2] = (byte)
(alarmDataAddressPacketLen & 0xFF);
[Link](alarmDataAddressResponse,
alarmDataAddressPacketLen - 1);
sendCRC =
crc_bytes([Link](2).Take(alarmDataAddressPacketLen -
1).ToArray());
alarmDataAddressResponse[alarmDataAddressPacketLen + 1]
= (byte)((sendCRC >> 8) & 0xFF);
alarmDataAddressResponse[alarmDataAddressPacketLen + 2]
= (byte)((sendCRC) & 0xFF);
//normal
if (Alarm[0] == 0x00)
{
WriteToFile("normal");
}
//SOS
if (Alarm[0] == 0x01)
{
WriteToFile("SOS");
sendNotification("SOS ALERT", "$VehicleName$ SOS
ALERT", "$VehicleNo$ SOS Button Pressed at " + date, [Link],
[Link]("yyyy-MM-dd HH:mm:ss"), "", "", "");
}
//Power Cut
if (Alarm[0] == 0x02)
{
WriteToFile("Power Cut");
sendNotification("POWERCUT ALERT", " $VehicleName$
POWERCUT ALERT", "$VehicleNo$ Power Failure at " + date, [Link],
[Link]("yyyy-MM-dd HH:mm:ss"), "", "", "");
}
//Vibration
if (Alarm[0] == 0x03)
{
WriteToFile("Vibration");
sendNotification("VIBRATION ALERT", "$VehicleName$
VIBRATION ALERT", "$VehicleNo$ Vibration at " + date, [Link],
[Link]("yyyy-MM-dd HH:mm:ss"), "", "", "");
}
//Enter Fence
if (Alarm[0] == 0x04)
{
WriteToFile("Enter Fence");
sendNotification("FENCE ALERT", "$VehicleName$
GEOFENCE ALERT", "$VehicleNo$ Enter Geo Fence at " + date, [Link],
[Link]("yyyy-MM-dd HH:mm:ss"), "In", "", "");
}
//Exit Fence
if (Alarm[0] == 0x05)
{
WriteToFile("Exit Fence");
sendNotification("FENCE ALERT", "$VehicleName$
GEOFENCE ALERT", "$VehicleNo$ Exit Geo Fence at " + date, [Link],
[Link]("yyyy-MM-dd HH:mm:ss"), "Out", "", "");
}
//Over speed
if (Alarm[0] == 0x06)
{
WriteToFile("Over speed");
sendNotification("OVER SPEED", "$VehicleName$
OVERSPEED ALERT", "$VehicleNo$ Overspeed at " + date, [Link],
[Link]("yyyy-MM-dd HH:mm:ss"), "", "", "");
}
//Send([Link], alarmDataAddressResponse);
//send responce
NetworkStream Netstream2 = [Link]();
[Link](alarmDataAddressResponse, 0,
[Link]);
break;
case PROTOCOL_NUMBER.STATUS_INFO:
[Link](heartbeatResponse, 4);
res = UpdateHeartbeatData([Link],
[Link](), "", [Link](), Status,
[Link]("yyyy-MM-dd hh:mm:ss tt"), MainPower);
//WriteToFile("res of status heartbeat:" + res);
sendCRC =
crc_bytes([Link](2).Take([Link] - 6).ToArray());
heartbeatResponse[[Link] - 4] =
(byte)((sendCRC >> 8) & 0xFF);
heartbeatResponse[[Link] - 3] =
(byte)((sendCRC) & 0xFF);
//Send([Link], heartbeatResponse);
//send responce
NetworkStream Netstream3 = [Link]();
[Link](heartbeatResponse, 0,
[Link]);
switch (alarm)
{
//reset cut off alarm
case ALARM.POWER_CUT_ALARM:
int connectOilAndElectricityPacketLen =
[Link] - 5;
[Link](connectOilAndEletricity,
connectOilAndElectricityPacketLen - 1);
sendCRC =
crc_bytes([Link](2).Take([Link] -
6).ToArray());
lengthOfCommand = connectOilAndEletricity[4];
stringArray = new byte[lengthOfCommand -
4]; //do not include server flag bit
[Link](connectOilAndEletricity, 9,
stringArray, 0, lengthOfCommand - 4);
stringMessage =
[Link](stringArray);
//send responce
NetworkStream Netstream4 = [Link]();
[Link](connectOilAndEletricity, 0,
[Link]);
break;
}
break;
case PROTOCOL_NUMBER.STRING_INFO:
lengthOfCommand = receiveMessage[4];
serverFlagBit = new byte[4];
[Link](receiveMessage, 5, serverFlagBit, 0, 4);
stringMessage = [Link](stringArray);
string server = BitConverter.ToInt32(serverFlagBit,
0).ToString();
break;
}
[Link](startCommands, 0, FinalCommands, 0,
[Link]);
[Link](cmdbyte, 0, FinalCommands,
[Link], [Link]);
FinalCommands[[Link] - 6] = 0x00;
FinalCommands[[Link] - 5] = 0x01;
sendCRC =
crc_bytes([Link](2).Take([Link] - 6).ToArray());
FinalCommands[[Link] - 4] = (byte)
((sendCRC >> 8) & 0xFF);
FinalCommands[[Link] - 3] = (byte)
((sendCRC) & 0xFF);
FinalCommands[[Link] - 2] = 0x0D;
FinalCommands[[Link] - 1] = 0x0A;
//protocol = [Link]("$deviceid$",
[Link]);
//protocol = [Link]("$minlat$",
[Link](0, [Link] - 1));
//WriteToFile("protocol1:" + protocol);
#region lock
if ([Link][1].[Link] > 0 && ![Link][1].Rows[0]
[0].ToString().Contains("Error"))
{
WriteToFile("Lock command");
byte[] startCommands = { 0x78, 0x78, 0x00, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00 };
//{ 0x78, 0x78, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x0D, 0x0A };
//string protocol = [Link][1].Rows[0]
["ProtocolData"].ToString();
[Link](startCommands, 0, FinalCommands, 0,
[Link]);
FinalCommands[[Link] - 6] = 0x00;
FinalCommands[[Link] - 5] = 0x01;
sendCRC =
crc_bytes([Link](2).Take([Link] - 6).ToArray());
FinalCommands[[Link] - 4] = (byte)((sendCRC
>> 8) & 0xFF);
FinalCommands[[Link] - 3] = (byte)((sendCRC)
& 0xFF);
FinalCommands[[Link] - 2] = 0x0D;
FinalCommands[[Link] - 1] = 0x0A;
#region overspeed
if ([Link][2].[Link] > 0 && ![Link][2].Rows[0]
[0].ToString().Contains("Error"))
{
WriteToFile("Over Speed");
[Link](startCommands, 0, FinalCommands, 0,
[Link]);
FinalCommands[[Link] - 6] = 0x00;
FinalCommands[[Link] - 5] = 0x01;
sendCRC =
crc_bytes([Link](2).Take([Link] - 6).ToArray());
FinalCommands[[Link] - 4] = (byte)((sendCRC
>> 8) & 0xFF);
FinalCommands[[Link] - 3] = (byte)((sendCRC)
& 0xFF);
FinalCommands[[Link] - 2] = 0x0D;
FinalCommands[[Link] - 1] = 0x0A;
}
#endregion
//{ 0x78, 0x78, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x0D, 0x0A };
//string protocol = [Link][1].Rows[0]
["ProtocolData"].ToString();
string strCommand = "";
if ([Link][3].Rows[0]["Defence"].ToString() == "True")
{
strCommand = "SENALM,ON,0#";
//0/1/2, way of alarming, 0 :GPRS only, 1: SMS+GPRS,
2 : GPRS+SMS+phone call
}
else
{
strCommand = "SENALM,OFF#";
}
[Link](startCommands, 0, FinalCommands, 0,
[Link]);
FinalCommands[[Link] - 6] = 0x00;
FinalCommands[[Link] - 5] = 0x01;
sendCRC =
crc_bytes([Link](2).Take([Link] - 6).ToArray());
FinalCommands[[Link] - 4] = (byte)((sendCRC
>> 8) & 0xFF);
FinalCommands[[Link] - 3] = (byte)((sendCRC)
& 0xFF);
FinalCommands[[Link] - 2] = 0x0D;
FinalCommands[[Link] - 1] = 0x0A;
#region Commands
if ([Link][4].[Link] > 0 && ![Link][4].Rows[0]
[0].ToString().Contains("Error"))
{
WriteToFile("command");
byte[] startCommands = { 0x78, 0x78, 0x00, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00 };
//{ 0x78, 0x78, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x0D, 0x0A };
//string protocol = [Link][1].Rows[0]
["ProtocolData"].ToString();
[Link](startCommands, 0, FinalCommands, 0,
[Link]);
FinalCommands[[Link] - 6] = 0x00;
FinalCommands[[Link] - 5] = 0x01;
sendCRC =
crc_bytes([Link](2).Take([Link] - 6).ToArray());
FinalCommands[[Link] - 4] = (byte)((sendCRC
>> 8) & 0xFF);
FinalCommands[[Link] - 3] = (byte)((sendCRC)
& 0xFF);
FinalCommands[[Link] - 2] = 0x0D;
FinalCommands[[Link] - 1] = 0x0A;
#endregion
return res;
}
return bytes;
}
try
{
if ([Link][0].[Link] > 0 && [Link][0].Rows[0]
[0].ToString() == "1")
{
if ([Link][0].Rows[0]["IsNotification"].ToString() ==
"True")
{
sendNotification("IGNITION ALERT", [Link][0].Rows[0]
["NotificationTitle"].ToString(), [Link][0].Rows[0]
["NotificationBody"].ToString(), DeviceID, Date, "", "", "");
}
else
{
}
}
}
catch (Exception ex)
{
WriteToFile([Link]);
}
return [Link][0].Rows[0][0].ToString();
}
}
//WriteToFile(qry);
for (int i = 0; i < [Link]; i++)
{
[Link](body, Title, [Link][i]
["FCMID"].ToString());
}
}
}
}