committed by
GitHub
49 changed files with 1002 additions and 168 deletions
@ -0,0 +1,79 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.processor.cf; |
|||
|
|||
import com.datastax.oss.driver.api.core.uuid.Uuids; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.data.util.Pair; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.thingsboard.server.common.data.cf.CalculatedField; |
|||
import org.thingsboard.server.common.data.id.CalculatedFieldId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.service.DataValidator; |
|||
import org.thingsboard.server.gen.edge.v1.CalculatedFieldUpdateMsg; |
|||
import org.thingsboard.server.service.edge.rpc.processor.BaseEdgeProcessor; |
|||
|
|||
@Slf4j |
|||
public abstract class BaseCalculatedFieldProcessor extends BaseEdgeProcessor { |
|||
|
|||
@Autowired |
|||
private DataValidator<CalculatedField> calculatedFieldValidator; |
|||
|
|||
protected Pair<Boolean, Boolean> saveOrUpdateCalculatedField(TenantId tenantId, CalculatedFieldId calculatedFieldId, CalculatedFieldUpdateMsg calculatedFieldUpdateMsg) { |
|||
boolean isCreated = false; |
|||
boolean isNameUpdated = false; |
|||
try { |
|||
CalculatedField calculatedField = JacksonUtil.fromString(calculatedFieldUpdateMsg.getEntity(), CalculatedField.class, true); |
|||
if (calculatedField == null) { |
|||
throw new RuntimeException("[{" + tenantId + "}] calculatedFieldUpdateMsg {" + calculatedFieldUpdateMsg + " } cannot be converted to calculatedField"); |
|||
} |
|||
|
|||
CalculatedField calculatedFieldById = edgeCtx.getCalculatedFieldService().findById(tenantId, calculatedFieldId); |
|||
if (calculatedFieldById == null) { |
|||
calculatedField.setCreatedTime(Uuids.unixTimestamp(calculatedFieldId.getId())); |
|||
isCreated = true; |
|||
calculatedField.setId(null); |
|||
} else { |
|||
calculatedField.setId(calculatedFieldId); |
|||
} |
|||
|
|||
String calculatedFieldName = calculatedField.getName(); |
|||
CalculatedField calculatedFieldByName = edgeCtx.getCalculatedFieldService().findByEntityIdAndName(calculatedField.getEntityId(), calculatedFieldName); |
|||
if (calculatedFieldByName != null && !calculatedFieldByName.getId().equals(calculatedFieldId)) { |
|||
calculatedFieldName = calculatedFieldName + "_" + StringUtils.randomAlphabetic(15); |
|||
log.warn("[{}] calculatedField with name {} already exists. Renaming calculatedField name to {}", |
|||
tenantId, calculatedField.getName(), calculatedFieldByName.getName()); |
|||
isNameUpdated = true; |
|||
} |
|||
calculatedField.setName(calculatedFieldName); |
|||
|
|||
calculatedFieldValidator.validate(calculatedField, CalculatedField::getTenantId); |
|||
|
|||
if (isCreated) { |
|||
calculatedField.setId(calculatedFieldId); |
|||
} |
|||
|
|||
edgeCtx.getCalculatedFieldService().save(calculatedField, false); |
|||
} catch (Exception e) { |
|||
log.error("[{}] Failed to process calculatedField update msg [{}]", tenantId, calculatedFieldUpdateMsg, e); |
|||
throw e; |
|||
} |
|||
return Pair.of(isCreated, isNameUpdated); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,169 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.processor.cf; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.data.util.Pair; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.cf.CalculatedField; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.CalculatedFieldId; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.EntityIdFactory; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.msg.TbMsgType; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
import org.thingsboard.server.dao.exception.DataValidationException; |
|||
import org.thingsboard.server.gen.edge.v1.CalculatedFieldUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.DownlinkMsg; |
|||
import org.thingsboard.server.gen.edge.v1.EdgeVersion; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
import org.thingsboard.server.gen.transport.TransportProtos; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.edge.EdgeMsgConstructorUtils; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
@TbCoreComponent |
|||
public class CalculatedFieldEdgeProcessor extends BaseCalculatedFieldProcessor implements CalculatedFieldProcessor { |
|||
|
|||
@Override |
|||
public ListenableFuture<Void> processCalculatedFieldMsgFromEdge(TenantId tenantId, Edge edge, CalculatedFieldUpdateMsg calculatedFieldUpdateMsg) { |
|||
CalculatedFieldId calculatedFieldId = new CalculatedFieldId(new UUID(calculatedFieldUpdateMsg.getIdMSB(), calculatedFieldUpdateMsg.getIdLSB())); |
|||
try { |
|||
edgeSynchronizationManager.getEdgeId().set(edge.getId()); |
|||
|
|||
switch (calculatedFieldUpdateMsg.getMsgType()) { |
|||
case ENTITY_CREATED_RPC_MESSAGE: |
|||
case ENTITY_UPDATED_RPC_MESSAGE: |
|||
processCalculatedField(tenantId, calculatedFieldId, calculatedFieldUpdateMsg, edge); |
|||
return Futures.immediateFuture(null); |
|||
case ENTITY_DELETED_RPC_MESSAGE: |
|||
CalculatedField calculatedField = edgeCtx.getCalculatedFieldService().findById(tenantId, calculatedFieldId); |
|||
if (calculatedField != null) { |
|||
edgeCtx.getCalculatedFieldService().deleteCalculatedField(tenantId, calculatedFieldId); |
|||
} |
|||
return Futures.immediateFuture(null); |
|||
case UNRECOGNIZED: |
|||
default: |
|||
return handleUnsupportedMsgType(calculatedFieldUpdateMsg.getMsgType()); |
|||
} |
|||
} catch (DataValidationException e) { |
|||
if (e.getMessage().contains("limit reached")) { |
|||
log.warn("[{}] Number of allowed calculatedField violated {}", tenantId, calculatedFieldUpdateMsg, e); |
|||
return Futures.immediateFuture(null); |
|||
} else { |
|||
return Futures.immediateFailedFuture(e); |
|||
} |
|||
} finally { |
|||
edgeSynchronizationManager.getEdgeId().remove(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public DownlinkMsg convertEdgeEventToDownlink(EdgeEvent edgeEvent, EdgeVersion edgeVersion) { |
|||
CalculatedFieldId calculatedFieldId = new CalculatedFieldId(edgeEvent.getEntityId()); |
|||
switch (edgeEvent.getAction()) { |
|||
case ADDED, UPDATED -> { |
|||
CalculatedField calculatedField = edgeCtx.getCalculatedFieldService().findById(edgeEvent.getTenantId(), calculatedFieldId); |
|||
if (calculatedField != null) { |
|||
UpdateMsgType msgType = getUpdateMsgType(edgeEvent.getAction()); |
|||
CalculatedFieldUpdateMsg calculatedFieldUpdateMsg = EdgeMsgConstructorUtils.constructCalculatedFieldUpdatedMsg(msgType, calculatedField); |
|||
return DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addCalculatedFieldUpdateMsg(calculatedFieldUpdateMsg) |
|||
.build(); |
|||
} |
|||
} |
|||
case DELETED -> { |
|||
CalculatedFieldUpdateMsg calculatedFieldUpdateMsg = EdgeMsgConstructorUtils.constructCalculatedFieldDeleteMsg(calculatedFieldId); |
|||
return DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addCalculatedFieldUpdateMsg(calculatedFieldUpdateMsg) |
|||
.build(); |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public EdgeEventType getEdgeEventType() { |
|||
return EdgeEventType.CALCULATED_FIELD; |
|||
} |
|||
|
|||
@Override |
|||
public ListenableFuture<Void> processEntityNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) { |
|||
EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType()); |
|||
EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction()); |
|||
EntityId entityId = EntityIdFactory.getByEdgeEventTypeAndUuid(type, new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB())); |
|||
EdgeId originatorEdgeId = safeGetEdgeId(edgeNotificationMsg.getOriginatorEdgeIdMSB(), edgeNotificationMsg.getOriginatorEdgeIdLSB()); |
|||
|
|||
switch (actionType) { |
|||
case UPDATED: |
|||
case ADDED: |
|||
EntityId calculatedFieldOwnerId = JacksonUtil.fromString(edgeNotificationMsg.getBody(), EntityId.class); |
|||
if (calculatedFieldOwnerId != null && |
|||
(EntityType.DEVICE.equals(calculatedFieldOwnerId.getEntityType()) || EntityType.ASSET.equals(calculatedFieldOwnerId.getEntityType()))) { |
|||
JsonNode body = JacksonUtil.toJsonNode(edgeNotificationMsg.getBody()); |
|||
EdgeId edgeId = safeGetEdgeId(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()); |
|||
|
|||
return edgeId != null ? |
|||
saveEdgeEvent(tenantId, edgeId, type, actionType, entityId, body) : |
|||
processNotificationToRelatedEdges(tenantId, calculatedFieldOwnerId, entityId, type, actionType, originatorEdgeId); |
|||
} else { |
|||
return processActionForAllEdges(tenantId, type, actionType, entityId, null, originatorEdgeId); |
|||
} |
|||
default: |
|||
return super.processEntityNotification(tenantId, edgeNotificationMsg); |
|||
} |
|||
} |
|||
|
|||
private void processCalculatedField(TenantId tenantId, CalculatedFieldId calculatedFieldId, CalculatedFieldUpdateMsg calculatedFieldUpdateMsg, Edge edge) { |
|||
Pair<Boolean, Boolean> resultPair = super.saveOrUpdateCalculatedField(tenantId, calculatedFieldId, calculatedFieldUpdateMsg); |
|||
Boolean wasCreated = resultPair.getFirst(); |
|||
if (wasCreated) { |
|||
pushCalculatedFieldCreatedEventToRuleEngine(tenantId, edge, calculatedFieldId); |
|||
} |
|||
Boolean nameWasUpdated = resultPair.getSecond(); |
|||
if (nameWasUpdated) { |
|||
saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.CALCULATED_FIELD, EdgeEventActionType.UPDATED, calculatedFieldId, null); |
|||
} |
|||
} |
|||
|
|||
private void pushCalculatedFieldCreatedEventToRuleEngine(TenantId tenantId, Edge edge, CalculatedFieldId calculatedFieldId) { |
|||
try { |
|||
CalculatedField calculatedField = edgeCtx.getCalculatedFieldService().findById(tenantId, calculatedFieldId); |
|||
String calculatedFieldAsString = JacksonUtil.toString(calculatedField); |
|||
TbMsgMetaData msgMetaData = getEdgeActionTbMsgMetaData(edge, edge.getCustomerId()); |
|||
pushEntityEventToRuleEngine(tenantId, calculatedFieldId, edge.getCustomerId(), TbMsgType.ENTITY_CREATED, calculatedFieldAsString, msgMetaData); |
|||
} catch (Exception e) { |
|||
log.warn("[{}][{}] Failed to push calculatedField action to rule engine: {}", tenantId, calculatedFieldId, TbMsgType.ENTITY_CREATED.name(), e); |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.processor.cf; |
|||
|
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.gen.edge.v1.CalculatedFieldUpdateMsg; |
|||
import org.thingsboard.server.service.edge.rpc.processor.EdgeProcessor; |
|||
|
|||
public interface CalculatedFieldProcessor extends EdgeProcessor { |
|||
|
|||
ListenableFuture<Void> processCalculatedFieldMsgFromEdge(TenantId tenantId, Edge edge, CalculatedFieldUpdateMsg calculatedFieldUpdateMsg); |
|||
|
|||
} |
|||
@ -0,0 +1,267 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.edge; |
|||
|
|||
import com.datastax.oss.driver.api.core.uuid.Uuids; |
|||
import com.google.protobuf.AbstractMessage; |
|||
import com.google.protobuf.InvalidProtocolBufferException; |
|||
import org.junit.Assert; |
|||
import org.junit.Test; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.cf.CalculatedField; |
|||
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|||
import org.thingsboard.server.common.data.cf.configuration.Argument; |
|||
import org.thingsboard.server.common.data.cf.configuration.ArgumentType; |
|||
import org.thingsboard.server.common.data.cf.configuration.Output; |
|||
import org.thingsboard.server.common.data.cf.configuration.OutputType; |
|||
import org.thingsboard.server.common.data.cf.configuration.ReferencedEntityKey; |
|||
import org.thingsboard.server.common.data.cf.configuration.SimpleCalculatedFieldConfiguration; |
|||
import org.thingsboard.server.common.data.debug.DebugSettings; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.gen.edge.v1.CalculatedFieldRequestMsg; |
|||
import org.thingsboard.server.gen.edge.v1.CalculatedFieldUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
import org.thingsboard.server.gen.edge.v1.UplinkMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UplinkResponseMsg; |
|||
|
|||
import java.util.Map; |
|||
import java.util.Optional; |
|||
import java.util.UUID; |
|||
|
|||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|||
|
|||
@DaoSqlTest |
|||
public class CalculatedFieldEdgeTest extends AbstractEdgeTest { |
|||
private static final String DEFAULT_CF_NAME = "Edge Test CalculatedField"; |
|||
private static final String UPDATED_CF_NAME = "Updated Edge Test CalculatedField"; |
|||
|
|||
@Test |
|||
public void testCalculatedField_create_update_delete() throws Exception { |
|||
Device savedDevice = saveDeviceOnCloudAndVerifyDeliveryToEdge(); |
|||
|
|||
// create calculatedField
|
|||
SimpleCalculatedFieldConfiguration config = new SimpleCalculatedFieldConfiguration(); |
|||
CalculatedField calculatedField = createSimpleCalculatedField(savedDevice.getId(), config); |
|||
|
|||
edgeImitator.expectMessageAmount(1); |
|||
CalculatedField savedCalculatedField = doPost("/api/calculatedField", calculatedField, CalculatedField.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
AbstractMessage latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof CalculatedFieldUpdateMsg); |
|||
CalculatedFieldUpdateMsg calculatedFieldUpdateMsg = (CalculatedFieldUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, calculatedFieldUpdateMsg.getMsgType()); |
|||
Assert.assertEquals(savedCalculatedField.getUuidId().getMostSignificantBits(), calculatedFieldUpdateMsg.getIdMSB()); |
|||
Assert.assertEquals(savedCalculatedField.getUuidId().getLeastSignificantBits(), calculatedFieldUpdateMsg.getIdLSB()); |
|||
CalculatedField calculatedFieldFromMsg = JacksonUtil.fromString(calculatedFieldUpdateMsg.getEntity(), CalculatedField.class, true); |
|||
Assert.assertNotNull(calculatedFieldFromMsg); |
|||
|
|||
Assert.assertEquals(DEFAULT_CF_NAME, calculatedFieldFromMsg.getName()); |
|||
Assert.assertEquals(savedDevice.getId(), calculatedFieldFromMsg.getEntityId()); |
|||
Assert.assertEquals(config, calculatedFieldFromMsg.getConfiguration()); |
|||
|
|||
edgeImitator.expectMessageAmount(1); |
|||
savedCalculatedField.setName(UPDATED_CF_NAME); |
|||
savedCalculatedField = doPost("/api/calculatedField", savedCalculatedField, CalculatedField.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof CalculatedFieldUpdateMsg); |
|||
calculatedFieldUpdateMsg = (CalculatedFieldUpdateMsg) latestMessage; |
|||
calculatedFieldFromMsg = JacksonUtil.fromString(calculatedFieldUpdateMsg.getEntity(), CalculatedField.class, true); |
|||
Assert.assertNotNull(calculatedFieldFromMsg); |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, calculatedFieldUpdateMsg.getMsgType()); |
|||
Assert.assertEquals(UPDATED_CF_NAME, calculatedFieldFromMsg.getName()); |
|||
|
|||
// delete calculatedField
|
|||
edgeImitator.expectMessageAmount(1); |
|||
doDelete("/api/calculatedField/" + savedCalculatedField.getUuidId()) |
|||
.andExpect(status().isOk()); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof CalculatedFieldUpdateMsg); |
|||
calculatedFieldUpdateMsg = (CalculatedFieldUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, calculatedFieldUpdateMsg.getMsgType()); |
|||
Assert.assertEquals(savedCalculatedField.getUuidId().getMostSignificantBits(), calculatedFieldUpdateMsg.getIdMSB()); |
|||
Assert.assertEquals(savedCalculatedField.getUuidId().getLeastSignificantBits(), calculatedFieldUpdateMsg.getIdLSB()); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendCalculatedFieldToCloud() throws Exception { |
|||
Device savedDevice = saveDeviceOnCloudAndVerifyDeliveryToEdge(); |
|||
|
|||
// create calculatedField
|
|||
SimpleCalculatedFieldConfiguration config = new SimpleCalculatedFieldConfiguration(); |
|||
CalculatedField calculatedField = createSimpleCalculatedField(savedDevice.getId(), config); |
|||
UUID uuid = Uuids.timeBased(); |
|||
UplinkMsg uplinkMsg = getUplinkMsg(uuid, calculatedField, UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE); |
|||
|
|||
checkCalculatedFieldOnCloud(uplinkMsg, uuid, calculatedField.getName()); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendCalculatedFieldRequestToCloud() throws Exception { |
|||
Device savedDevice = saveDeviceOnCloudAndVerifyDeliveryToEdge(); |
|||
|
|||
// create calculatedField
|
|||
SimpleCalculatedFieldConfiguration config = new SimpleCalculatedFieldConfiguration(); |
|||
CalculatedField calculatedField = createSimpleCalculatedField(savedDevice.getId(), config); |
|||
|
|||
edgeImitator.expectMessageAmount(1); |
|||
CalculatedField savedCalculatedField = doPost("/api/calculatedField", calculatedField, CalculatedField.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder(); |
|||
CalculatedFieldRequestMsg.Builder calculatedFieldRequestMsgBuilder = CalculatedFieldRequestMsg.newBuilder(); |
|||
calculatedFieldRequestMsgBuilder.setEntityIdMSB(savedDevice.getId().getId().getMostSignificantBits()); |
|||
calculatedFieldRequestMsgBuilder.setEntityIdLSB(savedDevice.getId().getId().getLeastSignificantBits()); |
|||
calculatedFieldRequestMsgBuilder.setEntityType(savedDevice.getId().getEntityType().name()); |
|||
testAutoGeneratedCodeByProtobuf(calculatedFieldRequestMsgBuilder); |
|||
|
|||
uplinkMsgBuilder.addCalculatedFieldRequestMsg(calculatedFieldRequestMsgBuilder.build()); |
|||
testAutoGeneratedCodeByProtobuf(uplinkMsgBuilder); |
|||
|
|||
edgeImitator.expectResponsesAmount(1); |
|||
edgeImitator.expectMessageAmount(1); |
|||
edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build()); |
|||
Assert.assertTrue(edgeImitator.waitForResponses()); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
AbstractMessage latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof CalculatedFieldUpdateMsg); |
|||
CalculatedFieldUpdateMsg calculatedFieldUpdateMsg = (CalculatedFieldUpdateMsg) latestMessage; |
|||
CalculatedField calculatedFieldFromEdge = JacksonUtil.fromString(calculatedFieldUpdateMsg.getEntity(), CalculatedField.class, true); |
|||
Assert.assertNotNull(calculatedFieldFromEdge); |
|||
Assert.assertEquals(savedCalculatedField, calculatedFieldFromEdge); |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, calculatedFieldUpdateMsg.getMsgType()); |
|||
} |
|||
|
|||
@Test |
|||
public void testUpdateCalculatedFieldNameOnCloud() throws Exception { |
|||
Device savedDevice = saveDeviceOnCloudAndVerifyDeliveryToEdge(); |
|||
|
|||
// create calculatedField
|
|||
SimpleCalculatedFieldConfiguration config = new SimpleCalculatedFieldConfiguration(); |
|||
CalculatedField calculatedField = createSimpleCalculatedField(savedDevice.getId(), config); |
|||
UUID uuid = Uuids.timeBased(); |
|||
UplinkMsg uplinkMsg = getUplinkMsg(uuid, calculatedField, UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE); |
|||
|
|||
checkCalculatedFieldOnCloud(uplinkMsg, uuid, calculatedField.getName()); |
|||
|
|||
calculatedField.setName(UPDATED_CF_NAME); |
|||
UplinkMsg updatedUplinkMsg = getUplinkMsg(uuid, calculatedField, UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE); |
|||
|
|||
checkCalculatedFieldOnCloud(updatedUplinkMsg, uuid, calculatedField.getName()); |
|||
} |
|||
|
|||
@Test |
|||
public void testCalculatedFieldToCloudWithNameThatAlreadyExistsOnCloud() throws Exception { |
|||
Device savedDevice = saveDeviceOnCloudAndVerifyDeliveryToEdge(); |
|||
|
|||
// create calculatedField
|
|||
SimpleCalculatedFieldConfiguration config = new SimpleCalculatedFieldConfiguration(); |
|||
CalculatedField calculatedField = createSimpleCalculatedField(savedDevice.getId(), config); |
|||
|
|||
edgeImitator.expectMessageAmount(1); |
|||
CalculatedField savedCalculatedField = doPost("/api/calculatedField", calculatedField, CalculatedField.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
UUID uuid = Uuids.timeBased(); |
|||
|
|||
UplinkMsg uplinkMsg = getUplinkMsg(uuid, calculatedField, UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE); |
|||
|
|||
edgeImitator.expectResponsesAmount(1); |
|||
edgeImitator.expectMessageAmount(1); |
|||
|
|||
edgeImitator.sendUplinkMsg(uplinkMsg); |
|||
|
|||
Assert.assertTrue(edgeImitator.waitForResponses()); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
Optional<CalculatedFieldUpdateMsg> calculatedFieldUpdateMsgOpt = edgeImitator.findMessageByType(CalculatedFieldUpdateMsg.class); |
|||
Assert.assertTrue(calculatedFieldUpdateMsgOpt.isPresent()); |
|||
CalculatedFieldUpdateMsg latestCalculatedFieldUpdateMsg = calculatedFieldUpdateMsgOpt.get(); |
|||
CalculatedField calculatedFieldFromMsg = JacksonUtil.fromString(latestCalculatedFieldUpdateMsg.getEntity(), CalculatedField.class, true); |
|||
Assert.assertNotNull(calculatedFieldFromMsg); |
|||
Assert.assertNotEquals(DEFAULT_CF_NAME, calculatedFieldFromMsg.getName()); |
|||
|
|||
Assert.assertNotEquals(savedCalculatedField.getUuidId(), uuid); |
|||
|
|||
CalculatedField calculatedFieldFromCloud = doGet("/api/calculatedField/" + uuid, CalculatedField.class); |
|||
Assert.assertNotNull(calculatedFieldFromCloud); |
|||
Assert.assertNotEquals(DEFAULT_CF_NAME, calculatedFieldFromCloud.getName()); |
|||
} |
|||
|
|||
private CalculatedField createSimpleCalculatedField(EntityId entityId, SimpleCalculatedFieldConfiguration config) { |
|||
CalculatedField calculatedField = new CalculatedField(); |
|||
calculatedField.setEntityId(entityId); |
|||
calculatedField.setTenantId(tenantId); |
|||
calculatedField.setType(CalculatedFieldType.SIMPLE); |
|||
calculatedField.setName(DEFAULT_CF_NAME); |
|||
calculatedField.setDebugSettings(DebugSettings.all()); |
|||
|
|||
Argument argument = new Argument(); |
|||
ReferencedEntityKey refEntityKey = new ReferencedEntityKey("temperature", ArgumentType.TS_LATEST, null); |
|||
argument.setRefEntityKey(refEntityKey); |
|||
argument.setDefaultValue("12"); // not used because real telemetry value in db is present
|
|||
config.setArguments(Map.of("T", argument)); |
|||
|
|||
config.setExpression("(T * 9/5) + 32"); |
|||
|
|||
Output output = new Output(); |
|||
output.setName("fahrenheitTemp"); |
|||
output.setType(OutputType.TIME_SERIES); |
|||
output.setDecimalsByDefault(2); |
|||
config.setOutput(output); |
|||
|
|||
calculatedField.setConfiguration(config); |
|||
|
|||
return calculatedField; |
|||
} |
|||
|
|||
private UplinkMsg getUplinkMsg(UUID uuid, CalculatedField calculatedField, UpdateMsgType updateMsgType) throws InvalidProtocolBufferException { |
|||
UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder(); |
|||
CalculatedFieldUpdateMsg.Builder calculatedFieldUpdateMsgBuilder = CalculatedFieldUpdateMsg.newBuilder(); |
|||
calculatedFieldUpdateMsgBuilder.setIdMSB(uuid.getMostSignificantBits()); |
|||
calculatedFieldUpdateMsgBuilder.setIdLSB(uuid.getLeastSignificantBits()); |
|||
calculatedFieldUpdateMsgBuilder.setEntity(JacksonUtil.toString(calculatedField)); |
|||
calculatedFieldUpdateMsgBuilder.setMsgType(updateMsgType); |
|||
testAutoGeneratedCodeByProtobuf(calculatedFieldUpdateMsgBuilder); |
|||
uplinkMsgBuilder.addCalculatedFieldUpdateMsg(calculatedFieldUpdateMsgBuilder.build()); |
|||
|
|||
testAutoGeneratedCodeByProtobuf(uplinkMsgBuilder); |
|||
|
|||
return uplinkMsgBuilder.build(); |
|||
} |
|||
|
|||
private void checkCalculatedFieldOnCloud(UplinkMsg uplinkMsg, UUID uuid, String resourceTitle) throws Exception { |
|||
edgeImitator.expectResponsesAmount(1); |
|||
edgeImitator.sendUplinkMsg(uplinkMsg); |
|||
|
|||
Assert.assertTrue(edgeImitator.waitForResponses()); |
|||
|
|||
UplinkResponseMsg latestResponseMsg = edgeImitator.getLatestResponseMsg(); |
|||
Assert.assertTrue(latestResponseMsg.getSuccess()); |
|||
|
|||
CalculatedField calculatedField = doGet("/api/calculatedField/" + uuid, CalculatedField.class); |
|||
Assert.assertNotNull(calculatedField); |
|||
Assert.assertEquals(resourceTitle, calculatedField.getName()); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue