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.app.service;
|
|
|
|
|
|
|
2024-10-23 17:07:57 +08:00
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
import sanbing.jcpp.app.data.PileSession;
|
|
|
|
|
|
import sanbing.jcpp.app.service.cache.session.PileSessionCacheKey;
|
|
|
|
|
|
import sanbing.jcpp.infrastructure.cache.CacheValueWrapper;
|
|
|
|
|
|
import sanbing.jcpp.infrastructure.cache.TransactionalCache;
|
|
|
|
|
|
import sanbing.jcpp.infrastructure.queue.discovery.ServiceInfoProvider;
|
2024-10-22 17:11:05 +08:00
|
|
|
|
import sanbing.jcpp.proto.gen.ProtocolProto.DownlinkRequestMessage;
|
2025-03-24 02:58:11 +00:00
|
|
|
|
import sanbing.jcpp.proto.gen.ProtocolProto.LoginRequest;
|
|
|
|
|
|
import sanbing.jcpp.proto.gen.ProtocolProto.UplinkQueueMessage;
|
2024-10-23 17:07:57 +08:00
|
|
|
|
import sanbing.jcpp.protocol.adapter.DownlinkController;
|
2024-10-08 09:38:54 +08:00
|
|
|
|
|
2024-10-24 15:41:26 +08:00
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
2024-10-08 09:38:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @author baigod
|
|
|
|
|
|
*/
|
2024-10-23 17:07:57 +08:00
|
|
|
|
@Slf4j
|
|
|
|
|
|
public abstract class DownlinkCallService {
|
2024-10-08 09:38:54 +08:00
|
|
|
|
|
2024-10-23 17:07:57 +08:00
|
|
|
|
@Resource
|
|
|
|
|
|
protected ServiceInfoProvider serviceInfoProvider;
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
|
protected DownlinkController downlinkController;
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
|
protected TransactionalCache<PileSessionCacheKey, PileSession> pileSessionCache;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${cache.type}")
|
|
|
|
|
|
protected String cacheType;
|
|
|
|
|
|
|
|
|
|
|
|
public void sendDownlinkMessage(DownlinkRequestMessage.Builder downlinkMessageBuilder, String pileCode) {
|
|
|
|
|
|
CacheValueWrapper<PileSession> pileSessionCacheValueWrapper = pileSessionCache.get(new PileSessionCacheKey(pileCode));
|
|
|
|
|
|
|
|
|
|
|
|
if (pileSessionCacheValueWrapper == null) {
|
|
|
|
|
|
log.warn("充电桩会话不存在 {}", pileCode);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PileSession pileSession = pileSessionCacheValueWrapper.get();
|
|
|
|
|
|
|
2024-10-24 15:41:26 +08:00
|
|
|
|
UUID protocolSessionId = pileSession.getProtocolSessionId();
|
|
|
|
|
|
|
|
|
|
|
|
if (downlinkMessageBuilder.getSessionIdMSB() == 0) {
|
|
|
|
|
|
downlinkMessageBuilder.setSessionIdMSB(protocolSessionId.getMostSignificantBits());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (downlinkMessageBuilder.getSessionIdLSB() == 0) {
|
|
|
|
|
|
downlinkMessageBuilder.setSessionIdLSB(protocolSessionId.getLeastSignificantBits());
|
|
|
|
|
|
}
|
2025-03-24 02:58:11 +00:00
|
|
|
|
if (downlinkMessageBuilder.getProtocolName() == null) {
|
2024-10-24 15:41:26 +08:00
|
|
|
|
downlinkMessageBuilder.setProtocolName(pileSession.getProtocolName());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-24 02:58:11 +00:00
|
|
|
|
String nodeId = pileSession.getNodeId();
|
|
|
|
|
|
String nodeIp = pileSession.getNodeIp();
|
|
|
|
|
|
int nodeRestPort = pileSession.getNodeRestPort();
|
|
|
|
|
|
int nodeGrpcPort = pileSession.getNodeGrpcPort();
|
|
|
|
|
|
|
|
|
|
|
|
sendDownlinkMessage(downlinkMessageBuilder, nodeId, nodeIp, nodeRestPort, nodeGrpcPort);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void sendDownlinkMessage(DownlinkRequestMessage.Builder downlinkMessageBuilder, UplinkQueueMessage uplinkQueueMessage, LoginRequest loginRequest) {
|
|
|
|
|
|
|
|
|
|
|
|
if (downlinkMessageBuilder.getSessionIdMSB() == 0) {
|
|
|
|
|
|
downlinkMessageBuilder.setSessionIdMSB(uplinkQueueMessage.getSessionIdMSB());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (downlinkMessageBuilder.getSessionIdLSB() == 0) {
|
|
|
|
|
|
downlinkMessageBuilder.setSessionIdLSB(uplinkQueueMessage.getSessionIdLSB());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (downlinkMessageBuilder.getProtocolName() == null) {
|
|
|
|
|
|
downlinkMessageBuilder.setProtocolName(uplinkQueueMessage.getProtocolName());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String nodeId = loginRequest.getNodeId();
|
|
|
|
|
|
String nodeIp = loginRequest.getNodeHostAddress();
|
|
|
|
|
|
int nodeRestPort = loginRequest.getNodeRestPort();
|
|
|
|
|
|
int nodeGrpcPort = loginRequest.getNodeGrpcPort();
|
|
|
|
|
|
|
|
|
|
|
|
sendDownlinkMessage(downlinkMessageBuilder, nodeId, nodeIp, nodeRestPort, nodeGrpcPort);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void sendDownlinkMessage(DownlinkRequestMessage.Builder downlinkMessageBuilder, String nodeId, String nodeIp, int nodeRestPort, int nodeGrpcPort) {
|
2024-10-23 17:07:57 +08:00
|
|
|
|
if (serviceInfoProvider.isMonolith() &&
|
2025-03-24 02:58:11 +00:00
|
|
|
|
("caffeine".equalsIgnoreCase(cacheType) || serviceInfoProvider.getServiceId().equalsIgnoreCase(nodeId))) {
|
2024-10-23 17:07:57 +08:00
|
|
|
|
|
|
|
|
|
|
downlinkController.onDownlink(downlinkMessageBuilder.build())
|
|
|
|
|
|
.setResultHandler(result -> log.debug("下行消息发送完成"));
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
2025-03-24 02:58:11 +00:00
|
|
|
|
_sendDownlinkMessage(downlinkMessageBuilder.build(), nodeIp, nodeRestPort, nodeGrpcPort);
|
2024-10-23 17:07:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-24 02:58:11 +00:00
|
|
|
|
protected abstract void _sendDownlinkMessage(DownlinkRequestMessage downlinkMessage, String nodeIp, int nodeRestPort, int nodeGrpcPort);
|
2024-10-08 09:38:54 +08:00
|
|
|
|
}
|