Exemple #1
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;
                }
            }
        }