Browse Source

创建项目

master
白凤吉 2 weeks ago
commit
98f4713be1
  1. 3
      .gitattributes
  2. 37
      .gitignore
  3. 55
      build.gradle
  4. BIN
      gradle/wrapper/gradle-wrapper.jar
  5. 7
      gradle/wrapper/gradle-wrapper.properties
  6. 251
      gradlew
  7. 94
      gradlew.bat
  8. 1
      settings.gradle
  9. 13
      src/main/java/com/iflytop/colortitration/ColorTitrationApplication.java
  10. 6
      src/main/java/com/iflytop/colortitration/app/common/annotation/CheckedRunnable.java
  11. 12
      src/main/java/com/iflytop/colortitration/app/common/annotation/CommandMapping.java
  12. 55
      src/main/java/com/iflytop/colortitration/app/common/command/CommandFuture.java
  13. 10
      src/main/java/com/iflytop/colortitration/app/common/command/CommandHandler.java
  14. 39
      src/main/java/com/iflytop/colortitration/app/common/command/CyclicNumberGenerator.java
  15. 35
      src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommand.java
  16. 19
      src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommandBundle.java
  17. 11
      src/main/java/com/iflytop/colortitration/app/common/command/DeviceCommandGenerator.java
  18. 47
      src/main/java/com/iflytop/colortitration/app/common/constant/CommandStatus.java
  19. 7
      src/main/java/com/iflytop/colortitration/app/common/enums/UserRole.java
  20. 82
      src/main/java/com/iflytop/colortitration/app/common/handler/GlobalExceptionHandler.java
  21. 36
      src/main/java/com/iflytop/colortitration/app/common/handler/MyMetaObjectHandler.java
  22. 23
      src/main/java/com/iflytop/colortitration/app/common/utils/CommandUtil.java
  23. 16
      src/main/java/com/iflytop/colortitration/app/common/utils/LambdaUtil.java
  24. 27
      src/main/java/com/iflytop/colortitration/app/controller/TestController.java
  25. 67
      src/main/java/com/iflytop/colortitration/app/model/dto/CommandDTO.java
  26. 44
      src/main/java/com/iflytop/colortitration/app/model/entity/User.java
  27. 15
      src/main/java/com/iflytop/colortitration/app/service/TestService.java
  28. 51
      src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketMessageType.java
  29. 45
      src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketSender.java
  30. 51
      src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketServer.java
  31. 14
      src/main/java/com/iflytop/colortitration/app/websocket/server/WebSocketServerConfig.java
  32. 18
      src/main/java/com/iflytop/colortitration/app/websocket/server/WebsocketResult.java
  33. 44
      src/main/java/com/iflytop/colortitration/common/base/BaseEntity.java
  34. 26
      src/main/java/com/iflytop/colortitration/common/base/BasePageQuery.java
  35. 84
      src/main/java/com/iflytop/colortitration/common/base/IBaseEnum.java
  36. 20
      src/main/java/com/iflytop/colortitration/common/base/Point2D.java
  37. 22
      src/main/java/com/iflytop/colortitration/common/base/Point3D.java
  38. 11
      src/main/java/com/iflytop/colortitration/common/cmd/DeviceCommandParams.java
  39. 9
      src/main/java/com/iflytop/colortitration/common/enums/Action.java
  40. 50
      src/main/java/com/iflytop/colortitration/common/enums/Device.java
  41. 6
      src/main/java/com/iflytop/colortitration/common/enums/EnableStatus.java
  42. 21
      src/main/java/com/iflytop/colortitration/common/exception/AppException.java
  43. 12
      src/main/java/com/iflytop/colortitration/common/result/IResultCode.java
  44. 43
      src/main/java/com/iflytop/colortitration/common/result/PageResult.java
  45. 75
      src/main/java/com/iflytop/colortitration/common/result/Result.java
  46. 97
      src/main/java/com/iflytop/colortitration/common/result/ResultCode.java
  47. 125
      src/main/java/com/iflytop/colortitration/common/utils/ByteArray.java
  48. 15
      src/main/java/com/iflytop/colortitration/common/utils/LocalDateTimeUtil.java
  49. 61
      src/main/java/com/iflytop/colortitration/config/MybatisPlusConfig.java
  50. 68
      src/main/java/com/iflytop/colortitration/config/SwaggerConfig.java
  51. 16
      src/main/java/com/iflytop/colortitration/config/WebConfig.java
  52. 47
      src/main/resources/application-dev.yml
  53. 47
      src/main/resources/application-prod.yml
  54. 47
      src/main/resources/application-test.yml
  55. 3
      src/main/resources/application.yml
  56. 58
      src/main/resources/logback.xml
  57. 17
      src/main/resources/sql/init.sql
  58. BIN
      src/main/resources/static/favicon.ico
  59. 13
      src/test/java/com/iflytop/colortitration/ColorTitrationApplicationTests.java

3
.gitattributes

@ -0,0 +1,3 @@
/gradlew text eol=lf
*.bat text eol=crlf
*.jar binary

37
.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/

55
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 MVCTomcat
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 MapperService
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'// JUnitMockito
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'// JUnit JUnit Platform IDE
}
tasks.named('test') {
useJUnitPlatform()
}

BIN
gradle/wrapper/gradle-wrapper.jar

7
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

251
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" "$@"

94
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

1
settings.gradle

@ -0,0 +1 @@
rootProject.name = 'color-titration'

13
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);
}
}

6
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;
}

12
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();
}

55
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<JSONObject> 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();
}
}

