Forráskód Böngészése

feat:添加系统与app的数据下行

tangfudong 1 éve
szülő
commit
c5ac5b66db

+ 1 - 1
iot-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/DeviceInfoDataCache.java

@@ -278,7 +278,7 @@ public class DeviceInfoDataCache implements IDeviceInfoData, SmartInitializingSi
 
 
     @Override
     @Override
     public List<DeviceInfo> findAllByCondition(DeviceInfo data) {
     public List<DeviceInfo> findAllByCondition(DeviceInfo data) {
-        return Collections.emptyList();
+        return deviceInfoData.findAllByCondition(data);
     }
     }
 
 
     @Override
     @Override

+ 19 - 0
iot-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/config/UserIDAuditorConfig.java

@@ -0,0 +1,19 @@
+package cc.iotkit.data.config;
+
+import cc.iotkit.common.satoken.utils.LoginHelper;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.domain.AuditorAware;
+
+import java.util.Optional;
+
+/**
+ * @Author:tfd
+ * @Date:2024/1/12 15:06
+ */
+@Configuration
+public class UserIDAuditorConfig implements AuditorAware<Long> {
+    @Override
+    public Optional<Long> getCurrentAuditor() {
+        return Optional.of(LoginHelper.getUserId());
+    }
+}

+ 5 - 0
iot-module/iot-manager/pom.xml

@@ -49,6 +49,11 @@
             <artifactId>iot-common-oss</artifactId>
             <artifactId>iot-common-oss</artifactId>
         </dependency>
         </dependency>
 
 
+        <dependency>
+            <groupId>cc.iotkit</groupId>
+            <artifactId>iot-common-websocket</artifactId>
+        </dependency>
+
         <dependency>
         <dependency>
             <groupId>cc.iotkit</groupId>
             <groupId>cc.iotkit</groupId>
             <artifactId>iot-temporal-service</artifactId>
             <artifactId>iot-temporal-service</artifactId>

+ 22 - 8
iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/SpaceDeviceController.java

