289 lines
13 KiB
C#
289 lines
13 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO.Ports;
|
|
using System.Net;
|
|
using System.Net.Mail;
|
|
using System.Management;
|
|
using Nini.Config;
|
|
using System.IO;
|
|
|
|
namespace Tetra_GW
|
|
{
|
|
class Utils
|
|
{
|
|
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 String Mode = "DMO";
|
|
|
|
public static Boolean autoconnect = true;
|
|
public static Boolean autoupdate = true;
|
|
public static Boolean isDevelop = false;
|
|
public static String localIP = "";
|
|
|
|
|
|
private static IConfigSource source = null;
|
|
|
|
|
|
public static void getAvailableSerialPorts(ref Hashtable portHT)
|
|
{
|
|
SafeMobileLib.Utils.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);
|
|
SafeMobileLib.Utils.WriteLine("found available port: " + s);
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Console.WriteLine("could not open port: " + ex.ToString());
|
|
}
|
|
}
|
|
portsList = comPorts.ToArray();
|
|
Array.Sort(portsList);
|
|
}
|
|
|
|
public static String[] getOpenPorts()
|
|
{
|
|
return portsList;
|
|
}
|
|
|
|
public static Params readConfig()
|
|
{
|
|
MulticastParams multParams = new MulticastParams();
|
|
GatewayParams gatewayParams = new GatewayParams();
|
|
Int32 TIMEARS=1;
|
|
try
|
|
{
|
|
if (!File.Exists(configFile))
|
|
{
|
|
SafeMobileLib.Utils.WriteLine("Config file didn't exist. Creating it...", ConsoleColor.Yellow);
|
|
try
|
|
{
|
|
using (FileStream fs = File.Create(configFile)) { }
|
|
}
|
|
catch (Exception)
|
|
{
|
|
SafeMobileLib.Utils.WriteLine("Application ended!!!! " + configFile + " could not be created");
|
|
Console.WriteLine("Press any key to exit...");
|
|
Console.ReadKey();
|
|
System.Environment.Exit(0);
|
|
}
|
|
}
|
|
|
|
|
|
source = new IniConfigSource(configFile);
|
|
|
|
|
|
if (source.Configs["Gateway"] == null)
|
|
source.Configs.Add("Gateway");
|
|
|
|
gatewayParams.ID = GetInt32Value("Gateway", "id", 1); // Convert.ToInt32(source.Configs["Gateway"].Get("id"));
|
|
gatewayParams.RADIO_GW_ID = GetInt32Value("Gateway", "gw_radio_id", 1); // Convert.ToInt32(source.Configs["Gateway"].Get("gw_radio_id"));
|
|
gatewayParams.IP = GetStringValue("Gateway", "ip", "192.168.10.60"); // source.Configs["Gateway"].Get("ip");
|
|
|
|
gatewayParams.COMPort = GetStringValue("Gateway", "COMPort", "COM35"); // source.Configs["Gateway"].Get("COMPort");
|
|
gatewayParams.autoconnect = GetBooleanValue("Gateway", "autoconnect", false); //Convert.ToBoolean(source.Configs["Gateway"].Get("autoconnect"));
|
|
|
|
localIP = GetStringValue("Gateway", "localIP", "");
|
|
|
|
|
|
if (source.Configs["ARSRefresh"] == null)
|
|
source.Configs.Add("ARSRefresh");
|
|
|
|
TIMEARS = GetInt32Value("ARSRefresh", "timesec", 200); // Convert.ToInt32(source.Configs["ARSRefresh"].Get("timesec"));
|
|
|
|
|
|
if (source.Configs["MessageBus"] == null)
|
|
source.Configs.Add("MessageBus");
|
|
|
|
multParams.mIP = GetStringValue("MessageBus", "ip", "224.30.0.1"); // multParams.mIP = source.Configs["MessageBus"].Get("ip");
|
|
multParams.mPort = GetInt32Value("MessageBus", "port", 17233); // multParams.mPort = Convert.ToInt32(source.Configs["MessageBus"].Get("port"));
|
|
|
|
if (source.Configs["TYPE"] == null)
|
|
source.Configs.Add("TYPE");
|
|
|
|
isTetra = GetBooleanValue("TYPE", "Tetra", true); // Convert.ToBoolean(source.Configs["TYPE"].Get("Tetra"));
|
|
isFug = GetBooleanValue("TYPE", "FUG", false); //Convert.ToBoolean(source.Configs["TYPE"].Get("FUG"));
|
|
Mode = GetStringValue("TYPE", "mode", "DMO"); //Convert.ToBoolean(source.Configs["TYPE"].Get("FUG"));
|
|
|
|
|
|
if (source.Configs["Update"] == null)
|
|
source.Configs.Add("Update");
|
|
|
|
autoupdate = GetBooleanValue("Update", "autoupdate", true);
|
|
|
|
if (source.Configs["Update"].Contains("develop"))
|
|
isDevelop = source.Configs["Update"].GetBoolean("develop");
|
|
|
|
GetBooleanValue("Update", "Enable", true);
|
|
|
|
|
|
|
|
// Save the INI file
|
|
source.Save();
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine("Configuration file reading error: " + e.Message);
|
|
}
|
|
return new Params(multParams, gatewayParams, TIMEARS);
|
|
}
|
|
|
|
public static void writeConfig(Params configParams)
|
|
{
|
|
IConfigSource source = new IniConfigSource(configFile);
|
|
source.Configs["Gateway"].Set("id", configParams.gateParams.ID);
|
|
source.Configs["Gateway"].Set("ip", configParams.gateParams.IP);
|
|
source.Configs["ARSRefresh"].Set("timesec", configParams.TIMEARS);
|
|
|
|
source.Configs["MessageBus"].Set("ip", configParams.multParams.mIP);
|
|
source.Configs["MessageBus"].Set("port", configParams.multParams.mPort);
|
|
|
|
source.Save();
|
|
}
|
|
|
|
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 = "";
|
|
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];
|
|
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)
|
|
{
|
|
//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];
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
}
|
|
|
|
}
|
|
} |