67 lines
2.3 KiB
C#
67 lines
2.3 KiB
C#
using log4net;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace bsmd.AIS2Service
|
|
{
|
|
internal static class AISManager
|
|
{
|
|
private static readonly List<IAISThread> _tasks = new List<IAISThread>();
|
|
private static readonly ConcurrentQueue<string> _inputLines = new ConcurrentQueue<string>();
|
|
private static readonly ConcurrentQueue<AISClass> _decodedClasses = new ConcurrentQueue<AISClass>();
|
|
private static readonly ILog _log = LogManager.GetLogger(typeof(AISManager));
|
|
private static readonly ConcurrentDictionary<int, AIS_Target> _sitRepList = new ConcurrentDictionary<int, AIS_Target>();
|
|
private static Timer _staleTargetTimer;
|
|
|
|
public static void Start()
|
|
{
|
|
_tasks.Add(new SerialTCPReader(Properties.Settings.Default.DataSourceHost, Properties.Settings.Default.DataSourcePort, _inputLines));
|
|
_tasks.Add(new AISDecoder(_inputLines, _decodedClasses));
|
|
_tasks.Add(new SitRep(_decodedClasses, _sitRepList));
|
|
|
|
foreach (var task in _tasks)
|
|
{
|
|
task.Start();
|
|
_log.InfoFormat("{0} started", task.Name);
|
|
}
|
|
|
|
_staleTargetTimer = new Timer(staleTimerCheck, null, 0, 60000); // check every minute
|
|
|
|
}
|
|
|
|
private static void staleTimerCheck(object state)
|
|
{
|
|
List<int> removeKeyList = new List<int>();
|
|
foreach (int key in _sitRepList.Keys)
|
|
{
|
|
if (!_sitRepList[key].LastUpdate.HasValue) { removeKeyList.Add(key); }
|
|
else
|
|
{
|
|
if ((DateTime.Now - _sitRepList[key].LastUpdate.Value).TotalMinutes > Properties.Settings.Default.StaleTargetTimeoutMins)
|
|
removeKeyList.Add(key);
|
|
}
|
|
}
|
|
foreach(int key in removeKeyList)
|
|
{
|
|
_sitRepList.TryRemove(key, out _);
|
|
}
|
|
}
|
|
|
|
public static void Stop()
|
|
{
|
|
foreach (var task in _tasks)
|
|
{
|
|
task.Stop();
|
|
_log.InfoFormat("{0} stopped", task.Name);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|