10
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<Void> handle(CommandDTO commandDTO) throws Exception;
}

39
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;
}
}

35
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;
}

19
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;
}

11
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 {
}

47
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";
}

7
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;
}

82
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());
}
}

36
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);
}
}

23
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);
}
}

16
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);
}
};
}
}

27
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();
}
}

67
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<String, Object> 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);
}
}

44
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;
}

15
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 {
}

51
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";
}

45
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);
}
}

51
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<Session> 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);
}
}

14
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();
}
}

18
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;
}

44
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;
/**
* 基础实体类
* 实体类的基类包含了实体类的公共属性如创建时间更新时间逻辑删除标识等</p>
*/
@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;
}

26
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;
}

84
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<T> {
/**
* 根据值获取枚举
*
* @param value
* @param clazz
* @param <E> 枚举
* @return
*/
static <E extends Enum<E> & IBaseEnum> E getEnumByValue(Object value, Class<E> clazz) {
Objects.requireNonNull(value);
EnumSet<E> 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 <E>
* @return
*/
static <E extends Enum<E> & IBaseEnum> String getLabelByValue(Object value, Class<E> clazz) {
Objects.requireNonNull(value);
EnumSet<E> 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 <E>
* @return
*/
static <E extends Enum<E> & IBaseEnum> Object getValueByLabel(String label, Class<E> clazz) {
Objects.requireNonNull(label);
EnumSet<E> 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();
}

20
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;
}
}

22
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;
}
}

11
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;
}

9
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
}

50
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
}

6
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
}

21
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() + '}';
}
}

12
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();
}

43
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<T> implements Serializable {
private String code;
private Data<T> data;
private String msg;
public static <T> PageResult<T> success(IPage<T> page) {
PageResult<T> result = new PageResult<>();
result.setCode(ResultCode.SUCCESS.getCode());
Data<T> 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<T> {
private List<T> list;
private long total;
}
}

75
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<T> implements Serializable {
private String code;
private T data;
private String msg;
public static <T> Result<T> success() {
return success(null);
}
public static <T> Result<T> success(T data) {
Result<T> result = new Result<>();
result.setCode(ResultCode.SUCCESS.getCode());
result.setMsg(ResultCode.SUCCESS.getMsg());
result.setData(data);
return result;
}
public static <T> Result<T> failed() {
return result(ResultCode.SYSTEM_ERROR.getCode(), ResultCode.SYSTEM_ERROR.getMsg(), null);
}
public static <T> Result<T> failed(String msg) {
return result(ResultCode.SYSTEM_ERROR.getCode(), msg, null);
}
public static <T> Result<T> judge(boolean status) {
if (status) {
return success();
} else {
return failed();
}
}
public static <T> Result<T> failed(IResultCode resultCode) {
return result(resultCode.getCode(), resultCode.getMsg(), null);
}
public static <T> Result<T> failed(IResultCode resultCode, String msg) {
return result(resultCode.getCode(), StrUtil.isNotBlank(msg) ? msg : resultCode.getMsg(), null);
}
public static <T> Result<T> failed(String code, String msg) {
return result(code, msg, null);
}
private static <T> Result<T> result(IResultCode resultCode, T data) {
return result(resultCode.getCode(), resultCode.getMsg(), data);
}
private static <T> Result<T> result(String code, String msg, T data) {
Result<T> 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());
}
}

97
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 + "\"}";
}
}

125
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));
// }
}

15
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);
}
}

61
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;
}
/**
* 分页插件自动识别数据库类型
* <a href="https://baomidou.com/guide/interceptor-pagination.html">...</a>
*/
public PaginationInnerInterceptor paginationInnerInterceptor() {
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 设置数据库类型
paginationInnerInterceptor.setDbType(DbType.SQLITE);
// 设置最大单页限制数量默认 500 -1 不受限制
paginationInnerInterceptor.setMaxLimit(-1L);
return paginationInnerInterceptor;
}
/**
* 乐观锁插件
* <a href="https://baomidou.com/guide/interceptor-optimistic-locker.html">...</a>
*/
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
return new OptimisticLockerInnerInterceptor();
}
/**
* 自动填充数据库创建人创建时间更新人更新时间
*/
@Bean
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setMetaObjectHandler(new MyMetaObjectHandler());
return globalConfig;
}
}

68
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")
)
);
}
}

16
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("*");
}
}

47
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

47
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

47
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

3
src/main/resources/application.yml

@ -0,0 +1,3 @@
spring:
profiles:
active: dev

58
src/main/resources/logback.xml

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<property name="LOG_PATH" value="./logs"/>
<property name="LOG_PATTERN" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
<!-- 控制台 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- 系统日志(INFO 及以上),按日期+大小滚动,归档后压缩 -->
<appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/sys-info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/history/info/sys-info.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>20MB</maxFileSize>
<maxHistory>7</maxHistory>
<totalSizeCap>200MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
<!-- 系统错误日志(ERROR 及以上),按日期+大小滚动,归档后压缩 -->
<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/sys-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/history/error/sys-error.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
<maxFileSize>20MB</maxFileSize>
<maxHistory>7</maxHistory>
<totalSizeCap>200MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
<!-- 包级别日志 -->
<logger name="com.iflytop" level="DEBUG"/>
<logger name="org.springframework" level="WARN"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE_INFO"/>
<appender-ref ref="FILE_ERROR"/>
</root>
</configuration>

17
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');

BIN
src/main/resources/static/favicon.ico

13
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() {
}
}
Loading…
Cancel
Save