Search in sources :

Example 1 with DataType

use of org.thingsboard.server.common.data.kv.DataType in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesDao method save.

@Override
public ListenableFuture<Void> save(EntityId entityId, TsKvEntry tsKvEntry, long ttl) {
    long partition = toPartitionTs(tsKvEntry.getTs());
    DataType type = tsKvEntry.getDataType();
    BoundStatement stmt = (ttl == 0 ? getSaveStmt(type) : getSaveTtlStmt(type)).bind();
    stmt.setString(0, entityId.getEntityType().name()).setUUID(1, entityId.getId()).setString(2, tsKvEntry.getKey()).setLong(3, partition).setLong(4, tsKvEntry.getTs());
    addValue(tsKvEntry, stmt, 5);
    if (ttl > 0) {
        stmt.setInt(6, (int) ttl);
    }
    return getFuture(executeAsyncWrite(stmt), rs -> null);
}
Also used : DataType(org.thingsboard.server.common.data.kv.DataType)

Example 2 with DataType

use of org.thingsboard.server.common.data.kv.DataType in project thingsboard by thingsboard.

the class TbSubscriptionUtils method getKvEntry.

private static KvEntry getKvEntry(KeyValueProto proto) {
    KvEntry entry = null;
    DataType type = DataType.values()[proto.getType().getNumber()];
    switch(type) {
        case BOOLEAN:
            entry = new BooleanDataEntry(proto.getKey(), proto.getBoolV());
            break;
        case LONG:
            entry = new LongDataEntry(proto.getKey(), proto.getLongV());
            break;
        case DOUBLE:
            entry = new DoubleDataEntry(proto.getKey(), proto.getDoubleV());
            break;
        case STRING:
            entry = new StringDataEntry(proto.getKey(), proto.getStringV());
            break;
        case JSON:
            entry = new JsonDataEntry(proto.getKey(), proto.getJsonV());
            break;
    }
    return entry;
}
Also used : BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) DataType(org.thingsboard.server.common.data.kv.DataType) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) JsonDataEntry(org.thingsboard.server.common.data.kv.JsonDataEntry)

Example 3 with DataType

use of org.thingsboard.server.common.data.kv.DataType in project thingsboard by thingsboard.

the class AbstractBulkImportService method parseData.

private List<EntityData> parseData(BulkImportRequest request) throws Exception {
    List<List<String>> records = CsvUtils.parseCsv(request.getFile(), request.getMapping().getDelimiter());
    AtomicInteger linesCounter = new AtomicInteger(0);
    if (request.getMapping().getHeader()) {
        records.remove(0);
        linesCounter.incrementAndGet();
    }
    List<ColumnMapping> columnsMappings = request.getMapping().getColumns();
    return records.stream().map(record -> {
        EntityData entityData = new EntityData();
        Stream.iterate(0, i -> i < record.size(), i -> i + 1).map(i -> Map.entry(columnsMappings.get(i), record.get(i))).filter(entry -> StringUtils.isNotEmpty(entry.getValue())).forEach(entry -> {
            if (!entry.getKey().getType().isKv()) {
                entityData.getFields().put(entry.getKey().getType(), entry.getValue());
            } else {
                Map.Entry<DataType, Object> castResult = TypeCastUtil.castValue(entry.getValue());
                entityData.getKvs().put(entry.getKey(), new ParsedValue(castResult.getValue(), castResult.getKey()));
            }
        });
        entityData.setLineNumber(linesCounter.incrementAndGet());
        return entityData;
    }).collect(Collectors.toList());
}
Also used : JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) CsvUtils(org.thingsboard.server.utils.CsvUtils) SneakyThrows(lombok.SneakyThrows) DonAsynchron(org.thingsboard.common.util.DonAsynchron) Autowired(org.springframework.beans.factory.annotation.Autowired) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) TelemetrySubscriptionService(org.thingsboard.server.service.telemetry.TelemetrySubscriptionService) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) PreDestroy(javax.annotation.PreDestroy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) JsonConverter(org.thingsboard.server.common.transport.adaptor.JsonConverter) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) TypeCastUtil(org.thingsboard.server.utils.TypeCastUtil) HasTenantId(org.thingsboard.server.common.data.HasTenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) SecurityContext(org.springframework.security.core.context.SecurityContext) PostConstruct(javax.annotation.PostConstruct) HasId(org.thingsboard.server.common.data.id.HasId) Operation(org.thingsboard.server.service.security.permission.Operation) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ActionType(org.thingsboard.server.common.data.audit.ActionType) DataType(org.thingsboard.server.common.data.kv.DataType) EntityId(org.thingsboard.server.common.data.id.EntityId) BaseController(org.thingsboard.server.controller.BaseController) JsonPrimitive(com.google.gson.JsonPrimitive) UUIDBased(org.thingsboard.server.common.data.id.UUIDBased) Nullable(javax.annotation.Nullable) AccessValidator(org.thingsboard.server.service.security.AccessValidator) FutureCallback(com.google.common.util.concurrent.FutureCallback) ColumnMapping(org.thingsboard.server.service.importing.BulkImportRequest.ColumnMapping) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EntityActionService(org.thingsboard.server.service.action.EntityActionService) Data(lombok.Data) Resource(org.thingsboard.server.service.security.permission.Resource) AccessControlService(org.thingsboard.server.service.security.permission.AccessControlService) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) ColumnMapping(org.thingsboard.server.service.importing.BulkImportRequest.ColumnMapping)

