316 lines
13 KiB
C#
316 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using SafeNetLib;
|
|
using System.Threading;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Collections;
|
|
|
|
namespace Teltonika_SOC
|
|
{
|
|
class Program
|
|
{
|
|
public static Config cfg;
|
|
public static bool unitsLoaded = false;
|
|
public static bool addressLoaded = false;
|
|
public static DateTime startTime;
|
|
public static LOGS hLOG;
|
|
|
|
LocationThread LocationConnection = null; // the threads for the 5 services we handle
|
|
DBconnThread DBConnection = null;
|
|
|
|
Thread LocationThreadobj = null;
|
|
Thread DBThreadobjGPS = null;
|
|
Thread DBThreadobjAddr = null;
|
|
Thread DBThreadobjAux = null;
|
|
|
|
//alerts
|
|
private static AlertHandler alertHandler;
|
|
|
|
#region setupmode variables
|
|
public static string[] commands;
|
|
public static List<string> commandsList = new List<string>();
|
|
public static int parametersCount = 0;
|
|
public static bool setupMode = false;
|
|
public static bool loadUnitsFromFile = false;
|
|
public static readonly object locker = new object();
|
|
public static Hashtable ImeiHT = new Hashtable();
|
|
public static bool isUnix = false;
|
|
#endregion setupmode variables
|
|
|
|
#region Connection_TYPE
|
|
public static Connection_TYPE con_type = Connection_TYPE.TCP;
|
|
|
|
#endregion
|
|
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
|
|
hLOG = new LOGS();
|
|
Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|
Console.WriteLine("Safemobile Teltonika Gateway for Safenet " + v.ToString() + ", Copyright 2012");
|
|
Console.WriteLine(" (www.Safemobile.com) ");
|
|
Console.WriteLine("-----------------------------------\n");
|
|
Console.WriteLine("Version: " + v.ToString());
|
|
LOGS.LOG(DateTime.Now.ToString() + " : Application started!!!!");
|
|
startTime = DateTime.Now;
|
|
isUnix = Utils.isUnix();
|
|
if (args.Length > 0)
|
|
{
|
|
if (args[0] == "-v")
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "Application ended!!!!");
|
|
LOGS.LOG(DateTime.Now.ToString() + " : Application ended!!!!");
|
|
System.Environment.Exit(0);
|
|
}
|
|
if (args[0] == "-h" || args[0] == "--help")
|
|
{
|
|
Console.WriteLine("-v : Version Number");
|
|
Console.WriteLine("-s <file path> : Setup Mode (OVER TCP ONLY)");
|
|
//Console.WriteLine("-u : Start UDP Server");
|
|
//Console.WriteLine("-t : Start TCP Server");
|
|
System.Environment.Exit(0);
|
|
}
|
|
//if (args[0] == "-u")
|
|
//{
|
|
// con_type = Connection_TYPE.UDP;
|
|
//}
|
|
//if (args[0] == "-t")
|
|
//{
|
|
// con_type = Connection_TYPE.TCP;
|
|
//}
|
|
|
|
if (args[0] == "-s")
|
|
{
|
|
if (args.Count() == 2 && File.Exists(args[1]))
|
|
{
|
|
lock (locker)
|
|
{
|
|
ThreadPool.QueueUserWorkItem(state => ReadSetupFile(args[1]));
|
|
setupMode = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Utils.WriteLine("Unable to start Setupmode! Invalid setup file location! ", ConsoleColor.Red, Program.isUnix);
|
|
System.Environment.Exit(0);
|
|
}
|
|
}
|
|
}
|
|
|
|
//creat PID file
|
|
Process currentProcess = Process.GetCurrentProcess();
|
|
int procid = currentProcess.Id;
|
|
if (File.Exists("pid.pid"))
|
|
{
|
|
File.Delete("pid.pid");
|
|
}
|
|
System.IO.File.WriteAllText("pid.pid", procid.ToString());
|
|
|
|
Program p = new Program();
|
|
//load config
|
|
|
|
if (!p.LoadConfig())
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "EERROR loading configs!!!!\nPlease fix configs in DB/cfg file and restart the GW!!!");
|
|
LOGS.LOG(DateTime.Now.ToString() + " : EERROR loading configs!!!!\nPlease fix configs in DB/cfg file and restart the GW!!!");
|
|
return;
|
|
}
|
|
if (cfg.transport == "udp")
|
|
con_type = Connection_TYPE.UDP;
|
|
else
|
|
con_type = Connection_TYPE.TCP;
|
|
|
|
lock (locker)
|
|
{
|
|
Utils.WriteLine("»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»««««««««««««««««««««««««««««««", ConsoleColor.Magenta, Program.isUnix);
|
|
Utils.WriteLine(string.Format("»»»»»»»»»»»»»»»» {0} Server Mode ««««««««««««««««", con_type.ToString()), ConsoleColor.Magenta, Program.isUnix);
|
|
Utils.WriteLine("»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»««««««««««««««««««««««««««««««", ConsoleColor.Magenta, Program.isUnix);
|
|
}
|
|
|
|
try
|
|
{
|
|
if (args.Length > 0 && args[0] != null && args[0] != "" && args[0].Substring(0,1) != "-")
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.CFG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
|
Utils.ConsWrite(DebugMSG_Type.CFG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
|
Utils.ConsWrite(DebugMSG_Type.CFG, "GW started with param:" + args[0]);
|
|
Utils.ConsWrite(DebugMSG_Type.CFG, "Replacing config.ini GW_ID:" + cfg.gatewayID + " with:" + args[0]);
|
|
cfg.gatewayID = args[0];
|
|
Utils.ConsWrite(DebugMSG_Type.CFG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
|
Utils.ConsWrite(DebugMSG_Type.CFG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
|
|
return;
|
|
}
|
|
|
|
//start DEBUG
|
|
//SafeNetLib.Debug d = new SafeNetLib.Debug(cfg.debug_port);
|
|
|
|
p.StartDBService();
|
|
//wait
|
|
while (!Program.unitsLoaded)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Waiting for units to be fetched!!! sleep 5 secs!!!");
|
|
Thread.Sleep(10000);
|
|
}
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "------------------------------");
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "------------------------------");
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "---- Unit load finished -----");
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "------------------------------");
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "------------------------------");
|
|
LOGS.LOG(DateTime.Now.ToString() + " : ---- Unit load finished -----");
|
|
|
|
p.StartLocationService();
|
|
|
|
p.StartAlertService();
|
|
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "###### Gateway started #######");
|
|
LOGS.LOG(DateTime.Now.ToString() + " : ###### Gateway started #######");
|
|
while (true)
|
|
{
|
|
Thread.Sleep(10000);
|
|
}
|
|
}
|
|
|
|
private bool LoadConfig()
|
|
{
|
|
bool ret = false;
|
|
cfg = new Config();
|
|
ret = true;
|
|
return ret;
|
|
}
|
|
|
|
void StartDBService()
|
|
{
|
|
try
|
|
{
|
|
//SOC_connection sc = new SOC_connection(Server_IP, Server_Port, gatewayID);
|
|
string MyConString = "SERVER=" + cfg.SERVER + ";" +
|
|
"DATABASE=" + cfg.DATABASE + ";" +
|
|
"UID=" + cfg.UID + ";" +
|
|
"PASSWORD=" + cfg.PASSWORD + ";Pooling=false;";
|
|
|
|
DBConnection = new DBconnThread(MyConString, cfg.gatewayID);
|
|
|
|
//Start GPS DB thread
|
|
DBThreadobjGPS = new Thread(new ThreadStart(DBConnection.HandleConnectionGPS));
|
|
DBThreadobjGPS.IsBackground = true;
|
|
DBThreadobjGPS.Start();
|
|
|
|
//Start Units DB thread
|
|
DBThreadobjAux = new Thread(new ThreadStart(DBConnection.HandleConnectionAux));
|
|
DBThreadobjAux.IsBackground = true;
|
|
DBThreadobjAux.Start();
|
|
|
|
//Start Addres insert thread
|
|
DBThreadobjAddr = new Thread(new ThreadStart(DBConnection.HandleConnectionAddr));
|
|
DBThreadobjAddr.IsBackground = true;
|
|
DBThreadobjAddr.Start();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "Could not intialize the connection with location server!\r\n" + e.Message + "\r\nPlease check your internet connection !!");
|
|
//System.Environment.Exit(1);
|
|
}
|
|
|
|
}
|
|
|
|
void StartLocationService()
|
|
{
|
|
if (con_type == Connection_TYPE.TCP)
|
|
{
|
|
// create thread to listen TCP
|
|
LocationConnection = new LocationThread();
|
|
LocationConnection.port = cfg.locPort;
|
|
//LocationConnection.parent = this;
|
|
LocationThreadobj = new Thread(new ThreadStart(LocationConnection.HandleConnection));
|
|
LocationThreadobj.IsBackground = true;
|
|
LocationThreadobj.Start();
|
|
}
|
|
else
|
|
{
|
|
UdpConnectionThread udpConnection = new UdpConnectionThread();
|
|
}
|
|
}
|
|
|
|
void StartAlertService()
|
|
{
|
|
string MyConString = "SERVER=" + cfg.SERVER + ";" +
|
|
"DATABASE=" + cfg.DATABASE + ";" +
|
|
"UID=" + cfg.UID + ";" +
|
|
"PASSWORD=" + cfg.PASSWORD + ";Pooling=false;";
|
|
DBhandle DB = new DBhandle(MyConString, cfg.gatewayID);
|
|
alertHandler = new AlertHandler(DB, cfg.alert_smtpServer, cfg.alert_smtpPort, cfg.alert_user, cfg.alert_psw, cfg.alert_sslState);
|
|
|
|
//alertHandler.TestSendEmail("gabriel.oprea@safemobile.com", "test");
|
|
}
|
|
|
|
static void ReadSetupFile(string filename)
|
|
{
|
|
lock (locker)
|
|
{
|
|
try
|
|
{
|
|
if (File.ReadLines(filename).Count() == 0)
|
|
{
|
|
Utils.WriteLine("Setup file is empty! ", ConsoleColor.Red, Program.isUnix);
|
|
System.Environment.Exit(0);
|
|
}
|
|
using (var rd = new StreamReader(filename))
|
|
{
|
|
lock (locker)
|
|
{
|
|
Utils.WriteLine("Reading setup file... ", ConsoleColor.Yellow, Program.isUnix);
|
|
|
|
while (!rd.EndOfStream)
|
|
{
|
|
int param = -1;
|
|
var command = rd.ReadLine().TrimStart().TrimEnd();
|
|
var splits = command.Split(':');
|
|
if (splits[0] == "command")
|
|
{
|
|
|
|
|
|
commandsList.Add(splits[1].TrimStart().TrimEnd());
|
|
parametersCount++;
|
|
Utils.WriteLine("Add command: <<" + splits[1].TrimStart().TrimEnd() + ">> to commands array!", ConsoleColor.Green, Program.isUnix);
|
|
}
|
|
if (command.StartsWith("units[") && command.Contains("units[") && command.Contains("]"))
|
|
{
|
|
loadUnitsFromFile = true;
|
|
command = command.Remove(0, command.IndexOf("units[") + 6);
|
|
command = command.Remove(command.IndexOf("]"));
|
|
var units = command.Split(',');
|
|
foreach (var unit in units)
|
|
{
|
|
if (!ImeiHT.Contains(unit))
|
|
ImeiHT.Add(unit, 0);
|
|
}
|
|
}
|
|
}
|
|
if (loadUnitsFromFile)
|
|
{
|
|
Utils.WriteLine("Setup mode: Done loading " + Program.ImeiHT.Count + " units from Setup file!", ConsoleColor.Cyan, Program.isUnix);
|
|
}
|
|
}
|
|
}
|
|
Utils.WriteLine("Setup commands successfully read from file! ", ConsoleColor.Yellow, Program.isUnix);
|
|
commands = commandsList.ToArray();
|
|
}
|
|
catch
|
|
{
|
|
Utils.WriteLine("File used by another process! ", ConsoleColor.Red,Program.isUnix);
|
|
System.Environment.Exit(0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|