public async Task <IActionResult> GetUnitsList() { 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); var groups = await _departmentGroupsService.GetAllGroupsForDepartmentAsync(DepartmentId); foreach (var unit in units) { var unitJson = new UnitForListJson(); unitJson.Name = unit.Name; unitJson.Type = unit.Type; unitJson.UnitId = unit.UnitId; if (unit.StationGroupId.HasValue) { var group = groups.FirstOrDefault(x => x.DepartmentGroupId == unit.StationGroupId.Value); if (group != null) { unitJson.Station = group.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); } unitsJson.Add(unitJson); } return(Json(unitsJson)); }
public async Task <IActionResult> GetUnitsList(int linkId) { var link = await _departmentLinksService.GetLinkByIdAsync(linkId); if (link.DepartmentId != DepartmentId && link.LinkedDepartmentId != DepartmentId) { Unauthorized(); } List <UnitForListJson> unitsJson = new List <UnitForListJson>(); var units = await _unitsService.GetUnitsForDepartmentAsync(link.DepartmentId); var states = await _unitsService.GetAllLatestStatusForUnitsByDepartmentIdAsync(link.DepartmentId); var department = await _departmentsService.GetDepartmentByIdAsync(link.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); } unitsJson.Add(unitJson); } return(Json(unitsJson)); }
public async Task <ActionResult <List <UnitViewModel> > > GetUnitStatuses() { var units = await _unitsService.GetUnitsForDepartmentAsync(DepartmentId); var unitStates = await _unitsService.GetAllLatestStatusForUnitsByDepartmentIdAsync(DepartmentId); var unitViewModels = new List <UnitViewModel>(); var sortedUnits = from u in units let station = u.StationGroup let stationName = station == null ? "" : station.Name orderby stationName, u.Name ascending select new { Unit = u, Station = station, StationName = stationName }; foreach (var unit in sortedUnits) { var stateFound = unitStates.FirstOrDefault(x => x.UnitId == unit.Unit.UnitId); var state = "Unknown"; var stateCss = ""; var stateStyle = ""; int? destinationId = 0; decimal?latitude = 0; decimal?longitude = 0; DateTime?timestamp = null; if (stateFound != null) { var customState = await CustomStatesHelper.GetCustomUnitState(stateFound); if (customState != null) { state = customState.ButtonText; stateCss = customState.ButtonColor; stateStyle = customState.ButtonColor; } else { state = stateFound.ToStateDisplayText(); stateCss = stateFound.ToStateCss(); } destinationId = stateFound.DestinationId; latitude = stateFound.Latitude; longitude = stateFound.Longitude; timestamp = stateFound.Timestamp; } int groupId = 0; if (unit.Station != null) { groupId = unit.Station.DepartmentGroupId; } var unitViewModel = new UnitViewModel { Name = unit.Unit.Name, Type = unit.Unit.Type, State = state, StateCss = stateCss, StateStyle = stateStyle, Timestamp = timestamp, DestinationId = destinationId, Latitude = latitude, Longitude = longitude, GroupId = groupId, GroupName = unit.StationName }; unitViewModels.Add(unitViewModel); } return(unitViewModels); }
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)); }