public async Task <string> GetMessageAsync(string modelId, string modelPath)
        {
            string messageString = string.Empty;

            var modelContainer = await DTDLHelper.GetModelsAndBuildDynamicContentAsync(modelId, modelPath);

            if (modelContainer == null)
            {
                throw new Exception($"No model container has been found corresponding to the parameters provided:: modelId: {modelId} - modelPath: {modelPath}");
            }

            //var modelContent = modelContainer.SingleOrDefault(i => i.Key == modelId);
            var contentWithTelemetries = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Telemetries != null);

            if (contentWithTelemetries != null && contentWithTelemetries.Any())
            {
                JArray telemetries = new JArray();
                foreach (var item in contentWithTelemetries)
                {
                    foreach (var telemetry in item.Value.DTDLGeneratedData.Telemetries)
                    {
                        telemetries.Add(telemetry);
                    }
                }

                messageString = JsonConvert.SerializeObject(telemetries, Formatting.Indented);
            }
            else
            {
                throw new ArgumentException($"No telemetry has been built from the provided model::modelId: {modelId} - modelPath: {modelPath}");
            }

            return(messageString);
        }
Beispiel #2
0
        public async Task GetModelsAndBuildDynamicContentAsync_Generic3_Components_Full_WithAdditionalItems_OK()
        {
            string dtdlModelPath = @"./Tests/Components/jmi.simulator.pnp.model.full.withAdditionalItems.json";
            string modelId       = "dtmi:com:jmi:simulator5;1";

            var modelContainer = await DTDLHelper.GetModelsAndBuildDynamicContentAsync(modelId, dtdlModelPath);

            Assert.IsNotNull(modelContainer);

            //Readable properties
            var data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.ReadableProperties != null);

            Assert.IsTrue(data.Any());

            //Telemetries
            data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Telemetries != null);
            Assert.IsTrue(data.Any());

            var rawModel = await DTDLHelper.GetDTDLFromModelIdAsync(modelId, dtdlModelPath);

            Assert.IsNotNull(rawModel);

            JArray arrayModel = null;

            if (rawModel is JObject)
            {
                arrayModel = new JArray();
                arrayModel.Add(rawModel);
            }
            else if (rawModel is JArray)
            {
                arrayModel = rawModel as JArray;
            }

            //We control the number of models / components with telemetries
            var components = arrayModel.Single(i => i.Value <string>("@id") == modelId)["contents"].Where(i => i.Value <string>("@type").ToLower() == "component");

            Assert.IsNotNull(components);
            Assert.IsTrue(components.Any());

            var modelsWithTelemetries = arrayModel.Where(i => ((JArray)i["contents"]).Count(i => i.Value <string>("@type").ToLower() == "telemetry") > 0);

            Assert.IsNotNull(modelsWithTelemetries);
            Assert.IsTrue(modelsWithTelemetries.Any());

            var actualModelsWithTelemetries = components.Join(modelsWithTelemetries, c => c.Value <string>("schema"), m => m.Value <string>("@id"), (c, m) => c);

            Assert.IsNotNull(actualModelsWithTelemetries);

            Assert.IsTrue(data.Count() == actualModelsWithTelemetries.Count());

            //Commands
            data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Commands != null);
            Assert.IsTrue(data.Any());

            //Writable properties
            data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.WritableProperties != null);
            Assert.IsTrue(data.Any());
        }
        public async Task GetModelsAndBuildDynamicContentAsync_Generic3_ManyTelemetries_BadModelId_OK()
        {
            string dtdlModelPath = @"./Tests/jmi.simulator.pnp.model.generic3.json";
            string modelId       = "dtmi:com:jmi:simulator:devicemessages:error;1";

            var modelContainer = await DTDLHelper.GetModelsAndBuildDynamicContentAsync(modelId, dtdlModelPath);

            Assert.IsNull(modelContainer);
        }
        public async Task GetModelsAndBuildDynamicContentAsync_Generic3_ManyTelemetries_OK()
        {
            string dtdlModelPath = @"./Tests/jmi.simulator.pnp.model.generic3.json";
            string modelId       = "dtmi:com:jmi:simulator:devicemessages;1";

            var modelContainer = await DTDLHelper.GetModelsAndBuildDynamicContentAsync(modelId, dtdlModelPath);

            Assert.IsNotNull(modelContainer);
            Assert.IsTrue(modelContainer.Count > 0);

            Assert.IsNotNull(modelContainer[modelId].DTDLGeneratedData);
            Assert.IsNotNull(modelContainer[modelId].DTDLGeneratedData.Telemetries);
        }
        public async Task GetModelsAndBuildDynamicContentAsync_Generic2_OneTelemetry_Errors_OK()
        {
            string dtdlModelPath = @"./Tests/jmi.simulator.pnp.model.generic2-errors.json";
            string modelId       = "dtmi:com:jmi:simulator:devicemessages;1";

            var modelContainer = await DTDLHelper.GetModelsAndBuildDynamicContentAsync(modelId, dtdlModelPath);

            Assert.IsNotNull(modelContainer);
            Assert.IsTrue(modelContainer.Count > 0);

            Assert.IsNotNull(modelContainer[modelId].ParsingErrors);
            Assert.IsTrue(modelContainer[modelId].ParsingErrors.Count() > 0);
        }
        public async Task GetModelsAndBuildDynamicContentAsync_Thermostat_OK()
        {
            string dtdlModelPath = @"./Tests/thermostat.json";
            string modelId       = "dtmi:com:example:thermostat;1";

            var modelContainer = await DTDLHelper.GetModelsAndBuildDynamicContentAsync(modelId, dtdlModelPath);

            Assert.IsNotNull(modelContainer);
            Assert.IsTrue(modelContainer.Count > 0);

            Assert.IsNotNull(modelContainer[modelId].DTDLGeneratedData);
            Assert.IsNotNull(modelContainer[modelId].DTDLGeneratedData.Telemetries);
        }
