Skip to content

Commit

Permalink
Workaround for poorly designed Thingpark API
Browse files Browse the repository at this point in the history
  • Loading branch information
cpoder committed Jun 1, 2024
1 parent 63758c9 commit 333c731
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import c8y.ConnectionState;
import feign.Client;
import feign.Feign;
import feign.FeignException.FeignClientException;
import feign.Logger.Level;
import feign.form.FormEncoder;
import feign.jackson.JacksonDecoder;
Expand All @@ -41,11 +42,13 @@
import lora.ns.actility.api.DeviceApi;
import lora.ns.actility.api.DownlinkApi;
import lora.ns.actility.api.model.appserver.AppServer;
import lora.ns.actility.api.model.appserver.AppServer.ContentTypeEnum;
import lora.ns.actility.api.model.appserver.AppServerCustomHttpHeadersInner;
import lora.ns.actility.api.model.appserver.AppServerHttpLorawanDestination;
import lora.ns.actility.api.model.appserver.AppServerStrategy;
import lora.ns.actility.api.model.appserver.DownlinkSecurity;
import lora.ns.actility.api.model.basestation.Bs;
import lora.ns.actility.api.model.basestation.BsAppServersInner;
import lora.ns.actility.api.model.basestation.BsBrief;
import lora.ns.actility.api.model.basestation.BsHealthState;
import lora.ns.actility.api.model.basestation.BsModel;
Expand Down Expand Up @@ -227,11 +230,15 @@ public String sendDownlink(DownlinkData operation) {
public void provisionDevice(DeviceProvisioning deviceProvisioning) {
var device = new Device();
device.EUI(deviceProvisioning.getDevEUI()).name(deviceProvisioning.getName()).activation(ActivationEnum.OTAA)
.addDomainsItem(new Domain().name(properties.getProperty("domain"))
.group(new DomainGroup().name(properties.getProperty("group"))))
.appKey(deviceProvisioning.getAppKey()).appEUI(deviceProvisioning.getAppEUI())
.addAppServersItem(new DeviceLorawanAppServer().ID(this.appServerId)).model(new DeviceModel()
.ID(deviceProvisioning.getAdditionalProperties().getProperty("deviceProfile")));
if (properties.containsKey("domain")) {
device.addDomainsItem(new Domain().name(properties.getProperty("domain"))
.group(new DomainGroup().name(properties.getProperty("group"))));
} else {
device.setDomains(null);
}
deviceApi.createDevice(device);
}

Expand All @@ -242,28 +249,34 @@ public void configureRoutings(String url, String tenant, String login, String pa
var appServers = appServerApi.getAppServersByName(tenant + "-" + getId());
if (!appServers.getBriefs().isEmpty()) {
// Update appserver
var appServer = appServerApi.getAppServer(appServers.getBriefs().iterator().next().getID());
this.appServerId = appServers.getBriefs().iterator().next().getID();
String uid = appServers.getBriefs().iterator().next().getID().split("\\.")[1];
var appServer = new AppServer();
appServer.setType(null);
appServer.setContentType(ContentTypeEnum.JSON);
appServer.setCustomHttpHeaders(new ArrayList<>());
appServer.addCustomHttpHeadersItem(new AppServerCustomHttpHeadersInner().name("Authentication")
.value("Basic " + Base64.getEncoder()
.encodeToString((tenant + "/" + login + ":" + password).getBytes())));
appServer.setDestinations(new ArrayList<>());
appServer.addDestinationsItem(new AppServerHttpLorawanDestination().addAddressesItem(url + "/uplink")
.strategy(AppServerStrategy.SEQUENTIAL));
.strategy(AppServerStrategy.SEQUENTIAL).ports("*"));
appServer.downlinkSecurity(new DownlinkSecurity(getAsId(), getAsKey()));
appServerApi.updateAppServer(appServer.getID(), appServer);
// appServerApi.updateAppServer(uid, appServer); <- doesn't work, 403
} else {
// Create appserver
var appServer = new AppServer()
.addDomainsItem(new Domain().name(properties.getProperty("domain"))
.group(new DomainGroup().name(properties.getProperty("group"))))
var appServer = new AppServer().contentType(ContentTypeEnum.JSON)
.addCustomHttpHeadersItem(new AppServerCustomHttpHeadersInner().name("Authentication")
.value("Basic " + Base64.getEncoder().encodeToString(
(tenant + "/" + login + ":" + password).getBytes())))
.addDestinationsItem(new AppServerHttpLorawanDestination().addAddressesItem(url + "/uplink")
.strategy(AppServerStrategy.SEQUENTIAL))
.strategy(AppServerStrategy.SEQUENTIAL).ports("*"))
.name(tenant + "-" + getId()).downlinkSecurity(new DownlinkSecurity(getAsId(), getAsKey()));
appServerApi.createAppServer(appServer);
if (properties.containsKey("domain")) {
appServer.addDomainsItem(new Domain().name(properties.getProperty("domain"))
.group(new DomainGroup().name(properties.getProperty("group"))));
}
this.appServerId = appServerApi.createAppServer(appServer).getID();
}
}

