diff --git a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/ISocketServer.java b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/ISocketServer.java index dad1489..8c8da13 100644 --- a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/ISocketServer.java +++ b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/ISocketServer.java @@ -1,5 +1,6 @@ package com.zjyr.beidouservice.adapter.impl.netty; +import io.netty.channel.Channel; import jakarta.annotation.PreDestroy; /** @@ -19,9 +20,8 @@ public interface ISocketServer { * @param the type parameter * @param data the data * @param devId the dev id - * @return the int */ - int sendData(T data, Long devId); + void sendData(Channel ch, Object data); /** * Destory. diff --git a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java index 0208456..74b6766 100644 --- a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java +++ b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java @@ -2,10 +2,7 @@ package com.zjyr.beidouservice.adapter.impl.netty; import com.zjyr.beidouservice.adapter.impl.netty.notify.SocketNotifyEvent; import com.zjyr.beidouservice.common.impl.SocketEventName; -import com.zjyr.beidouservice.misc.HelperUtils; -import com.zjyr.beidouservice.pojo.vo.ControlAdapterSocketCtx; import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol; -import com.zjyr.beidouservice.pojo.vo.binary.ControllerStatus; import com.zjyr.beidouservice.pojo.vo.binary.HeartProtocol; import com.zjyr.beidouservice.pojo.vo.binary.MessageContent; import io.netty.channel.Channel; @@ -21,7 +18,6 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; import java.net.InetSocketAddress; -import java.util.concurrent.ConcurrentHashMap; /** * The type Message handler. @@ -33,33 +29,17 @@ import java.util.concurrent.ConcurrentHashMap; @ChannelHandler.Sharable @RequiredArgsConstructor public class MessageHandler extends SimpleChannelInboundHandler> { - /** - * The constant ctxMap. - */ - public static ConcurrentHashMap ctxMap = new ConcurrentHashMap<>(); - /** * The Application event publisher. */ @Resource ApplicationEventPublisher applicationEventPublisher; - //@Resource - //private ControlDeviceServiceImpl controlDeviceService; @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent idleStateEvent) { if (idleStateEvent.state() == IdleState.ALL_IDLE) { log.info("{}:: Trigger Heart Signe", ctx.channel().id()); -// MessageContent msgCont = MessageContent.builder() -// .msgBody(new HeartProtocol()) -// .build(); -// BaseBinaryProtocol h = BaseBinaryProtocol.builder() -// .msgContent(msgCont) -// .build(); -// -// ctx.writeAndFlush(h); - SocketNotifyEvent notify = new SocketNotifyEvent(this, ctx.channel(), SocketEventName.SOCKET_EVT_IDLE_TIMEOUT, @@ -73,40 +53,17 @@ public class MessageHandler extends SimpleChannelInboundHandler message) throws Exception { - if (message.getMsgContent().getMsgBody() instanceof ControllerStatus status) { - log.info("{}:: Receive Message: {}", - ctx.channel().id(), - HelperUtils.bytesToHexString(status.getBeidouSignalStrength())); - } else { - log.info("{}:: Receive Message: {}", ctx.channel().id(), ""); - } - + SocketNotifyEvent notify = new SocketNotifyEvent(this, + ctx.channel(), + SocketEventName.SOCKET_EVT_RECV, + message.getMsgContent().getMsgBody()); + applicationEventPublisher.publishEvent(notify); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { InetSocketAddress sa = (InetSocketAddress) ctx.channel().remoteAddress(); - //List list = controlDeviceMapper.selectAll(); log.info("{}:: Connected <-- {}:{}", ctx.channel().id(), sa.getAddress().getHostAddress(), sa.getPort()); - -// ControlDevice dev = ControlDevice.builder() -// .deviceType(BeidouAdapterTypeName.ADAPTER_SOCKET_TCP) -// .deviceAddr(sa.getAddress().getHostAddress()) -// .build(); - - //controlDeviceService.addControlDevice(dev); -// if (ctxMap.get(dev.getId()) != null) { -// ctxMap.remove(dev.getId()); -// } else { -// ctxMap.put(dev.getId(), -// ControlAdapterSocketCtx.builder().controlAdapterId(dev.getId()).channel(ctx.channel()).build -// ()); -// } - -// MessageContent msgCont = MessageContent.builder() -// .msgBody(new HeartProtocol()) -// .build(); -// ctx.writeAndFlush(BaseBinaryProtocol.builder().msgContent(msgCont).build()); super.channelActive(ctx); SocketNotifyEvent notify = new SocketNotifyEvent(this, ctx.channel(), SocketEventName.SOCKET_EVT_CONNECT, null); applicationEventPublisher.publishEvent(notify); @@ -117,28 +74,14 @@ public class MessageHandler extends SimpleChannelInboundHandler entry : ctxMap.entrySet()) { - if (entry.getValue().getChannel().id() == ctx.channel().id()) { - ctxMap.remove(entry.getKey()); - } - } - - super.channelActive(ctx); + super.channelInactive(ctx); ctx.close(); - } - /** - * Channel send data. - * - * @param the type parameter - * @param proMsg the pro msg - * @param devId the dev id - */ - public void channelSendData(E proMsg, Long devId) { - ControlAdapterSocketCtx ctx = ctxMap.get(devId); - if (ctx != null && ctx.getChannel() != null && ctx.getChannel().isActive()) { - ctx.getChannel().writeAndFlush(proMsg); - } + SocketNotifyEvent notify = new SocketNotifyEvent(this, + ctx.channel(), + SocketEventName.SOCKET_EVT_DISCONNECT, + null); + applicationEventPublisher.publishEvent(notify); } /** @@ -147,12 +90,9 @@ public class MessageHandler extends SimpleChannelInboundHandler msgCont = MessageContent.builder() - .msgBody(new HeartProtocol()) - .build(); - ctxChannel.writeAndFlush(BaseBinaryProtocol.builder().msgContent(msgCont).build()); + ctxChannel.writeAndFlush(proMsg); } } } diff --git a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/impl/TcpServer.java b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/impl/TcpServer.java index 5a137bf..2341746 100644 --- a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/impl/TcpServer.java +++ b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/impl/TcpServer.java @@ -4,6 +4,7 @@ import com.zjyr.beidouservice.adapter.impl.netty.ChannelInit; import com.zjyr.beidouservice.adapter.impl.netty.ISocketServer; import com.zjyr.beidouservice.config.NettySocketConfigure; import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.Channel; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; @@ -83,8 +84,7 @@ public class TcpServer implements ISocketServer { } @Override - public int sendData(T data, Long devId) { - channelInit.getMessageHandler().channelSendData(data, devId); - return 0; + public void sendData(Channel ch, Object data) { + channelInit.getMessageHandler().channelSendData(ch, data); } } diff --git a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/notify/SocketNotifyEvent.java b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/notify/SocketNotifyEvent.java index e97e707..2d06ba2 100644 --- a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/notify/SocketNotifyEvent.java +++ b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/notify/SocketNotifyEvent.java @@ -35,7 +35,7 @@ public class SocketNotifyEvent extends ApplicationEvent { * @param evtType the evt type * @param evtMessage the evt message */ - public SocketNotifyEvent(Object source, Channel ctx, SocketEventName evtType, List evtMessage) { + public SocketNotifyEvent(Object source, Channel ctx, SocketEventName evtType, Object evtMessage) { super(source); this.socketEvent = evtType; this.evtMessage = evtMessage; diff --git a/src/main/java/com/zjyr/beidouservice/misc/HelperUtils.java b/src/main/java/com/zjyr/beidouservice/misc/HelperUtils.java index d5ab3f3..aa2c260 100644 --- a/src/main/java/com/zjyr/beidouservice/misc/HelperUtils.java +++ b/src/main/java/com/zjyr/beidouservice/misc/HelperUtils.java @@ -1,5 +1,7 @@ package com.zjyr.beidouservice.misc; +import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol; + /** * The type Helper utils. */ @@ -18,8 +20,17 @@ public class HelperUtils { if (sTemp.length() < 2) { sb.append(0); } - sb.append(sTemp.toUpperCase() + " "); + sb.append(sTemp.toUpperCase()).append(" "); } return sb.toString(); } +// +// @SuppressWarnings("unchecked") +// public static BaseBinaryProtocol CastBaseBinaryProtocol(Object obj) { +// return (BaseBinaryProtocol)obj; +// } +// +// private HelperUtils() { +// throw new UnsupportedOperationException(); +// } } diff --git a/src/main/java/com/zjyr/beidouservice/pojo/po/BeidouAdapterControlContent.java b/src/main/java/com/zjyr/beidouservice/pojo/po/BeidouAdapterControlContent.java new file mode 100644 index 0000000..0ac8b45 --- /dev/null +++ b/src/main/java/com/zjyr/beidouservice/pojo/po/BeidouAdapterControlContent.java @@ -0,0 +1,22 @@ +package com.zjyr.beidouservice.pojo.po; + +import com.zjyr.beidouservice.common.impl.SensorControlActionName; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class BeidouAdapterControlContent { + /** + * The Districts code. + */ + private Integer districtsCode; + /** + * The Control action. + */ + private SensorControlActionName controlAction; + /** + * The Sensor id. + */ + private Integer sensorId; +} diff --git a/src/main/java/com/zjyr/beidouservice/pojo/vo/ControlAdapterSocketCtx.java b/src/main/java/com/zjyr/beidouservice/pojo/vo/ControlAdapterSocketCtx.java index 47b2e0c..c0555f1 100644 --- a/src/main/java/com/zjyr/beidouservice/pojo/vo/ControlAdapterSocketCtx.java +++ b/src/main/java/com/zjyr/beidouservice/pojo/vo/ControlAdapterSocketCtx.java @@ -3,12 +3,14 @@ package com.zjyr.beidouservice.pojo.vo; import io.netty.channel.Channel; import lombok.Builder; import lombok.Data; +import lombok.RequiredArgsConstructor; /** * The type Control adapter socket ctx. */ @Data @Builder +@RequiredArgsConstructor public class ControlAdapterSocketCtx { /** * The Control adapter id. @@ -18,4 +20,9 @@ public class ControlAdapterSocketCtx { * The Channel. */ private Channel channel; + + public ControlAdapterSocketCtx(Long id, Channel ch) { + this.controlAdapterId = id; + this.channel = ch; + } } diff --git a/src/main/java/com/zjyr/beidouservice/service/AdapterProtocolService.java b/src/main/java/com/zjyr/beidouservice/service/AdapterProtocolService.java index d011077..b194aa0 100644 --- a/src/main/java/com/zjyr/beidouservice/service/AdapterProtocolService.java +++ b/src/main/java/com/zjyr/beidouservice/service/AdapterProtocolService.java @@ -1,15 +1,35 @@ package com.zjyr.beidouservice.service; +import com.zjyr.beidouservice.common.impl.SensorControlModeName; +import com.zjyr.beidouservice.common.impl.SensorControlTunnelName; +import com.zjyr.beidouservice.pojo.po.BeidouAdapterControlContent; import com.zjyr.beidouservice.pojo.vo.binary.HeartProtocol; +import com.zjyr.beidouservice.pojo.vo.binary.QuerySensorProtocol; +import com.zjyr.beidouservice.pojo.vo.binary.SensorControlProtocol; +import com.zjyr.beidouservice.pojo.vo.binary.SensorControllContent; + +import java.util.List; -/** - * The interface Adapter protocol service. - */ public interface AdapterProtocolService { - /** - * Create heart protocol heart protocol. - * - * @return the heart protocol - */ HeartProtocol createHeartProtocol(); + + List createSensorCtrlInfo(List ctxList); + + SensorControlProtocol createSensorControlProtocol(SensorControlTunnelName tunnelName, + byte[] tunnelAddr, + Integer month, + Integer days, + Integer hour, + Integer minute, + Integer province, + Integer city, + SensorControlModeName ctrlMode, + Integer ctrlCmd, + List ctrlItems); + + QuerySensorProtocol createSensorQueryProtocol(Integer province, + Integer city, + List ctrlItems); + + Object createTransmissionProtocol(T proContent); } diff --git a/src/main/java/com/zjyr/beidouservice/service/BaidouAdapterService.java b/src/main/java/com/zjyr/beidouservice/service/BaidouAdapterService.java index 8fd96cb..224c205 100644 --- a/src/main/java/com/zjyr/beidouservice/service/BaidouAdapterService.java +++ b/src/main/java/com/zjyr/beidouservice/service/BaidouAdapterService.java @@ -1,47 +1,20 @@ package com.zjyr.beidouservice.service; import com.zjyr.beidouservice.pojo.po.BeidouAdapterDevice; +import io.netty.channel.Channel; import java.util.List; -/** - * The interface Baidou adapter service. - */ public interface BaidouAdapterService { - /** - * Start beidou adapter. - */ void startBeidouAdapter(); - /** - * Send commond. - * - * @param the type parameter - * @param adapterId the adapter id - * @param command the command - */ - void sendCommond(Long adapterId, T command); + void sendCommond(Long adapterId, Object command); + + void sendCommond(Channel ch, Object command); - /** - * Gets all adapter. - * - * @return the all adapter - */ List getAllAdapter(); - /** - * Add new beidou adapter int. - * - * @param adapter the adapter - * @return the int - */ - int addNewBeidouAdapter(BeidouAdapterDevice adapter); + void addNewBeidouAdapter(BeidouAdapterDevice adapter); - /** - * Gets adapter by addr. - * - * @param addr the addr - * @return the adapter by addr - */ BeidouAdapterDevice getAdapterByAddr(String addr); } diff --git a/src/main/java/com/zjyr/beidouservice/service/impl/BaidouSocketAdapterServiceImpl.java b/src/main/java/com/zjyr/beidouservice/service/impl/BaidouSocketAdapterServiceImpl.java index 113e936..7282f7b 100644 --- a/src/main/java/com/zjyr/beidouservice/service/impl/BaidouSocketAdapterServiceImpl.java +++ b/src/main/java/com/zjyr/beidouservice/service/impl/BaidouSocketAdapterServiceImpl.java @@ -4,14 +4,15 @@ import com.zjyr.beidouservice.adapter.impl.netty.impl.TcpServer; import com.zjyr.beidouservice.adapter.impl.netty.notify.SocketNotifyEvent; import com.zjyr.beidouservice.common.impl.BeidouAdapterTypeName; import com.zjyr.beidouservice.mapper.ControlDeviceMapper; +import com.zjyr.beidouservice.misc.HelperUtils; import com.zjyr.beidouservice.pojo.entry.ControlDevice; import com.zjyr.beidouservice.pojo.po.BeidouAdapterDevice; import com.zjyr.beidouservice.pojo.vo.ControlAdapterSocketCtx; -import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol; +import com.zjyr.beidouservice.pojo.vo.binary.ControllerStatus; import com.zjyr.beidouservice.pojo.vo.binary.HeartProtocol; -import com.zjyr.beidouservice.pojo.vo.binary.MessageContent; import com.zjyr.beidouservice.service.AdapterProtocolService; import com.zjyr.beidouservice.service.BaidouAdapterService; +import io.netty.channel.Channel; import jakarta.annotation.Nonnull; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; @@ -26,36 +27,19 @@ import java.util.concurrent.ConcurrentHashMap; import static com.zjyr.beidouservice.common.impl.SocketEventName.SOCKET_EVT_CONNECT; -/** - * The type Baidou socket adapter service. - * - * @param the type parameter - */ @Service @Slf4j public class BaidouSocketAdapterServiceImpl implements BaidouAdapterService, ApplicationListener { - /** - * The Ctx map. - */ private final ConcurrentHashMap ctxMap = new ConcurrentHashMap<>(); - /** - * The Control device mapper. - */ @Resource ControlDeviceMapper controlDeviceMapper; - /** - * The Adapter protocol service. - */ @Resource AdapterProtocolService adapterProtocolService; - /** - * The Tcp server. - */ @Resource private TcpServer tcpServer; @@ -65,29 +49,56 @@ public class BaidouSocketAdapterServiceImpl implemen } @Override - public void sendCommond(Long adapterId, E command) { + public void sendCommond(Long adapterId, Object command) { + ControlAdapterSocketCtx ctx = ctxMap.get(adapterId); + if (ctx != null) { + sendCommond(ctx.getChannel(), command); + } + } + + @Override + public void sendCommond(Channel ch, Object command) { + if (command instanceof HeartProtocol heart) { + tcpServer.sendData(ch, adapterProtocolService.createTransmissionProtocol(heart)); + } } @Override public void onApplicationEvent(@Nonnull T evtContent) { if (evtContent instanceof SocketNotifyEvent event) { - log.info("Recvie {} event data {}", event.getSocketEvent(), event.getEvtMessage()); + InetSocketAddress sa = (InetSocketAddress) event.getCtxChannel().remoteAddress(); switch (event.getSocketEvent()) { case SOCKET_EVT_CONNECT, SOCKET_EVT_IDLE_TIMEOUT -> { - MessageContent msgCont = MessageContent.builder().msgBody( - adapterProtocolService.createHeartProtocol()).build(); - event.getCtxChannel().writeAndFlush(BaseBinaryProtocol.builder() - .msgContent(msgCont) - .build()); + sendCommond(event.getCtxChannel(), adapterProtocolService.createHeartProtocol()); if (event.getSocketEvent() == SOCKET_EVT_CONNECT) { - InetSocketAddress sa = (InetSocketAddress) event.getCtxChannel().remoteAddress(); + BeidouAdapterDevice a = BeidouAdapterDevice.builder() .adapterType(BeidouAdapterTypeName.ADAPTER_SOCKET_TCP) .adapterAddr(sa.getAddress().getHostAddress()) .build(); addNewBeidouAdapter(a); + + ControlAdapterSocketCtx sockCtx = new ControlAdapterSocketCtx(a.getId(), event.getCtxChannel()); + ctxMap.put(a.getId(), sockCtx); + } + } + + case SOCKET_EVT_DISCONNECT -> { + for (ConcurrentHashMap.Entry entry : ctxMap.entrySet()) { + if (entry.getValue().getChannel().id() == event.getCtxChannel().id()) { + ctxMap.remove(entry.getKey()); + } + } + } + + case SOCKET_EVT_RECV -> { + if (event.getEvtMessage() instanceof ControllerStatus status) { + log.info("+++{}, {}, {}", + status.getWirelessStatus(), + status.getTelphoneStatus(), + HelperUtils.bytesToHexString(status.getBeidouSignalStrength())); } } } @@ -119,7 +130,7 @@ public class BaidouSocketAdapterServiceImpl implemen } @Override - public int addNewBeidouAdapter(BeidouAdapterDevice adapter) { + public void addNewBeidouAdapter(BeidouAdapterDevice adapter) { ControlDevice device = ControlDevice.builder() .id(adapter.getId()) .deviceType(adapter.getAdapterType()) @@ -130,10 +141,10 @@ public class BaidouSocketAdapterServiceImpl implemen .build(); BeidouAdapterDevice dev = getAdapterByAddr(adapter.getAdapterAddr()); if (dev == null) { - return controlDeviceMapper.addControlDevice(device); + controlDeviceMapper.addControlDevice(device); } else { adapter.setId(dev.getId()); - return controlDeviceMapper.upgradeControlDevice(device); + controlDeviceMapper.upgradeControlDevice(device); } } diff --git a/src/main/java/com/zjyr/beidouservice/service/impl/BeidouSocketProtocolServiceImpl.java b/src/main/java/com/zjyr/beidouservice/service/impl/BeidouSocketProtocolServiceImpl.java index 27e66c5..a2feebf 100644 --- a/src/main/java/com/zjyr/beidouservice/service/impl/BeidouSocketProtocolServiceImpl.java +++ b/src/main/java/com/zjyr/beidouservice/service/impl/BeidouSocketProtocolServiceImpl.java @@ -1,13 +1,21 @@ package com.zjyr.beidouservice.service.impl; +import com.zjyr.beidouservice.common.impl.SensorControlModeName; +import com.zjyr.beidouservice.common.impl.SensorControlTunnelName; +import com.zjyr.beidouservice.pojo.po.BeidouAdapterControlContent; +import com.zjyr.beidouservice.pojo.vo.binary.BaseBinaryProtocol; import com.zjyr.beidouservice.pojo.vo.binary.HeartProtocol; +import com.zjyr.beidouservice.pojo.vo.binary.MessageContent; +import com.zjyr.beidouservice.pojo.vo.binary.QuerySensorProtocol; +import com.zjyr.beidouservice.pojo.vo.binary.SensorControlProtocol; +import com.zjyr.beidouservice.pojo.vo.binary.SensorControllContent; import com.zjyr.beidouservice.service.AdapterProtocolService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; -/** - * The type Beidou socket protocol service. - */ +import java.util.ArrayList; +import java.util.List; + @Service @Slf4j public class BeidouSocketProtocolServiceImpl implements AdapterProtocolService { @@ -16,4 +24,64 @@ public class BeidouSocketProtocolServiceImpl implements AdapterProtocolService { return new HeartProtocol(); } + @Override + public List createSensorCtrlInfo(List ctxList) { + List list = new ArrayList<>(); + + ctxList.forEach(v -> { + list.add(SensorControllContent.builder() + .districtsCode(v.getDistrictsCode().byteValue()) + .controlAction(v.getControlAction()) + .sensorId(v.getSensorId().shortValue()) + .build()); + }); + + return list; + } + + @Override + public SensorControlProtocol createSensorControlProtocol(SensorControlTunnelName tunnelName, + byte[] tunnelAddr, + Integer month, + Integer days, + Integer hour, + Integer minute, + Integer province, + Integer city, + SensorControlModeName ctrlMode, + Integer ctrlCmd, + List ctrlItems) { + return SensorControlProtocol.builder() + .controlTunnel(tunnelName) + .tunnelAddr(tunnelAddr) + .month(month.byteValue()) + .days(days.byteValue()) + .hour(hour.byteValue()) + .minute(minute.byteValue()) + .province(province.byteValue()) + .city(city.byteValue()) + .controlMode(ctrlMode) + .controlCommand(ctrlCmd.byteValue()) + .nControlInfo(((Integer) ctrlItems.size()).byteValue()) + .controllContents(new ArrayList<>(ctrlItems)) + .build(); + } + + @Override + public QuerySensorProtocol createSensorQueryProtocol(Integer province, + Integer city, + List ctrlItems) { + return QuerySensorProtocol.builder() + .province(province.byteValue()) + .city(city.byteValue()) + .nControlInfo(((Integer) ctrlItems.size()).byteValue()) + .controllContents(new ArrayList<>(ctrlItems)) + .build(); + } + + @Override + public Object createTransmissionProtocol(T proContent) { + MessageContent msgCont = MessageContent.builder().msgBody(proContent).build(); + return BaseBinaryProtocol.builder().msgContent(msgCont).build(); + } }