diff --git a/cloud-modules/cloud-modules-vehiclegateway/pom.xml b/cloud-modules/cloud-modules-vehiclegateway/pom.xml index 34448dd..33e4264 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/pom.xml +++ b/cloud-modules/cloud-modules-vehiclegateway/pom.xml @@ -131,7 +131,18 @@ 4.2.0 + + com.muyu.server + saas-server + 3.6.3 + + + + com.dtflys.forest + forest-spring-boot-starter + 1.5.36 + diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/VehicleGatewayApplication.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/VehicleGatewayApplication.java index 62108bf..884f25f 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/VehicleGatewayApplication.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/VehicleGatewayApplication.java @@ -6,6 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableMyFeignClients + public class VehicleGatewayApplication { public static void main(String[] args) { SpringApplication.run(VehicleGatewayApplication.class,args); 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 272ab93..51e009b 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,15 +1,16 @@ package com.muyu.vehicle; - 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.service.OpenInstance; -import com.muyu.vehicle.service.SelectInstance; +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; @@ -19,155 +20,113 @@ import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; + @Component @Log4j2 +/** + * 项目启动创建实例 + */ public class ManageInstance implements ApplicationRunner { + @Autowired + private RedisService redisService; - /** - * ACCESS_KEY_ID - */ - public static final String ALIBABA_CLOUD_ACCESS_KEY_ID="LTAI5tGabdxedjfCh2uXHNrw"; + /** + * 镜像ID + */ + public static final String IMAGE_ID = "m-uf6ffgkry85fwu4znr6s"; - /** - *ACCESS_KEY_SECRET - */ - public static final String ACCESS_KEY_SECRET="NHb7wHVpesLW6Axc0bFBs6ThhuNR10"; + /** + * 实例类型 + */ + 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 V_SWITCH_ID = "vsw-uf6xy4rbt9ggcz93t6oib"; - /** - * 镜像ID - */ - public static final String IMAGE_ID="m-uf6agr9i6g27gj23om34"; + /** + * 实例付费类型 + */ + public static final String INSTANCE_CHARGE_TY = "PostPaid"; - /** - * 实例类型 - */ - public static final String INSTANCE_TYPE="ecs.e-c1m1.large"; + 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"); - /** - * 安全组ID - */ - public static final String SECURITY_GROUP_ID="sg-uf6glo8c4k17szhxu7sk"; + // 创建创建实例请求对象并设置参数 - /** - *交换机ID - */ - public static final String V_SWITCH_ID="vsw-uf6xy4rbt9ggcz93t6oib"; + 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); - /** - * 实例付费类型 - */ - public static final String INSTANCE_CHARGE_TY="PostPaid"; - - - - /** - * 使用AK&SK初始化账号Client - * @return Client - * @throws Exception - */ - - public static Client createClient() throws Exception { - // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 - Config config = new Config() - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 - .setAccessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID) - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 - .setAccessKeySecret(ACCESS_KEY_SECRET); - // Endpoint 请参考 https://api.aliyun.com/product/Ecs - config.endpoint = "ecs-cn-hangzhou.aliyuncs.com"; - return new com.aliyun.ecs20140526.Client(config); - } - - - public static void generateInstance() throws Exception { - // 创建阿里云ECS客户端 - Client client = ManageInstance.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); - - - //创建运行时选择对象 - 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); + //创建运行时选择对象 + 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()); } - log.info("ESC创建成功,实例ID为:" + 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 + public void run(ApplicationArguments args) throws Exception { + List list = generateInstance(); + log.info("创建实例成功"); + log.info("正在加载实例"); + Thread.sleep(30000); + List instanceInfos = SelectInstance.selectInstance(list); + log.info("实例信息查询成功"); +// for (InstanceInfo instanceInfo : instanceInfos) { +// redisService.getCacheObject(instanceInfo.getInstanceId()); +// } + log.info("实例信息:",instanceInfos); } } - private static List selectInstance() throws Exception { - Client client = ManageInstance.createClient(); - ArrayList instanceInfos = new ArrayList<>();// 实例基础信息 - DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() - .setRegionId("cn-shanghai") - .setInternetChargeType("PayByTraffic") - .setInstanceChargeType("PostPaid") - .setInstanceName("cloud-MQTT") // 设置实例名称 - ; - // 创建运行时选项对象 - RuntimeOptions runtime = new RuntimeOptions(); - //实例ID Instances.Instance.InstanceId - //实例IP Instances.Instance.PublicIpAddress.IpAddress - //状态 Instances.Instance.Status - DescribeInstancesResponse resp =client.describeInstancesWithOptions(describeInstancesRequest, runtime); - DescribeInstancesResponseBody body = resp.getBody(); - - for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()){ - InstanceInfo instanceInfo = new InstanceInfo(); - instanceInfo.setInstanceId(instance.getInstanceId()); - instanceInfo.setIpAddress(String.valueOf(instance.getPublicIpAddress().getIpAddress())); - instanceInfo.setStatus(instance.getStatus()); - instanceInfos.add(instanceInfo); - - } - log.info("实例信息为:"+Common.toJSONString(instanceInfos)); - return instanceInfos; - } - - @Override - public void run(ApplicationArguments args) throws Exception { - generateInstance(); - selectInstance(); - } -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/CloseInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java similarity index 73% rename from cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/CloseInstance.java rename to cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java index 8843c7c..d8ad297 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/CloseInstance.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java @@ -1,6 +1,4 @@ -package com.muyu.vehicle.service; - - +package com.muyu.vehicle.config; import com.aliyun.ecs20140526.Client; import com.aliyun.ecs20140526.models.DeleteInstancesRequest; import com.aliyun.ecs20140526.models.DescribeInstancesRequest; @@ -10,42 +8,31 @@ 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.vehicle.utils.CreateClient; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.DisposableBean; import org.springframework.stereotype.Component; import java.util.ArrayList; - +/** + * 删除实例信息 + */ @Component @Log4j2 -public class CloseInstance implements DisposableBean { - /** - * ACCESS_KEY_ID - */ - public static final String ALIBABA_CLOUD_ACCESS_KEY_ID="LTAI5tGabdxedjfCh2uXHNrw"; +public class CloseInstance implements DisposableBean{ /** - *ACCESS_KEY_SECRET + * description : + *

使用AK&SK初始化账号Client

+ * @return Client + * + * @throws Exception */ - public static final String ACCESS_KEY_SECRET="NHb7wHVpesLW6Axc0bFBs6ThhuNR10"; - public static Client createClient() throws Exception { - // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 - // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。 - Config config = new Config() - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 - .setAccessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID) - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 - .setAccessKeySecret(ACCESS_KEY_SECRET); - // Endpoint 请参考 https://api.aliyun.com/product/Ecs - config.endpoint = "ecs.cn-shanghai.aliyuncs.com"; - return new Client(config); - } - public static void delInstance() throws Exception { // 创建ECS客户端对象,用于后续调用ECS相关API - Client client = CloseInstance.createClient(); + Client client = CreateClient.createClient(); DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() .setRegionId("cn-shanghai"); @@ -61,7 +48,7 @@ public class CloseInstance implements DisposableBean { DescribeInstancesResponseBody body = describeInstancesResponse.getBody(); for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()) { - if (!instance.getInstanceId().equals("i-uf68jwsbbqq4b4xc893s")){ + if (!instance.getInstanceId().equals("i-uf68jwsbbqq4b4xc893s")) { list.add(instance.getInstanceId()); } } @@ -79,7 +66,7 @@ public class CloseInstance implements DisposableBean { .setInstanceId(list); // 创建运行时选项对象,用于配置运行时的选项参数 - RuntimeOptions runtime = new RuntimeOptions(); + RuntimeOptions runtime = new RuntimeOptions(); try { // 复制代码运行请自行打印 API 的返回值 client.deleteInstancesWithOptions(deleteInstancesRequest, runtime); @@ -108,3 +95,5 @@ public class CloseInstance implements DisposableBean { delInstance(); } } + + 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 new file mode 100644 index 0000000..44fa9e6 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/ConnectFluxMq.java @@ -0,0 +1,8 @@ +package com.muyu.vehicle.config; + +/** + * 链接fluxMq + */ +public class ConnectFluxMq { + +} 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 new file mode 100644 index 0000000..c6645c2 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java @@ -0,0 +1,53 @@ +package com.muyu.vehicle.config; + +import com.alibaba.fastjson.JSON; +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.vehicle.domain.InstanceInfo; +import com.muyu.vehicle.utils.CreateClient; +import lombok.extern.log4j.Log4j2; + +import java.util.ArrayList; +import java.util.List; + +/** + * 查询实例信息 + */ + +@Log4j2 +public class SelectInstance { + public static List selectInstance(List instanceIds) throws Exception { + // 创建ECS客户端对象,用于后续调用ECS相关API + Client client = CreateClient.createClient(); + ArrayList instanceInfos = new ArrayList<>();// 实例基础信息 + com.aliyun.ecs20140526.models.DescribeInstancesRequest describeInstancesRequest = new com.aliyun.ecs20140526.models.DescribeInstancesRequest() + .setInstanceIds(JSON.toJSONString(instanceIds)) + .setRegionId("cn-shanghai"); + // 创建运行时选项对象 + RuntimeOptions runtime = new RuntimeOptions(); + //实例ID Instances.Instance.InstanceId + //实例IP Instances.Instance.PublicIpAddress.IpAddress + //状态 Instances.Instance.Status + DescribeInstancesResponse resp = client.describeInstancesWithOptions(describeInstancesRequest, runtime); + DescribeInstancesResponseBody body = resp.getBody(); + + ArrayList list = new ArrayList<>(); + for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()){ + InstanceInfo instanceInfo = new InstanceInfo(); + instanceInfo.setInstanceId(instance.getInstanceId()); + log.info("实例ID:{}",instanceInfo.getInstanceId()); + instanceInfo.setStatus(instance.getStatus()); + log.info("实例状态:{}",instanceInfo.getStatus()); + instanceInfo.setIpAddress(String.valueOf(instance.getPublicIpAddress().getIpAddress())); + log.info("实例IP:{}",instanceInfo.getIpAddress()); + list.add(instanceInfo); + } + log.info("实例信息:",list); + return instanceInfos; + } + +} 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 new file mode 100644 index 0000000..4c90f15 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/controller/CarInstanceController.java @@ -0,0 +1,27 @@ +package com.muyu.vehicle.controller; +import com.muyu.common.core.domain.Result; +import com.muyu.vehicle.domain.VehicleAuthentication; +import com.muyu.vehicle.domain.req.VehicleConnectionReq; +import com.muyu.vehicle.service.VehicleAuthenticationService; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +@Log4j2 +@RestController +@RequestMapping("/carInstance") +public class CarInstanceController { + + @Autowired + private VehicleAuthenticationService vehicleAuthenticationService; + + + @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(); + } + +} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/FluxMqProperties.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/FluxMqProperties.java new file mode 100644 index 0000000..9d20965 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/FluxMqProperties.java @@ -0,0 +1,40 @@ +package com.muyu.vehicle.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * fluxMq配置 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class FluxMqProperties { + + + /** + * 节点 + */ + private String broker; + + /** + * 主题 + */ + private String topic; + /** + * 密码 + */ + private String password; + + /** + * 节点ID + */ + private String clientId; + + + + +} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/InstanceInfo.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/InstanceInfo.java index 2b4a528..a3b9192 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/InstanceInfo.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/InstanceInfo.java @@ -4,6 +4,9 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +/** + * 网关实例信息 + */ @Data @AllArgsConstructor @NoArgsConstructor 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 new file mode 100644 index 0000000..c6009ae --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/MqttServerModel.java @@ -0,0 +1,28 @@ +package com.muyu.vehicle.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * Mqtt服务器模型 + * @author YunFei.Du + * @date 22:08 2024/5/29 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class MqttServerModel { + + /** + * MQTT服务节点 + */ + 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 new file mode 100644 index 0000000..1a3a95e --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/VehicleAuthentication.java @@ -0,0 +1,51 @@ +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 new file mode 100644 index 0000000..e47991c --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/domain/req/VehicleConnectionReq.java @@ -0,0 +1,36 @@ +package com.muyu.vehicle.domain.req; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 车辆连接信息 + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class VehicleConnectionReq { + + private String vehicleVin; + + /** + * 时间戳 + */ + + private String timestamp; + + /** + * 用户名 + */ + + private String username; + + /** + * 随机字符串 + */ + private String nonce; + +} 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 new file mode 100644 index 0000000..0777888 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleAuthenticationMapper.java @@ -0,0 +1,9 @@ +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/service/CarInstanceService.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/CarInstanceService.java new file mode 100644 index 0000000..cb519b4 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/CarInstanceService.java @@ -0,0 +1,19 @@ +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/OpenInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/OpenInstance.java deleted file mode 100644 index 2a6cb6e..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/OpenInstance.java +++ /dev/null @@ -1,129 +0,0 @@ -package com.muyu.vehicle.service; - -import com.aliyun.ecs20140526.Client; -import com.aliyun.ecs20140526.models.RunInstancesRequest; -import com.aliyun.ecs20140526.models.RunInstancesResponse; -import com.aliyun.ecs20140526.models.RunInstancesResponseBody; -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.vehicle.ManageInstance; -import lombok.extern.log4j.Log4j2; - -import java.util.ArrayList; - -@Log4j2 -public class OpenInstance { - /** - * ACCESS_KEY_ID - */ - public static final String ALIBABA_CLOUD_ACCESS_KEY_ID="LTAI5tGabdxedjfCh2uXHNrw"; - - /** - *ACCESS_KEY_SECRET - */ - public static final String ACCESS_KEY_SECRET="NHb7wHVpesLW6Axc0bFBs6ThhuNR10"; - - - /** - * 镜像ID - */ - public static final String IMAGE_ID="m-uf6agr9i6g27gj23om34"; - - /** - * 实例类型 - */ - 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 V_SWITCH_ID="vsw-uf6xy4rbt9ggcz93t6oib"; - - - /** - * 实例付费类型 - */ - public static final String INSTANCE_CHARGE_TY="PostPaid"; - - - - /** - * 使用AK&SK初始化账号Client - * @return Client - * @throws Exception - */ - - public static Client createClient() throws Exception { - // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 - Config config = new Config() - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 - .setAccessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID) - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 - .setAccessKeySecret(ACCESS_KEY_SECRET); - // Endpoint 请参考 https://api.aliyun.com/product/Ecs - config.endpoint = "ecs-cn-hangzhou.aliyuncs.com"; - return new com.aliyun.ecs20140526.Client(config); - } - - - public static void generateInstance() throws Exception { - // 创建阿里云ECS客户端 - Client client = OpenInstance.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); - - - //创建运行时选择对象 - 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); - } - } 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()); - } - } -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/SelectInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/SelectInstance.java deleted file mode 100644 index 6340b52..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/SelectInstance.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.muyu.vehicle.service; - -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.teaopenapi.models.Config; -import com.aliyun.teautil.Common; -import com.aliyun.teautil.models.RuntimeOptions; -import com.muyu.vehicle.domain.InstanceInfo; -import lombok.extern.log4j.Log4j2; - -import java.util.ArrayList; - -@Log4j2 -public class SelectInstance { - /** - * ACCESS_KEY_ID - */ - public static final String ALIBABA_CLOUD_ACCESS_KEY_ID="LTAI5tGabdxedjfCh2uXHNrw"; - - /** - *ACCESS_KEY_SECRET - */ - public static final String ACCESS_KEY_SECRET="NHb7wHVpesLW6Axc0bFBs6ThhuNR10"; - - public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception { - Config config = new com.aliyun.teaopenapi.models.Config() - // 必填,您的 AccessKey ID - .setAccessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID) - // 必填,您的 AccessKey Secret - .setAccessKeySecret(ACCESS_KEY_SECRET); - // 访问的域名 - config.endpoint = "ecs-cn-hangzhou.aliyuncs.com"; - return new Client(config); - } - - public static void main(String[] args_) throws Exception { - java.util.List args = java.util.Arrays.asList(args_); - // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 - // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式 - Client client = SelectInstance.createClient(ALIBABA_CLOUD_ACCESS_KEY_ID, ACCESS_KEY_SECRET); - DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() - .setRegionId("cn-shanghai") - .setInternetChargeType("PayByTraffic") - .setInstanceChargeType("PostPaid") - .setInstanceName("cloud-MQTT") // 设置实例名称 - ; - //实例ID Instances.Instance.InstanceId - //实例IP Instances.Instance.PublicIpAddress.IpAddress - //状态 Instances.Instance.Status - RuntimeOptions runtime = new RuntimeOptions(); - DescribeInstancesResponse resp = client.describeInstancesWithOptions(describeInstancesRequest, runtime); - DescribeInstancesResponseBody body = resp.getBody(); - ArrayList instanceInfos = new ArrayList<>();// 实例基础信息 - for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()){ - - InstanceInfo instanceInfo = new InstanceInfo(); - instanceInfo.setInstanceId(instance.getInstanceId()); - instanceInfo.setIpAddress(String.valueOf(instance.getPublicIpAddress().getIpAddress())); - instanceInfo.setStatus(instance.getStatus()); - instanceInfos.add(instanceInfo); - } - log.info(Common.toJSONString(instanceInfos)); - } -} 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 new file mode 100644 index 0000000..6a4833e --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleAuthenticationService.java @@ -0,0 +1,10 @@ +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/impl/CarInstanceServiceImpl.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/CarInstanceServiceImpl.java new file mode 100644 index 0000000..e5f57a6 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/CarInstanceServiceImpl.java @@ -0,0 +1,37 @@ +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 new file mode 100644 index 0000000..e85b35b --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleAuthenticationServiceImpl.java @@ -0,0 +1,19 @@ +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/utils/CreateClient.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/utils/CreateClient.java new file mode 100644 index 0000000..092215a --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/utils/CreateClient.java @@ -0,0 +1,32 @@ +package com.muyu.vehicle.utils; + +import com.aliyun.ecs20140526.Client; +import com.aliyun.teaopenapi.models.Config; + +/** + * 创建ECS客户端对象 + */ +public class CreateClient { + /** + * ACCESS_KEY_ID + */ + public static final String ALIBABA_CLOUD_ACCESS_KEY_ID = "LTAI5tGabdxedjfCh2uXHNrw"; + + /** + *ACCESS_KEY_SECRET + */ + public static final String ACCESS_KEY_SECRET = "NHb7wHVpesLW6Axc0bFBs6ThhuNR10"; + + public static Client createClient() throws Exception { + // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 + // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。 + Config config = new Config() + // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 + .setAccessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID) + // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 + .setAccessKeySecret(ACCESS_KEY_SECRET); + // Endpoint 请参考 https://api.aliyun.com/product/Ecs + config.endpoint = "ecs.cn-shanghai.aliyuncs.com"; + return new Client(config); + } +} 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 b027b47..8ac2666 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/resources/bootstrap.yml +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/resources/bootstrap.yml @@ -7,7 +7,7 @@ nacos: addr: 47.101.53.251:8848 user-name: nacos password: nacos - namespace: yzl + namespace: four # Spring spring: