Commit f7bc6f2c authored by tianchao's avatar tianchao

1光伏预测调整,预测算法页面

parent 332c1d57
......@@ -17,7 +17,7 @@ public class PlantPowerDataEnt implements Serializable {
@TableId(type = IdType.AUTO)
private Integer id;
@XText("线路id")
@XText("电站id")
@TableField
private String plantId;
......
package pps.core.prediction.entity;
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 lombok.Data;
import xstartup.annotation.XText;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class PlantPowerDataView implements Serializable {
@TableId(type = IdType.AUTO)
private Integer id;
@XText("电站id")
@TableField
private String plantId;
@XText("日期")
@TableField
private Date dataDate;
@XText("温度")
@TableField
private BigDecimal temperature;
@XText("湿度")
@TableField
private BigDecimal humidity;
@XText("风速")
@TableField
private BigDecimal windSpeed;
@XText("风向")
@TableField
private BigDecimal windDirection;
@XText("压强")
@TableField
private BigDecimal pressure;
@XText("组件平面辐照度")
@TableField
private BigDecimal planeIrradiance;
@XText("全水平辐照度")
@TableField
private BigDecimal horizontalIrradiance;
@XText("实际功率")
@TableField
private BigDecimal power;
}
package pps.core.prediction.enums;
public enum ImportFieldDic {
dataDate("dataDate","日期",1),
windSpeed("windSpeed","风速",2),
windDirection("windDirection","风向",3),
temperature("temperature","温度",4),
pressure("pressure","压强",5),
humidity("humidity","湿度",6),
planeIrradiance("planeIrradiance","组件平面辐照度",7),
horizontalIrradiance("horizontalIrradiance","全水平辐照度",8),
power("power","实际功率",9),
;
ImportFieldDic(String col, String name, Integer sort) {
this.col = col;
this.name = name;
this.sort = sort;
}
private String col;
private String name;
private Integer sort;
public String getCol() {
return col;
}
public void setCol(String col) {
this.col = col;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public static ImportFieldDic findByValue(String col){
for(ImportFieldDic item : values()){
if(item.getCol().equals(col)){
return item;
}
}
return null;
}
}
package pps.core.prediction.mapper;
import org.springframework.stereotype.Repository;
import pps.core.prediction.entity.PlantPowerDataView;
import java.util.List;
@Repository(value="pps.core.prediction.mapper.PlantPredictedPowerDataViewMapper")
public interface PlantPowerDataViewMapper {
int insertBatch(List<PlantPowerDataView> list);
}
package pps.core.prediction.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import pps.core.common.utils.ExcelUtils;
import pps.core.common.utils.ExportExcelUtils;
import pps.core.common.utils.MapUtil;
import pps.core.prediction.entity.PlantPowerDataEnt;
import pps.core.prediction.entity.PlantPowerDataView;
import pps.core.prediction.enums.ImportFieldDic;
import pps.core.prediction.mapper.PlantPowerDataMapper;
import pps.core.prediction.mapper.PlantPowerDataViewMapper;
import pps.core.prediction.service.data.plant_power_data.ImportFileInput;
import pps.core.prediction.service.data.plant_power_data.QueryPlantPowerDataInput;
import pps.core.prediction.service.data.plant_power_data.QueryPlantPowerDataOutput;
import pps.core.prediction.utils.HttpUtils;
import xstartup.annotation.XService;
import xstartup.annotation.XText;
import xstartup.base.XContext;
import xstartup.base.tool.XStorageTool;
import xstartup.base.util.XDateUtils;
import xstartup.base.util.XStringUtils;
import xstartup.data.XFileResult;
import xstartup.data.XFileType;
import xstartup.data.XPageResult;
import xstartup.data.XServiceResult;
import xstartup.feature.api.annotation.XApiGet;
import xstartup.feature.api.annotation.XApiPost;
import xstartup.feature.api.annotation.XApiUpload;
import xstartup.feature.mybatis.helper.XMapperHelper;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
@XService
@XText("电站实际发电数据")
public class PlantPowerDataService {
@XApiGet
public XPageResult<QueryPlantPowerDataOutput> queryPlantPowerDataList(XContext context, QueryPlantPowerDataInput input){
PlantPowerDataMapper mapper = context.getBean(PlantPowerDataMapper.class);
QueryWrapper<PlantPowerDataEnt> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(PlantPowerDataEnt::getPlantId,input.getPlantId());
if(XStringUtils.isNotEmpty(input.getStartTime()))
queryWrapper.lambda().ge(PlantPowerDataEnt::getDataDate,input.getStartTime());
if(XStringUtils.isNotEmpty(input.getEndTime()))
queryWrapper.lambda().ge(PlantPowerDataEnt::getDataDate,input.getEndTime());
queryWrapper.lambda().orderByDesc(PlantPowerDataEnt::getDataDate);
return XMapperHelper.query(mapper, input, queryWrapper, QueryPlantPowerDataOutput.class);
}
@XText("下载电站实际发电数据导入模板")
@XApiGet
public XFileResult downloadImportTemplate(XContext context){
String fileSavePath = XStorageTool.getAbsolutePath("/temp/excel/导入模板_" + XDateUtils.getString(new Date(),"yyyy-MM-dd") + ".xlsx");
String templateFilePath = "template/import_plant_data_template.xlsx";
InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream(templateFilePath);
byte[] excelContent = ExportExcelUtils.genSingleExcelFileData(templateInputStream, null);
try {
ExportExcelUtils.outputFileData(excelContent,fileSavePath);
} catch (Exception e) {
e.printStackTrace();
}
return XFileResult.success(XFileType.APPLICATION_XLSX, fileSavePath);
}
@XText("导入电站实际发电数据")
@XApiUpload
public XServiceResult importPlantPowerData(XContext context, ImportFileInput input) {
List<String> headerList = getExcelHeaderList();
List<Map<String, Object>> mapList = ExcelUtils.readExcel(input.getFile().getInputStream(), input.getFile().getFileName(), headerList, 0);
try {
List<PlantPowerDataView> dataList = new ArrayList<>();
Date minDate = null;
Date maxDate = null;
for (Map<String, Object> objectMap : mapList) {
PlantPowerDataView dataView = MapUtil.mapToObj(objectMap, PlantPowerDataView.class);
dataView.setPlantId(input.getPlantId());
if(dataView.getDataDate()!=null){
if(minDate==null){
minDate = dataView.getDataDate();
maxDate = dataView.getDataDate();
}
if(minDate.compareTo(dataView.getDataDate())>0){
minDate = dataView.getDataDate();
}else if(maxDate.compareTo(dataView.getDataDate())<0){
maxDate = dataView.getDataDate();
}
}
dataList.add(dataView);
}
if(minDate==null){
return XServiceResult.error(1000,"日期错误");
}
PlantPowerDataMapper mapper = context.getBean(PlantPowerDataMapper.class);
QueryWrapper<PlantPowerDataEnt> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(PlantPowerDataEnt::getPlantId,input.getPlantId())
.ge(PlantPowerDataEnt::getDataDate,minDate)
.le(PlantPowerDataEnt::getDataDate,maxDate);
mapper.delete(queryWrapper);
PlantPowerDataViewMapper viewMapper = context.getBean(PlantPowerDataViewMapper.class);
viewMapper.insertBatch(dataList);
//进行训练
doTrainData(context , input.getPlantId());
}catch (Exception e){
return XServiceResult.error(-1,e.getMessage());
}
return XServiceResult.OK;
}
private List<String> getExcelHeaderList(){
List<Map<String,Object>> fieldList = new ArrayList<>();
for (ImportFieldDic value : ImportFieldDic.values()) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("col",value.getCol());
map.put("sort",value.getSort());
fieldList.add(map);
}
fieldList.sort(new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
return ((Integer)o1.get("sort")).compareTo((Integer)o2.get("sort"));
}
});
return fieldList.stream().map(x->((String)x.get("col"))).collect(Collectors.toList());
}
public void doTrainData(XContext context, String plantId ){
try {
Object ret = HttpUtils.send2("http://127.0.0.1:10098/aiprediction/xgbtrain?plantId=" + plantId,
"");;
context.getLogger().info(ret.toString());
}catch (Exception e) {
throw new RuntimeException("调用训练接口失败");
}
}
}
......@@ -5,8 +5,10 @@ import pps.core.prediction.entity.PlantPredictedPowerDataEnt;
import pps.core.prediction.entity.PlantPredictedPowerLongTermDataEnt;
import pps.core.prediction.mapper.PlantPredictedPowerDataMapper;
import pps.core.prediction.mapper.PlantPredictedPowerLongTermDataMapper;
import pps.core.prediction.service.data.plant_predicted_power_data.DoPlantPredictedPowerDataInput;
import pps.core.prediction.service.data.plant_predicted_power_data.GetPlantPredictedPowerDataInput;
import pps.core.prediction.service.data.plant_predicted_power_data.GetPlantPredictedPowerDataOutput;
import pps.core.prediction.utils.HttpUtils;
import xstartup.annotation.XService;
import xstartup.annotation.XText;
import xstartup.base.XContext;
......@@ -14,7 +16,9 @@ import xstartup.base.util.XCopyUtils;
import xstartup.base.util.XDateUtils;
import xstartup.base.util.XStringUtils;
import xstartup.data.XListResult;
import xstartup.data.XServiceResult;
import xstartup.feature.api.annotation.XApiGet;
import xstartup.feature.api.annotation.XApiPost;
import java.math.BigDecimal;
import java.util.Date;
......@@ -123,4 +127,21 @@ public class PlantPredictedPowerDataService {
return XListResult.success(outputs);
}
@XText("预测电站发电数据")
@XApiPost
public XServiceResult doPredictedPowerData(XContext context, DoPlantPredictedPowerDataInput input){
doPredictedData(context , input.getPlantId());
return XServiceResult.OK;
}
public void doPredictedData(XContext context, String plantId ){
try {
Object ret = HttpUtils.send2("http://127.0.0.1:10098/aiprediction/xgbreason?plantId=" + plantId,
"");
context.getLogger().info(ret.toString());
}catch (Exception e) {
throw new RuntimeException("调用训练接口失败");
}
}
}
package pps.core.prediction.service.data.plant_power_data;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import xstartup.annotation.XText;
import xstartup.data.XUploadFile;
/**
* @author lixueyan
* @date 2023/6/9 0009 9:45
*/
@Data
public class ImportFileInput {
@XText("上传的文件")
@NotNull
private XUploadFile file;
@XText("电站id")
@NotNull
private String plantId;
}
package pps.core.prediction.service.data.plant_power_data;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import xstartup.annotation.XText;
import xstartup.base.data.XPageInput;
import java.math.BigDecimal;
@Data
public class QueryPlantPowerDataInput extends XPageInput {
private Integer id;
@XText("电站id")
private String plantId;
@XText("日期")
private String dataDate;
@XText("温度")
private BigDecimal temperature;
@XText("湿度")
private BigDecimal humidity;
@XText("风速")
private BigDecimal windSpeed;
@XText("风向")
private BigDecimal windDirection;
@XText("压强")
private BigDecimal pressure;
@XText("组件平面辐照度")
private BigDecimal planeIrradiance;
@XText("全水平辐照度")
private BigDecimal horizontalIrradiance;
@XText("实际功率")
private BigDecimal power;
@XText("开始时间")
private String startTime;
@XText("结束时间")
private String endTime;
}
\ No newline at end of file
package pps.core.prediction.service.data.plant_power_data;
import lombok.Data;
import xstartup.annotation.XText;
import xstartup.base.data.XPageInput;
import java.math.BigDecimal;
@Data
public class QueryPlantPowerDataOutput extends XPageInput {
private Integer id;
@XText("电站id")
private String plantId;
@XText("日期")
private String dataDate;
@XText("温度")
private BigDecimal temperature;
@XText("湿度")
private BigDecimal humidity;
@XText("风速")
private BigDecimal windSpeed;
@XText("风向")
private BigDecimal windDirection;
@XText("压强")
private BigDecimal pressure;
@XText("组件平面辐照度")
private BigDecimal planeIrradiance;
@XText("全水平辐照度")
private BigDecimal horizontalIrradiance;
@XText("实际功率")
private BigDecimal power;
}
\ No newline at end of file
package pps.core.prediction.service.data.plant_predicted_power_data;
import lombok.Data;
import xstartup.annotation.XText;
@Data
public class DoPlantPredictedPowerDataInput {
@XText("电站id")
private String plantId;
}
package pps.core.prediction.utils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HttpUtils {
public static Object sendPost(String url, Object param){
// 请求头设置
HttpHeaders headers = new HttpHeaders();
List<MediaType> acceptableMediaType = new ArrayList<>();
acceptableMediaType.add(MediaType.APPLICATION_JSON);
headers.setAccept(acceptableMediaType);
headers.setContentType(MediaType.APPLICATION_JSON);
// 组装请求体
//String jsonString = XJsonUtils.toJson(param);
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity(param,headers);
//发起请求
RestTemplate restTemplate = new RestTemplate();
Object res = restTemplate.postForObject(url, request, Object.class);
return res;
}
public static String send2(String url, String param) throws Exception {
URL localURL = new URL(url);
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept", "application/json");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(param.length()));
httpURLConnection.setConnectTimeout(10000);
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
String resultBuffer = "";
try {
outputStream = httpURLConnection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(param.toString());
outputStreamWriter.flush();
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception(
"HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
inputStream = httpURLConnection.getInputStream();
resultBuffer = convertStreamToString(inputStream);
System.out.println(resultBuffer);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (outputStream != null) {
outputStream.close();
}
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer;
}
public static String convertStreamToString(InputStream is) {
StringBuilder sb1 = new StringBuilder();
byte[] bytes = new byte[4096];
int size = 0;
try {
while ((size = is.read(bytes)) > 0) {
String str = new String(bytes, 0, size, "UTF-8");
sb1.append(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb1.toString();
}
public static void main(String[] args)throws Exception {
Map<String,String> object = new HashMap<>();
object.put("pipelineId","11");
object.put("stationId", "11");
object.put("customerId", "11");
object.put("startTime", "2022-10-23 00:00:00");
object.put("strByteString","11");
String parm = "pipelineId=" +"12"+
"&stationId=" +"12"+
"&customerId=" +"12"+
"&startTime=" + "2022-12-01 00:00:00"+
"&strByteString=" +"12" ;
send2("http://10.20.89.75/interface/pdf/InsertSIFile?"+parm , parm);
}
}
<?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="pps.core.prediction.mapper.PlantPowerDataViewMapper">
<insert id="insertBatch" parameterType="java.util.List">
INSERT INTO plant_power_data
(plant_id,
data_date,
temperature,
humidity,
wind_speed,
wind_direction,
pressure,
plane_irradiance,
horizontal_irradiance,
power) VALUES
<foreach collection="list" separator="," item="item">
(#{item.plantId},
#{item.dataDate},
#{item.temperature},
#{item.humidity},
#{item.windSpeed},
#{item.windDirection},
#{item.pressure},
#{item.planeIrradiance},
#{item.horizontalIrradiance},
#{item.power})
</foreach>
</insert>
</mapper>
\ No newline at end of file
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