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 ComponentSendTelemetry()
        {
            await comp.SendTelemetryValueAsync("{t1:2}");

            Assert.Equal("application/json", mockClient.MessageSent.ContentType);
            Assert.Equal("utf-8", mockClient.MessageSent.ContentEncoding);
            Assert.Equal("c1", mockClient.MessageSent.Properties["$.sub"]);
            Assert.Equal("{t1:2}", Encoding.UTF8.GetString(mockClient.MessageSent.GetBytes()));
        }
    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();
        }
      });
    }
Ejemplo n.º 4
0
        private async Task ProcessTempUpdateAsync(double targetTemp)
        {
            logger.LogWarning($"Ajusting temp from {CurrentTemperature} to {targetTemp}");
            // gradually increase current temp to target temp
            double step = (targetTemp - CurrentTemperature) / 10d;

            for (int i = 9; i >= 0; i--)
            {
                CurrentTemperature = targetTemp - step * (double)i;
                await tempSensor.SendTelemetryValueAsync(JsonConvert.SerializeObject(new { temperature = CurrentTemperature }));

                await tempSensor.ReportPropertyAsync("currentTemperature", CurrentTemperature);

                await Task.Delay(1000);
            }
            logger.LogWarning($"Adjustment complete");
        }
Ejemplo n.º 5
0
        private async Task ProcessTempUpdateAsync(double targetTemp)
        {
            logger.LogWarning($"Ajusting temp from {CurrentTemperature} to {targetTemp}");
            // gradually increase current temp to target temp
            double step = (targetTemp - CurrentTemperature) / 10d;

            for (int i = 9; i >= 0; i--)
            {
                CurrentTemperature = targetTemp - step * i;
                await component.SendTelemetryValueAsync("{temperature:" + CurrentTemperature + "}");

                await component.ReportPropertyAsync("currentTemperature", CurrentTemperature);

                await Task.Delay(1000);
            }
            logger.LogWarning($"Adjustment complete");
        }
        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);
                }
            });
        }