77 lines
2.9 KiB
C#
77 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Web.Http;
|
|
|
|
namespace bsmd.AIS2Service
|
|
{
|
|
public class SLRController : ApiController
|
|
{
|
|
[HttpGet]
|
|
public IEnumerable<ShipLocationReport> Get([FromUri] int? id)
|
|
{
|
|
if (!id.HasValue) return null;
|
|
List<ShipLocationReport> result = AISManager.SQLiteStorage.GetShipLocationReports(id.Value);
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|