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>
<version>1.5.36</version>
</dependency>
</dependencies>
<build>

View File

@ -1,16 +1,15 @@
package com.muyu.vehicle;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.ecs20140526.Client;
import com.aliyun.ecs20140526.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.muyu.common.redis.service.RedisService;
import com.muyu.vehicle.config.SelectInstance;
import com.muyu.vehicle.domain.InstanceInfo;
import com.muyu.vehicle.utils.CreateClient;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
@ -21,6 +20,7 @@ import java.util.ArrayList;
import java.util.List;
@Component
@Log4j2
/**
@ -114,6 +114,7 @@ public class ManageInstance implements ApplicationRunner {
}
@Override
public void run(ApplicationArguments args) throws Exception {
List<String> list = generateInstance();
@ -122,10 +123,11 @@ public class ManageInstance implements ApplicationRunner {
Thread.sleep(30000);
List<InstanceInfo> instanceInfos = SelectInstance.selectInstance(list);
log.info("实例信息查询成功");
// for (InstanceInfo instanceInfo : instanceInfos) {
// redisService.getCacheObject(instanceInfo.getInstanceId());
// }
log.info("实例信息:",instanceInfos);
for (InstanceInfo instanceInfo : instanceInfos) {
redisService.setCacheObject("FourInstanceIdKey:"+instanceInfo.getInstanceId(),instanceInfo);
}
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 lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
@ -18,6 +19,7 @@ import java.util.ArrayList;
/**
*
*/
@Component
@Log4j2
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;
import com.muyu.vehicle.domain.MqttServerModel;
import feign.Client;
import lombok.extern.log4j.Log4j2;
import org.eclipse.paho.client.mqttv3.*;
/**
* fluxMq
*/
@Log4j2
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;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.ecs20140526.Client;
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
import com.aliyun.teautil.Common;
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.utils.CreateClient;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
/**
*
*/
@ -46,8 +55,9 @@ public class SelectInstance {
log.info("实例IP:{}",instanceInfo.getIpAddress());
list.add(instanceInfo);
}
System.out.println("实例信息:"+list);
log.info("实例信息:",list);
return instanceInfos;
return list;
}
}

View File

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

View File

@ -21,8 +21,11 @@ public class MqttServerModel {
*/
private String broker;
/**
* MQTT
*/
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;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -12,8 +15,19 @@ import lombok.NoArgsConstructor;
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "vehicle_connection")
public class VehicleConnectionReq {
/**
* ID
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* VIN
*/
private String vehicleVin;
/**
@ -28,9 +42,17 @@ public class VehicleConnectionReq {
private String username;
/**
*
*/
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;
import com.aliyun.ecs20140526.Client;

View File

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