Example #1
0
        public void UpdateUtilityAppendMultipleOperations()
        {
            var    op           = new UpdateOperationsUtility();
            string addPath      = "testPath1";
            int    addValue     = 10;
            string replacePath  = "testpath2";
            int    replaceValue = 20;

            op.AppendAddPropertyOp(addPath, addValue);
            op.AppendReplacePropertyOp(replacePath, replaceValue);
            string operations = op.Serialize();

            // There should be two operations added.
            var jArray = JArray.Parse(operations);

            jArray.Count.Should().Be(2);

            // The patch operation added should have an "add" and a "replace" operation.
            var expectedOperations = new List <string> {
                Add, Replace
            };
            var actualOperations = new List <string>();

            foreach (JObject item in jArray)
            {
                actualOperations.Add(item.Value <string>(Op));
            }
            actualOperations.Should().OnlyContain(item => expectedOperations.Contains(item));
        }
        private async Task UpdateDigitalTwinComponentPropertyAsync()
        {
            // Choose a random value to assign to the targetTemperature property in thermostat1 component
            int desiredTargetTemperature = Random.Next(0, 100);

            const string targetTemperaturePropertyName = "targetTemperature";
            var          updateOperation = new UpdateOperationsUtility();

            // First let's take a look at when the property was updated and what was it set to.
            HttpOperationResponse <TemperatureControllerTwin, DigitalTwinGetHeaders> getDigitalTwinResponse = await _digitalTwinClient
                                                                                                              .GetDigitalTwinAsync <TemperatureControllerTwin>(_digitalTwinId);

            ThermostatTwin thermostat1 = getDigitalTwinResponse.Body.Thermostat1;

            if (thermostat1 != null)
            {
                // Thermostat1 is present in the TemperatureController twin. We can add/replace the component-level property "targetTemperature"
                double?currentComponentTargetTemperature = getDigitalTwinResponse.Body.Thermostat1.TargetTemperature;
                if (currentComponentTargetTemperature != null)
                {
                    DateTimeOffset targetTemperatureDesiredLastUpdateTime = getDigitalTwinResponse.Body.Thermostat1.Metadata.TargetTemperature.LastUpdateTime;
                    _logger.LogDebug($"The property {targetTemperaturePropertyName} under component {Thermostat1Component} was last updated on `" +
                                     $"{targetTemperatureDesiredLastUpdateTime.ToLocalTime()} `" +
                                     $" with a value of {getDigitalTwinResponse.Body.Thermostat1.Metadata.TargetTemperature.DesiredValue}.");

                    // The property path to be replaced should be prepended with a '/'
                    updateOperation.AppendReplacePropertyOp($"/{Thermostat1Component}/{targetTemperaturePropertyName}", desiredTargetTemperature);
                }
                else
                {
                    _logger.LogDebug($"The property {targetTemperaturePropertyName} under component {Thermostat1Component} `" +
                                     $"was never set on the {_digitalTwinId} digital twin.");

                    // The property path to be added should be prepended with a '/'
                    updateOperation.AppendAddPropertyOp($"/{Thermostat1Component}/{targetTemperaturePropertyName}", desiredTargetTemperature);
                }
            }
            else
            {
                // Thermostat1 is not present in the TemperatureController twin. We will add the component
                var componentProperty = new Dictionary <string, object> {
                    { targetTemperaturePropertyName, desiredTargetTemperature }, { "$metadata", new object() }
                };
                _logger.LogDebug($"The component {Thermostat1Component} does not exist on the {_digitalTwinId} digital twin.");

                // The property path to be replaced should be prepended with a '/'
                updateOperation.AppendAddComponentOp($"/{Thermostat1Component}", componentProperty);
            }

            _logger.LogDebug($"Update the {targetTemperaturePropertyName} property under component {Thermostat1Component} on the {_digitalTwinId} `" +
                             $"digital twin to {desiredTargetTemperature}.");
            HttpOperationHeaderResponse <DigitalTwinUpdateHeaders> updateDigitalTwinResponse = await _digitalTwinClient
                                                                                               .UpdateDigitalTwinAsync(_digitalTwinId, updateOperation.Serialize());

            _logger.LogDebug($"Update {_digitalTwinId} digital twin response: {updateDigitalTwinResponse.Response.StatusCode}.");

            // Print the TemperatureController digital twin
            await GetAndPrintDigitalTwinAsync <TemperatureControllerTwin>();
        }
        private async Task UpdateCurrentTemperaturePropertyAsync()
        {
            // Choose a random value to assign to the currentTemperature property
            int currentTemperature = Random.Next(0, 100);

            const string currentTemperaturePropertyName = "currentTemperature";
            var          updateOperation = new UpdateOperationsUtility();

            // First, add the property to the digital twin
            updateOperation.AppendAddPropertyOp($"/{currentTemperaturePropertyName}", currentTemperature);
            _logger.LogDebug($"Add the {currentTemperaturePropertyName} property on the {_digitalTwinId} digital twin " +
                             $"with a value of {currentTemperature}.");
            HttpOperationHeaderResponse <DigitalTwinUpdateHeaders> addPropertyToDigitalTwinResponse = await _digitalTwinClient
                                                                                                      .UpdateDigitalTwinAsync(_digitalTwinId, updateOperation.Serialize());

            _logger.LogDebug($"Update {_digitalTwinId} digital twin response: {addPropertyToDigitalTwinResponse.Response.StatusCode}.");

            // Print the Thermostat digital twin
            await GetAndPrintDigitalTwinAsync <ThermostatTwin>();

            // Second, replace the property to a different value
            int newCurrentTemperature = Random.Next(0, 100);

            updateOperation.AppendReplacePropertyOp($"/{currentTemperaturePropertyName}", newCurrentTemperature);
            _logger.LogDebug($"Replace the {currentTemperaturePropertyName} property on the {_digitalTwinId} digital twin " +
                             $"with a value of {newCurrentTemperature}.");
            HttpOperationHeaderResponse <DigitalTwinUpdateHeaders> replacePropertyInDigitalTwinResponse = await _digitalTwinClient
                                                                                                          .UpdateDigitalTwinAsync(_digitalTwinId, updateOperation.Serialize());

            _logger.LogDebug($"Update {_digitalTwinId} digital twin response: {replacePropertyInDigitalTwinResponse.Response.StatusCode}.");

            // Print the Thermostat digital twin
            await GetAndPrintDigitalTwinAsync <ThermostatTwin>();

            // Third, remove the currentTemperature property
            updateOperation.AppendRemoveOp($"/{currentTemperaturePropertyName}");
            _logger.LogDebug($"Remove the {currentTemperaturePropertyName} property on the {_digitalTwinId} digital twin.");
            HttpOperationHeaderResponse <DigitalTwinUpdateHeaders> removePropertyInDigitalTwinResponse = await _digitalTwinClient
                                                                                                         .UpdateDigitalTwinAsync(_digitalTwinId, updateOperation.Serialize());

            _logger.LogDebug($"Update {_digitalTwinId} digital twin response: {removePropertyInDigitalTwinResponse.Response.StatusCode}.");

            // Print the Thermostat digital twin
            await GetAndPrintDigitalTwinAsync <ThermostatTwin>();
        }
