Search in sources :

Example 1 with TelemetryUploadRequest

use of org.thingsboard.server.common.msg.core.TelemetryUploadRequest in project thingsboard by thingsboard.

the class TelemetryRestMsgHandler method handleHttpPostTimeseries.

private void handleHttpPostTimeseries(PluginContext ctx, PluginRestMsg msg, RestRequest request, EntityId entityId, long ttl) {
    TelemetryUploadRequest telemetryRequest;
    JsonElement telemetryJson;
    try {
        telemetryJson = new JsonParser().parse(request.getRequestBody());
    } catch (Exception e) {
        throw new IllegalArgumentException("Unable to parse timeseries payload: Invalid JSON body!");
    }
    try {
        telemetryRequest = JsonConverter.convertToTelemetry(telemetryJson);
    } catch (JsonSyntaxException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
    List<TsKvEntry> entries = new ArrayList<>();
    for (Map.Entry<Long, List<KvEntry>> entry : telemetryRequest.getData().entrySet()) {
        for (KvEntry kv : entry.getValue()) {
            entries.add(new BasicTsKvEntry(entry.getKey(), kv));
        }
    }
    if (entries.isEmpty()) {
        throw new IllegalArgumentException("No timeseries data found in request body!");
    }
    ctx.saveTsData(entityId, entries, ttl, new PluginCallback<Void>() {

        @Override
        public void onSuccess(PluginContext ctx, Void value) {
            msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.OK));
            subscriptionManager.onTimeseriesUpdateFromServer(ctx, entityId, entries);
        }

        @Override
        public void onFailure(PluginContext ctx, Exception e) {
            log.error("Failed to save attributes", e);
            handleError(e, msg, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    });
}
Also used : TelemetryUploadRequest(org.thingsboard.server.common.msg.core.TelemetryUploadRequest) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) ServletException(javax.servlet.ServletException) UncheckedApiException(org.thingsboard.server.extensions.api.exception.UncheckedApiException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) InvalidParametersException(org.thingsboard.server.extensions.api.exception.InvalidParametersException) ToErrorResponseEntity(org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity) ResponseEntity(org.springframework.http.ResponseEntity) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 ResponseEntity (org.springframework.http.ResponseEntity)1 TelemetryUploadRequest (org.thingsboard.server.common.msg.core.TelemetryUploadRequest)1 InvalidParametersException (org.thingsboard.server.extensions.api.exception.InvalidParametersException)1 ToErrorResponseEntity (org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity)1 UncheckedApiException (org.thingsboard.server.extensions.api.exception.UncheckedApiException)1 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)1