/// <summary>
 /// Gets an item from the cache by URL.
 /// </summary>
 /// <param name="url">The URL to get the item from.</param>
 /// <param name="desc">The description to add the URL cache.</param>
 /// <returns>The service description found or null if none.</returns>
 public void AddURL(string url, ServiceDescription desc)
 {
     mdURLCache[url] = desc;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates the code for all actions.
        /// </summary>
        /// <param name="consts">The stirng constants created.</param>
        /// <param name="actionMethods">The StringBuilder to contain the action call methods.</param>
        /// <param name="enumStateVars">A HashSet of the state variables which are enumerations.</param>
        /// <param name="stateVarTypes">A Dictionary containing the state variables and their data types.</param>
        /// <param name="desc">The service description to generate the action code for.</param>
        private void GenerateActionCode(
            StringConstants consts, StringBuilder actionMethods, HashSet<string> enumStateVars,
            Dictionary<string, string> stateVarTypes, ServiceDescription desc)
        {
            // For each action
            foreach (ActionDescription ladDesc in desc.Actions.Values)
            {
                string lsFriendlyName = CodeGenProvider.CodeFriendlyIdentifier(ladDesc.Name, false);
                int liInArgumentCount = 0;
                int liInArgumentIndex = 0;
                int liOutArgumentIndex = 0;
                StringBuilder lsbInArguments = new StringBuilder();
                StringBuilder lsbOutArguments = new StringBuilder();
                StringBuilder lsbInSetValues = new StringBuilder();
                StringBuilder lsbOutSetValues = new StringBuilder();
                StringBuilder lsbInParamComments = new StringBuilder();
                StringBuilder lsbOutParamComments = new StringBuilder();
                StringBuilder lsbReturnsComments = new StringBuilder();
                int liOutArguments = ladDesc.Arguments.OutArgCount;
                string lsLastOutType = CodeGenProvider.UnknownType;

                // For each argument
                foreach (ArgumentDescription ladArgDesc in ladDesc.Arguments.Values)
                {
                    bool lbEnumStateVar = enumStateVars.Contains(ladArgDesc.RelatedStateVariable);
                    string lsArgFriendlyName = CodeGenProvider.CodeFriendlyIdentifier(ladArgDesc.Name, true);
                    string lsRelatedStateVarFriendlyName = CodeGenProvider.CodeFriendlyIdentifier(ladArgDesc.RelatedStateVariable, false);
                    string lsType;

                    // Get the type of the related state variable, or use unknown type if state variable cannot be found
                    if (!stateVarTypes.TryGetValue(ladArgDesc.RelatedStateVariable, out lsType))
                        lsType = CodeGenProvider.UnknownType;

                    // Determine direction
                    if (ladArgDesc.DirectionValue == ArgumentDirection.Out)
                    {
                        lsLastOutType = lsType;

                        // If there is more than 1 out argument then use out parameters
                        if (liOutArguments > 1)
                            GenerateOutArgumentCode(
                                ladArgDesc, liOutArgumentIndex, lsbOutArguments, lsbOutSetValues, lbEnumStateVar,
                                lsArgFriendlyName, lsRelatedStateVarFriendlyName, lsType, lsbOutParamComments);
                        else
                            // Otherwise use a returning function
                            GenerateOutArgumentReturnCode(
                                ladArgDesc, liOutArgumentIndex, lsbOutSetValues, lbEnumStateVar,
                                lsRelatedStateVarFriendlyName, lsType, lsbReturnsComments);

                        // Increment the out argument index
                        liOutArgumentIndex++;
                    }
                    else
                    {
                        // Generate the in argument code
                        GenerateInArgumentCode(
                            ladArgDesc, liInArgumentIndex, lsbInArguments, lsbInSetValues,
                            lbEnumStateVar, lsArgFriendlyName, lsRelatedStateVarFriendlyName,
                            lsType, lsbInParamComments);

                        // Increment in argument count and index
                        liInArgumentCount++;
                        liInArgumentIndex++;
                    }
                }

                // Generate the method code
                actionMethods.Append(
                    string.Format(
                        (liOutArguments == 1 ? CodeGenProvider.ReturnAction : CodeGenProvider.Action),
                        lsFriendlyName,
                        lsbInArguments,
                        lsbOutArguments,
                        CodeGenProvider.ArraySizeForCount(liInArgumentCount),
                        lsbInSetValues,
                        consts[CodeGenProvider.ActionNameGroup, ladDesc.Name, string.Format(CodeGenProvider.ActionNameComment, ladDesc.Name)],
                        lsbOutSetValues,
                        (lsbInArguments.Length > 0 && lsbOutArguments.Length > 0 ? CodeGenProvider.ParameterSeperator : String.Empty),
                        lsLastOutType,
                        lsbInParamComments,
                        lsbOutParamComments,
                        lsbReturnsComments,
                        (liOutArguments > 0 ? CodeGenProvider.OutVar : string.Empty)
                    )
                );
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates the code for all state variables.
        /// </summary>
        /// <param name="service">The service containing the state variables.</param>
        /// <param name="testStateVars">
        /// True to test each state variable to ensure it is 
        /// usuable for accessing as property, false to include
        /// all state variables as properties.</param>
        /// <param name="consts">The string constants created.</param>
        /// <param name="stateVarProps">A StringBuilder to contain property definitions.</param>
        /// <param name="stateVarConversion">A StringBuilder to contain the conversion method definitions.</param>
        /// <param name="stateVarEnums">A StringBuilder to contain the enumerations for the state variables.</param>
        /// <param name="eventHandlers">A StringBuilder to contain event handler declarations for the state variable changes.</param>
        /// <param name="eventCallers">A StringBuilder to contain event caller methods for the state variable changes.</param>
        /// <param name="stateVarEventIntercept">A StringBuilder to contain case statement code for each event state variable.</param>
        /// <param name="enumStateVars">A HashSet containing the names of all enumerated state variables.</param>
        /// <param name="stateVarTypes">A Dictionary to contain each state variaible name and its code data type.</param>
        /// <param name="desc">The service description to create the state variable code from.</param>
        private void GenerateStateVarCode(
            Service service, bool testStateVars, StringConstants consts, StringBuilder stateVarProps, StringBuilder stateVarConversion,
            StringBuilder stateVarEnums, StringBuilder eventHandlers, StringBuilder eventCallers, StringBuilder stateVarEventIntercept,
            HashSet<string> enumStateVars, Dictionary<string, string> stateVarTypes,
            ServiceDescription desc)
        {
            // For each state variaible description
            foreach (StateVariableDescription lsdStateVarDesc in desc.StateVariables.Values)
            {
                string lsFriendlyName = CodeGenProvider.CodeFriendlyIdentifier(lsdStateVarDesc.Name, false);

                // Determine if we actually want the property accessor for the state variable
                bool lbAddProp = IsStateVarQueryable(service, testStateVars, lsdStateVarDesc);

                // If the state variable is a string and has an allowed value list
                if (lsdStateVarDesc.DataTypeValue == StateVariableDataType.tstring && lsdStateVarDesc.AllowedValues.Count > 0)
                    // Generate it using an enumeration
                    GenerateEnumStateVarCode(
                        consts, stateVarProps, stateVarConversion, stateVarEnums,
                        eventHandlers, eventCallers, stateVarEventIntercept, enumStateVars,
                        stateVarTypes, lsdStateVarDesc, lsFriendlyName, lbAddProp);
                else
                    // Otherwise generate it using simple properties
                    GenerateStateVarCode(
                        consts, stateVarProps, eventHandlers, eventCallers, stateVarEventIntercept,
                        stateVarTypes, lsdStateVarDesc, lsFriendlyName, lbAddProp);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the service description for this service from the root description.
        /// </summary>
        /// <param name="root">The root description to get the service description from.</param>
        /// <returns>The service description or null if it cannot be located.</returns>
        public ServiceDescription GetDescription(RootDescription root)
        {
            if (Logging.Enabled)
                Logging.Log(this, string.Format("Getting ServiceDescription for DeviceServiceDescription: '{0}'", this.ServiceId), 1);

            try
            {
                string lsURLBase = URLBase(root);

                if (lsURLBase.Length > 0 && SCPDURL.Length > 0)
                {
                    string lsURL = Utils.CombineURL(lsURLBase, SCPDURL);
                    if (Logging.Enabled)
                        Logging.Log(this, string.Format("Combined URL: {0}", lsURL));

                    ServiceDescription lsdDesc = ServiceDescriptionCache.Cache.ByURL(lsURL);
                    if (lsdDesc != null)
                    {
                        if (Logging.Enabled)
                            Logging.Log(this, string.Format("Description Document found in cache by URL"));
                        return lsdDesc;
                    }

                    if (Logging.Enabled)
                        Logging.Log(this, String.Format("Getting Document URL: '{0}'", lsURL));

                    try
                    {
                        using (XmlTextReader lrReader = Utils.GetXMLTextReader(lsURL))
                        {
                            if (Logging.Enabled)
                                Logging.Log(this, "Finding start node");

                            while (lrReader.Read())
                                if (ServiceDescription.IsStartNodeFor(lrReader)) break;

                            if (ServiceDescription.IsStartNodeFor(lrReader))
                            {
                                if (Logging.Enabled)
                                    Logging.Log(this, "Start node found, processing description");
                                lsdDesc = new ServiceDescription(this, lsURL, Device.UDN, ServiceId, lrReader);
                            }
                        }
                    }
                    catch (Exception loE)
                    {
                        if (Logging.Enabled)
                            Logging.Log(this, String.Format("Downloading and processing of URL failed with error: {0}", loE.ToString()));
                        throw;
                    }

                    if (Logging.Enabled)
                        Logging.Log(this, "Adding to URL description document cache");
                    ServiceDescriptionCache.Cache.AddURL(lsURL, lsdDesc);

                    return lsdDesc;
                }
                else
                    return null;
            }
            finally
            {
                if (Logging.Enabled)
                    Logging.Log(this, "Finished getting ServiceDescription", -1);
            }
        }