|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 73 KiB After Width: | Height: | Size: 73 KiB |
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 83 KiB |
|
Before Width: | Height: | Size: 111 KiB After Width: | Height: | Size: 111 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 26 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 137 KiB After Width: | Height: | Size: 137 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 8.9 KiB After Width: | Height: | Size: 9.0 KiB |
|
Before Width: | Height: | Size: 76 KiB After Width: | Height: | Size: 76 KiB |
|
Before Width: | Height: | Size: 49 KiB After Width: | Height: | Size: 49 KiB |
|
Before Width: | Height: | Size: 113 KiB After Width: | Height: | Size: 113 KiB |
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 84 KiB |
|
Before Width: | Height: | Size: 111 KiB After Width: | Height: | Size: 111 KiB |
|
Before Width: | Height: | Size: 118 KiB After Width: | Height: | Size: 118 KiB |
|
Before Width: | Height: | Size: 119 KiB After Width: | Height: | Size: 119 KiB |
|
Before Width: | Height: | Size: 112 KiB After Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 72 KiB |
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 54 KiB |
|
Before Width: | Height: | Size: 8.9 KiB After Width: | Height: | Size: 9.0 KiB |
|
Before Width: | Height: | Size: 8.9 KiB After Width: | Height: | Size: 9.0 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
|
Before Width: | Height: | Size: 73 KiB After Width: | Height: | Size: 72 KiB |
|
Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 73 KiB |
|
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 54 KiB |
|
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 50 KiB |
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 99 KiB After Width: | Height: | Size: 99 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 46 KiB |
|
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 44 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 46 KiB |
|
Before Width: | Height: | Size: 104 KiB After Width: | Height: | Size: 104 KiB |
|
Before Width: | Height: | Size: 117 KiB After Width: | Height: | Size: 117 KiB |
|
Before Width: | Height: | Size: 118 KiB After Width: | Height: | Size: 118 KiB |
|
Before Width: | Height: | Size: 120 KiB After Width: | Height: | Size: 120 KiB |
|
Before Width: | Height: | Size: 107 KiB After Width: | Height: | Size: 107 KiB |
|
Before Width: | Height: | Size: 119 KiB After Width: | Height: | Size: 119 KiB |
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 27 KiB |
|
Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 72 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 8.9 KiB After Width: | Height: | Size: 9.0 KiB |
|
Before Width: | Height: | Size: 76 KiB After Width: | Height: | Size: 76 KiB |
|
Before Width: | Height: | Size: 100 KiB After Width: | Height: | Size: 100 KiB |
|
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 50 KiB |
|
Before Width: | Height: | Size: 112 KiB After Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
@ -0,0 +1,178 @@ |
|||
/** |
|||
* 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 com.google.common.util.concurrent.ListenableFuture; |
|||
import dev.langchain4j.model.chat.request.ChatRequest; |
|||
import io.swagger.v3.oas.annotations.Parameter; |
|||
import io.swagger.v3.oas.annotations.media.Schema; |
|||
import jakarta.validation.Valid; |
|||
import lombok.RequiredArgsConstructor; |
|||
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.springframework.web.context.request.async.DeferredResult; |
|||
import org.thingsboard.server.common.data.ai.AiModel; |
|||
import org.thingsboard.server.common.data.ai.dto.TbChatRequest; |
|||
import org.thingsboard.server.common.data.ai.dto.TbChatResponse; |
|||
import org.thingsboard.server.common.data.ai.model.chat.AiChatModelConfig; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.AiModelId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.config.annotations.ApiOperation; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.ai.AiChatModelService; |
|||
import org.thingsboard.server.service.security.permission.Operation; |
|||
import org.thingsboard.server.service.security.permission.Resource; |
|||
|
|||
import java.time.Duration; |
|||
import java.util.Optional; |
|||
import java.util.UUID; |
|||
|
|||
import static com.google.common.util.concurrent.MoreExecutors.directExecutor; |
|||
import static org.thingsboard.server.controller.ControllerConstants.AI_MODEL_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 |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
@RequestMapping("/api/ai/model") |
|||
class AiModelController extends BaseController { |
|||
|
|||
private final AiChatModelService aiChatModelService; |
|||
|
|||
@ApiOperation( |
|||
value = "Create or update AI model (saveAiModel)", |
|||
notes = "Creates or updates an AI model record.\n\n" + |
|||
"• **Create:** Omit the `id` to create a new record. The platform assigns a UUID to the new record 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 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 AiModel saveAiModel(@RequestBody @Valid AiModel model) throws ThingsboardException { |
|||
var user = getCurrentUser(); |
|||
model.setTenantId(user.getTenantId()); |
|||
checkEntity(model.getId(), model, Resource.AI_MODEL); |
|||
return tbAiModelService.save(model, user); |
|||
} |
|||
|
|||
@ApiOperation( |
|||
value = "Get AI model by ID (getAiModelById)", |
|||
notes = "Fetches an AI model record by its `id`." + |
|||
TENANT_AUTHORITY_PARAGRAPH |
|||
) |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
@GetMapping("/{modelUuid}") |
|||
public AiModel getAiModelById( |
|||
@Parameter( |
|||
description = "ID of the AI model record", |
|||
required = true, |
|||
example = "de7900d4-30e2-11f0-9cd2-0242ac120002" |
|||
) |
|||
@PathVariable UUID modelUuid |
|||
) throws ThingsboardException { |
|||
return checkAiModelId(new AiModelId(modelUuid), Operation.READ); |
|||
} |
|||
|
|||
@ApiOperation( |
|||
value = "Get AI models (getAiModels)", |
|||
notes = "Returns a page of AI models. " + |
|||
PAGE_DATA_PARAMETERS + TENANT_AUTHORITY_PARAGRAPH |
|||
) |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
@GetMapping |
|||
public PageData<AiModel> getAiModels( |
|||
@Parameter(description = PAGE_SIZE_DESCRIPTION, required = true) |
|||
@RequestParam int pageSize, |
|||
@Parameter(description = PAGE_NUMBER_DESCRIPTION, required = true) |
|||
@RequestParam int page, |
|||
@Parameter(description = AI_MODEL_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, Operation.READ); |
|||
var pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); |
|||
return aiModelService.findAiModelsByTenantId(user.getTenantId(), pageLink); |
|||
} |
|||
|
|||
@ApiOperation( |
|||
value = "Delete AI model by ID (deleteAiModelById)", |
|||
notes = "Deletes the AI model 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("/{modelUuid}") |
|||
public boolean deleteAiModelById( |
|||
@Parameter( |
|||
description = "ID of the AI model record", |
|||
required = true, |
|||
example = "de7900d4-30e2-11f0-9cd2-0242ac120002" |
|||
) |
|||
@PathVariable UUID modelUuid |
|||
) throws ThingsboardException { |
|||
var user = getCurrentUser(); |
|||
var modelId = new AiModelId(modelUuid); |
|||
accessControlService.checkPermission(user, Resource.AI_MODEL, Operation.DELETE); |
|||
Optional<AiModel> toDelete = aiModelService.findAiModelByTenantIdAndId(user.getTenantId(), modelId); |
|||
if (toDelete.isEmpty()) { |
|||
return false; |
|||
} |
|||
accessControlService.checkPermission(user, Resource.AI_MODEL, Operation.DELETE, modelId, toDelete.get()); |
|||
return tbAiModelService.delete(toDelete.get(), user); |
|||
} |
|||
|
|||
@ApiOperation( |
|||
value = "Send request to AI chat model (sendChatRequest)", |
|||
notes = "Submits a single prompt - made up of an optional system message and a required user message - to the specified AI chat model " + |
|||
"and returns either the generated answer or an error envelope." + |
|||
TENANT_AUTHORITY_PARAGRAPH |
|||
) |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
@PostMapping("/chat") |
|||
public DeferredResult<TbChatResponse> sendChatRequest(@Valid @RequestBody TbChatRequest tbChatRequest) { |
|||
ChatRequest langChainChatRequest = tbChatRequest.toLangChainChatRequest(); |
|||
AiChatModelConfig<?> chatModelConfig = tbChatRequest.chatModelConfig(); |
|||
|
|||
ListenableFuture<TbChatResponse> future = aiChatModelService.sendChatRequestAsync(chatModelConfig, langChainChatRequest) |
|||
.transform(chatResponse -> (TbChatResponse) new TbChatResponse.Success(chatResponse.aiMessage().text()), directExecutor()) |
|||
.catching(Throwable.class, ex -> new TbChatResponse.Failure(ex.getMessage()), directExecutor()); |
|||
|
|||
Integer requestTimeoutSeconds = chatModelConfig.timeoutSeconds(); |
|||
return requestTimeoutSeconds != null ? wrapFuture(future, Duration.ofSeconds(requestTimeoutSeconds).toMillis()) : wrapFuture(future); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
/** |
|||
* 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 lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.security.access.prepost.PreAuthorize; |
|||
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.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RequestParam; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
import org.thingsboard.rule.engine.api.JobManager; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.JobId; |
|||
import org.thingsboard.server.common.data.job.Job; |
|||
import org.thingsboard.server.common.data.job.JobFilter; |
|||
import org.thingsboard.server.common.data.job.JobStatus; |
|||
import org.thingsboard.server.common.data.job.JobType; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.security.permission.Operation; |
|||
|
|||
import java.util.List; |
|||
import java.util.UUID; |
|||
|
|||
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; |
|||
|
|||
@RestController |
|||
@TbCoreComponent |
|||
@RequestMapping("/api") |
|||
@RequiredArgsConstructor |
|||
@Slf4j |
|||
public class JobController extends BaseController { |
|||
|
|||
private final JobManager jobManager; |
|||
|
|||
@GetMapping("/job/{id}") |
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
|||
public Job getJobById(@PathVariable UUID id) throws ThingsboardException { |
|||
JobId jobId = new JobId(id); |
|||
return checkJobId(jobId, Operation.READ); |
|||
} |
|||
|
|||
@GetMapping("/jobs") |
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
|||
public PageData<Job> getJobs(@Parameter(description = PAGE_SIZE_DESCRIPTION, required = true) |
|||
@RequestParam int pageSize, |
|||
@Parameter(description = PAGE_NUMBER_DESCRIPTION, required = true) |
|||
@RequestParam int page, |
|||
@Parameter(description = "Case-insensitive 'substring' filter based on job's description") |
|||
@RequestParam(required = false) String textSearch, |
|||
@Parameter(description = SORT_PROPERTY_DESCRIPTION) |
|||
@RequestParam(required = false) String sortProperty, |
|||
@Parameter(description = SORT_ORDER_DESCRIPTION) |
|||
@RequestParam(required = false) String sortOrder, |
|||
@RequestParam(required = false) List<JobType> types, |
|||
@RequestParam(required = false) List<JobStatus> statuses, |
|||
@RequestParam(required = false) List<UUID> entities, |
|||
@RequestParam(required = false) Long startTime, |
|||
@RequestParam(required = false) Long endTime) throws ThingsboardException { |
|||
PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); |
|||
JobFilter filter = JobFilter.builder() |
|||
.types(types) |
|||
.statuses(statuses) |
|||
.entities(entities) |
|||
.startTime(startTime) |
|||
.endTime(endTime) |
|||
.build(); |
|||
return jobService.findJobsByFilter(getTenantId(), filter, pageLink); |
|||
} |
|||
|
|||
@PostMapping("/job/{id}/cancel") |
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
|||
public void cancelJob(@PathVariable UUID id) throws ThingsboardException { |
|||
JobId jobId = new JobId(id); |
|||
checkJobId(jobId, Operation.WRITE); |
|||
jobManager.cancelJob(getTenantId(), jobId); |
|||
} |
|||
|
|||
@PostMapping("/job/{id}/reprocess") |
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
|||
public void reprocessJob(@PathVariable UUID id) throws ThingsboardException { |
|||
JobId jobId = new JobId(id); |
|||
checkJobId(jobId, Operation.WRITE); |
|||
jobManager.reprocessJob(getTenantId(), jobId); |
|||
} |
|||
|
|||
@DeleteMapping("/job/{id}") |
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
|||
public void deleteJob(@PathVariable UUID id) throws ThingsboardException { |
|||
JobId jobId = new JobId(id); |
|||
checkJobId(jobId, Operation.DELETE); |
|||
jobService.deleteJob(getTenantId(), jobId); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
/** |
|||
* 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.ai; |
|||
|
|||
import org.thingsboard.rule.engine.api.RuleEngineAiChatModelService; |
|||
|
|||
public interface AiChatModelService extends RuleEngineAiChatModelService {} |
|||
@ -0,0 +1,40 @@ |
|||
/** |
|||
* 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.ai; |
|||
|
|||
import com.google.common.util.concurrent.FluentFuture; |
|||
import dev.langchain4j.model.chat.ChatModel; |
|||
import dev.langchain4j.model.chat.request.ChatRequest; |
|||
import dev.langchain4j.model.chat.response.ChatResponse; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.ai.model.chat.AiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.Langchain4jChatModelConfigurer; |
|||
|
|||
@Service |
|||
@RequiredArgsConstructor |
|||
class AiChatModelServiceImpl implements AiChatModelService { |
|||
|
|||
private final Langchain4jChatModelConfigurer chatModelConfigurer; |
|||
private final AiRequestsExecutor aiRequestsExecutor; |
|||
|
|||
@Override |
|||
public <C extends AiChatModelConfig<C>> FluentFuture<ChatResponse> sendChatRequestAsync(AiChatModelConfig<C> chatModelConfig, ChatRequest chatRequest) { |
|||
ChatModel langChainChatModel = chatModelConfig.configure(chatModelConfigurer); |
|||
return aiRequestsExecutor.sendChatRequestAsync(langChainChatModel, chatRequest); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
/** |
|||
* 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.ai; |
|||
|
|||
import com.google.common.util.concurrent.FluentFuture; |
|||
import dev.langchain4j.model.chat.ChatModel; |
|||
import dev.langchain4j.model.chat.request.ChatRequest; |
|||
import dev.langchain4j.model.chat.response.ChatResponse; |
|||
|
|||
public interface AiRequestsExecutor { |
|||
|
|||
FluentFuture<ChatResponse> sendChatRequestAsync(ChatModel chatModel, ChatRequest chatRequest); |
|||
|
|||
} |
|||
@ -0,0 +1,86 @@ |
|||
/** |
|||
* 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.ai; |
|||
|
|||
import com.google.common.util.concurrent.FluentFuture; |
|||
import com.google.common.util.concurrent.ListeningExecutorService; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import dev.langchain4j.model.chat.ChatModel; |
|||
import dev.langchain4j.model.chat.request.ChatRequest; |
|||
import dev.langchain4j.model.chat.response.ChatResponse; |
|||
import jakarta.annotation.PostConstruct; |
|||
import jakarta.annotation.PreDestroy; |
|||
import jakarta.validation.constraints.Min; |
|||
import jakarta.validation.constraints.NotBlank; |
|||
import lombok.Data; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.context.annotation.Lazy; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.validation.annotation.Validated; |
|||
import org.thingsboard.common.util.ThingsBoardThreadFactory; |
|||
|
|||
import java.time.Duration; |
|||
import java.util.concurrent.Executors; |
|||
|
|||
@Lazy |
|||
@Component |
|||
@RequiredArgsConstructor |
|||
class DefaultAiRequestsExecutor implements AiRequestsExecutor { |
|||
|
|||
private final AiRequestsExecutorProperties properties; |
|||
|
|||
@Data |
|||
@Validated |
|||
@Configuration |
|||
@ConfigurationProperties(prefix = "actors.rule.ai-requests-thread-pool") |
|||
private static class AiRequestsExecutorProperties { |
|||
|
|||
@NotBlank(message = "Pool name must be not blank") |
|||
private String poolName = "ai-requests"; |
|||
|
|||
@Min(value = 1, message = "Pool size must be at least 1") |
|||
private int poolSize = 50; |
|||
|
|||
@Min(value = 1, message = "Termination timeout must be at least 1 second") |
|||
private int terminationTimeoutSeconds = 60; |
|||
|
|||
} |
|||
|
|||
private ListeningExecutorService executorService; |
|||
|
|||
@PostConstruct |
|||
private void init() { |
|||
executorService = MoreExecutors.listeningDecorator( |
|||
Executors.newFixedThreadPool(properties.getPoolSize(), ThingsBoardThreadFactory.forName(properties.getPoolName())) |
|||
); |
|||
} |
|||
|
|||
@Override |
|||
public FluentFuture<ChatResponse> sendChatRequestAsync(ChatModel chatModel, ChatRequest chatRequest) { |
|||
return FluentFuture.from(executorService.submit(() -> chatModel.chat(chatRequest))); |
|||
} |
|||
|
|||
@PreDestroy |
|||
private void destroy() { |
|||
if (executorService != null) { |
|||
MoreExecutors.shutdownAndAwaitTermination(executorService, Duration.ofSeconds(properties.getTerminationTimeoutSeconds())); |
|||
executorService = null; |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,269 @@ |
|||
/** |
|||
* 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.ai; |
|||
|
|||
import com.google.api.gax.core.FixedCredentialsProvider; |
|||
import com.google.api.gax.retrying.RetrySettings; |
|||
import com.google.auth.oauth2.ServiceAccountCredentials; |
|||
import com.google.cloud.vertexai.Transport; |
|||
import com.google.cloud.vertexai.VertexAI; |
|||
import com.google.cloud.vertexai.api.GenerationConfig; |
|||
import com.google.cloud.vertexai.api.PredictionServiceClient; |
|||
import com.google.cloud.vertexai.api.PredictionServiceSettings; |
|||
import com.google.cloud.vertexai.generativeai.GenerativeModel; |
|||
import dev.langchain4j.model.anthropic.AnthropicChatModel; |
|||
import dev.langchain4j.model.azure.AzureOpenAiChatModel; |
|||
import dev.langchain4j.model.bedrock.BedrockChatModel; |
|||
import dev.langchain4j.model.chat.ChatModel; |
|||
import dev.langchain4j.model.chat.request.ChatRequestParameters; |
|||
import dev.langchain4j.model.github.GitHubModelsChatModel; |
|||
import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; |
|||
import dev.langchain4j.model.mistralai.MistralAiChatModel; |
|||
import dev.langchain4j.model.openai.OpenAiChatModel; |
|||
import dev.langchain4j.model.vertexai.gemini.VertexAiGeminiChatModel; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.ai.model.chat.AmazonBedrockChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.AnthropicChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.AzureOpenAiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.GitHubModelsChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.GoogleAiGeminiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.GoogleVertexAiGeminiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.Langchain4jChatModelConfigurer; |
|||
import org.thingsboard.server.common.data.ai.model.chat.MistralAiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.OpenAiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.provider.AmazonBedrockProviderConfig; |
|||
import org.thingsboard.server.common.data.ai.provider.AzureOpenAiProviderConfig; |
|||
import org.thingsboard.server.common.data.ai.provider.GoogleVertexAiGeminiProviderConfig; |
|||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
|||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
|||
import software.amazon.awssdk.regions.Region; |
|||
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; |
|||
|
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.IOException; |
|||
import java.time.Duration; |
|||
|
|||
@Component |
|||
class Langchain4jChatModelConfigurerImpl implements Langchain4jChatModelConfigurer { |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(OpenAiChatModelConfig chatModelConfig) { |
|||
return OpenAiChatModel.builder() |
|||
.apiKey(chatModelConfig.providerConfig().apiKey()) |
|||
.modelName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.frequencyPenalty(chatModelConfig.frequencyPenalty()) |
|||
.presencePenalty(chatModelConfig.presencePenalty()) |
|||
.maxTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(AzureOpenAiChatModelConfig chatModelConfig) { |
|||
AzureOpenAiProviderConfig providerConfig = chatModelConfig.providerConfig(); |
|||
return AzureOpenAiChatModel.builder() |
|||
.endpoint(providerConfig.endpoint()) |
|||
.serviceVersion(providerConfig.serviceVersion()) |
|||
.apiKey(providerConfig.apiKey()) |
|||
.deploymentName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.frequencyPenalty(chatModelConfig.frequencyPenalty()) |
|||
.presencePenalty(chatModelConfig.presencePenalty()) |
|||
.maxTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(GoogleAiGeminiChatModelConfig chatModelConfig) { |
|||
return GoogleAiGeminiChatModel.builder() |
|||
.apiKey(chatModelConfig.providerConfig().apiKey()) |
|||
.modelName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.topK(chatModelConfig.topK()) |
|||
.frequencyPenalty(chatModelConfig.frequencyPenalty()) |
|||
.presencePenalty(chatModelConfig.presencePenalty()) |
|||
.maxOutputTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(GoogleVertexAiGeminiChatModelConfig chatModelConfig) { |
|||
GoogleVertexAiGeminiProviderConfig providerConfig = chatModelConfig.providerConfig(); |
|||
|
|||
// construct service account credentials using service account key JSON
|
|||
ServiceAccountCredentials serviceAccountCredentials; |
|||
try { |
|||
serviceAccountCredentials = ServiceAccountCredentials.fromStream(new ByteArrayInputStream(providerConfig.serviceAccountKey().getBytes())); |
|||
} catch (IOException e) { |
|||
throw new RuntimeException("Failed to parse service account key JSON", e); |
|||
} |
|||
|
|||
PredictionServiceSettings predictionServiceClientSettings; |
|||
try { |
|||
// create prediction service settings for REST transport with service account key credentials
|
|||
PredictionServiceSettings.Builder settingsBuilder = PredictionServiceSettings.newHttpJsonBuilder() |
|||
.setCredentialsProvider(FixedCredentialsProvider.create(serviceAccountCredentials)); |
|||
|
|||
// get the retry settings that control request timeout for generateContent RPC
|
|||
RetrySettings.Builder retrySettings = settingsBuilder |
|||
.generateContentSettings() |
|||
.getRetrySettings() |
|||
.toBuilder(); |
|||
|
|||
// set request timeout from model config
|
|||
if (chatModelConfig.timeoutSeconds() != null) { |
|||
retrySettings.setTotalTimeout(org.threeten.bp.Duration.ofSeconds(chatModelConfig.timeoutSeconds())); |
|||
} |
|||
|
|||
// set updated retry settings
|
|||
settingsBuilder.generateContentSettings().setRetrySettings(retrySettings.build()); |
|||
|
|||
// build the client settings
|
|||
predictionServiceClientSettings = settingsBuilder.build(); |
|||
} catch (IOException e) { |
|||
throw new RuntimeException("Failed to create prediction service client settings", e); |
|||
} |
|||
|
|||
// construct Vertex AI instance
|
|||
var vertexAI = new VertexAI.Builder() |
|||
.setProjectId(providerConfig.projectId()) |
|||
.setLocation(providerConfig.location()) |
|||
.setPredictionClientSupplier(() -> createPredictionServiceClient(predictionServiceClientSettings)) |
|||
.setTransport(Transport.REST) // GRPC also possible, but likely does not work with service account keys
|
|||
.build(); |
|||
|
|||
// map model config to generation config
|
|||
var generationConfigBuilder = GenerationConfig.newBuilder(); |
|||
if (chatModelConfig.temperature() != null) { |
|||
generationConfigBuilder.setTemperature(chatModelConfig.temperature().floatValue()); |
|||
} |
|||
if (chatModelConfig.topP() != null) { |
|||
generationConfigBuilder.setTopP(chatModelConfig.topP().floatValue()); |
|||
} |
|||
if (chatModelConfig.topK() != null) { |
|||
generationConfigBuilder.setTopK(chatModelConfig.topK()); |
|||
} |
|||
if (chatModelConfig.frequencyPenalty() != null) { |
|||
generationConfigBuilder.setFrequencyPenalty(chatModelConfig.frequencyPenalty().floatValue()); |
|||
} |
|||
if (chatModelConfig.frequencyPenalty() != null) { |
|||
generationConfigBuilder.setPresencePenalty(chatModelConfig.frequencyPenalty().floatValue()); |
|||
} |
|||
if (chatModelConfig.maxOutputTokens() != null) { |
|||
generationConfigBuilder.setMaxOutputTokens(chatModelConfig.maxOutputTokens()); |
|||
} |
|||
var generationConfig = generationConfigBuilder.build(); |
|||
|
|||
// construct generative model instance
|
|||
var generativeModel = new GenerativeModel(chatModelConfig.modelId(), vertexAI).withGenerationConfig(generationConfig); |
|||
|
|||
return new VertexAiGeminiChatModel(generativeModel, generationConfig, chatModelConfig.maxRetries()); |
|||
} |
|||
|
|||
private static PredictionServiceClient createPredictionServiceClient(PredictionServiceSettings settings) { |
|||
try { |
|||
return PredictionServiceClient.create(settings); |
|||
} catch (IOException e) { |
|||
throw new RuntimeException("Failed to create prediction service client", e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(MistralAiChatModelConfig chatModelConfig) { |
|||
return MistralAiChatModel.builder() |
|||
.apiKey(chatModelConfig.providerConfig().apiKey()) |
|||
.modelName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.frequencyPenalty(chatModelConfig.frequencyPenalty()) |
|||
.presencePenalty(chatModelConfig.presencePenalty()) |
|||
.maxTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(AnthropicChatModelConfig chatModelConfig) { |
|||
return AnthropicChatModel.builder() |
|||
.apiKey(chatModelConfig.providerConfig().apiKey()) |
|||
.modelName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.topK(chatModelConfig.topK()) |
|||
.maxTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(AmazonBedrockChatModelConfig chatModelConfig) { |
|||
AmazonBedrockProviderConfig providerConfig = chatModelConfig.providerConfig(); |
|||
|
|||
var credentialsProvider = StaticCredentialsProvider.create( |
|||
AwsBasicCredentials.create(providerConfig.accessKeyId(), providerConfig.secretAccessKey()) |
|||
); |
|||
|
|||
var bedrockClient = BedrockRuntimeClient.builder() |
|||
.region(Region.of(providerConfig.region())) |
|||
.credentialsProvider(credentialsProvider) |
|||
.build(); |
|||
|
|||
var defaultChatRequestParams = ChatRequestParameters.builder() |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.maxOutputTokens(chatModelConfig.maxOutputTokens()) |
|||
.build(); |
|||
|
|||
return BedrockChatModel.builder() |
|||
.client(bedrockClient) |
|||
.modelId(chatModelConfig.modelId()) |
|||
.defaultRequestParameters(defaultChatRequestParams) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(GitHubModelsChatModelConfig chatModelConfig) { |
|||
return GitHubModelsChatModel.builder() |
|||
.gitHubToken(chatModelConfig.providerConfig().personalAccessToken()) |
|||
.modelName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.frequencyPenalty(chatModelConfig.frequencyPenalty()) |
|||
.presencePenalty(chatModelConfig.presencePenalty()) |
|||
.maxTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
private static Duration toDuration(Integer timeoutSeconds) { |
|||
return timeoutSeconds != null ? Duration.ofSeconds(timeoutSeconds) : null; |
|||
} |
|||
|
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
|
|||
} |
|||