From 3afe56706b52e3ba41e9d788e66455dd8903ab73 Mon Sep 17 00:00:00 2001 From: mi9688 Date: Fri, 15 Nov 2024 17:51:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=A9=E6=B0=94=E9=A2=84?= =?UTF-8?q?=E6=B5=8B=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/weather/WeatherController.java | 27 +++ .../api/dashboard/service/ApiDemo.java | 208 ------------------ .../devlink/service/DevLinkCountService.java | 18 ++ .../service/DevLinkRealTimeDataService.java | 4 +- .../api/xinzhitianqi/TianqiApi.java | 57 +++++ .../api/xinzhitianqi/UrlConstant.java | 19 ++ 6 files changed, 123 insertions(+), 210 deletions(-) create mode 100644 fastbee-open-api/src/main/java/com/fastbee/data/controller/weather/WeatherController.java delete mode 100644 fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/dashboard/service/ApiDemo.java create mode 100644 fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/devlink/service/DevLinkCountService.java create mode 100644 fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/xinzhitianqi/TianqiApi.java create mode 100644 fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/xinzhitianqi/UrlConstant.java diff --git a/fastbee-open-api/src/main/java/com/fastbee/data/controller/weather/WeatherController.java b/fastbee-open-api/src/main/java/com/fastbee/data/controller/weather/WeatherController.java new file mode 100644 index 0000000..3c66c91 --- /dev/null +++ b/fastbee-open-api/src/main/java/com/fastbee/data/controller/weather/WeatherController.java @@ -0,0 +1,27 @@ +package com.fastbee.data.controller.weather; + + +import com.fastbee.common.core.domain.AjaxResult; +import com.fastbee.deviceData.api.xinzhitianqi.TianqiApi; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 天气预报接口 + */ +@RestController +@RequestMapping("/weather") +public class WeatherController { + + /** + * 获取七天的天气预报 + */ + @GetMapping("/qitian") + public AjaxResult getWeather() { + return AjaxResult.success(TianqiApi.QitianApi()); + } + + + +} diff --git a/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/dashboard/service/ApiDemo.java b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/dashboard/service/ApiDemo.java deleted file mode 100644 index e9271cb..0000000 --- a/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/dashboard/service/ApiDemo.java +++ /dev/null @@ -1,208 +0,0 @@ -package com.fastbee.deviceData.api.dashboard.service; - - - -import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; - -import java.io.*; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLEncoder; -import java.util.Map; -import java.util.HashMap; - -public class ApiDemo { - - public static void main(String[] args) { - - // 发送http请求的url - String url = "http://apis.juhe.cn/simpleWeather/query"; - - Map params = new HashMap(); - params.put("key", "8fd5a2d4b0f26e5ba9c168b75c6281d5"); // 在个人中心->我的数据,接口名称上方查看 - params.put("city", "苏州"); // 要查询的城市名称或城市ID - - - String paramsStr = urlencode(params); - System.out.println(paramsStr); - String response = doGet(url,paramsStr); - // // post请求 - // String response = doPost(url,paramsStr); - - // 输出请求结果 - System.out.println(response); - - try { - // 解析请求结果,json: - JSONObject jsonObject = JSONUtil.parseObj(response); - System.out.println(jsonObject); - // 具体返回示例值,参考返回参数说明、json返回示例 - } catch (Exception e) { - e.printStackTrace(); - } - } - - // 将map型转为请求参数型 - public static String urlencode(Map data) { - StringBuilder sb = new StringBuilder(); - for (Map.Entry i : data.entrySet()) { - try { - sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&"); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } - return sb.toString(); - } - - /** - * get方式的http请求 - * - * @param httpUrl 请求地址 - * @param paramStr 请求参数 - * @return 返回结果 - */ - - public static String doGet(String httpUrl,String paramStr) { - HttpURLConnection connection = null; - InputStream inputStream = null; - BufferedReader bufferedReader = null; - String result = null;// 返回结果字符串 - try { - httpUrl += "?"+paramStr; - // 创建远程url连接对象 - URL url = new URL(httpUrl); - // 通过远程url连接对象打开一个连接,强转成httpURLConnection类 - connection = (HttpURLConnection) url.openConnection(); - // 设置连接方式:get - connection.setRequestMethod("GET"); - // 设置连接主机服务器的超时时间:15000毫秒 - connection.setConnectTimeout(15000); - // 设置读取远程返回的数据时间:60000毫秒 - connection.setReadTimeout(60000); - // 设置请求头 - connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - // 发送请求 - connection.connect(); - // 通过connection连接,获取输入流 - if (connection.getResponseCode() == 200) { - inputStream = connection.getInputStream(); - // 封装输入流,并指定字符集 - bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); - // 存放数据 - StringBuilder sbf = new StringBuilder(); - String temp; - while ((temp = bufferedReader.readLine()) != null) { - sbf.append(temp); - sbf.append(System.getProperty("line.separator")); - } - result = sbf.toString(); - } - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - // 关闭资源 - if (null != bufferedReader) { - try { - bufferedReader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (null != inputStream) { - try { - inputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (connection != null) { - connection.disconnect();// 关闭远程连接 - } - } - return result; - } - - /** - * post方式的http请求 - * - * @param httpUrl 请求地址 - * @param paramStr 请求参数 - * @return 返回结果 - */ - public static String doPost(String httpUrl, String paramStr) { - HttpURLConnection connection = null; - InputStream inputStream = null; - OutputStream outputStream = null; - BufferedReader bufferedReader = null; - String result = null; - try { - URL url = new URL(httpUrl); - // 通过远程url连接对象打开连接 - connection = (HttpURLConnection) url.openConnection(); - // 设置连接请求方式 - connection.setRequestMethod("POST"); - // 设置连接主机服务器超时时间:15000毫秒 - connection.setConnectTimeout(15000); - // 设置读取主机服务器返回数据超时时间:60000毫秒 - connection.setReadTimeout(60000); - // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true - connection.setDoOutput(true); - // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。 - connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - // 通过连接对象获取一个输出流 - outputStream = connection.getOutputStream(); - // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的 - outputStream.write(paramStr.getBytes()); - // 通过连接对象获取一个输入流,向远程读取 - if (connection.getResponseCode() == 200) { - inputStream = connection.getInputStream(); - // 对输入流对象进行包装:charset根据工作项目组的要求来设置 - bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); - StringBuilder sbf = new StringBuilder(); - String temp; - // 循环遍历一行一行读取数据 - while ((temp = bufferedReader.readLine()) != null) { - sbf.append(temp); - sbf.append(System.getProperty("line.separator")); - } - result = sbf.toString(); - } - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - // 关闭资源 - if (null != bufferedReader) { - try { - bufferedReader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (null != outputStream) { - try { - outputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (null != inputStream) { - try { - inputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (connection != null) { - connection.disconnect(); - } - } - return result; - } -} diff --git a/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/devlink/service/DevLinkCountService.java b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/devlink/service/DevLinkCountService.java new file mode 100644 index 0000000..6e44bf9 --- /dev/null +++ b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/devlink/service/DevLinkCountService.java @@ -0,0 +1,18 @@ +package com.fastbee.deviceData.api.devlink.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class DevLinkCountService { + @Autowired + private DevLinkAuthorizationService authorizationService; + + + /** + * 统计在线设备数量 + */ + public void countOnlineDevice() { +// authorizationService.countOnlineDevice(); + } +} diff --git a/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/devlink/service/DevLinkRealTimeDataService.java b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/devlink/service/DevLinkRealTimeDataService.java index b319d94..7269dd0 100644 --- a/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/devlink/service/DevLinkRealTimeDataService.java +++ b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/devlink/service/DevLinkRealTimeDataService.java @@ -20,7 +20,7 @@ public class DevLinkRealTimeDataService { /** - * 获取气象设备实时数据 + * 获取设备实时数据 * @param deviceId 设备id * @return 实时数据 */ @@ -74,7 +74,7 @@ public class DevLinkRealTimeDataService { } /** - * 请求气象设备实时数据 + * 请求设备实时数据 * @param deviceId 设备id * @return 实时数据 */ diff --git a/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/xinzhitianqi/TianqiApi.java b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/xinzhitianqi/TianqiApi.java new file mode 100644 index 0000000..c9f2432 --- /dev/null +++ b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/xinzhitianqi/TianqiApi.java @@ -0,0 +1,57 @@ +package com.fastbee.deviceData.api.xinzhitianqi; + + +import cn.hutool.http.HttpResponse; +import cn.hutool.http.HttpStatus; +import cn.hutool.http.HttpUtil; +import cn.hutool.json.JSONObject; + +/** + * @Author wei + * @Date 2023/5/23 + */ +public class TianqiApi { + + /** + * 获取用户所有设备信息 + */ + public static JSONObject QitianApi() { + + String url = UrlConstant.QitianApi + "?key=" + + UrlConstant.PrivateKey + + "&location=kenli&language=zh-Hans&unit=c";//指定URL + HttpResponse execute = HttpUtil.createGet(url).execute(); + if (execute.getStatus() == HttpStatus.HTTP_OK) { + JSONObject jsonObject = new JSONObject(execute.body()); +// System.out.println("jsonObject = " + jsonObject); + return jsonObject; + } + + + return null; + } + + /** + * 未来15天逐日天气预报和昨日天气 + * + * @param start 0 代表当天 当天+n + * @param days 获取几天的数据 + * @return + */ + + public static JSONObject weatherDaily(int start, int days) { + + String url = UrlConstant.QitianApi + "?key=" + + UrlConstant.PrivateKey + + "&location=kenli&language=zh-Hans&unit=c&start=" + start + "&days=" + days;//指定URL + HttpResponse execute = HttpUtil.createGet(url).execute(); + if (execute.getStatus() == HttpStatus.HTTP_OK) { + JSONObject jsonObject = new JSONObject(execute.body()); +// System.out.println("jsonObject = " + jsonObject); + return jsonObject; + } + + + return null; + } +} diff --git a/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/xinzhitianqi/UrlConstant.java b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/xinzhitianqi/UrlConstant.java new file mode 100644 index 0000000..7f6492e --- /dev/null +++ b/fastbee-service/fastbee-device-service/src/main/java/com/fastbee/deviceData/api/xinzhitianqi/UrlConstant.java @@ -0,0 +1,19 @@ +package com.fastbee.deviceData.api.xinzhitianqi; + +/** + * 瀚臻老设备平台常量 + */ +public class UrlConstant { + + + public static String BaseUrl = "https://api.seniverse.com"; + public static String PublicKey = "PxWBOr8VPR88ELlVZ"; + public static String PrivateKey = "S5R9CdTx7dJeXq5V-"; + + public static String ChuanyiApi = BaseUrl + "/v3/life/suggestion.json"; + // 实时天气 + public static String ShishiApi = BaseUrl + "/v3/weather/now.json"; + // 未来15天逐日天气预报和昨日天气 + public static String QitianApi = BaseUrl + "/v3/weather/daily.json"; + +}