diff --git a/.github/ISSUE_TEMPLATE/---bug-report.md b/.github/ISSUE_TEMPLATE/---bug-report.md index bffb88ebb7..6d8889f98d 100644 --- a/.github/ISSUE_TEMPLATE/---bug-report.md +++ b/.github/ISSUE_TEMPLATE/---bug-report.md @@ -1,9 +1,9 @@ --- name: "\U0001F41E Bug report" about: Create a report to help us improve -title: "[Bug] " -labels: bug -assignees: ashvayka, vvlladd28 +title: "Your title here" +labels: ['bug', 'unconfirmed'] +assignees: AndriichnekoDm --- @@ -12,11 +12,13 @@ A clear and concise description of what the bug is. **Your Server Environment** -* demo.thingsboard.io -* cloud.thingsboard.io +* [https://demo.thingsboard.io](demo.thingsboard.io) +* [https://thingsboard.cloud](thingsboard.cloud) * own setup - * cloud or local infrastructure or docker deployment + * Deployment: monolith or microservices + * Deployment type: deb, rpm, exe, docker-compose, k8s, ami * ThingsBoard Version + * Community or Professional Edition * OS Name and Version **Your Client Environment** @@ -54,7 +56,7 @@ Steps to reproduce the behavior: A clear and concise description of what you expected to happen. **Screenshots** -If applicable, add screenshots to help explain your problem. +If applicable, please add screenshots to help explain your problem. **Additional context** -Add any other context about the problem here. +Please feel free to add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index e5a710c155..c73f810cd8 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -1,8 +1,8 @@ --- name: Feature request about: Suggest an idea for this project -title: "[Feature Request]" -labels: ['Feature'] +title: "Your title here" +labels: ['feature'] assignees: 'AndriichnekoDm' --- diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md index 99d82ca698..2f15690f68 100644 --- a/.github/ISSUE_TEMPLATE/question.md +++ b/.github/ISSUE_TEMPLATE/question.md @@ -1,9 +1,9 @@ --- name: Question -about: Describe your questions in details -title: "[Question] your title here" -labels: question -assignees: ashvayka +about: Describe your questions in detail +title: "Your title here" +labels: ['question'] +assignees: 'AndriichnekoDm' --- @@ -16,7 +16,7 @@ assignees: ashvayka * Generic **Description** -A clear and concise details. +Clear and concise details. **Environment** diff --git a/application/src/main/data/json/system/widget_types/single_switch.json b/application/src/main/data/json/system/widget_types/single_switch.json index 7e5440b41d..4a62cc0598 100644 --- a/application/src/main/data/json/system/widget_types/single_switch.json +++ b/application/src/main/data/json/system/widget_types/single_switch.json @@ -11,7 +11,7 @@ "resources": [], "templateHtml": "\n", "templateCss": "", - "controllerScript": "self.onInit = function() {\n self.ctx.$scope.rpcWidget.onInit();\n}\n\nself.typeParameters = function() {\n return {\n previewWidth: '280px',\n previewHeight: '80px',\n embedTitlePanel: true,\n displayRpcMessageToast: false\n };\n};\n\nself.onDestroy = function() {\n}\n", + "controllerScript": "self.onInit = function() {\n self.ctx.$scope.actionWidget.onInit();\n}\n\nself.typeParameters = function() {\n return {\n previewWidth: '280px',\n previewHeight: '80px',\n embedTitlePanel: true,\n displayRpcMessageToast: false\n };\n};\n\nself.onDestroy = function() {\n}\n", "settingsSchema": "", "dataKeySettingsSchema": "{}\n", "settingsDirective": "tb-single-switch-widget-settings", diff --git a/application/src/main/java/org/thingsboard/server/controller/BaseController.java b/application/src/main/java/org/thingsboard/server/controller/BaseController.java index f36f63f069..c40760dda3 100644 --- a/application/src/main/java/org/thingsboard/server/controller/BaseController.java +++ b/application/src/main/java/org/thingsboard/server/controller/BaseController.java @@ -16,14 +16,13 @@ package org.thingsboard.server.controller; import com.fasterxml.jackson.databind.node.ObjectNode; -import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.MoreExecutors; import lombok.Getter; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.dao.DataAccessException; import org.springframework.http.MediaType; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -376,9 +375,14 @@ public abstract class BaseController { return new ThingsboardException("Unable to send mail: " + exception.getMessage(), ThingsboardErrorCode.GENERAL); } else if (exception instanceof AsyncRequestTimeoutException) { return new ThingsboardException("Request timeout", ThingsboardErrorCode.GENERAL); - } else { - return new ThingsboardException(exception.getMessage(), exception, ThingsboardErrorCode.GENERAL); + } else if (exception instanceof DataAccessException) { + String errorType = exception.getClass().getSimpleName(); + if (!logControllerErrorStackTrace) { // not to log the error twice + log.warn("Database error: {} - {}", errorType, ExceptionUtils.getRootCauseMessage(exception)); + } + return new ThingsboardException("Database error", ThingsboardErrorCode.GENERAL); } + return new ThingsboardException(exception.getMessage(), exception, ThingsboardErrorCode.GENERAL); } /** diff --git a/application/src/main/java/org/thingsboard/server/service/subscription/DefaultSubscriptionManagerService.java b/application/src/main/java/org/thingsboard/server/service/subscription/DefaultSubscriptionManagerService.java index 96bdbd5561..5dc6b0e636 100644 --- a/application/src/main/java/org/thingsboard/server/service/subscription/DefaultSubscriptionManagerService.java +++ b/application/src/main/java/org/thingsboard/server/service/subscription/DefaultSubscriptionManagerService.java @@ -19,7 +19,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; -import org.thingsboard.server.common.msg.rule.engine.DeviceAttributesEventNotificationMsg; import org.thingsboard.server.cluster.TbClusterService; import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.EntityType; @@ -37,15 +36,16 @@ import org.thingsboard.server.common.data.kv.TsKvEntry; import org.thingsboard.server.common.msg.queue.ServiceType; import org.thingsboard.server.common.msg.queue.TbCallback; import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; +import org.thingsboard.server.common.msg.rule.engine.DeviceAttributesEventNotificationMsg; import org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg; import org.thingsboard.server.queue.TbQueueProducer; import org.thingsboard.server.queue.common.TbProtoQueueMsg; -import org.thingsboard.server.queue.discovery.TopicService; import org.thingsboard.server.queue.discovery.PartitionService; import org.thingsboard.server.queue.discovery.TbApplicationEventListener; import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; -import org.thingsboard.server.queue.discovery.event.PartitionChangeEvent; +import org.thingsboard.server.queue.discovery.TopicService; import org.thingsboard.server.queue.discovery.event.OtherServiceShutdownEvent; +import org.thingsboard.server.queue.discovery.event.PartitionChangeEvent; import org.thingsboard.server.queue.provider.TbQueueProducerProvider; import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.state.DefaultDeviceStateService; @@ -154,7 +154,7 @@ public class DefaultSubscriptionManagerService extends TbApplicationEventListene protected void onTbApplicationEvent(PartitionChangeEvent partitionChangeEvent) { if (ServiceType.TB_CORE.equals(partitionChangeEvent.getServiceType())) { entitySubscriptions.values().removeIf(sub -> - !partitionService.resolve(ServiceType.TB_CORE, sub.getTenantId(), sub.getEntityId()).isMyPartition()); + !partitionService.isMyPartition(ServiceType.TB_CORE, sub.getTenantId(), sub.getEntityId())); } } diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/audit/AuditLogService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/audit/AuditLogService.java index 08dd64275a..2e65a58647 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/audit/AuditLogService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/audit/AuditLogService.java @@ -38,7 +38,7 @@ public interface AuditLogService { PageData findAuditLogsByTenantId(TenantId tenantId, List actionTypes, TimePageLink pageLink); - ListenableFuture> logEntityAction( + ListenableFuture logEntityAction( TenantId tenantId, CustomerId customerId, UserId userId, diff --git a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/HashPartitionService.java b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/HashPartitionService.java index 6caf17aeef..c05bc77df3 100644 --- a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/HashPartitionService.java +++ b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/HashPartitionService.java @@ -251,7 +251,12 @@ public class HashPartitionService implements PartitionService { @Override public boolean isMyPartition(ServiceType serviceType, TenantId tenantId, EntityId entityId) { - return resolve(serviceType, tenantId, entityId).isMyPartition(); + try { + return resolve(serviceType, tenantId, entityId).isMyPartition(); + } catch (TenantNotFoundException e) { + log.warn("Tenant with id {} not found", tenantId, new RuntimeException("stacktrace")); + return false; + } } private TopicPartitionInfo resolve(QueueKey queueKey, EntityId entityId) { @@ -376,7 +381,12 @@ public class HashPartitionService implements PartitionService { .collect(Collectors.toList())) .collect(Collectors.joining(System.lineSeparator()))); } - applicationEventPublisher.publishEvent(new PartitionChangeEvent(this, serviceType, partitionsMap)); + PartitionChangeEvent event = new PartitionChangeEvent(this, serviceType, partitionsMap); + try { + applicationEventPublisher.publishEvent(event); + } catch (Exception e) { + log.error("Failed to publish partition change event {}", event, e); + } } @Override diff --git a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/TbApplicationEventListener.java b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/TbApplicationEventListener.java index 42039bf417..2d6d707731 100644 --- a/common/queue/src/main/java/org/thingsboard/server/queue/discovery/TbApplicationEventListener.java +++ b/common/queue/src/main/java/org/thingsboard/server/queue/discovery/TbApplicationEventListener.java @@ -41,7 +41,11 @@ public abstract class TbApplicationEventListener i seqNumberLock.unlock(); } if (validUpdate && filterTbApplicationEvent(event)) { - onTbApplicationEvent(event); + try { + onTbApplicationEvent(event); + } catch (Exception e) { + log.error("Failed to handle partition change event: {}", event, e); + } } else { log.info("Application event ignored due to invalid sequence number ({} > {}). Event: {}", lastProcessedSequenceNumber, event.getSequenceNumber(), event); } diff --git a/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/DefaultClusterVersionControlService.java b/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/DefaultClusterVersionControlService.java index 60e75b5907..1d6688cebc 100644 --- a/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/DefaultClusterVersionControlService.java +++ b/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/DefaultClusterVersionControlService.java @@ -160,7 +160,7 @@ public class DefaultClusterVersionControlService extends TbApplicationEventListe @Override protected void onTbApplicationEvent(PartitionChangeEvent event) { for (TenantId tenantId : vcService.getActiveRepositoryTenants()) { - if (!partitionService.resolve(ServiceType.TB_VC_EXECUTOR, tenantId, tenantId).isMyPartition()) { + if (!partitionService.isMyPartition(ServiceType.TB_VC_EXECUTOR, tenantId, tenantId)) { var lock = getRepoLock(tenantId); lock.lock(); try { diff --git a/dao/src/main/java/org/thingsboard/server/dao/alarm/AlarmCommentDao.java b/dao/src/main/java/org/thingsboard/server/dao/alarm/AlarmCommentDao.java index b1395ee34b..91b4f339b1 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/alarm/AlarmCommentDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/alarm/AlarmCommentDao.java @@ -18,7 +18,6 @@ package org.thingsboard.server.dao.alarm; import com.google.common.util.concurrent.ListenableFuture; import org.thingsboard.server.common.data.alarm.AlarmComment; import org.thingsboard.server.common.data.alarm.AlarmCommentInfo; -import org.thingsboard.server.common.data.id.AlarmCommentId; import org.thingsboard.server.common.data.id.AlarmId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; @@ -29,13 +28,10 @@ import java.util.UUID; public interface AlarmCommentDao extends Dao { - AlarmComment createAlarmComment(TenantId tenantId, AlarmComment alarmComment); - - void deleteAlarmComment(TenantId tenantId, AlarmCommentId alarmCommentId); - AlarmComment findAlarmCommentById(TenantId tenantId, UUID key); PageData findAlarmComments(TenantId tenantId, AlarmId id, PageLink pageLink); ListenableFuture findAlarmCommentByIdAsync(TenantId tenantId, UUID key); + } diff --git a/dao/src/main/java/org/thingsboard/server/dao/alarm/BaseAlarmCommentService.java b/dao/src/main/java/org/thingsboard/server/dao/alarm/BaseAlarmCommentService.java index 5bdd9803b2..ac6f4955d8 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/alarm/BaseAlarmCommentService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/alarm/BaseAlarmCommentService.java @@ -15,7 +15,6 @@ */ package org.thingsboard.server.dao.alarm; -import com.datastax.oss.driver.api.core.uuid.Uuids; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.util.concurrent.ListenableFuture; @@ -35,8 +34,6 @@ import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent; import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent; import org.thingsboard.server.dao.service.DataValidator; -import java.util.UUID; - import static org.thingsboard.server.dao.service.Validator.validateId; @Service @@ -101,12 +98,7 @@ public class BaseAlarmCommentService extends AbstractEntityService implements Al if (alarmComment.getType() == null) { alarmComment.setType(AlarmCommentType.OTHER); } - if (alarmComment.getId() == null) { - UUID uuid = Uuids.timeBased(); - alarmComment.setId(new AlarmCommentId(uuid)); - alarmComment.setCreatedTime(Uuids.unixTimestamp(uuid)); - } - return alarmCommentDao.createAlarmComment(tenantId, alarmComment); + return alarmCommentDao.save(tenantId, alarmComment); } private AlarmComment updateAlarmComment(TenantId tenantId, AlarmComment newAlarmComment) { diff --git a/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogDao.java b/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogDao.java index ba4b4283e7..f938aba5a9 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogDao.java @@ -30,8 +30,6 @@ import java.util.UUID; public interface AuditLogDao extends Dao { - ListenableFuture saveByTenantId(AuditLog auditLog); - PageData findAuditLogsByTenantIdAndEntityId(UUID tenantId, EntityId entityId, List actionTypes, TimePageLink pageLink); PageData findAuditLogsByTenantIdAndCustomerId(UUID tenantId, CustomerId customerId, List actionTypes, TimePageLink pageLink); diff --git a/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogServiceImpl.java index 3252a481c4..4350f2a5d1 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/audit/AuditLogServiceImpl.java @@ -15,11 +15,9 @@ */ package org.thingsboard.server.dao.audit; -import com.datastax.oss.driver.api.core.uuid.Uuids; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; -import com.google.common.collect.Lists; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import lombok.extern.slf4j.Slf4j; @@ -34,7 +32,6 @@ import org.thingsboard.server.common.data.alarm.AlarmComment; import org.thingsboard.server.common.data.audit.ActionStatus; import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.audit.AuditLog; -import org.thingsboard.server.common.data.id.AuditLogId; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.TenantId; @@ -50,11 +47,11 @@ import org.thingsboard.server.dao.audit.sink.AuditLogSink; import org.thingsboard.server.dao.device.provision.ProvisionRequest; import org.thingsboard.server.dao.entity.EntityService; import org.thingsboard.server.dao.service.DataValidator; +import org.thingsboard.server.dao.sql.JpaExecutorService; import java.io.PrintWriter; import java.io.StringWriter; import java.util.List; -import java.util.UUID; import java.util.stream.Collectors; import static org.thingsboard.server.dao.service.Validator.validateEntityId; @@ -66,7 +63,6 @@ import static org.thingsboard.server.dao.service.Validator.validateId; public class AuditLogServiceImpl implements AuditLogService { private static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; - private static final int INSERTS_PER_ENTRY = 3; @Autowired private AuditLogLevelFilter auditLogLevelFilter; @@ -80,6 +76,9 @@ public class AuditLogServiceImpl implements AuditLogService { @Autowired private AuditLogSink auditLogSink; + @Autowired + private JpaExecutorService executor; + @Autowired private DataValidator auditLogValidator; @@ -115,7 +114,7 @@ public class AuditLogServiceImpl implements AuditLogService { } @Override - public ListenableFuture> + public ListenableFuture logEntityAction(TenantId tenantId, CustomerId customerId, UserId userId, String userName, I entityId, E entity, ActionType actionType, Exception e, Object... additionalInfo) { if (canLog(entityId.getEntityType(), actionType)) { @@ -370,9 +369,6 @@ public class AuditLogServiceImpl implements AuditLogService { ActionStatus actionStatus, String actionFailureDetails) { AuditLog result = new AuditLog(); - UUID id = Uuids.timeBased(); - result.setId(new AuditLogId(id)); - result.setCreatedTime(Uuids.unixTimestamp(id)); result.setTenantId(tenantId); result.setEntityId(entityId); result.setEntityName(entityName); @@ -386,16 +382,16 @@ public class AuditLogServiceImpl implements AuditLogService { return result; } - private ListenableFuture> logAction(TenantId tenantId, - EntityId entityId, - String entityName, - CustomerId customerId, - UserId userId, - String userName, - ActionType actionType, - JsonNode actionData, - ActionStatus actionStatus, - String actionFailureDetails) { + private ListenableFuture logAction(TenantId tenantId, + EntityId entityId, + String entityName, + CustomerId customerId, + UserId userId, + String userName, + ActionType actionType, + JsonNode actionData, + ActionStatus actionStatus, + String actionFailureDetails) { AuditLog auditLogEntry = createAuditLogEntry(tenantId, entityId, entityName, customerId, userId, userName, actionType, actionData, actionStatus, actionFailureDetails); log.trace("Executing logAction [{}]", auditLogEntry); @@ -408,12 +404,12 @@ public class AuditLogServiceImpl implements AuditLogService { return Futures.immediateFailedFuture(e); } } - List> futures = Lists.newArrayListWithExpectedSize(INSERTS_PER_ENTRY); - futures.add(auditLogDao.saveByTenantId(auditLogEntry)); - auditLogSink.logAction(auditLogEntry); - - return Futures.allAsList(futures); + return executor.submit(() -> { + AuditLog auditLog = auditLogDao.save(tenantId, auditLogEntry); + auditLogSink.logAction(auditLog); + return null; + }); } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/audit/DummyAuditLogServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/audit/DummyAuditLogServiceImpl.java index 87ede9452a..28c00e4d58 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/audit/DummyAuditLogServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/audit/DummyAuditLogServiceImpl.java @@ -55,7 +55,7 @@ public class DummyAuditLogServiceImpl implements AuditLogService { } @Override - public ListenableFuture> logEntityAction(TenantId tenantId, CustomerId customerId, UserId userId, String userName, I entityId, E entity, ActionType actionType, Exception e, Object... additionalInfo) { + public ListenableFuture logEntityAction(TenantId tenantId, CustomerId customerId, UserId userId, String userName, I entityId, E entity, ActionType actionType, Exception e, Object... additionalInfo) { return null; } diff --git a/dao/src/main/java/org/thingsboard/server/dao/audit/sink/ElasticsearchAuditLogSink.java b/dao/src/main/java/org/thingsboard/server/dao/audit/sink/ElasticsearchAuditLogSink.java index 18e24b9094..f2dc272848 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/audit/sink/ElasticsearchAuditLogSink.java +++ b/dao/src/main/java/org/thingsboard/server/dao/audit/sink/ElasticsearchAuditLogSink.java @@ -34,14 +34,18 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; import org.thingsboard.common.util.JacksonUtil; +import org.thingsboard.common.util.ThingsBoardThreadFactory; import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.audit.AuditLog; import org.thingsboard.server.common.data.id.TenantId; import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Collections; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; @Component @ConditionalOnProperty(prefix = "audit-log.sink", value = "type", havingValue = "elasticsearch") @@ -68,6 +72,7 @@ public class ElasticsearchAuditLogSink implements AuditLogSink { private String dateFormat; private RestClient restClient; + private ExecutorService executor; @PostConstruct public void init() { @@ -87,14 +92,32 @@ public class ElasticsearchAuditLogSink implements AuditLogSink { } this.restClient = builder.build(); + this.executor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("elasticsearch-audit-log")); } catch (Exception e) { log.error("Sink init failed!", e); throw new RuntimeException(e.getMessage(), e); } } + @PreDestroy + private void destroy() { + if (executor != null) { + executor.shutdownNow(); + } + } + @Override public void logAction(AuditLog auditLogEntry) { + executor.execute(() -> { + try { + doLogAction(auditLogEntry); + } catch (Exception e) { + log.error("Failed to log action", e); + } + }); + } + + private void doLogAction(AuditLog auditLogEntry) { String jsonContent = createElasticJsonRecord(auditLogEntry); HttpEntity entity = new NStringEntity( diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/JpaAbstractDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/JpaAbstractDao.java index 3d3dbd9bbc..35a161b94d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/JpaAbstractDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/JpaAbstractDao.java @@ -27,6 +27,8 @@ import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.model.BaseEntity; import org.thingsboard.server.dao.util.SqlDao; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; import java.util.Collection; import java.util.List; import java.util.Optional; @@ -45,9 +47,6 @@ public abstract class JpaAbstractDao, D> protected abstract JpaRepository getRepository(); - protected void setSearchText(E entity) { - } - @Override @Transactional public D save(TenantId tenantId, D domain) { @@ -58,17 +57,21 @@ public abstract class JpaAbstractDao, D> log.error("Can't create entity for domain object {}", domain, e); throw new IllegalArgumentException("Can't create entity for domain object {" + domain + "}", e); } - setSearchText(entity); log.debug("Saving entity {}", entity); - if (entity.getUuid() == null) { + boolean isNew = entity.getUuid() == null; + if (isNew) { UUID uuid = Uuids.timeBased(); entity.setUuid(uuid); entity.setCreatedTime(Uuids.unixTimestamp(uuid)); } - entity = getRepository().save(entity); + entity = doSave(entity, isNew); return DaoUtil.getData(entity); } + protected E doSave(E entity, boolean isNew) { + return getRepository().save(entity); + } + @Override @Transactional public D saveAndFlush(TenantId tenantId, D domain) { @@ -121,4 +124,5 @@ public abstract class JpaAbstractDao, D> List entities = Lists.newArrayList(getRepository().findAll()); return DaoUtil.convertDataList(entities); } + } diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/JpaPartitionedAbstractDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/JpaPartitionedAbstractDao.java new file mode 100644 index 0000000000..76db367ffb --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/JpaPartitionedAbstractDao.java @@ -0,0 +1,43 @@ +/** + * Copyright © 2016-2023 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.dao.sql; + +import org.thingsboard.server.dao.model.BaseEntity; +import org.thingsboard.server.dao.util.SqlDao; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +@SqlDao +public abstract class JpaPartitionedAbstractDao, D> extends JpaAbstractDao { + + @PersistenceContext + private EntityManager entityManager; + + @Override + protected E doSave(E entity, boolean isNew) { + createPartition(entity); + if (isNew) { + entityManager.persist(entity); + } else { + entity = entityManager.merge(entity); + } + return entity; + } + + public abstract void createPartition(E entity); + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/alarm/JpaAlarmCommentDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/alarm/JpaAlarmCommentDao.java index 1f83211b79..3d366a5a56 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/alarm/JpaAlarmCommentDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/alarm/JpaAlarmCommentDao.java @@ -24,7 +24,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.alarm.AlarmComment; import org.thingsboard.server.common.data.alarm.AlarmCommentInfo; -import org.thingsboard.server.common.data.id.AlarmCommentId; import org.thingsboard.server.common.data.id.AlarmId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.PageData; @@ -32,7 +31,7 @@ import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.alarm.AlarmCommentDao; import org.thingsboard.server.dao.model.sql.AlarmCommentEntity; -import org.thingsboard.server.dao.sql.JpaAbstractDao; +import org.thingsboard.server.dao.sql.JpaPartitionedAbstractDao; import org.thingsboard.server.dao.sqlts.insert.sql.SqlPartitioningRepository; import org.thingsboard.server.dao.util.SqlDao; @@ -45,7 +44,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.ALARM_COMMENT_TABL @Component @SqlDao @RequiredArgsConstructor -public class JpaAlarmCommentDao extends JpaAbstractDao implements AlarmCommentDao { +public class JpaAlarmCommentDao extends JpaPartitionedAbstractDao implements AlarmCommentDao { private final SqlPartitioningRepository partitioningRepository; @Value("${sql.alarm_comments.partition_size:168}") private int partitionSizeInHours; @@ -54,21 +53,7 @@ public class JpaAlarmCommentDao extends JpaAbstractDao findAlarmComments(TenantId tenantId, AlarmId id, PageLink pageLink){ + public PageData findAlarmComments(TenantId tenantId, AlarmId id, PageLink pageLink) { log.trace("Try to find alarm comments by alarm id using [{}]", id); return DaoUtil.toPageData( alarmCommentRepository.findAllByAlarmId(id.getId(), DaoUtil.toPageable(pageLink))); @@ -86,6 +71,11 @@ public class JpaAlarmCommentDao extends JpaAbstractDao getEntityClass() { return AlarmCommentEntity.class; diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/audit/JpaAuditLogDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/audit/JpaAuditLogDao.java index f78dc339dc..e859b1538e 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/audit/JpaAuditLogDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/audit/JpaAuditLogDao.java @@ -15,8 +15,6 @@ */ package org.thingsboard.server.dao.sql.audit; -import com.datastax.oss.driver.api.core.uuid.Uuids; -import com.google.common.util.concurrent.ListenableFuture; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -25,10 +23,8 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.audit.AuditLog; -import org.thingsboard.server.common.data.id.AuditLogId; import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.EntityId; -import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.TimePageLink; @@ -36,12 +32,11 @@ import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.audit.AuditLogDao; import org.thingsboard.server.dao.model.ModelConstants; import org.thingsboard.server.dao.model.sql.AuditLogEntity; -import org.thingsboard.server.dao.sql.JpaAbstractDao; +import org.thingsboard.server.dao.sql.JpaPartitionedAbstractDao; import org.thingsboard.server.dao.sqlts.insert.sql.SqlPartitioningRepository; import org.thingsboard.server.dao.util.SqlDao; import java.util.List; -import java.util.Objects; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -49,7 +44,7 @@ import java.util.concurrent.TimeUnit; @SqlDao @RequiredArgsConstructor @Slf4j -public class JpaAuditLogDao extends JpaAbstractDao implements AuditLogDao { +public class JpaAuditLogDao extends JpaPartitionedAbstractDao implements AuditLogDao { private final AuditLogRepository auditLogRepository; private final SqlPartitioningRepository partitioningRepository; @@ -72,25 +67,6 @@ public class JpaAuditLogDao extends JpaAbstractDao imp return auditLogRepository; } - @Override - public ListenableFuture saveByTenantId(AuditLog auditLog) { - return service.submit(() -> { - save(auditLog.getTenantId(), auditLog); - return null; - }); - } - - @Override - public AuditLog save(TenantId tenantId, AuditLog auditLog) { - if (auditLog.getId() == null) { - UUID uuid = Uuids.timeBased(); - auditLog.setId(new AuditLogId(uuid)); - auditLog.setCreatedTime(Uuids.unixTimestamp(uuid)); - } - partitioningRepository.createPartitionIfNotExists(TABLE_NAME, auditLog.getCreatedTime(), TimeUnit.HOURS.toMillis(partitionSizeInHours)); - return super.save(tenantId, auditLog); - } - @Override public PageData findAuditLogsByTenantIdAndEntityId(UUID tenantId, EntityId entityId, List actionTypes, TimePageLink pageLink) { return DaoUtil.toPageData( @@ -182,4 +158,9 @@ public class JpaAuditLogDao extends JpaAbstractDao imp jdbcTemplate.update("CALL migrate_audit_logs(?, ?, ?)", startTime, endTime, partitionSizeInMs); } + @Override + public void createPartition(AuditLogEntity entity) { + partitioningRepository.createPartitionIfNotExists(TABLE_NAME, entity.getCreatedTime(), TimeUnit.HOURS.toMillis(partitionSizeInHours)); + } + } diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/edge/JpaBaseEdgeEventDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/edge/JpaBaseEdgeEventDao.java index f5ecaaab84..3e8e8ebf54 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/edge/JpaBaseEdgeEventDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/edge/JpaBaseEdgeEventDao.java @@ -35,7 +35,7 @@ import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.edge.EdgeEventDao; import org.thingsboard.server.dao.model.ModelConstants; import org.thingsboard.server.dao.model.sql.EdgeEventEntity; -import org.thingsboard.server.dao.sql.JpaAbstractDao; +import org.thingsboard.server.dao.sql.JpaPartitionedAbstractDao; import org.thingsboard.server.dao.sql.ScheduledLogExecutorComponent; import org.thingsboard.server.dao.sql.TbSqlBlockingQueueParams; import org.thingsboard.server.dao.sql.TbSqlBlockingQueueWrapper; @@ -47,7 +47,6 @@ import javax.annotation.PreDestroy; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.Objects; import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.function.Function; @@ -58,7 +57,7 @@ import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; @SqlDao @RequiredArgsConstructor @Slf4j -public class JpaBaseEdgeEventDao extends JpaAbstractDao implements EdgeEventDao { +public class JpaBaseEdgeEventDao extends JpaPartitionedAbstractDao implements EdgeEventDao { private final UUID systemTenantId = NULL_UUID; @@ -151,8 +150,9 @@ public class JpaBaseEdgeEventDao extends JpaAbstractDao save(EdgeEventEntity entity) { @@ -227,4 +227,10 @@ public class JpaBaseEdgeEventDao extends JpaAbstractDao implements NotificationDao { +public class JpaNotificationDao extends JpaPartitionedAbstractDao implements NotificationDao { private final NotificationRepository notificationRepository; private final SqlPartitioningRepository partitioningRepository; @@ -52,18 +50,6 @@ public class JpaNotificationDao extends JpaAbstractDao findUnreadByRecipientIdAndPageLink(TenantId tenantId, UserId recipientId, PageLink pageLink) { return DaoUtil.toPageData(notificationRepository.findByRecipientIdAndStatusNot(recipientId.getId(), NotificationStatus.READ, @@ -114,6 +100,12 @@ public class JpaNotificationDao extends JpaAbstractDao getEntityClass() { return NotificationEntity.class; diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/ota/JpaOtaPackageInfoDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/ota/JpaOtaPackageInfoDao.java index 9236d0fbb0..10833b11df 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/ota/JpaOtaPackageInfoDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/ota/JpaOtaPackageInfoDao.java @@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; import org.thingsboard.server.common.data.OtaPackageInfo; import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.OtaPackageId; @@ -58,6 +59,7 @@ public class JpaOtaPackageInfoDao extends JpaAbstractDao
widgets.single-switch.behavior
-
widgets.rpc-state.initial-state
- +
widgets.value-action.initial-state
+
-
widgets.rpc-state.turn-on
- widgets.value-action.turn-on
+ + formControlName="onUpdateState">
-
widgets.rpc-state.turn-off
- widgets.value-action.turn-off
+ + formControlName="offUpdateState">
diff --git a/ui-ngx/src/app/modules/home/components/widget/config/basic/rpc/single-switch-basic-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/config/basic/rpc/single-switch-basic-config.component.ts index ed95847654..82c2766a23 100644 --- a/ui-ngx/src/app/modules/home/components/widget/config/basic/rpc/single-switch-basic-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/config/basic/rpc/single-switch-basic-config.component.ts @@ -37,7 +37,7 @@ import { ValueType } from '@shared/models/constants'; templateUrl: './single-switch-basic-config.component.html', styleUrls: ['../basic-config.scss'] }) -export class SingSwitchBasicConfigComponent extends BasicWidgetConfigComponent { +export class SingleSwitchBasicConfigComponent extends BasicWidgetConfigComponent { get targetDevice(): TargetDevice { return this.singleSwitchWidgetConfigForm.get('targetDevice').value; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/action/action-widget.models.ts b/ui-ngx/src/app/modules/home/components/widget/lib/action/action-widget.models.ts new file mode 100644 index 0000000000..8557c9cbd3 --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/widget/lib/action/action-widget.models.ts @@ -0,0 +1,659 @@ +/// +/// Copyright © 2016-2024 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. +/// + +import { + AttributeData, + AttributeScope, + LatestTelemetry, + TelemetrySubscriber, + TelemetryType, + telemetryTypeTranslationsShort +} from '@shared/models/telemetry/telemetry.models'; +import { WidgetContext } from '@home/models/widget-component.models'; +import { BehaviorSubject, forkJoin, Observable, Observer, of, throwError } from 'rxjs'; +import { catchError, delay, map, share, take } from 'rxjs/operators'; +import { UtilsService } from '@core/services/utils.service'; +import { AfterViewInit, ChangeDetectorRef, Directive, Input, OnDestroy, OnInit, TemplateRef } from '@angular/core'; +import { + DataToValueSettings, + DataToValueType, + GetAttributeValueSettings, + GetTelemetryValueSettings, + GetValueAction, + GetValueSettings, + RpcSettings, + SetAttributeValueSettings, + SetValueAction, + SetValueSettings, + TelemetryValueSettings, + ValueActionSettings, + ValueToDataSettings, + ValueToDataType +} from '@shared/models/action-widget-settings.models'; +import { ValueType } from '@shared/models/constants'; +import { EntityType, entityTypeTranslations } from '@shared/models/entity-type.models'; +import { EntityId } from '@shared/models/id/entity-id'; + +@Directive() +// eslint-disable-next-line @angular-eslint/directive-class-suffix +export abstract class BasicActionWidgetComponent implements OnInit, OnDestroy, AfterViewInit { + + @Input() + ctx: WidgetContext; + + @Input() + widgetTitlePanel: TemplateRef; + + private loadingSubject = new BehaviorSubject(false); + private valueGetters: ValueGetter[] = []; + private valueActions: ValueAction[] = []; + + loading$ = this.loadingSubject.asObservable().pipe(share()); + + error = ''; + + protected constructor(protected cd: ChangeDetectorRef) { + } + + ngOnInit(): void { + this.ctx.$scope.actionWidget = this; + } + + ngAfterViewInit(): void { + const getValueObservables: Array> = []; + this.valueGetters.forEach(valueGetter => { + getValueObservables.push(valueGetter.getValue()); + }); + this.loadingSubject.next(true); + forkJoin(getValueObservables).subscribe( + { + next: () => { + this.loadingSubject.next(false); + }, + error: () => { + this.loadingSubject.next(false); + } + } + ); + } + + ngOnDestroy() { + this.valueActions.forEach(v => v.destroy()); + } + + public onInit() { + } + + public clearError() { + this.error = ''; + this.cd.markForCheck(); + } + + protected createValueGetter(getValueSettings: GetValueSettings, + valueType: ValueType, + valueObserver?: Partial>): ValueGetter { + const observer: Partial> = { + next: (value: V) => { + if (valueObserver?.next) { + valueObserver.next(value); + } + }, + error: (err: any) => { + this.onError(err); + if (valueObserver?.error) { + valueObserver.error(err); + } + } + }; + const valueGetter = ValueGetter.fromSettings(this.ctx, getValueSettings, valueType, observer); + this.valueGetters.push(valueGetter); + this.valueActions.push(valueGetter); + return valueGetter; + } + + protected createValueSetter(setValueSettings: SetValueSettings): ValueSetter { + const valueSetter = ValueSetter.fromSettings(this.ctx, setValueSettings); + this.valueActions.push(valueSetter); + return valueSetter; + } + + private onError(error: string) { + this.error = error; + this.cd.markForCheck(); + } + + protected updateValue(valueSetter: ValueSetter, + value: V, + setValueObserver?: Partial>): void { + this.clearError(); + this.loadingSubject.next(true); + valueSetter.setValue(value).subscribe({ + next: () => { + if (setValueObserver?.next) { + setValueObserver.next(); + } + this.loadingSubject.next(false); + }, + error: (err) => { + this.loadingSubject.next(false); + if (setValueObserver?.error) { + setValueObserver.error(err); + } + const message = parseError(this.ctx, err); + this.onError(message); + } + }); + } +} + +type DataToValueFunction = (data: any) => V; + +export class DataToValueConverter { + + private readonly dataToValueFunction: DataToValueFunction; + private readonly compareToValue: any; + + constructor(private settings: DataToValueSettings, + private valueType: ValueType) { + this.compareToValue = settings.compareToValue; + switch (settings.type) { + case DataToValueType.FUNCTION: + try { + this.dataToValueFunction = new Function('data', settings.dataToValueFunction) as DataToValueFunction; + } catch (e) { + this.dataToValueFunction = (data) => data; + } + break; + case DataToValueType.NONE: + break; + } + } + + dataToValue(data: any): V { + let result: V; + switch (this.settings.type) { + case DataToValueType.FUNCTION: + result = data; + try { + result = this.dataToValueFunction(!!data ? JSON.parse(data) : data); + } catch (e) {} + break; + case DataToValueType.NONE: + result = data; + break; + } + if (this.valueType === ValueType.BOOLEAN) { + result = (result === this.compareToValue) as any; + } + return result; + } +} + +export abstract class ValueAction { + + protected constructor(protected ctx: WidgetContext, + protected settings: ValueActionSettings) {} + + protected handleError(err: any): Error { + const reason = parseError(this.ctx, err); + let errorMessage = this.ctx.translate.instant('widgets.value-action.error.failed-to-perform-action', + {actionLabel: this.settings.actionLabel}); + if (reason) { + errorMessage += '
' + reason; + } + return new Error(errorMessage); + } + + destroy(): void {} +} + +export abstract class ValueGetter extends ValueAction { + + static fromSettings(ctx: WidgetContext, + settings: GetValueSettings, + valueType: ValueType, + valueObserver: Partial>): ValueGetter { + switch (settings.action) { + case GetValueAction.DO_NOTHING: + return new DefaultValueGetter(ctx, settings, valueType, valueObserver); + case GetValueAction.EXECUTE_RPC: + return new ExecuteRpcValueGetter(ctx, settings, valueType, valueObserver); + case GetValueAction.GET_ATTRIBUTE: + return new AttributeValueGetter(ctx, settings, valueType, valueObserver); + case GetValueAction.GET_TIME_SERIES: + return new TimeSeriesValueGetter(ctx, settings, valueType, valueObserver); + } + } + + private readonly isSimulated: boolean; + private readonly dataConverter: DataToValueConverter; + + protected constructor(protected ctx: WidgetContext, + protected settings: GetValueSettings, + protected valueType: ValueType, + protected valueObserver: Partial>) { + super(ctx, settings); + this.isSimulated = this.ctx.$injector.get(UtilsService).widgetEditMode; + if (this.settings.action !== GetValueAction.DO_NOTHING) { + this.dataConverter = new DataToValueConverter(settings.dataToValue, valueType); + } + } + + getValue(): Observable { + const valueObservable: Observable = (this.isSimulated ? of(null).pipe(delay(500)) : this.doGetValue()).pipe( + map((data) => { + if (this.dataConverter) { + return this.dataConverter.dataToValue(data); + } else { + return data; + } + }), + catchError(err => { + throw this.handleError(err); + }) + ); + valueObservable.subscribe({ + next: (value) => { + this.valueObserver.next(value); + }, + error: (err) => { + this.valueObserver.error(err); + } + }); + return valueObservable.pipe( + take(1) + ); + } + + destroy() { + super.destroy(); + } + + protected abstract doGetValue(): Observable; +} + +type ValueToDataFunction = (value: V) => any; + +export class ValueToDataConverter { + + private readonly constantValue: any; + private readonly valueToDataFunction: ValueToDataFunction; + + constructor(protected settings: ValueToDataSettings) { + switch (settings.type) { + case ValueToDataType.CONSTANT: + this.constantValue = this.settings.constantValue; + break; + case ValueToDataType.FUNCTION: + try { + this.valueToDataFunction = new Function('value', settings.valueToDataFunction) as ValueToDataFunction; + } catch (e) { + this.valueToDataFunction = (data) => data; + } + break; + case ValueToDataType.NONE: + break; + } + } + + valueToData(value: V): any { + switch (this.settings.type) { + case ValueToDataType.CONSTANT: + return this.constantValue; + case ValueToDataType.FUNCTION: + let result = value; + try { + result = this.valueToDataFunction(value); + } catch (e) {} + return result; + case ValueToDataType.NONE: + return null; + } + } +} + +export abstract class ValueSetter extends ValueAction { + + static fromSettings(ctx: WidgetContext, + settings: SetValueSettings): ValueSetter { + switch (settings.action) { + case SetValueAction.EXECUTE_RPC: + return new ExecuteRpcValueSetter(ctx, settings); + case SetValueAction.SET_ATTRIBUTE: + return new AttributeValueSetter(ctx, settings); + case SetValueAction.ADD_TIME_SERIES: + return new TimeSeriesValueSetter(ctx, settings); + } + } + + private readonly isSimulated: boolean; + private readonly valueToDataConverter: ValueToDataConverter; + + protected constructor(protected ctx: WidgetContext, + protected settings: SetValueSettings) { + super(ctx, settings); + this.isSimulated = this.ctx.$injector.get(UtilsService).widgetEditMode; + this.valueToDataConverter = new ValueToDataConverter(settings.valueToData); + } + + setValue(value: V): Observable { + if (this.isSimulated) { + return of(null).pipe(delay(500)); + } else { + return this.doSetValue(this.valueToDataConverter.valueToData(value)).pipe( + catchError(err => { + throw this.handleError(err); + }) + ); + } + } + + protected abstract doSetValue(data: any): Observable; +} + +export class DefaultValueGetter extends ValueGetter { + + private readonly defaultValue: V; + + constructor(protected ctx: WidgetContext, + protected settings: GetValueSettings, + protected valueType: ValueType, + protected valueObserver: Partial>) { + super(ctx, settings, valueType, valueObserver); + this.defaultValue = settings.defaultValue; + } + + protected doGetValue(): Observable { + return of(this.defaultValue); + } +} + +export class ExecuteRpcValueGetter extends ValueGetter { + + private readonly executeRpcSettings: RpcSettings; + + constructor(protected ctx: WidgetContext, + protected settings: GetValueSettings, + protected valueType: ValueType, + protected valueObserver: Partial>) { + super(ctx, settings, valueType, valueObserver); + this.executeRpcSettings = settings.executeRpc; + } + + protected doGetValue(): Observable { + return this.ctx.controlApi.sendTwoWayCommand(this.executeRpcSettings.method, null, + this.executeRpcSettings.requestTimeout, + this.executeRpcSettings.requestPersistent, + this.executeRpcSettings.persistentPollingInterval).pipe( + catchError((err) => { + throw handleRpcError(this.ctx, err); + }) + ); + } +} + +export abstract class TelemetryValueGetter extends ValueGetter { + + protected targetEntityId: EntityId; + private telemetrySubscriber: TelemetrySubscriber; + + protected constructor(protected ctx: WidgetContext, + protected settings: GetValueSettings, + protected valueType: ValueType, + protected valueObserver: Partial>) { + super(ctx, settings, valueType, valueObserver); + const entityInfo = this.ctx.defaultSubscription.getFirstEntityInfo(); + this.targetEntityId = entityInfo?.entityId; + } + + protected doGetValue(): Observable { + if (!this.targetEntityId && !this.ctx.defaultSubscription.rpcEnabled) { + return throwError(() => new Error(this.ctx.translate.instant('widgets.value-action.error.target-entity-is-not-set'))); + } + if (this.targetEntityId) { + const err = validateAttributeScope(this.ctx, this.targetEntityId, this.scope()); + if (err) { + return throwError(() => err); + } + if (this.getTelemetryValueSettings().subscribeForUpdates) { + return this.subscribeForTelemetryValue(); + } else { + return this.doGetTelemetryValue(); + } + } else { + return of(null); + } + } + + private subscribeForTelemetryValue(): Observable { + this.telemetrySubscriber = + TelemetrySubscriber.createEntityAttributesSubscription(this.ctx.telemetryWsService, this.targetEntityId, + this.scope(), this.ctx.ngZone, [this.getTelemetryValueSettings().key]); + this.telemetrySubscriber.subscribe(); + return this.telemetrySubscriber.attributeData$().pipe( + map((data) => { + let value: V = null; + const entry = data.find(attr => attr.key === this.getTelemetryValueSettings().key); + if (entry) { + value = entry.value; + try { + value = JSON.parse(entry.value); + } catch (_e) {} + } + return value; + }) + ); + } + + protected scope(): TelemetryType { + return LatestTelemetry.LATEST_TELEMETRY; + } + + protected abstract getTelemetryValueSettings(): S; + + protected abstract doGetTelemetryValue(): Observable; + + destroy() { + if (this.telemetrySubscriber) { + this.telemetrySubscriber.unsubscribe(); + this.telemetrySubscriber = null; + } + super.destroy(); + } +} + +export class AttributeValueGetter extends TelemetryValueGetter { + + constructor(protected ctx: WidgetContext, + protected settings: GetValueSettings, + protected valueType: ValueType, + protected valueObserver: Partial>) { + super(ctx, settings, valueType, valueObserver); + } + + protected getTelemetryValueSettings(): GetAttributeValueSettings { + return this.settings.getAttribute; + } + + protected scope(): TelemetryType { + return this.getTelemetryValueSettings().scope; + } + + protected doGetTelemetryValue(): Observable { + const getAttributeValueSettings = this.getTelemetryValueSettings(); + return this.ctx.attributeService.getEntityAttributes(this.targetEntityId, + getAttributeValueSettings.scope, [getAttributeValueSettings.key], {ignoreLoading: true, ignoreErrors: true}).pipe( + map((data) => data.find(attr => attr.key === getAttributeValueSettings.key)?.value) + ); + } + +} + +export class TimeSeriesValueGetter extends TelemetryValueGetter { + + constructor(protected ctx: WidgetContext, + protected settings: GetValueSettings, + protected valueType: ValueType, + protected valueObserver: Partial>) { + super(ctx, settings, valueType, valueObserver); + } + + protected getTelemetryValueSettings(): GetTelemetryValueSettings { + return this.settings.getTimeSeries; + } + + protected doGetTelemetryValue(): Observable { + const getTelemetryValueSettings = this.getTelemetryValueSettings(); + return this.ctx.attributeService.getEntityTimeseriesLatest(this.targetEntityId, + [getTelemetryValueSettings.key], true, {ignoreLoading: true, ignoreErrors: true}) + .pipe( + map((data) => { + let value: any = null; + if (data[getTelemetryValueSettings.key]) { + const dataSet = data[getTelemetryValueSettings.key]; + if (dataSet.length) { + value = dataSet[0].value; + } + } + return value; + }) + ); + } + +} + +export class ExecuteRpcValueSetter extends ValueSetter { + + private readonly executeRpcSettings: RpcSettings; + + constructor(protected ctx: WidgetContext, + protected settings: SetValueSettings) { + super(ctx, settings); + this.executeRpcSettings = settings.executeRpc; + } + + protected doSetValue(data: any): Observable { + return this.ctx.controlApi.sendOneWayCommand(this.executeRpcSettings.method, data, + this.executeRpcSettings.requestTimeout, + this.executeRpcSettings.requestPersistent, + this.executeRpcSettings.persistentPollingInterval).pipe( + catchError((err) => { + throw handleRpcError(this.ctx, err); + }) + ); + } +} + +export abstract class TelemetryValueSetter extends ValueSetter { + + protected targetEntityId: EntityId; + + protected constructor(protected ctx: WidgetContext, + protected settings: SetValueSettings) { + super(ctx, settings); + const entityInfo = this.ctx.defaultSubscription.getFirstEntityInfo(); + this.targetEntityId = entityInfo?.entityId; + } + + protected doSetValue(data: any): Observable { + if (!this.targetEntityId && !this.ctx.defaultSubscription.rpcEnabled) { + return throwError(() => new Error(this.ctx.translate.instant('widgets.value-action.error.target-entity-is-not-set'))); + } + if (this.targetEntityId) { + const err = validateAttributeScope(this.ctx, this.targetEntityId, this.scope()); + if (err) { + return throwError(() => err); + } + return this.doSetTelemetryValue(data); + } else { + return of(null); + } + } + + protected scope(): TelemetryType { + return LatestTelemetry.LATEST_TELEMETRY; + } + + protected abstract doSetTelemetryValue(data: any): Observable; + +} + +export class AttributeValueSetter extends TelemetryValueSetter { + + private readonly setAttributeValueSettings: SetAttributeValueSettings; + + constructor(protected ctx: WidgetContext, + protected settings: SetValueSettings) { + super(ctx, settings); + this.setAttributeValueSettings = settings.setAttribute; + } + + protected doSetTelemetryValue(data: any): Observable { + const attributes: Array = [{key: this.setAttributeValueSettings.key, value: data}]; + return this.ctx.attributeService.saveEntityAttributes(this.targetEntityId, + this.setAttributeValueSettings.scope, attributes, {ignoreLoading: true, ignoreErrors: true}); + } + + protected scope(): TelemetryType { + return this.setAttributeValueSettings.scope; + } + +} + +export class TimeSeriesValueSetter extends TelemetryValueSetter { + + private readonly putTimeSeriesValueSettings: TelemetryValueSettings; + + constructor(protected ctx: WidgetContext, + protected settings: SetValueSettings) { + super(ctx, settings); + this.putTimeSeriesValueSettings = settings.putTimeSeries; + } + + protected doSetTelemetryValue(data: any): Observable { + const timeSeries: Array = [{key: this.putTimeSeriesValueSettings.key, value: data}]; + return this.ctx.attributeService.saveEntityTimeseries(this.targetEntityId, + LatestTelemetry.LATEST_TELEMETRY, timeSeries, {ignoreLoading: true, ignoreErrors: true}); + } + +} + +const parseError = (ctx: WidgetContext, err: any): string => + ctx.$injector.get(UtilsService).parseException(err).message || 'Unknown Error'; + +const handleRpcError = (ctx: WidgetContext, err: any): Error => { + let reason: string; + if (ctx.defaultSubscription.rpcErrorText) { + reason = ctx.defaultSubscription.rpcErrorText; + } else { + reason = parseError(ctx, err); + } + return new Error(reason); +}; + +const validateAttributeScope = (ctx: WidgetContext, targetEntityId: EntityId, scope?: TelemetryType): Error | null => { + if (targetEntityId.entityType !== EntityType.DEVICE && scope && + ![AttributeScope.SERVER_SCOPE, LatestTelemetry.LATEST_TELEMETRY].includes(scope)) { + const scopeStr = ctx.translate.instant(telemetryTypeTranslationsShort.get(scope)); + const entityType = + ctx.translate.instant(entityTypeTranslations.get(targetEntityId.entityType).type); + const errorMessage = + ctx.translate.instant('widgets.value-action.error.invalid-attribute-scope', {scope: scopeStr, entityType}); + return new Error(errorMessage); + } else { + return null; + } +}; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/rpc/rpc-widget.models.ts b/ui-ngx/src/app/modules/home/components/widget/lib/rpc/rpc-widget.models.ts deleted file mode 100644 index f47e654fdb..0000000000 --- a/ui-ngx/src/app/modules/home/components/widget/lib/rpc/rpc-widget.models.ts +++ /dev/null @@ -1,626 +0,0 @@ -/// -/// Copyright © 2016-2024 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. -/// - -import { - AttributeData, - AttributeScope, - LatestTelemetry, - telemetryTypeTranslationsShort -} from '@shared/models/telemetry/telemetry.models'; -import { WidgetContext } from '@home/models/widget-component.models'; -import { BehaviorSubject, Observable, of, throwError } from 'rxjs'; -import { catchError, delay, map, share } from 'rxjs/operators'; -import { UtilsService } from '@core/services/utils.service'; -import { AfterViewInit, ChangeDetectorRef, Directive, Input, OnInit, TemplateRef } from '@angular/core'; -import { backgroundStyle, ComponentStyle, overlayStyle } from '@shared/models/widget-settings.models'; -import { ImagePipe } from '@shared/pipe/image.pipe'; -import { DomSanitizer } from '@angular/platform-browser'; -import { - RpcActionSettings, - RpcGetAttributeSettings, - RpcSetAttributeSettings, - RpcSettings, - RpcStateToParamsSettings, - RpcStateToParamsType, - RpcStateWidgetSettings, - RpcTelemetrySettings, - RpcUpdateStateAction -} from '@shared/models/rpc-widget-settings.models'; -import { - RpcDataToStateSettings, - RpcDataToStateType, - RpcInitialStateAction, - RpcInitialStateSettings, - RpcStateBehaviourSettings, - RpcUpdateStateSettings -} from '@app/shared/models/rpc-widget-settings.models'; -import { ValueType } from '@shared/models/constants'; -import { EntityType, entityTypeTranslations } from '@shared/models/entity-type.models'; - -@Directive() -// eslint-disable-next-line @angular-eslint/directive-class-suffix -export abstract class BasicRpcStateWidgetComponent> implements OnInit, AfterViewInit { - - @Input() - ctx: WidgetContext; - - @Input() - widgetTitlePanel: TemplateRef; - - settings: S; - - behaviorApi: RpcStateBehaviorApi; - loading$: Observable; - - backgroundStyle$: Observable; - overlayStyle: ComponentStyle = {}; - - value: V; - - error = ''; - - protected constructor(protected imagePipe: ImagePipe, - protected sanitizer: DomSanitizer, - protected cd: ChangeDetectorRef) { - } - - ngOnInit(): void { - this.ctx.$scope.rpcWidget = this; - this.settings = {...this.defaultSettings(), ...this.ctx.settings}; - this.backgroundStyle$ = backgroundStyle(this.settings.background, this.imagePipe, this.sanitizer); - this.overlayStyle = overlayStyle(this.settings.background.overlay); - - const behaviourSettings: RpcStateBehaviourSettings = { - initialState: this.initialState(), - updateStateByValue: val => this.getUpdateStateSettingsForValue(val) - }; - - const callbacks: RpcStateCallbacks = { - onStateValue: val => this.setValue(val), - onError: err => this.onError(err), - validateStateValue: val => this.validateValue(val) - }; - - this.behaviorApi = new RpcStateBehaviorApi(this.defaultValue(), this.ctx, - behaviourSettings, callbacks, this.stateValueType()); - this.loading$ = this.behaviorApi.loading$.pipe(share()); - } - - ngAfterViewInit(): void { - this.behaviorApi.initState(); - } - - public onInit() { - const borderRadius = this.ctx.$widgetElement.css('borderRadius'); - this.overlayStyle = {...this.overlayStyle, ...{borderRadius}}; - this.cd.detectChanges(); - } - - public clearError() { - this.error = ''; - } - - public updateValue() { - this.behaviorApi.updateState(this.value); - } - - private onError(error: string) { - this.error = error; - this.ctx.detectChanges(); - } - - protected abstract defaultSettings(): S; - - protected abstract initialState(): RpcInitialStateSettings; - - protected abstract getUpdateStateSettingsForValue(value: V): RpcUpdateStateSettings; - - protected stateValueType(): ValueType { - return ValueType.BOOLEAN; - } - - protected defaultValue(): V { - return null; - } - - protected setValue(value: V) { - this.value = value; - } - - protected validateValue(value: any): V { - return value; - } - -} - -export abstract class RpcHasLoading { - - public get loading$() { - return this.loadingSubject.asObservable(); - } - - protected loadingSubject = new BehaviorSubject(false); -} - -export interface RpcStateCallbacks { - onStateValue: (value: V) => void; - validateStateValue: (value: any) => V; - onError: (error: string) => void; -} - -export class RpcStateBehaviorApi extends RpcHasLoading { - - private readonly initialStateGetter: RpcInitialStateGetter; - private readonly stateUpdatersMap: Map>; - - constructor(private state: V, - private ctx: WidgetContext, - private settings: RpcStateBehaviourSettings, - private callbacks: RpcStateCallbacks, - stateValueType: ValueType) { - super(); - this.initialStateGetter = RpcInitialStateGetter.fromSettings(ctx, settings.initialState, stateValueType, callbacks); - this.stateUpdatersMap = new Map>(); - } - - initState() { - if (this.ctx.defaultSubscription.targetEntityId || this.ctx.defaultSubscription.rpcEnabled) { - this.loadingSubject.next(true); - this.initialStateGetter.initState().subscribe( - { - next: (value) => { - this.state = value; - this.loadingSubject.next(false); - this.callbacks.onStateValue(value); - this.ctx.detectChanges(); - }, - error: (err: any) => { - this.loadingSubject.next(false); - const message = parseError(this.ctx, err); - this.callbacks.onError(message); - } - } - ); - } else { - this.callbacks.onError(this.ctx.translate.instant('widgets.rpc-state.error.target-entity-is-not-set')); - } - } - - updateState(value: V) { - this.callbacks.onError(null); - let updater: RpcStateUpdater; - const updateStateSettings = this.settings.updateStateByValue(value); - if (updateStateSettings) { - updater = this.stateUpdatersMap.get(updateStateSettings); - if (!updater) { - updater = RpcStateUpdater.fromSettings(this.ctx, updateStateSettings); - this.stateUpdatersMap.set(updateStateSettings, updater); - } - } - if (updater) { - this.loadingSubject.next(true); - updater.updateState(value).subscribe( - { - next: () => { - this.state = value; - this.loadingSubject.next(false); - this.ctx.detectChanges(); - }, - error: (err: any) => { - this.loadingSubject.next(false); - const message = parseError(this.ctx, err); - this.callbacks.onStateValue(this.state); - this.callbacks.onError(message); - this.ctx.detectChanges(); - } - }); - } - } - -} - -type RpcDataToStateFunction = (data: any) => V; - -export class RpcDataToStateConverter { - - private readonly dataToStateFunction: RpcDataToStateFunction; - private readonly compareToValue: any; - - constructor(private settings: RpcDataToStateSettings, - private stateValueType: ValueType, - private callbacks: RpcStateCallbacks) { - this.compareToValue = settings.compareToValue; - switch (settings.type) { - case RpcDataToStateType.FUNCTION: - try { - this.dataToStateFunction = new Function('data', settings.dataToStateFunction) as RpcDataToStateFunction; - } catch (e) { - this.dataToStateFunction = (data) => data; - } - break; - case RpcDataToStateType.NONE: - break; - } - } - - dataToStateValue(data: any): V { - let result: V; - switch (this.settings.type) { - case RpcDataToStateType.FUNCTION: - result = data; - try { - result = this.dataToStateFunction(!!data ? JSON.parse(data) : data); - } catch (e) {} - break; - case RpcDataToStateType.NONE: - result = data; - break; - } - if (this.stateValueType === ValueType.BOOLEAN) { - result = (result === this.compareToValue) as any; - } - result = this.callbacks.validateStateValue(result); - return result; - } -} - -export abstract class RpcAction { - - protected constructor(protected ctx: WidgetContext, - protected settings: RpcActionSettings) {} - - handleError(err: any): Error { - const reason = parseError(this.ctx, err); - let errorMessage = this.ctx.translate.instant('widgets.rpc-state.error.failed-to-perform-action', - {actionLabel: this.settings.actionLabel}); - if (reason) { - errorMessage += '
' + reason; - } - return new Error(errorMessage); - } -} - -export abstract class RpcInitialStateGetter extends RpcAction { - - static fromSettings(ctx: WidgetContext, - settings: RpcInitialStateSettings, - stateValueType: ValueType, - callbacks: RpcStateCallbacks): RpcInitialStateGetter { - switch (settings.action) { - case RpcInitialStateAction.DO_NOTHING: - return new RpcDefaultStateGetter(ctx, settings, stateValueType, callbacks); - case RpcInitialStateAction.EXECUTE_RPC: - return new ExecuteRpcStateGetter(ctx, settings, stateValueType, callbacks); - case RpcInitialStateAction.GET_ATTRIBUTE: - return new RpcAttributeStateGetter(ctx, settings, stateValueType, callbacks); - case RpcInitialStateAction.GET_TIME_SERIES: - return new RpcTimeSeriesStateGetter(ctx, settings, stateValueType, callbacks); - } - } - - private readonly isSimulated: boolean; - private readonly dataConverter: RpcDataToStateConverter; - - protected constructor(protected ctx: WidgetContext, - protected settings: RpcInitialStateSettings, - protected stateValueType: ValueType, - protected callbacks: RpcStateCallbacks) { - super(ctx, settings); - this.isSimulated = this.ctx.$injector.get(UtilsService).widgetEditMode; - if (this.settings.action !== RpcInitialStateAction.DO_NOTHING) { - this.dataConverter = new RpcDataToStateConverter(settings.dataToState, stateValueType, this.callbacks); - } - } - - initState(): Observable { - const stateObservable: Observable = this.isSimulated ? of(null).pipe(delay(500)) : this.doGetState(); - return stateObservable.pipe( - map((data) => { - if (this.dataConverter) { - return this.dataConverter.dataToStateValue(data); - } else { - return data; - } - }), - catchError(err => { - throw this.handleError(err); - }) - ); - } - - protected abstract doGetState(): Observable; -} - -type RpcStateToParamsFunction = (state: V) => any; - -export class RpcStateToParamsConverter { - - private readonly constantValue: any; - private readonly stateToParamsFunction: RpcStateToParamsFunction; - - constructor(protected settings: RpcStateToParamsSettings) { - switch (settings.type) { - case RpcStateToParamsType.CONSTANT: - this.constantValue = this.settings.constantValue; - break; - case RpcStateToParamsType.FUNCTION: - try { - this.stateToParamsFunction = new Function('value', settings.stateToParamsFunction) as RpcStateToParamsFunction; - } catch (e) { - this.stateToParamsFunction = (data) => data; - } - break; - case RpcStateToParamsType.NONE: - break; - } - } - - stateToParams(state: V): any { - switch (this.settings.type) { - case RpcStateToParamsType.CONSTANT: - return this.constantValue; - case RpcStateToParamsType.FUNCTION: - let result = state; - try { - result = this.stateToParamsFunction(state); - } catch (e) {} - return result; - case RpcStateToParamsType.NONE: - return null; - } - } -} - -export abstract class RpcStateUpdater extends RpcAction { - - static fromSettings(ctx: WidgetContext, - settings: RpcUpdateStateSettings): RpcStateUpdater { - switch (settings.action) { - case RpcUpdateStateAction.EXECUTE_RPC: - return new ExecuteRpcStateUpdater(ctx, settings); - case RpcUpdateStateAction.SET_ATTRIBUTE: - return new RpcAttributeStateUpdater(ctx, settings); - case RpcUpdateStateAction.ADD_TIME_SERIES: - return new RpcTimeSeriesStateUpdater(ctx, settings); - } - } - - private readonly isSimulated: boolean; - private readonly paramsConverter: RpcStateToParamsConverter; - - protected constructor(protected ctx: WidgetContext, - protected settings: RpcUpdateStateSettings) { - super(ctx, settings); - this.isSimulated = this.ctx.$injector.get(UtilsService).widgetEditMode; - this.paramsConverter = new RpcStateToParamsConverter(settings.stateToParams); - } - - updateState(state: V): Observable { - if (this.isSimulated) { - return of(null).pipe(delay(500)); - } else { - return this.doUpdateState(this.paramsConverter.stateToParams(state)).pipe( - catchError(err => { - throw this.handleError(err); - }) - ); - } - } - - protected abstract doUpdateState(params: any): Observable; -} - -export class RpcDefaultStateGetter extends RpcInitialStateGetter { - - private readonly defaultValue: V; - - constructor(protected ctx: WidgetContext, - protected settings: RpcInitialStateSettings, - protected stateValueType: ValueType, - protected callbacks: RpcStateCallbacks) { - super(ctx, settings, stateValueType, callbacks); - this.defaultValue = settings.defaultValue; - } - - protected doGetState(): Observable { - return of(this.defaultValue); - } -} - -export class ExecuteRpcStateGetter extends RpcInitialStateGetter { - - private readonly executeRpcSettings: RpcSettings; - - constructor(protected ctx: WidgetContext, - protected settings: RpcInitialStateSettings, - protected stateValueType: ValueType, - protected callbacks: RpcStateCallbacks) { - super(ctx, settings, stateValueType, callbacks); - this.executeRpcSettings = settings.executeRpc; - } - - protected doGetState(): Observable { - return this.ctx.controlApi.sendTwoWayCommand(this.executeRpcSettings.method, null, - this.executeRpcSettings.requestTimeout, - this.executeRpcSettings.requestPersistent, - this.executeRpcSettings.persistentPollingInterval).pipe( - catchError((err) => { - throw handleRpcError(this.ctx, err); - }) - ); - } -} - -export class RpcAttributeStateGetter extends RpcInitialStateGetter { - - private readonly getAttributeSettings: RpcGetAttributeSettings; - - constructor(protected ctx: WidgetContext, - protected settings: RpcInitialStateSettings, - protected stateValueType: ValueType, - protected callbacks: RpcStateCallbacks) { - super(ctx, settings, stateValueType, callbacks); - this.getAttributeSettings = settings.getAttribute; - } - - protected doGetState(): Observable { - if (this.ctx.defaultSubscription.targetEntityId) { - const err = validateAttributeScope(this.ctx, this.getAttributeSettings.scope); - if (err) { - return throwError(() => err); - } - return this.ctx.attributeService.getEntityAttributes(this.ctx.defaultSubscription.targetEntityId, - this.getAttributeSettings.scope, [this.getAttributeSettings.key], {ignoreLoading: true, ignoreErrors: true}) - .pipe( - map((data) => data.find(attr => attr.key === this.getAttributeSettings.key)?.value) - ); - } else { - return of(null); - } - } - -} - -export class RpcTimeSeriesStateGetter extends RpcInitialStateGetter { - - private readonly getTimeSeriesSettings: RpcTelemetrySettings; - - constructor(protected ctx: WidgetContext, - protected settings: RpcInitialStateSettings, - protected stateValueType: ValueType, - protected callbacks: RpcStateCallbacks) { - super(ctx, settings, stateValueType, callbacks); - this.getTimeSeriesSettings = settings.getTimeSeries; - } - - protected doGetState(): Observable { - if (this.ctx.defaultSubscription.targetEntityId) { - return this.ctx.attributeService.getEntityTimeseriesLatest(this.ctx.defaultSubscription.targetEntityId, - [this.getTimeSeriesSettings.key], true, {ignoreLoading: true, ignoreErrors: true}) - .pipe( - map((data) => { - let value: any = null; - if (data[this.getTimeSeriesSettings.key]) { - const dataSet = data[this.getTimeSeriesSettings.key]; - if (dataSet.length) { - value = dataSet[0].value; - } - } - return value; - }) - ); - } else { - return of(null); - } - } - -} - -export class ExecuteRpcStateUpdater extends RpcStateUpdater { - - private readonly executeRpcSettings: RpcSettings; - - constructor(protected ctx: WidgetContext, - protected settings: RpcUpdateStateSettings) { - super(ctx, settings); - this.executeRpcSettings = settings.executeRpc; - } - - protected doUpdateState(params: any): Observable { - return this.ctx.controlApi.sendOneWayCommand(this.executeRpcSettings.method, params, - this.executeRpcSettings.requestTimeout, - this.executeRpcSettings.requestPersistent, - this.executeRpcSettings.persistentPollingInterval).pipe( - catchError((err) => { - throw handleRpcError(this.ctx, err); - }) - ); - } -} - -export class RpcAttributeStateUpdater extends RpcStateUpdater { - - private readonly setAttributeSettings: RpcSetAttributeSettings; - - constructor(protected ctx: WidgetContext, - protected settings: RpcUpdateStateSettings) { - super(ctx, settings); - this.setAttributeSettings = settings.setAttribute; - } - - protected doUpdateState(params: any): Observable { - if (this.ctx.defaultSubscription.targetEntityId) { - const err = validateAttributeScope(this.ctx, this.setAttributeSettings.scope); - if (err) { - return throwError(() => err); - } - const attributes: Array = [{key: this.setAttributeSettings.key, value: params}]; - return this.ctx.attributeService.saveEntityAttributes(this.ctx.defaultSubscription.targetEntityId, - this.setAttributeSettings.scope, attributes, {ignoreLoading: true, ignoreErrors: true}); - } else { - return of(null); - } - } - -} - -export class RpcTimeSeriesStateUpdater extends RpcStateUpdater { - - private readonly putTimeSeriesSettings: RpcTelemetrySettings; - - constructor(protected ctx: WidgetContext, - protected settings: RpcUpdateStateSettings) { - super(ctx, settings); - this.putTimeSeriesSettings = settings.putTimeSeries; - } - - protected doUpdateState(params: any): Observable { - if (this.ctx.defaultSubscription.targetEntityId) { - const timeSeries: Array = [{key: this.putTimeSeriesSettings.key, value: params}]; - return this.ctx.attributeService.saveEntityTimeseries(this.ctx.defaultSubscription.targetEntityId, - LatestTelemetry.LATEST_TELEMETRY, timeSeries, {ignoreLoading: true, ignoreErrors: true}); - } else { - return of(null); - } - } - -} - -const parseError = (ctx: WidgetContext, err: any): string => - ctx.$injector.get(UtilsService).parseException(err).message || 'Unknown Error'; - -const handleRpcError = (ctx: WidgetContext, err: any): Error => { - let reason: string; - if (ctx.defaultSubscription.rpcErrorText) { - reason = ctx.defaultSubscription.rpcErrorText; - } else { - reason = parseError(ctx, err); - } - return new Error(reason); -}; - -const validateAttributeScope = (ctx: WidgetContext, scope?: AttributeScope): Error | null => { - if (ctx.defaultSubscription.targetEntityId.entityType !== EntityType.DEVICE && scope && scope !== AttributeScope.SERVER_SCOPE) { - const scopeStr = ctx.translate.instant(telemetryTypeTranslationsShort.get(scope)); - const entityType = - ctx.translate.instant(entityTypeTranslations.get(ctx.defaultSubscription.targetEntityId.entityType).type); - const errorMessage = - ctx.translate.instant('widgets.rpc-state.error.invalid-attribute-scope', {scope: scopeStr, entityType}); - return new Error(errorMessage); - } else { - return null; - } -}; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.component.html index ee27f83458..04ec9f22ab 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.component.html @@ -27,7 +27,7 @@
{{ offLabel }}
- +
{{ onLabel }}
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.component.ts index 7794e51ebc..61d4fb9322 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.component.ts @@ -25,20 +25,25 @@ import { ViewChild, ViewEncapsulation } from '@angular/core'; -import { BasicRpcStateWidgetComponent } from '@home/components/widget/lib/rpc/rpc-widget.models'; +import { BasicActionWidgetComponent, ValueSetter } from '@home/components/widget/lib/action/action-widget.models'; import { singleSwitchDefaultSettings, SingleSwitchLayout, SingleSwitchWidgetSettings } from '@home/components/widget/lib/rpc/single-switch-widget.models'; -import { ComponentStyle, iconStyle, textStyle } from '@shared/models/widget-settings.models'; +import { + backgroundStyle, + ComponentStyle, + iconStyle, + overlayStyle, + textStyle +} from '@shared/models/widget-settings.models'; import { Observable } from 'rxjs'; import { ResizeObserver } from '@juggle/resize-observer'; import { ImagePipe } from '@shared/pipe/image.pipe'; import { DomSanitizer } from '@angular/platform-browser'; import cssjs from '@core/css/css'; import { hashCode } from '@core/utils'; -import { RpcInitialStateSettings, RpcUpdateStateSettings } from '@shared/models/rpc-widget-settings.models'; import { ValueType } from '@shared/models/constants'; const horizontalLayoutPadding = 48; @@ -51,7 +56,7 @@ const verticalLayoutPadding = 36; encapsulation: ViewEncapsulation.None }) export class SingleSwitchWidgetComponent extends - BasicRpcStateWidgetComponent implements OnInit, AfterViewInit, OnDestroy { + BasicActionWidgetComponent implements OnInit, AfterViewInit, OnDestroy { @ViewChild('singleSwitchPanel', {static: false}) singleSwitchPanel: ElementRef; @@ -65,6 +70,13 @@ export class SingleSwitchWidgetComponent extends @ViewChild('singleSwitchToggleRow', {static: false}) singleSwitchToggleRow: ElementRef; + settings: SingleSwitchWidgetSettings; + + backgroundStyle$: Observable; + overlayStyle: ComponentStyle = {}; + + value = false; + layout: SingleSwitchLayout; showIcon = false; @@ -87,16 +99,24 @@ export class SingleSwitchWidgetComponent extends private panelResize$: ResizeObserver; + private onValueSetter: ValueSetter; + private offValueSetter: ValueSetter; + constructor(protected imagePipe: ImagePipe, protected sanitizer: DomSanitizer, private renderer: Renderer2, protected cd: ChangeDetectorRef, private elementRef: ElementRef) { - super(imagePipe, sanitizer, cd); + super(cd); } ngOnInit(): void { super.ngOnInit(); + this.settings = {...singleSwitchDefaultSettings, ...this.ctx.settings}; + + this.backgroundStyle$ = backgroundStyle(this.settings.background, this.imagePipe, this.sanitizer); + this.overlayStyle = overlayStyle(this.settings.background.overlay); + this.layout = this.settings.layout; this.autoScale = this.settings.autoScale; @@ -134,6 +154,20 @@ export class SingleSwitchWidgetComponent extends cssParser.cssPreviewNamespace = namespace; cssParser.createStyleElement(namespace, switchVariablesCss); this.renderer.addClass(this.elementRef.nativeElement, namespace); + + const getInitialStateSettings = + {...this.settings.initialState, actionLabel: this.ctx.translate.instant('widgets.value-action.initial-state')}; + this.createValueGetter(getInitialStateSettings, ValueType.BOOLEAN, { + next: (value) => this.onValue(value) + }); + + const onUpdateStateSettings = {...this.settings.onUpdateState, + actionLabel: this.ctx.translate.instant('widgets.value-action.turn-on')}; + this.onValueSetter = this.createValueSetter(onUpdateStateSettings); + + const offUpdateStateSettings = {...this.settings.offUpdateState, + actionLabel: this.ctx.translate.instant('widgets.value-action.turn-off')}; + this.offValueSetter = this.createValueSetter(offUpdateStateSettings); } ngAfterViewInit(): void { @@ -156,31 +190,30 @@ export class SingleSwitchWidgetComponent extends if (this.panelResize$) { this.panelResize$.disconnect(); } + super.ngOnDestroy(); } - protected stateValueType(): ValueType { - return ValueType.BOOLEAN; - } - - protected defaultValue(): boolean { - return false; - } - - protected defaultSettings(): SingleSwitchWidgetSettings { - return {...singleSwitchDefaultSettings}; - } - - protected initialState(): RpcInitialStateSettings { - return {...this.settings.initialState, actionLabel: this.ctx.translate.instant('widgets.rpc-state.initial-state')}; + public onInit() { + super.onInit(); + const borderRadius = this.ctx.$widgetElement.css('borderRadius'); + this.overlayStyle = {...this.overlayStyle, ...{borderRadius}}; + this.cd.detectChanges(); } - protected getUpdateStateSettingsForValue(value: boolean): RpcUpdateStateSettings { - const targetSettings = value ? this.settings.onUpdateState : this.settings.offUpdateState; - return {...targetSettings, actionLabel: this.ctx.translate.instant(value ? 'widgets.rpc-state.turn-on' : 'widgets.rpc-state.turn-off')}; + public onToggleChange(event: MouseEvent) { + event.preventDefault(); + const targetValue = this.value; + const targetSetter = targetValue ? this.onValueSetter : this.offValueSetter; + this.updateValue(targetSetter, targetValue, { + next: () => this.onValue(targetValue), + error: () => this.onValue(!targetValue) + }); } - protected validateValue(value: any): boolean { - return !!value; + private onValue(value: boolean): void { + console.log(`onValue: ${value}`); + this.value = !!value; + this.cd.markForCheck(); } private onResize() { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.models.ts b/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.models.ts index 57c2af3b5e..23510e174a 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.models.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/rpc/single-switch-widget.models.ts @@ -14,16 +14,16 @@ /// limitations under the License. /// -import { BackgroundType, cssUnit, Font } from '@shared/models/widget-settings.models'; +import { BackgroundSettings, BackgroundType, cssUnit, Font } from '@shared/models/widget-settings.models'; import { AttributeScope } from '@shared/models/telemetry/telemetry.models'; import { - RpcDataToStateType, - RpcInitialStateAction, - RpcStateToParamsType, - RpcStateWidgetSettings, - RpcUpdateStateAction, - RpcUpdateStateSettings -} from '@shared/models/rpc-widget-settings.models'; + DataToValueType, + GetValueAction, + GetValueSettings, + SetValueAction, + SetValueSettings, + ValueToDataType +} from '@shared/models/action-widget-settings.models'; export enum SingleSwitchLayout { right = 'right', @@ -49,9 +49,10 @@ export const singleSwitchLayoutImages = new Map( ] ); -export interface SingleSwitchWidgetSettings extends RpcStateWidgetSettings { - onUpdateState: RpcUpdateStateSettings; - offUpdateState: RpcUpdateStateSettings; +export interface SingleSwitchWidgetSettings { + initialState: GetValueSettings; + onUpdateState: SetValueSettings; + offUpdateState: SetValueSettings; layout: SingleSwitchLayout; autoScale: boolean; showLabel: boolean; @@ -77,11 +78,12 @@ export interface SingleSwitchWidgetSettings extends RpcStateWidgetSettings -
-
widgets.rpc-state.initial-state
-
+
+
{{ panelTitle | translate }}
+
-
{{ 'widgets.rpc-state.action' | translate }}
+
{{ 'widgets.value-action.action' | translate }}
- - {{ rpcInitialStateTranslationsMap.get(action) | translate }} + + {{ getValueActionTranslationsMap.get(action) | translate }}
- - + +
-
widgets.rpc-state.value
- widgets.value-action.value
+
- +
-
{{ 'widgets.rpc-state.method' | translate }}*
+
{{ 'widgets.value-action.method' | translate }}*
warning
- +
-
{{ 'widgets.rpc-state.attribute-scope' | translate }}
+
{{ 'widgets.value-action.attribute-scope' | translate }}
@@ -71,58 +71,68 @@
-
{{ 'widgets.rpc-state.attribute-key' | translate }}*
+
{{ 'widgets.value-action.attribute-key' | translate }}*
+ [attributeScope]="getValueSettingsFormGroup.get('getAttribute').get('scope').value">
+
+ + {{ 'widgets.value-action.subscribe-for-updates' | translate }} + +
- +
-
{{ 'widgets.rpc-state.time-series-key' | translate }}*
+
{{ 'widgets.value-action.time-series-key' | translate }}*
+
+ + {{ 'widgets.value-action.subscribe-for-updates' | translate }} + +
-
+
-
widgets.rpc-state.action-result-converter
+
widgets.value-action.action-result-converter
- {{ 'widgets.rpc-state.converter-none' | translate }} - {{ 'widgets.rpc-state.converter-function' | translate }} + {{ 'widgets.value-action.converter-none' | translate }} + {{ 'widgets.value-action.converter-function' | translate }}
- -
-
widgets.rpc-state.on-when-result-is
+
+
widgets.value-action.on-when-result-is
-
@@ -142,16 +152,16 @@
-
{{ 'widgets.rpc-state.request-timeout-ms' | translate }}
+
{{ 'widgets.value-action.request-timeout-ms' | translate }}
warning @@ -159,28 +169,28 @@
+ [expanded]="getValueSettingsFormGroup.get('executeRpc').get('requestPersistent').value" + [disabled]="!getValueSettingsFormGroup.get('executeRpc').get('requestPersistent').value"> - {{ 'widgets.rpc-state.request-persistent' | translate }} + {{ 'widgets.value-action.request-persistent' | translate }}
-
{{ 'widgets.rpc-state.persistent-polling-interval' | translate }}
+
{{ 'widgets.value-action.persistent-polling-interval' | translate }}
warning @@ -193,7 +203,7 @@
-
+
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/get-value-action-settings-panel.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/get-value-action-settings-panel.component.ts new file mode 100644 index 0000000000..8f40d59bd1 --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/get-value-action-settings-panel.component.ts @@ -0,0 +1,182 @@ +/// +/// Copyright © 2016-2024 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. +/// + +import { Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation } from '@angular/core'; +import { PageComponent } from '@shared/components/page.component'; +import { TbPopoverComponent } from '@shared/components/popover.component'; +import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; +import { merge } from 'rxjs'; +import { + DataToValueType, + GetValueAction, + getValueActions, + getValueActionTranslations, + GetValueSettings +} from '@shared/models/action-widget-settings.models'; +import { ValueType } from '@shared/models/constants'; +import { TargetDevice } from '@shared/models/widget.models'; +import { AttributeScope, DataKeyType, telemetryTypeTranslationsShort } from '@shared/models/telemetry/telemetry.models'; +import { IAliasController } from '@core/api/widget-api.models'; +import { WidgetService } from '@core/http/widget.service'; + +@Component({ + selector: 'tb-get-value-action-settings-panel', + templateUrl: './get-value-action-settings-panel.component.html', + providers: [], + styleUrls: ['./value-action-settings-panel.component.scss'], + encapsulation: ViewEncapsulation.None +}) +export class GetValueActionSettingsPanelComponent extends PageComponent implements OnInit { + + @Input() + getValueSettings: GetValueSettings; + + @Input() + panelTitle: string; + + @Input() + valueType: ValueType; + + @Input() + aliasController: IAliasController; + + @Input() + targetDevice: TargetDevice; + + @Input() + popover: TbPopoverComponent; + + @Output() + getValueSettingsApplied = new EventEmitter>(); + + getValueAction = GetValueAction; + + getValueActions = getValueActions; + + getValueActionTranslationsMap = getValueActionTranslations; + + telemetryTypeTranslationsMap = telemetryTypeTranslationsShort; + + attributeScopes = Object.keys(AttributeScope) as AttributeScope[]; + + dataKeyType = DataKeyType; + + dataToValueType = DataToValueType; + + functionScopeVariables = this.widgetService.getWidgetScopeVariables(); + + ValueType = ValueType; + + getValueSettingsFormGroup: UntypedFormGroup; + + constructor(private fb: UntypedFormBuilder, + private widgetService: WidgetService, + protected store: Store) { + super(store); + } + + ngOnInit(): void { + this.getValueSettingsFormGroup = this.fb.group( + { + action: [this.getValueSettings?.action, []], + defaultValue: [this.getValueSettings?.defaultValue, [Validators.required]], + executeRpc: this.fb.group({ + method: [this.getValueSettings?.executeRpc?.method, [Validators.required]], + requestTimeout: [this.getValueSettings?.executeRpc?.requestTimeout, [Validators.required, Validators.min(5000)]], + requestPersistent: [this.getValueSettings?.executeRpc?.requestPersistent, []], + persistentPollingInterval: + [this.getValueSettings?.executeRpc?.persistentPollingInterval, [Validators.required, Validators.min(1000)]] + }), + getAttribute: this.fb.group({ + scope: [this.getValueSettings?.getAttribute?.scope, []], + key: [this.getValueSettings?.getAttribute?.key, [Validators.required]], + subscribeForUpdates: [this.getValueSettings?.getAttribute?.subscribeForUpdates, []] + }), + getTimeSeries: this.fb.group({ + key: [this.getValueSettings?.getTimeSeries?.key, [Validators.required]], + subscribeForUpdates: [this.getValueSettings?.getTimeSeries?.subscribeForUpdates, []] + }), + dataToValue: this.fb.group({ + type: [this.getValueSettings?.dataToValue?.type, [Validators.required]], + dataToValueFunction: [this.getValueSettings?.dataToValue?.dataToValueFunction, [Validators.required]], + }), + } + ); + if (this.valueType === ValueType.BOOLEAN) { + (this.getValueSettingsFormGroup.get('dataToValue') as UntypedFormGroup).addControl( + 'compareToValue', this.fb.control(this.getValueSettings?.dataToValue?.compareToValue, [Validators.required]) + ); + } + + merge(this.getValueSettingsFormGroup.get('action').valueChanges, + this.getValueSettingsFormGroup.get('dataToValue').get('type').valueChanges, + this.getValueSettingsFormGroup.get('executeRpc').get('requestPersistent').valueChanges).subscribe(() => { + this.updateValidators(); + }); + this.updateValidators(); + } + + cancel() { + this.popover?.hide(); + } + + applyGetValueSettings() { + const getValueSettings: GetValueSettings = this.getValueSettingsFormGroup.getRawValue(); + this.getValueSettingsApplied.emit(getValueSettings); + } + + private updateValidators() { + const action: GetValueAction = this.getValueSettingsFormGroup.get('action').value; + const dataToValueType: DataToValueType = this.getValueSettingsFormGroup.get('dataToValue').get('type').value; + + this.getValueSettingsFormGroup.get('defaultValue').disable({emitEvent: false}); + this.getValueSettingsFormGroup.get('executeRpc').disable({emitEvent: false}); + this.getValueSettingsFormGroup.get('getAttribute').disable({emitEvent: false}); + this.getValueSettingsFormGroup.get('getTimeSeries').disable({emitEvent: false}); + switch (action) { + case GetValueAction.DO_NOTHING: + this.getValueSettingsFormGroup.get('defaultValue').enable({emitEvent: false}); + break; + case GetValueAction.EXECUTE_RPC: + this.getValueSettingsFormGroup.get('executeRpc').enable({emitEvent: false}); + const requestPersistent: boolean = this.getValueSettingsFormGroup.get('executeRpc').get('requestPersistent').value; + if (requestPersistent) { + this.getValueSettingsFormGroup.get('executeRpc').get('persistentPollingInterval').enable({emitEvent: false}); + } else { + this.getValueSettingsFormGroup.get('executeRpc').get('persistentPollingInterval').disable({emitEvent: false}); + } + break; + case GetValueAction.GET_ATTRIBUTE: + this.getValueSettingsFormGroup.get('getAttribute').enable({emitEvent: false}); + break; + case GetValueAction.GET_TIME_SERIES: + this.getValueSettingsFormGroup.get('getTimeSeries').enable({emitEvent: false}); + break; + } + if (action === GetValueAction.DO_NOTHING) { + this.getValueSettingsFormGroup.get('dataToValue').disable({emitEvent: false}); + } else { + this.getValueSettingsFormGroup.get('dataToValue').enable({emitEvent: false}); + if (dataToValueType === DataToValueType.FUNCTION) { + this.getValueSettingsFormGroup.get('dataToValue').get('dataToValueFunction').enable({emitEvent: false}); + } else { + this.getValueSettingsFormGroup.get('dataToValue').get('dataToValueFunction').disable({emitEvent: false}); + } + } + } +} diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-initial-state-settings.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/get-value-action-settings.component.ts similarity index 61% rename from ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-initial-state-settings.component.ts rename to ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/get-value-action-settings.component.ts index 61c053d43c..3c3b00531a 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-initial-state-settings.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/get-value-action-settings.component.ts @@ -28,35 +28,38 @@ import { import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { MatButton } from '@angular/material/button'; import { TbPopoverService } from '@shared/components/popover.service'; -import { RpcInitialStateAction, RpcInitialStateSettings } from '@shared/models/rpc-widget-settings.models'; +import { GetValueAction, GetValueSettings } from '@shared/models/action-widget-settings.models'; import { TranslateService } from '@ngx-translate/core'; import { ValueType } from '@shared/models/constants'; import { - RpcInitialStateSettingsPanelComponent -} from '@home/components/widget/lib/settings/common/rpc/rpc-initial-state-settings-panel.component'; + GetValueActionSettingsPanelComponent +} from '@home/components/widget/lib/settings/common/action/get-value-action-settings-panel.component'; import { IAliasController } from '@core/api/widget-api.models'; import { TargetDevice } from '@shared/models/widget.models'; @Component({ - selector: 'tb-rpc-initial-state-settings', - templateUrl: './rpc-state-settings-button.component.html', - styleUrls: ['./rpc-state-settings-button.scss'], + selector: 'tb-get-value-action-settings', + templateUrl: './value-action-settings-button.component.html', + styleUrls: ['./value-action-settings-button.scss'], providers: [ { provide: NG_VALUE_ACCESSOR, - useExisting: forwardRef(() => RpcInitialStateSettingsComponent), + useExisting: forwardRef(() => GetValueActionSettingsComponent), multi: true } ], encapsulation: ViewEncapsulation.None }) -export class RpcInitialStateSettingsComponent implements OnInit, ControlValueAccessor { +export class GetValueActionSettingsComponent implements OnInit, ControlValueAccessor { @HostBinding('style.overflow') overflow = 'hidden'; @Input() - stateValueType: ValueType; + panelTitle: string; + + @Input() + valueType: ValueType; @Input() aliasController: IAliasController; @@ -67,7 +70,7 @@ export class RpcInitialStateSettingsComponent implements OnInit, ControlValueAcc @Input() disabled = false; - modelValue: RpcInitialStateSettings; + modelValue: GetValueSettings; displayValue: string; @@ -95,12 +98,12 @@ export class RpcInitialStateSettingsComponent implements OnInit, ControlValueAcc } } - writeValue(value: RpcInitialStateSettings): void { + writeValue(value: GetValueSettings): void { this.modelValue = value; this.updateDisplayValue(); } - openRpcStateSettingsPopup($event: Event, matButton: MatButton) { + openValueActionSettingsPopup($event: Event, matButton: MatButton) { if ($event) { $event.stopPropagation(); } @@ -109,21 +112,22 @@ export class RpcInitialStateSettingsComponent implements OnInit, ControlValueAcc this.popoverService.hidePopover(trigger); } else { const ctx: any = { - initialState: this.modelValue, - stateValueType: this.stateValueType, + getValueSettings: this.modelValue, + panelTitle: this.panelTitle, + valueType: this.valueType, aliasController: this.aliasController, targetDevice: this.targetDevice }; - const initialStateSettingsPanelPopover = this.popoverService.displayPopover(trigger, this.renderer, - this.viewContainerRef, RpcInitialStateSettingsPanelComponent, + const getValueSettingsPanelPopover = this.popoverService.displayPopover(trigger, this.renderer, + this.viewContainerRef, GetValueActionSettingsPanelComponent, ['leftTopOnly', 'leftOnly', 'leftBottomOnly'], true, null, ctx, {}, {}, {}, true); - initialStateSettingsPanelPopover.tbComponentRef.instance.popover = initialStateSettingsPanelPopover; - initialStateSettingsPanelPopover.tbComponentRef.instance.initialStateSettingsApplied.subscribe((initialState) => { - initialStateSettingsPanelPopover.hide(); - this.modelValue = initialState; + getValueSettingsPanelPopover.tbComponentRef.instance.popover = getValueSettingsPanelPopover; + getValueSettingsPanelPopover.tbComponentRef.instance.getValueSettingsApplied.subscribe((getValueSettings) => { + getValueSettingsPanelPopover.hide(); + this.modelValue = getValueSettings; this.updateDisplayValue(); this.propagateChange(this.modelValue); }); @@ -132,22 +136,22 @@ export class RpcInitialStateSettingsComponent implements OnInit, ControlValueAcc private updateDisplayValue() { switch (this.modelValue.action) { - case RpcInitialStateAction.DO_NOTHING: - if (this.stateValueType === ValueType.BOOLEAN) { - this.displayValue = this.translate.instant(!!this.modelValue.defaultValue ? 'widgets.rpc-state.on' : 'widgets.rpc-state.off'); + case GetValueAction.DO_NOTHING: + if (this.valueType === ValueType.BOOLEAN) { + this.displayValue = this.translate.instant(!!this.modelValue.defaultValue ? 'widgets.value-action.on' : 'widgets.value-action.off'); } else { this.displayValue = this.modelValue.defaultValue + ''; } break; - case RpcInitialStateAction.EXECUTE_RPC: + case GetValueAction.EXECUTE_RPC: const methodName = this.modelValue.executeRpc.method; - this.displayValue = this.translate.instant('widgets.rpc-state.execute-rpc-text', {methodName}); + this.displayValue = this.translate.instant('widgets.value-action.execute-rpc-text', {methodName}); break; - case RpcInitialStateAction.GET_ATTRIBUTE: - this.displayValue = this.translate.instant('widgets.rpc-state.get-attribute-text', {key: this.modelValue.getAttribute.key}); + case GetValueAction.GET_ATTRIBUTE: + this.displayValue = this.translate.instant('widgets.value-action.get-attribute-text', {key: this.modelValue.getAttribute.key}); break; - case RpcInitialStateAction.GET_TIME_SERIES: - this.displayValue = this.translate.instant('widgets.rpc-state.get-time-series-text', {key: this.modelValue.getTimeSeries.key}); + case GetValueAction.GET_TIME_SERIES: + this.displayValue = this.translate.instant('widgets.value-action.get-time-series-text', {key: this.modelValue.getTimeSeries.key}); break; } this.cd.markForCheck(); diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-update-state-settings-panel.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/set-value-action-settings-panel.component.html similarity index 56% rename from ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-update-state-settings-panel.component.html rename to ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/set-value-action-settings-panel.component.html index ad70579d1c..faf27484f9 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-update-state-settings-panel.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/set-value-action-settings-panel.component.html @@ -15,41 +15,41 @@ limitations under the License. --> -
-
{{ panelTitle | translate }}
-
+
+
{{ panelTitle | translate }}
+
-
{{ 'widgets.rpc-state.action' | translate }}
+
{{ 'widgets.value-action.action' | translate }}
- - {{ rpcUpdateStateTranslationsMap.get(action) | translate }} + + {{ setValueActionTranslationsMap.get(action) | translate }}
- - + +
-
{{ 'widgets.rpc-state.method' | translate }}*
+
{{ 'widgets.value-action.method' | translate }}*
warning
- +
-
{{ 'widgets.rpc-state.attribute-scope' | translate }}
+
{{ 'widgets.value-action.attribute-scope' | translate }}
@@ -59,30 +59,30 @@
-
{{ 'widgets.rpc-state.attribute-key' | translate }}*
+
{{ 'widgets.value-action.attribute-key' | translate }}*
+ [attributeScope]="setValueSettingsFormGroup.get('setAttribute').get('scope').value">
- +
-
{{ 'widgets.rpc-state.time-series-key' | translate }}*
+
{{ 'widgets.value-action.time-series-key' | translate }}*
-
+
-
{{ (updateStateSettingsFormGroup.get('action').value === rpcUpdateStateAction.EXECUTE_RPC ? - 'widgets.rpc-state.parameters' : 'widgets.rpc-state.value') | translate }}
+
{{ (setValueSettingsFormGroup.get('action').value === setValueAction.EXECUTE_RPC ? + 'widgets.value-action.parameters' : 'widgets.value-action.value') | translate }}
- {{ 'widgets.rpc-state.converter-constant' | translate }} - {{ 'widgets.rpc-state.converter-function' | translate }} - {{ 'widgets.rpc-state.converter-none' | translate }} + {{ 'widgets.value-action.converter-constant' | translate }} + {{ 'widgets.value-action.converter-function' | translate }} + {{ 'widgets.value-action.converter-none' | translate }}
- -
-
@@ -132,16 +132,16 @@
-
{{ 'widgets.rpc-state.request-timeout-ms' | translate }}
+
{{ 'widgets.value-action.request-timeout-ms' | translate }}
warning @@ -149,28 +149,28 @@
+ [expanded]="setValueSettingsFormGroup.get('executeRpc').get('requestPersistent').value" + [disabled]="!setValueSettingsFormGroup.get('executeRpc').get('requestPersistent').value"> - {{ 'widgets.rpc-state.request-persistent' | translate }} + {{ 'widgets.value-action.request-persistent' | translate }}
-
{{ 'widgets.rpc-state.persistent-polling-interval' | translate }}
+
{{ 'widgets.value-action.persistent-polling-interval' | translate }}
warning @@ -183,7 +183,7 @@
-
+
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/set-value-action-settings-panel.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/set-value-action-settings-panel.component.ts new file mode 100644 index 0000000000..229d55c531 --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/set-value-action-settings-panel.component.ts @@ -0,0 +1,182 @@ +/// +/// Copyright © 2016-2024 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. +/// + +import { Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation } from '@angular/core'; +import { PageComponent } from '@shared/components/page.component'; +import { TbPopoverComponent } from '@shared/components/popover.component'; +import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; +import { merge } from 'rxjs'; +import { + getValueActions, + SetValueAction, + setValueActions, + setValueActionTranslations, + SetValueSettings, + ValueToDataType +} from '@shared/models/action-widget-settings.models'; +import { ValueType } from '@shared/models/constants'; +import { TargetDevice } from '@shared/models/widget.models'; +import { AttributeScope, DataKeyType, telemetryTypeTranslationsShort } from '@shared/models/telemetry/telemetry.models'; +import { IAliasController } from '@core/api/widget-api.models'; +import { WidgetService } from '@core/http/widget.service'; + +@Component({ + selector: 'tb-set-value-action-settings-panel', + templateUrl: './set-value-action-settings-panel.component.html', + providers: [], + styleUrls: ['./value-action-settings-panel.component.scss'], + encapsulation: ViewEncapsulation.None +}) +export class SetValueActionSettingsPanelComponent extends PageComponent implements OnInit { + + @Input() + panelTitle: string; + + @Input() + setValueSettings: SetValueSettings; + + @Input() + valueType: ValueType; + + @Input() + aliasController: IAliasController; + + @Input() + targetDevice: TargetDevice; + + @Input() + popover: TbPopoverComponent; + + @Output() + setValueSettingsApplied = new EventEmitter(); + + setValueAction = SetValueAction; + + setValueActions = setValueActions; + + setValueActionTranslationsMap = setValueActionTranslations; + + telemetryTypeTranslationsMap = telemetryTypeTranslationsShort; + + attributeScopes = [AttributeScope.SERVER_SCOPE, AttributeScope.SHARED_SCOPE]; + + dataKeyType = DataKeyType; + + valueToDataType = ValueToDataType; + + functionScopeVariables = this.widgetService.getWidgetScopeVariables(); + + ValueType = ValueType; + + setValueSettingsFormGroup: UntypedFormGroup; + + constructor(private fb: UntypedFormBuilder, + private widgetService: WidgetService, + protected store: Store) { + super(store); + } + + ngOnInit(): void { + this.setValueSettingsFormGroup = this.fb.group( + { + action: [this.setValueSettings?.action, []], + executeRpc: this.fb.group({ + method: [this.setValueSettings?.executeRpc?.method, [Validators.required]], + requestTimeout: [this.setValueSettings?.executeRpc?.requestTimeout, [Validators.required, Validators.min(5000)]], + requestPersistent: [this.setValueSettings?.executeRpc?.requestPersistent, []], + persistentPollingInterval: + [this.setValueSettings?.executeRpc?.persistentPollingInterval, [Validators.required, Validators.min(1000)]] + }), + setAttribute: this.fb.group({ + scope: [this.setValueSettings?.setAttribute?.scope, []], + key: [this.setValueSettings?.setAttribute?.key, [Validators.required]], + }), + putTimeSeries: this.fb.group({ + key: [this.setValueSettings?.putTimeSeries?.key, [Validators.required]], + }), + valueToData: this.fb.group({ + type: [this.setValueSettings?.valueToData?.type, [Validators.required]], + constantValue: [this.setValueSettings?.valueToData?.constantValue, [Validators.required]], + valueToDataFunction: [this.setValueSettings?.valueToData?.valueToDataFunction, [Validators.required]], + }), + } + ); + + merge(this.setValueSettingsFormGroup.get('action').valueChanges, + this.setValueSettingsFormGroup.get('valueToData').get('type').valueChanges, + this.setValueSettingsFormGroup.get('executeRpc').get('requestPersistent').valueChanges).subscribe(() => { + this.updateValidators(); + }); + this.updateValidators(); + } + + cancel() { + this.popover?.hide(); + } + + applySetValueSettings() { + const setValueSettings: SetValueSettings = this.setValueSettingsFormGroup.getRawValue(); + this.setValueSettingsApplied.emit(setValueSettings); + } + + private updateValidators() { + const action: SetValueAction = this.setValueSettingsFormGroup.get('action').value; + let valueToDataType: ValueToDataType = this.setValueSettingsFormGroup.get('valueToData').get('type').value; + + this.setValueSettingsFormGroup.get('executeRpc').disable({emitEvent: false}); + this.setValueSettingsFormGroup.get('setAttribute').disable({emitEvent: false}); + this.setValueSettingsFormGroup.get('putTimeSeries').disable({emitEvent: false}); + switch (action) { + case SetValueAction.EXECUTE_RPC: + this.setValueSettingsFormGroup.get('executeRpc').enable({emitEvent: false}); + const requestPersistent: boolean = this.setValueSettingsFormGroup.get('executeRpc').get('requestPersistent').value; + if (requestPersistent) { + this.setValueSettingsFormGroup.get('executeRpc').get('persistentPollingInterval').enable({emitEvent: false}); + } else { + this.setValueSettingsFormGroup.get('executeRpc').get('persistentPollingInterval').disable({emitEvent: false}); + } + break; + case SetValueAction.SET_ATTRIBUTE: + case SetValueAction.ADD_TIME_SERIES: + if (valueToDataType === ValueToDataType.NONE) { + valueToDataType = ValueToDataType.CONSTANT; + this.setValueSettingsFormGroup.get('valueToData').get('type').patchValue(valueToDataType, {emitEvent: false}); + } + if (action === SetValueAction.SET_ATTRIBUTE) { + this.setValueSettingsFormGroup.get('setAttribute').enable({emitEvent: false}); + } else { + this.setValueSettingsFormGroup.get('putTimeSeries').enable({emitEvent: false}); + } + break; + } + switch (valueToDataType) { + case ValueToDataType.CONSTANT: + this.setValueSettingsFormGroup.get('valueToData').get('constantValue').enable({emitEvent: false}); + this.setValueSettingsFormGroup.get('valueToData').get('valueToDataFunction').disable({emitEvent: false}); + break; + case ValueToDataType.FUNCTION: + this.setValueSettingsFormGroup.get('valueToData').get('constantValue').disable({emitEvent: false}); + this.setValueSettingsFormGroup.get('valueToData').get('valueToDataFunction').enable({emitEvent: false}); + break; + case ValueToDataType.NONE: + this.setValueSettingsFormGroup.get('valueToData').get('constantValue').disable({emitEvent: false}); + this.setValueSettingsFormGroup.get('valueToData').get('valueToDataFunction').disable({emitEvent: false}); + break; + } + } +} diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-update-state-settings.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/set-value-action-settings.component.ts similarity index 62% rename from ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-update-state-settings.component.ts rename to ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/set-value-action-settings.component.ts index 8a65861783..d0110226f5 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-update-state-settings.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/set-value-action-settings.component.ts @@ -28,34 +28,30 @@ import { import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { MatButton } from '@angular/material/button'; import { TbPopoverService } from '@shared/components/popover.service'; -import { - RpcStateToParamsType, - RpcUpdateStateAction, - RpcUpdateStateSettings -} from '@shared/models/rpc-widget-settings.models'; +import { SetValueAction, SetValueSettings, ValueToDataType } from '@shared/models/action-widget-settings.models'; import { TranslateService } from '@ngx-translate/core'; import { ValueType } from '@shared/models/constants'; import { IAliasController } from '@core/api/widget-api.models'; import { TargetDevice } from '@shared/models/widget.models'; import { isDefinedAndNotNull } from '@core/utils'; import { - RpcUpdateStateSettingsPanelComponent -} from '@home/components/widget/lib/settings/common/rpc/rpc-update-state-settings-panel.component'; + SetValueActionSettingsPanelComponent +} from '@home/components/widget/lib/settings/common/action/set-value-action-settings-panel.component'; @Component({ - selector: 'tb-rpc-update-state-settings', - templateUrl: './rpc-state-settings-button.component.html', - styleUrls: ['./rpc-state-settings-button.scss'], + selector: 'tb-set-value-action-settings', + templateUrl: './value-action-settings-button.component.html', + styleUrls: ['./value-action-settings-button.scss'], providers: [ { provide: NG_VALUE_ACCESSOR, - useExisting: forwardRef(() => RpcUpdateStateSettingsComponent), + useExisting: forwardRef(() => SetValueActionSettingsComponent), multi: true } ], encapsulation: ViewEncapsulation.None }) -export class RpcUpdateStateSettingsComponent implements OnInit, ControlValueAccessor { +export class SetValueActionSettingsComponent implements OnInit, ControlValueAccessor { @HostBinding('style.overflow') overflow = 'hidden'; @@ -64,7 +60,7 @@ export class RpcUpdateStateSettingsComponent implements OnInit, ControlValueAcce panelTitle: string; @Input() - stateValueType: ValueType; + valueType: ValueType; @Input() aliasController: IAliasController; @@ -75,7 +71,7 @@ export class RpcUpdateStateSettingsComponent implements OnInit, ControlValueAcce @Input() disabled = false; - modelValue: RpcUpdateStateSettings; + modelValue: SetValueSettings; displayValue: string; @@ -103,12 +99,12 @@ export class RpcUpdateStateSettingsComponent implements OnInit, ControlValueAcce } } - writeValue(value: RpcUpdateStateSettings): void { + writeValue(value: SetValueSettings): void { this.modelValue = value; this.updateDisplayValue(); } - openRpcStateSettingsPopup($event: Event, matButton: MatButton) { + openValueActionSettingsPopup($event: Event, matButton: MatButton) { if ($event) { $event.stopPropagation(); } @@ -117,22 +113,22 @@ export class RpcUpdateStateSettingsComponent implements OnInit, ControlValueAcce this.popoverService.hidePopover(trigger); } else { const ctx: any = { - updateState: this.modelValue, + setValueSettings: this.modelValue, panelTitle: this.panelTitle, - stateValueType: this.stateValueType, + valueType: this.valueType, aliasController: this.aliasController, targetDevice: this.targetDevice }; - const updateStateSettingsPanelPopover = this.popoverService.displayPopover(trigger, this.renderer, - this.viewContainerRef, RpcUpdateStateSettingsPanelComponent, + const setValueSettingsPanelPopover = this.popoverService.displayPopover(trigger, this.renderer, + this.viewContainerRef, SetValueActionSettingsPanelComponent, ['leftTopOnly', 'leftOnly', 'leftBottomOnly'], true, null, ctx, {}, {}, {}, true); - updateStateSettingsPanelPopover.tbComponentRef.instance.popover = updateStateSettingsPanelPopover; - updateStateSettingsPanelPopover.tbComponentRef.instance.updateStateSettingsApplied.subscribe((updateState) => { - updateStateSettingsPanelPopover.hide(); - this.modelValue = updateState; + setValueSettingsPanelPopover.tbComponentRef.instance.popover = setValueSettingsPanelPopover; + setValueSettingsPanelPopover.tbComponentRef.instance.setValueSettingsApplied.subscribe((setValueSettings) => { + setValueSettingsPanelPopover.hide(); + this.modelValue = setValueSettings; this.updateDisplayValue(); this.propagateChange(this.modelValue); }); @@ -141,31 +137,31 @@ export class RpcUpdateStateSettingsComponent implements OnInit, ControlValueAcce private updateDisplayValue() { let value: any; - switch (this.modelValue.stateToParams.type) { - case RpcStateToParamsType.CONSTANT: - value = this.modelValue.stateToParams.constantValue; + switch (this.modelValue.valueToData.type) { + case ValueToDataType.CONSTANT: + value = this.modelValue.valueToData.constantValue; break; - case RpcStateToParamsType.FUNCTION: + case ValueToDataType.FUNCTION: value = 'f(value)'; break; - case RpcStateToParamsType.NONE: + case ValueToDataType.NONE: break; } switch (this.modelValue.action) { - case RpcUpdateStateAction.EXECUTE_RPC: + case SetValueAction.EXECUTE_RPC: let methodName = this.modelValue.executeRpc.method; if (isDefinedAndNotNull(value)) { methodName = `${methodName}(${value})`; } - this.displayValue = this.translate.instant('widgets.rpc-state.execute-rpc-text', {methodName}); + this.displayValue = this.translate.instant('widgets.value-action.execute-rpc-text', {methodName}); break; - case RpcUpdateStateAction.SET_ATTRIBUTE: - this.displayValue = this.translate.instant('widgets.rpc-state.set-attribute-to-value-text', + case SetValueAction.SET_ATTRIBUTE: + this.displayValue = this.translate.instant('widgets.value-action.set-attribute-to-value-text', {key: this.modelValue.setAttribute.key, value}); break; - case RpcUpdateStateAction.ADD_TIME_SERIES: - this.displayValue = this.translate.instant('widgets.rpc-state.add-time-series-value-text', - {key: this.modelValue.setAttribute.key, value}); + case SetValueAction.ADD_TIME_SERIES: + this.displayValue = this.translate.instant('widgets.value-action.add-time-series-value-text', + {key: this.modelValue.putTimeSeries.key, value}); break; } this.cd.markForCheck(); diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-state-settings-button.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/value-action-settings-button.component.html similarity index 88% rename from ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-state-settings-button.component.html rename to ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/value-action-settings-button.component.html index da24002e31..1e54dc3270 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/rpc/rpc-state-settings-button.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/settings/common/action/value-action-settings-button.component.html @@ -16,9 +16,9 @@ -->
diff --git a/ui-ngx/src/app/shared/components/image/image-dialog.component.ts b/ui-ngx/src/app/shared/components/image/image-dialog.component.ts index 434d76d74d..1a777d1bf7 100644 --- a/ui-ngx/src/app/shared/components/image/image-dialog.component.ts +++ b/ui-ngx/src/app/shared/components/image/image-dialog.component.ts @@ -154,7 +154,7 @@ export class ImageDialogComponent extends this.imageChanged = true; this.image = result; this.imagePreviewData = { - url: this.image.public ? this.image.publicLink : this.image.link + url: this.image.public ? `${this.image.publicLink}?ts=${new Date().getTime()}` : this.image.link }; } }); diff --git a/ui-ngx/src/app/shared/models/action-widget-settings.models.ts b/ui-ngx/src/app/shared/models/action-widget-settings.models.ts new file mode 100644 index 0000000000..d1442c09c5 --- /dev/null +++ b/ui-ngx/src/app/shared/models/action-widget-settings.models.ts @@ -0,0 +1,128 @@ +/// +/// Copyright © 2016-2024 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. +/// + +import { AttributeScope } from '@shared/models/telemetry/telemetry.models'; + +export enum GetValueAction { + DO_NOTHING = 'DO_NOTHING', + EXECUTE_RPC = 'EXECUTE_RPC', + GET_ATTRIBUTE = 'GET_ATTRIBUTE', + GET_TIME_SERIES = 'GET_TIME_SERIES' +} + +export const getValueActions = Object.keys(GetValueAction) as GetValueAction[]; + +export const getValueActionTranslations = new Map( + [ + [GetValueAction.DO_NOTHING, 'widgets.value-action.do-nothing'], + [GetValueAction.EXECUTE_RPC, 'widgets.value-action.execute-rpc'], + [GetValueAction.GET_ATTRIBUTE, 'widgets.value-action.get-attribute'], + [GetValueAction.GET_TIME_SERIES, 'widgets.value-action.get-time-series'] + ] +); + +export interface RpcSettings { + method: string; + requestTimeout: number; + requestPersistent: boolean; + persistentPollingInterval: number; +} + +export interface TelemetryValueSettings { + key: string; +} + +export interface GetTelemetryValueSettings extends TelemetryValueSettings { + subscribeForUpdates: boolean; +} + +export interface GetAttributeValueSettings extends GetTelemetryValueSettings { + scope: AttributeScope | null; +} + +export interface SetAttributeValueSettings extends TelemetryValueSettings { + scope: AttributeScope.SERVER_SCOPE | AttributeScope.SHARED_SCOPE; +} + +export enum DataToValueType { + NONE = 'NONE', + FUNCTION = 'FUNCTION' +} + +export interface DataToValueSettings { + type: DataToValueType; + dataToValueFunction: string; + compareToValue?: any; +} + +export interface ValueActionSettings { + actionLabel?: string; +} + +export interface GetValueSettings extends ValueActionSettings { + action: GetValueAction; + defaultValue: V; + executeRpc: RpcSettings; + getAttribute: GetAttributeValueSettings; + getTimeSeries: GetTelemetryValueSettings; + dataToValue: DataToValueSettings; +} + +export enum SetValueAction { + EXECUTE_RPC = 'EXECUTE_RPC', + SET_ATTRIBUTE = 'SET_ATTRIBUTE', + ADD_TIME_SERIES = 'ADD_TIME_SERIES' +} + +export const setValueActions = Object.keys(SetValueAction) as SetValueAction[]; + +export const setValueActionTranslations = new Map( + [ + [SetValueAction.EXECUTE_RPC, 'widgets.value-action.execute-rpc'], + [SetValueAction.SET_ATTRIBUTE, 'widgets.value-action.set-attribute'], + [SetValueAction.ADD_TIME_SERIES, 'widgets.value-action.add-time-series'] + ] +); + +export enum ValueToDataType { + CONSTANT = 'CONSTANT', + FUNCTION = 'FUNCTION', + NONE = 'NONE' +} + +export interface ValueToDataSettings { + type: ValueToDataType; + constantValue: any; + valueToDataFunction: string; +} + +export interface SetValueSettings extends ValueActionSettings { + action: SetValueAction; + executeRpc: RpcSettings; + setAttribute: SetAttributeValueSettings; + putTimeSeries: TelemetryValueSettings; + valueToData: ValueToDataSettings; +} + +/*export interface RpcStateBehaviourSettings { + initialState: RpcInitialStateSettings; + updateStateByValue: (value: V) => RpcUpdateStateSettings; +} + +export interface RpcStateWidgetSettings { + initialState: RpcInitialStateSettings; + background: BackgroundSettings; +}*/ diff --git a/ui-ngx/src/app/shared/models/rpc-widget-settings.models.ts b/ui-ngx/src/app/shared/models/rpc-widget-settings.models.ts deleted file mode 100644 index b912361805..0000000000 --- a/ui-ngx/src/app/shared/models/rpc-widget-settings.models.ts +++ /dev/null @@ -1,125 +0,0 @@ -/// -/// Copyright © 2016-2024 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. -/// - -import { AttributeScope } from '@shared/models/telemetry/telemetry.models'; -import { BackgroundSettings } from '@shared/models/widget-settings.models'; - -export enum RpcInitialStateAction { - DO_NOTHING = 'DO_NOTHING', - EXECUTE_RPC = 'EXECUTE_RPC', - GET_ATTRIBUTE = 'GET_ATTRIBUTE', - GET_TIME_SERIES = 'GET_TIME_SERIES' -} - -export const rpcInitialStateActions = Object.keys(RpcInitialStateAction) as RpcInitialStateAction[]; - -export const rpcInitialStateTranslations = new Map( - [ - [RpcInitialStateAction.DO_NOTHING, 'widgets.rpc-state.do-nothing'], - [RpcInitialStateAction.EXECUTE_RPC, 'widgets.rpc-state.execute-rpc'], - [RpcInitialStateAction.GET_ATTRIBUTE, 'widgets.rpc-state.get-attribute'], - [RpcInitialStateAction.GET_TIME_SERIES, 'widgets.rpc-state.get-time-series'] - ] -); - -export interface RpcSettings { - method: string; - requestTimeout: number; - requestPersistent: boolean; - persistentPollingInterval: number; -} - -export interface RpcTelemetrySettings { - key: string; -} - -export interface RpcGetAttributeSettings extends RpcTelemetrySettings { - scope: AttributeScope | null; -} - -export interface RpcSetAttributeSettings extends RpcTelemetrySettings { - scope: AttributeScope.SERVER_SCOPE | AttributeScope.SHARED_SCOPE; -} - -export enum RpcDataToStateType { - NONE = 'NONE', - FUNCTION = 'FUNCTION' -} - -export interface RpcDataToStateSettings { - type: RpcDataToStateType; - dataToStateFunction: string; - compareToValue?: any; -} - -export interface RpcActionSettings { - actionLabel?: string; -} - -export interface RpcInitialStateSettings extends RpcActionSettings { - action: RpcInitialStateAction; - defaultValue: V; - executeRpc: RpcSettings; - getAttribute: RpcGetAttributeSettings; - getTimeSeries: RpcTelemetrySettings; - dataToState: RpcDataToStateSettings; -} - -export enum RpcUpdateStateAction { - EXECUTE_RPC = 'EXECUTE_RPC', - SET_ATTRIBUTE = 'SET_ATTRIBUTE', - ADD_TIME_SERIES = 'ADD_TIME_SERIES' -} - -export const rpcUpdateStateActions = Object.keys(RpcUpdateStateAction) as RpcUpdateStateAction[]; - -export const rpcUpdateStateTranslations = new Map( - [ - [RpcUpdateStateAction.EXECUTE_RPC, 'widgets.rpc-state.execute-rpc'], - [RpcUpdateStateAction.SET_ATTRIBUTE, 'widgets.rpc-state.set-attribute'], - [RpcUpdateStateAction.ADD_TIME_SERIES, 'widgets.rpc-state.add-time-series'] - ] -); - -export enum RpcStateToParamsType { - CONSTANT = 'CONSTANT', - FUNCTION = 'FUNCTION', - NONE = 'NONE' -} - -export interface RpcStateToParamsSettings { - type: RpcStateToParamsType; - constantValue: any; - stateToParamsFunction: string; -} - -export interface RpcUpdateStateSettings extends RpcActionSettings { - action: RpcUpdateStateAction; - executeRpc: RpcSettings; - setAttribute: RpcSetAttributeSettings; - putTimeSeries: RpcTelemetrySettings; - stateToParams: RpcStateToParamsSettings; -} - -export interface RpcStateBehaviourSettings { - initialState: RpcInitialStateSettings; - updateStateByValue: (value: V) => RpcUpdateStateSettings; -} - -export interface RpcStateWidgetSettings { - initialState: RpcInitialStateSettings; - background: BackgroundSettings; -} diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index d4f57953e4..1e55d903bc 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -6102,60 +6102,6 @@ "min-value": "Minimum value", "max-value": "Maximum value" }, - "rpc-state": { - "initial-state": "Initial state", - "initial-state-hint": "Action to get the initial value of the component.", - "turn-on": "Turn 'On'", - "turn-on-hint": "Action performed to turn ON the component.", - "turn-off": "Turn 'Off'", - "turn-off-hint": "Action performed to turn OFF the component.", - "on": "On", - "off": "Off", - "do-nothing": "Do nothing", - "execute-rpc": "Execute RPC", - "get-attribute": "Get attribute", - "set-attribute": "Set attribute", - "get-time-series": "Get time-series", - "add-time-series": "Add time-series", - "execute-rpc-text": "Execute RPC method '{{methodName}}'", - "get-attribute-text": "Use attribute '{{key}}'", - "get-time-series-text": "Use time-series '{{key}}'", - "set-attribute-to-value-text": "Set '{{key}}' attribute to: {{value}}", - "add-time-series-value-text": "Add '{{key}}' time-series value: {{value}}", - "set-attribute-text": "Set '{{key}}' attribute", - "add-time-series-text": "Add '{{key}}' time-series", - "action": "Action", - "value": "Value", - "init-value-hint": "Value that will be set until device sends data.", - "method": "Method", - "method-name-required": "Method name is required.", - "request-timeout-ms": "RPC request timeout (ms)", - "request-timeout-required": "Request timeout is required.", - "min-request-timeout-error": "Request timeout value should be greater or equal 5000 ms (5 seconds).", - "request-persistent": "RPC request persistent", - "persistent-polling-interval": "Persistent polling interval (ms)", - "persistent-polling-interval-hint": "Polling interval (ms) to get persistent RPC command response", - "persistent-polling-interval-required": "Persistent polling interval is required.", - "min-persistent-polling-interval-error": "Persistent polling interval value should be greater or equal 1000 ms (1 second).", - "attribute-scope": "Attribute scope", - "attribute-key": "Attribute key", - "attribute-key-required": "Attribute key is required.", - "time-series-key": "Time-series key", - "time-series-key-required": "Time-series key is required.", - "action-result-converter": "Action result converter", - "converter-none": "None", - "converter-function": "Function", - "converter-constant": "Constant", - "parse-value-function": "Parse value function", - "on-when-result-is": "'On' when result is", - "parameters": "Parameters", - "convert-value-function": "Convert value function", - "error": { - "target-entity-is-not-set": "Target entity is not set!", - "failed-to-perform-action": "Failed to perform the {{ actionLabel }} action.", - "invalid-attribute-scope": "{{scope}} attribute scope is not supported by {{entityType}} entity." - } - }, "maps": { "select-entity": "Select entity", "select-entity-hint": "Hint: after selection click at the map to set position", @@ -6628,6 +6574,62 @@ "source-entity-alias": "Source entity alias", "source-entity-attribute": "Source entity attribute" }, + "value-action": { + "initial-state": "Initial state", + "initial-state-hint": "Action to get the initial value of the component.", + "turn-on": "Turn 'On'", + "turn-on-hint": "Action performed to turn ON the component.", + "turn-off": "Turn 'Off'", + "turn-off-hint": "Action performed to turn OFF the component.", + "on": "On", + "off": "Off", + "do-nothing": "Do nothing", + "execute-rpc": "Execute RPC", + "get-attribute": "Get attribute", + "set-attribute": "Set attribute", + "get-time-series": "Get time-series", + "add-time-series": "Add time-series", + "execute-rpc-text": "Execute RPC method '{{methodName}}'", + "get-attribute-text": "Use attribute '{{key}}'", + "get-time-series-text": "Use time-series '{{key}}'", + "set-attribute-to-value-text": "Set '{{key}}' attribute to: {{value}}", + "add-time-series-value-text": "Add '{{key}}' time-series value: {{value}}", + "set-attribute-text": "Set '{{key}}' attribute", + "add-time-series-text": "Add '{{key}}' time-series", + "action": "Action", + "value": "Value", + "init-value-hint": "Value that will be set until device sends data.", + "method": "Method", + "method-name-required": "Method name is required.", + "request-timeout-ms": "RPC request timeout (ms)", + "request-timeout-required": "Request timeout is required.", + "min-request-timeout-error": "Request timeout value should be greater or equal 5000 ms (5 seconds).", + "request-persistent": "RPC request persistent", + "persistent-polling-interval": "Persistent polling interval (ms)", + "persistent-polling-interval-hint": "Polling interval (ms) to get persistent RPC command response", + "persistent-polling-interval-required": "Persistent polling interval is required.", + "min-persistent-polling-interval-error": "Persistent polling interval value should be greater or equal 1000 ms (1 second).", + "attribute-scope": "Attribute scope", + "attribute-key": "Attribute key", + "attribute-key-required": "Attribute key is required.", + "time-series-key": "Time-series key", + "time-series-key-required": "Time-series key is required.", + "subscribe-for-updates": "Subscribe for updates", + "subscribe-for-updates-hint": "Subscribe for updates", + "action-result-converter": "Action result converter", + "converter-none": "None", + "converter-function": "Function", + "converter-constant": "Constant", + "parse-value-function": "Parse value function", + "on-when-result-is": "'On' when result is", + "parameters": "Parameters", + "convert-value-function": "Convert value function", + "error": { + "target-entity-is-not-set": "Target entity is not set!", + "failed-to-perform-action": "Failed to perform the {{ actionLabel }} action.", + "invalid-attribute-scope": "{{scope}} attribute scope is not supported by {{entityType}} entity." + } + }, "widget-font": { "font-settings": "Font settings", "font-family": "Font family",