private DeviceInGeofence GetDeviceInGeofence(int deviceId, int geofenceId) { DeviceInGeofence result = this.DevicesInGeofencesRepository .GetByIds(deviceId, geofenceId); if (result == null) { result = new DeviceInGeofence() { DeviceId = deviceId, GeofenceId = geofenceId, IsInGeofence = false }; this.DevicesInGeofencesRepository.Save(result); } return(result); }
private void CheckForNotifications(IEnumerable <Position> positions) { int deviceId = positions.FirstOrDefault().DeviceId; var geofences = this.AlarmRepository.GetAll() .Where(item => item.DeviceId == deviceId) .Select(item => item.Geofence); foreach (var geofence in geofences) { DeviceInGeofence deviceInGeofence = this.GetDeviceInGeofence(deviceId, geofence.Id); foreach (var position in positions) { bool positionInGeofence = this.GeofenceContainsPosition(geofence, position); if (deviceInGeofence.IsInGeofence != positionInGeofence) { deviceInGeofence.IsInGeofence = positionInGeofence; var userDevices = this.UserDeviceRepository.GetAll() .Where(item => item.DeviceId == deviceId && item.ApprovalStatus == ApprovalStatus.Approved); foreach (var userDevice in userDevices) { var notification = new Notification() { Date = DateTime.Now, UserId = userDevice.UserId, Message = string.Format( positionInGeofence ? "Устройство {0} влезе в зона {1}" : "Устройство {0} излезе от зона {1}", userDevice.Device.Name, geofence.Name) }; this.NotificationRepository.Save(notification); } } } } }