171 lines
7.4 KiB
C#
171 lines
7.4 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// getting rest api url from AppSetting in web.config file
|
|
/// </summary>
|
|
private static string _RestClientUrl = ConfigurationManager.AppSettings["RestfulApiUrl"].ToString();
|
|
|
|
#endregion
|
|
|
|
#region Device Live Tracking
|
|
/// <summary>
|
|
/// returns the current status including IsConnected and Current position of all the Vehicles whose alias is passed to the function as a parameter
|
|
/// </summary>
|
|
/// <param name="minutes">Time duration since u wanna calculate</param>
|
|
/// <param name="gps_ConnString">gps log database conn string</param>
|
|
/// <returns></returns>
|
|
public List<TrackingModel> GetDeviceUpdatedLocation(double offsetInMin, int minutes, string gps_ConnString)
|
|
{
|
|
//SqlDataReader drCurrentInfo = null;
|
|
NpgsqlDataReader drCurrentInfo = null;
|
|
|
|
try
|
|
{
|
|
List<TrackingModel> listLogHistoryModels = new List<TrackingModel>();
|
|
//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(); }
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// To live tracking users and their tracking data.
|
|
/// </summary>
|
|
/// <param name="Token">security token</param>
|
|
/// <param name="UtcMinutes">UTC minutes</param>
|
|
/// <param name="UserId">user id</param>
|
|
/// <param name="state">state name</param>
|
|
/// <param name="city">city name</param>
|
|
/// <param name="dealerId">dealer id</param>
|
|
/// <returns>Live Tracking report model</returns>
|
|
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<LiveTrackingModel>(content);
|
|
|
|
return model;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region Device History Tracking
|
|
/// <summary>
|
|
/// Get position history of a vehicle b/w two dates by its device alias
|
|
/// </summary>
|
|
/// <param name="startDate">minimum date criteria</param>
|
|
/// <param name="toDate">Maximum date criteria</param>
|
|
/// <param name="alias">device Alias</param>
|
|
/// <param name="gps_ConnString">gpsdevice log conn string</param>
|
|
/// <returns></returns>
|
|
public List<TrackingModel> GetDeviceHistory(DateTime startDate, DateTime toDate, string[] alias, string gps_ConnString)
|
|
{
|
|
//SqlDataReader drCurrentInfo = null;
|
|
NpgsqlDataReader drCurrentInfo = null;
|
|
|
|
try
|
|
{
|
|
List<TrackingModel> listLogHistoryModels = new List<TrackingModel>();
|
|
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
|
|
}
|
|
} |