Example 4 with DataType

use of org.thingsboard.server.common.data.kv.DataType in project thingsboard by thingsboard.

the class SnmpTransportService method onToDeviceRpcRequest.

public void onToDeviceRpcRequest(DeviceSessionContext sessionContext, TransportProtos.ToDeviceRpcRequestMsg toDeviceRpcRequestMsg) {
    SnmpMethod snmpMethod = SnmpMethod.valueOf(toDeviceRpcRequestMsg.getMethodName());
    JsonObject params = JsonConverter.parse(toDeviceRpcRequestMsg.getParams()).getAsJsonObject();
    String key = Optional.ofNullable(params.get("key")).map(JsonElement::getAsString).orElse(null);
    String value = Optional.ofNullable(params.get("value")).map(JsonElement::getAsString).orElse(null);
    if (value == null && snmpMethod == SnmpMethod.SET) {
        throw new IllegalArgumentException("Value must be specified for SNMP method 'SET'");
    }
    SnmpCommunicationConfig communicationConfig = sessionContext.getProfileTransportConfiguration().getCommunicationConfigs().stream().filter(config -> config.getSpec() == SnmpCommunicationSpec.TO_DEVICE_RPC_REQUEST).findFirst().orElseThrow(() -> new IllegalArgumentException("No communication config found with RPC spec"));
    SnmpMapping snmpMapping = communicationConfig.getAllMappings().stream().filter(mapping -> mapping.getKey().equals(key)).findFirst().orElseThrow(() -> new IllegalArgumentException("No SNMP mapping found in the config for specified key"));
    String oid = snmpMapping.getOid();
    DataType dataType = snmpMapping.getDataType();
    PDU request = pduService.createSingleVariablePdu(sessionContext, snmpMethod, oid, value, dataType);
    RequestInfo requestInfo = new RequestInfo(toDeviceRpcRequestMsg.getRequestId(), communicationConfig.getSpec(), communicationConfig.getAllMappings());
    sendRequest(sessionContext, request, requestInfo);
}
Also used : SnmpCommunicationConfig(org.thingsboard.server.common.data.transport.snmp.config.SnmpCommunicationConfig) RepeatingQueryingSnmpCommunicationConfig(org.thingsboard.server.common.data.transport.snmp.config.RepeatingQueryingSnmpCommunicationConfig) SnmpMapping(org.thingsboard.server.common.data.transport.snmp.SnmpMapping) PDU(org.snmp4j.PDU) SnmpMethod(org.thingsboard.server.common.data.transport.snmp.SnmpMethod) JsonObject(com.google.gson.JsonObject) DataType(org.thingsboard.server.common.data.kv.DataType) OctetString(org.snmp4j.smi.OctetString)

