package com.huimv.environ.eco.controller; import cn.hutool.http.HttpUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.dahuatech.icc.exception.ClientException; import com.huimv.common.utils.AirQualityIndexUtil; import com.huimv.common.utils.Result; import com.huimv.common.utils.ResultCode; import com.huimv.environ.eco.entity.DayWeatherEntity; import com.huimv.environ.eco.entity.Farm; import com.huimv.environ.eco.entity.WeatherResultEntity; import com.huimv.environ.eco.mapper.FarmMapper; import net.sf.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @RestController @RequestMapping("/weather") public class WeatherController { @Autowired private FarmMapper farmMapper; @RequestMapping("/info") public Result getAccidentRecord(@RequestParam Map params) throws ClientException { String farmId = (String) params.get("farmId"); LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(Farm::getId, farmId);//未删除的阈值 Farm farm = farmMapper.selectOne(wrapper); String[] location = farm.getFrontLocation().split(","); String province = location[0]; String city = location[1]; String county = location[2]; // log.info("当前城市为=======>" +province+city+county); //直接通过它计算出24小时之内的温度同时以一个新的数组存入表中,数据的取出和比较值后面进行 String url = "https://wis.qq.com/weather/common"; String param = "source=xw&weather_type=observe|forecast_1h|forecast_24h|index|alarm|limit|tips&province=" + province + "&city=" + city + "&county=" + county; String result = HttpUtil.urlWithForm(url, param, Charset.defaultCharset(), true); // System.out.println(result); String s = HttpUtil.get(result); //对S进行解析,首先获取当天24小时的温度情况 JSONObject jsonObject = JSONObject.fromObject(s); Object data = jsonObject.get("data"); JSONObject jsonData = JSONObject.fromObject(data); // System.out.println(jsonData); Object forecastobject24hDetail = JSONObject.fromObject(jsonData.get("forecast_24h")).get("1"); //24小时天气预报的 “1” 为当天的天气预报 Object tomorrow = JSONObject.fromObject(jsonData.get("forecast_24h")).get("2"); //24小时天气预报的 “1” 为当天的天气预报 Object towTomorrow = JSONObject.fromObject(jsonData.get("forecast_24h")).get("3"); //24小时天气预报的 “1” 为当天的天气预报 DayWeatherEntity tomorrowEntity = new DayWeatherEntity(); tomorrowEntity.setMax_degree((String)JSONObject.fromObject(tomorrow).get("max_degree") ); tomorrowEntity.setMin_degree((String)JSONObject.fromObject(tomorrow).get("min_degree") ); tomorrowEntity.setDay_weather((String)JSONObject.fromObject(tomorrow).get("day_weather") ); DayWeatherEntity towTomorrowEntity = new DayWeatherEntity(); towTomorrowEntity.setMax_degree((String)JSONObject.fromObject(towTomorrow).get("max_degree") ); towTomorrowEntity.setMin_degree((String)JSONObject.fromObject(towTomorrow).get("min_degree") ); towTomorrowEntity.setDay_weather((String)JSONObject.fromObject(towTomorrow).get("day_weather") ); Object forecastobject01hDetail = JSONObject.fromObject(jsonData.get("forecast_1h")).get("0"); //24小时天气预报的 “1” 为当天的天气预报 Object alarm = JSONObject.fromObject(jsonData).get("alarm");//报警信息 Object degree = JSONObject.fromObject(forecastobject01hDetail).get("degree");//当前温度 Object maxDegree = JSONObject.fromObject(forecastobject24hDetail).get("max_degree");//当天最高温 Object minDegree = JSONObject.fromObject(forecastobject24hDetail).get("min_degree");//当天最 低温 Object day_weather = JSONObject.fromObject(forecastobject24hDetail).get("day_weather");//百天天气 Object day_wind_direction = JSONObject.fromObject(forecastobject24hDetail).get("day_wind_direction");//西风 Object day_wind_power = JSONObject.fromObject(forecastobject24hDetail).get("day_wind_power");//风力 Object night_weather = JSONObject.fromObject(forecastobject24hDetail).get("night_weather");//夜间天气 Object night_wind_direction = JSONObject.fromObject(forecastobject24hDetail).get("night_wind_direction");//夜间风向 Object night_wind_power = JSONObject.fromObject(forecastobject24hDetail).get("night_wind_power");//夜间风力 Object humidityValu = JSONObject.fromObject(jsonData.get("observe")).get("humidity");//湿度 String airQualityIndex = AirQualityIndexUtil.queryWeather(city.substring(0, city.length() - 1)); // log.info("空气质量为====>" + airQualityIndex); JSONObject forecastByhour = JSONObject.fromObject(jsonData.get("forecast_1h")); //获得逐小时数据并遍历 List TimeList = new ArrayList<>(); List TempList = new ArrayList<>(); for (int i = 0; i <24; i++) { //现在需要将逐小时的气温数据存在表里面 //初步实现,裁剪 JSONObject HoureDate = JSONObject.fromObject(forecastByhour.get(String.valueOf(i))); String timeString = (String)HoureDate.get("update_time"); //时间为这种格式,进行格式化处理 20210714180000 StringBuffer stringDate=new StringBuffer(timeString).insert(4,"-").insert(7,"-").insert(10," ").insert(13,":").insert(16,":"); //存储第二天逐小时天气预报以及信息 TimeList.add(stringDate.toString()); TempList.add(HoureDate.get("degree").toString()); } WeatherResultEntity weatherResultEntity = new WeatherResultEntity(); weatherResultEntity.setAlarm( alarm.toString()); weatherResultEntity.setDegree((String) degree); weatherResultEntity.setMax_degree((String) maxDegree); weatherResultEntity.setMin_degree((String) minDegree); weatherResultEntity.setDay_weather((String) day_weather); weatherResultEntity.setDay_wind_direction((String) day_wind_direction); weatherResultEntity.setDay_wind_power((String) day_wind_power); weatherResultEntity.setNight_weather((String) night_weather); weatherResultEntity.setNight_wind_direction((String)night_wind_direction); weatherResultEntity.setNight_wind_power((String) night_wind_power); weatherResultEntity.setHumidity((String) humidityValu); weatherResultEntity.setAirQuility( airQualityIndex); weatherResultEntity.setSendTime(new Date()); weatherResultEntity.setTimeList(TimeList); weatherResultEntity.setTempList(TempList); weatherResultEntity.setTomorrow(tomorrowEntity);//明天天气 weatherResultEntity.setTowTomorrow(towTomorrowEntity);//后天天气 return new Result(ResultCode.SUCCESS, weatherResultEntity); } /*public static void main(String[] args) { // 浙江省,杭州市,余杭区,咯咯3 String province = "浙江省"; String city = "杭州市"; String county = "余杭区"; Integer farmid =2; // log.info("当前城市为=======>" +province+city+county); //直接通过它计算出24小时之内的温度同时以一个新的数组存入表中,数据的取出和比较值后面进行 String url = "https://wis.qq.com/weather/common"; String param = "source=xw&weather_type=observe|forecast_1h|forecast_24h|index|alarm|limit|tips&province=" + province + "&city=" + city + "&county=" + county; String result = HttpUtil.urlWithForm(url, param, Charset.defaultCharset(), true); System.out.println(result); String s = HttpUtil.get(result); //对S进行解析,首先获取当天24小时的温度情况 JSONObject jsonObject = JSONObject.fromObject(s); Object data = jsonObject.get("data"); JSONObject jsonData = JSONObject.fromObject(data); System.out.println(jsonData); Object forecastobject24hDetail = JSONObject.fromObject(jsonData.get("forecast_24h")).get("1"); //24小时天气预报的 “1” 为当天的天气预报 Object forecastobject01hDetail = JSONObject.fromObject(jsonData.get("forecast_1h")).get("0"); //24小时天气预报的 “1” 为当天的天气预报 Object alarm = JSONObject.fromObject(jsonData).get("alarm");//报警信息 Object degree = JSONObject.fromObject(forecastobject01hDetail).get("degree");//当前温度 Object maxDegree = JSONObject.fromObject(forecastobject24hDetail).get("max_degree");//当天最高温 Object minDegree = JSONObject.fromObject(forecastobject24hDetail).get("min_degree");//当天最低温 Object day_weather = JSONObject.fromObject(forecastobject24hDetail).get("day_weather");//百天天气 Object day_wind_direction = JSONObject.fromObject(forecastobject24hDetail).get("day_wind_direction");//西风 Object day_wind_power = JSONObject.fromObject(forecastobject24hDetail).get("day_wind_power");//风力 Object night_weather = JSONObject.fromObject(forecastobject24hDetail).get("night_weather");//夜间天气 Object night_wind_direction = JSONObject.fromObject(forecastobject24hDetail).get("night_wind_direction");//夜间风向 Object night_wind_power = JSONObject.fromObject(forecastobject24hDetail).get("night_wind_power");//夜间风力 Object humidityValu = JSONObject.fromObject(jsonData.get("observe")).get("humidity");//湿度 System.out.println("canshu "+alarm); System.out.println("canshu "+degree); System.out.println("canshu "+maxDegree); System.out.println("canshu "+minDegree); System.out.println("canshu "+day_weather); System.out.println("canshu "+day_wind_direction); System.out.println("canshu "+day_wind_power); System.out.println("canshu "+night_weather); System.out.println("canshu "+night_wind_direction); System.out.println("canshu "+night_wind_power); System.out.println("canshu "+humidityValu); }*/ }