Commit 8f2eacc9 authored by AfirSraftGarrier's avatar AfirSraftGarrier

修改使用记录

parent 2a9f05b3
...@@ -5,11 +5,14 @@ import iot.sixiang.license.device.DeviceManager; ...@@ -5,11 +5,14 @@ import iot.sixiang.license.device.DeviceManager;
import iot.sixiang.license.device.DeviceProtocol; import iot.sixiang.license.device.DeviceProtocol;
import iot.sixiang.license.forward.ForwardManager; import iot.sixiang.license.forward.ForwardManager;
import iot.sixiang.license.model.SessionContext; import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.service.PmsUseService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component @Component
@Slf4j @Slf4j
public class ForwardMessageResponseEventHandler { public class ForwardMessageResponseEventHandler {
...@@ -19,7 +22,8 @@ public class ForwardMessageResponseEventHandler { ...@@ -19,7 +22,8 @@ public class ForwardMessageResponseEventHandler {
ForwardManager forwardManager; ForwardManager forwardManager;
@Autowired @Autowired
EventPublisher eventPublisher; EventPublisher eventPublisher;
@Resource
private PmsUseService pmsUseService;
public ForwardMessageResponseEventHandler() { public ForwardMessageResponseEventHandler() {
} }
...@@ -31,15 +35,20 @@ public class ForwardMessageResponseEventHandler { ...@@ -31,15 +35,20 @@ public class ForwardMessageResponseEventHandler {
DeviceProtocol protocol = event.getProtocol(); DeviceProtocol protocol = event.getProtocol();
SessionContext forwardSessionContext = forwardManager.getSessionByChannelId(channelId); SessionContext forwardSessionContext = forwardManager.getSessionByChannelId(channelId);
String appId = forwardSessionContext.getAppId(); String sn = forwardSessionContext.getSn();
SessionContext deviceSessionContext = deviceManager.getSessionContextBySN(appId); SessionContext deviceSessionContext = deviceManager.getSessionContextBySN(sn);
if (deviceSessionContext != null) { if (deviceSessionContext != null) {
SocketChannel deviceClientChannel = deviceSessionContext.getClientChannel(); SocketChannel deviceClientChannel = deviceSessionContext.getClientChannel();
log.info("forward client response..." + appId + ",forward session:" + deviceSessionContext); log.info("forward client response..." + sn + ",forward session:" + deviceSessionContext);
deviceClientChannel.writeAndFlush(protocol); deviceClientChannel.writeAndFlush(protocol);
} else { } else {
log.info("forward client response undo ..." + appId); log.info("forward client response undo ..." + sn);
}
// 说明已经成功
if (protocol.getCmd() == 0xF2) {
pmsUseService.success(deviceSessionContext.getUseLogId());
} }
} }
} }
...@@ -7,7 +7,6 @@ import iot.sixiang.license.consts.Consts; ...@@ -7,7 +7,6 @@ import iot.sixiang.license.consts.Consts;
import iot.sixiang.license.net.BaseChannelInitializer; import iot.sixiang.license.net.BaseChannelInitializer;
public class ForwardChannelInitializer extends BaseChannelInitializer { public class ForwardChannelInitializer extends BaseChannelInitializer {
private ForwardClientHandler handler; private ForwardClientHandler handler;
static final EventExecutorGroup workGroup = new DefaultEventExecutorGroup(Consts.FORWARD_THREAD_NUM); static final EventExecutorGroup workGroup = new DefaultEventExecutorGroup(Consts.FORWARD_THREAD_NUM);
...@@ -18,10 +17,8 @@ public class ForwardChannelInitializer extends BaseChannelInitializer { ...@@ -18,10 +17,8 @@ public class ForwardChannelInitializer extends BaseChannelInitializer {
@Override @Override
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new ForwardDecoder()); ch.pipeline().addLast("decoder", new ForwardDecoder());
ch.pipeline().addLast("encoder", new ForwardEncoder()); ch.pipeline().addLast("encoder", new ForwardEncoder());
ch.pipeline().addLast(workGroup, "handler", handler); ch.pipeline().addLast(workGroup, "handler", handler);
} }
} }
...@@ -8,11 +8,14 @@ import iot.sixiang.license.device.DeviceProtocol; ...@@ -8,11 +8,14 @@ import iot.sixiang.license.device.DeviceProtocol;
import iot.sixiang.license.event.EventPublisher; import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.ForwardClientInactiveEvent; import iot.sixiang.license.event.ForwardClientInactiveEvent;
import iot.sixiang.license.event.ForwardMessageResponseEvent; import iot.sixiang.license.event.ForwardMessageResponseEvent;
import iot.sixiang.license.service.PmsUseService;
import iot.sixiang.license.util.HexUtil; import iot.sixiang.license.util.HexUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component @Component
@ChannelHandler.Sharable @ChannelHandler.Sharable
@Slf4j @Slf4j
...@@ -33,6 +36,7 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> { ...@@ -33,6 +36,7 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> {
//String serverIp = socketAddr.getHostString(); //String serverIp = socketAddr.getHostString();
//int serverPort = socketAddr.getPort(); //int serverPort = socketAddr.getPort();
DeviceProtocol protocol = (DeviceProtocol) msg; DeviceProtocol protocol = (DeviceProtocol) msg;
String channelId = channel.id().asLongText(); String channelId = channel.id().asLongText();
log.info("桥接客户端,channelRead0:{},{}", channelId, HexUtil.bytes2hex(protocol.getContent())); log.info("桥接客户端,channelRead0:{},{}", channelId, HexUtil.bytes2hex(protocol.getContent()));
......
...@@ -55,12 +55,13 @@ public class ForwardConnectionListener extends BaseConnectionListener { ...@@ -55,12 +55,13 @@ public class ForwardConnectionListener extends BaseConnectionListener {
EventPublisher eventPublisher = SpringUtil.getBean(EventPublisher.class); EventPublisher eventPublisher = SpringUtil.getBean(EventPublisher.class);
eventPublisher.publishEvent(forwardClientConnectEvent); eventPublisher.publishEvent(forwardClientConnectEvent);
// 将日志记录数据返回
SessionContext deviceSessionContext = deviceManager.getSessionContextBySN(this.sn); SessionContext deviceSessionContext = deviceManager.getSessionContextBySN(this.sn);
int useLogId = deviceSessionContext.getUseLogId(); int useLogId = deviceSessionContext.getUseLogId();
short stx = 21930; short stx = 21930;
byte ack = 0x0; byte ack = 0x0;
int len = 3; int len = 6;
byte cmd = 0x1; byte cmd = 0x1;
byte[] content = new byte[4]; byte[] content = new byte[4];
content[0] = 0x7e; content[0] = 0x7e;
......
...@@ -43,7 +43,7 @@ public class ForwardDecoder extends ByteToMessageDecoder { ...@@ -43,7 +43,7 @@ public class ForwardDecoder extends ByteToMessageDecoder {
return; return;
} }
// buffer.resetReaderIndex();//复位 // buffer.resetReaderIndex();//复位
// 读取data数据 // 读取data数据
byte[] content = new byte[real_len - cmd_ack_len]; byte[] content = new byte[real_len - cmd_ack_len];
...@@ -55,6 +55,4 @@ public class ForwardDecoder extends ByteToMessageDecoder { ...@@ -55,6 +55,4 @@ public class ForwardDecoder extends ByteToMessageDecoder {
DeviceProtocol protocol = new DeviceProtocol(stx, real_len, cmd, ack, content, end); DeviceProtocol protocol = new DeviceProtocol(stx, real_len, cmd, ack, content, end);
out.add(protocol); out.add(protocol);
} }
} }
\ No newline at end of file
...@@ -8,10 +8,8 @@ import lombok.extern.slf4j.Slf4j; ...@@ -8,10 +8,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class ForwardEncoder extends MessageToByteEncoder<DeviceProtocol> { public class ForwardEncoder extends MessageToByteEncoder<DeviceProtocol> {
@Override @Override
protected void encode(ChannelHandlerContext tcx, DeviceProtocol msg, ByteBuf out) { protected void encode(ChannelHandlerContext tcx, DeviceProtocol msg, ByteBuf out) {
out.writeShort(msg.getStx()); out.writeShort(msg.getStx());
out.writeShortLE(msg.getLen()); out.writeShortLE(msg.getLen());
out.writeByte(msg.getCmd()); out.writeByte(msg.getCmd());
......
...@@ -9,4 +9,6 @@ public interface PmsUseService { ...@@ -9,4 +9,6 @@ public interface PmsUseService {
int createUseLog(String sn); int createUseLog(String sn);
void createFailUseLog(String sn, String message); void createFailUseLog(String sn, String message);
void success(int useLogId);
} }
...@@ -31,6 +31,14 @@ public class PmsUseServiceImpl implements PmsUseService { ...@@ -31,6 +31,14 @@ public class PmsUseServiceImpl implements PmsUseService {
pmsUseLogMapper.insert(pmsUseLog); pmsUseLogMapper.insert(pmsUseLog);
} }
@Override
public void success(int useLogId) {
PmsUseLog pmsUseLog = new PmsUseLog();
pmsUseLog.setId(useLogId);
pmsUseLog.setStatus(1);
pmsUseLogMapper.updateById(pmsUseLog);
}
private PmsUseLog getPmsUseLog(String sn) { private PmsUseLog getPmsUseLog(String sn) {
PmsUseLog pmsUseLog = new PmsUseLog(); PmsUseLog pmsUseLog = new PmsUseLog();
Date date = new Date(); Date date = new Date();
......
...@@ -18,4 +18,9 @@ public class PmsUseServiceTest extends BaseTest { ...@@ -18,4 +18,9 @@ public class PmsUseServiceTest extends BaseTest {
void addLog() { void addLog() {
log(pmsUseService.createUseLog("abcd")); log(pmsUseService.createUseLog("abcd"));
} }
@Test
void updateLog() {
pmsUseService.success(1);
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment