suntianwu 3 سال پیش
والد
کامیت
db3ea83ce1

+ 8 - 5
src/main/java/com/care/common/cache/RedisKeyConstant.java

@@ -34,7 +34,10 @@ public class RedisKeyConstant {
     //工单处理,同一工单仅1个人处理
     public static final String DO_ORDER = "SEAT_DO_ORDER:LOCK";
 
-    //生成工单,同一设备同时只能生成一个工单
+//    //生成工单,同一设备同时只能生成一个工单
+//    public static final String CREATE_ORDER = "CREATE_ORDER:LOCK";
+
+    //PeopleEvent信息处理
     public static final String CREATE_ORDER = "CREATE_ORDER:LOCK";
 
     //设备监测信息
@@ -81,13 +84,13 @@ public class RedisKeyConstant {
     public static final String OPTINOUT_BEDROOM_INFO = "OPTINOUT:BEDROOM";
     public static final int OPTINOUT_BEDROOM_INFO_TIME = 3600 * 24 * 2;
 
-    //活动信息 1天
+    //活动信息 20秒
     public static final String PEOPLEEVENT_ACTIVE_INFO = "PEOPLEEVENT:ACTIVE";
-    public static final int PEOPLEEVENT_ACTIVE_INFO_TIME = 3600 * 24;
+    public static final int PEOPLEEVENT_ACTIVE_INFO_TIME = 20;
 
-    //活动信息最近index 1天
+    //活动信息最近index 8小时
     public static final String PEOPLEEVENT_ACTIVE_INFO_LAST_INDEX = "PEOPLEEVENT:ACTIVE_LAST_INDEX";
-    public static final int PEOPLEEVENT_ACTIVE_INFO_ACTIVE_LAST_INDEX_TIME = 3600 * 24;
+    public static final int PEOPLEEVENT_ACTIVE_INFO_ACTIVE_LAST_INDEX_TIME = 3600 * 8;
 
     // 与安服务平台 -> 小程序, QRCODE
     public static final String YUANSERVICE_QRCODE = "YUANSERVICE:QRCODE";

+ 76 - 61
src/main/java/com/care/mqtt/service/MqttMsgService.java

@@ -15,14 +15,12 @@ import com.care.common.entity.*;
 import com.care.common.enums.*;
 import com.care.common.service.*;
 import com.care.common.vo.device.MqttMsgVO;
-import io.swagger.models.auth.In;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.math.BigDecimal;
 import java.util.Date;
 
 import java.util.List;
@@ -163,75 +161,92 @@ public class MqttMsgService {
     }
 
     private void handlePeopleEvent(Map msg,CareDevice careDevice,Date date){
-        Integer amount = (Integer) msg.get("amount");
-        List<Map> targets = (List) msg.get("targets");
-        String moveState = DeviceMoveStateEnum.STATIC.getValue();
-        if(amount == 0){ //无人
-            moveState = DeviceMoveStateEnum.NONE.getValue();
-        } else { //有人,静止或活动
-            for(Map target : targets) {
-                Integer tid = (Integer) target.get("tid");
-                Integer cls = (Integer) target.get("cls");
-                double x = Double.parseDouble(String.valueOf(target.get("x"))) ;
-                double y = Double.parseDouble(String.valueOf(target.get("y"))) ;
-                if (cls == 1) { //是人
-                    //先查是否存在
-                    Integer lastIndex = (Integer)redisUtil.hget(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_LAST_INDEX,careDevice.getDevCode() + "_" + tid);
-                    logger.warn("lastIndex:{}",lastIndex);
-                    if(lastIndex == null) {//之前没有存储redis,则存储第一个
-                        redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, 1 + "@" + careDevice.getDevCode() + "_" + tid , System.currentTimeMillis() +  "_" + x + "_" + y , RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_TIME);
-                        redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_LAST_INDEX, careDevice.getDevCode() + "_" + tid , 1 , RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_ACTIVE_LAST_INDEX_TIME);
-                    } else{
-                        //计算当前和最近之间时间,若超过1秒,删除之前的存储的,重新计数存储当前的
-                        String lastContent = (String)redisUtil.hget(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO,lastIndex + "@" + careDevice.getDevCode() + "_" + tid );
-                        logger.warn("lastIndex:{},lastContent:{}",lastIndex,lastContent);
-                        long duration = (date.getTime() - Long.parseLong(lastContent.split("_")[0]));
-                        logger.warn("lastIndex:{},duration:{}",lastIndex,duration);
-                        if (duration > 3000) { //超过1秒,删除之前的存储的,从头开始
-                            logger.warn("超过3000,lastIndex:{},duration:{}",lastIndex,duration);
-                            delBeforeContent(lastIndex,careDevice.getDevCode(),tid);
-                            redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, 1 + "@" + careDevice.getDevCode() + "_" + tid , System.currentTimeMillis() +  "_" + x + "_" + y, RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_TIME);
-                            redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_LAST_INDEX, careDevice.getDevCode() + "_" + tid , 1 , RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_ACTIVE_LAST_INDEX_TIME);
-                        } else {
-                            //存储以供后面计算
-                            redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, (lastIndex + 1) + "@" + careDevice.getDevCode() + "_" + tid , System.currentTimeMillis() +  "_" + x + "_" + y, RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_TIME);
-                            redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_LAST_INDEX, careDevice.getDevCode() + "_" + tid , lastIndex + 1 , RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_ACTIVE_LAST_INDEX_TIME);
-                            if(lastIndex >= 9) {
-                                int currIndex = lastIndex + 1;//当前是第10个或以上 开始计算
-                                //求前5个点平均坐标和后5个点的平均坐标,二者之间的距离即结果
-                                double distance = getDistance(currIndex,careDevice.getDevCode(),tid);
-                                logger.warn("计算结果:lastIndex:{},duration:{},distance{}",lastIndex,duration,distance);
-                                if(distance > 0.3){
-                                    moveState = DeviceMoveStateEnum.MOVE.getValue();
-                                    //存储行走距离明细表
-                                    CareWalkingDistanceDetail careWalkingDistanceDetail = new CareWalkingDistanceDetail();
-                                    careWalkingDistanceDetail.setDevId(careDevice.getId());
-                                    careWalkingDistanceDetail.setTid(tid);
-                                    careWalkingDistanceDetail.setDevScene(careDevice.getDevScene());
-                                    careWalkingDistanceDetail.setWalkingDistance(distance);
-                                    //careWalkingDistanceDetail.setWalkingDuration(duration); 时间计算暂时不要
-                                    careWalkingDistanceDetail.setCreateTime(date);
-                                    careWalkingDistanceDetail.setModifyTime(date);
-                                    careWalkingDistanceDetailService.save(careWalkingDistanceDetail);
+//        String key = RedisKeyConstant.CREATE_ORDER + ":" + careDevice.getId();
+//        String requestId = UUID.randomUUID().toString();
+//        boolean result = redisUtil.tryLock(key,requestId,10);
+//        try {
+//            if (result) {
+                Integer amount = (Integer) msg.get("amount");
+                List<Map> targets = (List) msg.get("targets");
+                String moveState = DeviceMoveStateEnum.STATIC.getValue();
+                if(amount == 0){ //无人
+                    moveState = DeviceMoveStateEnum.NONE.getValue();
+                } else { //有人,静止或活动
+                    for(Map target : targets) {
+                        Integer tid = (Integer) target.get("tid");
+                        Integer cls = (Integer) target.get("cls");
+                        double x = Double.parseDouble(String.valueOf(target.get("x"))) ;
+                        double y = Double.parseDouble(String.valueOf(target.get("y"))) ;
+                        if (cls == 1) { //是人
+                            //先查是否存在
+                            Integer lastIndex = (Integer)redisUtil.hget(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_LAST_INDEX,careDevice.getDevCode() + "_" + tid);
+                            logger.warn("lastIndex:{}",lastIndex);
+                            if(lastIndex == null) {//之前没有存储redis,则存储第一个
+                                redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, 1 + "@" + careDevice.getDevCode() + "_" + tid , System.currentTimeMillis() +  "_" + x + "_" + y , RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_TIME);
+                                redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_LAST_INDEX, careDevice.getDevCode() + "_" + tid , 1 , RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_ACTIVE_LAST_INDEX_TIME);
+                            } else{
+                                //计算当前和最近之间时间,若超过1秒,删除之前的存储的,重新计数存储当前的
+                                String lastContent = (String)redisUtil.hget(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO,lastIndex + "@" + careDevice.getDevCode() + "_" + tid );
+                                logger.warn("lastIndex:{},lastContent:{}",lastIndex,lastContent);
+                                long duration = (date.getTime() - Long.parseLong(lastContent.split("_")[0]));
+                                logger.warn("lastIndex:{},duration:{}",lastIndex,duration);
+                                if (duration > 1000) { //超过1秒,删除之前的存储的,从头开始
+                                    logger.warn("超过3000,lastIndex:{},duration:{}",lastIndex,duration);
+                                    delBeforeContent(lastIndex,careDevice.getDevCode(),tid);
+                                    redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, 1 + "@" + careDevice.getDevCode() + "_" + tid , System.currentTimeMillis() +  "_" + x + "_" + y, RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_TIME);
+                                    redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_LAST_INDEX, careDevice.getDevCode() + "_" + tid , 1 , RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_ACTIVE_LAST_INDEX_TIME);
+                                } else {
+                                    //存储以供后面计算
+                                    redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, (lastIndex + 1) + "@" + careDevice.getDevCode() + "_" + tid , System.currentTimeMillis() +  "_" + x + "_" + y, RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_TIME);
+                                    redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_LAST_INDEX, careDevice.getDevCode() + "_" + tid , lastIndex + 1 , RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_ACTIVE_LAST_INDEX_TIME);
+                                    if(lastIndex >= 9) {
+                                        int currIndex = lastIndex + 1;//当前是第10个或以上 开始计算
+                                        //求前5个点平均坐标和后5个点的平均坐标,二者之间的距离即结果
+                                        double distance = getDistance(currIndex,careDevice.getDevCode(),tid);
+                                        logger.warn("计算结果:lastIndex:{},duration:{},distance{}",lastIndex,duration,distance);
+                                        if(distance > 0.3){
+                                            moveState = DeviceMoveStateEnum.MOVE.getValue();
+                                            //存储行走距离明细表
+                                            CareWalkingDistanceDetail careWalkingDistanceDetail = new CareWalkingDistanceDetail();
+                                            careWalkingDistanceDetail.setDevId(careDevice.getId());
+                                            careWalkingDistanceDetail.setTid(tid);
+                                            careWalkingDistanceDetail.setDevScene(careDevice.getDevScene());
+                                            careWalkingDistanceDetail.setWalkingDistance(distance);
+                                            //careWalkingDistanceDetail.setWalkingDuration(duration); 时间计算暂时不要
+                                            careWalkingDistanceDetail.setCreateTime(date);
+                                            careWalkingDistanceDetail.setModifyTime(date);
+                                            careWalkingDistanceDetailService.save(careWalkingDistanceDetail);
+                                        }
+                                    }
                                 }
                             }
                         }
                     }
                 }
