Esempio n. 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 scriptAction in script.Actions)
                {
                    // Action string is not present in ActionType enum
                    if (!Enum.TryParse <ActionType> (scriptAction.Type, out type))
                    {
                        throw new ScriptActionTypeInvalidException(scriptAction.Type);
                    }

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

                    Parameter.ParameterType paramTypeToAdd;
                    foreach (PropertyInfo propertyInfo in scriptAction.GetType().GetProperties())
                    {
                        var property   = actionType.GetProperty(propertyInfo.Name);
                        var paramValue = property.GetValue(scriptAction, 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
                            if (!Enum.TryParse <Parameter.ParameterType> (propertyInfo.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));
                            }
                        }
                    }

                    // Additional parameters
                    Parameter parameter;
                    foreach (var entry in scriptAction.AdditionalParameters)
                    {
                        parameter          = new Parameter(entry.Key, entry.Key, entry.Value);
                        parameter.Optional = true;
                        new_action.AddAdditionalParameter(parameter);
                    }

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

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