Skip to content

Commit

Permalink
Use record to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Oct 6, 2024
1 parent 5176217 commit 5fce182
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package nl.bertriksikken.gls;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.fasterxml.jackson.annotation.JsonProperty;

@SuppressWarnings("UnusedVariable")
public final class GeoLocationRequest {

Expand Down Expand Up @@ -39,20 +39,8 @@ public void add(String macAddress, int signalStrength, int channel) {
wifiAccessPoints.add(new WifiAccessPoint(macAddress, signalStrength, channel));
}

private static final class WifiAccessPoint {
@JsonProperty("macAddress")
private final String macAddress;

@JsonProperty("signalStrength")
private final int signalStrength;

@JsonProperty("channel")
private final int channel;

WifiAccessPoint(String macAddress, int signalStrength, int channel) {
this.macAddress = macAddress;
this.signalStrength = signalStrength;
this.channel = channel;
}
private record WifiAccessPoint(@JsonProperty("macAddress") String macAddress,
@JsonProperty("signalStrength") int signalStrength,
@JsonProperty("channel") int channel) {
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,9 @@
package nl.bertriksikken.loraforwarder;

import java.util.Locale;
import java.util.Objects;

/**
* Combination of application name and device id that uniquely identifies a device.<br>
* This class has a hashCode() and equals() implementation that allows it to be used in set and maps.
*/
public final class AppDeviceId {

private final String appName;
private final String deviceId;
public record AppDeviceId(String appName, String deviceId) {

public AppDeviceId(String appName, String deviceId) {
this.appName = appName;
this.deviceId = deviceId;
}

public String getAppName() {
return appName;
}

public String getDeviceId() {
return deviceId;
}

@Override
public boolean equals(Object object) {
if (object instanceof AppDeviceId) {
AppDeviceId other = (AppDeviceId) object;
return appName.equals(other.appName) && deviceId.equals(other.deviceId);
}
return false;
}

@Override
public int hashCode() {
return Objects.hash(appName, deviceId);
}

@Override
public String toString() {
return String.format(Locale.ROOT, "%s/%s", appName, deviceId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void scheduleUpload(AppDeviceId appDeviceId, SensorData data) {
}

// schedule upload
String sensComId = "TTN-" + appDeviceId.getDeviceId();
String sensComId = "TTN-" + appDeviceId.deviceId();
executor.execute(new CatchingRunnable(LOG, () -> uploadMeasurement(boxId, sensComId, message)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private SensComWorker getOrCreateWorker(String id) {

@Override
public void scheduleUpload(AppDeviceId appDeviceId, SensorData data) {
SensComWorker worker = getOrCreateWorker(appDeviceId.getAppName());
SensComWorker worker = getOrCreateWorker(appDeviceId.appName());
worker.scheduleUpload(appDeviceId, data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static okhttp3.Response addUserAgent(Interceptor.Chain chain) throws IOE

void stop() {
LOG.info("Stopping sensor.community worker '{}'", appId);
executor.shutdownNow();
executor.shutdown();
}

// schedules an upload to all pins
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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;

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

/**
* Runs the SensComUploader to send a basic message to a local server, in order
* to inspect the actual HTTP request.
Expand All @@ -24,17 +24,19 @@ public static void main(String[] args) {

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

Map<String, AttributeMap> attributes = new HashMap<>();
AppDeviceId appDeviceId = new AppDeviceId("app", "dev");
attributes.put(appDeviceId.getDeviceId(), new AttributeMap(Map.of("senscom-id", "sensor")));
uploader.scheduleProcessAttributes(appDeviceId.getAppName(), attributes);
attributes.put(appDeviceId.deviceId(), new AttributeMap(Map.of("senscom-id", "sensor")));
uploader.scheduleProcessAttributes(appDeviceId.appName(), attributes);

SensorData sensorData = new SensorData();
sensorData.putValue(ESensorItem.TEMPERATURE, 12.34);
uploader.scheduleUpload(appDeviceId, sensorData);

uploader.stop();
}

}

0 comments on commit 5fce182

Please sign in to comment.