Beispiel #7
0
        private async Task OnDesiredPropertyChange(TwinCollection desiredproperties, object usercontext)
        {
            string logPrefix = "c2dtwins".BuildLogPrefix();

            _logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::TWINS-PROPERTIES-DESIRED properties changes request notification.");

            if (desiredproperties != null)
            {
                _logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::TWINS-PROPERTIES-DESIRED::{JsonConvert.SerializeObject(desiredproperties, Formatting.Indented)}");

                //Check if the properties belong to the WritableProperties
                //https://docs.microsoft.com/en-us/azure/iot-central/core/concepts-telemetry-properties-commands

                var dtdlParsedMode = await DTDLHelper.GetAndParseDTDLAsync(_defaultModel.ModelId, _defaultModel.ModelPath);

                if (dtdlParsedMode != null && dtdlParsedMode.Any())
                {
                    //Writable properties
                    var writableProperties = dtdlParsedMode.Where(i => i.Value.EntityKind == DTEntityKind.Property && ((DTPropertyInfo)i.Value).Writable)?.Select(i => i.Value as DTPropertyInfo).AsQueryable();
                    if (writableProperties != null && writableProperties.Any())
                    {
                        JObject objectDesiredProperties = JObject.Parse(desiredproperties.ToJson(Formatting.Indented));
                        var     intersection            = writableProperties.Join <DTPropertyInfo, JProperty, string, DTPropertyInfo>(objectDesiredProperties.Properties(), w => w.Name, o => o.Name, (w, o) => w);

                        if (intersection != null && intersection.Any())
                        {
                            StringBuilder builder = new StringBuilder();
                            foreach (var item in intersection)
                            {
                                builder.Append($"Desired property:'{item.Name}'-Value:{desiredproperties[item.Name]}.");
                            }

                            _logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::TWINS-PROPERTIES-DESIRED INCLUDED IN DTDL-CHANGED: {builder.ToString()}");
                        }
                        else
                        {
                            _logger.LogWarning($"{logPrefix}::{_deviceSettings.ArtifactId}::TWINS-PROPERTIES-DESIRED::None of the properties sent by the solution is defined in the DTDL model.");
                        }
                    }
                }
            }
            else
            {
                _logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::TWINS-PROPERTIES-DESIRED properties change is emtpy.");
            }
        }
Beispiel #8
0
        public async Task MT3620StarterKit_Simple_OK()
        {
            string dtdlModelPath = @"./Tests/Avnet/mt3620starterkit-1.json";
            string modelId       = "dtmi:avnet:mt3620Starterkit;1";

            var modelContainer = await DTDLHelper.GetModelsAndBuildDynamicContentAsync(modelId, dtdlModelPath);

            Assert.IsNotNull(modelContainer);

            var data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.ReadableProperties != null);

            Assert.IsTrue(data.Any());

            data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Telemetries != null);
            Assert.IsTrue(data.Any());

            data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Commands != null);
            Assert.IsTrue(data.Any());

            data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.WritableProperties != null);
            Assert.IsTrue(data.Any());
        }
Beispiel #9
0
        public async Task GetModelsAndBuildDynamicContentAsync_Generic3_Components_WritableProperties_OK()
        {
            string dtdlModelPath = @"./Tests/Components/jmi.simulator.pnp.model.writableproperties.json";
            string modelId       = "dtmi:com:jmi:simulator5;1";

            var modelContainer = await DTDLHelper.GetModelsAndBuildDynamicContentAsync(modelId, dtdlModelPath);

            Assert.IsNotNull(modelContainer);

            var data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.ReadableProperties != null);

            Assert.IsTrue(!data.Any());

            data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Telemetries != null);
            Assert.IsTrue(!data.Any());

            data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Commands != null);
            Assert.IsTrue(!data.Any());

            data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.WritableProperties != null);
            Assert.IsTrue(data.Any());
        }
