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 Get([FromUri] int? id) { if (!id.HasValue) return null; List result = AISManager.SQLiteStorage.GetShipLocationReports(id.Value); int classBReportCnt = result.RemoveAll(x => AISManager.SitRep.ContainsKey(x.MMSI) && (AISManager.SitRep[x.MMSI].IsClassB ?? false)); _log.DebugFormat("removed {0} class B SLR reports from list"); // tut des? // auch alles entfernen was "abgelaufen" ist und nicht im SitRep enthalten ist Dictionary> mmsiDict = new Dictionary>(); foreach(ShipLocationReport report in result) { if (!mmsiDict.ContainsKey(report.MMSI)) mmsiDict[report.MMSI] = new List(); 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; } } }