Skip to content

Commit 861d920

Browse files
authored
Merge pull request #1 from JMayrbaeurl/multiplesenseboxes
Multiple sense boxes
2 parents 1ff63a9 + e31ad50 commit 861d920

File tree

6 files changed

+196
-90
lines changed

6 files changed

+196
-90
lines changed

sensorbox-publisher/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>com.microsoft.samples.iot.opensense</groupId>
4444
<artifactId>opensense-intf</artifactId>
45-
<version>1.0.1-SNAPSHOT</version>
45+
<version>1.0.0</version>
4646
</dependency>
4747

4848
<dependency>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.microsoft.samples.iot.sensorbox;
2+
3+
import com.microsoft.samples.iot.opensense.dto.SenseBoxValues;
4+
5+
import org.apache.commons.logging.Log;
6+
import org.apache.commons.logging.LogFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
9+
public abstract class AbstractBoxSchedulerConfiguration {
10+
11+
private static final Log logger = LogFactory.getLog(AbstractBoxSchedulerConfiguration.class);
12+
13+
@Autowired
14+
private OpenSenseMapReader reader;
15+
16+
public abstract void publishLatestValue();
17+
18+
protected void publishLatestValueTo(final String boxID, final IoTHubSender boxSender) {
19+
20+
final SenseBoxValues latestValue = this.reader.readLatestValues(boxID);
21+
if (latestValue != null) {
22+
boxSender.sendLatestValues(latestValue);
23+
} else {
24+
logger.error("Could not read latest measurements for Sensor Box with ID '" + boxID + "'");
25+
}
26+
}
27+
28+
public OpenSenseMapReader getReader() {
29+
return reader;
30+
}
31+
32+
public void setReader(OpenSenseMapReader reader) {
33+
this.reader = reader;
34+
}
35+
}

sensorbox-publisher/src/main/java/com/microsoft/samples/iot/sensorbox/AppStartupRunner.java

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.microsoft.samples.iot.sensorbox;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import javax.annotation.PostConstruct;
8+
9+
import com.microsoft.samples.iot.opensense.dto.SenseBoxValues;
10+
11+
import org.apache.commons.logging.Log;
12+
import org.apache.commons.logging.LogFactory;
13+
import org.springframework.beans.factory.annotation.Value;
14+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
15+
import org.springframework.context.annotation.Configuration;
16+
import org.springframework.scheduling.annotation.Scheduled;
17+
import org.springframework.util.StringUtils;
18+
19+
@ConditionalOnProperty(name = "sensorbox.publisher.dps.boxesCreds")
20+
@Configuration
21+
public class MultipleBoxesSchedulerConfiguration extends AbstractBoxSchedulerConfiguration {
22+
23+
private static final Log logger = LogFactory.getLog(MultipleBoxesSchedulerConfiguration.class);
24+
25+
@Value("${sensorbox.publisher.dps.scope}")
26+
private String dpsScopeID;
27+
28+
@Value("${sensorbox.publisher.dps.boxesCreds:}")
29+
private String[] deviceCredentials;
30+
31+
private Map<String, IoTHubSender> boxesSender = new HashMap<String, IoTHubSender>();
32+
33+
@Scheduled(fixedRateString = "${opensensemap.publisher.fixedRate:60000}", initialDelay = 5000)
34+
public void publishLatestValue() {
35+
36+
if (this.boxesSender != null && this.boxesSender.size() > 0) {
37+
38+
for(String boxID : this.boxesSender.keySet()) {
39+
logger.info("Requesting last measurements for Sensor Box with ID '" + boxID + "'");
40+
this.publishLatestValueTo(boxID, this.boxesSender.get(boxID));
41+
}
42+
}
43+
}
44+
45+
@PostConstruct
46+
public void setupSenders() {
47+
if (this.deviceCredentials != null && this.deviceCredentials.length > 0) {
48+
for(String idAndKey : this.deviceCredentials) {
49+
50+
SenderConfig config = new SenderConfig(idAndKey);
51+
if (config.isValid()) {
52+
IoTHubSender sender = new IoTHubSender();
53+
try {
54+
sender.setupWithDPS(this.dpsScopeID, config.deviceID, config.deviceKey);
55+
sender.open();
56+
57+
SenseBoxValues values = this.getReader().readLatestValues(config.boxID);
58+
sender.reportSettings(values);
59+
} catch(IOException ex) {
60+
logger.error("Could not open connection for device with ID '" + config.deviceID + "'");
61+
}
62+
this.boxesSender.put(config.boxID, sender);
63+
}
64+
}
65+
}
66+
}
67+
68+
private static class SenderConfig {
69+
70+
private final String boxID;
71+
private final String deviceID;
72+
private final String deviceKey;
73+
74+
public SenderConfig(String fromString) {
75+
String[] idAndKeyArray = fromString.split(";");
76+
this.boxID = idAndKeyArray.length == 3 ? idAndKeyArray[0] : null;
77+
this.deviceID = idAndKeyArray.length == 3 ? idAndKeyArray[1] : null;
78+
this.deviceKey = idAndKeyArray.length == 3 ? idAndKeyArray[2] : null;
79+
}
80+
81+
public boolean isValid() {
82+
return StringUtils.hasText(this.boxID) && StringUtils.hasText(this.deviceID) && StringUtils.hasText(this.deviceKey);
83+
}
84+
}
85+
86+
public Map<String, IoTHubSender> getBoxesSender() {
87+
return boxesSender;
88+
}
89+
90+
public void setBoxesSender(Map<String, IoTHubSender> boxesSender) {
91+
this.boxesSender = boxesSender;
92+
}
93+
94+
public String getDpsScopeID() {
95+
return dpsScopeID;
96+
}
97+
98+
public void setDpsScopeID(String dpsScopeID) {
99+
this.dpsScopeID = dpsScopeID;
100+
}
101+
102+
public String[] getDeviceCredentials() {
103+
return deviceCredentials;
104+
}
105+
106+
public void setDeviceCredentials(String[] deviceCredentials) {
107+
this.deviceCredentials = deviceCredentials;
108+
}
109+
}
Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,14 @@
11
package com.microsoft.samples.iot.sensorbox;
22

3-
import java.io.IOException;
4-
import java.net.URISyntaxException;
5-
6-
import com.microsoft.azure.sdk.iot.device.DeviceClient;
7-
import com.microsoft.azure.sdk.iot.device.IotHubClientProtocol;
8-
9-
import org.springframework.beans.factory.annotation.Value;
103
import org.springframework.boot.SpringApplication;
114
import org.springframework.boot.autoconfigure.SpringBootApplication;
12-
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
13-
import org.springframework.context.annotation.Bean;
145
import org.springframework.scheduling.annotation.EnableScheduling;
156

167
@SpringBootApplication
178
@EnableScheduling
189
public class SensorboxPublisherApplication {
19-
20-
@Value("${sensorbox.publisher.iothub.connectionstring:}")
21-
private String iotHubConnectionString;
22-
23-
@Value("${sensorbox.publisher.dps.scope:}")
24-
private String dpsScopeID;
25-
26-
@Value("${sensorbox.publisher.dps.deviceID:}")
27-
private String deviceID;
28-
29-
@Value("${sensorbox.publisher.dps.deviceKey:}")
30-
private String deviceKey;
3110

3211
public static void main(String[] args) {
3312
SpringApplication.run(SensorboxPublisherApplication.class, args);
3413
}
35-
36-
@Bean
37-
@ConditionalOnProperty(name="sensorbox.publisher.iothub.connectionstring")
38-
public IoTHubSender createSenderFromConnectionString() throws IllegalArgumentException, URISyntaxException, IOException {
39-
40-
IoTHubSender ioTHubSender = new IoTHubSender();
41-
ioTHubSender.setDeviceClient(new DeviceClient(this.iotHubConnectionString, IotHubClientProtocol.AMQPS));
42-
ioTHubSender.open();
43-
44-
return ioTHubSender;
45-
}
46-
47-
@Bean
48-
@ConditionalOnProperty(name="sensorbox.publisher.dps.scope")
49-
public IoTHubSender createSenderFromDPS() throws IOException {
50-
51-
IoTHubSender ioTHubSender = new IoTHubSender();
52-
ioTHubSender.setupWithDPS(this.dpsScopeID, this.deviceID, this.deviceKey);
53-
ioTHubSender.open();
54-
55-
return ioTHubSender;
56-
}
5714
}
Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package com.microsoft.samples.iot.sensorbox;
22

