Files
JChargePointProtocol/jcpp-protocol-api/src/main/java/sanbing/jcpp/protocol/adapter/DownlinkController.java

76 lines
2.7 KiB
Java
Raw Normal View History

2024-10-08 09:38:54 +08:00
/**
2025-03-04 10:42:17 +08:00
* 开源代码仅供学习和交流研究使用商用请联系三丙
* 微信mohan_88888
* 抖音程序员三丙
* 付费课程知识星球https://t.zsxq.com/aKtXo
2024-10-08 09:38:54 +08:00
*/
package sanbing.jcpp.protocol.adapter;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
2024-10-22 17:11:05 +08:00
import sanbing.jcpp.proto.gen.ProtocolProto.DownlinkRequestMessage;
2024-10-08 09:38:54 +08:00
import sanbing.jcpp.protocol.domain.ProtocolSession;
import sanbing.jcpp.protocol.provider.ProtocolSessionRegistryProvider;
import java.util.UUID;
/**
* @author baigod
*/
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Slf4j
public class DownlinkController {
@Value("${api.timeout.onDownlink:3000}")
public long onDownlinkTimeout;
@Resource
ProtocolSessionRegistryProvider protocolSessionRegistryProvider;
@PostMapping(value = "/onDownlink", consumes = "application/x-protobuf", produces = "application/x-protobuf")
2024-10-22 17:11:05 +08:00
public DeferredResult<ResponseEntity<String>> onDownlink(@RequestBody DownlinkRequestMessage downlinkMsg) {
2024-10-16 14:59:21 +08:00
log.debug("收到REST下行请求 {}", downlinkMsg);
2024-10-08 09:38:54 +08:00
final DeferredResult<ResponseEntity<String>> response = new DeferredResult<>(onDownlinkTimeout,
ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT).build());
UUID protocolSessionId = new UUID(downlinkMsg.getSessionIdMSB(), downlinkMsg.getSessionIdLSB());
2024-10-08 09:38:54 +08:00
ProtocolSession protocolSession = protocolSessionRegistryProvider.get(protocolSessionId);
2024-10-08 09:38:54 +08:00
try {
if (protocolSession != null) {
2024-10-08 09:38:54 +08:00
protocolSession.onDownlink(downlinkMsg);
2024-10-08 09:38:54 +08:00
response.setResult(ResponseEntity.status(HttpStatus.OK).build());
} else {
2024-10-16 14:59:21 +08:00
log.info("下发报文时Session未找到 sessionId: {}", protocolSessionId);
2024-10-08 09:38:54 +08:00
response.setResult(ResponseEntity.status(HttpStatus.NOT_FOUND).body("Protocol Session not found for ID:" + protocolSessionId));
}
} catch (Exception e) {
2024-10-08 09:38:54 +08:00
log.warn("下发报文时处理失败 sessionId: {}", protocolSessionId, e);
2024-10-08 09:38:54 +08:00
if (!response.isSetOrExpired()) {
2024-10-08 09:38:54 +08:00
response.setResult(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()));
2024-10-08 09:38:54 +08:00
}
}
2024-10-08 09:38:54 +08:00
return response;
}
}