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);
}
});
}
Aggregations