Explorar o código

权限配置/搜索设备接口

xiwa %!s(int64=3) %!d(string=hai) anos
pai
achega
af3408bfe6

+ 3 - 1
common/src/main/java/cc/iotkit/common/Constants.java

@@ -10,7 +10,9 @@ public interface Constants {
 
     String DEVICE_CACHE = "device_cache";
 
-    String SPACE_CACHE = "device_cache";
+    String CATEGORY_CACHE = "category_cache";
+
+    String SPACE_CACHE = "space_cache";
 
     String THING_MODEL_CACHE = "thing_model_cache";
 

+ 32 - 0
dao/src/main/java/cc/iotkit/dao/CategoryCache.java

@@ -0,0 +1,32 @@
+package cc.iotkit.dao;
+
+import cc.iotkit.common.Constants;
+import cc.iotkit.model.product.Category;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.stereotype.Repository;
+
+import javax.annotation.PostConstruct;
+
+@Repository
+public class CategoryCache {
+
+    @Autowired
+    private CategoryRepository categoryRepository;
+
+    private static CategoryCache INSTANCE;
+
+    @PostConstruct
+    public void init() {
+        INSTANCE = this;
+    }
+
+    public static CategoryCache getInstance() {
+        return INSTANCE;
+    }
+
+    @Cacheable(value = Constants.CATEGORY_CACHE, key = "#id")
+    public Category getById(String id) {
+        return categoryRepository.findById(id).orElse(null);
+    }
+}

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

@@ -15,4 +15,6 @@ public interface DeviceRepository extends MongoRepository<DeviceInfo, String> {
 
     List<DeviceInfo> findByParentId(String parentId);
 
+    List<DeviceInfo> findByDeviceName(String deviceName);
+
 }

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

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

+ 5 - 0
manager/src/main/java/cc/iotkit/manager/config/CacheConfig.java

@@ -56,6 +56,11 @@ public class CacheConfig {
                         Caffeine.newBuilder()
                                 .expireAfterWrite(5, TimeUnit.MINUTES)
                                 .build()
+                ), new CaffeineCache(
+                        Constants.CATEGORY_CACHE,
+                        Caffeine.newBuilder()
+                                .expireAfterWrite(5, TimeUnit.MINUTES)
+                                .build()
                 )
         ));
         return manager;

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

@@ -62,6 +62,11 @@ public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter
                 .antMatchers("/space/saveSpace/**").hasRole("iot_client_user")
                 .antMatchers("/space/delSpace/**").hasRole("iot_client_user")
                 .antMatchers("/space/saveHome/**").hasRole("iot_client_user")
+                .antMatchers("/space/currentHome/**").hasRole("iot_client_user")
+                .antMatchers("/space/myRecentDevices/**").hasRole("iot_client_user")
+                .antMatchers("/space/spaces/**").hasRole("iot_client_user")
+                .antMatchers("/space/myDevices/**").hasRole("iot_client_user")
+                .antMatchers("/space/findDevice/**").hasRole("iot_client_user")
 
                 .antMatchers(HttpMethod.DELETE).hasRole("iot_write")
                 .antMatchers(HttpMethod.PUT).hasRole("iot_write")

+ 54 - 21
manager/src/main/java/cc/iotkit/manager/config/ResponseResultHandler.java

@@ -1,12 +1,16 @@
 package cc.iotkit.manager.controller;
 
+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.utils.AuthUtil;
 import cc.iotkit.model.device.DeviceInfo;
+import cc.iotkit.model.product.Category;
 import cc.iotkit.model.product.Product;
 import cc.iotkit.model.space.Space;
 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;
