namespace GODATA.Models.Tracking { using System; using System.Collections.Generic; using System.Configuration; using System.Data; //using System.Data.SqlClient; using System.Web; using System.Linq; //using SQLUtility; using Npgsql; using DBHelper; using Newtonsoft.Json; using RestSharp; public class TrackingRepository : ITracking { #region Global Variables private string _con = string.Empty; /// /// getting rest api url from AppSetting in web.config file /// private static string _RestClientUrl = ConfigurationManager.AppSettings["RestfulApiUrl"].ToString(); #endregion #region Device Live Tracking /// /// returns the current status including IsConnected and Current position of all the Vehicles whose alias is passed to the function as a parameter /// /// Time duration since u wanna calculate /// gps log database conn string /// public List GetDeviceUpdatedLocation(double offsetInMin, int minutes, string gps_ConnString) { //SqlDataReader drCurrentInfo = null; NpgsqlDataReader drCurrentInfo = null; try { List listLogHistoryModels = new List(); //SqlParameter[] param = new SqlParameter[1]; NpgsqlParameter[] param = new NpgsqlParameter[1]; //param[0] = new SqlParameter("@minutes", minutes); //drCurrentInfo = SqlHelper.ExecuteReader(gps_ConnString, CommandType.StoredProcedure, "sp_GetFFTUpdatedLocation", param); param[0] = new NpgsqlParameter("@minutes", minutes); drCurrentInfo = NpgSqlHelper.ExecuteReader(gps_ConnString, CommandType.StoredProcedure, "sp_GetFFTUpdatedLocation", param); while (drCurrentInfo.Read()) { // oLogHistoryModels listLogHistoryModels.Add(new TrackingModel { DeviceAlias = drCurrentInfo["DeviceAlias"].ToString(), Latitude = drCurrentInfo["Latitude"].ToString(), Longitude = drCurrentInfo["Longitude"].ToString(), LogId = Convert.ToInt32(drCurrentInfo["LogId"]), IsConnected = Convert.ToBoolean(drCurrentInfo["IsConnected"]), LogTime = Convert.ToDateTime(drCurrentInfo["LogTime"]).AddMinutes(offsetInMin) }); } return listLogHistoryModels; } catch { throw new Exception(); } finally { if (!drCurrentInfo.IsClosed)drCurrentInfo.Close(); } } /// /// To live tracking users and their tracking data. /// /// security token /// UTC minutes /// user id /// state name /// city name /// dealer id /// Live Tracking report model public LiveTrackingModel GetLiveTrackingReport(string Token, int UtcMinutes, string UserId, string state, string city, string dealer) { try { LiveTrackingModel model = new LiveTrackingModel(); var client = new RestSharp.RestClient(_RestClientUrl); var request = new RestRequest(ConfigurationManager.AppSettings["liveTrackingApi"].ToString(), Method.POST); request.AddParameter("Token", Token); request.AddParameter("UtcMinute", UtcMinutes); request.AddParameter("UserId", UserId); request.AddParameter("VanStateParam", state); request.AddParameter("VanCityParam", city); request.AddParameter("DealerNameParam", dealer); request.RequestFormat = DataFormat.Json; request.AddHeader("content-type", "application/json"); var response = client.Execute(request); var content = response.Content; model = JsonConvert.DeserializeObject(content); return model; } catch (Exception ex) { throw ex; } } #endregion #region Device History Tracking /// /// Get position history of a vehicle b/w two dates by its device alias /// /// minimum date criteria /// Maximum date criteria /// device Alias /// gpsdevice log conn string /// public List GetDeviceHistory(DateTime startDate, DateTime toDate, string[] alias, string gps_ConnString) { //SqlDataReader drCurrentInfo = null; NpgsqlDataReader drCurrentInfo = null; try { List listLogHistoryModels = new List(); TrackingModel oLogHistoryModels = null; string _alias = alias != null ? string.Join(",", alias) : null; //SqlParameter[] param = new SqlParameter[3]; //param[0] = new SqlParameter("@minDate", startDate); //param[1] = new SqlParameter("@maxDate", toDate); //param[2] = new SqlParameter("@DeviceAlias", _alias); //drCurrentInfo = SqlHelper.ExecuteReader(gps_ConnString, CommandType.StoredProcedure, "sp_GetVehicleHistory", param); NpgsqlParameter[] param = new NpgsqlParameter[3]; param[0] = new NpgsqlParameter("@minDate", startDate); param[1] = new NpgsqlParameter("@maxDate", toDate); param[2] = new NpgsqlParameter("@DeviceAlias", _alias); drCurrentInfo = NpgSqlHelper.ExecuteReader(gps_ConnString, CommandType.StoredProcedure, "sp_GetVehicleHistory", param); while (drCurrentInfo.Read()) { oLogHistoryModels = new TrackingModel(); oLogHistoryModels.DeviceAlias = drCurrentInfo["DeviceAlias"].ToString(); oLogHistoryModels.Latitude = drCurrentInfo["Latitude"].ToString(); oLogHistoryModels.Longitude = drCurrentInfo["Longitude"].ToString(); oLogHistoryModels.LogId = Convert.ToInt32(drCurrentInfo["LogId"]); oLogHistoryModels.LogTime = Convert.ToDateTime(drCurrentInfo["LogTime"]); if (listLogHistoryModels.SingleOrDefault(s => s.Latitude == oLogHistoryModels.Latitude && s.Longitude == oLogHistoryModels.Longitude) == null) { listLogHistoryModels.Add(oLogHistoryModels); } } return listLogHistoryModels; } catch { throw new Exception(); } finally { if (!drCurrentInfo.IsClosed)drCurrentInfo.Close(); } } #endregion } }