wzh 2 лет назад
Родитель
Сommit
6c2d95fc99

+ 0 - 1
ruoyi-admin/src/main/resources/application-dev.yml

@@ -202,7 +202,6 @@ xss:
 # knif4jAPI测试文档
 knife4j:
   enable: true
-receiveService: http://127.0.0.1:8052
 msgService: http://127.0.0.1:8051
 
 

+ 1 - 3
ruoyi-admin/src/main/resources/application-test.yml

@@ -175,8 +175,7 @@ mybatis-plus:
   # 配置mapper的扫描,找到所有的mapper.xml映射文件
   mapperLocations: classpath*:mapper/**/*Mapper.xml
   # 加载全局的配置文件
-  configLocation: classpath:mybatis/mybatis-config.xml
-
+  configLocation: classpath:mybatis/mybatis-config-test.xml
 # PageHelper分页插件
 pagehelper:
   helperDialect: mysql
@@ -207,6 +206,5 @@ kafka:
   topic: deviceMessage
 productKey: 52
 splitTable: true
-receiveService: http://192.168.20.102:5052
 msgService: http://192.168.20.101:5051
 

+ 2 - 1
ruoyi-admin/src/main/resources/application.yml

@@ -1,3 +1,4 @@
 spring:
   profiles:
-    active: test
+    active: dev
+redisMsgChanel: foreignmsgqueue

+ 21 - 0
ruoyi-admin/src/main/resources/mybatis/mybatis-config-test.xml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE configuration
+PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
+"http://mybatis.org/dtd/mybatis-3-config.dtd">
+<configuration>
+    <!-- 全局参数 -->
+    <settings>
+        <!-- 使全局的映射器启用或禁用缓存 -->
+        <setting name="cacheEnabled"             value="true"   />
+        <!-- 允许JDBC 支持自动生成主键 -->
+        <setting name="useGeneratedKeys"         value="true"   />
+        <!-- 配置默认的执行器.SIMPLE就是普通执行器;REUSE执行器会重用预处理语句(prepared statements);BATCH执行器将重用语句并执行批量更新 -->
+        <setting name="defaultExecutorType"      value="SIMPLE" />
+		<!-- 指定 MyBatis 所用日志的具体实现 -->
+        <setting name="logImpl"                  value="SLF4J"  />
+        <!-- 使用驼峰命名法转换字段 -->
+		<!-- <setting name="mapUnderscoreToCamelCase" value="true"/> -->
+        <setting name="logImpl" value="STDOUT_LOGGING"/>
+	</settings>
+	
+</configuration>

+ 38 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/TmznTool.java

@@ -0,0 +1,38 @@
+package com.ruoyi.common.utils;
+
+import com.ruoyi.common.core.domain.entity.SysRole;
+import com.ruoyi.common.core.domain.entity.SysUser;
+
+import java.util.List;
+
+public class TmznTool {
+
+    /**
+     * 判断是否具有某个角色
+     * @param user
+     * @param rolaName
+     * @return
+     */
+    public static boolean hasRole(SysUser user,String rolaName){
+        List<SysRole> roles = user.getRoles();
+        for (int i = 0; i < roles.size(); i++) {
+            if (roles.get(i).getRoleKey().equals(rolaName)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+    /**
+     * 是否是管理员
+     * @param user
+     * @return
+     */
+    public static boolean isAdmin(SysUser user){
+        return hasRole(user,"admin");
+    }
+
+
+
+}

+ 4 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java

@@ -1,5 +1,6 @@
 package com.ruoyi.framework.config;
 
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cache.annotation.CachingConfigurerSupport;
 import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.context.annotation.Bean;
@@ -18,6 +19,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
 @EnableCaching
 public class RedisConfig extends CachingConfigurerSupport
 {
+
     @Bean
     @SuppressWarnings(value = { "unchecked", "rawtypes" })
     public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
@@ -66,4 +68,6 @@ public class RedisConfig extends CachingConfigurerSupport
                 "end\n" +
                 "return tonumber(current);";
     }
+
+
 }

+ 9 - 0
ruoyi-iot/src/main/java/com/ruoyi/iot/controller/DeviceController.java

@@ -83,6 +83,15 @@ public class DeviceController {
         return AjaxResult.success(deviceInfoByQrcode);
     }
 
+
+    @PostMapping("/unbindDevice")
+    @ApiOperation("解绑设备")
+    public AjaxResult unbindDevice(@RequestParam("qrcode") String qrcode){
+        Long userId = getLoginUser().getUserId();
+        WeitiandiDeviceInfo deviceInfoByQrcode = iWeitiandiDeviceInfoService.unbindDevice(qrcode, userId);
+        return AjaxResult.success(deviceInfoByQrcode);
+    }
+
     @PostMapping("/deviceList")
     @ApiOperation("设备列表")
     public AjaxResult deviceList(){

+ 5 - 0
ruoyi-iot/src/main/java/com/ruoyi/iot/domain/TChargeRecord.java

@@ -35,4 +35,9 @@ public class TChargeRecord implements Serializable {
 
     /** 备注信息 */
     private String charge_desc;
+
+    /**
+     * 二维码ID
+     */
+    private String sn;
 }

+ 0 - 7
ruoyi-iot/src/main/java/com/ruoyi/iot/queue/MsgQueue.java

@@ -18,8 +18,6 @@ public class MsgQueue extends Thread implements ApplicationRunner {
 
     private BlockingQueue<String> queue = new ArrayBlockingQueue(1024);
 
-    @Value("${receiveService}")
-    private String receiveService;
     @Autowired
     private TaskRunner taskRunner;
     public void add(String object){
@@ -47,11 +45,6 @@ public class MsgQueue extends Thread implements ApplicationRunner {
     @Override
     public void run(ApplicationArguments args) throws Exception {
             this.start();
-            try {
-                HutoolHttpUtil.get(receiveService+"/receive/reset",new HashMap<>());
-            }catch (Exception e){
-                log.error("连接消息接收服务器失败",e);
-            }
             log.info("处理器启动");
     }
 }

+ 5 - 1
ruoyi-iot/src/main/java/com/ruoyi/iot/queue/TaskRunner.java

@@ -1,6 +1,7 @@
 package com.ruoyi.iot.queue;
 
 import com.alibaba.fastjson2.JSONObject;
+import com.ruoyi.iot.service.ITChargeRecordService;
 import com.ruoyi.iot.service.ITDevicePlanService;
 import com.ruoyi.iot.socket.WebSocket;
 import lombok.extern.slf4j.Slf4j;
@@ -14,6 +15,9 @@ public class TaskRunner {
     @Autowired
     private ITDevicePlanService itDevicePlanService;
 
+    @Autowired
+    private ITChargeRecordService itChargeRecordService;
+
     @Async("myTaskAsyncPool")
     public void handlerMsg(String s) {
         try {
@@ -38,7 +42,7 @@ public class TaskRunner {
                 }
 
                 if(type == 113){
-//                    itChargeRecordService.endChargeNotice(imei);
+                    itChargeRecordService.endChargeNotice(imei);
                 }
             }
 //            /**

+ 44 - 0
ruoyi-iot/src/main/java/com/ruoyi/iot/redis/RedisMessageListener.java

@@ -0,0 +1,44 @@
+package com.ruoyi.iot.redis;
+
+import com.ruoyi.iot.queue.MsgQueue;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.data.redis.connection.Message;
+import org.springframework.data.redis.connection.MessageListener;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.listener.ChannelTopic;
+import org.springframework.data.redis.listener.RedisMessageListenerContainer;
+import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
+import org.springframework.stereotype.Component;
+
+@Component
+public class RedisMessageListener implements MessageListener {
+    @Autowired
+    private MsgQueue msgQueue;
+    @Override
+    public void onMessage(Message message, byte[] pattern) {
+        String channel = new String(message.getChannel());
+        String s = new String(message.getBody());
+        s = s.substring(1,s.length()-1);
+        s = s.replace("\\", "");
+        msgQueue.add(s);
+
+    }
+    @Bean
+    public MessageListenerAdapter messageListenerAdapter(RedisMessageListener redisMessageListener) {
+        return new MessageListenerAdapter(redisMessageListener);
+    }
+
+    @Bean
+    public RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter) {
+        final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
+        container.setConnectionFactory(connectionFactory);
+        container.addMessageListener(messageListenerAdapter, new ChannelTopic(redisMsgChanel));
+        return container;
+    }
+
+
+    @Value("${redisMsgChanel}")
+    private String redisMsgChanel;
+}

+ 2 - 0
ruoyi-iot/src/main/java/com/ruoyi/iot/service/IWeitiandiDeviceInfoService.java

@@ -22,4 +22,6 @@ public interface IWeitiandiDeviceInfoService extends IService<WeitiandiDeviceInf
     List<WeitiandiDeviceInfo> listDevice(Long userId);
 
     WeitiandiDeviceInfo bindDevice(String qrcode, Long userId);
+
+    WeitiandiDeviceInfo unbindDevice(String qrcode, Long userId);
 }

+ 10 - 0
ruoyi-iot/src/main/java/com/ruoyi/iot/service/impl/TChargeRecordServiceImpl.java

@@ -4,9 +4,11 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.iot.domain.TChargeRecord;
+import com.ruoyi.iot.domain.WeitiandiDeviceInfo;
 import com.ruoyi.iot.mapper.TChargeRecordMapper;
 import com.ruoyi.iot.service.DeviceControlerService;
 import com.ruoyi.iot.service.ITChargeRecordService;
+import com.ruoyi.iot.service.IWeitiandiDeviceInfoService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -19,12 +21,20 @@ public class TChargeRecordServiceImpl extends ServiceImpl<TChargeRecordMapper, T
 
     @Autowired
     private DeviceControlerService deviceControlerService;
+
+    @Autowired
+    private IWeitiandiDeviceInfoService iWeitiandiDeviceInfoService;
     @Override
     public void startCharge(String deviceId, String ccid, Long userId,Integer port) {
         deviceControlerService.startCharge(deviceId,ccid,port);
         TChargeRecord tChargeRecord = new TChargeRecord();
         tChargeRecord.setCreateTime(LocalDateTime.now());
         tChargeRecord.setDeviceId(deviceId);
+        LambdaQueryWrapper<WeitiandiDeviceInfo> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
+        objectLambdaQueryWrapper.eq(WeitiandiDeviceInfo::getImei,deviceId);
+        objectLambdaQueryWrapper.last(" limit 1");
+        WeitiandiDeviceInfo deviceInfo = iWeitiandiDeviceInfoService.getOne(objectLambdaQueryWrapper);
+        tChargeRecord.setSn(deviceInfo.getQrcode());
         tChargeRecord.setStatus(0L);
         tChargeRecord.setUserId(userId);
         this.save(tChargeRecord);

+ 24 - 0
ruoyi-iot/src/main/java/com/ruoyi/iot/service/impl/WeitiandiDeviceInfoServiceImpl.java

@@ -1,8 +1,11 @@
 package com.ruoyi.iot.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.core.domain.model.LoginUser;
+import com.ruoyi.common.utils.TmznTool;
 import com.ruoyi.iot.domain.WeitiandiDeviceInfo;
 import com.ruoyi.iot.mapper.WeitiandiDeviceInfoMapper;
 import com.ruoyi.iot.service.IWeitiandiDeviceInfoService;
@@ -12,6 +15,8 @@ import org.springframework.stereotype.Service;
 import java.util.List;
 import java.util.Map;
 
+import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;
+
 /**
  * <p>
  *  服务实现类
@@ -58,4 +63,23 @@ public class WeitiandiDeviceInfoServiceImpl extends ServiceImpl<WeitiandiDeviceI
         }
         return null;
     }
+
+    @Override
+    public WeitiandiDeviceInfo unbindDevice(String qrcode, Long userId) {
+        LoginUser loginUser = getLoginUser();
+        Long loginUserUserId = loginUser.getUserId();
+
+        LambdaQueryWrapper<WeitiandiDeviceInfo> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
+        objectLambdaQueryWrapper.eq(WeitiandiDeviceInfo::getQrcode,qrcode);
+        WeitiandiDeviceInfo deviceInfoByQrcode = getOne(objectLambdaQueryWrapper);
+        Long user_id = deviceInfoByQrcode.getUserId();
+        if(loginUserUserId == user_id || TmznTool.isAdmin(loginUser.getUser())) {
+            deviceInfoByQrcode.setUserId(null);
+            UpdateWrapper<WeitiandiDeviceInfo> updateWrapper = new UpdateWrapper<>();
+            updateWrapper.eq("id", deviceInfoByQrcode.getId()).set("user_id", null);
+            this.update(updateWrapper);
+            return deviceInfoByQrcode;
+        }
+        return null;
+    }
 }