SafeNet/SafeNetLib/Debug.cs
2021-02-24 13:50:23 +02:00

193 lines
6.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Collections;
namespace SafeNetLib
{
public class Debug
{
TCPServer tcp;
public Debug(int debugPort)
{
tcp = new TCPServer();
tcp.OnConnectionRecv += new TCPServer.ConnectionRecv(tcp_OnConnectionRecv);
tcp.OnMessageRecv += new TCPServer.MessageRecv(tcp_OnMessageRecv);
tcp.Start(debugPort);
}
void tcp_OnConnectionRecv(NetworkStream ns, string IP)
{
Utils.ConsWrite(DebugMSG_Type.Debug, "Debug client: " + IP);
string msg = "Welcome to Connect+GW remote debug.\n" +
"Type -i for SUinfo;\n" +
"Type -r for Reporting interval;\n" +
"Type -gl for last GPS time table;\n" +
"Type -g for GPS requests pennding messages;\n";
System.Text.Encoding enc = System.Text.Encoding.ASCII; byte[] buf = enc.GetBytes(msg);
tcp.Send(buf, buf.Length, ns);
}
void tcp_OnMessageRecv(byte[] data, int recv, NetworkStream ns)
{
String stringData = System.Text.Encoding.ASCII.GetString(data, 0, recv);
String usefulData = stringData.Trim();
Utils.ConsWrite(DebugMSG_Type.Debug, "Debug RX:" + usefulData);
ProcessRequest(usefulData, ns);
}
private void ProcessRequest(string request, NetworkStream ns)
{
string ret = "";
switch (request)
{
case "-i":
{
ret = Process_ht_SUInfo();
break;
}
case "-r":
{
ret = Process_ht_UnitsInterval();
break;
}
case "-g":
{
ret = Process_ht_pendingMsg();
break;
}
case "-gl":
{
ret = Process_ht_location_hash();
break;
}
default:
{
ret = "Unknown command: " + request;
break;
}
}
System.Text.Encoding enc = System.Text.Encoding.ASCII; byte[] buf = enc.GetBytes(ret);
tcp.Send(buf, buf.Length, ns);
}
private string Process_ht_UnitsInterval()
{
string ret = "Error\n";
try
{
string temp = "ht_UnitsInterval:\n";
lock (SN_Queues.ht_SUInfo.SyncRoot)
{
foreach (DictionaryEntry item in SN_Queues.ht_SUInfo)
{
SUinfo msg = (SUinfo)item.Value;
temp += "suid:" + item.Key + "\t interval:" + msg.repInterval + "\n";
}
}
ret = temp;
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, "ERROR Process_ht_UnitsIntervalt:\n" + ex.ToString());
ret += " " + ex.ToString();
}
return ret;
}
private string Process_ht_SUInfo()
{
string ret = "Error\n";
try
{
string temp = "ht_SUInfo:\n";
lock (SN_Queues.ht_SUInfo.SyncRoot)
{
foreach (DictionaryEntry item in SN_Queues.ht_SUInfo)
{
SUinfo msg = (SUinfo)item.Value;
temp += "suid:" + msg.suid + "\t ARS:" + msg.ARSon + "\t arsCheckTime:" + msg.arsCheckTime + "\n";
}
}
ret = temp;
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, "ERROR Process_ht_SUInfo:\n" + ex.ToString());
ret += " " + ex.ToString();
}
return ret;
}
private string Process_ht_pendingMsg()
{
string ret = "Error\n";
LOGS.LOG("Debug: LOCK2");
lock (SN_Queues.ht_pendingMsg.SyncRoot)
{
try
{
string temp = "ht_pendingMsg:\n";
foreach (DictionaryEntry item in SN_Queues.ht_pendingMsg)
{
MotoTRBOcmdMsg msg = (MotoTRBOcmdMsg)item.Value;
temp += "suid:" + msg.m_suid + "\t m_cmd:" + msg.m_cmd + "\t m_payload:" + msg.m_payload +
"\t m_seqID:" + msg.m_seqID + "\t m_time:" + msg.m_time + "\n";
}
ret = temp;
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, "ERROR Process_ht_pendingMsg:\n" + ex.ToString());
ret += " " + ex.ToString() + "\n";
}
}
LOGS.LOG("Debug: UN-LOCK2");
return ret;
}
private string Process_ht_location_hash()
{
string ret = "Error\n";
try
{
string temp = "ht_location_hash:\n";
lock (SN_Queues.ht_location_hash.SyncRoot)
{
foreach (DictionaryEntry item in SN_Queues.ht_location_hash)
{
//MotoTRBOcmdMsg msg = (MotoTRBOcmdMsg)item.Value;
temp += "suid:" + item.Key + "\t last GPS at:" + ((DateTime)item.Value).ToString() + "\n";
}
}
ret = temp;
}
catch (Exception ex)
{
Utils.ConsWrite(DebugMSG_Type.always, "ERROR Process_ht_location_hash:\n" + ex.ToString());
ret += " " + ex.ToString() + "\n";
}
return ret;
}
}
}