SafeNet/MotoRepeater_SOC/MotoRepeater_GW.cs
2021-02-24 13:50:23 +02:00

328 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SN_Server;
using SafeNetLib;
using SN_Server.ServerMSGS;
using System.Threading;
using System.Collections;
namespace MotoRepeater_SOC
{
class MotoRepeater_GW
{
public static LOGS hLOG;
private ServerConnector connector;
private QueueManagerThread queueManager;
public static UnitList unitList;
private WatcherServerThread ddmsWatcher;
private LocationThread LocationConnection;
private ReceiveSMSThread ReceiveSMSthr = null;
private SendSMSThread SendSMSthr = null;
public static System.Net.Sockets.UdpClient smsUDPclient = null;
private Thread LocationThreadobj = null;
private Thread ReceiveSMSThreadobj = null;
private Thread SendSMSThreadobj = null;
private Thread ServerThreadobjGPS = null;
private Thread ServerThreadobjARS = null;
private Thread ServerThreadobjSMS = null;
private Thread ServerThreadobjPOLL = null;
private bool isUnitLoad = false;
static void Main(string[] args)
{
//start file loging!!!!!!!
hLOG = new LOGS();
MotoRepeater_GW gw = new MotoRepeater_GW();
Console.ReadKey();
}
public MotoRepeater_GW()
{
string test = "#7#one##7#two##9#three##8#four#";
ArrayList strings = ServerMSG.Split(test, test.Length);
foreach (string s in strings)
{
Console.WriteLine(s);
}
unitList = new UnitList();
connector = new ServerConnector("97531");
connector.OnNewDataRecv += new ServerConnector.newData4Send(connector_OnNewDataRecv);
//staRT ping pong
ControlCmdHandler ppHandler = new ControlCmdHandler(connector);
//start queue manager
StartQueueManager(connector);
while (!isUnitLoad)
{
Console.WriteLine("Waiting for units ...next try in 5 secs");
Thread.Sleep(5000);
}
//Console.ReadKey();
unitList.Print();
ddmsWatcher = new WatcherServerThread("127.0.0.1", 3000);
ddmsWatcher.OnMessageRecv += new WatcherServerThread.MessageRecv(ddmsWatcher_OnMessageRecv);
ddmsWatcher.Start();
ddmsWatcher.SubscribeALL(0x2222);
// create thread to listen UDP
LocationConnection = new LocationThread(4001);
//LocationConnection.parent = this;
LocationThreadobj = new Thread(new ThreadStart(LocationConnection.HandleConnection));
LocationThreadobj.IsBackground = true;
LocationThreadobj.Start();
//SMS
smsUDPclient = new System.Net.Sockets.UdpClient(4007);
// create thread to listen UDP
ReceiveSMSthr = new ReceiveSMSThread(4007);
//ARSConnection.parent = this;
ReceiveSMSThreadobj = new Thread(new ThreadStart(ReceiveSMSthr.HandleConnection));
ReceiveSMSThreadobj.IsBackground = true;
ReceiveSMSThreadobj.Start();
// create thread to listen UDP
SendSMSthr = new SendSMSThread(4007);
//ARSConnection.parent = this;
SendSMSThreadobj = new Thread(new ThreadStart(SendSMSthr.HandleConnection));
SendSMSThreadobj.IsBackground = true;
SendSMSThreadobj.Start();
}
private void StartQueueManager(ServerConnector connector)
{
try
{
queueManager = new QueueManagerThread(connector);
//Start GPS server thread
ServerThreadobjGPS = new Thread(new ThreadStart(queueManager.HandleConnectionGPS));
ServerThreadobjGPS.IsBackground = true;
ServerThreadobjGPS.Start();
Thread.Sleep(200);
//Start ARS server thread
ServerThreadobjARS = new Thread(new ThreadStart(queueManager.HandleConnectionARS));
ServerThreadobjARS.IsBackground = true;
ServerThreadobjARS.Start();
Thread.Sleep(200);
//Start SMS server thread
ServerThreadobjSMS = new Thread(new ThreadStart(queueManager.HandleConnectionSMS));
ServerThreadobjSMS.IsBackground = true;
ServerThreadobjSMS.Start();
Thread.Sleep(200);
//Start POLL server thread
ServerThreadobjPOLL = new Thread(new ThreadStart(queueManager.HandleConnectionPOLL));
ServerThreadobjPOLL.IsBackground = true;
ServerThreadobjPOLL.Start();
Thread.Sleep(200);
}
catch (Exception e)
{
Utils.ConsWrite(DebugMSG_Type.always, "Could not intialize the connection with location server!\r\n" + e.Message + "\r\nPlease check your internet connection !!");
//System.Environment.Exit(1);
}
}
private void StopQueueManager()
{
queueManager.Stop();
}
void ddmsWatcher_OnMessageRecv(NotifyRespons resp)
{
if (!resp.Error)
{
string arsState = (resp.Active) ? "ON" : "OFF";
Console.WriteLine("resp.SUID:" + resp.SUID + " value:" + arsState);
//connector.InsertARS(resp.SUID, arsState);
if(resp.Active)
{
PerformeARSon(resp.SUID);
}
try
{
ArsMSG ars = new ArsMSG();
ars.imei = resp.SUID;
ars.msg = arsState;
SN_Queues.arsMsgQueue.PostItem(ars);
}
catch (Exception e)
{
Utils.ConsWrite(DebugMSG_Type.always, "Error inserting ARS in Queue");
Utils.ConsWrite(DebugMSG_Type.always, e.Message);
}
}
}
void connector_OnNewDataRecv(string str)
{
try
{
ServerMSG sm = new ServerMSG(str, str.Length);
switch (sm.OPcode)
{
case 0x7705:
{
SM_IPList smIP = new SM_IPList(sm);
connector.UpdateIPlist(smIP.ipList);
break;
}
case 0x7701:
{
SM_UnitList smUnit = new SM_UnitList(sm);
unitList.Add(smUnit.unit);
//unitList.Print();
isUnitLoad = true;
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;
}
case 0x7711:
{
SN_Server.ServerMSGS.SM_POLLmsg msg = SN_Server.ServerMSGS.SM_GPS.DecodePoll(sm.Payload);
SM_GPS.PrintPoll(msg);
//check to see if we already have this poll
lock (SM_GPS.ht_POLL_List.SyncRoot)
{
if (!SM_GPS.ht_POLL_List.ContainsKey(msg.DBid))
{
//add to poll hashtable
SM_GPS.ht_POLL_List.Add(msg.DBid, msg);
//add to location que
MotoTRBOcmdMsg TRBOmsg = new MotoTRBOcmdMsg();
TRBOmsg.m_cmd = (byte)MotoTRBOcmd.SEND_POLL;
TRBOmsg.m_suid = msg.suid;
TRBOmsg.m_payload = msg.DBid.ToString();
SN_Queues.locationQueue.PostItem(TRBOmsg);
Utils.ConsWrite(DebugMSG_Type.DEV, "Poll for:" + msg.suid + " added to loc queue ");
}
}
SM_GPS.readPOLLQueue.PostItem(msg);
break;
}
case 0x7712:
{
SM_SMS smSMS = new SM_SMS(sm);
SMS msg = new SMS();
msg.Conf = true;
msg.RadioID = smSMS.sms.RadioID;
msg.Msg = smSMS.sms.Msg;
msg.DBid = smSMS.sms.DBid;
//check to see if we already sent the message
lock (SM_SMS.waitConfSMSList.SyncRoot)
{
int index = -1;
int count = 0;
foreach (SMS msgTmp in SM_SMS.waitConfSMSList)
{
if (msg.DBid == msgTmp.DBid)
{
index = count;
break;
}
count++;
}
if (index == -1)//we didn't found the message
{
Utils.ConsWrite(DebugMSG_Type.DB, "Message added to sendSMSQueue");
LOGS.LOG("@SMS@ " + "Message added to sendSMSQueue");
SM_SMS.sendSMSQueue.PostItem(msg);
}
else
{
LOGS.LOG("@SMS@ " + "Message already in SN_Queues.waitConfSMSList .RadioID:" + msg.RadioID + " msg:" + msg.Msg);
}
}
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void PerformeARSon(string SUID)
{
try
{
int suDBid = MotoRepeater_GW.unitList.GetDBid(SUID);
if (suDBid != 0)
{
//get reporting interval
int repInter = unitList.GetReportingInterval(SUID);
Utils.ConsWrite(DebugMSG_Type.GPS, "Interval for SUID:" + SUID + " =" + repInter);
//create location message!!
MotoTRBOcmdMsg msg = new MotoTRBOcmdMsg();
msg.m_cmd = (byte)MotoTRBOcmd.SET_REPORT_INTERVAL;
msg.m_suid = SUID;
msg.m_payload = "30";// repInter.ToString();
if (repInter != 0)
{
SN_Queues.locationQueue.PostItem(msg);
}
else
{
Utils.ConsWrite(DebugMSG_Type.GPS, "SUID:" + SUID + " interval=0 , no GPS reporting needed.");
}
}
else
{
Utils.ConsWrite(DebugMSG_Type.GPS, "!!!!!! SUID:" + SUID + " Not found in DB!!!!!!!");
}
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.ARS, "SUID:" + SUID + " ERROT in PerformeARSon");
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
}
}
}
}