SafeDispatch/CPlus_GW/Utils.cs

313 lines
13 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using SafeMobileLib;
namespace CPlus_GW
{
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("Data: ");
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 class LocationEventArgs : EventArgs
{
public Int64 RadioID { get; set; }
public Int64 GPSTime { 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 Int64 seqID { get; set; }
public LocationEventArgs()
{
RadioID = 0;
GPSTime = DateTime.UtcNow.DateTo70Format();
Speed = 0;
Latitude = 0;
Longitude = 0;
Altitude = 0;
seqID = 0;
}
}
public class TelemetryReceivedEventArgs : EventArgs
{
public String RadioID { get; set; }
public String GPIO { get; set; }
public String Type { get; set; }
public Int64 seqID { get; set; }
public TelemetryReceivedEventArgs()
{
RadioID = "";
GPIO = "";
Type = "";
seqID = 0;
}
}
public class TallysmanEventArgs : EventArgs
{
public sealed class TallysmanEventType
{
private readonly String name;
private readonly int value;
public static readonly TallysmanEventType IMMEDIATE = new TallysmanEventType(1, "IMMEDIATE");
public static readonly TallysmanEventType PTT = new TallysmanEventType(2, "PTT");
public static readonly TallysmanEventType TURN = new TallysmanEventType(3, "TURN");
public static readonly TallysmanEventType SPEEDING = new TallysmanEventType(4, "SPEEDING");
public static readonly TallysmanEventType IGNITION_ON = new TallysmanEventType(5, "IGNITION_ON");
public static readonly TallysmanEventType IGNITION_OFF = new TallysmanEventType(6, "IGNITION_OFF");
public static readonly TallysmanEventType RADIO_ON = new TallysmanEventType(7, "RADIO_ON");
public static readonly TallysmanEventType RADIO_OFF = new TallysmanEventType(8, "RADIO_OFF");
public static readonly TallysmanEventType STOPPED = new TallysmanEventType(9, "STOPPED");
public static readonly TallysmanEventType MOVING = new TallysmanEventType(10, "MOVING");
public static readonly TallysmanEventType GPS_FIX = new TallysmanEventType(11, "GPS_FIX");
public static readonly TallysmanEventType GPS_NO_FIX = new TallysmanEventType(12, "GPS_NO_FIX");
public static readonly TallysmanEventType DISTANCE = new TallysmanEventType(13, "DISTANCE");
public static readonly TallysmanEventType VIO = new TallysmanEventType(14, "VIO");
public static readonly TallysmanEventType PERIODIC = new TallysmanEventType(15, "PERIODIC");
public static readonly TallysmanEventType CIRCLE_WAYPOINT_IN = new TallysmanEventType(16, "CIRCLE_WAYPOINT_IN");
public static readonly TallysmanEventType EMERGENCY = new TallysmanEventType(17, "EMERGENCY");
public static readonly TallysmanEventType CHANNEL_CHANGED = new TallysmanEventType(18, "CHANNEL_CHANGED");
public static readonly TallysmanEventType TRACKING = new TallysmanEventType(19, "TRACKING");
public static readonly TallysmanEventType SAFE_AREA = new TallysmanEventType(20, "SAFE_AREA");
public static readonly TallysmanEventType RSSI = new TallysmanEventType(21, "RSSI");
public static readonly TallysmanEventType USER_INPUT = new TallysmanEventType(22, "USER_INPUT");
public static readonly TallysmanEventType POLYGON_WAYPOINT_IN = new TallysmanEventType(23, "POLYGON_WAYPOINT_IN");
public static readonly TallysmanEventType POLYGON_WAYPOINT_OUT = new TallysmanEventType(24, "POLYGON_WAYPOINT_OUT");
public static readonly TallysmanEventType CIRCLE_WAYPOINT_OUT = new TallysmanEventType(25, "CIRCLE_WAYPOINT_OUT");
public static readonly TallysmanEventType BUS_STOP = new TallysmanEventType(26, "BUS_STOP");
public static readonly TallysmanEventType PERIODIC1 = new TallysmanEventType(27, "PERIODIC1");
public static readonly TallysmanEventType PERIODIC2 = new TallysmanEventType(28, "PERIODIC2");
public static readonly TallysmanEventType PERIODIC3 = new TallysmanEventType(29, "PERIODIC3");
public static readonly TallysmanEventType PERIODIC4 = new TallysmanEventType(30, "PERIODIC4");
public static readonly TallysmanEventType VIO1 = new TallysmanEventType(31, "VIO1");
public static readonly TallysmanEventType VIO2 = new TallysmanEventType(32, "VIO2");
public static readonly TallysmanEventType VIO3 = new TallysmanEventType(33, "VIO3");
public static readonly TallysmanEventType VIO4 = new TallysmanEventType(34, "VIO4");
public static readonly TallysmanEventType DISTANCE1 = new TallysmanEventType(35, "DISTANCE1");
public static readonly TallysmanEventType DISTANCE2 = new TallysmanEventType(36, "DISTANCE2");
private TallysmanEventType(int value, String name)
{
this.name = name;
this.value = value;
}
public static TallysmanEventType GetEventType(int type)
{
switch (type)
{
case 1: return IMMEDIATE;
case 2: return PTT;
case 3: return TURN;
case 4: return SPEEDING;
case 5: return IGNITION_ON;
case 6: return IGNITION_OFF;
case 7: return RADIO_ON;
case 8: return RADIO_OFF;
case 9: return STOPPED;
case 10: return MOVING;
case 11: return GPS_FIX;
case 12: return GPS_NO_FIX;
case 13: return DISTANCE;
case 14: return VIO;
case 15: return PERIODIC;
case 16: return CIRCLE_WAYPOINT_IN;
case 17: return EMERGENCY;
case 18: return CHANNEL_CHANGED;
case 19: return TRACKING;
case 20: return SAFE_AREA;
case 21: return RSSI;
case 22: return USER_INPUT;
case 23: return POLYGON_WAYPOINT_IN;
case 24: return POLYGON_WAYPOINT_OUT;
case 25: return CIRCLE_WAYPOINT_OUT;
case 26: return BUS_STOP;
case 27: return PERIODIC1;
case 28: return PERIODIC2;
case 29: return PERIODIC3;
case 30: return PERIODIC4;
case 31: return VIO1;
case 32: return VIO2;
case 33: return VIO3;
case 34: return VIO4;
case 35: return DISTANCE1;
case 36: return DISTANCE2;
}
return BUS_STOP;
}
public override String ToString()
{
return name;
}
}
public Int64 Id { get; set; }
public TallysmanEventType EventType { get; set; }
public Int64 RadioID { get; set; }
public Double GPSFixTime { get; set; }
public Double EventTime { 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 Int64 LogID { get; set; }
public TallysmanEventArgs()
{
Id = 0;
EventType = TallysmanEventType.BUS_STOP;
RadioID = 0;
GPSFixTime = 0;
EventTime = 0;
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;
LogID = 0;
}
public SafeMobileLib.TallysmanMsg GetTallysmanMessage()
{
return new SafeMobileLib.TallysmanMsg()
{
Id = this.Id,
EventType = this.EventType.ToString(),
RadioID = this.RadioID,
GPSFixTime = this.GPSFixTime,
EventTime = this.EventTime,
Speed = this.Speed,
Latitude = this.Latitude,
Longitude = this.Longitude,
Altitude = this.Altitude,
LevelOfConfidence = this.LevelOfConfidence,
Bearing = this.Bearing,
HorizontalAccuracy = this.HorizontalAccuracy,
VerticalAccuracy = this.VerticalAccuracy,
Odometer = this.Odometer,
RunTime = this.RunTime,
IdleTime = this.IdleTime,
VioStatus = this.VioStatus,
VioChanged = this.VioChanged,
AverageSpeed = this.AverageSpeed,
WaypointId = this.WaypointId,
FirmwareVersion = this.FirmwareVersion,
RSSI = this.RSSI,
VitalId = this.VitalId,
LogId = this.LogID
};
}
}
}