Exemple #1
0
        /// <summary>
        /// Renames a <see cref="FhemObject"/>.
        /// </summary>
        /// <param name="a_fhemObject">
        /// The affected FhemObject.
        /// </param>
        /// <param name="a_newName">
        /// The new name.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The FhemObject may not be null.
        /// </exception>
        public FhemClientResponse RenameFhemObject(FhemObject a_fhemObject, string a_newName)
        {
            //-- Check that we are really connected to a Fhem server
            this.CheckConnection();

            if (a_fhemObject == null)
            {
                throw new ArgumentNullException("The FhemObject may not be null!");
            }

            if (a_fhemObject.Name != a_newName)
            {
                //-- Assemble the native command for renaming the Fhem object
                var nativeRenameCommand = String.Format("rename {0} {1}", a_fhemObject.Name, a_newName);

                //-- Send the renaming command
                var response = this.SendNativeCommand(nativeRenameCommand);

                if (response != null)
                {
                    //-- Any response will describe an error in this case
                    return(new FhemClientResponse(response));
                }
            }

            //-- Everything ok
            return(new FhemClientResponse());
        }
Exemple #2
0
        /// <summary>
        /// Gets a specific <see cref="FhemObject"/>.
        /// </summary>
        /// <param name="a_fhemObjectName">
        /// The name of the desired Fhem object.
        /// </param>
        /// <returns>
        /// The fitting Fhem object or null when no fitting object was found.
        /// </returns>
        public FhemObject GetFhemObject(string a_fhemObjectName)
        {
            //-- Check that we are really connected to a Fhem server
            this.CheckConnection();

            //-- Use the 'jsonlist2' command for creating the FHEM object list
            var jsonlist2Response = this.SendNativeCommand(String.Format("jsonlist2 {0}", a_fhemObjectName));

            //-- Parse the response into a JSON object
            var jsonObject = JObject.Parse(jsonlist2Response);

            //-- Determine the 3 main json tokens
            var argJsonToken          = jsonObject.First;
            var resultsJsonToken      = argJsonToken.Next;
            var totalResultsJsonToken = (JProperty)resultsJsonToken.Next;

            //-- Determine the first json token that represents a fhem object
            var fhemObjectAsJsonObject = (JObject)resultsJsonToken.First.First;

            //-- Get the number of results
            var resultsCount = (int)totalResultsJsonToken.Value;

            if (resultsCount > 0)
            {
                //-- Parse the Fhem object from the JObject
                var fhemObject = FhemObject.FromJObject(fhemObjectAsJsonObject);

                //-- Return the first found Fhem object
                return(fhemObject);
            }

            //-- Nothing found :(
            return(null);
        }
Exemple #3
0
        /// <summary>
        /// Gets all available Fhem objects.
        /// </summary>
        /// <returns></returns>
        public FhemObjectsCollection GetFhemObjects()
        {
            //-- Check that we are really connected to a Fhem server
            this.CheckConnection();

            //-- Use the 'jsonlist2' command for creating the FHEM object list
            var jsonlist2Response = this.SendNativeCommand("jsonlist2");

            //-- Parse the response into a JSON object
            var jsonObject = JObject.Parse(jsonlist2Response);

            //-- Determine the 3 main json tokens
            var argJsonToken          = jsonObject.First;
            var resultsJsonToken      = argJsonToken.Next;
            var totalResultsJsonToken = (JProperty)resultsJsonToken.Next;

            //-- Determine the first json token that represents a fhem object
            var fhemObjectAsJsonObject = (JObject)resultsJsonToken.First.First;

            //-- Prepare the list for storing all Fhem objects
            var fhemObjectsCollection = new FhemObjectsCollection();

            while (fhemObjectAsJsonObject != null)
            {
                //-- Parse the Fhem object from the JObject
                var fhemObject = FhemObject.FromJObject(fhemObjectAsJsonObject);

                //-- Add it to the list
                fhemObjectsCollection.Add(fhemObject);

                //-- Update to the next JObject
                fhemObjectAsJsonObject = (JObject)fhemObjectAsJsonObject.Next;
            }

            return(fhemObjectsCollection);
        }
Exemple #4
0
        //-- Constructors
        #endregion
        //---------------------------------------------------------------------
        #region Methods

        /// <summary>
        /// Parses a Fhem object from its json object representation.
        /// </summary>
        /// <param name="a_jsonObject">
        /// The json object that represents the Fhem object.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The json object may not be null.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// The json object must have 6 children
        /// </exception>
        /// <returns>
        /// The parses Fhem object.
        /// </returns>
        public static FhemObject FromJObject(JObject a_jsonObject)
        {
            //-- Validate argument
            if (a_jsonObject == null)
            {
                throw new ArgumentNullException("The json object may not be null!");
            }

            if (a_jsonObject.Count != 6)
            {
                throw new ArgumentOutOfRangeException("The json object must have 6 children!");
            }

            //-- Create the new fhem object
            var me = new FhemObject();

            //-- Analyze the children
            foreach (var jsonProperty in a_jsonObject.Children <JProperty>())
            {
                switch (jsonProperty.Name)
                {
                    #region case "Attributes":

                case "Attributes":

                    //-- Get the Json object that contains the attributes
                    var attributesAsJsonObject = (JObject)jsonProperty.First;

                    //-- Prepare a dictionary
                    var attributes = new Dictionary <string, string>(attributesAsJsonObject.Count);

                    foreach (var jsonAttribute in attributesAsJsonObject.Children <JProperty>())
                    {
                        attributes.Add(jsonAttribute.Name, (string)jsonAttribute.Value);
                    }

                    //-- Wrap the dictionary with a readonly dictionary
                    me.Attributes = new ReadOnlyDictionary <string, string>(attributes);

                    break;

                    //-- case "Attributes":
                    #endregion

                    #region case "Internals":

                case "Internals":

                    //-- Get the Json object that contains the attributes
                    var internalsAsJsonObject = (JObject)jsonProperty.First;

                    //-- Prepare a dictionary
                    var internals = new Dictionary <string, string>(internalsAsJsonObject.Count);

                    foreach (var jsonInternal in internalsAsJsonObject.Children <JProperty>())
                    {
                        internals.Add(jsonInternal.Name, (string)jsonInternal.Value);
                    }

                    //-- Wrap the dictionary with a readonly dictionary
                    me.Internals = new ReadOnlyDictionary <string, string>(internals);

                    break;

                    //-- case "Internals":
                    #endregion

                case "Name": me.Name = (string)jsonProperty.Value; break;

                case "PossibleAttrs": me.PossibleAttributes = FhemPossibleAttributesCollection.Parse((string)jsonProperty.Value); break;

                    #region case "PossibleSets":

                case "PossibleSets":

                    //-- Get the string that contains all possible sets
                    var encodedPossibleSets = (string)jsonProperty.Value;

                    //-- Just separate the sets
                    me.PossibleSets = encodedPossibleSets.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    //-- Sort the result
                    Array.Sort(me.PossibleSets);

                    break;

                    //-- case "PossibleSets":
                    #endregion

                case "Readings": me.Readings = FhemReadingItemsCollection.FromJObject((JObject)jsonProperty.First); break;

                default: break;
                }
            }

            //-- Initialize properties
            me.ID = int.Parse(me.Internals[INTERNALS_ID_TAG]);

            //-- Store the 'state' in an own property
            if (me.Internals.ContainsKey(INTERNALS_STATE_TAG))
            {
                me.State = me.Internals[INTERNALS_STATE_TAG];
            }

            return(me);
        }