Search in sources :

Example 1 with TsKvEntry

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

the class DefaultActorServiceTest method testBasicPostWithSyncSession.

@Test
public void testBasicPostWithSyncSession() throws Exception {
    SessionContext ssnCtx = mock(SessionContext.class);
    KvEntry entry1 = new StringDataEntry("key1", "value1");
    KvEntry entry2 = new StringDataEntry("key2", "value2");
    BasicTelemetryUploadRequest telemetry = new BasicTelemetryUploadRequest();
    long ts = 42;
    telemetry.add(ts, entry1);
    telemetry.add(ts, entry2);
    BasicAdaptorToSessionActorMsg msg = new BasicAdaptorToSessionActorMsg(ssnCtx, telemetry);
    DeviceId deviceId = new DeviceId(UUID.randomUUID());
    DeviceCredentialsFilter filter = new DeviceTokenCredentials("token1");
    Device device = mock(Device.class);
    when(device.getId()).thenReturn(deviceId);
    when(device.getTenantId()).thenReturn(tenantId);
    when(ssnCtx.getSessionId()).thenReturn(new DummySessionID("session1"));
    when(ssnCtx.getSessionType()).thenReturn(SessionType.SYNC);
    when(deviceAuthService.process(filter)).thenReturn(DeviceAuthResult.of(deviceId));
    when(deviceService.findDeviceById(deviceId)).thenReturn(device);
    ObjectMapper ruleMapper = new ObjectMapper();
    when(ruleMock.getFilters()).thenReturn(ruleMapper.readTree(FILTERS_CONFIGURATION));
    when(ruleMock.getAction()).thenReturn(ruleMapper.readTree(ACTION_CONFIGURATION));
    ComponentDescriptor filterComp = new ComponentDescriptor();
    filterComp.setClazz("org.thingsboard.server.extensions.core.filter.MsgTypeFilter");
    filterComp.setType(ComponentType.FILTER);
    when(componentService.getComponent("org.thingsboard.server.extensions.core.filter.MsgTypeFilter")).thenReturn(Optional.of(filterComp));
    ComponentDescriptor actionComp = new ComponentDescriptor();
    actionComp.setClazz("org.thingsboard.server.extensions.core.action.telemetry.TelemetryPluginAction");
    actionComp.setType(ComponentType.ACTION);
    when(componentService.getComponent("org.thingsboard.server.extensions.core.action.telemetry.TelemetryPluginAction")).thenReturn(Optional.of(actionComp));
    ObjectMapper pluginMapper = new ObjectMapper();
    JsonNode pluginAdditionalInfo = pluginMapper.readTree(PLUGIN_CONFIGURATION);
    when(pluginMock.getConfiguration()).thenReturn(pluginAdditionalInfo);
    when(pluginMock.getClazz()).thenReturn(TelemetryStoragePlugin.class.getName());
    when(attributesService.findAll(deviceId, DataConstants.CLIENT_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
    when(attributesService.findAll(deviceId, DataConstants.SHARED_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
    when(attributesService.findAll(deviceId, DataConstants.SERVER_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
    initActorSystem();
    Thread.sleep(1000);
    actorService.process(new BasicToDeviceActorSessionMsg(device, msg));
    // Check that device data was saved to DB;
    List<TsKvEntry> expected = new ArrayList<>();
    expected.add(new BasicTsKvEntry(ts, entry1));
    expected.add(new BasicTsKvEntry(ts, entry2));
    verify(tsService, Mockito.timeout(5000)).save(deviceId, expected, 0L);
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) Device(org.thingsboard.server.common.data.Device) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) TelemetryStoragePlugin(org.thingsboard.server.extensions.core.plugin.telemetry.TelemetryStoragePlugin) JsonNode(com.fasterxml.jackson.databind.JsonNode) DeviceCredentialsFilter(org.thingsboard.server.common.data.security.DeviceCredentialsFilter) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) BasicTelemetryUploadRequest(org.thingsboard.server.common.msg.core.BasicTelemetryUploadRequest) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with TsKvEntry

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

the class TelemetryRuleMsgHandler method handleTelemetryUploadRequest.

@Override
public void handleTelemetryUploadRequest(PluginContext ctx, TenantId tenantId, RuleId ruleId, TelemetryUploadRequestRuleToPluginMsg msg) {
    TelemetryUploadRequest request = msg.getPayload();
    List<TsKvEntry> tsKvEntries = new ArrayList<>();
    for (Map.Entry<Long, List<KvEntry>> entry : request.getData().entrySet()) {
        for (KvEntry kv : entry.getValue()) {
            tsKvEntries.add(new BasicTsKvEntry(entry.getKey(), kv));
        }
    }
    ctx.saveTsData(msg.getDeviceId(), tsKvEntries, msg.getTtl(), new PluginCallback<Void>() {

        @Override
        public void onSuccess(PluginContext ctx, Void data) {
            ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(request.getMsgType(), request.getRequestId())));
            subscriptionManager.onLocalSubscriptionUpdate(ctx, msg.getDeviceId(), SubscriptionType.TIMESERIES, s -> prepareSubscriptionUpdate(request, s));
        }

        @Override
        public void onFailure(PluginContext ctx, Exception e) {
            log.error("Failed to process telemetry upload request", e);
            ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onError(request.getMsgType(), request.getRequestId(), e)));
        }
    });
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) RuleId(org.thingsboard.server.common.data.id.RuleId) java.util(java.util) DataConstants(org.thingsboard.server.common.data.DataConstants) org.thingsboard.server.common.msg.core(org.thingsboard.server.common.msg.core) UpdateAttributesRequestRuleToPluginMsg(org.thingsboard.server.extensions.api.plugins.msg.UpdateAttributesRequestRuleToPluginMsg) SubscriptionManager(org.thingsboard.server.extensions.core.plugin.telemetry.SubscriptionManager) ResponsePluginToRuleMsg(org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg) BasicAttributeKVMsg(org.thingsboard.server.common.msg.kv.BasicAttributeKVMsg) Collectors(java.util.stream.Collectors) TenantId(org.thingsboard.server.common.data.id.TenantId) SubscriptionType(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionType) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) GetAttributesRequestRuleToPluginMsg(org.thingsboard.server.extensions.api.plugins.msg.GetAttributesRequestRuleToPluginMsg) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) DefaultRuleMsgHandler(org.thingsboard.server.extensions.api.plugins.handlers.DefaultRuleMsgHandler) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback) TelemetryUploadRequestRuleToPluginMsg(org.thingsboard.server.extensions.api.plugins.msg.TelemetryUploadRequestRuleToPluginMsg) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) ResponsePluginToRuleMsg(org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg)

Aggregations

BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)2 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)2 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Test (org.junit.Test)1 DataConstants (org.thingsboard.server.common.data.DataConstants)1 Device (org.thingsboard.server.common.data.Device)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 RuleId (org.thingsboard.server.common.data.id.RuleId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)1 ComponentDescriptor (org.thingsboard.server.common.data.plugin.ComponentDescriptor)1 DeviceCredentialsFilter (org.thingsboard.server.common.data.security.DeviceCredentialsFilter)1 DeviceTokenCredentials (org.thingsboard.server.common.data.security.DeviceTokenCredentials)1 org.thingsboard.server.common.msg.core (org.thingsboard.server.common.msg.core)1