Merge remote-tracking branch 'origin/dev.vehiclegateway' into dev

dev.business
刘武 2024-10-08 09:03:44 +08:00
commit 2a0bd1178e
20 changed files with 273 additions and 230 deletions

View File

@ -143,6 +143,7 @@
<artifactId>forest-spring-boot-starter</artifactId> <artifactId>forest-spring-boot-starter</artifactId>
<version>1.5.36</version> <version>1.5.36</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,16 +1,15 @@
package com.muyu.vehicle; package com.muyu.vehicle;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.ecs20140526.Client; import com.aliyun.ecs20140526.Client;
import com.aliyun.ecs20140526.models.*; import com.aliyun.ecs20140526.models.*;
import com.aliyun.tea.TeaException; import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common; import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions; import com.aliyun.teautil.models.RuntimeOptions;
import com.muyu.common.redis.service.RedisService; import com.muyu.common.redis.service.RedisService;
import com.muyu.vehicle.config.SelectInstance; import com.muyu.vehicle.config.SelectInstance;
import com.muyu.vehicle.domain.InstanceInfo; import com.muyu.vehicle.domain.InstanceInfo;
import com.muyu.vehicle.utils.CreateClient; import com.muyu.vehicle.utils.CreateClient;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
@ -21,6 +20,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@Component @Component
@Log4j2 @Log4j2
/** /**
@ -30,88 +30,89 @@ public class ManageInstance implements ApplicationRunner {
@Autowired @Autowired
private RedisService redisService; private RedisService redisService;
/** /**
* ID * ID
*/ */
public static final String IMAGE_ID = "m-uf6ffgkry85fwu4znr6s"; public static final String IMAGE_ID = "m-uf6ffgkry85fwu4znr6s";
/** /**
* *
*/ */
public static final String INSTANCE_TYPE = "ecs.e-c1m1.large"; public static final String INSTANCE_TYPE = "ecs.e-c1m1.large";
/** /**
* ID * ID
*/ */
public static final String SECURITY_GROUP_ID = "sg-uf6glo8c4k17szhxu7sk"; public static final String SECURITY_GROUP_ID = "sg-uf6glo8c4k17szhxu7sk";
/** /**
*ID *ID
*/ */
public static final String V_SWITCH_ID = "vsw-uf6xy4rbt9ggcz93t6oib"; public static final String V_SWITCH_ID = "vsw-uf6xy4rbt9ggcz93t6oib";
/** /**
* *
*/ */
public static final String INSTANCE_CHARGE_TY = "PostPaid"; public static final String INSTANCE_CHARGE_TY = "PostPaid";
public static List<String> generateInstance() throws Exception { public static List<String> generateInstance() throws Exception {
// 创建阿里云ECS客户端 // 创建阿里云ECS客户端
// 创建ECS客户端对象用于后续调用ECS相关API // 创建ECS客户端对象用于后续调用ECS相关API
Client client = CreateClient.createClient(); Client client = CreateClient.createClient();
// 配置系统盘参数 // 配置系统盘参数
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk =
new RunInstancesRequest.RunInstancesRequestSystemDisk() new RunInstancesRequest.RunInstancesRequestSystemDisk()
.setSize("40") .setSize("40")
.setCategory("cloud_essd"); .setCategory("cloud_essd");
// 创建创建实例请求对象并设置参数 // 创建创建实例请求对象并设置参数
RunInstancesRequest runInstancesRequest = new RunInstancesRequest() RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
.setRegionId("cn-shanghai") // 设置地域ID .setRegionId("cn-shanghai") // 设置地域ID
.setImageId(IMAGE_ID) // 设置镜像ID .setImageId(IMAGE_ID) // 设置镜像ID
.setInstanceType(INSTANCE_TYPE) // 设置实例类型 .setInstanceType(INSTANCE_TYPE) // 设置实例类型
.setSecurityGroupId(SECURITY_GROUP_ID) // 设置安全组ID .setSecurityGroupId(SECURITY_GROUP_ID) // 设置安全组ID
.setVSwitchId(V_SWITCH_ID) // 设置虚拟交换机ID .setVSwitchId(V_SWITCH_ID) // 设置虚拟交换机ID
.setInstanceName("cloud-MQTT") // 设置实例名称 .setInstanceName("cloud-MQTT") // 设置实例名称
.setInstanceChargeType(INSTANCE_CHARGE_TY) // 设置实例付费类型为后付费按量付费 .setInstanceChargeType(INSTANCE_CHARGE_TY) // 设置实例付费类型为后付费按量付费
.setSystemDisk(systemDisk) // 设置系统盘配置 .setSystemDisk(systemDisk) // 设置系统盘配置
.setHostName("root") // 设置主机名 .setHostName("root") // 设置主机名
.setPassword("2112A-four") // 设置实例密码 .setPassword("2112A-four") // 设置实例密码
.setAmount(2) // 设置创建实例的数量 .setAmount(2) // 设置创建实例的数量
.setInternetChargeType("PayByTraffic") .setInternetChargeType("PayByTraffic")
.setInternetMaxBandwidthOut(1); .setInternetMaxBandwidthOut(1);
//创建运行时选择对象 //创建运行时选择对象
RuntimeOptions runTime = RuntimeOptions runTime =
new RuntimeOptions(); new RuntimeOptions();
// 尝试执行创建实例请求 // 尝试执行创建实例请求
try { try {
ArrayList<String> list = new ArrayList<>(); ArrayList<String> list = new ArrayList<>();
// 复制代码运行请自行打印 API 的返回值 // 复制代码运行请自行打印 API 的返回值
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runTime); RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runTime);
RunInstancesResponseBody body = runInstancesResponse.getBody(); RunInstancesResponseBody body = runInstancesResponse.getBody();
for (String instance : body.getInstanceIdSets().getInstanceIdSet()) { for (String instance : body.getInstanceIdSets().getInstanceIdSet()) {
list.add(instance); list.add(instance);
}
log.info("ESC创建成功,实例ID为:" + list);
return list;
} catch (TeaException error) {
// 错误 message
log.info(error.getMessage());
// 诊断地址
log.info(error.getData().get("Recommend"));
Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
log.info("实例创建失败:" + error.getMessage());
} }
return null; log.info("ESC创建成功,实例ID为:" + list);
return list;
} catch (TeaException error) {
// 错误 message
log.info(error.getMessage());
// 诊断地址
log.info(error.getData().get("Recommend"));
Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
log.info("实例创建失败:" + error.getMessage());
} }
return null;
}
@Override @Override
@ -122,10 +123,11 @@ public class ManageInstance implements ApplicationRunner {
Thread.sleep(30000); Thread.sleep(30000);
List<InstanceInfo> instanceInfos = SelectInstance.selectInstance(list); List<InstanceInfo> instanceInfos = SelectInstance.selectInstance(list);
log.info("实例信息查询成功"); log.info("实例信息查询成功");
// for (InstanceInfo instanceInfo : instanceInfos) { for (InstanceInfo instanceInfo : instanceInfos) {
// redisService.getCacheObject(instanceInfo.getInstanceId()); redisService.setCacheObject("FourInstanceIdKey:"+instanceInfo.getInstanceId(),instanceInfo);
// } }
log.info("实例信息:",instanceInfos); System.out.println("实例信息:"+instanceInfos);
log.info("实例信息:", JSONObject.toJSONString(instanceInfos));
} }
} }

View File

@ -11,6 +11,7 @@ import com.aliyun.teautil.models.RuntimeOptions;
import com.muyu.vehicle.utils.CreateClient; import com.muyu.vehicle.utils.CreateClient;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
@ -18,6 +19,7 @@ import java.util.ArrayList;
/** /**
* *
*/ */
@Component @Component
@Log4j2 @Log4j2
public class CloseInstance implements DisposableBean{ public class CloseInstance implements DisposableBean{
@ -96,4 +98,3 @@ public class CloseInstance implements DisposableBean{
} }
} }

View File

@ -1,8 +1,54 @@
package com.muyu.vehicle.config; package com.muyu.vehicle.config;
import com.muyu.vehicle.domain.MqttServerModel;
import feign.Client;
import lombok.extern.log4j.Log4j2;
import org.eclipse.paho.client.mqttv3.*;
/** /**
* fluxMq * fluxMq
*/ */
@Log4j2
public class ConnectFluxMq { public class ConnectFluxMq {
public void FluxMqConnect(String IP,String vin){
String topic = "car";
String broker = "tcp://"+IP+":1883";
String clientId = vin+"vehicleGateway";
MqttClient client;
try {
//创建
client = new MqttClient(broker,clientId);
//设置连接参数
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
//连接到Broker
client.connect(options);
log.info("Connecting to broker: " + broker);
//连接
client.subscribe(topic,0);
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
} catch (MqttException e) {
throw new RuntimeException(e);
}
}
} }

View File

@ -0,0 +1,41 @@
package com.muyu.vehicle.config;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Log4j2
@Component
public class CreateExchange implements ApplicationRunner {
@Autowired
private ConnectionFactory connectionFactory;
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("=====>开始创建交换机");
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
// 创建Fanout类型的交换机
FanoutExchange exchange = new FanoutExchange("ONLINE_EXCHANGE", true, false);
rabbitAdmin.declareExchange(exchange);
// 创建队列
Queue queue = new Queue("GO_ONLINE", true, false, false);
rabbitAdmin.declareQueue(queue);
// Fanout交换机绑定
rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange));
log.info("=====>交换机创建成功");
log.info("=====>队列创建成功并绑定到交换机");
}
}

