Browse Source

空间设备管理修改

xiwa 3 years ago
parent
commit
a201aed29f

+ 4 - 0
manager/src/main/java/cc/iotkit/manager/config/KeycloakSecurityConfig.java

@@ -68,6 +68,9 @@ public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter
                 .antMatchers("/space/myDevices/**").hasRole("iot_client_user")
                 .antMatchers("/space/findDevice/**").hasRole("iot_client_user")
                 .antMatchers("/space/addDevice/**").hasRole("iot_client_user")
+                .antMatchers("/space/saveDevice").hasRole("iot_client_user")
+                .antMatchers("/space/removeDevice").hasRole("iot_client_user")
+                .antMatchers("/space/device/*").hasRole("iot_client_user")
                 .antMatchers("/device/*/service/property/set").hasRole("iot_client_user")
                 .antMatchers("/device/*/service/*/invoke").hasRole("iot_client_user")
 
@@ -75,6 +78,7 @@ public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter
                 .antMatchers(HttpMethod.DELETE).hasRole("iot_write")
                 .antMatchers(HttpMethod.PUT).hasRole("iot_write")
                 .antMatchers("/**/save*/**").hasRole("iot_write")
+                .antMatchers("/**/remove*/**").hasRole("iot_write")
                 .antMatchers("/**/del*/**").hasRole("iot_write")
                 .antMatchers("/**/add*/**").hasRole("iot_write")
                 .antMatchers("/**/clear*/**").hasRole("iot_write")

+ 29 - 16
manager/src/main/java/cc/iotkit/manager/controller/SpaceDeviceController.java

@@ -69,6 +69,7 @@ public class SpaceDeviceController {
         DeviceInfo device = deviceCache.get(sd.getDeviceId());
         Space space = spaceCache.getSpace(sd.getSpaceId());
         Product product = productCache.findById(device.getProductKey());
+        Category category = categoryCache.getById(product.getCategory());
         DeviceInfo.State state = device.getState();
 
         return SpaceDeviceVo.builder()
@@ -79,7 +80,9 @@ public class SpaceDeviceController {
                 .spaceId(sd.getSpaceId())
                 .spaceName(space.getName())
                 .productKey(device.getProductKey())
+                .productName(product.getName())
                 .category(product.getCategory())
+                .categoryName(category.getName())
                 .picUrl(product.getImg())
                 .online(state != null && state.isOnline())
                 .property(device.getProperty())
@@ -89,22 +92,8 @@ public class SpaceDeviceController {
 
     @GetMapping("/{userId}/devices")
     public List<SpaceDeviceVo> getDevices(@PathVariable("userId") String userId) {
-        List<SpaceDeviceVo> deviceVos = new ArrayList<>();
-        List<SpaceDevice> devices = spaceDeviceRepository.findAll(Example.of(SpaceDevice.builder().uid(userId).build()));
-        devices.forEach(sd -> {
-            DeviceInfo deviceInfo = deviceCache.get(sd.getDeviceId());
-            Product product = productCache.findById(deviceInfo.getProductKey());
-            deviceVos.add(SpaceDeviceVo.builder()
-                    .deviceId(sd.getDeviceId())
-                    .name(sd.getName())
-                    .picUrl(product.getImg())
-                    .spaceName("")
-                    .online(deviceInfo.getState().isOnline())
-                    .property(deviceInfo.getProperty())
-                    .productKey(deviceInfo.getProductKey())
-                    .build());
-        });
-        return deviceVos;
+        List<SpaceDevice> spaceDevices = spaceDeviceRepository.findAll(Example.of(SpaceDevice.builder().uid(userId).build()));
+        return spaceDevices.stream().map((this::parseSpaceDevice)).collect(Collectors.toList());
     }
 
     @GetMapping("/findDevice")
@@ -206,6 +195,7 @@ public class SpaceDeviceController {
         if (spaceDevice == null) {
             throw new BizException("space device does not exist");
         }
+        dataOwnerService.checkOwner(spaceDevice);
 
         spaceDeviceRepository.deleteById(spaceDevice.getId());
         DeviceInfo deviceInfo = deviceRepository.findByDeviceId(deviceId);
@@ -215,4 +205,27 @@ public class SpaceDeviceController {
             deviceRepository.save(deviceInfo);
         }
     }
+
+    @PostMapping("/saveDevice")
+    public void saveDevice(SpaceDevice spaceDevice) {
+        dataOwnerService.checkOwner(spaceDevice);
+        Optional<SpaceDevice> optData = spaceDeviceRepository.findById(spaceDevice.getId());
+        if (!optData.isPresent()) {
+            throw new BizException("space device does not exist");
+        }
+        SpaceDevice oldData = optData.get();
+        oldData.setName(spaceDevice.getName());
+        oldData.setSpaceId(spaceDevice.getSpaceId());
+        spaceDeviceRepository.save(oldData);
+    }
+
+    @GetMapping("/device/{deviceId}")
+    public SpaceDeviceVo getSpaceDevice(@PathVariable("deviceId") String deviceId) {
+        String uid = AuthUtil.getUserId();
+        SpaceDevice spaceDevice = spaceDeviceRepository.findByDeviceIdAndUid(deviceId, uid);
+        //更新设备使用时间
+        spaceDevice.setUseAt(System.currentTimeMillis());
+        spaceDeviceRepository.save(spaceDevice);
+        return parseSpaceDevice(spaceDevice);
+    }
 }

+ 10 - 0
manager/src/main/java/cc/iotkit/manager/model/vo/SpaceDeviceVo.java

@@ -68,8 +68,18 @@ public class SpaceDeviceVo {
      */
     private String productKey;
 
+    /**
+     * 产品名
+     */
+    private String productName;
+
     /**
      * 品类
      */
     private String category;
+
+    /**
+     * 品类名
+     */
+    private String categoryName;
 }