|
@@ -12,9 +12,7 @@ import com.care.common.cache.RedisKeyConstant;
|
|
|
import com.care.common.cache.RedisUtil;
|
|
|
import com.care.common.entity.*;
|
|
|
|
|
|
-import com.care.common.enums.MqttMsgStatusEnum;
|
|
|
-import com.care.common.enums.OrderStatusEnum;
|
|
|
-import com.care.common.enums.OrderTypeEnum;
|
|
|
+import com.care.common.enums.*;
|
|
|
import com.care.common.service.*;
|
|
|
import com.care.common.vo.device.MqttMsgVO;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -25,6 +23,7 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.Date;
|
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
|
|
@@ -53,6 +52,12 @@ public class MqttMsgService {
|
|
|
private MqttMsgRedisService mqttMsgRedisService;
|
|
|
@Resource
|
|
|
private RedisUtil redisUtil;
|
|
|
+ @Resource
|
|
|
+ private CareToiletInoutDetailService careToiletInoutDetailService;
|
|
|
+ @Resource
|
|
|
+ private CareBedroomInbedDetailService careBedroomInbedDetailService;
|
|
|
+ @Resource
|
|
|
+ private CareWalkingDistanceDetailService careWalkingDistanceDetailService;
|
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(MqttMsgService.class);
|
|
@@ -89,7 +94,8 @@ public class MqttMsgService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if(!"BreathHeartRate".equals(type) && !"FallDown".equals(type) && !"StayTooLong".equals(type)){
|
|
|
+ if(!"BreathHeartRate".equals(type) && !"FallDown".equals(type) && !"StayTooLong".equals(type)
|
|
|
+ && !"OptInOut".equals(type) && !"PeopleEvent".equals(type)){
|
|
|
// logger.error("不是要处理的消息");
|
|
|
return;
|
|
|
}
|
|
@@ -124,11 +130,27 @@ public class MqttMsgService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if( careDevice.getHouseId() == null ) {
|
|
|
+ if (careDevice.getHouseId() == null) {
|
|
|
logger.error("设备没有配置房屋地址");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ //活动信息
|
|
|
+ if ("PeopleEvent".equals(type)) {
|
|
|
+ careMqttMsg.setStatus(MqttMsgStatusEnum.HANDLED.getValue());
|
|
|
+ this.careMqttMsgService.save(careMqttMsg);
|
|
|
+ handlePeopleEvent(msg,careDevice,date);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //进出事件
|
|
|
+ if ("OptInOut".equals(type)) {
|
|
|
+ careMqttMsg.setStatus(MqttMsgStatusEnum.HANDLED.getValue());
|
|
|
+ this.careMqttMsgService.save(careMqttMsg);
|
|
|
+ handleOptInOut(msg,careDevice,date);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
handleOrderEvent(map,type,careDevice,date,careMqttMsg);
|
|
|
|
|
|
} catch (Exception e){
|
|
@@ -136,6 +158,129 @@ 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) target.get("x");
|
|
|
+ double y = (double) target.get("y");
|
|
|
+ if (cls == 1) { //是人
|
|
|
+ //先查是否存在
|
|
|
+ String content = (String)redisUtil.hget(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO,careDevice.getDevCode()+ "_" + tid);
|
|
|
+ if (content != null) {//之前有存储redis
|
|
|
+ //计算当前的和存储的之间距离
|
|
|
+ double distance = getDistance(Double.parseDouble(content.split("_")[1]),x,Double.parseDouble(content.split("_")[2]),y);
|
|
|
+ //加上之前累积的距离
|
|
|
+ double distanceTotal = distance + Double.parseDouble(content.split("_")[3]);
|
|
|
+
|
|
|
+ //存储行走距离明细表
|
|
|
+ CareWalkingDistanceDetail careWalkingDistanceDetail = new CareWalkingDistanceDetail();
|
|
|
+ careWalkingDistanceDetail.setDevId(careDevice.getId());
|
|
|
+ careWalkingDistanceDetail.setTid(tid);
|
|
|
+ careWalkingDistanceDetail.setDevScene(careDevice.getDevScene());
|
|
|
+ careWalkingDistanceDetail.setWalkingDistance(distance);
|
|
|
+ careWalkingDistanceDetail.setCreateTime(date);
|
|
|
+ careWalkingDistanceDetail.setModifyTime(date);
|
|
|
+ careWalkingDistanceDetailService.save(careWalkingDistanceDetail);
|
|
|
+
|
|
|
+ long timeTotal = (date.getTime() - Long.parseLong(content.split("_")[0])) + Long.parseLong(content.split("_")[4]);
|
|
|
+
|
|
|
+ if ( timeTotal < 1000) { //累计时间不够一秒
|
|
|
+ //更新redis
|
|
|
+ redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, careDevice.getDevCode() + "_" + tid, System.currentTimeMillis() + "_" + x + "_" + y + "_" + distanceTotal +"_" + timeTotal, RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_TIME);
|
|
|
+
|
|
|
+ } else { //够一秒
|
|
|
+ if ( distanceTotal > 0.3) {
|
|
|
+ moveState = DeviceMoveStateEnum.MOVE.getValue();
|
|
|
+ }
|
|
|
+ //清除redis
|
|
|
+ redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, careDevice.getDevCode() + "_" + tid, null, RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_TIME);
|
|
|
+ }
|
|
|
+ } else { //之前没有存储redis,则存储redis
|
|
|
+ redisUtil.hset(RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO, careDevice.getDevCode() + "_" + tid, System.currentTimeMillis() + "_" + x + "_" + y + "_" + 0 + "_" + 0, RedisKeyConstant.PEOPLEEVENT_ACTIVE_INFO_TIME);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //更新数据库dev
|
|
|
+ careDevice.setMoveStatus(moveState);
|
|
|
+ careDeviceService.updateById(careDevice);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)));
|
|
|
+ return distance;
|
|
|
+ }
|
|
|
+ private void handleOptInOut(Map msg,CareDevice careDevice,Date date){
|
|
|
+ Integer event = (Integer) msg.get("event"); //1 进事件,2 出事件
|
|
|
+ Integer type = (Integer) msg.get("type");
|
|
|
+ Integer subType = (Integer) msg.get("subType");
|
|
|
+ Integer tid = (Integer) msg.get("tid");
|
|
|
+ if(DeviceSceneEnum.TOILET.getValue().equals(careDevice.getDevScene()) && type == 2 ){ //type字段的2代表进出事件,卫生间
|
|
|
+ if(subType == 1) { //subtype字段的1代表门
|
|
|
+ if(event == 1) {
|
|
|
+ //进事件,先存起来
|
|
|
+ redisUtil.hset(RedisKeyConstant.OPTINOUT_TOILET_INFO, careDevice.getDevCode()+ "_" + tid, System.currentTimeMillis(), RedisKeyConstant.OPTINOUT_TOILET_INFO_TIME);
|
|
|
+ } else {//出事件,
|
|
|
+ //先查是否存在进事件
|
|
|
+ Long inTime = (Long)redisUtil.hget(RedisKeyConstant.OPTINOUT_TOILET_INFO,careDevice.getDevCode()+ "_" + tid);
|
|
|
+ if(inTime != null) { //存在进事件
|
|
|
+ CareToiletInoutDetail careToiletInoutDetail = new CareToiletInoutDetail();
|
|
|
+ careToiletInoutDetail.setDevId(careDevice.getId());
|
|
|
+ careToiletInoutDetail.setTid(tid);
|
|
|
+ careToiletInoutDetail.setInTime(new Date(inTime));
|
|
|
+ careToiletInoutDetail.setOutTime(date);
|
|
|
+ careToiletInoutDetail.setDuration((date.getTime() - inTime) / 1000);
|
|
|
+ careToiletInoutDetail.setCreateTime(date);
|
|
|
+ careToiletInoutDetail.setModifyTime(date);
|
|
|
+ careToiletInoutDetailService.save(careToiletInoutDetail);
|
|
|
+ redisUtil.hset(RedisKeyConstant.OPTINOUT_TOILET_INFO, careDevice.getDevCode()+ "_" + tid, null, RedisKeyConstant.OPTINOUT_TOILET_INFO_TIME);
|
|
|
+ } else { //不存在进事件
|
|
|
+ //do nothing
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if(DeviceSceneEnum.BEDROOM.getValue().equals(careDevice.getDevScene()) && type == 1 ) { //type字段的1代表上下床事件,卧室
|
|
|
+ if(subType == 1) { //subtype字段的1代表床
|
|
|
+ if(event == 1) {
|
|
|
+ //上床事件,先存起来
|
|
|
+ redisUtil.hset(RedisKeyConstant.OPTINOUT_BEDROOM_INFO, careDevice.getDevCode()+ "_" + tid, System.currentTimeMillis(), RedisKeyConstant.OPTINOUT_BEDROOM_INFO_TIME);
|
|
|
+ //更新数据库dev
|
|
|
+ careDevice.setInbedStatus(DeviceInbedStateEnum.INBED.getValue());
|
|
|
+ careDeviceService.updateById(careDevice);
|
|
|
+ } else {//下床事件,
|
|
|
+ //先查是否存在上床事件
|
|
|
+ Long inTime = (Long)redisUtil.hget(RedisKeyConstant.OPTINOUT_BEDROOM_INFO,careDevice.getDevCode()+ "_" + tid);
|
|
|
+ if(inTime != null) { //存在上床事件
|
|
|
+ CareBedroomInbedDetail careBedroomInbedDetail = new CareBedroomInbedDetail();
|
|
|
+ careBedroomInbedDetail.setDevId(careDevice.getId());
|
|
|
+ careBedroomInbedDetail.setTid(tid);
|
|
|
+ careBedroomInbedDetail.setInTime(new Date(inTime));
|
|
|
+ careBedroomInbedDetail.setOutTime(date);
|
|
|
+ careBedroomInbedDetail.setDuration((date.getTime() - inTime) / 1000);
|
|
|
+ careBedroomInbedDetail.setCreateTime(date);
|
|
|
+ careBedroomInbedDetail.setModifyTime(date);
|
|
|
+ careBedroomInbedDetailService.save(careBedroomInbedDetail);
|
|
|
+ redisUtil.hset(RedisKeyConstant.OPTINOUT_BEDROOM_INFO, careDevice.getDevCode()+ "_" + tid, null, RedisKeyConstant.OPTINOUT_BEDROOM_INFO_TIME);
|
|
|
+ } else { //不存在上床事件
|
|
|
+ //do nothing
|
|
|
+ }
|
|
|
+ //更新数据库dev
|
|
|
+ careDevice.setInbedStatus(DeviceInbedStateEnum.OUTBED.getValue());
|
|
|
+ careDeviceService.updateById(careDevice);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void handleOrderEvent(Map map,String type,CareDevice careDevice,Date date, CareMqttMsg careMqttMsg){
|
|
|
String key = RedisKeyConstant.CREATE_ORDER + ":" + careDevice.getId();
|
|
|
String requestId = UUID.randomUUID().toString();
|