EnvTimer.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package com.huimv.admin.timer;
  2. import cn.hutool.core.codec.Base64;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import cn.hutool.json.JSONUtil;
  7. import com.alibaba.fastjson.JSON;
  8. import com.alibaba.fastjson.JSONObject;
  9. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  10. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  11. import com.huimv.admin.common.utils.HttpClientSSLUtils;
  12. import com.huimv.admin.common.utils.Result;
  13. import com.huimv.admin.entity.*;
  14. import com.huimv.admin.entity.zengxindto.LoginOnlyDto;
  15. import com.huimv.admin.entity.zengxindto.LonginOnlyDtoToken;
  16. import com.huimv.admin.entity.zengxindto.ShackDatasDto;
  17. import com.huimv.admin.entity.zengxindto.ShackDatasSensor;
  18. import com.huimv.admin.service.*;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.context.annotation.Configuration;
  21. import org.springframework.http.HttpEntity;
  22. import org.springframework.http.HttpHeaders;
  23. import org.springframework.http.HttpMethod;
  24. import org.springframework.http.ResponseEntity;
  25. import org.springframework.scheduling.annotation.EnableScheduling;
  26. import org.springframework.scheduling.annotation.Scheduled;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import org.springframework.web.client.RestTemplate;
  29. import javax.servlet.http.HttpServletRequest;
  30. import java.text.NumberFormat;
  31. import java.util.Date;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. @Configuration
  36. @EnableScheduling
  37. public class EnvTimer {
  38. @Autowired
  39. private RestTemplate restTemplate;
  40. @Autowired
  41. private IEnvDeviceService envDeviceService;
  42. @Autowired
  43. private IEnvDeviceOnlineService envDeviceOnlineService;
  44. @Autowired
  45. private IFarmService farmService;
  46. @Autowired
  47. private IEnvDataService envDataService;
  48. @Autowired
  49. private IEnvWarningThresholdService envWarningThresholdService;
  50. @Autowired
  51. private IEnvWarningInfoService envWarningInfoService;
  52. //环控数据
  53. String username = "江西增鑫";
  54. String password = "888888";
  55. String passwordMD5 = "21218cca77804d2ba1922c33e0151105";
  56. String baseurl ="https://yzwlw.loongk.com/";
  57. @Scheduled(cron = "0 0/11 * * * ? ")
  58. @Transactional
  59. private void getHuanKong() throws Exception {
  60. //目前是这家
  61. Integer farmId = 21;
  62. String encode = loginOnly();
  63. System.out.println("encode"+encode);
  64. //获取阈值
  65. EnvWarningThreshold envWarningThreshold = envWarningThresholdService.getOne(new QueryWrapper<EnvWarningThreshold>().eq("farm_id", farmId));
  66. HttpHeaders headers = new HttpHeaders();
  67. headers.add("Authorization",encode);
  68. HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
  69. //TODO 只有一家,先拿全部
  70. List<EnvDevice> list = envDeviceService.list();
  71. for (EnvDevice envDevice : list) {
  72. if (ObjectUtil.isNotEmpty(envDevice) && StringUtils.isNotBlank(envDevice.getDeviceCode())){
  73. System.out.println(envDevice.getDeviceCode());
  74. String shishiBody = "";
  75. try {
  76. ResponseEntity<String> exchange = restTemplate.exchange("https://yzwlw.loongk.com/mobile/loadShackDatas/" + envDevice.getDeviceCode(), HttpMethod.GET, requestEntity, String.class);
  77. shishiBody = exchange.getBody();
  78. }catch (Exception e){
  79. System.out.println(e);
  80. }
  81. if (StringUtils.isBlank(shishiBody)){
  82. System.out.println(new Date() +"实时数据"+ shishiBody);
  83. return;
  84. }
  85. ShackDatasDto shackDatasDto = JSONUtil.toBean(shishiBody, ShackDatasDto.class);
  86. String onLine = shackDatasDto.getData().getOnLine();
  87. if ("N".equals(onLine)){
  88. envDevice.setDeviceStatus(0);
  89. }else {
  90. envDevice.setDeviceStatus(1);
  91. }
  92. envDeviceService.updateById(envDevice);
  93. List<ShackDatasSensor> sensorDatas = shackDatasDto.getData().getSensorDatas();
  94. EnvData envData = new EnvData();
  95. for (ShackDatasSensor sensorData : sensorDatas) {
  96. if (sensorData.getId().equals(envDevice.getOhter1())){
  97. String val = sensorData.getVal();
  98. //TODO 预警
  99. saveTemWarning(val,envWarningThreshold,envDevice,farmId);
  100. envData.setEnvTemp(val);
  101. }
  102. if (sensorData.getId().equals(envDevice.getOhter2())){
  103. String val = sensorData.getVal();
  104. //TODO 预警
  105. saveHumWarning(val,envWarningThreshold,envDevice,farmId);
  106. envData.setEnvHum(val);
  107. }
  108. }
  109. envData.setCreateTime(new Date());
  110. envData.setDeviceId(envDevice.getDeviceCode());
  111. envData.setFarmId(farmId);
  112. envData.setUnitId(envDevice.getUnitId());
  113. envDataService.save(envData);
  114. }
  115. }
  116. }
  117. private void saveHumWarning(String val, EnvWarningThreshold envWarningThreshold, EnvDevice envDevice, Integer farmId) {
  118. if (StringUtils.isNotBlank(val)){
  119. String maxHum ;
  120. String minHum ;
  121. if (ObjectUtil.isEmpty(envWarningThreshold) || StringUtils.isBlank(envWarningThreshold.getMaxHum())){
  122. maxHum ="90";
  123. }else {
  124. maxHum = envWarningThreshold.getMaxHum();
  125. }
  126. if (ObjectUtil.isEmpty(envWarningThreshold) || StringUtils.isBlank(envWarningThreshold.getMinHum())){
  127. minHum ="0";
  128. }else {
  129. minHum =envWarningThreshold.getMinHum();
  130. }
  131. if (Double.parseDouble(maxHum) < Double.parseDouble(val)){
  132. EnvWarningInfo envWarningInfo = new EnvWarningInfo();
  133. envWarningInfo.setBuildLocation(envDevice.getUnitName());
  134. envWarningInfo.setDate(new Date());
  135. envWarningInfo.setDeviceId(envDevice.getDeviceCode());
  136. envWarningInfo.setFarmId(farmId);
  137. envWarningInfo.setUnitId(envDevice.getUnitId());
  138. envWarningInfo.setUnitName(envDevice.getUnitName());
  139. envWarningInfo.setUserIds(envWarningThreshold.getUserIds());
  140. envWarningInfo.setWarningContent("当前湿度为"+val+"%超过阈值,请及时检查");
  141. envWarningInfo.setWarningType(2);
  142. envWarningInfoService.save(envWarningInfo);
  143. }
  144. if (Double.parseDouble(minHum) >Double.parseDouble(val)){
  145. EnvWarningInfo envWarningInfo = new EnvWarningInfo();
  146. envWarningInfo.setBuildLocation(envDevice.getUnitName());
  147. envWarningInfo.setDate(new Date());
  148. envWarningInfo.setDeviceId(envDevice.getDeviceCode());
  149. envWarningInfo.setFarmId(farmId);
  150. envWarningInfo.setUnitId(envDevice.getUnitId());
  151. envWarningInfo.setUnitName(envDevice.getUnitName());
  152. envWarningInfo.setUserIds(envWarningThreshold.getUserIds());
  153. envWarningInfo.setWarningContent("当前湿度为"+val+"%低于阈值,请及时检查");
  154. envWarningInfo.setWarningType(2);
  155. envWarningInfoService.save(envWarningInfo);
  156. }
  157. }
  158. }
  159. private void saveTemWarning(String val, EnvWarningThreshold envWarningThreshold,EnvDevice envDevice,Integer farmId) {
  160. if (StringUtils.isNotBlank(val)){
  161. String maxTem ;
  162. String minTem ;
  163. if (ObjectUtil.isEmpty(envWarningThreshold) || StringUtils.isBlank(envWarningThreshold.getMaxTem())){
  164. maxTem ="40";
  165. }else {
  166. maxTem = envWarningThreshold.getMaxTem();
  167. }
  168. if (ObjectUtil.isEmpty(envWarningThreshold) || StringUtils.isBlank(envWarningThreshold.getMinTem())){
  169. minTem ="0";
  170. }else {
  171. minTem =envWarningThreshold.getMinTem();
  172. }
  173. System.out.println("maxTem:"+Double.parseDouble(maxTem)+" minTem:"+Double.parseDouble(minTem)+" val:"+Double.parseDouble(val));
  174. if (Double.parseDouble(maxTem) < Double.parseDouble(val)){
  175. EnvWarningInfo envWarningInfo = new EnvWarningInfo();
  176. envWarningInfo.setBuildLocation(envDevice.getUnitName());
  177. envWarningInfo.setDate(new Date());
  178. envWarningInfo.setDeviceId(envDevice.getDeviceCode());
  179. envWarningInfo.setFarmId(farmId);
  180. envWarningInfo.setUnitId(envDevice.getUnitId());
  181. envWarningInfo.setUnitName(envDevice.getUnitName());
  182. envWarningInfo.setUserIds(envWarningThreshold.getUserIds());
  183. envWarningInfo.setWarningContent("当前温度为"+val+"°超过阈值,请及时检查");
  184. envWarningInfo.setWarningType(1);
  185. envWarningInfoService.save(envWarningInfo);
  186. }
  187. if (Double.parseDouble(minTem)>Double.parseDouble(val)){
  188. EnvWarningInfo envWarningInfo = new EnvWarningInfo();
  189. envWarningInfo.setBuildLocation(envDevice.getUnitName());
  190. envWarningInfo.setDate(new Date());
  191. envWarningInfo.setDeviceId(envDevice.getDeviceCode());
  192. envWarningInfo.setFarmId(farmId);
  193. envWarningInfo.setUnitId(envDevice.getUnitId());
  194. envWarningInfo.setUnitName(envDevice.getUnitName());
  195. envWarningInfo.setUserIds(envWarningThreshold.getUserIds());
  196. envWarningInfo.setWarningContent("当前温度为"+val+"°低于阈值,请及时检查");
  197. envWarningInfo.setWarningType(1);
  198. envWarningInfoService.save(envWarningInfo);
  199. }
  200. }
  201. }
  202. private String loginOnly() throws Exception {
  203. Map<String, Object> map = new HashMap<String, Object>();
  204. String s = HttpClientSSLUtils.doPost(baseurl + "/mobile/login?username=" + username + "&password=" + passwordMD5, JSON.toJSONString(map));
  205. LoginOnlyDto loginDto = JSONUtil.toBean(s, LoginOnlyDto.class);
  206. LonginOnlyDtoToken token = loginDto.getData().getToken();
  207. return Base64.encode(token.getUserId() + "_" + token.getToken());
  208. }
  209. @Scheduled(cron = "0 0 0 * * ? ")
  210. private void update() throws Exception {
  211. Date date = new Date();
  212. DateTime beginOfMonth = DateUtil.beginOfMonth(date);
  213. List<Farm> list = farmService.list();
  214. for (Farm farm : list) {
  215. Integer farmId = farm.getId();
  216. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  217. queryWrapper.eq("farm_id", farmId);
  218. Integer count = envDeviceService.count(queryWrapper);
  219. QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
  220. queryWrapper1.eq("device_status", 1);
  221. Integer count1 = envDeviceService.count(queryWrapper1);
  222. Integer OffDeviceCount = count- count1;
  223. //创建一个数值格式化对象
  224. NumberFormat numberFormat = NumberFormat.getInstance();
  225. //设置精确到小数点后两位
  226. numberFormat.setMaximumFractionDigits(2);
  227. String onDeviceRate = numberFormat.format((float)count1 / (float) count* 100);
  228. EnvDeviceOnline envDeviceOnline = envDeviceOnlineService.getOne(new QueryWrapper<EnvDeviceOnline>().eq("farm_id", farmId).ge("creat_time", beginOfMonth));
  229. if (ObjectUtil.isEmpty(envDeviceOnline)){
  230. envDeviceOnline = new EnvDeviceOnline();
  231. envDeviceOnline.setCreatTime(date);
  232. envDeviceOnline.setDeviceOff(OffDeviceCount);
  233. envDeviceOnline.setDeviceOn(count1);
  234. envDeviceOnline.setFarmId(farmId+"");
  235. envDeviceOnline.setOnlineRate(onDeviceRate);
  236. envDeviceOnline.setNowMonth(beginOfMonth.month());
  237. envDeviceOnline.setNowYear(beginOfMonth.year());
  238. envDeviceOnlineService.save(envDeviceOnline);
  239. }else {
  240. envDeviceOnline = new EnvDeviceOnline();
  241. envDeviceOnline.setCreatTime(date);
  242. envDeviceOnline.setDeviceOff(OffDeviceCount);
  243. envDeviceOnline.setDeviceOn(count1);
  244. envDeviceOnline.setFarmId(farmId+"");
  245. envDeviceOnline.setOnlineRate(onDeviceRate);
  246. envDeviceOnline.setNowMonth(beginOfMonth.month());
  247. envDeviceOnline.setNowYear(beginOfMonth.year());
  248. envDeviceOnlineService.updateById(envDeviceOnline);
  249. }
  250. }
  251. }
  252. }