From 9ec5972cc4f6f422d1c8653a1211f87568181c23 Mon Sep 17 00:00:00 2001 From: David Lu Date: Thu, 30 Jan 2020 10:00:43 -0800 Subject: [PATCH] modified for telemetry explosion --- .../device/AndroidSample/app/build.gradle | 2 +- .../sample/EnvironmentalSensor.java | 63 +++++-------------- .../iot/digitaltwin/sample/MainActivity.java | 15 ++--- .../sdk/iot/digitaltwin/sample/UiHandler.java | 3 +- .../sample/EnvironmentalSensor.java | 58 +++++------------ 5 files changed, 40 insertions(+), 101 deletions(-) diff --git a/digital-twin/Samples/device/AndroidSample/app/build.gradle b/digital-twin/Samples/device/AndroidSample/app/build.gradle index 28a2968..e601749 100644 --- a/digital-twin/Samples/device/AndroidSample/app/build.gradle +++ b/digital-twin/Samples/device/AndroidSample/app/build.gradle @@ -35,7 +35,7 @@ dependencies { implementation 'com.google.android.material:material:1.0.0' implementation 'com.github.tony19:logback-android:2.0.0' implementation 'commons-io:commons-io:2.6' - implementation 'com.microsoft.azure.sdk.iot:digital-twin-device-client-preview:1.0.+' + implementation 'com.microsoft.azure.sdk.iot:digital-twin-device-client-preview:1.+' compileOnly 'org.projectlombok:lombok:1.18.10' annotationProcessor 'org.projectlombok:lombok:1.18.10' } diff --git a/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/EnvironmentalSensor.java b/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/EnvironmentalSensor.java index 37c0d53..ad5502d 100644 --- a/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/EnvironmentalSensor.java +++ b/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/EnvironmentalSensor.java @@ -32,6 +32,7 @@ import static com.microsoft.azure.sdk.iot.digitaltwin.device.serializer.JsonSerializer.deserialize; import static com.microsoft.azure.sdk.iot.digitaltwin.device.serializer.JsonSerializer.serialize; import static java.util.Collections.singletonList; +import static java.util.Collections.singletonMap; import static java.util.concurrent.TimeUnit.SECONDS; @Slf4j @@ -54,16 +55,13 @@ protected EnvironmentalSensor(@NonNull String digitalTwinComponentName, @NonNull this.uiHandler = uiHandler; } - public Single updateTemperatureAsync(double temperature) throws IOException { - log.debug("Temperature changed to {}.", temperature); - uiHandler.updateTemperature(temperature); - return sendTelemetryAsync(TELEMETRY_NAME_TEMPERATURE, serialize(temperature)); - } - - public Single updateHumidityAsync(double humidity) throws IOException { - log.debug("Humidity changed to {}.", humidity); - uiHandler.updateHumidity(humidity); - return sendTelemetryAsync(TELEMETRY_NAME_HUMIDITY, serialize(humidity)); + public Single updateTemperatureAndHumidityAsync(double temperature, double humidity) throws IOException { + log.info("Temperature changed to {}, Humidity changed to {}.", temperature, humidity); + uiHandler.updateTemperatureAndHumidity(temperature, humidity); + Map properties = new HashMap<>(); + properties.put(TELEMETRY_NAME_TEMPERATURE, temperature); + properties.put(TELEMETRY_NAME_HUMIDITY, humidity); + return sendTelemetryAsync(serialize(properties)); } public Single updateStatusAsync(final boolean state) { @@ -80,23 +78,20 @@ public Single updateStatusAsync(final boolean state) { public void ready() { super.ready(); final Random random = new Random(); - Disposable temperatureReportProcess= Single.just(random) + Disposable reportProcess = Single.just(random) .delay(10, SECONDS) - .map(new Function() { + .flatMap(new Function>() { @Override - public Double apply(Random random) { - return random.nextDouble() * 100; + public Single apply(Random random) throws IOException { + double temperature = random.nextDouble() * 100, humidity = random.nextDouble() * 100; + return updateTemperatureAndHumidityAsync(temperature, humidity); } - }).flatMap(new Function>() { - @Override - public Single apply(Double temperature) throws IOException { - return updateTemperatureAsync(temperature); - } - }).repeat() + }) + .repeat() .subscribe(new Consumer() { @Override public void accept(DigitalTwinClientResult result) { - log.debug("Update temperature was {}", result); + log.info("Update temperature was {}", result); } }, new Consumer() { @Override @@ -104,31 +99,7 @@ public void accept(Throwable throwable) { log.debug("Update temperature failed.", throwable); } }); - Disposable humidityReportProcess = Single.just(random) - .delay(10, SECONDS) - .map(new Function() { - @Override - public Double apply(Random random) { - return random.nextDouble() * 100; - } - }).flatMap(new Function>() { - @Override - public Single apply(Double humidity) throws IOException { - return updateHumidityAsync(humidity); - } - }).repeat() - .subscribe(new Consumer() { - @Override - public void accept(DigitalTwinClientResult result) { - log.debug("Update humidity was {}", result); - } - }, new Consumer() { - @Override - public void accept(Throwable throwable) { - log.debug("Update humidity failed.", throwable); - } - }); - log.debug("Once application quit, should dispose {} and {}.", temperatureReportProcess, humidityReportProcess); + log.debug("Once application quit, should dispose {}.", reportProcess); } @Override diff --git a/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/MainActivity.java b/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/MainActivity.java index 499a1a2..fdebad7 100644 --- a/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/MainActivity.java +++ b/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/MainActivity.java @@ -125,6 +125,7 @@ public void accept(Throwable throwable) { } } + @Override public void updateName(final String name) { runOnUiThread(new Runnable() { @Override @@ -134,6 +135,7 @@ public void run() { }); } + @Override public void updateBrightness(final double brightness) { runOnUiThread(new Runnable() { @Override @@ -143,19 +145,12 @@ public void run() { }); } - public void updateTemperature(final double temperature) { + @Override + public void updateTemperatureAndHumidity(final double temperature, final double humidity) { runOnUiThread(new Runnable() { @Override public void run() { temperatureView.setText(String.valueOf(temperature)); - } - }); - } - - public void updateHumidity(final double humidity) { - runOnUiThread(new Runnable() { - @Override - public void run() { humidityView.setText(String.valueOf(humidity)); } }); @@ -170,6 +165,7 @@ public void run() { }); } + @Override public void updateOnoff(final boolean on) { runOnUiThread(new Runnable() { @Override @@ -179,6 +175,7 @@ public void run() { }); } + @Override public void startBlink(final long interval) { runOnUiThread(new Runnable() { @Override diff --git a/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/UiHandler.java b/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/UiHandler.java index 7c4bb6a..9ed762d 100644 --- a/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/UiHandler.java +++ b/digital-twin/Samples/device/AndroidSample/app/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/UiHandler.java @@ -3,8 +3,7 @@ interface UiHandler { void updateName(String name); void updateBrightness(double brightness); - void updateTemperature(double temperature); - void updateHumidity(double humidity); + void updateTemperatureAndHumidity(double temperature, double humidity); void updateOnoff(boolean on); void startBlink(long interval); } diff --git a/digital-twin/Samples/device/JdkSample/environmental-sensor-sample/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/EnvironmentalSensor.java b/digital-twin/Samples/device/JdkSample/environmental-sensor-sample/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/EnvironmentalSensor.java index 4a318ab..393ba45 100644 --- a/digital-twin/Samples/device/JdkSample/environmental-sensor-sample/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/EnvironmentalSensor.java +++ b/digital-twin/Samples/device/JdkSample/environmental-sensor-sample/src/main/java/com/microsoft/azure/sdk/iot/digitaltwin/sample/EnvironmentalSensor.java @@ -30,6 +30,7 @@ import static com.microsoft.azure.sdk.iot.digitaltwin.device.serializer.JsonSerializer.deserialize; import static com.microsoft.azure.sdk.iot.digitaltwin.device.serializer.JsonSerializer.serialize; import static java.util.Collections.singletonList; +import static java.util.Collections.singletonMap; import static java.util.concurrent.TimeUnit.SECONDS; @Slf4j @@ -50,14 +51,12 @@ public EnvironmentalSensor(@NonNull String digitalTwinComponentName) { super(digitalTwinComponentName, ENVIRONMENTAL_SENSOR_INTERFACE_ID); } - public Single updateTemperatureAsync(double temperature) throws IOException { - log.info("Temperature changed to {}.", temperature); - return sendTelemetryAsync(TELEMETRY_NAME_TEMPERATURE, serialize(temperature)); - } - - public Single updateHumidityAsync(double humidity) throws IOException { - log.info("Humidity changed to {}.", humidity); - return sendTelemetryAsync(TELEMETRY_NAME_HUMIDITY, serialize(humidity)); + public Single updateTemperatureAndHumidityAsync(double temperature, double humidity) throws IOException { + log.info("Temperature changed to {}, Humidity changed to {}.", temperature, humidity); + Map properties = new HashMap<>(); + properties.put(TELEMETRY_NAME_TEMPERATURE, temperature); + properties.put(TELEMETRY_NAME_HUMIDITY, humidity); + return sendTelemetryAsync(serialize(properties)); } public Single updateStatusAsync(final boolean state) { @@ -73,19 +72,16 @@ public Single updateStatusAsync(final boolean state) { public void ready() { super.ready(); final Random random = new Random(); - Disposable temperatureReportProcess = Single.just(random) + Disposable reportProcess = Single.just(random) .delay(10, SECONDS) - .map(new Function() { + .flatMap(new Function>() { @Override - public Double apply(Random random) { - return random.nextDouble() * 100; + public Single apply(Random random) throws IOException { + double temperature = random.nextDouble() * 100, humidity = random.nextDouble() * 100; + return updateTemperatureAndHumidityAsync(temperature, humidity); } - }).flatMap(new Function>() { - @Override - public Single apply(Double temperature) throws IOException { - return updateTemperatureAsync(temperature); - } - }).repeat() + }) + .repeat() .subscribe(new Consumer() { @Override public void accept(DigitalTwinClientResult result) { @@ -97,31 +93,7 @@ public void accept(Throwable throwable) { log.debug("Update temperature failed.", throwable); } }); - Disposable humidityReportProcess = Single.just(random) - .delay(10, SECONDS) - .map(new Function() { - @Override - public Double apply(Random random) { - return random.nextDouble() * 100; - } - }).flatMap(new Function>() { - @Override - public Single apply(Double humidity) throws IOException { - return updateHumidityAsync(humidity); - } - }).repeat() - .subscribe(new Consumer() { - @Override - public void accept(DigitalTwinClientResult result) { - log.info("Update humidity was {}", result); - } - }, new Consumer() { - @Override - public void accept(Throwable throwable) { - log.debug("Update humidity failed.", throwable); - } - }); - log.debug("Once application quit, should dispose {} and {}.", temperatureReportProcess, humidityReportProcess); + log.debug("Once application quit, should dispose {}.", reportProcess); } @Override