View File

@ -1,19 +1,28 @@
package com.muyu.vehicle.config; package com.muyu.vehicle.config;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.ecs20140526.Client; import com.aliyun.ecs20140526.Client;
import com.aliyun.ecs20140526.models.DescribeInstancesRequest; import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
import com.aliyun.ecs20140526.models.DescribeInstancesResponse; import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody; import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
import com.aliyun.teautil.Common; import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions; import com.aliyun.teautil.models.RuntimeOptions;
import com.muyu.common.core.exception.ServiceException;
import com.muyu.common.redis.service.RedisService;
import com.muyu.vehicle.domain.InstanceInfo; import com.muyu.vehicle.domain.InstanceInfo;
import com.muyu.vehicle.utils.CreateClient; import com.muyu.vehicle.utils.CreateClient;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
*
*/
/** /**
* *
*/ */
@ -46,8 +55,9 @@ public class SelectInstance {
log.info("实例IP:{}",instanceInfo.getIpAddress()); log.info("实例IP:{}",instanceInfo.getIpAddress());
list.add(instanceInfo); list.add(instanceInfo);
} }
System.out.println("实例信息:"+list);
log.info("实例信息:",list); log.info("实例信息:",list);
return instanceInfos; return list;
} }
} }

View File

@ -1,8 +1,8 @@
package com.muyu.vehicle.controller; package com.muyu.vehicle.controller;
import com.muyu.common.core.domain.Result; import com.muyu.common.core.domain.Result;
import com.muyu.vehicle.domain.VehicleAuthentication; import com.muyu.vehicle.domain.MqttServerModel;
import com.muyu.vehicle.domain.req.VehicleConnectionReq; import com.muyu.vehicle.domain.req.VehicleConnectionReq;
import com.muyu.vehicle.service.VehicleAuthenticationService; import com.muyu.vehicle.service.VehicleConnectService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -13,15 +13,14 @@ import org.springframework.web.bind.annotation.*;
public class CarInstanceController { public class CarInstanceController {
@Autowired @Autowired
private VehicleAuthenticationService vehicleAuthenticationService; private VehicleConnectService vehicleConnectService;
@PostMapping("/receiveMsg") @PostMapping("/receiveMsg")
public Result receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){ public Result receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
log.info("=======>"+vehicleConnectionReq); log.info("=======>"+vehicleConnectionReq);
VehicleAuthentication vehicleAuthentication = VehicleAuthentication.buildVehicle(vehicleConnectionReq); Result<MqttServerModel> connect = vehicleConnectService.getConnect(vehicleConnectionReq);
Integer i = vehicleAuthenticationService.insertVehicleAuthentication(vehicleAuthentication); return Result.success(connect);
return i>0?Result.success():Result.error();
} }
} }

View File

@ -21,8 +21,11 @@ public class MqttServerModel {
*/ */
private String broker; private String broker;
/** /**
* MQTT * MQTT
*/ */
private String topic; private String topic;
} }

View File

@ -1,51 +0,0 @@
package com.muyu.vehicle.domain;
import com.muyu.vehicle.domain.req.VehicleConnectionReq;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
*
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class VehicleAuthentication {
/**
* ID
*/
private Integer id;
/**
* VIN
*/
private String vin;
/**
*
*/
private String userName;
/**
*
*/
private String password;
public static VehicleAuthentication buildVehicle(VehicleConnectionReq req){
return builder()
.vin(req.getVehicleVin())
.userName(req.getUsername())
.password((req.getVehicleVin()+req.getTimestamp()+req.getNonce()))
.build();
}
}

View File

@ -1,5 +1,8 @@
package com.muyu.vehicle.domain.req; package com.muyu.vehicle.domain.req;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@ -12,8 +15,19 @@ import lombok.NoArgsConstructor;
@Builder @Builder
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@TableName(value = "vehicle_connection")
public class VehicleConnectionReq { public class VehicleConnectionReq {
/**
* ID
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* VIN
*/
private String vehicleVin; private String vehicleVin;
/** /**
@ -28,9 +42,17 @@ public class VehicleConnectionReq {
private String username; private String username;
/** /**
* *
*/ */
private String nonce; private String nonce;
/**
*
*/
private String password;
} }

View File

@ -1,9 +0,0 @@
package com.muyu.vehicle.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.muyu.vehicle.domain.VehicleAuthentication;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface VehicleAuthenticationMapper extends BaseMapper<VehicleAuthentication> {
}

View File

@ -0,0 +1,12 @@
package com.muyu.vehicle.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.muyu.vehicle.domain.req.VehicleConnectionReq;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface VehicleConnectMapper extends BaseMapper<VehicleConnectionReq> {
VehicleConnectionReq selectByVehicleVin(String vin);
}

