342 lines
11 KiB
C#
342 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Diagnostics;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
|
|
namespace SafeNetLib
|
|
{
|
|
public class Utils
|
|
{
|
|
public static String ID2IP(string p_CAI, string p_radioIP)
|
|
{
|
|
uint radioID = Convert.ToUInt32(p_radioIP);
|
|
if (radioID > 255 * 255 * 255)
|
|
throw new Exception("Radio ID out of range");
|
|
|
|
byte c = (byte)(radioID & 0x0000FF);
|
|
byte b = (byte)((radioID & 0x00FF00) >> 8);
|
|
byte a = (byte)((radioID & 0xFF0000) >> 16);
|
|
|
|
String toReturn = "" + p_CAI + "." + a + "." + b + "." + c;
|
|
return toReturn;
|
|
}
|
|
|
|
public static byte[] ID2ByteARR(string p_CAI, string p_radioIP)
|
|
{
|
|
byte[] arr = new byte[4];
|
|
uint radioID = Convert.ToUInt32(p_radioIP);
|
|
if (radioID > 255 * 255 * 255)
|
|
throw new Exception("Radio ID out of range");
|
|
|
|
arr[0] = Convert.ToByte(p_CAI);
|
|
arr[3] = (byte)(radioID & 0x0000FF);
|
|
arr[2] = (byte)((radioID & 0x00FF00) >> 8);
|
|
arr[1] = (byte)((radioID & 0xFF0000) >> 16);
|
|
return arr;
|
|
}
|
|
|
|
public static string Byte2String(byte[] data, int startIndex, int len)
|
|
{
|
|
string sdata = "";
|
|
int i;
|
|
|
|
if (startIndex > data.Length)
|
|
return "";
|
|
|
|
for (i = startIndex; i < startIndex + len && i < data.Length; i++)
|
|
{
|
|
int ii;
|
|
ii = (int)data[i];
|
|
sdata += "0x" + ii.ToString("X") + " ";
|
|
}
|
|
return sdata;
|
|
}
|
|
|
|
public static void printBytesArray(Byte[] receivedBytes)
|
|
{
|
|
Console.Write("\nData: ");
|
|
for (int i = 0; i < receivedBytes.Length; i++)
|
|
{
|
|
Console.Write("0x{0:X} ", receivedBytes[i]);
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
//temporat
|
|
public static Int32 GetSecondsLocalFromDT(DateTime param)
|
|
{
|
|
System.DateTime datetime = param;
|
|
long nOfSeconds;
|
|
System.DateTime dt70 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
|
System.DateTime dttmp1 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
|
System.DateTime dttmp2;
|
|
dttmp2 = dttmp1.ToLocalTime();
|
|
TimeSpan span2 = dttmp2 - dttmp1;
|
|
TimeSpan span = datetime - dt70;
|
|
//nOfSeconds = (long)span.TotalSeconds - (long)span2.TotalSeconds -3600; //mai scot o ora - 3600
|
|
if (System.TimeZone.CurrentTimeZone.IsDaylightSavingTime(param)) nOfSeconds = (long)span.TotalSeconds - (long)span2.TotalSeconds - 3600; //mai scot o ora - 3600
|
|
else nOfSeconds = (long)span.TotalSeconds - (long)span2.TotalSeconds;
|
|
return ((Int32)nOfSeconds);
|
|
}
|
|
|
|
public static void ConsWrite(DebugMSG_Type type, string str)
|
|
{
|
|
try
|
|
{
|
|
if (type != DebugMSG_Type.DEV)
|
|
Console.WriteLine("[### {0:H:mm:ss} ###] " + str, DateTime.Now);
|
|
}
|
|
catch (ThreadAbortException tae)
|
|
{
|
|
Console.WriteLine("[### {0:H:mm:ss} ###] " + str, DateTime.Now);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("####ConsWrite: Could not print debug information ####\n" + ex.ToString());
|
|
}
|
|
|
|
//add to log file
|
|
LOGS.LOG(str);
|
|
|
|
|
|
/*
|
|
//if (type == DebugMSG_Type.always)
|
|
{
|
|
LOGS.LOG_Error(str);
|
|
}
|
|
*/
|
|
}
|
|
|
|
public static void ConsWrite(DebugMSG_Type type, string[] strs)
|
|
{
|
|
foreach (string str in strs)
|
|
{
|
|
ConsWrite(type, str);
|
|
}
|
|
}
|
|
|
|
|
|
public static void WriteLine(string str, ConsoleColor color)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.DarkGray;
|
|
Console.Write(String.Format("[### {0:H:mm:ss} ###] ", DateTime.Now));
|
|
Console.ForegroundColor = color;
|
|
Console.Write(String.Format("{0}\n", str));
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
|
|
//add to log file
|
|
LOGS.LOG(str);
|
|
}
|
|
|
|
public static void WriteLine(string str, ConsoleColor color, bool isUnix)
|
|
{
|
|
if (!isUnix) Console.ForegroundColor = ConsoleColor.DarkGray;
|
|
Console.Write(String.Format("[### {0:H:mm:ss} ###] ", DateTime.Now));
|
|
if (!isUnix) Console.ForegroundColor = color;
|
|
Console.Write(String.Format("{0}\n", str));
|
|
if (!isUnix) Console.ForegroundColor = ConsoleColor.Gray;
|
|
|
|
//add to log file
|
|
LOGS.LOG(str);
|
|
|
|
}
|
|
|
|
public static bool isUnix()
|
|
{
|
|
OperatingSystem os = Environment.OSVersion;
|
|
PlatformID pid = os.Platform;
|
|
switch (pid)
|
|
{
|
|
case PlatformID.Win32NT:
|
|
case PlatformID.Win32S:
|
|
case PlatformID.Win32Windows:
|
|
case PlatformID.WinCE:
|
|
return false;
|
|
case PlatformID.Unix:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static string Compute4digitALG(double lat, double lng)
|
|
{
|
|
string ret = "";
|
|
//ret = Math.Round(lat, 4).ToString() + Math.Round(lng, 4).ToString();
|
|
ret = String.Format("{0:0.0000}", lat) + String.Format("{0:0.0000}", lng);
|
|
return ret;
|
|
}
|
|
|
|
public static int GetDBidFromSUID(string suid)
|
|
{
|
|
int DBid = -1;
|
|
//add data to ht_SUInfo
|
|
lock (SN_Queues.ht_SUInfo.SyncRoot)
|
|
{
|
|
if (SN_Queues.ht_SUInfo != null)
|
|
{
|
|
if (SN_Queues.ht_SUInfo.ContainsKey(suid))
|
|
DBid = ((SUinfo)SN_Queues.ht_SUInfo[suid]).DBid;
|
|
}
|
|
}
|
|
return DBid;
|
|
}
|
|
|
|
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
|
|
{
|
|
// Unix timestamp is seconds past epoch
|
|
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
|
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
|
|
return dtDateTime;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get the unix timestamp of a specific Date
|
|
/// </summary>
|
|
/// <param name="param">Date which needs to be converted</param>
|
|
/// <returns>An int representing the number of seconds since 1 Jan 1970</returns>
|
|
public static uint DateTo70Format(DateTime param)
|
|
{
|
|
long nOfSeconds;
|
|
//Console.WriteLine("DateTo70Format param=" + param);
|
|
System.DateTime dt70 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
TimeSpan span = param - dt70;
|
|
nOfSeconds = (long)span.TotalSeconds;
|
|
return ((uint)nOfSeconds);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Check if the application is already running in order to prevent it from running multiple
|
|
/// </summary>
|
|
public static bool TestIfAlreadyRunning()
|
|
{
|
|
Process[] processlist = Process.GetProcesses();
|
|
Process curentP = Process.GetCurrentProcess();
|
|
int count = 0;
|
|
foreach (Process theprocess in processlist)
|
|
{
|
|
if (theprocess.ProcessName.Equals(curentP.ProcessName) && !curentP.ProcessName.Contains("vshost"))
|
|
{
|
|
count++;
|
|
}
|
|
}
|
|
if (count > 1)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static void ShowActiveTcpConnections()
|
|
{
|
|
Console.WriteLine("Active TCP Connections");
|
|
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
|
|
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
|
|
foreach (TcpConnectionInformation c in connections)
|
|
{
|
|
Console.WriteLine("{0} <==> {1}",
|
|
c.LocalEndPoint.ToString(),
|
|
c.RemoteEndPoint.ToString(),
|
|
c.State);
|
|
}
|
|
}
|
|
|
|
public static void CloseClient(TcpClient whichClient)
|
|
{
|
|
whichClient.Client.Close();
|
|
// dispose of the client object
|
|
System.GC.SuppressFinalize(whichClient);
|
|
whichClient = null;
|
|
}
|
|
|
|
}
|
|
|
|
public class Hyt_cmdMsg
|
|
{
|
|
public string m_suid;
|
|
public int m_cmd;
|
|
public string m_payload;
|
|
}
|
|
|
|
public enum Hyt_cmd
|
|
{
|
|
SET_REPORT_INTERVAL = 0x01,
|
|
SEND_SMS = 0x02,
|
|
SEND_POLL = 0x03
|
|
};
|
|
|
|
public class ARSmsg
|
|
{
|
|
public string SUID;
|
|
public string status;
|
|
}
|
|
public enum DebugMSG_Type
|
|
{
|
|
ARS, SMS, CTRL, ALL, POLL, always, DB, GPS, DEV, Email, Debug, Address,CFG, Routes, ALERTS, test
|
|
}
|
|
|
|
public enum GatewayStatus { ONLINE = 1, OFFLINE = 0, LOADING = 2 };
|
|
|
|
|
|
|
|
public class TallysmanMsg
|
|
{
|
|
public Int64 Id { get; set; }
|
|
public String EventType { get; set; }
|
|
public Int64 RadioID { get; set; }
|
|
public DateTime EventTime { get; set; }
|
|
public Double GPSFixTime { get; set; }
|
|
public Double Speed { get; set; }
|
|
public Double Latitude { get; set; }
|
|
public Double Longitude { get; set; }
|
|
public Double Altitude { get; set; }
|
|
public Double LevelOfConfidence { get; set; }
|
|
public Double Bearing { get; set; }
|
|
public Double HorizontalAccuracy { get; set; }
|
|
public Double VerticalAccuracy { get; set; }
|
|
public Double Odometer { get; set; }
|
|
public Int64 RunTime { get; set; }
|
|
public Int64 IdleTime { get; set; }
|
|
public Int32 VioStatus { get; set; }
|
|
public Int32 VioChanged { get; set; }
|
|
public Double AverageSpeed { get; set; }
|
|
public Int64 WaypointId { get; set; }
|
|
public String FirmwareVersion { get; set; }
|
|
public Double RSSI { get; set; }
|
|
public Int64 VitalId { get; set; }
|
|
|
|
public TallysmanMsg()
|
|
{
|
|
Id = 0;
|
|
EventType = "BUS_STOP";
|
|
RadioID = 0;
|
|
GPSFixTime = 0;
|
|
EventTime = DateTime.Now;
|
|
Speed = 0;
|
|
Latitude = 0;
|
|
Longitude = 0;
|
|
Altitude = -1;
|
|
LevelOfConfidence = -1;
|
|
Bearing = -1;
|
|
HorizontalAccuracy = 0;
|
|
VerticalAccuracy = 0;
|
|
Odometer = 0;
|
|
RunTime = 0;
|
|
IdleTime = 0;
|
|
VioStatus = 0;
|
|
VioChanged = 0;
|
|
AverageSpeed = 0;
|
|
WaypointId = 0;
|
|
FirmwareVersion = "";
|
|
RSSI = 0;
|
|
VitalId = 0;
|
|
}
|
|
}
|
|
|
|
}
|