3+
import java.io.IOException;
4+
import java.net.URISyntaxException;
5+
6+
import javax.annotation.PostConstruct;
7+
8+
import com.microsoft.azure.sdk.iot.device.DeviceClient;
9+
import com.microsoft.azure.sdk.iot.device.IotHubClientProtocol;
310
import com.microsoft.samples.iot.opensense.dto.SenseBoxValues;
411

512
import org.apache.commons.logging.Log;
613
import org.apache.commons.logging.LogFactory;
714
import org.springframework.beans.factory.annotation.Autowired;
815
import org.springframework.beans.factory.annotation.Value;
916
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
17+
import org.springframework.context.annotation.Bean;
1018
import org.springframework.context.annotation.Configuration;
1119
import org.springframework.scheduling.annotation.Scheduled;
1220

@@ -15,20 +23,30 @@
1523
*/
1624
@ConditionalOnProperty(name = "opensensemap.publisher.boxid")
1725
@Configuration
18-
public class SingleBoxSchedulerConfiguration {
26+
public class SingleBoxSchedulerConfiguration extends AbstractBoxSchedulerConfiguration {
1927

2028
private static final Log logger = LogFactory.getLog(SingleBoxSchedulerConfiguration.class);
2129

22-
@Autowired
23-
private IoTHubSender sender;
30+
@Value("${sensorbox.publisher.iothub.connectionstring:}")
31+
private String iotHubConnectionString;
2432

33+
@Value("${sensorbox.publisher.dps.scope:}")
34+
private String dpsScopeID;
35+
36+
@Value("${sensorbox.publisher.dps.deviceID:}")
37+
private String deviceID;
38+
39+
@Value("${sensorbox.publisher.dps.deviceKey:}")
40+
private String deviceKey;
41+
2542
@Autowired
26-
private OpenSenseMapReader reader;
43+
private IoTHubSender sender;
2744

2845
@Value("${opensensemap.publisher.boxid}")
2946
private String sensorBoxID;
3047

3148
@Scheduled(fixedRateString = "${opensensemap.publisher.fixedRate:60000}", initialDelay = 5000)
49+
@Override
3250
public void publishLatestValue() {
3351

3452
if (this.sensorBoxID == null) {
@@ -38,11 +56,34 @@ public void publishLatestValue() {
3856

3957
logger.info("Requesting last measurements for Sensor Box with ID '" + this.sensorBoxID + "'");
4058

41-
SenseBoxValues latestValue = this.reader.readLatestValues(this.sensorBoxID);
42-
if (latestValue != null) {
43-
this.sender.sendLatestValues(latestValue);
44-
} else {
45-
logger.error("Could not read latest measurements for Sensor Box with ID '" + this.sensorBoxID + "'");
46-
}
59+
this.publishLatestValueTo(this.sensorBoxID, this.sender);
4760
}
61+
62+
@PostConstruct
63+
public void reportCurrentState() throws IllegalArgumentException, IOException {
64+
SenseBoxValues values = this.getReader().readLatestValues(this.sensorBoxID);
65+
this.sender.reportSettings(values);
66+
}
67+
68+
@Bean
69+
@ConditionalOnProperty(name="sensorbox.publisher.iothub.connectionstring")
70+
public IoTHubSender createSenderFromConnectionString() throws IllegalArgumentException, URISyntaxException, IOException {
71+
72+
IoTHubSender ioTHubSender = new IoTHubSender();
73+
ioTHubSender.setDeviceClient(new DeviceClient(this.iotHubConnectionString, IotHubClientProtocol.AMQPS));
74+
ioTHubSender.open();
75+
76+
return ioTHubSender;
77+
}
78+
79+
@Bean
80+
@ConditionalOnProperty(name="sensorbox.publisher.dps.scope")
81+
public IoTHubSender createSenderFromDPS() throws IOException {
82+
83+
IoTHubSender ioTHubSender = new IoTHubSender();
84+
ioTHubSender.setupWithDPS(this.dpsScopeID, this.deviceID, this.deviceKey);
85+
ioTHubSender.open();
86+
87+
return ioTHubSender;
88+
}
4889
}

0 commit comments

Comments
 (0)