public IActionResult GetUnitsForCallGrid(string callLat, string callLong) { List <UnitForListJson> unitsJson = new List <UnitForListJson>(); var units = _unitsService.GetUnitsForDepartment(DepartmentId); var states = _unitsService.GetAllLatestStatusForUnitsByDepartmentId(DepartmentId); var department = _departmentsService.GetDepartmentById(DepartmentId, false); foreach (var unit in units) { var unitJson = new UnitForListJson(); unitJson.Name = unit.Name; unitJson.Type = unit.Type; unitJson.UnitId = unit.UnitId; if (unit.StationGroup != null) { unitJson.Station = unit.StationGroup.Name; } var state = states.FirstOrDefault(x => x.UnitId == unit.UnitId); if (state != null) { var customState = CustomStatesHelper.GetCustomUnitState(state); unitJson.StateId = state.State; unitJson.State = customState.ButtonText; unitJson.StateColor = customState.ButtonColor; unitJson.TextColor = customState.TextColor; unitJson.Timestamp = state.Timestamp.TimeConverterToString(department); if (String.IsNullOrWhiteSpace(callLat) || String.IsNullOrWhiteSpace(callLong)) { unitJson.Eta = "N/A"; } else { var location = _unitsService.GetLatestUnitLocation(state.UnitId, state.Timestamp); if (location != null) { var eta = _geoService.GetEtaInSeconds($"{location.Latitude},{location.Longitude}", String.Format("{0},{1}", callLat, callLong)); if (eta > 0) { unitJson.Eta = $"{Math.Round(eta / 60, MidpointRounding.AwayFromZero)}m"; } else { unitJson.Eta = "N/A"; } } else if (!String.IsNullOrWhiteSpace(state.GeoLocationData)) { var eta = _geoService.GetEtaInSeconds(state.GeoLocationData, String.Format("{0},{1}", callLat, callLong)); if (eta > 0) { unitJson.Eta = $"{Math.Round(eta / 60, MidpointRounding.AwayFromZero)}m"; } else { unitJson.Eta = "N/A"; } } else { unitJson.Eta = "N/A"; } } } unitsJson.Add(unitJson); } return(Json(unitsJson)); }