// Copyright (c) 2008-2018 schick Informatik
// Description:
//
using System;
using bsmd.AISService.AIS;
using log4net;
namespace bsmd.AISService.DB
{
internal class AISPosReport : AISBaseEntity {
private static readonly ILog _log = LogManager.GetLogger(typeof(AISPosReport));
///
/// Saves a (class A or B) position report
///
/// target to save
/// id of insert operation (to update hotposition table)
public static Guid? Save(AIS_Target target, DBConnector con, AISStation aisStation)
{
if (target.LastPosReport == null) return null;
if (target.LastPosReport is AIS_PosReport)
{
// Trace.WriteLine("saving class A pos report");
AIS_PosReport pr = target.LastPosReport as AIS_PosReport;
if (aisStation != null)
{
aisStation.UpdateWithPositionReport(pr.MMSI, pr.Latitude, pr.Longitude, pr.Timestamp);
aisStation.LastPosTimestamp = pr.Timestamp;
aisStation.OnAir = true;
}
if (!pr.IsNew) return pr.Id;
string query = string.Format("INSERT INTO aisposreport (Id, MMSI, NavStatus, ROT, COG, SOG, Accuracy, Longitude, Latitude, Heading, Timestamp, Reserved, Spare, Raim, CommState, AISStationId) VALUES ('{0}', {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, '{10}', {11},{12},{13},{14},'{15}')",
pr.Id, pr.MMSI, pr.NavStatusVal, pr.ROTVal, pr.COGVal, pr.SOGVal, pr.Accuracy,
pr.LongitudeVal, pr.LatitudeVal, pr.TrueHeading ?? 511, pr.DBTimestamp, pr.Reserved, pr.Spare, pr.Raim, pr.CommState,
(aisStation != null) ? aisStation.Id : Guid.Empty);
con.ExecuteNonQuery(query);
pr.IsNew = false;
return pr.Id;
}
if (target.LastPosReport is AIS_ClassB)
{
// Trace.WriteLine("saving class B pos report");
AIS_ClassB pr = target.LastPosReport as AIS_ClassB;
aisStation.UpdateWithPositionReport(pr.MMSI, pr.Latitude, pr.Longitude, pr.Timestamp);
if (!pr.IsNew) return pr.Id;
string query = string.Format("INSERT INTO aisposreport (Id, MMSI, NavStatus, ROT, COG, SOG, Accuracy, Longitude, Latitude, Heading, Timestamp, Reserved, Spare, Raim, CommState, AISStationId) VALUES ('{0}', {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, '{10}', {11}, {12}, {13}, {14}, '{15}')",
pr.Id, pr.MMSI, 0, 0, pr.CogVal, pr.SogVal, 0, pr.LongitudeVal, pr.LatitudeVal,
pr.TrueHeading ?? 511, pr.DBTimestamp, pr.Reserved, pr.Spare, pr.Raim, pr.CommState, (aisStation != null) ? aisStation.Id : Guid.Empty);
con.ExecuteNonQuery(query);
pr.IsNew = false;
return pr.Id;
}
if (target.LastPosReport is AIS_ClassBExt)
{
_log.Info("AIS class B ext not supported (yet)");
}
_log.WarnFormat("save pos report: we should not be here.. class type: {0}", target);
return null;
}
public static int ClearPosReports(DBConnector con, int olderThanDays)
{
string query = string.Format("DELETE FROM aisposreport WHERE[Timestamp] < DATEADD(day, -{0}, GETDATE())", olderThanDays);
return con.ExecuteNonQuery(query);
}
}
}