46 lines
1.8 KiB
C#
46 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using log4net;
|
|
|
|
namespace bsmd.pasttrack.service
|
|
{
|
|
public class Service : IService
|
|
{
|
|
private static ILog _log = LogManager.GetLogger(typeof(Service));
|
|
|
|
public Pasttrack[] GetPasttrack(int mmsi, int mins)
|
|
{
|
|
List<Pasttrack> aList = new List<Pasttrack>();
|
|
|
|
// Query
|
|
try
|
|
{
|
|
SqlConnection con = new SqlConnection(Properties.Settings.Default.ConnectionString);
|
|
con.Open();
|
|
string cmdText = string.Format("select Latitude, Longitude, Heading, Timestamp from aisposreport where mmsi=@MMSI And timestamp > DATEADD(MINUTE, @MIN, GETDATE()) order by timestamp desc");
|
|
SqlCommand cmd = new SqlCommand(cmdText, con);
|
|
cmd.Parameters.AddWithValue("@MMSI", mmsi);
|
|
cmd.Parameters.AddWithValue("@MIN", -mins);
|
|
SqlDataReader reader = cmd.ExecuteReader();
|
|
while(reader.Read())
|
|
{
|
|
Pasttrack p = new Pasttrack();
|
|
p.Latitude = (double) reader.GetInt32(0) / 600000.0;
|
|
p.Longitude = (double) reader.GetInt32(1) / 600000.0;
|
|
p.Heading = reader.GetInt32(2);
|
|
p.Timestamp = reader.GetDateTime(3);
|
|
aList.Add(p);
|
|
}
|
|
reader.Close();
|
|
con.Close();
|
|
} catch (SqlException ex)
|
|
{
|
|
_log.ErrorFormat("Error on data query: {0}", ex.ToString());
|
|
}
|
|
_log.InfoFormat("Result pasttrack array contains {0} elems", aList.Count);
|
|
return aList.ToArray();
|
|
}
|
|
}
|
|
}
|