Search in sources :

Example 11 with AttributeKvEntry

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

the class BaseAttributesService method save.

@Override
public ListenableFuture<List<Void>> save(EntityId entityId, String scope, List<AttributeKvEntry> attributes) {
    validate(entityId, scope);
    attributes.forEach(attribute -> validate(attribute));
    List<ListenableFuture<Void>> futures = Lists.newArrayListWithExpectedSize(attributes.size());
    for (AttributeKvEntry attribute : attributes) {
        futures.add(attributesDao.save(entityId, scope, attribute));
    }
    return Futures.allAsList(futures);
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

Example 12 with AttributeKvEntry

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

the class CassandraBaseAttributesDao method convertResultToAttributesKvEntryList.

private List<AttributeKvEntry> convertResultToAttributesKvEntryList(ResultSet resultSet) {
    List<Row> rows = resultSet.all();
    List<AttributeKvEntry> entries = new ArrayList<>(rows.size());
    if (!rows.isEmpty()) {
        rows.forEach(row -> {
            String key = row.getString(ModelConstants.ATTRIBUTE_KEY_COLUMN);
            AttributeKvEntry kvEntry = convertResultToAttributesKvEntry(key, row);
            if (kvEntry != null) {
                entries.add(kvEntry);
            }
        });
    }
    return entries;
}
Also used : BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) ArrayList(java.util.ArrayList)

Example 13 with AttributeKvEntry

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

the class CassandraBaseAttributesDao method convertResultToAttributesKvEntry.

private AttributeKvEntry convertResultToAttributesKvEntry(String key, Row row) {
    AttributeKvEntry attributeEntry = null;
    if (row != null) {
        long lastUpdateTs = row.get(LAST_UPDATE_TS_COLUMN, Long.class);
        attributeEntry = new BaseAttributeKvEntry(CassandraBaseTimeseriesDao.toKvEntry(row, key), lastUpdateTs);
    }
    return attributeEntry;
}
Also used : BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)

Example 14 with AttributeKvEntry

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

the class AuditLogServiceImpl method constructActionData.

private <E extends BaseData<I> & HasName, I extends UUIDBased & EntityId> JsonNode constructActionData(I entityId, E entity, ActionType actionType, Object... additionalInfo) {
    ObjectNode actionData = objectMapper.createObjectNode();
    switch(actionType) {
        case ADDED:
        case UPDATED:
            ObjectNode entityNode = objectMapper.valueToTree(entity);
            if (entityId.getEntityType() == EntityType.DASHBOARD) {
                entityNode.put("configuration", "");
            }
            actionData.set("entity", entityNode);
            break;
        case DELETED:
        case ACTIVATED:
        case SUSPENDED:
        case CREDENTIALS_READ:
            String strEntityId = extractParameter(String.class, additionalInfo);
            actionData.put("entityId", strEntityId);
            break;
        case ATTRIBUTES_UPDATED:
            actionData.put("entityId", entityId.toString());
            String scope = extractParameter(String.class, 0, additionalInfo);
            List<AttributeKvEntry> attributes = extractParameter(List.class, 1, additionalInfo);
            actionData.put("scope", scope);
            ObjectNode attrsNode = objectMapper.createObjectNode();
            if (attributes != null) {
                for (AttributeKvEntry attr : attributes) {
                    attrsNode.put(attr.getKey(), attr.getValueAsString());
                }
            }
            actionData.set("attributes", attrsNode);
            break;
        case ATTRIBUTES_DELETED:
        case ATTRIBUTES_READ:
            actionData.put("entityId", entityId.toString());
            scope = extractParameter(String.class, 0, additionalInfo);
            actionData.put("scope", scope);
            List<String> keys = extractParameter(List.class, 1, additionalInfo);
            ArrayNode attrsArrayNode = actionData.putArray("attributes");
            if (keys != null) {
                keys.forEach(attrsArrayNode::add);
            }
            break;
        case RPC_CALL:
            actionData.put("entityId", entityId.toString());
            Boolean oneWay = extractParameter(Boolean.class, 1, additionalInfo);
            String method = extractParameter(String.class, 2, additionalInfo);
            String params = extractParameter(String.class, 3, additionalInfo);
            actionData.put("oneWay", oneWay);
            actionData.put("method", method);
            actionData.put("params", params);
            break;
        case CREDENTIALS_UPDATED:
            actionData.put("entityId", entityId.toString());
            DeviceCredentials deviceCredentials = extractParameter(DeviceCredentials.class, additionalInfo);
            actionData.set("credentials", objectMapper.valueToTree(deviceCredentials));
            break;
        case ASSIGNED_TO_CUSTOMER:
            strEntityId = extractParameter(String.class, 0, additionalInfo);
            String strCustomerId = extractParameter(String.class, 1, additionalInfo);
            String strCustomerName = extractParameter(String.class, 2, additionalInfo);
            actionData.put("entityId", strEntityId);
            actionData.put("assignedCustomerId", strCustomerId);
            actionData.put("assignedCustomerName", strCustomerName);
            break;
        case UNASSIGNED_FROM_CUSTOMER:
            strEntityId = extractParameter(String.class, 0, additionalInfo);
            strCustomerId = extractParameter(String.class, 1, additionalInfo);
            strCustomerName = extractParameter(String.class, 2, additionalInfo);
            actionData.put("entityId", strEntityId);
            actionData.put("unassignedCustomerId", strCustomerId);
            actionData.put("unassignedCustomerName", strCustomerName);
            break;
    }
    return actionData;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Example 15 with AttributeKvEntry

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

the class BaseAttributesServiceTest method saveAndFetch.

@Test
public void saveAndFetch() throws Exception {
    DeviceId deviceId = new DeviceId(UUIDs.timeBased());
    KvEntry attrValue = new StringDataEntry("attribute1", "value1");
    AttributeKvEntry attr = new BaseAttributeKvEntry(attrValue, 42L);
    attributesService.save(deviceId, DataConstants.CLIENT_SCOPE, Collections.singletonList(attr)).get();
    Optional<AttributeKvEntry> saved = attributesService.find(deviceId, DataConstants.CLIENT_SCOPE, attr.getKey()).get();
    Assert.assertTrue(saved.isPresent());
    Assert.assertEquals(attr, saved.get());
}
Also used : BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) DeviceId(org.thingsboard.server.common.data.id.DeviceId) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) Test(org.junit.Test) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest)

Aggregations

AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)16 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)10 DeviceId (org.thingsboard.server.common.data.id.DeviceId)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 BooleanDataEntry (org.thingsboard.server.common.data.kv.BooleanDataEntry)5 DeviceAttributes (org.thingsboard.server.extensions.api.device.DeviceAttributes)5 DeviceMetaData (org.thingsboard.server.extensions.api.device.DeviceMetaData)5 DoubleDataEntry (org.thingsboard.server.common.data.kv.DoubleDataEntry)4 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)3 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)3 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)3 HashMap (java.util.HashMap)2 ActorContext (akka.actor.ActorContext)1 ActorRef (akka.actor.ActorRef)1 LoggingAdapter (akka.event.LoggingAdapter)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 java.util (java.util)1