Skip to content

Commit

Permalink
Use the github URL as user-agent and specify a date-code as software …
Browse files Browse the repository at this point in the history
…version in the sens-com upload message.
  • Loading branch information
bertrik committed Oct 15, 2023
1 parent 049546f commit 9dd6574
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public final class AttributeMap extends HashMap<String, String> {

private static final long serialVersionUID = 1L;

AttributeMap(Map<String, String> map) {
public AttributeMap(Map<String, String> map) {
super(map);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ public final class SensComConfig extends RestApiConfig {

// jackson no-arg constructor
public SensComConfig() {
super("https://api.sensor.community", 30);
this("https://api.sensor.community", 30);
}

SensComConfig(String host, int timeout) {
super(host, timeout);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -19,7 +20,9 @@
import nl.bertriksikken.loraforwarder.util.CatchingRunnable;
import nl.bertriksikken.pm.ESensorItem;
import nl.bertriksikken.pm.SensorData;
import okhttp3.Interceptor.Chain;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
Expand All @@ -31,7 +34,8 @@
public final class SensComUploader implements IUploader {

private static final Logger LOG = LoggerFactory.getLogger(SensComUploader.class);
private static final String SOFTWARE_VERSION = "https://github.com/bertrik/sensor-data-bridge";
private static final String USER_AGENT = "github.com/bertrik/sensor-data-bridge";
private static final String SOFTWARE_VERSION = "20231015";

private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final ObjectMapper mapper = new ObjectMapper();
Expand All @@ -46,7 +50,7 @@ public final class SensComUploader implements IUploader {
* @param restClient the REST client
*/
SensComUploader(ISensComApi restClient) {
this.restClient = restClient;
this.restClient = Objects.requireNonNull(restClient);
}

/**
Expand All @@ -55,15 +59,20 @@ public final class SensComUploader implements IUploader {
public static SensComUploader create(SensComConfig config) {
LOG.info("Creating new REST client for '{}' with timeout {}", config.getUrl(), config.getTimeout());
Duration timeout = config.getTimeout();
OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(timeout).readTimeout(timeout)
.writeTimeout(timeout).build();
OkHttpClient client = new OkHttpClient().newBuilder().addInterceptor(SensComUploader::addUserAgent)
.connectTimeout(timeout).readTimeout(timeout).writeTimeout(timeout).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(config.getUrl())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(JacksonConverterFactory.create()).client(client).build();
ISensComApi restClient = retrofit.create(ISensComApi.class);
return new SensComUploader(restClient);
}

private static okhttp3.Response addUserAgent(Chain chain) throws IOException {
Request userAgentRequest = chain.request().newBuilder().header("User-Agent", USER_AGENT).build();
return chain.proceed(userAgentRequest);
}

@Override
public void start() {
LOG.info("Starting sensor.community uploader");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package nl.bertriksikken.senscom;

import java.util.HashMap;
import java.util.Map;

import nl.bertriksikken.loraforwarder.AppDeviceId;
import nl.bertriksikken.loraforwarder.AttributeMap;
import nl.bertriksikken.pm.ESensorItem;
import nl.bertriksikken.pm.SensorData;

/**
* Runs the SensComUploader to send a basic message to a local server, in order
* to inspect the actual HTTP request.
* <p>
* Use with (for example):
* https://gist.github.com/mdonkers/63e115cc0c79b4f6b8b3a6b797e485c7
*/
public final class RunSenscomUploader {

public static void main(String[] args) {
RunSenscomUploader runner = new RunSenscomUploader();
runner.run();
}

private void run() {
SensComConfig config = new SensComConfig("http://localhost:8080", 10);
SensComUploader uploader = SensComUploader.create(config);
uploader.start();

AppDeviceId id = new AppDeviceId("app", "device");
Map<AppDeviceId, AttributeMap> attributes = new HashMap<>();
attributes.put(id, new AttributeMap(Map.of("senscom-id", "sensor")));
uploader.scheduleProcessAttributes(attributes);

SensorData sensorData = new SensorData();
sensorData.addValue(ESensorItem.TEMP, 12.34);
uploader.scheduleUpload(id, sensorData);
}

}

0 comments on commit 9dd6574

Please sign in to comment.