调整mq消息发送, 新增日志信息
调整test单元测试
This commit is contained in:
parent
a899934904
commit
6ea0e7586b
@ -5,6 +5,7 @@ import java.util.Date;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -93,6 +94,7 @@ public class MessageUtilService {
|
||||
* @param dataBody
|
||||
*/
|
||||
public void publishAESMessage(MessageTopic topic, MqttMessageHeader header, JSONObject dataBody) {
|
||||
log.info("MQTT->>>>>>>>>>>>>请求发送mqtt消息: topic:{}; header:{}, data:{}",JSONUtil.toJsonStr(topic),JSONUtil.toJsonStr(header),JSONUtil.toJsonStr(dataBody));
|
||||
// 1. 获取AES
|
||||
SymmetricCrypto aes = getAes(topic.getStationCode());
|
||||
// 2. 数据
|
||||
@ -104,9 +106,14 @@ public class MessageUtilService {
|
||||
// 3. 发送MQTT消息
|
||||
try {
|
||||
MqttMessage mqttMessage = publishMessage.publish(encrypt, topic.toString());
|
||||
addMqttMessage(topic, mqttMessage.getId()+"", mqttMessage.getQos(), dataBody);
|
||||
try {
|
||||
addMqttMessage(topic, mqttMessage.getId()+"", mqttMessage.getQos(), dataBody);
|
||||
} catch (Exception e) {
|
||||
log.error("MQTT->>>>>>>>>>>>>记录mqtt消息出现了错误{},topic:{}; header:{}, data:{}; message:{}",e.getMessage(),JSONUtil.toJsonStr(topic),JSONUtil.toJsonStr(header),JSONUtil.toJsonStr(dataBody), JSONUtil.toJsonStr(mqttMessage));
|
||||
throw new RuntimeException("记录mqtt消息出现了错误"+e.getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
log.error("MQTT->>>>>>>>>>>>>发送站端mqtt消息出现异常{},topic:{}; header:{}, data:{}",e.getMessage(),JSONUtil.toJsonStr(topic),JSONUtil.toJsonStr(header),JSONUtil.toJsonStr(dataBody));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -7,10 +7,16 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.evotech.hd.cloud.dao.*;
|
||||
import com.evotech.hd.cloud.entity.BatteryStationSecretKey;
|
||||
import com.evotech.hd.cloud.mqtt.enums.RequestFunctionTypesEnum;
|
||||
import com.evotech.hd.cloud.mqtt.message.MessageTopic;
|
||||
import com.evotech.hd.cloud.mqtt.message.handle.RequestMessageService;
|
||||
import com.evotech.hd.cloud.service.BatteryStationCdStrategyService;
|
||||
import com.evotech.hd.common.core.constant.HDConstant;
|
||||
import com.evotech.hd.common.redis.utils.RedisUtil;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@ -18,15 +24,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.evotech.hd.cloud.controller.test.service.MqttConfigService;
|
||||
import com.evotech.hd.cloud.controller.test.service.MqttTestUtil;
|
||||
import com.evotech.hd.cloud.dao.BatteryStationDao;
|
||||
import com.evotech.hd.cloud.dao.BatteryStationDcDao;
|
||||
import com.evotech.hd.cloud.dao.CompanyDao;
|
||||
import com.evotech.hd.cloud.dao.OrderSwapBatteryPreDao;
|
||||
import com.evotech.hd.cloud.dao.TradeDetailDao;
|
||||
import com.evotech.hd.cloud.dao.VehicleInfoDao;
|
||||
import com.evotech.hd.cloud.dao.VehicleWechatUserRelationDao;
|
||||
import com.evotech.hd.cloud.dao.WalletAccountDao;
|
||||
import com.evotech.hd.cloud.dao.WalletAccountDetailDao;
|
||||
import com.evotech.hd.cloud.mqtt.enums.MqttMessageTypeEnum;
|
||||
import com.evotech.hd.cloud.mqtt.enums.StateFunctionTypesEnum;
|
||||
import com.evotech.hd.cloud.mqtt.message.MqttMessageHeader;
|
||||
@ -107,6 +104,10 @@ public class AddTestData {
|
||||
@Resource
|
||||
private MqttConfigService mqttService;
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
@Resource
|
||||
private BatteryStationSecretKeyDao batteryStationSecretKeyDao;
|
||||
@Resource
|
||||
private MessageUtilService messageUtilService;
|
||||
@Resource
|
||||
private BatteryStationDcDao batteryStationDcDao;
|
||||
@ -131,13 +132,40 @@ public class AddTestData {
|
||||
@Resource
|
||||
private BatteryStationCdStrategyService batteryStationCdStrategyService;
|
||||
|
||||
@Test
|
||||
// @Test
|
||||
void strategyFind(){
|
||||
System.out.println(batteryStationCdStrategyService.chargeStationFindList("123456"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// @Test
|
||||
void testStrategyAndMqttM2S() {
|
||||
testClient = mqttService.connect();
|
||||
// 1. 查 站
|
||||
List<BatteryStation> stationList = batteryStationDao.selectList(new LambdaUpdateWrapper<BatteryStation>().eq(BatteryStation::getPkId, 4));
|
||||
BatteryStation station = stationList.get(0);
|
||||
sendStrategyInfo(station.getCode());
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void sendStrategyInfo(String stationCode) {
|
||||
JSONObject jo = JSONUtil.parseObj("{stationCode:"+stationCode+"}");
|
||||
String topic = mqttTestUtil.getTopic(stationCode, MqttMessageTypeEnum.REQUEST.getType());
|
||||
MqttMessageHeader header = mqttTestUtil.getHeader(RequestFunctionTypesEnum.FUN_STRATEGY_INFO.getFunction());
|
||||
publishMessage(stationCode, jo, topic, header);
|
||||
|
||||
}
|
||||
|
||||
// private void sendStrategyInfo(String stationCode) {
|
||||
// JSONObject jo = JSONUtil.parseObj("{stationCode:"+stationCode+"}");
|
||||
// String topic = mqttTestUtil.getTopic(stationCode, MqttMessageTypeEnum.REQUEST.getType());
|
||||
// MqttMessageHeader header = mqttTestUtil.getHeader(RequestFunctionTypesEnum.FUN_STRATEGY_INFO.getFunction());
|
||||
// publishMessage(stationCode, jo, topic, header);
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
// @Test
|
||||
void addDC() {
|
||||
@ -655,7 +683,7 @@ public class AddTestData {
|
||||
|
||||
private void publishMessage(String stationCode, JSONObject jo, String topic, MqttMessageHeader header) {
|
||||
// 1. 获取AES
|
||||
JSONObject aesJo = messageUtilService.getAESKey(stationCode);
|
||||
JSONObject aesJo = getAESKey(stationCode);
|
||||
SymmetricCrypto aes = new AES(Mode.CBC, Padding.PKCS5Padding, aesJo.getStr("aesSecretKey").getBytes(), aesJo.getStr("aesIv").getBytes());
|
||||
// 2. 数据
|
||||
MyMqttMessage message = new MyMqttMessage();
|
||||
@ -665,6 +693,46 @@ public class AddTestData {
|
||||
// 3. 发送MQTT消息
|
||||
mqttService.publish(testClient, encrypt, topic);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取AES秘钥和IV
|
||||
* @param stationCode
|
||||
* @return
|
||||
*/
|
||||
public JSONObject getAESKey(String stationCode) {
|
||||
JSONObject jo = new JSONObject();
|
||||
Object o1 = null;
|
||||
try {
|
||||
o1 = redisUtil.get(HDConstant.HD_STATION_SECRET_KEY_AES_PREFIX + stationCode + ":key");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Object o2 = null;
|
||||
try {
|
||||
o2 = redisUtil.get(HDConstant.HD_STATION_SECRET_KEY_AES_PREFIX + stationCode + ":iv");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (!ObjectUtil.isEmpty(o1) && !ObjectUtil.isEmpty(o2)) {
|
||||
jo.set("aesSecretKey", o1.toString());
|
||||
jo.set("aesIv", o2.toString());
|
||||
// jo.set("encryptKey", o1.toString());
|
||||
// jo.set("encryptVector", o2.toString());
|
||||
return jo;
|
||||
}
|
||||
BatteryStationSecretKey bssk = batteryStationSecretKeyDao.selectOne(new QueryWrapper<BatteryStationSecretKey>().eq("type", 2).eq("station_code", stationCode));
|
||||
if (bssk == null) {
|
||||
throw new RuntimeException("AES秘钥未获取到,请站端重新登陆!");
|
||||
// return setAesKey(stationCode);
|
||||
}
|
||||
redisUtil.set(HDConstant.HD_STATION_SECRET_KEY_AES_PREFIX + stationCode + ":key", bssk.getPublicKey());
|
||||
redisUtil.set(HDConstant.HD_STATION_SECRET_KEY_AES_PREFIX + stationCode + ":iv", bssk.getPrivateKey());
|
||||
jo.set("aesSecretKey", o1.toString());
|
||||
jo.set("aesIv", o2.toString());
|
||||
// jo.set("encryptKey", o1.toString());
|
||||
// jo.set("encryptVector", o2.toString());
|
||||
return jo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 反馈换电步骤
|
||||
|
||||
Loading…
Reference in New Issue
Block a user