Exemplo n.º 1
0
        public void AddUnitStateRoleForEvent(int unitStateId, string userId, int roleId, string unitName, DateTime timestamp, string roleName = "Unknown")
        {
            if (unitStateId <= 0)
            {
                throw new ArgumentException("Unit State Id cannot be 0", "unitStateId");
            }

            if (String.IsNullOrWhiteSpace(userId))
            {
                throw new ArgumentException("User Id cannot be an empty Guid", "userId");
            }

            if (roleId <= 0)
            {
                throw new ArgumentException("Role Id cannot be 0", "roleId");
            }

            //var role = GetRoleById(roleId);
            //string roleName = "Unknown";

            //if (role != null)
            //	roleName = role.Name;

            var unitStateRole = new UnitStateRole();

            unitStateRole.UnitStateId = unitStateId;
            unitStateRole.UserId      = userId;
            unitStateRole.Role        = roleName;

            //_userStateService.CreateUserState(userId, (int)UserStateTypes.Committed, string.Format("On {0}", unitName), timestamp);

            _unitStateRoleRepository.SaveOrUpdate(unitStateRole);
        }
Exemplo n.º 2
0
        public async Task <UnitStateRole> AddUnitStateRoleForEventAsync(int unitStateId, string userId, int roleId, string unitName, DateTime timestamp, string roleName = "Unknown", CancellationToken cancellationToken = default(CancellationToken))
        {
            if (unitStateId <= 0)
            {
                throw new ArgumentException("Unit State Id cannot be 0", "unitStateId");
            }

            if (String.IsNullOrWhiteSpace(userId))
            {
                throw new ArgumentException("User Id cannot be an empty Guid", "userId");
            }

            if (roleId <= 0)
            {
                throw new ArgumentException("Role Id cannot be 0", "roleId");
            }

            var unitStateRole = new UnitStateRole();

            unitStateRole.UnitStateId = unitStateId;
            unitStateRole.UserId      = userId;
            unitStateRole.Role        = roleName;

            return(await _unitStateRoleRepository.SaveOrUpdateAsync(unitStateRole, cancellationToken));
        }
Exemplo n.º 3
0
        public async Task <UnitState> SetUnitStateAsync(UnitState state, int departmentId, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (state.Accuracy > 100000)
            {
                state.Accuracy = 100000;
            }

            if (state.Roles == null || !state.Roles.Any())
            {
                var activeRoles = await GetActiveRolesForUnitAsync(state.UnitId);

                if (activeRoles != null && activeRoles.Any())
                {
                    state.Roles = new List <UnitStateRole>();
                    foreach (var activeRole in activeRoles)
                    {
                        UnitStateRole role = new UnitStateRole();
                        role.Role   = activeRole.Role;
                        role.UserId = activeRole.UserId;

                        state.Roles.Add(role);
                    }
                }
            }

            var saved = await _unitStatesRepository.SaveOrUpdateAsync(state, cancellationToken);

            _eventAggregator.SendMessage <UnitStatusEvent>(new UnitStatusEvent {
                DepartmentId = departmentId, Status = saved
            });

            return(state);
        }
Exemplo n.º 4
0
        public async Task <UnitState> SetUnitStateAsync(int unitId, int unitStateType, int departmentId, CancellationToken cancellationToken = default(CancellationToken))
        {
            var state = new UnitState();

            state.UnitId    = unitId;
            state.State     = unitStateType;
            state.Timestamp = DateTime.UtcNow;

            var activeRoles = await GetActiveRolesForUnitAsync(state.UnitId);

            if (activeRoles != null && activeRoles.Any())
            {
                state.Roles = new List <UnitStateRole>();
                foreach (var activeRole in activeRoles)
                {
                    UnitStateRole role = new UnitStateRole();
                    role.Role   = activeRole.Role;
                    role.UserId = activeRole.UserId;

                    state.Roles.Add(role);
                }
            }

            var saved = await _unitStatesRepository.SaveOrUpdateAsync(state, cancellationToken);

            _eventAggregator.SendMessage <UnitStatusEvent>(new UnitStatusEvent {
                DepartmentId = departmentId, Status = saved
            });

            return(saved);
        }
Exemplo n.º 5
0
        private HttpResponseMessage ProcessSetUnitState(UnitStateInput stateInput)
        {
            var unit = _unitsService.GetUnitById(stateInput.Uid);

            if (unit == null)
            {
                throw HttpStatusCode.NotFound.AsException();
            }

            if (unit.DepartmentId != DepartmentId)
            {
                throw HttpStatusCode.Unauthorized.AsException();
            }

            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 = _unitsService.SetUnitState(state, DepartmentId);

                    if (stateInput.Roles != null && stateInput.Roles.Count > 0)
                    {
                        var unitRoles = _unitsService.GetRolesForUnit(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.Id = 0;
                                unitRole.UnitStateRoleId = 0;

                                roles.Add(unitRole);
                                //_unitsService.AddUnitStateRoleForEvent(savedState.UnitStateId, role.Uid, role.Rid, savedState.Unit.Name, savedState.Timestamp);
                            }
                        }

                        _unitsService.AddAllUnitStateRoles(roles);
                    }

                    OutboundEventProvider.UnitStatusTopicHandler handler = new OutboundEventProvider.UnitStatusTopicHandler();
                    handler.Handle(new UnitStatusEvent()
                    {
                        DepartmentId = DepartmentId, Status = savedState
                    });

                    if (savedState.UnitStateId > 0)
                    {
                        return(Request.CreateResponse(HttpStatusCode.Created));
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogException(ex);
                    throw HttpStatusCode.InternalServerError.AsException();
                }
            }

            throw HttpStatusCode.BadRequest.AsException();
        }
Exemplo n.º 6
0
        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());
        }