物理设备管理及邮件提醒功能集成
parent
516a676e66
commit
accbc9e869
@ -0,0 +1,57 @@
|
|||||||
|
package com.example.venue_reservation_service.controller;
|
||||||
|
|
||||||
|
import com.example.venue_reservation_service.domain.Device;
|
||||||
|
import com.example.venue_reservation_service.mapper.DeviceMapper;
|
||||||
|
import com.example.venue_reservation_service.service.DeviceService;
|
||||||
|
import com.example.venue_reservation_service.vo.Result;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/device")
|
||||||
|
@Api("设备数据管理")
|
||||||
|
@CrossOrigin
|
||||||
|
public class DeviceController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DeviceService deviceService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DeviceMapper deviceMapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SimpMessagingTemplate messagingTemplate;
|
||||||
|
|
||||||
|
// 获取最新设备数据
|
||||||
|
@ApiOperation("获取最新设备数据")
|
||||||
|
@GetMapping("/latest")
|
||||||
|
public Result getLatestDevices() {
|
||||||
|
return Result.ok(deviceService.getLatestDevices()).message("加载成功");
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// 定时任务 - 每秒生成一条数据
|
||||||
|
// @Scheduled(fixedRate = 1000)
|
||||||
|
// public void generateAndSendDeviceData() {
|
||||||
|
// // 生成并保存新数据
|
||||||
|
// Device newDevice = deviceService.generateDeviceData();
|
||||||
|
// deviceService.save(newDevice);
|
||||||
|
//
|
||||||
|
// // 检查并清理旧数据
|
||||||
|
// long count = deviceService.count();
|
||||||
|
// if (count > 100) {
|
||||||
|
// deviceMapper.deleteOldestRecords(50);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 推送新数据到前端
|
||||||
|
// messagingTemplate.convertAndSend("/topic/devices", newDevice);
|
||||||
|
// }
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.example.venue_reservation_service.domain;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @TableName venue_device
|
||||||
|
*/
|
||||||
|
@TableName(value ="venue_device")
|
||||||
|
@Data
|
||||||
|
public class Device implements Serializable {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
|
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
|
private LocalDateTime timestamp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private Double temperature;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private Double humidity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private Integer fanStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private Integer warningStatus;
|
||||||
|
|
||||||
|
@TableField(exist = false)
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.example.venue_reservation_service.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ResetDTO {
|
||||||
|
|
||||||
|
private String phone;
|
||||||
|
|
||||||
|
private String email;
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.example.venue_reservation_service.entity;
|
||||||
|
|
||||||
|
import com.example.venue_reservation_service.domain.Information;
|
||||||
|
import com.example.venue_reservation_service.vo.excel.ReservationExcel;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class RecommendVO {
|
||||||
|
|
||||||
|
private ReservationExcel excel;
|
||||||
|
|
||||||
|
private Information information;
|
||||||
|
|
||||||
|
private Double price;
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.example.venue_reservation_service.mapper;
|
||||||
|
|
||||||
|
import com.example.venue_reservation_service.domain.Device;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Delete;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 31586
|
||||||
|
* @description 针对表【venue_device】的数据库操作Mapper
|
||||||
|
* @createDate 2025-06-29 14:38:46
|
||||||
|
* @Entity generator.domain.Device
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface DeviceMapper extends BaseMapper<Device> {
|
||||||
|
|
||||||
|
@Select("SELECT * FROM venue_device ORDER BY timestamp DESC LIMIT #{limit}")
|
||||||
|
List<Device> selectLatestDevices(@Param("limit") int limit);
|
||||||
|
|
||||||
|
@Delete("DELETE FROM venue_device ORDER BY timestamp ASC LIMIT #{count}")
|
||||||
|
void deleteOldestRecords(@Param("count") int count);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.example.venue_reservation_service.service;
|
||||||
|
|
||||||
|
import com.example.venue_reservation_service.domain.Device;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 31586
|
||||||
|
* @description 针对表【venue_device】的数据库操作Service
|
||||||
|
* @createDate 2025-06-29 14:38:46
|
||||||
|
*/
|
||||||
|
public interface DeviceService extends IService<Device> {
|
||||||
|
Device generateDeviceData();
|
||||||
|
|
||||||
|
List<Device> getLatestDevices();
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.example.venue_reservation_service.service;
|
||||||
|
|
||||||
|
import org.springframework.core.io.InputStreamSource;
|
||||||
|
|
||||||
|
public interface EmailService {
|
||||||
|
/**
|
||||||
|
* 发送简单邮件
|
||||||
|
* @param to 收件人
|
||||||
|
* @param subject 主题
|
||||||
|
* @param text 内容
|
||||||
|
*/
|
||||||
|
void sendSimpleMessage(String to, String subject, String text);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送HTML格式邮件
|
||||||
|
* @param to 收件人
|
||||||
|
* @param subject 主题
|
||||||
|
* @param htmlContent HTML内容
|
||||||
|
*/
|
||||||
|
void sendHtmlMessage(String to, String subject, String htmlContent);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送带附件的邮件
|
||||||
|
* @param to 收件人
|
||||||
|
* @param subject 主题
|
||||||
|
* @param text 内容
|
||||||
|
* @param attachmentFilename 附件文件名
|
||||||
|
* @param attachment 附件
|
||||||
|
*/
|
||||||
|
void sendMessageWithAttachment(
|
||||||
|
String to,
|
||||||
|
String subject,
|
||||||
|
String text,
|
||||||
|
String attachmentFilename,
|
||||||
|
InputStreamSource attachment
|
||||||
|
);
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.example.venue_reservation_service.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.example.venue_reservation_service.domain.Device;
|
||||||
|
import com.example.venue_reservation_service.mapper.DeviceMapper;
|
||||||
|
import com.example.venue_reservation_service.service.DeviceService;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 31586
|
||||||
|
* @description 针对表【venue_device】的数据库操作Service实现
|
||||||
|
* @createDate 2025-06-29 14:38:46
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DeviceMapper deviceMapper;
|
||||||
|
|
||||||
|
// 模拟设备数据
|
||||||
|
@Override
|
||||||
|
public Device generateDeviceData() {
|
||||||
|
Device device = new Device();
|
||||||
|
device.setTimestamp(LocalDateTime.now());
|
||||||
|
device.setTemperature(20 + Math.random() * 15);
|
||||||
|
device.setHumidity(30 + Math.random() * 50);
|
||||||
|
device.setFanStatus(device.getTemperature() > 25 ? 1 : 0);
|
||||||
|
device.setWarningStatus((device.getTemperature() > 30 ||
|
||||||
|
device.getHumidity() > 70 ||
|
||||||
|
device.getHumidity() < 30) ? 1 : 0);
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取最新设备数据
|
||||||
|
@Override
|
||||||
|
public List<Device> getLatestDevices() {
|
||||||
|
return deviceMapper.selectLatestDevices(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,80 @@
|
|||||||
|
package com.example.venue_reservation_service.service.impl;
|
||||||
|
|
||||||
|
import com.example.venue_reservation_service.service.EmailService;
|
||||||
|
import jakarta.mail.MessagingException;
|
||||||
|
import jakarta.mail.internet.MimeMessage;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.core.io.InputStreamSource;
|
||||||
|
import org.springframework.mail.MailException;
|
||||||
|
import org.springframework.mail.SimpleMailMessage;
|
||||||
|
import org.springframework.mail.javamail.JavaMailSender;
|
||||||
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class EmailServiceImpl implements EmailService {
|
||||||
|
|
||||||
|
private final JavaMailSender mailSender;
|
||||||
|
|
||||||
|
@Value("${spring.mail.username}")
|
||||||
|
private String from;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendSimpleMessage(String to, String subject, String text) {
|
||||||
|
try {
|
||||||
|
SimpleMailMessage message = new SimpleMailMessage();
|
||||||
|
message.setFrom(from);
|
||||||
|
message.setTo(to);
|
||||||
|
message.setSubject(subject);
|
||||||
|
message.setText(text);
|
||||||
|
|
||||||
|
mailSender.send(message);
|
||||||
|
} catch (MailException e) {
|
||||||
|
throw new RuntimeException("邮件发送失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendHtmlMessage(String to, String subject, String htmlContent) {
|
||||||
|
try {
|
||||||
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||||
|
|
||||||
|
helper.setFrom(from);
|
||||||
|
helper.setTo(to);
|
||||||
|
helper.setSubject(subject);
|
||||||
|
helper.setText(htmlContent, true);
|
||||||
|
|
||||||
|
mailSender.send(message);
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
throw new RuntimeException("HTML邮件发送失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendMessageWithAttachment(
|
||||||
|
String to,
|
||||||
|
String subject,
|
||||||
|
String text,
|
||||||
|
String attachmentFilename,
|
||||||
|
InputStreamSource attachment
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||||
|
|
||||||
|
helper.setFrom(from);
|
||||||
|
helper.setTo(to);
|
||||||
|
helper.setSubject(subject);
|
||||||
|
helper.setText(text);
|
||||||
|
|
||||||
|
helper.addAttachment(attachmentFilename, attachment);
|
||||||
|
|
||||||
|
mailSender.send(message);
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
throw new RuntimeException("带附件邮件发送失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
<?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.example.venue_reservation_service.mapper.DeviceMapper">
|
||||||
|
|
||||||
|
<resultMap id="BaseResultMap" type="com.example.venue_reservation_service.domain.Device">
|
||||||
|
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||||
|
<result property="timestamp" column="timestamp" jdbcType="TIMESTAMP"/>
|
||||||
|
<result property="temperature" column="temperature" jdbcType="FLOAT"/>
|
||||||
|
<result property="humidity" column="humidity" jdbcType="FLOAT"/>
|
||||||
|
<result property="fanStatus" column="fan_status" jdbcType="TINYINT"/>
|
||||||
|
<result property="warningStatus" column="warning_status" jdbcType="TINYINT"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<sql id="Base_Column_List">
|
||||||
|
id,timestamp,temperature,
|
||||||
|
humidity,fan_status,warning_status
|
||||||
|
</sql>
|
||||||
|
</mapper>
|
Loading…
Reference in New Issue