Skip to content

Commit

Permalink
Merge pull request Azure-Samples#52 from Azure-Samples/feature/update…
Browse files Browse the repository at this point in the history
…ToGANuget

Feature/update to pre-ga nuget
  • Loading branch information
David R. Williamson authored Oct 30, 2020
2 parents 261704f + a0373bc commit 59c3265
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 101 deletions.
143 changes: 69 additions & 74 deletions AdtSampleApp/SampleClientApp/CommandLoop.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Azure;
using Azure.DigitalTwins.Core;
using Azure.DigitalTwins.Core.Serialization;
using Microsoft.Azure.DigitalTwins.Parser;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -54,10 +53,10 @@ public async Task CommandCreateModels(string[] cmd)
r.Close();
dtdlList.Add(dtdl);
}
Response<ModelData[]> res = await client.CreateModelsAsync(dtdlList);
Response<DigitalTwinsModelData[]> res = await client.CreateModelsAsync(dtdlList);
Log.Ok($"Model(s) created successfully!");
foreach (ModelData md in res.Value)
LogResponse(md.Model);
foreach (DigitalTwinsModelData md in res.Value)
LogResponse(md.DtdlModel);
}
catch (RequestFailedException e)
{
Expand Down Expand Up @@ -109,7 +108,7 @@ public async Task CommandGetModels(string[] cmd)
{
includeModelDefinitions = bool.Parse(cmd[1]);
}
catch (Exception e)
catch (Exception)
{
Log.Error("If you specify more than one parameter, your second parameter needs to be a boolean (return full model yes/no)");
}
Expand All @@ -121,13 +120,18 @@ public async Task CommandGetModels(string[] cmd)
Log.Alert($"Submitting...");
try
{
var reslist = new List<ModelData>();
AsyncPageable<ModelData> results = client.GetModelsAsync(dependenciesFor, includeModelDefinitions);
await foreach (ModelData md in results)
AsyncPageable<DigitalTwinsModelData> results = client.GetModelsAsync(
new GetModelsOptions
{
DependenciesFor = dependenciesFor,
IncludeModelDefinition = includeModelDefinitions
});
var reslist = new List<DigitalTwinsModelData>();
await foreach (DigitalTwinsModelData md in results)
{
Log.Out(md.Id);
if (md.Model != null)
LogResponse(md.Model);
if (md.DtdlModel != null)
LogResponse(md.DtdlModel);
reslist.Add(md);
}
Log.Out("");
Expand Down Expand Up @@ -157,8 +161,8 @@ public async Task CommandGetModel(string[] cmd)
Log.Alert($"Submitting...");
try
{
Response<ModelData> res = await client.GetModelAsync(modelId);
LogResponse(res.Value.Model);
Response<DigitalTwinsModelData> res = await client.GetModelAsync(modelId);
LogResponse(res.Value.DtdlModel);
}
catch (RequestFailedException e)
{
Expand Down Expand Up @@ -206,14 +210,14 @@ public async Task CommandDeleteAllModels(string[] cmd)
try
{
var reslist = new List<string>();
AsyncPageable<ModelData> results = client.GetModelsAsync(null, true);
await foreach (ModelData md in results)
AsyncPageable<DigitalTwinsModelData> results = client.GetModelsAsync(new GetModelsOptions { IncludeModelDefinition = true });
await foreach (DigitalTwinsModelData md in results)
{
Log.Out(md.Id);
if (md.Model != null)
if (md.DtdlModel != null)
{
Log.Out(md.Id);
reslist.Add(md.Model);
reslist.Add(md.DtdlModel);
}
}
Log.Out("");
Expand Down Expand Up @@ -318,22 +322,22 @@ public async Task CommandQuery(string[] cmd)
query = sb.ToString();
}
Log.Alert($"Submitting query: {query}...");
List<string> reslist = await Query(query);
List<BasicDigitalTwin> reslist = await Query(query);
if (reslist != null)
{
foreach (string item in reslist)
LogResponse(item);
foreach (BasicDigitalTwin item in reslist)
LogResponse(JsonSerializer.Serialize(item));
}
Log.Out("End Query");
}

private async Task<List<string>> Query(string query)
private async Task<List<BasicDigitalTwin>> Query(string query)
{
try
{
AsyncPageable<string> qresult = client.QueryAsync(query);
var reslist = new List<string>();
await foreach (string item in qresult)
AsyncPageable<BasicDigitalTwin> qresult = client.QueryAsync<BasicDigitalTwin>(query);
var reslist = new List<BasicDigitalTwin>();
await foreach (BasicDigitalTwin item in qresult)
reslist.Add(item);
return reslist;
}
Expand Down Expand Up @@ -377,13 +381,13 @@ public async Task CommandCreateDigitalTwin(string[] cmd)

for (int i = 0; i < args.Length; i += 3)
{
twinData.CustomProperties.Add(args[i], ConvertStringToType(args[i + 1], args[i + 2]));
twinData.Contents.Add(args[i], ConvertStringToType(args[i + 1], args[i + 2]));
}
Log.Alert($"Submitting...");

try
{
await client.CreateDigitalTwinAsync(twinData.Id, JsonSerializer.Serialize(twinData));
await client.CreateOrReplaceDigitalTwinAsync<BasicDigitalTwin>(twinData.Id, twinData);
Log.Ok($"Twin '{twinId}' created successfully!");
}
catch (RequestFailedException e)
Expand Down Expand Up @@ -439,9 +443,9 @@ public async Task CommandGetDigitalTwin(string[] cmd)
Log.Alert($"Submitting...");
try
{
Response<string> res = await client.GetDigitalTwinAsync(twinId);
Response<BasicDigitalTwin> res = await client.GetDigitalTwinAsync<BasicDigitalTwin>(twinId);
if (res != null)
LogResponse(res.Value);
LogResponse(System.Text.Json.JsonSerializer.Serialize(res.Value));
}
catch (RequestFailedException e)
{
Expand Down Expand Up @@ -471,21 +475,29 @@ public async Task CommandUpdateDigitalTwin(string[] cmd)
return;
}

var twinData = new List<object>();
var updateTwinData = new JsonPatchDocument();
for (int i = 0; i < args.Length; i += 4)
{
twinData.Add(
new Dictionary<string, object>
{
{ "op", args[i] },
{ "path", args[i + 1] },
{ "value", ConvertStringToType(args[i + 2], args[i + 3]) }
});
switch (args[i])
{
case "add":
updateTwinData.AppendAdd(args[i + 1], ConvertStringToType(args[i + 2], args[i + 3]));
break;

case "replace":
updateTwinData.AppendReplace(args[i + 1], ConvertStringToType(args[i + 2], args[i + 3]));
break;

case "remove":
updateTwinData.AppendRemove(args[i + 1]);
break;
}
}

Log.Alert($"Submitting...");
try
{
await client.UpdateDigitalTwinAsync(twinId, JsonSerializer.Serialize(twinData));
await client.UpdateDigitalTwinAsync(twinId, updateTwinData);
Log.Ok($"Twin '{twinId}' updated successfully!");
}
catch (RequestFailedException e)
Expand Down Expand Up @@ -536,14 +548,14 @@ public async Task CommandCreateRelationship(string[] cmd)
{
for (int i = 0; i < args.Length; i += 3)
{
relationship.CustomProperties.Add(args[i], ConvertStringToType(args[i + 1], args[i + 2]));
relationship.Properties.Add(args[i], ConvertStringToType(args[i + 1], args[i + 2]));
}
}

Log.Out($"Submitting...");
try
{
await client.CreateRelationshipAsync(sourceTwinId, relationshipId, JsonSerializer.Serialize(relationship));
await client.CreateOrReplaceRelationshipAsync(sourceTwinId, relationshipId, relationship);
Log.Ok($"Relationship {relationshipId} of type {relationshipName} created successfully from {sourceTwinId} to {targetTwinId}!");
}
catch (RequestFailedException e)
Expand Down Expand Up @@ -599,10 +611,10 @@ public async Task CommandGetRelationships(string[] cmd)
Log.Alert($"Submitting...");
try
{
AsyncPageable<string> res = client.GetRelationshipsAsync(sourceTwinId);
await foreach (string s in res)
AsyncPageable<BasicRelationship> relationships = client.GetRelationshipsAsync<BasicRelationship>(sourceTwinId);
await foreach (BasicRelationship relationship in relationships)
{
LogResponse(s);
LogResponse(JsonSerializer.Serialize(relationship));
}
}
catch (RequestFailedException e)
Expand Down Expand Up @@ -631,9 +643,9 @@ public async Task CommandGetRelationshipAsync(string[] cmd)
Log.Alert($"Submitting...");
try
{
Response<string> res = await client.GetRelationshipAsync(sourceTwinId, relationshipId);
Response<BasicRelationship> res = await client.GetRelationshipAsync<BasicRelationship>(sourceTwinId, relationshipId);
if (res != null)
LogResponse(res.Value);
LogResponse(JsonSerializer.Serialize(res.Value));
}
catch (RequestFailedException e)
{
Expand Down Expand Up @@ -685,7 +697,7 @@ public async Task CommandCreateEventRoute(string[] cmd)
}

string routeId = cmd[1];
var er = new EventRoute(cmd[2]);
var er = new DigitalTwinsEventRoute(cmd[2], cmd[3]);

var sb = new StringBuilder();
for (int i = 3; i < cmd.Length; i++)
Expand All @@ -694,7 +706,7 @@ public async Task CommandCreateEventRoute(string[] cmd)
Log.Alert($"Submitting...");
try
{
await client.CreateEventRouteAsync(routeId, er);
await client.CreateOrReplaceEventRouteAsync(routeId, er);
Log.Ok("Command completed");
}
catch (RequestFailedException e)
Expand Down Expand Up @@ -722,7 +734,7 @@ public async Task CommandGetEventRoute(string[] cmd)
Log.Alert($"Submitting...");
try
{
Response<EventRoute> res = await client.GetEventRouteAsync(routeId);
Response<DigitalTwinsEventRoute> res = await client.GetEventRouteAsync(routeId);
if (res != null && res.Value != null)
{
Log.Out($"Route {res.Value.Id} to {res.Value.EndpointName}");
Expand All @@ -748,8 +760,8 @@ public async Task CommandGetEventRoutes(string[] cmd)
Log.Alert($"Submitting...");
try
{
AsyncPageable<EventRoute> res = client.GetEventRoutesAsync();
await foreach (EventRoute er in res)
AsyncPageable<DigitalTwinsEventRoute> res = client.GetEventRoutesAsync();
await foreach (DigitalTwinsEventRoute er in res)
{
Log.Out($"Route {er.Id} to {er.EndpointName}");
Log.Out($" Filter: {er.Filter}");
Expand Down Expand Up @@ -800,13 +812,12 @@ public async Task FindAndDeleteOutgoingRelationshipsAsync(string dtId)
try
{
// GetRelationshipsAsync will throw if an error occurs
AsyncPageable<string> relsJson = client.GetRelationshipsAsync(dtId);
AsyncPageable<BasicRelationship> relationships = client.GetRelationshipsAsync<BasicRelationship>(dtId);

await foreach (string relJson in relsJson)
await foreach (BasicRelationship relationship in relationships)
{
var rel = JsonSerializer.Deserialize<BasicRelationship>(relJson);
await client.DeleteRelationshipAsync(dtId, rel.Id);
Log.Ok($"Deleted relationship {rel.Id} from {dtId}");
await client.DeleteRelationshipAsync(dtId, relationship.Id);
Log.Ok($"Deleted relationship {relationship.Id} from {dtId}");
}
}
catch (RequestFailedException ex)
Expand Down Expand Up @@ -843,26 +854,10 @@ public async Task DeleteAllTwinsAsync()
List<string> twinList = new List<string>();
try
{
AsyncPageable<string> queryResult = client.QueryAsync("SELECT * FROM DIGITALTWINS");
await foreach (string item in queryResult)
AsyncPageable<BasicDigitalTwin> queryResult = client.QueryAsync<BasicDigitalTwin>("SELECT * FROM DIGITALTWINS");
await foreach (BasicDigitalTwin item in queryResult)
{
JsonDocument document = JsonDocument.Parse(item);
if (document.RootElement.TryGetProperty("$dtId", out JsonElement eDtdl))
{
try
{
string twinId = eDtdl.GetString();
twinList.Add(twinId);
}
catch (Exception)
{
Log.Error("No DTDL property in query result");
}
}
else
{
Log.Error($"Error: Can't find twin id in query result:\n {item}");
}
twinList.Add(item.Id);
}
}
catch (Exception ex)
Expand Down Expand Up @@ -1086,9 +1081,9 @@ private void CheckState(object state)
{
try
{
Response<string> res0 = client.GetDigitalTwin(ts.Arguments[i]);
Response<BasicDigitalTwin> res0 = client.GetDigitalTwin<BasicDigitalTwin>(ts.Arguments[i]);
if (res0 != null)
LogProperty(res0.Value, ts.Arguments[i + 1]);
LogProperty(JsonSerializer.Serialize(res0.Value), ts.Arguments[i + 1]);
}
catch (RequestFailedException e)
{
Expand Down
5 changes: 3 additions & 2 deletions AdtSampleApp/SampleClientApp/SampleClientApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.DigitalTwins.Core" Version="1.0.0-preview.3" />
<PackageReference Include="Azure.Identity" Version="1.2.3" />
<!-- This pre-GA package version is available at https://dev.azure.com/azure-sdk/public/_packaging?_a=package&feed=azure-sdk-for-net&view=overview&package=Azure.DigitalTwins.Core&version=1.0.0-alpha.20201030.1&protocolType=NuGet -->
<PackageReference Include="Azure.DigitalTwins.Core" Version="1.0.0-alpha.20201030.1" />
<PackageReference Include="Azure.Identity" Version="1.2.3" />
<PackageReference Include="Microsoft.Azure.DigitalTwins.Parser" Version="3.12.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.9" />
</ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions AdtSampleApp/SampleClientApp/serviceConfig.json.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tenantId": "",
"clientId": "",
"instanceUrl": ""
}
20 changes: 7 additions & 13 deletions AdtSampleApp/SampleFunctionsApp/AdtUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Azure;
using Azure.DigitalTwins.Core;
using Azure.DigitalTwins.Core.Serialization;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;

namespace SampleFunctionsApp
Expand Down Expand Up @@ -40,12 +37,10 @@ public static async Task<string> FindParentByQueryAsync(DigitalTwinsClient clien

try
{
AsyncPageable<string> res = client.QueryAsync(query);

await foreach (string s in res)
AsyncPageable<BasicDigitalTwin> twins = client.QueryAsync<BasicDigitalTwin>(query);
await foreach (BasicDigitalTwin twin in twins)
{
JObject parentTwin = (JObject)JsonConvert.DeserializeObject(s);
return (string)parentTwin["Parent"]["$dtId"];
return twin.Id;
}
log.LogWarning($"*** No parent found");
}
Expand All @@ -61,12 +56,11 @@ public static async Task UpdateTwinPropertyAsync(DigitalTwinsClient client, stri
// If the twin does not exist, this will log an error
try
{
var uou = new UpdateOperationsUtility();
uou.AppendReplaceOp(propertyPath, value);
string patchPayload = uou.Serialize();
log.LogInformation($"UpdateTwinPropertyAsync sending {patchPayload}");
var updateTwinData = new JsonPatchDocument();
updateTwinData.AppendAdd(propertyPath, value);

await client.UpdateDigitalTwinAsync(twinId, patchPayload);
log.LogInformation($"UpdateTwinPropertyAsync sending {updateTwinData}");
await client.UpdateDigitalTwinAsync(twinId, updateTwinData);
}
catch (RequestFailedException exc)
{
Expand Down
Loading

0 comments on commit 59c3265

Please sign in to comment.