添加能耗采集定时任务
This commit is contained in:
@@ -2,8 +2,8 @@ spring:
|
||||
application:
|
||||
name: lideeyunji-server
|
||||
profiles:
|
||||
# active: local # 读取配置
|
||||
active: prod # 读取配置
|
||||
active: local # 读取配置
|
||||
# active: prod # 读取配置
|
||||
main:
|
||||
allow-circular-references: true # 允许循环依赖,因为项目是三层架构,无法避免这个情况。
|
||||
|
||||
|
||||
@@ -17,8 +17,6 @@ import org.springframework.stereotype.Component;
|
||||
@Component("SaSetdtlSyncJob")
|
||||
public class SaSetdtlSyncJob implements JobHandler {
|
||||
|
||||
@Autowired
|
||||
private ILideeYunJiService lideeYunJiService;
|
||||
|
||||
@Autowired
|
||||
private IGrBiSaSetdtlService grBiSaSetdtlService;
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
|
||||
package com.lideeyunji.core.framework.config.job;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.lideeyunji.core.framework.service.ILideeYunJiService;
|
||||
import com.lideeyunji.tool.framework.common.util.http.HttpUtils;
|
||||
import com.lideeyunji.tool.framework.quartz.core.handler.JobHandler;
|
||||
import com.lideeyunji.tool.framework.tenant.core.aop.TenantIgnore;
|
||||
import com.lideeyunji.tool.framework.yunji.component.redis.lideeYunjiRedisUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ehcache.xml.model.CacheTemplate;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 保存模块使用记录 能耗模块使用记录
|
||||
*/
|
||||
@Slf4j
|
||||
@Component("anyToGrBiNh")
|
||||
public class anyToGrBiNh implements JobHandler {
|
||||
|
||||
@Autowired
|
||||
private lideeYunjiRedisUtils redisUtil;
|
||||
|
||||
@Autowired
|
||||
private ILideeYunJiService lideeYunJiService;
|
||||
|
||||
@Override
|
||||
@TenantIgnore
|
||||
public String execute(String param) {
|
||||
log.info("*********** 同步能耗数据 ************");
|
||||
|
||||
getToken();
|
||||
String resultStr = "*********** 同步能耗数据保存成功 ************";
|
||||
log.info(resultStr);
|
||||
return resultStr;
|
||||
}
|
||||
|
||||
private void getToken() {
|
||||
try {
|
||||
String nhtoken = redisUtil.get("nh_token", String.class);
|
||||
|
||||
System.out.println(nhtoken);
|
||||
if(nhtoken==null){
|
||||
|
||||
// 2. POST 表单请求
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("fLoginname", "9287ee99ae49b47dc7ce1746eac04f8b");
|
||||
params.put("fPassword", "d23d0731bcf61d824aa30372da5fae0b");
|
||||
params.put("loginType", "password");
|
||||
params.put("projectType", "4");
|
||||
String postResult = HttpUtils.doPost("http://111.39.171.64:8090/SubstationWEBV2/user/login", params, null);
|
||||
|
||||
JSONObject jsonObject = new JSONObject(postResult);
|
||||
jsonObject= new JSONObject(jsonObject.get("data").toString());
|
||||
redisUtil.set("nh_token",jsonObject.get("token"),(Long)jsonObject.get("tokenExpireTime"));
|
||||
System.out.println(jsonObject.get("token"));
|
||||
getNhsj(jsonObject.get("token").toString());
|
||||
}
|
||||
else {
|
||||
nhtoken = redisUtil.get("nh_token").toString();
|
||||
getNhsj(nhtoken);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void getNhsj(String token){
|
||||
try {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Authorization", token);
|
||||
String getResult = HttpUtils.doGet("http://111.39.171.64:8090/SubstationWEBV2/main/Compare/overview?fCoaccountno=1", headers);
|
||||
System.out.println("GET Response:\n" + getResult);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 3. POST JSON 请求
|
||||
// String jsonBody = "{\"name\":\"Alice\",\"age\":25}";
|
||||
// Map<String, String> headers = new HashMap<>();
|
||||
// headers.put("Authorization", "Bearer your_token_here");
|
||||
// String jsonResult = HttpUtils.doPostJson("https://api.example.com/user", jsonBody, headers);
|
||||
// System.out.println("POST JSON Response:\n" + jsonResult);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void main(String[] args) {
|
||||
getToken();
|
||||
// 2. POST 表单请求
|
||||
// Map<String, String> params = new HashMap<>();
|
||||
// params.put("fLoginname", "2613504b593d16343ccf368879f9519c");
|
||||
// params.put("fPassword", "c7d967eaeaa0963fe33c740b46316590");
|
||||
// params.put("loginType", "password");
|
||||
// params.put("projectType", "4");
|
||||
// String postResult = HttpUtils.doPost("http://111.39.171.64:8090/SubstationWEBV2/user/login", params, null);
|
||||
// System.out.println("POST Form Response:\n" + postResult);
|
||||
// JSONObject jsonObject = new JSONObject(postResult);
|
||||
// jsonObject= new JSONObject(jsonObject.get("data").toString());
|
||||
//System.out.println(jsonObject.get("token"));
|
||||
|
||||
// 1. GET 请求
|
||||
// String getResult = HttpUtils.doGet("https://api.example.com/data", null);
|
||||
// System.out.println("GET Response:\n" + getResult);
|
||||
|
||||
|
||||
// 3. POST JSON 请求
|
||||
// String jsonBody = "{\"name\":\"Alice\",\"age\":25}";
|
||||
// Map<String, String> headers = new HashMap<>();
|
||||
// headers.put("Authorization", "Bearer your_token_here");
|
||||
// String jsonResult = HttpUtils.doPostJson("https://api.example.com/user", jsonBody, headers);
|
||||
// System.out.println("POST JSON Response:\n" + jsonResult);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -278,6 +278,9 @@ public class Func extends FuncBase {
|
||||
return fieldValObj;
|
||||
}
|
||||
|
||||
/*
|
||||
* http post请求
|
||||
* */
|
||||
public static String sendPost(String url, Object body) throws Exception {
|
||||
String accept = HttpRequest.post(url)
|
||||
.body(Func.json2Str(body))
|
||||
|
||||
@@ -7,6 +7,18 @@
|
||||
<artifactId>lidee-tool</artifactId>
|
||||
<version>${lidee.version}</version>
|
||||
</parent>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>10</source>
|
||||
<target>10</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>tool-common</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
@@ -10,8 +10,12 @@ import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -21,6 +25,9 @@ import java.util.Map;
|
||||
*/
|
||||
public class HttpUtils {
|
||||
|
||||
private static final int CONNECT_TIMEOUT = 5000; // 连接超时(毫秒)
|
||||
private static final int READ_TIMEOUT = 10000; // 读取超时(毫秒)
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static String replaceUrlQuery(String url, String key, String value) {
|
||||
UrlBuilder builder = UrlBuilder.of(url, Charset.defaultCharset());
|
||||
@@ -122,5 +129,203 @@ public class HttpUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送 HTTP GET 请求
|
||||
*
|
||||
* @param urlStr 请求地址
|
||||
* @param headers 请求头(可为 null)
|
||||
* @return 响应字符串
|
||||
*/
|
||||
public static String doGet(String urlStr, Map<String, String> headers) throws IOException {
|
||||
HttpURLConnection conn = null;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(CONNECT_TIMEOUT);
|
||||
conn.setReadTimeout(READ_TIMEOUT);
|
||||
conn.setDoInput(true);
|
||||
|
||||
// 设置请求头
|
||||
if (headers != null && !headers.isEmpty()) {
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
conn.setRequestProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
int responseCode = conn.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
reader = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
||||
StringBuilder result = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
result.append(line);
|
||||
}
|
||||
return result.toString();
|
||||
} else {
|
||||
throw new IOException("GET request failed, response code: " + responseCode);
|
||||
}
|
||||
} finally {
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
if (conn != null) {
|
||||
conn.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送 HTTP POST 请求(application/x-www-form-urlencoded)
|
||||
*
|
||||
* @param urlStr 请求地址
|
||||
* @param params 表单参数(可为 null)
|
||||
* @param headers 请求头(可为 null)
|
||||
* @return 响应字符串
|
||||
*/
|
||||
public static String doPost(String urlStr, Map<String, String> params, Map<String, String> headers)
|
||||
throws IOException {
|
||||
HttpURLConnection conn = null;
|
||||
BufferedWriter writer = null;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setConnectTimeout(CONNECT_TIMEOUT);
|
||||
conn.setReadTimeout(READ_TIMEOUT);
|
||||
conn.setDoOutput(true);
|
||||
conn.setDoInput(true);
|
||||
|
||||
// 设置请求头
|
||||
if (headers != null && !headers.isEmpty()) {
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
conn.setRequestProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
// 写入表单参数
|
||||
if (params != null && !params.isEmpty()) {
|
||||
StringBuilder paramStr = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
if (paramStr.length() > 0) {
|
||||
paramStr.append("&");
|
||||
}
|
||||
paramStr.append(entry.getKey())
|
||||
.append("=")
|
||||
.append(java.net.URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
|
||||
}
|
||||
writer = new BufferedWriter(
|
||||
new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
|
||||
writer.write(paramStr.toString());
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
int responseCode = conn.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
reader = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
||||
StringBuilder result = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
result.append(line);
|
||||
}
|
||||
return result.toString();
|
||||
} else {
|
||||
throw new IOException("POST request failed, response code: " + responseCode);
|
||||
}
|
||||
} finally {
|
||||
if (writer != null) {
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
if (conn != null) {
|
||||
conn.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送 JSON 格式的 POST 请求
|
||||
*
|
||||
* @param urlStr 请求地址
|
||||
* @param json 请求体 JSON 字符串
|
||||
* @param headers 请求头(可为 null)
|
||||
* @return 响应字符串
|
||||
*/
|
||||
public static String doPostJson(String urlStr, String json, Map<String, String> headers)
|
||||
throws IOException {
|
||||
HttpURLConnection conn = null;
|
||||
BufferedWriter writer = null;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setConnectTimeout(CONNECT_TIMEOUT);
|
||||
conn.setReadTimeout(READ_TIMEOUT);
|
||||
conn.setDoOutput(true);
|
||||
conn.setDoInput(true);
|
||||
|
||||
// 设置默认 Content-Type
|
||||
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
||||
|
||||
// 设置自定义请求头
|
||||
if (headers != null && !headers.isEmpty()) {
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
conn.setRequestProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
// 写入 JSON 请求体
|
||||
writer = new BufferedWriter(
|
||||
new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
|
||||
writer.write(json);
|
||||
writer.flush();
|
||||
|
||||
int responseCode = conn.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
reader = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
||||
StringBuilder result = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
result.append(line);
|
||||
}
|
||||
return result.toString();
|
||||
} else {
|
||||
throw new IOException("POST JSON request failed, response code: " + responseCode);
|
||||
}
|
||||
} finally {
|
||||
if (writer != null) {
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
if (conn != null) {
|
||||
conn.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user