|
@@ -0,0 +1,181 @@
|
|
|
+package com.huimv.admin.timer;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.codec.Base64;
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.huimv.admin.common.utils.HttpClientSSLUtils;
|
|
|
+import com.huimv.admin.common.utils.NumberUtils;
|
|
|
+import com.huimv.admin.common.utils.Result;
|
|
|
+import com.huimv.admin.entity.*;
|
|
|
+import com.huimv.admin.entity.zengxindto.*;
|
|
|
+import com.huimv.admin.mapper.FeedAreaMapper;
|
|
|
+import com.huimv.admin.mapper.FeedingDataMapper;
|
|
|
+import com.huimv.admin.mapper.FeedingEnvMapper;
|
|
|
+import com.huimv.admin.service.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+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.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.text.NumberFormat;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@EnableScheduling
|
|
|
+public class FeedTimer {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FeedAreaMapper feedAreaMapper;
|
|
|
+ @Autowired
|
|
|
+ private FeedingEnvMapper envMapper;
|
|
|
+ @Autowired
|
|
|
+ private FeedingDataMapper dataMapper;
|
|
|
+
|
|
|
+
|
|
|
+ @Scheduled(cron = "0 0/11 * * * ? ")
|
|
|
+ @Transactional
|
|
|
+ public void getFeed() throws Exception {
|
|
|
+ QueryWrapper<FeedArea> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("farm_id", 26);
|
|
|
+ List<FeedArea> feedAreas = feedAreaMapper.selectList(queryWrapper);
|
|
|
+ Date now = new Date(); // 获取当前时间
|
|
|
+ long oneHour = 1 * 30 * 1000; // 1小时的毫秒数
|
|
|
+ Date oneHourAgo = new Date(now.getTime() - oneHour); // 获取1小时前的时间
|
|
|
+ List<String> time = new ArrayList<>();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ time.add(sdf.format(oneHourAgo));
|
|
|
+ time.add(sdf.format(new Date()));
|
|
|
+ for (FeedArea feedArea : feedAreas) {
|
|
|
+ HashMap<String, String> paramMap = new HashMap<>();
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("pageNum", "1");
|
|
|
+ jsonObject.put("pageSize", "20");
|
|
|
+ jsonObject.put("createTime", time);
|
|
|
+ jsonObject.put("fdQrcode", "");
|
|
|
+ jsonObject.put("fenceNum", "");
|
|
|
+ jsonObject.put("gatewayNo", "");
|
|
|
+ jsonObject.put("areaId", feedArea.getAreaId());
|
|
|
+ paramMap.put("Content-Type", "application/json;charset=utf-8");
|
|
|
+ paramMap.put("AuthorizationF", getToken());
|
|
|
+ String post = HttpRequest.post("http://xxxxxxxx:xx /feed/api/mqtt/pageQryRealtime").headerMap(paramMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
|
|
|
+ JSONObject jsonObject1 = JSONObject.parseObject(post);
|
|
|
+ Map map = (Map) jsonObject1.get("data");
|
|
|
+ List<Map<String, String>> list = (List) map.get("records");
|
|
|
+ for (Map<String, String> stringMap : list) {
|
|
|
+ FeedingData data = new FeedingData();
|
|
|
+ data.setFarmId(26);
|
|
|
+ data.setUnitName(feedArea.getUnitName());
|
|
|
+ QueryWrapper<FeedingEnv> queryWrapper1 = new QueryWrapper<>();
|
|
|
+ queryWrapper1.eq("device_code", stringMap.get("fdQrcode"));
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
+ data.setUploadTime(dateFormat.parse(stringMap.get("createTime")));
|
|
|
+ data.setBlanking(stringMap.get("zkAlram"));
|
|
|
+ data.setFoodIntake(stringMap.get("intFdWeight"));
|
|
|
+ data.setMeals(ThreadLocalRandom.current().nextInt());
|
|
|
+ dataMapper.insert(data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getToken() {
|
|
|
+ HashMap<String, String> paramMap = new HashMap<>();
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("username", "henlu");
|
|
|
+ jsonObject.put("password", "123456");
|
|
|
+ paramMap.put("Content-Type", "application/json;charset=utf-8");
|
|
|
+ String post = HttpRequest.post("http://znsw.zxkj2006.com/feed/api/admin/appLogin").headerMap(paramMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
|
|
|
+ System.out.println(post);
|
|
|
+ JSONObject jsonObject1 = JSONObject.parseObject(post);
|
|
|
+ System.out.println(jsonObject1);
|
|
|
+ Map map = (Map) jsonObject1.get("data");
|
|
|
+ String s = map.get("tokenHead") + " " + map.get("token");
|
|
|
+ System.out.println(s);
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ Date now = new Date(); // 获取当前时间
|
|
|
+ long oneHour = 1 * 30 * 1000; // 1小时的毫秒数
|
|
|
+ Date oneHourAgo = new Date(now.getTime() - oneHour); // 获取1小时前的时间
|
|
|
+ List<String> time = new ArrayList<>();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ time.add(sdf.format(oneHourAgo));
|
|
|
+ time.add(sdf.format(new Date()));
|
|
|
+ HashMap<String, String> paramMap = new HashMap<>();
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("pageNum", 1);
|
|
|
+ jsonObject.put("pageSize", 20);
|
|
|
+ jsonObject.put("createTime", time);
|
|
|
+ jsonObject.put("areaId","1017517");
|
|
|
+ paramMap.put("Content-Type", "application/json;charset=utf-8");
|
|
|
+ paramMap.put("AuthorizationF","Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJoZW5sdSIsImNyZWF0ZWQiOjE2OTUzNjg1OTE4MjEsImV4cCI6MTY5NTQ1NDk5MX0.63A5FNJD714MVLp2cdH9HLyN25lYtAzsXHysXLEp1eR-FfkQZel6FnuoLfkIoFbgaht1OsEN-2ERabw0Lby7Pg");
|
|
|
+ String post = HttpRequest.post("http://znsw.zxkj2006.com/feed/api/mqtt/pageQryRealtime").headerMap(paramMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
|
|
|
+ JSONObject jsonObject1 = JSONObject.parseObject(post);
|
|
|
+ Map map = (Map) jsonObject1.get("data");
|
|
|
+ System.out.println(map);
|
|
|
+ List<Map<String, String>> list = (List) map.get("records");
|
|
|
+ System.out.println(list);
|
|
|
+// List<String> jsonArray = (ArrayList) map.get("records");
|
|
|
+// System.out.println(jsonArray);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //饲养器添加
|
|
|
+ @Scheduled(cron = "0 0/1 * * * ? ")
|
|
|
+ @Transactional
|
|
|
+ public void getFeedEnv() throws Exception {
|
|
|
+ QueryWrapper<FeedArea> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("farm_id", 26);
|
|
|
+ List<FeedArea> feedAreas = feedAreaMapper.selectList(queryWrapper);
|
|
|
+ for (FeedArea feedArea : feedAreas) {
|
|
|
+ HashMap<String, String> paramMap = new HashMap<>();
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("pageNum", "1");
|
|
|
+ jsonObject.put("pageSize", "20");
|
|
|
+ jsonObject.put("areaId", feedArea.getAreaId());
|
|
|
+ paramMap.put("Content-Type", "application/json;charset=utf-8");
|
|
|
+ paramMap.put("AuthorizationF", getToken());
|
|
|
+ String post = HttpRequest.post("http://znsw.zxkj2006.com/feed/api/api/v1/feeder/pageQry").headerMap(paramMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
|
|
|
+ JSONObject jsonObject1 = JSONObject.parseObject(post);
|
|
|
+ Map map = (Map) jsonObject1.get("data");
|
|
|
+ List<Map<String, String>> list = (List) map.get("records");
|
|
|
+ for (Map<String, String> stringMap : list) {
|
|
|
+ QueryWrapper<FeedingEnv> queryWrapper1 = new QueryWrapper<>();
|
|
|
+ queryWrapper1.eq("farm_id", 26).eq("eartag", stringMap.get("earTag"));
|
|
|
+ if (ObjectUtil.isEmpty(envMapper.selectOne(queryWrapper1))) {
|
|
|
+ FeedingEnv feedingEnv = new FeedingEnv();
|
|
|
+ feedingEnv.setFarmId(26);
|
|
|
+ feedingEnv.setEartag(stringMap.get("earTag"));
|
|
|
+ feedingEnv.setDeviceCode(stringMap.get("qrcode"));
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
+ feedingEnv.setLastTime(dateFormat.parse(stringMap.get("createTime")));
|
|
|
+ feedingEnv.setDeviceType(Integer.valueOf(stringMap.get("feedStatus")));
|
|
|
+ envMapper.insert(feedingEnv);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|