13 changed files with 342 additions and 4 deletions
@ -0,0 +1,168 @@ |
|||||
|
/** |
||||
|
* 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.ai; |
||||
|
|
||||
|
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.ai.AiModel; |
||||
|
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.AiModelId; |
||||
|
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.AiModelUpdateMsg; |
||||
|
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.queue.util.TbCoreComponent; |
||||
|
import org.thingsboard.server.service.edge.EdgeMsgConstructorUtils; |
||||
|
|
||||
|
import java.util.Optional; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Component |
||||
|
@TbCoreComponent |
||||
|
public class AiModelEdgeProcessor extends BaseAiModelProcessor implements AiModelProcessor { |
||||
|
|
||||
|
@Override |
||||
|
public ListenableFuture<Void> processAiModelMsgFromEdge(TenantId tenantId, Edge edge, AiModelUpdateMsg aiModelUpdateMsg) { |
||||
|
AiModelId aiModelId = new AiModelId(new UUID(aiModelUpdateMsg.getIdMSB(), aiModelUpdateMsg.getIdLSB())); |
||||
|
try { |
||||
|
edgeSynchronizationManager.getEdgeId().set(edge.getId()); |
||||
|
|
||||
|
switch (aiModelUpdateMsg.getMsgType()) { |
||||
|
case ENTITY_CREATED_RPC_MESSAGE: |
||||
|
case ENTITY_UPDATED_RPC_MESSAGE: |
||||
|
processAiModel(tenantId, aiModelId, aiModelUpdateMsg, edge); |
||||
|
return Futures.immediateFuture(null); |
||||
|
case ENTITY_DELETED_RPC_MESSAGE: |
||||
|
Optional<AiModel> aiModel = edgeCtx.getAiModelService().findAiModelById(tenantId, aiModelId); |
||||
|
if (aiModel.isPresent()) { |
||||
|
edgeCtx.getAiModelService().deleteByTenantIdAndId(tenantId, aiModelId); |
||||
|
} |
||||
|
return Futures.immediateFuture(null); |
||||
|
case UNRECOGNIZED: |
||||
|
default: |
||||
|
return handleUnsupportedMsgType(aiModelUpdateMsg.getMsgType()); |
||||
|
} |
||||
|
} catch (DataValidationException e) { |
||||
|
if (e.getMessage().contains("limit reached")) { |
||||
|
log.warn("[{}] Number of allowed aiModel violated {}", tenantId, aiModelUpdateMsg, e); |
||||
|
return Futures.immediateFuture(null); |
||||
|
} else { |
||||
|
return Futures.immediateFailedFuture(e); |
||||
|
} |
||||
|
} finally { |
||||
|
edgeSynchronizationManager.getEdgeId().remove(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public DownlinkMsg convertEdgeEventToDownlink(EdgeEvent edgeEvent, EdgeVersion edgeVersion) { |
||||
|
AiModelId aiModelId = new AiModelId(edgeEvent.getEntityId()); |
||||
|
switch (edgeEvent.getAction()) { |
||||
|
case ADDED, UPDATED -> { |
||||
|
Optional<AiModel> aiModel = edgeCtx.getAiModelService().findAiModelById(edgeEvent.getTenantId(), aiModelId); |
||||
|
if (aiModel.isPresent()) { |
||||
|
UpdateMsgType msgType = getUpdateMsgType(edgeEvent.getAction()); |
||||
|
AiModelUpdateMsg aiModelUpdateMsg = EdgeMsgConstructorUtils.constructAiModelUpdatedMsg(msgType, aiModel.get()); |
||||
|
return DownlinkMsg.newBuilder() |
||||
|
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
||||
|
.addAiModelUpdateMsg(aiModelUpdateMsg) |
||||
|
.build(); |
||||
|
} |
||||
|
} |
||||
|
case DELETED -> { |
||||
|
AiModelUpdateMsg aiModelUpdateMsg = EdgeMsgConstructorUtils.constructAiModelDeleteMsg(aiModelId); |
||||
|
return DownlinkMsg.newBuilder() |
||||
|
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
||||
|
.addAiModelUpdateMsg(aiModelUpdateMsg) |
||||
|
.build(); |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public EdgeEventType getEdgeEventType() { |
||||
|
return EdgeEventType.AI_MODEL; |
||||
|
} |
||||
|
|
||||
|
// @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 processAiModel(TenantId tenantId, AiModelId aiModelId, AiModelUpdateMsg aiModelUpdateMsg, Edge edge) { |
||||
|
Pair<Boolean, Boolean> resultPair = super.saveOrUpdateAiModel(tenantId, aiModelId, aiModelUpdateMsg); |
||||
|
Boolean wasCreated = resultPair.getFirst(); |
||||
|
if (wasCreated) { |
||||
|
pushAiModelCreatedEventToRuleEngine(tenantId, edge, aiModelId); |
||||
|
} |
||||
|
Boolean nameWasUpdated = resultPair.getSecond(); |
||||
|
if (nameWasUpdated) { |
||||
|
saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.AI_MODEL, EdgeEventActionType.UPDATED, aiModelId, null); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void pushAiModelCreatedEventToRuleEngine(TenantId tenantId, Edge edge, AiModelId aiModelId) { |
||||
|
try { |
||||
|
Optional<AiModel> aiModel = edgeCtx.getAiModelService().findAiModelById(tenantId, aiModelId); |
||||
|
if (aiModel.isPresent()) { |
||||
|
String aiModelAsString = JacksonUtil.toString(aiModel.get()); |
||||
|
TbMsgMetaData msgMetaData = getEdgeActionTbMsgMetaData(edge, edge.getCustomerId()); |
||||
|
pushEntityEventToRuleEngine(tenantId, aiModelId, edge.getCustomerId(), TbMsgType.ENTITY_CREATED, aiModelAsString, msgMetaData); |
||||
|
} else { |
||||
|
log.warn("[{}][{}] Failed to find AiModel", tenantId, aiModelId); |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
log.warn("[{}][{}] Failed to push aiModel action to rule engine: {}", tenantId, aiModelId, 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.ai; |
||||
|
|
||||
|
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.AiModelUpdateMsg; |
||||
|
import org.thingsboard.server.service.edge.rpc.processor.EdgeProcessor; |
||||
|
|
||||
|
public interface AiModelProcessor extends EdgeProcessor { |
||||
|
|
||||
|
ListenableFuture<Void> processAiModelMsgFromEdge(TenantId tenantId, Edge edge, AiModelUpdateMsg aiModelUpdateMsg); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,81 @@ |
|||||
|
/** |
||||
|
* 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.ai; |
||||
|
|
||||
|
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.ai.AiModel; |
||||
|
import org.thingsboard.server.common.data.id.AiModelId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.dao.service.DataValidator; |
||||
|
import org.thingsboard.server.gen.edge.v1.AiModelUpdateMsg; |
||||
|
import org.thingsboard.server.service.edge.rpc.processor.BaseEdgeProcessor; |
||||
|
|
||||
|
import java.util.Optional; |
||||
|
|
||||
|
@Slf4j |
||||
|
public abstract class BaseAiModelProcessor extends BaseEdgeProcessor { |
||||
|
|
||||
|
@Autowired |
||||
|
private DataValidator<AiModel> aiModelValidator; |
||||
|
|
||||
|
protected Pair<Boolean, Boolean> saveOrUpdateAiModel(TenantId tenantId, AiModelId aiModelId, AiModelUpdateMsg aiModelUpdateMsg) { |
||||
|
boolean isCreated = false; |
||||
|
boolean isNameUpdated = false; |
||||
|
try { |
||||
|
AiModel aiModel = JacksonUtil.fromString(aiModelUpdateMsg.getEntity(), AiModel.class, true); |
||||
|
if (aiModel == null) { |
||||
|
throw new RuntimeException("[{" + tenantId + "}] aiModelUpdateMsg {" + aiModelUpdateMsg + " } cannot be converted to aiModel"); |
||||
|
} |
||||
|
|
||||
|
Optional<AiModel> aiModelById = edgeCtx.getAiModelService().findAiModelById(tenantId, aiModelId); |
||||
|
if (aiModelById.isEmpty()) { |
||||
|
aiModel.setCreatedTime(Uuids.unixTimestamp(aiModelId.getId())); |
||||
|
isCreated = true; |
||||
|
aiModel.setId(null); |
||||
|
} else { |
||||
|
aiModel.setId(aiModelId); |
||||
|
} |
||||
|
|
||||
|
String aiModelName = aiModel.getName(); |
||||
|
Optional<AiModel> aiModelByName = edgeCtx.getAiModelService().findAiModelByTenantIdAndName(aiModel.getTenantId(), aiModelName); |
||||
|
if (aiModelByName.isPresent() && !aiModelByName.get().getId().equals(aiModelId)) { |
||||
|
aiModelName = aiModelName + "_" + StringUtils.randomAlphabetic(15); |
||||
|
log.warn("[{}] aiModel with name {} already exists. Renaming aiModel name to {}", |
||||
|
tenantId, aiModel.getName(), aiModelByName.get().getName()); |
||||
|
isNameUpdated = true; |
||||
|
} |
||||
|
aiModel.setName(aiModelName); |
||||
|
|
||||
|
aiModelValidator.validate(aiModel, AiModel::getTenantId); |
||||
|
|
||||
|
if (isCreated) { |
||||
|
aiModel.setId(aiModelId); |
||||
|
} |
||||
|
|
||||
|
edgeCtx.getAiModelService().save(aiModel, false); |
||||
|
} catch (Exception e) { |
||||
|
log.error("[{}] Failed to process aiModel update msg [{}]", tenantId, aiModelUpdateMsg, e); |
||||
|
throw e; |
||||
|
} |
||||
|
return Pair.of(isCreated, isNameUpdated); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue