Commit f4b9cc4bb03d8cfaf492e01e6acb0c9f781af287

Authored by 李晓鹤
0 parents

feat: 初始化IoT调度器项目基础结构

- 添加Spring Boot应用主类IotSchedulerApplication
- 实现健康检查接口HealthController
- 配置定时任务线程池SchedulerConfig
- 添加抽象基类AbstractZoneScheduler
- 实现池州园区调度器ChizhouZoneScheduler
- 配置application.yml定时任务参数
- 添加项目README文档说明
  1 +# IoT Scheduler 项目
  2 +
  3 +这是一个基于 Spring Boot 的定时任务应用程序,用于管理跨多个园区的 IoT 设备数据同步任务。
  4 +
  5 +## 功能特性
  6 +
  7 +- **基于园区的调度**:支持多个经开区(例如:怀宁、池州、潘集)。
  8 +- **双向同步**:
  9 + - **拉取 (Pull)**:从第三方平台拉取设备数据到我们的 IoT 平台。
  10 + - **推送 (Push)**:将我们 IoT 平台的设备数据推送到第三方平台。
  11 +- **企业级定制**:
  12 + - 支持同一园区下不同企业的定制化同步逻辑(通过 `EnterpriseSyncHandler` 接口实现)。
  13 +- **灵活配置**:
  14 + - 定时任务的时间(Cron 表达式)可以在 `application.yml` 中配置,无需修改代码。
  15 +
  16 +## 项目结构
  17 +
  18 +- `com.iot.scheduler.task`: 包含抽象基类 `AbstractZoneScheduler`。
  19 +- `com.iot.scheduler.zone`: 包含各园区的具体调度器实现(如 `HuainingZoneScheduler`)。
  20 +- `com.iot.scheduler.service`: 定义了 `EnterpriseSyncHandler` 接口,用于处理特定企业的业务逻辑。
  21 +- `com.iot.scheduler.service.impl`: 包含各企业的具体同步实现(如 `HuainingPaperFactoryHandler`)。
  22 +- `com.iot.scheduler.config`: 配置类(如线程池配置)。
  23 +
  24 +## 配置说明
  25 +
  26 +您可以在 `src/main/resources/application.yml` 中调整定时任务的执行频率:
  27 +
  28 +```yaml
  29 +scheduler:
  30 + huaining:
  31 + pull: "0 0/5 * * * ?" # 每5分钟执行一次
  32 + push: "0 0/10 * * * ?" # 每10分钟执行一次
  33 + chizhou:
  34 + pull: "0 0/5 * * * ?"
  35 + push: "0 0/10 * * * ?"
  36 + panji:
  37 + pull: "0 0/5 * * * ?"
  38 + push: "0 0/10 * * * ?"
  39 +```
  40 +
  41 +## 如何运行
  42 +
  43 +### 环境要求
  44 +- Java 17+
  45 +- Maven
  46 +
  47 +### 启动应用
  48 +
  49 +使用 Maven 插件启动:
  50 +
  51 +```bash
  52 +mvn spring-boot:run
  53 +```
  54 +
  55 +或者构建并运行 JAR 包:
  56 +
  57 +```bash
  58 +mvn clean package
  59 +java -jar target/iot-scheduler-0.0.1-SNAPSHOT.jar
  60 +```
  61 +
  62 +### 健康检查
  63 +
  64 +检查服务是否正常运行:
  65 +http://localhost:8080/health
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4 + <modelVersion>4.0.0</modelVersion>
  5 + <parent>
  6 + <groupId>org.springframework.boot</groupId>
  7 + <artifactId>spring-boot-starter-parent</artifactId>
  8 + <version>3.2.1</version>
  9 + <relativePath/> <!-- lookup parent from repository -->
  10 + </parent>
  11 + <groupId>com.iot</groupId>
  12 + <artifactId>iot-scheduler</artifactId>
  13 + <version>0.0.1-SNAPSHOT</version>
  14 + <name>iot-scheduler</name>
  15 + <description>IoT Scheduler for Device Sync</description>
  16 + <properties>
  17 + <java.version>17</java.version>
  18 + </properties>
  19 + <dependencies>
  20 + <dependency>
  21 + <groupId>org.springframework.boot</groupId>
  22 + <artifactId>spring-boot-starter-web</artifactId>
  23 + </dependency>
  24 + <dependency>
  25 + <groupId>org.springframework.boot</groupId>
  26 + <artifactId>spring-boot-starter</artifactId>
  27 + </dependency>
  28 + <dependency>
  29 + <groupId>org.projectlombok</groupId>
  30 + <artifactId>lombok</artifactId>
  31 + <optional>true</optional>
  32 + </dependency>
  33 + <dependency>
  34 + <groupId>org.springframework.boot</groupId>
  35 + <artifactId>spring-boot-starter-test</artifactId>
  36 + <scope>test</scope>
  37 + </dependency>
  38 + </dependencies>
  39 +
  40 + <build>
  41 + <plugins>
  42 + <plugin>
  43 + <groupId>org.springframework.boot</groupId>
  44 + <artifactId>spring-boot-maven-plugin</artifactId>
  45 + <configuration>
  46 + <excludes>
  47 + <exclude>
  48 + <groupId>org.projectlombok</groupId>
  49 + <artifactId>lombok</artifactId>
  50 + </exclude>
  51 + </excludes>
  52 + </configuration>
  53 + </plugin>
  54 + </plugins>
  55 + </build>
  56 +
  57 +</project>
  1 +package com.iot.scheduler;
  2 +
  3 +import org.springframework.boot.SpringApplication;
  4 +import org.springframework.boot.autoconfigure.SpringBootApplication;
  5 +import org.springframework.scheduling.annotation.EnableScheduling;
  6 +
  7 +@SpringBootApplication
  8 +@EnableScheduling
  9 +public class IotSchedulerApplication {
  10 +
  11 + public static void main(String[] args) {
  12 + SpringApplication.run(IotSchedulerApplication.class, args);
  13 + }
  14 +
  15 +}
  1 +package com.iot.scheduler.config;
  2 +
  3 +import org.springframework.context.annotation.Bean;
  4 +import org.springframework.context.annotation.Configuration;
  5 +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
  6 +
  7 +@Configuration
  8 +public class SchedulerConfig {
  9 +
  10 + @Bean
  11 + public ThreadPoolTaskScheduler taskScheduler() {
  12 + ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
  13 + scheduler.setPoolSize(10); // Adjust based on number of zones/tasks
  14 + scheduler.setThreadNamePrefix("iot-scheduler-");
  15 + scheduler.initialize();
  16 + return scheduler;
  17 + }
  18 +}
  1 +package com.iot.scheduler.controller;
  2 +
  3 +import org.springframework.web.bind.annotation.GetMapping;
  4 +import org.springframework.web.bind.annotation.RestController;
  5 +
  6 +@RestController
  7 +public class HealthController {
  8 +
  9 + @GetMapping("/health")
  10 + public String health() {
  11 + return "IoT Scheduler is running...";
  12 + }
  13 +}
  1 +package com.iot.scheduler.task;
  2 +
  3 +import lombok.extern.slf4j.Slf4j;
  4 +
  5 +/**
  6 + * Base class for Zone Schedulers.
  7 + * Provides common logging and helper methods.
  8 + */
  9 +@Slf4j
  10 +public abstract class AbstractZoneScheduler {
  11 +
  12 + protected abstract String getZoneName();
  13 +
  14 + protected void logStart(String taskName) {
  15 + log.info("[{}] Starting task: {}", getZoneName(), taskName);
  16 + }
  17 +
  18 + protected void logEnd(String taskName) {
  19 + log.info("[{}] Completed task: {}", getZoneName(), taskName);
  20 + }
  21 +
  22 + protected void logError(String taskName, Exception e) {
  23 + log.error("[{}] Error in task: {}", getZoneName(), taskName, e);
  24 + }
  25 +}
  1 +package com.iot.scheduler.zone;
  2 +
  3 +import com.iot.scheduler.task.AbstractZoneScheduler;
  4 +import lombok.extern.slf4j.Slf4j;
  5 +import org.springframework.scheduling.annotation.Scheduled;
  6 +import org.springframework.stereotype.Component;
  7 +
  8 +@Slf4j
  9 +@Component
  10 +public class ChizhouZoneScheduler extends AbstractZoneScheduler {
  11 +
  12 + @Override
  13 + protected String getZoneName() {
  14 + return "Chizhou (池州经开区)";
  15 + }
  16 +
  17 + @Scheduled(cron = "${scheduler.chizhou.pull:0 0/10 * * * ?}")
  18 + public void pullDevicesFromThirdParty() {
  19 + String taskName = "Pull Devices (3rd Party -> IoT)";
  20 + logStart(taskName);
  21 + try {
  22 + // TODO: Implement actual logic
  23 + log.info("[{}] Simulating pulling devices...", getZoneName());
  24 + Thread.sleep(1000);
  25 + } catch (Exception e) {
  26 + logError(taskName, e);
  27 + } finally {
  28 + logEnd(taskName);
  29 + }
  30 + }
  31 +
  32 + @Scheduled(cron = "${scheduler.chizhou.push:0 0/15 * * * ?}")
  33 + public void pushDevicesToThirdParty() {
  34 + String taskName = "Push Devices (IoT -> 3rd Party)";
  35 + logStart(taskName);
  36 + try {
  37 + // TODO: Implement actual logic
  38 + log.info("[{}] Simulating pushing devices...", getZoneName());
  39 + Thread.sleep(1000);
  40 + } catch (Exception e) {
  41 + logError(taskName, e);
  42 + } finally {
  43 + logEnd(taskName);
  44 + }
  45 + }
  46 +}
  1 +spring:
  2 + application:
  3 + name: iot-scheduler
  4 + main:
  5 + banner-mode: off
  6 +
  7 +server:
  8 + port: 8080
  9 +
  10 +logging:
  11 + level:
  12 + root: INFO
  13 + com.iot.scheduler: DEBUG
  14 +
  15 +# Scheduler Configuration (Cron Expressions)
  16 +# Format: sec min hour day month day-of-week
  17 +scheduler:
  18 + huaining:
  19 + pull: "0 0/5 * * * ?" # Every 5 minutes
  20 + push: "0 0/10 * * * ?" # Every 10 minutes
  21 + chizhou:
  22 + pull: "0 0/5 * * * ?"
  23 + push: "0 0/10 * * * ?"
  24 + panji:
  25 + pull: "0 0/5 * * * ?"
  26 + push: "0 0/10 * * * ?"
  1 +spring:
  2 + application:
  3 + name: iot-scheduler
  4 + main:
  5 + banner-mode: off
  6 +
  7 +server:
  8 + port: 8080
  9 +
  10 +logging:
  11 + level:
  12 + root: INFO
  13 + com.iot.scheduler: DEBUG
  14 +
  15 +# Scheduler Configuration (Cron Expressions)
  16 +# Format: sec min hour day month day-of-week
  17 +scheduler:
  18 + huaining:
  19 + pull: "0 0/5 * * * ?" # Every 5 minutes
  20 + push: "0 0/10 * * * ?" # Every 10 minutes
  21 + chizhou:
  22 + pull: "0 0/5 * * * ?"
  23 + push: "0 0/10 * * * ?"
  24 + panji:
  25 + pull: "0 0/5 * * * ?"
  26 + push: "0 0/10 * * * ?"
