This commit is contained in:
龚祖望 2022-10-26 10:52:25 +08:00
commit 6ca6a29c34
15 changed files with 605 additions and 6 deletions

3
.gitignore vendored
View File

@ -6,3 +6,6 @@ xiuosiot-frontend/dist/
xiuosiot-frontend/node_modules/
xiuosiot-backend/src/main/resources/mybatisGenerate/generatormapper/ProtocolProductInfoMapper.java
xiuosiot-backend/src/main/resources/generatorConfig.xml
xiuosiot-backend/src/main/resources/mybatisGenerate/
*.log
*.gz

View File

@ -95,6 +95,13 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
</dependencies>

View File

@ -0,0 +1,115 @@
package com.aiit.xiuos.Interceptor;
import com.aiit.xiuos.Utils.Constant;
import com.aiit.xiuos.Utils.MyUtils;
import com.aiit.xiuos.Utils.ResultRespons;
import com.aiit.xiuos.model.RequestLogInfo;
import com.aiit.xiuos.model.UserInfo;
import com.aiit.xiuos.service.LogInfoService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
@Aspect
@Slf4j
@Component
public class AroundLogAspect {
@Autowired
LogInfoService requestLogInfoService;
public AroundLogAspect(){}
@Pointcut("execution(* com.aiit.xiuos.controller..*.*(..))")
public void normalPointcut() {
}
@Pointcut("execution(* com.aiit.xiuos.controller.LoginController.*(..))")
public void excludePointcut() {
}
@Pointcut("normalPointcut() && !excludePointcut()")
public void pointCutMethod() {
}
@Around("pointCutMethod()")
public Object aroundLog(ProceedingJoinPoint point) throws ParseException {
RequestLogInfo requestLogInfo =new RequestLogInfo();
StopWatch started = new StopWatch();
//请求时间
requestLogInfo.setRequesttime(MyUtils.getDateTime());
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
//所属公司
UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user");
if(userInfo==null){
return new ResultRespons(Constant.SessionTimeOut_CODE,"用户尚未登录,请先登录!");
}
requestLogInfo.setOrg(userInfo.getOrg());
//IP
String ip = MyUtils.getIp(request);
requestLogInfo.setRequestip(ip);
//url
String url = request.getRequestURI();
requestLogInfo.setRequesturl(url);
//type
String type = request.getMethod();
requestLogInfo.setRequesttype(type);
//header 根据header名获取header值
Map<String, String> headerMap = new HashMap<>();
headerMap.put("accept-encoding",request.getHeader("accept-encoding"));
headerMap.put("user-agent",request.getHeader("user-agent"));
headerMap.put("connection",request.getHeader("connection"));
headerMap.put("content-type",request.getHeader("content-type"));
requestLogInfo.setRequestheader(headerMap.toString());
//类名
String className= point.getTarget().getClass().getName();
requestLogInfo.setClassname(className);
//方法名
String methodName = method.getName();
requestLogInfo.setMethodname(methodName);
//参数名
Parameter[] parameters = method.getParameters();
requestLogInfo.setRequestparam(parameters[0].getName());
//计时开始
started.start();
ResultRespons proceed = (ResultRespons) point.proceed();
//计时结束
started.stop();
//请求耗时
requestLogInfo.setProcesstime(String.valueOf(started.getTotalTimeMillis()));
log.info("processtime:"+started.getTotalTimeMillis());
//请求结果
requestLogInfo.setRequestresult("resultCoe="+proceed.getCode()+" resultMsg="+proceed.getMessage());
requestLogInfoService.addLog(requestLogInfo);
return proceed;
} catch (RuntimeException e) {
throw e;
} catch (Throwable throwable) {
throw new RuntimeException("系统异常!");
}
}
}

View File

@ -17,6 +17,12 @@ public class GenerateIdUtil {
sb.append("-"+user);
return sb.toString();
}
public static synchronized String getTimeId(String org,String user){
String time = new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis());
return time;
}
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

View File