Beispiel #10
0
        public async Task <Dictionary <string, DTDLCommandContainer> > GetCommandsAsync(string modelId, string modelPath)
        {
            string logPrefix = "DTDLCommandService.GetCommandsAsync".BuildLogPrefix();

            return(await DTDLHelper.GetModelsAndExtratCommandsAsync(modelId, modelPath));
        }
Beispiel #11
0
        public async Task MT3620StarterKit_DTDL_With_Semantics_And_Units_OK()
        {
            string dtdlModelPath = @"./Tests/Avnet/mt3620starterkit-1.json";
            string modelId       = "dtmi:avnet:mt3620Starterkit;1";

            Assert.IsTrue(File.Exists(dtdlModelPath));

            var modelContainer = await DTDLHelper.GetModelsAndBuildDynamicContentAsync(modelId, dtdlModelPath);

            Assert.IsNotNull(modelContainer);

            var readablePropertiesGenerated = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.ReadableProperties != null);

            Assert.IsTrue(readablePropertiesGenerated.Any());

            var telemetriesGenerated = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Telemetries != null);

            Assert.IsTrue(telemetriesGenerated.Any());

            var commandsGenerated = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Commands != null);

            Assert.IsTrue(commandsGenerated.Any());

            var writablePropertiesGenerated = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.WritableProperties != null);

            Assert.IsTrue(writablePropertiesGenerated.Any());

            //Additional assets
            string stringModel = await File.ReadAllTextAsync(dtdlModelPath);

            Assert.IsTrue(!string.IsNullOrEmpty(stringModel));

            ModelParser modelParser = new ModelParser();
            IReadOnlyDictionary <Dtmi, DTEntityInfo> dtdl = await modelParser.ParseAsync(new List <string> {
                stringModel
            });

            Assert.IsNotNull(dtdl);
            Assert.IsNotNull(dtdl.Values);

            var interfaces = dtdl.Values.Where(i => i.EntityKind == DTEntityKind.Interface);

            Assert.IsNotNull(interfaces);

            //Telemetries
            var telemetriesParsed = dtdl.Values.Where(i => i.EntityKind == DTEntityKind.Telemetry);

            Assert.IsNotNull(telemetriesParsed);

            var generatedTelemetriesContainer = telemetriesGenerated.SingleOrDefault();

            Assert.IsNotNull(generatedTelemetriesContainer.Value);
            Assert.IsNotNull(generatedTelemetriesContainer.Value.DTDLGeneratedData);

            var generatedTelemetriesContent = generatedTelemetriesContainer.Value.DTDLGeneratedData.Telemetries;

            Assert.IsNotNull(generatedTelemetriesContent);
            Assert.IsTrue(generatedTelemetriesContent.Count() == telemetriesParsed.Count());

            //Commands
            var commandsParsed = dtdl.Values.Where(i => i.EntityKind == DTEntityKind.Command);

            Assert.IsNotNull(commandsParsed);

            var generatedCommandContainer = commandsGenerated.SingleOrDefault();

            Assert.IsNotNull(generatedCommandContainer.Value);
            Assert.IsNotNull(generatedCommandContainer.Value.DTDLGeneratedData);

            var generatedCommandsContent = generatedCommandContainer.Value.DTDLGeneratedData.Commands;

            Assert.IsNotNull(generatedCommandsContent);
            Assert.IsTrue(generatedCommandsContent.Count() == commandsParsed.Count());

            //Properties
            var propertiesParsed = dtdl.Values.Where(i => i.EntityKind == DTEntityKind.Property);

            Assert.IsNotNull(propertiesParsed);

            var generatedWritablePropertiesContainer = writablePropertiesGenerated.SingleOrDefault();

            Assert.IsNotNull(generatedWritablePropertiesContainer.Value);
            Assert.IsNotNull(generatedWritablePropertiesContainer.Value.DTDLGeneratedData);
            var generatedWritableContent = generatedWritablePropertiesContainer.Value.DTDLGeneratedData.WritableProperties;

            int propertiesCounter = generatedWritableContent != null ? generatedWritableContent.Count : 0;

            var generatedReadablePropertiesContainer = readablePropertiesGenerated.SingleOrDefault();

            Assert.IsNotNull(generatedReadablePropertiesContainer.Value);
            Assert.IsNotNull(generatedReadablePropertiesContainer.Value.DTDLGeneratedData);
            var generatedReadableContent = generatedReadablePropertiesContainer.Value.DTDLGeneratedData.ReadableProperties;

            propertiesCounter = propertiesCounter + ((generatedReadableContent != null) ? generatedReadableContent.Count : 0);

            Assert.IsTrue(propertiesCounter == propertiesParsed.Count());
        }