Expand All @@ -280,47 +293,55 @@ public void deprovisionDevice(String deveui) {
@Override
public List<Gateway> getGateways() {
final List<Gateway> result = new ArrayList<>();
// var baseStations = baseStationApi.getBaseStations();
Bss baseStations = null;
;
for (var i = 1; baseStations == null
|| baseStations.getMore(); baseStations = baseStationApi.getBaseStations(i++)) {
for (BsBrief b : baseStations.getBriefs()) {
var baseStation = baseStationApi.getBaseStation(b.getHref());
Gateway g = new Gateway();
g.setGwEUI(baseStation.getLrrUUID());
if (baseStation.getSmn() != null) {
g.setSerial(baseStation.getSmn());
}
g.setName(baseStation.getName());
if (baseStation.getLastGeoLat() != null) {
g.setLat(BigDecimal.valueOf(baseStation.getLastGeoLat()));
}
if (baseStation.getLastGeoLon() != null) {
g.setLng(BigDecimal.valueOf(baseStation.getLastGeoLon()));
}
if (baseStation.getHealthState() != null) {
C8YData data = new C8YData();
if (baseStation.getHealthState() == BsHealthState.ACTIVE) {
g.setStatus(ConnectionState.AVAILABLE);
} else {
g.setStatus(ConnectionState.UNAVAILABLE);
try {

int i = 1;
Bss baseStations;
do {
baseStations = baseStationApi.getBaseStations(i++);
for (BsBrief b : baseStations.getBriefs()) {
var baseStation = baseStationApi.getBaseStation(b.getHref());
Gateway g = new Gateway();
g.setGwEUI(baseStation.getLrrUUID());
if (baseStation.getSmn() != null) {
g.setSerial(baseStation.getSmn());
}
if (baseStation.getTemp() != null) {
data.addMeasurement(null, "Temperature", "T", "°C", BigDecimal.valueOf(baseStation.getTemp()),
new DateTime());
g.setName(baseStation.getName());
if (baseStation.getLastGeoLat() != null) {
g.setLat(BigDecimal.valueOf(baseStation.getLastGeoLat()));
}
if (baseStation.getCpu() != null) {
data.addMeasurement(null, "CPU", "Usage", "%", BigDecimal.valueOf(baseStation.getCpu()),
new DateTime());
if (baseStation.getLastGeoLon() != null) {
g.setLng(BigDecimal.valueOf(baseStation.getLastGeoLon()));
}
if (baseStation.getRam() != null) {
data.addMeasurement(null, "RAM", "Usage", "%", BigDecimal.valueOf(baseStation.getRam()),
new DateTime());
if (baseStation.getHealthState() != null) {
C8YData data = new C8YData();
if (baseStation.getHealthState() == BsHealthState.ACTIVE) {
g.setStatus(ConnectionState.AVAILABLE);
} else {
g.setStatus(ConnectionState.UNAVAILABLE);
}
if (baseStation.getTemp() != null) {
data.addMeasurement(null, "Temperature", "T", "°C",
BigDecimal.valueOf(baseStation.getTemp()), new DateTime());
}
if (baseStation.getCpu() != null) {
data.addMeasurement(null, "CPU", "Usage", "%", BigDecimal.valueOf(baseStation.getCpu()),
new DateTime());
}
if (baseStation.getRam() != null) {
data.addMeasurement(null, "RAM", "Usage", "%", BigDecimal.valueOf(baseStation.getRam()),
new DateTime());
}
g.setData(data);
}
g.setData(data);
result.add(g);
}
result.add(g);
} while (baseStations.getMore());
} catch (FeignClientException e) {
if (e.status() == 401) {
log.error("User can't access base stations API");
} else {
throw e;
}
}

Expand All @@ -329,23 +350,30 @@ public List<Gateway> getGateways() {

public List<DeviceProfilesBriefsInner> getDeviceProfiles() {
List<DeviceProfilesBriefsInner> result = new ArrayList<>();
DeviceProfiles deviceProfiles = null;
for (int i = 1; deviceProfiles == null || deviceProfiles.getMore(); deviceApi.getDeviceProfiles(i++)) {
DeviceProfiles deviceProfiles;
int i = 1;
do {
deviceProfiles = deviceApi.getDeviceProfiles(i++);
result.addAll(deviceProfiles.getBriefs());
}
} while (deviceProfiles.getMore());
return result;
}

public void provisionGateway(GatewayProvisioning gatewayProvisioning) {
var baseStation = new Bs();
baseStation.setName(gatewayProvisioning.getName());
baseStation.addDomainsItem(new Domain().name(properties.getProperty("domain"))
.group(new DomainGroup().name(properties.getProperty("group"))));
if (properties.containsKey("domain")) {
baseStation.addDomainsItem(new Domain().name(properties.getProperty("domain"))
.group(new DomainGroup().name(properties.getProperty("group"))));
} else {
baseStation.setDomains(null);
}
baseStation.setLrrUUID(gatewayProvisioning.getGwEUI());
baseStation.setSmn(gatewayProvisioning.getAdditionalProperties().getProperty("SMN"));
baseStation.setPublicKey(gatewayProvisioning.getAdditionalProperties().getProperty("publicKey"));
baseStation.setModel(
new BsModel().ID(gatewayProvisioning.getAdditionalProperties().getProperty("gatewayProfile")));
baseStation.addAppServersItem(new BsAppServersInner().ID(this.appServerId));
baseStationApi.createBaseStation(baseStation);
}

Expand All @@ -363,10 +391,12 @@ public Properties getInitProperties() {

public List<BsProfilesBriefsInner> getBaseStationProfiles() {
List<BsProfilesBriefsInner> result = new ArrayList<>();
BsProfiles bsProfiles = null;
for (int i = 1; bsProfiles == null || bsProfiles.getMore(); baseStationApi.getBaseStationProfiles(i++)) {
BsProfiles bsProfiles;
int i = 1;
do {
bsProfiles = baseStationApi.getBaseStationProfiles(i++);
result.addAll(bsProfiles.getBriefs());
}
} while (bsProfiles.getMore());
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface DeviceApi {
@Headers({ "Accept: application/json", })
Device deleteDevice(@Param("deviceUid") String deviceUid);

@RequestLine("GET /subscriptions/mine/devicesProfiles?pageIndex={pageIndex}")
@RequestLine("GET /subscriptions/mine/deviceProfiles?pageIndex={pageIndex}")
@Headers({ "Accept: application/json", })
DeviceProfiles getDeviceProfiles(@Param("pageIndex") Integer pageIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ public String getID() {
**/
@javax.annotation.Nonnull
@JsonProperty(JSON_PROPERTY_BASE_STATION_COUNT)
@JsonInclude(value = JsonInclude.Include.ALWAYS)

public Integer getBaseStationCount() {
return baseStationCount;
Expand Down Expand Up @@ -321,7 +320,6 @@ public void setDescription(String description) {
**/
@javax.annotation.Nonnull
@JsonProperty(JSON_PROPERTY_DEVICE_COUNT)
@JsonInclude(value = JsonInclude.Include.ALWAYS)

public Integer getDeviceCount() {
return deviceCount;
Expand Down Expand Up @@ -396,7 +394,6 @@ public void setDownlinkSecurity(DownlinkSecurity downlinkSecurity) {
**/
@javax.annotation.Nonnull
@JsonProperty(JSON_PROPERTY_HEALTH_STATE)
@JsonInclude(value = JsonInclude.Include.ALWAYS)

public HealthStateEnum getHealthState() {
return healthState;
Expand All @@ -413,7 +410,6 @@ public HealthStateEnum getHealthState() {
**/
@javax.annotation.Nonnull
@JsonProperty(JSON_PROPERTY_MULTICAST_GROUP_COUNT)
@JsonInclude(value = JsonInclude.Include.ALWAYS)

public Integer getMulticastGroupCount() {
return multicastGroupCount;
Expand Down Expand Up @@ -503,7 +499,6 @@ public void setType(TypeEnum type) {
**/
@javax.annotation.Nonnull
@JsonProperty(JSON_PROPERTY_VALIDATION_STATE)
@JsonInclude(value = JsonInclude.Include.ALWAYS)

public AppServerValidationState getValidationState() {
return validationState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

/**
* AppServerHttpLorawanDestination
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonPropertyOrder({ AppServerHttpLorawanDestination.JSON_PROPERTY_ADDRESSES,
AppServerHttpLorawanDestination.JSON_PROPERTY_PORTS, AppServerHttpLorawanDestination.JSON_PROPERTY_STRATEGY })
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2024-05-29T11:38:36.048437806+02:00[Europe/Paris]", comments = "Generator version: 7.6.0")
Expand Down
Loading

0 comments on commit 333c731

Please sign in to comment.