commit 98f4713be1c69938b52552bb7958bb0c59758be0 Author: 白凤吉 Date: Tue Jul 22 10:21:03 2025 +0800 创建项目 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..8af972c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +/gradlew text eol=lf +*.bat text eol=crlf +*.jar binary diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2065bc --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..92ea793 --- /dev/null +++ b/build.gradle @@ -0,0 +1,55 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '3.5.3' + id 'io.spring.dependency-management' version '1.1.7' +} + +group = 'com.iflytop' +version = '0.0.1-SNAPSHOT' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} + +repositories { + maven { url 'https://maven.aliyun.com/repository/public' } + maven { url 'https://maven.aliyun.com/repository/central' } + maven { url 'https://maven.aliyun.com/repository/spring' } +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter'// Spring Boot 核心依赖,包含自动配置、日志、Spring Context 等基础功能 + implementation 'org.springframework.boot:spring-boot-starter-web'// 用于构建 REST API 的 Web 功能,包含 Spring MVC、Tomcat 等 + implementation 'org.springframework.boot:spring-boot-starter-websocket:3.5.3' // WebSocket 支持 + implementation 'org.springframework.statemachine:spring-statemachine-core:4.0.1'// Spring 状态机(有限状态机)核心包,用于处理状态转换逻辑 + implementation 'org.springframework.boot:spring-boot-starter-validation:3.5.3' // Bean 校验支持(如 @Valid、@NotNull 等注解) + implementation 'org.springframework.boot:spring-boot-starter-aop'// AOP(面向切面编程)支持,用于实现日志、权限校验等横切逻辑 + implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.9'// OpenAPI 文档生成器,用于生成 Swagger UI 接口文档 + implementation 'org.freemarker:freemarker:2.3.34'// 模板引擎 FreeMarker,可用于生成动态内容(如生成文件或 Web 页面) + implementation 'org.xerial:sqlite-jdbc:3.50.3.0'// SQLite JDBC 驱动,提供对 SQLite 数据库的连接支持 + implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.5' // MyBatis Spring Boot 启动器,简化 MyBatis 配置 + implementation 'com.baomidou:mybatis-plus-boot-starter:3.5.12'// MyBatis Plus 启动器,对 MyBatis 的增强支持(简化 CRUD、分页等) + implementation 'com.baomidou:mybatis-plus-generator:3.5.12'// MyBatis Plus 代码生成器,用于自动生成实体类、Mapper、Service 等 + implementation 'com.baomidou:mybatis-plus-jsqlparser:3.5.12'// MyBatis Plus SQL 解析器,MyBatis Plus 的内部依赖(用于条件构造器) + implementation 'ch.qos.logback:logback-core:1.5.16' // Logback 核心库,用于日志输出 + implementation 'cn.hutool:hutool-all:5.8.39'// Hutool 工具包,提供各种常用工具类,如日期处理、IO、字符串等 + implementation 'com.github.xiaoymin:knife4j-openapi3-jakarta-spring-boot-starter:4.5.0'// Knife4j Swagger 增强工具,提供更友好的 API 文档界面 + implementation 'jakarta.annotation:jakarta.annotation-api:3.0.0' // Jakarta 标准注解包(如 @PostConstruct、@Resource 等) + implementation 'com.fazecast:jSerialComm:2.11.2'// 串口通信库 jSerialComm,用于与硬件设备通过串口通信 + implementation 'com.opencsv:opencsv:5.11.2'// CSV 解析器 OpenCSV,用于读写 CSV 文件 + implementation fileTree(dir: 'lib', include: '*.jar')// 引入本地 lib 目录下的所有 jar 包 + + compileOnly 'org.projectlombok:lombok' // Lombok 提供简化 Java 开发的注解(如 @Getter @Setter 等),编译期依赖 + annotationProcessor 'org.projectlombok:lombok' + + + testImplementation 'org.springframework.boot:spring-boot-starter-test'// 单元测试支持,包含 JUnit、Mockito 等常用测试库 + testRuntimeOnly 'org.junit.platform:junit-platform-launcher'// JUnit 平台运行时,用于在测试时启动 JUnit Platform(例如在 IDE 或构建工具中运行测试) +} + + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..1b33c55 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..04bedef --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://mirrors.aliyun.com/gradle/distributions/v8.14.3/gradle-8.14.3-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..23d15a9 --- /dev/null +++ b/gradlew @@ -0,0 +1,251 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH="\\\"\\\"" + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..db3a6ac --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH= + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..8743254 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'color-titration' diff --git a/src/main/java/com/iflytop/colortitration/ColorTitrationApplication.java b/src/main/java/com/iflytop/colortitration/ColorTitrationApplication.java new file mode 100644 index 0000000..2c6c0df --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/ColorTitrationApplication.java @@ -0,0 +1,13 @@ +package com.iflytop.colortitration; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ColorTitrationApplication { + + public static void main(String[] args) { + SpringApplication.run(ColorTitrationApplication.class, args); + } + +} diff --git a/src/main/java/com/iflytop/colortitration/app/common/annotation/CheckedRunnable.java b/src/main/java/com/iflytop/colortitration/app/common/annotation/CheckedRunnable.java new file mode 100644 index 0000000..0fee250 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/annotation/CheckedRunnable.java @@ -0,0 +1,6 @@ +package com.iflytop.colortitration.app.common.annotation; + +@FunctionalInterface +public interface CheckedRunnable { + void run() throws Exception; +} diff --git a/src/main/java/com/iflytop/colortitration/app/common/annotation/CommandMapping.java b/src/main/java/com/iflytop/colortitration/app/common/annotation/CommandMapping.java new file mode 100644 index 0000000..9aad771 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/annotation/CommandMapping.java @@ -0,0 +1,12 @@ +package com.iflytop.colortitration.app.common.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface CommandMapping { + String value(); +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/app/common/command/CommandFuture.java b/src/main/java/com/iflytop/colortitration/app/common/command/CommandFuture.java new file mode 100644 index 0000000..78dbe99 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/command/CommandFuture.java @@ -0,0 +1,55 @@ +package com.iflytop.colortitration.app.common.command; + +import cn.hutool.json.JSONObject; +import lombok.Getter; +import lombok.Setter; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +@Setter +@Getter +public class CommandFuture { + /** + * 用于保存response反馈 + */ + private CompletableFuture responseFuture = new CompletableFuture<>(); + + private long startSendTime; + private long endSendTime; + + /** + * 业务指令id + */ + private String cmdId; + /** + * 业务指令code + */ + private String cmdCode; + /** + * 设备指令 + */ + private DeviceCommandBundle deviceCommandBundle; + + /** + * 完成response + */ + public void completeResponse(JSONObject result) { + responseFuture.complete(result); + } + + /** + * 异常完成response + */ + public void completeResponseExceptionally(Throwable ex) { + responseFuture.completeExceptionally(ex); + } + + /** + * 获取response的json + */ + public JSONObject getResponseResult() throws ExecutionException, InterruptedException { + return responseFuture.get(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/app/common/command/CommandHandler.java b/src/main/java/com/iflytop/colortitration/app/common/command/CommandHandler.java new file mode 100644 index 0000000..48ff8e2 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/command/CommandHandler.java @@ -0,0 +1,10 @@ +package com.iflytop.colortitration.app.common.command; + + +import com.iflytop.colortitration.app.model.dto.CommandDTO; + +import java.util.concurrent.CompletableFuture; + +public interface CommandHandler { + CompletableFuture handle(CommandDTO commandDTO) throws Exception; +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/app/common/command/CyclicNumberGenerator.java b/src/main/java/com/iflytop/colortitration/app/common/command/CyclicNumberGenerator.java new file mode 100644 index 0000000..9bdc3fb --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/command/CyclicNumberGenerator.java @@ -0,0 +1,39 @@ +package com.iflytop.colortitration.app.common.command; + +public class CyclicNumberGenerator { + // 饿汉式单例,在类加载时就创建实例 + private static final CyclicNumberGenerator INSTANCE = new CyclicNumberGenerator(); + // 当前生成的数字,初始值为 1 + private int currentNumber = 1; + + // 私有构造函数,防止外部实例化 + private CyclicNumberGenerator() { + } + + // 提供全局访问点获取单例实例 + public static CyclicNumberGenerator getInstance() { + return INSTANCE; + } + + public static void main(String[] args) { + CyclicNumberGenerator generator = CyclicNumberGenerator.getInstance(); + for (int i = 0; i < 4096; i++) { + System.out.println(generator.generateNumber()); + } + } + + /** + * 生成 1 到 255 之间的循环整数 + * + * @return 生成的整数 + */ + public synchronized int generateNumber() { + int result = currentNumber; + // 每次生成后将当前数字加 1 + currentNumber++; + if (currentNumber > 4096) { + currentNumber = 1; + } + return result; + } +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommand.java b/src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommand.java new file mode 100644 index 0000000..c345d44 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommand.java @@ -0,0 +1,35 @@ +package com.iflytop.colortitration.app.common.command; + +import com.iflytop.colortitration.common.cmd.DeviceCommandParams; +import com.iflytop.colortitration.common.enums.Action; +import com.iflytop.colortitration.common.enums.Device; +import lombok.Data; + +@Data +public class DeviceCommand { + /** + * 指令ID + */ + private Integer cmdId; + + /** + * 指令代码 例如 "device_status_get" + */ + private String cmdCode; + + /** + * 目标设备 + */ + private Device device; + + /** + * 执行动作 + */ + private Action action; + + /** + * 指令参数 + */ + private DeviceCommandParams param; +} + diff --git a/src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommandBundle.java b/src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommandBundle.java new file mode 100644 index 0000000..f9b422f --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommandBundle.java @@ -0,0 +1,19 @@ +package com.iflytop.colortitration.app.common.command; + +import lombok.Data; + +@Data +public class DeviceCommandBundle { + + /** + * 指令名称 + */ + private String cmdName; + + /** + * 指令 + */ + private DeviceCommand deviceCommand; + +} + diff --git a/src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommandGenerator.java b/src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommandGenerator.java new file mode 100644 index 0000000..5a9ea8e --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommandGenerator.java @@ -0,0 +1,11 @@ +package com.iflytop.colortitration.app.common.command; + + + +/** + * 生成给设备发送的指令 + */ +public class DeviceCommandGenerator { + + +} diff --git a/src/main/java/com/iflytop/colortitration/app/common/constant/CommandStatus.java b/src/main/java/com/iflytop/colortitration/app/common/constant/CommandStatus.java new file mode 100644 index 0000000..a8a2b2a --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/constant/CommandStatus.java @@ -0,0 +1,47 @@ +package com.iflytop.colortitration.app.common.constant; + +public class CommandStatus { + /** + * 普通消息反馈 + */ + public static final String SEND = "send"; + + /** + * 收到指令 + */ + public static final String RECEIVE = "receive"; + /** + * 开始执行指令 + */ + public static final String START = "start"; + /** + * 等待执行指令 + */ + public static final String WAIT = "wait"; + /** + * 执行成功 + */ + public static final String SUCCESS = "success"; + /** + * 执行失败 + */ + public static final String FAIL = "fail"; + /** + * 指令处理完毕反馈 + */ + public static final String FINISH = "finish"; + + + /** + * 设备指令 发送设备指令 + */ + public static final String DEVICE_SEND = "device_send"; + /** + * 设备指令 收到设备指令反馈 + */ + public static final String DEVICE_RESULT = "device_result"; + /** + * 设备指令 收到设备指令反馈 错误 + */ + public static final String DEVICE_ERROR = "device_error"; +} diff --git a/src/main/java/com/iflytop/colortitration/app/common/enums/UserRole.java b/src/main/java/com/iflytop/colortitration/app/common/enums/UserRole.java new file mode 100644 index 0000000..cae05ce --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/enums/UserRole.java @@ -0,0 +1,7 @@ +package com.iflytop.colortitration.app.common.enums; + +public enum UserRole { + ADMIN, + DEVELOPER, + USER; +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/app/common/handler/GlobalExceptionHandler.java b/src/main/java/com/iflytop/colortitration/app/common/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..dcb13b7 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/handler/GlobalExceptionHandler.java @@ -0,0 +1,82 @@ +package com.iflytop.colortitration.app.common.handler; + +import com.iflytop.colortitration.common.exception.AppException; +import com.iflytop.colortitration.common.result.Result; +import com.iflytop.colortitration.common.result.ResultCode; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.validation.ConstraintViolationException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.BindException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.stream.Collectors; + +@Slf4j +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public Result handleMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request) { + String method = request.getMethod(); + String url = request.getRequestURL().toString(); + String allowed = ex.getSupportedHttpMethods() != null + ? ex.getSupportedHttpMethods().toString() + : "[]"; + String msg = String.format( + "请求方法 '%s' 不支持 (URL: %s)。支持的方法有:%s", + method, url, allowed); + log.error(msg, ex); + return Result.failed(ResultCode.METHOD_NOT_ALLOWED.getCode(), msg); + } + + // 1) JSON Body 校验失败 + @ExceptionHandler(MethodArgumentNotValidException.class) + public Result handleBodyValid(MethodArgumentNotValidException ex) { + String msg = ex.getBindingResult().getFieldErrors().stream() + .map(f -> f.getField() + ": " + f.getDefaultMessage()) + .collect(Collectors.joining("; ")); + return Result.failed(ResultCode.INVALID_PARAMETER.getCode(), msg); + } + + // 2) 方法级参数校验失败(PathVariable/RequestParam) + @ExceptionHandler(ConstraintViolationException.class) + public Result handleParamValid(ConstraintViolationException ex) { + String msg = ex.getConstraintViolations().stream() + .map(v -> { + String path = v.getPropertyPath().toString(); + String field = path.substring(path.lastIndexOf('.') + 1); + return field + ": " + v.getMessage(); + }) + .collect(Collectors.joining("; ")); + return Result.failed(ResultCode.INVALID_PARAMETER.getCode(), msg); + } + + // 3) 表单绑定失败 + @ExceptionHandler(BindException.class) + public Result handleBind(BindException ex) { + String msg = ex.getFieldErrors().stream() + .map(f -> f.getField() + ": " + f.getDefaultMessage()) + .collect(Collectors.joining("; ")); + return Result.failed(ResultCode.INVALID_PARAMETER.getCode(), msg); + } + + @ExceptionHandler(Exception.class) + public Result handleException(Exception ex, HttpServletRequest request) { + // 获取请求方法和请求路径 + String method = request.getMethod(); + String url = request.getRequestURL().toString(); + + if (ex instanceof AppException ae) { + log.warn("AppException at {} {}:", method, url, ae); + return Result.failed(ae.getResultCode()); + } + + // 在日志中打印出请求地址 + log.error("Unhandled exception at {} {}:", method, url, ex); + return Result.failed(ResultCode.SYSTEM_ERROR.getCode(), ex.getMessage()); + } + +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/app/common/handler/MyMetaObjectHandler.java b/src/main/java/com/iflytop/colortitration/app/common/handler/MyMetaObjectHandler.java new file mode 100644 index 0000000..e3b6389 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/handler/MyMetaObjectHandler.java @@ -0,0 +1,36 @@ +package com.iflytop.colortitration.app.common.handler; + +import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; +import org.apache.ibatis.reflection.MetaObject; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; + +/** + * mybatis-plus 字段自动填充 + */ +@Component +public class MyMetaObjectHandler implements MetaObjectHandler { + + /** + * 新增填充创建时间 + * + * @param metaObject 元数据 + */ + @Override + public void insertFill(MetaObject metaObject) { + this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class); + this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); + } + + /** + * 更新填充更新时间 + * + * @param metaObject 元数据 + */ + @Override + public void updateFill(MetaObject metaObject) { + this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); + } + +} diff --git a/src/main/java/com/iflytop/colortitration/app/common/utils/CommandUtil.java b/src/main/java/com/iflytop/colortitration/app/common/utils/CommandUtil.java new file mode 100644 index 0000000..39d5ded --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/utils/CommandUtil.java @@ -0,0 +1,23 @@ +package com.iflytop.colortitration.app.common.utils; + + +import com.iflytop.colortitration.app.common.command.CommandFuture; + +import java.util.Arrays; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +public class CommandUtil { + + public static void wait(CommandFuture... futures) throws Exception { + wait(120L, futures); + } + + public static void wait(Long timeout, CommandFuture... futures) throws Exception { + CompletableFuture[] responseFutures = Arrays.stream(futures) + .map(CommandFuture::getResponseFuture) + .toArray(CompletableFuture[]::new); + CompletableFuture.allOf(responseFutures) + .get(timeout, TimeUnit.SECONDS); + } +} diff --git a/src/main/java/com/iflytop/colortitration/app/common/utils/LambdaUtil.java b/src/main/java/com/iflytop/colortitration/app/common/utils/LambdaUtil.java new file mode 100644 index 0000000..cd21ab5 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/common/utils/LambdaUtil.java @@ -0,0 +1,16 @@ +package com.iflytop.colortitration.app.common.utils; + + +import com.iflytop.colortitration.app.common.annotation.CheckedRunnable; + +public class LambdaUtil { + public static Runnable unchecked(CheckedRunnable runnable) { + return () -> { + try { + runnable.run(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }; + } +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/app/controller/TestController.java b/src/main/java/com/iflytop/colortitration/app/controller/TestController.java new file mode 100644 index 0000000..f6233ff --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/controller/TestController.java @@ -0,0 +1,27 @@ +package com.iflytop.colortitration.app.controller; + +import com.iflytop.colortitration.common.result.Result; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 测试用 + */ +@Tag(name = "测试用") +@RestController +@RequestMapping("/api/test") +@RequiredArgsConstructor +@Slf4j +public class TestController { + + @Operation(summary = "启动虚拟模式") + @PostMapping("/virtual") + public Result changeVirtualMode() { + return Result.success(); + } +} diff --git a/src/main/java/com/iflytop/colortitration/app/model/dto/CommandDTO.java b/src/main/java/com/iflytop/colortitration/app/model/dto/CommandDTO.java new file mode 100644 index 0000000..c2070ee --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/model/dto/CommandDTO.java @@ -0,0 +1,67 @@ +package com.iflytop.colortitration.app.model.dto; + +import cn.hutool.json.JSONArray; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import lombok.Data; + +import java.util.Map; + +@Schema(description = "指令") +@Data +public class CommandDTO { + + @NotNull + @Schema(description = "指令id,前端生成唯一ID") + private String commandId; + + @NotNull + @Schema(description = "命令名称") + private String command; + + @Schema(description = "参数") + private Map params; + + // 获取 Double 类型的参数,null 或空字符串时返回 null + public Double getDoubleParam(String key) { + String value = getStringParam(key); + return (value != null && !value.isEmpty()) ? Double.parseDouble(value) : null; + } + + // 获取 String 类型的参数,null 或空字符串时返回 null + public String getStringParam(String key) { + Object value = params.get(key); + // 确保值不是 null 或空字符串,转换成 String 后返回 + return (value != null && !value.toString().isEmpty()) ? value.toString() : null; + } + + // 获取 Integer 类型的参数,null 或空字符串时返回 null + public Integer getIntegerParam(String key) { + String value = getStringParam(key); + return (value != null && !value.isEmpty()) ? Integer.parseInt(value) : null; + } + + // 获取 Boolean 类型的参数,null 或空字符串时返回 null + public Boolean getBooleanParam(String key) { + String value = getStringParam(key); + return (value != null && !value.isEmpty()) ? Boolean.parseBoolean(value) : null; + } + + public JSONObject getJsonObjectParam(String key) { + Object value = params.get(key); + return new JSONObject(value); + + } + + public JSONArray getJSONArrayParam(String key) { + Object value = params.get(key); + return new JSONArray(value); + } + + @Override + public String toString() { + return JSONUtil.toJsonStr(this); + } +} diff --git a/src/main/java/com/iflytop/colortitration/app/model/entity/User.java b/src/main/java/com/iflytop/colortitration/app/model/entity/User.java new file mode 100644 index 0000000..f29ad4a --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/model/entity/User.java @@ -0,0 +1,44 @@ +package com.iflytop.colortitration.app.model.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import com.iflytop.colortitration.common.base.BaseEntity; +import com.iflytop.colortitration.common.enums.EnableStatus; +import com.iflytop.colortitration.app.common.enums.UserRole; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * 用户实体 + */ +@EqualsAndHashCode(callSuper = true) +@Schema(description = "用户") +@TableName("user") +@Data +public class User extends BaseEntity { + + @NotNull + @Schema(description = "用户名") + private String username; + + @NotNull + @Schema(description = "昵称") + private String nickname; + + @NotNull + @Schema(description = "密码") + private String password; + + @NotNull + @Schema(description = "人员角色") + private UserRole role; + + @NotNull + @Schema(description = "是否删除") + private EnableStatus deleted; + + @Schema(description = "是否是系统固定用户") + private EnableStatus fixedUser; + +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/app/service/TestService.java b/src/main/java/com/iflytop/colortitration/app/service/TestService.java new file mode 100644 index 0000000..c4e11e6 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/service/TestService.java @@ -0,0 +1,15 @@ +package com.iflytop.colortitration.app.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +/** + * 测试用 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class TestService { + +} diff --git a/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketMessageType.java b/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketMessageType.java new file mode 100644 index 0000000..9f770bd --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketMessageType.java @@ -0,0 +1,51 @@ +package com.iflytop.colortitration.app.websocket.server; + +public class WebSocketMessageType { + /** + * 设备状态 + */ + public static final String STATUS = "status"; + /** + * 设备报警 + */ + public static final String ALARM = "alarm"; + /** + * 自检移动电机测试 + */ + public static final String SELF_MOVE_TEST = "self_move_test"; + /** + * 指令反馈 + */ + public static final String CMD_RESPONSE = "cmd_response"; + + /** + * 工艺执行步骤反馈 + */ + public static final String CRAFTS_STEP = "crafts_step"; + + /** + * 工艺执行状态反馈 + */ + public static final String CRAFTS_STATE = "crafts_state"; + + /** + * 工艺DEBUG + */ + public static final String CRAFTS_DEBUG = "crafts_debug"; + + /** + * 容器剩余状态 + */ + public static final String CONTAINER = "container"; + + /** + * DEBUG消息推送 + */ + public static final String CMD_DEBUG = "cmd_debug"; + + /** + * 加热倒计时 + */ + public static final String HEAT_COUNTDOWN = "heat_countdown"; + +} diff --git a/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketSender.java b/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketSender.java new file mode 100644 index 0000000..73e2a32 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketSender.java @@ -0,0 +1,45 @@ +package com.iflytop.colortitration.app.websocket.server; + +import cn.hutool.json.JSONUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.management.Notification; + +@Slf4j +@Component +public class WebSocketSender { + + public void push(String type, Object data) { + WebsocketResult websocketResult = new WebsocketResult(); + websocketResult.setType(type); + websocketResult.setData(data); + WebSocketServer.sendMessageToClients(JSONUtil.toJsonStr(websocketResult)); +// log.info("WS::{}", JSONUtil.toJsonStr(websocketResult)); + } + + public void pushCraftsDebug(Object data) { + push(WebSocketMessageType.CRAFTS_DEBUG, data); + } + + public void pushDebug(Object data) { + push(WebSocketMessageType.CMD_DEBUG, data); + } + + public void pushCMDResponse(Object data) { + push(WebSocketMessageType.CMD_RESPONSE, data); + } + + public void pushSelfMoveTest(Object data) { + push(WebSocketMessageType.SELF_MOVE_TEST, data); + } + + public void pushHeatCountdown(Object data) { + push(WebSocketMessageType.HEAT_COUNTDOWN, data); + } + + public void pushNotification(Notification notification) { + push("notification", notification); + } + +} diff --git a/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketServer.java b/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketServer.java new file mode 100644 index 0000000..d043a97 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketServer.java @@ -0,0 +1,51 @@ +package com.iflytop.colortitration.app.websocket.server; + +import jakarta.websocket.*; +import jakarta.websocket.server.ServerEndpoint; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +@Slf4j +@ServerEndpoint("/ws") +@Component +public class WebSocketServer { + private static final Set sessions = Collections.synchronizedSet(new HashSet<>()); + + public static void sendMessageToClients(String message) { + synchronized (sessions) { + for (Session session : sessions) { + try { + session.getBasicRemote().sendText(message); + } catch (Exception e) { + log.error("WS发送给客户端失败 sessionId={}", session.getId(), e); + } + } + } + } + + @OnOpen + public void onOpen(Session session) { + sessions.add(session); + log.info("新连接加入,sessionId={}", session.getId()); + } + + @OnMessage + public void onMessage(String message, Session session) { + log.info("收到消息 sessionId={},内容:{}", session.getId(), message); + } + + @OnClose + public void onClose(Session session) { + sessions.remove(session); + log.info("连接已关闭,sessionId={}", session.getId()); + } + + @OnError + public void onError(Session session, Throwable error) { + log.error("发生错误,sessionId={}", session.getId(), error); + } +} diff --git a/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketServerConfig.java b/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketServerConfig.java new file mode 100644 index 0000000..8f76258 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketServerConfig.java @@ -0,0 +1,14 @@ +package com.iflytop.colortitration.app.websocket.server; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.server.standard.ServerEndpointExporter; + +@Configuration +public class WebSocketServerConfig { + + @Bean + public ServerEndpointExporter serverEndpointExporter() { + return new ServerEndpointExporter(); + } +} diff --git a/src/main/java/com/iflytop/colortitration/app/websocket/server/WebsocketResult.java b/src/main/java/com/iflytop/colortitration/app/websocket/server/WebsocketResult.java new file mode 100644 index 0000000..a8a74f3 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/websocket/server/WebsocketResult.java @@ -0,0 +1,18 @@ +package com.iflytop.colortitration.app.websocket.server; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +public class WebsocketResult { + /** + * 推送类型(指令 cmd,报警 warn ,状态 status) + */ + @Schema(description = "推送类型(指令 cmd,报警 warn ,状态 status)") + private String type; + /** + * 执行结果 + */ + @Schema(description = "推送数据") + private Object data; +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/common/base/BaseEntity.java b/src/main/java/com/iflytop/colortitration/common/base/BaseEntity.java new file mode 100644 index 0000000..8bbc088 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/base/BaseEntity.java @@ -0,0 +1,44 @@ +package com.iflytop.colortitration.common.base; + +import com.baomidou.mybatisplus.annotation.FieldFill; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * 基础实体类 + * 实体类的基类,包含了实体类的公共属性,如创建时间、更新时间、逻辑删除标识等

+ */ +@Data +public class BaseEntity implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * 主键ID + */ + @TableId(type = IdType.AUTO) + private Long id; + + /** + * 创建时间 + */ + @TableField(fill = FieldFill.INSERT) + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime createTime; + + /** + * 更新时间 + */ + @TableField(fill = FieldFill.INSERT_UPDATE) + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime updateTime; + +} diff --git a/src/main/java/com/iflytop/colortitration/common/base/BasePageQuery.java b/src/main/java/com/iflytop/colortitration/common/base/BasePageQuery.java new file mode 100644 index 0000000..d607afb --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/base/BasePageQuery.java @@ -0,0 +1,26 @@ +package com.iflytop.colortitration.common.base; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +/** + * 基础分页请求对象 + */ +@Data +@Schema +public class BasePageQuery implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + @Schema(description = "页码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + private int pageNum = 1; + + @Schema(description = "每页记录数", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") + private int pageSize = 10; + + +} diff --git a/src/main/java/com/iflytop/colortitration/common/base/IBaseEnum.java b/src/main/java/com/iflytop/colortitration/common/base/IBaseEnum.java new file mode 100644 index 0000000..d6961a5 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/base/IBaseEnum.java @@ -0,0 +1,84 @@ +package com.iflytop.colortitration.common.base; + + +import cn.hutool.core.util.ObjectUtil; + +import java.util.EnumSet; +import java.util.Objects; + +/** + * 枚举通用接口 + */ +public interface IBaseEnum { + + /** + * 根据值获取枚举 + * + * @param value + * @param clazz + * @param 枚举 + * @return + */ + static & IBaseEnum> E getEnumByValue(Object value, Class clazz) { + Objects.requireNonNull(value); + EnumSet allEnums = EnumSet.allOf(clazz); // 获取类型下的所有枚举 + E matchEnum = allEnums.stream() + .filter(e -> ObjectUtil.equal(e.getValue(), value)) + .findFirst() + .orElse(null); + return matchEnum; + } + + /** + * 根据文本标签获取值 + * + * @param value + * @param clazz + * @param + * @return + */ + static & IBaseEnum> String getLabelByValue(Object value, Class clazz) { + Objects.requireNonNull(value); + EnumSet allEnums = EnumSet.allOf(clazz); // 获取类型下的所有枚举 + E matchEnum = allEnums.stream() + .filter(e -> ObjectUtil.equal(e.getValue(), value)) + .findFirst() + .orElse(null); + + String label = null; + if (matchEnum != null) { + label = matchEnum.getLabel(); + } + return label; + } + + /** + * 根据文本标签获取值 + * + * @param label + * @param clazz + * @param + * @return + */ + static & IBaseEnum> Object getValueByLabel(String label, Class clazz) { + Objects.requireNonNull(label); + EnumSet allEnums = EnumSet.allOf(clazz); // 获取类型下的所有枚举 + String finalLabel = label; + E matchEnum = allEnums.stream() + .filter(e -> ObjectUtil.equal(e.getLabel(), finalLabel)) + .findFirst() + .orElse(null); + + Object value = null; + if (matchEnum != null) { + value = matchEnum.getValue(); + } + return value; + } + + T getValue(); + + String getLabel(); + + +} diff --git a/src/main/java/com/iflytop/colortitration/common/base/Point2D.java b/src/main/java/com/iflytop/colortitration/common/base/Point2D.java new file mode 100644 index 0000000..f07ed0a --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/base/Point2D.java @@ -0,0 +1,20 @@ +package com.iflytop.colortitration.common.base; + + +import lombok.Data; + +@Data +public class Point2D { + public Double x; + public Double y; + + public Point2D() { + } + + public Point2D(Double x, Double y) { + this.x = x; + this.y = y; + } + + +} diff --git a/src/main/java/com/iflytop/colortitration/common/base/Point3D.java b/src/main/java/com/iflytop/colortitration/common/base/Point3D.java new file mode 100644 index 0000000..74c47b0 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/base/Point3D.java @@ -0,0 +1,22 @@ +package com.iflytop.colortitration.common.base; + + +import lombok.Data; + +@Data +public class Point3D { + public Double x; + public Double y; + public Double z; + + public Point3D() { + } + + public Point3D(Double x, Double y, Double z) { + this.x = x; + this.y = y; + this.z = z; + } + + +} diff --git a/src/main/java/com/iflytop/colortitration/common/cmd/DeviceCommandParams.java b/src/main/java/com/iflytop/colortitration/common/cmd/DeviceCommandParams.java new file mode 100644 index 0000000..1b5ef05 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/cmd/DeviceCommandParams.java @@ -0,0 +1,11 @@ +package com.iflytop.colortitration.common.cmd; + + +import lombok.Data; + +@Data +public class DeviceCommandParams { + private Double x; + private Double y; + private Double z; +} diff --git a/src/main/java/com/iflytop/colortitration/common/enums/Action.java b/src/main/java/com/iflytop/colortitration/common/enums/Action.java new file mode 100644 index 0000000..bc94b35 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/enums/Action.java @@ -0,0 +1,9 @@ +package com.iflytop.colortitration.common.enums; + +public enum Action { + move, move_by, move_joint, move_point, origin, rotate, move_end, + + open, close, stop, start, set, get, + open_power, close_power, open_circle, close_circle, + open_heart, close_heart, open_cool, close_cool, take_photo +} diff --git a/src/main/java/com/iflytop/colortitration/common/enums/Device.java b/src/main/java/com/iflytop/colortitration/common/enums/Device.java new file mode 100644 index 0000000..494e1da --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/enums/Device.java @@ -0,0 +1,50 @@ +package com.iflytop.colortitration.common.enums; + +public enum Device { + door_motor, + shake_motor, + cap_motor, + dual_robot, + gantry_x, + gantry_y, + gantry_z, + heater_motor_1, + heater_motor_2, + heater_motor_3, + heater_motor_4, + heater_motor_5, + heater_motor_6, + acid_pump_1, + acid_pump_2, + acid_pump_3, + acid_pump_4, + acid_pump_5, + acid_pump_6, + acid_pump_7, + acid_pump_8, + claw, + fan_1, + fan_2, + fan_3, + fan_4, + fan_5, + fan_6, + water_pump_power, + ventilator_power, + heat_rod_1, + heat_rod_2, + heat_rod_3, + heat_rod_4, + heat_rod_5, + heat_rod_6, + vacuum_pump_valve_1, + vacuum_pump_valve_2, + vacuum_pump_valve_3, + vacuum_pump_valve_4, + vacuum_pump_valve_5, + vacuum_pump_valve_6, + tricolor_light, + fill_light, + cold_trap, + photo +} diff --git a/src/main/java/com/iflytop/colortitration/common/enums/EnableStatus.java b/src/main/java/com/iflytop/colortitration/common/enums/EnableStatus.java new file mode 100644 index 0000000..92bb195 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/enums/EnableStatus.java @@ -0,0 +1,6 @@ +package com.iflytop.colortitration.common.enums; + +public enum EnableStatus { + ENABLE, + DISABLE +} diff --git a/src/main/java/com/iflytop/colortitration/common/exception/AppException.java b/src/main/java/com/iflytop/colortitration/common/exception/AppException.java new file mode 100644 index 0000000..efd1203 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/exception/AppException.java @@ -0,0 +1,21 @@ +package com.iflytop.colortitration.common.exception; + +import com.iflytop.colortitration.common.result.IResultCode; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class AppException extends RuntimeException { + private final IResultCode resultCode; + + public AppException(IResultCode resultCode) { + super(resultCode.getMsg()); + this.resultCode = resultCode; + } + + @Override + public String toString() { + return "AppException{" + "code='" + resultCode.getCode() + ", msg=" + resultCode.getMsg() + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/common/result/IResultCode.java b/src/main/java/com/iflytop/colortitration/common/result/IResultCode.java new file mode 100644 index 0000000..f17ed8c --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/result/IResultCode.java @@ -0,0 +1,12 @@ +package com.iflytop.colortitration.common.result; + +/** + * 响应码接口 + **/ +public interface IResultCode { + + String getCode(); + + String getMsg(); + +} diff --git a/src/main/java/com/iflytop/colortitration/common/result/PageResult.java b/src/main/java/com/iflytop/colortitration/common/result/PageResult.java new file mode 100644 index 0000000..3542686 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/result/PageResult.java @@ -0,0 +1,43 @@ +package com.iflytop.colortitration.common.result; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * 分页响应结构体 + */ +@Data +public class PageResult implements Serializable { + + private String code; + + private Data data; + + private String msg; + + public static PageResult success(IPage page) { + PageResult result = new PageResult<>(); + result.setCode(ResultCode.SUCCESS.getCode()); + + Data data = new Data<>(); + data.setList(page.getRecords()); + data.setTotal(page.getTotal()); + + result.setData(data); + result.setMsg(ResultCode.SUCCESS.getMsg()); + return result; + } + + @lombok.Data + public static class Data { + + private List list; + + private long total; + + } + +} diff --git a/src/main/java/com/iflytop/colortitration/common/result/Result.java b/src/main/java/com/iflytop/colortitration/common/result/Result.java new file mode 100644 index 0000000..9638c0a --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/result/Result.java @@ -0,0 +1,75 @@ +package com.iflytop.colortitration.common.result; + +import cn.hutool.core.util.StrUtil; +import lombok.Data; + +import java.io.Serializable; + +/** + * 统一响应结构体 + **/ +@Data +public class Result implements Serializable { + + private String code; + + private T data; + + private String msg; + + public static Result success() { + return success(null); + } + + public static Result success(T data) { + Result result = new Result<>(); + result.setCode(ResultCode.SUCCESS.getCode()); + result.setMsg(ResultCode.SUCCESS.getMsg()); + result.setData(data); + return result; + } + + public static Result failed() { + return result(ResultCode.SYSTEM_ERROR.getCode(), ResultCode.SYSTEM_ERROR.getMsg(), null); + } + + public static Result failed(String msg) { + return result(ResultCode.SYSTEM_ERROR.getCode(), msg, null); + } + + public static Result judge(boolean status) { + if (status) { + return success(); + } else { + return failed(); + } + } + + public static Result failed(IResultCode resultCode) { + return result(resultCode.getCode(), resultCode.getMsg(), null); + } + + public static Result failed(IResultCode resultCode, String msg) { + return result(resultCode.getCode(), StrUtil.isNotBlank(msg) ? msg : resultCode.getMsg(), null); + } + + public static Result failed(String code, String msg) { + return result(code, msg, null); + } + + private static Result result(IResultCode resultCode, T data) { + return result(resultCode.getCode(), resultCode.getMsg(), data); + } + + private static Result result(String code, String msg, T data) { + Result result = new Result<>(); + result.setCode(code); + result.setData(data); + result.setMsg(msg); + return result; + } + + public static boolean isSuccess(Result result) { + return result != null && ResultCode.SUCCESS.getCode().equals(result.getCode()); + } +} diff --git a/src/main/java/com/iflytop/colortitration/common/result/ResultCode.java b/src/main/java/com/iflytop/colortitration/common/result/ResultCode.java new file mode 100644 index 0000000..274207a --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/result/ResultCode.java @@ -0,0 +1,97 @@ +package com.iflytop.colortitration.common.result; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.io.Serializable; + +/** + * 统一前端响应码定义 + */ +@Getter +@AllArgsConstructor +public enum ResultCode implements IResultCode, Serializable { + //================================ 通用 ================================= + SUCCESS("0", "成功"), + FAILED("-1", "未知错误"), + + //============================ 1xxx:请求 & 参数 ============================ + INVALID_PARAMETER("1000", "参数无效或缺失"), + PARAMETER_TYPE_MISMATCH("1001", "参数类型不匹配"), + PARAMETER_OUT_OF_RANGE("1002", "参数超出允许范围"), + + //============================ 2xxx:认证 & 授权 ============================ + UNAUTHORIZED("2000", "未认证或登录失效"), + FORBIDDEN("2001", "无访问权限"), + TOKEN_EXPIRED("2002", "Token 已过期"), + TOKEN_INVALID("2003", "Token 无效"), + + //============================ 3xxx:资源访问 ============================ + NOT_FOUND("3000", "资源不存在"), + METHOD_NOT_ALLOWED("3001", "不支持的请求方法"), + + //============================ 4xxx:业务错误 ============================ + USER_NOT_FOUND("4000", "用户不存在"), + USER_ALREADY_EXISTS("4001", "用户已存在"), + INVALID_CREDENTIALS("4002", "用户名或密码错误"), + OPERATION_NOT_ALLOWED("4003", "业务操作不允许"), + DATA_ALREADY_EXISTS("4004", "数据已存在"), + DATA_ALREADY_NOT_EXISTS("4005", "数据不存在"), + + CRAFT_RUNNING("4101", "工艺正在执行"), + CRAFT_CONTEXT_NULL("4102", "请先配置该加热区工艺"), + CRAFT_NO_TRAY("4005", "工艺未找到托盘"), + + CONTAINER_NOT_FOUND("4201", "未找到对应溶液容器"), + //============================ 5xxx:系统 & 第三方 ============================ + SYSTEM_ERROR("5000", "系统内部错误"), + SERVICE_UNAVAILABLE("5001", "服务暂不可用"), + EXTERNAL_API_ERROR("5002", "第三方服务调用失败"), + COMMAND_EXEC_TIMEOUT("5003", "命令执行超时"), + HARDWARE_ERROR("5004", "硬件错误"), + EMERGENCY_STOP("5555", "设备急停中"), + //============================ 6xxx:设备、指令相关 ============================ + COMMAND_NOT_FOUND("6000", "指令未找到"), + COMMAND_ALREADY_EXECUTING("6001", "指令正在执行,无法重复执行"), + SENSOR_STATUS_FAILED("6010", "获取传感器状态失败"), + TARGET_HEAT_MODULE_OCCUPIED("6021", "目标加热模块被占用"), + TARGET_HEAT_MODULE_NO_TRAY("6022", "目标加热模块无托盘"), + SOLUTION_MODULE_NO_TRAY("6023", "加液模块无托盘"), + CAP_LIFT_ERROR("6024", "拍子升降错误"), + CMD_BUSY("6025", "设备忙,请稍后"), + HEAT_MODULE_NO_IDLE("6026", "加热模块无空闲"), + CAP_MODULE_NO_CAP("6027", "拍子存放区未检测到拍子"), + SOLUTION_MODULE_OCCUPIED("6028", "加液模块被占用"), + ; + /** 状态码 */ + private final String code; + /** 提示信息 */ + private final String msg; + + @Override + public String getCode() { + return code; + } + + @Override + public String getMsg() { + return msg; + } + + /** + * 根据 code 获取枚举 + */ + public static ResultCode parse(String code) { + for (ResultCode item : values()) { + if (item.code.equals(code)) { + return item; + } + } + return FAILED; + } + + @Override + public String toString() { + return "{\"code\":\"" + code + "\", \"msg\":\"" + msg + "\"}"; + } +} diff --git a/src/main/java/com/iflytop/colortitration/common/utils/ByteArray.java b/src/main/java/com/iflytop/colortitration/common/utils/ByteArray.java new file mode 100644 index 0000000..4e1db29 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/utils/ByteArray.java @@ -0,0 +1,125 @@ +// Source code is decompiled from a .class file using FernFlower decompiler. +package com.iflytop.colortitration.common.utils; + +import org.springframework.lang.NonNull; + +public class ByteArray { + public ByteArray() { + } + + public static int readU8bit(byte[] code, int index) { + if (index >= code.length) + return 0; + return code[index] & 255; + } + + public static int readS8bit(byte[] code, int index) { + if (index >= code.length) + return 0; + + return code[index]; + } + + public static int readU16bit(byte[] code, int index) { + if (index + 1 >= code.length) + return 0; + return (code[index + 1] & 255) << 8 | code[index] & 255; + } + + public static void setU16bit(byte[] code, int index, int value) { + code[index + 1] = (byte) (value >> 8); + code[index] = (byte) value; + } + + public static void setU8(byte[] code, int off, int value) { + code[off] = (byte) value; + } + + public static int readS16bit(byte[] code, int index) { + if (index + 1 >= code.length) + return 0; + + return code[index + 1] << 8 | code[index] & 255; + } + + public static int read32bit(byte[] code, int index) { + if (index + 3 >= code.length) + return 0; + return code[index + 3] << 24 | (code[index + 2] & 255) << 16 | (code[index + 1] & 255) << 8 | code[index] & 255; + } + + public static Integer[] read32bitArray(byte[] code) { + int count = code.length / 4; + Integer[] array = new Integer[count]; + for (int i = 0; i < count; i++) { + array[i] = read32bit(code, i * 4); + } + return array; + } + + public static Integer[] read16bitArray(byte[] code) { + int count = code.length / 2; + Integer[] array = new Integer[count]; + for (int i = 0; i < count; i++) { + array[i] = readS16bit(code, i * 2); + } + return array; + } + + public static Integer[] readU16bitArray(byte[] code) { + int count = code.length / 2; + Integer[] array = new Integer[count]; + for (int i = 0; i < count; i++) { + array[i] = readU16bit(code, i * 2); + } + return array; + } + + + public static String toByteString(byte[] arrary) { + StringBuilder sb = new StringBuilder(); + for (byte b : arrary) { + sb.append(String.format("%02X", b)); + } + return sb.toString(); + } + + public static byte[] hexStringToBytes(@NonNull String str) { + if (str.isEmpty()) { + return new byte[0]; + } else { + byte[] byteArray = new byte[str.length() / 2]; + for (int i = 0; i < byteArray.length; i++) { + int high = Character.digit(str.charAt(i * 2), 16); + int low = Character.digit(str.charAt(i * 2 + 1), 16); + if (high == -1 || low == -1) { + return null; + } + byteArray[i] = (byte) (high * 16 + low); + } + return byteArray; + } + } + + + public static byte[] concat(byte[]... arrays) { + int length = 0; + for (byte[] array : arrays) { + length += array.length; + } + + byte[] result = new byte[length]; + int destPos = 0; + for (byte[] array : arrays) { + System.arraycopy(array, 0, result, destPos, array.length); + destPos += array.length; + } + + return result; + } + + // public static void main(String[] args) { + // byte[] bytes = new byte[]{0x01, 0x02, 0x03, 0x04}; + // System.out.println(toByteString(bytes)); + // } +} diff --git a/src/main/java/com/iflytop/colortitration/common/utils/LocalDateTimeUtil.java b/src/main/java/com/iflytop/colortitration/common/utils/LocalDateTimeUtil.java new file mode 100644 index 0000000..53c9b1c --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/utils/LocalDateTimeUtil.java @@ -0,0 +1,15 @@ +package com.iflytop.colortitration.common.utils; + +public class LocalDateTimeUtil { + + /** + * 秒数格式化为 “mm:ss” 格式。 + */ + public static String formatSecondsToHMS(long diffSeconds) { + long absSeconds = Math.abs(diffSeconds); + long minutes = absSeconds / 60; + long seconds = absSeconds % 60; + return String.format("%02d:%02d", minutes, seconds); + } + +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/colortitration/config/MybatisPlusConfig.java b/src/main/java/com/iflytop/colortitration/config/MybatisPlusConfig.java new file mode 100644 index 0000000..756a0c3 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/config/MybatisPlusConfig.java @@ -0,0 +1,61 @@ +package com.iflytop.colortitration.config; + +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.core.config.GlobalConfig; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import com.iflytop.colortitration.app.common.handler.MyMetaObjectHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +/** + * + */ +@EnableTransactionManagement +@Configuration +public class MybatisPlusConfig { + + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor() { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + // 乐观锁插件 + interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); + // 分页插件 + interceptor.addInnerInterceptor(paginationInnerInterceptor()); + + return interceptor; + } + + /** + * 分页插件,自动识别数据库类型 + * ... + */ + public PaginationInnerInterceptor paginationInnerInterceptor() { + PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); + // 设置数据库类型 + paginationInnerInterceptor.setDbType(DbType.SQLITE); + // 设置最大单页限制数量,默认 500 条,-1 不受限制 + paginationInnerInterceptor.setMaxLimit(-1L); + return paginationInnerInterceptor; + } + + /** + * 乐观锁插件 + * ... + */ + public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() { + return new OptimisticLockerInnerInterceptor(); + } + + /** + * 自动填充数据库创建人、创建时间、更新人、更新时间 + */ + @Bean + public GlobalConfig globalConfig() { + GlobalConfig globalConfig = new GlobalConfig(); + globalConfig.setMetaObjectHandler(new MyMetaObjectHandler()); + return globalConfig; + } +} diff --git a/src/main/java/com/iflytop/colortitration/config/SwaggerConfig.java b/src/main/java/com/iflytop/colortitration/config/SwaggerConfig.java new file mode 100644 index 0000000..7eb605a --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/config/SwaggerConfig.java @@ -0,0 +1,68 @@ +package com.iflytop.colortitration.config; + +import cn.hutool.core.util.ArrayUtil; +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.security.SecurityScheme; +import org.springdoc.core.customizers.GlobalOpenApiCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpHeaders; +import org.springframework.util.AntPathMatcher; + +import java.util.stream.Stream; + +/*** + * 创建Swagger配置 + */ +@Configuration +public class SwaggerConfig { + + @Bean + public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() { + return openApi -> { + // 全局添加Authorization + if (openApi.getPaths() != null) { + openApi.getPaths().forEach((path, pathItem) -> { + + // 忽略认证的请求无需携带 Authorization + String[] ignoreUrls = {"/api/auth/login"}; + if (ArrayUtil.isNotEmpty(ignoreUrls)) { + // Ant 匹配忽略的路径,不添加Authorization + AntPathMatcher antPathMatcher = new AntPathMatcher(); + if (Stream.of(ignoreUrls).anyMatch(ignoreUrl -> antPathMatcher.match(ignoreUrl, path))) { + return; + } + } + + // 其他接口统一添加Authorization +// pathItem.readOperations() +// .forEach(operation -> +// operation.addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION)) +// ); + }); + } + }; + } + + @Bean + public OpenAPI customOpenAPI() { + return new OpenAPI() + .info(new Info() + .title("系统API") + .version("1.0")) // 配置全局鉴权参数-Authorize + .components(new Components() + .addSecuritySchemes(HttpHeaders.AUTHORIZATION, + new SecurityScheme() + .name(HttpHeaders.AUTHORIZATION) + .type(SecurityScheme.Type.APIKEY) + .in(SecurityScheme.In.HEADER) + .scheme("Bearer") + .bearerFormat("JWT") + ) + ); + } + + +} diff --git a/src/main/java/com/iflytop/colortitration/config/WebConfig.java b/src/main/java/com/iflytop/colortitration/config/WebConfig.java new file mode 100644 index 0000000..3003ad0 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/config/WebConfig.java @@ -0,0 +1,16 @@ +package com.iflytop.colortitration.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/api/**") + .allowedOrigins("*") + .allowedMethods("GET", "POST", "PUT", "DELETE") + .allowedHeaders("*"); + } +} \ No newline at end of file diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml new file mode 100644 index 0000000..5a29946 --- /dev/null +++ b/src/main/resources/application-dev.yml @@ -0,0 +1,47 @@ +server: + servlet: + context-path: / + port: 8080 + +spring: + application: + name: graphite_digestion_service + datasource: + url: jdbc:sqlite:db/app.db + driver-class-name: org.sqlite.JDBC + sql: + init: + #always embedded never + mode: always + schema-locations: classpath:/sql/init.sql + +mybatis-plus: + configuration: + # 如果需要加载 XML 文件(自定义 SQL),可配置 mapper-locations: + mapper-locations: classpath*:mapper/*.xml + +#开启 SQL 打印,调试时方便查看 +logging: + level: + root: INFO + org.mybatis: DEBUG + +springdoc: + default-flat-param-object: true + +device.enableCanBus: true + +iflytophald: + ip: 127.0.0.1 + cmdch.port: 19004 + datach.port: 19005 + +#window config +#modbus: +# port: COM18 +# baudrate: 9600 + +#工控机 config +modbus: + port: ttyS1 + baudrate: 9600 diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml new file mode 100644 index 0000000..5a29946 --- /dev/null +++ b/src/main/resources/application-prod.yml @@ -0,0 +1,47 @@ +server: + servlet: + context-path: / + port: 8080 + +spring: + application: + name: graphite_digestion_service + datasource: + url: jdbc:sqlite:db/app.db + driver-class-name: org.sqlite.JDBC + sql: + init: + #always embedded never + mode: always + schema-locations: classpath:/sql/init.sql + +mybatis-plus: + configuration: + # 如果需要加载 XML 文件(自定义 SQL),可配置 mapper-locations: + mapper-locations: classpath*:mapper/*.xml + +#开启 SQL 打印,调试时方便查看 +logging: + level: + root: INFO + org.mybatis: DEBUG + +springdoc: + default-flat-param-object: true + +device.enableCanBus: true + +iflytophald: + ip: 127.0.0.1 + cmdch.port: 19004 + datach.port: 19005 + +#window config +#modbus: +# port: COM18 +# baudrate: 9600 + +#工控机 config +modbus: + port: ttyS1 + baudrate: 9600 diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml new file mode 100644 index 0000000..5a29946 --- /dev/null +++ b/src/main/resources/application-test.yml @@ -0,0 +1,47 @@ +server: + servlet: + context-path: / + port: 8080 + +spring: + application: + name: graphite_digestion_service + datasource: + url: jdbc:sqlite:db/app.db + driver-class-name: org.sqlite.JDBC + sql: + init: + #always embedded never + mode: always + schema-locations: classpath:/sql/init.sql + +mybatis-plus: + configuration: + # 如果需要加载 XML 文件(自定义 SQL),可配置 mapper-locations: + mapper-locations: classpath*:mapper/*.xml + +#开启 SQL 打印,调试时方便查看 +logging: + level: + root: INFO + org.mybatis: DEBUG + +springdoc: + default-flat-param-object: true + +device.enableCanBus: true + +iflytophald: + ip: 127.0.0.1 + cmdch.port: 19004 + datach.port: 19005 + +#window config +#modbus: +# port: COM18 +# baudrate: 9600 + +#工控机 config +modbus: + port: ttyS1 + baudrate: 9600 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..caf4dfc --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,3 @@ +spring: + profiles: + active: dev \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..0e6c853 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,58 @@ + + + + + + + + + + ${LOG_PATTERN} + + + + + + ${LOG_PATH}/sys-info.log + + ${LOG_PATH}/history/info/sys-info.%d{yyyy-MM-dd}.%i.log.zip + 20MB + 7 + 200MB + + + ${LOG_PATTERN} + + + INFO + + + + + + ${LOG_PATH}/sys-error.log + + ${LOG_PATH}/history/error/sys-error.%d{yyyy-MM-dd}.%i.log.zip + 20MB + 7 + 200MB + + + ${LOG_PATTERN} + + + ERROR + + + + + + + + + + + + + + diff --git a/src/main/resources/sql/init.sql b/src/main/resources/sql/init.sql new file mode 100644 index 0000000..ea6a39c --- /dev/null +++ b/src/main/resources/sql/init.sql @@ -0,0 +1,17 @@ +-- 用户 表 +CREATE TABLE IF NOT EXISTS user +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT UNIQUE, + nickname TEXT, + password TEXT, + role TEXT, + fixed_user TEXT, + deleted TEXT, + create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +INSERT OR IGNORE INTO user (username, nickname, password, role, fixed_user, deleted) +VALUES ('admin', 'Admin', '123456', 'ADMIN', 'ENABLE', 'DISABLE'), + ('test', 'test', '123456', 'ADMIN', 'ENABLE', 'DISABLE'); diff --git a/src/main/resources/static/favicon.ico b/src/main/resources/static/favicon.ico new file mode 100644 index 0000000..cd20d77 Binary files /dev/null and b/src/main/resources/static/favicon.ico differ diff --git a/src/test/java/com/iflytop/colortitration/ColorTitrationApplicationTests.java b/src/test/java/com/iflytop/colortitration/ColorTitrationApplicationTests.java new file mode 100644 index 0000000..001f4f1 --- /dev/null +++ b/src/test/java/com/iflytop/colortitration/ColorTitrationApplicationTests.java @@ -0,0 +1,13 @@ +package com.iflytop.colortitration; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ColorTitrationApplicationTests { + + @Test + void contextLoads() { + } + +}