Commit 95b58f7f245fddaa422297a3642cb45931887eb4

Authored by xp.Huang
2 parents 4d720663 9b6b8776

Merge branch '20230223' into 'master'

refactor: 设备事件上报逻辑调整

See merge request yunteng/thingskit!161
Showing 18 changed files with 196 additions and 118 deletions
@@ -41,31 +41,13 @@ import org.thingsboard.server.common.data.edge.EdgeEventType; @@ -41,31 +41,13 @@ import org.thingsboard.server.common.data.edge.EdgeEventType;
41 import org.thingsboard.server.common.data.edge.EdgeInfo; 41 import org.thingsboard.server.common.data.edge.EdgeInfo;
42 import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; 42 import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
43 import org.thingsboard.server.common.data.exception.ThingsboardException; 43 import org.thingsboard.server.common.data.exception.ThingsboardException;
44 -import org.thingsboard.server.common.data.id.AlarmId;  
45 -import org.thingsboard.server.common.data.id.AssetId;  
46 -import org.thingsboard.server.common.data.id.CustomerId;  
47 -import org.thingsboard.server.common.data.id.DashboardId;  
48 -import org.thingsboard.server.common.data.id.DeviceId;  
49 -import org.thingsboard.server.common.data.id.DeviceProfileId;  
50 -import org.thingsboard.server.common.data.id.EdgeId;  
51 -import org.thingsboard.server.common.data.id.EntityId;  
52 -import org.thingsboard.server.common.data.id.EntityIdFactory;  
53 -import org.thingsboard.server.common.data.id.EntityViewId;  
54 -import org.thingsboard.server.common.data.id.OtaPackageId;  
55 -import org.thingsboard.server.common.data.id.RpcId;  
56 -import org.thingsboard.server.common.data.id.RuleChainId;  
57 -import org.thingsboard.server.common.data.id.RuleNodeId;  
58 -import org.thingsboard.server.common.data.id.TbResourceId;  
59 -import org.thingsboard.server.common.data.id.TenantId;  
60 -import org.thingsboard.server.common.data.id.TenantProfileId;  
61 -import org.thingsboard.server.common.data.id.UserId;  
62 -import org.thingsboard.server.common.data.id.WidgetTypeId;  
63 -import org.thingsboard.server.common.data.id.WidgetsBundleId; 44 +import org.thingsboard.server.common.data.id.*;
64 import org.thingsboard.server.common.data.page.PageDataIterableByTenantIdEntityId; 45 import org.thingsboard.server.common.data.page.PageDataIterableByTenantIdEntityId;
65 import org.thingsboard.server.common.data.page.PageLink; 46 import org.thingsboard.server.common.data.page.PageLink;
66 import org.thingsboard.server.common.data.page.SortOrder; 47 import org.thingsboard.server.common.data.page.SortOrder;
67 import org.thingsboard.server.common.data.page.TimePageLink; 48 import org.thingsboard.server.common.data.page.TimePageLink;
68 import org.thingsboard.server.common.data.plugin.ComponentDescriptor; 49 import org.thingsboard.server.common.data.plugin.ComponentDescriptor;
  50 +import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
69 import org.thingsboard.server.common.data.plugin.ComponentType; 51 import org.thingsboard.server.common.data.plugin.ComponentType;
70 import org.thingsboard.server.common.data.relation.EntityRelation; 52 import org.thingsboard.server.common.data.relation.EntityRelation;
71 import org.thingsboard.server.common.data.rpc.Rpc; 53 import org.thingsboard.server.common.data.rpc.Rpc;
@@ -74,6 +56,9 @@ import org.thingsboard.server.common.data.rule.RuleChainType; @@ -74,6 +56,9 @@ import org.thingsboard.server.common.data.rule.RuleChainType;
74 import org.thingsboard.server.common.data.rule.RuleNode; 56 import org.thingsboard.server.common.data.rule.RuleNode;
75 import org.thingsboard.server.common.data.widget.WidgetTypeDetails; 57 import org.thingsboard.server.common.data.widget.WidgetTypeDetails;
76 import org.thingsboard.server.common.data.widget.WidgetsBundle; 58 import org.thingsboard.server.common.data.widget.WidgetsBundle;
  59 +import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
  60 +import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException;
  61 +import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
