git_bsmd/AIS/bsmd.AIS2Service/AIS_Target.cs
2025-11-11 16:54:41 +01:00

311 lines
8.2 KiB
C#

using System;
using System.Data;
namespace bsmd.AIS2Service
{
public class AIS_Target
{
#region private members
private readonly int _mmsi;
private bool? _isClassB = false;
private DateTime? _lastUpdate;
private string _name;
private string _callSign;
private double? _latitude;
private double? _longitude;
private int? _heading;
private double? _cog;
private int? _rot;
private double? _sog;
private bool? _accuracy;
private int? _maneuverIndicator;
private bool? _raim;
private int? _radio;
private int? _imo;
private int? _shipType;
private int? _aisVersion;
private int? _breadth;
private int? _length;
private int? _typeOfDevice;
private DateTime? _eta;
private double? _draught;
private string _destination;
private bool? _dte;
private AIS_Target.Type _type = Type.OTHER;
private int _navStatus = 15; // not defined
#endregion
#region public defs
public enum Type
{
PASSENGER,
CARGO,
TANKER,
HSC,
WIG,
TUG,
YACHT,
OTHER
}
#endregion
#region Construction
public AIS_Target(int mmsi)
{
this._mmsi = mmsi;
}
#endregion
#region Properties
public int MMSI
{
get { return this._mmsi; }
}
public DateTime? LastUpdate
{
get { return this._lastUpdate; } private set { this._lastUpdate = value; }
}
public string Name
{
get { return this._name; }
private set { this._name = value; }
}
public string Callsign
{
get { return this._callSign; }
private set { this._callSign = value; }
}
public double? Latitude
{
get { return this._latitude; } private set { this._latitude = value; }
}
public double? Longitude
{
get { return this._longitude; } private set { this._longitude = value; }
}
public Type TargetType
{
get { return this._type; }
set { this._type = value; }
}
public int NavStatus
{
get { return this._navStatus; } private set { this._navStatus = value; }
}
public bool? IsClassB
{
get { return this._isClassB; }
private set { this._isClassB = value; }
}
public int? Heading
{
get { return _heading; } private set { _heading = value; }
}
public double? COG
{
get { return _cog; } private set { _cog = value; }
}
public int? ROT
{
get { return _rot; } private set { _rot = value; }
}
public double? SOG
{
get { return _sog; } private set { _sog = value; }
}
public bool? Accuracy
{
get { return _accuracy; } private set { _accuracy = value; }
}
public int? ManeuverIndicator
{
get { return _maneuverIndicator; } private set { _maneuverIndicator = value; }
}
public bool? RAIM
{
get { return _raim; } private set { _raim = value; }
}
public int? Radio
{
get { return _radio; } private set { _radio = value; }
}
public int? AISVersion
{
get { return _aisVersion; } private set { _aisVersion = value; }
}
public int? IMO
{
get { return _imo; } private set { _imo = value; }
}
public int? ShipType
{
get { return _shipType; } private set { _shipType = value; }
}
public int? Breadth
{
get { return _breadth; } private set { _breadth = value; }
}
public int? Length
{
get { return _length; } private set { _length = value; }
}
public int? TypeOfDevice
{
get { return _typeOfDevice; } private set { _typeOfDevice = value; }
}
public DateTime? ETA
{
get { return _eta; } private set { _eta = value; }
}
public double? Draught
{
get { return _draught; } private set { _draught = value; }
}
public string Destination
{
get { return _destination; } private set { _destination = value; }
}
public bool? DTE
{
get { return _dte; } private set { _dte = value; }
}
public int UpdateCount { get; private set; }
#endregion
#region public methods
internal void Update(AIS_PosReport posReport)
{
this.NavStatus = posReport.NavStatusVal;
this.ROT = posReport.ROT;
this.SOG = posReport.SOG;
this.Accuracy = (posReport.Accuracy == 1);
this.Latitude = posReport.Latitude;
this.Longitude = posReport.Longitude;
this.COG = posReport.COG;
this.Heading = posReport.TrueHeading;
this.ManeuverIndicator = posReport.ManeuverIndicator;
this.RAIM = posReport.Raim == 1;
this.Radio = posReport.CommState;
this.LastUpdate = DateTime.Now;
}
internal void Update(AIS_StaticData staticData)
{
this.AISVersion = staticData.Version;
this.IMO = staticData.IMONumber;
this.Callsign = staticData.Callsign;
this.Name = staticData.Name;
this.ShipType = staticData.ShipType;
this.Breadth = staticData.Breadth;
this.Length = staticData.Length;
this.TypeOfDevice = staticData.TypeOfDevice;
this.ETA = staticData.ETA;
this.Draught = staticData.Draught;
this.Destination = staticData.Destination;
this.DTE = staticData.DTE == 0;
this.IsClassB = false;
this.LastUpdate = DateTime.Now;
}
internal void Update(AIS_ClassBStatic staticData)
{
this.LastUpdate = DateTime.Now;
}
internal static AIS_Target CreateFromReader(IDataReader reader)
{
int mmsi = reader.GetInt32(0);
AIS_Target result = new AIS_Target(mmsi);
if (!reader.IsDBNull(1))
result.AISVersion = reader.GetInt32(1);
if (!reader.IsDBNull(2))
result.IMO = reader.GetInt32(2);
if (!reader.IsDBNull(3))
result.Callsign = reader.GetString(3);
if (!reader.IsDBNull(4))
result.Name = reader.GetString(4);
if (!reader.IsDBNull(5))
result.ShipType = reader.GetInt32(5);
if (!reader.IsDBNull(6))
result.TypeOfDevice = reader.GetInt32(6);
if (!reader.IsDBNull(7))
result.Draught = reader.GetInt32(7) / 10.0;
if (!reader.IsDBNull(8))
result.Destination = reader.GetString(8);
if (!reader.IsDBNull(9))
result.DTE = reader.GetBoolean(9);
if (!reader.IsDBNull(10))
result.IsClassB = reader.GetBoolean(10);
if (!reader.IsDBNull(11))
result.Breadth = reader.GetInt32(11);
if (!reader.IsDBNull(12))
result.Length = reader.GetInt32(12);
if (!reader.IsDBNull(13))
result.ETA = DateTime.Parse(reader.GetString(13));
if (!reader.IsDBNull(14))
result.LastUpdate = DateTime.Parse(reader.GetString(14));
result.UpdateCount = reader.GetInt32(15);
return result;
}
#endregion
#region overrides
public override string ToString()
{
return string.Format("{0} [{1}]", this.Name, this.MMSI);
}
#endregion
}
}