@@ -1,14 +1,46 @@
package com.maibu.netty.handler ;
import java.math.BigDecimal ;
import java.nio.charset.StandardCharsets ;
import java.time.LocalDateTime ;
import java.util.List ;
import java.util.Map ;
import java.util.concurrent.ConcurrentHashMap ;
import javax.annotation.Resource ;
import org.apache.commons.codec.binary.Hex ;
import org.apache.commons.lang3.StringUtils ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.context.ApplicationContext ;
import org.springframework.stereotype.Component ;
import org.springframework.util.CollectionUtils ;
import com.maibu.common.CommandConstant ;
import com.maibu.common.Constant ;
import com.maibu.core.business.Device ;
import com.maibu.core.business.device.NettyDevice ;
import com.maibu.core.business.inter.DeviceWebSocketInterace ;
import com.maibu.core.business.inter.WebsocketMesDispather ;
import com.maibu.core.business.path.LatAndLngEntity ;
import com.maibu.core.domain.entity.SysUser ;
import com.maibu.core.domain.entity.UserClient ;
import com.maibu.core.enums.* ;
import com.maibu.dto.* ;
import com.maibu.core.enums.CommandRequestType ;
import com.maibu.core.enums.ConnectionCloseReason ;
import com.maibu.core.enums.ConnectorStatus ;
import com.maibu.core.enums.ConnectorType ;
import com.maibu.core.enums.DeviceStatus ;
import com.maibu.core.enums.DeviceTaskStaus ;
import com.maibu.core.enums.MesType ;
import com.maibu.core.enums.RespondCode ;
import com.maibu.dto.DeviceLoginResponseDTO ;
import com.maibu.dto.DeviceRequestDTO ;
import com.maibu.dto.DeviceRespondDTO ;
import com.maibu.dto.DeviceStatusChangeDTO ;
import com.maibu.dto.DeviceTaskReportDTO ;
import com.maibu.dto.DeviceTaskStatusMessageDTO ;
import com.maibu.manager.DeviceSessionManager ;
import com.maibu.mapper.DeviceRunStatisticsMapper ;
import com.maibu.service.IDeviceService ;
@@ -16,28 +48,14 @@ import com.maibu.service.ISysUserClientService;
import com.maibu.service.ISysUserService ;
import com.maibu.utils.CommandUtils ;
import com.maibu.utils.json.JsonUtils ;
import io.netty.buffer.ByteBuf ;
import io.netty.buffer.Unpooled ;
import io.netty.channel.Channel ;
import io.netty.channel.ChannelHandler ;
import io.netty.channel.ChannelHandlerContext ;
import io.netty.channel.SimpleChannelInboundHandler ;
import io.netty.util.AttributeKey ;
import io.netty.util.CharsetUtil ;
import org.apache.commons.codec.binary.Hex ;
import org.apache.commons.lang3.StringUtils ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Component ;
import org.springframework.util.CollectionUtils ;
import java.math.BigDecimal ;
import java.nio.charset.StandardCharsets ;
import java.time.LocalDateTime ;
import java.util.List ;
import java.util.Queue ;
import java.util.concurrent.ConcurrentHashMap ;
@Component
@ChannelHandler.Sharable
@@ -63,6 +81,9 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
@Autowired
private DeviceRunStatisticsMapper deviceRunStatisticsMapper ;
@Resource
private WebsocketMesDispather websocketMesDispather ;
public DeviceConnectHandler ( DeviceSessionManager sessionManager ) {
this . sessionManager = sessionManager ;
}
@@ -317,29 +338,13 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
String deviceId = channel . attr ( Constant . ATT_DEVICE_ID ) . get ( ) ;
NettyDevice device = sessionManager . getDevice ( deviceId ) ;
if ( CollectionUtils . isEmpty ( device . getLocationQueue ( ) ) ) {
Long taskId = device . finishTask ( ) ;
// todo 推送信息给前端
DeviceTaskReportDTO changeDTO = new DeviceTaskReportDTO ( ) ;
changeDTO . setDeviceId ( deviceId ) ;
changeDTO . setStatus ( DeviceTaskStaus . FINISH ) ;
changeDTO . setEvent ( RespondCode . device_task_change ) ;
changeDTO . setTaskId ( taskId ) ;
// ByteBuf buf = ctx.alloc().buffer();
ByteBuf buf = Unpooled . buffer ( ) ;
CommandUtils . buildCommand ( buf , changeDTO , CommandConstant . interaction ) ;
List < NettyDevice > controlMasters = sessionManager
. getAllSlaveControl ( deviceId ) ;
if ( ! CollectionUtils . isEmpty ( controlMasters ) ) {
controlMasters . forEach ( x - > {
if ( x . getChannel ( ) ! = null & & x . getChannel ( ) . isActive ( ) ) {
x . getChannel ( ) . writeAndFlush ( buf ) ;
logger . info ( " 任务完成状态推送 task:{},device:{} " , taskId , deviceId ) ;
}
} ) ;
}
// 推送到点
pushArriveData ( device , deviceId ) ;
// 推送完成+执行结束更新内存
pushFinishData ( device , deviceId ) ;
} else {
pushArriveData ( device , deviceId ) ;
device . sendNextPoint ( ) ;
//todo 推送到点给前端
}
}
}
@@ -350,6 +355,65 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
}
}
public void pushFinishData ( NettyDevice device , String deviceId ) {
Long taskId = device . finishTask ( ) ;
// todo 推送信息给前端
DeviceTaskStatusMessageDTO changeDTO = new DeviceTaskStatusMessageDTO ( ) ;
changeDTO . setDeviceId ( deviceId ) ;
changeDTO . setStatus ( DeviceTaskStaus . FINISH . toString ( ) ) ;
changeDTO . setType ( MesType . device_task_change ) ;
changeDTO . setTaskId ( taskId ) ;
// ByteBuf buf = ctx.alloc().buffer();
// ByteBuf buf = Unpooled.buffer();
// CommandUtils.buildCommand(buf, changeDTO, CommandConstant.interaction);
List < NettyDevice > controlMasters = sessionManager
. getAllSlaveControl ( deviceId ) ;
if ( ! CollectionUtils . isEmpty ( controlMasters ) ) {
controlMasters . forEach ( x - > {
websocketMesDispather . dispather ( x . getConnectorId ( ) ,
JsonUtils . toJsonString ( changeDTO ) ) ;
// String content = buf.toString(CharsetUtil.UTF_8);
logger . info ( " 任务完成状态推送 task:{},device:{}, content:{} " , taskId , deviceId ,
JsonUtils . toJsonString ( changeDTO ) ) ;
// if (x.getChannel() != null && x.getChannel().isActive()) {
// x.getChannel().writeAndFlush(buf);
// String content = buf.toString(CharsetUtil.UTF_8);
// logger.info("任务完成状态推送 task:{},device:{}, content:{}", taskId, deviceId,
// content);
// }
} ) ;
}
}
public void pushArriveData ( NettyDevice device , String deviceId ) {
Long taskId = device . getTask ( ) = = null ? null : device . getTask ( ) . getId ( ) ;
// todo 推送信息给前端
DeviceTaskStatusMessageDTO reportDTO = new DeviceTaskStatusMessageDTO ( ) ;
reportDTO . setDeviceId ( deviceId ) ;
reportDTO . setTaskId ( taskId ) ;
reportDTO . setEntity ( device . getCurrentPoint ( ) ) ;
reportDTO . setType ( MesType . device_task_arrive_point ) ;
// ByteBuf buf = ctx.alloc().buffer();
// ByteBuf buf = Unpooled.buffer();
// CommandUtils.buildCommand(buf, reportDTO, CommandConstant.interaction);
List < NettyDevice > controlMasters = sessionManager
. getAllSlaveControl ( deviceId ) ;
if ( ! CollectionUtils . isEmpty ( controlMasters ) ) {
controlMasters . forEach ( x - > {
websocketMesDispather . dispather ( x . getConnectorId ( ) ,
JsonUtils . toJsonString ( reportDTO ) ) ;
logger . info ( " 任务点位到达推送 task:{},device:{},content:{} " , taskId , deviceId ,
JsonUtils . toJsonString ( reportDTO ) ) ;
// if (x.getChannel() != null && x.getChannel().isActive()) {
// x.getChannel().writeAndFlush(buf);
// String content = buf.toString(CharsetUtil.UTF_8);
// logger.info("任务点位到达推送 task:{},device:{},content:{}", taskId, deviceId,
// content);
// }
} ) ;
}
}
/**
* 处理反馈 待扩展
*/