Example #1
0
        private IEnumerable <Impl.Command> CreateCommands(
            IControllerTag controllerTag,
            Controller controller,
            Tag userDefinedInterfaceTag,
            ITagListener tagListener)
        {
            var commandsTag = controller.FindTag(TagName.CmdList(parent: null), userDefinedInterfaceTag);

            if (commandsTag != null)
            {
                Tag modeTag    = null;
                Tag commandTag = null;

                // assuming that the job interface is defined in the parent tag of the commandsTag
                var jobTag = commandsTag.Parent;

                // get the mode tag
                modeTag    = controller.FindTag(TagName.Mode(parent: null), jobTag);
                commandTag = controller.FindTag(TagName.ManualCommand(parent: null), jobTag);

                if (jobTag.NestedName != TagName.Job(parent: null) ||
                    modeTag == null || commandTag == null)
                {
                    throw new InvalidOperationException(new StringBuilder()
                                                        .AppendFormat("The {0} is not defined inside a {1} structure for the controller {2} or ",
                                                                      TagName.CmdList(parent: null),
                                                                      TagName.Job(parent: null),
                                                                      controller.Name)
                                                        .AppendFormat("the {0} or {1} was not defined in the {2} structure. ",
                                                                      TagName.Mode(parent: null),
                                                                      TagName.ManualCommand(parent: null),
                                                                      TagName.Job(parent: null))
                                                        .AppendLine()
                                                        .AppendFormat("Set the {0} as a subelement of a valid {1} structure.",
                                                                      commandsTag.Name, TagName.Job(parent: null)).ToString());
                }

                tagListener.ReadTagSynchronously(commandsTag);
                var commands     = commandsTag.ArrayValues <Command>();
                int commandIndex = 0;

                if (commands.Any(c => c == null))
                {
                    throw new InvalidOperationException(new StringBuilder()
                                                        .AppendFormat("The commands {0} of the controller {1} could not be read. ",
                                                                      commandsTag.Name,
                                                                      controller.Name)
                                                        .AppendLine()
                                                        .AppendFormat("Expected array datatype {0}.", CmdList.PlcArrayDataType())
                                                        .AppendLine()
                                                        .AppendFormat("Current array datatype {0}.", commandsTag.DataType).ToString());
                }

                foreach (Command command in commands.Where(c => c != null && !c.Name.IsNullOrEmpty()))
                {
                    Tag tag = controller.FindTag(TagName.CmdList(null, commandIndex), commandsTag);
                    if (tag == null)
                    {
                        continue;
                    }
                    var commandImpl = new Impl.Command(controller, command, tag.Childs.ToReadOnly(), modeTag, commandTag);
                    yield return(commandImpl);

                    commandIndex++;
                }

                // remove CmdListArray-Tag in order to prevent that the same tags exists more
                // than once --> For example the tag "bolAvailable"
                jobTag.Childs.Remove(commandsTag);
            }
        }