@@ -32,6 +36,8 @@ public class SpaceDeviceController {
     @Autowired
     private ProductCache productCache;
     @Autowired
+    private CategoryCache categoryCache;
+    @Autowired
     private SpaceCache spaceCache;
 
     /**
@@ -49,7 +55,7 @@ public class SpaceDeviceController {
      * @param spaceId 空间id
      */
     @GetMapping("/myDevices/{spaceId}")
-    public List<SpaceDeviceVo> getMyDevices(String spaceId) {
+    public List<SpaceDeviceVo> getMyDevices(@PathVariable("spaceId") String spaceId) {
         List<SpaceDevice> spaceDevices = spaceDeviceRepository.findByUidOrderByUseAtDesc(AuthUtil.getUserId());
         return spaceDevices.stream().map((this::parseSpaceDevice)).collect(Collectors.toList());
     }
@@ -76,26 +82,6 @@ public class SpaceDeviceController {
                 .build();
     }
 
-
-//    @PostMapping("/list")
-//    public Paging<SpaceInfo> getDevices(int page,
-//                                            int limit,
-//                                            String address) {
-//        Criteria condition = new Criteria();
-//        if (StringUtils.isNotBlank(address)) {
-//            condition.and("address").regex(".*" + address + ".*");
-//        }
-//        List<UserInfo> userInfoList = userInfoDao.find(condition, (page - 1) * limit,
-//                limit, Sort.Order.desc("createAt"));
-//
-//        List<SpaceInfo> spaces = userInfoList.stream().map((u ->
-//                new SpaceInfo(u.getAddress(), u.getUid())))
-//                .collect(Collectors.toList());
-//
-//        return new Paging<>(userInfoDao.count(condition),
-//                spaces);
-//    }
-
     @GetMapping("/{userId}/devices")
     public List<SpaceDeviceVo> getDevices(@PathVariable("userId") String userId) {
         List<SpaceDeviceVo> deviceVos = new ArrayList<>();
@@ -116,4 +102,51 @@ public class SpaceDeviceController {
         return deviceVos;
     }
 
+    @GetMapping("/findDevice")
+    List<FindDeviceVo> findDevice(String mac) {
+        if (StringUtils.isBlank(mac)) {
+            throw new BizException("mac is blank");
+        }
+
+        List<FindDeviceVo> findDeviceVos = new ArrayList<>();
+        List<DeviceInfo> devices = deviceRepository.findByDeviceName(mac);
+        if (devices == null) {
+            return findDeviceVos;
+        }
+
+        //查找网关下子设备
+        List<DeviceInfo> subDevices = new ArrayList<>();
+        for (DeviceInfo device : devices) {
+            if (device.getParentId() == null) {
+                subDevices = deviceRepository.findByParentId(device.getDeviceId());
+            }
+        }
+        devices.addAll(subDevices);
+
+        //查找空间设备
+        for (DeviceInfo device : devices) {
+            SpaceDevice spaceDevice = spaceDeviceRepository.findByDeviceId(device.getDeviceId());
+            if (spaceDevice == null) {
+                //没有被其它人占用
+                findDeviceVos.add(getFindDeviceVo(device));
+            }
+        }
+        return findDeviceVos;
+    }
+
+    private FindDeviceVo getFindDeviceVo(DeviceInfo device) {
+        FindDeviceVo findDeviceVo = FindDeviceVo.builder()
+                .deviceId(device.getDeviceId())
+                .deviceName(device.getDeviceName())
+                .productKey(device.getProductKey())
+                .build();
+
+        Product product = productCache.findById(device.getProductKey());
+        Category category = categoryCache.getById(product.getCategory());
+        findDeviceVo.setProductName(product.getName());
+        findDeviceVo.setProductImg(product.getImg());
+        findDeviceVo.setCategoryName(category.getName());
+        return findDeviceVo;
+    }
+
 }

+ 26 - 0
manager/src/main/java/cc/iotkit/manager/model/vo/FindDeviceVo.java

@@ -0,0 +1,26 @@
+package cc.iotkit.manager.model.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@Builder
+public class FindDeviceVo {
+
+    private String deviceId;
+
+    private String productKey;
+
+    private String deviceName;
+
+    private String productName;
+
+    private String categoryName;
+
+    private String productImg;
+
+}

+ 0 - 0
model/src/main/java/cc/iotkit/model/product/ProductModel.java