springBoot对接第三方系统

yml文件

yun:ip: port: username: password: 

controller

package com.ruoyi.web.controller.materials;import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.materials.service.IYunService;
import org.springframework.beans.factory.annotation.Autowired;
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;/*** Created with IntelliJ IDEA.** @Author: yxy* @Date: 2025/07/02/9:46* @Description:*/
@RestController
@RequestMapping("/yun")
public class YunController extends BaseController {@Autowiredprivate IYunService yunService;// 查询分组列表@GetMapping("/getGroupList")public AjaxResult getGroupList(){return success(yunService.getGroupList());}// 查询设备列表@GetMapping("/getDeviceList")public AjaxResult getDeviceList(@RequestParam(required = false) String groupId){return success(yunService.getDeviceList(groupId));}// 查询设备列表@GetMapping("/getDevice")public AjaxResult getDeviceListByDeviceAddr(@RequestParam Long deviceAddr){return success(yunService.getDeviceListByDeviceAddr(deviceAddr));}//根据设备地址获取设备继电器列表@GetMapping("/getRelayList")public AjaxResult getRelayList(@RequestParam Long deviceAddr){return success(yunService.getRelayList(deviceAddr));}}

ServiceImpl

package com.ruoyi.materials.service.impl;import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.redis.RedisCache;import com.ruoyi.materials.domain.yun.DeviceVo;
import com.ruoyi.materials.domain.yun.GroupVo;
import com.ruoyi.materials.domain.yun.RelayVo;
import com.ruoyi.materials.domain.yun.YunUtils;
import com.ruoyi.materials.service.IYunService;
import com.ruoyi.materials.until.SmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;/*** Created with IntelliJ IDEA.** @Author: yxy* @Date: 2025/07/02/9:48* @Description:*/
@Slf4j
@Service
public class YunServiceImpl implements IYunService {@Resourceprivate RedisCache redisCache;private static final String YUN_TOKEN_KEY = "yun_token";private final YunUtils yunUtils;// 构造函数注入public YunServiceImpl(YunUtils yunUtils) {this.yunUtils = yunUtils;}/*** @Description: 查询分组列表* @Author:  yxy* @date:  2025/7/2 10:06*/@Overridepublic List<GroupVo> getGroupList() {// 获取tokenString token = redisCache.getCacheObject(YUN_TOKEN_KEY);// 尝试获取分组列表List<GroupVo> result = yunUtils.fetchGroupList(token);// 如果token无效,重新登录并再次尝试if (result == null || result.size()==0) {yunUtils.loginYun();token = redisCache.getCacheObject(YUN_TOKEN_KEY);result = yunUtils.fetchGroupList(token);}return result != null ? result : Collections.emptyList();}/*** @Description: 查询设备列表* @Author:  yxy* @date:  2025/7/2 14:25*/@Overridepublic List<DeviceVo> getDeviceList(String groupId) {// 获取tokenString token = redisCache.getCacheObject(YUN_TOKEN_KEY);// 尝试获取分组列表List<DeviceVo> result = yunUtils.fetchDeviceList(token,groupId);// 如果token无效,重新登录并再次尝试if (result == null || result.size()==0) {yunUtils.loginYun();token = redisCache.getCacheObject(YUN_TOKEN_KEY);result = yunUtils.fetchDeviceList(token,groupId);}return result != null ? result : Collections.emptyList();}/*** @Description: 根据设备地址查询设备信息* @Author:  yxy* @date:  2025/7/2 17:13*/@Overridepublic List<DeviceVo> getDeviceListByDeviceAddr(Long deviceAddr) {// 获取tokenString token = redisCache.getCacheObject(YUN_TOKEN_KEY);// 尝试获取分组列表List<DeviceVo> result = yunUtils.fetchDeviceListByDeviceAddr(token,deviceAddr);// 如果token无效,重新登录并再次尝试if (result == null || result.size()==0) {yunUtils.loginYun();token = redisCache.getCacheObject(YUN_TOKEN_KEY);result = yunUtils.fetchDeviceListByDeviceAddr(token,deviceAddr);}return result != null ? result : Collections.emptyList();}/*** @Description: 根据设备地址获取设备继电器列表* @Author:  yxy* @date:  2025/7/3 9:56*/@Overridepublic List<RelayVo> getRelayList(Long deviceAddr) {// 获取tokenString token = redisCache.getCacheObject(YUN_TOKEN_KEY);// 尝试获取分组列表List<RelayVo> result = yunUtils.fetchRelayListByDeviceAddr(token,deviceAddr);// 如果token无效,重新登录并再次尝试if (result == null || result.size()==0) {yunUtils.loginYun();token = redisCache.getCacheObject(YUN_TOKEN_KEY);result = yunUtils.fetchRelayListByDeviceAddr(token,deviceAddr);}return result != null ? result : Collections.emptyList();}}

YunUtils

package com.ruoyi.materials.domain.yun;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.materials.until.SmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;/*** Created with IntelliJ IDEA.** @Author: yxy* @Date: 2025/07/02/9:53* @Description:*/
@Slf4j
@Component
public class YunUtils {@Resourceprivate RedisCache redisCache;@Resourceprivate RestTemplate restTemplate;@Value("${yun.ip}")private String yunIp;@Value("${yun.port}")private String yunPort;@Value("${yun.username}")private String yunUsername;@Value("${yun.password}")private String yunPassword;private static final String YUN_TOKEN_KEY = "yun_token";/*** @Description:登录yun系统 存 token 到 redis* @Author:  yxy* @date:  2025/7/1 15:31*/public  void loginYun() {try {ResponseEntity<String> response = SmsUtils.getForList(restTemplate,null,"/api/getToken",yunIp,yunPort,new YunLoginDto(yunUsername, yunPassword));String body = response.getBody();ObjectMapper objectMapper = new ObjectMapper();TokenResponse tokenResponse = objectMapper.readValue(body, TokenResponse.class);if (tokenResponse.getCode() == 1000) {String token = tokenResponse.getData().getToken();redisCache.setCacheObject(YUN_TOKEN_KEY, token, 5, TimeUnit.MINUTES);log.info("云平台登录成功,token已存入Redis");} else {log.error("云平台登录失败,错误码: {}, 错误信息: {}",tokenResponse.getCode(),tokenResponse.getMessage());}} catch (Exception e) {log.error("云平台登录异常", e);throw new RuntimeException("云平台登录失败", e);}}/*** 实际调用API获取分组列表的方法*/public List<GroupVo> fetchGroupList(String token) {if (StringUtils.isBlank(token)) {log.warn("调用fetchGroupList时token为空");return Collections.emptyList();}try {ResponseEntity<String> response = SmsUtils.getForList(restTemplate,token,"/api/device/getGroupList",yunIp,yunPort,null);if (!response.getStatusCode().is2xxSuccessful()) {log.error("获取分组列表失败,HTTP状态码: {}", response.getStatusCode());return Collections.emptyList();}String body = response.getBody();ObjectMapper objectMapper = new ObjectMapper();GroupResponse groupResponse = objectMapper.readValue(body, GroupResponse.class);if (groupResponse.getCode() == 1000  && groupResponse.getData() != null) {return groupResponse.getData();} else {log.error("获取分组列表失败,业务错误码: {}, 错误信息: {}",groupResponse.getCode(),groupResponse.getMessage());return Collections.emptyList();}} catch (JsonProcessingException e) {// 处理JSON解析异常log.error("解析API响应失败,响应内容: ", e);return Collections.emptyList();} catch (Exception e) {log.error("获取分组列表发生未知异常", e);return Collections.emptyList();}}/*** 实际调用API获取设备列表的方法*/public List<DeviceVo> fetchDeviceList(String token,String groupId) {if (StringUtils.isBlank(token)) {log.warn("调用fetchDeviceList时token为空");return Collections.emptyList();}DeviceVo deviceVo = new DeviceVo();if (groupId!=null){deviceVo.setGroupId(groupId);}try {ResponseEntity<String> response = SmsUtils.getForList(restTemplate,token,"/api/device/getDeviceList",yunIp,yunPort,deviceVo);if (!response.getStatusCode().is2xxSuccessful()) {log.error("获取设备列表失败,HTTP状态码: {}", response.getStatusCode());return Collections.emptyList();}String body = response.getBody();ObjectMapper objectMapper = new ObjectMapper();DeviceResponse deviceResponse = objectMapper.readValue(body, DeviceResponse.class);if (deviceResponse.getCode() == 1000  && deviceResponse.getData() != null) {return deviceResponse.getData();} else {log.error("获取设备列表失败,业务错误码: {}, 错误信息: {}",deviceResponse.getCode(),deviceResponse.getMessage());return Collections.emptyList();}} catch (JsonProcessingException e) {// 处理JSON解析异常log.error("解析API响应失败,响应内容: ", e);return Collections.emptyList();} catch (Exception e) {log.error("获取设备列表发生未知异常", e);return Collections.emptyList();}}/*** 根据设备地址查询设备信息*/public List<DeviceVo> fetchDeviceListByDeviceAddr(String token,Long deviceAddr) {if (StringUtils.isBlank(token)) {log.warn("调用fetchDeviceListByDeviceAddr时token为空");return Collections.emptyList();}DeviceVo deviceVo = new DeviceVo();if (deviceAddr!=null){deviceVo.setDeviceAddr(deviceAddr);}try {ResponseEntity<String> response = SmsUtils.getForList(restTemplate,token,"/api/device/getDevice",yunIp,yunPort,deviceVo);if (!response.getStatusCode().is2xxSuccessful()) {log.error("根据设备地址查询设备信息失败,HTTP状态码: {}", response.getStatusCode());return Collections.emptyList();}String body = response.getBody();ObjectMapper objectMapper = new ObjectMapper();DeviceResponse deviceResponse = objectMapper.readValue(body, DeviceResponse.class);if (deviceResponse.getCode() == 1000  && deviceResponse.getData() != null) {return deviceResponse.getData();} else {log.error("根据设备地址查询设备信息失败,业务错误码: {}, 错误信息: {}",deviceResponse.getCode(),deviceResponse.getMessage());return Collections.emptyList();}} catch (JsonProcessingException e) {// 处理JSON解析异常log.error("解析API响应失败,响应内容: ", e);return Collections.emptyList();} catch (Exception e) {log.error("根据设备地址查询设备信息发生未知异常", e);return Collections.emptyList();}}/*** 根据设备地址获取设备继电器列表*/public List<RelayVo> fetchRelayListByDeviceAddr(String token,Long deviceAddr) {if (StringUtils.isBlank(token)) {log.warn("调用 fetchRelayListByDeviceAddr 时token为空");return Collections.emptyList();}RelayVo relayVo = new RelayVo();if (deviceAddr!=null){relayVo.setDeviceAddr(deviceAddr);}try {ResponseEntity<String> response = SmsUtils.getForList(restTemplate,token,"/api/device/getRelayList",yunIp,yunPort,relayVo);if (!response.getStatusCode().is2xxSuccessful()) {log.error("根据设备地址获取设备继电器列表 信息失败,HTTP状态码: {}", response.getStatusCode());return Collections.emptyList();}String body = response.getBody();ObjectMapper objectMapper = new ObjectMapper();RelayResponse relayResponse = objectMapper.readValue(body, RelayResponse.class);if (relayResponse.getCode() == 1000  && relayResponse.getData() != null) {return relayResponse.getData();} else {log.error("根据设备地址获取设备继电器列表 信息失败,业务错误码: {}, 错误信息: {}",relayResponse.getCode(),relayResponse.getMessage());return Collections.emptyList();}} catch (JsonProcessingException e) {// 处理JSON解析异常log.error("解析API响应失败,响应内容: ", e);return Collections.emptyList();} catch (Exception e) {log.error("根据设备地址获取设备继电器列表 信息发生未知异常", e);return Collections.emptyList();}}}

实体类

@Data
public class DeviceResponse {private Integer code;private String message;private List<DeviceVo> data;
}@Data
public class DeviceVo {private Long deviceAddr;          // 设备地址private String groupId;           // 所属分组IDprivate String deviceName;        // 设备名称private Integer offlineinterval;  // 离线间隔private Integer savedatainterval; // 保存数据间隔private Integer alarmSwitch;      // 报警开关状态private Integer alarmRecord;      // 报警记录状态private Double lng;               // 经度private Double lat;               // 纬度private Boolean useMarkLocation;  // 是否使用标记位置private Integer sort;             // 排序号private String deviceCode;        // 设备编码private List<FactorInfo> factors; // 监测因子列表@Datapublic static class FactorInfo {private String factorId;          // 因子IDprivate Long deviceAddr;          // 关联的设备地址private Integer nodeId;           // 节点IDprivate Integer registerId;       // 寄存器IDprivate String factorName;        // 因子名称private String factorIcon;        // 因子图标private Double coefficient;       // 系数private Double offset;            // 偏移量private Integer alarmDelay;       // 报警延迟private Integer alarmRate;        // 报警阈值比例private Integer backToNormalDelay;// 恢复正常延迟private Integer digits;           // 小数位数private String unit;              // 单位private Boolean enabled;          // 是否启用private Integer sort;             // 排序号private Integer maxVoiceAlarmTimes; // 最大语音报警次数private Integer maxSmsAlarmTimes;   // 最大短信报警次数}}@Data
public class GroupResponse implements Serializable {private static final long serialVersionUID = 1L;private Integer code;private String message;private List<GroupVo> data;
}@Data
public class GroupVo {private String groupId;private String parentId;private String groupName;
}@Data
public class RelayResponse {private Integer code;private String message;private List<RelayVo> data;
}@Data
public class RelayVo {private Long deviceAddr;     // 设备地址private String deviceName;   // 设备名称private Boolean enabled;     // 是否启用private String relayName;    // 继电器名称private Integer relayNo;     // 继电器编号private Integer relayStatus; // 继电器状态}@Data
public class ResponseVo implements Serializable {private static final long serialVersionUID = 1L;private int code;private String msg;private String token;private Object data;
}@Data
public class TokenResponse {private Integer code;private String message;private TokenData data;@Datapublic static class TokenData {private Integer expiration;private String token;}
}@Data
@AllArgsConstructor
@NoArgsConstructor
public class YunLoginDto {private String loginName;private String password;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/news/914362.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/914362.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【PTA数据结构 | C语言版】车厢重排

本专栏持续输出数据结构题目集&#xff0c;欢迎订阅。 文章目录题目代码题目 一列挂有 n 节车厢&#xff08;编号从 1 到 n&#xff09;的货运列车途径 n 个车站&#xff0c;计划在行车途中将各节车厢停放在不同的车站。假设 n 个车站的编号从 1 到 n&#xff0c;货运列车按照…

量子计算能为我们做什么?

科技公司正斥资数十亿美元投入量子计算领域&#xff0c;尽管这项技术距离实际应用还有数年时间。那么&#xff0c;未来的量子计算机将用于哪些方面&#xff1f;为何众多专家坚信它们会带来颠覆性变革&#xff1f; 自 20 世纪 80 年代起&#xff0c;打造一台利用量子力学独特性质…

BKD 树(Block KD-Tree)Lucene

BKD 树&#xff08;Block KD-Tree&#xff09;是 Lucene 用来存储和快速查询 **多维数值型数据** 的一种磁盘友好型数据结构&#xff0c;可以把它想成&#xff1a;> **“把 KD-Tree 分块压缩后落到磁盘上&#xff0c;既能做磁盘顺序读&#xff0c;又能像内存 KD-Tree 一样做…

【Mysql作业】

第一次作业要求1.首先打开Windows PowerShell2.连接到MYSQL服务器3.执行以下SQL语句&#xff1a;-- 创建数据库 CREATE DATABASE mydb6_product;-- 使用数据库 USE mydb6_product;-- 创建employees表 CREATE TABLE employees (id INT PRIMARY KEY,name VARCHAR(50) NOT NULL,ag…

(C++)STL:list认识与使用全解析

本篇基于https://cplusplus.com/reference/list/list/讲解 认识 list是一个带头结点的双向循环链表翻译总结&#xff1a; 序列容器&#xff1a;list是一种序列容器&#xff0c;允许在序列的任何位置进行常数时间的插入和删除操作。双向迭代&#xff1a;list支持双向迭代&#x…

Bash函数详解

目录**1. 基础函数****2. 参数处理函数****3. 文件操作函数****4. 日志与错误处理****5. 实用工具函数****6. 高级函数技巧****7. 常用函数库示例****总结&#xff1a;Bash 函数核心要点**1. 基础函数 1.1 定义与调用 可以自定义函数名称&#xff0c;例如将greet改为yana。❌…

Python爬虫实战:研究rows库相关技术

1. 引言 在当今数字化时代,互联网上存在着大量有价值的表格数据,这些数据以 HTML 表格、CSV、Excel 等多种格式存在。然而,由于数据源的多样性和不规范性,表格结构往往存在复杂表头、合并单元格、不规则数据行等问题,给数据的自动化处理带来了巨大挑战。 传统的数据处理工…

通过同态加密实现可编程隐私和链上合规

1. 引言 2023年9月28日&#xff0c;a16z 的加密团队发布了 Nakamoto Challenge&#xff0c;列出了区块链中需要解决的最重要问题。尤其是其中的第四个问题格外引人注意&#xff1a;“合规的可编程隐私”&#xff0c;因为Zama团队已经在这方面积极思考了一段时间。本文提出了使…

封装---统一封装处理页面标题

一.采用工具来实现(setPageTitle.ts)多个页面中用更统一的方式设置 document.title&#xff0c;可以封装一个工具函数:在utils目录下新建文件:setPageTitle.ts如果要在每个页面设置相同的网站标志可以使用下面的appNameconst appName: string import.meta.env.VITE_APP_NAMEex…

JAVA学习笔记 首个HelloWorld程序-002

目录 1 前言 2 开发首个程序 3 小结 1 前言 在所有的开发语言中&#xff0c;基本上首先程序就是输出HelloWorld&#xff0c;这里也不例外。这个需要注意的是&#xff0c;程序的核心功能是数据输出&#xff0c;是要有一个结果&#xff0c;可能没有输入&#xff0c;但是一定有…

智慧监所:科技赋能监狱管理新变革

1. 高清教育&#xff1a;告别模糊画面&#xff0c;学习更清晰传统电视的雪花屏终于成为历史&#xff01;新系统采用高清传输&#xff0c;课件文字清晰可见&#xff0c;教学视频细节分明。某监狱教育科王警官说&#xff1a;"现在播放法律课程&#xff0c;服刑人员能清楚看到…

专题:2025供应链数智化与效率提升报告|附100+份报告PDF、原数据表汇总下载

全文链接&#xff1a;https://tecdat.cn/?p42926 在全球产业链重构与数字技术革命的双重驱动下&#xff0c;供应链正经历从传统经验驱动向数据智能驱动的范式变革。从快消品产能区域化布局到垂类折扣企业的效率竞赛&#xff0c;从人形机器人的成本优化到供应链金融对中小企业的…

uniapp+vue3+ts项目:实现小程序文件下载、预览、进度监听(含项目、案例、插件)

uniapp+vue3+ts项目:实现小程序文件下载、预览、进度监听(含项目、案例、插件) 支持封装调用: 项目采用uniapp+vue3+ts +京东nutUI 开发nutUi文档:loadingPage组件:https://uniapp-nutui.tech/components/exhibition/loadingpage.html案例效果图: 略博主自留地:参考本地…

用Python和OpenCV从零搭建一个完整的双目视觉系统(六 最终篇)

本系列文章旨在系统性地阐述如何利用 Python 与 OpenCV 库&#xff0c;从零开始构建一个完整的双目立体视觉系统。 本项目github地址&#xff1a;https://github.com/present-cjn/stereo-vision-python.git 1. 概述 欢迎来到本系列文章的最后一篇。在过去的几篇文章中&#…

Android View 绘制流程 简述 (无限递归+BitMap问题)

绘制流程 在 Android 的 View 系统中&#xff0c;draw(canvas) 和 dispatchDraw(canvas) 是绘制流程中的两个关键方法&#xff1a; 1. draw(canvas) 方法的作用 draw(canvas) 是 View 类中的核心绘制方法&#xff0c;它的主要职责包括&#xff1a; 绘制背景 - 调用 drawBac…

算法学习笔记:18.拉斯维加斯算法 ——从原理到实战,涵盖 LeetCode 与考研 408 例题

在随机化算法领域&#xff0c;拉斯维加斯&#xff08;Las Vegas&#xff09;算法以其独特的设计思想占据重要地位。与蒙特卡洛&#xff08;Monte Carlo&#xff09;算法不同&#xff0c;拉斯维加斯算法总能给出正确的结果&#xff0c;但运行时间具有随机性 —— 在最坏情况下可…

26-计组-指令执行过程

一、指令周期1. 定义与组成定义&#xff1a;CPU取出并执行一条指令所需的全部时间&#xff0c;称为指令周期。子周期划分&#xff1a;取指周期&#xff08;必选&#xff09;&#xff1a;从存储器取指令到指令寄存器&#xff08;IR&#xff09;。间址周期&#xff08;可选&#…

【JMeter】数据驱动测试

文章目录创建数据文件加载数据文件根据数据文件请求接口、传递参数拓展含义&#xff1a;根据数据的数量、内容&#xff0c;自动的决定用例的数据和内容。数据驱动测试用例。步骤&#xff1a; 创建数据文件加载数据文件根据数据文件请求接口、传递参数 创建数据文件 Jmeter支…

Springboot实现一个接口加密

首先来看效果这个主要是为了防止篡改请求的。 我们这里采用的是一个AOP的拦截&#xff0c;在有需要这样的接口上添加了加密处理。 下面是一些功能防篡改HMAC-SHA256 参数签名密钥仅客户端 & 服务器持有防重放秒级时间戳 有效窗口校验默认允许 5 分钟防窃听AES/CBC/PKCS5Pa…

斯坦福 CS336 动手大语言模型 Assignment1 BPE Tokenizer TransformerLM

所有代码更新至 https://github.com/WangYuHang-cmd/CS336/tree/main/assignment1-basics 作业文件结构: CS336/assignment1-basics/ ├── tests/ # 测试文件目录 │ ├── adapters.py # 适配器测试 │ ├── conftest.py # pyt…