From 2235097f051628748801c73d025852820b7ef0fe Mon Sep 17 00:00:00 2001 From: SuiXxx <1752599835@qq.com> Date: Mon, 7 Oct 2024 21:39:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat():=E8=B4=A6=E5=8F=B7=E5=AF=86=E7=A0=81?= =?UTF-8?q?=E5=AD=98=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cloud-modules-vehiclegateway/pom.xml | 9 +- .../java/com/muyu/vehicle/ManageInstance.java | 154 +++++++++--------- .../muyu/vehicle/config/CloseInstance.java | 3 +- .../muyu/vehicle/config/ConnectFluxMq.java | 46 ++++++ .../muyu/vehicle/config/CreateExchange.java | 41 +++++ .../muyu/vehicle/config/SelectInstance.java | 9 + .../controller/CarInstanceController.java | 11 +- .../muyu/vehicle/domain/MqttServerModel.java | 3 + .../vehicle/domain/VehicleAuthentication.java | 51 ------ .../domain/req/VehicleConnectionReq.java | 22 +++ .../mapper/VehicleAuthenticationMapper.java | 9 - .../vehicle/mapper/VehicleConnectMapper.java | 12 ++ .../vehicle/service/CarInstanceService.java | 19 --- .../service/VehicleAuthenticationService.java | 10 -- .../service/VehicleConnectService.java | 12 ++ .../service/impl/CarInstanceServiceImpl.java | 37 ----- .../VehicleAuthenticationServiceImpl.java | 19 --- .../impl/VehicleConnectServiceImpl.java | 36 ++++ .../com/muyu/vehicle/utils/CreateClient.java | 1 + 19 files changed, 272 insertions(+), 232 deletions(-) create mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CreateExchange.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/VehicleAuthentication.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleAuthenticationMapper.java create mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleConnectMapper.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/CarInstanceService.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleAuthenticationService.java create mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleConnectService.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/CarInstanceServiceImpl.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleAuthenticationServiceImpl.java create mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleConnectServiceImpl.java diff --git a/cloud-modules/cloud-modules-vehiclegateway/pom.xml b/cloud-modules/cloud-modules-vehiclegateway/pom.xml index 33e4264..b4a45f4 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/pom.xml +++ b/cloud-modules/cloud-modules-vehiclegateway/pom.xml @@ -86,10 +86,10 @@ - - com.muyu - cloud-common-rabbit - + + + + org.springframework.boot @@ -143,6 +143,7 @@ forest-spring-boot-starter 1.5.36 + diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/ManageInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/ManageInstance.java index 51e009b..c8457c5 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/ManageInstance.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/ManageInstance.java @@ -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 /** @@ -30,88 +30,89 @@ public class ManageInstance implements ApplicationRunner { @Autowired private RedisService redisService; - /** - * 镜像ID - */ - public static final String IMAGE_ID = "m-uf6ffgkry85fwu4znr6s"; + /** + * 镜像ID + */ + 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 - */ - public static final String SECURITY_GROUP_ID = "sg-uf6glo8c4k17szhxu7sk"; + /** + * 安全组ID + */ + public static final String SECURITY_GROUP_ID = "sg-uf6glo8c4k17szhxu7sk"; - /** - *交换机ID - */ - public static final String V_SWITCH_ID = "vsw-uf6xy4rbt9ggcz93t6oib"; + /** + *交换机ID + */ + 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 generateInstance() throws Exception { - // 创建阿里云ECS客户端 - // 创建ECS客户端对象,用于后续调用ECS相关API - Client client = CreateClient.createClient(); - // 配置系统盘参数 - RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = - new RunInstancesRequest.RunInstancesRequestSystemDisk() - .setSize("40") - .setCategory("cloud_essd"); + public static List generateInstance() throws Exception { + // 创建阿里云ECS客户端 + // 创建ECS客户端对象,用于后续调用ECS相关API + Client client = CreateClient.createClient(); + // 配置系统盘参数 + RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = + new RunInstancesRequest.RunInstancesRequestSystemDisk() + .setSize("40") + .setCategory("cloud_essd"); - // 创建创建实例请求对象并设置参数 + // 创建创建实例请求对象并设置参数 - RunInstancesRequest runInstancesRequest = new RunInstancesRequest() - .setRegionId("cn-shanghai") // 设置地域ID - .setImageId(IMAGE_ID) // 设置镜像ID - .setInstanceType(INSTANCE_TYPE) // 设置实例类型 - .setSecurityGroupId(SECURITY_GROUP_ID) // 设置安全组ID - .setVSwitchId(V_SWITCH_ID) // 设置虚拟交换机ID - .setInstanceName("cloud-MQTT") // 设置实例名称 - .setInstanceChargeType(INSTANCE_CHARGE_TY) // 设置实例付费类型为后付费按量付费 - .setSystemDisk(systemDisk) // 设置系统盘配置 - .setHostName("root") // 设置主机名 - .setPassword("2112A-four") // 设置实例密码 - .setAmount(2) // 设置创建实例的数量 - .setInternetChargeType("PayByTraffic") - .setInternetMaxBandwidthOut(1); + RunInstancesRequest runInstancesRequest = new RunInstancesRequest() + .setRegionId("cn-shanghai") // 设置地域ID + .setImageId(IMAGE_ID) // 设置镜像ID + .setInstanceType(INSTANCE_TYPE) // 设置实例类型 + .setSecurityGroupId(SECURITY_GROUP_ID) // 设置安全组ID + .setVSwitchId(V_SWITCH_ID) // 设置虚拟交换机ID + .setInstanceName("cloud-MQTT") // 设置实例名称 + .setInstanceChargeType(INSTANCE_CHARGE_TY) // 设置实例付费类型为后付费按量付费 + .setSystemDisk(systemDisk) // 设置系统盘配置 + .setHostName("root") // 设置主机名 + .setPassword("2112A-four") // 设置实例密码 + .setAmount(2) // 设置创建实例的数量 + .setInternetChargeType("PayByTraffic") + .setInternetMaxBandwidthOut(1); - //创建运行时选择对象 - RuntimeOptions runTime = - new RuntimeOptions(); - // 尝试执行创建实例请求 - try { - ArrayList list = new ArrayList<>(); - // 复制代码运行请自行打印 API 的返回值 - RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runTime); - RunInstancesResponseBody body = runInstancesResponse.getBody(); - for (String instance : body.getInstanceIdSets().getInstanceIdSet()) { - 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()); + //创建运行时选择对象 + RuntimeOptions runTime = + new RuntimeOptions(); + // 尝试执行创建实例请求 + try { + ArrayList list = new ArrayList<>(); + // 复制代码运行请自行打印 API 的返回值 + RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runTime); + RunInstancesResponseBody body = runInstancesResponse.getBody(); + for (String instance : body.getInstanceIdSets().getInstanceIdSet()) { + list.add(instance); } - 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 @@ -122,10 +123,11 @@ public class ManageInstance implements ApplicationRunner { Thread.sleep(30000); List 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)); } } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java index d8ad297..dd6ab92 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java @@ -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{ } } - diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/ConnectFluxMq.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/ConnectFluxMq.java index 44fa9e6..fbe5ae6 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/ConnectFluxMq.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/ConnectFluxMq.java @@ -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); + } + } + } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CreateExchange.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CreateExchange.java new file mode 100644 index 0000000..eb7436b --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CreateExchange.java @@ -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("=====>队列创建成功并绑定到交换机"); + } +} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java index c6645c2..71a3643 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java @@ -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; +/** + * 查询实例信息 + */ + /** * 查询实例信息 */ diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/controller/CarInstanceController.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/controller/CarInstanceController.java index 4c90f15..0ba1477 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/controller/CarInstanceController.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/controller/CarInstanceController.java @@ -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 connect = vehicleConnectService.getConnect(vehicleConnectionReq); + return Result.success(connect); } } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/MqttServerModel.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/MqttServerModel.java index c6009ae..4436afb 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/MqttServerModel.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/MqttServerModel.java @@ -21,8 +21,11 @@ public class MqttServerModel { */ private String broker; + /** * MQTT订阅主题 */ private String topic; + + } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/VehicleAuthentication.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/VehicleAuthentication.java deleted file mode 100644 index 1a3a95e..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/VehicleAuthentication.java +++ /dev/null @@ -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(); - } - - - -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/req/VehicleConnectionReq.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/req/VehicleConnectionReq.java index e47991c..5a984e1 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/req/VehicleConnectionReq.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/req/VehicleConnectionReq.java @@ -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; + + } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleAuthenticationMapper.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleAuthenticationMapper.java deleted file mode 100644 index 0777888..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleAuthenticationMapper.java +++ /dev/null @@ -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 { -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleConnectMapper.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleConnectMapper.java new file mode 100644 index 0000000..50af952 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleConnectMapper.java @@ -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 selectByVehicleVin(String vin); + +} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/CarInstanceService.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/CarInstanceService.java deleted file mode 100644 index cb519b4..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/CarInstanceService.java +++ /dev/null @@ -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 getConnect(VehicleConnectionReq carConnectionReq); - - /** - * 车辆初始化 - */ - void carClientStart(String vin); - -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleAuthenticationService.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleAuthenticationService.java deleted file mode 100644 index 6a4833e..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleAuthenticationService.java +++ /dev/null @@ -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 { - Integer insertVehicleAuthentication(VehicleAuthentication vehicleAuthentication); -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleConnectService.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleConnectService.java new file mode 100644 index 0000000..cf276da --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleConnectService.java @@ -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 { + + Result getConnect(VehicleConnectionReq vehicleConnectionReq); + +} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/CarInstanceServiceImpl.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/CarInstanceServiceImpl.java deleted file mode 100644 index e5f57a6..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/CarInstanceServiceImpl.java +++ /dev/null @@ -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 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(); - - //获取网关节点信息 - } -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleAuthenticationServiceImpl.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleAuthenticationServiceImpl.java deleted file mode 100644 index e85b35b..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleAuthenticationServiceImpl.java +++ /dev/null @@ -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 implements VehicleAuthenticationService { - @Autowired - private VehicleAuthenticationMapper vehicleAuthenticationMapper; - @Override - public Integer insertVehicleAuthentication(VehicleAuthentication vehicleAuthentication) { - return vehicleAuthenticationMapper.insert(vehicleAuthentication); - } -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleConnectServiceImpl.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleConnectServiceImpl.java new file mode 100644 index 0000000..340c273 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleConnectServiceImpl.java @@ -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 implements VehicleConnectService { + @Autowired + private VehicleConnectMapper vehicleConnectMapper; + + @Override + public Result 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; + } +} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/utils/CreateClient.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/utils/CreateClient.java index 092215a..abb7023 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/utils/CreateClient.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/utils/CreateClient.java @@ -1,3 +1,4 @@ + package com.muyu.vehicle.utils; import com.aliyun.ecs20140526.Client; From cd5e46b6503d442a5d37db2aabc690aecd615d42 Mon Sep 17 00:00:00 2001 From: SuiXxx <1752599835@qq.com> Date: Mon, 7 Oct 2024 21:51:04 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat():=E9=A1=B9=E7=9B=AE=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E4=BA=A4=E6=8D=A2=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud-modules/cloud-modules-vehiclegateway/pom.xml | 8 ++++---- .../main/java/com/muyu/vehicle/config/SelectInstance.java | 3 ++- .../src/main/resources/bootstrap.yml | 4 +++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cloud-modules/cloud-modules-vehiclegateway/pom.xml b/cloud-modules/cloud-modules-vehiclegateway/pom.xml index b4a45f4..112b41e 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/pom.xml +++ b/cloud-modules/cloud-modules-vehiclegateway/pom.xml @@ -86,10 +86,10 @@ - - - - + + com.muyu + cloud-common-rabbit + org.springframework.boot diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java index 71a3643..e2048d7 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java @@ -55,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; } } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/resources/bootstrap.yml b/cloud-modules/cloud-modules-vehiclegateway/src/main/resources/bootstrap.yml index 8ac2666..7739f36 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/resources/bootstrap.yml +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/resources/bootstrap.yml @@ -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