77 import org.thingsboard.server.dao.asset.AssetService; 62 import org.thingsboard.server.dao.asset.AssetService;
78 import org.thingsboard.server.dao.attributes.AttributesService; 63 import org.thingsboard.server.dao.attributes.AttributesService;
79 import org.thingsboard.server.dao.audit.AuditLogService; 64 import org.thingsboard.server.dao.audit.AuditLogService;
@@ -121,12 +106,7 @@ import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService; @@ -121,12 +106,7 @@ import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService;
121 106
122 import javax.mail.MessagingException; 107 import javax.mail.MessagingException;
123 import javax.servlet.http.HttpServletResponse; 108 import javax.servlet.http.HttpServletResponse;
124 -import java.util.ArrayList;  
125 -import java.util.Collections;  
126 -import java.util.List;  
127 -import java.util.Optional;  
128 -import java.util.Set;  
129 -import java.util.UUID; 109 +import java.util.*;
130 110
131 import static org.thingsboard.server.controller.ControllerConstants.DEFAULT_PAGE_SIZE; 111 import static org.thingsboard.server.controller.ControllerConstants.DEFAULT_PAGE_SIZE;
132 import static org.thingsboard.server.controller.ControllerConstants.INCORRECT_TENANT_ID; 112 import static org.thingsboard.server.controller.ControllerConstants.INCORRECT_TENANT_ID;
@@ -929,4 +909,45 @@ public abstract class BaseController { @@ -929,4 +909,45 @@ public abstract class BaseController {
929 return deviceProfileData; 909 return deviceProfileData;
930 910
931 } 911 }
  912 +
  913 +
  914 + /**
  915 + * 更新thingsboard的设备配置信息
  916 + *
  917 + * @param deviceProfile 设备配置
  918 + * @throws ThingsboardException
  919 + */
  920 + protected DeviceProfile updateTbDeviceProfile(DeviceProfile deviceProfile)
  921 + throws ThingsboardException {
  922 + boolean isFirmwareChanged = false;
  923 + boolean isSoftwareChanged = false;
  924 +
  925 + DeviceProfile oldDeviceProfile =
  926 + deviceProfileService.findDeviceProfileById(getTenantId(), deviceProfile.getId());
  927 + if (!Objects.equals(deviceProfile.getFirmwareId(), oldDeviceProfile.getFirmwareId())) {
  928 + isFirmwareChanged = true;
  929 + }
  930 + if (!Objects.equals(deviceProfile.getSoftwareId(), oldDeviceProfile.getSoftwareId())) {
  931 + isSoftwareChanged = true;
  932 + }
  933 + if (FastIotConstants.ASSERT_DEFAULT_NAME.equals(oldDeviceProfile.getName())
  934 + && !Objects.equals(deviceProfile.getName(), oldDeviceProfile.getName())) {
  935 + throw new TkDataValidationException(ErrorMessage.ASSERT_DEFAULT_NAME_NO_CHANGED.getMessage());
  936 + }
  937 +
  938 + DeviceProfile savedDeviceProfile =
  939 + checkNotNull(deviceProfileService.saveDeviceProfile(deviceProfile));
  940 +
  941 + tbClusterService.onDeviceProfileChange(savedDeviceProfile, null);
  942 + tbClusterService.broadcastEntityStateChangeEvent(
  943 + deviceProfile.getTenantId(), savedDeviceProfile.getId(), ComponentLifecycleEvent.UPDATED);
  944 +
  945 + logEntityAction(savedDeviceProfile.getId(), savedDeviceProfile, null, ActionType.UPDATED, null);
  946 +
  947 + otaPackageStateService.update(savedDeviceProfile, isFirmwareChanged, isSoftwareChanged);
  948 +
  949 + sendEntityNotificationMsg(
  950 + getTenantId(), savedDeviceProfile.getId(), EdgeEventActionType.UPDATED);
  951 + return savedDeviceProfile;
  952 + }
932 } 953 }
1 package org.thingsboard.server.controller.yunteng; 1 package org.thingsboard.server.controller.yunteng;
2 2
  3 +import com.fasterxml.jackson.core.type.TypeReference;
3 import com.fasterxml.jackson.databind.JsonNode; 4 import com.fasterxml.jackson.databind.JsonNode;
4 import io.swagger.annotations.Api; 5 import io.swagger.annotations.Api;
5 import io.swagger.annotations.ApiOperation; 6 import io.swagger.annotations.ApiOperation;
@@ -9,7 +10,10 @@ import org.springframework.http.ResponseEntity; @@ -9,7 +10,10 @@ import org.springframework.http.ResponseEntity;
9 import org.springframework.security.access.prepost.PreAuthorize; 10 import org.springframework.security.access.prepost.PreAuthorize;
10 import org.springframework.validation.annotation.Validated; 11 import org.springframework.validation.annotation.Validated;
11 import org.springframework.web.bind.annotation.*; 12 import org.springframework.web.bind.annotation.*;
  13 +import org.thingsboard.server.common.data.DeviceProfile;
  14 +import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
12 import org.thingsboard.server.common.data.exception.ThingsboardException; 15 import org.thingsboard.server.common.data.exception.ThingsboardException;
  16 +import org.thingsboard.server.common.data.id.DeviceProfileId;
13 import org.thingsboard.server.common.data.yunteng.common.AddGroup; 17 import org.thingsboard.server.common.data.yunteng.common.AddGroup;
14 import org.thingsboard.server.common.data.yunteng.common.DeleteGroup; 18 import org.thingsboard.server.common.data.yunteng.common.DeleteGroup;
15 import org.thingsboard.server.common.data.yunteng.common.UpdateGroup; 19 import org.thingsboard.server.common.data.yunteng.common.UpdateGroup;
@@ -18,9 +22,11 @@ import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; @@ -18,9 +22,11 @@ import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
18 import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO; 22 import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO;
19 import org.thingsboard.server.common.data.yunteng.dto.DeviceProfileDTO; 23 import org.thingsboard.server.common.data.yunteng.dto.DeviceProfileDTO;
20 import org.thingsboard.server.common.data.yunteng.dto.ThingsModelDTO; 24 import org.thingsboard.server.common.data.yunteng.dto.ThingsModelDTO;
  25 +import org.thingsboard.server.common.data.yunteng.dto.TkThingsModel;
21 import org.thingsboard.server.common.data.yunteng.enums.FunctionTypeEnum; 26 import org.thingsboard.server.common.data.yunteng.enums.FunctionTypeEnum;
22 import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum; 27 import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum;
23 import org.thingsboard.server.common.data.yunteng.enums.StatusEnum; 28 import org.thingsboard.server.common.data.yunteng.enums.StatusEnum;
  29 +import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
