WeatherController.java 11 KB

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