using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Globalization; using bsmd.AISService.AIS; namespace bsmd.AISService.DB { public class AISStation { #region private members private int station_Id; private string name; private bool active; private string comport; private int baudrate; private int telnetPort; private string telnetHost; private bool onAir = false; private double rangeMax = 0; private double rangeAverage; private double coverage; private double latitude; private double longitude; private string address; private DateTime? lastPosTimestamp; private bool isDirty = false; private Dictionary targets = new Dictionary(); #endregion #region Properties public string Name { get { return this.name; } set { this.name = value; } } public int Id { get { return this.station_Id; } } public bool Active { get { return this.active; } set { this.active = value; } } public string COMPort { get { return this.comport; } set { this.comport = value; } } public int Baudrate { get { return this.baudrate; } set { this.baudrate = value; } } public string TelnetHost { get { return this.telnetHost; } set { this.telnetHost = value; } } public int TelnetPort { get { return this.telnetPort; } set { this.telnetPort = value; } } public bool OnAir { get { return this.onAir; } set { this.onAir = value; } } public double RangeMax { get { return this.rangeMax; } } public double RangeAverage { get { return this.rangeAverage; } } public double Coverage { get { return this.coverage; } } public string CoverageText { get { return string.Format("{0} qkm", this.coverage.ToString("N2")); } } public double Latitude { get { return this.latitude; } set { this.latitude = value; this.isDirty = true; this.rangeMax = 0; } } public double Longitude { get { return this.longitude; } set { this.longitude = value; this.isDirty = true; this.rangeMax = 0; } } public string Address { get { return this.address; } } public bool IsDirty { get { return this.isDirty; } } public DateTime? LastPosTimestamp { get { return this.lastPosTimestamp; } set { this.lastPosTimestamp = value; } } public int NumTargets { get { return this.targets.Count; } } public Dictionary Targets { get { return this.targets; } } public bool MustDelete { get; set; } #endregion #region public methods public bool Save(DBConnector con) { string query = string.Format("UPDATE aisstation SET lat={0}, lon={1}, telnetHost='{2}', telnetPort={3}, comPort='{4}', name='{5}', baudrate={6} WHERE id={7}", (int) (this.latitude * 600000), (int) (this.longitude * 600000), this.telnetHost, this.telnetPort, this.comport, this.name, this.baudrate, this.station_Id); if (con.ExecuteNonQuery(query) == 1) { this.isDirty = false; return true; } return false; } public void UpdateWithPositionReport(int mmsi, double lat, double lon, DateTime timestamp) { double distance = bsmd.AISService.AIS.AIS.GetDistance(this.Latitude, this.Longitude, lat, lon); if (distance > this.rangeMax) { this.rangeMax = distance; this.coverage = Math.PI * this.rangeMax * this.rangeMax; } lock (this.Targets) { if (!this.targets.ContainsKey(mmsi)) this.targets.Add(mmsi, distance); else this.targets[mmsi] = distance; } // durchschnittl. Reichweite double sumRange = 0; foreach (int key in this.targets.Keys) sumRange += this.targets[mmsi]; this.rangeAverage = sumRange / (double)this.targets.Count; if (!this.lastPosTimestamp.HasValue) this.lastPosTimestamp = timestamp; else { if (this.lastPosTimestamp.Value < timestamp) this.lastPosTimestamp = timestamp; } } /// /// clear targets and reset coverage /// public void ResetStation() { this.targets.Clear(); this.coverage = 0; this.rangeAverage = 0; this.rangeMax = 0; } /// /// deletes this station /// public void Delete(DBConnector con) { string query = string.Format("DELETE FROM aisstation WHERE id={0}", this.Id); con.ExecuteNonQuery(query); } #endregion #region static methods public static List LoadStations(DBConnector con) { List result = new List(); string query = "SELECT id, name, active, lat, lon, address, telnetHost, telnetPort, comPort, baudrate FROM aisstation"; IDataReader reader = con.ExecuteQuery(query); if (reader == null) return result; while (reader.Read()) { AISStation station = new AISStation(); station.station_Id = reader.GetInt32(0); station.name = reader.GetString(1); station.active = reader.GetBoolean(2); station.latitude = (double) reader.GetInt32(3) / 600000; station.longitude = (double) reader.GetInt32(4) / 600000; if(!reader.IsDBNull(5)) station.address = reader.GetString(5); if (!reader.IsDBNull(6)) station.telnetHost = reader.GetString(6); if (!reader.IsDBNull(7)) station.telnetPort = reader.GetInt32(7); if (!reader.IsDBNull(8)) station.comport = reader.GetString(8); if (!reader.IsDBNull(9)) station.baudrate = reader.GetInt32(9); result.Add(station); } reader.Close(); return result; } public static AISStation CreateStation(string name, DBConnector con) { AISStation newStation = new AISStation(); newStation.name = name; newStation.active = true; string query = string.Format("INSERT INTO aisstation SET name='{0}',active=1", name); con.ExecuteNonQuery(query); newStation.station_Id = Convert.ToInt32(con.ExecuteScalar("SELECT LAST_INSERT_ID()")); return newStation; } public static List CreateSerial_IOs(List stationList) { List result = new List(); foreach (AISStation station in stationList) { if ((station.COMPort != null) && (station.COMPort.Length > 0)) { Serial_IO serialIO = new Serial_IO(); serialIO.BaudRate = (station.Baudrate == 0) ? 9600 : station.Baudrate; serialIO.ComPort = station.COMPort; serialIO.StationName = station.Name; result.Add(serialIO); } } return result; } public static List CreateAIS_Telnets(List stationList) { List result = new List(); foreach (AISStation station in stationList) { if ((station.TelnetHost != null) && (station.TelnetHost.Length > 0)) { try { AIS_Telnet telnet = new AIS_Telnet(station.TelnetHost, station.TelnetPort); telnet.StationName = station.Name; result.Add(telnet); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(string.Format("AIS_Telnet: cannot connect to host {0} port {1}: {2}", station.TelnetHost ?? "", station.TelnetPort, ex.Message)); } } } return result; } #endregion } }