git_bsmd/AIS/bsmd.AISService/AISService.cs

99 lines
3.3 KiB
C#

// Copyright (c) 2008-2018 schick Informatik
// Description: Windows Service Main File
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceProcess;
using bsmd.AISService.AIS;
using bsmd.AISService.DB;
using log4net;
using System.IO;
namespace bsmd.AISService
{
public partial class AISService : ServiceBase
{
private const string config_filename = "ais_config.xml";
private readonly ILog _log = LogManager.GetLogger(typeof(AISService));
private AIS_QueueManager qManager;
public AISService()
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
InitializeComponent();
}
protected override void OnStart(string[] args)
{
string errorMessage = "";
this.EventLog.Source = this.ServiceName;
this.EventLog.Log = "Application";
this.Init(args);
if (qManager.Start(ref errorMessage))
{
this.EventLog.WriteEntry("BSMD AIS Service started.", EventLogEntryType.Information);
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
_log.InfoFormat("Starting AIS Service. v.{0} -------------- ", version);
} else
{
_log.ErrorFormat("AIS Service start failed: {0}", errorMessage);
}
}
internal void DebugStart()
{
this.Init(null);
string msg = "";
if(this.qManager.Start(ref msg))
{
Console.Read(); // hold it right here
}
}
protected override void OnStop()
{
this.qManager.Stop();
_log.Info("AIS Service stopped.");
}
protected void Init(string[] args)
{
AIS_Configuration configuration = AIS_Configuration.Load(config_filename);
if (configuration == null)
{
_log.ErrorFormat("cannot read configuration {0}", config_filename);
return;
}
DBConnector dbConnector = new DBConnector { ConnectionString = configuration.DBConnectionString };
if (!dbConnector.Open())
{
_log.Error("Error connecting to database");
return;
}
List<AISStation> stationList = AISStation.LoadStations(dbConnector);
this.qManager = new AIS_QueueManager(configuration, AISStation.CreateSerial_IOs(stationList), AISStation.CreateAIS_Telnets(stationList));
qManager.DBUpdateRequired += new AIS_QueueManager.AISQueueChangedHandler(dbConnector.Update);
// qManager.AISQueueChanged += new AIS_QueueManager.AISQueueChangedHandler(aisDecoder_AISMessageReceived);
}
/*
protected void aisDecoder_AISMessageReceived(AIS_Target target)
{
Console.WriteLine(string.Format("{0}: {1} Pos:{2} {3} at {4}", target.Station, target.Name, target.Latitude, target.Longitude, target.LastUpdate));
}
*/
}
}