390 lines
17 KiB
C#
390 lines
17 KiB
C#
using LoggingHelper;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
using System.Net;
|
|
using GODATA.Models.Tracking;
|
|
using System.Configuration;
|
|
using GODATA.Models.Calcuation;
|
|
using Microsoft.Practices.EnterpriseLibrary.Logging;
|
|
using System.Globalization;
|
|
using System.Data;
|
|
using ExcelHelper;
|
|
using ExportToExcel;
|
|
|
|
namespace GODATA.Controllers
|
|
{
|
|
/// <summary>
|
|
/// class that contains all the tracking related methods (Live/History Tracking)
|
|
/// </summary>
|
|
[GODATA.MvcApplication.SessionExpire]
|
|
public class TrackingController : Controller
|
|
{
|
|
|
|
#region Global Variables
|
|
|
|
/// <summary>
|
|
/// Represent object of LoggingUtility class
|
|
/// </summary>
|
|
LoggingUtility objLog = new LoggingUtility();
|
|
|
|
/// <summary>
|
|
/// Represent string object contain log file path
|
|
/// </summary>
|
|
//string path = "~/Log/";
|
|
string path = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["PathLog"]);
|
|
|
|
/// <summary>
|
|
/// Represent string object contain log status
|
|
/// </summary>
|
|
string logtf = (ConfigurationManager.AppSettings["Log"]);
|
|
|
|
/// <summary>
|
|
/// Represent string object contain Error log status
|
|
/// </summary>
|
|
string errorlogtf = (ConfigurationManager.AppSettings["ErrorLog"]);
|
|
|
|
/// <summary>
|
|
/// getting user's utc minutes from session
|
|
/// </summary>
|
|
private static string _timeOffSetMinutes = null;
|
|
|
|
/// <summary>
|
|
/// getting login user's id
|
|
/// </summary>
|
|
private static string _LoginUserId = null;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private static string _TimeZoneId = ConfigurationManager.AppSettings["TimeZoneId"].ToString();
|
|
|
|
/// <summary>
|
|
/// URI to Path to Excel Export location.
|
|
/// </summary>
|
|
private static string _exportLocation = ConfigurationManager.AppSettings["excelExportPath"].ToString();
|
|
|
|
/// <summary>
|
|
/// Absolute Path on server in which excel files are saved
|
|
/// </summary>
|
|
private static string _excelExportPathOnServer = ConfigurationManager.AppSettings["excelExportPathOnServer"].ToString();
|
|
|
|
/// <summary>
|
|
/// getting date time format from AppSetting in web.config file
|
|
/// </summary>
|
|
private static string _dateTimeFormat = ConfigurationManager.AppSettings["dateTimeFormat"].ToString();
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// An attribute to allow CORS (Cross-site HTTP requests)
|
|
/// </summary>
|
|
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
|
|
{
|
|
public override void OnActionExecuting(ActionExecutingContext filterContext)
|
|
{
|
|
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
|
|
base.OnActionExecuting(filterContext);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// To show main page of Tracking
|
|
/// </summary>
|
|
/// <returns>main page of Tracking</returns>
|
|
[AllowCrossSiteJson]
|
|
public ActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
#region Manage Live Tracking
|
|
/// <summary>
|
|
/// To show main page of Live Tracking
|
|
/// </summary>
|
|
/// <returns>main page of Live Tracking</returns>
|
|
[AllowCrossSiteJson]
|
|
public ActionResult LiveTracking()
|
|
{
|
|
objLog.AddLogFile("Tracking_Live", DateTime.Now.ToString(ConfigurationManager.AppSettings["dateTimeFormat"]), path, logtf);
|
|
try
|
|
{
|
|
_LoginUserId = Session["UserId"].ToString();
|
|
_timeOffSetMinutes = Session["UtcMinute"].ToString();
|
|
ViewBag.SecurityToken = ConfigurationManager.AppSettings["RESTfulSecurityToken"].ToString();
|
|
ViewBag.UtcMinutes = _timeOffSetMinutes;
|
|
ViewBag.Userid = _LoginUserId;
|
|
ViewBag.UserRole = Session["UserRole"].ToString();
|
|
|
|
objLog.AddLogFile(DateTime.Now.ToString(ConfigurationManager.AppSettings["dateTimeFormat"]), path, logtf);
|
|
return View();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
objLog.ErrorLogFile("Tracking_Live", ex.Message, path, errorlogtf);
|
|
objLog.AddLogFile(DateTime.Now.ToString(ConfigurationManager.AppSettings["dateTimeFormat"]), path, logtf);
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Exports Timeslot Closure report
|
|
/// </summary>
|
|
/// <param name="Token">security token</param>
|
|
/// <param name="UtcMinutes">UTC minutes</param>
|
|
/// <param name="UserId">user id</param>
|
|
/// <param name="fromDate">start date</param>
|
|
/// <param name="toDate">end date</param>
|
|
/// <param name="state">state name</param>
|
|
/// <param name="city">city name</param>
|
|
/// <param name="dealerId">dealer id</param>
|
|
/// <returns>generated report path</returns>
|
|
//[HttpPost]
|
|
//public string ExportReport(string vanState, string vanCity, string dealerName)
|
|
//{
|
|
// try
|
|
// {
|
|
// //Getting login user's id, name and utc minutes from session
|
|
// _LoginUserId = Session["UserId"].ToString();
|
|
// //_LoginUserName = Session["UserName"].ToString();
|
|
// int timeOffSetMinutes = Convert.ToInt32(Session["UtcMinute"]);
|
|
// string securityToken = ConfigurationManager.AppSettings["RESTfulSecurityToken"].ToString();
|
|
|
|
// TrackingRepository oReportRepository = new TrackingRepository();
|
|
// LiveTrackingModel model = new LiveTrackingModel();
|
|
// model = oReportRepository.GetLiveTrackingReport(securityToken, timeOffSetMinutes, _LoginUserId, vanState, vanCity, dealerName);
|
|
|
|
// //=======================Model Mnaupulatin========================
|
|
// foreach (ListUser user in model.ListUsers)
|
|
// {
|
|
// // Making EOS team name
|
|
// string dealername = (user.DealerDealerName == null) ? "" : user.DealerDealerName.ToString();
|
|
// string serviceEngName = (user.ServiceEngineerName == null) ? "" : user.ServiceEngineerName.ToString();
|
|
// user.EOSteam = dealername + " (" + serviceEngName + ')';
|
|
|
|
// // initializing the battery status
|
|
// user.BatteryStatus = "0 %";
|
|
|
|
// // Geeting tracked user and putting the live values into it.
|
|
// foreach (ListTracking trackedUser in model.ListTracking)
|
|
// {
|
|
// string trackedUserDeviceAlias = (trackedUser.DeviceAlias == null) ? "" : trackedUser.DeviceAlias.ToString();
|
|
// if (user.ServiceEngineerDeviceAlias.ToString() == trackedUserDeviceAlias)
|
|
// {
|
|
// string isConnected = (trackedUser.IsConnected == null) ? "false" : trackedUser.IsConnected.ToString();
|
|
// user.IsConnected = (isConnected.ToUpper() == "FALSE") ? "OFFLINE" : "ONLINE";
|
|
// user.Message = Convert.ToDateTime(trackedUser.LogTime).ToString(_dateTimeFormat);
|
|
// user.BatteryStatus = trackedUser.BatteryStatus + " %";
|
|
// //user.GpsStatus = trackedUser.GpsStatus;
|
|
// //user.IsCharging = (trackedUser.IsCharging.ToString().ToUpper() == "FALSE" ? "NOT CHARGING" : "CHARGING");
|
|
// }
|
|
// }
|
|
// // replacing all the falses with OFFLINE
|
|
// user.IsConnected = (user.IsConnected.ToString().ToUpper() == "FALSE") ? "OFFLINE" : user.IsConnected;
|
|
// }
|
|
// List<ListUser> sortedUserList = model.ListUsers.OrderByDescending(o => o.IsConnected).ToList();
|
|
// //================================================================
|
|
|
|
// DataTable liveTrackingReport = new DataTable();
|
|
// if (sortedUserList != null)
|
|
// {
|
|
// liveTrackingReport = sortedUserList.ToDataTable();
|
|
// liveTrackingReport.Columns.RemoveAt(0);
|
|
// liveTrackingReport.AcceptChanges();
|
|
// }
|
|
|
|
// // getting required dataset
|
|
// DataSet ds = new DataSet();
|
|
// ds.Tables.Add(liveTrackingReport);
|
|
|
|
// //================================ Excel Functionality ===================================//
|
|
// ExcelUtility objExcelUtility = new ExcelHelper.ExcelUtility();
|
|
// List<WorkbookMappingModel> workbokkMapping = new List<WorkbookMappingModel>();
|
|
// if (ds.Tables[0].Rows.Count > 0)
|
|
// {
|
|
// workbokkMapping.Add(new WorkbookMappingModel { DataTableName = ds.Tables[0].TableName, WorkSheetName = "Live_Tracking", StartColumnName = "B", StartRowNumber = 5, WorkSheetNumber = 1, AutoFit = true });
|
|
// }
|
|
|
|
// string filename = "LiveTrackingReport_" + DateTime.Now.ToString("ddMMMyyyyHHmmss") + ".xlsx";
|
|
// string pathToExcelFile = _exportLocation + filename;
|
|
|
|
// string templatePath = Server.MapPath(ConfigurationManager.AppSettings["LiveTrackingTemplate"].ToString()); ;
|
|
|
|
// string saveAs = Server.MapPath(_excelExportPathOnServer + filename);
|
|
|
|
// ExcelHelper.StatusModel result = objExcelUtility.ExportToExcel(ds, templatePath, workbokkMapping, saveAs, false);
|
|
// //================================ Excel Functionality End ===================================//
|
|
|
|
// // returning path to generated Excel file
|
|
// if (result.Status == 1) { return pathToExcelFile; }
|
|
// else { return "error"; }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// objLog.ErrorLogFile("LiveTracking_ExportReport", ex.Message, path, errorlogtf);
|
|
// return "error";
|
|
// }
|
|
//}
|
|
|
|
#endregion
|
|
|
|
#region Manage Histroy Tracking
|
|
/// <summary>
|
|
/// To show main page of HistoryTracking
|
|
/// </summary>
|
|
/// <returns>main page of HistoryTracking</returns>
|
|
[AllowCrossSiteJson]
|
|
public ActionResult HistoryTracking()
|
|
{
|
|
objLog.AddLogFile("Tracking_History", DateTime.Now.ToString(ConfigurationManager.AppSettings["dateTimeFormat"]), path, logtf);
|
|
try
|
|
{
|
|
_LoginUserId = Session["UserId"].ToString();
|
|
_timeOffSetMinutes = Session["UtcMinute"].ToString();
|
|
ViewBag.SecurityToken = ConfigurationManager.AppSettings["RESTfulSecurityToken"].ToString();
|
|
ViewBag.UtcMinutes = _timeOffSetMinutes;
|
|
ViewBag.Userid = _LoginUserId;
|
|
|
|
objLog.AddLogFile(DateTime.Now.ToString(ConfigurationManager.AppSettings["dateTimeFormat"]), path, logtf);
|
|
return View();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
objLog.ErrorLogFile("Tracking_History", ex.Message, path, errorlogtf);
|
|
objLog.AddLogFile(DateTime.Now.ToString(ConfigurationManager.AppSettings["dateTimeFormat"]), path, logtf);
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Function used to get coler code according to number of path.
|
|
/// </summary>
|
|
/// <param name="index">Index</param>
|
|
/// <returns>Color code</returns>
|
|
[AllowCrossSiteJson]
|
|
private JsonResult GetPathColorCode(int index)
|
|
{
|
|
string[] colorCode = { "#633ebe", "#2878ed", "#db552d", "#bb1d48", "#00a31b" };
|
|
if (index > 1)
|
|
{
|
|
index = index % colorCode.Length;
|
|
return new JsonResult
|
|
{
|
|
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
|
|
Data = new { success = true, result = index }
|
|
};
|
|
}
|
|
else
|
|
{
|
|
return new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new { success = false } };
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// The popup for Showing Dealer Map
|
|
/// <returns>returns PartialView for showing Dealer Map</returns>
|
|
/// </summary>
|
|
public ActionResult WorkingHours(string van_start_working_hours, string van_end_working_hours)
|
|
{
|
|
ViewBag.vanStartWorkingHours = van_start_working_hours;
|
|
ViewBag.vanEndWorkingHours = van_end_working_hours;
|
|
return PartialView();
|
|
}
|
|
|
|
[HttpPost]
|
|
public string ExportReport(string vanState, string vanCity, string dealerName)
|
|
{
|
|
try
|
|
{
|
|
//Getting login user's id, name and utc minutes from session
|
|
_LoginUserId = Session["UserId"].ToString();
|
|
//_LoginUserName = Session["UserName"].ToString();
|
|
int timeOffSetMinutes = Convert.ToInt32(Session["UtcMinute"]);
|
|
string securityToken = ConfigurationManager.AppSettings["RESTfulSecurityToken"].ToString();
|
|
|
|
TrackingRepository oReportRepository = new TrackingRepository();
|
|
LiveTrackingModel model = new LiveTrackingModel();
|
|
model = oReportRepository.GetLiveTrackingReport(securityToken, timeOffSetMinutes, _LoginUserId, vanState, vanCity, dealerName);
|
|
|
|
//=======================Model Mnaupulatin========================
|
|
foreach (ListUser user in model.ListUsers)
|
|
{
|
|
// Making EOS team name
|
|
string dealername = (user.DealerDealerName == null) ? "" : user.DealerDealerName.ToString();
|
|
string serviceEngName = (user.ServiceEngineerName == null) ? "" : user.ServiceEngineerName.ToString();
|
|
user.EOSteam = dealername + " (" + serviceEngName + ')';
|
|
|
|
// initializing the battery status
|
|
user.BatteryStatus = "0 %";
|
|
|
|
// Geeting tracked user and putting the live values into it.
|
|
foreach (ListTracking trackedUser in model.ListTracking)
|
|
{
|
|
string trackedUserDeviceAlias = (trackedUser.DeviceAlias == null) ? "" : trackedUser.DeviceAlias.ToString();
|
|
if (user.ServiceEngineerDeviceAlias.ToString() == trackedUserDeviceAlias)
|
|
{
|
|
string isConnected = (trackedUser.IsConnected == null) ? "false" : trackedUser.IsConnected.ToString();
|
|
user.IsConnected = (isConnected.ToUpper() == "FALSE") ? "OFFLINE" : "ONLINE";
|
|
user.Message = Convert.ToDateTime(trackedUser.LogTime).ToString(_dateTimeFormat);
|
|
user.BatteryStatus = trackedUser.BatteryStatus + " %";
|
|
|
|
//user.GpsStatus = trackedUser.GpsStatus;
|
|
//user.IsCharging = (trackedUser.IsCharging.ToString().ToUpper() == "FALSE" ? "NOT CHARGING" : "CHARGING");
|
|
}
|
|
}
|
|
// replacing all the falses with OFFLINE
|
|
user.IsConnected = (user.IsConnected.ToString().ToUpper() == "FALSE") ? "OFFLINE" : user.IsConnected;
|
|
|
|
}
|
|
List<ListUser> sortedUserList = model.ListUsers.OrderByDescending(o => o.IsConnected).ToList();
|
|
//================================================================
|
|
|
|
DataTable liveTrackingReport = new DataTable();
|
|
if (sortedUserList != null)
|
|
{
|
|
liveTrackingReport = sortedUserList.ToDataTable();
|
|
liveTrackingReport.Columns.RemoveAt(0);
|
|
liveTrackingReport.Columns.RemoveAt(liveTrackingReport.Columns.Count - 2);
|
|
liveTrackingReport.Columns.RemoveAt(liveTrackingReport.Columns.Count - 2);
|
|
liveTrackingReport.AcceptChanges();
|
|
}
|
|
|
|
// getting required dataset
|
|
DataSet ds = new DataSet();
|
|
ds.Tables.Add(liveTrackingReport);
|
|
|
|
//================================ Excel Functionality ===================================//
|
|
ExcelUtility objExcelUtility = new ExcelHelper.ExcelUtility();
|
|
List<WorkbookMappingModel> workbokkMapping = new List<WorkbookMappingModel>();
|
|
if (ds.Tables[0].Rows.Count > 0)
|
|
{
|
|
workbokkMapping.Add(new WorkbookMappingModel { DataTableName = ds.Tables[0].TableName, WorkSheetName = "Live_Tracking", StartColumnName = "B", StartRowNumber = 5, WorkSheetNumber = 1, AutoFit = true });
|
|
}
|
|
|
|
string filename = "LiveTrackingReport_" + DateTime.Now.ToString("ddMMMyyyyHHmmss") + ".xlsx";
|
|
string pathToExcelFile = _exportLocation + filename;
|
|
|
|
string templatePath = Server.MapPath(ConfigurationManager.AppSettings["LiveTrackingTemplate"].ToString()); ;
|
|
|
|
string saveAs = Server.MapPath(_excelExportPathOnServer + filename);
|
|
bool isCreated = CreateExcelFile.CreateExcelDocument(ds, saveAs);
|
|
// ExcelHelper.StatusModel result = objExcelUtility.ExportToExcel(ds, templatePath, workbokkMapping, saveAs, false);
|
|
//================================ Excel Functionality End ===================================//
|
|
if (isCreated == true) { return pathToExcelFile; }
|
|
else { return "error"; }
|
|
// returning path to generated Excel file
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
objLog.ErrorLogFile("LiveTracking_ExportReport", ex.Message, path, errorlogtf);
|
|
return "error";
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|