@@ -31,6 +31,7 @@ import cc.iotkit.model.space.Home;
 import cc.iotkit.model.space.Space;
 import cc.iotkit.model.space.Space;
 import cc.iotkit.model.space.SpaceDevice;
 import cc.iotkit.model.space.SpaceDevice;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -67,6 +68,8 @@ public class SpaceDeviceController {
     private DataOwnerService dataOwnerService;
     private DataOwnerService dataOwnerService;
     @Autowired
     @Autowired
     private IUserInfoData userInfoData;
     private IUserInfoData userInfoData;
+    @Autowired
+    private IDeviceManagerService deviceServiceImpl;
 
 
     /**
     /**
      * 我最近使用的设备列表
      * 我最近使用的设备列表
@@ -127,7 +130,7 @@ public class SpaceDeviceController {
         Map<String, Object> property = new HashMap<>();
         Map<String, Object> property = new HashMap<>();
         if (tm != null) {
         if (tm != null) {
             tm.getModel().setEvents(null);
             tm.getModel().setEvents(null);
-            property = JsonUtils.parseObject(JsonUtils.toJsonString(tm), Map.class);
+            property = JsonUtils.parseObject(JsonUtils.toJsonString(tm.getModel()), Map.class);
         }
         }
         Category category = categoryData.findById(product.getCategory());
         Category category = categoryData.findById(product.getCategory());
         DeviceInfo.State state = device.getState();
         DeviceInfo.State state = device.getState();
@@ -175,14 +178,18 @@ public class SpaceDeviceController {
         }
         }
 
 
         List<FindDeviceVo> findDeviceVos = new ArrayList<>();
         List<FindDeviceVo> findDeviceVos = new ArrayList<>();
-        DeviceInfo findDevice = deviceInfoData.findByDeviceName(mac);
-
-        //查找网关下子设备
-        List<DeviceInfo> subDevices = new ArrayList<>();
-        if (findDevice.getParentId() == null) {
-            subDevices = deviceInfoData.findByParentId(findDevice.getDeviceId());
+        DeviceInfo query=new DeviceInfo();
+        query.setDeviceName(mac);
+        List<DeviceInfo> devices = deviceInfoData.findAllByCondition(query);
+        if(devices == null){
+            return findDeviceVos;
         }
         }
-        List<DeviceInfo> devices = new ArrayList<>(subDevices);
+        //查找网关下子设备
+//        List<DeviceInfo> subDevices = new ArrayList<>();
+//        if (findDevice.getParentId() == null) {//如果是网关设备
+//            subDevices = deviceInfoData.findByParentId(findDevice.getDeviceId());
+//        }
+//        List<DeviceInfo> devices = new ArrayList<>(subDevices);
 
 
         //查找空间设备
         //查找空间设备
         for (DeviceInfo device : devices) {
         for (DeviceInfo device : devices) {
@@ -210,6 +217,12 @@ public class SpaceDeviceController {
         return findDeviceVo;
         return findDeviceVo;
     }
     }
 
 
+    @ApiOperation("获取设备详情")
+    @PostMapping("/detail")
+    public DeviceInfo getDetail(@RequestBody @Validated Request<String> request) {
+        return deviceServiceImpl.getDetail(request.getData());
+    }
+
     /**
     /**
      * REMOVE_DEVICE
      * REMOVE_DEVICE
      * 往指定房间中添加设备
      * 往指定房间中添加设备
@@ -239,6 +252,7 @@ public class SpaceDeviceController {
                 .deviceId(deviceId)
                 .deviceId(deviceId)
                 .name(device.getName())
                 .name(device.getName())
                 .homeId(space.getHomeId())
                 .homeId(space.getHomeId())
+                .collect(false)
                 .build();
                 .build();
         spaceDeviceService.save(spaceDevice);
         spaceDeviceService.save(spaceDevice);
 
 

+ 15 - 0
iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/DeferredDataConsumer.java

@@ -12,9 +12,13 @@ package cc.iotkit.manager.service;
 import cc.iotkit.common.constant.Constants;
 import cc.iotkit.common.constant.Constants;
 import cc.iotkit.common.thing.ThingModelMessage;
 import cc.iotkit.common.thing.ThingModelMessage;
 import cc.iotkit.common.utils.JsonUtils;
 import cc.iotkit.common.utils.JsonUtils;
+import cc.iotkit.common.websocket.holder.WebSocketSessionHolder;
+import cc.iotkit.common.websocket.utils.WebSocketUtils;
 import cc.iotkit.model.device.DeviceInfo;
 import cc.iotkit.model.device.DeviceInfo;
+import cc.iotkit.model.space.SpaceDevice;
 import cc.iotkit.mq.ConsumerHandler;
 import cc.iotkit.mq.ConsumerHandler;
 import cc.iotkit.mq.MqConsumer;
 import cc.iotkit.mq.MqConsumer;
+import cn.hutool.core.util.ObjectUtil;
 import lombok.AllArgsConstructor;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.NoArgsConstructor;
@@ -40,6 +44,9 @@ public class DeferredDataConsumer implements ConsumerHandler<ThingModelMessage>,
     private final Map<String, DeferredResultInfo> consumerDeferred = new ConcurrentHashMap<>();
     private final Map<String, DeferredResultInfo> consumerDeferred = new ConcurrentHashMap<>();
     private final DelayQueue<DelayedPush> delayedPushes = new DelayQueue<>();
     private final DelayQueue<DelayedPush> delayedPushes = new DelayQueue<>();
 
 
+    @Autowired
+    private ISpaceDeviceService deviceServiceImpl;
+
     @Autowired
     @Autowired
     private MqConsumer<ThingModelMessage> thingModelMessageConsumer;
     private MqConsumer<ThingModelMessage> thingModelMessageConsumer;
 
 
@@ -104,9 +111,17 @@ public class DeferredDataConsumer implements ConsumerHandler<ThingModelMessage>,
                 ThingModelMessage.TYPE_STATE.equals(type)) {
                 ThingModelMessage.TYPE_STATE.equals(type)) {
             publish(Constants.HTTP_CONSUMER_DEVICE_INFO_TOPIC + msg.getDeviceId(),
             publish(Constants.HTTP_CONSUMER_DEVICE_INFO_TOPIC + msg.getDeviceId(),
                     msg);
                     msg);
+            sendWebSocket(msg);
         }
         }
     }
     }
 
 
+    public void sendWebSocket(ThingModelMessage msg){
+        SpaceDevice deviceInfo=deviceServiceImpl.findByDeviceId(msg.getDeviceId());
+        if(ObjectUtil.isNotNull(deviceInfo)&&ObjectUtil.isNotNull(deviceInfo.getCreateBy())&&WebSocketSessionHolder.existSession(deviceInfo.getCreateBy())){
+            WebSocketUtils.sendMessage(deviceInfo.getCreateBy(),JsonUtils.toJsonString(msg));
+        }
+
+    }
 
 
     @Override
     @Override
     public void run() {
     public void run() {

+ 2 - 2
iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/DeviceManagerServiceImpl.java

@@ -245,13 +245,13 @@ public class DeviceManagerServiceImpl implements IDeviceManagerService {
 
 
     @Override
     @Override
     public DeferredResult addConsumer(String deviceId, String clientId) {
     public DeferredResult addConsumer(String deviceId, String clientId) {
-
+    //修改为通过websocket下发给app
         DeferredResult<ThingModelMessage> result = new DeferredResult<>(0L);
         DeferredResult<ThingModelMessage> result = new DeferredResult<>(0L);
         String uid = AuthUtil.getUserId();
         String uid = AuthUtil.getUserId();
         DeviceInfo deviceInfo = deviceInfoData.findByDeviceId(deviceId);
         DeviceInfo deviceInfo = deviceInfoData.findByDeviceId(deviceId);
         dataOwnerService.checkOwner(deviceInfo);
         dataOwnerService.checkOwner(deviceInfo);
 
 
-        //按用户+客户端ID订阅
+//        按用户+客户端ID订阅
         return deferredDataConsumer.newConsumer(uid + clientId,
         return deferredDataConsumer.newConsumer(uid + clientId,
                 Constants.HTTP_CONSUMER_DEVICE_INFO_TOPIC + deviceId);
                 Constants.HTTP_CONSUMER_DEVICE_INFO_TOPIC + deviceId);
     }
     }

+ 0 - 1
iot-starter/src/main/java/cc/iotkit/web/controller/AuthController.java

@@ -20,7 +20,6 @@ import cn.dev33.satoken.annotation.SaIgnore;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.CollUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiOperation;
-import jakarta.validation.constraints.NotBlank;
 import lombok.RequiredArgsConstructor;
 import lombok.RequiredArgsConstructor;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PostMapping;

+ 2 - 1
iot-starter/src/main/java/cc/iotkit/web/service/SysLoginService.java

@@ -314,12 +314,13 @@ public class SysLoginService {
         // 使用 openid 查询绑定用户 如未绑定用户 则根据业务自行处理 例如 创建默认用户
         // 使用 openid 查询绑定用户 如未绑定用户 则根据业务自行处理 例如 创建默认用户
         UserInfo user=userInfoData.findByUid(openid);
         UserInfo user=userInfoData.findByUid(openid);
         if (ObjectUtil.isNull(user)) {
         if (ObjectUtil.isNull(user)) {
-            log.info("登录用户:{} 不存在.", openid);
+            log.info("小程序用户:{} 不存在,开始初始化", openid);
             user=new UserInfo();
             user=new UserInfo();
             user.setType(UserInfo.USER_TYPE_CLIENT);
             user.setType(UserInfo.USER_TYPE_CLIENT);
             user.setUid(openid);
             user.setUid(openid);
             user.setRoles(Collections.singletonList(Constants.ROLE_CLIENT));
             user.setRoles(Collections.singletonList(Constants.ROLE_CLIENT));
             user.setSecret(AuthUtil.enCryptPwd(Constants.PWD_CLIENT_USER));
             user.setSecret(AuthUtil.enCryptPwd(Constants.PWD_CLIENT_USER));
+            user.setTenantId(tenantId);
             user = userInfoData.save(user);
             user = userInfoData.save(user);
             //添加默认家庭
             //添加默认家庭
             Home home = homeService.save(HomeBo.builder()
             Home home = homeService.save(HomeBo.builder()

+ 5 - 0
iot-starter/src/main/resources/application.yml

@@ -199,6 +199,11 @@ weixin:
   secret: xx
   secret: xx
   authUrl: xx
   authUrl: xx
 
 
+websocket:
+  enabled: true
+  path: /websocket
+  allowedOrigins: '*'
+
 # 插件配置
 # 插件配置
 plugin:
 plugin:
   runMode: prod
   runMode: prod

+ 6 - 0
pom.xml

@@ -230,6 +230,12 @@
                 <version>${iot-iita-core.version}</version>
                 <version>${iot-iita-core.version}</version>
             </dependency>
             </dependency>
 
 
+            <dependency>
+                <groupId>cc.iotkit</groupId>
+                <artifactId>iot-common-websocket</artifactId>
+                <version>${iot-iita-core.version}</version>
+            </dependency>
+
             <dependency>
             <dependency>
                 <groupId>cc.iotkit</groupId>
                 <groupId>cc.iotkit</groupId>
                 <artifactId>iot-common-web</artifactId>
                 <artifactId>iot-common-web</artifactId>