24 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; 30 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
25 import org.thingsboard.server.controller.BaseController; 31 import org.thingsboard.server.controller.BaseController;
26 import org.thingsboard.server.dao.yunteng.service.ThingsModelService; 32 import org.thingsboard.server.dao.yunteng.service.ThingsModelService;
@@ -119,9 +125,12 @@ public class ThingsModelController extends BaseController { @@ -119,9 +125,12 @@ public class ThingsModelController extends BaseController {
119 if (null == dto) { 125 if (null == dto) {
120 throw new TkDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); 126 throw new TkDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
121 } 127 }
122 - return ResponseEntity.ok(  
123 - thingsModelService.changeTSLStatus(  
124 - deviceProfileId, tenantId, StatusEnum.ENABLE.getIndex())); 128 + List<TkThingsModel> thingsModels = thingsModelService.changeTSLStatus(deviceProfileId, tenantId, StatusEnum.ENABLE.getIndex());
  129 + DeviceProfile tbProfile = deviceProfileService.findDeviceProfileById(getTenantId(), DeviceProfileId.fromString(dto.getTbProfileId()));
  130 + tbProfile.getProfileData()
  131 + .setThingsModel(thingsModels);
  132 + updateTbDeviceProfile(tbProfile);
  133 + return ResponseEntity.ok(true);
125 } 134 }
126 135
127 @GetMapping("/get_services/{deviceProfileId}") 136 @GetMapping("/get_services/{deviceProfileId}")
@@ -114,45 +114,6 @@ public class TkDeviceScriptController extends BaseController { @@ -114,45 +114,6 @@ public class TkDeviceScriptController extends BaseController {
114 return ResponseEntity.ok(true); 114 return ResponseEntity.ok(true);
115 } 115 }
116 116
117 - /**  
118 - * 更新thingsboard的设备配置信息  
119 - *  
120 - * @param deviceProfile 设备配置  
121 - * @throws ThingsboardException  
122 - */  
123 - private DeviceProfile updateTbDeviceProfile(DeviceProfile deviceProfile)  
124 - throws ThingsboardException {  
125 - boolean isFirmwareChanged = false;  
126 - boolean isSoftwareChanged = false;  
127 -  
128 - DeviceProfile oldDeviceProfile =  
129 - deviceProfileService.findDeviceProfileById(getTenantId(), deviceProfile.getId());  
130 - if (!Objects.equals(deviceProfile.getFirmwareId(), oldDeviceProfile.getFirmwareId())) {  
131 - isFirmwareChanged = true;  
132 - }  
133 - if (!Objects.equals(deviceProfile.getSoftwareId(), oldDeviceProfile.getSoftwareId())) {  
134 - isSoftwareChanged = true;  
135 - }  
136 - if (FastIotConstants.ASSERT_DEFAULT_NAME.equals(oldDeviceProfile.getName())  
137 - && !Objects.equals(deviceProfile.getName(), oldDeviceProfile.getName())) {  
138 - throw new TkDataValidationException(ErrorMessage.ASSERT_DEFAULT_NAME_NO_CHANGED.getMessage());  
139 - }  
140 -  
141 - DeviceProfile savedDeviceProfile =  
142 - checkNotNull(deviceProfileService.saveDeviceProfile(deviceProfile));  
143 -  
144 - tbClusterService.onDeviceProfileChange(savedDeviceProfile, null);  
145 - tbClusterService.broadcastEntityStateChangeEvent(  
146 - deviceProfile.getTenantId(), savedDeviceProfile.getId(), ComponentLifecycleEvent.UPDATED);  
147 -  
148 - logEntityAction(savedDeviceProfile.getId(), savedDeviceProfile, null, ActionType.UPDATED, null);  
149 -  
150 - otaPackageStateService.update(savedDeviceProfile, isFirmwareChanged, isSoftwareChanged);  
151 -  
152 - sendEntityNotificationMsg(  
153 - getTenantId(), savedDeviceProfile.getId(), EdgeEventActionType.UPDATED);  
154 - return savedDeviceProfile;  
155 - }  
156 117
157 @GetMapping("{id}") 118 @GetMapping("{id}")
158 @ApiOperation("详情") 119 @ApiOperation("详情")
@@ -32,6 +32,7 @@ import org.thingsboard.server.common.data.id.DeviceProfileId; @@ -32,6 +32,7 @@ import org.thingsboard.server.common.data.id.DeviceProfileId;
32 import org.thingsboard.server.common.data.id.EntityId; 32 import org.thingsboard.server.common.data.id.EntityId;
33 import org.thingsboard.server.common.data.id.TenantId; 33 import org.thingsboard.server.common.data.id.TenantId;
34 import org.thingsboard.server.common.data.kv.*; 34 import org.thingsboard.server.common.data.kv.*;
  35 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
