示例#1
0
        private void buildScriptActions(ISerial serial_device, Script script)
        {
            actions = new List <Action>();

            int step = 0;

            if (string.IsNullOrEmpty(script.UserName))
            {
                throw new ScriptUserNameMissingException();
            }

            Mobile.LogUserPath = script.UserName;

            // Using invalid log file/path
            if (string.IsNullOrEmpty(script.LogFile) ||
                !Regex.IsMatch(script.LogFile, @"^[a-zA-Z_][a-zA-Z0-9_-]*.xml$"))
            {
                throw new ScriptLogfileInvalidException();
            }

            else
            {
                ActionType type;
                foreach (Xml.ScriptAction action in script.Actions)
                {
                    // Action string is not present in ActionType enum
                    if (!Enum.TryParse <ActionType> (action.Type, out type))
                    {
                        throw new ScriptActionTypeInvalidException(action.Type);
                    }

                    Action new_action = new Action(Singleton.Get.Configuration, serial_device, type, script.UserName, script.LogFile);
                    Type   actionType = action.GetType();

                    Parameter.ParameterType paramTypeToAdd;
                    foreach (PropertyInfo parameter in action.GetType().GetProperties())
                    {
                        var paramValue = actionType.GetProperty(parameter.Name).GetValue(action, null);
                        if (paramValue is null)
                        {
                            continue;
                        }

                        Type valueType = paramValue.GetType();

                        if (valueType.Name.ToLower().Contains("actionparameter"))
                        {
                            // The parameter name is not listed in the ParameterType enumeration
                            // NOTE: This also is useful to ignore automaticaly created properties ( xxx_deserialize )
                            if (!Enum.TryParse <Parameter.ParameterType> (parameter.Name, out paramTypeToAdd))
                            {
                                continue;
                            }

                            List <ActionParameter> list = new List <ActionParameter> ();

                            // If the parameter is an Array is a field for
                            // a port, and if not is a field for the MTU
                            if (!paramValue.GetType().IsArray)
                            {
                                list.Add(( ActionParameter   )paramValue);
                            }
                            else
                            {
                                list.AddRange(( ActionParameter[] )paramValue);
                            }

                            foreach (ActionParameter aParam in list)
                            {
                                new_action.AddParameter(
                                    new Parameter(
                                        paramTypeToAdd,
                                        aParam.Value,
                                        aParam.Port));
                            }
                        }
                    }

                    new_action.order       = step;
                    new_action.OnProgress += Action_OnProgress;
                    new_action.OnFinish   += Action_OnFinish;
                    new_action.OnError    += Action_OnError;

                    actions.Add(new_action);
                    step++;
                }
            }
        }
示例#2
0
        private void buildScriptActions(String base_path, ISerial serial_device, Script script)
        {
            actions = new List <Action>();

            int step = 0;

            // Using invalid log file/path
            if (string.IsNullOrEmpty(script.LogFile) ||
                !Regex.IsMatch(script.LogFile, @"^[a-zA-Z_][a-zA-Z0-9_-]*.xml$"))
            {
                Errors.LogErrorNow(new ScriptLogfileInvalidException());
            }
            else
            {
                ActionType type;
                foreach (Xml.ScriptAction action in script.Actions)
                {
                    // Action string is not present in ActionType enum
                    if (!Enum.TryParse <ActionType> (action.Type, out type))
                    {
                        Errors.LogErrorNow(new ScriptActionTypeInvalidException(action.Type));
                        return;
                    }

                    Action new_action = new Action(Configuration.GetInstance(), serial_device, type, script.UserName, script.LogFile);
                    Type   actionType = action.GetType();

                    foreach (PropertyInfo parameter in action.GetType().GetProperties())
                    {
                        try
                        {
                            var  paramValue = actionType.GetProperty(parameter.Name).GetValue(action, null);
                            Type valueType  = paramValue.GetType();

                            if (valueType.Name.ToLower().Contains("actionparameter"))
                            {
                                List <ActionParameter> list = new List <ActionParameter> ();

                                // If the parameter is an Array is a field for
                                // a port, and if not is a field for the MTU
                                if (!paramValue.GetType().IsArray)
                                {
                                    list.Add(( ActionParameter   )paramValue);
                                }
                                else
                                {
                                    list.AddRange(( ActionParameter[] )paramValue);
                                }

                                foreach (ActionParameter aParam in list)
                                {
                                    new_action.AddParameter(
                                        new Parameter(
                                            parseParameterType(parameter.Name),
                                            aParam.Value,
                                            aParam.Port));
                                }
                            }
                        }
                        catch (Exception e)
                        {
                        }
                    }

                    new_action.order       = step;
                    new_action.OnProgress += Action_OnProgress;
                    new_action.OnFinish   += Action_OnFinish;
                    new_action.OnError    += Action_OnError;

                    actions.Add(new_action);
                    step++;
                }
            }
        }