|
@@ -0,0 +1,138 @@
|
|
|
+package com.huimv.environ.eco.schedule;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+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.SysConfigService;
|
|
|
+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.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv-farm-datacenter
|
|
|
+ * @Package : ${}
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2022/11/9
|
|
|
+ **/
|
|
|
+@Component
|
|
|
+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";
|
|
|
+
|
|
|
+ @Scheduled(cron = "0 0/5 * * * ?") //每5分钟执行一次
|
|
|
+ public void getDeviceFlow() throws ParseException {
|
|
|
+ System.out.println("getDeviceFlow " + new Date().getTime());
|
|
|
+ //查询所有设备
|
|
|
+ List<EcoDevice> ecoDeviceList = ecoDeviceService.getDeviceList();
|
|
|
+ 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);
|
|
|
+ String cfgName = "userId";
|
|
|
+ // 查询用户userId
|
|
|
+ String userId = sysConfigService.getUserId(farmId, cfgName);
|
|
|
+ if (userId == null) {
|
|
|
+ System.out.println("userId不存在.");
|
|
|
+ } 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// List headerList = new ArrayList();
|
|
|
+// List getParamsList = new ArrayList();
|
|
|
+// List postParamsList = new ArrayList();
|
|
|
+// //获取远程数据
|
|
|
+// String remoteData = getBodyFromRemoteURL(headerList, getParamsList);
|
|
|
+//// System.out.println("result = " + result.getBody(remoteData));
|
|
|
+// JSONObject resultJo = JSONObject.parseObject(remoteData);
|
|
|
+// if (resultJo.getInteger("code") != 1000) {
|
|
|
+// System.out.println("返回出错.");
|
|
|
+// } else {
|
|
|
+// JSONArray dataJa = resultJo.getJSONArray("data");
|
|
|
+// for (int a = 0; a < dataJa.size(); a++) {
|
|
|
+// JSONObject dataJo = dataJa.getJSONObject(a);
|
|
|
+// ecoDeviceFlowService.save(dataJo);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// 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();
|
|
|
+ }
|
|
|
+}
|