xiwa пре 3 година
родитељ
комит
7189336cd1

+ 2 - 0
dao/src/main/java/cc/iotkit/dao/SpaceDeviceRepository.java

@@ -14,4 +14,6 @@ public interface SpaceDeviceRepository extends MongoRepository<SpaceDevice, Stri
     List<SpaceDevice> findByUidAndSpaceIdOrderByAddAtDesc(String uid, String spaceId);
 
     SpaceDevice findByDeviceId(String deviceId);
+
+    SpaceDevice findByDeviceIdAndUid(String deviceId, String uid);
 }

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

@@ -67,6 +67,10 @@ public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter
                 .antMatchers("/space/spaces/**").hasRole("iot_client_user")
                 .antMatchers("/space/myDevices/**").hasRole("iot_client_user")
                 .antMatchers("/space/findDevice/**").hasRole("iot_client_user")
+                .antMatchers("/space/addDevice/**").hasRole("iot_client_user")
+                .antMatchers("/**/service/property/set").hasRole("iot_client_user")
+                .antMatchers("/**/service/*/invoke").hasRole("iot_client_user")
+
 
                 .antMatchers(HttpMethod.DELETE).hasRole("iot_write")
                 .antMatchers(HttpMethod.PUT).hasRole("iot_write")
@@ -75,6 +79,8 @@ public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter
                 .antMatchers("/**/add*/**").hasRole("iot_write")
                 .antMatchers("/**/clear*/**").hasRole("iot_write")
                 .antMatchers("/**/set*/**").hasRole("iot_write")
+                .antMatchers("/**/service/property/set").hasRole("iot_write")
+                .antMatchers("/**/service/*/invoke").hasRole("iot_write")
                 .antMatchers("/**").hasAnyRole(systemRole)
                 .and().csrf().disable();
     }

+ 0 - 2
manager/src/main/java/cc/iotkit/manager/controller/DeviceController.java

@@ -62,14 +62,12 @@ public class DeviceController {
         if (StringUtils.isBlank(deviceId) || StringUtils.isBlank(service)) {
             throw new RuntimeException("deviceId/service is blank.");
         }
-        dataOwnerService.checkWriteRole();
         return new InvokeResult(deviceService.invokeService(deviceId, service, args));
     }
 
     @PostMapping(Constants.API.DEVICE_SET_PROPERTIES)
     public InvokeResult setProperty(@PathVariable("deviceId") String deviceId,
                                     @RequestBody Map<String, Object> args) {
-        dataOwnerService.checkWriteRole();
         return new InvokeResult(deviceService.setProperty(deviceId, args));
     }
 

+ 70 - 5
manager/src/main/java/cc/iotkit/manager/controller/SpaceDeviceController.java

@@ -4,6 +4,7 @@ import cc.iotkit.common.exception.BizException;
 import cc.iotkit.dao.*;
 import cc.iotkit.manager.model.vo.FindDeviceVo;
 import cc.iotkit.manager.model.vo.SpaceDeviceVo;
+import cc.iotkit.manager.service.DataOwnerService;
 import cc.iotkit.manager.utils.AuthUtil;
 import cc.iotkit.model.device.DeviceInfo;
 import cc.iotkit.model.product.Category;
