Exemplo n.º 1
0
        public async Task <IActionResult> Put(Guid id, [FromBody] BaseDeviceRequest request)
        {
            try
            {
                await _deviceService.Update(request, id, Token);

                return(NoContent());
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (ApplicationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (KeyNotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Device PUT Error: {ex.Message} : {ex.StackTrace} : {ex.Source}", ex.Message, ex.StackTrace, ex.Source);
                return(BadRequest(ex.Message));
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public async Task Update(BaseDeviceRequest request, Guid id, string token)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException(nameof(token));
            }

            var device = await _deviceRepository.GetAsync(id);

            if (device == null)
            {
                throw new KeyNotFoundException($"Device with id: {id} not found");
            }

            var scopeArray       = new [] { Const.Permissions.Device.AdminUpdate };
            var correctCompanyId = await _validationHelper.GetCompanyIdByPermission(token, scopeArray, request.CompanyId);

            if (!correctCompanyId.HasValue)
            {
                throw new ArgumentException("Invalid companyId", nameof(request.CompanyId));
            }
            request.CompanyId = correctCompanyId.Value;

            await _validationHelper.ValidateCompanyAndBranch(request.CompanyId, request.BranchId, token);

            device.CompanyId   = request.CompanyId;
            device.DeviceName  = request.DeviceName;
            device.BranchId    = request.BranchId;
            device.Latitude    = request.Latitude;
            device.Longitude   = request.Longitude;
            device.Radius      = request.Radius;
            device.HexnodeUdid = request.HexnodeUdid;
            device.Phone       = request.Phone;
            device.IsPending   = false;

            await _deviceRepository.UpdateAsync(device);

            await AddHistoryIfLocationChanged(request, device.Id);
        }
Exemplo n.º 3
0
        private async Task AddHistoryIfLocationChanged(BaseDeviceRequest request, Guid Id)
        {
            var deviceFromDb = await _deviceRepository.GetByAsync(x => x.Id == Id);

            var lastDeviceHistory = (await _deviceHistoryRepository.GetAsync(x => x.DeviceId == deviceFromDb.Id))
                                    .OrderByDescending(y => y.CreatedOn)
                                    .FirstOrDefault();

            if (lastDeviceHistory != null)
            {
                bool calcIsLocation = CalculateDistanceForDevice.DeviceIsInLocation(
                    lastDeviceHistory.CurrentDeviceLocationLatitude,
                    lastDeviceHistory.CurrentDeviceLocationLongitude,
                    request.Latitude,
                    request.Longitude,
                    request.Radius);

                await AddAndSendDeviceHistory(request.UDID, new DeviceHistory()
                {
                    CreatedOn = DateTime.UtcNow,
                    IsOnline  = lastDeviceHistory.IsOnline,
                    CurrentDeviceLocationLatitude  = lastDeviceHistory.CurrentDeviceLocationLatitude,
                    CurrentDeviceLocationLongitude = lastDeviceHistory.CurrentDeviceLocationLongitude,
                    DeviceLocationLatitude         = request.Latitude,
                    DeviceLocationLongitude        = request.Longitude,
                    DeviceRadius   = request.Radius,
                    LoggedInUserId = lastDeviceHistory?.LoggedInUserId ?? null,
                    DeviceId       = Id,
                    CompanyId      = request.CompanyId,
                    IsInLocation   = calcIsLocation
                });

                DeviceUpdateLocationModel device = new DeviceUpdateLocationModel()
                {
                    Lat  = lastDeviceHistory.CurrentDeviceLocationLatitude,
                    Long = lastDeviceHistory.CurrentDeviceLocationLongitude
                };

                _logger.LogError("Before Send Notification in DeviceService");
                await _notificationSenderExtention.NotificationForChangeLocation(request.UDID, calcIsLocation);
            }
        }