Example #4
0
        public void UpdateUtilityAppendsReplacePropertyOp()
        {
            var    op    = new UpdateOperationsUtility();
            string path  = "testPath";
            int    value = 10;

            op.AppendReplacePropertyOp(path, value);
            string operations = op.Serialize();

            // There should be a single operation added.
            var jArray = JArray.Parse(operations);

            jArray.Count.Should().Be(1);

            // The patch operation added should be a "replace" operation.
            JToken jObject = jArray.First;

            jObject.Value <string>(Op).Should().Be(Replace);
        }
        private async Task UpdateTargetTemperaturePropertyAsync()
        {
            const string targetTemperaturePropertyName = "targetTemperature";
            var          updateOperation = new UpdateOperationsUtility();

            // Choose a random value to assign to the targetTemperature property
            int desiredTargetTemperature = Random.Next(0, 100);

            // First let's take a look at when the property was updated and what was it set to.
            HttpOperationResponse <ThermostatTwin, DigitalTwinGetHeaders> getDigitalTwinResponse = await _digitalTwinClient
                                                                                                   .GetDigitalTwinAsync <ThermostatTwin>(_digitalTwinId);

            double?currentTargetTemperature = getDigitalTwinResponse.Body.TargetTemperature;

            if (currentTargetTemperature != null)
            {
                DateTimeOffset targetTemperatureDesiredLastUpdateTime = getDigitalTwinResponse.Body.Metadata.TargetTemperature.LastUpdateTime;
                _logger.LogDebug($"The property {targetTemperaturePropertyName} was last updated on " +
                                 $"{targetTemperatureDesiredLastUpdateTime.ToLocalTime()} `" +
                                 $" with a value of {getDigitalTwinResponse.Body.Metadata.TargetTemperature.DesiredValue}.");

                // The property path to be replaced should be prepended with a '/'
                updateOperation.AppendReplacePropertyOp($"/{targetTemperaturePropertyName}", desiredTargetTemperature);
            }
            else
            {
                _logger.LogDebug($"The property {targetTemperaturePropertyName} was never set on the ${_digitalTwinId} digital twin.");

                // The property path to be added should be prepended with a '/'
                updateOperation.AppendAddPropertyOp($"/{targetTemperaturePropertyName}", desiredTargetTemperature);
            }

            _logger.LogDebug($"Update the {targetTemperaturePropertyName} property on the " +
                             $"{_digitalTwinId} digital twin to {desiredTargetTemperature}.");
            HttpOperationHeaderResponse <DigitalTwinUpdateHeaders> updateDigitalTwinResponse = await _digitalTwinClient
                                                                                               .UpdateDigitalTwinAsync(_digitalTwinId, updateOperation.Serialize());

            _logger.LogDebug($"Update {_digitalTwinId} digital twin response: {updateDigitalTwinResponse.Response.StatusCode}.");

            // Print the Thermostat digital twin
            await GetAndPrintDigitalTwinAsync <ThermostatTwin>();
        }