Skip to content

Commit

Permalink
changed filter to datamessage so it's now possible to filter on devic…
Browse files Browse the repository at this point in the history
…e groups
  • Loading branch information
cpoder committed Jul 14, 2023
1 parent 4e0ac2d commit 2791ae3
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
import lora.ns.liveobjects.rest.model.CommandStatusTrigger;
import lora.ns.liveobjects.rest.model.ConnectivityPlan;
import lora.ns.liveobjects.rest.model.CreateDevice;
import lora.ns.liveobjects.rest.model.DataMessageFilter;
import lora.ns.liveobjects.rest.model.DataMessageTrigger;
import lora.ns.liveobjects.rest.model.DeviceInterface;
import lora.ns.liveobjects.rest.model.DeviceInterfaceDefinition;
import lora.ns.liveobjects.rest.model.Group;
import lora.ns.liveobjects.rest.model.GroupPath;
import lora.ns.liveobjects.rest.model.HttpPushAction;
import lora.ns.liveobjects.rest.model.LoraNetworkFilter;
import lora.ns.liveobjects.rest.model.LoraNetworkFilter.MessageType;
Expand Down Expand Up @@ -164,6 +168,9 @@ public LNSResponse<Void> configureRoutings(String url, String tenant, String log
String authorization = "Basic "
+ Base64.getEncoder().encodeToString((tenant + "/" + login + ":" + password).getBytes());
try {
DataMessageFilter dataMessageFilter = new DataMessageFilter();
dataMessageFilter.getGroupPaths().add(new GroupPath().withPath(properties.getProperty("groupId")));
dataMessageFilter.getConnectors().add("lora");
Response<ActionPolicy> result = service.createActionPolicy(new ActionPolicy()
.withId(null)
.withName("Cumulocity webhook for tenant " + tenant)
Expand All @@ -173,10 +180,8 @@ public LNSResponse<Void> configureRoutings(String url, String tenant, String log
.withWebhookUrl(url + "/uplink")
.withHeaders(Map.of("Authorization", List.of(authorization))))))
.withTriggers(new ActionTriggers()
.withLoraNetwork(new LoraNetworkTrigger()
.withFilter(new LoraNetworkFilter()
.withMessageTypes(List.of(MessageType.UNCONFIRMED_DATA_UP,
MessageType.CONFIRMED_DATA_UP))))))
.withDataMessage(new DataMessageTrigger()
.withFilter(dataMessageFilter))))
.execute();
if (!result.isSuccessful()) {
response.setOk(false);
Expand Down Expand Up @@ -309,4 +314,19 @@ public List<String> getProfiles() {
}
return result;
}

