using System; using System.Collections; using System.Text; using System.Diagnostics; using log4net; namespace bsmd.AIS2Service { internal class AIS_ClassB : AISClass { #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 Accuracy { get { return accuracy; } } 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 #region abstract method implementation protected override Status Decode() { BitArray bits = DecodeBinary(this._data); Status result = Status.OK; try { int type = GetInt(bits, 0, 5); if (type != 18) { result = Status.ILLEGAL_ARGUMENT; } else { this.repeatIndicator = GetInt(bits, 6, 7); this._mmsi = GetInt(bits, 8, 37); this.reserved = GetInt(bits, 38, 45); this.sog = GetInt(bits, 46, 55); this.accuracy = GetInt(bits, 56, 56); this.longitude = GetInt(bits, 57, 84); this.latitude = GetInt(bits, 85, 111); this.cog = GetInt(bits, 112, 123); this.trueHeading = GetInt(bits, 124, 132); this.utcTimestampSecs = GetInt(bits, 133,138); this.timestamp = DateTime.Now; this.reservedRegional = GetInt(bits, 139, 140); this.spare = GetInt(bits, 141, 145); this.assignedModeFlag = GetInt(bits, 146, 146); this.raimFlag = GetInt(bits, 147, 147); this.commStateSelectedFlag = GetInt(bits, 148, 148); this.commState = GetInt(bits, 149, 167); } } catch (Exception e) { _log.WarnFormat("Error decoding AIS class B posreport: {0}", e.Message); result = Status.PARSE_ERROR; } return result; } #endregion } }