fix - 日志上传
This commit is contained in:
@@ -25,6 +25,9 @@ import org.springframework.web.bind.annotation.*;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 小程序二维码进场Controller
|
||||
@@ -148,6 +151,38 @@ public class AdminNettyController extends BaseApiController {
|
||||
return ResponseDTO.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收客户端上传的日志文件
|
||||
*/
|
||||
@RequestMapping(value = "/api/log/receive", method = RequestMethod.POST)
|
||||
public ResponseDTO receiveLog(@RequestBody Map<String, String> logData) {
|
||||
try {
|
||||
String deviceSn = logData.get("deviceSn");
|
||||
String commLog = logData.get("commLog");
|
||||
String errorLog = logData.get("errorLog");
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
String dateStr = sdf.format(new Date());
|
||||
|
||||
if (commLog != null) {
|
||||
String fileName = "/home/uploadlog/comm-" + deviceSn + "-" + dateStr + "-client.log";
|
||||
try (FileOutputStream fos = new FileOutputStream(fileName)) {
|
||||
fos.write(commLog.getBytes());
|
||||
}
|
||||
}
|
||||
if (errorLog != null) {
|
||||
String fileName = "/home/uploadlog/error-" + deviceSn + "-" + dateStr + "-client.log";
|
||||
try (FileOutputStream fos = new FileOutputStream(fileName)) {
|
||||
fos.write(errorLog.getBytes());
|
||||
}
|
||||
}
|
||||
logger.info("log received from device: " + deviceSn);
|
||||
return ResponseDTO.ok();
|
||||
} catch (Exception e) {
|
||||
logger.error("receive log fail", e);
|
||||
throw new ServiceException("receive log fail");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载线下入场凭证(二维码)
|
||||
* @param id
|
||||
|
||||
@@ -12,11 +12,7 @@ import io.netty.handler.timeout.IdleStateEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -85,9 +81,6 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LOG_DATA:
|
||||
handleLogData(message);
|
||||
break;
|
||||
default:
|
||||
logger.info( "default");
|
||||
}
|
||||
@@ -147,34 +140,6 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
}
|
||||
}
|
||||
|
||||
private void handleLogData(VenueMessage message) {
|
||||
try {
|
||||
String json = message.getMessage();
|
||||
Map<String, String> logData = JsonUtils.decode(json, Map.class);
|
||||
String deviceSn = logData.get("deviceSn");
|
||||
String commLog = logData.get("commLog");
|
||||
String errorLog = logData.get("errorLog");
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
String dateStr = sdf.format(new Date());
|
||||
|
||||
if (commLog != null) {
|
||||
String fileName = "/home/uploadlog/comm-" + deviceSn + "-" + dateStr + "-client.log";
|
||||
try (FileOutputStream fos = new FileOutputStream(fileName)) {
|
||||
fos.write(commLog.getBytes());
|
||||
}
|
||||
}
|
||||
if (errorLog != null) {
|
||||
String fileName = "/home/uploadlog/error-" + deviceSn + "-" + dateStr + "-client.log";
|
||||
try (FileOutputStream fos = new FileOutputStream(fileName)) {
|
||||
fos.write(errorLog.getBytes());
|
||||
}
|
||||
}
|
||||
logger.info("log data saved for device: " + deviceSn);
|
||||
} catch (Exception e) {
|
||||
logger.error("handle log data fail", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* IdleStateHandler 如果几秒之后没有读操作,那么就会触发这个方法
|
||||
*/
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package com.sv.intergration;
|
||||
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
@@ -14,26 +18,51 @@ public class LogService {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(LogService.class);
|
||||
|
||||
public Map<String, String> readLogContent() {
|
||||
Map<String, String> logContent = new HashMap<>();
|
||||
public void uploadLogViaHttp() {
|
||||
try {
|
||||
String deviceSn = System.getProperty(NettyConstant.VENUE_CLIENT_SN);
|
||||
logContent.put("deviceSn", deviceSn);
|
||||
Map<String, String> logData = new HashMap<>();
|
||||
logData.put("deviceSn", deviceSn);
|
||||
|
||||
File file1 = new File("/home/venue/logs/common-default.log");
|
||||
File file2 = new File("/home/venue/logs/common-error.log");
|
||||
|
||||
if (file1.exists()) {
|
||||
logContent.put("commLog", new String(Files.readAllBytes(Paths.get(file1.getPath()))));
|
||||
logData.put("commLog", new String(Files.readAllBytes(Paths.get(file1.getPath()))));
|
||||
}
|
||||
if (file2.exists()) {
|
||||
logContent.put("errorLog", new String(Files.readAllBytes(Paths.get(file2.getPath()))));
|
||||
logData.put("errorLog", new String(Files.readAllBytes(Paths.get(file2.getPath()))));
|
||||
}
|
||||
logger.info("read log content success");
|
||||
|
||||
if (!file1.exists() && !file2.exists()) {
|
||||
logger.warn("no log files found");
|
||||
return;
|
||||
}
|
||||
|
||||
String json = JsonUtils.encode(logData);
|
||||
URL url = new URL("https://api.hongyutiyu.top/api/log/receive");
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
|
||||
conn.setDoOutput(true);
|
||||
conn.setConnectTimeout(10000);
|
||||
conn.setReadTimeout(30000);
|
||||
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
os.write(json.getBytes("utf-8"));
|
||||
os.flush();
|
||||
}
|
||||
|
||||
int code = conn.getResponseCode();
|
||||
if (code == 200) {
|
||||
logger.info("upload log success via http");
|
||||
} else {
|
||||
logger.error("upload log fail, http code: " + code);
|
||||
}
|
||||
conn.disconnect();
|
||||
} catch (Exception e) {
|
||||
logger.error("read log content fail", e);
|
||||
logger.error("upload log via http fail", e);
|
||||
}
|
||||
return logContent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
package com.sv.intergration;
|
||||
|
||||
import com.sv.intergration.impl.OldDoorService;
|
||||
import com.sv.netty.config.MessageType;
|
||||
import com.sv.netty.config.VenueMessage;
|
||||
import com.sv.netty.utils.EncodeMsg;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 消息服务
|
||||
* MessageService.java
|
||||
@@ -37,10 +31,9 @@ public class MessageService {
|
||||
/**
|
||||
* 解析并执行接受服务器消息
|
||||
*
|
||||
* @param ctx
|
||||
* @param message
|
||||
*/
|
||||
public void execute(ChannelHandlerContext ctx, VenueMessage message) {
|
||||
public void execute(VenueMessage message) {
|
||||
switch (message.getMessageType()) {
|
||||
case ENTER_DOOR:
|
||||
enterDoor();
|
||||
@@ -49,23 +42,16 @@ public class MessageService {
|
||||
outDoor();
|
||||
break;
|
||||
case LOG:
|
||||
sendLog(ctx);
|
||||
sendLog();
|
||||
break;
|
||||
default:
|
||||
logger.info( "default");
|
||||
}
|
||||
}
|
||||
|
||||
private void sendLog(ChannelHandlerContext ctx) {
|
||||
try {
|
||||
private void sendLog() {
|
||||
LogService logService = new LogService();
|
||||
Map<String, String> logContent = logService.readLogContent();
|
||||
VenueMessage response = new VenueMessage(MessageType.LOG_DATA, JsonUtils.encode(logContent));
|
||||
ctx.channel().writeAndFlush(EncodeMsg.INSTANCE.encode(response));
|
||||
logger.info("send log data success");
|
||||
} catch (Exception e) {
|
||||
logger.error("send log data fail", e);
|
||||
}
|
||||
logService.uploadLogViaHttp();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -58,7 +58,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
||||
logger.error("收到无法解析的消息,忽略: " + msg);
|
||||
return;
|
||||
}
|
||||
MessageService.getInstance().execute(ctx, message);
|
||||
MessageService.getInstance().execute(message);
|
||||
} catch (Exception e) {
|
||||
logger.error("处理服务器消息异常,不影响连接: " + msg, e);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ public class ClientService {
|
||||
System.setProperty(NettyConstant.VENUE_SERVER_PORT,decode.getServerPort());
|
||||
System.setProperty(NettyConstant.VENUE_CLIENT_SN,decode.getClientSN());
|
||||
System.setProperty(NettyConstant.VENUE_CLIENT_VID,decode.getClientVid());
|
||||
System.setProperty(NettyConstant.VENUE_CHECK_PWD,decode.getClientPwd());
|
||||
}
|
||||
bufferedReader.close();
|
||||
fileReader.close();
|
||||
|
||||
@@ -5,8 +5,7 @@ public enum MessageType {
|
||||
SCAN_CODE("扫码"),
|
||||
ENTER_DOOR("进门"),
|
||||
OUT_DOOR("出门"),
|
||||
LOG("日志"),
|
||||
LOG_DATA("日志数据");
|
||||
LOG("日志");
|
||||
|
||||
private String message;
|
||||
MessageType(String message) {
|
||||
|
||||
@@ -8,7 +8,6 @@ public interface NettyConstant {
|
||||
String VENUE_SERVER_PORT = "VENUE_SERVER_PORT";
|
||||
String VENUE_CLIENT_SN = "VENUE_CLIENT_SN";
|
||||
String VENUE_CLIENT_VID = "VENUE_CLIENT_VID";
|
||||
String VENUE_CHECK_PWD = "VENUE_CHECK_PWD";
|
||||
|
||||
/**
|
||||
* session中存储终端发送的额外参数
|
||||
|
||||
Reference in New Issue
Block a user