Commit c108a349 authored by AfirSraftGarrier's avatar AfirSraftGarrier

上报错误方法改变

parent 0493c149
package iot.sixiang.license.device; package iot.sixiang.license.device;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
...@@ -10,11 +11,14 @@ import iot.sixiang.license.auth.AuthManager; ...@@ -10,11 +11,14 @@ import iot.sixiang.license.auth.AuthManager;
import iot.sixiang.license.consts.Constant; import iot.sixiang.license.consts.Constant;
import iot.sixiang.license.event.CreateForwardClientEvent; import iot.sixiang.license.event.CreateForwardClientEvent;
import iot.sixiang.license.event.DeviceClientInactiveEvent; import iot.sixiang.license.event.DeviceClientInactiveEvent;
import iot.sixiang.license.event.DeviceClientLicenseEvent;
import iot.sixiang.license.event.EventPublisher; import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.ForwardClientRequestEvent; import iot.sixiang.license.event.ForwardClientRequestEvent;
import iot.sixiang.license.idreader.Safety; import iot.sixiang.license.idreader.Safety;
import iot.sixiang.license.model.SessionContext; import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.model.dto.ReportErrorMsgDTO;
import iot.sixiang.license.service.PmsUseService; import iot.sixiang.license.service.PmsUseService;
import iot.sixiang.license.service.TerminalDeviceService;
import iot.sixiang.license.util.CommonUtil; import iot.sixiang.license.util.CommonUtil;
import iot.sixiang.license.util.HexUtil; import iot.sixiang.license.util.HexUtil;
import iot.sixiang.license.util.SpringUtil; import iot.sixiang.license.util.SpringUtil;
...@@ -24,17 +28,21 @@ import org.springframework.stereotype.Component; ...@@ -24,17 +28,21 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.List;
@Component @Component
@ChannelHandler.Sharable @ChannelHandler.Sharable
@Slf4j @Slf4j
public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> { public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
@Autowired @Autowired
EventPublisher eventPublisher; EventPublisher eventPublisher;
@Autowired @Autowired
Safety safety; Safety safety;
@Resource @Resource
private PmsUseService pmsUseService; private PmsUseService pmsUseService;
@Resource
TerminalDeviceService terminalDeviceService;
public DeviceServerHandler() { public DeviceServerHandler() {
super(); super();
...@@ -56,12 +64,17 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -56,12 +64,17 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
boolean auth = false; boolean auth = false;
if (cmdInt == Constant.CMD_LICENSE) { if (cmdInt == Constant.CMD_LICENSE) {
auth = handlerCheckAuth(channel, remoteIp, remotePort, protocol); auth = handleCheckAuth(channel, remoteIp, remotePort, protocol);
} else if (cmdInt == Constant.CMD_UPLOAD_ERROR) {
auth = handleCheckAuthStatus(channel, remoteIp, remotePort, protocol);
if (auth) {
handleUploadError(channel, remoteIp, remotePort, protocol);
}
} else { } else {
auth = handlerCheckAuthStatus(channel, remoteIp, remotePort, protocol); auth = handleCheckAuthStatus(channel, remoteIp, remotePort, protocol);
if (auth) { if (auth) {
log.info("auth valid..."); log.info("auth valid...");
handlerForward(channel, remoteIp, remotePort, protocol); handleForward(channel, remoteIp, remotePort, protocol);
} }
} }
...@@ -110,7 +123,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -110,7 +123,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
ctx.close(); ctx.close();
} }
private boolean handlerCheckAuth(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) { private boolean handleCheckAuth(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
// 正式代码要放开 // 正式代码要放开
byte[] bytes = safety.decodeExtendedPayload(protocol.getContent(), 0, protocol.getContent().length); byte[] bytes = safety.decodeExtendedPayload(protocol.getContent(), 0, protocol.getContent().length);
if (bytes == null) { if (bytes == null) {
...@@ -163,17 +176,50 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -163,17 +176,50 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
return license; return license;
} }
private boolean handlerCheckAuthStatus(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) { private void handleUploadError(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
byte[] bytes = safety.decodeExtendedPayload(protocol.getContent(), 0, protocol.getContent().length);
if (bytes == null) {
return;
}
String decodeInfo = CommonUtil.bytesToStr(bytes);
log.info("upload error byte to string:" + decodeInfo);
if (decodeInfo == null) {
return;
}
List<ReportErrorMsgDTO> reportErrorMsgDTOS = JSONArray.parseArray(decodeInfo, ReportErrorMsgDTO.class);
terminalDeviceService.reportErrorMsg(reportErrorMsgDTOS);
short stx = 21930;
byte ack = 0x0;
int len = 3;
byte cmd = 0x2;
byte[] content = new byte[1];
content[0] = 0x7e;
byte end = 0x1;
DeviceProtocol theProtocol = new DeviceProtocol(stx, len, cmd, ack, content, end);
handleClient(channel, theProtocol);
}
private boolean handleCheckAuthStatus(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
String channelId = channel.id().asLongText(); String channelId = channel.id().asLongText();
DeviceManager deviceManager = SpringUtil.getBean(DeviceManager.class); DeviceManager deviceManager = SpringUtil.getBean(DeviceManager.class);
return deviceManager.getAuthStatusByChannelId(channelId); return deviceManager.getAuthStatusByChannelId(channelId);
} }
private void handlerForward(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) { private void handleForward(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
String channelId = channel.id().asLongText(); String channelId = channel.id().asLongText();
ForwardClientRequestEvent forwardClientRequestEvent = new ForwardClientRequestEvent(); ForwardClientRequestEvent forwardClientRequestEvent = new ForwardClientRequestEvent();
forwardClientRequestEvent.setDeviceChannelId(channelId); forwardClientRequestEvent.setDeviceChannelId(channelId);
forwardClientRequestEvent.setProtocol(protocol); forwardClientRequestEvent.setProtocol(protocol);
eventPublisher.publishEvent(forwardClientRequestEvent); eventPublisher.publishEvent(forwardClientRequestEvent);
} }
private void handleClient(SocketChannel channel, DeviceProtocol protocol) {
String channelId = channel.id().asLongText();
DeviceClientLicenseEvent deviceClientLicenseEvent = new DeviceClientLicenseEvent();
deviceClientLicenseEvent.setDeviceChannelId(channelId);
deviceClientLicenseEvent.setProtocol(protocol);
eventPublisher.publishEvent(deviceClientLicenseEvent);
}
} }
...@@ -6,5 +6,6 @@ import lombok.Data; ...@@ -6,5 +6,6 @@ import lombok.Data;
@Data @Data
public class DeviceClientLicenseEvent extends BaseEvent { public class DeviceClientLicenseEvent extends BaseEvent {
private String sn; private String sn;
private String deviceChannelId;
private DeviceProtocol protocol; private DeviceProtocol protocol;
} }
...@@ -17,15 +17,19 @@ public class DeviceClientLicenseEventHandler { ...@@ -17,15 +17,19 @@ public class DeviceClientLicenseEventHandler {
EventPublisher eventPublisher; EventPublisher eventPublisher;
public DeviceClientLicenseEventHandler() { public DeviceClientLicenseEventHandler() {
} }
@EventListener @EventListener
public void handlerEvent(DeviceClientLicenseEvent event) { public void handlerEvent(DeviceClientLicenseEvent event) {
String appId = event.getSn(); String sn = event.getSn();
DeviceProtocol protocol = event.getProtocol(); DeviceProtocol protocol = event.getProtocol();
SessionContext session = null;
if (sn != null) {
session = deviceManager.getSessionContextBySN(sn);
} else if (event.getDeviceChannelId() != null) {
session = deviceManager.getSessionByChannelId(event.getDeviceChannelId());
}
SessionContext session = deviceManager.getSessionContextBySN(appId);
if (session == null) { if (session == null) {
log.debug("device client license undo ..."); log.debug("device client license undo ...");
return; return;
......
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