SafeDispatch/MotoTrbo_GW/Utils.cs

138 lines
5.6 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using SafeMobileLib;
namespace MotoTrbo_GW
{
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 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, ConsoleType type)
{
SafeMobileLib.Utils.Write("Data: ", type);
for (int i = 0; i < receivedBytes.Length; i++)
{
SafeMobileLib.Utils.Write(String.Format("0x{0:X} ", receivedBytes[i]), type);
}
SafeMobileLib.Utils.WriteLine("", type);
}
//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 Byte[] createLocationMessage(int opCode, string suid, String[] dataString)
{
string msg = "";
string seqID = GenerateSeqID(1);
msg = "#" + seqID + "#" + opCode + "#" + suid + "#";
msg = msg + dataString[0] + "#" + dataString[1] + "#" + dataString[2] + "#" + dataString[3];
int totalSize = 5 + msg.Length;
string sizeMsg = String.Format("#{0:000}", totalSize);
msg = sizeMsg + msg;
Byte[] toSendMulticast = new Byte[totalSize];
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Byte[] toSendMulticastPartial = encoding.GetBytes(msg);
for (int i = 0; i < toSendMulticastPartial.Length; i++)
{
toSendMulticast[i] = toSendMulticastPartial[i];
}
toSendMulticast[totalSize - 1] = 35;//'#'
return toSendMulticast;
}
public static Byte[] createMulticastMessage(int opCode, string suid, Byte[] dataBytes, out string seqID)
{
seqID = GenerateSeqID(1);
string partialMsg = "#" + seqID + "#" + opCode + "#" + suid + "#";
int totalSize = 4 + partialMsg.Length + dataBytes.Length + 1;//
string sizeMsg = String.Format("#{0:000}", totalSize);
partialMsg = sizeMsg + partialMsg;
Byte[] toSendMulticast = new Byte[totalSize];
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Byte[] toSendMulticastPartial = encoding.GetBytes(partialMsg);
for (int j = 0; j < toSendMulticastPartial.Length; j++)
toSendMulticast[j] = toSendMulticastPartial[j];
for (int i = 0; i < dataBytes.Length; i++)
{
toSendMulticast[partialMsg.Length+i] = dataBytes[i]; //data
}
toSendMulticast[totalSize-1] = 35;//'#'
return toSendMulticast;
/*
Byte[] toSendMulticast = new Byte[dataBytes.Length + 10];
toSendMulticast[0] = 35; //#
toSendMulticast[1] = (byte)(dataBytes.Length + 10); //length
toSendMulticast[2] = 35; //#
toSendMulticast[3] = 131; // opcode
toSendMulticast[4] = 35; //#
toSendMulticast[5] = Byte.Parse(su1); //suid byte 1
toSendMulticast[6] = Byte.Parse(su2); //suid byte 2
toSendMulticast[7] = Byte.Parse(su3); //suid byte 3
toSendMulticast[8] = 35; //#
for (int i = 0; i < dataBytes.Length; i++)
{
toSendMulticast[i + 9] = dataBytes[i]; //data
}
toSendMulticast[dataBytes.Length + 9] = 35; //#
return toSendMulticast;
*/
}
/// <summary>
/// Generates a sequence id for mbus
/// </summary>
/// <param name="number">The first number in sequence id, before the dot (ex: 1.323234343...)</param>
/// <returns></returns>
public static string GenerateSeqID(int number)
{
DateTime now = DateTime.Now;
return number + "." + now.GetSecondsLocalFromDT() + now.Millisecond;
}
}
}