public async Task <IActionResult> GetUnitsForCallGrid(string callLat, string callLong) { List <UnitForListJson> unitsJson = new List <UnitForListJson>(); var units = await _unitsService.GetUnitsForDepartmentAsync(DepartmentId); var states = await _unitsService.GetAllLatestStatusForUnitsByDepartmentIdAsync(DepartmentId); var department = await _departmentsService.GetDepartmentByIdAsync(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 = await 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 = await _unitsService.GetLatestUnitLocationAsync(state.UnitId, state.Timestamp); if (location != null) { var eta = await _geoService.GetEtaInSecondsAsync($"{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 = await _geoService.GetEtaInSecondsAsync(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)); }