public List<Group> getGroups() {
List<Group> groups = new ArrayList<>();

try {
Response<List<Group>> response = service.getGroups().execute();
if (response.isSuccessful()) {
groups = response.body();
}
} catch (IOException e) {
e.printStackTrace();
}

return groups;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lora.ns.liveobjects;

import java.math.BigDecimal;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
Expand All @@ -13,7 +14,6 @@
import com.cumulocity.model.measurement.MeasurementValue;
import com.cumulocity.model.operation.OperationStatus;
import com.cumulocity.rest.representation.measurement.MeasurementRepresentation;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.BaseEncoding;
Expand Down Expand Up @@ -43,6 +43,20 @@ public java.util.LinkedList<PropertyDescription> getPropertyDescriptions() {
return propertyDescriptions;
}
});
wizard.add(new LNSConnectorWizardStep() {
protected LinkedList<PropertyDescription> propertyDescriptions = new LinkedList<>();
{
propertyDescriptions.add(PropertyDescription.list("groupId", "Group", "/groups", true));
}

public String getName() {
return "step2";
}

public java.util.LinkedList<PropertyDescription> getPropertyDescriptions() {
return propertyDescriptions;
}
});
deviceProvisioningAdditionalProperties
.add(PropertyDescription.list("connectivityPlan", "Connectivity Plan", "/connectivityPlans", true));
deviceProvisioningAdditionalProperties
Expand All @@ -56,20 +70,23 @@ public DeviceData processUplinkEvent(String event) {
JsonNode rootNode;
try {
rootNode = mapper.readTree(event);
String devEUI = rootNode.at("/devEUI").asText();
int fPort = rootNode.at("/port").asInt();
JsonNode payloadNode = rootNode.get("payload");
String devEUI = rootNode.at("/metadata/network/lora/devEUI").asText();
if (devEUI == null) {
throw new InvalidParameterException("DevEUI can't be null");
}
int fPort = rootNode.at("/metadata/network/lora/port").asInt();
JsonNode payloadNode = rootNode.at("/value/payload");
byte[] payload = new byte[0];
if (payloadNode != null && !payloadNode.isNull()) {
payload = BaseEncoding.base16().decode(payloadNode.asText().toUpperCase());
}
Long updateTime = new DateTime(rootNode.at("/timestamp").asText()).getMillis();
log.info("Update time is: {}", updateTime);

double rssi = rootNode.at("/signal/rssi").asDouble();
double snr = rootNode.at("/signal/snr").asDouble();
double esp = rootNode.at("/signal/esp").asDouble();
double sf = rootNode.at("/signal/sf").asDouble();
double rssi = rootNode.at("/metadata/network/lora/rssi").asDouble();
double snr = rootNode.at("/metadata/network/lora/snr").asDouble();
double esp = rootNode.at("/metadata/network/lora/esp").asDouble();
double sf = rootNode.at("/metadata/network/lora/sf").asDouble();

List<MeasurementRepresentation> measurements = new ArrayList<>();
MeasurementRepresentation m = new MeasurementRepresentation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import lora.common.IdNameEntry;
import lora.ns.connector.LNSConnector;
import lora.ns.connector.LNSConnectorManager;
import lora.ns.liveobjects.rest.model.Group;

@RestController
public class LiveObjectsRestController {
Expand Down Expand Up @@ -45,4 +49,40 @@ private List<IdNameEntry> getProfiles(@PathVariable String lnsConnectorId) {
return result;
}

/***
* Should be used when updating the connector to get the list of available
* device groups.
*
* @param lnsConnectorId
* @param properties
* @return
*/
@PostMapping(value = "/{lnsConnectorId}/groups", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public List<IdNameEntry> getGroups(@PathVariable String lnsConnectorId, @RequestBody Properties properties) {
List<IdNameEntry> result = new ArrayList<>();
Optional<LNSConnector> connector = lnsConnectorManager.getConnector(lnsConnectorId);
if (connector.isPresent()) {
properties = connector.get().mergeProperties(properties);
result = new LiveObjectsConnector(properties).getGroups().stream()
.map(g -> new IdNameEntry(g.getPath(), g.getPath())).collect(Collectors.toList());
;
}

return result;
}

/***
* Retrieves the list of avaible device groups in Live Object tenant when
* creating
* a new Live Object connector.
*
* @param properties
* @return
*/
@PostMapping(value = "/groups", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public List<IdNameEntry> getGroups(@RequestBody Properties properties) {
return new LiveObjectsConnector(properties).getGroups().stream()
.map(g -> new IdNameEntry(g.getPath(), g.getPath())).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lora.ns.liveobjects.rest.model.CommandResponse;
import lora.ns.liveobjects.rest.model.ConnectivityPlan;
import lora.ns.liveobjects.rest.model.CreateDevice;
import lora.ns.liveobjects.rest.model.Group;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
Expand Down Expand Up @@ -44,4 +45,8 @@ public interface LiveObjectsService {
@Headers({ "Content-Type: application/json", "Accept: application/json" })
@GET("api/v1/deviceMgt/connectors/lora/profiles")
Call<List<String>> getProfiles();

@Headers({ "Content-Type: application/json", "Accept: application/json" })
@GET("api/v1/deviceMgt/groups")
Call<List<Group>> getGroups();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@AllArgsConstructor
@With
public class ActionTriggers {
LoraNetworkTrigger loraNetwork;
CommandStatusTrigger commandStatus;
private LoraNetworkTrigger loraNetwork;
private CommandStatusTrigger commandStatus;
private DataMessageTrigger dataMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lora.ns.liveobjects.rest.model;

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

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;

@Data
@NoArgsConstructor
@AllArgsConstructor
@With
public class DataMessageFilter {
private List<GroupPath> groupPaths = new ArrayList<>();
private List<String> connectors = new ArrayList<>();
private List<String> deviceIds = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lora.ns.liveobjects.rest.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;

@Data
@NoArgsConstructor
@AllArgsConstructor
@With
public class DataMessageTrigger {
private int version = 1;
private DataMessageFilter filter;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package lora.ns.liveobjects.rest.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;

@Data
@NoArgsConstructor
@AllArgsConstructor
@With
@JsonIgnoreProperties(ignoreUnknown = true)
public class Group {
private String id;
private String pathNode;
private String path;
private String parentId;
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lora.ns.liveobjects.rest.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;

@Data
@NoArgsConstructor
@AllArgsConstructor
@With
public class GroupPath {
private String path;
private boolean includeSubPath = true;
}

0 comments on commit 2791ae3

Please sign in to comment.