Example 5 with DataType

use of org.thingsboard.server.common.data.kv.DataType in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesDao method save.

@Override
public ListenableFuture<Integer> save(TenantId tenantId, EntityId entityId, TsKvEntry tsKvEntry, long ttl) {
    List<ListenableFuture<Void>> futures = new ArrayList<>();
    ttl = computeTtl(ttl);
    int dataPointDays = tsKvEntry.getDataPoints() * Math.max(1, (int) (ttl / SECONDS_IN_DAY));
    long partition = toPartitionTs(tsKvEntry.getTs());
    DataType type = tsKvEntry.getDataType();
    if (setNullValuesEnabled) {
        processSetNullValues(tenantId, entityId, tsKvEntry, ttl, futures, partition, type);
    }
    BoundStatementBuilder stmtBuilder = new BoundStatementBuilder((ttl == 0 ? getSaveStmt(type) : getSaveTtlStmt(type)).bind());
    stmtBuilder.setString(0, entityId.getEntityType().name()).setUuid(1, entityId.getId()).setString(2, tsKvEntry.getKey()).setLong(3, partition).setLong(4, tsKvEntry.getTs());
    addValue(tsKvEntry, stmtBuilder, 5);
    if (ttl > 0) {
        stmtBuilder.setInt(6, (int) ttl);
    }
    BoundStatement stmt = stmtBuilder.build();
    futures.add(getFuture(executeAsyncWrite(tenantId, stmt), rs -> null));
    return Futures.transform(Futures.allAsList(futures), result -> dataPointDays, MoreExecutors.directExecutor());
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) Arrays(java.util.Arrays) Autowired(org.springframework.beans.factory.annotation.Autowired) TenantId(org.thingsboard.server.common.data.id.TenantId) AggregationTimeseriesDao(org.thingsboard.server.dao.sqlts.AggregationTimeseriesDao) TbResultSet(org.thingsboard.server.dao.nosql.TbResultSet) PreDestroy(javax.annotation.PreDestroy) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Profiles(org.springframework.core.env.Profiles) ZoneOffset(java.time.ZoneOffset) Select(com.datastax.oss.driver.api.querybuilder.select.Select) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) Function(com.google.common.base.Function) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) DeleteTsKvQuery(org.thingsboard.server.common.data.kv.DeleteTsKvQuery) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) QueryBuilder.literal(com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal) Environment(org.springframework.core.env.Environment) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) LocalDateTime(java.time.LocalDateTime) NoSqlTsDao(org.thingsboard.server.dao.util.NoSqlTsDao) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) DataType(org.thingsboard.server.common.data.kv.DataType) EntityId(org.thingsboard.server.common.data.id.EntityId) Row(com.datastax.oss.driver.api.core.cql.Row) Nullable(javax.annotation.Nullable) TbResultSetFuture(org.thingsboard.server.dao.nosql.TbResultSetFuture) ReentrantLock(java.util.concurrent.locks.ReentrantLock) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) Aggregation(org.thingsboard.server.common.data.kv.Aggregation) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) FutureCallback(com.google.common.util.concurrent.FutureCallback) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) Component(org.springframework.stereotype.Component) Lock(java.util.concurrent.locks.Lock) ChronoUnit(java.time.temporal.ChronoUnit) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) Collections(java.util.Collections) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DataType(org.thingsboard.server.common.data.kv.DataType) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Aggregations

DataType (org.thingsboard.server.common.data.kv.DataType)5 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)3 FutureCallback (com.google.common.util.concurrent.FutureCallback)2 JsonObject (com.google.gson.JsonObject)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 Nullable (javax.annotation.Nullable)2 PostConstruct (javax.annotation.PostConstruct)2 PreDestroy (javax.annotation.PreDestroy)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 EntityId (org.thingsboard.server.common.data.id.EntityId)2 TenantId (org.thingsboard.server.common.data.id.TenantId)2 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)2 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)2 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)2 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)1 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)1