Ejemplo n.º 1
0
        public static bool UpdateTaskFromSchedule(Event @event)
        {
            CrontabSchedule schedule = CrontabSchedule.TryParse(
                @event.EventFrequency.GetCronValue(),
                new CrontabSchedule.ParseOptions {
                IncludingSeconds = true
            });

            if (schedule != null)
            {
                if (crontabSchedule.ContainsKey(@event.Id))
                {
                    crontabSchedule[@event.Id] = schedule;
                    crontabNextRun[@event.Id]  = schedule.GetNextOccurrence(DateTime.UtcNow);
                }
                else
                {
                    crontabSchedule.Add(@event.Id, schedule);
                    crontabNextRun.Add(@event.Id, schedule.GetNextOccurrence(DateTime.UtcNow));
                }
            }
            else
            {
                crontabSchedule.Remove(@event.Id);
                crontabNextRun.Remove(@event.Id);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public static bool AddTaskToSchedule(Event @event)
        {
            CrontabSchedule schedule = CrontabSchedule.TryParse(
                @event.EventFrequency.GetCronValue(),
                new CrontabSchedule.ParseOptions {
                IncludingSeconds = true
            });

            if (schedule != null)
            {
                crontabSchedule.Add(@event.Id, schedule);
                crontabNextRun.Add(@event.Id, schedule.GetNextOccurrence(DateTime.UtcNow));
                //crontabNextRun.Add(@event.Id, DateTime.UtcNow.AddSeconds(10));
            }

            return(true);
        }
Ejemplo n.º 3
0
 public static bool RemoveTaskFromSchedule(Event @event)
 {
     return(crontabSchedule.Remove(@event.Id) & crontabNextRun.Remove(@event.Id));
 }
Ejemplo n.º 4
0
        private static async Task SendNotificationToUser(string type, string title, string message, Event @event)
        {
            if (@event.NotificationType != 0)
            {
                await _notifications.PostNotifications(new NotificationDTO
                {
                    Type        = type,
                    Title       = title,
                    Description = message,
                    Origin      = "API",
                    Destination = @event.Username.ToString()
                });
            }

            User user = GraphApiConnector.users.Where(user => user.Id.Equals(@event.Username.ToString())).First();

            if (@event.NotificationType.HasFlag(NotificationType.Email))
            {
                await SendGridAPIConnector.SendEmail(
                    new SendGrid.Helpers.Mail.EmailAddress(user.Identities.Where(identity => identity.SignInType.Equals("emailAddress")).FirstOrDefault().IssuerAssignedId, user.DisplayName),
                    title,
                    message,
                    message
                    );
            }

            if (@event.NotificationType.HasFlag(NotificationType.PopUp))
            {
                await _notificationsHub.Clients.User(@event.Username.ToString()).SendAsync("Notification", type, title, "API", message);
            }

            if (@event.NotificationType.HasFlag(NotificationType.Telegram))
            {
                await TelegramAPIConnector.SendMessageToChat(user.AdditionalData["extension_257f2369f5054b62a0f21c2c82ad96fc_TelegramChatId"].ToString(), message);
            }
        }
Ejemplo n.º 5
0
        public static async Task Process(Event @event)
        {
            if (@event.OnHold)
            {
                return;
            }

            string title;
            string message;

            string  thingId;
            Gateway gateway;
            long    locationId;

            IEnumerable <Measure> measures;

            long recentMeasuresSum, oldMeasuresSum, olderMeasuresSum, delta;

            switch (@event.EventType)
            {
            case Models.EventType.Threshold:
                thingId    = "EMSfIIoT:" + @event.Variable.Split(":").First();
                gateway    = BoschIoTSuiteApiConnector.gateways.Where(gateway => gateway.thingId.Equals(thingId)).First();
                locationId = gateway.attributes.configuration.devices.Where(device => device.deviceId.Equals(@event.Variable)).First().locationId;

                MeasuresApiConnector.GetDeviceLastMeasures(locationId, out measures);
                recentMeasuresSum = measures.Sum(measure => measure.Value);

                MeasuresApiConnector.GetDeviceLastMeasures(locationId, out measures, 1);
                oldMeasuresSum = measures.Sum(measure => measure.Value);

                delta = recentMeasuresSum - oldMeasuresSum;

                title = "Device threshold";

                switch (@event.EventValueType)
                {
                case EventValueType.EqualTo:
                    if (delta == @event.EventValue)
                    {
                        message = $"Your device {@event.Variable} read equaled the defined threshold {@event.EventValue}";
                        break;
                    }
                    else
                    {
                        return;
                    }

                case EventValueType.GreaterThan:
                    if (delta > @event.EventValue)
                    {
                        message = $"Your device {@event.Variable} read surpassed the defined threshold {@event.EventValue}";
                        break;
                    }
                    else
                    {
                        return;
                    }

                case EventValueType.GreaterThanOrEqualTo:
                    if (delta >= @event.EventValue)
                    {
                        message = $"Your device {@event.Variable} read surpassed or equaled the defined threshold {@event.EventValue}";
                        break;
                    }
                    else
                    {
                        return;
                    }

                case EventValueType.LessThan:
                    if (delta < @event.EventValue)
                    {
                        message = $"Your device {@event.Variable} read falled behind the defined threshold {@event.EventValue}";
                        break;
                    }
                    else
                    {
                        return;
                    }

                case EventValueType.LessThanOrEqualTo:
                    if (delta <= @event.EventValue)
                    {
                        message = $"Your device {@event.Variable} read falled behind or equaled the defined threshold {@event.EventValue}";
                        break;
                    }
                    else
                    {
                        return;
                    }

                case EventValueType.NotEqualTo:
                    if (delta != @event.EventValue)
                    {
                        message = $"Your device {@event.Variable} read differed the the defined threshold {@event.EventValue}";
                        break;
                    }
                    else
                    {
                        return;
                    }

                default:
                    return;
                }
                break;

            case Models.EventType.Inactive:
                gateway    = BoschIoTSuiteApiConnector.gateways.Where(gateway => gateway.thingId.Equals(@event.Variable)).First();
                locationId = gateway.attributes.configuration.devices.First().locationId;

                MeasuresApiConnector.GetDeviceLastMeasures(locationId, out var lastRead);
                DateTime lastMeasureDate = lastRead.First().TimeStamp;

                if (lastMeasureDate.AddMinutes(@event.EventValue) < DateTime.UtcNow)
                {
                    message = $"Your gateway {@event.Variable} last communication was {Math.Floor(DateTime.UtcNow.Subtract(lastMeasureDate).TotalMinutes)} minutes ago";
                    title   = "Gateway inactive";
                    break;
                }
                else
                {
                    return;
                }

            case Models.EventType.Algorithm:
                thingId    = "EMSfIIoT:" + @event.Variable.Split(":").First();
                gateway    = BoschIoTSuiteApiConnector.gateways.Where(gateway => gateway.thingId.Equals(thingId)).First();
                locationId = gateway.attributes.configuration.devices.Where(device => device.deviceId.Equals(@event.Variable)).First().locationId;

                double percentage;
                double multiplier;
                string timeSpan;

                switch (@event.EventFrequency)
                {
                case EventFrequency.Every15Minutes:
                    multiplier = 1 / 4.0;
                    timeSpan   = "15 minutes";
                    break;

                case EventFrequency.Every30Minutes:
                    multiplier = 1 / 2.0;
                    timeSpan   = "30 minutes";
                    break;

                case EventFrequency.EveryHour:
                    multiplier = 1;
                    timeSpan   = "hour";
                    break;

                case EventFrequency.Every6Hours:
                    multiplier = 6;
                    timeSpan   = "6 hours";
                    break;

                case EventFrequency.EveryDay:
                    multiplier = 24;
                    timeSpan   = "day";
                    break;

                case EventFrequency.EveryWeek:
                    multiplier = 24 * 7;
                    timeSpan   = "week";
                    break;

                default:
                    return;
                }

                MeasuresApiConnector.GetDeviceLastMeasures(locationId, out measures);
                recentMeasuresSum = measures.Sum(measure => measure.Value);

                MeasuresApiConnector.GetDeviceMeasures(locationId, out measures, Convert.ToInt32(multiplier * 60));

                MeasuresApiConnector.GetDeviceLastMeasures(locationId, out measures, measures.Count());
                oldMeasuresSum = measures.Sum(measure => measure.Value);

                MeasuresApiConnector.GetDeviceMeasures(locationId, out measures, Convert.ToInt32(2 * multiplier * 60));

                MeasuresApiConnector.GetDeviceLastMeasures(locationId, out measures, measures.Count());
                olderMeasuresSum = measures.Sum(measure => measure.Value);

                if (oldMeasuresSum != olderMeasuresSum)
                {
                    percentage = Convert.ToDouble(recentMeasuresSum - oldMeasuresSum) / Convert.ToDouble(oldMeasuresSum - olderMeasuresSum);
                }
                else
                {
                    percentage = double.PositiveInfinity;
                }

                if (Math.Abs(percentage) > @event.EventValue)
                {
                    message = $"Your device {@event.Variable} read from the last {timeSpan} differed the previous one more than the defined threshold of {@event.EventValue}%. Difference was {percentage}%.";
                }
                else
                {
                    return;
                }

                title = "Device threshold";

                break;

            default:
                return;
            }
            await SendNotificationToUser("APINotification", title, message, @event);


            await _events.PutEventOnHold(@event.Id);
        }