Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
0
        public async Task UpdateLocation(DeviceUpdateLocationModel device)
        {
            try
            {
                var key = KeyPrefix + Context.ConnectionId;

                if (await _rediscache.KeyExistsAsync(key))
                {
                    string udid = await _rediscache.StringGetAsync(key);

                    _logger.LogInformation($"Start UpdateLocation {Context.ConnectionId}, udid: {udid}", Context.ConnectionId, udid);

                    var deviceFromDb = await _deviceService.GetDeviceByUdidAsync(udid);

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

                    bool calcIsLocation = CalculateDistanceForDevice.DeviceIsInLocation(
                        deviceFromDb.Latitude,
                        deviceFromDb.Longitude,
                        device.Lat,
                        device.Long,
                        deviceFromDb.Radius);

                    _logger.LogInformation($"location changed devHub: calc {calcIsLocation.ToString()}, lastcurr: " +
                                           $"{lastHistoryByDevice.CurrentDeviceLocationLatitude} {lastHistoryByDevice.CurrentDeviceLocationLongitude} newCoord" +
                                           $"{device.Lat} {device.Long}");

                    if ((calcIsLocation != lastHistoryByDevice.IsInLocation) || (!calcIsLocation))
                    {
                        await AddAndSendDeviceHistory(udid, new DeviceHistory()
                        {
                            CreatedOn = DateTime.UtcNow,
                            IsOnline  = true,
                            CurrentDeviceLocationLatitude  = device.Lat,
                            CurrentDeviceLocationLongitude = device.Long,
                            DeviceLocationLatitude         = deviceFromDb.Latitude,
                            DeviceLocationLongitude        = deviceFromDb.Longitude,
                            DeviceRadius   = deviceFromDb.Radius,
                            LoggedInUserId = lastHistoryByDevice?.LoggedInUserId ?? null,
                            DeviceId       = deviceFromDb.Id,
                            CompanyId      = deviceFromDb.CompanyId,
                            IsInLocation   = calcIsLocation
                        });

                        await _notificationSenderExtention.NotificationForChangeLocation(udid, calcIsLocation);
                    }

                    _logger.LogInformation($"Finish UpdateLocation {Context.ConnectionId}, udid: {udid}", Context.ConnectionId, udid);
                }
                else
                {
                    _logger.LogWarning("Device not found in UpdateLocation");
                }
            }
            catch (RedisConnectionException exm)
            {
                RedisConnection.ForceReconnect();
                _logger.LogError($"Error RedisConnectionException ContextID: {Context.ConnectionId} EXC: {exm.Message} {exm.StackTrace} {exm.Source}");
                return;
            }
            catch (ObjectDisposedException ex)
            {
                RedisConnection.ForceReconnect();
                _logger.LogCritical("Cannot force reconnect to redis cache!!", ex);
            }
        }