// Copyright (c) 2008-2018 schick Informatik // Description: // using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; 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 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(); } protected void Init(string[] args) { AIS_Configuration configuration = AIS_Configuration.Load(config_filename); if (configuration == null) { Console.WriteLine(string.Format("cannot read configuration {0}", config_filename)); return; } DBConnector dbConnector = new DBConnector(); dbConnector.ConnectionString = configuration.DBConnectionString; if (!dbConnector.Open()) { Console.WriteLine("Error connecting to database"); return; } List 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)); } } }