git_bsmd/AIS/bsmd.AIS2Service/webservice/SLRController.cs

88 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Web.Http;
using log4net;
namespace bsmd.AIS2Service
{
public class SLRController : ApiController
{
private static readonly ILog _log = LogManager.GetLogger(typeof(SLRController));
[HttpGet]
public IEnumerable<ShipLocationReport> Get([FromUri] int? id)
{
if (!id.HasValue) return null;
List<ShipLocationReport> result = AISManager.SQLiteStorage.GetShipLocationReports(id.Value);
// Class B targets entfernen
int classBReportCnt = result.RemoveAll(x => AISManager.SitRep.ContainsKey(x.MMSI) && (AISManager.SitRep[x.MMSI].IsClassB ?? false));
_log.DebugFormat("removed {0} class B alarms from list"); // tut des?
// auch alles entfernen was "abgelaufen" ist und nicht im SitRep enthalten ist
int expiredCnt = result.RemoveAll(x => AISManager.SitRep.ContainsKey(x.MMSI) && ((DateTime.Now - x.Timestamp_Last).TotalMinutes > 1440));
_log.InfoFormat("removed {0} expired (> 1 day) alarms from list");
Dictionary<int, List<ShipLocationReport>> mmsiDict = new Dictionary<int, List<ShipLocationReport>>();
foreach(ShipLocationReport report in result) {
if (!mmsiDict.ContainsKey(report.MMSI)) mmsiDict[report.MMSI] = new List<ShipLocationReport>();
mmsiDict[report.MMSI].Add(report);
if (AISManager.SitRep.ContainsKey(report.MMSI))
{
report.Destination = AISManager.SitRep[report.MMSI].Destination;
report.Name = AISManager.SitRep[report.MMSI].Name;
report.NavStatus = AIS_PosReport.GetNavStatus(AISManager.SitRep[report.MMSI].NavStatus);
report.IMO = AISManager.SitRep[report.MMSI].IMO;
}
}
// determine "state" of vessel through alarm comparison. Possible values are:
// 0 = stationary (= equals)
// 1 = incoming
// 2 = outgoing
// 3 = expired
foreach(int key in mmsiDict.Keys)
{
bool expired = true;
DateTime? lastDate= null;
bool? incoming = null;
// first run through alarm list to determine state
foreach(ShipLocationReport slr in mmsiDict[key])
{
if((DateTime.Now - slr.Timestamp_Last).TotalHours < Properties.Settings.Default.AutoAlarmExpiryHours) expired = false;
if (lastDate == null)
{
lastDate = slr.Timestamp_Last;
}
else
{
incoming = slr.Timestamp_Last < lastDate;
}
}
// second run through alarm list to set state flag in all entries
foreach (ShipLocationReport slr in mmsiDict[key])
{
if (expired)
{
slr.VoyageDirection = 3;
}
else if (incoming.HasValue)
{
if (incoming.Value) { slr.VoyageDirection = 1; }
else { slr.VoyageDirection = 2; }
}
else
{
slr.VoyageDirection = 0; // stationary / no comparison value
}
}
}
return result;
}
}
}