Beispiel #1
0
        public void 解析_CactiSnmp_ALERT訊息()
        {
            //Arrange
            var parser = ParserFactory.CreateInstance(Detector.Cacti);

            var raw = "{\"id\":\"192.168.10.99\", \"target\": \"Traffic - Gi1/0/20 [traffic_in]\", \"action\":\"ALERT\", \"info\":\"current value is 5630.6207\",\"time\":\"2018/11/06 18:08:34\"}";

            var expected = new List <Monitor>
            {
                new Monitor
                {
                    DEVICE_ID      = "192.168.10.99",
                    TARGET_NAME    = "Traffic - Gi1/0/20 [traffic_in]",
                    TARGET_VALUE   = "ALERT",
                    TARGET_MESSAGE = "current value is 5630.6207",
                    RECEIVE_TIME   = DateTime.Parse("2018/11/06 18:08:34", CultureInfo.InvariantCulture)
                }
            };

            //Act
            var actual = parser.ParseRecord(raw);

            //Assert
            CollectionAssert.AreEqual(expected, actual);
        }
        public object GetValue(ArgumentInfo argumentInfo)
        {
            PromptForValue(argumentInfo);

            //when user has provided a value
            if (argumentInfo.ValueInfo.HasValue && argumentInfo.ValueInfo.Value != null)
            {
                //parse value
                IParser parser = _parserFactory.CreateInstance(argumentInfo);
                return(parser.Parse(argumentInfo));
            }

            //when value not present but method parameter has a default value defined
            if (!argumentInfo.DefaultValue.IsNullValue())
            {
                //use default parameter or property value
                return(argumentInfo.DefaultValue);
            }

            //when there no value from input and no default value, return default value of the type
            return(argumentInfo.Type.GetDefaultValue());
        }
Beispiel #3
0
 /// <summary>
 /// 初始工作站
 /// </summary>
 private void InitWorkStation()
 {
     _parser  = ParserFactory.CreateInstance(_detector);
     _alarmer = new Alarmer();
 }
        private static bool TryBindArgument(IArgument argument, IConsole console, ParserFactory parserFactory)
        {
            bool SetFromStringInput(IArgument arg, IEnumerable <string> values)
            {
                try
                {
                    var parser = parserFactory.CreateInstance(arg);
                    var value  = parser.Parse(arg, values);
                    arg.Value = value;
                    return(true);
                }
                catch (ValueParsingException ex)
                {
                    console.Error.WriteLine($"Failure parsing value for {argument.GetType().Name}: {arg.Name}.  value(s)={values?.ToCsv()}");
                    console.Error.WriteLine(ex.Message);
                    console.Error.WriteLine();
                    return(false);
                }
            }

            if (argument.InputValues.Any())
            {
                if (!SetFromStringInput(argument, argument.InputValues.SelectMany(iv => iv.Values)))
                {
                    return(false);
                }
            }
            else if (argument.Default != null)
            {
                var defaultValue = argument.Default.Value;
                // middleware could set the default to any type.
                // string values could be assigned from attributes or config values
                // so they are parsed as if submitted from the shell.
                switch (defaultValue)
                {
                case string stringValue:
                    if (!SetFromStringInput(argument, stringValue.ToEnumerable()))
                    {
                        return(false);
                    }

                    break;

                case IEnumerable <string> multiString:
                    if (!SetFromStringInput(argument, multiString))
                    {
                        return(false);
                    }

                    break;

                default:
                    try
                    {
                        var argumentType = argument.TypeInfo.Type;
                        if (argumentType.IsInstanceOfType(defaultValue))
                        {
                            argument.Value = defaultValue;
                        }
                        else
                        {
                            // cover cases like converting int to long.
                            var convertedValue = Convert.ChangeType(defaultValue, argumentType);
                            argument.Value = convertedValue;
                        }
                    }
                    catch (Exception ex)
                    {
                        console.Error.WriteLine(
                            $"Failure assigning value to {argument}. Value={defaultValue}");
                        console.Error.WriteLine(ex.Message);
                        console.Error.WriteLine();
                        return(false);
                    }

                    break;
                }
            }

            return(true);
        }
        internal static Task <int> BindValues(CommandContext commandContext, ExecutionDelegate next)
        {
            var console       = commandContext.Console;
            var parserFactory = new ParserFactory(commandContext.AppConfig.AppSettings);

            bool SetFromStringInput(IArgument argument, IEnumerable <string> values, out int returnCode)
            {
                try
                {
                    var parser = parserFactory.CreateInstance(argument);
                    var value  = parser.Parse(argument, values);
                    argument.Value = value;
                    returnCode     = 0;
                    return(true);
                }
                catch (ValueParsingException ex)
                {
                    console.Error.WriteLine($"Failure parsing value for {argument}.  values={values?.ToCsv()}");
                    console.Error.WriteLine(ex.Message);
                    console.Error.WriteLine();
                    returnCode = 2;
                    return(false);
                }
            }

            var arguments = commandContext.InvocationPipeline.All
                            .SelectMany(ic => ic.Command.Options.Cast <IArgument>().Union(ic.Command.Operands))
                            .Where(a => a.GetArgumentDef() != null);

            foreach (var argument in arguments)
            {
                if (argument.InputValues.Any())
                {
                    if (!SetFromStringInput(argument, argument.InputValues.SelectMany(iv => iv.Values), out int returnCode))
                    {
                        return(Task.FromResult(returnCode));
                    }
                }
                else if (argument.DefaultValue != null)
                {
                    var defaultValue = argument.DefaultValue;
                    // middleware could set the default to any type.
                    // string values could be assigned from attributes or config values
                    // so they are parsed as if submitted from the shell.
                    switch (defaultValue)
                    {
                    case string stringValue:
                        if (!SetFromStringInput(argument, stringValue.ToEnumerable(), out int returnCode))
                        {
                            return(Task.FromResult(returnCode));
                        }

                        break;

                    case IEnumerable <string> multiString:
                        if (!SetFromStringInput(argument, multiString, out returnCode))
                        {
                            return(Task.FromResult(returnCode));
                        }

                        break;

                    default:
                        try
                        {
                            var argumentType = argument.TypeInfo.Type;
                            if (argumentType.IsInstanceOfType(defaultValue))
                            {
                                argument.Value = defaultValue;
                            }
                            else
                            {
                                // cover cases like converting int to long.
                                var convertedValue = Convert.ChangeType(defaultValue, argumentType);
                                argument.Value = convertedValue;
                            }
                        }
                        catch (Exception ex)
                        {
                            console.Error.WriteLine(
                                $"Failure assigning value to {argument}. Value={defaultValue}");
                            console.Error.WriteLine(ex.Message);
                            console.Error.WriteLine();
                            return(Task.FromResult(2));
                        }

                        break;
                    }
                }
            }

            return(next(commandContext));
        }