Compare commits

...

2 Commits

Author SHA1 Message Date
黄昕 4eabd13d41 OCT 1. 更新部分语法检查问题 2024-02-26 14:09:49 +08:00
黄昕 6d228b0448 OCT 1. 增加系统信息相关接口
2. 增加MapStruct结构体转换功能
2024-02-23 11:31:23 +08:00
13 changed files with 315 additions and 1 deletions

10
pom.xml
View File

@ -165,6 +165,16 @@
<artifactId>oshi-core-java11</artifactId>
<version>6.4.12</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.0.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,59 @@
package com.cmhi.cf.controller;
import com.cmhi.cf.restapi.annotation.EncryptionProtocol;
import com.cmhi.cf.restapi.annotation.OperationLogAnnotation;
import com.cmhi.cf.restapi.pojo.base.BaseRespStatus;
import com.cmhi.cf.restapi.pojo.vo.GetHwInfoResp;
import com.cmhi.cf.restapi.pojo.vo.GetOsInfoResp;
import com.cmhi.cf.restapi.pojo.vo.GetProcessorInfoResp;
import com.cmhi.cf.restapi.pojo.vo.ProtocolResp;
import com.cmhi.cf.service.SystemInfoService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.sql.Timestamp;
@Controller
@Slf4j
@Tag(name = "系统信息接口")
@RequestMapping(value = "/api/systemInfo")
public class SystemInfoApi {
@Resource
private SystemInfoService systemInfoService;
@GetMapping("/osInfo")
@ResponseBody
@EncryptionProtocol
@OperationLogAnnotation(OperationModule = "系统信息模块", OperationType = "读取", OperationDesc = "获取当前操作系统信息")
public ProtocolResp<? extends BaseRespStatus> getOsInfo() {
return ProtocolResp.result(GetOsInfoResp.builder()
.os(systemInfoService.getOsName())
.bootTime(new Timestamp(systemInfoService.getOsBootTimeStamp()))
.build());
}
@GetMapping("/processorInfo")
@ResponseBody
@EncryptionProtocol
@OperationLogAnnotation(OperationModule = "系统信息模块", OperationType = "读取", OperationDesc = "获取当前处理器信息")
public ProtocolResp<? extends BaseRespStatus> getProcessorInfo() {
return ProtocolResp.result(GetProcessorInfoResp.builder()
.cpuInfo(systemInfoService.getProcessInfo())
.build());
}
@GetMapping("/hwInfo")
@ResponseBody
@EncryptionProtocol
@OperationLogAnnotation(OperationModule = "系统信息模块", OperationType = "读取", OperationDesc = "获取系统硬件信息")
public ProtocolResp<? extends BaseRespStatus> getHwInfo() {
return ProtocolResp.result(GetHwInfoResp.builder()
.hwInfo(systemInfoService.getHardwareInfo())
.build());
}
}

View File

@ -0,0 +1,60 @@
package com.cmhi.cf.restapi.imapstruct;
import com.cmhi.cf.restapi.pojo.po.HwFirmware;
import com.cmhi.cf.restapi.pojo.po.HwInfo;
import com.cmhi.cf.restapi.pojo.po.HwMotherBoard;
import com.cmhi.cf.restapi.pojo.po.ProcessorInfo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import oshi.hardware.Baseboard;
import oshi.hardware.CentralProcessor;
import oshi.hardware.ComputerSystem;
import oshi.hardware.Firmware;
@Mapper
public interface IStructConvert {
IStructConvert INSTANCE = Mappers.getMapper(IStructConvert.class);
@Mappings({
@Mapping(expression = "java(f.getName())", target = "name"),
@Mapping(expression = "java(f.getVersion())", target = "version"),
@Mapping(expression = "java(f.getDescription())", target = "description"),
@Mapping(expression = "java(f.getManufacturer())", target = "manufacturer"),
@Mapping(expression = "java(f.getReleaseDate())", target = "releaseDate")
})
HwFirmware toHwFirmware(Firmware f);
@Mappings({
@Mapping(expression = "java(b.getVersion().trim())", target = "version"),
@Mapping(expression = "java(b.getManufacturer())", target = "manufacturer"),
@Mapping(expression = "java(b.getModel())", target = "model"),
@Mapping(expression = "java(b.getSerialNumber())", target = "serialNumber")
})
HwMotherBoard toHwMotherBoard(Baseboard b);
@Mappings({
@Mapping(expression = "java(c.getModel())", target = "model"),
@Mapping(expression = "java(c.getManufacturer())", target = "manufacturer"),
@Mapping(expression = "java(c.getHardwareUUID())", target = "uuid"),
@Mapping(expression = "java(c.getSerialNumber())", target = "serialNumber"),
@Mapping(expression = "java(toHwFirmware(c.getFirmware()))", target = "firmware"),
@Mapping(expression = "java(toHwMotherBoard(c.getBaseboard()))", target = "mb")
})
HwInfo toHwInfo(ComputerSystem c);
@Mappings({
@Mapping(expression = "java(p.getProcessorIdentifier().getVendor())", target = "cpuVendor"),
@Mapping(expression = "java(p.getProcessorIdentifier().getName())", target = "cpuName"),
@Mapping(expression = "java(p.getProcessorIdentifier().getProcessorID())", target = "processorId"),
@Mapping(expression = "java(p.getProcessorIdentifier().getIdentifier())", target = "cpuIdentifier"),
@Mapping(expression = "java(p.getProcessorIdentifier().getMicroarchitecture())", target = "microArchitecture"),
@Mapping(expression = "java(p.getProcessorIdentifier().isCpu64bit())", target = "cpu64bit"),
@Mapping(expression = "java(p.getMaxFreq())", target = "cpuVendorFreq"),
@Mapping(expression = "java(p.getPhysicalPackageCount())", target = "physicalCpus"),
@Mapping(expression = "java(p.getPhysicalProcessorCount())", target = "physicalCores"),
@Mapping(expression = "java(p.getLogicalProcessorCount())", target = "logicalCpu")
})
ProcessorInfo toProcessorInfo(CentralProcessor p, CentralProcessor.ProcessorIdentifier i);
}

View File

@ -0,0 +1,14 @@
package com.cmhi.cf.restapi.pojo.po;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;
@Data
@JsonPropertyOrder({"name", "version", "manufacturer", "releaseDate", "description"})
public class HwFirmware {
private String name;
private String version;
private String description;
private String manufacturer;
private String releaseDate;
}

View File

@ -0,0 +1,15 @@
package com.cmhi.cf.restapi.pojo.po;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;
@Data
@JsonPropertyOrder({"model", "manufacturer", "uuid", "serialNumber", "firmware", "mb"})
public class HwInfo {
private String model;
private String manufacturer;
private String uuid;
private String serialNumber;
private HwFirmware firmware;
private HwMotherBoard mb;
}

View File

@ -0,0 +1,13 @@
package com.cmhi.cf.restapi.pojo.po;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;
@Data
@JsonPropertyOrder({"version", "manufacturer", "model", "serialNumber"})
public class HwMotherBoard {
private String version;
private String manufacturer;
private String model;
private String serialNumber;
}

View File

@ -0,0 +1,20 @@
package com.cmhi.cf.restapi.pojo.po;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;
@Data
@JsonPropertyOrder({"cpuVendor", "cpuName", "processorId", "cpuIdentifier", "microArchitecture", "cpu64bit", "cpuVendorFreq",
"physicalCpus", "physicalCores", "logicalCpu"})
public class ProcessorInfo {
private String cpuVendor;
private String cpuName;
private String processorId;
private String cpuIdentifier;
private String microArchitecture;
private Boolean cpu64bit;
private Long cpuVendorFreq;
private Integer physicalCpus;
private Integer physicalCores;
private Integer logicalCpu;
}

View File

@ -0,0 +1,22 @@
package com.cmhi.cf.restapi.pojo.vo;
import com.cmhi.cf.restapi.pojo.base.BaseRespStatus;
import com.cmhi.cf.restapi.pojo.po.HwInfo;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@Schema(name = "系统硬件信息")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@JsonPropertyOrder({"hwInfo", "status", "message"})
@AllArgsConstructor
@NoArgsConstructor
public class GetHwInfoResp extends BaseRespStatus {
private HwInfo hwInfo;
}

View File

@ -0,0 +1,24 @@
package com.cmhi.cf.restapi.pojo.vo;
import com.cmhi.cf.restapi.pojo.base.BaseRespStatus;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.sql.Timestamp;
@Schema(name = "操作系统信息")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@JsonPropertyOrder({"os", "bootTime", "status", "message"})
@AllArgsConstructor
@NoArgsConstructor
public class GetOsInfoResp extends BaseRespStatus {
private String os;
private Timestamp bootTime;
}

View File

@ -0,0 +1,22 @@
package com.cmhi.cf.restapi.pojo.vo;
import com.cmhi.cf.restapi.pojo.base.BaseRespStatus;
import com.cmhi.cf.restapi.pojo.po.ProcessorInfo;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@Schema(name = "操作系统信息")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@JsonPropertyOrder({"cpuInfo", "status", "message"})
@AllArgsConstructor
@NoArgsConstructor
public class GetProcessorInfoResp extends BaseRespStatus {
private ProcessorInfo cpuInfo;
}

View File

@ -0,0 +1,13 @@
package com.cmhi.cf.service;
import com.cmhi.cf.restapi.pojo.po.HwInfo;
import com.cmhi.cf.restapi.pojo.po.ProcessorInfo;
public interface SystemInfoService {
String getOsName();
long getOsBootTimeStamp();
ProcessorInfo getProcessInfo();
HwInfo getHardwareInfo();
}

View File

@ -0,0 +1,37 @@
package com.cmhi.cf.service.impl;
import com.cmhi.cf.restapi.imapstruct.IStructConvert;
import com.cmhi.cf.restapi.pojo.po.HwInfo;
import com.cmhi.cf.restapi.pojo.po.ProcessorInfo;
import com.cmhi.cf.service.SystemInfoService;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.ComputerSystem;
@Service
public class SystemInfoServiceImpl implements SystemInfoService {
private final SystemInfo si = new SystemInfo();
@Override
public String getOsName() {
return String.valueOf(si.getOperatingSystem());
}
@Override
public long getOsBootTimeStamp() {
return si.getOperatingSystem().getSystemBootTime() * 1000L;
}
@Override
public ProcessorInfo getProcessInfo() {
CentralProcessor proc = si.getHardware().getProcessor();
return IStructConvert.INSTANCE.toProcessorInfo(proc, proc.getProcessorIdentifier());
}
@Override
public HwInfo getHardwareInfo() {
ComputerSystem cs = si.getHardware().getComputerSystem();
return IStructConvert.INSTANCE.toHwInfo(cs);
}
}

View File

@ -48,6 +48,9 @@ INSERT IGNORE INTO rbac_resource_data VALUES (406, 'e441dbb4-b844-461c-a5f8-07ee
INSERT IGNORE INTO rbac_resource_data VALUES (407, 'c803571e-ba0d-4f3d-bcc8-76488b1af22a', '/api/dict/user/addDictContent', '新增用户字典详细信息', 'PUT', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (408, '99106f7a-6c4e-4438-ad40-65ed9417d0b4', '/api/dict/user/upgradeDictContent', '更新用户字典详细信息', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (409, 'a4c04e8f-d495-46ca-b67e-79cc9a91d623', '/api/dict/user/deleteDictContent', '删除用户字典详细信息', 'DELETE', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (500, '3e7b6439-8d66-4409-84db-16fcbb115215', '/api/systemInfo/osInfo', '获取操作系统信息', 'GET', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (501, 'a958211f-083e-4a2d-b21d-5442090486d2', '/api/systemInfo/processorInfo', '获取系统处理器信息', 'GET', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (502, 'f5dcb609-692d-4f81-b3fa-0f4dc3c891fb', '/api/systemInfo/hwInfo', '获取系统硬件信息', 'GET', 0);
-- ----------------------------
-- Records of role_resource
@ -77,7 +80,9 @@ INSERT IGNORE INTO rbac_role_resource VALUES (1, 406, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 407, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 408, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 409, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 500, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 501, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 502, 1);
# INSERT IGNORE INTO role_resource (role_id, resource_id, authorize) VALUES (2, 1, 1);
# INSERT IGNORE INTO role_resource (role_id, resource_id, authorize) VALUES (2, 2, 1);
# INSERT IGNORE INTO role_resource (role_id, resource_id, authorize) VALUES (2, 3, 1);