No preview for this file type
  1 +artifactId=iot-scheduler
  2 +groupId=com.iot
  3 +version=0.0.1-SNAPSHOT
  1 +com\iot\scheduler\config\SchedulerConfig.class
  2 +com\iot\scheduler\zone\HuainingZoneScheduler.class
  3 +com\iot\scheduler\task\AbstractZoneScheduler.class
  4 +com\iot\scheduler\zone\ChizhouZoneScheduler.class
  5 +com\iot\scheduler\zone\PanjiZoneScheduler.class
  6 +com\iot\scheduler\IotSchedulerApplication.class
  7 +com\iot\scheduler\controller\HealthController.class
  1 +E:\Project\iot-scheduler\src\main\java\com\iot\scheduler\zone\HuainingZoneScheduler.java
  2 +E:\Project\iot-scheduler\src\main\java\com\iot\scheduler\zone\PanjiZoneScheduler.java
  3 +E:\Project\iot-scheduler\src\main\java\com\iot\scheduler\controller\HealthController.java
  4 +E:\Project\iot-scheduler\src\main\java\com\iot\scheduler\IotSchedulerApplication.java
  5 +E:\Project\iot-scheduler\src\main\java\com\iot\scheduler\task\AbstractZoneScheduler.java
  6 +E:\Project\iot-scheduler\src\main\java\com\iot\scheduler\config\SchedulerConfig.java
  7 +E:\Project\iot-scheduler\src\main\java\com\iot\scheduler\zone\ChizhouZoneScheduler.java