git_bsmd/AIS/bsmd.AISService/AIS/AIS_ClassB.cs

151 lines
4.3 KiB
C#

using System;
using System.Collections;
using System.Text;
using System.Diagnostics;
using log4net;
namespace bsmd.AISService.AIS
{
public class AIS_ClassB : AIS
{
#region private members
private Guid? id;
private int repeatIndicator;
private int reserved;
private int sog;
private int accuracy;
private int longitude;
private int latitude;
private int cog;
private int trueHeading;
private int utcTimestampSecs;
private DateTime timestamp;
private int reservedRegional;
private int spare;
private int assignedModeFlag;
private int raimFlag;
private int commStateSelectedFlag;
private int commState;
private static ILog _log = LogManager.GetLogger(typeof(AIS_ClassB));
#endregion
#region Properties
public Guid Id { get { if (!this.id.HasValue) this.id = Guid.NewGuid(); return this.id.Value; } }
public int CogVal { get { return this.cog; } }
public int SogVal { get { return this.sog; } }
public int LatitudeVal { get { return this.latitude; } }
public int LongitudeVal { get { return this.longitude; } }
public string DBTimestamp
{
get
{
return this.timestamp.ToString("yyyy-MM-ddTHH:mm:ss.000Z");
}
}
public double Cog
{
get { return this.cog / 10.0f; }
}
public double Sog
{
get { return this.sog / 10.0f; }
}
public double Latitude
{
get { return this.latitude / 600000.0f; }
}
public double Longitude
{
get { return this.longitude / 600000.0f; }
}
public DateTime Timestamp
{
get { return this.timestamp; }
}
public int? TrueHeading
{
get
{
if (this.trueHeading == 511) return null;
return this.trueHeading;
}
}
public int Reserved
{
get { return reserved; }
}
public int Spare
{
get { return this.spare; }
}
public int Raim
{
get { return this.raimFlag; }
}
public int CommState
{
get { return this.commState; }
}
#endregion
protected override AIS.Status Decode()
{
BitArray bits = AIS.DecodeBinary(this.data);
Status result = Status.OK;
try
{
int type = AIS.GetInt(bits, 0, 5);
if (type != 18)
{
result = Status.ILLEGAL_ARGUMENT;
}
else
{
this.repeatIndicator = AIS.GetInt(bits, 6, 7);
this.userId = AIS.GetInt(bits, 8, 37);
this.reserved = AIS.GetInt(bits, 38, 45);
this.sog = AIS.GetInt(bits, 46, 55);
this.accuracy = AIS.GetInt(bits, 56, 56);
this.longitude = AIS.GetInt(bits, 57, 84);
this.latitude = AIS.GetInt(bits, 85, 111);
this.cog = AIS.GetInt(bits, 112, 123);
this.trueHeading = AIS.GetInt(bits, 124, 132);
this.utcTimestampSecs = AIS.GetInt(bits, 133,138);
this.timestamp = DateTime.Now;
this.reservedRegional = AIS.GetInt(bits, 139, 140);
this.spare = AIS.GetInt(bits, 141, 145);
this.assignedModeFlag = AIS.GetInt(bits, 146, 146);
this.raimFlag = AIS.GetInt(bits, 147, 147);
this.commStateSelectedFlag = AIS.GetInt(bits, 148, 148);
this.commState = AIS.GetInt(bits, 149, 167);
}
}
catch (Exception e)
{
_log.WarnFormat("Error decoding AIS class B posreport: {0}", e.Message);
result = Status.PARSE_ERROR;
}
return result;
}
}
}