SafeDispatch/GatewayTetraVoice/Utils.cs
2024-02-22 18:43:59 +02:00

206 lines
8.6 KiB
C#

using Nini.Config;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Management;
using System.Text;
using smutils = SafeMobileLib.Utils;
namespace Tetra_GW
{
class Utils
{
#region variables
private static List<String> comPorts = new List<String>();
private static String[] portsList;
internal static string configFile = "GWconfig.ini";
public static Boolean isTetra = true;
public static Boolean isFug = false;
public static Boolean autoconnect = true;
public static Boolean autoupdate = true;
public static Boolean isDevelop = false;
private static IConfigSource source = null;
#endregion variables
#region ports
public static void GetAvailableSerialPorts(ref Hashtable portHT)
{
smutils.WriteLine("scanning for serial ports...");
foreach (String s in SerialPort.GetPortNames())
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_PnPEntity WHERE Caption like '%(" + s + ")'");
foreach (ManagementObject queryObj in searcher.Get())
{
if (!portHT.Contains(s))
portHT.Add(s, queryObj["Caption"].ToString());
}
try
{
SerialPort serialPort1 = new SerialPort();
serialPort1.PortName = s;
serialPort1.Open();
serialPort1.Close();
comPorts.Add(serialPort1.PortName);
smutils.WriteLine("found available port: " + s);
}
catch (Exception ex)
{
//Console.WriteLine("could not open port: " + ex.ToString());
}
}
portsList = comPorts.ToArray();
Array.Sort(portsList);
}
public static String[] GetOpenPorts()
{
return portsList;
}
#endregion ports
#region methods
public static Int32 GetSecondsLocalFromDT(DateTime param)
{
DateTime datetime = param;
long nOfSeconds;
DateTime dt70 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
DateTime dttmp1 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
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 (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);
}
/// <summary>
/// Get an int value from the configuration file. The desired value is identifiec by
/// the key and the header under which is placed is pointed by the header. Also a default
/// value can be set in case the value doesn't exist and needs to be created before
/// beeing returned
/// </summary>
/// <param name="header">Header unde which the desired config parameter is placed</param>
/// <param name="key">The Key that designates the desired config parameter</param>
/// <param name="defaultValue">A default value that will be used and returned in case the
/// config file doesn't have a value for the parameter</param>
/// <returns>Parameter value from the config file, or the default value in case it doesn't
/// exist</returns>
private static Int32 GetInt32Value(String header, String key, Int32 defaultValue)
{
if (!source.Configs[header].Contains(key))
source.Configs[header].Set(key, defaultValue);
return source.Configs[header].GetInt(key);
}
/// <summary>
/// Get a string value from the configuration file. The desired value is identifiec by
/// the key and the header under which is placed is pointed by the header. Also a default
/// value can be set in case the value doesn't exist and needs to be created before
/// beeing returned
/// </summary>
/// <param name="header">Header unde which the desired config parameter is placed</param>
/// <param name="key">The Key that designates the desired config parameter</param>
/// <param name="defaultValue">A default value that will be used and returned in case the
/// config file doesn't have a value for the parameter</param>
/// <returns>Parameter value from the config file, or the default value in case it doesn't
/// exist</returns>
private static String GetStringValue(String header, String key, String defaultValue)
{
if (!source.Configs[header].Contains(key))
source.Configs[header].Set(key, defaultValue);
return source.Configs[header].GetString(key);
}
/// <summary>
/// Get a boolean value from the configuration file. The desired value is identifiec by
/// the key and the header under which is placed is pointed by the header. Also a default
/// value can be set in case the value doesn't exist and needs to be created before
/// beeing returned
/// </summary>
/// <param name="header">Header unde which the desired config parameter is placed</param>
/// <param name="key">The Key that designates the desired config parameter</param>
/// <param name="defaultValue">A default value that will be used and returned in case the
/// config file doesn't have a value for the parameter</param>
/// <returns>Parameter value from the config file, or the default value in case it doesn't
/// exist</returns>
private static Boolean GetBooleanValue(String header, String key, Boolean defaultValue)
{
if (!source.Configs[header].Contains(key))
source.Configs[header].Set(key, defaultValue);
return source.Configs[header].GetBoolean(key);
}
public static Byte[] CreateLocationMessage(int opCode, string suid, String[] dataString)
{
string msg = "";
msg = "#1." + GetSecondsLocalFromDT(DateTime.Now).ToString() + DateTime.Now.Millisecond.ToString() + "#" + 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];
ASCIIEncoding encoding = new 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)
{
//string partialMsg = "#0.0#" + opCode + "#"+suid+"#";
//(new ConvertDT().GetSecondsLocalFromDT(DateTime.Now)) + DateTime.Now.Millisecond.ToString()
string partialMsg = "#1." + GetSecondsLocalFromDT(DateTime.Now).ToString() + DateTime.Now.Millisecond.ToString() + "#" + 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];
ASCIIEncoding encoding = new 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;
}
#endregion methods
}
}