Beispiel #1
0
        private async Task UpdateTriggerActionsAsync(List <NotificationTrigger> triggers, CancellationToken token)
        {
            var actions = ResponseParser.GroupTriggerActions(triggers);

            var parameters = new NotificationActionParameters(actions.Select(a => a.Key).ToArray());

            var tasks  = actions.Select(g => GetNotificationActionPropertiesAsync(g.Key, token));
            var normal = await ObjectEngine.GetObjectsXmlAsync(parameters, token : token).ConfigureAwait(false);

            //All the properties of all desired notifications
            var results = await Task.WhenAll(tasks).ConfigureAwait(false);

            //For each different notification action
            for (int i = 0; i < actions.Count; i++)
            {
                var xDoc = RequestParser.ExtractActionXml(normal, results[i], actions[i].Key);

                //Foreach notification action with the same ID
                foreach (var action in actions[i])
                {
                    action.LazyXml = new Lazy <XDocument>(() => xDoc);
                }
            }

            var list = ResponseParser.GroupActionSchedules(actions.SelectMany(g => g).ToList()).ToList();

            List <Schedule> schedules = new List <Schedule>();

            if (list.Count > 0)
            {
                schedules = await GetSchedulesAsync(Property.Id, list.Select(l => l.Key).ToArray(), token).ConfigureAwait(false);
            }

            foreach (var group in actions)
            {
                foreach (var action in group)
                {
                    if (action.lazyScheduleStr != null)
                    {
                        var id = PrtgObject.GetId(action.lazyScheduleStr);

                        if (id != -1)
                        {
                            action.schedule = new Lazy <Schedule>(() => schedules.First(s => s.Id == id));
                        }
                        else
                        {
                            action.schedule = new Lazy <Schedule>(() => new Schedule(action.lazyScheduleStr));
                        }
                    }
                    else
                    {
                        action.schedule = new Lazy <Schedule>(() => new Schedule(action.lazyScheduleStr));
                    }
                }
            }
        }
        internal async Task <List <NotificationAction> > GetNotificationActionsInternalAsync(NotificationActionParameters parameters)
        {
            var response = await RequestEngine.ExecuteRequestAsync(parameters).ConfigureAwait(false);

            var items = response.Descendants("item").ToList();

            await Task.WhenAll(items.Select(async item =>
            {
                var id = Convert.ToInt32(item.Element("objid").Value);

                var properties = await GetNotificationActionPropertiesAsync(id).ConfigureAwait(false);

                item.Add(properties.Nodes());
            })).ConfigureAwait(false);

            var actions = XmlDeserializer <NotificationAction> .DeserializeList(response).Items;

            var actionsWithSchedules = ResponseParser.GroupActionSchedules(actions).ToList();

            await UpdateActionSchedulesAsync(actionsWithSchedules).ConfigureAwait(false);

            return(actions);
        }
        //######################################
        // GetNotificationActionsInternal
        //######################################

        internal List <NotificationAction> GetNotificationActionsInternal(NotificationActionParameters parameters)
        {
            var response = RequestEngine.ExecuteRequest(parameters);

            var items = response.Descendants("item").ToList();

            foreach (var item in items)
            {
                var id = Convert.ToInt32(item.Element("objid").Value);

                var properties = GetNotificationActionProperties(id);

                item.Add(properties.Nodes());
            }

            var actions = XmlDeserializer <NotificationAction> .DeserializeList(response).Items;

            var actionsWithSchedules = ResponseParser.GroupActionSchedules(actions).ToList();

            UpdateActionSchedules(actionsWithSchedules);

            return(actions);
        }