Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
modified for telemetry explosion
Browse files Browse the repository at this point in the history
  • Loading branch information
davilu committed Jan 30, 2020
1 parent 31c5cce commit 9ec5972
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 101 deletions.
2 changes: 1 addition & 1 deletion digital-twin/Samples/device/AndroidSample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,16 +55,13 @@ protected EnvironmentalSensor(@NonNull String digitalTwinComponentName, @NonNull
this.uiHandler = uiHandler;
}

public Single<DigitalTwinClientResult> updateTemperatureAsync(double temperature) throws IOException {
log.debug("Temperature changed to {}.", temperature);
uiHandler.updateTemperature(temperature);
return sendTelemetryAsync(TELEMETRY_NAME_TEMPERATURE, serialize(temperature));
}

public Single<DigitalTwinClientResult> updateHumidityAsync(double humidity) throws IOException {
log.debug("Humidity changed to {}.", humidity);
uiHandler.updateHumidity(humidity);
return sendTelemetryAsync(TELEMETRY_NAME_HUMIDITY, serialize(humidity));
public Single<DigitalTwinClientResult> updateTemperatureAndHumidityAsync(double temperature, double humidity) throws IOException {
log.info("Temperature changed to {}, Humidity changed to {}.", temperature, humidity);
uiHandler.updateTemperatureAndHumidity(temperature, humidity);
Map<String, Double> properties = new HashMap<>();
properties.put(TELEMETRY_NAME_TEMPERATURE, temperature);
properties.put(TELEMETRY_NAME_HUMIDITY, humidity);
return sendTelemetryAsync(serialize(properties));
}

public Single<DigitalTwinClientResult> updateStatusAsync(final boolean state) {
Expand All @@ -80,55 +78,28 @@ public Single<DigitalTwinClientResult> 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<Random, Double>() {
.flatMap(new Function<Random, Single<DigitalTwinClientResult>>() {
@Override
public Double apply(Random random) {
return random.nextDouble() * 100;
public Single<DigitalTwinClientResult> apply(Random random) throws IOException {
double temperature = random.nextDouble() * 100, humidity = random.nextDouble() * 100;
return updateTemperatureAndHumidityAsync(temperature, humidity);
}
}).flatMap(new Function<Double, Single<DigitalTwinClientResult>>() {
@Override
public Single<DigitalTwinClientResult> apply(Double temperature) throws IOException {
return updateTemperatureAsync(temperature);
}
}).repeat()
})
.repeat()
.subscribe(new Consumer<DigitalTwinClientResult>() {
@Override
public void accept(DigitalTwinClientResult result) {
log.debug("Update temperature was {}", result);
log.info("Update temperature was {}", result);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
log.debug("Update temperature failed.", throwable);
}
});
Disposable humidityReportProcess = Single.just(random)
.delay(10, SECONDS)
.map(new Function<Random, Double>() {
@Override
public Double apply(Random random) {
return random.nextDouble() * 100;
}
}).flatMap(new Function<Double, Single<DigitalTwinClientResult>>() {
@Override
public Single<DigitalTwinClientResult> apply(Double humidity) throws IOException {
return updateHumidityAsync(humidity);
}
}).repeat()
.subscribe(new Consumer<DigitalTwinClientResult>() {
@Override
public void accept(DigitalTwinClientResult result) {
log.debug("Update humidity was {}", result);
}
}, new Consumer<Throwable>() {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void accept(Throwable throwable) {
}
}

@Override
public void updateName(final String name) {
runOnUiThread(new Runnable() {
@Override
Expand All @@ -134,6 +135,7 @@ public void run() {
});
}

@Override
public void updateBrightness(final double brightness) {
runOnUiThread(new Runnable() {
@Override
Expand All @@ -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));
}
});
Expand All @@ -170,6 +165,7 @@ public void run() {
});
}

@Override
public void updateOnoff(final boolean on) {
runOnUiThread(new Runnable() {
@Override
Expand All @@ -179,6 +175,7 @@ public void run() {
});
}

@Override
public void startBlink(final long interval) {
runOnUiThread(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,14 +51,12 @@ public EnvironmentalSensor(@NonNull String digitalTwinComponentName) {
super(digitalTwinComponentName, ENVIRONMENTAL_SENSOR_INTERFACE_ID);
}

public Single<DigitalTwinClientResult> updateTemperatureAsync(double temperature) throws IOException {
log.info("Temperature changed to {}.", temperature);
return sendTelemetryAsync(TELEMETRY_NAME_TEMPERATURE, serialize(temperature));
}

public Single<DigitalTwinClientResult> updateHumidityAsync(double humidity) throws IOException {
log.info("Humidity changed to {}.", humidity);
return sendTelemetryAsync(TELEMETRY_NAME_HUMIDITY, serialize(humidity));
public Single<DigitalTwinClientResult> updateTemperatureAndHumidityAsync(double temperature, double humidity) throws IOException {
log.info("Temperature changed to {}, Humidity changed to {}.", temperature, humidity);
Map<String, Double> properties = new HashMap<>();
properties.put(TELEMETRY_NAME_TEMPERATURE, temperature);
properties.put(TELEMETRY_NAME_HUMIDITY, humidity);
return sendTelemetryAsync(serialize(properties));
}

public Single<DigitalTwinClientResult> updateStatusAsync(final boolean state) {
Expand All @@ -73,19 +72,16 @@ public Single<DigitalTwinClientResult> 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<Random, Double>() {
.flatMap(new Function<Random, Single<DigitalTwinClientResult>>() {
@Override
public Double apply(Random random) {
return random.nextDouble() * 100;
public Single<DigitalTwinClientResult> apply(Random random) throws IOException {
double temperature = random.nextDouble() * 100, humidity = random.nextDouble() * 100;
return updateTemperatureAndHumidityAsync(temperature, humidity);
}
}).flatMap(new Function<Double, Single<DigitalTwinClientResult>>() {
@Override
public Single<DigitalTwinClientResult> apply(Double temperature) throws IOException {
return updateTemperatureAsync(temperature);
}
}).repeat()
})
.repeat()
.subscribe(new Consumer<DigitalTwinClientResult>() {
@Override
public void accept(DigitalTwinClientResult result) {
Expand All @@ -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<Random, Double>() {
@Override
public Double apply(Random random) {
return random.nextDouble() * 100;
}
}).flatMap(new Function<Double, Single<DigitalTwinClientResult>>() {
@Override
public Single<DigitalTwinClientResult> apply(Double humidity) throws IOException {
return updateHumidityAsync(humidity);
}
}).repeat()
.subscribe(new Consumer<DigitalTwinClientResult>() {
@Override
public void accept(DigitalTwinClientResult result) {
log.info("Update humidity was {}", result);
}
}, new Consumer<Throwable>() {
@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
Expand Down

0 comments on commit 9ec5972

Please sign in to comment.