@@ -13,13 +14,11 @@ import cc.iotkit.model.space.SpaceDevice;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Example;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 
@@ -39,6 +38,10 @@ public class SpaceDeviceController {
     private CategoryCache categoryCache;
     @Autowired
     private SpaceCache spaceCache;
+    @Autowired
+    private SpaceRepository spaceRepository;
+    @Autowired
+    private DataOwnerService dataOwnerService;
 
     /**
      * 我最近使用的设备列表
@@ -56,7 +59,8 @@ public class SpaceDeviceController {
      */
     @GetMapping("/myDevices/{spaceId}")
     public List<SpaceDeviceVo> getMyDevices(@PathVariable("spaceId") String spaceId) {
-        List<SpaceDevice> spaceDevices = spaceDeviceRepository.findByUidOrderByUseAtDesc(AuthUtil.getUserId());
+        List<SpaceDevice> spaceDevices = spaceDeviceRepository.
+                findByUidAndSpaceIdOrderByAddAtDesc(AuthUtil.getUserId(), spaceId);
         return spaceDevices.stream().map((this::parseSpaceDevice)).collect(Collectors.toList());
     }
 
@@ -75,6 +79,7 @@ public class SpaceDeviceController {
                 .spaceId(sd.getSpaceId())
                 .spaceName(space.getName())
                 .productKey(device.getProductKey())
+                .category(product.getCategory())
                 .picUrl(product.getImg())
                 .online(state != null && state.isOnline())
                 .property(device.getProperty())
@@ -149,4 +154,64 @@ public class SpaceDeviceController {
         return findDeviceVo;
     }
 
+    @PostMapping("/addDevice")
+    public void addDevice(SpaceDevice device) {
+        String deviceId = device.getDeviceId();
+        DeviceInfo deviceInfo = deviceRepository.findByDeviceId(deviceId);
+        if (deviceInfo == null) {
+            throw new BizException("device does not exist");
+        }
+        String spaceId = device.getSpaceId();
+        Optional<Space> optSpace = spaceRepository.findById(spaceId);
+        if (!optSpace.isPresent()) {
+            throw new BizException("space does not exist");
+        }
+
+        SpaceDevice oldSpaceDevice = spaceDeviceRepository.findByDeviceId(deviceId);
+        if (oldSpaceDevice != null) {
+            throw new BizException("device has been added");
+        }
+
+        Space space = optSpace.get();
+
+        SpaceDevice spaceDevice = SpaceDevice.builder()
+                .deviceId(deviceId)
+                .spaceId(spaceId)
+                .deviceId(deviceId)
+                .name(device.getName())
+                .homeId(space.getHomeId())
+                .uid(AuthUtil.getUserId())
+                .addAt(System.currentTimeMillis())
+                .build();
+        spaceDeviceRepository.save(spaceDevice);
+
+        //更新设备子用户列表
+        List<String> subUid = deviceInfo.getSubUid();
+        if (subUid == null) {
+            subUid = new ArrayList<>();
+        }
+
+        String uid = AuthUtil.getUserId();
+        if (!subUid.contains(uid)) {
+            subUid.add(uid);
+        }
+        deviceRepository.save(deviceInfo);
+    }
+
+    @DeleteMapping("/removeDevice")
+    public void removeDevice(String deviceId) {
+        String uid = AuthUtil.getUserId();
+        SpaceDevice spaceDevice = spaceDeviceRepository.findByDeviceIdAndUid(deviceId, uid);
+        if (spaceDevice == null) {
+            throw new BizException("space device does not exist");
+        }
+
+        spaceDeviceRepository.deleteById(spaceDevice.getId());
+        DeviceInfo deviceInfo = deviceRepository.findByDeviceId(deviceId);
+        List<String> subUid = deviceInfo.getSubUid();
+        if (subUid != null) {
+            subUid.remove(uid);
+            deviceRepository.save(deviceInfo);
+        }
+    }
 }

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

@@ -68,4 +68,8 @@ public class SpaceDeviceVo {
      */
     private String productKey;
 
+    /**
+     * 品类
+     */
+    private String category;
 }

+ 2 - 1
model/src/main/java/cc/iotkit/model/space/SpaceDevice.java

@@ -1,5 +1,6 @@
 package cc.iotkit.model.space;
 
+import cc.iotkit.model.Owned;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
@@ -12,7 +13,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
 @NoArgsConstructor
 @AllArgsConstructor
 @Document
-public class SpaceDevice {
+public class SpaceDevice implements Owned {
 
     @Id
     private String id;