ScheduleTask.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package com.huimv.environ.eco.schedule;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.huimv.common.utils.Result;
  5. import com.huimv.environ.eco.entity.EcoDevice;
  6. import com.huimv.environ.eco.entity.EcoDeviceFlow;
  7. import com.huimv.environ.eco.mapper.EcoDeviceFlowMapper;
  8. import com.huimv.environ.eco.service.EcoDeviceFlowService;
  9. import com.huimv.environ.eco.service.EcoDeviceService;
  10. import com.huimv.environ.eco.service.EcoDryListService;
  11. import com.huimv.environ.eco.service.SysConfigService;
  12. import com.huimv.environ.env.utils.DateUtil;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.http.*;
  18. import org.springframework.scheduling.annotation.Scheduled;
  19. import org.springframework.stereotype.Component;
  20. import org.springframework.util.LinkedMultiValueMap;
  21. import org.springframework.util.MultiValueMap;
  22. import org.springframework.web.client.RestTemplate;
  23. import java.lang.reflect.Array;
  24. import java.text.ParseException;
  25. import java.util.*;
  26. /**
  27. * @Project : huimv-farm-datacenter
  28. * @Package : ${}
  29. * @Description : TODO
  30. * @Version : 1.0
  31. * @Author : ZhuoNing
  32. * @Create : 2022/11/9
  33. **/
  34. @Component
  35. @Slf4j
  36. public class ScheduleTask {
  37. @Autowired
  38. private RestTemplate restTemplate;
  39. @Autowired
  40. private EcoDeviceFlowService ecoDeviceFlowService;
  41. @Autowired
  42. private EcoDeviceService ecoDeviceService;
  43. @Autowired
  44. private SysConfigService sysConfigService;
  45. @Autowired
  46. private EcoDryListService ecoDryListService;
  47. @Value("${url.threshold.service}")
  48. private String URL_THRESHOLD_SERVICE;
  49. @Value("${url.environ.device.service}")
  50. private String URL_ENVIRON_DEVICE_SERVICE;
  51. // private static String URL = "http://www.0531yun.com/app/GetDeviceData";
  52. // @Scheduled(cron = "0 0/5 * * * ?") //每5分钟执行一次
  53. // @Scheduled(cron = "0 0/1 * * * ?") //每1分钟执行一次
  54. // @Scheduled(cron = "*/10 * * * * ?") //每10秒钟执行一次
  55. public void autoRunDryList() throws ParseException {
  56. DateUtil dateUtil = new DateUtil();
  57. System.out.println("开始取数:" + dateUtil.fromLongToDate(new Date().getTime(), "yyyy-MM-dd HH:mm:ss.SSS"));
  58. //查询所有设备
  59. // List<Map> ecoDeviceList = ecoDeviceService.getDeviceList2();
  60. List<Map> ecoDeviceList = ecoDeviceService.getDryDeviceByDeviceType();
  61. System.out.println("ecoDeviceList = " + ecoDeviceList);
  62. System.out.println("ecoDeviceList.size=" + ecoDeviceList.size());
  63. if (ecoDeviceList.size() == 0) {
  64. System.out.println("设备记录为空.");
  65. } else {
  66. for (int a = 0; a < ecoDeviceList.size(); a++) {
  67. System.out.println("a========================================================================================================" + a);
  68. Map deviceMap = ecoDeviceList.get(a);
  69. Integer farmId = Integer.parseInt(deviceMap.get("farm_id") + "");
  70. String dryDeviceCode = deviceMap.get("device_code") + "";
  71. System.out.println("dryDeviceCode=" + dryDeviceCode);
  72. System.out.println("id=" + deviceMap.get("id"));
  73. //读取高温阈值数据
  74. JSONObject thresholdJo = getThresholdURL(farmId);
  75. // JSONObject thresholdJo = getThresholdURL2(farmId);
  76. System.out.println("调取牧场[" + farmId + "]服务获取阈值接口:" + thresholdJo);
  77. if (thresholdJo == null) {
  78. log.error("thresholdJo======" + thresholdJo);
  79. log.error("牧场[" + farmId + "]暂时没有洗消烘干温度阈值设置.");
  80. continue;
  81. }
  82. Date todayTime = dateUtil.getTodayDateTime();
  83. //读取最新一条烘干记录
  84. System.out.println("最近一条烘干记录 dryDeviceCode=" + dryDeviceCode);
  85. System.out.println("最近一条烘干记录 todayTime=" + todayTime);
  86. String datetime = dateUtil.formatDateTime(todayTime);
  87. System.out.println("最近一条烘干记录 datetime=" + datetime);
  88. EcoDeviceFlow ecoDeviceFlow = ecoDeviceFlowService.getLastDryDeviceFlow(dryDeviceCode, datetime);
  89. if (ecoDeviceFlow == null) {
  90. log.error("牧场[" + farmId + "]暂时没有取到洗消烘干温度任何数据.");
  91. continue;
  92. }
  93. //新建烘干记录
  94. String currentTemp = ecoDeviceFlow.getId1Value();
  95. System.out.println("当前的烘干温度currentTemp=" + currentTemp);
  96. ecoDryListService.saveDryList(deviceMap, currentTemp, thresholdJo, todayTime);
  97. }
  98. }
  99. System.out.println("完成取数:" + dateUtil.fromLongToDate(new Date().getTime(), "yyyy-MM-dd HH:mm:ss.SSS"));
  100. }
  101. //以下是旧的代码
  102. // @Scheduled(cron = "0 0/5 * * * ?") //每5分钟执行一次
  103. // @Scheduled(cron = "*/5 * * * * ?") //每10秒钟执行一次
  104. // public void getDeviceFlow() throws ParseException {
  105. // DateUtil dateUtil = new DateUtil();
  106. // System.out.println("开始取数:" + dateUtil.fromLongToDate(new Date().getTime(), "yyyy-MM-dd HH:mm:ss.SSS"));
  107. // //查询所有设备
  108. // List<Map> ecoDeviceList = ecoDeviceService.getDeviceList2();
  109. // System.out.println("ecoDeviceList.size=" + ecoDeviceList.size());
  110. // if (ecoDeviceList.size() == 0) {
  111. // System.out.println("设备记录为空.");
  112. // } else {
  113. // for (int a = 0; a < ecoDeviceList.size(); a++) {
  114. // System.out.println("a========================================================================================================" + a);
  115. // Map deviceMap = ecoDeviceList.get(a);
  116. // Integer farmId = Integer.parseInt(deviceMap.get("farm_id") + "");
  117. // String deviceGroupCode = deviceMap.get("device_code") + "";
  118. // System.out.println("id=" + deviceMap.get("id"));
  119. // //读取高温阈值数据
  120. // JSONObject thresholdJo = getThresholdURL(farmId);
  121. // System.out.println("调取牧场[" + farmId + "]服务获取阈值接口:" + thresholdJo);
  122. // if (thresholdJo == null) {
  123. // log.error("thresholdJo======" + thresholdJo);
  124. // log.error("牧场[" + farmId + "]暂时没有洗消烘干温度设置.");
  125. // continue;
  126. // }
  127. // String cfgName = "userId";
  128. // // 查询用户userId
  129. // String userId = sysConfigService.getUserId(farmId, cfgName);
  130. // if (userId == null) {
  131. // System.out.println("牧场[" + farmId + "]的userId不存在.");
  132. // continue;
  133. // }
  134. // //调用远程设备上传流水接口,查询远程数据
  135. // JSONObject resultJo = JSONObject.parseObject(getDeviceFlowFromRemoteURL(userId, deviceGroupCode));
  136. // if (resultJo.getInteger("code") != 1000) {
  137. // System.out.println("牧场[" + farmId + "]设备组编号(" + deviceGroupCode + ")调用远程接口返回结果出错.");
  138. // } else {
  139. // JSONArray dataJa = resultJo.getJSONArray("data");
  140. // System.out.println("dataJa.size=" + dataJa.size());
  141. // JSONObject dataJo = dataJa.getJSONObject(0);
  142. // Date todayTime = dateUtil.getTodayDateTime();
  143. // //保存温度流水记录
  144. // ecoDeviceFlowService.save(dataJo, todayTime);
  145. // System.out.println("22 dataJo=" + dataJo);
  146. // System.out.println("33 thresholdJo=" + thresholdJo);
  147. // //新建烘干记录
  148. // ecoDryListService.saveDryList(deviceMap, dataJo, thresholdJo, todayTime);
  149. // //新建洗消烘干温度时长不足报警记录
  150. // //dryMin = thresholdJo.getString("dryMin");
  151. //// newShortDryingtime();
  152. // }
  153. // }
  154. // }
  155. // System.out.println("完成取数:" + dateUtil.fromLongToDate(new Date().getTime(), "yyyy-MM-dd HH:mm:ss.SSS"));
  156. // }
  157. //获取阈值
  158. private JSONObject getThresholdURL(Integer farmId) {
  159. HttpHeaders httpHeaders = new HttpHeaders();
  160. MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
  161. httpHeaders.setContentType(type);
  162. HashMap<String, Object> map = new HashMap<>();
  163. map.put("farmId", farmId);
  164. HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map, httpHeaders);
  165. //读取真实数据
  166. // String remoteUrl = "http://192.168.1.67:8010/produce/threshold/getThresholdByFarmId";
  167. System.out.println("URL_THRESHOLD_SERVICE=" + URL_THRESHOLD_SERVICE);
  168. ResponseEntity<String> entity = restTemplate.postForEntity(URL_THRESHOLD_SERVICE, objectHttpEntity, String.class);
  169. JSONObject thresholdJo = JSONObject.parseObject(entity.getBody());
  170. if (thresholdJo.getInteger("code") != 10000) {
  171. return null;
  172. }
  173. return thresholdJo.getJSONObject("data");
  174. }
  175. /**
  176. * @Method : getThresholdURL2
  177. * @Description :
  178. * @Params : [farmId]
  179. * @Return : com.alibaba.fastjson.JSONObject
  180. * @Author : ZhuoNing
  181. * @Date : 2022/12/6
  182. * @Time : 20:12
  183. */
  184. private JSONObject getThresholdURL2(Integer farmId) {
  185. // 设置请求头,请求类型为json
  186. HttpHeaders headers = new HttpHeaders();
  187. headers.setContentType(MediaType.APPLICATION_JSON);
  188. // 设置请求参数
  189. HashMap<String, Object> map = new HashMap<>();
  190. map.put("farmId", farmId);
  191. //用HttpEntity封装整个请求报文
  192. HttpEntity<HashMap<String, Object>> request = new HttpEntity<>(map, headers);
  193. //读取真实数据
  194. // String remoteUrl = "http://192.168.1.67:8010/produce/threshold/getThresholdByFarmId";
  195. String url = "http://localhost:10052/threshold/getThresholdByFarmId";
  196. System.out.println("url=" + url);
  197. JSONObject resultJo = restTemplate.postForObject(url, request, JSONObject.class);
  198. System.out.println("resultJo=" + resultJo);
  199. if (resultJo.getInteger("code") != 10000) {
  200. return null;
  201. }
  202. return resultJo.getJSONObject("data");
  203. }
  204. //获取远程数据
  205. private String getDeviceFlowFromRemoteURL(String userId, String groupId) {
  206. //请求头
  207. HttpHeaders headers = new HttpHeaders();
  208. headers.add("userId", userId);
  209. //Body参数
  210. MultiValueMap<String, Object> paramsMap = new LinkedMultiValueMap<String, Object>();
  211. HttpEntity<MultiValueMap> objectHttpEntity = new HttpEntity<MultiValueMap>(paramsMap, headers);
  212. System.out.println("URL_ENVIRON_DEVICE_SERVICE=" + URL_ENVIRON_DEVICE_SERVICE);
  213. ResponseEntity<String> result = restTemplate.exchange(URL_ENVIRON_DEVICE_SERVICE + "?groupId=" + groupId, HttpMethod.GET, objectHttpEntity, String.class);
  214. return result.getBody();
  215. }
  216. // ################################# 以下部分是可以优化提炼的代码 #################################
  217. // List headerList = new ArrayList();
  218. // List getParamsList = new ArrayList();
  219. // List postParamsList = new ArrayList();
  220. // //获取远程数据
  221. // String remoteData = getBodyFromRemoteURL(headerList, getParamsList);
  222. //// System.out.println("result = " + result.getBody(remoteData));
  223. // JSONObject resultJo = JSONObject.parseObject(remoteData);
  224. // if (resultJo.getInteger("code") != 1000) {
  225. // System.out.println("返回出错.");
  226. // } else {
  227. // JSONArray dataJa = resultJo.getJSONArray("data");
  228. // for (int a = 0; a < dataJa.size(); a++) {
  229. // JSONObject dataJo = dataJa.getJSONObject(a);
  230. // ecoDeviceFlowService.save(dataJo);
  231. // }
  232. // }
  233. // String remoteData = getBodyFromRemoteURL(headerList, getParamsList);
  234. //做成通用远程接口
  235. // private String getBodyFromRemoteURL(List headerList, List getParamsList) {
  236. // //请求头
  237. // HttpHeaders headers = new HttpHeaders();
  238. // headers.add("userId", "-1618620464");
  239. // //Body参数
  240. // MultiValueMap<String, Object> paramsMap = new LinkedMultiValueMap<String, Object>();
  241. //// paramsMap.add("groupId", "24234390");
  242. // //封装请求头
  243. //// HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<Map<String,Object>>(paramsMap, headers);
  244. // HttpEntity<MultiValueMap> objectHttpEntity = new HttpEntity<MultiValueMap>(paramsMap, headers);
  245. // ResponseEntity<String> result = restTemplate.exchange(URL + "?groupId=24234390", HttpMethod.GET, objectHttpEntity, String.class);
  246. // return result.getBody();
  247. // }
  248. }