Specify GW timeout as a Duration.

pull/3/head
Bertrik Sikken 2019-05-12 00:01:29 +02:00
rodzic ff0b71c1d2
commit a6134817c6
4 zmienionych plików z 13 dodań i 11 usunięć

Wyświetl plik

@ -33,9 +33,9 @@ public interface ITtnHabBridgeConfig {
String getTtnAppKey();
/**
* @return the expiry time (seconds) of a gateway, i.e. the interval of listener information an telemetry uploads.
* @return the expiry time of a gateway, i.e. the interval of listener information an telemetry uploads.
*/
int getTtnGwCacheExpiry();
Duration getTtnGwCacheExpiry();
/**
* @return the payload encoding, can be "sodaq", "json", "cayenne"

Wyświetl plik

@ -17,7 +17,7 @@ final class TtnHabBridgeConfig extends BaseConfig implements ITtnHabBridgeConfig
TTN_MQTT_URL("ttn.mqtt.url", "tcp://eu.thethings.network", "URL of the TTN MQTT server"),
TTN_APP_ID("ttn.app.id", "habhub", "TTN Application Id (e.g. habhub, ttnmapper, etc.)"),
TTN_APP_KEY("ttn.app.key", "ttn-account-v2.Sh49WL90oQz-ZuxoDrS6yKuACL_jtAA0agdDfO_eVj4", "TTN Application key"),
TTN_GW_CACHE_EXPIRY("ttn.gwcache.expiry", "600", "Gateway cache expiration time (seconds)"),
TTN_GW_CACHE_EXPIRY_SEC("ttn.gwcache.expiry", "600", "Gateway cache expiration time (seconds)"),
TTN_PAYLOAD_ENCODING("ttn.payload.encoding", "cayenne",
"Payload format, allowed values: 'sodaqone','json','cayenne'"),
;
@ -70,8 +70,8 @@ final class TtnHabBridgeConfig extends BaseConfig implements ITtnHabBridgeConfig
}
@Override
public int getTtnGwCacheExpiry() {
return Integer.parseInt(get(EConfigItem.TTN_GW_CACHE_EXPIRY.key));
public Duration getTtnGwCacheExpiry() {
return Duration.ofSeconds(Integer.parseInt(get(EConfigItem.TTN_GW_CACHE_EXPIRY_SEC.key)));
}
@Override

Wyświetl plik

@ -1,5 +1,6 @@
package nl.sikken.bertrik.hab;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -10,15 +11,15 @@ import java.util.concurrent.ConcurrentHashMap;
public final class ExpiringCache {
private final Map<String, Instant> map = new ConcurrentHashMap<>();
private final long expiryTimeSec;
private final Duration expiryTime;
/**
* Constructor.
*
* @param expiryTimeSec the expiry time (seconds)
* @param expiryTime the expiry time
*/
public ExpiringCache(int expiryTimeSec) {
this.expiryTimeSec = expiryTimeSec;
public ExpiringCache(Duration expiryTime) {
this.expiryTime = expiryTime;
}
/**
@ -40,7 +41,7 @@ public final class ExpiringCache {
* @param now the current date
*/
private void cleanUp(Instant now) {
Instant limit = now.minusSeconds(expiryTimeSec);
Instant limit = now.minus(expiryTime);
map.forEach((k,v) -> {
if (v.isBefore(limit)) {
map.remove(k, v);

Wyświetl plik

@ -1,5 +1,6 @@
package nl.sikken.bertrik.hab;
import java.time.Duration;
import java.time.Instant;
import org.junit.Assert;
@ -15,7 +16,7 @@ public final class ExpiringCacheTest {
*/
@Test
public void testExpiry() {
ExpiringCache cache = new ExpiringCache(1);
ExpiringCache cache = new ExpiringCache(Duration.ofSeconds(1));
// add two items
Instant date1 = Instant.now();