456 lines
20 KiB
C#
456 lines
20 KiB
C#
namespace VECV_WebApi.Controllers.Tracking
|
|
{
|
|
#region Namespaces
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
using System.Net.Http;
|
|
using System.Net;
|
|
using VECV_WebApi.Models.Tracking;
|
|
using VECV_WebApi.Models.Util;
|
|
using System.Configuration;
|
|
using VECV_WebApi.Models.ServiceEngineer;
|
|
using LoggingHelper;
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// This controller is used for manage tracking details
|
|
/// </summary>
|
|
public class TrackingController : ApiController
|
|
{
|
|
#region Global Variable
|
|
|
|
/// <summary>
|
|
/// making object of LoggingUtility class available to this class
|
|
/// </summary>
|
|
LoggingUtility objLog = new LoggingUtility();
|
|
|
|
/// <summary>
|
|
/// making the data-log file path available to this class
|
|
/// </summary>
|
|
string path = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["PathLog"]);
|
|
|
|
/// <summary>
|
|
/// making data log file path available to this class
|
|
/// </summary>
|
|
string logtf = (ConfigurationManager.AppSettings["Log"]);
|
|
|
|
/// <summary>
|
|
/// making error log file path available to this class
|
|
/// </summary>
|
|
string errorlogtf = (ConfigurationManager.AppSettings["ErrorLog"]);
|
|
|
|
/// <summary>
|
|
/// making Tracking Repository object available to this class
|
|
/// </summary>
|
|
TrackingRepository objTrackingRepository;
|
|
|
|
/// <summary>
|
|
/// making Tracking status class object available to this class
|
|
/// </summary>
|
|
TrackingStatus objTrackingStatus;
|
|
|
|
/// <summary>
|
|
/// making the Database connection string available to this class
|
|
/// </summary>
|
|
private string _connStr = ConfigurationManager.ConnectionStrings["Vecv_Tracking"].ToString();
|
|
|
|
#endregion
|
|
|
|
|
|
#region APIs
|
|
|
|
/// <summary>
|
|
/// To insert tracking detail in database user wise
|
|
/// </summary>
|
|
/// <param name="model">user tracking detail</param>
|
|
/// <returns>status, if 1 record inserted successfully else 0 </returns>
|
|
public TrackingStatus Post([FromBody] TrackingModel model)
|
|
{
|
|
try
|
|
{
|
|
objTrackingStatus = new TrackingStatus();
|
|
objTrackingStatus.status = 0;
|
|
objTrackingRepository = new TrackingRepository(_connStr);
|
|
|
|
try
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(model.DeviceAlias.Trim()))
|
|
{
|
|
objTrackingStatus.status = 0;
|
|
objTrackingStatus.Message = "Tracking Detail is Missing Please Contact your Administrator" + model.DeviceAlias + model.Latitude + model.Longitude;
|
|
objLog.AddLogFile("InsertTrackingDetail_Controller_devicealias DeviceAlias null", model.DeviceAlias, DateTime.Now.ToString(ConfigurationManager.AppSettings["DateTimeFormat"]), path, logtf);
|
|
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(model.Longitude.Trim()))
|
|
{
|
|
objLog.AddLogFile("InsertTrackingDetail_Controller_latitude Longitude is null", model.DeviceAlias, DateTime.Now.ToString(ConfigurationManager.AppSettings["DateTimeFormat"]), path, logtf);
|
|
|
|
}
|
|
else
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(model.Latitude.Trim()))
|
|
{
|
|
objLog.AddLogFile("InsertTrackingDetail_Controller_latitude Latitude is null", model.DeviceAlias, DateTime.Now.ToString(ConfigurationManager.AppSettings["DateTimeFormat"]), path, logtf);
|
|
|
|
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(model.LogTime))
|
|
{
|
|
objLog.AddLogFile("InsertTrackingDetail_Controller_latitude LogTime is null", model.DeviceAlias, DateTime.Now.ToString(ConfigurationManager.AppSettings["DateTimeFormat"]), path, logtf);
|
|
|
|
}
|
|
else
|
|
{
|
|
model.Xcordinate = Convert.ToString(Convert.ToInt16(ConfigurationManager.AppSettings["EarthRadius"]) * Math.Cos(Convert.ToDouble(model.Latitude)) * Math.Cos(Convert.ToDouble(model.Longitude)));
|
|
model.Ycordinate = Convert.ToString(Convert.ToInt16(ConfigurationManager.AppSettings["EarthRadius"]) * Math.Cos(Convert.ToDouble(model.Latitude)) * Math.Sin(Convert.ToDouble(model.Longitude)));
|
|
model.Zcordinate = Convert.ToString(Convert.ToInt16(ConfigurationManager.AppSettings["EarthRadius"]) * Math.Sin(Convert.ToDouble(model.Latitude)));
|
|
objTrackingStatus = objTrackingRepository.InsertTrackingDetail(model);
|
|
|
|
|
|
}
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("InsertTrackingDetail_Controller_logtime", Ex.Message + model.DeviceAlias + "LogTime" + model.LogTime, path, errorlogtf);
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("InsertTrackingDetail_Controller_latitude", Ex.Message + model.DeviceAlias + "Latitude" + model.Latitude, path, errorlogtf);
|
|
|
|
}
|
|
}
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("InsertTrackingDetail_Controller_longitude", Ex.Message + model.DeviceAlias + "logitude" + model.Longitude, path, errorlogtf);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("InsertTrackingDetail_Controller_devicealias DeviceAlias error", Ex.Message + model.DeviceAlias, path, errorlogtf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return objTrackingStatus;
|
|
|
|
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("InsertTrackingDetail_Controller", Ex.Message, path, errorlogtf);
|
|
|
|
objTrackingStatus = new TrackingStatus();
|
|
objTrackingStatus.status = 0;
|
|
objTrackingStatus.Message = ConfigurationManager.AppSettings["PwdErrorMsg"].ToString() + Ex.Message + model.DeviceAlias; //added on 04/12/2020
|
|
return objTrackingStatus;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// To insert bulk data into tracking
|
|
/// </summary>
|
|
/// <param name="tracking">extra param to identify the api</param>
|
|
/// <param name="tracking1">extra param to identify the api</param>
|
|
/// <param name="tracking2">extra param to identify the api</param>
|
|
/// <param name="model">bulk tracking data</param>
|
|
/// <returns>status</returns>
|
|
public TrackingStatus post([FromUri] string tracking, [FromUri] string tracking1, [FromUri] string tracking2, [FromBody] TrackingModel model)
|
|
{
|
|
objTrackingStatus = new TrackingStatus();
|
|
objTrackingStatus.status = 0;
|
|
objTrackingRepository = new TrackingRepository(_connStr);
|
|
objTrackingStatus = objTrackingRepository.InsertBulkTrackingDetail(model);
|
|
return objTrackingStatus;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// To get tracking detail of a device date wise
|
|
/// </summary>
|
|
/// <param name="tracking">extra param to identify the api</param>
|
|
/// <param name="tracking1">extra param to identify the api</param>
|
|
/// <param name="model">device info</param>
|
|
/// <returns>status and data</returns>
|
|
public List<object> post([FromUri] string tracking, [FromUri] string tracking1, [FromBody] TrackingGetModel model)
|
|
{
|
|
// write data log into file
|
|
try
|
|
{
|
|
DistanceCalc oDistanceCalc = new DistanceCalc();
|
|
|
|
List<TrackingModel> objList = new List<TrackingModel>();
|
|
List<object> oList = new List<object>();
|
|
objTrackingRepository = new TrackingRepository(_connStr);
|
|
|
|
//Get tracking details for device history log.
|
|
objList = objTrackingRepository.GetTrackingDetailDeviceAliasWise(model);
|
|
|
|
List<object> oPathDetail = new List<object>();
|
|
List<object> oPathPoint = new List<object>();
|
|
|
|
// define local variable for deafult value.
|
|
double lat = 0.0; double lng = 0.0; double tempLat = 0.0; double tempLng = 0.0;
|
|
int counter = 0;
|
|
// filter tracking detail for a device alias.
|
|
var data = objList.Where(w => w.DeviceAlias == model.DeviceAlias);
|
|
int totalPoints = data.Count();
|
|
|
|
// get distance from config for default value of path point (like 5 km)
|
|
double distance = double.Parse(ConfigurationManager.AppSettings["Distance_BW_Add_KM"]);
|
|
|
|
// declare local variable for distance and lat long.
|
|
double totalDist = 0.0; double currDist = 0.0; double prevDist = 0.0;
|
|
double[] tempLatitude = { 0.0, 0.0 };
|
|
double[] tempLongitude = { 0.0, 0.0 };
|
|
|
|
if (data != null && totalPoints > 0)
|
|
{
|
|
// add lat long and distance in list
|
|
oPathPoint.Add(
|
|
new
|
|
{
|
|
latitude = Convert.ToDecimal(data.Select(s => s.Latitude).First()).ToString("0.00000"),
|
|
longitude = Convert.ToDecimal(data.Select(s => s.Longitude).First()).ToString("0.00000"),
|
|
recordTime = Convert.ToDateTime(data.Select(s => s.LogTime).First()).ToString("HH:mm:ss"),
|
|
distance = totalDist.ToString("N2") + " KM."
|
|
});
|
|
// calculate total distance and path points.
|
|
foreach (var item in data)
|
|
{
|
|
oPathDetail.Add(new { latitude = item.Latitude, longitude = item.Longitude, recordTime = Convert.ToDateTime(item.LogTime).ToString("HH:mm:ss") });
|
|
|
|
if (double.TryParse(item.Latitude, out tempLat) && double.TryParse(item.Longitude, out tempLng))
|
|
{
|
|
tempLatitude[0] = tempLatitude[1]; tempLongitude[0] = tempLongitude[1];
|
|
tempLatitude[1] = tempLat; tempLongitude[1] = tempLng;
|
|
if (counter == 0) { tempLatitude[0] = tempLat; tempLongitude[0] = tempLng; }
|
|
lat += tempLat; lng += tempLng; counter++;
|
|
}
|
|
|
|
currDist = oDistanceCalc.DistanceInKM(tempLatitude, tempLongitude);
|
|
totalDist = totalDist + currDist;
|
|
prevDist = prevDist + currDist;
|
|
|
|
//check distance is less then calculated distance if condition true and create a path point.
|
|
if (prevDist >= distance)
|
|
{
|
|
tempLatitude[0] = tempLatitude[1];
|
|
tempLongitude[0] = tempLongitude[1];
|
|
tempLatitude[1] = tempLat;
|
|
tempLongitude[1] = tempLng;
|
|
oPathPoint.Add(new { latitude = Convert.ToDecimal(item.Latitude).ToString("0.00000"), longitude = Convert.ToDecimal(item.Longitude).ToString("0.00000"), recordTime = Convert.ToDateTime(item.LogTime).ToString("HH:mm:ss"), distance = totalDist.ToString("N2") + " KM." });
|
|
prevDist = 0.0;
|
|
}
|
|
|
|
}
|
|
oPathPoint.Add(
|
|
new
|
|
{
|
|
latitude = Convert.ToDecimal(data.Select(s => s.Latitude).Last()).ToString("0.00000"),
|
|
longitude = Convert.ToDecimal(data.Select(s => s.Longitude).Last()).ToString("0.00000"),
|
|
recordTime = Convert.ToDateTime(data.Select(s => s.LogTime).Last()).ToString("HH:mm:ss"),
|
|
distance = (totalDist + currDist).ToString("N2") + " KM."
|
|
});
|
|
|
|
lat = lat / counter;
|
|
lng = lng / counter;
|
|
|
|
// add path point in another list and return objects list.
|
|
oList.Add(new
|
|
{
|
|
alias = model.DeviceAlias,
|
|
color = "",
|
|
centerLat = lat,
|
|
centerLng = lng,
|
|
path = oPathDetail,
|
|
sourceImage = "",
|
|
destinationImage = "",
|
|
imgCircle = "",
|
|
points = oPathPoint
|
|
});
|
|
}
|
|
|
|
return oList;
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("GetTrackingDetailDeviceAliasWise_Controller", Ex.Message, path, errorlogtf);
|
|
|
|
throw Ex;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// To get tracking detail of user id wise
|
|
/// </summary>
|
|
/// <param name="Tracking">extra param to idenitify the api</param>
|
|
/// <param name="model">device info</param>
|
|
/// <returns>status and data</returns>
|
|
public CurrentTracking post([FromUri] string Tracking, [FromBody] VanModel model)
|
|
{
|
|
// write data log into file
|
|
CurrentTracking objCurrentTracking = new CurrentTracking();
|
|
try
|
|
{
|
|
objTrackingRepository = new TrackingRepository(_connStr);
|
|
objCurrentTracking = objTrackingRepository.GetCurrentTrackingDetailUserIdWIse(model);
|
|
return objCurrentTracking;
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("GetCurrentTrackingDetailUserIdWIse_Controller", Ex.Message, path, errorlogtf);
|
|
|
|
objCurrentTracking.status = "0";
|
|
objCurrentTracking.Message = ConfigurationManager.AppSettings["PwdErrorMsg"].ToString() + Ex.Message; ;
|
|
return objCurrentTracking;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// To get tracking detail of dealer, city and state of van
|
|
/// </summary>
|
|
/// <param name="Tracking">extra param to identify the api</param>
|
|
/// <param name="Tracking1">extra param to identify the api</param>
|
|
/// <param name="Tracking2">extra param to identify the api</param>
|
|
/// <param name="Tracking3">extra param to identify the api</param>
|
|
/// <param name="model">van info</param>
|
|
/// <returns>status and data</returns>
|
|
public DealerVanModel Post([FromUri] string Tracking, [FromUri] string Tracking1, [FromUri] string Tracking2, [FromUri] string Tracking3, [FromBody] VanModel model)
|
|
{
|
|
// write data log into file
|
|
DealerVanModel objDealerVanModel = new DealerVanModel();
|
|
try
|
|
{
|
|
objTrackingRepository = new TrackingRepository(_connStr);
|
|
objDealerVanModel = objTrackingRepository.GetDealerVanDetail(model);
|
|
return objDealerVanModel;
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("GetVanDealerStateCityWise", Ex.Message, path, errorlogtf);
|
|
|
|
objDealerVanModel.status = "0";
|
|
return objDealerVanModel;
|
|
}
|
|
}
|
|
|
|
|
|
#region Tracking details device alias wise for open ticket report
|
|
|
|
/// <summary>
|
|
/// To get tracking detail of a device date wise
|
|
/// </summary>
|
|
/// <param name="tracking">extra param to identify the api</param>
|
|
/// <param name="tracking1">extra param to identify the api</param>
|
|
/// <param name="model">device info</param>
|
|
/// <returns>status and data</returns>
|
|
public List<TrackingModel> post([FromUri] string trackingDeviceWise, [FromBody] TrackingGetModel model)
|
|
{
|
|
// write data log into file
|
|
try
|
|
{
|
|
DistanceCalc oDistanceCalc = new DistanceCalc();
|
|
|
|
List<TrackingModel> objList = new List<TrackingModel>();
|
|
List<object> oList = new List<object>();
|
|
objTrackingRepository = new TrackingRepository(_connStr);
|
|
|
|
//Get tracking details for device history log.
|
|
objList = objTrackingRepository.GetTrackingDetailDeviceAliasWise(model);
|
|
|
|
|
|
return objList;
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("GetTrackingDetailDeviceAliasWise_Controller", Ex.Message, path, errorlogtf);
|
|
|
|
throw Ex;
|
|
}
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
public DealerVanModel Post([FromUri] string Tracking, [FromUri] string VECVUser, [FromUri] string Tracking1, [FromUri] string Tracking2, [FromUri] string Tracking3, [FromUri] string Tracking4, [FromBody] VanModel model)
|
|
{
|
|
DealerVanModel objDealerVanModel = new DealerVanModel();
|
|
try
|
|
{
|
|
string _connStrTemp = ConfigurationManager.ConnectionStrings["Vecv_GoData"].ToString();
|
|
objTrackingRepository = new TrackingRepository(_connStrTemp);
|
|
objDealerVanModel = objTrackingRepository.SaveVanWorkingHours(model);
|
|
return objDealerVanModel;
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("SaveVanWorkingTime", Ex.Message, path, errorlogtf);
|
|
|
|
objDealerVanModel.status = "0";
|
|
return objDealerVanModel;
|
|
}
|
|
}
|
|
|
|
|
|
public CurrentTracking post([FromUri] string Tracking, [FromUri] string VECVUser, [FromUri] string isdealer, [FromUri] string TicketType, [FromUri] string dealer, [FromBody] VanModel model)
|
|
{
|
|
CurrentTracking objCurrentTracking = new CurrentTracking();
|
|
try
|
|
{
|
|
objTrackingRepository = new TrackingRepository(_connStr);
|
|
objCurrentTracking = objTrackingRepository.GetCurrentTrackingDetailUserIdWIseVECV(model);
|
|
return objCurrentTracking;
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
// write error log into file
|
|
objLog.ErrorLogFile("GetCurrentTrackingDetailUserIdWIse_Controller", Ex.Message, path, errorlogtf);
|
|
|
|
objCurrentTracking.status = "0";
|
|
objCurrentTracking.Message = ConfigurationManager.AppSettings["PwdErrorMsg"].ToString() + Ex.Message; ;
|
|
return objCurrentTracking;
|
|
}
|
|
}
|
|
|
|
|
|
#endregion
|
|
}
|
|
}
|