Commit c84a5caab62a16556c82906fc029d467a52b4f33
Merge branch '20200307' into 'master'
fix: 网关子设备与网关设备的关联关系管理 See merge request huang/thingsboard3.3.2!59
Showing
2 changed files
with
54 additions
and
54 deletions
@@ -50,6 +50,7 @@ import java.util.HashMap; | @@ -50,6 +50,7 @@ import java.util.HashMap; | ||
50 | import java.util.List; | 50 | import java.util.List; |
51 | import java.util.Optional; | 51 | import java.util.Optional; |
52 | import java.util.UUID; | 52 | import java.util.UUID; |
53 | +import java.util.concurrent.ExecutionException; | ||
53 | 54 | ||
54 | import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.*; | 55 | import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.*; |
55 | 56 | ||
@@ -66,7 +67,7 @@ public class YtDeviceController extends BaseController { | @@ -66,7 +67,7 @@ public class YtDeviceController extends BaseController { | ||
66 | @ApiOperation("创建|编辑") | 67 | @ApiOperation("创建|编辑") |
67 | @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") | 68 | @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
68 | public ResponseEntity<DeviceDTO> saveDevice( | 69 | public ResponseEntity<DeviceDTO> saveDevice( |
69 | - @Validated(AddGroup.class) @RequestBody DeviceDTO deviceDTO) throws ThingsboardException { | 70 | + @Validated(AddGroup.class) @RequestBody DeviceDTO deviceDTO) throws ThingsboardException, ExecutionException, InterruptedException { |
70 | String currentTenantId = getCurrentUser().getCurrentTenantId(); | 71 | String currentTenantId = getCurrentUser().getCurrentTenantId(); |
71 | boolean enable = deviceService.validateFormdata(currentTenantId, deviceDTO); | 72 | boolean enable = deviceService.validateFormdata(currentTenantId, deviceDTO); |
72 | if (!enable) { | 73 | if (!enable) { |
@@ -74,86 +75,85 @@ public class YtDeviceController extends BaseController { | @@ -74,86 +75,85 @@ public class YtDeviceController extends BaseController { | ||
74 | } | 75 | } |
75 | DeviceDTO newDeviceDTO = null; | 76 | DeviceDTO newDeviceDTO = null; |
76 | boolean isIncludeRelation = false; | 77 | boolean isIncludeRelation = false; |
77 | - String gateWayDeviceId = deviceDTO.getGatewayId(); | 78 | + |
79 | + String gatewayId = deviceDTO.getGatewayId(); | ||
78 | DeviceDTO gateWayDevice = null; | 80 | DeviceDTO gateWayDevice = null; |
79 | - if (StringUtils.isNotEmpty(gateWayDeviceId)) { | 81 | + if (StringUtils.isNotEmpty(gatewayId)) { |
80 | gateWayDevice = | 82 | gateWayDevice = |
81 | deviceService.checkDeviceByTenantIdAndDeviceId( | 83 | deviceService.checkDeviceByTenantIdAndDeviceId( |
82 | - getCurrentUser().getCurrentTenantId(), gateWayDeviceId); | ||
83 | - } | ||
84 | - if (deviceDTO.getDeviceType().equals(DeviceTypeEnum.SENSOR) | ||
85 | - && StringUtils.isNotEmpty(gateWayDeviceId) | ||
86 | - && null == deviceDTO.getId()) { | 84 | + getCurrentUser().getCurrentTenantId(), gatewayId); |
85 | + | ||
87 | // 第一步判断该网关设备是否存在于该租户下面 | 86 | // 第一步判断该网关设备是否存在于该租户下面 |
88 | if (null == gateWayDevice) { | 87 | if (null == gateWayDevice) { |
89 | throw new YtDataValidationException( | 88 | throw new YtDataValidationException( |
90 | - ErrorMessage.DEVICE_NOT_EXISTENCE_IN_TENANT.getMessage()); | 89 | + ErrorMessage.DEVICE_NOT_EXISTENCE_IN_TENANT.getMessage()); |
91 | } | 90 | } |
92 | - try { | ||
93 | - // 第二步判断网关的关联设备是否已包含该设备 | ||
94 | - EntityId entityId = EntityIdFactory.getByTypeAndId("DEVICE", gateWayDevice.getTbDeviceId()); | 91 | + } |
92 | + | ||
93 | + /**子设备编辑时,TB中已经存在关联关系则值更新设备表信息*/ | ||
94 | + String selfTbDeviceIdStr = deviceDTO.getTbDeviceId(); | ||
95 | + if (selfTbDeviceIdStr != null | ||
96 | + && deviceDTO.getDeviceType().equals(DeviceTypeEnum.SENSOR) | ||
97 | + && StringUtils.isNotEmpty(gatewayId)) { | ||
98 | + | ||
99 | + | ||
100 | + // 第二步判断网关子设备是否已关联到网关设备 | ||
101 | + EntityId entityId = EntityIdFactory.getByTypeAndId("DEVICE", selfTbDeviceIdStr);//gateWayDevice.getTbDeviceId() | ||
95 | List<EntityRelationInfo> list = | 102 | List<EntityRelationInfo> list = |
96 | relationService.findInfoByTo(getTenantId(), entityId, RelationTypeGroup.COMMON).get(); | 103 | relationService.findInfoByTo(getTenantId(), entityId, RelationTypeGroup.COMMON).get(); |
97 | - int count = 0; | ||
98 | - int exit = 0; | 104 | + |
99 | for (EntityRelationInfo entityRelationInfo : list) { | 105 | for (EntityRelationInfo entityRelationInfo : list) { |
100 | - count++; | ||
101 | - if (entityRelationInfo.getToName().equals(deviceDTO.getName())) { | ||
102 | - exit = count; | 106 | + if (entityRelationInfo.getTo().getId().equals(selfTbDeviceIdStr) |
107 | + && entityRelationInfo.getFrom().getId().equals(gatewayId)) { | ||
108 | + deviceDTO.setTbDeviceId(entityRelationInfo.getTo().toString()); | ||
109 | + newDeviceDTO = | ||
110 | + deviceService.insertOrUpdate(getCurrentUser().getCurrentTenantId(), deviceDTO); | ||
111 | + isIncludeRelation = true; | ||
103 | break; | 112 | break; |
104 | } | 113 | } |
105 | } | 114 | } |
106 | - // 第三步如果包含,只需创建我方设备,不需调用TB创建设备,不包含,走正常逻辑,然后将关联关系创建 | ||
107 | - if (count > 0) { | ||
108 | - EntityRelationInfo entityRelationInfo = list.get(exit); | ||
109 | - deviceDTO.setTbDeviceId(entityRelationInfo.getTo().toString()); | ||
110 | - newDeviceDTO = | ||
111 | - deviceService.insertOrUpdate(getCurrentUser().getCurrentTenantId(), deviceDTO); | ||
112 | - isIncludeRelation = true; | ||
113 | - } | ||
114 | - } catch (Exception e) { | ||
115 | - handleException(e); | ||
116 | - } | ||
117 | } | 115 | } |
116 | + | ||
117 | + | ||
118 | + /**需要更新设备表和关联关系表*/ | ||
118 | if (!isIncludeRelation) { | 119 | if (!isIncludeRelation) { |
119 | Device tbDevice = buildTbDeviceFromDeviceDTO(getCurrentUser().getTenantId(), deviceDTO); | 120 | Device tbDevice = buildTbDeviceFromDeviceDTO(getCurrentUser().getTenantId(), deviceDTO); |
120 | 121 | ||
121 | - DeviceId tbDeviceId = updateTbDevice(tbDevice, deviceDTO.getDeviceToken()); | ||
122 | - String deviceId = tbDeviceId.getId().toString(); | ||
123 | - deviceDTO.setTbDeviceId(deviceId); | 122 | + DeviceId selfTbId = updateTbDevice(tbDevice, deviceDTO.getDeviceToken()); |
123 | + selfTbDeviceIdStr = selfTbId.getId().toString(); | ||
124 | + deviceDTO.setTbDeviceId( selfTbDeviceIdStr); | ||
124 | newDeviceDTO = deviceService.insertOrUpdate(getCurrentUser().getCurrentTenantId(), deviceDTO); | 125 | newDeviceDTO = deviceService.insertOrUpdate(getCurrentUser().getCurrentTenantId(), deviceDTO); |
126 | + | ||
125 | if (deviceDTO.getDeviceType().equals(DeviceTypeEnum.SENSOR) | 127 | if (deviceDTO.getDeviceType().equals(DeviceTypeEnum.SENSOR) |
126 | - && StringUtils.isNotEmpty(gateWayDeviceId) | ||
127 | - && gateWayDevice != null) { | ||
128 | - if (null != deviceDTO.getId()) { | ||
129 | - // 删除原來的关联关系 | ||
130 | - List<DeviceDTO> list = | ||
131 | - deviceService.findGateWayDeviceByTbDeviceId( | ||
132 | - getCurrentUser().getCurrentTenantId(), deviceId); | ||
133 | - if (null == list || list.size() < 1) { | ||
134 | - throw new YtDataValidationException(ErrorMessage.DEVICE_RELATION_IS_ABSENT.getMessage()); | ||
135 | - } | 128 | + && StringUtils.isNotEmpty(selfTbDeviceIdStr)) { |
129 | + // 删除原來的关联关系 | ||
130 | + List<DeviceDTO> list = | ||
131 | + deviceService.findGateWayDeviceByTbDeviceId( | ||
132 | + getCurrentUser().getCurrentTenantId(), selfTbDeviceIdStr); | ||
133 | + if (null != list && list.size() > 0) { | ||
136 | DeviceId form = new DeviceId(UUID.fromString(list.get(0).getTbDeviceId())); | 134 | DeviceId form = new DeviceId(UUID.fromString(list.get(0).getTbDeviceId())); |
137 | - DeviceId to = new DeviceId(UUID.fromString(deviceId)); | ||
138 | EntityRelation relation = | 135 | EntityRelation relation = |
139 | - new EntityRelation( | ||
140 | - form, to, FastIotConstants.Relation.relationType, RelationTypeGroup.COMMON); | 136 | + new EntityRelation( |
137 | + form, selfTbId, FastIotConstants.Relation.relationType, RelationTypeGroup.COMMON); | ||
141 | boolean found = | 138 | boolean found = |
142 | - relationService.deleteRelation( | ||
143 | - getTenantId(), | ||
144 | - form, | ||
145 | - to, | ||
146 | - FastIotConstants.Relation.relationType, | ||
147 | - RelationTypeGroup.COMMON); | 139 | + relationService.deleteRelation( |
140 | + getTenantId(), | ||
141 | + form, | ||
142 | + selfTbId, | ||
143 | + FastIotConstants.Relation.relationType, | ||
144 | + RelationTypeGroup.COMMON); | ||
148 | 145 | ||
149 | if (!found) { | 146 | if (!found) { |
150 | throw new ThingsboardException( | 147 | throw new ThingsboardException( |
151 | - "Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); | 148 | + "Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); |
152 | } | 149 | } |
153 | sendRelationNotificationMsg( | 150 | sendRelationNotificationMsg( |
154 | - getTenantId(), relation, EdgeEventActionType.RELATION_DELETED); | 151 | + getTenantId(), relation, EdgeEventActionType.RELATION_DELETED); |
152 | + } | ||
153 | + | ||
154 | + if(gateWayDevice != null){ | ||
155 | + addRelation(getTenantId(), gateWayDevice.getTbDeviceId(), selfTbDeviceIdStr); | ||
155 | } | 156 | } |
156 | - addRelation(getTenantId(), gateWayDevice.getTbDeviceId(), deviceId); | ||
157 | } | 157 | } |
158 | } | 158 | } |
159 | return ResponseEntity.ok(newDeviceDTO); | 159 | return ResponseEntity.ok(newDeviceDTO); |
@@ -83,7 +83,7 @@ class ReactState { | @@ -83,7 +83,7 @@ class ReactState { | ||
83 | } | 83 | } |
84 | 84 | ||
85 | 85 | ||
86 | - if(matched && conditions != null ){ | 86 | + if(matched && conditions.size() >0 ){ |
87 | matched = false; | 87 | matched = false; |
88 | for(DoCondition item:conditions){ | 88 | for(DoCondition item:conditions){ |
89 | TriggerState conditionState = getOrCreateConditionState(item.getEntityId(),item.getTriggerCondition()); | 89 | TriggerState conditionState = getOrCreateConditionState(item.getEntityId(),item.getTriggerCondition()); |