Example #1
0
        private static List <GenericSensorTarget> GetTargetFromName(string response, string tableName)
        {
            var checkboxes = CreateFromCheckbox(response, tableName, o => new GenericSensorTarget(o));

            if (checkboxes.Count > 0)
            {
                return(checkboxes);
            }

            var options = CreateFromDropDownOptions(response, tableName, o => new GenericSensorTarget(o));

            if (options.Count > 0)
            {
                return(options);
            }

            //todo: we should filter out the common items, and then dont display that item type description entirely if there are none
            //if there are none of EITHER, fall back and just list all of them (including hidden)

            var allCheckboxes = ObjectSettings.GetInput(response).Where(i => i.Type == InputType.Checkbox).ToList();
            var allDropdowns  = ObjectSettings.GetDropDownList(response).ToList();

            if (allCheckboxes.FirstOrDefault(c => c.Name == tableName) == null &&
                allDropdowns.FirstOrDefault(d => d.Name == tableName) == null)
            {
                throw GetInvalidNameException(allCheckboxes, allDropdowns, tableName);
            }

            return(new List <GenericSensorTarget>());
        }
Example #2
0
        /// <summary>
        /// Retrieves a list of sensor targets from a list of options on a specified dropdown list.
        /// </summary>
        /// <param name="response">The raw HTML response to parse.</param>
        /// <param name="name">The name of the dropdown list to parse.</param>
        /// <param name="createObj">A function used to construct a sensor target from the target's raw value.</param>
        /// <returns>A list of sensor targets of type T</returns>
        protected static List <T> CreateFromDropDownOptions(string response, string name, Func <string, T> createObj)
        {
            var files = ObjectSettings.GetDropDownList(response)
                        .Where(d => d.Name == name)
                        .SelectMany(d => d.Options
                                    .Select(o => createObj(o.Value)))
                        .ToList();

            return(files);
        }
Example #3
0
        internal static Dictionary <string, List <GenericSensorTarget> > GetAllTargets(string response)
        {
            var checkboxes = ObjectSettings.GetInput(response).Where(i => i.Type == InputType.Checkbox).ToList();
            var dropdowns  = ObjectSettings.GetDropDownList(response).ToList();

            var validCheckboxes = GetValidCheckboxes(checkboxes, false, false);
            var validDropdowns  = GetValidDropdowns(dropdowns, false, false);

            var checkboxDict = validCheckboxes.ToDictionary(c => c, c => CreateFromCheckbox(response, c, o => new GenericSensorTarget(o)));
            var boxDict      = validDropdowns.ToDictionary(c => c, d => CreateFromDropDownOptions(response, d, o => new GenericSensorTarget(o)));

            foreach (var d in boxDict)
            {
                checkboxDict.Add(d.Key, d.Value);
            }

            return(checkboxDict);
        }
Example #4
0
        private static List <GenericSensorTarget> GetTargetFromUnknown(string response)
        {
            var checkboxes = ObjectSettings.GetInput(response).Where(i => i.Type == InputType.Checkbox).ToList();
            var dropdowns  = ObjectSettings.GetDropDownList(response).ToList();

            var validCheckboxes = GetValidCheckboxes(checkboxes, false, false);
            var validDropdowns  = GetValidDropdowns(dropdowns, false, false);

            var all = new List <string>();

            all.AddRange(validCheckboxes);
            all.AddRange(validDropdowns);

            if (all.Count == 0)
            {
                throw new ArgumentException("Cannot guess sensor target table. Please specify tableName");
            }

            if (all.Count > 2)
            {
                throw new ArgumentException($"Cannot guess sensor target table: multiple tables found. Available tables: {string.Join(", ", all)}");
            }

            var tableName = all.First();

            if (validCheckboxes.Count == 1)
            {
                return(CreateFromCheckbox(response, tableName, o => new GenericSensorTarget(o)));
            }
            else if (validDropdowns.Count == 1)
            {
                return(CreateFromDropDownOptions(response, tableName, o => new GenericSensorTarget(o)));
            }
            else
            {
                throw new NotImplementedException("Table type was unhandled");
            }
        }
Example #5
0
        private void ParseResponse(string response)
        {
            var nameRegex = "(.+?name=\")(.+?_*)(\".+)";
            var inputs    = ObjectSettings.GetFilteredInputs(response, nameRegex).Where(n => n.Name != "tmpid" && n.Name != "id" && n.Name != "sensortype" && n.Name != "parenttags_").ToList();
            var lists     = ObjectSettings.GetDropDownList(response, nameRegex);
            var text      = ObjectSettings.GetTextAreaFields(response, nameRegex);

            Targets = GenericSensorTarget.GetAllTargets(response).ToDictionary(t => t.Key, t => t.Value.ToArray());

            foreach (var input in inputs)
            {
                AddCustom(input.Name, CleanValue(input.Value));
            }

            foreach (var list in lists)
            {
                AddCustom(list.Name, CleanValue(list.Options.FirstOrDefault(o => o.Selected)?.Value)); //todo: does this crash if its null?
            }
            foreach (var t in text)
            {
                Add(t.Key, CleanValue(t.Value));
            }
        }