Skip to content

Commit

Permalink
Cbor update and java sdk changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitmohan96 committed Oct 30, 2024
1 parent 14e748a commit 62ad79f
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 104 deletions.
8 changes: 5 additions & 3 deletions gateway/data_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import dataclasses
import time
from typing import Any
import cbor2
from flask import Flask
Expand Down Expand Up @@ -129,16 +130,17 @@ def publish_notification(self,
# ble_sub = data_app_pb2.DataSubscription() # pylint: disable=no-member
# ble_sub.data = value
ble_sub: dict[str, Any] = {
"data": value
"data": value,
"timestamp": time.time()
}

if topic.data_format == "default":
ble_sub["deviceId"] = str(device.device_id)
# pylint: disable-next=no-member
ble_sub["bleSubscription"] = {}
ble_subscription = ble_sub["bleSubscription"]
ble_subscription["serviceUuid"] = service_uuid
ble_subscription["characteristicUuid"] = char_uuid
ble_subscription["serviceId"] = service_uuid
ble_subscription["characteristicId"] = char_uuid

data = cbor2.dumps(obj=ble_sub)

Expand Down
1 change: 0 additions & 1 deletion java-sdk/sample-java-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'com.google.protobuf:protobuf-java-util:3.22.2'

compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.cisco.tiedie.clients.DataReceiverClient;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.util.JsonFormat;

@Component
public class ConnectionStatusHandler extends TextWebSocketHandler {
Expand All @@ -39,7 +38,7 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message)
dataReceiverClient.connect();
dataReceiverClient.subscribe(topics, (dataSubscription, topic) -> {
try {
var payload = JsonFormat.printer().print(dataSubscription);
var payload = mapper.writeValueAsString(dataSubscription);
if (session.isOpen()) {
session.sendMessage(new TextMessage(payload));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import org.springframework.web.socket.server.HandshakeInterceptor;

import com.cisco.tiedie.clients.DataReceiverClient;
import com.google.protobuf.util.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class GattHandler extends TextWebSocketHandler implements HandshakeInterceptor {
@Autowired
DataReceiverClient dataReceiverClient;

ObjectMapper mapper = new ObjectMapper();

@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
if (!session.isOpen()) {
Expand All @@ -39,7 +41,7 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message)

dataReceiverClient.subscribe(topic, (dataSubscription) -> {
try {
var payload = JsonFormat.printer().print(dataSubscription);
var payload = mapper.writeValueAsString(dataSubscription);
if (session.isOpen()) {
session.sendMessage(new TextMessage(payload));
}
Expand Down

This file was deleted.

17 changes: 1 addition & 16 deletions java-sdk/sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ plugins {
id 'java-library'
id "io.freefair.lombok" version "8.0.0-rc4"
id 'jacoco'
id 'com.google.protobuf' version '0.9.2'
}

group 'com.cisco.tiedie'
Expand All @@ -23,8 +22,8 @@ dependencies {
implementation platform('com.squareup.okhttp3:okhttp-bom:4.10.0')
implementation 'com.squareup.okhttp3:okhttp'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.14.2'
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
implementation 'com.google.protobuf:protobuf-java:3.22.2'

testImplementation 'com.squareup.okhttp3:mockwebserver'
testImplementation platform('org.junit:junit-bom:5.8.2')
Expand All @@ -42,18 +41,4 @@ test {

jacocoTestReport {
dependsOn test

afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: [
"com/cisco/tiedie/proto/**"
])
}))
}
}

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.22.2'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
package com.cisco.tiedie.clients;

import com.cisco.tiedie.auth.Authenticator;
import com.cisco.tiedie.proto.DataSubscription;
import com.cisco.tiedie.dto.telemetry.DataSubscription;
import com.cisco.tiedie.utils.ObjectMapperSingleton;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.NonNull;

import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
Expand Down Expand Up @@ -38,6 +41,8 @@ public class DataReceiverClient {
private final MqttClient mqttClient;
private final MqttConnectOptions mqttConnectOptions;

private final ObjectMapper mapper = ObjectMapperSingleton.getCborInstance();

/**
* Create a {@link DataReceiverClient} object.
*
Expand Down Expand Up @@ -89,7 +94,7 @@ public void subscribe(@NonNull String topic, Consumer<DataSubscription> callback

byte[] payload = message.getPayload();

DataSubscription dataSubscription = DataSubscription.parseFrom(payload);
DataSubscription dataSubscription = mapper.readValue(payload, DataSubscription.class);

callback.accept(dataSubscription);
});
Expand Down Expand Up @@ -121,7 +126,7 @@ public void subscribe(@NonNull List<String> topics, BiConsumer<DataSubscription,

byte[] payload = message.getPayload();

DataSubscription dataSubscription = DataSubscription.parseFrom(payload);
DataSubscription dataSubscription = mapper.readValue(payload, DataSubscription.class);

callback.accept(dataSubscription, topic);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public List<String> getSchemas() {
}

if (endpointAppsExtension != null) {
schemas.add("urn:ietf:params:scim:schemas:extension:endpointApps:2.0:Device");
schemas.add("urn:ietf:params:scim:schemas:extension:endpointAppsExt:2.0:Device");
}

return schemas;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cisco.tiedie.dto.telemetry;

import lombok.Data;

@Data
public class BleAdvertisement {
String macAddress;
int rssi;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.cisco.tiedie.dto.telemetry;

import lombok.Data;

@Data
public class BleConnectionStatus {
String macAddress;
boolean connected;
int reason;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cisco.tiedie.dto.telemetry;

import lombok.Data;

@Data
public class BleSubscription {
String serviceId;
String characteristicId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.cisco.tiedie.dto.telemetry;

import lombok.Data;

@Data
public class DataSubscription {
byte[] data;
float timestamp;
String deviceId;
String apMacAddress;
BleSubscription BleSubscription;
BleAdvertisement bleAdvertisement;
BleConnectionStatus bleConnectionStatus;
ZigbeeSubscription zigbeeSubscription;
RawPayload rawPayload;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.cisco.tiedie.dto.telemetry;

import lombok.Data;

@Data
public class RawPayload {
String contextId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.cisco.tiedie.dto.telemetry;

import lombok.Data;

@Data
public class ZigbeeSubscription {
int endpointId;
int clusterId;
int attributeId;
int attributeType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper;


public class ObjectMapperSingleton {
private static ObjectMapper objectMapper;
private static CBORMapper cborMapper;

private ObjectMapperSingleton() {}

Expand All @@ -25,4 +27,10 @@ public static ObjectMapper getInstance() {
return objectMapper;
}

public static ObjectMapper getCborInstance() {
cborMapper = new CBORMapper();
cborMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

return cborMapper;
}
}
57 changes: 0 additions & 57 deletions java-sdk/sdk/src/main/proto/data_app.proto

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ void testEndpointExtension() throws JsonProcessingException {

var json = mapper.writeValueAsString(device);
var expected = "{\n" +
" \"schemas\" : [ \"urn:ietf:params:scim:schemas:core:2.0:Device\", \"urn:ietf:params:scim:schemas:extension:ble:2.0:Device\", \"urn:ietf:params:scim:schemas:extension:endpointApps:2.0:Device\" ],\n"
" \"schemas\" : [ \"urn:ietf:params:scim:schemas:core:2.0:Device\", \"urn:ietf:params:scim:schemas:extension:ble:2.0:Device\", \"urn:ietf:params:scim:schemas:extension:endpointAppsExt:2.0:Device\" ],\n"
+
" \"displayName\" : \"BLE Monitor\",\n" +
" \"active\" : false,\n" +
Expand Down

0 comments on commit 62ad79f

Please sign in to comment.