Skip to content

Commit d985daf

Browse files
authored
Merge pull request #3 from jonmikeli/type-array-fix
PR-Evolution to take into account types with unit notation ('type' of type array)
2 parents 6ae4e59 + fdf01a0 commit d985daf

File tree

7 files changed

+291
-8
lines changed

7 files changed

+291
-8
lines changed

sources/IoT.Simulator/IoT.DTDL.Tests/DTDLComponentTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ public async Task GetModelsAndBuildDynamicContentAsync_Generic3_Components_Telem
3636
Assert.IsTrue(!data.Any());
3737
}
3838

39+
[TestMethod()]
40+
[TestCategory("Components")]
41+
public async Task GetModelsAndBuildDynamicContentAsync_Generic3_Components_Telemetries_With_Units_OK()
42+
{
43+
string dtdlModelPath = @"./Tests/Components/jmi.simulator.pnp.model.telemetries-type-with-units.json";
44+
string modelId = "dtmi:com:jmi:simulator5;1";
45+
46+
var modelContainer = await DTDLHelper.GetModelsAndBuildDynamicContentAsync(modelId, dtdlModelPath);
47+
48+
Assert.IsNotNull(modelContainer);
49+
50+
var data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.ReadableProperties != null);
51+
Assert.IsTrue(!data.Any());
52+
53+
data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Telemetries != null);
54+
Assert.IsTrue(data.Any());
55+
56+
data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.Commands != null);
57+
Assert.IsTrue(!data.Any());
58+
59+
data = modelContainer.Where(i => i.Value != null && i.Value.DTDLGeneratedData != null && i.Value.DTDLGeneratedData.WritableProperties != null);
60+
Assert.IsTrue(!data.Any());
61+
}
62+
3963
[TestMethod()]
4064
[TestCategory("Components")]
4165
public async Task GetModelsAndBuildDynamicContentAsync_Generic3_Components_ReadableProperties_OK()

sources/IoT.Simulator/IoT.DTDL.Tests/IoT.DTDL.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<None Update="Tests\Components\jmi.simulator.pnp.model.full.withAdditionalItems.json">
2525
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2626
</None>
27+
<None Update="Tests\Components\jmi.simulator.pnp.model.telemetries-type-with-units.json">
28+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
29+
</None>
2730
<None Update="Tests\Components\jmi.simulator.pnp.model.telemetries.commands.json">
2831
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2932
</None>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[
2+
{
3+
"@id": "dtmi:com:jmi:simulator5;1",
4+
"@type": "Interface",
5+
"@context": "dtmi:dtdl:context;2",
6+
"displayName": "Simulator",
7+
"contents": [
8+
{
9+
"@type": "Component",
10+
"name": "deviceMessages",
11+
"schema": "dtmi:com:jmi:simulator:devicemessages;1"
12+
}
13+
]
14+
},
15+
{
16+
"@id": "dtmi:com:jmi:simulator:devicemessages;1",
17+
"@type": "Interface",
18+
"@context": "dtmi:dtdl:context;2",
19+
"displayName": "Device Messages",
20+
"contents": [
21+
{
22+
"@type": [ "Telemetry", "Temperature" ],
23+
"name": "temperature",
24+
"schema": "double",
25+
"unit": "degreeCelsius"
26+
},
27+
{
28+
"@type": "Telemetry",
29+
"name": "pressure",
30+
"schema": "float"
31+
},
32+
{
33+
"@type": "Telemetry",
34+
"name": "rpm",
35+
"schema": "integer"
36+
}
37+
]
38+
}
39+
]

sources/IoT.Simulator/IoT.DTDL/DTDLHelper.cs

Lines changed: 174 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public static async Task<IReadOnlyDictionary<Dtmi, DTEntityInfo>> GetAndParseDTD
257257
}
258258
else
259259
return null;
260-
260+
261261
}
262262

263263
#endregion
@@ -380,6 +380,23 @@ private static DTDLContainer BuildDynamicContent(JObject dtdl)
380380
result.DTDLGeneratedData.Telemetries = telemetries;
381381
}
382382

383+
telemetries = ExtractTelemetriesWithUnit(contents);
384+
if (telemetries != null && telemetries.Any())
385+
{
386+
if (result.DTDLGeneratedData == null)
387+
result.DTDLGeneratedData = new DTDLGeneratedData();
388+
389+
if (result.DTDLGeneratedData.Telemetries == null)
390+
result.DTDLGeneratedData.Telemetries = telemetries;
391+
else
392+
{
393+
foreach (var item in telemetries)
394+
{
395+
result.DTDLGeneratedData.Telemetries.Add(item);
396+
}
397+
}
398+
}
399+
383400
//Look for properties (JSON)
384401
JArray readableProperties = ExtractReadableProperties(contents);
385402
if (readableProperties != null && readableProperties.Any())
@@ -390,6 +407,23 @@ private static DTDLContainer BuildDynamicContent(JObject dtdl)
390407
result.DTDLGeneratedData.ReadableProperties = readableProperties;
391408
}
392409

