Commit 8f2eacc9 authored by AfirSraftGarrier's avatar AfirSraftGarrier

修改使用记录

parent 2a9f05b3
......@@ -5,11 +5,14 @@ import iot.sixiang.license.device.DeviceManager;
import iot.sixiang.license.device.DeviceProtocol;
import iot.sixiang.license.forward.ForwardManager;
import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.service.PmsUseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@Slf4j
public class ForwardMessageResponseEventHandler {
......@@ -19,7 +22,8 @@ public class ForwardMessageResponseEventHandler {
ForwardManager forwardManager;
@Autowired
EventPublisher eventPublisher;
@Resource
private PmsUseService pmsUseService;
public ForwardMessageResponseEventHandler() {
}
......@@ -31,15 +35,20 @@ public class ForwardMessageResponseEventHandler {
DeviceProtocol protocol = event.getProtocol();
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) {
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);
} 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;
import iot.sixiang.license.net.BaseChannelInitializer;
public class ForwardChannelInitializer extends BaseChannelInitializer {
private ForwardClientHandler handler;
static final EventExecutorGroup workGroup = new DefaultEventExecutorGroup(Consts.FORWARD_THREAD_NUM);
......@@ -18,10 +17,8 @@ public class ForwardChannelInitializer extends BaseChannelInitializer {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new ForwardDecoder());
ch.pipeline().addLast("encoder", new ForwardEncoder());
ch.pipeline().addLast(workGroup, "handler", handler);
}
}
......@@ -8,11 +8,14 @@ import iot.sixiang.license.device.DeviceProtocol;
import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.ForwardClientInactiveEvent;
import iot.sixiang.license.event.ForwardMessageResponseEvent;
import iot.sixiang.license.service.PmsUseService;
import iot.sixiang.license.util.HexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@ChannelHandler.Sharable
@Slf4j
......@@ -33,6 +36,7 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> {
//String serverIp = socketAddr.getHostString();
//int serverPort = socketAddr.getPort();
DeviceProtocol protocol = (DeviceProtocol) msg;
String channelId = channel.id().asLongText();
log.info("桥接客户端,channelRead0:{},{}", channelId, HexUtil.bytes2hex(protocol.getContent()));
......
......@@ -55,12 +55,13 @@ public class ForwardConnectionListener extends BaseConnectionListener {
EventPublisher eventPublisher = SpringUtil.getBean(EventPublisher.class);
eventPublisher.publishEvent(forwardClientConnectEvent);
// 将日志记录数据返回
SessionContext deviceSessionContext = deviceManager.getSessionContextBySN(this.sn);
int useLogId = deviceSessionContext.getUseLogId();
short stx = 21930;
byte ack = 0x0;
int len = 3;
int len = 6;
byte cmd = 0x1;
byte[] content = new byte[4];
content[0] = 0x7e;
......
......@@ -43,7 +43,7 @@ public class ForwardDecoder extends ByteToMessageDecoder {
return;
}
// buffer.resetReaderIndex();//复位
// buffer.resetReaderIndex();//复位
// 读取data数据
byte[] content = new byte[real_len - cmd_ack_len];
......@@ -55,6 +55,4 @@ public class ForwardDecoder extends ByteToMessageDecoder {
DeviceProtocol protocol = new DeviceProtocol(stx, real_len, cmd, ack, content, end);
out.add(protocol);
}
}
\ No newline at end of file
......@@ -8,10 +8,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ForwardEncoder extends MessageToByteEncoder<DeviceProtocol> {
@Override
protected void encode(ChannelHandlerContext tcx, DeviceProtocol msg, ByteBuf out) {
out.writeShort(msg.getStx());
out.writeShortLE(msg.getLen());
out.writeByte(msg.getCmd());
......
......@@ -9,4 +9,6 @@ public interface PmsUseService {
int createUseLog(String sn);
void createFailUseLog(String sn, String message);
void success(int useLogId);
}
......@@ -31,6 +31,14 @@ public class PmsUseServiceImpl implements PmsUseService {
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) {
PmsUseLog pmsUseLog = new PmsUseLog();
Date date = new Date();
......
......@@ -18,4 +18,9 @@ public class PmsUseServiceTest extends BaseTest {
void addLog() {
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