-            }
-        }
 
-        if(!moveState.equals(careDevice.getMoveStatus())){
-            //更新数据库dev
-            careDevice.setMoveStatus(moveState);
-            careDeviceService.updateById(careDevice);
-        }
+                if(!moveState.equals(careDevice.getMoveStatus())){
+                    //更新数据库dev
+                    careDevice.setMoveStatus(moveState);
+                    careDeviceService.updateById(careDevice);
+                }
+//            } else {
+//                try {
+//                    Thread.sleep(3000);
+//                    handlePeopleEvent(msg,careDevice,date);
+//                } catch (Exception e){
+//                    e.printStackTrace();
+//                }
+//
+//            }
+//        }  finally {
+//            redisUtil.releaseLock(key,requestId);
+//        }
 
     }
 
     private void delBeforeContent(int lastIndex,String devCode,Integer tid) {
         for(int i = lastIndex; i >= 1; i--) {
-            redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, i + "@" + devCode + "_" + tid, null, RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_TIME);
+            redisUtil.hdel(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, i + "@" + devCode + "_" + tid);
         }
       }
 
@@ -267,7 +282,7 @@ public class MqttMsgService {
     }
 
     private double getDistance(double x1, double y1,double x2, double y2) {
-        double distance =  Math.sqrt(Math.abs((x1 - x2)*(x1 - x2))+Math.abs((y1 - y2)*(y1 - y2)));
+        double distance =  Math.sqrt(Math.abs((x1 - x2)*(x1 - x2)) + Math.abs((y1 - y2)*(y1 - y2)));
         return distance;
     }