View File

@ -1,19 +0,0 @@
package com.muyu.vehicle.service;
import com.muyu.common.core.domain.Result;
import com.muyu.vehicle.domain.MqttServerModel;
import com.muyu.vehicle.domain.req.VehicleConnectionReq;
/**
*
*/
public interface CarInstanceService {
Result<MqttServerModel> getConnect(VehicleConnectionReq carConnectionReq);
/**
*
*/
void carClientStart(String vin);
}

View File

@ -1,10 +0,0 @@
package com.muyu.vehicle.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.common.domain.DataType;
import com.muyu.vehicle.domain.VehicleAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
public interface VehicleAuthenticationService extends IService<VehicleAuthentication> {
Integer insertVehicleAuthentication(VehicleAuthentication vehicleAuthentication);
}

View File

@ -0,0 +1,12 @@
package com.muyu.vehicle.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.common.core.domain.Result;
import com.muyu.vehicle.domain.MqttServerModel;
import com.muyu.vehicle.domain.req.VehicleConnectionReq;
public interface VehicleConnectService extends IService<VehicleConnectionReq> {
Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq);
}

View File

@ -1,37 +0,0 @@
package com.muyu.vehicle.service.impl;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.uuid.UUID;
import com.muyu.vehicle.domain.MqttServerModel;
import com.muyu.vehicle.domain.req.VehicleConnectionReq;
import com.muyu.vehicle.service.CarInstanceService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
/**
*
*/
@Service
@Log4j2
public class CarInstanceServiceImpl implements CarInstanceService {
@Override
public Result<MqttServerModel> getConnect(VehicleConnectionReq carConnectionReq) {
return null;
}
@Override
public void carClientStart(String vin) {
String timestamp = String.valueOf(System.currentTimeMillis());
VehicleConnectionReq carConnectionReq = VehicleConnectionReq.builder()
.vehicleVin(vin)
.timestamp(timestamp)
.username(vin + timestamp)
.nonce(UUID.fastUUID().toString().replaceAll("-", ""))
.build();
//获取网关节点信息
}
}

View File

@ -1,19 +0,0 @@
package com.muyu.vehicle.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.vehicle.domain.VehicleAuthentication;
import com.muyu.vehicle.mapper.VehicleAuthenticationMapper;
import com.muyu.vehicle.service.VehicleAuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class VehicleAuthenticationServiceImpl extends ServiceImpl<VehicleAuthenticationMapper, VehicleAuthentication> implements VehicleAuthenticationService {
@Autowired
private VehicleAuthenticationMapper vehicleAuthenticationMapper;
@Override
public Integer insertVehicleAuthentication(VehicleAuthentication vehicleAuthentication) {
return vehicleAuthenticationMapper.insert(vehicleAuthentication);
}
}

View File

@ -0,0 +1,36 @@
package com.muyu.vehicle.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.domain.Result;
import com.muyu.vehicle.domain.MqttServerModel;
import com.muyu.vehicle.domain.req.VehicleConnectionReq;
import com.muyu.vehicle.mapper.VehicleConnectMapper;
import com.muyu.vehicle.service.VehicleConnectService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Log4j2
public class VehicleConnectServiceImpl extends ServiceImpl<VehicleConnectMapper, VehicleConnectionReq> implements VehicleConnectService {
@Autowired
private VehicleConnectMapper vehicleConnectMapper;
@Override
public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
log.info("车辆连接信息:{}", vehicleConnectionReq);
//生成密码
vehicleConnectionReq.setPassword(vehicleConnectionReq.getVehicleVin() + vehicleConnectionReq.getTimestamp()
+ vehicleConnectionReq.getNonce());
VehicleConnectionReq connection = vehicleConnectMapper.selectByVehicleVin(vehicleConnectionReq.getVehicleVin());
if (connection==null){
vehicleConnectMapper.insert(vehicleConnectionReq);
log.info("车辆预上线成功");
}else {
log.info("车辆已预上线成功,禁止重复");
}
return null;
}
}

View File

@ -1,3 +1,4 @@
package com.muyu.vehicle.utils; package com.muyu.vehicle.utils;
import com.aliyun.ecs20140526.Client; import com.aliyun.ecs20140526.Client;

View File

@ -7,10 +7,12 @@ nacos:
addr: 47.101.53.251:8848 addr: 47.101.53.251:8848
user-name: nacos user-name: nacos
password: nacos password: nacos
namespace: four namespace: sx
# Spring # Spring
spring: spring:
main:
allow-bean-definition-overriding: true
application: application:
# 应用名称 # 应用名称
name: cloud-vehicleGateway name: cloud-vehicleGateway