package cn.iocoder.yudao.module.iot.gateway.service.device.remote; import cn.hutool.core.lang.Assert; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.module.iot.core.biz.IotDeviceCommonApi; import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceAuthReqDTO; import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceGetReqDTO; import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceRespDTO; import cn.iocoder.yudao.module.iot.core.biz.dto.IotModbusDeviceConfigListReqDTO; import cn.iocoder.yudao.module.iot.core.biz.dto.IotModbusDeviceConfigRespDTO; import cn.iocoder.yudao.module.iot.core.biz.dto.IotSubDeviceRegisterFullReqDTO; import cn.iocoder.yudao.module.iot.core.topic.auth.IotDeviceRegisterReqDTO; import cn.iocoder.yudao.module.iot.core.topic.auth.IotDeviceRegisterRespDTO; import cn.iocoder.yudao.module.iot.core.topic.auth.IotSubDeviceRegisterRespDTO; import cn.iocoder.yudao.module.iot.gateway.config.IotGatewayProperties; import jakarta.annotation.PostConstruct; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.DefaultUriBuilderFactory; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR; /** * Iot 设备信息 Service 实现类:调用远程的 device http 接口,进行设备认证、设备获取等 * * @author 芋道源码 */ @Service @Slf4j public class IotDeviceApiImpl implements IotDeviceCommonApi { @Resource private IotGatewayProperties gatewayProperties; private RestTemplate restTemplate; @PostConstruct public void init() { IotGatewayProperties.RpcProperties rpc = gatewayProperties.getRpc(); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(rpc.getConnectTimeout()); requestFactory.setReadTimeout(rpc.getReadTimeout()); restTemplate = new RestTemplate(requestFactory); restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(rpc.getUrl())); } @Override public CommonResult authDevice(IotDeviceAuthReqDTO authReqDTO) { return doPost("/rpc-api/iot/device/auth", authReqDTO, new ParameterizedTypeReference<>() { }); } @Override public CommonResult getDevice(IotDeviceGetReqDTO getReqDTO) { return doPost("/rpc-api/iot/device/get", getReqDTO, new ParameterizedTypeReference<>() { }); } @Override public CommonResult> getModbusDeviceConfigList(IotModbusDeviceConfigListReqDTO listReqDTO) { return doPost("/rpc-api/iot/modbus/config-list", listReqDTO, new ParameterizedTypeReference<>() { }); } @Override public CommonResult registerDevice(IotDeviceRegisterReqDTO reqDTO) { return doPost("/rpc-api/iot/device/register", reqDTO, new ParameterizedTypeReference<>() { }); } @Override public CommonResult> registerSubDevices(IotSubDeviceRegisterFullReqDTO reqDTO) { return doPost("/rpc-api/iot/device/register-sub", reqDTO, new ParameterizedTypeReference<>() { }); } private CommonResult doPost(String url, T body, ParameterizedTypeReference> responseType) { try { // 请求 HttpEntity requestEntity = new HttpEntity<>(body); ResponseEntity> response = restTemplate.exchange( url, HttpMethod.POST, requestEntity, responseType); // 响应 CommonResult result = response.getBody(); Assert.notNull(result, "请求结果不能为空"); return result; } catch (Exception e) { log.error("[doPost][url({}) body({}) 发生异常]", url, body, e); return CommonResult.error(INTERNAL_SERVER_ERROR); } } }