-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTemperatureHumidityExample.java
63 lines (52 loc) · 2.6 KB
/
TemperatureHumidityExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package uk.ac.ic.doc.blocc.example;
import java.nio.file.Paths;
import java.util.Random;
import uk.ac.ic.doc.blocc.BloccContractGateway;
import uk.ac.ic.doc.blocc.TemperatureHumidityReadingContractApp;
import uk.ac.ic.doc.blocc.sensor.TemperatureHumiditySensor;
public class TemperatureHumidityExample {
private static class MockedSensor implements TemperatureHumiditySensor {
private final float mean;
private MockedSensor(float mean) {
this.mean = mean;
}
@Override
public float getTemperature() {
return (float) (new Random().nextGaussian() * 2 + mean);
}
@Override
public float getRelativeHumidity() {
return new Random().nextFloat();
}
}
public static void main(String[] args) throws Exception {
int containerNum = Integer.parseInt(System.getenv().getOrDefault("FABRIC_CONTAINER_NUM", String.valueOf(1)));
float mean = Float.parseFloat(System.getenv().getOrDefault("FABRIC_SENSOR_MEAN_TEMPERATURE", String.valueOf(20F)));
int interval = Integer.parseInt(System.getenv().getOrDefault("FABRIC_SENSOR_INTERVAL", String.valueOf(3000)));
int totalCount = Integer.parseInt(System.getenv().getOrDefault("FABRIC_SENSOR_NUM", String.valueOf(20)));
// Create gateway
BloccContractGateway gateway =
new BloccContractGateway(
System.getenv().getOrDefault("MSP_ID", String.format("Container%dMSP", containerNum)),
System.getenv().getOrDefault("CHANNEL_NAME", String.format("channel%d", containerNum)),
System.getenv().getOrDefault("CHAINCODE_NAME", "sensor_chaincode"),
Paths.get(
String.format("/home/tonywu/blocc/blocc-test-network/organizations/peerOrganizations/container%d.blocc.doc.ic.ac.uk", containerNum)),
Paths.get(String.format("users/User1@container%d.blocc.doc.ic.ac.uk/msp/signcerts/User1@container%d.blocc.doc.ic.ac.uk-cert.pem", containerNum, containerNum)),
Paths.get(String.format("users/User1@container%d.blocc.doc.ic.ac.uk/msp/keystore", containerNum)),
Paths.get(String.format("peers/peer0.container%d.blocc.doc.ic.ac.uk/tls/ca.crt", containerNum)),
String.format("localhost:%d051", containerNum),
String.format("blocc-container%d", containerNum));
TemperatureHumidityReadingContractApp app =
new TemperatureHumidityReadingContractApp(gateway.getContract(), new MockedSensor(mean));
// Invoke a transaction
try {
for (int count = 0; count < totalCount; count++) {
Thread.sleep(interval);
app.addReading();
}
} catch (Exception e) {
gateway.close();
}
}
}