@ -0,0 +1,51 @@
package com.aiit.xiuos.controller;
import com.aiit.xiuos.Utils.Constant;
import com.aiit.xiuos.Utils.ResultRespons;
import com.aiit.xiuos.model.DeviceInfo;
import com.aiit.xiuos.model.DeviceLogInfo;
import com.aiit.xiuos.model.RequestLogInfo;
import com.aiit.xiuos.model.UserInfo;
import com.aiit.xiuos.service.LogInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestController
@RequestMapping("/log")
public class LogController {
@Autowired
LogInfoService requestLogInfoService;
@GetMapping("/requestLog")
public ResultRespons selectRequestLog(HttpServletRequest request){
UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user");
List<RequestLogInfo> RequestLogInfo =requestLogInfoService.selectLog(userInfo.getOrg());
if(RequestLogInfo!=null&&RequestLogInfo.size()>0)
{
return new ResultRespons(Constant.SUCCESS_CODE,"查询日志成功!",RequestLogInfo);
}else
{
return new ResultRespons(Constant.ERROR_CODE,"日志不存在");
}
}
@GetMapping("/deviceLog")
public ResultRespons selectDeviceLog(HttpServletRequest request){
UserInfo userInfo =(UserInfo) request.getSession().getAttribute("user");
List<DeviceLogInfo> deviceLogInfos =requestLogInfoService.selectDeviceLog(userInfo.getOrg());
if(deviceLogInfos!=null&&deviceLogInfos.size()>0)
{
return new ResultRespons(Constant.SUCCESS_CODE,"查询日志成功!",deviceLogInfos);
}else
{
return new ResultRespons(Constant.ERROR_CODE,"日志不存在");
}
}
}

View File

@ -6,7 +6,6 @@ import com.aiit.xiuos.Utils.ResultRespons;
import com.aiit.xiuos.model.UserInfo;
import com.aiit.xiuos.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@ -14,6 +13,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@RestController
public class LoginController {
@Autowired
private UserInfoService userInfoService;

View File

@ -0,0 +1,29 @@
package com.aiit.xiuos.dao.mappers;
import com.aiit.xiuos.model.DeviceLogInfo;
import java.util.Date;
import java.util.List;
import com.aiit.xiuos.model.RequestLogInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
@Repository
public interface DeviceLogInfoMapper {
int deleteByPrimaryKey(@Param("deviceNo") String deviceNo, @Param("logTime") Date logTime);
int insert(DeviceLogInfo record);
int insertSelective(DeviceLogInfo record);
DeviceLogInfo selectByPrimaryKey(@Param("deviceNo") String deviceNo, @Param("logTime") Date logTime);
int updateByPrimaryKeySelective(DeviceLogInfo record);
int updateByPrimaryKey(DeviceLogInfo record);
@Select("select * from device_log_info where org =#{org}")
List<DeviceLogInfo> selectDeviceLog(@Param("org") String org);
}

View File

@ -0,0 +1,26 @@
package com.aiit.xiuos.dao.mappers;
import com.aiit.xiuos.model.RequestLogInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface RequestLogInfoMapper {
int deleteByPrimaryKey(Integer id);
int insert(RequestLogInfo record);
int insertSelective(RequestLogInfo record);
RequestLogInfo selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(RequestLogInfo record);
int updateByPrimaryKey(RequestLogInfo record);
@Select("select * from request_log_info where org =#{org}")
List<RequestLogInfo> selectLog(@Param("org") String org);
}

View File

@ -0,0 +1,39 @@
package com.aiit.xiuos.model;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class DeviceLogInfo {
private String deviceNo;
@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date logTime;
private String deviceType;
private String threadNo;
private Integer logLevel;
private String deviceLog;
private String org;
public DeviceLogInfo(String deviceNo, Date logTime, String deviceType, String threadNo, Integer logLevel, String deviceLog, String org) {
this.deviceNo = deviceNo;
this.logTime = logTime;
this.deviceType = deviceType;
this.threadNo = threadNo;
this.logLevel = logLevel;
this.deviceLog = deviceLog;
this.org = org;
}
public DeviceLogInfo() {
super();
}
}

View File

@ -0,0 +1,54 @@
package com.aiit.xiuos.model;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class RequestLogInfo {
private Integer id;
private String classname;
private String methodname;
private String requestparam;
private String requestip;
private String requestresult;
private String requestheader;
private String requesturl;
@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date requesttime;
private String processtime;
private String requesttype;
private String org;
public RequestLogInfo(Integer id, String classname, String methodname, String requestparam, String requestip, String requestresult, String requestheader, String requesturl, Date requesttime, String processtime, String requesttype, String org) {
this.id = id;
this.classname = classname;
this.methodname = methodname;
this.requestparam = requestparam;
this.requestip = requestip;
this.requestresult = requestresult;
this.requestheader = requestheader;
this.requesturl = requesturl;
this.requesttime = requesttime;
this.processtime = processtime;
this.requesttype = requesttype;
this.org = org;
}
public RequestLogInfo() {
super();
}
}

View File

@ -2,9 +2,12 @@ package com.aiit.xiuos.scheduled;
import com.aiit.xiuos.Utils.MyUtils;
import com.aiit.xiuos.model.AvgDayData;
import com.aiit.xiuos.model.DeviceLogInfo;
import com.aiit.xiuos.service.AvgDayDataService;
import com.aiit.xiuos.service.LogInfoService;
import com.aiit.xiuos.service.impl.AvgDayDataServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@ -13,14 +16,14 @@ import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.*;
@Component
public class TaskScheduled {
@Autowired
AvgDayDataService avgDayDataService;
@Autowired
LogInfoService deviceLogInfoService;
@Scheduled(cron = "0 0 */1 * * ?")//every hour
public void insertAvgDayData() throws ParseException {
AvgDayData avgDayData =new AvgDayData();
@ -80,4 +83,34 @@ public class TaskScheduled {
}
// @Scheduled(cron = "0 0 */1 * * ?")//every hour
// public void insertDeviceLog() {
// String []log={"nsh_main: mmcsd_readsingle: offset=1007095",
// "nsh_main: imxrt_blocksetup: blocklen=512, total transfer=512 (1 blocks)",
// " nsh_main: imxrt_sendcmd: cmd: 00000451 arg: 000f5df7 regval: 113a0000 mcrval: 80000011",
// "Idle Task: imxrt_interrupt: IRQSTAT: 0000000b IRQSIGEN 107001c2 enabled: 00000002",
// "nsh_main: mmcsd_read: startsector: 1289408 nsectors: 1 sectorsize: 512",
// "nsh_main: mmcsd_readsingle: startblock=1289408",
// "nsh_main: imxrt_status: cdstatus=01",
// "nsh_main: mmcsd_readsingle: offset=1289408",
// "nsh_main: imxrt_blocksetup: blocklen=512, total transfer=512 (1 blocks)",
// "nsh_main: imxrt_blocksetup: blocklen=512, total transfer=512 (1 blocks)"
// };
// Map<String,String> deviceMap = new HashMap<>();
// deviceMap.put("A000005","RV400-NPU16T-5G-AR100");
// deviceMap.put("A000007","M168-LoRa-FM100");
// deviceMap.put("A000008","M528-A800-5G-HM100");
// deviceMap.put("A000006","RV400-NPU4T-5G-SR100");
// deviceMap.put("A000014","RV400-NPU4T-5G-SR100");
// deviceMap.put("A000011","RV400-NPU16T-5G-AR100");
// deviceMap.put("A000063","RV400-4G-FR100");
// deviceMap.put("S01","M168-LoRa-FM100");
// deviceMap.put("S02","M528-A800-5G-HM100");
// deviceMap.put("S06","M528-A800-5G-HM100");
// deviceMap.put("S12","RV400-NPU16T-5G-AR100");
// deviceMap.put("S09","RV400-NPU4T-5G-SR100");
//
// }
}

View File

@ -0,0 +1,13 @@
package com.aiit.xiuos.service;
import com.aiit.xiuos.model.DeviceLogInfo;
import com.aiit.xiuos.model.RequestLogInfo;
import java.util.List;
public interface LogInfoService {
int addLog(RequestLogInfo requestLogInfo);
List<RequestLogInfo> selectLog(String org);
int addDeviceLog(DeviceLogInfo deviceLogInfo);
List<DeviceLogInfo> selectDeviceLog(String org);
}

View File

@ -0,0 +1,39 @@
package com.aiit.xiuos.service.impl;
import com.aiit.xiuos.dao.mappers.DeviceLogInfoMapper;
import com.aiit.xiuos.dao.mappers.RequestLogInfoMapper;
import com.aiit.xiuos.model.DeviceLogInfo;
import com.aiit.xiuos.model.RequestLogInfo;
import com.aiit.xiuos.service.LogInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RequestLogInfoServiceImpl implements LogInfoService {
@Autowired
RequestLogInfoMapper requestLogInfoMapper;
@Autowired
DeviceLogInfoMapper deviceLogInfoMapper;
@Override
public int addLog(RequestLogInfo requestLogInfo) {
return requestLogInfoMapper.insertSelective(requestLogInfo);
}
@Override
public List<RequestLogInfo> selectLog(String org) {
return requestLogInfoMapper.selectLog(org);
}
@Override
public int addDeviceLog(DeviceLogInfo deviceLogInfo) {
return deviceLogInfoMapper.insertSelective(deviceLogInfo);
}
@Override
public List<DeviceLogInfo> selectDeviceLog(String org) {
return deviceLogInfoMapper.selectDeviceLog(org);
}
}

View File

@ -31,7 +31,7 @@ mybatis:
#MQTT Config
mqtt:
#MQTT-服务器连接地
hostUrl: tcp://localhost:1883
hostUrl: tcp://192.168.88.225:1883
#MQTT-连接服务器默认客户端ID
clientId: xiuosiot-client
#MQTT-用户名
@ -44,3 +44,8 @@ mqtt:
keepalive: 100
#默认主题
default-topic: xiuosiot/#
logging:
file:
name: xiuosiot.log

View File

@ -0,0 +1,179 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aiit.xiuos.dao.mappers.RequestLogInfoMapper">
<resultMap id="BaseResultMap" type="com.aiit.xiuos.model.RequestLogInfo">
<constructor>
<idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="classname" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="methodname" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="requestparam" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="requestip" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="requestresult" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="requestheader" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="requesturl" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="requesttime" javaType="java.util.Date" jdbcType="TIMESTAMP" />
<arg column="processtime" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="requesttype" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="org" javaType="java.lang.String" jdbcType="VARCHAR" />
</constructor>
</resultMap>
<sql id="Base_Column_List">
id, classname, methodname, requestparam, requestip, requestresult, requestheader,
requesturl, requesttime, processtime, requesttype, org
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from request_log_info
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from request_log_info
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.aiit.xiuos.model.RequestLogInfo">
insert into request_log_info (id, classname, methodname,
requestparam, requestip, requestresult,
requestheader, requesturl, requesttime,
processtime, requesttype, org
)
values (#{id,jdbcType=INTEGER}, #{classname,jdbcType=VARCHAR}, #{methodname,jdbcType=VARCHAR},
#{requestparam,jdbcType=VARCHAR}, #{requestip,jdbcType=VARCHAR}, #{requestresult,jdbcType=VARCHAR},
#{requestheader,jdbcType=VARCHAR}, #{requesturl,jdbcType=VARCHAR}, #{requesttime,jdbcType=TIMESTAMP},
#{processtime,jdbcType=VARCHAR}, #{requesttype,jdbcType=VARCHAR}, #{org,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.aiit.xiuos.model.RequestLogInfo">
insert into request_log_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="classname != null">
classname,
</if>
<if test="methodname != null">
methodname,
</if>
<if test="requestparam != null">
requestparam,
</if>
<if test="requestip != null">
requestip,
</if>
<if test="requestresult != null">
requestresult,
</if>
<if test="requestheader != null">
requestheader,
</if>
<if test="requesturl != null">
requesturl,
</if>
<if test="requesttime != null">
requesttime,
</if>
<if test="processtime != null">
processtime,
</if>
<if test="requesttype != null">
requesttype,
</if>
<if test="org != null">
org,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="classname != null">
#{classname,jdbcType=VARCHAR},
</if>
<if test="methodname != null">
#{methodname,jdbcType=VARCHAR},
</if>
<if test="requestparam != null">
#{requestparam,jdbcType=VARCHAR},
</if>
<if test="requestip != null">
#{requestip,jdbcType=VARCHAR},
</if>
<if test="requestresult != null">
#{requestresult,jdbcType=VARCHAR},
</if>
<if test="requestheader != null">
#{requestheader,jdbcType=VARCHAR},
</if>
<if test="requesturl != null">
#{requesturl,jdbcType=VARCHAR},
</if>
<if test="requesttime != null">
#{requesttime,jdbcType=TIMESTAMP},
</if>
<if test="processtime != null">
#{processtime,jdbcType=VARCHAR},
</if>
<if test="requesttype != null">
#{requesttype,jdbcType=VARCHAR},
</if>
<if test="org != null">
#{org,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.aiit.xiuos.model.RequestLogInfo">
update request_log_info
<set>
<if test="classname != null">
classname = #{classname,jdbcType=VARCHAR},
</if>
<if test="methodname != null">
methodname = #{methodname,jdbcType=VARCHAR},
</if>
<if test="requestparam != null">
requestparam = #{requestparam,jdbcType=VARCHAR},
</if>
<if test="requestip != null">
requestip = #{requestip,jdbcType=VARCHAR},
</if>
<if test="requestresult != null">
requestresult = #{requestresult,jdbcType=VARCHAR},
</if>
<if test="requestheader != null">
requestheader = #{requestheader,jdbcType=VARCHAR},
</if>
<if test="requesturl != null">
requesturl = #{requesturl,jdbcType=VARCHAR},
</if>
<if test="requesttime != null">
requesttime = #{requesttime,jdbcType=TIMESTAMP},
</if>
<if test="processtime != null">
processtime = #{processtime,jdbcType=VARCHAR},
</if>
<if test="requesttype != null">
requesttype = #{requesttype,jdbcType=VARCHAR},
</if>
<if test="org != null">
org = #{org,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.aiit.xiuos.model.RequestLogInfo">
update request_log_info
set classname = #{classname,jdbcType=VARCHAR},
methodname = #{methodname,jdbcType=VARCHAR},
requestparam = #{requestparam,jdbcType=VARCHAR},
requestip = #{requestip,jdbcType=VARCHAR},
requestresult = #{requestresult,jdbcType=VARCHAR},
requestheader = #{requestheader,jdbcType=VARCHAR},
requesturl = #{requesturl,jdbcType=VARCHAR},
requesttime = #{requesttime,jdbcType=TIMESTAMP},
processtime = #{processtime,jdbcType=VARCHAR},
requesttype = #{requesttype,jdbcType=VARCHAR},
org = #{org,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>