IntelligentPlant.zip
立即下载
资源介绍:
IntelligentPlant.zip
package com.mergecloud.plant.modules.device.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mergecloud.plant.common.base.BaseEntity;
import com.mergecloud.plant.common.constant.CommonConstant;
import com.mergecloud.plant.common.utils.RedisUtil;
import com.mergecloud.plant.common.utils.StrUtils;
import com.mergecloud.plant.common.vo.Result;
import com.mergecloud.plant.modules.alarm.entity.AlarmEntity;
import com.mergecloud.plant.modules.device.entity.DeviceEntity;
import com.mergecloud.plant.modules.device.mapper.DeviceMapper;
import com.mergecloud.plant.modules.device.service.IDeviceProtocolService;
import com.mergecloud.plant.modules.device.utils.CustomUtils;
import com.mergecloud.plant.modules.device.utils.DeviceUtils;
import com.mergecloud.plant.modules.device.vo.ChannelInfoVo;
import com.mergecloud.plant.modules.device.vo.DevicePlant;
import com.mergecloud.plant.modules.device.vo.WarnVo;
import com.mergecloud.plant.modules.house.entity.HouseEntity;
import com.mergecloud.plant.modules.house.mapper.HouseMapper;
import com.mergecloud.plant.modules.house.vo.HouseAreaVo;
import com.mergecloud.plant.modules.wxmp.service.IMpService;
import com.mergecloud.plant.netty.protocol.Packet;
import com.mergecloud.plant.netty.protocol.command.Command;
import com.mergecloud.plant.netty.protocol.response.SendCmdOrResponse;
import com.mergecloud.plant.netty.session.Session;
import com.mergecloud.plant.netty.util.SessionUtil;
import com.mergecloud.plant.netty.util.Utilty;
import com.mergecloud.plant.modules.websocket.WebSocket;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.Executor;
import java.lang.String;
import static com.mergecloud.plant.common.constant.CacheConstant.HEART_CHANNEL_ROUTE_CACHE;
/**
* DeviceProtocolServiceImpl: 协议处理
*
* @author 宋强
* @version V1.0
* @date 2020/8/31 8:15 下午
*/
@Slf4j
@Service
public class DeviceProtocolServiceImpl extends ServiceImpl implements IDeviceProtocolService {
@Resource
private RedisUtil redisUtil;
@Resource
private WebSocket webSocket;
@Resource
private DeviceUtils deviceUtils;
@Resource
private Executor asyncServiceExecutor;
@Resource
private HouseMapper houseMapper;
@Resource
private IMpService mpService;
/**
* 不存在设备mac地址时
*/
private static final String NO_DEVICE_MAC = "000000000000";
@Override
public void dealWithTask(ChannelHandlerContext ctx, Packet msg) {
String sourceData = msg.getSourceData();
String cmd = msg.getCommand();
Session session = null;
if (ObjectUtil.isNotNull(ctx)) {
session = SessionUtil.getSession(ctx.channel());
}
//设备离线
if (msg.getCommand().equals(Command.DISCONNECT)) {
DeviceEntity device = this.lambdaQuery()
.select(DeviceEntity::getHouseId, DeviceEntity::getId)
.eq(DeviceEntity::getDeviceId, msg.getDeviceId())
.one();
if (ObjectUtil.isNotNull(device)) {
device.setIsOnline(CommonConstant.STATUS_0);
this.updateById(device);
//检查大棚区域下其他设备,如果其他设备中没有网关控制器,需要将其他设备全部设置为离线状态,
// 如果里面还有网关,只需要将该网关下的传感器设置为离线状态
if (StrUtils.isNotBlank(device.getHouseId())) {
List list = this.lambdaQuery()
.select(DeviceEntity::getId, DeviceEntity::getDeviceId, DeviceEntity::getPid, DeviceEntity::getType)
.eq(DeviceEntity::getHouseId, device.getHouseId())
.list();
//将控制器和传感器进行分组
Map> map = new HashMap<>();
//是否存在单纯的控制器
boolean hasController = false;
for (DeviceEntity item : list) {
if (item.getType().equals(CommonConstant.DEVICE_TYPE_GATEWAY)) {
map.computeIfAbsent(item.getDeviceId(), k -> new LinkedList<>());
} else if (item.getType().equals(CommonConstant.DEVICE_TYPE_CONTROLLER)) {
hasController = true;
map.computeIfAbsent(item.getDeviceId(), k -> new LinkedList<>());
} else if (item.getType().equals(CommonConstant.DEVICE_TYPE_SENSOR)) {
map.computeIfAbsent(item.getPid(), k -> new LinkedList<>());
item.setIsOnline(CommonConstant.STATUS_0);
map.get(item.getPid()).add(item);
}
}
if (CollectionUtil.isNotEmpty(map)) {
if (hasController) {
// 一般规定 一个大棚下面 如果存在非网关控制器,说明只有一个网关设备 可以将其他设备全部设置为离线
for (DeviceEntity deviceEntity : list) {
deviceEntity.setIsOnline(CommonConstant.STATUS_0);
deviceUtils.updateKv(device.getDeviceId(), CommonConstant.ONLINE, CommonConstant.STATUS_0);
}
this.updateBatchById(list);
} else {
//如果不存在控制器,则只需要将该网关相关的传感器变更为离线状态
for (String key : map.keySet()) {
if (key.equals(msg.getDeviceId())) {
List updateList = map.get(key);
if (CollectionUtil.isNotEmpty(updateList)) {
this.updateBatchById(updateList);
for (DeviceEntity deviceEntity : updateList) {
deviceUtils.updateKv(deviceEntity.getDeviceId(), CommonConstant.ONLINE, CommonConstant.STATUS_0);
}
}
break;
}
}
}
}
}
}
JSONObject map = new JSONObject();
map.put("type", "offline");
map.put("deviceId", msg.getDeviceId());
webSocket.sendOneMessage(msg.getDeviceId().toString(), map.toString());
return;
} else if (StrUtils.isBlank(sourceData)) {
return;
}
//心跳
if (cmd.startsWith(Command.HEART_BEAT)) {
if (session == null) {
ctx.channel().close();
} else {
sendHeart(ctx, session);
}
}
//处理心跳粘包情况 ,可能粘包在头部 ,可能粘包在尾部,如果粘包在中间 需要对数据进行拆分,通过子线程处理后面粘包的数据
i
资源文件列表:
IntelligentPlant.zip 大约有1930个文件