SafeDispatch/SafeMobileLIB_DLL/GeneralLocationManager.cs

145 lines
5.8 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SafeMobileLib
{
public class GeneralLocationManager
{
//multicast
private string ip;
private int port;
private UdpMulticast udpMulticast;
public GeneralLocationManager(string ip, int port)
{
this.ip = ip; this.port = port;
//ini multicast
try
{
udpMulticast = new UdpMulticast(ip, port);
Utils.WriteLine("Location manager connected to message bus on ip:"+ip+" port:"+port);
}
catch (Exception ex)
{
Utils.WriteLine("Location manager exception while joining the message bus: " + ex.ToString());
}
}
public GeneralLocationManager(UdpMulticast udp)
{
//ini multicast
try
{
udpMulticast = udp;
Utils.WriteLine("Location manager connected to message bus ");
}
catch (Exception ex)
{
Utils.WriteLine("Location manager exception while joining the message bus: " + ex.ToString());
}
}
public void SendLoc2messagebus(string imei,uint date70format, string speed, string lat, string lng, int poll)
{
String[] toSendString = new String[4];
toSendString[0] = date70format.ToString();
toSendString[1] = speed;
toSendString[2] = lat;
toSendString[3] = lng;
Byte[] toSendMulticast = createLocationMessage( (poll == 0 ? 131 : 231), imei, toSendString);
udpMulticast.Send(toSendMulticast, toSendMulticast.Length);
Utils.WriteLine(System.Text.Encoding.Default.GetString(toSendMulticast), ConsoleColor.White);
}
public void SendLoc2messagebusTallysman(string imei, uint date70format, string speed, string lat, string lng,string altitude, string logId, int poll)
{
String[] toSendString = new String[6];
toSendString[0] = date70format.ToString();
toSendString[1] = speed;
toSendString[2] = lat;
toSendString[3] = lng;
toSendString[4] = altitude;
toSendString[5] = logId;
Byte[] toSendMulticast = createLocationMessage((poll == 0 ? 131 : 231), imei, toSendString);
udpMulticast.Send(toSendMulticast, toSendMulticast.Length);
Utils.WriteLine(System.Text.Encoding.Default.GetString(toSendMulticast), ConsoleColor.White);
}
public void SendLoc2messagebusWithAltitude(string imei, uint date70format, string speed, string lat, string lng, string altitude, int poll)
{
String[] toSendString = new String[5];
toSendString[0] = date70format.ToString();
toSendString[1] = speed;
toSendString[2] = lat;
toSendString[3] = lng;
toSendString[4] = altitude;
Byte[] toSendMulticast = createLocationMessage((poll == 0 ? 131 : 231), imei, toSendString);
udpMulticast.Send(toSendMulticast, toSendMulticast.Length);
Utils.WriteLine(System.Text.Encoding.Default.GetString(toSendMulticast), ConsoleColor.White);
}
public void SendOnMsgBuss(string seqID, string test)
{
if (udpMulticast != null)
{
byte[] dataToSend = Utils.Convert_text_For_multicast("#" + seqID + test);
udpMulticast.Send(dataToSend, dataToSend.Length);
2024-02-22 16:43:59 +00:00
}
else
{
Utils.WriteLine("Error!!! SendOnMsgBuss!!!");
}
}
//aux
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;
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 = "";
msg = "#1." + GetSecondsLocalFromDT(DateTime.Now).ToString() + DateTime.Now.Millisecond.ToString() + "#" + opCode + "#" + suid + "#";
if (dataString.Length == 4)
msg = msg + dataString[0] + "#" + dataString[1] + "#" + dataString[2] + "#" + dataString[3];
else if (dataString.Length == 5)
msg = msg + dataString[0] + "#" + dataString[1] + "#" + dataString[2] + "#" + dataString[3] + "#" + dataString[4];
else
msg = msg + dataString[0] + "#" + dataString[1] + "#" + dataString[2] + "#" + dataString[3] + "#" + dataString[4] + "#" + dataString[5];
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;
}
}
}