Ejemplo n.º 1
0
        /// <summary>
        /// Returns true if the input signal doesn't match with this rule.
        /// </summary>
        /// <param name="input">The input signal.</param>
        /// <returns>
        ///   <c>true</c> if the input signal doesn't match with this rule; otherwise, <c>false</c>.
        /// </returns>
        public bool IsValid(SignalData input)
        {
            bool result = true;

            if (input != null)
            {
                if (Signal == input.Signal && ValueType.ToLowerInvariant() == input.ValueType.ToLowerInvariant())// && Value.GetType() == input.Value.GetType())
                {
                    switch (ComparisonType)
                    {
                    case "Equal":
                        result = (input.Value == Value);
                        break;

                    case "NotEqual":
                        result = (input.Value != Value);
                        break;

                    case "GreaterThan":
                        result = (input.Value > Value);
                        break;

                    case "GreaterThanOrEqual":
                        result = (input.Value >= Value);
                        break;

                    case "LessThan":
                        result = (input.Value < Value);
                        break;

                    case "LessThanOrEqual":
                        result = (input.Value <= Value);
                        break;

                    default:
                        return(result);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses the specified JSON input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="invalidSignals">The invalid signals.</param>
        /// <param name="rules">The rules.</param>
        /// <returns>
        /// Returns a list of invalid signals according to the rules.
        /// </returns>
        /// <exception cref="ArgumentNullException">input</exception>
        public List <SignalData> TryParse(string input, params IRule[] rules)
        {
            var invalidSignals = new List <SignalData>();

            try
            {
                if (string.IsNullOrEmpty(input))
                {
                    throw new ArgumentNullException("input");
                }



                foreach (var signal in input.Split(Constants.ObjectEnd))
                {
                    var elements = signal.Split(new[] { Constants.ElementSeparator }, StringSplitOptions.RemoveEmptyEntries);
                    if (elements.Length == 3) // Signal, Value and ValueType
                    {
                        string parsedSignal = string.Empty, parsedValue = string.Empty, parsedValueType = string.Empty;

                        foreach (var element in elements)
                        {
                            if (!string.IsNullOrWhiteSpace(element))
                            {
                                int    indexOfObjectSeparator = element.IndexOf(Constants.ObjectSeparator);
                                string fieldName  = element.Substring(0, indexOfObjectSeparator);
                                string fieldValue = element.Substring(indexOfObjectSeparator + 1);

                                if (!string.IsNullOrWhiteSpace(fieldName) && !string.IsNullOrWhiteSpace(fieldValue))
                                {
                                    fieldName  = fieldName.ToLowerInvariant().Split(Constants.ObjectWrapper)[1];
                                    fieldValue = fieldValue.Split(Constants.ObjectWrapper)[1];

                                    switch (fieldName.ToLowerInvariant())
                                    {
                                    case fieldSignalName:
                                        parsedSignal = fieldValue;
                                        continue;

                                    case fieldValueTypeName:
                                        if (fieldValue == "Integer")
                                        {
                                            parsedValueType = "Decimal";
                                        }
                                        else if (fieldValue == "String")
                                        {
                                            parsedValueType = "String";
                                        }
                                        else
                                        {
                                            parsedValueType = "DateTime";
                                        }
                                        continue;

                                    case fieldValueName:
                                        parsedValue = fieldValue;
                                        continue;

                                    default:
                                        break;
                                    }
                                }
                            }
                        }


                        var     typeInfo = Type.GetType($"System.{parsedValueType}");
                        dynamic value    = null;
                        if (typeInfo != null)
                        {
                            value = Convert.ChangeType(parsedValue, typeInfo);
                        }
                        else
                        {
                            value = parsedValue;
                        }

                        var signalData = new SignalData(parsedSignal, value, parsedValueType);
                        foreach (var rule in rules)
                        {
                            if (!rule.IsValid(signalData))
                            {
                                invalidSignals.Add(signalData); // Add to invalid signal list
                                break;
                            }
                        }
                    }
                }
                return(invalidSignals);
            }
            catch (Exception)
            {
                throw;
            }
        }