Ejemplo n.º 1
0
        public async Task RunDeviceAsync()
        {
            deviceClient = DeviceClient.CreateFromConnectionString(connectionString,
                                                                   TransportType.Mqtt, new ClientOptions {
                ModelId = modelId
            });

            component = new PnPComponent(deviceClient);

            await component.SetPnPDesiredPropertyHandlerAsync <double>("targetTemperature", root_tergetTemperature_UpdateHandler, this);

            await component.SetPnPCommandHandlerAsync("reboot", root_RebootCommandHadler, this);

            var targetTemperature = await component.ReadDesiredPropertyAsync <double>("targetTemperature");

            await this.ProcessTempUpdateAsync(targetTemperature);

            await Task.Run(async() =>
            {
                while (!quitSignal.IsCancellationRequested)
                {
                    await component.SendTelemetryValueAsync("{temperature:" + CurrentTemperature + "," +
                                                            " workingSet: " + Environment.WorkingSet + "}");

                    logger.LogInformation("Sending CurrentTemperature and workingset" + CurrentTemperature);
                    await Task.Delay(1000);
                }
            });
        }
Ejemplo n.º 2
0
        public async Task ComponentSetDesiredPropertyHandler()
        {
            string valueReaded = string.Empty;
            await comp.SetPnPDesiredPropertyHandlerAsync <string>("prop1", (object newValue) =>
            {
                valueReaded = newValue.ToString();
            }, this);

            TwinCollection desired = new TwinCollection(@"{c1: { __t: 'c', prop1: { value: 'val1'}}}");
            await mockClient.DesiredPropertyUpdateCallback(desired, this);

            Assert.Equal("val1", valueReaded);
            var reported = mockClient.ReportedCollection;

            Assert.Equal("{\"c1\":{\"__t\":\"c\",\"prop1\":{\"value\":\"val1\",\"ac\":200,\"av\":0,\"ad\":\"update complete\"}}}", reported.ToJson());
        }
    public async Task RunAsync(string connectionString, ILogger logger, CancellationToken quitSignal)
    {
    
      this.logger = logger;
      
      deviceClient = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Mqtt,
        new ClientOptions { ModelId = modelId });

      var refrigerator = new PnPComponent(deviceClient, logger);

      await refrigerator.SetPnPCommandHandlerAsync("Reset", async (MethodRequest req, object ctx) =>
      {
        logger.LogWarning("============> Processing Reset");
        MemoryLeak.FreeMemory();
        await refrigerator.ReportPropertyAsync("LastInitDateTime", DateTime.Now.ToUniversalTime());
        return await Task.FromResult(new MethodResponse(200));
      }, null);

      await refrigerator.SetPnPDesiredPropertyHandlerAsync<int>("RefreshInterval", (int newValue) =>
      {
        if (int.TryParse(newValue.ToString(), out int refreshInterval))
        {
          logger.LogWarning("=====================> RefreshInterval: " + refreshInterval);
          RefreshInterval = refreshInterval;
        }
      }, this);

      await Task.Run(async () =>
      {
        RefreshInterval = await refrigerator.ReadDesiredPropertyAsync<int>("RefreshInterval");
        if (RefreshInterval == default(int)) RefreshInterval = defaultRefreshInterval;

        await refrigerator.ReportPropertyAsync("SerialNumber", "1235435");
        await refrigerator.ReportPropertyAsync("LastInitDateTime", DateTime.Now.ToUniversalTime());

        int avg = 21;
        var rnd = new Random(Environment.TickCount);
        while (!quitSignal.IsCancellationRequested)
        {
          var payload = new
          {
            temp = avg + rnd.Next(10)
          };
          await refrigerator.SendTelemetryValueAsync(JsonConvert.SerializeObject(payload));
          logger.LogInformation("Sending CurrentTemperature: " + payload.temp);
          await Task.Delay(RefreshInterval * 1000);
          MemoryLeak.FillMemory();
        }
      });
    }
        public async Task RunAsync(string connectionString, ILogger logger, CancellationToken quitSignal)
        {
            this.logger = logger;

            deviceClient = DeviceClient.CreateFromConnectionString(connectionString,
                                                                   TransportType.Mqtt, new ClientOptions {
                ModelId = modelId
            });

            tempSensor = new PnPComponent(deviceClient, "tempSensor1", logger);
            diag       = new PnPComponent(deviceClient, "diag");
            deviceInfo = new PnPComponent(deviceClient, "deviceInfo");
            sdkInfo    = new PnPComponent(deviceClient, "sdkInfo");

            await deviceInfo.ReportPropertyCollectionAsync(DeviceInfo.ThisDeviceInfo.ToDictionary());

            await sdkInfo.ReportPropertyCollectionAsync(SdkInformation.ThisSdkInfo);

            await diag.SetPnPCommandHandlerAsync("reboot", Diag_RebootCommandHadler, this);

            await tempSensor.SetPnPDesiredPropertyHandlerAsync <double>("targetTemperature", tempSensor_tergetTemperature_UpdateHandler, this);

            var targetTemperature = await tempSensor.ReadDesiredPropertyAsync <double>("targetTemperature");

            await this.ProcessTempUpdateAsync(targetTemperature);

            await Task.Run(async() =>
            {
                logger.LogWarning("Entering Device Loop");
                while (!quitSignal.IsCancellationRequested)
                {
                    await tempSensor.SendTelemetryValueAsync(JsonConvert.SerializeObject(new { temperature = CurrentTemperature }));
                    await diag.SendTelemetryValueAsync(JsonConvert.SerializeObject(new { workingSet = Environment.WorkingSet }));

                    logger.LogInformation("Sending workingset and temp " + CurrentTemperature);
                    await Task.Delay(5000);
                }
            });
        }