public void ClientDemo()
        {
            var client = new DigitalTwinsClient();

            DigitalTwin twin = client.GetTwin();                  // creates DigitalTwin instance and stores the JSON payload in it, i.e. very cheap
            string      json = twin.ToString();                   // gets the origin JSON payload.

            Assert.IsTrue(ReferenceEquals(s_demo_payload, json)); // the payload is really the same string as was passed into DigitalTwin ctor

            Assert.AreEqual("ID0001", twin.Id);                   // this is where the JSON string is parsed (lazily)
            Assert.AreEqual(123, twin.CreatedAt);
            Assert.AreEqual(true, twin.Decomissioned);

            // Temperature and Unit are not on DigitaTwin (they are user defined properties), so let's use dynamic APIs.
            dynamic dynamic = twin;

            Assert.AreEqual(72, dynamic.Temperature);
            Assert.AreEqual("F", dynamic.Unit);
            Assert.AreEqual(123, dynamic.CreatedAt); // the service defined properties are also avaliable through dynamic calls.

            // the client also has strongly typed APIs
            TemperatureSensor sensor = client.GetTwin <TemperatureSensor>();

            Assert.AreEqual("F", sensor.Unit);
            Assert.AreEqual(72, sensor.Temperature);
            Assert.AreEqual("ID0001", sensor.Id);
            Assert.AreEqual(123, sensor.CreatedAt);
            Assert.AreEqual(true, sensor.Decomissioned);

            // Interestingly, the base twin type can be converted to user defined type
            sensor = twin.As <TemperatureSensor>();

            Assert.AreEqual("F", sensor.Unit);
            Assert.AreEqual(72, sensor.Temperature);

            Assert.AreEqual("ID0001", sensor.Id);
            Assert.AreEqual(123, sensor.CreatedAt);
            Assert.AreEqual(true, sensor.Decomissioned);
        }
Beispiel #2
0
        private DigitalTwinClient(Uri uri, IotServiceClientCredentials credentials, params DelegatingHandler[] handlers)
        {
            var httpMessageHandler = HttpClientHelper.CreateDefaultHttpMessageHandler(null, uri, ServicePointHelpers.DefaultConnectionLeaseTimeout);

#pragma warning disable CA2000 // Dispose objects before losing scope (httpMessageHandlerWithDelegatingHandlers is disposed when the http client owning it is disposed)
            HttpMessageHandler httpMessageHandlerWithDelegatingHandlers = CreateHttpHandlerPipeline(httpMessageHandler, handlers);
#pragma warning restore CA2000 // Dispose objects before losing scope

#pragma warning disable CA2000 // Dispose objects before losing scope (httpClient is disposed when the protocol layer client owning it is disposed)
            var httpClient = new HttpClient(httpMessageHandlerWithDelegatingHandlers, true)
            {
                BaseAddress = uri
            };
#pragma warning restore CA2000 // Dispose objects before losing scope

#pragma warning restore CA2000 // Dispose objects before losing scope

            // When this client is disposed, all the http message handlers and delegating handlers will be disposed automatically
            _client         = new IotHubGatewayServiceAPIs(credentials, httpClient, true);
            _client.BaseUri = uri;
            _protocolLayer  = new DigitalTwin(_client);
        }
 public TemperatureSensor(DigitalTwin twin) : base(twin.ToString())
 {
 }
Beispiel #4
0
 private DigitalTwinClient(Uri uri, IotServiceClientCredentials credentials, params DelegatingHandler[] handlers)
 {
     _client        = new IotHubGatewayServiceAPIs(uri, credentials, handlers);
     _protocolLayer = new DigitalTwin(_client);
 }