Esempio n. 1
0
        /// <summary>
        /// Generate a class for retrieving the result of a SOAP opeartion.
        /// </summary>
        /// <param name="serviceTypeName">The name of the class which implements the corresponding service.</param>
        /// <param name="actionName">The action for which a result type will be generated.</param>
        /// <param name="targetDirectory">The directory to which the wrapper will be generated.</param>
        /// <param name="targetNamespace">The namespace to which the wrapper will be generated.</param>
        /// <param name="elements">The elements of the type.</param>
        /// <param name="stateVariables">The state variables for this type.</param>
        /// <returns>The name of the generated type.</returns>
        private string GenerateResultType(string serviceTypeName, string actionName, string targetDirectory, string targetNamespace, IEnumerable <Argument> elements, List <ServiceStateVariable> stateVariables)
        {
            string typeName = serviceTypeName + actionName + "Result";
            string filename = Path.Combine(targetDirectory, typeName + ".cs");

            this.GenerateFileHeader(filename, targetNamespace, typeName, $"Result type for {actionName} in service {serviceTypeName}.");
            using (StreamWriter writer = new StreamWriter(filename, true))
            {
                foreach (Argument element in elements)
                {
                    ServiceStateVariable serviceStateVariable = stateVariables.First(sv => sv.Name == element.RelatedStateVariable);
                    writer.WriteLine($"    /// <summary>");
                    if (serviceStateVariable.DataType == DataType.Boolean)
                    {
                        writer.WriteLine($"    /// Gets or sets a value indicating whether the SOAP argument {element.Name} is set or not.");
                    }
                    else
                    {
                        writer.WriteLine($"    /// Gets or sets the SOAP argument {element.Name}.");
                    }

                    writer.WriteLine($"    /// </summary>");
                    writer.WriteLine($@"    [System.Xml.Serialization.XmlElement(""{element.Name}"")]");
                    writer.WriteLine($"    public {dataTypeMapping[serviceStateVariable.DataType]} {element.Name.Replace("-", "_")} {{ get; set; }}");
                    if (element != elements.Last())
                    {
                        writer.WriteLine();
                    }
                }
            }

            this.GenerateFileFooter(filename);
            return(typeName);
        }
Esempio n. 2
0
        /// <summary>
        /// Generates the service wrapper for the specific device.
        /// </summary>
        /// <param name="targetDirectory">The directory to which the wrapper will be generated.</param>
        /// <param name="targetNamespace">The namespace to which the wrapper will be generated.</param>
        /// <param name="device">The device.</param>
        private void GenerateServiceWrapper(string targetDirectory, string targetNamespace, Device device)
        {
            if (Directory.Exists(targetDirectory) == false)
            {
                Directory.CreateDirectory(targetDirectory);
            }

            foreach (SoapService soapService in device.Services)
            {
                int    end      = soapService.ServiceType.LastIndexOf(":") - 1;
                int    start    = soapService.ServiceType.LastIndexOf(":", end) + 1;
                string typeName = soapService.ServiceType.Substring(start, end - start + 1);
                typeName = typeName.Replace("-", "_");
                string filename = Path.Combine(targetDirectory, typeName + ".cs");
                this.GenerateFileHeader(filename, targetNamespace, typeName, $"Wrapper for the service {soapService.ServiceType}.", nameof(BaseService));
                using (StreamWriter writer = new StreamWriter(filename, true))
                {
                    writer.WriteLine($@"    /// <inheritdoc/>");
                    writer.WriteLine($@"    protected override string ServiceType {{ get; }} = ""{soapService.ServiceType}"";");
                    writer.WriteLine();

                    foreach (SoapAction action in soapService.Scpd.Actions)
                    {
                        writer.WriteLine($"    /// <summary>");
                        writer.WriteLine($"    /// Wrapper for the action {action.Name}.");
                        writer.WriteLine($"    /// </summary>");
                        List <string> parameters  = new List <string>();
                        var           inArguments = action.Arguments.Where(a => a.Direction == Direction.In);
                        foreach (Argument argument in inArguments)
                        {
                            string parameterName = this.ConvertArgumentNameToParameterName(argument.Name);
                            ServiceStateVariable serviceStateVariable = soapService.Scpd.StateVariables.First(sv => sv.Name == argument.RelatedStateVariable);
                            if (serviceStateVariable.AllowedValues.Any())
                            {
                                writer.WriteLine($@"    /// <param name=""{parameterName}"">The SOAP parameter {argument.Name}. Allowed values: {string.Join(", ", serviceStateVariable.AllowedValues)}.</param>");
                            }
                            else
                            {
                                writer.WriteLine($@"    /// <param name=""{parameterName}"">The SOAP parameter {argument.Name}.</param>");
                            }

                            parameters.Add($"{dataTypeMapping[serviceStateVariable.DataType]} {parameterName}");
                        }

                        string actionName         = action.Name.Replace("-", "_");
                        string returnType         = "void";
                        string defaultReturnValue = "null";
                        var    outArguments       = action.Arguments.Where(a => a.Direction == Direction.Out);
                        if (outArguments.Count() == 1)
                        {
                            Argument             argument             = outArguments.First();
                            ServiceStateVariable serviceStateVariable = soapService.Scpd.StateVariables.First(sv => sv.Name == argument.RelatedStateVariable);
                            returnType = dataTypeMapping[serviceStateVariable.DataType];
                            writer.WriteLine($"    /// <returns>The result ({argument.Name}) of the action.</returns>");
                            if (returnValueMapping.ContainsKey(serviceStateVariable.DataType))
                            {
                                defaultReturnValue = returnValueMapping[serviceStateVariable.DataType];
                            }
                        }
                        else if (outArguments.Count() > 1)
                        {
                            returnType = this.GenerateResultType(typeName, actionName, targetDirectory, targetNamespace, outArguments, soapService.Scpd.StateVariables);
                            writer.WriteLine($"    /// <returns>The result ({returnType}) of the action.</returns>");
                        }

                        writer.WriteLine($"    public {returnType} {actionName}({string.Join(", ", parameters)})");
                        writer.WriteLine($"    {{");
                        string secondParameter = string.Empty;
                        if (inArguments.Any())
                        {
                            secondParameter = ", arguments";
                            writer.WriteLine($@"      System.Collections.Generic.Dictionary<string, object> arguments = new System.Collections.Generic.Dictionary<string, object>();");
                            foreach (Argument argument in inArguments)
                            {
                                writer.WriteLine($@"      arguments.Add(""{argument.Name}"", {this.ConvertArgumentNameToParameterName(argument.Name)});");
                            }
                        }

                        if (outArguments.Any())
                        {
                            writer.WriteLine($@"      return this.SendRequest<{returnType}>(""{action.Name}""{secondParameter});");
                        }
                        else
                        {
                            writer.WriteLine($@"      this.SendRequest(""{action.Name}""{secondParameter});");
                        }

                        writer.WriteLine($"    }}");
                        if (action != soapService.Scpd.Actions.Last())
                        {
                            writer.WriteLine();
                        }
                    }
                }

                this.GenerateFileFooter(filename);
            }

            foreach (Device d in device.Devices)
            {
                int    end           = d.DeviceType.LastIndexOf(":") - 1;
                int    start         = d.DeviceType.LastIndexOf(":", end) + 1;
                string directoryName = d.DeviceType.Substring(start, end - start + 1);
                this.GenerateServiceWrapper(Path.Combine(targetDirectory, directoryName), targetNamespace + "." + directoryName, d);
            }
        }