From a0994c3e7ae61b914f78848ef2d35b847b27398c Mon Sep 17 00:00:00 2001 From: SuiXxx <1752599835@qq.com> Date: Sun, 29 Sep 2024 15:36:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat():=E5=BC=80=E5=90=AF=E5=85=B3=E9=97=AD?= =?UTF-8?q?ESC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/muyu/vehicle/CloseInstance.java | 110 +++++++++++++++ .../java/com/muyu/vehicle/OpenInstance.java | 128 +++++++++++++++++ .../com/muyu/vehicle/VehicleInstance.java | 132 ------------------ .../java/com/muyu/vehicle/VehicleThread.java | 24 ---- .../com/muyu/vehicle/api/ClientAdmin.java | 12 -- .../com/muyu/vehicle/core/LocalContainer.java | 24 ---- .../vehicle/core/VehicleConfiguration.java | 13 -- .../impl/VehicleInstanceServiceImpl.java | 5 +- .../muyu/web/common/ScheduledThreadPool.java | 33 ----- .../muyu/web/controller/testController.java | 40 ------ .../com/muyu/web/domain/MqttProperties.java | 42 ------ .../com/muyu/web/domain/ServerConfig.java | 76 ---------- .../java/com/muyu/web/domain/VehicleInfo.java | 92 ------------ .../web/domain/model/MqttServerModel.java | 37 ----- .../muyu/web/domain/model/PositionModel.java | 33 ----- .../web/domain/model/ServerConfigModel.java | 53 ------- .../web/domain/req/VehicleConnectionReq.java | 34 ----- .../web/service/VehicleInstanceService.java | 12 -- .../main/java/com/muyu/web/utils/MD5Util.java | 52 ------- 19 files changed, 239 insertions(+), 713 deletions(-) create mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/CloseInstance.java create mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/OpenInstance.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/VehicleInstance.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/VehicleThread.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/api/ClientAdmin.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/core/LocalContainer.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/core/VehicleConfiguration.java rename cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/{web => vehicle}/service/impl/VehicleInstanceServiceImpl.java (79%) delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/common/ScheduledThreadPool.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/controller/testController.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/MqttProperties.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/ServerConfig.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/VehicleInfo.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/MqttServerModel.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/PositionModel.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/ServerConfigModel.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/req/VehicleConnectionReq.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/service/VehicleInstanceService.java delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/utils/MD5Util.java diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/CloseInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/CloseInstance.java new file mode 100644 index 0000000..4e51133 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/CloseInstance.java @@ -0,0 +1,110 @@ +package com.muyu.vehicle; + + +import com.aliyun.ecs20140526.Client; +import com.aliyun.ecs20140526.models.DeleteInstancesRequest; +import com.aliyun.ecs20140526.models.DescribeInstancesRequest; +import com.aliyun.ecs20140526.models.DescribeInstancesResponse; +import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody; +import com.aliyun.tea.TeaException; +import com.aliyun.teaopenapi.models.Config; +import com.aliyun.teautil.Common; +import com.aliyun.teautil.models.RuntimeOptions; +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"; + + /** + *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); + } + + public static void delInstance() throws Exception { + + // 创建ECS客户端对象,用于后续调用ECS相关API + Client client = CloseInstance.createClient(); + + DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() + .setRegionId("cn-shanghai"); + + //创建运行时选择对象,用于配置运行时的选项参数 + RuntimeOptions runtimeOptions = new RuntimeOptions(); + + //获取实例列表 + DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtimeOptions); + + //提取实例ID集合 + ArrayList list = new ArrayList<>(); + DescribeInstancesResponseBody body = describeInstancesResponse.getBody(); + + for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()) { + if (!instance.getInstanceId().equals("i-uf68jwsbbqq4b4xc893s")){ + list.add(instance.getInstanceId()); + } + } + log.info("搜索到实例Instance IDs: " + list); + // 创建删除实例请求对象,并设置请求参数 + DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest() + // 设置地域ID,指定删除实例的地域 + .setRegionId("cn-shanghai") + // 设置DryRun为true,用于验证请求是否可以成功,但不实际执行删除操作 + .setDryRun(false) + // 设置Force为true,表示即使实例有正在运行的任务,也强制删除实例 + .setForce(true) + // 设置TerminateSubscription为true,表示删除按订阅付费的实例时终止订阅 + .setTerminateSubscription(true) + .setInstanceId(list); + + // 创建运行时选项对象,用于配置运行时的选项参数 + RuntimeOptions runtime = new RuntimeOptions(); + try { + // 复制代码运行请自行打印 API 的返回值 + client.deleteInstancesWithOptions(deleteInstancesRequest, runtime); + } 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 + System.out.println(error.getMessage()); + // 诊断地址 + log.info(error.getData().get("Recommend")); + Common.assertAsString(error.message); + } + } + + + @Override + public void destroy() throws Exception { + log.info("开始删除实例"); + delInstance(); + } +} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/OpenInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/OpenInstance.java new file mode 100644 index 0000000..0d21148 --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/OpenInstance.java @@ -0,0 +1,128 @@ +package com.muyu.vehicle; + +import com.aliyun.ecs20140526.Client; +import com.aliyun.ecs20140526.models.RunInstancesRequest; +import com.aliyun.tea.TeaException; +import com.aliyun.teaopenapi.models.Config; +import com.aliyun.teautil.Common; +import com.aliyun.teautil.models.RuntimeOptions; +import lombok.extern.log4j.Log4j2; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +@Component +@Log4j2 +public class OpenInstance implements ApplicationRunner { + + /** + * 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(1) // 设置创建实例的数量 + .setInternetChargeType("PayByTraffic") + .setInternetMaxBandwidthOut(1); + + + //创建运行时选择对象 + RuntimeOptions runTime= + new RuntimeOptions(); + // 尝试执行创建实例请求 + try { + // 复制代码运行请自行打印 API 的返回值 + client.runInstancesWithOptions(runInstancesRequest, runTime); + log.info("实例创建成功"); + } 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()); + } + } + @Override + public void run(ApplicationArguments args) throws Exception { + generateInstance(); + } +} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/VehicleInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/VehicleInstance.java deleted file mode 100644 index c55960e..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/VehicleInstance.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.muyu.vehicle; - -import com.alibaba.fastjson.JSONObject; -import com.muyu.web.common.ScheduledThreadPool; -import com.muyu.web.domain.MqttProperties; -import com.muyu.web.domain.VehicleInfo; -import com.muyu.web.domain.model.PositionModel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.extern.log4j.Log4j2; -import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttConnectOptions; -import org.eclipse.paho.client.mqttv3.MqttException; -import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; - -import java.util.Objects; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ScheduledFuture; - -@Data -@Builder -@Log4j2 -@NoArgsConstructor -@AllArgsConstructor -public class VehicleInstance { - /** - * 路线轨迹编码 - */ - private String positionCode; - - /** - * 路径队列 - */ - private LinkedBlockingQueue positionQueue=new LinkedBlockingQueue<>(); - - /** - * 车辆 - */ - private VehicleInfo vehicleInfo; - - /** - * 车辆工作线程 - */ - private VehicleThread vehicleThread; - - /** - * MQTT配置 - */ - private MqttProperties mqttProperties; - - /** - * 线程提交回调 - */ - private ScheduledFuturescheduledFuture; - - /** - * 连接上报 - */ - private MqttClient client; - - - /** - * 获取当前车辆VIN - */ - public String getVin(){ - return this.vehicleInfo.getVin(); - } - /** - * 获取车辆租户信息 - */ - public String getTenantId(){ - return this.vehicleInfo.getTenantId(); - } - - /** - * 发送消息 - */ - public void sengMsg(String msg){ - - } - - - /** - * 初始化客户端 - */ - public void initClient(){ - try { - client = new MqttClient(mqttProperties.getBroker(), mqttProperties.getClientId(), new MemoryPersistence()); - MqttConnectOptions options = new MqttConnectOptions(); - //设置用户名和密码 - if (Objects.nonNull(mqttProperties.getUserName())&&Objects.nonNull(mqttProperties.getPassword())){ - options.setUserName(mqttProperties.getUserName()); - options.setPassword(mqttProperties.getPassword().toCharArray()); - } - options.setConnectionTimeout(1); - options.setKeepAliveInterval(20); - //连接 - client.connect(options); - log.debug("车辆:[{}]客户端初始化成功连接配置:{}",getVin(), - JSONObject.toJSONString(this.mqttProperties) - ); - VehicleThread vehicleThread = new VehicleThread(); - vehicleThread.setVehicleInstance(this); - this.setVehicleThread(vehicleThread); - ScheduledFuture submit = ScheduledThreadPool.submit(vehicleThread); - this.setScheduledFuture(submit); - log.info("初始化车辆上报模拟线程开始:[{}]", this.getVin()); - } catch (MqttException e) { - log.error("车辆:[{}] 客户端初始化异常", getVin(), e); - throw new RuntimeException(e); - } - } - - /** - * 是否连接在线 - */ - public boolean isOnline(){ - if (this.client==null){ - return false; - } - return this.client.isConnected(); - } - - /** - * 是否建立车辆模拟线程 - */ - public boolean isSend(){ - return this.vehicleThread!=null; - } -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/VehicleThread.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/VehicleThread.java deleted file mode 100644 index 79433ba..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/VehicleThread.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.muyu.vehicle; - -import lombok.Data; -import lombok.extern.log4j.Log4j2; - -@Data -@Log4j2 -public class VehicleThread implements Runnable{ - /** - * 是否停止线程 - */ - private volatile boolean isStop; - - /** - * 车辆实例对象 - */ - private VehicleInstance vehicleInstance; - @Override - public void run() { - if (!isStop){ - log.info("{}-上报数据",this.vehicleInstance.getVin()); - } - } -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/api/ClientAdmin.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/api/ClientAdmin.java deleted file mode 100644 index fd2f51b..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/api/ClientAdmin.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.muyu.vehicle.api; - -/** - * 客户端管理 - */ -public interface ClientAdmin { - /** - * 获取车辆负载地址 - */ - - -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/core/LocalContainer.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/core/LocalContainer.java deleted file mode 100644 index 5db7cf5..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/core/LocalContainer.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.muyu.vehicle.core; - -import com.muyu.vehicle.VehicleInstance; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class LocalContainer { - private static final Map> tenantVehicleDataMap - =new ConcurrentHashMap<>(); - - /** - * 获取租户ID下车辆 - */ - public static MapgetVehicleDataMap(String tenantId){ - return tenantVehicleDataMap.computeIfAbsent(tenantId,k->new ConcurrentHashMap<>()); - } - - /** - * - */ - - -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/core/VehicleConfiguration.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/core/VehicleConfiguration.java deleted file mode 100644 index 0d3b1ff..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/core/VehicleConfiguration.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.muyu.vehicle.core; - -import lombok.AllArgsConstructor; -import lombok.extern.log4j.Log4j2; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.context.annotation.Configuration; - -@Log4j2 -@Configuration -@AllArgsConstructor -public class VehicleConfiguration { -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/service/impl/VehicleInstanceServiceImpl.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleInstanceServiceImpl.java similarity index 79% rename from cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/service/impl/VehicleInstanceServiceImpl.java rename to cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleInstanceServiceImpl.java index 13a408c..910e450 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/service/impl/VehicleInstanceServiceImpl.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleInstanceServiceImpl.java @@ -1,9 +1,6 @@ -package com.muyu.web.service.impl; +package com.muyu.vehicle.service.impl; import com.muyu.common.core.utils.uuid.UUID; -import com.muyu.web.domain.req.VehicleConnectionReq; -import com.muyu.web.service.VehicleInstanceService; -import com.muyu.web.utils.MD5Util; import lombok.extern.log4j.Log4j2; import org.springframework.stereotype.Service; diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/common/ScheduledThreadPool.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/common/ScheduledThreadPool.java deleted file mode 100644 index ab4c0a7..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/common/ScheduledThreadPool.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.muyu.web.common; - -import net.sf.jsqlparser.statement.select.KSQLWindow; - -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -public class ScheduledThreadPool { - /** - * 周期线程数 CPU*2+1 - */ - private static final ScheduledExecutorService scheduledThreadPool= Executors.newScheduledThreadPool( - Runtime.getRuntime().availableProcessors()*2+1 - ); - - public static ScheduledFuturesubmit(Runnable thread){ - // 参数分别是: 任务, 多久后开始执行, 每隔多久执行一次(周期),时间单位 - return submit(thread, 1); - } - - public static ScheduledFuturesubmit(Runnable thread,long period){ - return scheduledThreadPool.scheduleAtFixedRate(thread,0,period, TimeUnit.SECONDS); - } - - /** - * 关闭线程池 - */ - public static void shutdown(){ - scheduledThreadPool.shutdown(); - } -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/controller/testController.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/controller/testController.java deleted file mode 100644 index 6c5bd44..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/controller/testController.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.muyu.web.controller; - -import com.muyu.common.core.utils.uuid.UUID; -import org.eclipse.paho.client.mqttv3.*; - -public class testController { - public static void main(String[] args) { - String broker = "tcp://47.101.53.251:1883"; - String clientId = "SX-"+ UUID.randomUUID().toString(); - MqttClient client; - - try { - client = new MqttClient(broker, clientId); - MqttConnectOptions connectOptions = new MqttConnectOptions(); - connectOptions.setCleanSession(true); - System.out.println("Connect to broker:"+broker); - client.connect(connectOptions); - System.out.println("Connected"); - client.setCallback(new MqttCallback() { - @Override - public void connectionLost(Throwable throwable) { - System.out.println("连接丢失"); - } - - @Override - public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { - System.out.println("消息到达:"+new String(mqttMessage.getPayload())+topic); - } - - @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/web/domain/MqttProperties.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/MqttProperties.java deleted file mode 100644 index 03e468c..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/MqttProperties.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.muyu.web.domain; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -/** - * mqtt配置类 - */ -@Data -@AllArgsConstructor -@NoArgsConstructor -public class MqttProperties { - /** - * 节点 - */ - private String broker; - - /** - * 主题 - */ - private String topic; - /** - * 用户名 - */ - private String userName; - - /** - * 密码 - */ - private String password; - - /** - * 节点ID - */ - private String clientId; - /** - * 上报级别 - */ - private int qos=0; - -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/ServerConfig.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/ServerConfig.java deleted file mode 100644 index f480f5d..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/ServerConfig.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.muyu.web.domain; - -import com.baomidou.mybatisplus.annotation.IdType; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; -import com.muyu.web.domain.model.ServerConfigModel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.function.Supplier; - -/** - * 服务器配置类 - */ -@Data -@Builder -@NoArgsConstructor -@AllArgsConstructor -@TableName(value = "server_config") -public class ServerConfig { - - /** - * 主键 - */ - @TableId(value = "id",type = IdType.AUTO) - private Long id; - - - /** - * 主机地址 - */ - private String host; - - /** - * 端口号 - */ - private String port; - - /** - * 负载地址 - */ - private String url; - - /** - * 默认MOTT地址 - */ - private String defaultMqttAddr; - - /** - * 默认MQTT主题 - */ - private String defaultMqttTopic; - - /** - * 默认MOTT交付级别 - */ - private Integer defaultMqttQos; - - - public static ServerConfig modeBuild(ServerConfigModel serverConfigModel, Supplier idKey){ - return builder() - .id(idKey.get()) - .host(serverConfigModel.getHost()) - .port(serverConfigModel.getPort()) - .url(serverConfigModel.getUrl()) - .defaultMqttAddr(serverConfigModel.getDefaultMqttAddr()) - .defaultMqttTopic(serverConfigModel.getDefaultMqttTopic()) - .defaultMqttQos(serverConfigModel.getDefaultMqttQos()) - .build(); - - - - } -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/VehicleInfo.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/VehicleInfo.java deleted file mode 100644 index b717527..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/VehicleInfo.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.muyu.web.domain; - -import com.baomidou.mybatisplus.annotation.*; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.math.BigDecimal; -import java.util.Date; -import java.util.function.Supplier; - -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder -@TableName("vehicle_info") -public class VehicleInfo { - - /** - * 主键 - */ - @TableId( - value = "id", - type = IdType.AUTO - ) - private Long id; - /** - * VIN - */ - private String vin; - /** - * VIN - */ - @TableField(value = "tenant_id", fill = FieldFill.INSERT) - private String tenantId; - - /** - * 报文模板 - */ - @TableField("message_template_id") - private Long messageTemplateId; - - /** - * 电池剩余电量 - */ - @TableField("remaining_battery") - private BigDecimal remainingBattery; - - /** - * 电池电量 - */ - @TableField("battery_level") - private BigDecimal batteryLevel; - - /** - * 上一次经度 - */ - @TableField("last_longitude") - private String lastLongitude; - - /** - * 上一次维度 - */ - @TableField("last_latitude") - private String lastLatitude; - - /** - * 总里程 - */ - @TableField("total_mileage") - private BigDecimal totalMileage; - - /** - * 创建时间 - */ - @TableField(value = "create_time", fill = FieldFill.INSERT) - private Date createTime; - - - public static VehicleInfo create(String vin, SuppliermessageTemplateId){ - return VehicleInfo.builder() - .vin(vin) - .messageTemplateId(messageTemplateId.get()) - .createTime(new Date()) - .totalMileage(BigDecimal.ZERO) - .build(); - } - - - -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/MqttServerModel.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/MqttServerModel.java deleted file mode 100644 index 8e56869..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/MqttServerModel.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.muyu.web.domain.model; - - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - Mqtt服务器模型 - */ - -@Data -@NoArgsConstructor -@AllArgsConstructor -public class MqttServerModel { - private static final Logger log=LoggerFactory.getLogger(MqttServerModel.class); - - /** - * MQTT服务节点 - */ - private String broker; - - /** - * MQTT订阅主题 - */ - private String topic; - - public String getBroker(){ - log.info("broker:{}",broker); - return broker.contains("tcp://")?broker:"tcp://"+broker+":1883"; - } - - -} - diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/PositionModel.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/PositionModel.java deleted file mode 100644 index 7a9e920..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/PositionModel.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.muyu.web.domain.model; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -/** - * @description: 位置模型 - */ -@Data -@Builder -@NoArgsConstructor -@AllArgsConstructor -public class PositionModel { - /** - * 经度 - */ - private String longitude; - - /** - * 维度 - */ - private String latitude; - - public static PositionModel strBuild (String positionStr) { - String[] split = positionStr.split(","); - return PositionModel.builder() - .longitude(split[0]) - .latitude(split[1]) - .build(); - } -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/ServerConfigModel.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/ServerConfigModel.java deleted file mode 100644 index 3418155..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/model/ServerConfigModel.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.muyu.web.domain.model; - -import com.muyu.web.domain.ServerConfig; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@Builder -@AllArgsConstructor -@NoArgsConstructor -public class ServerConfigModel { - /** - * 主机地址 - */ - private String host; - - /** - * 端口号 - */ - private String port; - /** - * 负载地址 - */ - private String url; - - /** - * 默认MOTT地址 - */ - private String defaultMqttAddr; - - /** - * 默认MQTT主题 - */ - private String defaultMqttTopic; - - /** - * 默认MOTT交付级别 - */ - private Integer defaultMqttQos; - - public static ServerConfigModel serverConfigModelBuild(ServerConfig serverConfig){ - return builder() - .host(serverConfig.getHost().trim()) - .port(serverConfig.getPort()) - .url(serverConfig.getUrl().trim()) - .defaultMqttAddr(serverConfig.getDefaultMqttAddr().trim()) - .defaultMqttTopic(serverConfig.getDefaultMqttTopic().trim()) - .defaultMqttQos(serverConfig.getDefaultMqttQos()) - .build(); - } -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/req/VehicleConnectionReq.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/req/VehicleConnectionReq.java deleted file mode 100644 index 9368d2d..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/domain/req/VehicleConnectionReq.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.muyu.web.domain.req; - -import com.alibaba.fastjson.annotation.JSONField; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@Builder -@NoArgsConstructor -@AllArgsConstructor -public class VehicleConnectionReq { - - @JSONField(name = "vin") - private String vin; - - /** - * 时间戳 - */ - private String timestamp; - - - /** - * 用户名 - */ - @JSONField(name = "username") - private String userName; - - /** - * 随机字符串 - */ - private String nonce; -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/service/VehicleInstanceService.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/service/VehicleInstanceService.java deleted file mode 100644 index 0e4d702..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/service/VehicleInstanceService.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.muyu.web.service; - -public interface VehicleInstanceService { - - - /** - * 车辆客户端初始化 - */ - void vehicleClientStart(String vin); - - -} diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/utils/MD5Util.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/utils/MD5Util.java deleted file mode 100644 index 7668abb..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/web/utils/MD5Util.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.muyu.web.utils; - -import lombok.extern.log4j.Log4j2; - -import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; - -@Log4j2 -public class MD5Util { - private static final Integer SALT_LENGTH = 12; - - /** - * 指定数组转化为16进制字符串 - */ - public static String byteToHexString(byte[]b){ - StringBuilder stringBuilder = new StringBuilder(); - for (byte value : b) { - String hex = Integer.toHexString(value & 0xFF); - if (hex.length()==1){ - hex='0'+hex; - } - stringBuilder.append(hex.toUpperCase()); - } - return stringBuilder.toString(); - } - - /** - * 获得加密后的口令 - */ - public static String encrypted(String str){ - try { - byte[]pwd=null; - SecureRandom random = new SecureRandom(); - byte[] salt = new byte[SALT_LENGTH]; - random.nextBytes(salt); - MessageDigest md=null; - md = MessageDigest.getInstance("MD5"); - md.update(salt); - md.update(str.getBytes(StandardCharsets.UTF_8)); - byte[] digest = md.digest(); - pwd=new byte[digest.length+SALT_LENGTH]; - System.arraycopy(salt,0,pwd,0,SALT_LENGTH); - System.arraycopy(digest,0,pwd,SALT_LENGTH,digest.length); - return byteToHexString(pwd); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e); - } - } - -} From f4faf4d82823dc9306d51791037e728c035cb2a3 Mon Sep 17 00:00:00 2001 From: SuiXxx <1752599835@qq.com> Date: Sun, 29 Sep 2024 15:38:08 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat():=E5=BC=80=E5=90=AF=E5=85=B3=E9=97=AD?= =?UTF-8?q?ESC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cloud-modules-vehiclegateway/pom.xml | 25 +++++++++++++++++++ .../impl/VehicleInstanceServiceImpl.java | 22 ---------------- 2 files changed, 25 insertions(+), 22 deletions(-) delete mode 100644 cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleInstanceServiceImpl.java diff --git a/cloud-modules/cloud-modules-vehiclegateway/pom.xml b/cloud-modules/cloud-modules-vehiclegateway/pom.xml index 6be8747..e84de72 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/pom.xml +++ b/cloud-modules/cloud-modules-vehiclegateway/pom.xml @@ -98,6 +98,31 @@ org.eclipse.paho.client.mqttv3 1.2.5 + + + + + com.aliyun + ecs20140526 + 5.1.8 + + + com.aliyun + tea-openapi + 0.3.2 + + + com.aliyun + tea-console + 0.0.1 + + + com.aliyun + tea-util + 0.2.21 + + + diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleInstanceServiceImpl.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleInstanceServiceImpl.java deleted file mode 100644 index 910e450..0000000 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleInstanceServiceImpl.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.muyu.vehicle.service.impl; - -import com.muyu.common.core.utils.uuid.UUID; -import lombok.extern.log4j.Log4j2; -import org.springframework.stereotype.Service; - -@Log4j2 -@Service -public class VehicleInstanceServiceImpl implements VehicleInstanceService { - @Override - public void vehicleClientStart(String vin) { - log.info("车辆{},开始上线",vin); - String timestamp = String.valueOf(System.currentTimeMillis()); - VehicleConnectionReq.builder() - .vin(vin) - .timestamp(timestamp) - .userName(MD5Util.encrypted(vin+timestamp)) - .nonce(MD5Util.encrypted(UUID.randomUUID().toString().replace("-",""))) - .build(); - // - } -}