Parcourir la source

'开发公共天气信息接口'

james il y a 1 an
Parent
commit
c74182363a

+ 4 - 4
service-backend/service-backend-biz/src/main/java/com/usky/backend/service/impl/DataQueryServiceImpl.java

@@ -156,16 +156,16 @@ public class DataQueryServiceImpl implements DataQueryService {
                         for(int k=0;k<list1.size();k++){
                             if(dateList.get(i).equals(list1.get(k).getDataDate()) && deviceIdList.get(j).equals(list1.get(k).getDeviceId())){
                                 if(list1.get(k).getAttributeName().equals("wd")){
-                                    responseVO.setWd(Double.toString(list1.get(k).getAvrg()));
+                                    responseVO.setWd(String.format("%.1f",list1.get(k).getAvrg()));
                                 }
                                 if(list1.get(k).getAttributeName().equals("sd")){
-                                    responseVO.setSd(Double.toString(list1.get(k).getAvrg()));
+                                    responseVO.setSd(String.format("%.1f",list1.get(k).getAvrg()));
                                 }
                                 if(list1.get(k).getAttributeName().equals("pm2_5")){
-                                    responseVO.setPm2_5(Double.toString(list1.get(k).getAvrg()));
+                                    responseVO.setPm2_5(String.format("%.1f",list1.get(k).getAvrg()));
                                 }
                                 if(list1.get(k).getAttributeName().equals("co2")){
-                                    responseVO.setCo2(Double.toString(list1.get(k).getAvrg()));
+                                    responseVO.setCo2(String.format("%.1f",list1.get(k).getAvrg()));
                                 }
                                 if(list1.get(k).getAttributeName().equals("voc")){ //voc平均值(优就是1,良是2,差是3)
                                     String name = "";

+ 63 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/controller/web/WeatherController.java

@@ -0,0 +1,63 @@
+package com.usky.iot.controller.web;
+
+import com.alibaba.nacos.common.utils.StringUtils;
+import com.usky.common.core.bean.ApiResult;
+import com.usky.common.core.exception.BusinessException;
+import com.usky.common.core.util.HttpUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author zyj
+ * @date 2023/9/14 15:02
+ */
+@RestController
+@RequestMapping("weather")
+public class WeatherController {
+    private static final String ALI_WEATHER_HEADER_KEY = "Authorization";
+    private static final String ALI_WEATHER_APPCODE = "0f2b7fce6e104ba8835358b7b59b4fb6";
+    private static final String ALI_WEATHER_HEADER_VALUE = "APPCODE " + ALI_WEATHER_APPCODE;
+
+    private String weather = "";
+
+    private Date date = null;
+
+    /**
+     * 公共天气信息
+     * @param area
+     * @return
+     */
+    @GetMapping("getInfo")
+    public ApiResult<String> get(@RequestParam(value = "area") String area) {
+        if (StringUtils.isBlank(weather)) {
+            weather = getWeatherApi(area);
+            date = new Date();
+        } else {
+            if ((System.currentTimeMillis() - date.getTime()) >= (1000 * 60 * 60 * 5)) {
+                weather = getWeatherApi(area);
+                date = new Date();
+            }
+        }
+
+        return ApiResult.success(weather);
+    }
+
+    public String getWeatherApi(String area) {
+        try {
+            String ALI_WEATHER_API_URL = "https://weather01.market.alicloudapi.com/area-to-weather?area="+area;
+            Map<String, String> headerMap = new HashMap<>();
+            headerMap.put(ALI_WEATHER_HEADER_KEY, ALI_WEATHER_HEADER_VALUE);
+            headerMap.put("Content-Type","application/json;charset=UTF-8");
+            return HttpUtils.get(ALI_WEATHER_API_URL, headerMap);
+        } catch (IOException e) {
+            throw new BusinessException(e.getMessage());
+        }
+    }
+}