80 lines
3.2 KiB
C#
80 lines
3.2 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
using bsmd.AISService.AIS;
|
|
|
|
namespace bsmd.AISService.DB
|
|
{
|
|
|
|
internal class AISPosReport
|
|
{
|
|
/// <summary>
|
|
/// Saves a (class A or B) position report
|
|
/// </summary>
|
|
/// <param name="target">target to save</param>
|
|
/// <returns>id of insert operation (to update hotposition table)</returns>
|
|
public static int? 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;
|
|
}
|
|
|
|
string query = string.Format("INSERT INTO aisposreport (mmsi, navstatus, rot, cog, sog, accur, longitude, latitude, heading, timestamp, stationid) VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, '{9}', {10})",
|
|
pr.MMSI, pr.NavStatusVal, pr.ROTVal, pr.COGVal, pr.SOGVal, pr.Accuracy,
|
|
pr.LongitudeVal, pr.LatitudeVal, pr.TrueHeading ?? 511, pr.DBTimestamp,
|
|
(aisStation != null) ? aisStation.Id : 0);
|
|
|
|
con.ExecuteNonQuery(query);
|
|
|
|
object result = con.ExecuteScalar("SELECT LAST_INSERT_ID()");
|
|
if (result == null) return null;
|
|
int pid = Convert.ToInt32(result);
|
|
return pid;
|
|
}
|
|
|
|
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);
|
|
|
|
string query = string.Format("INSERT INTO aisposreport (mmsi, navstatus, rot, cog, sog, accur, longitude, latitude, heading, timestamp, stationid) VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, '{9}', {10})",
|
|
pr.MMSI, 0, 0, pr.CogVal, pr.SogVal, 0, pr.LongitudeVal, pr.LatitudeVal,
|
|
pr.TrueHeading ?? 511, pr.DBTimestamp, (aisStation != null) ? aisStation.Id : 0);
|
|
|
|
con.ExecuteNonQuery(query);
|
|
|
|
object result = con.ExecuteScalar("SELECT LAST_INSERT_ID()");
|
|
if (result == null) return null;
|
|
int pid = Convert.ToInt32(result);
|
|
return pid;
|
|
}
|
|
|
|
if (target.LastPosReport is AIS_ClassBExt)
|
|
{
|
|
Trace.WriteLine("AIS class B ext not supported (yet)");
|
|
// TODO: Import ClassB Extended report!
|
|
|
|
}
|
|
|
|
Trace.WriteLine(string.Format("save pos report: we should not be here.. class type: {0}", target));
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
}
|
|
}
|