|
@@ -2,19 +2,20 @@ package com.huimv.environ.eco.schedule;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.huimv.common.utils.Result;
|
|
|
import com.huimv.environ.eco.entity.EcoDevice;
|
|
|
import com.huimv.environ.eco.entity.EcoDeviceFlow;
|
|
|
import com.huimv.environ.eco.mapper.EcoDeviceFlowMapper;
|
|
|
import com.huimv.environ.eco.service.EcoDeviceFlowService;
|
|
|
import com.huimv.environ.eco.service.EcoDeviceService;
|
|
|
+import com.huimv.environ.eco.service.EcoDryListService;
|
|
|
import com.huimv.environ.eco.service.SysConfigService;
|
|
|
+import com.huimv.environ.env.utils.DateUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.http.HttpEntity;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.HttpMethod;
|
|
|
-import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.http.*;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
@@ -23,10 +24,7 @@ import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.lang.reflect.Array;
|
|
|
import java.text.ParseException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @Project : huimv-farm-datacenter
|
|
@@ -37,54 +35,115 @@ import java.util.Map;
|
|
|
* @Create : 2022/11/9
|
|
|
**/
|
|
|
@Component
|
|
|
+@Slf4j
|
|
|
public class ScheduleTask {
|
|
|
@Autowired
|
|
|
private RestTemplate restTemplate;
|
|
|
@Autowired
|
|
|
- private EcoDeviceFlowMapper ecoDeviceFlowMapper;
|
|
|
- @Autowired
|
|
|
private com.huimv.environ.eco.service.EcoDeviceFlowService ecoDeviceFlowService;
|
|
|
@Autowired
|
|
|
private EcoDeviceService ecoDeviceService;
|
|
|
@Autowired
|
|
|
private SysConfigService sysConfigService;
|
|
|
- private static String URL = "http://www.0531yun.com/app/GetDeviceData";
|
|
|
+ @Autowired
|
|
|
+ private EcoDryListService ecoDryListService;
|
|
|
+ @Value("${url.threshold.service}")
|
|
|
+ private String URL_THRESHOLD_SERVICE;
|
|
|
+ @Value("${url.environ.device.service}")
|
|
|
+ private String URL_ENVIRON_DEVICE_SERVICE;
|
|
|
+// private static String URL = "http://www.0531yun.com/app/GetDeviceData";
|
|
|
|
|
|
@Scheduled(cron = "0 0/5 * * * ?") //每5分钟执行一次
|
|
|
+// @Scheduled(cron = "*/10 * * * * ?") //每10秒钟执行一次
|
|
|
public void getDeviceFlow() throws ParseException {
|
|
|
- System.out.println("getDeviceFlow " + new Date().getTime());
|
|
|
+ DateUtil dateUtil = new DateUtil();
|
|
|
+ System.out.println("开始取数:" + dateUtil.fromLongToDate(new Date().getTime(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
//查询所有设备
|
|
|
- List<EcoDevice> ecoDeviceList = ecoDeviceService.getDeviceList();
|
|
|
+ List<Map> ecoDeviceList = ecoDeviceService.getDeviceList2();
|
|
|
+ System.out.println("ecoDeviceList.size=" + ecoDeviceList.size());
|
|
|
if (ecoDeviceList.size() == 0) {
|
|
|
System.out.println("设备记录为空.");
|
|
|
} else {
|
|
|
- for (EcoDevice ecoDevice : ecoDeviceList) {
|
|
|
- Integer farmId = ecoDevice.getFarmId();
|
|
|
- String deviceGroupCode = ecoDevice.getDeviceCode();
|
|
|
-// System.out.println("farmId=" + farmId);
|
|
|
-// System.out.println("deviceCode=" + deviceCode);
|
|
|
+ for (int a = 0; a < ecoDeviceList.size(); a++) {
|
|
|
+ System.out.println("a========================================================================================================" + a);
|
|
|
+ Map deviceMap = ecoDeviceList.get(a);
|
|
|
+ Integer farmId = Integer.parseInt(deviceMap.get("farm_id") + "");
|
|
|
+ String deviceGroupCode = deviceMap.get("device_code") + "";
|
|
|
+ System.out.println("id=" + deviceMap.get("id"));
|
|
|
+ //读取高温阈值数据
|
|
|
+ JSONObject thresholdJo = getThresholdURL(farmId);
|
|
|
+ System.out.println("调取牧场[" + farmId + "]服务获取阈值接口:" + thresholdJo);
|
|
|
+ if (thresholdJo == null) {
|
|
|
+ log.error("thresholdJo======" + thresholdJo);
|
|
|
+ log.error("牧场[" + farmId + "]暂时没有洗消烘干温度设置.");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
String cfgName = "userId";
|
|
|
// 查询用户userId
|
|
|
String userId = sysConfigService.getUserId(farmId, cfgName);
|
|
|
if (userId == null) {
|
|
|
- System.out.println("userId不存在.");
|
|
|
+ System.out.println("牧场[" + farmId + "]的userId不存在.");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //调用远程设备上传流水接口,查询远程数据
|
|
|
+ JSONObject resultJo = JSONObject.parseObject(getDeviceFlowFromRemoteURL(userId, deviceGroupCode));
|
|
|
+ if (resultJo.getInteger("code") != 1000) {
|
|
|
+ System.out.println("牧场[" + farmId + "]设备组编号(" + deviceGroupCode + ")调用远程接口返回结果出错.");
|
|
|
} else {
|
|
|
- //调用远程接口,查询远程数据
|
|
|
- String remoteData = getBodyFromRemoteURL(userId, deviceGroupCode);
|
|
|
- JSONObject resultJo = JSONObject.parseObject(remoteData);
|
|
|
- if (resultJo.getInteger("code") != 1000) {
|
|
|
- System.out.println("设备组编号(" + deviceGroupCode + ")调用出错.");
|
|
|
- } else {
|
|
|
- JSONArray dataJa = resultJo.getJSONArray("data");
|
|
|
- for (int a = 0; a < dataJa.size(); a++) {
|
|
|
- JSONObject dataJo = dataJa.getJSONObject(a);
|
|
|
- ecoDeviceFlowService.save(dataJo);
|
|
|
- }
|
|
|
- }
|
|
|
+ JSONArray dataJa = resultJo.getJSONArray("data");
|
|
|
+ System.out.println("dataJa.size=" + dataJa.size());
|
|
|
+// for (int b = 0; b < dataJa.size(); b++) {
|
|
|
+// JSONObject dataJo = dataJa.getJSONObject(b);
|
|
|
+ JSONObject dataJo = dataJa.getJSONObject(0);
|
|
|
+ Date todayTime = dateUtil.getTodayDateTime();
|
|
|
+ //保存温度流水记录
|
|
|
+ ecoDeviceFlowService.save(dataJo, todayTime);
|
|
|
+ System.out.println("22 dataJo=" + dataJo);
|
|
|
+ System.out.println("33 thresholdJo=" + thresholdJo);
|
|
|
+ //新建烘干记录
|
|
|
+ ecoDryListService.saveDryList(deviceMap, dataJo, thresholdJo, todayTime);
|
|
|
+ //新建洗消烘干温度时长不足报警记录
|
|
|
+// newShortDryingtime();
|
|
|
+// }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ System.out.println("完成取数:" + dateUtil.fromLongToDate(new Date().getTime(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取阈值
|
|
|
+ private JSONObject getThresholdURL(Integer farmId) {
|
|
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
|
|
|
+ httpHeaders.setContentType(type);
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+ map.put("farmId", farmId);
|
|
|
+ HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map, httpHeaders);
|
|
|
+ //读取真实数据
|
|
|
+// String remoteUrl = "http://192.168.1.67:8010/produce/threshold/getThresholdByFarmId";
|
|
|
+ System.out.println("URL_THRESHOLD_SERVICE=" + URL_THRESHOLD_SERVICE);
|
|
|
+ ResponseEntity<String> entity = restTemplate.postForEntity(URL_THRESHOLD_SERVICE, objectHttpEntity, String.class);
|
|
|
+ JSONObject thresholdJo = JSONObject.parseObject(entity.getBody());
|
|
|
+ if (thresholdJo.getInteger("code") != 10000) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return thresholdJo.getJSONObject("data");
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取远程数据
|
|
|
+ private String getDeviceFlowFromRemoteURL(String userId, String groupId) {
|
|
|
+ //请求头
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("userId", userId);
|
|
|
+ //Body参数
|
|
|
+ MultiValueMap<String, Object> paramsMap = new LinkedMultiValueMap<String, Object>();
|
|
|
+ HttpEntity<MultiValueMap> objectHttpEntity = new HttpEntity<MultiValueMap>(paramsMap, headers);
|
|
|
+ System.out.println("URL_ENVIRON_DEVICE_SERVICE=" + URL_ENVIRON_DEVICE_SERVICE);
|
|
|
+ ResponseEntity<String> result = restTemplate.exchange(URL_ENVIRON_DEVICE_SERVICE + "?groupId=" + groupId, HttpMethod.GET, objectHttpEntity, String.class);
|
|
|
+ return result.getBody();
|
|
|
+ }
|
|
|
|
|
|
+ // ################################# 以下部分是可以优化提炼的代码 #################################
|
|
|
|
|
|
// List headerList = new ArrayList();
|
|
|
// List getParamsList = new ArrayList();
|
|
@@ -103,36 +162,19 @@ public class ScheduleTask {
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
-// if(result)
|
|
|
- }
|
|
|
-
|
|
|
- private String getBodyFromRemoteURL(String userId, String groupId) {
|
|
|
- //请求头
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.add("userId", userId);
|
|
|
- //Body参数
|
|
|
- MultiValueMap<String, Object> paramsMap = new LinkedMultiValueMap<String, Object>();
|
|
|
- //封装请求头
|
|
|
-// HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<Map<String,Object>>(paramsMap, headers);
|
|
|
- HttpEntity<MultiValueMap> objectHttpEntity = new HttpEntity<MultiValueMap>(paramsMap, headers);
|
|
|
- ResponseEntity<String> result = restTemplate.exchange(URL + "?groupId=" + groupId, HttpMethod.GET, objectHttpEntity, String.class);
|
|
|
- return result.getBody();
|
|
|
- }
|
|
|
-
|
|
|
// String remoteData = getBodyFromRemoteURL(headerList, getParamsList);
|
|
|
-
|
|
|
//做成通用远程接口
|
|
|
- private String getBodyFromRemoteURL(List headerList, List getParamsList) {
|
|
|
- //请求头
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.add("userId", "-1618620464");
|
|
|
- //Body参数
|
|
|
- MultiValueMap<String, Object> paramsMap = new LinkedMultiValueMap<String, Object>();
|
|
|
-// paramsMap.add("groupId", "24234390");
|
|
|
- //封装请求头
|
|
|
-// HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<Map<String,Object>>(paramsMap, headers);
|
|
|
- HttpEntity<MultiValueMap> objectHttpEntity = new HttpEntity<MultiValueMap>(paramsMap, headers);
|
|
|
- ResponseEntity<String> result = restTemplate.exchange(URL + "?groupId=24234390", HttpMethod.GET, objectHttpEntity, String.class);
|
|
|
- return result.getBody();
|
|
|
- }
|
|
|
+// private String getBodyFromRemoteURL(List headerList, List getParamsList) {
|
|
|
+// //请求头
|
|
|
+// HttpHeaders headers = new HttpHeaders();
|
|
|
+// headers.add("userId", "-1618620464");
|
|
|
+// //Body参数
|
|
|
+// MultiValueMap<String, Object> paramsMap = new LinkedMultiValueMap<String, Object>();
|
|
|
+//// paramsMap.add("groupId", "24234390");
|
|
|
+// //封装请求头
|
|
|
+//// HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<Map<String,Object>>(paramsMap, headers);
|
|
|
+// HttpEntity<MultiValueMap> objectHttpEntity = new HttpEntity<MultiValueMap>(paramsMap, headers);
|
|
|
+// ResponseEntity<String> result = restTemplate.exchange(URL + "?groupId=24234390", HttpMethod.GET, objectHttpEntity, String.class);
|
|
|
+// return result.getBody();
|
|
|
+// }
|
|
|
}
|