Commit 889e4ec4118ba829852204b5bfc63775616bc5cf

Authored by 黄 x
1 parent 9d89fc70

fix: device profile some bug

... ... @@ -907,10 +907,12 @@ public abstract class BaseController {
907 907 * 构建设备配置的配置数据
908 908 * @param transportType 产品的通信协议
909 909 * @param deviceProfileData 空的设备配置数据
910   - * @param transportConfiguration 传输配置
911 910 * @param scriptText 自定义数据协议的解析脚本
912 911 */
913   - protected void buildDeviceProfileData(String transportType,DeviceProfileData deviceProfileData,DeviceProfileTransportConfiguration transportConfiguration,String scriptText) {
  912 + protected DeviceProfileData buildDeviceProfileData(String transportType,DeviceProfileData deviceProfileData,String scriptText) {
  913 + if(null == deviceProfileData){
  914 + deviceProfileData = new DeviceProfileData();
  915 + }
914 916 deviceProfileData.setConfiguration(new DefaultDeviceProfileConfiguration());
915 917 deviceProfileData.setProvisionConfiguration(new DisabledDeviceProfileProvisionConfiguration(null));
916 918
... ... @@ -918,13 +920,13 @@ public abstract class BaseController {
918 920 if(transportType ==null || DeviceTransportType.DEFAULT.name().equals(transportType)){
919 921 deviceProfileData.setTransportConfiguration(new DefaultDeviceProfileTransportConfiguration());
920 922 }else if(DeviceTransportType.TCP.name().equals(transportType)){
921   - YtTcpDeviceProfileTransportConfiguration tcpDeviceProfileTransportConfiguration = (YtTcpDeviceProfileTransportConfiguration) transportConfiguration;
922   - String scriptId = tcpDeviceProfileTransportConfiguration.getScriptId();
923   - tcpDeviceProfileTransportConfiguration.setPingText(scriptText);
  923 + YtTcpDeviceProfileTransportConfiguration tcpDeviceProfileTransportConfiguration = (YtTcpDeviceProfileTransportConfiguration) deviceProfileData.getTransportConfiguration();
  924 + tcpDeviceProfileTransportConfiguration.setScriptText(scriptText);
924 925 deviceProfileData.setTransportConfiguration(tcpDeviceProfileTransportConfiguration);
925 926 }else{
926   - deviceProfileData.setTransportConfiguration(transportConfiguration);
  927 + deviceProfileData.setTransportConfiguration(deviceProfileData.getTransportConfiguration());
927 928 }
  929 + return deviceProfileData;
928 930
929 931 }
930 932 }
... ...
... ... @@ -19,7 +19,6 @@ import org.thingsboard.server.common.data.exception.ThingsboardException;
19 19 import org.thingsboard.server.common.data.id.DeviceProfileId;
20 20 import org.thingsboard.server.common.data.id.RuleChainId;
21 21 import org.thingsboard.server.common.data.id.TenantId;
22   -import org.thingsboard.server.common.data.page.PageLink;
23 22 import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
24 23 import org.thingsboard.server.common.data.rule.RuleChain;
25 24 import org.thingsboard.server.common.data.yunteng.common.DeleteGroup;
... ... @@ -49,222 +48,244 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.
49 48 @RequestMapping("api/yt/device_profile")
50 49 @Api(tags = {"设备配置管理"})
51 50 public class YtDeviceProfileController extends BaseController {
52   - private final YtDeviceProfileService ytDeviceProfileService;
53   - private final YtDeviceScriptService javaScriptService;
54   - @PostMapping()
55   - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:deviceProfile:post','api:yt:deviceProfile:update'})")
56   - @ApiOperation("创建 | 编辑")
57   - public ResponseEntity<DeviceProfileDTO> saveDeviceProfile(
58   - @RequestBody DeviceProfileDTO deviceProfileDTO) throws ThingsboardException {
59   -
60   - boolean created = deviceProfileDTO.getId() == null;
61   -
62   - /**
63   - * 业务流程
64   - * 1/3.验证业务平台中表单数据的合法性
65   - * 2/3.处理TB业务逻辑
66   - * 3/3.处理业务平台的业务逻辑
67   - */
68   - String tenantId = getCurrentUser().getCurrentTenantId();
69   - deviceProfileDTO.setTenantId(tenantId);
70   - DeviceProfile tbDeviceProfile = buildTbDeviceProfileFromDeviceProfileDTO(deviceProfileDTO);
71   -
72   - updateTbDeviceProfile(tbDeviceProfile, created);
73   -
74   - ytDeviceProfileService.insertOrUpdate(deviceProfileDTO);
75   -
76   - return ResponseEntity.ok(deviceProfileDTO);
  51 + private final YtDeviceProfileService ytDeviceProfileService;
  52 + private final YtDeviceScriptService javaScriptService;
  53 +
  54 + @PostMapping()
  55 + @PreAuthorize(
  56 + "@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:deviceProfile:post','api:yt:deviceProfile:update'})")
  57 + @ApiOperation("创建 | 编辑")
  58 + public ResponseEntity<DeviceProfileDTO> saveDeviceProfile(
  59 + @RequestBody DeviceProfileDTO deviceProfileDTO) throws ThingsboardException {
  60 +
  61 + boolean created = deviceProfileDTO.getId() == null;
  62 +
  63 + /** 业务流程 1/3.验证业务平台中表单数据的合法性 2/3.处理TB业务逻辑 3/3.处理业务平台的业务逻辑 */
  64 + String tenantId = getCurrentUser().getCurrentTenantId();
  65 + deviceProfileDTO.setTenantId(tenantId);
  66 + DeviceProfile tbDeviceProfile = buildTbDeviceProfileFromDeviceProfileDTO(deviceProfileDTO);
  67 +
  68 + DeviceProfile saveDeviceProfile = updateTbDeviceProfile(tbDeviceProfile, created);
  69 + deviceProfileDTO.setTbProfileId(saveDeviceProfile.getId().toString());
  70 + ytDeviceProfileService.insertOrUpdate(deviceProfileDTO);
  71 +
  72 + return ResponseEntity.ok(deviceProfileDTO);
  73 + }
  74 +
  75 + /**
  76 + * 更新thingsboard的设备配置信息
  77 + *
  78 + * @param deviceProfile 设备配置
  79 + * @param created 新建设备
  80 + * @throws ThingsboardException
  81 + */
  82 + private DeviceProfile updateTbDeviceProfile(DeviceProfile deviceProfile, boolean created)
  83 + throws ThingsboardException {
  84 + boolean isFirmwareChanged = false;
  85 + boolean isSoftwareChanged = false;
  86 + if (!created) {
  87 + DeviceProfile oldDeviceProfile =
  88 + deviceProfileService.findDeviceProfileById(getTenantId(), deviceProfile.getId());
  89 + if (!Objects.equals(deviceProfile.getFirmwareId(), oldDeviceProfile.getFirmwareId())) {
  90 + isFirmwareChanged = true;
  91 + }
  92 + if (!Objects.equals(deviceProfile.getSoftwareId(), oldDeviceProfile.getSoftwareId())) {
  93 + isSoftwareChanged = true;
  94 + }
  95 + if (FastIotConstants.ASSERT_DEFAULT_NAME.equals(oldDeviceProfile.getName())
  96 + && !Objects.equals(deviceProfile.getName(), oldDeviceProfile.getName())) {
  97 + throw new YtDataValidationException(
  98 + ErrorMessage.ASSERT_DEFAULT_NAME_NO_CHANGED.getMessage());
  99 + }
77 100 }
78 101
79   - /**
80   - * 更新thingsboard的设备配置信息
81   - *
82   - * @param deviceProfile 设备配置
83   - * @param created 新建设备
84   - * @throws ThingsboardException
85   - */
86   - private DeviceProfile updateTbDeviceProfile(DeviceProfile deviceProfile, boolean created) throws ThingsboardException {
87   - boolean isFirmwareChanged = false;
88   - boolean isSoftwareChanged = false;
89   - if (!created) {
90   - DeviceProfile oldDeviceProfile = deviceProfileService.findDeviceProfileById(getTenantId(), deviceProfile.getId());
91   - if (!Objects.equals(deviceProfile.getFirmwareId(), oldDeviceProfile.getFirmwareId())) {
92   - isFirmwareChanged = true;
93   - }
94   - if (!Objects.equals(deviceProfile.getSoftwareId(), oldDeviceProfile.getSoftwareId())) {
95   - isSoftwareChanged = true;
96   - }
97   - if (FastIotConstants.ASSERT_DEFAULT_NAME.equals(oldDeviceProfile.getName()) && !Objects.equals(deviceProfile.getName(), oldDeviceProfile.getName())) {
98   - throw new YtDataValidationException(ErrorMessage.ASSERT_DEFAULT_NAME_NO_CHANGED.getMessage());
99   - }
100   - }
101   -
102   - DeviceProfile savedDeviceProfile = checkNotNull(deviceProfileService.saveDeviceProfile(deviceProfile));
103   -
104   - tbClusterService.onDeviceProfileChange(savedDeviceProfile, null);
105   - tbClusterService.broadcastEntityStateChangeEvent(deviceProfile.getTenantId(), savedDeviceProfile.getId(),
106   - created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
107   -
108   - logEntityAction(savedDeviceProfile.getId(), savedDeviceProfile,
109   - null,
110   - created ? ActionType.ADDED : ActionType.UPDATED, null);
111   -
112   - otaPackageStateService.update(savedDeviceProfile, isFirmwareChanged, isSoftwareChanged);
113   -
114   - sendEntityNotificationMsg(getTenantId(), savedDeviceProfile.getId(),
115   - deviceProfile.getId() == null ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED);
116   - return savedDeviceProfile;
  102 + DeviceProfile savedDeviceProfile =
  103 + checkNotNull(deviceProfileService.saveDeviceProfile(deviceProfile));
  104 +
  105 + tbClusterService.onDeviceProfileChange(savedDeviceProfile, null);
  106 + tbClusterService.broadcastEntityStateChangeEvent(
  107 + deviceProfile.getTenantId(),
  108 + savedDeviceProfile.getId(),
  109 + created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
  110 +
  111 + logEntityAction(
  112 + savedDeviceProfile.getId(),
  113 + savedDeviceProfile,
  114 + null,
  115 + created ? ActionType.ADDED : ActionType.UPDATED,
  116 + null);
  117 +
  118 + otaPackageStateService.update(savedDeviceProfile, isFirmwareChanged, isSoftwareChanged);
  119 +
  120 + sendEntityNotificationMsg(
  121 + getTenantId(),
  122 + savedDeviceProfile.getId(),
  123 + deviceProfile.getId() == null ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED);
  124 + return savedDeviceProfile;
  125 + }
  126 +
  127 + @GetMapping("{id}")
  128 + @ApiOperation("详情")
  129 + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:deviceProfile:get'})")
  130 + public ResponseEntity<DeviceProfileDTO> getDevice(@PathVariable("id") String id)
  131 + throws ThingsboardException {
  132 + return ResponseEntity.of(
  133 + ytDeviceProfileService.getDeviceProfile(getCurrentUser().getCurrentTenantId(), id));
  134 + }
  135 +
  136 + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
  137 + @GetMapping(params = {PAGE_SIZE, PAGE})
  138 + @ApiOperation("查询")
  139 + public YtPageData<DeviceProfileDTO> pageDeviceProfile(
  140 + @RequestParam(PAGE_SIZE) int pageSize,
  141 + @RequestParam(PAGE) int page,
  142 + @RequestParam(value = "name", required = false) String name,
  143 + @RequestParam(value = "transportType", required = false) String transportType,
  144 + @RequestParam(value = ORDER_FILED, required = false) String orderBy,
  145 + @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType)
  146 + throws ThingsboardException {
  147 + return ytDeviceProfileService.page(
  148 + page,
  149 + pageSize,
  150 + orderBy,
  151 + orderType,
  152 + getCurrentUser().getCurrentTenantId(),
  153 + name,
  154 + transportType);
  155 + }
  156 +
  157 + @GetMapping("/me/list")
  158 + @ApiOperation("选项列表")
  159 + public ResponseEntity listDeviceProfile() throws ThingsboardException {
  160 + List<DeviceProfileDTO> results =
  161 + ytDeviceProfileService.findDeviceProfile(getCurrentUser().getCurrentTenantId(), null);
  162 + return ResponseEntity.ok(results);
  163 + }
  164 +
  165 + @DeleteMapping
  166 + @ApiOperation("删除")
  167 + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:deviceProfile:delete'})")
  168 + public void deleteDevices(@Validated({DeleteGroup.class}) @RequestBody DeleteDTO deleteDTO)
  169 + throws ThingsboardException {
  170 + String tenantId = getCurrentUser().getCurrentTenantId();
  171 + ytDeviceProfileService.checkDeviceProfiles(tenantId, deleteDTO.getIds());
  172 +
  173 + for (String id : deleteDTO.getIds()) {
  174 + deleteTbDeviceProfile(id);
117 175 }
  176 + ytDeviceProfileService.deleteDeviceProfiles(tenantId, deleteDTO.getIds());
  177 + }
118 178
119   - @GetMapping("{id}")
120   - @ApiOperation("详情")
121   - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:deviceProfile:get'})")
122   - public ResponseEntity<DeviceProfileDTO> getDevice(@PathVariable("id") String id) throws ThingsboardException {
123   - return ResponseEntity.of(ytDeviceProfileService.getDeviceProfile(getCurrentUser().getCurrentTenantId(), id));
124   - }
125   - @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
126   - @GetMapping(params = {PAGE_SIZE, PAGE})
127   - @ApiOperation("查询")
128   - public YtPageData<DeviceProfileDTO> pageDeviceProfile(
129   - @RequestParam(PAGE_SIZE) int pageSize,
130   - @RequestParam(PAGE) int page,
131   - @RequestParam(value = "name", required = false) String name,
132   - @RequestParam(value = "transportType", required = false) String transportType,
133   - @RequestParam(value = ORDER_FILED, required = false) String orderBy,
134   - @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType) throws ThingsboardException {
135   - return ytDeviceProfileService.page(page,pageSize,orderBy,orderType, getCurrentUser().getCurrentTenantId(),name, transportType);
136   - }
137   -
138   - @GetMapping("/me/list")
139   - @ApiOperation("选项列表")
140   - public ResponseEntity listDeviceProfile() throws ThingsboardException {
141   - List<DeviceProfileDTO> results = ytDeviceProfileService.findDeviceProfile(getCurrentUser().getCurrentTenantId());
142   - return ResponseEntity.ok(results);
143   - }
  179 + private void deleteTbDeviceProfile(String profileId) throws ThingsboardException {
  180 + DeviceProfileDTO dto = ytDeviceProfileService.findDeviceProfileById(getCurrentUser().getCurrentTenantId(), profileId);
  181 + if(null != dto){
  182 + DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(dto.getTbProfileId()));
  183 + DeviceProfile deviceProfile = checkDeviceProfileId(deviceProfileId, Operation.DELETE);
  184 + deviceProfileService.deleteDeviceProfile(getTenantId(), deviceProfileId);
144 185
145   - @DeleteMapping
146   - @ApiOperation("删除")
147   - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:deviceProfile:delete'})")
148   - public void deleteDevices(@Validated({DeleteGroup.class}) @RequestBody DeleteDTO deleteDTO) throws ThingsboardException {
149   - String tenantId = getCurrentUser().getCurrentTenantId();
150   - ytDeviceProfileService.checkDeviceProfiles(tenantId, deleteDTO.getIds());
  186 + tbClusterService.onDeviceProfileDelete(deviceProfile, null);
  187 + tbClusterService.broadcastEntityStateChangeEvent(
  188 + deviceProfile.getTenantId(), deviceProfile.getId(), ComponentLifecycleEvent.DELETED);
151 189
152   - for (String id : deleteDTO.getIds()) {
153   - deleteTbDeviceProfile(id);
154   - }
155   - ytDeviceProfileService.deleteDeviceProfiles(tenantId,deleteDTO.getIds());
  190 + logEntityAction(
  191 + deviceProfileId, deviceProfile, null, ActionType.DELETED, null, deviceProfileId);
156 192
  193 + sendEntityNotificationMsg(getTenantId(), deviceProfile.getId(), EdgeEventActionType.DELETED);
157 194 }
158   -
159   - private void deleteTbDeviceProfile(String strDeviceProfileId) throws ThingsboardException {
160   - DeviceProfileId deviceProfileId = new DeviceProfileId(toUUID(strDeviceProfileId));
161   - DeviceProfile deviceProfile = checkDeviceProfileId(deviceProfileId, Operation.DELETE);
162   - deviceProfileService.deleteDeviceProfile(getTenantId(), deviceProfileId);
163   -
164   - tbClusterService.onDeviceProfileDelete(deviceProfile, null);
165   - tbClusterService.broadcastEntityStateChangeEvent(deviceProfile.getTenantId(), deviceProfile.getId(), ComponentLifecycleEvent.DELETED);
166   -
167   - logEntityAction(deviceProfileId, deviceProfile,
168   - null,
169   - ActionType.DELETED, null, deviceProfileId);
170   -
171   - sendEntityNotificationMsg(getTenantId(), deviceProfile.getId(), EdgeEventActionType.DELETED);
  195 + }
  196 +
  197 + @GetMapping("/me/default")
  198 + @ApiOperation("默认设备配置")
  199 + public ResponseEntity<DeviceProfile> findCurrentTenantDeviceProfiles()
  200 + throws ThingsboardException {
  201 + DeviceProfile result =
  202 + deviceProfileService.findDefaultDeviceProfile(getCurrentUser().getTenantId());
  203 + return ResponseEntity.ok(result);
  204 + }
  205 +
  206 + @PostMapping("/import")
  207 + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:deviceProfile:import'})")
  208 + @ApiOperation("导入配置")
  209 + public ResponseEntity<String> importDeviceProfile() {
  210 + // TODO 实现的业务功能
  211 + return ResponseEntity.ok("");
  212 + }
  213 +
  214 + @PostMapping("/export")
  215 + @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:deviceProfile:export'})")
  216 + @ApiOperation("导出")
  217 + public ResponseEntity<String> exportDeviceProfile() {
  218 + // TODO 实现的业务功能
  219 + return ResponseEntity.ok("");
  220 + }
  221 +
  222 + /**
  223 + * 构造调用TBDeviceProfile需要的参数
  224 + *
  225 + * @param deviceProfileDTO 页面接收的参数
  226 + * @return 封装好的TBDeviceProfile
  227 + */
  228 + private DeviceProfile buildTbDeviceProfileFromDeviceProfileDTO(DeviceProfileDTO deviceProfileDTO)
  229 + throws ThingsboardException {
  230 + DeviceProfile tbDeviceProfile = new DeviceProfile();
  231 + if (StringUtils.isNotBlank(deviceProfileDTO.getId())) {
  232 + DeviceProfileDTO findDeviceProfile =
  233 + ytDeviceProfileService.findDeviceProfileById(
  234 + getCurrentUser().getCurrentTenantId(), deviceProfileDTO.getId());
  235 + if (StringUtils.isNotEmpty(findDeviceProfile.getTbProfileId())) {
  236 + UUID profileId = UUID.fromString(findDeviceProfile.getTbProfileId());
  237 + tbDeviceProfile.setId(new DeviceProfileId(profileId));
  238 + tbDeviceProfile.setCreatedTime(
  239 + deviceProfileDTO.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
  240 + }
  241 + } else {
  242 + tbDeviceProfile.setCreatedTime(
  243 + LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
172 244 }
173   -
174   - @GetMapping("/me/default")
175   - @ApiOperation("默认设备配置")
176   - public ResponseEntity<DeviceProfile> findCurrentTenantDeviceProfiles() throws ThingsboardException {
177   - DeviceProfile result = deviceProfileService.findDefaultDeviceProfile(getCurrentUser().getTenantId());
178   - return ResponseEntity.ok(result);
  245 + tbDeviceProfile.setName(deviceProfileDTO.getName());
  246 + tbDeviceProfile.setImage(deviceProfileDTO.getImage());
  247 + tbDeviceProfile.setDescription(deviceProfileDTO.getDescription());
  248 + tbDeviceProfile.setType(DeviceProfileType.DEFAULT);
  249 + UUID tenantId = UUID.fromString(deviceProfileDTO.getTenantId());
  250 + tbDeviceProfile.setTenantId(TenantId.fromUUID(tenantId));
  251 + tbDeviceProfile.setDefault(deviceProfileDTO.isDefault());
  252 +
  253 + String chainStr = deviceProfileDTO.getDefaultRuleChainId();
  254 + // 获取当前租户的默认规则链
  255 + if (StringUtils.isNotBlank(chainStr)) {
  256 + UUID chainId = UUID.fromString(chainStr);
  257 + RuleChain chain =
  258 + ruleChainService.findRuleChainById(TenantId.SYS_TENANT_ID, new RuleChainId(chainId));
  259 + if (chain == null
  260 + || !deviceProfileDTO.getTenantId().equals(chain.getTenantId().getId().toString())) {
  261 + throw new YtDataValidationException(ErrorMessage.RULE_CHAIN_NOT_ENABLE.getMessage());
  262 + }
  263 + tbDeviceProfile.setDefaultRuleChainId(new RuleChainId(chainId));
179 264 }
180 265
181   - @PostMapping("/import")
182   - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:deviceProfile:import'})")
183   - @ApiOperation("导入配置")
184   - public ResponseEntity<String> importDeviceProfile(){
185   - //TODO 实现的业务功能
186   - return ResponseEntity.ok("");
187   - }
  266 + tbDeviceProfile.setDefaultQueueName(ServiceQueue.MAIN);
  267 + tbDeviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED);
188 268
189   - @PostMapping("/export")
190   - @PreAuthorize("@check.checkPermissions({'TENANT_ADMIN'},{'api:yt:deviceProfile:export'})")
191   - @ApiOperation("导出")
192   - public ResponseEntity<String> exportDeviceProfile(){
193   - //TODO 实现的业务功能
194   - return ResponseEntity.ok("");
  269 + // 传输类型默认都是Default
  270 + String transportType = deviceProfileDTO.getTransportType();
  271 + String scriptText = null;
  272 + if (transportType == null || DeviceTransportType.DEFAULT.name().equals(transportType)) {
  273 + tbDeviceProfile.setTransportType(DeviceTransportType.DEFAULT);
  274 + } else {
  275 + tbDeviceProfile.setTransportType(DeviceTransportType.valueOf(transportType));
195 276 }
196 277
197   -
198   -
199   - /**
200   - * 构造调用TBDeviceProfile需要的参数
201   - *
202   - * @param deviceProfileDTO 页面接收的参数
203   - * @return 封装好的TBDeviceProfile
204   - */
205   - private DeviceProfile buildTbDeviceProfileFromDeviceProfileDTO(DeviceProfileDTO deviceProfileDTO) {
206   - DeviceProfile tbDeviceProfile = new DeviceProfile();
207   - if (StringUtils.isNotBlank(deviceProfileDTO.getId())) {
208   - UUID profileId = UUID.fromString(deviceProfileDTO.getId());
209   - tbDeviceProfile.setId(new DeviceProfileId(profileId));
210   - tbDeviceProfile.setCreatedTime(deviceProfileDTO.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
211   - }else{
212   - tbDeviceProfile.setCreatedTime(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
213   - }
214   - tbDeviceProfile.setName(deviceProfileDTO.getName());
215   - tbDeviceProfile.setImage(deviceProfileDTO.getImage());
216   - tbDeviceProfile.setDescription(deviceProfileDTO.getDescription());
217   - tbDeviceProfile.setType(DeviceProfileType.DEFAULT);
218   - UUID tenantId = UUID.fromString(deviceProfileDTO.getTenantId());
219   - tbDeviceProfile.setTenantId(TenantId.fromUUID(tenantId));
220   - tbDeviceProfile.setDefault(deviceProfileDTO.isDefault());
221   -
222   -
223   - String chainStr = deviceProfileDTO.getDefaultRuleChainId();
224   - if(StringUtils.isBlank(chainStr)){
225   - throw new YtDataValidationException(ErrorMessage.RULE_CHAIN_NOT_ENABLE.getMessage());
226   - }
227   - UUID chainId = UUID.fromString(chainStr);
228   - RuleChain chain = ruleChainService.findRuleChainById(TenantId.SYS_TENANT_ID,new RuleChainId(chainId));
229   - if(chain==null || !deviceProfileDTO.getTenantId().equals(chain.getTenantId().getId().toString())){
230   - throw new YtDataValidationException(ErrorMessage.RULE_CHAIN_NOT_ENABLE.getMessage());
231   - }
232   -
233   - // 获取当前租户的默认规则链
234   - if (StringUtils.isNotBlank(deviceProfileDTO.getDefaultRuleChainId())) {
235   - tbDeviceProfile.setDefaultRuleChainId(new RuleChainId(chainId));
236   - }
237   -
238   - tbDeviceProfile.setDefaultQueueName(ServiceQueue.MAIN);
239   - tbDeviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED);
240   -
241   -
242   - // 传输类型默认都是Default
243   - String transportType = deviceProfileDTO.getTransportType();
244   - String scriptText=null;
245   - if(transportType ==null || DeviceTransportType.DEFAULT.name().equals(transportType)){
246   - tbDeviceProfile.setTransportType(DeviceTransportType.DEFAULT);
247   - }else{
248   - tbDeviceProfile.setTransportType(DeviceTransportType.valueOf(transportType));
249   - }
250   -
251   - if(DeviceTransportType.TCP.name().equals(transportType)){
252   - YtTcpDeviceProfileTransportConfiguration tcpDeviceProfileTransportConfiguration = (YtTcpDeviceProfileTransportConfiguration) deviceProfileDTO.getProfileData().getTransportConfiguration();
253   - String scriptId = tcpDeviceProfileTransportConfiguration.getScriptId();
254   - scriptText =javaScriptService.getScriptText(deviceProfileDTO.getTenantId(), scriptId);
255   - deviceProfileDTO.setScriptId(scriptId);
256   - }
257   -
258   - DeviceProfileData deviceProfileData = new DeviceProfileData();
259   - buildDeviceProfileData(transportType,deviceProfileData,deviceProfileDTO.getProfileData().getTransportConfiguration(),scriptText);
260   -
261   - if(deviceProfileDTO.getProfileData()!=null
262   - && deviceProfileDTO.getProfileData().getAlarms() !=null){
263   - deviceProfileData.setAlarms(deviceProfileDTO.getProfileData().getAlarms());
264   - }
265   -
266   - tbDeviceProfile.setProfileData(deviceProfileData);
267   -
268   - return tbDeviceProfile;
  278 + if (DeviceTransportType.TCP.name().equals(transportType)) {
  279 + YtTcpDeviceProfileTransportConfiguration tcpDeviceProfileTransportConfiguration =
  280 + (YtTcpDeviceProfileTransportConfiguration)
  281 + deviceProfileDTO.getProfileData().getTransportConfiguration();
  282 + String scriptId = tcpDeviceProfileTransportConfiguration.getScriptId();
  283 + scriptText = javaScriptService.getScriptText(deviceProfileDTO.getTenantId(), scriptId);
  284 + deviceProfileDTO.setScriptId(scriptId);
269 285 }
  286 +
  287 + tbDeviceProfile.setProfileData(
  288 + buildDeviceProfileData(transportType, deviceProfileDTO.getProfileData(), scriptText));
  289 + return tbDeviceProfile;
  290 + }
270 291 }
... ...
... ... @@ -257,13 +257,7 @@ public class YtDeviceScriptController extends BaseController {
257 257
258 258
259 259 deviceProfileDTO.setScriptId(scriptId);
260   -
261   - DeviceProfileData deviceProfileData = new DeviceProfileData();
262   - buildDeviceProfileData(transportType,deviceProfileData,deviceProfileDTO.getProfileData().getTransportConfiguration(),scriptText);
263   -
264   -
265   - tbDeviceProfile.setProfileData(deviceProfileData);
266   -
  260 + tbDeviceProfile.setProfileData(buildDeviceProfileData(transportType,deviceProfileDTO.getProfileData(),scriptText));
267 261 return tbDeviceProfile;
268 262 }
269 263 }
... ...
... ... @@ -106,6 +106,8 @@ public final class ModelConstants {
106 106 public static final String IOTFS_DATA_BOARD_NAME = "iotfs_data_board";
107 107 /** 数据组件 */
108 108 public static final String IOTFS_DATA_COMPONENT_NAME = "iotfs_data_component";
  109 + /** 物模型 */
  110 + public static final String IOTFS_THING_MODEL = "iotfs_model";
109 111 }
110 112
111 113 public static class TableFields {
... ...
... ... @@ -52,11 +52,7 @@ public class DeviceProfileDTO extends BaseDTO {
52 52 /**
53 53 * TB的设备配置文件
54 54 */
55   - @Deprecated
56 55 private String tbProfileId;
57   -
58   -
59   -
60 56 @Valid
61 57 private transient DeviceProfileData profileData;
62 58 @ApiModelProperty(value = "关联规则链,默认关联根规则链", required = false)
... ...
... ... @@ -5,6 +5,7 @@ import lombok.Data;
5 5 import lombok.EqualsAndHashCode;
6 6 import org.thingsboard.server.common.data.yunteng.common.AddGroup;
7 7 import org.thingsboard.server.common.data.yunteng.common.UpdateGroup;
  8 +import org.thingsboard.server.common.data.yunteng.enums.TcpDataTypeEnum;
8 9
9 10 import javax.validation.constraints.NotEmpty;
10 11 import javax.validation.constraints.NotNull;
... ... @@ -12,24 +13,31 @@ import javax.validation.constraints.NotNull;
12 13 @EqualsAndHashCode(callSuper = true)
13 14 @Data
14 15 public class YtDeviceScriptDTO extends BaseDTO {
15   - @NotEmpty(message = "解析脚本名称不能为空或者空字符串", groups = {AddGroup.class,UpdateGroup.class})
16   - @ApiModelProperty(value = "解析脚本名称")
17   - private String name;
18   -
19   - /** TB的租户ID */
20   - @ApiModelProperty(value = "租户ID")
21   - private String tenantId;
22   -
23   - @ApiModelProperty(value = "脚本描述")
24   - private String description;
25   - @NotEmpty(message = "解析脚本内容不能为空或者空字符串", groups = {AddGroup.class,UpdateGroup.class})
26   - @ApiModelProperty(value = "解析脚本方法体内容")
27   - private String convertJs;
28   -
29   - @ApiModelProperty(value = "状态:0禁用 1启用")
30   - @NotNull(message = "状态不能为空",groups = UpdateGroup.class)
31   - private Integer status;
32   -
33   - public YtDeviceScriptDTO() {
34   - }
  16 + @NotEmpty(
  17 + message = "解析脚本名称不能为空或者空字符串",
  18 + groups = {AddGroup.class, UpdateGroup.class})
  19 + @ApiModelProperty(value = "解析脚本名称")
  20 + private String name;
  21 +
  22 + /** TB的租户ID */
  23 + @ApiModelProperty(value = "租户ID")
  24 + private String tenantId;
  25 +
  26 + @ApiModelProperty(value = "脚本描述")
  27 + private String description;
  28 +
  29 + @NotEmpty(
  30 + message = "解析脚本内容不能为空或者空字符串",
  31 + groups = {AddGroup.class, UpdateGroup.class})
  32 + @ApiModelProperty(value = "解析脚本方法体内容")
  33 + private String convertJs;
  34 +
  35 + @ApiModelProperty(value = "状态:0禁用 1启用")
  36 + @NotNull(message = "状态不能为空", groups = UpdateGroup.class)
  37 + private Integer status;
  38 +
  39 + @ApiModelProperty(value = "脚本编数据编码类型:HEX ASCII")
  40 + private TcpDataTypeEnum dataType;
  41 +
  42 + public YtDeviceScriptDTO() {}
35 43 }
... ...
1 1 package org.thingsboard.server.common.data.yunteng.enums;
2 2
3 3 /**
4   - * 设备凭证
  4 + * 编码类型
5 5 */
6 6 public enum TcpDataTypeEnum {
7 7 HEX,
... ...
... ... @@ -9,16 +9,20 @@ import lombok.EqualsAndHashCode;
9 9 import org.apache.ibatis.type.EnumTypeHandler;
10 10 import org.thingsboard.server.common.data.yunteng.constant.ModelConstants;
11 11 import org.thingsboard.server.common.data.yunteng.enums.DeviceTypeEnum;
  12 +import org.thingsboard.server.common.data.yunteng.enums.TcpDataTypeEnum;
12 13
13 14 @Data
14 15 @TableName(value = ModelConstants.Table.IOTFS_DEVICE_SCRIPT_TABLE_NAME, autoResultMap = true)
15 16 @EqualsAndHashCode(callSuper = true)
16 17 public class YtDeviceScriptEntity extends TenantBaseEntity {
17   - private String name;
18   - private String convertJs;
19   - /**
20   - * 告警状态:0:正常 1:告警
21   - */
22   - private Integer status;
23   - private String description;
  18 + private String name;
  19 + private String convertJs;
  20 + /** 告警状态:0:正常 1:告警 */
  21 + private Integer status;
  22 +
  23 + private String description;
  24 +
  25 + /** 脚本编数据编码类型 */
  26 + @TableField(typeHandler = EnumTypeHandler.class)
  27 + private TcpDataTypeEnum dataType;
24 28 }
... ...
... ... @@ -37,14 +37,14 @@ import java.util.stream.Collectors;
37 37 @Service
38 38 @RequiredArgsConstructor
39 39 @Slf4j
40   -public class YtDeviceProfileServiceImpl extends AbstractBaseService<YtDeviceProfileMapper, YtDeviceProfileEntity>
41   - implements YtDeviceProfileService {
  40 +public class YtDeviceProfileServiceImpl
  41 + extends AbstractBaseService<YtDeviceProfileMapper, YtDeviceProfileEntity>
  42 + implements YtDeviceProfileService {
42 43
43 44 private final DeviceMapper deviceMapper;
44 45
45 46 private final YtJpaDeviceProfileDao deviceProfileDao;
46 47
47   -
48 48 @Override
49 49 public boolean validateFormdata(DeviceProfileDTO ytDeviceProfileDTO) {
50 50 TenantId tenantId = TenantId.fromUUID(UUID.fromString(ytDeviceProfileDTO.getTenantId()));
... ... @@ -67,7 +67,18 @@ public class YtDeviceProfileServiceImpl extends AbstractBaseService<YtDeviceProf
67 67 }
68 68
69 69 @Override
70   - public DeviceProfileDTO insertOrUpdate( DeviceProfileDTO deviceDTO) {
  70 + public DeviceProfileDTO findDeviceProfileById(String tenantId, String id) {
  71 + YtDeviceProfileEntity entity =
  72 + baseMapper.selectOne(
  73 + new LambdaQueryWrapper<YtDeviceProfileEntity>()
  74 + .eq(YtDeviceProfileEntity::getTenantId, tenantId)
  75 + .eq(YtDeviceProfileEntity::getId, id));
  76 + return Optional.ofNullable(entity).map(obj -> obj.getDTO(DeviceProfileDTO.class)).orElse(null);
  77 + }
  78 +
  79 + @Override
  80 + @Transactional
  81 + public DeviceProfileDTO insertOrUpdate(DeviceProfileDTO deviceDTO) {
71 82 if (StringUtils.isBlank(deviceDTO.getId())) {
72 83 return insert(deviceDTO);
73 84 } else {
... ... @@ -79,15 +90,14 @@ public class YtDeviceProfileServiceImpl extends AbstractBaseService<YtDeviceProf
79 90
80 91 YtDeviceProfileEntity profile = new YtDeviceProfileEntity();
81 92 deviceDTO.copyToEntity(
82   - profile,
83   - ModelConstants.TablePropertyMapping.ACTIVE_TIME,
84   - ModelConstants.TablePropertyMapping.DEVICE_STATE,
85   - ModelConstants.TablePropertyMapping.CREATOR,
86   - ModelConstants.TablePropertyMapping.UPDATER,
87   - ModelConstants.TablePropertyMapping.CREATE_TIME,
88   - ModelConstants.TablePropertyMapping.UPDATE,
89   - ModelConstants.TablePropertyMapping.UPDATE_TIME);
90   -
  93 + profile,
  94 + ModelConstants.TablePropertyMapping.ACTIVE_TIME,
  95 + ModelConstants.TablePropertyMapping.DEVICE_STATE,
  96 + ModelConstants.TablePropertyMapping.CREATOR,
  97 + ModelConstants.TablePropertyMapping.UPDATER,
  98 + ModelConstants.TablePropertyMapping.CREATE_TIME,
  99 + ModelConstants.TablePropertyMapping.UPDATE,
  100 + ModelConstants.TablePropertyMapping.UPDATE_TIME);
91 101
92 102 baseMapper.insert(profile);
93 103 return profile.getDTO(DeviceProfileDTO.class);
... ... @@ -96,27 +106,28 @@ public class YtDeviceProfileServiceImpl extends AbstractBaseService<YtDeviceProf
96 106 private DeviceProfileDTO update(DeviceProfileDTO deviceDTO) {
97 107 YtDeviceProfileEntity device = new YtDeviceProfileEntity();
98 108 deviceDTO.copyToEntity(
99   - device,
100   - ModelConstants.TablePropertyMapping.ACTIVE_TIME,
101   - ModelConstants.TablePropertyMapping.DEVICE_STATE,
102   - ModelConstants.TablePropertyMapping.TB_DEVICE_ID,
103   - ModelConstants.TablePropertyMapping.TENANT_CODE,
104   - ModelConstants.TablePropertyMapping.CREATOR,
105   - ModelConstants.TablePropertyMapping.UPDATER,
106   - ModelConstants.TablePropertyMapping.CREATE_TIME,
107   - ModelConstants.TablePropertyMapping.UPDATE,
108   - ModelConstants.TablePropertyMapping.UPDATE_TIME);
  109 + device,
  110 + ModelConstants.TablePropertyMapping.ACTIVE_TIME,
  111 + ModelConstants.TablePropertyMapping.DEVICE_STATE,
  112 + ModelConstants.TablePropertyMapping.TB_DEVICE_ID,
  113 + ModelConstants.TablePropertyMapping.TENANT_CODE,
  114 + ModelConstants.TablePropertyMapping.CREATOR,
  115 + ModelConstants.TablePropertyMapping.UPDATER,
  116 + ModelConstants.TablePropertyMapping.CREATE_TIME,
  117 + ModelConstants.TablePropertyMapping.UPDATE,
  118 + ModelConstants.TablePropertyMapping.UPDATE_TIME);
109 119 baseMapper.updateById(device);
110 120 return device.getDTO(DeviceProfileDTO.class);
111 121 }
112 122
113 123 @Override
  124 + @Transactional
114 125 public void deleteDeviceProfiles(String tenantId, Set<String> ids) {
115 126 LambdaQueryWrapper<YtDeviceProfileEntity> queryWrapper =
116   - new QueryWrapper<YtDeviceProfileEntity>()
117   - .lambda()
118   - .eq(YtDeviceProfileEntity::getTenantId, tenantId)
119   - .in(YtDeviceProfileEntity::getId, ids);
  127 + new QueryWrapper<YtDeviceProfileEntity>()
  128 + .lambda()
  129 + .eq(YtDeviceProfileEntity::getTenantId, tenantId)
  130 + .in(YtDeviceProfileEntity::getId, ids);
120 131
121 132 baseMapper.delete(queryWrapper);
122 133 }
... ... @@ -135,35 +146,36 @@ public class YtDeviceProfileServiceImpl extends AbstractBaseService<YtDeviceProf
135 146
136 147 @Override
137 148 public Optional<DeviceProfileDTO> getDeviceProfile(String tenantId, String id) {
138   - return Optional.ofNullable(baseMapper.selectDetail(tenantId,id));
  149 + return Optional.ofNullable(baseMapper.selectDetail(tenantId, id));
139 150 }
140 151
141 152 @Override
142   - public YtPageData<DeviceProfileDTO> page(Integer page, Integer pageSize, String orderField, OrderTypeEnum orderType
143   - , String tenantIdStr, String profileName, String transportType) {
144   -
145   - IPage<YtDeviceProfileEntity> currentPage = getCurrentPage(page,pageSize,orderField,orderType);
146   - IPage<DeviceProfileDTO> result = baseMapper.getProfilePage(currentPage,tenantIdStr,profileName,transportType);
  153 + public YtPageData<DeviceProfileDTO> page(
  154 + Integer page,
  155 + Integer pageSize,
  156 + String orderField,
  157 + OrderTypeEnum orderType,
  158 + String tenantIdStr,
  159 + String profileName,
  160 + String transportType) {
  161 +
  162 + IPage<YtDeviceProfileEntity> currentPage =
  163 + getCurrentPage(page, pageSize, orderField, orderType);
  164 + IPage<DeviceProfileDTO> result =
  165 + baseMapper.getProfilePage(currentPage, tenantIdStr, profileName, transportType);
147 166
148 167 return getPageData(result, DeviceProfileDTO.class);
149 168 }
150 169
151 170 @Override
152   - public List<DeviceProfileDTO> findDeviceProfile(String tenantId) {
153   - UUID profileId = UUID.fromString(tenantId);
154   - List<DeviceProfileDTO> results =
155   - deviceProfileDao.findDeviceProfileByTenantId(TenantId.fromUUID(profileId));
156   - return results;
157   - }
158   -
159   - @Override
160 171 public List<DeviceProfileDTO> findDeviceProfile(String tenantId, String scriptId) {
161 172 LambdaQueryWrapper<YtDeviceProfileEntity> queryWrapper =
162   - new QueryWrapper<YtDeviceProfileEntity>()
163   - .lambda()
164   - .eq(YtDeviceProfileEntity::getTenantId, tenantId)
165   - .eq(YtDeviceProfileEntity::getScriptId, scriptId);
166   - List<DeviceProfileDTO> results = baseMapper.selectList(queryWrapper).stream()
  173 + new QueryWrapper<YtDeviceProfileEntity>()
  174 + .lambda()
  175 + .eq(YtDeviceProfileEntity::getTenantId, tenantId)
  176 + .eq(StringUtils.isNotEmpty(scriptId), YtDeviceProfileEntity::getScriptId, scriptId);
  177 + List<DeviceProfileDTO> results =
  178 + baseMapper.selectList(queryWrapper).stream()
167 179 .map(item -> item.getDTO(DeviceProfileDTO.class))
168 180 .collect(Collectors.toList());
169 181 return results;
... ...
... ... @@ -18,8 +18,8 @@ public interface YtDeviceProfileMapper extends BaseMapper<YtDeviceProfileEntity>
18 18 * 获取产品(设备配置)详情
19 19 * @return
20 20 */
21   - public DeviceProfileDTO selectDetail( @Param("tenantId") String tenantId, @Param("id") String id);
  21 + DeviceProfileDTO selectDetail( @Param("tenantId") String tenantId, @Param("id") String id);
22 22
23   - public IPage<DeviceProfileDTO> getProfilePage(IPage<?> page, @Param("tenantId") String tenantId, @Param("profileName") String profileName, @Param("transportType") String transportType);
  23 + IPage<DeviceProfileDTO> getProfilePage(IPage<?> page, @Param("tenantId") String tenantId, @Param("profileName") String profileName, @Param("transportType") String transportType);
24 24
25 25 }
... ...
1 1 package org.thingsboard.server.dao.yunteng.service;
2 2
3   -import com.baomidou.mybatisplus.core.metadata.IPage;
4   -import org.thingsboard.server.common.data.page.PageLink;
5   -import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO;
6 3 import org.thingsboard.server.common.data.yunteng.dto.DeviceProfileDTO;
7 4 import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum;
8 5 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
... ... @@ -24,7 +21,6 @@ public interface YtDeviceProfileService {
24 21 YtPageData<DeviceProfileDTO> page(Integer page, Integer pageSize, String orderField, OrderTypeEnum orderType
25 22 ,String tenantIdStr, String profileName, String transportType);
26 23
27   - List<DeviceProfileDTO> findDeviceProfile(String tenantId);
28 24 List<DeviceProfileDTO> findDeviceProfile(String tenantId,String scriptId);
29 25
30 26
... ... @@ -35,4 +31,12 @@ public interface YtDeviceProfileService {
35 31 * @return
36 32 */
37 33 boolean validateFormdata(DeviceProfileDTO ytDeviceProfileDTO);
  34 +
  35 + /**
  36 + * 根据设备配置ID或者设备配置信息
  37 + * @param tenantId 租户ID
  38 + * @param id 设备配置ID
  39 + * @return 设备配置信息
  40 + */
  41 + DeviceProfileDTO findDeviceProfileById(String tenantId, String id);
38 42 }
... ...
... ... @@ -27,7 +27,7 @@
27 27
28 28 <sql id="basicColumns">
29 29 base.name,base.image,base.description,base.tenant_id,base.transport_type,base.provision_type,base.profile_data,base.default_queue_name,base.default_rule_chain_id,base.is_default,base.type
30   - ,iot.id,iot.script_id,iot.device_type,iot.create_time,iot.update_time,iot.creator,iot.updater
  30 + ,iot.id,iot.script_id,iot.device_type,iot.create_time,iot.update_time,iot.creator,iot.updater,iot.tb_profile_id
31 31 </sql>
32 32
33 33 <select id="getProfilePage" resultMap="detail">
... ... @@ -51,7 +51,7 @@
51 51
52 52 <select id="selectDetail" resultMap="detail">
53 53 SELECT
54   - <include refid="basicColumns"/>,d.customer_id::TEXT AS customer_id,cus.title AS cusotomer_name
  54 + <include refid="basicColumns"/>
55 55 FROM device_profile base
56 56 LEFT JOIN iotfs_device_profile iot ON iot.tb_profile_id = base.id::TEXT
57 57 <where>
... ... @@ -59,7 +59,7 @@
59 59 AND iot.tenant_id = #{tenantId}
60 60 </if>
61 61 <if test="id !=null and id !=''">
62   - AND base.id = #{id}
  62 + AND iot.id = #{id}
63 63 </if>
64 64 </where>
65 65 </select>
... ...