172 changed files with 2574 additions and 1595 deletions
@ -1,143 +0,0 @@ |
|||||
/** |
|
||||
* 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.controller; |
|
||||
|
|
||||
import io.swagger.v3.oas.annotations.Parameter; |
|
||||
import io.swagger.v3.oas.annotations.media.Schema; |
|
||||
import jakarta.validation.Valid; |
|
||||
import org.springframework.security.access.prepost.PreAuthorize; |
|
||||
import org.springframework.validation.annotation.Validated; |
|
||||
import org.springframework.web.bind.annotation.DeleteMapping; |
|
||||
import org.springframework.web.bind.annotation.GetMapping; |
|
||||
import org.springframework.web.bind.annotation.PathVariable; |
|
||||
import org.springframework.web.bind.annotation.PostMapping; |
|
||||
import org.springframework.web.bind.annotation.RequestBody; |
|
||||
import org.springframework.web.bind.annotation.RequestMapping; |
|
||||
import org.springframework.web.bind.annotation.RequestParam; |
|
||||
import org.springframework.web.bind.annotation.RestController; |
|
||||
import org.thingsboard.server.common.data.ai.AiModelSettings; |
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|
||||
import org.thingsboard.server.common.data.id.AiModelSettingsId; |
|
||||
import org.thingsboard.server.common.data.page.PageData; |
|
||||
import org.thingsboard.server.config.annotations.ApiOperation; |
|
||||
import org.thingsboard.server.service.security.permission.Operation; |
|
||||
import org.thingsboard.server.service.security.permission.Resource; |
|
||||
|
|
||||
import java.util.Optional; |
|
||||
import java.util.UUID; |
|
||||
|
|
||||
import static org.thingsboard.server.controller.ControllerConstants.AI_MODEL_SETTINGS_TEXT_SEARCH_DESCRIPTION; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.PAGE_DATA_PARAMETERS; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_DESCRIPTION; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.SORT_PROPERTY_DESCRIPTION; |
|
||||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHORITY_PARAGRAPH; |
|
||||
|
|
||||
@Validated |
|
||||
@RestController |
|
||||
@RequestMapping("/api/ai/model/settings") |
|
||||
class AiModelSettingsController extends BaseController { |
|
||||
|
|
||||
@ApiOperation( |
|
||||
value = "Create or update AI model settings (saveAiModelSettings)", |
|
||||
notes = "Creates or updates an AI model settings record.\n\n" + |
|
||||
"• **Create:** Omit the `id` to create a new record. The platform assigns a UUID to the new settings and returns it in the `id` field of the response.\n\n" + |
|
||||
"• **Update:** Include an existing `id` to modify that record. If no matching record exists, the API responds with **404 Not Found**.\n\n" + |
|
||||
"Tenant ID for the AI model settings will be taken from the authenticated user making the request, regardless of any value provided in the request body." + |
|
||||
TENANT_AUTHORITY_PARAGRAPH |
|
||||
) |
|
||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|
||||
@PostMapping |
|
||||
public AiModelSettings saveAiModelSettings(@RequestBody @Valid AiModelSettings settings) throws ThingsboardException { |
|
||||
var user = getCurrentUser(); |
|
||||
settings.setTenantId(user.getTenantId()); |
|
||||
checkEntity(settings.getId(), settings, Resource.AI_MODEL_SETTINGS); |
|
||||
return tbAiModelSettingsService.save(settings, user); |
|
||||
} |
|
||||
|
|
||||
@ApiOperation( |
|
||||
value = "Get AI model settings by ID (getAiModelSettingsById)", |
|
||||
notes = "Fetches an AI model settings record by its `id`." + |
|
||||
TENANT_AUTHORITY_PARAGRAPH |
|
||||
) |
|
||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|
||||
@GetMapping("/{settingsUuid}") |
|
||||
public AiModelSettings getAiModelSettingsById( |
|
||||
@Parameter( |
|
||||
description = "ID of the AI model settings record", |
|
||||
required = true, |
|
||||
example = "de7900d4-30e2-11f0-9cd2-0242ac120002" |
|
||||
) |
|
||||
@PathVariable UUID settingsUuid |
|
||||
) throws ThingsboardException { |
|
||||
return checkAiModelSettingsId(new AiModelSettingsId(settingsUuid), Operation.READ); |
|
||||
} |
|
||||
|
|
||||
@ApiOperation( |
|
||||
value = "Get AI model settings (getAiModelSettings)", |
|
||||
notes = "Returns a page of AI model settings. " + |
|
||||
PAGE_DATA_PARAMETERS + TENANT_AUTHORITY_PARAGRAPH |
|
||||
) |
|
||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|
||||
@GetMapping |
|
||||
public PageData<AiModelSettings> getAiModelSettings( |
|
||||
@Parameter(description = PAGE_SIZE_DESCRIPTION, required = true) |
|
||||
@RequestParam int pageSize, |
|
||||
@Parameter(description = PAGE_NUMBER_DESCRIPTION, required = true) |
|
||||
@RequestParam int page, |
|
||||
@Parameter(description = AI_MODEL_SETTINGS_TEXT_SEARCH_DESCRIPTION) |
|
||||
@RequestParam(required = false) String textSearch, |
|
||||
@Parameter(description = SORT_PROPERTY_DESCRIPTION, schema = @Schema(allowableValues = {"createdTime", "name", "provider", "modelId"})) |
|
||||
@RequestParam(required = false) String sortProperty, |
|
||||
@Parameter(description = SORT_ORDER_DESCRIPTION, schema = @Schema(allowableValues = {"ASC", "DESC"})) |
|
||||
@RequestParam(required = false) String sortOrder |
|
||||
) throws ThingsboardException { |
|
||||
var user = getCurrentUser(); |
|
||||
accessControlService.checkPermission(user, Resource.AI_MODEL_SETTINGS, Operation.READ); |
|
||||
var pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); |
|
||||
return aiModelSettingsService.findAiModelSettingsByTenantId(user.getTenantId(), pageLink); |
|
||||
} |
|
||||
|
|
||||
@ApiOperation( |
|
||||
value = "Delete AI model settings by ID (deleteAiModelSettingsById)", |
|
||||
notes = "Deletes the AI model settings record by its `id`. " + |
|
||||
"If a record with the specified `id` exists, the record is deleted and the endpoint returns `true`. " + |
|
||||
"If no such record exists, the endpoint returns `false`." + |
|
||||
TENANT_AUTHORITY_PARAGRAPH |
|
||||
) |
|
||||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|
||||
@DeleteMapping("/{settingsUuid}") |
|
||||
public boolean deleteAiModelSettingsById( |
|
||||
@Parameter( |
|
||||
description = "ID of the AI model settings record", |
|
||||
required = true, |
|
||||
example = "de7900d4-30e2-11f0-9cd2-0242ac120002" |
|
||||
) |
|
||||
@PathVariable UUID settingsUuid |
|
||||
) throws ThingsboardException { |
|
||||
var user = getCurrentUser(); |
|
||||
var settingsId = new AiModelSettingsId(settingsUuid); |
|
||||
accessControlService.checkPermission(user, Resource.AI_MODEL_SETTINGS, Operation.DELETE); |
|
||||
Optional<AiModelSettings> toDelete = aiModelSettingsService.findAiModelSettingsByTenantIdAndId(user.getTenantId(), settingsId); |
|
||||
if (toDelete.isEmpty()) { |
|
||||
return false; |
|
||||
} |
|
||||
accessControlService.checkPermission(user, Resource.AI_MODEL_SETTINGS, Operation.DELETE, settingsId, toDelete.get()); |
|
||||
return tbAiModelSettingsService.delete(toDelete.get(), user); |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -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,49 @@ |
|||||
|
/** |
||||
|
* 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.sync.ie.exporting.impl; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.common.data.EntityType; |
||||
|
import org.thingsboard.server.common.data.OtaPackage; |
||||
|
import org.thingsboard.server.common.data.id.OtaPackageId; |
||||
|
import org.thingsboard.server.common.data.sync.ie.OtaPackageExportData; |
||||
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
||||
|
import org.thingsboard.server.service.sync.vc.data.EntitiesExportCtx; |
||||
|
|
||||
|
import java.util.Set; |
||||
|
|
||||
|
@Service |
||||
|
@TbCoreComponent |
||||
|
@RequiredArgsConstructor |
||||
|
public class OtaPackageExportService extends BaseEntityExportService<OtaPackageId, OtaPackage, OtaPackageExportData> { |
||||
|
|
||||
|
@Override |
||||
|
protected void setRelatedEntities(EntitiesExportCtx<?> ctx, OtaPackage otaPackage, OtaPackageExportData exportData) { |
||||
|
otaPackage.setDeviceProfileId(getExternalIdOrElseInternal(ctx, otaPackage.getDeviceProfileId())); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected OtaPackageExportData newExportData() { |
||||
|
return new OtaPackageExportData(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Set<EntityType> getSupportedEntityTypes() { |
||||
|
return Set.of(EntityType.OTA_PACKAGE); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,76 @@ |
|||||
|
/** |
||||
|
* 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.sync.ie.importing.impl; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.common.data.EntityType; |
||||
|
import org.thingsboard.server.common.data.OtaPackage; |
||||
|
import org.thingsboard.server.common.data.OtaPackageInfo; |
||||
|
import org.thingsboard.server.common.data.id.OtaPackageId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.sync.ie.OtaPackageExportData; |
||||
|
import org.thingsboard.server.dao.ota.OtaPackageService; |
||||
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
||||
|
import org.thingsboard.server.service.sync.vc.data.EntitiesImportCtx; |
||||
|
|
||||
|
@Service |
||||
|
@TbCoreComponent |
||||
|
@RequiredArgsConstructor |
||||
|
public class OtaPackageImportService extends BaseEntityImportService<OtaPackageId, OtaPackage, OtaPackageExportData> { |
||||
|
|
||||
|
private final OtaPackageService otaPackageService; |
||||
|
|
||||
|
@Override |
||||
|
protected void setOwner(TenantId tenantId, OtaPackage otaPackage, IdProvider idProvider) { |
||||
|
otaPackage.setTenantId(tenantId); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected OtaPackage prepare(EntitiesImportCtx ctx, OtaPackage otaPackage, OtaPackage oldOtaPackage, OtaPackageExportData exportData, IdProvider idProvider) { |
||||
|
otaPackage.setDeviceProfileId(idProvider.getInternalId(otaPackage.getDeviceProfileId())); |
||||
|
return otaPackage; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected OtaPackage findExistingEntity(EntitiesImportCtx ctx, OtaPackage otaPackage, IdProvider idProvider) { |
||||
|
OtaPackage existingOtaPackage = super.findExistingEntity(ctx, otaPackage, idProvider); |
||||
|
if (existingOtaPackage == null && ctx.isFindExistingByName()) { |
||||
|
existingOtaPackage = otaPackageService.findOtaPackageByTenantIdAndTitleAndVersion(ctx.getTenantId(), otaPackage.getTitle(), otaPackage.getVersion()); |
||||
|
} |
||||
|
return existingOtaPackage; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected OtaPackage deepCopy(OtaPackage otaPackage) { |
||||
|
return new OtaPackage(otaPackage); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected OtaPackage saveOrUpdate(EntitiesImportCtx ctx, OtaPackage otaPackage, OtaPackageExportData exportData, IdProvider idProvider, CompareResult compareResult) { |
||||
|
if (otaPackage.hasUrl()) { |
||||
|
OtaPackageInfo info = new OtaPackageInfo(otaPackage); |
||||
|
return new OtaPackage(otaPackageService.saveOtaPackageInfo(info, info.hasUrl())); |
||||
|
} |
||||
|
return otaPackageService.saveOtaPackage(otaPackage); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public EntityType getEntityType() { |
||||
|
return EntityType.OTA_PACKAGE; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -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()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -1,84 +0,0 @@ |
|||||
/** |
|
||||
* 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.common.data.ai.model; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes; |
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.AmazonBedrockChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.AnthropicChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.AzureOpenAiChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.GitHubModelsChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.GoogleAiGeminiChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.GoogleVertexAiGeminiChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.MistralAiChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.chat.OpenAiChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.provider.AiProvider; |
|
||||
import org.thingsboard.server.common.data.ai.provider.AiProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.AmazonBedrockProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.AnthropicProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.AzureOpenAiProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.GitHubModelsProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.GoogleAiGeminiProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.GoogleVertexAiGeminiProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.MistralAiProviderConfig; |
|
||||
import org.thingsboard.server.common.data.ai.provider.OpenAiProviderConfig; |
|
||||
|
|
||||
@JsonTypeInfo( |
|
||||
use = JsonTypeInfo.Id.NAME, |
|
||||
include = JsonTypeInfo.As.PROPERTY, |
|
||||
property = "provider", |
|
||||
visible = true |
|
||||
) |
|
||||
@JsonSubTypes({ |
|
||||
@JsonSubTypes.Type(value = OpenAiChatModel.class, name = "OPENAI"), |
|
||||
@JsonSubTypes.Type(value = AzureOpenAiChatModel.class, name = "AZURE_OPENAI"), |
|
||||
@JsonSubTypes.Type(value = GoogleAiGeminiChatModel.class, name = "GOOGLE_AI_GEMINI"), |
|
||||
@JsonSubTypes.Type(value = GoogleVertexAiGeminiChatModel.class, name = "GOOGLE_VERTEX_AI_GEMINI"), |
|
||||
@JsonSubTypes.Type(value = MistralAiChatModel.class, name = "MISTRAL_AI"), |
|
||||
@JsonSubTypes.Type(value = AnthropicChatModel.class, name = "ANTHROPIC"), |
|
||||
@JsonSubTypes.Type(value = AmazonBedrockChatModel.class, name = "AMAZON_BEDROCK"), |
|
||||
@JsonSubTypes.Type(value = GitHubModelsChatModel.class, name = "GITHUB_MODELS") |
|
||||
}) |
|
||||
public interface AiModel<C extends AiModelConfig> { |
|
||||
|
|
||||
AiProvider provider(); |
|
||||
|
|
||||
@JsonTypeInfo( |
|
||||
use = JsonTypeInfo.Id.NAME, |
|
||||
include = JsonTypeInfo.As.EXTERNAL_PROPERTY, |
|
||||
property = "provider" |
|
||||
) |
|
||||
@JsonSubTypes({ |
|
||||
@JsonSubTypes.Type(value = OpenAiProviderConfig.class, name = "OPENAI"), |
|
||||
@JsonSubTypes.Type(value = AzureOpenAiProviderConfig.class, name = "AZURE_OPENAI"), |
|
||||
@JsonSubTypes.Type(value = GoogleAiGeminiProviderConfig.class, name = "GOOGLE_AI_GEMINI"), |
|
||||
@JsonSubTypes.Type(value = GoogleVertexAiGeminiProviderConfig.class, name = "GOOGLE_VERTEX_AI_GEMINI"), |
|
||||
@JsonSubTypes.Type(value = MistralAiProviderConfig.class, name = "MISTRAL_AI"), |
|
||||
@JsonSubTypes.Type(value = AnthropicProviderConfig.class, name = "ANTHROPIC"), |
|
||||
@JsonSubTypes.Type(value = AmazonBedrockProviderConfig.class, name = "AMAZON_BEDROCK"), |
|
||||
@JsonSubTypes.Type(value = GitHubModelsProviderConfig.class, name = "GITHUB_MODELS") |
|
||||
}) |
|
||||
AiProviderConfig providerConfig(); |
|
||||
|
|
||||
@JsonProperty("modelType") |
|
||||
AiModelType modelType(); |
|
||||
|
|
||||
C modelConfig(); |
|
||||
|
|
||||
AiModel<C> withModelConfig(C config); |
|
||||
|
|
||||
} |
|
||||
@ -1,41 +0,0 @@ |
|||||
/** |
|
||||
* 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.common.data.ai.model.chat; |
|
||||
|
|
||||
import dev.langchain4j.model.chat.ChatModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.AiModel; |
|
||||
import org.thingsboard.server.common.data.ai.model.AiModelType; |
|
||||
|
|
||||
public sealed interface AiChatModel<C extends AiChatModelConfig<C>> extends AiModel<C> |
|
||||
permits |
|
||||
OpenAiChatModel, AzureOpenAiChatModel, GoogleAiGeminiChatModel, |
|
||||
GoogleVertexAiGeminiChatModel, MistralAiChatModel, AnthropicChatModel, |
|
||||
AmazonBedrockChatModel, GitHubModelsChatModel { |
|
||||
|
|
||||
ChatModel configure(Langchain4jChatModelConfigurer configurer); |
|
||||
|
|
||||
@Override |
|
||||
default AiModelType modelType() { |
|
||||
return AiModelType.CHAT; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
C modelConfig(); |
|
||||
|
|
||||
@Override |
|
||||
AiChatModel<C> withModelConfig(C config); |
|
||||
|
|
||||
} |
|
||||
@ -0,0 +1,41 @@ |
|||||
|
/** |
||||
|
* 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.common.data.sync.ie; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import org.thingsboard.server.common.data.OtaPackage; |
||||
|
|
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
public class OtaPackageExportData extends EntityExportData<OtaPackage> { |
||||
|
|
||||
|
/* |
||||
|
* OtaPackage is not a versioned entity; its 'version' field is part of the domain model (not used for optimistic locking) |
||||
|
* We override both methods to ensure 'version' is not ignored during (de)serialization. |
||||
|
*/ |
||||
|
@JsonIgnoreProperties(value = {"tenantId", "createdTime"}, ignoreUnknown = true) |
||||
|
@Override |
||||
|
public OtaPackage getEntity() { |
||||
|
return super.getEntity(); |
||||
|
} |
||||
|
|
||||
|
@JsonIgnoreProperties(value = {"tenantId", "createdTime"}, ignoreUnknown = true) |
||||
|
@Override |
||||
|
public void setEntity(OtaPackage entity) { |
||||
|
super.setEntity(entity); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue