示例#1
0
        public async Task ResendFailedNotifications()
        {
            List <Notification> failedNotifications = await _storage.GetFailedNotifications();

            if (!failedNotifications.Any())
            {
                return;
            }

            List <NotificationToKommune> notificationsToKommune = DetectionSystemServiceUtil.NotificationsToKommuneNotifications(failedNotifications);

            try
            {
                await _kommuneService.SendNotifications(notificationsToKommune);

                await _storage.SetNotificationStatuses(notificationsToKommune.Select(x => x.NotificationId), true);
            }
            catch (KommuneCommunicationException e)
            {
                _logger.LogWarning("Kommune communication exception", e);
            }
        }
示例#2
0
        public async Task SendAndSaveNotifications(IEnumerable <UplinkMessage> uplinkMessageEnumerable)
        {
            List <UplinkMessage> uplinkMessages = uplinkMessageEnumerable.ToList();
            List <Notification>  notifications  = uplinkMessages.Select(async uplinkMessage =>
            {
                NotificationType notificationType;
                ObjectDetectionNotification objectDetectionNotification = null;
                if (uplinkMessage.Data.Length == 0)
                {
                    notificationType = NotificationType.Heartbeat;
                }
                else
                {
                    notificationType                = NotificationType.ObjectDetection;
                    ushort?widthCentimeters         = ushort.Parse(uplinkMessage.Data, NumberStyles.HexNumber);
                    ObjectDetection objectDetection = widthCentimeters switch
                    {
                        0 => ObjectDetection.Removed,
                        ushort.MaxValue => ObjectDetection.Detected,
                        _ => ObjectDetection.DetectedWithSize
                    };
                    if (objectDetection != ObjectDetection.DetectedWithSize)
                    {
                        widthCentimeters = null;
                    }

                    objectDetectionNotification = new ObjectDetectionNotification
                    {
                        ObjectDetection  = objectDetection,
                        WidthCentimeters = widthCentimeters,
                        SentToKommune    = false
                    };
                }

                string address = (await _storage.GetDevice(uplinkMessage.DeviceEui))?.Address ?? "";
                return(new Notification
                {
                    Address = address,
                    Timestamp = uplinkMessage.Timestamp,
                    Type = notificationType,
                    DeviceEui = uplinkMessage.DeviceEui,
                    ObjectDetectionNotification = objectDetectionNotification
                });
            })
                                                  .Select(task => task.Result)
                                                  .ToList();

            _logger.LogInformation("Saving notifications: " + JsonSerializer.Serialize(notifications));
            await _storage.AddNotifications(notifications);


            List <NotificationToKommune> notificationsToKommune = DetectionSystemServiceUtil.NotificationsToKommuneNotifications(notifications);

            try
            {
                await _kommuneService.SendNotifications(notificationsToKommune);

                foreach (Notification notification in notifications)
                {
                    notification.ObjectDetectionNotification.SentToKommune = true;
                }
            }
            catch (Exception e)
            {
                _logger.LogWarning("Kommune communication exception", e);
                foreach (Notification notification in notifications)
                {
                    notification.ObjectDetectionNotification.SentToKommune = false;
                }
            }
        }