WeatherController.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.huimv.produce.controller;
  2. import cn.hutool.http.HttpUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.baomidou.mybatisplus.extension.api.R;
  6. import com.dahuatech.icc.exception.ClientException;
  7. import com.huimv.common.utils.AirQualityIndexUtil;
  8. import com.huimv.common.utils.Result;
  9. import com.huimv.common.utils.ResultCode;
  10. import com.huimv.produce.entity.Farm;
  11. import com.huimv.produce.entity.WeatherResultEntity;
  12. import com.huimv.produce.mapper.FarmMapper;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.web.bind.annotation.RequestBody;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestParam;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import net.sf.json.JSONObject;
  19. import java.nio.charset.Charset;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.Map;
  24. @RestController
  25. @RequestMapping("/weather")
  26. public class WeatherController {
  27. @Autowired
  28. private FarmMapper farmMapper;
  29. @RequestMapping("/info")
  30. public Result getAccidentRecord(@RequestParam Map<String, Object> params) throws ClientException {
  31. String farmId = (String) params.get("farmId");
  32. LambdaQueryWrapper<Farm> wrapper = Wrappers.lambdaQuery();
  33. wrapper.eq(Farm::getId, farmId);//未删除的阈值
  34. Farm farm = farmMapper.selectOne(wrapper);
  35. String[] location = farm.getFrontLocation().split(",");
  36. String province = location[0];
  37. String city = location[1];
  38. String county = location[2];
  39. // log.info("当前城市为=======>" +province+city+county);
  40. //直接通过它计算出24小时之内的温度同时以一个新的数组存入表中,数据的取出和比较值后面进行
  41. String url = "https://wis.qq.com/weather/common";
  42. String param = "source=xw&weather_type=observe|forecast_1h|forecast_24h|index|alarm|limit|tips&province=" + province + "&city=" + city + "&county=" + county;
  43. String result = HttpUtil.urlWithForm(url, param, Charset.defaultCharset(), true);
  44. // System.out.println(result);
  45. String s = HttpUtil.get(result);
  46. //对S进行解析,首先获取当天24小时的温度情况
  47. JSONObject jsonObject = JSONObject.fromObject(s);
  48. Object data = jsonObject.get("data");
  49. JSONObject jsonData = JSONObject.fromObject(data);
  50. // System.out.println(jsonData);
  51. Object forecastobject24hDetail = JSONObject.fromObject(jsonData.get("forecast_24h")).get("1"); //24小时天气预报的 “1” 为当天的天气预报
  52. Object forecastobject01hDetail = JSONObject.fromObject(jsonData.get("forecast_1h")).get("0"); //24小时天气预报的 “1” 为当天的天气预报
  53. Object alarm = JSONObject.fromObject(jsonData).get("alarm");//报警信息
  54. Object degree = JSONObject.fromObject(forecastobject01hDetail).get("degree");//当前温度
  55. Object maxDegree = JSONObject.fromObject(forecastobject24hDetail).get("max_degree");//当天最高温
  56. Object minDegree = JSONObject.fromObject(forecastobject24hDetail).get("min_degree");//当天最低温
  57. Object day_weather = JSONObject.fromObject(forecastobject24hDetail).get("day_weather");//百天天气
  58. Object day_wind_direction = JSONObject.fromObject(forecastobject24hDetail).get("day_wind_direction");//西风
  59. Object day_wind_power = JSONObject.fromObject(forecastobject24hDetail).get("day_wind_power");//风力
  60. Object night_weather = JSONObject.fromObject(forecastobject24hDetail).get("night_weather");//夜间天气
  61. Object night_wind_direction = JSONObject.fromObject(forecastobject24hDetail).get("night_wind_direction");//夜间风向
  62. Object night_wind_power = JSONObject.fromObject(forecastobject24hDetail).get("night_wind_power");//夜间风力
  63. Object humidityValu = JSONObject.fromObject(jsonData.get("observe")).get("humidity");//湿度
  64. String airQualityIndex = AirQualityIndexUtil.queryWeather(city.substring(0, city.length() - 1));
  65. // log.info("空气质量为====>" + airQualityIndex);
  66. JSONObject forecastByhour = JSONObject.fromObject(jsonData.get("forecast_1h")); //获得逐小时数据并遍历
  67. List<String> TimeList = new ArrayList<>();
  68. List<String> TempList = new ArrayList<>();
  69. for (int i = 0; i <24; i++) {
  70. //现在需要将逐小时的气温数据存在表里面 //初步实现,裁剪
  71. JSONObject HoureDate = JSONObject.fromObject(forecastByhour.get(String.valueOf(i)));
  72. String timeString = (String)HoureDate.get("update_time");
  73. //时间为这种格式,进行格式化处理 20210714180000
  74. StringBuffer stringDate=new StringBuffer(timeString).insert(4,"-").insert(7,"-").insert(10," ").insert(13,":").insert(16,":");
  75. //存储第二天逐小时天气预报以及信息
  76. TimeList.add(stringDate.toString());
  77. TempList.add(HoureDate.get("degree").toString());
  78. }
  79. WeatherResultEntity weatherResultEntity = new WeatherResultEntity();
  80. weatherResultEntity.setAlarm( alarm.toString());
  81. weatherResultEntity.setDegree((String) degree);
  82. weatherResultEntity.setMax_degree((String) maxDegree);
  83. weatherResultEntity.setMin_degree((String) minDegree);
  84. weatherResultEntity.setDay_weather((String) day_weather);
  85. weatherResultEntity.setDay_wind_direction((String) day_wind_direction);
  86. weatherResultEntity.setDay_wind_power((String) day_wind_power);
  87. weatherResultEntity.setNight_weather((String) night_weather);
  88. weatherResultEntity.setNight_wind_direction((String)night_wind_direction);
  89. weatherResultEntity.setNight_wind_power((String) night_wind_power);
  90. weatherResultEntity.setHumidity((String) humidityValu);
  91. weatherResultEntity.setAirQuility( airQualityIndex);
  92. weatherResultEntity.setSendTime(new Date());
  93. weatherResultEntity.setTimeList(TimeList);
  94. weatherResultEntity.setTempList(TempList);
  95. return new Result(ResultCode.SUCCESS, weatherResultEntity);
  96. }
  97. /*public static void main(String[] args) {
  98. // 浙江省,杭州市,余杭区,咯咯3
  99. String province = "浙江省";
  100. String city = "杭州市";
  101. String county = "余杭区";
  102. Integer farmid =2;
  103. // log.info("当前城市为=======>" +province+city+county);
  104. //直接通过它计算出24小时之内的温度同时以一个新的数组存入表中,数据的取出和比较值后面进行
  105. String url = "https://wis.qq.com/weather/common";
  106. String param = "source=xw&weather_type=observe|forecast_1h|forecast_24h|index|alarm|limit|tips&province=" + province + "&city=" + city + "&county=" + county;
  107. String result = HttpUtil.urlWithForm(url, param, Charset.defaultCharset(), true);
  108. System.out.println(result);
  109. String s = HttpUtil.get(result);
  110. //对S进行解析,首先获取当天24小时的温度情况
  111. JSONObject jsonObject = JSONObject.fromObject(s);
  112. Object data = jsonObject.get("data");
  113. JSONObject jsonData = JSONObject.fromObject(data);
  114. System.out.println(jsonData);
  115. Object forecastobject24hDetail = JSONObject.fromObject(jsonData.get("forecast_24h")).get("1"); //24小时天气预报的 “1” 为当天的天气预报
  116. Object forecastobject01hDetail = JSONObject.fromObject(jsonData.get("forecast_1h")).get("0"); //24小时天气预报的 “1” 为当天的天气预报
  117. Object alarm = JSONObject.fromObject(jsonData).get("alarm");//报警信息
  118. Object degree = JSONObject.fromObject(forecastobject01hDetail).get("degree");//当前温度
  119. Object maxDegree = JSONObject.fromObject(forecastobject24hDetail).get("max_degree");//当天最高温
  120. Object minDegree = JSONObject.fromObject(forecastobject24hDetail).get("min_degree");//当天最低温
  121. Object day_weather = JSONObject.fromObject(forecastobject24hDetail).get("day_weather");//百天天气
  122. Object day_wind_direction = JSONObject.fromObject(forecastobject24hDetail).get("day_wind_direction");//西风
  123. Object day_wind_power = JSONObject.fromObject(forecastobject24hDetail).get("day_wind_power");//风力
  124. Object night_weather = JSONObject.fromObject(forecastobject24hDetail).get("night_weather");//夜间天气
  125. Object night_wind_direction = JSONObject.fromObject(forecastobject24hDetail).get("night_wind_direction");//夜间风向
  126. Object night_wind_power = JSONObject.fromObject(forecastobject24hDetail).get("night_wind_power");//夜间风力
  127. Object humidityValu = JSONObject.fromObject(jsonData.get("observe")).get("humidity");//湿度
  128. System.out.println("canshu "+alarm);
  129. System.out.println("canshu "+degree);
  130. System.out.println("canshu "+maxDegree);
  131. System.out.println("canshu "+minDegree);
  132. System.out.println("canshu "+day_weather);
  133. System.out.println("canshu "+day_wind_direction);
  134. System.out.println("canshu "+day_wind_power);
  135. System.out.println("canshu "+night_weather);
  136. System.out.println("canshu "+night_wind_direction);
  137. System.out.println("canshu "+night_wind_power);
  138. System.out.println("canshu "+humidityValu);
  139. }*/
  140. }