update 电单车

This commit is contained in:
Guoqs
2024-09-14 13:56:58 +08:00
parent bb18640175
commit 95a49969b4
9 changed files with 68 additions and 63 deletions

View File

@@ -42,7 +42,6 @@ public class EBikeMessageCmd82 extends AbsEBikeMessage {
// 消息ID
byte[] messageIdBytes = BytesUtil.intToBytesLittle(messageId, 2);
// 命令
// byte[] commandBytes = BytesUtil.intToBytesLittle(Integer.parseInt(command), 1);
byte[] commandBytes = new byte[]{(byte) 0x82};
// 数据
byte[] payloadBytes = data.getBytes();

View File

@@ -12,12 +12,12 @@ public interface EBikeSendCommandService {
* 启动充电
* @param command
*/
void sendStartChargingCommand(StartChargingCommand command);
void sendStartChargingCommand(StartChargingCommand command) throws Exception;
/**
* 停止充电
* @param command
*/
void sendStopChargingCommand(StopChargingCommand command);
void sendStopChargingCommand(StopChargingCommand command) throws Exception;
}

View File

@@ -126,8 +126,11 @@ public class PileRemoteService {
.transactionCode(transactionCode)
.chargeAmount(chargeAmount)
.build();
// ykcPushCommandService.pushStartChargingCommand(startChargingCommand);
eBikeSendCommandService.sendStartChargingCommand(startChargingCommand);
try {
eBikeSendCommandService.sendStartChargingCommand(startChargingCommand);
} catch (Exception e) {
log.error("电单车远程启动充电error", e);
}
}
/**

View File

@@ -1,8 +1,10 @@
package com.jsowell.pile.service.impl;
import com.jsowell.common.enums.ykc.PileChannelEntity;
import com.jsowell.common.protocol.SyncPromise;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.RandomUtil;
import com.jsowell.common.util.RpcUtil;
import com.jsowell.pile.domain.ebike.AbsEBikeMessage;
import com.jsowell.pile.domain.ebike.serversend.EBikeMessageCmd82;
import com.jsowell.pile.domain.ykcCommond.StartChargingCommand;
@@ -17,6 +19,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
@@ -29,7 +32,7 @@ public class EBikeSendCommandServiceImpl implements EBikeSendCommandService {
* 电单车发送启动充电指令
*/
@Override
public void sendStartChargingCommand(StartChargingCommand command) {
public void sendStartChargingCommand(StartChargingCommand command) throws Exception {
String pileSn = command.getPileSn();
String connectorCode = command.getConnectorCode();
String transactionCode = command.getTransactionCode();
@@ -81,7 +84,7 @@ public class EBikeSendCommandServiceImpl implements EBikeSendCommandService {
* @param command
*/
@Override
public void sendStopChargingCommand(StopChargingCommand command) {
public void sendStopChargingCommand(StopChargingCommand command) throws Exception {
String pileSn = command.getPileSn();
String connectorCode = command.getConnectorCode();
String transactionCode = command.getTransactionCode();
@@ -128,7 +131,10 @@ public class EBikeSendCommandServiceImpl implements EBikeSendCommandService {
* 公共方法, 发送指令
* @param msg
*/
private void send(AbsEBikeMessage msg) {
private void send(AbsEBikeMessage msg) throws Exception {
this.send(msg, 3, TimeUnit.SECONDS);
}
private void send(AbsEBikeMessage msg, long timeout, TimeUnit unit) throws Exception {
String pileSn = msg.getPhysicalId() + "";
byte[] messageBytes = msg.getMessageBytes();
// PileChannelEntity.output();
@@ -139,6 +145,14 @@ public class EBikeSendCommandServiceImpl implements EBikeSendCommandService {
log.error("电单车send命令失败, 桩号:{}无法获取到长连接, 请检查充电桩连接状态!", pileSn);
throw new NullPointerException("channel");
}
// 创造一个容器用于存放当前线程与rpcClient中的线程交互
SyncPromise syncPromise = new SyncPromise();
// 消息id = channelId + 帧类型(例如: "0x34")
String msgId = ctx.channel().id().toString() + "_" + msg.getCommand();
log.info("同步获取响应数据-发送消息, msgId:{}", msgId);
RpcUtil.getSyncPromiseMap().put(msgId, syncPromise);
ByteBuf byteBuf = ctx.channel().alloc().buffer().writeBytes(messageBytes);
ChannelFuture channelFuture = ctx.channel().writeAndFlush(byteBuf);
channelFuture.addListener((ChannelFutureListener) channelFutureListener -> {
@@ -154,6 +168,19 @@ public class EBikeSendCommandServiceImpl implements EBikeSendCommandService {
log.error("电单车send发送命令失败, pileSn:{}", pileSn, cause);
}
});
// 等待获取结果
byte[] rpcResponse = syncPromise.get(timeout, unit);
if(rpcResponse == null) {
if(syncPromise.isTimeout()) {
// throw new TimeoutException("等待响应结果超时");
log.error("发送[{}]指令后, 等待响应结果超时", msg.getCommand());
} else{
log.error("发送[{}]指令后, 发生其他异常", msg.getCommand());
}
}
// 移除容器
RpcUtil.getSyncPromiseMap().remove(msgId);
}
}

View File

@@ -1954,13 +1954,13 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
String redisKey = CacheConstants.PILE_REAL_TIME_MONITOR_DATA + pileConnectorCode + "_" + transactionCode;
// 拿到所有数据
Map<Object, Object> map = redisCache.hmget(redisKey);
// logger.info("查redis中的实时数据, pileConnectorCode:{}, transactionCode:{}, map:{}", pileConnectorCode, transactionCode, map);
// logger.debug("查redis中的实时数据, pileConnectorCode:{}, transactionCode:{}, map:{}", pileConnectorCode, transactionCode, map);
if (map == null || map.isEmpty()) {
// 按照电单车重新查
pileConnectorCode = transactionCode.substring(0, 10); // 电单车车桩
redisKey = CacheConstants.PILE_REAL_TIME_MONITOR_DATA + pileConnectorCode + "_" + transactionCode;
map = redisCache.hmget(redisKey);
// logger.info("按照电单车重新查, pileConnectorCode:{}, transactionCode:{}, map:{}", pileConnectorCode, transactionCode, map);
// logger.debug("按照电单车重新查, pileConnectorCode:{}, transactionCode:{}, map:{}", pileConnectorCode, transactionCode, map);
}
if (map != null && !map.isEmpty()) {

View File

@@ -154,7 +154,6 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
// 创造一个容器用于存放当前线程与rpcClient中的线程交互
SyncPromise syncPromise = new SyncPromise();
// 消息id = channelId + 帧类型(例如: "0x34")
String msgId = ctx.channel().id().toString() + "_" + YKCUtils.frameType2Str(((YKCFrameTypeCode) frameTypeCode).getBytes());
// log.info("同步获取响应数据-发送消息, msgId:{}", msgId);
@@ -208,7 +207,6 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
// 等待获取结果
byte[] rpcResponse = syncPromise.get(timeout, unit);
if(rpcResponse == null) {
if(syncPromise.isTimeout()) {
// throw new TimeoutException("等待响应结果超时");
@@ -217,7 +215,6 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
log.error("发送[{}]指令后, 发生其他异常", value);
}
}
// 移除容器
RpcUtil.getSyncPromiseMap().remove(msgId);