added new efento endpoints support

This commit is contained in:
ShvaykaD 2021-11-01 13:46:44 +02:00
parent b157827e44
commit 467a94f688
2 changed files with 44 additions and 9 deletions

View File

@ -39,7 +39,10 @@ public class CoapTransportService implements TbTransportService {
private static final String V1 = "v1"; private static final String V1 = "v1";
private static final String API = "api"; private static final String API = "api";
private static final String EFENTO = "efento"; private static final String EFENTO = "efento";
private static final String MEASUREMENTS = "m"; public static final String MEASUREMENTS = "m";
public static final String DEVICE_INFO = "i";
public static final String CONFIGURATION = "c";
public static final String CURRENT_TIMESTAMP = "t";
@Autowired @Autowired
private CoapServerService coapServerService; private CoapServerService coapServerService;
@ -56,9 +59,11 @@ public class CoapTransportService implements TbTransportService {
CoapResource api = new CoapResource(API); CoapResource api = new CoapResource(API);
api.add(new CoapTransportResource(coapTransportContext, coapServerService, V1)); api.add(new CoapTransportResource(coapTransportContext, coapServerService, V1));
CoapResource efento = new CoapResource(EFENTO); CoapEfentoTransportResource efento = new CoapEfentoTransportResource(coapTransportContext, EFENTO);
CoapEfentoTransportResource efentoMeasurementsTransportResource = new CoapEfentoTransportResource(coapTransportContext, MEASUREMENTS); efento.add(new CoapResource(MEASUREMENTS));
efento.add(efentoMeasurementsTransportResource); efento.add(new CoapResource(DEVICE_INFO));
efento.add(new CoapResource(CONFIGURATION));
efento.add(new CoapResource(CURRENT_TIMESTAMP));
coapServer.add(api); coapServer.add(api);
coapServer.add(efento); coapServer.add(efento);
coapServer.add(new OtaPackageTransportResource(coapTransportContext, OtaPackageType.FIRMWARE)); coapServer.add(new OtaPackageTransportResource(coapTransportContext, OtaPackageType.FIRMWARE));

View File

@ -41,6 +41,7 @@ import org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback;
import org.thingsboard.server.transport.coap.callback.CoapOkCallback; import org.thingsboard.server.transport.coap.callback.CoapOkCallback;
import org.thingsboard.server.transport.coap.efento.utils.CoapEfentoUtils; import org.thingsboard.server.transport.coap.efento.utils.CoapEfentoUtils;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -48,11 +49,15 @@ import java.util.TreeMap;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.thingsboard.server.transport.coap.CoapTransportService.CONFIGURATION;
import static org.thingsboard.server.transport.coap.CoapTransportService.CURRENT_TIMESTAMP;
import static org.thingsboard.server.transport.coap.CoapTransportService.DEVICE_INFO;
import static org.thingsboard.server.transport.coap.CoapTransportService.MEASUREMENTS;
@Slf4j @Slf4j
public class CoapEfentoTransportResource extends AbstractCoapTransportResource { public class CoapEfentoTransportResource extends AbstractCoapTransportResource {
private static final int MEASUREMENTS_POSITION = 2; private static final int CHILD_RESOURCE_POSITION = 2;
private static final String MEASUREMENTS = "m";
public CoapEfentoTransportResource(CoapTransportContext context, String name) { public CoapEfentoTransportResource(CoapTransportContext context, String name) {
super(context, name); super(context, name);
@ -63,7 +68,17 @@ public class CoapEfentoTransportResource extends AbstractCoapTransportResource {
@Override @Override
protected void processHandleGet(CoapExchange exchange) { protected void processHandleGet(CoapExchange exchange) {
exchange.respond(CoAP.ResponseCode.METHOD_NOT_ALLOWED); Exchange advanced = exchange.advanced();
Request request = advanced.getRequest();
List<String> uriPath = request.getOptions().getUriPath();
boolean validPath = uriPath.size() == CHILD_RESOURCE_POSITION && uriPath.get(1).equals(CURRENT_TIMESTAMP);
if (!validPath) {
exchange.respond(CoAP.ResponseCode.BAD_REQUEST);
} else {
int dateInSec = (int) (System.currentTimeMillis() / 1000);
byte[] bytes = ByteBuffer.allocate(4).putInt(dateInSec).array();
exchange.respond(CoAP.ResponseCode.CONTENT, bytes);
}
} }
@Override @Override
@ -71,11 +86,26 @@ public class CoapEfentoTransportResource extends AbstractCoapTransportResource {
Exchange advanced = exchange.advanced(); Exchange advanced = exchange.advanced();
Request request = advanced.getRequest(); Request request = advanced.getRequest();
List<String> uriPath = request.getOptions().getUriPath(); List<String> uriPath = request.getOptions().getUriPath();
boolean validPath = uriPath.size() == MEASUREMENTS_POSITION && uriPath.get(1).equals(MEASUREMENTS); if (uriPath.size() != CHILD_RESOURCE_POSITION) {
if (!validPath) {
exchange.respond(CoAP.ResponseCode.BAD_REQUEST); exchange.respond(CoAP.ResponseCode.BAD_REQUEST);
return; return;
} }
String requestType = uriPath.get(1);
switch (requestType) {
case MEASUREMENTS:
processMeasurementsRequest(exchange, request);
break;
case DEVICE_INFO:
case CONFIGURATION:
exchange.respond(CoAP.ResponseCode.CREATED);
break;
default:
exchange.respond(CoAP.ResponseCode.BAD_REQUEST);
break;
}
}
private void processMeasurementsRequest(CoapExchange exchange, Request request) {
byte[] bytes = request.getPayload(); byte[] bytes = request.getPayload();
try { try {
MeasurementsProtos.ProtoMeasurements protoMeasurements = MeasurementsProtos.ProtoMeasurements.parseFrom(bytes); MeasurementsProtos.ProtoMeasurements protoMeasurements = MeasurementsProtos.ProtoMeasurements.parseFrom(bytes);