410+
readableProperties = ExtractReadablePropertiesWithUnit(contents);
411+
if (readableProperties != null && readableProperties.Any())
412+
{
413+
if (result.DTDLGeneratedData == null)
414+
result.DTDLGeneratedData = new DTDLGeneratedData();
415+
416+
if (result.DTDLGeneratedData.ReadableProperties == null)
417+
result.DTDLGeneratedData.ReadableProperties = readableProperties;
418+
else
419+
{
420+
foreach (var item in readableProperties)
421+
{
422+
result.DTDLGeneratedData.ReadableProperties.Add(item);
423+
}
424+
}
425+
}
426+
393427
JArray writableProperties = ExtractWritableProperties(contents);
394428
if (writableProperties != null && writableProperties.Any())
395429
{
@@ -399,6 +433,23 @@ private static DTDLContainer BuildDynamicContent(JObject dtdl)
399433
result.DTDLGeneratedData.WritableProperties = writableProperties;
400434
}
401435

436+
writableProperties = ExtractWritablePropertiesWithUnit(contents);
437+
if (writableProperties != null && writableProperties.Any())
438+
{
439+
if (result.DTDLGeneratedData == null)
440+
result.DTDLGeneratedData = new DTDLGeneratedData();
441+
442+
if (result.DTDLGeneratedData.WritableProperties == null)
443+
result.DTDLGeneratedData.WritableProperties = writableProperties;
444+
else
445+
{
446+
foreach (var item in writableProperties)
447+
{
448+
result.DTDLGeneratedData.WritableProperties.Add(item);
449+
}
450+
}
451+
}
452+
402453
//Commands
403454
JArray commands = ExtractCommands(contents);
404455
if (commands != null && commands.Any())
@@ -414,7 +465,37 @@ private static DTDLContainer BuildDynamicContent(JObject dtdl)
414465
private static JArray ExtractTelemetries(JArray contents)
415466
{
416467
JArray result = null;
417-
var telemetries = contents.Where(i => i["@type"].Value<string>().ToLower() == "telemetry");
468+
var telemetries = contents.Where(i => i["@type"] is not JArray && i["@type"].Value<string>().ToLower() == "telemetry");
469+
if (telemetries != null && telemetries.Any())
470+
{
471+
result = new JArray();
472+
473+
JObject tmp = null;
474+
string tmpPropertyName = string.Empty;
475+
476+
Random random = new Random(DateTime.Now.Millisecond);
477+
foreach (var item in telemetries)
478+
{
479+
tmpPropertyName = item["name"].Value<string>();
480+
481+
tmp = new JObject();
482+
483+
JProperty jProperty = AddCreatedProperties(tmpPropertyName, item["schema"].Value<string>(), random);
484+
485+
if (jProperty != null)
486+
tmp.Add(jProperty);
487+
488+
result.Add(tmp);
489+
}
490+
}
491+
492+
return result;
493+
}
494+
495+
private static JArray ExtractTelemetriesWithUnit(JArray contents)
496+
{
497+
JArray result = null;
498+
var telemetries = contents.Where(i => i["@type"] is JArray && ((JArray)i["@type"])[0].Value<string>().ToLower() == "telemetry");
418499
if (telemetries != null && telemetries.Any())
419500
{
420501
result = new JArray();
@@ -449,6 +530,8 @@ private static JArray ExtractWritableProperties(JArray contents)
449530
i =>
450531
(i is JObject)
451532
&&
533+
(i["@type"] is not JArray)
534+
&&
452535
i["@type"].Value<string>().ToLower() == "property"
453536
&&
454537
((JObject)i).ContainsKey("writable") && i["writable"].Value<bool>()
@@ -481,13 +564,57 @@ private static JArray ExtractWritableProperties(JArray contents)
481564
return result;
482565
}
483566

567+
private static JArray ExtractWritablePropertiesWithUnit(JArray contents)
568+
{
569+
JArray result = null;
570+
571+
var properties = contents.Where(
572+
i =>
573+
(i is JObject)
574+
&&
575+
(i["@type"] is JArray)
576+
&&
577+
((JArray)i["@type"])[0].Value<string>().ToLower() == "property"
578+
&&
579+
((JObject)i).ContainsKey("writable") && i["writable"].Value<bool>()
580+
);
581+
582+
583+
if (properties != null && properties.Any())
584+
{
585+
result = new JArray();
586+
587+
JObject tmp = null;
588+
string tmpPropertyName = string.Empty;
589+
590+
Random random = new Random(DateTime.Now.Millisecond);
591+
foreach (var item in properties)
592+
{
593+
tmpPropertyName = item["name"].Value<string>();
594+
595+
tmp = new JObject();
596+
597+
JProperty jProperty = AddCreatedProperties(tmpPropertyName, item["schema"].Value<string>(), random);
598+
599+
if (jProperty != null)
600+
tmp.Add(jProperty);
601+
602+
result.Add(tmp);
603+
}
604+
}
605+
606+
return result;
607+
}
608+
484609
private static JArray ExtractReadableProperties(JArray contents)
485610
{
486611
JArray result = null;
487612
var properties = contents.Where(
488613
i =>
489614
(i is JObject)
490615
&&
616+
i["@type"] is not JArray
617+
&&
491618
i["@type"].Value<string>().ToLower() == "property"
492619
&&
493620
(
@@ -522,10 +649,53 @@ private static JArray ExtractReadableProperties(JArray contents)
522649
return result;
523650
}
524651

652+
private static JArray ExtractReadablePropertiesWithUnit(JArray contents)
653+
{
654+
JArray result = null;
655+
var properties = contents.Where(
656+
i =>
657+
(i is JObject)
658+
&&
659+
i["@type"] is JArray
660+
&&
661+
((JArray)i["@type"])[0].Value<string>().ToLower() == "property"
662+
&&
663+
(
664+
!((JObject)i).ContainsKey("writable")
665+
|
666+
(((JObject)i).ContainsKey("writable") && !i["writable"].Value<bool>())
667+
)
668+
);
669+
670+
if (properties != null && properties.Any())
671+
{
672+
result = new JArray();
673+
JObject tmp = null;
674+
string tmpPropertyName = string.Empty;
675+
676+
Random random = new Random(DateTime.Now.Millisecond);
677+
foreach (var item in properties)
678+
{
679+
tmpPropertyName = item["name"].Value<string>();
680+
681+
tmp = new JObject();
682+
683+
JProperty jProperty = AddCreatedProperties(tmpPropertyName, item["schema"].Value<string>(), random);
684+
685+
if (jProperty != null)
686+
tmp.Add(jProperty);
687+
688+
result.Add(tmp);
689+
}
690+
}
691+
692+
return result;
693+
}
694+
525695
private static JArray ExtractCommands(JArray contents)
526696
{
527697
JArray result = null;
528-
var commands = contents.Where(i => i["@type"].Value<string>().ToLower() == "command");
698+
var commands = contents.Where(i => i["@type"] is not JArray && i["@type"].Value<string>().ToLower() == "command");
529699
if (commands != null && commands.Any())
530700
{
531701
result = new JArray();
@@ -589,7 +759,7 @@ private static JArray ExtractCommands(JArray contents)
589759
private static JArray ExtractComponents(JArray contents)
590760
{
591761
JArray result = null;
592-
var properties = contents.Where(i => i["@type"].Value<string>().ToLower() == "component");
762+
var properties = contents.Where(i => i["@type"] is not JArray && i["@type"].Value<string>().ToLower() == "component");
593763
if (properties != null && properties.Any())
594764
result = JArray.FromObject(properties);
595765

sources/IoT.Simulator/IoT.DTDL/IoT.DTDL.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
<Version>0.3.0.0</Version>
5+
<Version>0.3.2.0</Version>
66
<Authors>Jon Mikel Inza</Authors>
77
<Description>Basic DTDL v2 reader/parser to help to work in IoT Plug and Play solutions.</Description>
88
<Company>JMI</Company>
99
<Copyright>Jon Mikel Inza</Copyright>
1010
<PackageTags>IoT Plug and Play, IoT PnP, DTDL</PackageTags>
1111
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1212
<RepositoryUrl>https://github.com/jonmikeli/azureiotdevicesimulator5-pnp</RepositoryUrl>
13+
<AssemblyVersion>0.3.2.0</AssemblyVersion>
14+
<FileVersion>0.3.2.0</FileVersion>
1315
</PropertyGroup>
1416

1517
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[
2+
{
3+
"@id": "dtmi:com:jmi:simulator5;1",
4+
"@type": "Interface",
5+
"@context": "dtmi:dtdl:context;2",
6+
"displayName": "Simulator",
7+
"contents": [
8+
{
9+
"@type": "Component",
10+
"name": "deviceMessages",
11+
"schema": "dtmi:com:jmi:simulator:devicemessages;1"
12+
}
13+
]
14+
},
15+
{
16+
"@id": "dtmi:com:jmi:simulator:devicemessages;1",
17+
"@type": "Interface",
18+
"@context": "dtmi:dtdl:context;2",
19+
"displayName": "Device Messages",
20+
"contents": [
21+
{
22+
"@type": [ "Telemetry", "Temperature" ],
23+
"name": "temperature",
24+
"schema": "double",
25+
"unit": "degreeCelsius"
26+
},
27+
{
28+
"@type": "Telemetry",
29+
"name": "pressure",
30+
"schema": "float"
31+
},
32+
{
33+
"@type": "Telemetry",
34+
"name": "rpm",
35+
"schema": "integer"
36+
}
37+
]
38+
}
39+
]

0 commit comments

Comments
 (0)