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 41 import org.thingsboard.server.common.data.edge.EdgeInfo;
42 42 import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
43 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 45 import org.thingsboard.server.common.data.page.PageDataIterableByTenantIdEntityId;
65 46 import org.thingsboard.server.common.data.page.PageLink;
66 47 import org.thingsboard.server.common.data.page.SortOrder;
67 48 import org.thingsboard.server.common.data.page.TimePageLink;
68 49 import org.thingsboard.server.common.data.plugin.ComponentDescriptor;
  50 +import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
69 51 import org.thingsboard.server.common.data.plugin.ComponentType;
70 52 import org.thingsboard.server.common.data.relation.EntityRelation;
71 53 import org.thingsboard.server.common.data.rpc.Rpc;
... ... @@ -74,6 +56,9 @@ import org.thingsboard.server.common.data.rule.RuleChainType;
74 56 import org.thingsboard.server.common.data.rule.RuleNode;
75 57 import org.thingsboard.server.common.data.widget.WidgetTypeDetails;
76 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 62 import org.thingsboard.server.dao.asset.AssetService;
78 63 import org.thingsboard.server.dao.attributes.AttributesService;
79 64 import org.thingsboard.server.dao.audit.AuditLogService;
... ... @@ -121,12 +106,7 @@ import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService;
121 106
122 107 import javax.mail.MessagingException;
123 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 111 import static org.thingsboard.server.controller.ControllerConstants.DEFAULT_PAGE_SIZE;
132 112 import static org.thingsboard.server.controller.ControllerConstants.INCORRECT_TENANT_ID;
... ... @@ -929,4 +909,45 @@ public abstract class BaseController {
929 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 1 package org.thingsboard.server.controller.yunteng;
2 2
  3 +import com.fasterxml.jackson.core.type.TypeReference;
3 4 import com.fasterxml.jackson.databind.JsonNode;
4 5 import io.swagger.annotations.Api;
5 6 import io.swagger.annotations.ApiOperation;
... ... @@ -9,7 +10,10 @@ import org.springframework.http.ResponseEntity;
9 10 import org.springframework.security.access.prepost.PreAuthorize;
10 11 import org.springframework.validation.annotation.Validated;
11 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 15 import org.thingsboard.server.common.data.exception.ThingsboardException;
  16 +import org.thingsboard.server.common.data.id.DeviceProfileId;
13 17 import org.thingsboard.server.common.data.yunteng.common.AddGroup;
14 18 import org.thingsboard.server.common.data.yunteng.common.DeleteGroup;
15 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 22 import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO;
19 23 import org.thingsboard.server.common.data.yunteng.dto.DeviceProfileDTO;
20 24 import org.thingsboard.server.common.data.yunteng.dto.ThingsModelDTO;
  25 +import org.thingsboard.server.common.data.yunteng.dto.TkThingsModel;
21 26 import org.thingsboard.server.common.data.yunteng.enums.FunctionTypeEnum;
22 27 import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum;
23 28 import org.thingsboard.server.common.data.yunteng.enums.StatusEnum;
  29 +import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
24 30 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
25 31 import org.thingsboard.server.controller.BaseController;
26 32 import org.thingsboard.server.dao.yunteng.service.ThingsModelService;
... ... @@ -119,9 +125,12 @@ public class ThingsModelController extends BaseController {
119 125 if (null == dto) {
120 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 136 @GetMapping("/get_services/{deviceProfileId}")
... ...
... ... @@ -114,45 +114,6 @@ public class TkDeviceScriptController extends BaseController {
114 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 118 @GetMapping("{id}")
158 119 @ApiOperation("详情")
... ...
... ... @@ -32,6 +32,7 @@ import org.thingsboard.server.common.data.id.DeviceProfileId;
32 32 import org.thingsboard.server.common.data.id.EntityId;
33 33 import org.thingsboard.server.common.data.id.TenantId;
34 34 import org.thingsboard.server.common.data.kv.*;
  35 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
35 36 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
36 37 import org.thingsboard.server.common.msg.queue.ServiceType;
37 38 import org.thingsboard.server.common.msg.queue.TbCallback;
... ... @@ -235,13 +236,13 @@ public class DefaultTelemetrySubscriptionService extends AbstractSubscriptionSer
235 236
236 237 //Thingskit function
237 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 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 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 246 addVoidCallback(saveFuture, callback);
246 247 // addWsCallback(saveFuture, success -> onAttributesUpdate(tenantId, entityId, scope, attributes, notifyDevice));
247 248 }
... ...
... ... @@ -39,7 +39,7 @@ public interface InternalTelemetryService extends RuleEngineTelemetryService {
39 39 void saveAndNotifyInternal(TenantId tenantId, EntityId entityId, String scope, List<AttributeKvEntry> attributes, boolean notifyDevice, FutureCallback<Void> callback);
40 40
41 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 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 22 import org.thingsboard.server.common.data.page.PageData;
23 23 import org.thingsboard.server.common.data.page.TimePageLink;
24 24 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto;
  25 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
25 26 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
26 27
27 28 import java.util.List;
... ... @@ -31,7 +32,7 @@ import java.util.List;
31 32 */
32 33 public interface TkEventsService {
33 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 18 import io.swagger.annotations.ApiModel;
19 19 import io.swagger.annotations.ApiModelProperty;
20 20 import lombok.Data;
  21 +import org.thingsboard.server.common.data.yunteng.dto.TkThingsModel;
21 22
22 23 import javax.validation.Valid;
23 24 import java.io.Serializable;
... ... @@ -38,4 +39,9 @@ public class DeviceProfileData implements Serializable {
38 39 @ApiModelProperty(position = 4, value = "JSON array of alarm rules configuration per device profile")
39 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 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 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 582 throw new TkDataValidationException(ErrorMessage.INVALID_TOPIC.getMessage());
583 583 }
584 584 String[] eventInfo = topicInfo[1].split("/");
585   - if(null == eventInfo || eventInfo.length !=3){
  585 + if(null == eventInfo || eventInfo.length !=2){
586 586 throw new TkDataValidationException(ErrorMessage.INVALID_TOPIC.getMessage());
587 587 }
588 588 reportActivityInternal(sessionInfo);
... ... @@ -593,7 +593,7 @@ public class DefaultTransportService implements TransportService {
593 593 DeviceId deviceId = new DeviceId(new UUID(sessionInfo.getDeviceIdMSB(), sessionInfo.getDeviceIdLSB()));
594 594 metaData.putValue("deviceName", sessionInfo.getDeviceName());
595 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 597 metaData.putValue("device_profile_id", deviceProfileId.toString());
598 598 metaData.putValue("deviceId",eventInfo[0]);
599 599 metaData.putValue("event_identifier", eventInfo[1]);
... ...
... ... @@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils;
8 8 import org.springframework.beans.BeanUtils;
9 9 import org.springframework.stereotype.Service;
10 10 import org.springframework.transaction.annotation.Transactional;
  11 +import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
11 12 import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
12 13 import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException;
13 14 import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
... ... @@ -195,8 +196,16 @@ public class ThingsModelServiceImpl
195 196
196 197 @Override
197 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 211 private boolean checkIdentifier(
... ...
... ... @@ -25,10 +25,13 @@ import org.thingsboard.server.common.data.id.TenantId;
25 25 import org.thingsboard.server.common.data.page.PageData;
26 26 import org.thingsboard.server.common.data.page.TimePageLink;
27 27 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto;
  28 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
28 29 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
  30 +import org.thingsboard.server.dao.attributes.AttributeUtils;
29 31 import org.thingsboard.server.dao.yunteng.event.TkEventsService;
30 32
31 33 import java.util.List;
  34 +import java.util.stream.Collectors;
32 35
33 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 56 }
54 57
55 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 63 return Futures.allAsList(saveFutures);
61 64 }
62 65
... ...
... ... @@ -44,11 +44,8 @@ public class EventUtils {
44 44 public static void validate(TkEventKvEntry kvEntry) {
45 45 if (kvEntry == null) {
46 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 22 import org.thingsboard.server.common.data.page.PageData;
23 23 import org.thingsboard.server.common.data.page.TimePageLink;
24 24 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto;
  25 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
25 26 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
26 27
  28 +import java.util.List;
  29 +
27 30 /**
28 31 * @author Andrew Shvayka
29 32 */
... ... @@ -31,6 +34,6 @@ public interface EventsDao {
31 34
32 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 26 import org.thingsboard.server.common.data.page.PageData;
27 27 import org.thingsboard.server.common.data.page.TimePageLink;
28 28 import org.thingsboard.server.common.data.yunteng.dto.TkEventKvDto;
  29 +import org.thingsboard.server.common.data.yunteng.dto.TkEventKvEntry;
29 30 import org.thingsboard.server.common.data.yunteng.enums.DeviceEventTypeEnum;
30 31 import org.thingsboard.server.common.stats.StatsFactory;
31 32 import org.thingsboard.server.dao.DaoUtil;
... ... @@ -40,6 +41,7 @@ import org.thingsboard.server.dao.yunteng.jpa.entity.events.TkEventKvEntity;
40 41 import javax.annotation.PostConstruct;
41 42 import javax.annotation.PreDestroy;
42 43 import java.util.Comparator;
  44 +import java.util.List;
43 45 import java.util.function.Function;
44 46
45 47 @Component
... ... @@ -111,10 +113,10 @@ public class JpaEventDao extends JpaAbstractDaoListeningExecutorService implemen
111 113
112 114
113 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 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 120 entity.setDeviceProfileId(profileId.getId());
119 121 entity.setEventValue(eventData);
120 122 return addToQueue(entity);
... ...
... ... @@ -3,8 +3,10 @@ package org.thingsboard.server.dao.yunteng.service;
3 3 import com.fasterxml.jackson.databind.JsonNode;
4 4 import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO;
5 5 import org.thingsboard.server.common.data.yunteng.dto.ThingsModelDTO;
  6 +import org.thingsboard.server.common.data.yunteng.dto.TkThingsModel;
6 7 import org.thingsboard.server.common.data.yunteng.enums.FunctionTypeEnum;
7 8 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
  9 +import org.thingsboard.server.dao.yunteng.entities.TkThingsModelEntity;
8 10
9 11 import java.util.List;
10 12 import java.util.Map;
... ... @@ -26,5 +28,5 @@ public interface ThingsModelService {
26 28
27 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 45 void saveAndNotify(TenantId tenantId, EntityId entityId, String scope, List<AttributeKvEntry> attributes, boolean notifyDevice, FutureCallback<Void> callback);
46 46
47 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 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 19 import org.thingsboard.rule.engine.api.*;
20 20 import org.thingsboard.rule.engine.api.util.TbNodeUtils;
21 21 import org.thingsboard.rule.engine.telemetry.TelemetryNodeCallback;
  22 +import org.thingsboard.server.common.data.DeviceProfile;
22 23 import org.thingsboard.server.common.data.id.DeviceProfileId;
  24 +import org.thingsboard.server.common.data.id.TenantId;
23 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 30 import org.thingsboard.server.common.msg.TbMsg;
26 31 import org.thingsboard.server.common.msg.session.SessionMsgType;
27 32
  33 +import java.util.ArrayList;
  34 +import java.util.List;
28 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 41 @Slf4j
33 42 @RuleNode(
... ... @@ -57,17 +66,32 @@ public class TkMsgEventNode implements TbNode {
57 66 ctx.tellFailure(msg, new IllegalArgumentException("Unsupported msg type: " + msg.getType()));
58 67 return;
59 68 }
60   - String eventIdentifier = msg.getMetaData().getValue(EVENT_IDENTIFIER_COLUMN);
61 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 88 ////TODO: 验证事件类型、事件标识符和数据建是否与产品物模型中的事件匹配
63 89 long ts = System.currentTimeMillis();
64 90 String src = msg.getData();
65   - DeviceEventTypeEnum eventType = DeviceEventTypeEnum.valueOf(msg.getMetaData().getValue(EVENT_TYPE_COLUMN));
66 91 ctx.getTelemetryService().saveAndNotify(
67   - ctx.getTenantId(),new DeviceProfileId(UUID.fromString(deviceProfileId)),
  92 + tenantId,profileId,
68 93 msg.getOriginator(),
69   - eventIdentifier,
70   - eventType,
  94 + entryList,
71 95 src,ts,
72 96 new TelemetryNodeCallback(ctx, msg)
73 97 );
... ...