35 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum; 36 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
36 import org.thingsboard.server.common.msg.queue.ServiceType; 37 import org.thingsboard.server.common.msg.queue.ServiceType;
37 import org.thingsboard.server.common.msg.queue.TbCallback; 38 import org.thingsboard.server.common.msg.queue.TbCallback;
@@ -235,13 +236,13 @@ public class DefaultTelemetrySubscriptionService extends AbstractSubscriptionSer @@ -235,13 +236,13 @@ public class DefaultTelemetrySubscriptionService extends AbstractSubscriptionSer
235 236
236 //Thingskit function 237 //Thingskit function
237 @Override 238 @Override
238 - public void saveAndNotify(TenantId tenantId, DeviceProfileId profileId, EntityId entityId, String eventId, DeviceEventTypeEnum eventType, String eventData, Long eventTime, FutureCallback<Void> callback) { 239 + public void saveAndNotify(TenantId tenantId, DeviceProfileId profileId, EntityId entityId, List<TkEventKvEntry> events, String eventData, Long eventTime, FutureCallback<Void> callback) {
239 checkInternalEntity(entityId); 240 checkInternalEntity(entityId);
240 - saveAndNotifyInternal(tenantId,profileId, entityId, eventId,eventType, eventData,eventTime, callback); 241 + saveAndNotifyInternal(tenantId,profileId, entityId, events, eventData,eventTime, callback);
241 } 242 }
242 @Override 243 @Override
243 - public void saveAndNotifyInternal(TenantId tenantId,DeviceProfileId profileId, EntityId entityId,String eventId, DeviceEventTypeEnum eventType, String eventData,Long eventTime, FutureCallback<Void> callback) {  
244 - ListenableFuture<List<Void>> saveFuture = eventsService.save(tenantId,profileId, entityId,eventId,eventType, eventData,eventTime); 244 + public void saveAndNotifyInternal(TenantId tenantId,DeviceProfileId profileId, EntityId entityId,List<TkEventKvEntry> events, String eventData,Long eventTime, FutureCallback<Void> callback) {
  245 + ListenableFuture<List<Void>> saveFuture = eventsService.save(tenantId,profileId, entityId,events, eventData,eventTime);
245 addVoidCallback(saveFuture, callback); 246 addVoidCallback(saveFuture, callback);
246 // addWsCallback(saveFuture, success -> onAttributesUpdate(tenantId, entityId, scope, attributes, notifyDevice)); 247 // addWsCallback(saveFuture, success -> onAttributesUpdate(tenantId, entityId, scope, attributes, notifyDevice));
247 } 248 }
@@ -39,7 +39,7 @@ public interface InternalTelemetryService extends RuleEngineTelemetryService { @@ -39,7 +39,7 @@ public interface InternalTelemetryService extends RuleEngineTelemetryService {
39 void saveAndNotifyInternal(TenantId tenantId, EntityId entityId, String scope, List<AttributeKvEntry> attributes, boolean notifyDevice, FutureCallback<Void> callback); 39 void saveAndNotifyInternal(TenantId tenantId, EntityId entityId, String scope, List<AttributeKvEntry> attributes, boolean notifyDevice, FutureCallback<Void> callback);
40 40
41 //Thingskit function 41 //Thingskit function
42 - void saveAndNotifyInternal(TenantId tenantId, DeviceProfileId profileId, EntityId entityId, String eventId, DeviceEventTypeEnum eventType, String eventData,Long eventTime, FutureCallback<Void> callback); 42 + void saveAndNotifyInternal(TenantId tenantId, DeviceProfileId profileId, EntityId entityId, List<TkEventKvEntry> eventKvEntry, String eventData, Long eventTime, FutureCallback<Void> callback);
43 43
44 void saveLatestAndNotifyInternal(TenantId tenantId, EntityId entityId, List<TsKvEntry> ts, FutureCallback<Void> callback); 44 void saveLatestAndNotifyInternal(TenantId tenantId, EntityId entityId, List<TsKvEntry> ts, FutureCallback<Void> callback);
45 45
@@ -22,6 +22,7 @@ import org.thingsboard.server.common.data.id.TenantId; @@ -22,6 +22,7 @@ import org.thingsboard.server.common.data.id.TenantId;
22 import org.thingsboard.server.common.data.page.PageData; 22 import org.thingsboard.server.common.data.page.PageData;
23 import org.thingsboard.server.common.data.page.TimePageLink; 23 import org.thingsboard.server.common.data.page.TimePageLink;
24 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto; 24 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto;
  25 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
25 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum; 26 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
26 27
27 import java.util.List; 28 import java.util.List;
@@ -31,7 +32,7 @@ import java.util.List; @@ -31,7 +32,7 @@ import java.util.List;
31 */ 32 */
32 public interface TkEventsService { 33 public interface TkEventsService {
33 PageData<TkEventKvDto> findEvents(EntityId entityId,String eventIdentifier, DeviceEventTypeEnum eventType, TimePageLink pageLink); 34 PageData<TkEventKvDto> findEvents(EntityId entityId,String eventIdentifier, DeviceEventTypeEnum eventType, TimePageLink pageLink);
34 - ListenableFuture<List<Void>> save(TenantId tenantId, DeviceProfileId profileId,EntityId entityId, String eventId, DeviceEventTypeEnum eventType, String eventData,Long eventTime); 35 + ListenableFuture<List<Void>> save(TenantId tenantId, DeviceProfileId profileId, EntityId entityId, List<TkEventKvEntry> eventKvEntry, String eventData, Long eventTime);
35 36
36 37
37 38
@@ -18,6 +18,7 @@ package org.thingsboard.server.common.data.device.profile; @@ -18,6 +18,7 @@ package org.thingsboard.server.common.data.device.profile;
18 import io.swagger.annotations.ApiModel; 18 import io.swagger.annotations.ApiModel;
19 import io.swagger.annotations.ApiModelProperty; 19 import io.swagger.annotations.ApiModelProperty;
20 import lombok.Data; 20 import lombok.Data;
  21 +import org.thingsboard.server.common.data.yunteng.dto.TkThingsModel;
21 22
22 import javax.validation.Valid; 23 import javax.validation.Valid;
23 import java.io.Serializable; 24 import java.io.Serializable;
@@ -38,4 +39,9 @@ public class DeviceProfileData implements Serializable { @@ -38,4 +39,9 @@ public class DeviceProfileData implements Serializable {
38 @ApiModelProperty(position = 4, value = "JSON array of alarm rules configuration per device profile") 39 @ApiModelProperty(position = 4, value = "JSON array of alarm rules configuration per device profile")
39 private List<DeviceProfileAlarm> alarms; 40 private List<DeviceProfileAlarm> alarms;
40 41
  42 + //Thingskit function
  43 + @Valid
  44 + @ApiModelProperty(position = 5, value = "JSON object of has published things model configuration")
  45 + private List<TkThingsModel> thingsModel;
  46 +
41 } 47 }
1 -/**  
2 - * Copyright © 2016-2022 The Thingsboard Authors  
3 - *  
4 - * Licensed under the Apache License, Version 2.0 (the "License");  
5 - * you may not use this file except in compliance with the License.  
6 - * You may obtain a copy of the License at  
7 - *  
8 - * http://www.apache.org/licenses/LICENSE-2.0  
9 - *  
10 - * Unless required by applicable law or agreed to in writing, software  
11 - * distributed under the License is distributed on an "AS IS" BASIS,  
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
13 - * See the License for the specific language governing permissions and  
14 - * limitations under the License.  
15 - */  
16 package org.thingsboard.server.common.data.yunteng.dto; 1 package org.thingsboard.server.common.data.yunteng.dto;
17 2
18 -import org.thingsboard.server.common.data.kv.KvEntry; 3 +import lombok.Data;
  4 +import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
  5 +
  6 +import java.io.Serializable;
19 7
20 /** 8 /**
21 * @author Andrew Shvayka 9 * @author Andrew Shvayka
22 */ 10 */
23 -public interface TkEventKvEntry extends KvEntry { 11 +@Data
  12 +public class TkEventKvEntry implements Serializable {
  13 +
24 14
25 - long getEventTime(); 15 + private DeviceEventTypeEnum eventType;
  16 + private String eventName;
  17 + private String eventIdentifier;
26 18
27 } 19 }
  1 +package org.thingsboard.server.common.data.yunteng.dto;
  2 +
  3 +import com.fasterxml.jackson.databind.JsonNode;
  4 +import io.swagger.annotations.ApiModel;
  5 +import io.swagger.annotations.ApiModelProperty;
  6 +import lombok.Data;
  7 +import lombok.EqualsAndHashCode;
  8 +import org.thingsboard.server.common.data.yunteng.common.AddGroup;
  9 +import org.thingsboard.server.common.data.yunteng.common.UpdateGroup;
  10 +import org.thingsboard.server.common.data.yunteng.enums.CallTypeEnum;
  11 +import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
  12 +import org.thingsboard.server.common.data.yunteng.enums.FunctionTypeEnum;
  13 +
  14 +import javax.validation.constraints.NotEmpty;
  15 +import javax.validation.constraints.NotNull;
  16 +import java.io.Serializable;
  17 +
  18 +@Data
  19 +public class TkThingsModel implements Serializable {
  20 +
  21 + private FunctionTypeEnum functionType;
  22 +
  23 +
  24 + private String functionName;
  25 +
  26 +
  27 + private String identifier;
  28 +
  29 +
  30 + private CallTypeEnum callType;
  31 +
  32 + private String accessMode;
  33 +
  34 +
  35 + private DeviceEventTypeEnum eventType;
  36 +
  37 +
  38 + private JsonNode functionJson;
  39 +
  40 +
  41 + private Integer status;
  42 +
  43 +
  44 + private String deviceProfileId;
  45 +
  46 + private String remark;
  47 +}
@@ -582,7 +582,7 @@ public class DefaultTransportService implements TransportService { @@ -582,7 +582,7 @@ public class DefaultTransportService implements TransportService {
582 throw new TkDataValidationException(ErrorMessage.INVALID_TOPIC.getMessage()); 582 throw new TkDataValidationException(ErrorMessage.INVALID_TOPIC.getMessage());
583 } 583 }
584 String[] eventInfo = topicInfo[1].split("/"); 584 String[] eventInfo = topicInfo[1].split("/");
585 - if(null == eventInfo || eventInfo.length !=3){ 585 + if(null == eventInfo || eventInfo.length !=2){
586 throw new TkDataValidationException(ErrorMessage.INVALID_TOPIC.getMessage()); 586 throw new TkDataValidationException(ErrorMessage.INVALID_TOPIC.getMessage());
587 } 587 }
588 reportActivityInternal(sessionInfo); 588 reportActivityInternal(sessionInfo);
@@ -593,7 +593,7 @@ public class DefaultTransportService implements TransportService { @@ -593,7 +593,7 @@ public class DefaultTransportService implements TransportService {
593 DeviceId deviceId = new DeviceId(new UUID(sessionInfo.getDeviceIdMSB(), sessionInfo.getDeviceIdLSB())); 593 DeviceId deviceId = new DeviceId(new UUID(sessionInfo.getDeviceIdMSB(), sessionInfo.getDeviceIdLSB()));
594 metaData.putValue("deviceName", sessionInfo.getDeviceName()); 594 metaData.putValue("deviceName", sessionInfo.getDeviceName());
595 metaData.putValue("deviceType", sessionInfo.getDeviceType()); 595 metaData.putValue("deviceType", sessionInfo.getDeviceType());
596 - UUID deviceProfileId = new UUID(sessionInfo.getDeviceProfileIdMSB(),sessionInfo.getDeviceProfileIdMSB()); 596 + UUID deviceProfileId = new UUID(sessionInfo.getDeviceProfileIdMSB(),sessionInfo.getDeviceProfileIdLSB());
597 metaData.putValue("device_profile_id", deviceProfileId.toString()); 597 metaData.putValue("device_profile_id", deviceProfileId.toString());
598 metaData.putValue("deviceId",eventInfo[0]); 598 metaData.putValue("deviceId",eventInfo[0]);
599 metaData.putValue("event_identifier", eventInfo[1]); 599 metaData.putValue("event_identifier", eventInfo[1]);
@@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils; @@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils;
8 import org.springframework.beans.BeanUtils; 8 import org.springframework.beans.BeanUtils;
9 import org.springframework.stereotype.Service; 9 import org.springframework.stereotype.Service;
10 import org.springframework.transaction.annotation.Transactional; 10 import org.springframework.transaction.annotation.Transactional;
  11 +import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
11 import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; 12 import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
12 import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException; 13 import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException;
13 import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; 14 import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
@@ -195,8 +196,16 @@ public class ThingsModelServiceImpl @@ -195,8 +196,16 @@ public class ThingsModelServiceImpl
195 196
196 @Override 197 @Override
197 @Transactional 198 @Transactional
198 - public boolean changeTSLStatus(String deviceProfileId, String tenantId, Integer status) {  
199 - return baseMapper.changeTSLStatus(deviceProfileId, tenantId, status); 199 + public List<TkThingsModel> changeTSLStatus(String deviceProfileId, String tenantId, Integer status) {
  200 + boolean result = baseMapper.changeTSLStatus(deviceProfileId, tenantId, status);
  201 + if(result){
  202 + List<TkThingsModelEntity> entityList = baseMapper.selectList(new LambdaQueryWrapper<TkThingsModelEntity>().eq(TkThingsModelEntity::getDeviceProfileId,deviceProfileId));
  203 + if (entityList.isEmpty()) {
  204 + return null;
  205 + }
  206 + return entityList.stream().map(en -> en.getDTO(TkThingsModel.class)).collect(Collectors.toList());
  207 + }
  208 + return null;
200 } 209 }
201 210
202 private boolean checkIdentifier( 211 private boolean checkIdentifier(
@@ -25,10 +25,13 @@ import org.thingsboard.server.common.data.id.TenantId; @@ -25,10 +25,13 @@ import org.thingsboard.server.common.data.id.TenantId;
25 import org.thingsboard.server.common.data.page.PageData; 25 import org.thingsboard.server.common.data.page.PageData;
26 import org.thingsboard.server.common.data.page.TimePageLink; 26 import org.thingsboard.server.common.data.page.TimePageLink;
27 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto; 27 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto;
  28 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
28 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum; 29 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
  30 +import org.thingsboard.server.dao.attributes.AttributeUtils;
29 import org.thingsboard.server.dao.yunteng.event.TkEventsService; 31 import org.thingsboard.server.dao.yunteng.event.TkEventsService;
30 32
31 import java.util.List; 33 import java.util.List;
  34 +import java.util.stream.Collectors;
32 35
33 import static org.thingsboard.server.dao.yunteng.jpa.dao.event.EventUtils.validate; 36 import static org.thingsboard.server.dao.yunteng.jpa.dao.event.EventUtils.validate;
34 37
@@ -53,10 +56,10 @@ public class BaseEventsService implements TkEventsService { @@ -53,10 +56,10 @@ public class BaseEventsService implements TkEventsService {
53 } 56 }
54 57
55 @Override 58 @Override
56 - public ListenableFuture<List<Void>> save(TenantId tenantId,DeviceProfileId profileId, EntityId entityId, String eventId, DeviceEventTypeEnum eventType, String eventData,Long eventTime) {  
57 - validate(entityId, eventId,eventType);  
58 -  
59 - ListenableFuture<Void> saveFutures = eventsDao.save(tenantId, profileId,entityId, eventType,eventId, eventData,eventTime); 59 + public ListenableFuture<List<Void>> save(TenantId tenantId, DeviceProfileId profileId, EntityId entityId, List<TkEventKvEntry> events, String eventData, Long eventTime) {
  60 + validate(entityId);
  61 + events.forEach(event -> validate(event));
  62 + List<ListenableFuture<Void>> saveFutures = events.stream().map(event -> eventsDao.save(tenantId, profileId,entityId, event,eventData,eventTime)).collect(Collectors.toList());
60 return Futures.allAsList(saveFutures); 63 return Futures.allAsList(saveFutures);
61 } 64 }
62 65
@@ -44,11 +44,8 @@ public class EventUtils { @@ -44,11 +44,8 @@ public class EventUtils {
44 public static void validate(TkEventKvEntry kvEntry) { 44 public static void validate(TkEventKvEntry kvEntry) {
45 if (kvEntry == null) { 45 if (kvEntry == null) {
46 throw new IncorrectParameterException("Key value entry can't be null"); 46 throw new IncorrectParameterException("Key value entry can't be null");
47 - } else if (kvEntry.getDataType() == null) {  
48 - throw new IncorrectParameterException("Incorrect kvEntry. Data type can't be null");  
49 - } else {  
50 - Validator.validateString(kvEntry.getKey(), "Incorrect kvEntry. Key can't be empty");  
51 - Validator.validatePositiveNumber(kvEntry.getEventTime(), "Incorrect last update ts. Ts should be positive"); 47 + } else if (kvEntry.getEventType() == null) {
  48 + throw new IncorrectParameterException("Incorrect event type. event type can't be null");
52 } 49 }
53 } 50 }
54 } 51 }
@@ -22,8 +22,11 @@ import org.thingsboard.server.common.data.id.TenantId; @@ -22,8 +22,11 @@ import org.thingsboard.server.common.data.id.TenantId;
22 import org.thingsboard.server.common.data.page.PageData; 22 import org.thingsboard.server.common.data.page.PageData;
23 import org.thingsboard.server.common.data.page.TimePageLink; 23 import org.thingsboard.server.common.data.page.TimePageLink;
24 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto; 24 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto;
  25 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
25 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum; 26 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
26 27
  28 +import java.util.List;
  29 +
27 /** 30 /**
28 * @author Andrew Shvayka 31 * @author Andrew Shvayka
29 */ 32 */
@@ -31,6 +34,6 @@ public interface EventsDao { @@ -31,6 +34,6 @@ public interface EventsDao {
31 34
32 PageData<TkEventKvDto> findEvents(EntityId entityId,String eventIdentifier, DeviceEventTypeEnum eventType, TimePageLink pageLink); 35 PageData<TkEventKvDto> findEvents(EntityId entityId,String eventIdentifier, DeviceEventTypeEnum eventType, TimePageLink pageLink);
33 36
34 - ListenableFuture<Void> save(TenantId tenantId, DeviceProfileId profileId,EntityId entityId, DeviceEventTypeEnum attributeType,String eventIdentifier, String eventData,Long eventTime); 37 + ListenableFuture<Void> save(TenantId tenantId, DeviceProfileId profileId, EntityId entityId, TkEventKvEntry eventKvEntry, String eventData, Long eventTime);
35 38
36 } 39 }
@@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.id.TenantId; @@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.id.TenantId;
26 import org.thingsboard.server.common.data.page.PageData; 26 import org.thingsboard.server.common.data.page.PageData;
27 import org.thingsboard.server.common.data.page.TimePageLink; 27 import org.thingsboard.server.common.data.page.TimePageLink;
28 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto; 28 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto;
  29 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
29 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum; 30 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
30 import org.thingsboard.server.common.stats.StatsFactory; 31 import org.thingsboard.server.common.stats.StatsFactory;
31 import org.thingsboard.server.dao.DaoUtil; 32 import org.thingsboard.server.dao.DaoUtil;
@@ -40,6 +41,7 @@ import org.thingsboard.server.dao.yunteng.jpa.entity.events.TkEventKvEntity; @@ -40,6 +41,7 @@ import org.thingsboard.server.dao.yunteng.jpa.entity.events.TkEventKvEntity;
40 import javax.annotation.PostConstruct; 41 import javax.annotation.PostConstruct;
41 import javax.annotation.PreDestroy; 42 import javax.annotation.PreDestroy;
42 import java.util.Comparator; 43 import java.util.Comparator;
  44 +import java.util.List;
43 import java.util.function.Function; 45 import java.util.function.Function;
44 46
45 @Component 47 @Component
@@ -111,10 +113,10 @@ public class JpaEventDao extends JpaAbstractDaoListeningExecutorService implemen @@ -111,10 +113,10 @@ public class JpaEventDao extends JpaAbstractDaoListeningExecutorService implemen
111 113
112 114
113 @Override 115 @Override
114 - public ListenableFuture<Void> save(TenantId tenantId, DeviceProfileId profileId,EntityId entityId,DeviceEventTypeEnum eventType, String eventIdentifier, String eventData,Long eventTime) { 116 + public ListenableFuture<Void> save(TenantId tenantId, DeviceProfileId profileId, EntityId entityId,TkEventKvEntry eventKvEntry, String eventData, Long eventTime) {
115 TkEventKvEntity entity = new TkEventKvEntity(); 117 TkEventKvEntity entity = new TkEventKvEntity();
116 - entity.setId(new TkEventKvCompositeKey(eventType, entityId.getId(), eventIdentifier, eventTime));  
117 - entity.setEventName("test"); 118 + entity.setId(new TkEventKvCompositeKey(eventKvEntry.getEventType(), entityId.getId(), eventKvEntry.getEventIdentifier(), eventTime));
  119 + entity.setEventName(eventKvEntry.getEventName());
118 entity.setDeviceProfileId(profileId.getId()); 120 entity.setDeviceProfileId(profileId.getId());
119 entity.setEventValue(eventData); 121 entity.setEventValue(eventData);
120 return addToQueue(entity); 122 return addToQueue(entity);
@@ -3,8 +3,10 @@ package org.thingsboard.server.dao.yunteng.service; @@ -3,8 +3,10 @@ package org.thingsboard.server.dao.yunteng.service;
3 import com.fasterxml.jackson.databind.JsonNode; 3 import com.fasterxml.jackson.databind.JsonNode;
4 import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO; 4 import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO;
5 import org.thingsboard.server.common.data.yunteng.dto.ThingsModelDTO; 5 import org.thingsboard.server.common.data.yunteng.dto.ThingsModelDTO;
  6 +import org.thingsboard.server.common.data.yunteng.dto.TkThingsModel;
6 import org.thingsboard.server.common.data.yunteng.enums.FunctionTypeEnum; 7 import org.thingsboard.server.common.data.yunteng.enums.FunctionTypeEnum;
7 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; 8 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
  9 +import org.thingsboard.server.dao.yunteng.entities.TkThingsModelEntity;
8 10
9 import java.util.List; 11 import java.util.List;
10 import java.util.Map; 12 import java.util.Map;
@@ -26,5 +28,5 @@ public interface ThingsModelService { @@ -26,5 +28,5 @@ public interface ThingsModelService {
26 28
27 JsonNode getTingsModelTSL(FunctionTypeEnum typeEnum, String tenantId, String deviceProfileId); 29 JsonNode getTingsModelTSL(FunctionTypeEnum typeEnum, String tenantId, String deviceProfileId);
28 30
29 - boolean changeTSLStatus(String deviceProfileId, String tenantId, Integer status); 31 + List<TkThingsModel> changeTSLStatus(String deviceProfileId, String tenantId, Integer status);
30 } 32 }
@@ -45,7 +45,7 @@ public interface RuleEngineTelemetryService { @@ -45,7 +45,7 @@ public interface RuleEngineTelemetryService {
45 void saveAndNotify(TenantId tenantId, EntityId entityId, String scope, List<AttributeKvEntry> attributes, boolean notifyDevice, FutureCallback<Void> callback); 45 void saveAndNotify(TenantId tenantId, EntityId entityId, String scope, List<AttributeKvEntry> attributes, boolean notifyDevice, FutureCallback<Void> callback);
46 46
47 //Thingskit function 47 //Thingskit function
48 - void saveAndNotify(TenantId tenantId, DeviceProfileId profileId, EntityId entityId, String eventId, DeviceEventTypeEnum eventType, String eventData,Long eventTime, FutureCallback<Void> callback); 48 + void saveAndNotify(TenantId tenantId, DeviceProfileId profileId, EntityId entityId, List<TkEventKvEntry> eventKvEntry, String eventData, Long eventTime, FutureCallback<Void> callback);
49 49
50 void saveLatestAndNotify(TenantId tenantId, EntityId entityId, List<TsKvEntry> ts, FutureCallback<Void> callback); 50 void saveLatestAndNotify(TenantId tenantId, EntityId entityId, List<TsKvEntry> ts, FutureCallback<Void> callback);
51 51
@@ -19,15 +19,24 @@ import lombok.extern.slf4j.Slf4j; @@ -19,15 +19,24 @@ import lombok.extern.slf4j.Slf4j;
19 import org.thingsboard.rule.engine.api.*; 19 import org.thingsboard.rule.engine.api.*;
20 import org.thingsboard.rule.engine.api.util.TbNodeUtils; 20 import org.thingsboard.rule.engine.api.util.TbNodeUtils;
21 import org.thingsboard.rule.engine.telemetry.TelemetryNodeCallback; 21 import org.thingsboard.rule.engine.telemetry.TelemetryNodeCallback;
  22 +import org.thingsboard.server.common.data.DeviceProfile;
22 import org.thingsboard.server.common.data.id.DeviceProfileId; 23 import org.thingsboard.server.common.data.id.DeviceProfileId;
  24 +import org.thingsboard.server.common.data.id.TenantId;
23 import org.thingsboard.server.common.data.plugin.ComponentType; 25 import org.thingsboard.server.common.data.plugin.ComponentType;
24 -import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum; 26 +import org.thingsboard.server.common.data.yunteng.core.exception.ThingsKitException;
  27 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
  28 +import org.thingsboard.server.common.data.yunteng.dto.TkThingsModel;
  29 +import org.thingsboard.server.common.data.yunteng.enums.FunctionTypeEnum;
25 import org.thingsboard.server.common.msg.TbMsg; 30 import org.thingsboard.server.common.msg.TbMsg;
26 import org.thingsboard.server.common.msg.session.SessionMsgType; 31 import org.thingsboard.server.common.msg.session.SessionMsgType;
27 32
  33 +import java.util.ArrayList;
  34 +import java.util.List;
28 import java.util.UUID; 35 import java.util.UUID;
  36 +import java.util.stream.Collectors;
29 37
30 -import static org.thingsboard.server.dao.model.ModelConstants.*; 38 +import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_DEVICE_PROFILE_ID_PROPERTY;
  39 +import static org.thingsboard.server.dao.model.ModelConstants.EVENT_IDENTIFIER_COLUMN;
31 40
32 @Slf4j 41 @Slf4j
33 @RuleNode( 42 @RuleNode(
@@ -57,17 +66,32 @@ public class TkMsgEventNode implements TbNode { @@ -57,17 +66,32 @@ public class TkMsgEventNode implements TbNode {
57 ctx.tellFailure(msg, new IllegalArgumentException("Unsupported msg type: " + msg.getType())); 66 ctx.tellFailure(msg, new IllegalArgumentException("Unsupported msg type: " + msg.getType()));
58 return; 67 return;
59 } 68 }
60 - String eventIdentifier = msg.getMetaData().getValue(EVENT_IDENTIFIER_COLUMN);  
61 String deviceProfileId = msg.getMetaData().getValue(DEVICE_DEVICE_PROFILE_ID_PROPERTY); 69 String deviceProfileId = msg.getMetaData().getValue(DEVICE_DEVICE_PROFILE_ID_PROPERTY);
  70 + TenantId tenantId = ctx.getTenantId();
  71 + DeviceProfileId profileId = new DeviceProfileId(UUID.fromString(deviceProfileId));
  72 + DeviceProfile profile = cache.get(tenantId,profileId);
  73 + String eventIdentifier = msg.getMetaData().getValue(EVENT_IDENTIFIER_COLUMN);
  74 + List<TkThingsModel> eventList =profile.getProfileData().getThingsModel().stream()
  75 + .filter(f -> f.getIdentifier().equals(eventIdentifier) && FunctionTypeEnum.events.equals(f.getFunctionType()))
  76 + .collect(Collectors.toList());
  77 + List<TkEventKvEntry> entryList = new ArrayList<>();
  78 + eventList.stream().forEach(i ->{
  79 + TkEventKvEntry item = new TkEventKvEntry();
  80 + item.setEventIdentifier(i.getIdentifier());
  81 + item.setEventType(i.getEventType());
  82 + item.setEventName(i.getFunctionName());
  83 + });
  84 + if(eventList.isEmpty()){
  85 + ctx.tellFailure(msg,new TbNodeException(String.format("产品物模型中未申明,上报的事件类型【%s】。", eventIdentifier)));
  86 + return;
  87 + }
62 ////TODO: 验证事件类型、事件标识符和数据建是否与产品物模型中的事件匹配 88 ////TODO: 验证事件类型、事件标识符和数据建是否与产品物模型中的事件匹配
63 long ts = System.currentTimeMillis(); 89 long ts = System.currentTimeMillis();
64 String src = msg.getData(); 90 String src = msg.getData();
65 - DeviceEventTypeEnum eventType = DeviceEventTypeEnum.valueOf(msg.getMetaData().getValue(EVENT_TYPE_COLUMN));  
66 ctx.getTelemetryService().saveAndNotify( 91 ctx.getTelemetryService().saveAndNotify(
67 - ctx.getTenantId(),new DeviceProfileId(UUID.fromString(deviceProfileId)), 92 + tenantId,profileId,
68 msg.getOriginator(), 93 msg.getOriginator(),
69 - eventIdentifier,  
70 - eventType, 94 + entryList,
71 src,ts, 95 src,ts,
72 new TelemetryNodeCallback(ctx, msg) 96 new TelemetryNodeCallback(ctx, msg)
73 ); 97 );