Skip to content

Instantly share code, notes, and snippets.

@rido-min

rido-min/mydevice.json

Last active Jul 27, 2020
Embed
What would you like to do?
PnP device
{
"@context": "dtmi:dtdl:context;2",
"@id": "dtmi:com:example:mydevice;1",
"@type": "Interface",
"displayName": "mydevice",
"contents": [
{
"@type": "Telemetry",
"name": "workingSet",
"schema": "double"
},
{
"@type": "Property",
"name": "refreshInterval",
"schema": "integer",
"writable": true
}
]
}
const dt = require('azure-iot-digitaltwins-service') // preview
const cs = 'HostName=IoTHub-Admin-ConnectionString'
const deviceId = 'mydevice01'
const dtclient = new dt.DigitalTwinServiceClient(new dt.IoTHubTokenCredentials(cs))
dtclient.getDigitalTwin(deviceId)
.then(dtresp => {
if (dtresp) {
const twin = dtresp._response.parsedBody
console.log(twin)
const modelId = twin.$metadata.$model
if (modelId === 'dtmi:com:example:mydevice;1') {
const patch = [{
op: 'add',
path: '/refreshInterval',
value: 1
}]
dtclient.updateDigitalTwin(deviceId, patch, dtresp.eTag)
.then(patchResp => {
console.log(patchResp)
})
}
}
})
using Microsoft.Azure.Amqp;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace pnp_blog_sample
{
class Program
{
static async Task Main(string[] args)
{
int refreshInterval = 5; //5s by default
string modelId = "dtmi:com:example:mydevice;1";
string connectionString = "<IoTHub-Device-Connection-String>";
var client = DeviceClient.CreateFromConnectionString(
connectionString,
TransportType.Mqtt,
new ClientOptions() { ModelId = modelId }
);
Console.WriteLine("client connected");
var twin = await client.GetTwinAsync();
if (twin.Properties.Desired.Contains(nameof(refreshInterval)))
{
JValue refreshIntervalProperty = twin.Properties.Desired[nameof(refreshInterval)];
refreshInterval = refreshIntervalProperty.Value<int>();
Console.WriteLine("Found refreshInterval:" + refreshInterval);
}
await client.SetDesiredPropertyUpdateCallbackAsync(async (desired, ctx) =>
{
if (desired.Contains(nameof(refreshInterval)))
{
JValue refreshIntervalProp = desired[nameof(refreshInterval)];
refreshInterval = refreshIntervalProp.Value<int>();
Console.WriteLine("Received refreshInterval:" + refreshInterval);
TwinCollection reportedProperties = new TwinCollection();
TwinCollection ackProps = new TwinCollection();
ackProps["value"] = refreshInterval;
ackProps["ac"] = 200;
ackProps["av"] = desired.Version;
ackProps["ad"] = "desired property received";
reportedProperties[nameof(refreshInterval)] = ackProps;
await client.UpdateReportedPropertiesAsync(reportedProperties);
}
}, null);
while (true)
{
var serializedTelemetry = JsonConvert.SerializeObject(
new { workingSet = Environment.WorkingSet }
);
var message = new Message(Encoding.UTF8.GetBytes(serializedTelemetry));
message.ContentType = "application/json";
message.ContentEncoding = "utf-8";
await client.SendEventAsync(message);
Console.WriteLine(serializedTelemetry + " " + DateTime.Now.ToLongTimeString());
await Task.Delay(refreshInterval * 1000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment