Commit 5508850a authored by ZWT's avatar ZWT

feat(能源管理系统): 邮件接收

1.添加天气数据邮件方式接收定时任务;
2.修改配置文件,增加新建定时任务,同时增加自定义参数;
3.创建天气邮件数据接收处理实现类,同时验证获取自定义参数方法;
4.添加收件工具类,验证是否能正常接收邮件;
5.天气邮件数据接收定时任务增加获取未读天气数据逻辑,增加附件文件临时存储方法并验证是否能正常读取文件数据同时转换为json数据;

BREAKING CHANGE: 无

Closes 无

[skip ci]
parent fd78bd66
package pps.core.base.service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.mail.imap.IMAPFolder;
import org.apache.commons.lang3.StringUtils;
import pps.cloud.base.service.IBaseWeatherCloudService;
import pps.core.base.service.data.DailyData;
import xstartup.annotation.XService;
import xstartup.base.XContext;
import xstartup.base.XStartup;
import xstartup.data.XServiceResult;
import javax.mail.*;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* 天气数据接收Cloud模块
*
......@@ -23,7 +42,136 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService {
*/
@Override
public XServiceResult weatherDataProcess(XContext context) {
String property = XStartup.getCurrent().getProperty("weather.file.temp.path");
String host = XStartup.getCurrent().getProperty("weather.mail.host");
String username = XStartup.getCurrent().getProperty("weather.mail.username");
String password = XStartup.getCurrent().getProperty("weather.mail.password");
String protocol = XStartup.getCurrent().getProperty("weather.mail.protocol");
//取未读邮件
Store store = null;
Folder folder = null;
try {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", protocol);
Session session = Session.getDefaultInstance(props, null);
store = session.getStore(protocol);
store.connect(host, username, password);
folder = store.getFolder("INBOX");
if (folder instanceof IMAPFolder) {
IMAPFolder imapFolder = (IMAPFolder) folder;
// javamail中使用id命令有校验checkOpened, 所以要去掉id方法中的checkOpened();
imapFolder.doCommand(p -> {
Map<String, String> gmap = new HashMap<>();
gmap.put("GUID", "FUTONG");
p.id(gmap);
return null;
});
}
if (Objects.nonNull(folder)) {
folder.open(Folder.READ_WRITE);
}
Message[] messages = folder.getMessages();
if (ArrayUtil.isNotEmpty(messages)) {
List<Message> messageList = new ArrayList<>(16);
for (Message message : messages) {
boolean isNew = false;
for (Flags.Flag flag : message.getFlags().getSystemFlags()) {
if (flag == Flags.Flag.SEEN) {
isNew = true;
}
}
if (!isNew && StrUtil.endWith(message.getSubject(), "天气预测数据")) {
messageList.add(message);
}
}
if (CollUtil.isNotEmpty(messageList)) {
//读取附件
for (Message message : messageList) {
String tempFilePath = this.saveAttachment(message);
FileReader fileReader = new FileReader(tempFilePath);
String jsonString = fileReader.readString();
JSONObject jsonObject = JSONObject.parseObject(jsonString);
Map<String, List<DailyData>> cityDataMap = new HashMap<>(16);
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof JSONArray) {
cityDataMap.put(key, ((JSONArray) value).toJavaList(DailyData.class));
}
}
FileUtil.del(tempFilePath);
}
}
}
} catch (MessagingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IoUtil.close(folder);
IoUtil.close(store);
}
return XServiceResult.OK;
}
/**
* 保存附件
*
* @param part 部分
* @return {@link String}
* @throws MessagingException 消息传递异常
* @throws IOException IOException
*/
private String saveAttachment(Part part) throws MessagingException, IOException {
String fileName;
String filePath = null;
if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
BodyPart mPart;
for (int i = 0; i < mp.getCount(); i++) {
mPart = mp.getBodyPart(i);
String disposition = mPart.getDisposition();
if (Part.ATTACHMENT.equals(disposition) || Part.INLINE.equals(disposition)) {
fileName = mPart.getFileName();
if (StrUtil.isNotEmpty(fileName) && StringUtils.lowerCase(fileName).indexOf("GB2312") != -1) {
fileName = MimeUtility.decodeText(fileName);
}
fileName = StrUtil.replace(fileName, "\\?|=", "");
filePath = this.saveTempFile(fileName, mPart.getInputStream());
} else if (mPart.isMimeType("multipart/*")) {
this.saveAttachment(mPart);
} else {
fileName = mPart.getFileName();
if (StrUtil.isNotEmpty(fileName) && (StringUtils.lowerCase(fileName).indexOf("GB2312") != -1)) {
fileName = MimeUtility.decodeText(fileName);
filePath = this.saveTempFile(fileName, mPart.getInputStream());
}
}
}
} else if (part.isMimeType("message/rfc822")) {
this.saveAttachment((Part) part.getContent());
}
return filePath;
}
/**
* 保存临时文件
*
* @param fileName 文件名
* @param in 在里面
* @return {@link String}
*/
private String saveTempFile(String fileName, InputStream in) {
String storeDir = XStartup.getCurrent().getProperty("weather.file.temp.path");
StringBuilder stringBuilder = new StringBuilder(storeDir)
.append(DateTime.now().toString("yyyyMMdd"));
File savePath = new File(stringBuilder.toString());
if (!savePath.exists()) {
savePath.mkdirs();
}
String filePath = stringBuilder.append(File.separator).append(fileName).toString();
FileUtil.writeFromStream(in, filePath);
IoUtil.close(in);
return filePath;
}
}
package pps.core.base.service.data;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List;
/**
* 日数据
*
* @author ZWT
* @date 2023/11/13 14:08
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DailyData {
/**
* 日期时间
*/
private Date dateTime;
/**
* 时间
*/
private List<String> hourTimeArray;
/**
* 天气
*/
private List<String> weatherArray;
/**
* 气温
*/
private List<String> temperatureArray;
/**
* 降水
*/
private List<String> precipitationArray;
/**
* 风速
*/
private List<String> windSpeedArray;
/**
* 风向
*/
private List<String> windDirectionArray;
/**
* 气压
*/
private List<String> pressureArray;
/**
* 湿度
*/
private List<String> humidityArray;
/**
* 云量
*/
private List<String> cloudCoverArray;
}
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