public async Task <IActionResult> EditUnit(int unitId) { var model = new NewUnitView(); model.Unit = await _unitsService.GetUnitByIdAsync(unitId); if (!await _authorizationService.CanUserModifyUnitAsync(UserId, unitId)) { Unauthorized(); } model.Types = await _unitsService.GetUnitTypesForDepartmentAsync(DepartmentId); var groups = new List <DepartmentGroup>(); groups.Add(new DepartmentGroup { Name = "No Station" }); groups.AddRange(await _departmentGroupsService.GetAllStationGroupsForDepartmentAsync(DepartmentId)); model.Stations = groups; model.UnitRoles = await _unitsService.GetRolesForUnitAsync(unitId); return(View(model)); }
public async Task <bool> CanUserModifyUnitAsync(string userId, int unitId) { var department = await _departmentsService.GetDepartmentByUserIdAsync(userId); var unit = await _unitsService.GetUnitByIdAsync(unitId); if (department == null || unit == null) { return(false); } if (unit.DepartmentId != department.DepartmentId) { return(false); } if (department.IsUserAnAdmin(userId)) { return(true); } return(false); }
private async Task <ActionResult> ProcessSetUnitState(UnitStateInput stateInput) { var unit = await _unitsService.GetUnitByIdAsync(stateInput.Uid); if (unit == null) { return(NotFound()); } if (unit.DepartmentId != DepartmentId) { return(Unauthorized()); } if (this.ModelState.IsValid) { try { var state = new UnitState(); state.UnitId = stateInput.Uid; state.LocalTimestamp = stateInput.Lts; if (!String.IsNullOrWhiteSpace(stateInput.Lat)) { state.Latitude = decimal.Parse(stateInput.Lat); } if (!String.IsNullOrWhiteSpace(stateInput.Lon)) { state.Longitude = decimal.Parse(stateInput.Lon); } if (!String.IsNullOrWhiteSpace(stateInput.Acc)) { state.Accuracy = decimal.Parse(stateInput.Acc); } if (!String.IsNullOrWhiteSpace(stateInput.Alt)) { state.Altitude = decimal.Parse(stateInput.Alt); } if (!String.IsNullOrWhiteSpace(stateInput.Alc)) { state.AltitudeAccuracy = decimal.Parse(stateInput.Alc); } if (!String.IsNullOrWhiteSpace(stateInput.Spd)) { state.Speed = decimal.Parse(stateInput.Spd); } if (!String.IsNullOrWhiteSpace(stateInput.Hdn)) { state.Heading = decimal.Parse(stateInput.Hdn); } state.State = (int)stateInput.Typ; state.Timestamp = stateInput.Tms ?? DateTime.UtcNow; state.Note = stateInput.Not; if (state.Latitude.HasValue && state.Longitude.HasValue) { state.GeoLocationData = string.Format("{0},{1}", state.Latitude.Value, state.Longitude.Value); } if (stateInput.Rto > 0) { state.DestinationId = stateInput.Rto; } var savedState = await _unitsService.SetUnitStateAsync(state, DepartmentId); if (stateInput.Roles != null && stateInput.Roles.Count > 0) { var unitRoles = await _unitsService.GetRolesForUnitAsync(savedState.UnitId); var roles = new List <UnitStateRole>(); foreach (var role in stateInput.Roles) { if (!string.IsNullOrWhiteSpace(role.Uid)) { var unitRole = new UnitStateRole(); unitRole.UnitStateId = savedState.UnitStateId; unitRole.UserId = role.Uid;; unitRole.UnitStateRoleId = role.Rid; if (String.IsNullOrWhiteSpace(role.Nme)) { var savedRole = unitRoles.FirstOrDefault(x => x.UnitRoleId == unitRole.UnitStateRoleId); if (savedRole != null) { unitRole.Role = savedRole.Name; } } else { unitRole.Role = role.Nme; } unitRole.IdValue = 0; unitRole.UnitStateRoleId = 0; roles.Add(unitRole); //_unitsService.AddUnitStateRoleForEvent(savedState.UnitStateId, role.Uid, role.Rid, savedState.Unit.Name, savedState.Timestamp); } } await _unitsService.AddAllUnitStateRolesAsync(roles); } //OutboundEventProvider.UnitStatusTopicHandler handler = new OutboundEventProvider.UnitStatusTopicHandler(); //handler.Handle(new UnitStatusEvent() { DepartmentId = DepartmentId, Status = savedState }); _eventAggregator.SendMessage <UnitStatusEvent>(new UnitStatusEvent() { DepartmentId = DepartmentId, Status = savedState }); if (savedState.UnitStateId > 0) { return(CreatedAtAction("SetUnitState", new { id = savedState.UnitStateId }, savedState)); } } catch (Exception ex) { Logging.LogException(ex); return(BadRequest()); } } return(BadRequest()); }
public static async Task <bool> ProcessCallQueueItem(CallQueueItem cqi) { try { if (cqi != null && cqi.Call != null && cqi.Call.HasAnyDispatches()) { if (_communicationService == null) { _communicationService = Bootstrapper.GetKernel().Resolve <ICommunicationService>(); } if (_callsService == null) { _callsService = Bootstrapper.GetKernel().Resolve <ICallsService>(); } List <int> groupIds = new List <int>(); /* Trying to see if I can eek out a little perf here now that profiles are in Redis. Previously the * the parallel operation would cause EF errors. This shouldn't be the case now because profiles are * cached and GetProfileForUser operations will hit that first. */ if (cqi.Profiles == null || !cqi.Profiles.Any()) { if (_userProfilesService == null) { _userProfilesService = Bootstrapper.GetKernel().Resolve <IUserProfileService>(); } cqi.Profiles = (await _userProfilesService.GetAllProfilesForDepartmentAsync(cqi.Call.DepartmentId)).Select(x => x.Value).ToList(); } if (cqi.CallDispatchAttachmentId > 0) { //var callsService = Bootstrapper.GetKernel().Resolve<ICallsService>(); cqi.Call.ShortenedAudioUrl = await _callsService.GetShortenedAudioUrlAsync(cqi.Call.CallId, cqi.CallDispatchAttachmentId); } cqi.Call.ShortenedCallUrl = await _callsService.GetShortenedCallLinkUrl(cqi.Call.CallId); try { cqi.Call.CallPriority = await _callsService.GetCallPrioritiesByIdAsync(cqi.Call.DepartmentId, cqi.Call.Priority, false); } catch { /* Doesn't matter */ } var dispatchedUsers = new HashSet <string>(); // Dispatch Personnel if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any()) { Parallel.ForEach(cqi.Call.Dispatches, d => { dispatchedUsers.Add(d.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == d.UserId); if (profile != null) { _communicationService.SendCallAsync(cqi.Call, d, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } } catch (SocketException sex) { } }); } if (_departmentGroupsService == null) { _departmentGroupsService = Bootstrapper.GetKernel().Resolve <IDepartmentGroupsService>(); } // Dispatch Groups if (cqi.Call.GroupDispatches != null && cqi.Call.GroupDispatches.Any()) { foreach (var d in cqi.Call.GroupDispatches) { if (!groupIds.Contains(d.DepartmentGroupId)) { groupIds.Add(d.DepartmentGroupId); } var members = await _departmentGroupsService.GetAllMembersForGroupAsync(d.DepartmentGroupId); foreach (var member in members) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); await _communicationService.SendCallAsync(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } } // Dispatch Units if (cqi.Call.UnitDispatches != null && cqi.Call.UnitDispatches.Any()) { if (_unitsService == null) { _unitsService = Bootstrapper.GetKernel().Resolve <IUnitsService>(); } foreach (var d in cqi.Call.UnitDispatches) { var unit = await _unitsService.GetUnitByIdAsync(d.UnitId); if (unit != null && unit.StationGroupId.HasValue) { if (!groupIds.Contains(unit.StationGroupId.Value)) { groupIds.Add(unit.StationGroupId.Value); } } await _communicationService.SendUnitCallAsync(cqi.Call, d, cqi.DepartmentTextNumber, cqi.Address); var unitAssignedMembers = await _unitsService.GetCurrentRolesForUnitAsync(d.UnitId); if (unitAssignedMembers != null && unitAssignedMembers.Count() > 0) { foreach (var member in unitAssignedMembers) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); await _communicationService.SendCallAsync(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } else { if (unit.StationGroupId.HasValue) { var members = await _departmentGroupsService.GetAllMembersForGroupAsync(unit.StationGroupId.Value); foreach (var member in members) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); await _communicationService.SendCallAsync(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } } } } // Dispatch Roles if (cqi.Call.RoleDispatches != null && cqi.Call.RoleDispatches.Any()) { if (_rolesService == null) { _rolesService = Bootstrapper.GetKernel().Resolve <IPersonnelRolesService>(); } foreach (var d in cqi.Call.RoleDispatches) { var members = await _rolesService.GetAllMembersOfRoleAsync(d.RoleId); foreach (var member in members) { if (!dispatchedUsers.Contains(member.UserId)) { dispatchedUsers.Add(member.UserId); try { var profile = cqi.Profiles.FirstOrDefault(x => x.UserId == member.UserId); await _communicationService.SendCallAsync(cqi.Call, new CallDispatch() { UserId = member.UserId }, cqi.DepartmentTextNumber, cqi.Call.DepartmentId, profile, cqi.Address); } catch (SocketException sex) { } catch (Exception ex) { Logging.LogException(ex); } } } } } // Send Call Print to Printer if (_printerProvider == null) { _printerProvider = Bootstrapper.GetKernel().Resolve <IPrinterProvider>(); } Dictionary <int, DepartmentGroup> fetchedGroups = new Dictionary <int, DepartmentGroup>(); if (cqi.Call.Dispatches != null && cqi.Call.Dispatches.Any()) { foreach (var d in cqi.Call.Dispatches) { var group = await _departmentGroupsService.GetGroupForUserAsync(d.UserId, cqi.Call.DepartmentId); if (group != null) { if (!groupIds.Contains(group.DepartmentGroupId)) { groupIds.Add(group.DepartmentGroupId); } if (!fetchedGroups.ContainsKey(group.DepartmentGroupId)) { fetchedGroups.Add(group.DepartmentGroupId, group); } } } } foreach (var groupId in groupIds) { try { DepartmentGroup group = null; if (fetchedGroups.ContainsKey(groupId)) { group = fetchedGroups[groupId]; } else { group = await _departmentGroupsService.GetGroupByIdAsync(groupId); } if (!String.IsNullOrWhiteSpace(group.PrinterData) && group.DispatchToPrinter) { var printerData = JsonConvert.DeserializeObject <DepartmentGroupPrinter>(group.PrinterData); var apiKey = SymmetricEncryption.Decrypt(printerData.ApiKey, Config.SystemBehaviorConfig.ExternalLinkUrlParamPassphrase); var callUrl = await _callsService.GetShortenedCallPdfUrl(cqi.Call.CallId, true, groupId); var printJob = _printerProvider.SubmitPrintJob(apiKey, printerData.PrinterId, "CallPrint", callUrl); } } catch (Exception ex) { Logging.LogException(ex); } } } } finally { _communicationService = null; } return(true); }
public async Task <ActionResult> SetUnitLocation(UnitLocationInput locationInput, CancellationToken cancellationToken) { var unit = await _unitsService.GetUnitByIdAsync(locationInput.Uid); if (unit == null) { return(NotFound()); } if (unit.DepartmentId != DepartmentId) { return(Unauthorized()); } if (this.ModelState.IsValid) { try { CqrsEvent locationEvent = new CqrsEvent(); UnitLocation location = new UnitLocation(); location.UnitId = locationInput.Uid; if (locationInput.Tms.HasValue) { location.Timestamp = locationInput.Tms.Value; } else { location.Timestamp = DateTime.UtcNow; } if (!String.IsNullOrWhiteSpace(locationInput.Lat) && locationInput.Lat != "NaN" && !String.IsNullOrWhiteSpace(locationInput.Lon) && locationInput.Lon != "NaN") { location.Latitude = decimal.Parse(locationInput.Lat); location.Longitude = decimal.Parse(locationInput.Lon); if (!String.IsNullOrWhiteSpace(locationInput.Acc) && locationInput.Acc != "NaN") { location.Accuracy = decimal.Parse(locationInput.Acc); } if (!String.IsNullOrWhiteSpace(locationInput.Alt) && locationInput.Alt != "NaN") { location.Altitude = decimal.Parse(locationInput.Alt); } if (!String.IsNullOrWhiteSpace(locationInput.Alc) && locationInput.Alc != "NaN") { location.AltitudeAccuracy = decimal.Parse(locationInput.Alc); } if (!String.IsNullOrWhiteSpace(locationInput.Spd) && locationInput.Spd != "NaN") { location.Speed = decimal.Parse(locationInput.Spd); } if (!String.IsNullOrWhiteSpace(locationInput.Hdn) && locationInput.Hdn != "NaN") { location.Heading = decimal.Parse(locationInput.Hdn); } locationEvent.Type = (int)CqrsEventTypes.UnitLocation; locationEvent.Data = ObjectSerialization.Serialize(location); await _cqrsProvider.EnqueueCqrsEventAsync(locationEvent); return(CreatedAtAction(nameof(SetUnitLocation), new { id = locationInput.Uid }, locationEvent)); } else { return(Ok()); } } catch (Exception ex) { Logging.LogException(ex); return(BadRequest()); } } return(BadRequest()); }
public async Task <ActionResult <GetSetUnitStateDataResult> > GetSetUnitStatusData(int unitId) { var results = new GetSetUnitStateDataResult(); results.UnitId = unitId; results.Stations = new List <GroupInfoResult>(); results.Calls = new List <CallResult>(); results.Statuses = new List <CustomStatusesResult>(); var unit = await _unitsService.GetUnitByIdAsync(unitId); results.UnitName = unit.Name; var type = await _unitsService.GetUnitTypeByNameAsync(DepartmentId, unit.Type); var activeCalls = await _callsService.GetActiveCallsByDepartmentAsync(DepartmentId); var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId); var stations = await _departmentGroupsService.GetAllStationGroupsForDepartmentAsync(DepartmentId); var callDefault = new CallResult(); callDefault.Cid = 0; callDefault.Nme = "No Call"; results.Calls.Add(callDefault); if (activeCalls != null) { foreach (var c in activeCalls) { var call = new CallResult(); call.Cid = c.CallId; call.Pri = c.Priority; call.Ctl = c.IsCritical; call.Nme = StringHelpers.SanitizeHtmlInString(c.Name); if (!String.IsNullOrWhiteSpace(c.NatureOfCall)) { call.Noc = StringHelpers.SanitizeHtmlInString(c.NatureOfCall); } call.Map = c.MapPage; if (!String.IsNullOrWhiteSpace(c.Notes)) { call.Not = StringHelpers.SanitizeHtmlInString(c.Notes); } if (c.CallNotes != null) { call.Nts = c.CallNotes.Count(); } else { call.Nts = 0; } if (c.Attachments != null) { call.Aud = c.Attachments.Count(x => x.CallAttachmentType == (int)CallAttachmentTypes.DispatchAudio); call.Img = c.Attachments.Count(x => x.CallAttachmentType == (int)CallAttachmentTypes.Image); call.Fls = c.Attachments.Count(x => x.CallAttachmentType == (int)CallAttachmentTypes.File); } else { call.Aud = 0; call.Img = 0; call.Fls = 0; } if (String.IsNullOrWhiteSpace(c.Address) && !String.IsNullOrWhiteSpace(c.GeoLocationData)) { var geo = c.GeoLocationData.Split(char.Parse(",")); if (geo.Length == 2) { call.Add = await _geoLocationProvider.GetAddressFromLatLong(double.Parse(geo[0]), double.Parse(geo[1])); } } else { call.Add = c.Address; } call.Geo = c.GeoLocationData; call.Lon = c.LoggedOn.TimeConverter(department); call.Utc = c.LoggedOn; call.Ste = c.State; call.Num = c.Number; results.Calls.Add(call); } } var groupInfoDefault = new GroupInfoResult(); groupInfoDefault.Gid = 0; groupInfoDefault.Nme = "No Station"; results.Stations.Add(groupInfoDefault); if (stations != null) { foreach (var group in stations) { var groupInfo = new GroupInfoResult(); groupInfo.Gid = group.DepartmentGroupId; groupInfo.Nme = group.Name; if (group.Type.HasValue) { groupInfo.Typ = group.Type.Value; } if (group.Address != null) { groupInfo.Add = group.Address.FormatAddress(); } results.Stations.Add(groupInfo); } } if (type != null && type.CustomStatesId.HasValue) { var customStates = await _customStateService.GetCustomSateByIdAsync(type.CustomStatesId.Value); if (!customStates.IsDeleted) { foreach (var stateDetail in customStates.GetActiveDetails()) { if (stateDetail.IsDeleted) { continue; } var customStateResult = new CustomStatusesResult(); customStateResult.Id = stateDetail.CustomStateDetailId; customStateResult.Type = customStates.Type; customStateResult.StateId = stateDetail.CustomStateId; customStateResult.Text = stateDetail.ButtonText; customStateResult.BColor = stateDetail.ButtonColor; customStateResult.Color = stateDetail.TextColor; customStateResult.Gps = stateDetail.GpsRequired; customStateResult.Note = stateDetail.NoteType; customStateResult.Detail = stateDetail.DetailType; results.Statuses.Add(customStateResult); } } } else { var customStateResult = new CustomStatusesResult(); customStateResult.Id = 0; customStateResult.Type = 0; customStateResult.StateId = 0; customStateResult.Text = "Available"; customStateResult.BColor = "#FFFFFF"; customStateResult.Color = "#000000"; customStateResult.Gps = false; customStateResult.Note = 0; customStateResult.Detail = 0; results.Statuses.Add(customStateResult); var customStateResult2 = new CustomStatusesResult(); customStateResult2.Id = 3; customStateResult2.Type = 3; customStateResult2.StateId = 3; customStateResult2.Text = "Committed"; customStateResult2.BColor = "#FFFFFF"; customStateResult2.Color = "#000000"; customStateResult2.Gps = false; customStateResult2.Note = 0; customStateResult2.Detail = 0; results.Statuses.Add(customStateResult2); var customStateResult3 = new CustomStatusesResult(); customStateResult3.Id = 1; customStateResult3.Type = 1; customStateResult3.StateId = 1; customStateResult3.Text = "Delayed"; customStateResult3.BColor = "#FFFFFF"; customStateResult3.Color = "#000000"; customStateResult3.Gps = false; customStateResult3.Note = 0; customStateResult3.Detail = 0; results.Statuses.Add(customStateResult3); var customStateResult4 = new CustomStatusesResult(); customStateResult4.Id = 4; customStateResult4.Type = 4; customStateResult4.StateId = 4; customStateResult4.Text = "Out Of Service"; customStateResult4.BColor = "#FFFFFF"; customStateResult4.Color = "#000000"; customStateResult4.Gps = false; customStateResult4.Note = 0; customStateResult4.Detail = 0; results.Statuses.Add(customStateResult4); var customStateResult5 = new CustomStatusesResult(); customStateResult5.Id = 2; customStateResult5.Type = 2; customStateResult5.StateId = 2; customStateResult5.Text = "Unavailable"; customStateResult5.BColor = "#FFFFFF"; customStateResult5.Color = "#000000"; customStateResult5.Gps = false; customStateResult5.Note = 0; customStateResult5.Detail = 0; results.Statuses.Add(customStateResult5); } return(results); }