40 Commits

Author SHA1 Message Date
8e69c96891 fix: 调整进度条的步幅 2025-08-17 14:23:35 +08:00
caa26c729e fix: 前端修复拨码开关第一个无法开关的问题;后端修复进度条停止在3%的问题 2025-08-17 13:46:14 +08:00
55edfd771e fix: 修复进度条的问题 2025-08-17 13:33:11 +08:00
97b86acfa8 fix: 修复拨码开关数字孪生无法正常工作的问题 2025-08-17 12:29:46 +08:00
b6720d867d feat: 实现拨动开关的数字孪生 2025-08-16 16:01:10 +08:00
a2ac1bcb3b fix: 修复比特流下载失败的问题 2025-08-16 15:55:14 +08:00
e61cf96c07 refactor: 使用更简洁的方式进行认证 2025-08-16 14:53:28 +08:00
c974de593a fix: 尝试去修复后端无法停止扫描数码管的问题 2025-08-16 14:21:26 +08:00
9bd3fb29e3 fix: 前后端修复七段数码管无法正常工作的问题 2025-08-16 13:05:01 +08:00
0a1e0982c2 feat: 前端七段数码管添加数字孪生功能 2025-08-16 11:56:27 +08:00
3644c75304 feat: 完成jpeg读取后端 2025-08-15 21:04:50 +08:00
774c9575d4 fix: 修复后端数码管无法正常读取/关闭的问题 2025-08-15 15:25:37 +08:00
a00cc84e48 fix: 修复数据库与SignalR无法连接的问题 2025-08-15 13:02:56 +08:00
6fa7fffa7f fix: 修复网络配置失败的问题 2025-08-14 20:31:43 +08:00
56eeb5dce3 feat: 完成数码管websocket通信 2025-08-14 20:25:32 +08:00
7bfc362b1f feat: 完成七段数码管后端 2025-08-14 15:21:18 +08:00
alivender
0e07a5996a feat: 合并冲突 2025-08-14 15:08:41 +08:00
alivender
4b2afe13db Merge branch 'master' of ssh://git.swordlost.top:222/SikongJueluo/FPGA_WebLab 2025-08-14 15:04:57 +08:00
9af4546a11 fix: 修复调整resource manager接口导致无法过编译的问题 2025-08-14 14:22:06 +08:00
66bc5882af feat: 完成jpeg后端 2025-08-14 14:19:46 +08:00
e5dac3e731 feat: 完成提交作业的后端 2025-08-14 11:38:09 +08:00
24622d30cf feat: 使首页的教程placehold支持中文,同时使markdown编辑器同app主题变化 2025-08-14 11:37:30 +08:00
c4b3a09198 feat: 添加Markdown编辑器 2025-08-13 19:27:09 +08:00
7a59c29e06 feat: 实现可编辑已有的实验 2025-08-13 16:11:06 +08:00
76342553ad feat: 认证管理实时获取token 2025-08-13 14:36:01 +08:00
efcdee2109 chore: 移除无用的库 2025-08-13 14:34:50 +08:00
37156c937a fix: 修复signalR无法认证的问题 2025-08-13 14:32:41 +08:00
6e84953740 refactor: 重新调整exam页面 2025-08-11 17:01:24 +08:00
b09961473e fix: 修复主题无法保存的问题 2025-08-11 16:21:25 +08:00
ed9eacf33f fix: 修复数据库无法正常获取信息的问题 2025-08-11 13:34:21 +08:00
c1d641c20c refactor: 视频流前后端适配 2025-08-11 13:09:30 +08:00
b95a61c532 refactor: 重构数据库相关操作 2025-08-10 20:13:44 +08:00
079004c17d fix: 修复生成api时,缺失main.ts的问题 2025-08-10 20:13:12 +08:00
11ef4dfba6 refactor: 重构videostream; fix: 修复进度条guid无法生成的问题 2025-08-10 20:07:37 +08:00
bbde060d11 feat: 完成基本的Jpeg控制 2025-08-10 20:06:05 +08:00
0547bb5a02 refactor: video stream service; fix: progress tracker guid 2025-08-09 14:09:03 +08:00
771f5e8e9f feat: 完成基本的Jpeg控制 2025-08-08 18:38:16 +08:00
58378851bb fix: 修复progresstracker堆栈溢出的问题 2025-08-08 16:34:31 +08:00
ae50ba3b9f feat: 为Number添加更多处理方式 2025-08-08 16:33:48 +08:00
d2508f6484 feat: 修改视频流后端服务,使其适配jpeg格式 2025-08-07 15:16:18 +08:00
91 changed files with 12139 additions and 8481 deletions

2
.gitignore vendored
View File

@@ -29,7 +29,7 @@ DebuggerCmd.md
*.ntvs* *.ntvs*
*.njsproj *.njsproj
*.sw? *.sw?
prompt.md
*.tsbuildinfo *.tsbuildinfo
# Generated Files # Generated Files

13
TODO.md
View File

@@ -1,13 +0,0 @@
# TODO
1. 后端HTTP视频流
640*480, RGB565
0x0000_0000 + 25800
2. 信号发生器界面导入.dat文件
3. 示波器后端交互、前端界面
4. 逻辑分析仪后端交互、前端界面
5. 前端重构
6. 数据库 —— 用户登录、板卡资源分配、板卡IP地址分配

View File

@@ -9,7 +9,10 @@
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f { forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
pkgs = import nixpkgs { pkgs = import nixpkgs {
inherit system; inherit system;
config.permittedInsecurePackages = ["dotnet-sdk-6.0.428"]; config.permittedInsecurePackages = [
"dotnet-sdk-6.0.428"
"beekeeper-studio-5.2.9"
];
}; };
}); });
in in
@@ -21,7 +24,7 @@
nodejs nodejs
sqlite sqlite
sqls sqls
sql-studio beekeeper-studio
zlib zlib
bash bash
# Backend # Backend

800
package-lock.json generated
View File

@@ -18,12 +18,12 @@
"axios": "^1.11.0", "axios": "^1.11.0",
"echarts": "^5.6.0", "echarts": "^5.6.0",
"highlight.js": "^11.11.1", "highlight.js": "^11.11.1",
"konva": "^9.3.20",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"log-symbols": "^7.0.0", "log-symbols": "^7.0.0",
"lucide-vue-next": "^0.525.0", "lucide-vue-next": "^0.525.0",
"marked": "^12.0.0", "marked": "^12.0.0",
"mathjs": "^14.4.0", "mathjs": "^14.4.0",
"md-editor-v3": "^5.8.4",
"pinia": "^3.0.1", "pinia": "^3.0.1",
"reka-ui": "^2.3.1", "reka-ui": "^2.3.1",
"ts-log": "^2.2.7", "ts-log": "^2.2.7",
@@ -549,6 +549,390 @@
"node": ">=6.9.0" "node": ">=6.9.0"
} }
}, },
"node_modules/@codemirror/autocomplete": {
"version": "6.18.6",
"resolved": "https://registry.npmjs.org/@codemirror/autocomplete/-/autocomplete-6.18.6.tgz",
"integrity": "sha512-PHHBXFomUs5DF+9tCOM/UoW6XQ4R44lLNNhRaW9PKPTU0D7lIjRg3ElxaJnTwsl/oHiR93WSXDBrekhoUGCPtg==",
"license": "MIT",
"dependencies": {
"@codemirror/language": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.17.0",
"@lezer/common": "^1.0.0"
}
},
"node_modules/@codemirror/commands": {
"version": "6.8.1",
"resolved": "https://registry.npmjs.org/@codemirror/commands/-/commands-6.8.1.tgz",
"integrity": "sha512-KlGVYufHMQzxbdQONiLyGQDUW0itrLZwq3CcY7xpv9ZLRHqzkBSoteocBHtMCoY7/Ci4xhzSrToIeLg7FxHuaw==",
"license": "MIT",
"dependencies": {
"@codemirror/language": "^6.0.0",
"@codemirror/state": "^6.4.0",
"@codemirror/view": "^6.27.0",
"@lezer/common": "^1.1.0"
}
},
"node_modules/@codemirror/lang-angular": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/@codemirror/lang-angular/-/lang-angular-0.1.4.tgz",
"integrity": "sha512-oap+gsltb/fzdlTQWD6BFF4bSLKcDnlxDsLdePiJpCVNKWXSTAbiiQeYI3UmES+BLAdkmIC1WjyztC1pi/bX4g==",
"license": "MIT",
"dependencies": {
"@codemirror/lang-html": "^6.0.0",
"@codemirror/lang-javascript": "^6.1.2",
"@codemirror/language": "^6.0.0",
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.3.3"
}
},
"node_modules/@codemirror/lang-cpp": {
"version": "6.0.3",
"resolved": "https://registry.npmjs.org/@codemirror/lang-cpp/-/lang-cpp-6.0.3.tgz",
"integrity": "sha512-URM26M3vunFFn9/sm6rzqrBzDgfWuDixp85uTY49wKudToc2jTHUrKIGGKs+QWND+YLofNNZpxcNGRynFJfvgA==",
"license": "MIT",
"dependencies": {
"@codemirror/language": "^6.0.0",
"@lezer/cpp": "^1.0.0"
}
},
"node_modules/@codemirror/lang-css": {
"version": "6.3.1",
"resolved": "https://registry.npmjs.org/@codemirror/lang-css/-/lang-css-6.3.1.tgz",
"integrity": "sha512-kr5fwBGiGtmz6l0LSJIbno9QrifNMUusivHbnA1H6Dmqy4HZFte3UAICix1VuKo0lMPKQr2rqB+0BkKi/S3Ejg==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@codemirror/language": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@lezer/common": "^1.0.2",
"@lezer/css": "^1.1.7"
}
},
"node_modules/@codemirror/lang-go": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/@codemirror/lang-go/-/lang-go-6.0.1.tgz",
"integrity": "sha512-7fNvbyNylvqCphW9HD6WFnRpcDjr+KXX/FgqXy5H5ZS0eC5edDljukm/yNgYkwTsgp2busdod50AOTIy6Jikfg==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@codemirror/language": "^6.6.0",
"@codemirror/state": "^6.0.0",
"@lezer/common": "^1.0.0",
"@lezer/go": "^1.0.0"
}
},
"node_modules/@codemirror/lang-html": {
"version": "6.4.9",
"resolved": "https://registry.npmjs.org/@codemirror/lang-html/-/lang-html-6.4.9.tgz",
"integrity": "sha512-aQv37pIMSlueybId/2PVSP6NPnmurFDVmZwzc7jszd2KAF8qd4VBbvNYPXWQq90WIARjsdVkPbw29pszmHws3Q==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@codemirror/lang-css": "^6.0.0",
"@codemirror/lang-javascript": "^6.0.0",
"@codemirror/language": "^6.4.0",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.17.0",
"@lezer/common": "^1.0.0",
"@lezer/css": "^1.1.0",
"@lezer/html": "^1.3.0"
}
},
"node_modules/@codemirror/lang-java": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/@codemirror/lang-java/-/lang-java-6.0.2.tgz",
"integrity": "sha512-m5Nt1mQ/cznJY7tMfQTJchmrjdjQ71IDs+55d1GAa8DGaB8JXWsVCkVT284C3RTASaY43YknrK2X3hPO/J3MOQ==",
"license": "MIT",
"dependencies": {
"@codemirror/language": "^6.0.0",
"@lezer/java": "^1.0.0"
}
},
"node_modules/@codemirror/lang-javascript": {
"version": "6.2.4",
"resolved": "https://registry.npmjs.org/@codemirror/lang-javascript/-/lang-javascript-6.2.4.tgz",
"integrity": "sha512-0WVmhp1QOqZ4Rt6GlVGwKJN3KW7Xh4H2q8ZZNGZaP6lRdxXJzmjm4FqvmOojVj6khWJHIb9sp7U/72W7xQgqAA==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@codemirror/language": "^6.6.0",
"@codemirror/lint": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.17.0",
"@lezer/common": "^1.0.0",
"@lezer/javascript": "^1.0.0"
}
},
"node_modules/@codemirror/lang-json": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/@codemirror/lang-json/-/lang-json-6.0.2.tgz",
"integrity": "sha512-x2OtO+AvwEHrEwR0FyyPtfDUiloG3rnVTSZV1W8UteaLL8/MajQd8DpvUb2YVzC+/T18aSDv0H9mu+xw0EStoQ==",
"license": "MIT",
"dependencies": {
"@codemirror/language": "^6.0.0",
"@lezer/json": "^1.0.0"
}
},
"node_modules/@codemirror/lang-less": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/@codemirror/lang-less/-/lang-less-6.0.2.tgz",
"integrity": "sha512-EYdQTG22V+KUUk8Qq582g7FMnCZeEHsyuOJisHRft/mQ+ZSZ2w51NupvDUHiqtsOy7It5cHLPGfHQLpMh9bqpQ==",
"license": "MIT",
"dependencies": {
"@codemirror/lang-css": "^6.2.0",
"@codemirror/language": "^6.0.0",
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@codemirror/lang-liquid": {
"version": "6.2.3",
"resolved": "https://registry.npmjs.org/@codemirror/lang-liquid/-/lang-liquid-6.2.3.tgz",
"integrity": "sha512-yeN+nMSrf/lNii3FJxVVEGQwFG0/2eDyH6gNOj+TGCa0hlNO4bhQnoO5ISnd7JOG+7zTEcI/GOoyraisFVY7jQ==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@codemirror/lang-html": "^6.0.0",
"@codemirror/language": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0",
"@lezer/common": "^1.0.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.3.1"
}
},
"node_modules/@codemirror/lang-markdown": {
"version": "6.3.4",
"resolved": "https://registry.npmjs.org/@codemirror/lang-markdown/-/lang-markdown-6.3.4.tgz",
"integrity": "sha512-fBm0BO03azXnTAsxhONDYHi/qWSI+uSEIpzKM7h/bkIc9fHnFp9y7KTMXKON0teNT97pFhc1a9DQTtWBYEZ7ug==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.7.1",
"@codemirror/lang-html": "^6.0.0",
"@codemirror/language": "^6.3.0",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0",
"@lezer/common": "^1.2.1",
"@lezer/markdown": "^1.0.0"
}
},
"node_modules/@codemirror/lang-php": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/@codemirror/lang-php/-/lang-php-6.0.2.tgz",
"integrity": "sha512-ZKy2v1n8Fc8oEXj0Th0PUMXzQJ0AIR6TaZU+PbDHExFwdu+guzOA4jmCHS1Nz4vbFezwD7LyBdDnddSJeScMCA==",
"license": "MIT",
"dependencies": {
"@codemirror/lang-html": "^6.0.0",
"@codemirror/language": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@lezer/common": "^1.0.0",
"@lezer/php": "^1.0.0"
}
},
"node_modules/@codemirror/lang-python": {
"version": "6.2.1",
"resolved": "https://registry.npmjs.org/@codemirror/lang-python/-/lang-python-6.2.1.tgz",
"integrity": "sha512-IRjC8RUBhn9mGR9ywecNhB51yePWCGgvHfY1lWN/Mrp3cKuHr0isDKia+9HnvhiWNnMpbGhWrkhuWOc09exRyw==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.3.2",
"@codemirror/language": "^6.8.0",
"@codemirror/state": "^6.0.0",
"@lezer/common": "^1.2.1",
"@lezer/python": "^1.1.4"
}
},
"node_modules/@codemirror/lang-rust": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/@codemirror/lang-rust/-/lang-rust-6.0.2.tgz",
"integrity": "sha512-EZaGjCUegtiU7kSMvOfEZpaCReowEf3yNidYu7+vfuGTm9ow4mthAparY5hisJqOHmJowVH3Upu+eJlUji6qqA==",
"license": "MIT",
"dependencies": {
"@codemirror/language": "^6.0.0",
"@lezer/rust": "^1.0.0"
}
},
"node_modules/@codemirror/lang-sass": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/@codemirror/lang-sass/-/lang-sass-6.0.2.tgz",
"integrity": "sha512-l/bdzIABvnTo1nzdY6U+kPAC51czYQcOErfzQ9zSm9D8GmNPD0WTW8st/CJwBTPLO8jlrbyvlSEcN20dc4iL0Q==",
"license": "MIT",
"dependencies": {
"@codemirror/lang-css": "^6.2.0",
"@codemirror/language": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@lezer/common": "^1.0.2",
"@lezer/sass": "^1.0.0"
}
},
"node_modules/@codemirror/lang-sql": {
"version": "6.9.1",
"resolved": "https://registry.npmjs.org/@codemirror/lang-sql/-/lang-sql-6.9.1.tgz",
"integrity": "sha512-ecSk3gm/mlINcURMcvkCZmXgdzPSq8r/yfCtTB4vgqGGIbBC2IJIAy7GqYTy5pgBEooTVmHP2GZK6Z7h63CDGg==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@codemirror/language": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@codemirror/lang-vue": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/@codemirror/lang-vue/-/lang-vue-0.1.3.tgz",
"integrity": "sha512-QSKdtYTDRhEHCfo5zOShzxCmqKJvgGrZwDQSdbvCRJ5pRLWBS7pD/8e/tH44aVQT6FKm0t6RVNoSUWHOI5vNug==",
"license": "MIT",
"dependencies": {
"@codemirror/lang-html": "^6.0.0",
"@codemirror/lang-javascript": "^6.1.2",
"@codemirror/language": "^6.0.0",
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.3.1"
}
},
"node_modules/@codemirror/lang-wast": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/@codemirror/lang-wast/-/lang-wast-6.0.2.tgz",
"integrity": "sha512-Imi2KTpVGm7TKuUkqyJ5NRmeFWF7aMpNiwHnLQe0x9kmrxElndyH0K6H/gXtWwY6UshMRAhpENsgfpSwsgmC6Q==",
"license": "MIT",
"dependencies": {
"@codemirror/language": "^6.0.0",
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@codemirror/lang-xml": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/@codemirror/lang-xml/-/lang-xml-6.1.0.tgz",
"integrity": "sha512-3z0blhicHLfwi2UgkZYRPioSgVTo9PV5GP5ducFH6FaHy0IAJRg+ixj5gTR1gnT/glAIC8xv4w2VL1LoZfs+Jg==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@codemirror/language": "^6.4.0",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0",
"@lezer/common": "^1.0.0",
"@lezer/xml": "^1.0.0"
}
},
"node_modules/@codemirror/lang-yaml": {
"version": "6.1.2",
"resolved": "https://registry.npmjs.org/@codemirror/lang-yaml/-/lang-yaml-6.1.2.tgz",
"integrity": "sha512-dxrfG8w5Ce/QbT7YID7mWZFKhdhsaTNOYjOkSIMt1qmC4VQnXSDSYVHHHn8k6kJUfIhtLo8t1JJgltlxWdsITw==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@codemirror/language": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.2.0",
"@lezer/lr": "^1.0.0",
"@lezer/yaml": "^1.0.0"
}
},
"node_modules/@codemirror/language": {
"version": "6.11.2",
"resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.11.2.tgz",
"integrity": "sha512-p44TsNArL4IVXDTbapUmEkAlvWs2CFQbcfc0ymDsis1kH2wh0gcY96AS29c/vp2d0y2Tquk1EDSaawpzilUiAw==",
"license": "MIT",
"dependencies": {
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.23.0",
"@lezer/common": "^1.1.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0",
"style-mod": "^4.0.0"
}
},
"node_modules/@codemirror/language-data": {
"version": "6.5.1",
"resolved": "https://registry.npmjs.org/@codemirror/language-data/-/language-data-6.5.1.tgz",
"integrity": "sha512-0sWxeUSNlBr6OmkqybUTImADFUP0M3P0IiSde4nc24bz/6jIYzqYSgkOSLS+CBIoW1vU8Q9KUWXscBXeoMVC9w==",
"license": "MIT",
"dependencies": {
"@codemirror/lang-angular": "^0.1.0",
"@codemirror/lang-cpp": "^6.0.0",
"@codemirror/lang-css": "^6.0.0",
"@codemirror/lang-go": "^6.0.0",
"@codemirror/lang-html": "^6.0.0",
"@codemirror/lang-java": "^6.0.0",
"@codemirror/lang-javascript": "^6.0.0",
"@codemirror/lang-json": "^6.0.0",
"@codemirror/lang-less": "^6.0.0",
"@codemirror/lang-liquid": "^6.0.0",
"@codemirror/lang-markdown": "^6.0.0",
"@codemirror/lang-php": "^6.0.0",
"@codemirror/lang-python": "^6.0.0",
"@codemirror/lang-rust": "^6.0.0",
"@codemirror/lang-sass": "^6.0.0",
"@codemirror/lang-sql": "^6.0.0",
"@codemirror/lang-vue": "^0.1.1",
"@codemirror/lang-wast": "^6.0.0",
"@codemirror/lang-xml": "^6.0.0",
"@codemirror/lang-yaml": "^6.0.0",
"@codemirror/language": "^6.0.0",
"@codemirror/legacy-modes": "^6.4.0"
}
},
"node_modules/@codemirror/legacy-modes": {
"version": "6.5.1",
"resolved": "https://registry.npmjs.org/@codemirror/legacy-modes/-/legacy-modes-6.5.1.tgz",
"integrity": "sha512-DJYQQ00N1/KdESpZV7jg9hafof/iBNp9h7TYo1SLMk86TWl9uDsVdho2dzd81K+v4retmK6mdC7WpuOQDytQqw==",
"license": "MIT",
"dependencies": {
"@codemirror/language": "^6.0.0"
}
},
"node_modules/@codemirror/lint": {
"version": "6.8.5",
"resolved": "https://registry.npmjs.org/@codemirror/lint/-/lint-6.8.5.tgz",
"integrity": "sha512-s3n3KisH7dx3vsoeGMxsbRAgKe4O1vbrnKBClm99PU0fWxmxsx5rR2PfqQgIt+2MMJBHbiJ5rfIdLYfB9NNvsA==",
"license": "MIT",
"dependencies": {
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.35.0",
"crelt": "^1.0.5"
}
},
"node_modules/@codemirror/search": {
"version": "6.5.11",
"resolved": "https://registry.npmjs.org/@codemirror/search/-/search-6.5.11.tgz",
"integrity": "sha512-KmWepDE6jUdL6n8cAAqIpRmLPBZ5ZKnicE8oGU/s3QrAVID+0VhLFrzUucVKHG5035/BSykhExDL/Xm7dHthiA==",
"license": "MIT",
"dependencies": {
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0",
"crelt": "^1.0.5"
}
},
"node_modules/@codemirror/state": {
"version": "6.5.2",
"resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.5.2.tgz",
"integrity": "sha512-FVqsPqtPWKVVL3dPSxy8wEF/ymIEuVzF1PK3VbUgrxXpJUSHQWWZz4JMToquRxnkw+36LTamCZG2iua2Ptq0fA==",
"license": "MIT",
"dependencies": {
"@marijn/find-cluster-break": "^1.0.0"
}
},
"node_modules/@codemirror/view": {
"version": "6.38.1",
"resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.38.1.tgz",
"integrity": "sha512-RmTOkE7hRU3OVREqFVITWHz6ocgBjv08GoePscAakgVQfciA3SGCEk7mb9IzwW61cKKmlTpHXG6DUE5Ubx+MGQ==",
"license": "MIT",
"dependencies": {
"@codemirror/state": "^6.5.0",
"crelt": "^1.0.6",
"style-mod": "^4.1.0",
"w3c-keyname": "^2.2.4"
}
},
"node_modules/@cspotcode/source-map-support": { "node_modules/@cspotcode/source-map-support": {
"version": "0.8.1", "version": "0.8.1",
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz",
@@ -1130,6 +1514,189 @@
"@jridgewell/sourcemap-codec": "^1.4.14" "@jridgewell/sourcemap-codec": "^1.4.14"
} }
}, },
"node_modules/@lezer/common": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.2.3.tgz",
"integrity": "sha512-w7ojc8ejBqr2REPsWxJjrMFsA/ysDCFICn8zEOR9mrqzOu2amhITYuLD8ag6XZf0CFXDrhKqw7+tW8cX66NaDA==",
"license": "MIT"
},
"node_modules/@lezer/cpp": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/@lezer/cpp/-/cpp-1.1.3.tgz",
"integrity": "sha512-ykYvuFQKGsRi6IcE+/hCSGUhb/I4WPjd3ELhEblm2wS2cOznDFzO+ubK2c+ioysOnlZ3EduV+MVQFCPzAIoY3w==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@lezer/css": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/@lezer/css/-/css-1.3.0.tgz",
"integrity": "sha512-pBL7hup88KbI7hXnZV3PQsn43DHy6TWyzuyk2AO9UyoXcDltvIdqWKE1dLL/45JVZ+YZkHe1WVHqO6wugZZWcw==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.3.0"
}
},
"node_modules/@lezer/go": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@lezer/go/-/go-1.0.1.tgz",
"integrity": "sha512-xToRsYxwsgJNHTgNdStpcvmbVuKxTapV0dM0wey1geMMRc9aggoVyKgzYp41D2/vVOx+Ii4hmE206kvxIXBVXQ==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.3.0"
}
},
"node_modules/@lezer/highlight": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.2.1.tgz",
"integrity": "sha512-Z5duk4RN/3zuVO7Jq0pGLJ3qynpxUVsh7IbUbGj88+uV2ApSAn6kWg2au3iJb+0Zi7kKtqffIESgNcRXWZWmSA==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.0.0"
}
},
"node_modules/@lezer/html": {
"version": "1.3.10",
"resolved": "https://registry.npmjs.org/@lezer/html/-/html-1.3.10.tgz",
"integrity": "sha512-dqpT8nISx/p9Do3AchvYGV3qYc4/rKr3IBZxlHmpIKam56P47RSHkSF5f13Vu9hebS1jM0HmtJIwLbWz1VIY6w==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@lezer/java": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/@lezer/java/-/java-1.1.3.tgz",
"integrity": "sha512-yHquUfujwg6Yu4Fd1GNHCvidIvJwi/1Xu2DaKl/pfWIA2c1oXkVvawH3NyXhCaFx4OdlYBVX5wvz2f7Aoa/4Xw==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@lezer/javascript": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/@lezer/javascript/-/javascript-1.5.1.tgz",
"integrity": "sha512-ATOImjeVJuvgm3JQ/bpo2Tmv55HSScE2MTPnKRMRIPx2cLhHGyX2VnqpHhtIV1tVzIjZDbcWQm+NCTF40ggZVw==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.1.3",
"@lezer/lr": "^1.3.0"
}
},
"node_modules/@lezer/json": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@lezer/json/-/json-1.0.3.tgz",
"integrity": "sha512-BP9KzdF9Y35PDpv04r0VeSTKDeox5vVr3efE7eBbx3r4s3oNLfunchejZhjArmeieBH+nVOpgIiBJpEAv8ilqQ==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@lezer/lr": {
"version": "1.4.2",
"resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.4.2.tgz",
"integrity": "sha512-pu0K1jCIdnQ12aWNaAVU5bzi7Bd1w54J3ECgANPmYLtQKP0HBj2cE/5coBD66MT10xbtIuUr7tg0Shbsvk0mDA==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.0.0"
}
},
"node_modules/@lezer/markdown": {
"version": "1.4.3",
"resolved": "https://registry.npmjs.org/@lezer/markdown/-/markdown-1.4.3.tgz",
"integrity": "sha512-kfw+2uMrQ/wy/+ONfrH83OkdFNM0ye5Xq96cLlaCy7h5UT9FO54DU4oRoIc0CSBh5NWmWuiIJA7NGLMJbQ+Oxg==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.0.0",
"@lezer/highlight": "^1.0.0"
}
},
"node_modules/@lezer/php": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@lezer/php/-/php-1.0.4.tgz",
"integrity": "sha512-D2dJ0t8Z28/G1guztRczMFvPDUqzeMLSQbdWQmaiHV7urc8NlEOnjYk9UrZ531OcLiRxD4Ihcbv7AsDpNKDRaQ==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.1.0"
}
},
"node_modules/@lezer/python": {
"version": "1.1.18",
"resolved": "https://registry.npmjs.org/@lezer/python/-/python-1.1.18.tgz",
"integrity": "sha512-31FiUrU7z9+d/ElGQLJFXl+dKOdx0jALlP3KEOsGTex8mvj+SoE1FgItcHWK/axkxCHGUSpqIHt6JAWfWu9Rhg==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@lezer/rust": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@lezer/rust/-/rust-1.0.2.tgz",
"integrity": "sha512-Lz5sIPBdF2FUXcWeCu1//ojFAZqzTQNRga0aYv6dYXqJqPfMdCAI0NzajWUd4Xijj1IKJLtjoXRPMvTKWBcqKg==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@lezer/sass": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@lezer/sass/-/sass-1.1.0.tgz",
"integrity": "sha512-3mMGdCTUZ/84ArHOuXWQr37pnf7f+Nw9ycPUeKX+wu19b7pSMcZGLbaXwvD2APMBDOGxPmpK/O6S1v1EvLoqgQ==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@lezer/xml": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/@lezer/xml/-/xml-1.0.6.tgz",
"integrity": "sha512-CdDwirL0OEaStFue/66ZmFSeppuL6Dwjlk8qk153mSQwiSH/Dlri4GNymrNWnUmPl2Um7QfV1FO9KFUyX3Twww==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.0.0"
}
},
"node_modules/@lezer/yaml": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@lezer/yaml/-/yaml-1.0.3.tgz",
"integrity": "sha512-GuBLekbw9jDBDhGur82nuwkxKQ+a3W5H0GfaAthDXcAu+XdpS43VlnxA9E9hllkpSP5ellRDKjLLj7Lu9Wr6xA==",
"license": "MIT",
"dependencies": {
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@lezer/lr": "^1.4.0"
}
},
"node_modules/@marijn/find-cluster-break": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@marijn/find-cluster-break/-/find-cluster-break-1.0.2.tgz",
"integrity": "sha512-l0h88YhZFyKdXIFNfSWpyjStDjGHwZ/U7iobcK1cQQD8sejsONdQtTVU+1wVN1PBw40PiiHB1vA5S7VTfQiP9g==",
"license": "MIT"
},
"node_modules/@microsoft/signalr": { "node_modules/@microsoft/signalr": {
"version": "9.0.6", "version": "9.0.6",
"resolved": "https://registry.npmjs.org/@microsoft/signalr/-/signalr-9.0.6.tgz", "resolved": "https://registry.npmjs.org/@microsoft/signalr/-/signalr-9.0.6.tgz",
@@ -1889,12 +2456,34 @@
"@types/sizzle": "*" "@types/sizzle": "*"
} }
}, },
"node_modules/@types/linkify-it": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-5.0.0.tgz",
"integrity": "sha512-sVDA58zAw4eWAffKOaQH5/5j3XeayukzDk+ewSsnv3p4yJEZHCCzMDiZM8e0OUrRvmpGZ85jf4yDHkHsgBNr9Q==",
"license": "MIT"
},
"node_modules/@types/lodash": { "node_modules/@types/lodash": {
"version": "4.17.16", "version": "4.17.16",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.16.tgz", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.16.tgz",
"integrity": "sha512-HX7Em5NYQAXKW+1T+FiuG27NGwzJfCX3s1GjOa7ujxZa52kjJLOr4FUxT+giF6Tgxv1e+/czV/iTtBw27WTU9g==", "integrity": "sha512-HX7Em5NYQAXKW+1T+FiuG27NGwzJfCX3s1GjOa7ujxZa52kjJLOr4FUxT+giF6Tgxv1e+/czV/iTtBw27WTU9g==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/markdown-it": {
"version": "14.1.2",
"resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-14.1.2.tgz",
"integrity": "sha512-promo4eFwuiW+TfGxhi+0x3czqTYJkG8qB17ZUJiVF10Xm7NLVRSLUsfRTU/6h1e24VvRnXCx+hG7li58lkzog==",
"license": "MIT",
"dependencies": {
"@types/linkify-it": "^5",
"@types/mdurl": "^2"
}
},
"node_modules/@types/mdurl": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@types/mdurl/-/mdurl-2.0.0.tgz",
"integrity": "sha512-RGdgjQUZba5p6QEFAVx2OGb8rQDL/cPRG7GiedRzMcJ1tYnUANBncjbSB1NRGwbvjcPeikRABz2nshyPk1bhWg==",
"license": "MIT"
},
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "22.14.1", "version": "22.14.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.14.1.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.14.1.tgz",
@@ -1926,6 +2515,18 @@
"integrity": "sha512-oIQLCGWtcFZy2JW77j9k8nHzAOpqMHLQejDA48XXMWH6tjCQHz5RCFz1bzsmROyL6PUm+LLnUiI4BCn221inxA==", "integrity": "sha512-oIQLCGWtcFZy2JW77j9k8nHzAOpqMHLQejDA48XXMWH6tjCQHz5RCFz1bzsmROyL6PUm+LLnUiI4BCn221inxA==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/@vavt/copy2clipboard": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@vavt/copy2clipboard/-/copy2clipboard-1.0.3.tgz",
"integrity": "sha512-HtG48r2FBYp9eRvGB3QGmtRBH1zzRRAVvFbGgFstOwz4/DDaNiX0uZc3YVKPydqgOav26pibr9MtoCaWxn7aeA==",
"license": "MIT"
},
"node_modules/@vavt/util": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/@vavt/util/-/util-2.1.0.tgz",
"integrity": "sha512-YIfAvArSFVXmWvoF+DEGD0FhkhVNcCtVWWkfYtj76eSrwHh/wuEEFhiEubg1XLNM3tChO8FH8xJCT/hnizjgFQ==",
"license": "MIT"
},
"node_modules/@vitejs/plugin-vue": { "node_modules/@vitejs/plugin-vue": {
"version": "5.2.3", "version": "5.2.3",
"resolved": "https://registry.npmjs.org/@vitejs/plugin-vue/-/plugin-vue-5.2.3.tgz", "resolved": "https://registry.npmjs.org/@vitejs/plugin-vue/-/plugin-vue-5.2.3.tgz",
@@ -2408,6 +3009,12 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/argparse": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",
"license": "Python-2.0"
},
"node_modules/aria-hidden": { "node_modules/aria-hidden": {
"version": "1.2.6", "version": "1.2.6",
"resolved": "https://registry.npmjs.org/aria-hidden/-/aria-hidden-1.2.6.tgz", "resolved": "https://registry.npmjs.org/aria-hidden/-/aria-hidden-1.2.6.tgz",
@@ -2644,6 +3251,21 @@
"fsevents": "~2.3.2" "fsevents": "~2.3.2"
} }
}, },
"node_modules/codemirror": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/codemirror/-/codemirror-6.0.2.tgz",
"integrity": "sha512-VhydHotNW5w1UGK0Qj96BwSk/Zqbp9WbnyK2W/eVMv4QyF41INRGpjUhFJY7/uDNuudSc33a/PKr4iDqRduvHw==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@codemirror/commands": "^6.0.0",
"@codemirror/language": "^6.0.0",
"@codemirror/lint": "^6.0.0",
"@codemirror/search": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0"
}
},
"node_modules/combined-stream": { "node_modules/combined-stream": {
"version": "1.0.8", "version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
@@ -2656,6 +3278,12 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/commander": {
"version": "2.20.3",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",
"integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==",
"license": "MIT"
},
"node_modules/complex.js": { "node_modules/complex.js": {
"version": "2.4.2", "version": "2.4.2",
"resolved": "https://registry.npmjs.org/complex.js/-/complex.js-2.4.2.tgz", "resolved": "https://registry.npmjs.org/complex.js/-/complex.js-2.4.2.tgz",
@@ -2705,6 +3333,12 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/crelt": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/crelt/-/crelt-1.0.6.tgz",
"integrity": "sha512-VQ2MBenTq1fWZUH9DJNGti7kKv6EeAuYr3cLwxUWhIu1baTaXh4Ib5W2CqHVqib4/MqbYGJqiL3Zb8GJZr3l4g==",
"license": "MIT"
},
"node_modules/cross-spawn": { "node_modules/cross-spawn": {
"version": "7.0.6", "version": "7.0.6",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
@@ -2743,6 +3377,12 @@
"node": ">= 8" "node": ">= 8"
} }
}, },
"node_modules/cssfilter": {
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/cssfilter/-/cssfilter-0.0.10.tgz",
"integrity": "sha512-FAaLDaplstoRsDR8XGYH51znUN0UY7nMc6Z9/fvE8EXGwvJE9hu7W2vHwx1+bd6gCYnln9nLbzxFTrcO9YQDZw==",
"license": "MIT"
},
"node_modules/csstype": { "node_modules/csstype": {
"version": "3.1.3", "version": "3.1.3",
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz",
@@ -3738,7 +4378,8 @@
"url": "https://github.com/sponsors/lavrton" "url": "https://github.com/sponsors/lavrton"
} }
], ],
"license": "MIT" "license": "MIT",
"peer": true
}, },
"node_modules/lightningcss": { "node_modules/lightningcss": {
"version": "1.29.2", "version": "1.29.2",
@@ -3979,6 +4620,15 @@
"url": "https://opencollective.com/parcel" "url": "https://opencollective.com/parcel"
} }
}, },
"node_modules/linkify-it": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-5.0.0.tgz",
"integrity": "sha512-5aHCbzQRADcdP+ATqnDuhhJ/MRIqDkZX5pyjFHRRysS8vZ5AbqGEoFIb6pYHPZ+L/OC2Lc+xT8uHVVR5CAK/wQ==",
"license": "MIT",
"dependencies": {
"uc.micro": "^2.0.0"
}
},
"node_modules/local-pkg": { "node_modules/local-pkg": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/local-pkg/-/local-pkg-1.1.1.tgz", "resolved": "https://registry.npmjs.org/local-pkg/-/local-pkg-1.1.1.tgz",
@@ -4054,6 +4704,47 @@
"dev": true, "dev": true,
"license": "ISC" "license": "ISC"
}, },
"node_modules/markdown-it": {
"version": "14.1.0",
"resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-14.1.0.tgz",
"integrity": "sha512-a54IwgWPaeBCAAsv13YgmALOF1elABB08FxO9i+r4VFk5Vl4pKokRPeX8u5TCgSsPi6ec1otfLjdOpVcgbpshg==",
"license": "MIT",
"dependencies": {
"argparse": "^2.0.1",
"entities": "^4.4.0",
"linkify-it": "^5.0.0",
"mdurl": "^2.0.0",
"punycode.js": "^2.3.1",
"uc.micro": "^2.1.0"
},
"bin": {
"markdown-it": "bin/markdown-it.mjs"
}
},
"node_modules/markdown-it-image-figures": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/markdown-it-image-figures/-/markdown-it-image-figures-2.1.1.tgz",
"integrity": "sha512-mwXSQ2nPeVUzCMIE3HlLvjRioopiqyJLNph0pyx38yf9mpqFDhNGnMpAXF9/A2Xv0oiF2cVyg9xwfF0HNAz05g==",
"license": "MIT",
"engines": {
"node": ">=12.0.0"
},
"peerDependencies": {
"markdown-it": "*"
}
},
"node_modules/markdown-it-sub": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/markdown-it-sub/-/markdown-it-sub-2.0.0.tgz",
"integrity": "sha512-iCBKgwCkfQBRg2vApy9vx1C1Tu6D8XYo8NvevI3OlwzBRmiMtsJ2sXupBgEA7PPxiDwNni3qIUkhZ6j5wofDUA==",
"license": "MIT"
},
"node_modules/markdown-it-sup": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/markdown-it-sup/-/markdown-it-sup-2.0.0.tgz",
"integrity": "sha512-5VgmdKlkBd8sgXuoDoxMpiU+BiEt3I49GItBzzw7Mxq9CxvnhE/k09HFli09zgfFDRixDQDfDxi0mgBCXtaTvA==",
"license": "MIT"
},
"node_modules/marked": { "node_modules/marked": {
"version": "12.0.2", "version": "12.0.2",
"resolved": "https://registry.npmjs.org/marked/-/marked-12.0.2.tgz", "resolved": "https://registry.npmjs.org/marked/-/marked-12.0.2.tgz",
@@ -4111,6 +4802,68 @@
"url": "https://github.com/sponsors/rawify" "url": "https://github.com/sponsors/rawify"
} }
}, },
"node_modules/md-editor-v3": {
"version": "5.8.4",
"resolved": "https://registry.npmjs.org/md-editor-v3/-/md-editor-v3-5.8.4.tgz",
"integrity": "sha512-z7OOvr+Zt86kf0v46L47OHENNzdYeG8tVnfBSQdei7efVs4MWtWJk4ofv1KGutsNUA9q12h9aDZzjELeS+qCog==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.18.6",
"@codemirror/commands": "^6.8.1",
"@codemirror/lang-markdown": "^6.3.0",
"@codemirror/language": "^6.11.0",
"@codemirror/language-data": "^6.5.1",
"@codemirror/search": "^6.5.11",
"@codemirror/state": "^6.5.2",
"@codemirror/view": "^6.36.8",
"@lezer/highlight": "^1.2.1",
"@types/markdown-it": "^14.0.1",
"@vavt/copy2clipboard": "^1.0.1",
"@vavt/util": "^2.1.0",
"codemirror": "^6.0.1",
"lru-cache": "^11.0.1",
"lucide-vue-next": "^0.453.0",
"markdown-it": "^14.0.0",
"markdown-it-image-figures": "^2.1.1",
"markdown-it-sub": "^2.0.0",
"markdown-it-sup": "^2.0.0",
"medium-zoom": "^1.1.0",
"xss": "^1.0.15"
},
"peerDependencies": {
"vue": "^3.5.3"
}
},
"node_modules/md-editor-v3/node_modules/lru-cache": {
"version": "11.1.0",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.1.0.tgz",
"integrity": "sha512-QIXZUBJUx+2zHUdQujWejBkcD9+cs94tLn0+YL8UrCh+D5sCXZ4c7LaEH48pNwRY3MLDgqUFyhlCyjJPf1WP0A==",
"license": "ISC",
"engines": {
"node": "20 || >=22"
}
},
"node_modules/md-editor-v3/node_modules/lucide-vue-next": {
"version": "0.453.0",
"resolved": "https://registry.npmjs.org/lucide-vue-next/-/lucide-vue-next-0.453.0.tgz",
"integrity": "sha512-5zmv83vxAs9SVoe22veDBi8Dw0Fh2F+oTngWgKnKOkrZVbZjceXLQ3tescV2boB0zlaf9R2Sd9RuUP2766xvsQ==",
"license": "ISC",
"peerDependencies": {
"vue": ">=3.0.1"
}
},
"node_modules/mdurl": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/mdurl/-/mdurl-2.0.0.tgz",
"integrity": "sha512-Lf+9+2r+Tdp5wXDXC4PcIBjTDtq4UKjCPMQhKIuzpJNW0b96kVqSwW0bT7FhRSfmAiFYgP+SCRvdrDozfh0U5w==",
"license": "MIT"
},
"node_modules/medium-zoom": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/medium-zoom/-/medium-zoom-1.1.0.tgz",
"integrity": "sha512-ewyDsp7k4InCUp3jRmwHBRFGyjBimKps/AJLjRSox+2q/2H4p/PNpQf+pwONWlJiOudkBXtbdmVbFjqyybfTmQ==",
"license": "MIT"
},
"node_modules/memorystream": { "node_modules/memorystream": {
"version": "0.3.1", "version": "0.3.1",
"resolved": "https://registry.npmjs.org/memorystream/-/memorystream-0.3.1.tgz", "resolved": "https://registry.npmjs.org/memorystream/-/memorystream-0.3.1.tgz",
@@ -4595,6 +5348,15 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/punycode.js": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/punycode.js/-/punycode.js-2.3.1.tgz",
"integrity": "sha512-uxFIHU0YlHYhDQtV4R9J6a52SLx28BCjT+4ieh7IGbgwVJWO+km431c4yRlREUAsAmt/uMjQUyQHNEPf0M39CA==",
"license": "MIT",
"engines": {
"node": ">=6"
}
},
"node_modules/quansync": { "node_modules/quansync": {
"version": "0.2.10", "version": "0.2.10",
"resolved": "https://registry.npmjs.org/quansync/-/quansync-0.2.10.tgz", "resolved": "https://registry.npmjs.org/quansync/-/quansync-0.2.10.tgz",
@@ -4895,6 +5657,12 @@
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/style-mod": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/style-mod/-/style-mod-4.1.2.tgz",
"integrity": "sha512-wnD1HyVqpJUI2+eKZ+eo1UwghftP6yuFheBqqe+bWCotBjC2K1YnteJILRMs3SM4V/0dLEW1SC27MWP5y+mwmw==",
"license": "MIT"
},
"node_modules/superjson": { "node_modules/superjson": {
"version": "2.2.2", "version": "2.2.2",
"resolved": "https://registry.npmjs.org/superjson/-/superjson-2.2.2.tgz", "resolved": "https://registry.npmjs.org/superjson/-/superjson-2.2.2.tgz",
@@ -5105,6 +5873,12 @@
"node": ">=14.17" "node": ">=14.17"
} }
}, },
"node_modules/uc.micro": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-2.1.0.tgz",
"integrity": "sha512-ARDJmphmdvUk6Glw7y9DQ2bFkKBHwQHLi2lsaH6PPmz/Ka9sFOBsBluozhDltWmnv9u/cF6Rt87znRTPV+yp/A==",
"license": "MIT"
},
"node_modules/ufo": { "node_modules/ufo": {
"version": "1.6.1", "version": "1.6.1",
"resolved": "https://registry.npmjs.org/ufo/-/ufo-1.6.1.tgz", "resolved": "https://registry.npmjs.org/ufo/-/ufo-1.6.1.tgz",
@@ -5560,6 +6334,12 @@
"typescript": ">=5.0.0" "typescript": ">=5.0.0"
} }
}, },
"node_modules/w3c-keyname": {
"version": "2.2.8",
"resolved": "https://registry.npmjs.org/w3c-keyname/-/w3c-keyname-2.2.8.tgz",
"integrity": "sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==",
"license": "MIT"
},
"node_modules/web-streams-polyfill": { "node_modules/web-streams-polyfill": {
"version": "3.3.3", "version": "3.3.3",
"resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",
@@ -5630,6 +6410,22 @@
} }
} }
}, },
"node_modules/xss": {
"version": "1.0.15",
"resolved": "https://registry.npmjs.org/xss/-/xss-1.0.15.tgz",
"integrity": "sha512-FVdlVVC67WOIPvfOwhoMETV72f6GbW7aOabBC3WxN/oUdoEMDyLz4OgRv5/gck2ZeNqEQu+Tb0kloovXOfpYVg==",
"license": "MIT",
"dependencies": {
"commander": "^2.20.3",
"cssfilter": "0.0.10"
},
"bin": {
"xss": "bin/xss"
},
"engines": {
"node": ">= 0.10.0"
}
},
"node_modules/yallist": { "node_modules/yallist": {
"version": "3.1.1", "version": "3.1.1",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz",

View File

@@ -22,12 +22,12 @@
"axios": "^1.11.0", "axios": "^1.11.0",
"echarts": "^5.6.0", "echarts": "^5.6.0",
"highlight.js": "^11.11.1", "highlight.js": "^11.11.1",
"konva": "^9.3.20",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"log-symbols": "^7.0.0", "log-symbols": "^7.0.0",
"lucide-vue-next": "^0.525.0", "lucide-vue-next": "^0.525.0",
"marked": "^12.0.0", "marked": "^12.0.0",
"mathjs": "^14.4.0", "mathjs": "^14.4.0",
"md-editor-v3": "^5.8.4",
"pinia": "^3.0.1", "pinia": "^3.0.1",
"reka-ui": "^2.3.1", "reka-ui": "^2.3.1",
"ts-log": "^2.2.7", "ts-log": "^2.2.7",

View File

@@ -304,7 +304,7 @@ async function generateSignalRClient(): Promise<void> {
console.log("Generating SignalR TypeScript client..."); console.log("Generating SignalR TypeScript client...");
try { try {
const { stdout, stderr } = await execAsync( const { stdout, stderr } = await execAsync(
"dotnet tsrts --project ./server/server.csproj --output ./src/", "dotnet tsrts --project ./server/server.csproj --output ./src/utils/signalR",
); );
if (stdout) console.log(stdout); if (stdout) console.log(stdout);
if (stderr) console.error(stderr); if (stderr) console.error(stderr);

View File

@@ -35,57 +35,133 @@ public class NumberTest
} }
/// <summary> /// <summary>
/// 测试 BytesToUInt64 的正常与异常情况 /// 测试 BytesToUInt64 的正常与异常情况,覆盖不同参数组合
/// </summary> /// </summary>
[Fact] [Fact]
public void Test_BytesToUInt64() public void Test_BytesToUInt64()
{ {
// 正常大端 // 正常大端isLowNumHigh=false
var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x01 }; var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x01 };
var result = Number.BytesToUInt64((byte[])bytes.Clone(), false); var result = Number.BytesToUInt64((byte[])bytes.Clone());
Assert.True(result.IsSuccessful); Assert.True(result.IsSuccessful);
Assert.Equal(0x12345678ABCDEF01UL, result.Value); Assert.Equal(0x12345678ABCDEF01UL, result.Value);
// 正常小端 // 正常小端isLowNumHigh=true
var bytes2 = new byte[] { 0x01, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12 }; var bytes2 = new byte[] { 0x01, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12 };
var result2 = Number.BytesToUInt64((byte[])bytes2.Clone(), true); var result2 = Number.BytesToUInt64((byte[])bytes2.Clone(), true);
Assert.True(result2.IsSuccessful); Assert.True(result2.IsSuccessful);
Assert.Equal(0x12345678ABCDEF01UL, result2.Value); Assert.Equal(0x12345678ABCDEF01UL, result2.Value);
// 异常:长度超限 // 长度不足8字节numLength=4大端
var result3 = Number.BytesToUInt64(new byte[9], false); var bytes3 = new byte[] { 0x12, 0x34, 0x56, 0x78 };
Assert.False(result3.IsSuccessful); var result3 = Number.BytesToUInt64((byte[])bytes3.Clone(), 0, 4, false);
Assert.True(result3.IsSuccessful);
Assert.Equal(0x1234567800000000UL, result3.Value);
// 异常:不足8字节 // 长度不足8字节numLength=4小端
var result4 = Number.BytesToUInt64(new byte[] { 0x01, 0x02 }, false); var bytes4 = new byte[] { 0x78, 0x56, 0x34, 0x12 };
Assert.False(result4.IsSuccessful); // BitConverter.ToUInt64 需要8字节 var result4 = Number.BytesToUInt64((byte[])bytes4.Clone(), 0, 4, true);
Assert.True(result4.IsSuccessful);
Assert.Equal(0x12345678UL, result4.Value);
// numLength=0
var bytes5 = new byte[] { 0x12, 0x34, 0x56, 0x78 };
var result5 = Number.BytesToUInt64((byte[])bytes5.Clone(), 0, 0, false);
Assert.True(result5.IsSuccessful);
Assert.Equal(0UL, result5.Value);
// offset测试
var bytes6 = new byte[] { 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x01 };
var result6 = Number.BytesToUInt64(bytes6, 2, 8, false);
Assert.True(result6.IsSuccessful);
Assert.Equal(0x12345678ABCDEF01UL, result6.Value);
// numLength超限>8应返回异常
var bytes7 = new byte[9];
var result7 = Number.BytesToUInt64(bytes7, 0, 9, false);
Assert.False(result7.IsSuccessful);
// offset+numLength超限
var bytes8 = new byte[] { 0x01, 0x02, 0x03, 0x04 };
var result8 = Number.BytesToUInt64(bytes8, 2, 4, false);
Assert.True(result8.IsSuccessful);
Assert.Equal(0x0304000000000000UL, result8.Value);
// bytes长度不足offset+numLength
var bytes9 = new byte[] { 0x01, 0x02 };
var result9 = Number.BytesToUInt64(bytes9, 1, 2, true);
Assert.True(result9.IsSuccessful);
Assert.Equal(0x02UL, result9.Value);
// 空数组
var result10 = Number.BytesToUInt64(new byte[0], 0, 0, false);
Assert.True(result10.IsSuccessful);
Assert.Equal(0UL, result10.Value);
} }
/// <summary> /// <summary>
/// 测试 BytesToUInt32 的正常与异常情况 /// 测试 BytesToUInt32 的正常与异常情况,覆盖不同参数组合
/// </summary> /// </summary>
[Fact] [Fact]
public void Test_BytesToUInt32() public void Test_BytesToUInt32()
{ {
// 正常大端 // 正常大端isLowNumHigh=false
var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78 }; var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78 };
var result = Number.BytesToUInt32((byte[])bytes.Clone(), false); var result = Number.BytesToUInt32((byte[])bytes.Clone());
Assert.True(result.IsSuccessful); Assert.True(result.IsSuccessful);
Assert.Equal(0x12345678U, result.Value); Assert.Equal(0x12345678U, result.Value);
// 正常小端 // 正常小端isLowNumHigh=true
var bytes2 = new byte[] { 0x78, 0x56, 0x34, 0x12 }; var bytes2 = new byte[] { 0x78, 0x56, 0x34, 0x12 };
var result2 = Number.BytesToUInt32((byte[])bytes2.Clone(), true); var result2 = Number.BytesToUInt32((byte[])bytes2.Clone(), true);
Assert.True(result2.IsSuccessful); Assert.True(result2.IsSuccessful);
Assert.Equal(0x12345678U, result2.Value); Assert.Equal(0x12345678U, result2.Value);
// 异常:长度超限 // 长度不足4字节numLength=2大端
var result3 = Number.BytesToUInt32(new byte[5], false); var bytes3 = new byte[] { 0x12, 0x34 };
Assert.False(result3.IsSuccessful); var result3 = Number.BytesToUInt32((byte[])bytes3.Clone(), 0, 2, false);
Assert.True(result3.IsSuccessful);
Assert.Equal(0x12340000U, result3.Value);
// 异常:不足4字节 // 长度不足4字节numLength=2小端
var result4 = Number.BytesToUInt32(new byte[] { 0x01, 0x02 }, false); var bytes4 = new byte[] { 0x34, 0x12 };
Assert.False(result4.IsSuccessful); // BitConverter.ToUInt32 需要4字节 var result4 = Number.BytesToUInt32((byte[])bytes4.Clone(), 0, 2, true);
Assert.True(result4.IsSuccessful);
Assert.Equal(0x1234U, result4.Value);
// numLength=0
var bytes5 = new byte[] { 0x12, 0x34, 0x56, 0x78 };
var result5 = Number.BytesToUInt32((byte[])bytes5.Clone(), 0, 0, false);
Assert.True(result5.IsSuccessful);
Assert.Equal(0U, result5.Value);
// offset测试
var bytes6 = new byte[] { 0x00, 0x00, 0x12, 0x34, 0x56, 0x78 };
var result6 = Number.BytesToUInt32(bytes6, 2, 4, false);
Assert.True(result6.IsSuccessful);
Assert.Equal(0x12345678U, result6.Value);
// numLength超限>4应返回异常
var bytes7 = new byte[5];
var result7 = Number.BytesToUInt32(bytes7, 0, 5, false);
Assert.False(result7.IsSuccessful);
// offset+numLength超限
var bytes8 = new byte[] { 0x01, 0x02, 0x03, 0x04 };
var result8 = Number.BytesToUInt32(bytes8, 2, 2, false);
Assert.True(result8.IsSuccessful);
Assert.Equal(0x03040000U, result8.Value);
// bytes长度不足offset+numLength
var bytes9 = new byte[] { 0x01, 0x02 };
var result9 = Number.BytesToUInt32(bytes9, 1, 1, true);
Assert.True(result9.IsSuccessful);
Assert.Equal(0x02U, result9.Value);
// 空数组
var result10 = Number.BytesToUInt32(new byte[0], 0, 0, false);
Assert.True(result10.IsSuccessful);
Assert.Equal(0U, result10.Value);
} }
/// <summary> /// <summary>

View File

@@ -6,7 +6,7 @@ using server.Services;
public class ProgressTrackerTest public class ProgressTrackerTest
{ {
[Fact] [Fact]
public async Task Test_ProgressReporter_Basic() public void Test_ProgressReporter_Basic()
{ {
int reportedValue = -1; int reportedValue = -1;
var reporter = new ProgressReporter(async v => { reportedValue = v; await Task.CompletedTask; }, 0, 100, 10); var reporter = new ProgressReporter(async v => { reportedValue = v; await Task.CompletedTask; }, 0, 100, 10);

3
server/.gitignore vendored
View File

@@ -1,5 +1,6 @@
# Generate
obj obj
bin bin
bitstream bitstream
bsdl bsdl
data

View File

@@ -62,8 +62,40 @@ try
IssuerSigningKey = new SymmetricSecurityKey( IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes("my secret key 1234567890my secret key 1234567890")), Encoding.UTF8.GetBytes("my secret key 1234567890my secret key 1234567890")),
}; };
options.Authority = $"http://{Global.localhost}:5000"; options.Authority = $"http://{Global.LocalHost}:5000";
options.RequireHttpsMetadata = false; options.RequireHttpsMetadata = false;
// We have to hook the OnMessageReceived event in order to
// allow the JWT authentication handler to read the access
// token from the query string when a WebSocket or
// Server-Sent Events request comes in.
// Sending the access token in the query string is required when using WebSockets or ServerSentEvents
// due to a limitation in Browser APIs. We restrict it to only calls to the
// SignalR hub in this code.
// See https://docs.microsoft.com/aspnet/core/signalr/security#access-token-logging
// for more information about security considerations when using
// the query string to transmit the access token.
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
// If the request is for our hub...
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) && (
path.StartsWithSegments("/hubs/JtagHub") ||
path.StartsWithSegments("/hubs/ProgressHub") ||
path.StartsWithSegments("/hubs/DigitalTubesHub")
))
{
// Read the token out of the query string
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
}); });
// Add JWT Token Authorization Policy // Add JWT Token Authorization Policy
builder.Services.AddAuthorization(options => builder.Services.AddAuthorization(options =>
@@ -71,7 +103,7 @@ try
options.AddPolicy("Admin", policy => options.AddPolicy("Admin", policy =>
{ {
policy.RequireClaim(ClaimTypes.Role, new string[] { policy.RequireClaim(ClaimTypes.Role, new string[] {
Database.User.UserPermission.Admin.ToString(), Database.UserPermission.Admin.ToString(),
}); });
}); });
}); });
@@ -141,7 +173,6 @@ try
options.OperationProcessors.Add(new OperationSecurityScopeProcessor("Bearer")); options.OperationProcessors.Add(new OperationSecurityScopeProcessor("Bearer"));
}); });
// 添加 HTTP 视频流服务 // 添加 HTTP 视频流服务
builder.Services.AddSingleton<HttpVideoStreamService>(); builder.Services.AddSingleton<HttpVideoStreamService>();
builder.Services.AddHostedService(provider => provider.GetRequiredService<HttpVideoStreamService>()); builder.Services.AddHostedService(provider => provider.GetRequiredService<HttpVideoStreamService>());
@@ -149,8 +180,7 @@ try
builder.Services.AddHostedService(provider => provider.GetRequiredService<HttpHdmiVideoStreamService>()); builder.Services.AddHostedService(provider => provider.GetRequiredService<HttpHdmiVideoStreamService>());
// 添加进度跟踪服务 // 添加进度跟踪服务
builder.Services.AddSingleton<ProgressTrackerService>(); builder.Services.AddSingleton<ProgressTracker>();
builder.Services.AddHostedService(provider => provider.GetRequiredService<ProgressTrackerService>());
// Application Settings // Application Settings
var app = builder.Build(); var app = builder.Build();
@@ -209,7 +239,7 @@ try
settings.PostProcess = (document, httpRequest) => settings.PostProcess = (document, httpRequest) =>
{ {
document.Servers.Clear(); document.Servers.Clear();
document.Servers.Add(new NSwag.OpenApiServer { Url = $"http://{Global.localhost}:5000" }); document.Servers.Add(new NSwag.OpenApiServer { Url = $"http://{Global.LocalHost}:5000" });
}; };
}); });
app.UseSwaggerUi(); app.UseSwaggerUi();
@@ -221,18 +251,21 @@ try
// Router // Router
app.MapControllers(); app.MapControllers();
app.MapHub<server.Hubs.JtagHub>("hubs/JtagHub"); app.MapHub<server.Hubs.JtagHub>("/hubs/JtagHub");
app.MapHub<server.Hubs.ProgressHub>("hubs/ProgressHub"); app.MapHub<server.Hubs.ProgressHub>("/hubs/ProgressHub");
app.MapHub<server.Hubs.DigitalTubesHub>("/hubs/DigitalTubesHub");
// Setup Program // Setup Program
MsgBus.Init(); MsgBus.Init();
var progressTracker = app.Services.GetRequiredService<ProgressTracker>();
MsgBus.SetProgressTracker(progressTracker);
// Generate API Client // Generate API Client
app.MapGet("GetAPIClientCode", async (HttpContext context) => app.MapGet("GetAPIClientCode", async (HttpContext context) =>
{ {
try try
{ {
var document = await OpenApiDocument.FromUrlAsync($"http://{Global.localhost}:5000/swagger/v1/swagger.json"); var document = await OpenApiDocument.FromUrlAsync($"http://{Global.LocalHost}:5000/swagger/v1/swagger.json");
var settings = new TypeScriptClientGeneratorSettings var settings = new TypeScriptClientGeneratorSettings
{ {

View File

@@ -11,6 +11,7 @@
<SpaRoot>../</SpaRoot> <SpaRoot>../</SpaRoot>
<SpaProxyServerUrl>http://localhost:5173</SpaProxyServerUrl> <SpaProxyServerUrl>http://localhost:5173</SpaProxyServerUrl>
<SpaProxyLaunchCommand>npm run dev</SpaProxyLaunchCommand> <SpaProxyLaunchCommand>npm run dev</SpaProxyLaunchCommand>
<NoWarn>CS1591</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -24,13 +25,13 @@
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="9.0.4" /> <PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="9.0.4" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.23" /> <PackageReference Include="Microsoft.OpenApi" Version="1.6.23" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.4.0" /> <PackageReference Include="NLog" Version="5.4.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.4.0" /> <PackageReference Include="NLog.Web.AspNetCore" Version="5.4.0" />
<PackageReference Include="NSwag.AspNetCore" Version="14.3.0" /> <PackageReference Include="NSwag.AspNetCore" Version="14.3.0" />
<PackageReference Include="NSwag.CodeGeneration.TypeScript" Version="14.4.0" /> <PackageReference Include="NSwag.CodeGeneration.TypeScript" Version="14.4.0" />
<PackageReference Include="OpenCvSharp4" Version="4.11.0.20250507" /> <PackageReference Include="OpenCvSharp4" Version="4.11.0.20250507" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.11.0.20250507" /> <PackageReference Include="OpenCvSharp4.runtime.win" Version="4.11.0.20250507" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.10" /> <PackageReference Include="SixLabors.ImageSharp" Version="3.1.11" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" /> <PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
<PackageReference Include="Tapper.Analyzer" Version="1.13.1"> <PackageReference Include="Tapper.Analyzer" Version="1.13.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -4,7 +4,8 @@ using System.Net.Sockets;
public static class Global public static class Global
{ {
public static readonly string localhost = "127.0.0.1"; public static readonly string LocalHost = "127.0.0.1";
public static readonly string DataPath = Path.Combine(Environment.CurrentDirectory, "data");
public static string GetLocalIPAddress() public static string GetLocalIPAddress()
{ {

View File

@@ -72,7 +72,7 @@ public class Image
var b8 = (byte)((b5 * 255) / 31); // 5位扩展到8位 var b8 = (byte)((b5 * 255) / 31); // 5位扩展到8位
// 存储到 RGB24 数组 // 存储到 RGB24 数组
var rgb24Index = (i%2 == 0)?((i+1) * 3):((i-1) * 3); var rgb24Index = (i % 2 == 0) ? ((i + 1) * 3) : ((i - 1) * 3);
rgb24Data[rgb24Index] = r8; // R rgb24Data[rgb24Index] = r8; // R
rgb24Data[rgb24Index + 1] = g8; // G rgb24Data[rgb24Index + 1] = g8; // G
rgb24Data[rgb24Index + 2] = b8; // B rgb24Data[rgb24Index + 2] = b8; // B
@@ -255,13 +255,169 @@ public class Image
return Encoding.ASCII.GetBytes("\r\n"); return Encoding.ASCII.GetBytes("\r\n");
} }
/// <summary>
/// 将原始 JPEG 数据补全 JPEG 头部,生成完整的 JPEG 图片
/// </summary>
/// <param name="jpegData">原始 JPEG 数据(可能缺少完整头部)</param>
/// <param name="width">图像宽度</param>
/// <param name="height">图像高度</param>
/// <param name="quality">JPEG质量1-100默认80</param>
/// <returns>完整的 JPEG 图片数据</returns>
public static Result<byte[]> CompleteJpegData(byte[] jpegData, int width, int height, int quality = 80)
{
if (jpegData == null)
return new(new ArgumentNullException(nameof(jpegData)));
if (width <= 0 || height <= 0)
return new(new ArgumentException("Width and height must be positive"));
if (quality < 1 || quality > 100)
return new(new ArgumentException("Quality must be between 1 and 100"));
try
{
// 检查是否已经是完整的 JPEG 文件(以 FFD8 开头FFD9 结尾)
if (jpegData.Length >= 4 &&
jpegData[0] == 0xFF && jpegData[1] == 0xD8 &&
jpegData[jpegData.Length - 2] == 0xFF && jpegData[jpegData.Length - 1] == 0xD9)
{
// 已经是完整的 JPEG 文件,直接返回
return jpegData;
}
// 创建一个临时的 RGB24 图像用于生成 JPEG 头部
using var tempImage = new SixLabors.ImageSharp.Image<Rgb24>(new Configuration
{
}, width, height);
// 填充临时图像(使用简单的渐变色作为占位符)
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
tempImage[x, y] = new Rgb24((byte)(x % 256), (byte)(y % 256), 128);
}
}
using var stream = new MemoryStream();
tempImage.SaveAsJpeg(stream, new JpegEncoder { Quality = quality });
var completeJpeg = stream.ToArray();
// 如果原始数据看起来是 JPEG 扫描数据,尝试替换扫描数据部分
if (jpegData.Length > 0)
{
// 查找 JPEG 扫描数据开始位置SOS 标记 0xFFDA 后)
int sosIndex = -1;
for (int i = 0; i < completeJpeg.Length - 1; i++)
{
if (completeJpeg[i] == 0xFF && completeJpeg[i + 1] == 0xDA)
{
// 跳过 SOS 段头部,找到实际扫描数据开始位置
i += 2; // 跳过 FF DA
if (i < completeJpeg.Length - 1)
{
int segmentLength = (completeJpeg[i] << 8) | completeJpeg[i + 1];
sosIndex = i + segmentLength;
break;
}
}
}
// 查找 EOI 标记位置0xFFD9
int eoiIndex = -1;
for (int i = completeJpeg.Length - 2; i >= 0; i--)
{
if (completeJpeg[i] == 0xFF && completeJpeg[i + 1] == 0xD9)
{
eoiIndex = i;
break;
}
}
if (sosIndex > 0 && eoiIndex > sosIndex)
{
// 替换扫描数据部分
var headerLength = sosIndex;
var footerStart = eoiIndex;
var footerLength = completeJpeg.Length - footerStart;
var newJpegLength = headerLength + jpegData.Length + footerLength;
var newJpegData = new byte[newJpegLength];
// 复制头部
Array.Copy(completeJpeg, 0, newJpegData, 0, headerLength);
// 复制原始扫描数据
Array.Copy(jpegData, 0, newJpegData, headerLength, jpegData.Length);
// 复制尾部
Array.Copy(completeJpeg, footerStart, newJpegData, headerLength + jpegData.Length, footerLength);
return newJpegData;
}
}
// 如果无法智能合并,返回完整的模板 JPEG
return completeJpeg;
}
catch (Exception ex)
{
return new(ex);
}
}
/// <summary>
/// 从 JPEG 数据生成 MJPEG 帧数据
/// </summary>
/// <param name="jpegData">完整的 JPEG 数据</param>
/// <param name="boundary">边界字符串(默认为"--boundary"</param>
/// <returns>MJPEG 帧数据</returns>
public static Result<(byte[] header, byte[] footer, byte[] data)> CreateMjpegFrameFromJpeg(
byte[] jpegData, string boundary = "--boundary")
{
if (jpegData == null)
return new(new ArgumentNullException(nameof(jpegData)));
// 验证是否为有效的 JPEG 数据
if (jpegData.Length < 4 || jpegData[0] != 0xFF || jpegData[1] != 0xD8)
{
return new(new ArgumentException("Invalid JPEG data: missing JPEG header"));
}
try
{
var header = CreateMjpegFrameHeader(jpegData.Length, boundary);
var footer = CreateMjpegFrameFooter();
var totalLength = header.Length + jpegData.Length + footer.Length;
var frameData = new byte[totalLength];
var offset = 0;
Array.Copy(header, 0, frameData, offset, header.Length);
offset += header.Length;
Array.Copy(jpegData, 0, frameData, offset, jpegData.Length);
offset += jpegData.Length;
Array.Copy(footer, 0, frameData, offset, footer.Length);
return (header, footer, frameData);
}
catch (Exception ex)
{
return new(ex);
}
}
/// <summary> /// <summary>
/// 创建完整的 MJPEG 帧数据 /// 创建完整的 MJPEG 帧数据
/// </summary> /// </summary>
/// <param name="jpegData">JPEG数据</param> /// <param name="jpegData">JPEG数据</param>
/// <param name="boundary">边界字符串(默认为"--boundary"</param> /// <param name="boundary">边界字符串(默认为"--boundary"</param>
/// <returns>完整的MJPEG帧数据</returns> /// <returns>完整的MJPEG帧数据</returns>
public static Result<byte[]> CreateMjpegFrame(byte[] jpegData, string boundary = "--boundary") public static Result<(byte[] header, byte[] footer, byte[] data)> CreateMjpegFrame(
byte[] jpegData, string boundary = "--boundary")
{ {
if (jpegData == null) if (jpegData == null)
return new(new ArgumentNullException(nameof(jpegData))); return new(new ArgumentNullException(nameof(jpegData)));
@@ -283,7 +439,7 @@ public class Image
Array.Copy(footer, 0, frameData, offset, footer.Length); Array.Copy(footer, 0, frameData, offset, footer.Length);
return frameData; return (header, footer, frameData);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -78,6 +78,56 @@ public class Number
return arr; return arr;
} }
/// <summary>
/// 二进制字节数组转成64bits整数
/// </summary>
/// <param name="bytes">二进制字节数组</param>
/// <param name="offset">字节数组偏移量</param>
/// <param name="numLength">字节数组长度</param>
/// <param name="isLowNumHigh">是否高位在数组索引低</param>
/// <returns>整数</returns>
public static Result<UInt64> BytesToUInt64(byte[] bytes, int offset = 0, int numLength = 8, bool isLowNumHigh = false)
{
if (bytes.Length < offset)
{
return new(new ArgumentException($"The Length of bytes is less than offset"));
}
if (numLength > 8)
{
return new(new ArgumentException($"The Length of bytes is greater than 8"));
}
if (numLength <= 0) return 0;
try
{
byte[] numBytes = new byte[8]; // 8字节
int copyLen = Math.Min(numLength, bytes.Length - offset);
if (isLowNumHigh)
{
// 小端:拷贝到低位
Buffer.BlockCopy(bytes, offset, numBytes, 0, copyLen);
}
else
{
// 大端:拷贝到高位
byte[] temp = new byte[copyLen];
Buffer.BlockCopy(bytes, offset, temp, 0, copyLen);
Array.Reverse(temp);
Buffer.BlockCopy(temp, 0, numBytes, 8 - copyLen, copyLen);
}
UInt64 num = BitConverter.ToUInt64(numBytes, 0);
return num;
}
catch (Exception error)
{
return new(error);
}
}
/// <summary> /// <summary>
/// 二进制字节数组转成64bits整数 /// 二进制字节数组转成64bits整数
/// </summary> /// </summary>
@@ -86,25 +136,78 @@ public class Number
/// <returns>整数</returns> /// <returns>整数</returns>
public static Result<UInt64> BytesToUInt64(byte[] bytes, bool isLowNumHigh = false) public static Result<UInt64> BytesToUInt64(byte[] bytes, bool isLowNumHigh = false)
{ {
if (bytes.Length > 8) return BytesToUInt64(bytes, 0, 8, isLowNumHigh);
}
/// <summary>
/// 二进制字节数组转成64bits整数
/// </summary>
/// <param name="bytes">二进制字节数组</param>
/// <returns>整数</returns>
public static Result<UInt64> BytesToUInt64(byte[] bytes)
{
return BytesToUInt64(bytes, 0, 8, false);
}
/// <summary>
/// 二进制字节数组转成32bits整数
/// </summary>
/// <param name="bytes">二进制字节数组</param>
/// <param name="offset">字节数组偏移量</param>
/// <param name="numLength">整形所占字节数组长度</param>
/// <param name="isLowNumHigh">是否高位在数组索引低</param>
/// <returns>整数</returns>
public static Result<UInt32> BytesToUInt32(byte[] bytes, int offset = 0, int numLength = 4, bool isLowNumHigh = false)
{
if (bytes.Length < offset)
{ {
return new(new ArgumentException( return new(new ArgumentException($"The Length of bytes is less than offset"));
"Unsigned long number can't over 8 bytes(64 bits).",
nameof(bytes)
));
} }
UInt64 num = 0; if (numLength > 4)
int len = bytes.Length; {
return new(new ArgumentException($"The Length of bytes is greater than 4"));
}
if (bytes.Length < offset)
{
return new(new ArgumentException($"The Length of bytes is less than offset"));
}
if (numLength > 4)
{
return new(new ArgumentException($"The Length of bytes is greater than 4"));
}
if (numLength <= 0) return 0;
try try
{ {
if (!isLowNumHigh) byte[] numBytes = new byte[4]; // 4字节
{ int copyLen = Math.Min(numLength, bytes.Length - offset);
Array.Reverse(bytes); if (copyLen < 0) copyLen = 0;
}
num = BitConverter.ToUInt64(bytes, 0);
if (isLowNumHigh)
{
// 小端:拷贝到低位
if (copyLen > 0)
{
Buffer.BlockCopy(bytes, offset, numBytes, 0, copyLen);
}
}
else
{
// 大端:拷贝到高位
if (copyLen > 0)
{
byte[] temp = new byte[copyLen];
Buffer.BlockCopy(bytes, offset, temp, 0, copyLen);
Array.Reverse(temp);
Buffer.BlockCopy(temp, 0, numBytes, 4 - copyLen, copyLen);
}
}
UInt32 num = BitConverter.ToUInt32(numBytes, 0);
return num; return num;
} }
catch (Exception error) catch (Exception error)
@@ -121,32 +224,17 @@ public class Number
/// <returns>整数</returns> /// <returns>整数</returns>
public static Result<UInt32> BytesToUInt32(byte[] bytes, bool isLowNumHigh = false) public static Result<UInt32> BytesToUInt32(byte[] bytes, bool isLowNumHigh = false)
{ {
if (bytes.Length > 4) return BytesToUInt32(bytes, 0, 4, isLowNumHigh);
{ }
return new(new ArgumentException(
"Unsigned long number can't over 4 bytes(32 bits).",
nameof(bytes)
));
}
UInt32 num = 0;
int len = bytes.Length;
try
{
if (!isLowNumHigh)
{
Array.Reverse(bytes);
}
num = BitConverter.ToUInt32(bytes, 0);
return num;
}
catch (Exception error)
{
return new(error);
}
/// <summary>
/// 二进制字节数组转成32bits整数
/// </summary>
/// <param name="bytes">二进制字节数组</param>
/// <returns>整数</returns>
public static Result<UInt32> BytesToUInt32(byte[] bytes)
{
return BytesToUInt32(bytes, 0, 4, false);
} }
/// <summary> /// <summary>

View File

@@ -17,40 +17,12 @@ namespace server.Controllers;
public class DataController : ControllerBase public class DataController : ControllerBase
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly Database.UserManager _userManager = new();
// 固定的实验板IP,端口,MAC地址 // 固定的实验板IP,端口,MAC地址
private const string BOARD_IP = "169.254.109.0"; private const string BOARD_IP = "169.254.109.0";
/// <summary>
/// [TODO:description]
/// </summary>
public class UserInfo
{
/// <summary>
/// 用户的唯一标识符
/// </summary>
public Guid ID { get; set; }
/// <summary>
/// 用户的名称
/// </summary>
public required string Name { get; set; }
/// <summary>
/// 用户的电子邮箱
/// </summary>
public required string EMail { get; set; }
/// <summary>
/// 用户关联的板卡ID
/// </summary>
public Guid BoardID { get; set; }
/// <summary>
/// 用户绑定板子的过期时间
/// </summary>
public DateTime? BoardExpireTime { get; set; }
}
/// <summary> /// <summary>
/// 获取本机IP地址优先选择与实验板同网段的IP /// 获取本机IP地址优先选择与实验板同网段的IP
/// </summary> /// </summary>
@@ -112,8 +84,7 @@ public class DataController : ControllerBase
public IActionResult Login(string name, string password) public IActionResult Login(string name, string password)
{ {
// 验证用户密码 // 验证用户密码
using var db = new Database.AppDataConnection(); var ret = _userManager.CheckUserPassword(name, password);
var ret = db.CheckUserPassword(name, password);
if (!ret.IsSuccessful) return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败"); if (!ret.IsSuccessful) return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
if (!ret.Value.HasValue) return BadRequest("用户名或密码错误"); if (!ret.Value.HasValue) return BadRequest("用户名或密码错误");
var user = ret.Value.Value; var user = ret.Value.Value;
@@ -188,8 +159,7 @@ public class DataController : ControllerBase
return Unauthorized("未找到用户名信息"); return Unauthorized("未找到用户名信息");
// Get User Info // Get User Info
using var db = new Database.AppDataConnection(); var ret = _userManager.GetUserByName(userName);
var ret = db.GetUserByName(userName);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败"); return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
@@ -236,8 +206,7 @@ public class DataController : ControllerBase
try try
{ {
using var db = new Database.AppDataConnection(); var ret = _userManager.AddUser(name, email, password);
var ret = db.AddUser(name, email, password);
return Ok(ret); return Ok(ret);
} }
catch (Exception ex) catch (Exception ex)
@@ -265,15 +234,14 @@ public class DataController : ControllerBase
if (string.IsNullOrEmpty(userName)) if (string.IsNullOrEmpty(userName))
return Unauthorized("未找到用户名信息"); return Unauthorized("未找到用户名信息");
using var db = new Database.AppDataConnection(); var userRet = _userManager.GetUserByName(userName);
var userRet = db.GetUserByName(userName);
if (!userRet.IsSuccessful || !userRet.Value.HasValue) if (!userRet.IsSuccessful || !userRet.Value.HasValue)
return BadRequest("用户不存在"); return BadRequest("用户不存在");
var user = userRet.Value.Value; var user = userRet.Value.Value;
var expireTime = DateTime.UtcNow.AddHours(durationHours); var expireTime = DateTime.UtcNow.AddHours(durationHours);
var boardOpt = db.GetAvailableBoard(user.ID, expireTime); var boardOpt = _userManager.GetAvailableBoard(user.ID, expireTime);
if (!boardOpt.HasValue) if (!boardOpt.HasValue)
return NotFound("没有可用的实验板"); return NotFound("没有可用的实验板");
@@ -309,13 +277,12 @@ public class DataController : ControllerBase
if (string.IsNullOrEmpty(userName)) if (string.IsNullOrEmpty(userName))
return Unauthorized("未找到用户名信息"); return Unauthorized("未找到用户名信息");
using var db = new Database.AppDataConnection(); var userRet = _userManager.GetUserByName(userName);
var userRet = db.GetUserByName(userName);
if (!userRet.IsSuccessful || !userRet.Value.HasValue) if (!userRet.IsSuccessful || !userRet.Value.HasValue)
return BadRequest("用户不存在"); return BadRequest("用户不存在");
var user = userRet.Value.Value; var user = userRet.Value.Value;
var result = db.UnbindUserFromBoard(user.ID); var result = _userManager.UnbindUserFromBoard(user.ID);
return Ok(result > 0); return Ok(result > 0);
} }
catch (Exception ex) catch (Exception ex)
@@ -338,8 +305,7 @@ public class DataController : ControllerBase
{ {
try try
{ {
using var db = new Database.AppDataConnection(); var ret = _userManager.GetBoardByID(id);
var ret = db.GetBoardByID(id);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败"); return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
if (!ret.Value.HasValue) if (!ret.Value.HasValue)
@@ -375,8 +341,7 @@ public class DataController : ControllerBase
return BadRequest("板子名称不能为空"); return BadRequest("板子名称不能为空");
try try
{ {
using var db = new Database.AppDataConnection(); var ret = _userManager.AddBoard(name);
var ret = db.AddBoard(name);
return Ok(ret); return Ok(ret);
} }
catch (Exception ex) catch (Exception ex)
@@ -402,8 +367,7 @@ public class DataController : ControllerBase
try try
{ {
using var db = new Database.AppDataConnection(); var ret = _userManager.DeleteBoardByID(id);
var ret = db.DeleteBoardByID(id);
return Ok(ret); return Ok(ret);
} }
catch (Exception ex) catch (Exception ex)
@@ -425,8 +389,7 @@ public class DataController : ControllerBase
{ {
try try
{ {
using var db = new Database.AppDataConnection(); var boards = _userManager.GetAllBoard();
var boards = db.GetAllBoard();
return Ok(boards); return Ok(boards);
} }
catch (Exception ex) catch (Exception ex)
@@ -453,8 +416,7 @@ public class DataController : ControllerBase
return BadRequest("新名称不能为空"); return BadRequest("新名称不能为空");
try try
{ {
using var db = new Database.AppDataConnection(); var result = _userManager.UpdateBoardName(boardId, newName);
var result = db.UpdateBoardName(boardId, newName);
return Ok(result); return Ok(result);
} }
catch (Exception ex) catch (Exception ex)
@@ -473,14 +435,13 @@ public class DataController : ControllerBase
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult UpdateBoardStatus(Guid boardId, Database.Board.BoardStatus newStatus) public IActionResult UpdateBoardStatus(Guid boardId, Database.BoardStatus newStatus)
{ {
if (boardId == Guid.Empty) if (boardId == Guid.Empty)
return BadRequest("板子Guid不能为空"); return BadRequest("板子Guid不能为空");
try try
{ {
using var db = new Database.AppDataConnection(); var result = _userManager.UpdateBoardStatus(boardId, newStatus);
var result = db.UpdateBoardStatus(boardId, newStatus);
return Ok(result); return Ok(result);
} }
catch (Exception ex) catch (Exception ex)
@@ -489,4 +450,54 @@ public class DataController : ControllerBase
return StatusCode(StatusCodes.Status500InternalServerError, "更新失败,请稍后重试"); return StatusCode(StatusCodes.Status500InternalServerError, "更新失败,请稍后重试");
} }
} }
[HttpPost("AddEmptyBoard")]
[EnableCors("Development")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult AddEmptyBoard()
{
try
{
var boardId = _userManager.AddBoard("Test");
var result = _userManager.UpdateBoardStatus(boardId, Database.BoardStatus.Available);
return Ok();
}
catch (Exception ex)
{
logger.Error(ex, "新增板子时发生异常");
return StatusCode(StatusCodes.Status500InternalServerError, "新增失败,请稍后重试");
}
}
/// <summary>
/// [TODO:description]
/// </summary>
public class UserInfo
{
/// <summary>
/// 用户的唯一标识符
/// </summary>
public Guid ID { get; set; }
/// <summary>
/// 用户的名称
/// </summary>
public required string Name { get; set; }
/// <summary>
/// 用户的电子邮箱
/// </summary>
public required string EMail { get; set; }
/// <summary>
/// 用户关联的板卡ID
/// </summary>
public Guid BoardID { get; set; }
/// <summary>
/// 用户绑定板子的过期时间
/// </summary>
public DateTime? BoardExpireTime { get; set; }
}
} }

View File

@@ -15,78 +15,7 @@ public class DebuggerController : ControllerBase
{ {
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
/// <summary> private readonly Database.UserManager _userManager = new();
/// 表示单个信号通道的配置信息
/// </summary>
public class ChannelConfig
{
/// <summary>
/// 通道名称
/// </summary>
required public string name;
/// <summary>
/// 通道显示颜色(如前端波形显示用)
/// </summary>
required public string color;
/// <summary>
/// 通道信号线宽度(位数)
/// </summary>
required public UInt32 wireWidth;
/// <summary>
/// 信号线在父端口中的起始索引bit
/// </summary>
required public UInt32 wireStartIndex;
/// <summary>
/// 父端口编号
/// </summary>
required public UInt32 parentPort;
/// <summary>
/// 捕获模式(如上升沿、下降沿等)
/// </summary>
required public CaptureMode mode;
}
/// <summary>
/// 调试器整体配置信息
/// </summary>
public class DebuggerConfig
{
/// <summary>
/// 时钟频率
/// </summary>
required public UInt32 clkFreq;
/// <summary>
/// 总端口数量
/// </summary>
required public UInt32 totalPortNum;
/// <summary>
/// 捕获深度(采样点数)
/// </summary>
required public UInt32 captureDepth;
/// <summary>
/// 触发器数量
/// </summary>
required public UInt32 triggerNum;
/// <summary>
/// 所有信号通道的配置信息
/// </summary>
required public ChannelConfig[] channelConfigs;
}
/// <summary>
/// 单个通道的捕获数据
/// </summary>
public class ChannelCaptureData
{
/// <summary>
/// 通道名称
/// </summary>
required public string name;
/// <summary>
/// 通道捕获到的数据Base64编码的UInt32数组
/// </summary>
required public string data;
}
/// <summary> /// <summary>
/// 获取当前用户绑定的调试器实例 /// 获取当前用户绑定的调试器实例
@@ -99,8 +28,7 @@ public class DebuggerController : ControllerBase
if (string.IsNullOrEmpty(userName)) if (string.IsNullOrEmpty(userName))
return null; return null;
using var db = new Database.AppDataConnection(); var userRet = _userManager.GetUserByName(userName);
var userRet = db.GetUserByName(userName);
if (!userRet.IsSuccessful || !userRet.Value.HasValue) if (!userRet.IsSuccessful || !userRet.Value.HasValue)
return null; return null;
@@ -108,7 +36,7 @@ public class DebuggerController : ControllerBase
if (user.BoardID == Guid.Empty) if (user.BoardID == Guid.Empty)
return null; return null;
var boardRet = db.GetBoardByID(user.BoardID); var boardRet = _userManager.GetBoardByID(user.BoardID);
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue) if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
return null; return null;
@@ -464,4 +392,77 @@ public class DebuggerController : ControllerBase
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试"); return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
} }
} }
/// <summary>
/// 表示单个信号通道的配置信息
/// </summary>
public class ChannelConfig
{
/// <summary>
/// 通道名称
/// </summary>
required public string name;
/// <summary>
/// 通道显示颜色(如前端波形显示用)
/// </summary>
required public string color;
/// <summary>
/// 通道信号线宽度(位数)
/// </summary>
required public UInt32 wireWidth;
/// <summary>
/// 信号线在父端口中的起始索引bit
/// </summary>
required public UInt32 wireStartIndex;
/// <summary>
/// 父端口编号
/// </summary>
required public UInt32 parentPort;
/// <summary>
/// 捕获模式(如上升沿、下降沿等)
/// </summary>
required public CaptureMode mode;
}
/// <summary>
/// 调试器整体配置信息
/// </summary>
public class DebuggerConfig
{
/// <summary>
/// 时钟频率
/// </summary>
required public UInt32 clkFreq;
/// <summary>
/// 总端口数量
/// </summary>
required public UInt32 totalPortNum;
/// <summary>
/// 捕获深度(采样点数)
/// </summary>
required public UInt32 captureDepth;
/// <summary>
/// 触发器数量
/// </summary>
required public UInt32 triggerNum;
/// <summary>
/// 所有信号通道的配置信息
/// </summary>
required public ChannelConfig[] channelConfigs;
}
/// <summary>
/// 单个通道的捕获数据
/// </summary>
public class ChannelCaptureData
{
/// <summary>
/// 通道名称
/// </summary>
required public string name;
/// <summary>
/// 通道捕获到的数据Base64编码的UInt32数组
/// </summary>
required public string data;
}
} }

View File

@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using DotNext; using DotNext;
using Database;
namespace server.Controllers; namespace server.Controllers;
@@ -14,128 +15,9 @@ public class ExamController : ControllerBase
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
/// <summary> private readonly ExamManager _examManager = new();
/// 实验信息类 private readonly ResourceManager _resourceManager = new();
/// </summary> private readonly UserManager _userManager = new();
public class ExamInfo
{
/// <summary>
/// 实验的唯一标识符
/// </summary>
public required string ID { get; set; }
/// <summary>
/// 实验名称
/// </summary>
public required string Name { get; set; }
/// <summary>
/// 实验描述
/// </summary>
public required string Description { get; set; }
/// <summary>
/// 实验创建时间
/// </summary>
public DateTime CreatedTime { get; set; }
/// <summary>
/// 实验最后更新时间
/// </summary>
public DateTime UpdatedTime { get; set; }
/// <summary>
/// 实验标签
/// </summary>
public string[] Tags { get; set; } = Array.Empty<string>();
/// <summary>
/// 实验难度1-5
/// </summary>
public int Difficulty { get; set; } = 1;
/// <summary>
/// 普通用户是否可见
/// </summary>
public bool IsVisibleToUsers { get; set; } = true;
}
/// <summary>
/// 实验简要信息类(用于列表显示)
/// </summary>
public class ExamSummary
{
/// <summary>
/// 实验的唯一标识符
/// </summary>
public required string ID { get; set; }
/// <summary>
/// 实验名称
/// </summary>
public required string Name { get; set; }
/// <summary>
/// 实验创建时间
/// </summary>
public DateTime CreatedTime { get; set; }
/// <summary>
/// 实验最后更新时间
/// </summary>
public DateTime UpdatedTime { get; set; }
/// <summary>
/// 实验标签
/// </summary>
public string[] Tags { get; set; } = Array.Empty<string>();
/// <summary>
/// 实验难度1-5
/// </summary>
public int Difficulty { get; set; } = 1;
/// <summary>
/// 普通用户是否可见
/// </summary>
public bool IsVisibleToUsers { get; set; } = true;
}
/// <summary>
/// 创建实验请求类
/// </summary>
public class CreateExamRequest
{
/// <summary>
/// 实验ID
/// </summary>
public required string ID { get; set; }
/// <summary>
/// 实验名称
/// </summary>
public required string Name { get; set; }
/// <summary>
/// 实验描述
/// </summary>
public required string Description { get; set; }
/// <summary>
/// 实验标签
/// </summary>
public string[] Tags { get; set; } = Array.Empty<string>();
/// <summary>
/// 实验难度1-5
/// </summary>
public int Difficulty { get; set; } = 1;
/// <summary>
/// 普通用户是否可见
/// </summary>
public bool IsVisibleToUsers { get; set; } = true;
}
/// <summary> /// <summary>
/// 获取所有实验列表 /// 获取所有实验列表
@@ -144,29 +26,19 @@ public class ExamController : ControllerBase
[Authorize] [Authorize]
[HttpGet("list")] [HttpGet("list")]
[EnableCors("Users")] [EnableCors("Users")]
[ProducesResponseType(typeof(ExamSummary[]), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ExamInfo[]), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult GetExamList() public IActionResult GetExamList()
{ {
try try
{ {
using var db = new Database.AppDataConnection(); var exams = _examManager.GetAllExams();
var exams = db.GetAllExams();
var examSummaries = exams.Select(exam => new ExamSummary var examInfos = exams.Select(exam => new ExamInfo(exam)).ToArray();
{
ID = exam.ID,
Name = exam.Name,
CreatedTime = exam.CreatedTime,
UpdatedTime = exam.UpdatedTime,
Tags = exam.GetTagsList(),
Difficulty = exam.Difficulty,
IsVisibleToUsers = exam.IsVisibleToUsers
}).ToArray();
logger.Info($"成功获取实验列表,共 {examSummaries.Length} 个实验"); logger.Info($"成功获取实验列表,共 {examInfos.Length} 个实验");
return Ok(examSummaries); return Ok(examInfos);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -195,8 +67,7 @@ public class ExamController : ControllerBase
try try
{ {
using var db = new Database.AppDataConnection(); var result = _examManager.GetExamByID(examId);
var result = db.GetExamByID(examId);
if (!result.IsSuccessful) if (!result.IsSuccessful)
{ {
@@ -211,17 +82,7 @@ public class ExamController : ControllerBase
} }
var exam = result.Value.Value; var exam = result.Value.Value;
var examInfo = new ExamInfo var examInfo = new ExamInfo(exam);
{
ID = exam.ID,
Name = exam.Name,
Description = exam.Description,
CreatedTime = exam.CreatedTime,
UpdatedTime = exam.UpdatedTime,
Tags = exam.GetTagsList(),
Difficulty = exam.Difficulty,
IsVisibleToUsers = exam.IsVisibleToUsers
};
logger.Info($"成功获取实验信息: {examId}"); logger.Info($"成功获取实验信息: {examId}");
return Ok(examInfo); return Ok(examInfo);
@@ -239,7 +100,7 @@ public class ExamController : ControllerBase
/// <param name="request">创建实验请求</param> /// <param name="request">创建实验请求</param>
/// <returns>创建结果</returns> /// <returns>创建结果</returns>
[Authorize("Admin")] [Authorize("Admin")]
[HttpPost] [HttpPost("create")]
[EnableCors("Users")] [EnableCors("Users")]
[ProducesResponseType(typeof(ExamInfo), StatusCodes.Status201Created)] [ProducesResponseType(typeof(ExamInfo), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
@@ -247,37 +108,26 @@ public class ExamController : ControllerBase
[ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status409Conflict)] [ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult CreateExam([FromBody] CreateExamRequest request) public IActionResult CreateExam([FromBody] ExamDto request)
{ {
if (string.IsNullOrWhiteSpace(request.ID) || string.IsNullOrWhiteSpace(request.Name) || string.IsNullOrWhiteSpace(request.Description)) if (string.IsNullOrWhiteSpace(request.ID) || string.IsNullOrWhiteSpace(request.Name) || string.IsNullOrWhiteSpace(request.Description))
return BadRequest("实验ID、名称和描述不能为空"); return BadRequest("实验ID、名称和描述不能为空");
try try
{ {
using var db = new Database.AppDataConnection(); var result = _examManager.CreateExam(request.ID, request.Name, request.Description, request.Tags, request.Difficulty, request.IsVisibleToUsers);
var result = db.CreateExam(request.ID, request.Name, request.Description, request.Tags, request.Difficulty, request.IsVisibleToUsers);
if (!result.IsSuccessful) if (!result.IsSuccessful)
{ {
if (result.Error.Message.Contains("已存在")) if (result.Error.Message.Contains("已存在"))
return Conflict(result.Error.Message); return Conflict(result.Error.Message);
logger.Error($"创建实验时出错: {result.Error.Message}"); logger.Error($"创建实验时出错: {result.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"创建实验失败: {result.Error.Message}"); return StatusCode(StatusCodes.Status500InternalServerError, $"创建实验失败: {result.Error.Message}");
} }
var exam = result.Value; var exam = result.Value;
var examInfo = new ExamInfo var examInfo = new ExamInfo(exam);
{
ID = exam.ID,
Name = exam.Name,
Description = exam.Description,
CreatedTime = exam.CreatedTime,
UpdatedTime = exam.UpdatedTime,
Tags = exam.GetTagsList(),
Difficulty = exam.Difficulty,
IsVisibleToUsers = exam.IsVisibleToUsers
};
logger.Info($"成功创建实验: {request.ID}"); logger.Info($"成功创建实验: {request.ID}");
return CreatedAtAction(nameof(GetExam), new { examId = request.ID }, examInfo); return CreatedAtAction(nameof(GetExam), new { examId = request.ID }, examInfo);
@@ -288,4 +138,381 @@ public class ExamController : ControllerBase
return StatusCode(StatusCodes.Status500InternalServerError, $"创建实验失败: {ex.Message}"); return StatusCode(StatusCodes.Status500InternalServerError, $"创建实验失败: {ex.Message}");
} }
} }
/// <summary>
/// 更新实验信息
/// </summary>
/// <param name="request">更新实验请求</param>
/// <returns>更新结果</returns>
[Authorize("Admin")]
[HttpPost("update")]
[EnableCors("Users")]
[ProducesResponseType(typeof(ExamInfo), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult UpdateExam([FromBody] ExamDto request)
{
var examId = request.ID;
try
{
// 首先检查实验是否存在
var existingExamResult = _examManager.GetExamByID(examId);
if (!existingExamResult.IsSuccessful)
{
logger.Error($"检查实验是否存在时出错: {existingExamResult.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"检查实验失败: {existingExamResult.Error.Message}");
}
if (!existingExamResult.Value.HasValue)
{
logger.Warn($"要更新的实验不存在: {examId}");
return NotFound($"实验 {examId} 不存在");
}
// 执行更新
var updateResult = _examManager.UpdateExam(
examId,
request.Name,
request.Description,
request.Tags,
request.Difficulty,
request.IsVisibleToUsers
);
if (!updateResult.IsSuccessful)
{
logger.Error($"更新实验时出错: {updateResult.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"更新实验失败: {updateResult.Error.Message}");
}
// 获取更新后的实验信息并返回
var updatedExamResult = _examManager.GetExamByID(examId);
if (!updatedExamResult.IsSuccessful || !updatedExamResult.Value.HasValue)
{
logger.Error($"获取更新后的实验信息失败: {examId}");
return StatusCode(StatusCodes.Status500InternalServerError, "更新成功但获取更新后信息失败");
}
var updatedExam = updatedExamResult.Value.Value;
var examInfo = new ExamInfo(updatedExam);
logger.Info($"成功更新实验: {examId},更新记录数: {updateResult.Value}");
return Ok(examInfo);
}
catch (Exception ex)
{
logger.Error($"更新实验 {examId} 时出错: {ex.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"更新实验失败: {ex.Message}");
}
}
/// <summary>
/// 提交作业
/// </summary>
/// <param name="examId">实验ID</param>
/// <param name="file">提交的文件</param>
/// <returns>提交结果</returns>
[Authorize]
[HttpPost("commit/{examId}")]
[EnableCors("Users")]
[ProducesResponseType(typeof(ResourceInfo), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Commit(string examId, IFormFile file)
{
if (string.IsNullOrWhiteSpace(examId))
return BadRequest("实验ID不能为空");
if (file == null || file.Length == 0)
return BadRequest("文件不能为空");
try
{
// 获取当前用户信息
var userName = User.Identity?.Name;
if (string.IsNullOrEmpty(userName))
return Unauthorized("无法获取用户信息");
var userResult = _userManager.GetUserByName(userName);
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
return Unauthorized("用户不存在");
var user = userResult.Value.Value;
// 检查实验是否存在
var examResult = _examManager.GetExamByID(examId);
if (!examResult.IsSuccessful)
{
logger.Error($"检查实验是否存在时出错: {examResult.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"检查实验失败: {examResult.Error.Message}");
}
if (!examResult.Value.HasValue)
{
logger.Warn($"实验不存在: {examId}");
return NotFound($"实验 {examId} 不存在");
}
// 读取文件内容
byte[] fileData;
using (var memoryStream = new MemoryStream())
{
await file.CopyToAsync(memoryStream);
fileData = memoryStream.ToArray();
}
// 提交作业
var commitResult = _resourceManager.AddResource(
user.ID, ResourceTypes.Compression, ResourcePurpose.Homework,
file.FileName, fileData, examId);
if (!commitResult.IsSuccessful)
{
logger.Error($"提交作业时出错: {commitResult.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"提交作业失败: {commitResult.Error.Message}");
}
var commit = new ResourceInfo(commitResult.Value);
logger.Info($"用户 {userName} 成功提交实验 {examId} 的作业Commit ID: {commit.ID}");
return CreatedAtAction(nameof(GetCommitsByExamId), new { examId = examId }, commit);
}
catch (Exception ex)
{
logger.Error($"提交实验 {examId} 作业时出错: {ex.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"提交作业失败: {ex.Message}");
}
}
/// <summary>
/// 获取用户在指定实验中的提交记录
/// </summary>
/// <param name="examId">实验ID</param>
/// <returns>提交记录列表</returns>
[Authorize]
[HttpGet("commits/{examId}")]
[EnableCors("Users")]
[ProducesResponseType(typeof(ResourceInfo[]), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult GetCommitsByExamId(string examId)
{
if (string.IsNullOrWhiteSpace(examId))
return BadRequest("实验ID不能为空");
try
{
// 获取当前用户信息
var userName = User.Identity?.Name;
if (string.IsNullOrEmpty(userName))
return Unauthorized("无法获取用户信息");
var userResult = _userManager.GetUserByName(userName);
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
return Unauthorized("用户不存在");
var user = userResult.Value.Value;
// 检查实验是否存在
var examResult = _examManager.GetExamByID(examId);
if (!examResult.IsSuccessful)
{
logger.Error($"检查实验是否存在时出错: {examResult.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"检查实验失败: {examResult.Error.Message}");
}
if (!examResult.Value.HasValue)
{
logger.Warn($"实验不存在: {examId}");
return NotFound($"实验 {examId} 不存在");
}
// 获取用户的提交记录
var commitsResult = _resourceManager.GetResourceListByType(
ResourceTypes.Compression, ResourcePurpose.Homework, examId);
if (!commitsResult.IsSuccessful)
{
logger.Error($"获取提交记录时出错: {commitsResult.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"获取提交记录失败: {commitsResult.Error.Message}");
}
var commits = commitsResult.Value.Select(x => new ResourceInfo(x)).ToArray();
logger.Info($"成功获取用户 {userName} 在实验 {examId} 中的提交记录,共 {commits.Length} 条");
return Ok(commits);
}
catch (Exception ex)
{
logger.Error($"获取实验 {examId} 提交记录时出错: {ex.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"获取提交记录失败: {ex.Message}");
}
}
/// <summary>
/// 删除提交记录
/// </summary>
/// <param name="commitId">提交记录ID</param>
/// <returns>删除结果</returns>
[Authorize]
[HttpDelete("commit/{commitId}")]
[EnableCors("Users")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult DeleteCommit(Guid commitId)
{
try
{
// 获取当前用户信息
var userName = User.Identity?.Name;
if (string.IsNullOrEmpty(userName))
return Unauthorized("无法获取用户信息");
var userResult = _userManager.GetUserByName(userName);
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
return Unauthorized("用户不存在");
var user = userResult.Value.Value;
// 检查是否是管理员
var isAdmin = user.Permission == UserPermission.Admin;
// 如果不是管理员,检查提交记录是否属于当前用户
if (!isAdmin)
{
var commitResult = _resourceManager.GetResourceById(commitId);
if (!commitResult.HasValue)
{
logger.Warn($"提交记录不存在: {commitId}");
return NotFound($"提交记录 {commitId} 不存在");
}
var commit = commitResult.Value;
if (commit.UserID != user.ID)
{
logger.Warn($"用户 {userName} 尝试删除不属于自己的提交记录: {commitId}");
return Forbid("您只能删除自己的提交记录");
}
}
// 执行删除
var deleteResult = _resourceManager.DeleteResource(commitId);
if (!deleteResult)
{
logger.Warn($"提交记录不存在: {commitId}");
return NotFound($"提交记录 {commitId} 不存在");
}
logger.Info($"用户 {userName} 成功删除提交记录: {commitId}");
return Ok($"提交记录 {commitId} 已成功删除");
}
catch (Exception ex)
{
logger.Error($"删除提交记录 {commitId} 时出错: {ex.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"删除提交记录失败: {ex.Message}");
}
}
}
/// <summary>
/// 实验信息
/// </summary>
public class ExamInfo
{
/// <summary>
/// 实验的唯一标识符
/// </summary>
public string ID { get; set; }
/// <summary>
/// 实验名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 实验描述
/// </summary>
public string Description { get; set; }
/// <summary>
/// 实验创建时间
/// </summary>
public DateTime CreatedTime { get; set; }
/// <summary>
/// 实验最后更新时间
/// </summary>
public DateTime UpdatedTime { get; set; }
/// <summary>
/// 实验标签
/// </summary>
public string[] Tags { get; set; } = Array.Empty<string>();
/// <summary>
/// 实验难度1-5
/// </summary>
public int Difficulty { get; set; } = 1;
/// <summary>
/// 普通用户是否可见
/// </summary>
public bool IsVisibleToUsers { get; set; } = true;
public ExamInfo(Exam exam)
{
ID = exam.ID;
Name = exam.Name;
Description = exam.Description;
CreatedTime = exam.CreatedTime;
UpdatedTime = exam.UpdatedTime;
Tags = exam.GetTagsList();
Difficulty = exam.Difficulty;
IsVisibleToUsers = exam.IsVisibleToUsers;
}
}
/// <summary>
/// 统一的实验数据传输对象
/// </summary>
public class ExamDto
{
/// <summary>
/// 实验的唯一标识符
/// </summary>
public required string ID { get; set; }
/// <summary>
/// 实验名称
/// </summary>
public required string Name { get; set; }
/// <summary>
/// 实验描述
/// </summary>
public required string Description { get; set; }
/// <summary>
/// 实验标签
/// </summary>
public string[] Tags { get; set; } = Array.Empty<string>();
/// <summary>
/// 实验难度1-5
/// </summary>
public int Difficulty { get; set; } = 1;
/// <summary>
/// 普通用户是否可见
/// </summary>
public bool IsVisibleToUsers { get; set; } = true;
} }

View File

@@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using System.Security.Claims; using System.Security.Claims;
using server.Services; using server.Services;
using Database;
namespace server.Controllers; namespace server.Controllers;
@@ -12,9 +11,11 @@ namespace server.Controllers;
[EnableCors("Users")] [EnableCors("Users")]
public class HdmiVideoStreamController : ControllerBase public class HdmiVideoStreamController : ControllerBase
{ {
private readonly HttpHdmiVideoStreamService _videoStreamService;
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly HttpHdmiVideoStreamService _videoStreamService;
private readonly Database.UserManager _userManager = new();
public HdmiVideoStreamController(HttpHdmiVideoStreamService videoStreamService) public HdmiVideoStreamController(HttpHdmiVideoStreamService videoStreamService)
{ {
_videoStreamService = videoStreamService; _videoStreamService = videoStreamService;
@@ -40,11 +41,7 @@ public class HdmiVideoStreamController : ControllerBase
if (string.IsNullOrEmpty(userName)) if (string.IsNullOrEmpty(userName))
return Unauthorized("User name not found in claims."); return Unauthorized("User name not found in claims.");
var db = new AppDataConnection(); var userRet = _userManager.GetUserByName(userName);
if (db == null)
return NotFound("Database connection failed.");
var userRet = db.GetUserByName(userName);
if (!userRet.IsSuccessful || !userRet.Value.HasValue) if (!userRet.IsSuccessful || !userRet.Value.HasValue)
return NotFound("User not found."); return NotFound("User not found.");
@@ -53,7 +50,7 @@ public class HdmiVideoStreamController : ControllerBase
if (boardId == Guid.Empty) if (boardId == Guid.Empty)
return NotFound("No board bound to this user."); return NotFound("No board bound to this user.");
var boardRet = db.GetBoardByID(boardId); var boardRet = _userManager.GetBoardByID(boardId);
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue) if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
return NotFound("Board not found."); return NotFound("Board not found.");
@@ -70,11 +67,7 @@ public class HdmiVideoStreamController : ControllerBase
if (string.IsNullOrEmpty(userName)) if (string.IsNullOrEmpty(userName))
return Unauthorized("User name not found in claims."); return Unauthorized("User name not found in claims.");
var db = new AppDataConnection(); var userRet = _userManager.GetUserByName(userName);
if (db == null)
return NotFound("Database connection failed.");
var userRet = db.GetUserByName(userName);
if (!userRet.IsSuccessful || !userRet.Value.HasValue) if (!userRet.IsSuccessful || !userRet.Value.HasValue)
return NotFound("User not found."); return NotFound("User not found.");

View File

@@ -16,15 +16,12 @@ public class JtagController : ControllerBase
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly ProgressTrackerService _tracker; private readonly ProgressTracker _tracker = MsgBus.ProgressTracker;
private readonly UserManager _userManager = new();
private readonly ResourceManager _resourceManager = new();
private const string BITSTREAM_PATH = "bitstream/Jtag"; private const string BITSTREAM_PATH = "bitstream/Jtag";
public JtagController(ProgressTrackerService tracker)
{
_tracker = tracker;
}
/// <summary> /// <summary>
/// 控制器首页信息 /// 控制器首页信息
/// </summary> /// </summary>
@@ -127,6 +124,7 @@ public class JtagController : ControllerBase
/// <param name="address">JTAG 设备地址</param> /// <param name="address">JTAG 设备地址</param>
/// <param name="port">JTAG 设备端口</param> /// <param name="port">JTAG 设备端口</param>
/// <param name="bitstreamId">比特流ID</param> /// <param name="bitstreamId">比特流ID</param>
/// <param name="cancelToken">取消令牌</param>
/// <returns>进度跟踪TaskID</returns> /// <returns>进度跟踪TaskID</returns>
[HttpPost("DownloadBitstream")] [HttpPost("DownloadBitstream")]
[EnableCors("Users")] [EnableCors("Users")]
@@ -134,7 +132,7 @@ public class JtagController : ControllerBase
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)] [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async ValueTask<IResult> DownloadBitstream(string address, int port, int bitstreamId, CancellationToken cancelToken) public IResult DownloadBitstream(string address, int port, Guid bitstreamId, CancellationToken cancelToken)
{ {
logger.Info($"User {User.Identity?.Name} initiating bitstream download to device {address}:{port} using bitstream ID: {bitstreamId}"); logger.Info($"User {User.Identity?.Name} initiating bitstream download to device {address}:{port} using bitstream ID: {bitstreamId}");
@@ -149,35 +147,33 @@ public class JtagController : ControllerBase
} }
// 从数据库获取用户信息 // 从数据库获取用户信息
using var db = new Database.AppDataConnection(); var userResult = _userManager.GetUserByName(username);
var userResult = db.GetUserByName(username);
if (!userResult.IsSuccessful || !userResult.Value.HasValue) if (!userResult.IsSuccessful || !userResult.Value.HasValue)
{ {
logger.Error($"User {username} not found in database"); logger.Error($"User {username} not found in database");
return TypedResults.BadRequest("用户不存在"); return TypedResults.BadRequest("用户不存在");
} }
var user = userResult.Value.Value;
// 从数据库获取比特流 // 从数据库获取比特流
var bitstreamResult = db.GetResourceById(bitstreamId); var user = userResult.Value.Value;
var resourceRet = _resourceManager.GetResourceById(bitstreamId);
if (!bitstreamResult.IsSuccessful) if (!resourceRet.HasValue)
{
logger.Error($"User {username} failed to get bitstream from database: {bitstreamResult.Error}");
return TypedResults.InternalServerError($"数据库查询失败: {bitstreamResult.Error?.Message}");
}
if (!bitstreamResult.Value.HasValue)
{ {
logger.Warn($"User {username} attempted to download non-existent bitstream ID: {bitstreamId}"); logger.Warn($"User {username} attempted to download non-existent bitstream ID: {bitstreamId}");
return TypedResults.BadRequest("比特流不存在"); return TypedResults.BadRequest("比特流不存在");
} }
var bitstream = bitstreamResult.Value.Value;
// 处理比特流数据 // 处理比特流数据
var fileBytes = bitstream.Data; var resource = resourceRet.Value;
var bitstreamRet = _resourceManager.ReadBytesFromPath(resource.Path);
if (!bitstreamRet.IsSuccessful)
{
logger.Error($"User {username} failed to read bitstream file: {bitstreamRet.Error}");
return TypedResults.InternalServerError($"比特流读取失败: {bitstreamRet.Error?.Message}");
}
var fileBytes = bitstreamRet.Value;
if (fileBytes == null || fileBytes.Length == 0) if (fileBytes == null || fileBytes.Length == 0)
{ {
logger.Warn($"User {username} found empty bitstream data for ID: {bitstreamId}"); logger.Warn($"User {username} found empty bitstream data for ID: {bitstreamId}");
@@ -187,8 +183,8 @@ public class JtagController : ControllerBase
logger.Info($"User {username} processing bitstream file of size: {fileBytes.Length} bytes"); logger.Info($"User {username} processing bitstream file of size: {fileBytes.Length} bytes");
// 定义进度跟踪 // 定义进度跟踪
var (taskId, progress) = _tracker.CreateTask(cancelToken); var taskId = _tracker.CreateTask(10000);
progress.Report(10); _tracker.AdvanceProgress(taskId, 10);
_ = Task.Run(async () => _ = Task.Run(async () =>
{ {
@@ -209,7 +205,8 @@ public class JtagController : ControllerBase
if (!retBuffer.IsSuccessful) if (!retBuffer.IsSuccessful)
{ {
logger.Error($"User {username} failed to reverse bytes: {retBuffer.Error}"); logger.Error($"User {username} failed to reverse bytes: {retBuffer.Error}");
progress.Error($"User {username} failed to reverse bytes: {retBuffer.Error}"); _tracker.FailProgress(taskId,
$"User {username} failed to reverse bytes: {retBuffer.Error}");
return; return;
} }
revBuffer = retBuffer.Value; revBuffer = retBuffer.Value;
@@ -227,21 +224,22 @@ public class JtagController : ControllerBase
var processedBytes = outputStream.ToArray(); var processedBytes = outputStream.ToArray();
logger.Info($"User {username} processed {totalBytesProcessed} bytes for device {address}"); logger.Info($"User {username} processed {totalBytesProcessed} bytes for device {address}");
progress.Report(20); _tracker.AdvanceProgress(taskId, 20);
// 下载比特流 // 下载比特流
var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port); var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port);
var ret = await jtagCtrl.DownloadBitstream(processedBytes); var ret = await jtagCtrl.DownloadBitstream(processedBytes, taskId);
if (ret.IsSuccessful) if (ret.IsSuccessful)
{ {
logger.Info($"User {username} successfully downloaded bitstream '{bitstream.ResourceName}' to device {address}"); logger.Info($"User {username} successfully downloaded bitstream '{resource.ResourceName}' to device {address}");
progress.Finish(); _tracker.CompleteProgress(taskId);
} }
else else
{ {
logger.Error($"User {username} failed to download bitstream to device {address}: {ret.Error}"); logger.Error($"User {username} failed to download bitstream to device {address}: {ret.Error}");
progress.Error($"User {username} failed to download bitstream to device {address}: {ret.Error}"); _tracker.FailProgress(taskId,
$"User {username} failed to download bitstream to device {address}: {ret.Error}");
} }
} }
}); });

View File

@@ -15,57 +15,7 @@ public class LogicAnalyzerController : ControllerBase
{ {
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
/// <summary> private readonly Database.UserManager _userManager = new();
/// 信号触发配置
/// </summary>
public class SignalTriggerConfig
{
/// <summary>
/// 信号索引 (0-7)
/// </summary>
public int SignalIndex { get; set; }
/// <summary>
/// 操作符
/// </summary>
public SignalOperator Operator { get; set; }
/// <summary>
/// 信号值
/// </summary>
public SignalValue Value { get; set; }
}
/// <summary>
/// 捕获配置
/// </summary>
public class CaptureConfig
{
/// <summary>
/// 全局触发模式
/// </summary>
public GlobalCaptureMode GlobalMode { get; set; }
/// <summary>
/// 捕获深度
/// </summary>
public int CaptureLength { get; set; } = 2048 * 32;
/// <summary>
/// 预采样深度
/// </summary>
public int PreCaptureLength { get; set; } = 2048;
/// <summary>
/// 有效通道
/// </summary>
public AnalyzerChannelDiv ChannelDiv { get; set; } = AnalyzerChannelDiv.EIGHT;
/// <summary>
/// 时钟分频系数
/// </summary>
public AnalyzerClockDiv ClockDiv { get; set; } = AnalyzerClockDiv.DIV1;
/// <summary>
/// 信号触发配置列表
/// </summary>
public SignalTriggerConfig[] SignalConfigs { get; set; } = Array.Empty<SignalTriggerConfig>();
}
/// <summary> /// <summary>
/// 获取逻辑分析仪实例 /// 获取逻辑分析仪实例
@@ -78,8 +28,7 @@ public class LogicAnalyzerController : ControllerBase
if (string.IsNullOrEmpty(userName)) if (string.IsNullOrEmpty(userName))
return null; return null;
using var db = new Database.AppDataConnection(); var userRet = _userManager.GetUserByName(userName);
var userRet = db.GetUserByName(userName);
if (!userRet.IsSuccessful || !userRet.Value.HasValue) if (!userRet.IsSuccessful || !userRet.Value.HasValue)
return null; return null;
@@ -87,7 +36,7 @@ public class LogicAnalyzerController : ControllerBase
if (user.BoardID == Guid.Empty) if (user.BoardID == Guid.Empty)
return null; return null;
var boardRet = db.GetBoardByID(user.BoardID); var boardRet = _userManager.GetBoardByID(user.BoardID);
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue) if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
return null; return null;
@@ -422,4 +371,57 @@ public class LogicAnalyzerController : ControllerBase
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试"); return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
} }
} }
/// <summary>
/// 信号触发配置
/// </summary>
public class SignalTriggerConfig
{
/// <summary>
/// 信号索引 (0-7)
/// </summary>
public int SignalIndex { get; set; }
/// <summary>
/// 操作符
/// </summary>
public SignalOperator Operator { get; set; }
/// <summary>
/// 信号值
/// </summary>
public SignalValue Value { get; set; }
}
/// <summary>
/// 捕获配置
/// </summary>
public class CaptureConfig
{
/// <summary>
/// 全局触发模式
/// </summary>
public GlobalCaptureMode GlobalMode { get; set; }
/// <summary>
/// 捕获深度
/// </summary>
public int CaptureLength { get; set; } = 2048 * 32;
/// <summary>
/// 预采样深度
/// </summary>
public int PreCaptureLength { get; set; } = 2048;
/// <summary>
/// 有效通道
/// </summary>
public AnalyzerChannelDiv ChannelDiv { get; set; } = AnalyzerChannelDiv.EIGHT;
/// <summary>
/// 时钟分频系数
/// </summary>
public AnalyzerClockDiv ClockDiv { get; set; } = AnalyzerClockDiv.DIV1;
/// <summary>
/// 信号触发配置列表
/// </summary>
public SignalTriggerConfig[] SignalConfigs { get; set; } = Array.Empty<SignalTriggerConfig>();
}
} }

View File

@@ -21,8 +21,8 @@ public class NetConfigController : ControllerBase
private const int BOARD_PORT = 1234; private const int BOARD_PORT = 1234;
// 本机网络信息 // 本机网络信息
private readonly IPAddress _localIP; private readonly IPAddress _localIP = IPAddress.Any;
private readonly byte[] _localMAC; private readonly byte[] _localMAC = new byte[6];
private readonly string _localIPString; private readonly string _localIPString;
private readonly string _localMACString; private readonly string _localMACString;
private readonly string _localInterface; private readonly string _localInterface;

View File

@@ -15,72 +15,7 @@ public class OscilloscopeApiController : ControllerBase
{ {
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
/// <summary> private readonly Database.UserManager _userManager = new();
/// 示波器完整配置
/// </summary>
public class OscilloscopeFullConfig
{
/// <summary>
/// 是否启动捕获
/// </summary>
public bool CaptureEnabled { get; set; }
/// <summary>
/// 触发电平0-255
/// </summary>
public byte TriggerLevel { get; set; }
/// <summary>
/// 触发边沿true为上升沿false为下降沿
/// </summary>
public bool TriggerRisingEdge { get; set; }
/// <summary>
/// 水平偏移量0-1023
/// </summary>
public ushort HorizontalShift { get; set; }
/// <summary>
/// 抽样率0-1023
/// </summary>
public ushort DecimationRate { get; set; }
/// <summary>
/// 是否自动刷新RAM
/// </summary>
public bool AutoRefreshRAM { get; set; } = true;
}
/// <summary>
/// 示波器状态和数据
/// </summary>
public class OscilloscopeDataResponse
{
/// <summary>
/// AD采样频率
/// </summary>
public uint ADFrequency { get; set; }
/// <summary>
/// AD采样幅度
/// </summary>
public byte ADVpp { get; set; }
/// <summary>
/// AD采样最大值
/// </summary>
public byte ADMax { get; set; }
/// <summary>
/// AD采样最小值
/// </summary>
public byte ADMin { get; set; }
/// <summary>
/// 波形数据Base64编码
/// </summary>
public string WaveformData { get; set; } = string.Empty;
}
/// <summary> /// <summary>
/// 获取示波器实例 /// 获取示波器实例
@@ -93,8 +28,7 @@ public class OscilloscopeApiController : ControllerBase
if (string.IsNullOrEmpty(userName)) if (string.IsNullOrEmpty(userName))
return null; return null;
using var db = new Database.AppDataConnection(); var userRet = _userManager.GetUserByName(userName);
var userRet = db.GetUserByName(userName);
if (!userRet.IsSuccessful || !userRet.Value.HasValue) if (!userRet.IsSuccessful || !userRet.Value.HasValue)
return null; return null;
@@ -102,7 +36,7 @@ public class OscilloscopeApiController : ControllerBase
if (user.BoardID == Guid.Empty) if (user.BoardID == Guid.Empty)
return null; return null;
var boardRet = db.GetBoardByID(user.BoardID); var boardRet = _userManager.GetBoardByID(user.BoardID);
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue) if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
return null; return null;
@@ -481,4 +415,72 @@ public class OscilloscopeApiController : ControllerBase
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试"); return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
} }
} }
}
/// <summary>
/// 示波器完整配置
/// </summary>
public class OscilloscopeFullConfig
{
/// <summary>
/// 是否启动捕获
/// </summary>
public bool CaptureEnabled { get; set; }
/// <summary>
/// 触发电平0-255
/// </summary>
public byte TriggerLevel { get; set; }
/// <summary>
/// 触发边沿true为上升沿false为下降沿
/// </summary>
public bool TriggerRisingEdge { get; set; }
/// <summary>
/// 水平偏移量0-1023
/// </summary>
public ushort HorizontalShift { get; set; }
/// <summary>
/// 抽样率0-1023
/// </summary>
public ushort DecimationRate { get; set; }
/// <summary>
/// 是否自动刷新RAM
/// </summary>
public bool AutoRefreshRAM { get; set; } = true;
}
/// <summary>
/// 示波器状态和数据
/// </summary>
public class OscilloscopeDataResponse
{
/// <summary>
/// AD采样频率
/// </summary>
public uint ADFrequency { get; set; }
/// <summary>
/// AD采样幅度
/// </summary>
public byte ADVpp { get; set; }
/// <summary>
/// AD采样最大值
/// </summary>
public byte ADMax { get; set; }
/// <summary>
/// AD采样最小值
/// </summary>
public byte ADMin { get; set; }
/// <summary>
/// 波形数据Base64编码
/// </summary>
public string WaveformData { get; set; } = string.Empty;
}
}

View File

@@ -15,67 +15,8 @@ public class ResourceController : ControllerBase
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
/// <summary> private readonly UserManager _userManager = new();
/// 资源信息类 private readonly ResourceManager _resourceManager = new();
/// </summary>
public class ResourceInfo
{
/// <summary>
/// 资源ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// 资源名称
/// </summary>
public required string Name { get; set; }
/// <summary>
/// 资源类型
/// </summary>
public required string Type { get; set; }
/// <summary>
/// 资源用途template/user
/// </summary>
public required string Purpose { get; set; }
/// <summary>
/// 上传时间
/// </summary>
public DateTime UploadTime { get; set; }
/// <summary>
/// 所属实验ID可选
/// </summary>
public string? ExamID { get; set; }
/// <summary>
/// MIME类型
/// </summary>
public string? MimeType { get; set; }
}
/// <summary>
/// 添加资源请求类
/// </summary>
public class AddResourceRequest
{
/// <summary>
/// 资源类型
/// </summary>
public required string ResourceType { get; set; }
/// <summary>
/// 资源用途template/user
/// </summary>
public required string ResourcePurpose { get; set; }
/// <summary>
/// 所属实验ID可选
/// </summary>
public string? ExamID { get; set; }
}
/// <summary> /// <summary>
/// 添加资源(文件上传) /// 添加资源(文件上传)
@@ -93,27 +34,25 @@ public class ResourceController : ControllerBase
[ProducesResponseType(StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> AddResource([FromForm] AddResourceRequest request, IFormFile file) public async Task<IActionResult> AddResource([FromForm] AddResourceRequest request, IFormFile file)
{ {
if (string.IsNullOrWhiteSpace(request.ResourceType) || string.IsNullOrWhiteSpace(request.ResourcePurpose) || file == null) if (string.IsNullOrWhiteSpace(request.ResourceType) || file == null)
return BadRequest("资源类型、资源用途和文件不能为空"); return BadRequest("资源类型、资源用途和文件不能为空");
// 验证资源用途 // 验证资源用途
if (request.ResourcePurpose != Resource.ResourcePurposes.Template && request.ResourcePurpose != Resource.ResourcePurposes.User) if (request.ResourcePurpose != ResourcePurpose.Template && request.ResourcePurpose != ResourcePurpose.User)
return BadRequest($"无效的资源用途: {request.ResourcePurpose}"); return BadRequest($"无效的资源用途: {request.ResourcePurpose}");
// 模板资源需要管理员权限 // 模板资源需要管理员权限
if (request.ResourcePurpose == Resource.ResourcePurposes.Template && !User.IsInRole("Admin")) if (request.ResourcePurpose == ResourcePurpose.Template && !User.IsInRole("Admin"))
return Forbid("只有管理员可以添加模板资源"); return Forbid("只有管理员可以添加模板资源");
try try
{ {
using var db = new Database.AppDataConnection();
// 获取当前用户ID // 获取当前用户ID
var userName = User.Identity?.Name; var userName = User.Identity?.Name;
if (string.IsNullOrEmpty(userName)) if (string.IsNullOrEmpty(userName))
return Unauthorized("无法获取用户信息"); return Unauthorized("无法获取用户信息");
var userResult = db.GetUserByName(userName); var userResult = _userManager.GetUserByName(userName);
if (!userResult.IsSuccessful || !userResult.Value.HasValue) if (!userResult.IsSuccessful || !userResult.Value.HasValue)
return Unauthorized("用户不存在"); return Unauthorized("用户不存在");
@@ -124,31 +63,23 @@ public class ResourceController : ControllerBase
await file.CopyToAsync(memoryStream); await file.CopyToAsync(memoryStream);
var fileData = memoryStream.ToArray(); var fileData = memoryStream.ToArray();
var result = db.AddResource(user.ID, request.ResourceType, request.ResourcePurpose, file.FileName, fileData, request.ExamID); var result = _resourceManager.AddResource(
user.ID, request.ResourceType, request.ResourcePurpose,
file.FileName, fileData, request.ExamID);
if (!result.IsSuccessful) if (!result.IsSuccessful)
{ {
if (result.Error.Message.Contains("不存在")) if (result.Error.Message.Contains("不存在"))
return NotFound(result.Error.Message); return NotFound(result.Error.Message);
logger.Error($"添加资源时出错: {result.Error.Message}"); logger.Error($"添加资源时出错: {result.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"添加资源失败: {result.Error.Message}"); return StatusCode(StatusCodes.Status500InternalServerError, $"添加资源失败: {result.Error.Message}");
} }
var resource = result.Value; var resourceInfo = new ResourceInfo(result.Value);
var resourceInfo = new ResourceInfo
{
ID = resource.ID,
Name = resource.ResourceName,
Type = resource.ResourceType,
Purpose = resource.ResourcePurpose,
UploadTime = resource.UploadTime,
ExamID = resource.ExamID,
MimeType = resource.MimeType
};
logger.Info($"成功添加资源: {request.ResourceType}/{request.ResourcePurpose}/{file.FileName}"); logger.Info($"成功添加资源: {request.ResourceType}/{request.ResourcePurpose}/{file.FileName} ID: {resourceInfo.ID}");
return CreatedAtAction(nameof(GetResourceById), new { resourceId = resource.ID }, resourceInfo); return CreatedAtAction(nameof(GetResourceById), new { resourceId = resourceInfo.ID }, resourceInfo);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -170,69 +101,62 @@ public class ResourceController : ControllerBase
[ProducesResponseType(typeof(ResourceInfo[]), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ResourceInfo[]), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult GetResourceList([FromQuery] string? examId = null, [FromQuery] string? resourceType = null, [FromQuery] string? resourcePurpose = null) public IActionResult GetResourceList(
[FromQuery] string? examId = null,
[FromQuery] string? resourceType = null,
[FromQuery] ResourcePurpose? resourcePurpose = null)
{ {
try try
{ {
using var db = new Database.AppDataConnection();
// 获取当前用户ID // 获取当前用户ID
var userName = User.Identity?.Name; var userName = User.Identity?.Name;
if (string.IsNullOrEmpty(userName)) if (string.IsNullOrEmpty(userName))
return Unauthorized("无法获取用户信息"); return Unauthorized("无法获取用户信息");
var userResult = db.GetUserByName(userName); var userResult = _userManager.GetUserByName(userName);
if (!userResult.IsSuccessful || !userResult.Value.HasValue) if (!userResult.IsSuccessful || !userResult.Value.HasValue)
return Unauthorized("用户不存在"); return Unauthorized("用户不存在");
var user = userResult.Value.Value; var user = userResult.Value.Value;
// 普通用户只能查看自己的资源和模板资源 Result<List<Resource>> result;
Guid? userId = null; // 管理员
if (!User.IsInRole("Admin")) if (user.Permission == UserPermission.Admin)
{ {
// 如果指定了用户资源用途,则只查看自己的资源 result = _resourceManager.GetFullResourceList(examId, resourceType, resourcePurpose);
if (resourcePurpose == Resource.ResourcePurposes.User)
{
userId = user.ID;
}
// 如果指定了模板资源用途则不限制用户ID
else if (resourcePurpose == Resource.ResourcePurposes.Template)
{
userId = null;
}
// 如果没有指定用途,则查看自己的用户资源和所有模板资源
else
{
// 这种情况下需要分别查询并合并结果
var userResourcesResult = db.GetFullResourceList(examId, resourceType, Resource.ResourcePurposes.User, user.ID);
var templateResourcesResult = db.GetFullResourceList(examId, resourceType, Resource.ResourcePurposes.Template, null);
if (!userResourcesResult.IsSuccessful || !templateResourcesResult.IsSuccessful)
{
logger.Error($"获取资源列表时出错");
return StatusCode(StatusCodes.Status500InternalServerError, "获取资源列表失败");
}
var allResources = userResourcesResult.Value.Concat(templateResourcesResult.Value)
.OrderByDescending(r => r.UploadTime);
var mergedResourceInfos = allResources.Select(r => new ResourceInfo
{
ID = r.ID,
Name = r.ResourceName,
Type = r.ResourceType,
Purpose = r.ResourcePurpose,
UploadTime = r.UploadTime,
ExamID = r.ExamID,
MimeType = r.MimeType
}).ToArray();
logger.Info($"成功获取资源列表,共 {mergedResourceInfos.Length} 个资源");
return Ok(mergedResourceInfos);
}
} }
// 用户
else if (resourcePurpose == ResourcePurpose.User)
{
result = _resourceManager.GetFullResourceList(examId, resourceType, resourcePurpose, user.ID);
}
// 模板
else if (resourcePurpose == ResourcePurpose.Template)
{
result = _resourceManager.GetFullResourceList(examId, resourceType, resourcePurpose);
}
// 其他
else
{
// 这种情况下需要分别查询并合并结果
var userResourcesResult = _resourceManager.GetFullResourceList(
examId, resourceType, ResourcePurpose.User, user.ID);
var templateResourcesResult = _resourceManager.GetFullResourceList(
examId, resourceType, ResourcePurpose.Template, null);
var result = db.GetFullResourceList(examId, resourceType, resourcePurpose, userId); if (!userResourcesResult.IsSuccessful || !templateResourcesResult.IsSuccessful)
{
logger.Error($"获取资源列表时出错");
return StatusCode(StatusCodes.Status500InternalServerError, "获取资源列表失败");
}
var allResources = userResourcesResult.Value.Concat(templateResourcesResult.Value)
.OrderByDescending(r => r.UploadTime);
var mergedResourceInfos = allResources.Select(r => new ResourceInfo(r)).ToArray();
logger.Info($"成功获取资源列表,共 {mergedResourceInfos.Length} 个资源");
return Ok(mergedResourceInfos);
}
if (!result.IsSuccessful) if (!result.IsSuccessful)
{ {
@@ -240,16 +164,7 @@ public class ResourceController : ControllerBase
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源列表失败: {result.Error.Message}"); return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源列表失败: {result.Error.Message}");
} }
var resources = result.Value.Select(r => new ResourceInfo var resources = result.Value.Select(r => new ResourceInfo(r)).ToArray();
{
ID = r.ID,
Name = r.ResourceName,
Type = r.ResourceType,
Purpose = r.ResourcePurpose,
UploadTime = r.UploadTime,
ExamID = r.ExamID,
MimeType = r.MimeType
}).ToArray();
logger.Info($"成功获取资源列表,共 {resources.Length} 个资源"); logger.Info($"成功获取资源列表,共 {resources.Length} 个资源");
return Ok(resources); return Ok(resources);
@@ -272,28 +187,29 @@ public class ResourceController : ControllerBase
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult GetResourceById(int resourceId) public IActionResult GetResourceById(Guid resourceId)
{ {
try try
{ {
using var db = new Database.AppDataConnection(); var result = _resourceManager.GetResourceById(resourceId);
var result = db.GetResourceById(resourceId);
if (!result.IsSuccessful) if (!result.HasValue)
{
logger.Error($"获取资源时出错: {result.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源失败: {result.Error.Message}");
}
if (!result.Value.HasValue)
{ {
logger.Warn($"资源不存在: {resourceId}"); logger.Warn($"资源不存在: {resourceId}");
return NotFound($"资源 {resourceId} 不存在"); return NotFound($"资源 {resourceId} 不存在");
} }
var resource = result.Value.Value; var resource = result.Value;
logger.Info($"成功获取资源: {resourceId} ({resource.ResourceName})"); logger.Info($"成功获取资源: {resourceId} ({resource.ResourceName})");
return File(resource.Data, resource.MimeType ?? "application/octet-stream", resource.ResourceName);
var dataRet = _resourceManager.ReadBytesFromPath(resource.Path);
if (!dataRet.IsSuccessful)
{
logger.Error($"读取资源数据时出错: {dataRet.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"读取资源数据失败: {dataRet.Error.Message}");
}
return File(dataRet.Value, resource.MimeType ?? "application/octet-stream", resource.ResourceName);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -315,50 +231,43 @@ public class ResourceController : ControllerBase
[ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult DeleteResource(int resourceId) public IActionResult DeleteResource(Guid resourceId)
{ {
try try
{ {
using var db = new Database.AppDataConnection();
// 获取当前用户信息 // 获取当前用户信息
var userName = User.Identity?.Name; var userName = User.Identity?.Name;
if (string.IsNullOrEmpty(userName)) if (string.IsNullOrEmpty(userName))
return Unauthorized("无法获取用户信息"); return Unauthorized("无法获取用户信息");
var userResult = db.GetUserByName(userName); var userResult = _userManager.GetUserByName(userName);
if (!userResult.IsSuccessful || !userResult.Value.HasValue) if (!userResult.IsSuccessful || !userResult.Value.HasValue)
return Unauthorized("用户不存在"); return Unauthorized("用户不存在");
var user = userResult.Value.Value; var user = userResult.Value.Value;
// 先获取资源信息以验证权限 // 先获取资源信息以验证权限
var resourceResult = db.GetResourceById(resourceId); var resourceResult = _resourceManager.GetResourceById(resourceId);
if (!resourceResult.IsSuccessful)
{
logger.Error($"获取资源时出错: {resourceResult.Error.Message}");
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源失败: {resourceResult.Error.Message}");
}
if (!resourceResult.Value.HasValue) if (!resourceResult.HasValue)
{ {
logger.Warn($"资源不存在: {resourceId}"); logger.Warn($"资源不存在: {resourceId}");
return NotFound($"资源 {resourceId} 不存在"); return NotFound($"资源 {resourceId} 不存在");
} }
var resource = resourceResult.Value.Value; var resource = resourceResult.Value;
// 权限检查:管理员可以删除所有资源,普通用户只能删除自己的用户资源 // 权限检查:管理员可以删除所有资源,普通用户只能删除自己的用户资源
if (!User.IsInRole("Admin")) if (!User.IsInRole("Admin"))
{ {
if (resource.ResourcePurpose == Resource.ResourcePurposes.Template) if (resource.Purpose == ResourcePurpose.Template)
return Forbid("普通用户不能删除模板资源"); return Forbid("普通用户不能删除模板资源");
if (resource.UserID != user.ID) if (resource.UserID != user.ID)
return Forbid("只能删除自己的资源"); return Forbid("只能删除自己的资源");
} }
var deleteResult = db.DeleteResource(resourceId); var deleteResult = _resourceManager.DeleteResource(resourceId);
if (!deleteResult.IsSuccessful) if (!deleteResult.IsSuccessful)
{ {
logger.Error($"删除资源时出错: {deleteResult.Error.Message}"); logger.Error($"删除资源时出错: {deleteResult.Error.Message}");
@@ -375,3 +284,76 @@ public class ResourceController : ControllerBase
} }
} }
} }
/// <summary>
/// 资源信息类
/// </summary>
public class ResourceInfo
{
/// <summary>
/// 资源ID
/// </summary>
public Guid ID { get; set; }
/// <summary>
/// 资源名称
/// </summary>
public string Name { get; set; } = string.Empty;
/// <summary>
/// 资源类型
/// </summary>
public string Type { get; set; } = string.Empty;
/// <summary>
/// 资源用途template/user
/// </summary>
public ResourcePurpose Purpose { get; set; }
/// <summary>
/// 上传时间
/// </summary>
public DateTime UploadTime { get; set; }
/// <summary>
/// 所属实验ID可选
/// </summary>
public string? ExamID { get; set; }
/// <summary>
/// MIME类型
/// </summary>
public string? MimeType { get; set; }
public ResourceInfo(Resource resource)
{
ID = resource.ID;
Name = resource.ResourceName;
Type = resource.ResourceType;
Purpose = resource.Purpose;
UploadTime = resource.UploadTime;
ExamID = resource.ExamID;
MimeType = resource.MimeType;
}
}
/// <summary>
/// 添加资源请求类
/// </summary>
public class AddResourceRequest
{
/// <summary>
/// 资源类型
/// </summary>
public required string ResourceType { get; set; }
/// <summary>
/// 资源用途template/user
/// </summary>
public required ResourcePurpose ResourcePurpose { get; set; }
/// <summary>
/// 所属实验ID可选
/// </summary>
public string? ExamID { get; set; }
}

View File

@@ -0,0 +1,127 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors;
using Peripherals.SwitchClient;
namespace server.Controllers;
[ApiController]
[Route("api/[controller]")]
public class SwitchController : ControllerBase
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly Database.UserManager _userManager = new();
/// <summary>
/// 获取示波器实例
/// </summary>
private SwitchCtrl? GetSwitchCtrl()
{
var userName = User.Identity?.Name;
if (string.IsNullOrEmpty(userName))
return null;
var userRet = _userManager.GetUserByName(userName);
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
return null;
var user = userRet.Value.Value;
if (user.BoardID == Guid.Empty)
return null;
var boardRet = _userManager.GetBoardByID(user.BoardID);
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
return null;
var board = boardRet.Value.Value;
return new SwitchCtrl(board.IpAddr, board.Port, 0);
}
/// <summary>
/// 启用或禁用 Switch 外设
/// </summary>
/// <param name="enable">是否启用</param>
/// <returns>操作结果</returns>
[HttpPost("enable")]
[EnableCors("Users")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> SetEnable([FromQuery] bool enable)
{
var switchCtrl = GetSwitchCtrl();
if (switchCtrl == null)
return BadRequest("Can't get user or board info");
var result = await switchCtrl.SetEnable(enable);
if (!result.IsSuccessful)
{
logger.Error(result.Error, "SetEnable failed");
return StatusCode(500, result.Error);
}
return Ok(result.Value);
}
/// <summary>
/// 控制指定编号的 Switch 开关
/// </summary>
/// <param name="num">开关编号</param>
/// <param name="onOff">开/关</param>
/// <returns>操作结果</returns>
[HttpPost("switch")]
[EnableCors("Users")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ArgumentException), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> SetSwitchOnOff([FromQuery] int num, [FromQuery] bool onOff)
{
if (num <= 0 || num > 6)
return BadRequest(new ArgumentException($"Switch num should be 1~5, instead of {num}"));
var switchCtrl = GetSwitchCtrl();
if (switchCtrl == null)
return BadRequest("Can't get user or board info");
var result = await switchCtrl.SetSwitchOnOff(num, onOff);
if (!result.IsSuccessful)
{
logger.Error(result.Error, $"SetSwitchOnOff({num}, {onOff}) failed");
return StatusCode(500, result.Error);
}
return Ok(result.Value);
}
/// <summary>
/// 控制 Switch 开关
/// </summary>
/// <param name="keyStatus">开关状态</param>
/// <returns>操作结果</returns>
[HttpPost("MultiSwitch")]
[EnableCors("Users")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ArgumentException), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> SetMultiSwitchsOnOff(bool[] keyStatus)
{
if (keyStatus.Length == 0 || keyStatus.Length > 6) return BadRequest(
new ArgumentException($"Switch num should be 1~5, instead of {keyStatus.Length}"));
var switchCtrl = GetSwitchCtrl();
if (switchCtrl == null)
return BadRequest("Can't get user or board info");
for (int i = 0; i < keyStatus.Length; i++)
{
var result = await switchCtrl.SetSwitchOnOff(i + 1, keyStatus[i]);
if (!result.IsSuccessful)
{
logger.Error(result.Error, $"SetSwitchOnOff({i}, {keyStatus[i]}) failed");
return StatusCode(500, result.Error);
}
if (!result.Value) return Ok(false);
}
return Ok(true);
}
}

View File

@@ -1,121 +1,81 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using DotNext;
using server.Services;
/// <summary> /// <summary>
/// 视频流控制器,支持动态配置摄像头连接 /// 视频流控制器,支持动态配置摄像头连接
/// </summary> /// </summary>
[ApiController] [ApiController]
[Authorize]
[EnableCors("Users")]
[Route("api/[controller]")] [Route("api/[controller]")]
public class VideoStreamController : ControllerBase public class VideoStreamController : ControllerBase
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly server.Services.HttpVideoStreamService _videoStreamService;
/// <summary> private readonly HttpVideoStreamService _videoStreamService;
/// 视频流信息结构体 private readonly Database.UserManager _userManager = new();
/// </summary>
public class StreamInfoResult public class AvailableResolutionsResponse
{ {
/// <summary>
/// TODO:
/// </summary>
public int FrameRate { get; set; }
/// <summary>
/// TODO:
/// </summary>
public int FrameWidth { get; set; }
/// <summary>
/// TODO:
/// </summary>
public int FrameHeight { get; set; }
/// <summary>
/// TODO:
/// </summary>
public string Format { get; set; } = "MJPEG";
/// <summary>
/// TODO:
/// </summary>
public string HtmlUrl { get; set; } = "";
/// <summary>
/// TODO:
/// </summary>
public string MjpegUrl { get; set; } = "";
/// <summary>
/// TODO:
/// </summary>
public string SnapshotUrl { get; set; } = "";
/// <summary>
/// TODO:
/// </summary>
public string UsbCameraUrl { get; set; } = "";
}
/// <summary>
/// 摄像头配置请求模型
/// </summary>
public class CameraConfigRequest
{
/// <summary>
/// 摄像头地址
/// </summary>
[Required]
[RegularExpression(@"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", ErrorMessage = "IP地址")]
public string Address { get; set; } = "";
/// <summary>
/// 摄像头端口
/// </summary>
[Required]
[Range(1, 65535, ErrorMessage = "端口必须在1-65535范围内")]
public int Port { get; set; }
}
/// <summary>
/// 分辨率配置请求模型
/// </summary>
public class ResolutionConfigRequest
{
/// <summary>
/// 宽度
/// </summary>
[Required]
[Range(640, 1920, ErrorMessage = "宽度必须在640-1920范围内")]
public int Width { get; set; } public int Width { get; set; }
/// <summary>
/// 高度
/// </summary>
[Required]
[Range(480, 1080, ErrorMessage = "高度必须在480-1080范围内")]
public int Height { get; set; } public int Height { get; set; }
public string Name { get; set; } = string.Empty;
public string Value => $"{Width}x{Height}";
} }
/// <summary> /// <summary>
/// 初始化HTTP视频流控制器 /// 初始化HTTP视频流控制器
/// </summary> /// </summary>
/// <param name="videoStreamService">HTTP视频流服务</param> /// <param name="videoStreamService">HTTP视频流服务</param>
public VideoStreamController(server.Services.HttpVideoStreamService videoStreamService) public VideoStreamController(HttpVideoStreamService videoStreamService)
{ {
logger.Info("创建VideoStreamController命名空间{Namespace}", this.GetType().Namespace); logger.Info("创建VideoStreamController命名空间{Namespace}", this.GetType().Namespace);
_videoStreamService = videoStreamService; _videoStreamService = videoStreamService;
} }
private Optional<string> TryGetBoardId()
{
var userName = User.FindFirstValue(ClaimTypes.Name);
if (string.IsNullOrEmpty(userName))
{
logger.Error("User name not found in claims.");
return Optional<string>.None;
}
var userRet = _userManager.GetUserByName(userName);
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
{
logger.Error("User not found.");
return Optional<string>.None;
}
var user = userRet.Value.Value;
var boardId = user.BoardID;
if (boardId == Guid.Empty)
{
logger.Error("No board bound to this user.");
return Optional<string>.None;
}
return boardId.ToString();
}
/// <summary> /// <summary>
/// 获取 HTTP 视频流服务状态 /// 获取 HTTP 视频流服务状态
/// </summary> /// </summary>
/// <returns>服务状态信息</returns> /// <returns>服务状态信息</returns>
[HttpGet("Status")] [HttpGet("ServiceStatus")]
[EnableCors("Users")] [ProducesResponseType(typeof(VideoStreamServiceStatus), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)] [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
public IResult GetStatus() public IResult GetServiceStatus()
{ {
try try
{ {
logger.Info("GetStatus方法被调用控制器{Controller}路径api/VideoStream/Status", this.GetType().Name);
// 使用HttpVideoStreamService提供的状态信息 // 使用HttpVideoStreamService提供的状态信息
var status = _videoStreamService.GetServiceStatus(); var status = _videoStreamService.GetServiceStatus();
@@ -129,101 +89,17 @@ public class VideoStreamController : ControllerBase
} }
} }
/// <summary> [HttpGet("MyEndpoint")]
/// 获取 HTTP 视频流信息 [ProducesResponseType(typeof(VideoStreamEndpoint), StatusCodes.Status200OK)]
/// </summary>
/// <returns>流信息</returns>
[HttpGet("StreamInfo")]
[EnableCors("Users")]
[ProducesResponseType(typeof(StreamInfoResult), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)] [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
public IResult GetStreamInfo() public IResult MyEndpoint()
{ {
try try
{ {
logger.Info("获取 HTTP 视频流信息"); var boardId = TryGetBoardId().OrThrow(() => new Exception("Board ID not found"));
var result = new StreamInfoResult var endpoint = _videoStreamService.GetVideoEndpoint(boardId);
{
FrameRate = _videoStreamService.FrameRate,
FrameWidth = _videoStreamService.FrameWidth,
FrameHeight = _videoStreamService.FrameHeight,
Format = "MJPEG",
HtmlUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/video-feed.html",
MjpegUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/video-stream",
SnapshotUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/snapshot",
UsbCameraUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/usb-camera"
};
return TypedResults.Ok(result);
}
catch (Exception ex)
{
logger.Error(ex, "获取 HTTP 视频流信息失败");
return TypedResults.InternalServerError(ex.Message);
}
}
/// <summary> return TypedResults.Ok(endpoint);
/// 配置摄像头连接参数
/// </summary>
/// <param name="config">摄像头配置</param>
/// <returns>配置结果</returns>
[HttpPost("ConfigureCamera")]
[EnableCors("Users")]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
public async Task<IResult> ConfigureCamera([FromBody] CameraConfigRequest config)
{
try
{
logger.Info("配置摄像头连接: {Address}:{Port}", config.Address, config.Port);
var success = await _videoStreamService.ConfigureCameraAsync(config.Address, config.Port);
if (success)
{
return TypedResults.Ok(new
{
success = true,
message = "摄像头配置成功",
cameraAddress = config.Address,
cameraPort = config.Port
});
}
else
{
return TypedResults.BadRequest(new
{
success = false,
message = "摄像头配置失败",
cameraAddress = config.Address,
cameraPort = config.Port
});
}
}
catch (Exception ex)
{
logger.Error(ex, "配置摄像头连接失败");
return TypedResults.InternalServerError(ex.Message);
}
}
/// <summary>
/// 获取当前摄像头配置
/// </summary>
/// <returns>摄像头配置信息</returns>
[HttpGet("CameraConfig")]
[EnableCors("Users")]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
public IResult GetCameraConfig()
{
try
{
logger.Info("获取摄像头配置");
var cameraStatus = _videoStreamService.GetCameraStatus();
return TypedResults.Ok(cameraStatus);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -232,60 +108,34 @@ public class VideoStreamController : ControllerBase
} }
} }
/// <summary>
/// 控制 HTTP 视频流服务开关
/// </summary>
/// <param name="enabled">是否启用服务</param>
/// <returns>操作结果</returns>
[HttpPost("SetEnabled")]
[EnableCors("Users")]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
public async Task<IResult> SetEnabled([FromQuery] bool enabled)
{
logger.Info("设置视频流服务开关: {Enabled}", enabled);
await _videoStreamService.SetEnable(enabled);
return TypedResults.Ok();
}
/// <summary> /// <summary>
/// 测试 HTTP 视频流连接 /// 测试 HTTP 视频流连接
/// </summary> /// </summary>
/// <returns>连接测试结果</returns> /// <returns>连接测试结果</returns>
[HttpPost("TestConnection")] [HttpPost("TestConnection")]
[EnableCors("Users")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)] [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)] [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
public async Task<IResult> TestConnection() public async Task<IResult> TestConnection()
{ {
try try
{ {
logger.Info("测试 HTTP 视频流连接"); var boardId = TryGetBoardId().OrThrow(() => new Exception("Board ID not found"));
var endpoint = _videoStreamService.GetVideoEndpoint(boardId);
// 尝试通过HTTP请求检查视频流服务是否可访问 // 尝试通过HTTP请求检查视频流服务是否可访问
bool isConnected = false; bool isConnected = false;
using (var httpClient = new HttpClient()) using (var httpClient = new HttpClient())
{ {
httpClient.Timeout = TimeSpan.FromSeconds(2); // 设置较短的超时时间 httpClient.Timeout = TimeSpan.FromSeconds(2); // 设置较短的超时时间
var response = await httpClient.GetAsync($"http://{Global.localhost}:{_videoStreamService.ServerPort}/"); var response = await httpClient.GetAsync(endpoint.MjpegUrl);
// 只要能连接上就认为成功,不管返回状态 // 只要能连接上就认为成功,不管返回状态
isConnected = response.IsSuccessStatusCode; isConnected = response.IsSuccessStatusCode;
} }
logger.Info("测试摄像头连接"); var ret = await _videoStreamService.TestCameraConnection(boardId);
var (isSuccess, message) = await _videoStreamService.TestCameraConnectionAsync(); return TypedResults.Ok(ret);
return TypedResults.Ok(new
{
isConnected = isConnected,
success = isSuccess,
message = message,
cameraAddress = _videoStreamService.CameraAddress,
cameraPort = _videoStreamService.CameraPort,
timestamp = DateTime.Now
});
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -295,6 +145,25 @@ public class VideoStreamController : ControllerBase
} }
} }
[HttpPost("SetVideoStreamEnable")]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> SetVideoStreamEnable(bool enable)
{
try
{
var boardId = TryGetBoardId().OrThrow(() => new ArgumentException("Board ID is required"));
await _videoStreamService.SetVideoStreamEnableAsync(boardId.ToString(), enable);
return Ok($"HDMI transmission for board {boardId} disabled.");
}
catch (Exception ex)
{
logger.Error(ex, $"Failed to disable HDMI transmission for board");
return StatusCode(500, $"Error disabling HDMI transmission: {ex.Message}");
}
}
/// <summary> /// <summary>
/// 设置视频流分辨率 /// 设置视频流分辨率
/// </summary> /// </summary>
@@ -309,16 +178,16 @@ public class VideoStreamController : ControllerBase
{ {
try try
{ {
logger.Info($"设置视频流分辨率为 {request.Width}x{request.Height}"); var boardId = TryGetBoardId().OrThrow(() => new Exception("Board ID not found"));
var (isSuccess, message) = await _videoStreamService.SetResolutionAsync(request.Width, request.Height); var ret = await _videoStreamService.SetResolutionAsync(boardId, request.Width, request.Height);
if (isSuccess) if (ret.IsSuccessful && ret.Value)
{ {
return TypedResults.Ok(new return TypedResults.Ok(new
{ {
success = true, success = true,
message = message, message = $"成功设置分辨率为 {request.Width}x{request.Height}",
width = request.Width, width = request.Width,
height = request.Height, height = request.Height,
timestamp = DateTime.Now timestamp = DateTime.Now
@@ -329,7 +198,7 @@ public class VideoStreamController : ControllerBase
return TypedResults.BadRequest(new return TypedResults.BadRequest(new
{ {
success = false, success = false,
message = message, message = ret.Error?.ToString() ?? "未知错误",
timestamp = DateTime.Now timestamp = DateTime.Now
}); });
} }
@@ -341,70 +210,29 @@ public class VideoStreamController : ControllerBase
} }
} }
/// <summary>
/// 获取当前分辨率
/// </summary>
/// <returns>当前分辨率信息</returns>
[HttpGet("Resolution")]
[EnableCors("Users")]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public IResult GetCurrentResolution()
{
try
{
logger.Info("获取当前视频流分辨率");
var (width, height) = _videoStreamService.GetCurrentResolution();
return TypedResults.Ok(new
{
width = width,
height = height,
resolution = $"{width}x{height}",
timestamp = DateTime.Now
});
}
catch (Exception ex)
{
logger.Error(ex, "获取当前分辨率失败");
return TypedResults.InternalServerError($"获取当前分辨率失败: {ex.Message}");
}
}
/// <summary> /// <summary>
/// 获取支持的分辨率列表 /// 获取支持的分辨率列表
/// </summary> /// </summary>
/// <returns>支持的分辨率列表</returns> /// <returns>支持的分辨率列表</returns>
[HttpGet("SupportedResolutions")] [HttpGet("SupportedResolutions")]
[EnableCors("Users")] [EnableCors("Users")]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)] [ProducesResponseType(typeof(AvailableResolutionsResponse[]), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public IResult GetSupportedResolutions() public IResult GetSupportedResolutions()
{ {
try // (640, 480, "640x480 (VGA)"),
// (960, 540, "960x540 (qHD)"),
// (1280, 720, "1280x720 (HD)"),
// (1280, 960, "1280x960 (SXGA)"),
// (1920, 1080, "1920x1080 (Full HD)")
return TypedResults.Ok(new AvailableResolutionsResponse[]
{ {
logger.Info("获取支持的分辨率列表"); new AvailableResolutionsResponse { Width = 640, Height = 480, Name = "640x480(VGA)" },
new AvailableResolutionsResponse { Width = 960, Height = 480, Name = "960x480(qHD)" },
var resolutions = _videoStreamService.GetSupportedResolutions(); new AvailableResolutionsResponse { Width = 1280, Height = 720, Name = "1280x720(HD)" },
new AvailableResolutionsResponse { Width = 1280, Height = 960, Name = "1280x960(SXGA)" },
return TypedResults.Ok(new new AvailableResolutionsResponse { Width = 1920, Height = 1080, Name = "1920x1080(Full HD)" }
{ });
resolutions = resolutions.Select(r => new
{
width = r.Width,
height = r.Height,
name = r.Name,
value = $"{r.Width}x{r.Height}"
}),
timestamp = DateTime.Now
});
}
catch (Exception ex)
{
logger.Error(ex, "获取支持的分辨率列表失败");
return TypedResults.InternalServerError($"获取支持的分辨率列表失败: {ex.Message}");
}
} }
/// <summary> /// <summary>
@@ -420,9 +248,9 @@ public class VideoStreamController : ControllerBase
{ {
try try
{ {
logger.Info("收到初始化自动对焦请求"); var boardId = TryGetBoardId().OrThrow(() => new Exception("Board ID not found"));
var result = await _videoStreamService.InitAutoFocusAsync(); var result = await _videoStreamService.InitAutoFocusAsync(boardId);
if (result) if (result)
{ {
@@ -465,9 +293,9 @@ public class VideoStreamController : ControllerBase
{ {
try try
{ {
logger.Info("收到执行自动对焦请求"); var boardId = TryGetBoardId().OrThrow(() => new Exception("Board ID not found"));
var result = await _videoStreamService.PerformAutoFocusAsync(); var result = await _videoStreamService.PerformAutoFocusAsync(boardId);
if (result) if (result)
{ {
@@ -498,59 +326,55 @@ public class VideoStreamController : ControllerBase
} }
/// <summary> /// <summary>
/// 执行一次自动对焦 (GET方式) /// 配置摄像头连接参数
/// </summary> /// </summary>
/// <returns>对焦结果</returns> /// <returns>配置结果</returns>
[HttpGet("Focus")] [HttpPost("ConfigureCamera")]
[EnableCors("Users")] [EnableCors("Users")]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)] [ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
public async Task<IResult> Focus() public async Task<IResult> ConfigureCamera()
{ {
try try
{ {
logger.Info("收到执行一次对焦请求 (GET)"); var boardId = TryGetBoardId().OrThrow(() => new Exception("Board ID not found"));
// 检查摄像头是否已配置 var ret = await _videoStreamService.ConfigureCameraAsync(boardId);
if (!_videoStreamService.IsCameraConfigured())
if (ret)
{ {
logger.Warn("摄像头未配置,无法执行对焦"); return TypedResults.Ok(new { Message = "配置成功" });
return TypedResults.BadRequest(new
{
success = false,
message = "摄像头未配置,请先配置摄像头连接",
timestamp = DateTime.Now
});
}
var result = await _videoStreamService.PerformAutoFocusAsync();
if (result)
{
logger.Info("对焦执行成功");
return TypedResults.Ok(new
{
success = true,
message = "对焦执行成功",
timestamp = DateTime.Now
});
} }
else else
{ {
logger.Warn("对焦执行失败"); return TypedResults.BadRequest(new { Message = "配置失败" });
return TypedResults.BadRequest(new
{
success = false,
message = "对焦执行失败",
timestamp = DateTime.Now
});
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.Error(ex, "执行对焦时发生异常"); logger.Error(ex, "配置摄像头连接失败");
return TypedResults.InternalServerError($"执行对焦失败: {ex.Message}"); return TypedResults.InternalServerError(ex.Message);
} }
} }
/// <summary>
/// 分辨率配置请求模型
/// </summary>
public class ResolutionConfigRequest
{
/// <summary>
/// 宽度
/// </summary>
[Required]
[Range(640, 1920, ErrorMessage = "宽度必须在640-1920范围内")]
public int Width { get; set; }
/// <summary>
/// 高度
/// </summary>
[Required]
[Range(480, 1080, ErrorMessage = "高度必须在480-1080范围内")]
public int Height { get; set; }
}
} }

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,98 @@
using DotNext;
using LinqToDB;
using LinqToDB.Data;
namespace Database;
/// <summary>
/// 应用程序数据连接类,用于与数据库交互
/// </summary>
public class AppDataConnection : DataConnection
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
static readonly string DATABASE_FILEPATH = $"{Global.DataPath}/Database.sqlite";
static readonly LinqToDB.DataOptions options =
new LinqToDB.DataOptions().UseSQLite($"Data Source={DATABASE_FILEPATH}");
/// <summary>
/// 用户表
/// </summary>
public ITable<User> UserTable => this.GetTable<User>();
/// <summary>
/// FPGA 板子表
/// </summary>
public ITable<Board> BoardTable => this.GetTable<Board>();
/// <summary>
/// 实验表
/// </summary>
public ITable<Exam> ExamTable => this.GetTable<Exam>();
/// <summary>
/// 资源表(统一管理实验资源、用户比特流等)
/// </summary>
public ITable<Resource> ResourceTable => this.GetTable<Resource>();
/// <summary>
/// 初始化应用程序数据连接
/// </summary>
public AppDataConnection() : base(options)
{
var filePath = Path.GetDirectoryName(DATABASE_FILEPATH);
if (!string.IsNullOrEmpty(filePath) && !Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
if (!Path.Exists(DATABASE_FILEPATH))
{
logger.Info($"数据库文件不存在,正在创建新数据库: {DATABASE_FILEPATH}");
LinqToDB.DataProvider.SQLite.SQLiteTools.CreateDatabase(DATABASE_FILEPATH);
this.CreateAllTables();
var user = new User()
{
Name = "Admin",
EMail = "selfconfusion@gmail.com",
Password = "12345678",
Permission = Database.UserPermission.Admin,
};
this.Insert(user);
logger.Info("默认管理员用户已创建");
}
else
{
logger.Info($"数据库连接已建立: {DATABASE_FILEPATH}");
}
}
/// <summary>
/// 创建所有数据库表
/// </summary>
public void CreateAllTables()
{
logger.Info("正在创建数据库表...");
this.CreateTable<User>();
this.CreateTable<Board>();
this.CreateTable<Exam>();
this.CreateTable<Resource>();
logger.Info("数据库表创建完成");
}
/// <summary>
/// 删除所有数据库表
/// </summary>
public void DropAllTables()
{
logger.Warn("正在删除所有数据库表...");
this.DropTable<User>();
this.DropTable<Board>();
this.DropTable<Exam>();
this.DropTable<Resource>();
logger.Warn("所有数据库表已删除");
}
}

View File

@@ -0,0 +1,149 @@
using DotNext;
using LinqToDB;
using LinqToDB.Data;
namespace Database;
public class ExamManager
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private AppDataConnection _db = new();
/// <summary>
/// 创建新实验
/// </summary>
/// <param name="id">实验ID</param>
/// <param name="name">实验名称</param>
/// <param name="description">实验描述</param>
/// <param name="tags">实验标签</param>
/// <param name="difficulty">实验难度</param>
/// <param name="isVisibleToUsers">普通用户是否可见</param>
/// <returns>创建的实验</returns>
public Result<Exam> CreateExam(string id, string name, string description, string[]? tags = null, int difficulty = 1, bool isVisibleToUsers = true)
{
try
{
// 检查实验ID是否已存在
var existingExam = _db.ExamTable.Where(e => e.ID.ToString() == id).FirstOrDefault();
if (existingExam != null)
{
logger.Error($"实验ID已存在: {id}");
return new(new Exception($"实验ID已存在: {id}"));
}
var exam = new Exam
{
ID = id,
Name = name,
Description = description,
Difficulty = Math.Max(1, Math.Min(5, difficulty)),
IsVisibleToUsers = isVisibleToUsers,
CreatedTime = DateTime.Now,
UpdatedTime = DateTime.Now
};
if (tags != null)
{
exam.SetTagsList(tags);
}
_db.Insert(exam);
logger.Info($"新实验已创建: {id} ({name})");
return new(exam);
}
catch (Exception ex)
{
logger.Error($"创建实验时出错: {ex.Message}");
return new(ex);
}
}
/// <summary>
/// 更新实验信息
/// </summary>
/// <param name="id">实验ID</param>
/// <param name="name">实验名称</param>
/// <param name="description">实验描述</param>
/// <param name="tags">实验标签</param>
/// <param name="difficulty">实验难度</param>
/// <param name="isVisibleToUsers">普通用户是否可见</param>
/// <returns>更新的记录数</returns>
public Result<int> UpdateExam(string id, string? name = null, string? description = null, string[]? tags = null, int? difficulty = null, bool? isVisibleToUsers = null)
{
try
{
int result = 0;
if (name != null)
{
result += _db.ExamTable.Where(e => e.ID.ToString() == id).Set(e => e.Name, name).Update();
}
if (description != null)
{
result += _db.ExamTable.Where(e => e.ID.ToString() == id).Set(e => e.Description, description).Update();
}
if (tags != null)
{
var tagsString = string.Join(",", tags.Where(tag => !string.IsNullOrWhiteSpace(tag)).Select(tag => tag.Trim()));
result += _db.ExamTable.Where(e => e.ID.ToString() == id).Set(e => e.Tags, tagsString).Update();
}
if (difficulty.HasValue)
{
result += _db.ExamTable.Where(e => e.ID.ToString() == id).Set(e => e.Difficulty, Math.Max(1, Math.Min(5, difficulty.Value))).Update();
}
if (isVisibleToUsers.HasValue)
{
result += _db.ExamTable.Where(e => e.ID.ToString() == id).Set(e => e.IsVisibleToUsers, isVisibleToUsers.Value).Update();
}
// 更新时间
_db.ExamTable.Where(e => e.ID.ToString() == id).Set(e => e.UpdatedTime, DateTime.Now).Update();
logger.Info($"实验已更新: {id},更新记录数: {result}");
return new(result);
}
catch (Exception ex)
{
logger.Error($"更新实验时出错: {ex.Message}");
return new(ex);
}
}
/// <summary>
/// 获取所有实验信息
/// </summary>
/// <returns>所有实验的数组</returns>
public Exam[] GetAllExams()
{
var exams = _db.ExamTable.OrderBy(e => e.ID).ToArray();
logger.Debug($"获取所有实验,共 {exams.Length} 个");
return exams;
}
/// <summary>
/// 根据实验ID获取实验信息
/// </summary>
/// <param name="examId">实验ID</param>
/// <returns>包含实验信息的结果,如果未找到则返回空</returns>
public Result<Optional<Exam>> GetExamByID(string examId)
{
var exams = _db.ExamTable.Where(exam => exam.ID.ToString() == examId).ToArray();
if (exams.Length > 1)
{
logger.Error($"数据库中存在多个相同ID的实验: {examId}");
return new(new Exception($"数据库中存在多个相同ID的实验: {examId}"));
}
if (exams.Length == 0)
{
logger.Info($"未找到ID对应的实验: {examId}");
return new(Optional<Exam>.None);
}
logger.Debug($"成功获取实验信息: {examId}");
return new(exams[0]);
}
}

View File

@@ -0,0 +1,348 @@
using DotNext;
using LinqToDB;
using LinqToDB.Data;
using System.Security.Cryptography;
namespace Database;
public class ResourceManager
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly AppDataConnection _db = new();
/// <summary>
/// 根据文件扩展名获取MIME类型
/// </summary>
/// <param name="extension">文件扩展名</param>
/// <param name="fileName">文件名(可选,用于特殊文件判断)</param>
/// <returns>MIME类型</returns>
private string GetMimeTypeFromExtension(string extension, string fileName = "")
{
// 特殊文件名处理
if (!string.IsNullOrEmpty(fileName) && fileName.Equals("diagram.json", StringComparison.OrdinalIgnoreCase))
{
return "application/json";
}
return extension.ToLowerInvariant() switch
{
".png" => "image/png",
".jpg" or ".jpeg" => "image/jpeg",
".gif" => "image/gif",
".bmp" => "image/bmp",
".svg" => "image/svg+xml",
".bit" => "application/octet-stream",
".sbit" => "application/octet-stream",
".bin" => "application/octet-stream",
".mcs" => "application/octet-stream",
".hex" => "text/plain",
".json" => "application/json",
".zip" => "application/zip",
".md" => "text/markdown",
_ => "application/octet-stream"
};
}
/// <summary>
/// 将二进制数据写入指定路径
/// </summary>
/// <param name="path">目标文件路径</param>
/// <param name="data">要写入的二进制数据</param>
/// <returns>写入是否成功</returns>
public Result<bool> WriteBytesToPath(string path, byte[] data)
{
try
{
var filePath = Path.Combine(Global.DataPath, path);
var directory = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
File.WriteAllBytes(filePath, data);
logger.Info($"成功写入文件: {filePath},大小: {data.Length} bytes");
return new(true);
}
catch (Exception ex)
{
logger.Error($"写入文件时出错: {ex.Message}");
return new(ex);
}
}
/// <summary>
/// 从指定路径读取二进制数据
/// </summary>
/// <param name="path">要读取的文件路径</param>
/// <returns>读取到的二进制数据</returns>
public Result<byte[]> ReadBytesFromPath(string path)
{
try
{
var filePath = Path.Combine(Global.DataPath, path);
if (!File.Exists(filePath))
{
logger.Error($"文件不存在: {filePath}");
return new(new Exception($"文件不存在: {filePath}"));
}
var data = File.ReadAllBytes(filePath);
logger.Info($"成功读取文件: {filePath},大小: {data.Length} bytes");
return new(data);
}
catch (Exception ex)
{
logger.Error($"读取文件时出错: {ex.Message}");
return new(ex);
}
}
/// <summary>
/// 添加资源
/// </summary>
/// <param name="userId">上传用户ID</param>
/// <param name="resourceType">资源类型</param>
/// <param name="resourcePurpose">资源用途template 或 user</param>
/// <param name="resourceName">资源名称</param>
/// <param name="data">资源二进制数据</param>
/// <param name="examId">所属实验ID可选</param>
/// <param name="mimeType">MIME类型可选将根据文件扩展名自动确定</param>
/// <returns>创建的资源</returns>
public Result<Resource> AddResource(
Guid userId, string resourceType, ResourcePurpose resourcePurpose,
string resourceName, byte[] data, string? examId = null, string? mimeType = null)
{
try
{
// 验证用户是否存在
var user = _db.UserTable.Where(u => u.ID == userId).FirstOrDefault();
if (user == null)
{
logger.Error($"用户不存在: {userId}");
return new(new Exception($"用户不存在: {userId}"));
}
// 如果指定了实验ID验证实验是否存在
if (!string.IsNullOrEmpty(examId))
{
var exam = _db.ExamTable.Where(e => e.ID.ToString() == examId).FirstOrDefault();
if (exam == null)
{
logger.Error($"实验不存在: {examId}");
return new(new Exception($"实验不存在: {examId}"));
}
}
// 验证资源用途
if (resourcePurpose != ResourcePurpose.Template &&
resourcePurpose != ResourcePurpose.User)
{
logger.Error($"无效的资源用途: {resourcePurpose}");
return new(new Exception($"无效的资源用途: {resourcePurpose}"));
}
// 如果未指定MIME类型根据文件扩展名自动确定
if (string.IsNullOrEmpty(mimeType))
{
var extension = Path.GetExtension(resourceName).ToLowerInvariant();
mimeType = GetMimeTypeFromExtension(extension, resourceName);
}
// 计算数据的SHA256
var sha256 = SHA256.HashData(data).ToString();
if (string.IsNullOrEmpty(sha256))
{
logger.Error($"SHA256计算失败");
return new(new Exception("SHA256计算失败"));
}
var duplicateResource = _db.ResourceTable.Where(r => r.SHA256 == sha256).FirstOrDefault();
if (duplicateResource != null && duplicateResource.ResourceName == resourceName)
{
logger.Info($"资源已存在: {resourceName}, ID: {duplicateResource.ID}, UserID: {duplicateResource.UserID}");
return duplicateResource;
}
var nowTime = DateTime.Now;
var resource = new Resource
{
UserID = userId,
ExamID = examId,
ResourceType = resourceType,
Purpose = resourcePurpose,
ResourceName = resourceName,
Path = duplicateResource == null ?
Path.Combine(resourceType, nowTime.ToString("yyyyMMddHH"), resourceName) :
duplicateResource.Path,
SHA256 = sha256,
MimeType = mimeType,
UploadTime = nowTime
};
var insertedId = _db.Insert(resource);
var writeRet = WriteBytesToPath(resource.Path, data);
if (writeRet.IsSuccessful && writeRet.Value)
{
logger.Info($"新资源已添加: {userId}/{resourceType}/{resourcePurpose}/{resourceName} ({data.Length} bytes)" +
(examId != null ? $" [实验: {examId}]" : "") + $" [ID: {resource.ID}]");
return new(resource);
}
else
{
_db.ResourceTable.Where(r => r.ID == resource.ID).Delete();
logger.Error($"写入资源文件时出错: {writeRet.Error}");
return new(new Exception(writeRet.Error?.ToString() ?? $"写入失败"));
}
}
catch (Exception ex)
{
logger.Error($"添加资源时出错: {ex.Message}");
return new(ex);
}
}
/// <summary>
/// 获取资源信息列表返回ID和名称
/// <param name="resourceType">资源类型</param>
/// <param name="examId">实验ID可选</param>
/// <param name="resourcePurpose">资源用途(可选)</param>
/// <param name="userId">用户ID可选</param>
/// </summary>
/// <returns>资源信息列表</returns>
public Result<Resource[]> GetResourceListByType(
string resourceType,
ResourcePurpose? resourcePurpose = null,
string? examId = null,
Guid? userId = null)
{
try
{
var query = _db.ResourceTable.Where(r => r.ResourceType == resourceType);
if (examId != null)
{
query = query.Where(r => r.ExamID == examId);
}
if (resourcePurpose != null)
{
query = query.Where(r => r.Purpose == resourcePurpose);
}
if (userId != null)
{
query = query.Where(r => r.UserID == userId);
}
var resources = query.ToArray();
logger.Info($"获取资源列表: {resourceType}" +
(examId != null ? $"/{examId}" : "") +
($"/{resourcePurpose.ToString()}") +
(userId != null ? $"/{userId}" : "") +
$",共 {resources.Length} 个资源");
return new(resources);
}
catch (Exception ex)
{
logger.Error($"获取资源列表时出错: {ex.Message}");
return new(ex);
}
}
/// <summary>
/// 获取完整的资源列表
/// </summary>
/// <param name="examId">实验ID可选</param>
/// <param name="resourceType">资源类型(可选)</param>
/// <param name="resourcePurpose">资源用途(可选)</param>
/// <param name="userId">用户ID可选</param>
/// <returns>完整的资源对象列表</returns>
public Result<List<Resource>> GetFullResourceList(
string? examId = null,
string? resourceType = null,
ResourcePurpose? resourcePurpose = null,
Guid? userId = null)
{
try
{
var query = _db.ResourceTable.AsQueryable();
if (examId != null)
{
query = query.Where(r => r.ExamID == examId);
}
if (resourceType != null)
{
query = query.Where(r => r.ResourceType == resourceType);
}
if (resourcePurpose != null)
{
query = query.Where(r => r.Purpose == resourcePurpose);
}
if (userId != null)
{
query = query.Where(r => r.UserID == userId);
}
var resources = query.OrderByDescending(r => r.UploadTime).ToList();
logger.Info($"获取完整资源列表" +
(examId != null ? $" [实验: {examId}]" : "") +
(resourceType != null ? $" [类型: {resourceType}]" : "") +
($" [用途: {resourcePurpose.ToString()}]") +
(userId != null ? $" [用户: {userId}]" : "") +
$",共 {resources.Count} 个资源");
return new(resources);
}
catch (Exception ex)
{
logger.Error($"获取完整资源列表时出错: {ex.Message}");
return new(ex);
}
}
/// <summary>
/// 根据资源ID获取资源
/// </summary>
/// <param name="resourceId">资源ID</param>
/// <returns>资源数据</returns>
public Optional<Resource> GetResourceById(Guid resourceId)
{
var resource = _db.ResourceTable.Where(r => r.ID == resourceId).FirstOrDefault();
if (resource == null)
{
logger.Info($"未找到资源: {resourceId}");
return new(null);
}
logger.Info($"成功获取资源: {resourceId} ({resource.ResourceName})");
return new(resource);
}
/// <summary>
/// 删除资源
/// </summary>
/// <param name="resourceId">资源ID</param>
/// <returns>删除的记录数</returns>
public Result<int> DeleteResource(Guid resourceId)
{
try
{
var result = _db.ResourceTable.Where(r => r.ID == resourceId).Delete();
logger.Info($"资源已删除: {resourceId},删除记录数: {result}");
return new(result);
}
catch (Exception ex)
{
logger.Error($"删除资源时出错: {ex.Message}");
return new(ex);
}
}
}

350
server/src/Database/Type.cs Normal file
View File

@@ -0,0 +1,350 @@
using DotNext;
using LinqToDB;
using LinqToDB.Mapping;
namespace Database;
/// <summary>
/// 用户权限枚举
/// </summary>
public enum UserPermission
{
/// <summary>
/// 管理员权限,可以管理用户和实验板
/// </summary>
Admin,
/// <summary>
/// 普通用户权限,只能使用实验板
/// </summary>
Normal,
}
/// <summary>
/// 用户类,表示用户信息
/// </summary>
public class User
{
/// <summary>
/// 用户的唯一标识符
/// </summary>
[PrimaryKey]
public Guid ID { get; set; } = Guid.NewGuid();
/// <summary>
/// 用户的名称
/// </summary>
[NotNull]
public required string Name { get; set; }
/// <summary>
/// 用户的电子邮箱
/// </summary>
[NotNull]
public required string EMail { get; set; }
/// <summary>
/// 用户的密码(应该进行哈希处理)
/// </summary>
[NotNull]
public required string Password { get; set; }
/// <summary>
/// 用户权限等级
/// </summary>
[NotNull]
public required UserPermission Permission { get; set; }
/// <summary>
/// 绑定的实验板ID如果未绑定则为空
/// </summary>
[Nullable]
public Guid BoardID { get; set; }
/// <summary>
/// 用户绑定板子的过期时间
/// </summary>
[Nullable]
public DateTime? BoardExpireTime { get; set; }
}
/// <summary>
/// FPGA 板子状态枚举
/// </summary>
public enum BoardStatus
{
/// <summary>
/// 未启用状态,无法被使用
/// </summary>
Disabled,
/// <summary>
/// 繁忙状态,正在被用户使用
/// </summary>
Busy,
/// <summary>
/// 可用状态,可以被分配给用户
/// </summary>
Available,
}
/// <summary>
/// FPGA 板子类,表示板子信息
/// </summary>
public class Board
{
/// <summary>
/// FPGA 板子的唯一标识符
/// </summary>
[PrimaryKey]
public Guid ID { get; set; } = Guid.NewGuid();
/// <summary>
/// FPGA 板子的名称
/// </summary>
[NotNull]
public required string BoardName { get; set; }
/// <summary>
/// FPGA 板子的IP地址
/// </summary>
[NotNull]
public required string IpAddr { get; set; }
/// <summary>
/// FPGA 板子的MAC地址
/// </summary>
[NotNull]
public required string MacAddr { get; set; }
/// <summary>
/// FPGA 板子的通信端口
/// </summary>
[NotNull]
public int Port { get; set; } = 1234;
/// <summary>
/// FPGA 板子的当前状态
/// </summary>
[NotNull]
public required BoardStatus Status { get; set; }
/// <summary>
/// 占用该板子的用户的唯一标识符
/// </summary>
[Nullable]
public Guid OccupiedUserID { get; set; }
/// <summary>
/// 占用该板子的用户的用户名
/// </summary>
[Nullable]
public string? OccupiedUserName { get; set; }
/// <summary>
/// FPGA 板子的固件版本号
/// </summary>
[NotNull]
public string FirmVersion { get; set; } = "1.0.0";
}
/// <summary>
/// 实验类,表示实验信息
/// </summary>
public class Exam
{
/// <summary>
/// 实验的唯一标识符
/// </summary>
[PrimaryKey]
public required string ID { get; set; }
/// <summary>
/// 实验名称
/// </summary>
[NotNull]
public required string Name { get; set; }
/// <summary>
/// 实验描述
/// </summary>
[NotNull]
public required string Description { get; set; }
/// <summary>
/// 实验创建时间
/// </summary>
[NotNull]
public DateTime CreatedTime { get; set; } = DateTime.Now;
/// <summary>
/// 实验最后更新时间
/// </summary>
[NotNull]
public DateTime UpdatedTime { get; set; } = DateTime.Now;
/// <summary>
/// 实验标签(以逗号分隔的字符串)
/// </summary>
[NotNull]
public string Tags { get; set; } = "";
/// <summary>
/// 实验难度1-51为最简单
/// </summary>
[NotNull]
public int Difficulty { get; set; } = 1;
/// <summary>
/// 普通用户是否可见
/// </summary>
[NotNull]
public bool IsVisibleToUsers { get; set; } = true;
/// <summary>
/// 获取标签列表
/// </summary>
/// <returns>标签数组</returns>
public string[] GetTagsList()
{
if (string.IsNullOrWhiteSpace(Tags))
return Array.Empty<string>();
return Tags.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(tag => tag.Trim())
.Where(tag => !string.IsNullOrEmpty(tag))
.ToArray();
}
/// <summary>
/// 设置标签列表
/// </summary>
/// <param name="tags">标签数组</param>
public void SetTagsList(string[] tags)
{
Tags = string.Join(",", tags.Where(tag => !string.IsNullOrWhiteSpace(tag)).Select(tag => tag.Trim()));
}
}
/// <summary>
/// 资源类型枚举
/// </summary>
public static class ResourceTypes
{
/// <summary>
/// 图片资源类型
/// </summary>
public const string Images = "images";
/// <summary>
/// Markdown文档资源类型
/// </summary>
public const string Markdown = "markdown";
/// <summary>
/// 比特流文件资源类型
/// </summary>
public const string Bitstream = "bitstream";
/// <summary>
/// 原理图资源类型
/// </summary>
public const string Diagram = "diagram";
/// <summary>
/// 项目文件资源类型
/// </summary>
public const string Project = "project";
/// <summary>
/// 压缩文件资源类型
/// </summary>
public const string Compression = "compression";
}
public enum ResourcePurpose : int
{
/// <summary>
/// 模板资源,通常由管理员上传,供用户参考
/// </summary>
Template,
/// <summary>
/// 用户上传的资源
/// </summary>
User,
/// <summary>
/// 用户提交的作业
/// </summary>
Homework
}
/// <summary>
/// 资源类,统一管理实验资源、用户比特流等各类资源
/// </summary>
public class Resource
{
/// <summary>
/// 资源的唯一标识符
/// </summary>
[PrimaryKey]
public Guid ID { get; set; } = Guid.NewGuid();
/// <summary>
/// 上传资源的用户ID
/// </summary>
[NotNull]
public required Guid UserID { get; set; }
/// <summary>
/// 所属实验ID可选如果不属于特定实验则为空
/// </summary>
[Nullable]
public string? ExamID { get; set; }
/// <summary>
/// 资源类型images, markdown, bitstream, diagram, project等
/// </summary>
[NotNull]
public required string ResourceType { get; set; }
/// <summary>
/// 资源用途template模板或 user用户上传
/// </summary>
[NotNull]
public required ResourcePurpose Purpose { get; set; }
/// <summary>
/// 资源名称(包含文件扩展名)
/// </summary>
[NotNull]
public required string ResourceName { get; set; }
/// <summary>
/// 资源路径(包含文件名和扩展名)
/// </summary>
[NotNull]
public required string Path { get; set; }
/// <summary>
/// 资源SHA256哈希值
/// </summary>
[NotNull]
public required string SHA256 { get; set; }
/// <summary>
/// 资源创建/上传时间
/// </summary>
[NotNull]
public DateTime UploadTime { get; set; } = DateTime.Now;
/// <summary>
/// 资源的MIME类型
/// </summary>
[NotNull]
public string MimeType { get; set; } = "application/octet-stream";
}

View File

@@ -0,0 +1,453 @@
using DotNext;
using LinqToDB;
using LinqToDB.Data;
namespace Database;
public class UserManager
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly AppDataConnection _db = new();
/// <summary>
/// 添加一个新的用户到数据库
/// </summary>
/// <param name="name">用户的名称</param>
/// <param name="email">用户的电子邮箱地址</param>
/// <param name="password">用户的密码</param>
/// <returns>插入的记录数</returns>
public int AddUser(string name, string email, string password)
{
var user = new User()
{
Name = name,
EMail = email,
Password = password,
Permission = UserPermission.Normal,
};
var result = _db.Insert(user);
logger.Info($"新用户已添加: {name} ({email})");
return result;
}
/// <summary>
/// 根据用户名获取用户信息
/// </summary>
/// <param name="name">用户名</param>
/// <returns>包含用户信息的结果,如果未找到或出错则返回相应状态</returns>
public Result<Optional<User>> GetUserByName(string name)
{
var user = _db.UserTable.Where((user) => user.Name == name).ToArray();
if (user.Length > 1)
{
logger.Error($"数据库中存在多个同名用户: {name}");
return new(new Exception($"数据库中存在多个同名用户: {name}"));
}
if (user.Length == 0)
{
logger.Info($"未找到用户: {name}");
return new(Optional<User>.None);
}
logger.Debug($"成功获取用户信息: {name}");
return new(user[0]);
}
/// <summary>
/// 根据电子邮箱获取用户信息
/// </summary>
/// <param name="email">用户的电子邮箱地址</param>
/// <returns>包含用户信息的结果,如果未找到或出错则返回相应状态</returns>
public Result<Optional<User>> GetUserByEMail(string email)
{
var user = _db.UserTable.Where((user) => user.EMail == email).ToArray();
if (user.Length > 1)
{
logger.Error($"数据库中存在多个相同邮箱的用户: {email}");
return new(new Exception($"数据库中存在多个相同邮箱的用户: {email}"));
}
if (user.Length == 0)
{
logger.Info($"未找到邮箱对应的用户: {email}");
return new(Optional<User>.None);
}
logger.Debug($"成功获取用户信息: {email}");
return new(user[0]);
}
/// <summary>
/// 验证用户密码
/// </summary>
/// <param name="name">用户名</param>
/// <param name="password">用户密码</param>
/// <returns>如果密码正确返回用户信息,否则返回空</returns>
public Result<Optional<User>> CheckUserPassword(string name, string password)
{
var ret = GetUserByName(name);
if (!ret.IsSuccessful)
return new(ret.Error);
if (!ret.Value.HasValue)
return new(Optional<User>.None);
var user = ret.Value.Value;
if (user.Password == password)
{
logger.Info($"用户 {name} 密码验证成功");
return new(user);
}
else
{
logger.Warn($"用户 {name} 密码验证失败");
return new(Optional<User>.None);
}
}
/// <summary>
/// 绑定用户与实验板
/// </summary>
/// <param name="userId">用户的唯一标识符</param>
/// <param name="boardId">实验板的唯一标识符</param>
/// <param name="expireTime">绑定过期时间</param>
/// <returns>更新的记录数</returns>
public int BindUserToBoard(Guid userId, Guid boardId, DateTime expireTime)
{
// 获取用户信息
var user = _db.UserTable.Where(u => u.ID == userId).FirstOrDefault();
if (user == null)
{
logger.Error($"未找到用户: {userId}");
return 0;
}
// 更新用户的板子绑定信息
var userResult = _db.UserTable
.Where(u => u.ID == userId)
.Set(u => u.BoardID, boardId)
.Set(u => u.BoardExpireTime, expireTime)
.Update();
// 更新板子的用户绑定信息
var boardResult = _db.BoardTable
.Where(b => b.ID == boardId)
.Set(b => b.Status, BoardStatus.Busy)
.Set(b => b.OccupiedUserID, userId)
.Set(b => b.OccupiedUserName, user.Name)
.Update();
logger.Info($"用户 {userId} ({user.Name}) 已绑定到实验板 {boardId},过期时间: {expireTime}");
return userResult + boardResult;
}
/// <summary>
/// 解除用户与实验板的绑定
/// </summary>
/// <param name="userId">用户的唯一标识符</param>
/// <returns>更新的记录数</returns>
public int UnbindUserFromBoard(Guid userId)
{
// 获取用户当前绑定的板子ID
var user = _db.UserTable.Where(u => u.ID == userId).FirstOrDefault();
Guid boardId = user?.BoardID ?? Guid.Empty;
// 清空用户的板子绑定信息
var userResult = _db.UserTable
.Where(u => u.ID == userId)
.Set(u => u.BoardID, Guid.Empty)
.Set(u => u.BoardExpireTime, (DateTime?)null)
.Update();
// 如果用户原本绑定了板子,则清空板子的用户绑定信息
int boardResult = 0;
if (boardId != Guid.Empty)
{
boardResult = _db.BoardTable
.Where(b => b.ID == boardId)
.Set(b => b.Status, BoardStatus.Available)
.Set(b => b.OccupiedUserID, Guid.Empty)
.Set(b => b.OccupiedUserName, (string?)null)
.Update();
logger.Info($"实验板 {boardId} 状态已设置为空闲,用户绑定信息已清空");
}
logger.Info($"用户 {userId} 已解除实验板绑定");
return userResult + boardResult;
}
/// <summary>
/// 自动分配一个未被占用的IP地址
/// </summary>
/// <returns>分配的IP地址字符串</returns>
public string AllocateIpAddr()
{
var usedIps = _db.BoardTable.Select(b => b.IpAddr).ToArray();
for (int i = 1; i <= 254; i++)
{
string ip = $"169.254.109.{i}";
if (!usedIps.Contains(ip))
return ip;
}
throw new Exception("没有可用的IP地址");
}
/// <summary>
/// 自动分配一个未被占用的MAC地址
/// </summary>
/// <returns>分配的MAC地址字符串</returns>
public string AllocateMacAddr()
{
var usedMacs = _db.BoardTable.Select(b => b.MacAddr).ToArray();
// 以 02-00-00-xx-xx-xx 格式分配02 表示本地管理地址
for (int i = 1; i <= 0xFFFFFF; i++)
{
string mac = $"02-00-00-{(i >> 16) & 0xFF:X2}-{(i >> 8) & 0xFF:X2}-{i & 0xFF:X2}";
if (!usedMacs.Contains(mac))
return mac;
}
throw new Exception("没有可用的MAC地址");
}
/// <summary>
/// 添加一块新的 FPGA 板子到数据库
/// </summary>
/// <param name="name">FPGA 板子的名称</param>
/// <returns>插入的记录数</returns>
public Guid AddBoard(string name)
{
if (string.IsNullOrWhiteSpace(name) || name.Contains('\'') || name.Contains(';'))
{
logger.Error("实验板名称非法,包含不允许的字符");
throw new ArgumentException("实验板名称非法");
}
var board = new Board()
{
BoardName = name,
IpAddr = AllocateIpAddr(),
MacAddr = AllocateMacAddr(),
Status = BoardStatus.Disabled,
};
var result = _db.Insert(board);
logger.Info($"新实验板已添加: {name} ({board.IpAddr}:{board.MacAddr})");
return board.ID;
}
/// <summary>
/// 根据名称删除实验板
/// </summary>
/// <param name="name">实验板的名称</param>
/// <returns>删除的记录数</returns>
public int DeleteBoardByName(string name)
{
// 先获取要删除的板子信息
var board = _db.BoardTable.Where(b => b.BoardName == name).FirstOrDefault();
if (board == null)
{
logger.Warn($"未找到名称为 {name} 的实验板");
return 0;
}
// 如果板子被占用,先解除绑定
if (board.OccupiedUserID != Guid.Empty)
{
_db.UserTable
.Where(u => u.ID == board.OccupiedUserID)
.Set(u => u.BoardID, Guid.Empty)
.Set(u => u.BoardExpireTime, (DateTime?)null)
.Update();
logger.Info($"已解除用户 {board.OccupiedUserID} 与实验板 {name} 的绑定");
}
var result = _db.BoardTable.Where(b => b.BoardName == name).Delete();
logger.Info($"实验板已删除: {name},删除记录数: {result}");
return result;
}
/// <summary>
/// 根据ID删除实验板
/// </summary>
/// <param name="id">实验板的唯一标识符</param>
/// <returns>删除的记录数</returns>
public int DeleteBoardByID(Guid id)
{
// 先获取要删除的板子信息
var board = _db.BoardTable.Where(b => b.ID == id).FirstOrDefault();
if (board == null)
{
logger.Warn($"未找到ID为 {id} 的实验板");
return 0;
}
// 如果板子被占用,先解除绑定
if (board.OccupiedUserID != Guid.Empty)
{
_db.UserTable
.Where(u => u.ID == board.OccupiedUserID)
.Set(u => u.BoardID, Guid.Empty)
.Set(u => u.BoardExpireTime, (DateTime?)null)
.Update();
logger.Info($"已解除用户 {board.OccupiedUserID} 与实验板 {id} 的绑定");
}
var result = _db.BoardTable.Where(b => b.ID == id).Delete();
logger.Info($"实验板已删除: {id},删除记录数: {result}");
return result;
}
/// <summary>
/// 根据实验板ID获取实验板信息
/// </summary>
/// <param name="id">实验板的唯一标识符</param>
/// <returns>包含实验板信息的结果,如果未找到则返回空</returns>
public Result<Optional<Board>> GetBoardByID(Guid id)
{
var boards = _db.BoardTable.Where(board => board.ID == id).ToArray();
if (boards.Length > 1)
{
logger.Error($"数据库中存在多个相同ID的实验板: {id}");
return new(new Exception($"数据库中存在多个相同ID的实验板: {id}"));
}
if (boards.Length == 0)
{
logger.Info($"未找到ID对应的实验板: {id}");
return new(Optional<Board>.None);
}
logger.Debug($"成功获取实验板信息: {id}");
return new(boards[0]);
}
/// <summary>
/// 根据用户名获取实验板信息
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>包含实验板信息的结果,如果未找到则返回空</returns>
public Result<Optional<Board>> GetBoardByUserName(string userName)
{
var boards = _db.BoardTable.Where(board => board.OccupiedUserName == userName).ToArray();
if (boards.Length > 1)
{
logger.Error($"数据库中存在多个相同用户名的实验板: {userName}");
return new(new Exception($"数据库中存在多个相同用户名的实验板: {userName}"));
}
if (boards.Length == 0)
{
logger.Info($"未找到用户名对应的实验板: {userName}");
return new(Optional<Board>.None);
}
logger.Debug($"成功获取实验板信息: {userName}");
return new(boards[0]);
}
/// <summary>
/// 获取所有实验板信息
/// </summary>
/// <returns>所有实验板的数组</returns>
public Board[] GetAllBoard()
{
var boards = _db.BoardTable.ToArray();
logger.Debug($"获取所有实验板,共 {boards.Length} 块");
return boards;
}
/// <summary>
/// 获取一块可用的实验板并将其状态设置为繁忙
/// </summary>
/// <param name="userId">要分配板子的用户ID</param>
/// <param name="expireTime">绑定过期时间</param>
/// <returns>可用的实验板,如果没有可用的板子则返回空</returns>
public Optional<Board> GetAvailableBoard(Guid userId, DateTime expireTime)
{
var boards = _db.BoardTable.Where(
(board) => board.Status == BoardStatus.Available
).ToArray();
if (boards.Length == 0)
{
logger.Warn("没有可用的实验板");
return new(null);
}
else
{
var board = boards[0];
var user = _db.UserTable.Where(u => u.ID == userId).FirstOrDefault();
if (user == null)
{
logger.Error($"未找到用户: {userId}");
return new(null);
}
// 更新板子状态和用户绑定信息
_db.BoardTable
.Where(target => target.ID == board.ID)
.Set(target => target.Status, BoardStatus.Busy)
.Set(target => target.OccupiedUserID, userId)
.Set(target => target.OccupiedUserName, user.Name)
.Update();
// 更新用户的板子绑定信息
_db.UserTable
.Where(u => u.ID == userId)
.Set(u => u.BoardID, board.ID)
.Set(u => u.BoardExpireTime, expireTime)
.Update();
board.Status = BoardStatus.Busy;
board.OccupiedUserID = userId;
board.OccupiedUserName = user.Name;
logger.Info($"实验板 {board.BoardName} ({board.ID}) 已分配给用户 {user.Name} ({userId}),过期时间: {expireTime}");
return new(board);
}
}
/// <summary>
/// [TODO:description]
/// </summary>
/// <param name="boardId">[TODO:parameter]</param>
/// <param name="newName">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public int UpdateBoardName(Guid boardId, string newName)
{
if (string.IsNullOrWhiteSpace(newName) || newName.Contains('\'') || newName.Contains(';'))
{
logger.Error("实验板名称非法,包含不允许的字符");
return 0;
}
var result = _db.BoardTable
.Where(b => b.ID == boardId)
.Set(b => b.BoardName, newName)
.Update();
logger.Info($"实验板名称已更新: {boardId} -> {newName}");
return result;
}
/// <summary>
/// [TODO:description]
/// </summary>
/// <param name="boardId">[TODO:parameter]</param>
/// <param name="newStatus">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public int UpdateBoardStatus(Guid boardId, BoardStatus newStatus)
{
var result = _db.BoardTable
.Where(b => b.ID == boardId)
.Set(b => b.Status, newStatus)
.Update();
logger.Info($"TODO");
return result;
}
}

View File

@@ -0,0 +1,257 @@
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Cors;
using TypedSignalR.Client;
using Tapper;
using DotNext;
using Peripherals.SevenDigitalTubesClient;
using System.Collections.Concurrent;
#pragma warning disable 1998
namespace server.Hubs;
[Hub]
public interface IDigitalTubesHub
{
Task<bool> StartScan();
Task<bool> StopScan();
Task<bool> SetFrequency(int frequency);
Task<DigitalTubeTaskStatus?> GetStatus();
}
[Receiver]
public interface IDigitalTubesReceiver
{
Task OnReceive(byte[] data);
}
[TranspilationSource]
public class DigitalTubeTaskStatus
{
public int Frequency { get; set; } = 100;
public bool IsRunning { get; set; } = false;
public DigitalTubeTaskStatus(ScanTaskInfo info)
{
Frequency = info.Frequency;
IsRunning = info.IsRunning;
}
}
public class ScanTaskInfo
{
public string BoardID { get; set; }
public string ClientID { get; set; }
public Task? ScanTask { get; set; }
public SevenDigitalTubesCtrl TubeClient { get; set; }
public CancellationTokenSource CTS { get; set; } = new();
public int Frequency { get; set; } = 100;
public bool IsRunning { get; set; } = false;
public ScanTaskInfo(
string boardID, string clientID, SevenDigitalTubesCtrl client)
{
BoardID = boardID;
ClientID = clientID;
TubeClient = client;
}
}
[Authorize]
[EnableCors("SignalR")]
public class DigitalTubesHub : Hub<IDigitalTubesReceiver>, IDigitalTubesHub
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly IHubContext<DigitalTubesHub, IDigitalTubesReceiver> _hubContext;
private readonly Database.UserManager _userManager = new();
private ConcurrentDictionary<(string, string), ScanTaskInfo> _scanTasks = new();
public DigitalTubesHub(IHubContext<DigitalTubesHub, IDigitalTubesReceiver> hubContext)
{
_hubContext = hubContext;
}
private Optional<Database.Board> TryGetBoard()
{
var userName = Context.User?.FindFirstValue(ClaimTypes.Name);
if (string.IsNullOrEmpty(userName))
{
logger.Error("User name is null or empty");
return null;
}
var userRet = _userManager.GetUserByName(userName);
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
{
logger.Error($"User '{userName}' not found");
return null;
}
var user = userRet.Value.Value;
var boardRet = _userManager.GetBoardByID(user.BoardID);
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
{
logger.Error($"Board not found");
return null;
}
return boardRet.Value.Value;
}
private Task ScanAllTubes(ScanTaskInfo scanInfo)
{
var token = scanInfo.CTS.Token;
return Task.Run(async () =>
{
var cntError = 0;
while (!token.IsCancellationRequested)
{
var beginTime = DateTime.Now;
var waitTime = TimeSpan.FromMilliseconds(1000 / scanInfo.Frequency);
var dataRet = await scanInfo.TubeClient.ScanAllTubes();
if (!dataRet.IsSuccessful)
{
logger.Error($"Failed to scan tubes: {dataRet.Error}");
cntError++;
if (cntError > 3)
{
logger.Error($"Too many errors, stopping scan");
break;
}
}
await _hubContext.Clients.Client(scanInfo.ClientID).OnReceive(dataRet.Value);
var processTime = DateTime.Now - beginTime;
if (processTime < waitTime)
{
await Task.Delay(waitTime - processTime, token);
}
}
scanInfo.IsRunning = false;
}, token)
.ContinueWith((task) =>
{
if (task.IsFaulted)
{
logger.Error(
$"Digital tubes scan operation failesj for board {task.Exception}");
}
else if (task.IsCanceled)
{
logger.Info(
$"Digital tubes scan operation cancelled for board {scanInfo.BoardID}");
}
else
{
logger.Info(
$"Digital tubes scan completed successfully for board {scanInfo.BoardID}");
}
});
}
public async Task<bool> StartScan()
{
try
{
var board = TryGetBoard().OrThrow(() => new Exception("Board not found"));
var key = (board.ID.ToString(), Context.ConnectionId);
if (_scanTasks.TryGetValue(key, out var existing) && existing.IsRunning)
return true;
var cts = new CancellationTokenSource();
var scanTaskInfo = new ScanTaskInfo(
board.ID.ToString(), Context.ConnectionId,
new SevenDigitalTubesCtrl(board.IpAddr, board.Port, 0)
);
scanTaskInfo.ScanTask = ScanAllTubes(scanTaskInfo);
_scanTasks[key] = scanTaskInfo;
return true;
}
catch (Exception ex)
{
logger.Error(ex, "Failed to start scan");
return false;
}
}
public async Task<bool> StopScan()
{
try
{
var board = TryGetBoard().OrThrow(() => new Exception("Board not found"));
var key = (board.ID.ToString(), Context.ConnectionId);
if (_scanTasks.TryRemove(key, out var scanInfo))
{
scanInfo.IsRunning = false;
scanInfo.CTS.Cancel();
if (scanInfo.ScanTask != null)
await scanInfo.ScanTask;
scanInfo.CTS.Dispose();
}
return true;
}
catch (Exception ex)
{
logger.Error(ex, "Failed to stop scan");
return false;
}
}
public async Task<bool> SetFrequency(int frequency)
{
try
{
if (frequency < 1 || frequency > 1000)
return false;
var board = TryGetBoard().OrThrow(() => new Exception("Board not found"));
var key = (board.ID.ToString(), Context.ConnectionId);
if (_scanTasks.TryGetValue(key, out var scanInfo) && scanInfo.IsRunning)
{
scanInfo.Frequency = frequency;
return true;
}
else
{
logger.Warn($"SetFrequency called but no running scan for board {board.ID} and client {Context.ConnectionId}");
return false;
}
}
catch (Exception ex)
{
logger.Error(ex, "Failed to set frequency");
return false;
}
}
public async Task<DigitalTubeTaskStatus?> GetStatus()
{
try
{
var board = TryGetBoard().OrThrow(() => new Exception("Board not found"));
var key = (board.ID.ToString(), Context.ConnectionId);
if (_scanTasks.TryGetValue(key, out var scanInfo))
{
return new DigitalTubeTaskStatus(scanInfo);
}
else
{
return null;
}
}
catch (Exception ex)
{
logger.Error(ex, "Failed to get status");
throw new Exception("Failed to get status", ex);
}
}
}

View File

@@ -28,10 +28,12 @@ public interface IJtagReceiver
public class JtagHub : Hub<IJtagReceiver>, IJtagHub public class JtagHub : Hub<IJtagReceiver>, IJtagHub
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static ConcurrentDictionary<string, int> FreqTable = new();
private static ConcurrentDictionary<string, CancellationTokenSource> CancellationTokenSourceTable = new();
private readonly IHubContext<JtagHub, IJtagReceiver> _hubContext; private readonly IHubContext<JtagHub, IJtagReceiver> _hubContext;
private readonly Database.UserManager _userManager = new();
private static ConcurrentDictionary<string, int> FreqTable = new();
private static ConcurrentDictionary<string, CancellationTokenSource> CancellationTokenSourceTable = new();
public JtagHub(IHubContext<JtagHub, IJtagReceiver> hubContext) public JtagHub(IHubContext<JtagHub, IJtagReceiver> hubContext)
{ {
@@ -42,8 +44,7 @@ public class JtagHub : Hub<IJtagReceiver>, IJtagHub
{ {
try try
{ {
using var db = new Database.AppDataConnection(); var board = _userManager.GetBoardByUserName(userName);
var board = db.GetBoardByUserName(userName);
if (!board.IsSuccessful) if (!board.IsSuccessful)
{ {
logger.Error($"Find Board {board.Value.Value.ID} failed because {board.Error}"); logger.Error($"Find Board {board.Value.Value.ID} failed because {board.Error}");
@@ -67,23 +68,26 @@ public class JtagHub : Hub<IJtagReceiver>, IJtagHub
public async Task<bool> SetBoundaryScanFreq(int freq) public async Task<bool> SetBoundaryScanFreq(int freq)
{ {
try return await Task.Run(() =>
{ {
var userName = Context.User?.FindFirstValue(ClaimTypes.Name); try
if (userName is null)
{ {
logger.Error("Can't get user info"); var userName = Context.User?.FindFirstValue(ClaimTypes.Name);
if (userName is null)
{
logger.Error("Can't get user info");
return false;
}
FreqTable.AddOrUpdate(userName, freq, (key, value) => freq);
return true;
}
catch (Exception error)
{
logger.Error(error);
return false; return false;
} }
});
FreqTable.AddOrUpdate(userName, freq, (key, value) => freq);
return true;
}
catch (Exception error)
{
logger.Error(error);
return false;
}
} }
public async Task<bool> StartBoundaryScan(int freq = 100) public async Task<bool> StartBoundaryScan(int freq = 100)
@@ -143,23 +147,27 @@ public class JtagHub : Hub<IJtagReceiver>, IJtagHub
public async Task<bool> StopBoundaryScan() public async Task<bool> StopBoundaryScan()
{ {
var userName = Context.User?.FindFirstValue(ClaimTypes.Name); return await Task.Run(() =>
if (userName is null)
{ {
logger.Error("No Such User"); var userName = Context.User?.FindFirstValue(ClaimTypes.Name);
return false; if (userName is null)
} {
logger.Error("No Such User");
return false;
}
if (!CancellationTokenSourceTable.TryGetValue(userName, out var cts)) if (!CancellationTokenSourceTable.TryGetValue(userName, out var cts))
{ {
return false; return false;
} }
cts.Cancel(); cts.Cancel();
cts.Token.WaitHandle.WaitOne(); cts.Token.WaitHandle.WaitOne();
logger.Info($"Boundary scan stopped for user {userName}");
return true;
});
logger.Info($"Boundary scan stopped for user {userName}");
return true;
} }
private async Task BoundaryScanLogicPorts(string connectionID, string userName, CancellationToken cancellationToken) private async Task BoundaryScanLogicPorts(string connectionID, string userName, CancellationToken cancellationToken)

View File

@@ -1,17 +1,20 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using TypedSignalR.Client; using TypedSignalR.Client;
using Tapper; using Tapper;
using server.Services; using server.Services;
#pragma warning disable 1998
namespace server.Hubs; namespace server.Hubs;
[Hub] [Hub]
public interface IProgressHub public interface IProgressHub
{ {
Task<bool> Join(string taskId); Task<bool> Join(string taskId);
Task<bool> Leave(string taskId);
Task<ProgressInfo?> GetProgress(string taskId);
} }
[Receiver] [Receiver]
@@ -23,8 +26,7 @@ public interface IProgressReceiver
[TranspilationSource] [TranspilationSource]
public enum ProgressStatus public enum ProgressStatus
{ {
Pending, Running,
InProgress,
Completed, Completed,
Canceled, Canceled,
Failed Failed
@@ -33,10 +35,10 @@ public enum ProgressStatus
[TranspilationSource] [TranspilationSource]
public class ProgressInfo public class ProgressInfo
{ {
public string TaskId { get; } public required string TaskId { get; set; }
public ProgressStatus Status { get; } public required ProgressStatus Status { get; set; }
public int ProgressPercent { get; } public required double ProgressPercent { get; set; }
public string ErrorMessage { get; } public required string ErrorMessage { get; set; }
}; };
[Authorize] [Authorize]
@@ -44,18 +46,32 @@ public class ProgressInfo
public class ProgressHub : Hub<IProgressReceiver>, IProgressHub public class ProgressHub : Hub<IProgressReceiver>, IProgressHub
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly ProgressTracker _progressTracker = MsgBus.ProgressTracker;
private readonly IHubContext<ProgressHub, IProgressReceiver> _hubContext;
private readonly ProgressTrackerService _tracker;
public ProgressHub(IHubContext<ProgressHub, IProgressReceiver> hubContext, ProgressTrackerService tracker)
{
_hubContext = hubContext;
_tracker = tracker;
}
public async Task<bool> Join(string taskId) public async Task<bool> Join(string taskId)
{ {
return _tracker.BindTask(taskId, Context.ConnectionId); await Groups.AddToGroupAsync(Context.ConnectionId, taskId);
// 发送当前状态(如果存在)
var task = _progressTracker.GetTask(taskId);
if (task != null)
{
await Clients.Caller.OnReceiveProgress(task.Value.ToProgressInfo());
}
logger.Info($"Client {Context.ConnectionId} joined task {taskId}");
return true;
}
public async Task<bool> Leave(string taskId)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, taskId);
logger.Info($"Client {Context.ConnectionId} left task {taskId}");
return true;
}
public async Task<ProgressInfo?> GetProgress(string taskId)
{
return _progressTracker.GetTask(taskId)?.ToProgressInfo();
} }
} }

View File

@@ -1,7 +1,8 @@
using server.Services;
/// <summary> /// <summary>
/// 多线程通信总线 /// 多线程通信总线
/// </summary> /// </summary>
public static class MsgBus public sealed class MsgBus
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
@@ -11,17 +12,44 @@ public static class MsgBus
/// </summary> /// </summary>
public static UDPServer UDPServer { get { return udpServer; } } public static UDPServer UDPServer { get { return udpServer; } }
// 添加静态ProgressTracker引用
private static ProgressTracker? _progressTracker;
/// <summary>
/// 设置全局ProgressTracker实例
/// </summary>
public static void SetProgressTracker(ProgressTracker progressTracker)
{
_progressTracker = progressTracker;
}
public static ProgressTracker ProgressTracker
{
get
{
if (_progressTracker == null)
{
throw new InvalidOperationException("ProgressTracker is not set.");
}
return _progressTracker;
}
}
private static bool isRunning = false; private static bool isRunning = false;
/// <summary> /// <summary>
/// 获取通信总线运行状态 /// 获取通信总线运行状态
/// </summary> /// </summary>
public static bool IsRunning { get { return isRunning; } } public static bool IsRunning { get { return isRunning; } }
private MsgBus() { }
static MsgBus() { }
/// <summary> /// <summary>
/// 通信总线初始化 /// 通信总线初始化
/// </summary> /// </summary>
/// <returns>无</returns> /// <returns>无</returns>
public async static void Init() public static void Init()
{ {
if (!ArpClient.IsAdministrator()) if (!ArpClient.IsAdministrator())
{ {

View File

@@ -1,6 +1,5 @@
using System.Net; using System.Net;
using DotNext; using DotNext;
using Peripherals.PowerClient;
using WebProtocol; using WebProtocol;
namespace Peripherals.CameraClient; namespace Peripherals.CameraClient;
@@ -16,7 +15,7 @@ static class CameraAddr
public const UInt32 CAMERA_POWER = BASE + 0x10; //[0]: rstn, 0 is reset. [8]: power down, 1 is down. public const UInt32 CAMERA_POWER = BASE + 0x10; //[0]: rstn, 0 is reset. [8]: power down, 1 is down.
} }
class Camera public class Camera
{ {
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
@@ -276,7 +275,7 @@ class Camera
{ {
var currentAddress = (UInt16)(baseAddress + i - 1); var currentAddress = (UInt16)(baseAddress + i - 1);
var data = (byte)cmd[i]; var data = (byte)cmd[i];
logger.Debug($"ConfigureRegisters: 写入地址=0x{currentAddress:X4}, 数据=0x{data:X2}"); logger.Debug($"ConfigureRegisters: 写入地址=0x{currentAddress:X4}, 数据=0x{data:X2}");
// 准备I2C数据16位地址 + 8位数据 // 准备I2C数据16位地址 + 8位数据
@@ -322,14 +321,14 @@ class Camera
public async ValueTask<Result<byte>> ReadRegister(UInt16 register) public async ValueTask<Result<byte>> ReadRegister(UInt16 register)
{ {
var i2c = new Peripherals.I2cClient.I2c(this.address, this.port, this.taskID, this.timeout); var i2c = new Peripherals.I2cClient.I2c(this.address, this.port, this.taskID, this.timeout);
// Convert 16-bit register address to byte array // Convert 16-bit register address to byte array
var registerBytes = new byte[] { (byte)(register >> 8), (byte)(register & 0xFF) }; var registerBytes = new byte[] { (byte)(register >> 8), (byte)(register & 0xFF) };
var ret = await i2c.ReadData(CAM_I2C_ADDR, registerBytes, 1, CAM_PROTO); var ret = await i2c.ReadData(CAM_I2C_ADDR, registerBytes, 1, CAM_PROTO);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
return new(ret.Error); return new(ret.Error);
return new Result<byte>(ret.Value[0]); return new Result<byte>(ret.Value[0]);
} }
@@ -412,25 +411,25 @@ class Camera
[0x3801, unchecked((byte)(hStart & 0xFF))], [0x3801, unchecked((byte)(hStart & 0xFF))],
[0x3802, unchecked((byte)((vStart >> 8) & 0xFF))], [0x3802, unchecked((byte)((vStart >> 8) & 0xFF))],
[0x3803, unchecked((byte)(vStart & 0xFF))], [0x3803, unchecked((byte)(vStart & 0xFF))],
// H_END/V_END // H_END/V_END
[0x3804, unchecked((byte)((hEnd >> 8) & 0xFF))], [0x3804, unchecked((byte)((hEnd >> 8) & 0xFF))],
[0x3805, unchecked((byte)(hEnd & 0xFF))], [0x3805, unchecked((byte)(hEnd & 0xFF))],
[0x3806, unchecked((byte)((vEnd >> 8) & 0xFF))], [0x3806, unchecked((byte)((vEnd >> 8) & 0xFF))],
[0x3807, unchecked((byte)(vEnd & 0xFF))], [0x3807, unchecked((byte)(vEnd & 0xFF))],
// 输出像素个数 // 输出像素个数
[0x3808, unchecked((byte)((dvpHo >> 8) & 0xFF))], [0x3808, unchecked((byte)((dvpHo >> 8) & 0xFF))],
[0x3809, unchecked((byte)(dvpHo & 0xFF))], [0x3809, unchecked((byte)(dvpHo & 0xFF))],
[0x380A, unchecked((byte)((dvpVo >> 8) & 0xFF))], [0x380A, unchecked((byte)((dvpVo >> 8) & 0xFF))],
[0x380B, unchecked((byte)(dvpVo & 0xFF))], [0x380B, unchecked((byte)(dvpVo & 0xFF))],
// 总像素 // 总像素
[0x380C, unchecked((byte)((hts >> 8) & 0xFF))], [0x380C, unchecked((byte)((hts >> 8) & 0xFF))],
[0x380D, unchecked((byte)(hts & 0xFF))], [0x380D, unchecked((byte)(hts & 0xFF))],
[0x380E, unchecked((byte)((vts >> 8) & 0xFF))], [0x380E, unchecked((byte)((vts >> 8) & 0xFF))],
[0x380F, unchecked((byte)(vts & 0xFF))], [0x380F, unchecked((byte)(vts & 0xFF))],
// H_OFFSET/V_OFFSET // H_OFFSET/V_OFFSET
[0x3810, unchecked((byte)((hOffset >> 8) & 0xFF))], [0x3810, unchecked((byte)((hOffset >> 8) & 0xFF))],
[0x3811, unchecked((byte)(hOffset & 0xFF))], [0x3811, unchecked((byte)(hOffset & 0xFF))],
@@ -521,7 +520,7 @@ class Camera
hOffset: 16, vOffset: 4, hOffset: 16, vOffset: 4,
hWindow: 2624, vWindow: 1456 hWindow: 2624, vWindow: 1456
); );
} }
/// <summary> /// <summary>
@@ -537,7 +536,7 @@ class Camera
hOffset: 16, vOffset: 4, hOffset: 16, vOffset: 4,
hWindow: 2624, vWindow: 1456 hWindow: 2624, vWindow: 1456
); );
} }
/// <summary> /// <summary>
@@ -637,7 +636,7 @@ class Camera
[0x3008, 0x42] // 休眠命令 [0x3008, 0x42] // 休眠命令
}; };
return await ConfigureRegisters(sleepRegisters, customDelayMs: 50); return await ConfigureRegisters(sleepRegisters, customDelayMs: 50);
} }
/// <summary> /// <summary>
@@ -1305,7 +1304,7 @@ class Camera
UInt16 firmwareAddr = 0x8000; UInt16 firmwareAddr = 0x8000;
var firmwareCommand = new UInt16[1 + OV5640_AF_FIRMWARE.Length]; var firmwareCommand = new UInt16[1 + OV5640_AF_FIRMWARE.Length];
firmwareCommand[0] = firmwareAddr; firmwareCommand[0] = firmwareAddr;
// 将固件数据复制到命令数组中 // 将固件数据复制到命令数组中
for (int i = 0; i < OV5640_AF_FIRMWARE.Length; i++) for (int i = 0; i < OV5640_AF_FIRMWARE.Length; i++)
{ {
@@ -1425,7 +1424,7 @@ class Camera
logger.Error($"自动对焦超时,状态: 0x{readResult.Value:X2}"); logger.Error($"自动对焦超时,状态: 0x{readResult.Value:X2}");
return new(new Exception($"自动对焦超时,状态: 0x{readResult.Value:X2}")); return new(new Exception($"自动对焦超时,状态: 0x{readResult.Value:X2}"));
} }
await Task.Delay(100); await Task.Delay(100);
} }

View File

@@ -55,28 +55,28 @@ class DebuggerCmd
public const UInt32 ClearSignal = 0xFFFF_FFFF; public const UInt32 ClearSignal = 0xFFFF_FFFF;
} }
/// <summary> /// <summary>
/// 信号捕获模式枚举 /// 信号捕获模式枚举
/// </summary> /// </summary>
public enum CaptureMode : byte public enum CaptureMode : byte
{ {
/// <summary> /// <summary>
/// 无捕获模式 /// 无捕获模式
/// </summary> /// </summary>
None = 0, None = 0,
/// <summary> /// <summary>
/// 低电平触发模式 /// 低电平触发模式
/// </summary> /// </summary>
Logic0 = 1, Logic0 = 1,
/// <summary> /// <summary>
/// 高电平触发模式 /// 高电平触发模式
/// </summary> /// </summary>
Logic1 = 2, Logic1 = 2,
/// <summary> /// <summary>
/// 上升沿触发模式 /// 上升沿触发模式
/// </summary> /// </summary>
Rise = 3, Rise = 3,
/// <summary> /// <summary>
/// 下降沿触发模式 /// 下降沿触发模式
/// </summary> /// </summary>
Fall = 4, Fall = 4,
@@ -170,7 +170,7 @@ public class DebuggerClient
/// <returns>操作结果,成功返回状态标志字节,失败返回错误信息</returns> /// <returns>操作结果,成功返回状态标志字节,失败返回错误信息</returns>
public async ValueTask<Result<byte>> ReadFlag() public async ValueTask<Result<byte>> ReadFlag()
{ {
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, DebuggerAddr.Signal, this.timeout); var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, DebuggerAddr.Signal, this.timeout);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
{ {
logger.Error($"Failed to read flag: {ret.Error}"); logger.Error($"Failed to read flag: {ret.Error}");

View File

@@ -1,6 +1,5 @@
using System.Net; using System.Net;
using DotNext; using DotNext;
using Peripherals.PowerClient;
using WebProtocol; using WebProtocol;
namespace Peripherals.HdmiInClient; namespace Peripherals.HdmiInClient;
@@ -12,7 +11,7 @@ static class HdmiInAddr
public const UInt32 HdmiIn_READFIFO = BASE + 0x1; public const UInt32 HdmiIn_READFIFO = BASE + 0x1;
} }
class HdmiIn public class HdmiIn
{ {
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
@@ -100,6 +99,51 @@ class HdmiIn
return result.Value; return result.Value;
} }
public async ValueTask<(byte[] header, byte[] data, byte[] footer)?> GetMJpegFrame()
{
// 从HDMI读取RGB24数据
var readStartTime = DateTime.UtcNow;
var frameResult = await ReadFrame();
var readEndTime = DateTime.UtcNow;
var readTime = (readEndTime - readStartTime).TotalMilliseconds;
if (!frameResult.IsSuccessful || frameResult.Value == null)
{
logger.Warn("HDMI帧读取失败或为空");
return null;
}
var rgb24Data = frameResult.Value;
// 验证数据长度是否正确 (RGB24为每像素2字节)
var expectedLength = _currentWidth * _currentHeight * 2;
if (rgb24Data.Length != expectedLength)
{
logger.Warn("HDMI数据长度不匹配期望: {Expected}, 实际: {Actual}",
expectedLength, rgb24Data.Length);
}
// 将RGB24转换为JPEG参考Camera版本的处理
var jpegStartTime = DateTime.UtcNow;
var jpegResult = Common.Image.ConvertRGB24ToJpeg(rgb24Data, _currentWidth, _currentHeight, 80);
var jpegEndTime = DateTime.UtcNow;
var jpegTime = (jpegEndTime - jpegStartTime).TotalMilliseconds;
if (!jpegResult.IsSuccessful)
{
logger.Error("HDMI RGB24转JPEG失败: {Error}", jpegResult.Error);
return null;
}
var jpegData = jpegResult.Value;
// 发送MJPEG帧使用Camera版本的格式
var mjpegFrameHeader = Common.Image.CreateMjpegFrameHeader(jpegData.Length);
var mjpegFrameFooter = Common.Image.CreateMjpegFrameFooter();
return (mjpegFrameHeader, jpegData, mjpegFrameFooter);
}
/// <summary> /// <summary>
/// 获取当前分辨率 /// 获取当前分辨率
/// </summary> /// </summary>

View File

@@ -8,8 +8,8 @@ static class I2cAddr
const UInt32 Base = 0x6000_0000; const UInt32 Base = 0x6000_0000;
/// <summary> /// <summary>
/// 0x0000_0000: /// 0x0000_0000:
/// [7:0] 本次传输的i2c地址(最高位总为0); /// [7:0] 本次传输的i2c地址(最高位总为0);
/// [8] 1为读0为写; /// [8] 1为读0为写;
/// [16] 1为SCCB协议0为I2C协议; /// [16] 1为SCCB协议0为I2C协议;
@@ -17,45 +17,45 @@ static class I2cAddr
/// </summary> /// </summary>
public const UInt32 BaseConfig = Base + 0x0000_0000; public const UInt32 BaseConfig = Base + 0x0000_0000;
/// <summary> /// <summary>
/// 0x0000_0001: /// 0x0000_0001:
/// [15:0] 本次传输的数据量以字节为单位0为传1个字节; /// [15:0] 本次传输的数据量以字节为单位0为传1个字节;
/// [31:16] 若本次传输为读的DUMMY数据量字节为单位0为传1个字节 /// [31:16] 若本次传输为读的DUMMY数据量字节为单位0为传1个字节
/// </summary> /// </summary>
public const UInt32 TranConfig = Base + 0x0000_0001; public const UInt32 TranConfig = Base + 0x0000_0001;
/// <summary> /// <summary>
/// 0x0000_0002: [0] cmd_done; [8] cmd_error; /// 0x0000_0002: [0] cmd_done; [8] cmd_error;
/// </summary> /// </summary>
public const UInt32 Flag = Base + 0x0000_0002; public const UInt32 Flag = Base + 0x0000_0002;
/// <summary> /// <summary>
/// 0x0000_0003: FIFO写入口仅低8位有效只写 /// 0x0000_0003: FIFO写入口仅低8位有效只写
/// </summary> /// </summary>
public const UInt32 Write = Base + 0x0000_0003; public const UInt32 Write = Base + 0x0000_0003;
/// <summary> /// <summary>
/// 0x0000_0004: FIFO读出口仅低8位有效只读 /// 0x0000_0004: FIFO读出口仅低8位有效只读
/// </summary> /// </summary>
public const UInt32 Read = Base + 0x0000_0004; public const UInt32 Read = Base + 0x0000_0004;
/// <summary> /// <summary>
/// 0x0000_0005: [0] FIFO写入口清空[8] FIFO读出口清空 /// 0x0000_0005: [0] FIFO写入口清空[8] FIFO读出口清空
/// </summary> /// </summary>
public const UInt32 Clear = Base + 0x0000_0005; public const UInt32 Clear = Base + 0x0000_0005;
} }
/// <summary> /// <summary>
/// [TODO:Enum] /// [TODO:Enum]
/// </summary> /// </summary>
public enum I2cProtocol public enum I2cProtocol
{ {
/// <summary> /// <summary>
/// [TODO:Enum] /// [TODO:Enum]
/// </summary> /// </summary>
I2c = 0, I2c = 0,
/// <summary> /// <summary>
/// [TODO:Enum] /// [TODO:Enum]
/// </summary> /// </summary>
SCCB = 1 SCCB = 1
@@ -296,7 +296,7 @@ public class I2c
// 读取数据 // 读取数据
{ {
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, I2cAddr.Read); var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, I2cAddr.Read);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
{ {
logger.Error($"Failed to read data from I2C FIFO: {ret.Error}"); logger.Error($"Failed to read data from I2C FIFO: {ret.Error}");

View File

@@ -0,0 +1,513 @@
using System.Net;
using DotNext;
using Common;
namespace Peripherals.JpegClient;
static class JpegAddr
{
const UInt32 BASE = 0x0000_0000;
public const UInt32 CAPTURE_RD_CTRL = BASE + 0x0;
public const UInt32 CAPTURE_WR_CTRL = BASE + 0x1;
public const UInt32 START_WR_ADDR0 = BASE + 0x2;
public const UInt32 END_WR_ADDR0 = BASE + 0x3;
public const UInt32 START_WR_ADDR1 = BASE + 0x4;
public const UInt32 END_WR_ADDR1 = BASE + 0x5;
public const UInt32 START_RD_ADDR0 = BASE + 0x6;
public const UInt32 END_RD_ADDR0 = BASE + 0x7;
public const UInt32 HDMI_NOT_READY = BASE + 0x8;
public const UInt32 HDMI_HEIGHT_WIDTH = BASE + 0x9;
public const UInt32 JPEG_HEIGHT_WIDTH = BASE + 0xA;
public const UInt32 JPEG_ADD_NEED_FRAME_NUM = BASE + 0xB;
public const UInt32 JPEG_FRAME_SAVE_NUM = BASE + 0xC;
public const UInt32 JPEG_FIFO_FRAME_INFO = BASE + 0xD;
public const UInt32 ADDR_HDMI_WD_START = 0x4000_0000;
public const UInt32 ADDR_JPEG_START = 0x8000_0000;
public const UInt32 ADDR_JPEG_END = 0xA000_0000;
}
public class JpegInfo
{
public UInt32 Width { get; set; }
public UInt32 Height { get; set; }
public UInt32 Size { get; set; }
public JpegInfo(UInt32 width, UInt32 height, UInt32 size)
{
Width = width;
Height = height;
Size = size;
}
public JpegInfo(byte[] data)
{
if (data.Length < 8)
throw new ArgumentException("Invalid data length", nameof(data));
Width = ((UInt32)(data[5] << 8 + data[6] & 0xF0));
Height = ((UInt32)((data[6] & 0x0F) << 4 + data[7]));
Size = Number.BytesToUInt32(data, 0, 4).Value;
}
}
public enum JpegSampleRate : UInt32
{
RATE_1_1 = 0b1111_1111_1111_1111_1111_1111_1111_1111,
RATE_1_2 = 0b1010_1010_1010_1010_1010_1010_1010_1010,
RATE_1_4 = 0b1000_1000_1000_1000_1000_1000_1000_1000,
RATE_3_4 = 0b1110_1110_1110_1110_1110_1110_1110_1110,
RATE_1_8 = 0b1000_0000_1000_0000_1000_0000_1000_0000,
RATE_3_8 = 0b1001_0010_0100_1001_1001_0010_0100_1001,
RATE_7_8 = 0b1111_1110_1111_1110_1111_1110_1111_1110,
RATE_1_16 = 0b1000_0000_0000_0000_1000_0000_0000_0000,
RATE_3_16 = 0b1000_0100_0010_0000_1000_0100_0010_0000,
RATE_5_16 = 0b1001_0001_0010_0010_0100_0100_1000_1001,
RATE_15_16 = 0b1111_1111_1111_1110_1111_1111_1111_1110,
RATE_1_32 = 0b1000_0000_0000_0000_0000_0000_0000_0000,
RATE_31_32 = 0b1111_1111_1111_1111_1111_1111_1111_1110,
}
public class Jpeg
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
readonly int timeout = 2000;
readonly int taskID;
readonly int port;
readonly string address;
private IPEndPoint ep;
public int Width { get; set; }
public int Height { get; set; }
public Jpeg(string address, int port, int taskID, int timeout = 2000)
{
if (timeout < 0)
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
this.address = address;
this.taskID = taskID;
this.port = port;
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
this.timeout = timeout;
}
public async ValueTask<Result<bool>> Init(bool enable = true)
{
{
var ret = await CheckHdmiIsReady();
if (!ret.IsSuccessful)
{
logger.Error($"Failed to check HDMI ready: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error("HDMI not ready");
return new(false);
}
}
int width = -1, height = -1;
{
var ret = await GetHdmiResolution();
if (!ret.IsSuccessful)
{
logger.Error($"Failed to get HDMI resolution: {ret.Error}");
return new(ret.Error);
}
(width, height) = ret.Value;
}
{
var ret = await ConnectJpeg2Hdmi(width, height);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to connect JPEG to HDMI: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error("Failed to connect JPEG to HDMI");
return false;
}
}
if (enable)
return await SetEnable(true);
else return true;
}
public async ValueTask<bool> SetEnable(bool enable)
{
if (enable)
{
var ret = await UDPClientPool.WriteAddrSeq(
this.ep,
this.taskID,
[JpegAddr.CAPTURE_RD_CTRL, JpegAddr.CAPTURE_WR_CTRL],
[0b11, 0b01],
this.timeout
);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set JPEG enable: {ret.Error}");
return false;
}
return ret.Value;
}
else
{
var ret = await UDPClientPool.WriteAddrSeq(
this.ep,
this.taskID,
[JpegAddr.CAPTURE_RD_CTRL, JpegAddr.CAPTURE_WR_CTRL],
[0b00, 0b00],
this.timeout
);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set JPEG disable: {ret.Error}");
return false;
}
return ret.Value;
}
}
public async ValueTask<Result<bool>> CheckHdmiIsReady()
{
var ret = await UDPClientPool.ReadAddrWithWait(
this.ep, this.taskID, JpegAddr.HDMI_NOT_READY, 0b01, 0b01, 100, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to check HDMI status: {ret.Error}");
return new(ret.Error);
}
return ret.Value;
}
public async ValueTask<Result<(int, int)>> GetHdmiResolution()
{
var ret = await UDPClientPool.ReadAddr(
this.ep, this.taskID, JpegAddr.HDMI_HEIGHT_WIDTH, 0, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to get HDMI resolution: {ret.Error}");
return new(ret.Error);
}
var data = ret.Value.Options.Data;
if (data == null || data.Length != 4)
{
logger.Error($"Invalid HDMI resolution data length: {data?.Length ?? 0}");
return new(new Exception("Invalid HDMI resolution data length"));
}
var width = data[0] | (data[1] << 8);
var height = data[2] | (data[3] << 8);
this.Width = width;
this.Height = height;
return new((width, height));
}
public async ValueTask<Result<bool>> ConnectJpeg2Hdmi(int width, int height)
{
if (width <= 0 || height <= 0)
{
logger.Error($"Invalid HDMI resolution: {width}x{height}");
return new(new ArgumentException("Invalid HDMI resolution"));
}
var frameSize = (UInt32)(width * height / 4);
{
var ret = await UDPClientPool.WriteAddr(
this.ep, this.taskID, JpegAddr.START_WR_ADDR0, JpegAddr.ADDR_HDMI_WD_START, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set HDMI output start address: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"Failed to set HDMI output start address");
return false;
}
}
{
var ret = await UDPClientPool.WriteAddr(
this.ep, this.taskID, JpegAddr.END_WR_ADDR0,
JpegAddr.ADDR_HDMI_WD_START + frameSize, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set HDMI output end address: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"Failed to set HDMI output address");
return false;
}
}
{
var ret = await UDPClientPool.WriteAddr(
this.ep, this.taskID, JpegAddr.START_RD_ADDR0, JpegAddr.ADDR_HDMI_WD_START, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set jpeg input start address: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"Failed to set jpeg input address");
return false;
}
}
{
var ret = await UDPClientPool.WriteAddr(
this.ep, this.taskID, JpegAddr.END_RD_ADDR0,
JpegAddr.ADDR_HDMI_WD_START + frameSize, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set jpeg input end address: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"Failed to set jpeg input end address");
return false;
}
}
{
var ret = await UDPClientPool.WriteAddr(
this.ep, this.taskID, JpegAddr.START_WR_ADDR1, JpegAddr.ADDR_JPEG_START, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set jpeg output start address: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"Failed to set jpeg output start address");
return false;
}
}
{
var ret = await UDPClientPool.WriteAddr(
this.ep, this.taskID, JpegAddr.END_WR_ADDR1, JpegAddr.ADDR_JPEG_END, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set jpeg output end address: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"Failed to set jpeg output end address");
return false;
}
}
return true;
}
// public async ValueTask<bool> SetSampleRate(uint rate)
// {
// var ret = await UDPClientPool.WriteAddr(
// this.ep, this.taskID, JpegAddr.FRAME_SAMPLE_RATE, rate, this.timeout);
// if (!ret.IsSuccessful)
// {
// logger.Error($"Failed to set JPEG sample rate: {ret.Error}");
// return false;
// }
// return ret.Value;
// }
// public async ValueTask<bool> SetSampleRate(JpegSampleRate rate)
// {
// return await SetSampleRate((uint)rate);
// }
public async ValueTask<uint> GetFrameNumber()
{
var ret = await UDPClientPool.ReadAddrByte(
this.ep, this.taskID, JpegAddr.JPEG_FRAME_SAVE_NUM, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to get JPEG frame number: {ret.Error}");
return 0;
}
return Number.BytesToUInt32(ret.Value.Options.Data ?? Array.Empty<byte>()).Value;
}
public async ValueTask<Optional<List<JpegInfo>>> GetFrameInfo(int num)
{
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, JpegAddr.JPEG_FIFO_FRAME_INFO, num, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to get JPEG frame info: {ret.Error}");
return new(null);
}
var data = ret.Value.Options.Data;
if (data == null || data.Length == 0)
{
logger.Error($"Data is null or empty");
return new(null);
}
if (data.Length != num * 2)
{
logger.Error(
$"Data length should be {num * 2} bytes, instead of {data.Length} bytes");
return new(null);
}
var infos = new List<JpegInfo>();
for (int i = 0; i < num; i++)
{
infos.Add(new JpegInfo(data[i..(i + 1)]));
}
return new(infos);
}
public async ValueTask<bool> AddFrameNum2Process(uint cnt)
{
var ret = await UDPClientPool.WriteAddr(
this.ep, this.taskID, JpegAddr.JPEG_ADD_NEED_FRAME_NUM, cnt, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to update pointer: {ret.Error}");
return false;
}
return ret.Value;
}
public async ValueTask<Result<byte[]?>> GetFrame(uint offset, uint length)
{
if (!MsgBus.IsRunning)
{
logger.Error("Message bus is not running");
return new(new Exception("Message bus is not running"));
}
MsgBus.UDPServer.ClearUDPData(this.ep.Address.ToString(), this.ep.Port);
var firstReadLength = (int)(Math.Min(
length,
JpegAddr.ADDR_JPEG_END - JpegAddr.ADDR_JPEG_START - offset
));
var secondReadLength = (int)(length - firstReadLength);
var dataBytes = new byte[length];
{
var ret = await UDPClientPool.ReadAddr4Bytes(
this.ep, this.taskID, JpegAddr.ADDR_JPEG_START + offset, firstReadLength, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to get JPEG frame data: {ret.Error}");
return null;
}
if (ret.Value.Length != firstReadLength)
{
logger.Error($"Data length should be {firstReadLength} bytes, instead of {ret.Value.Length} bytes");
return null;
}
Buffer.BlockCopy(ret.Value, 0, dataBytes, 0, firstReadLength);
}
if (secondReadLength > 0)
{
var ret = await UDPClientPool.ReadAddr4Bytes(
this.ep, this.taskID, JpegAddr.ADDR_JPEG_START, secondReadLength, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to get JPEG frame data: {ret.Error}");
return null;
}
if (ret.Value.Length != secondReadLength)
{
logger.Error($"Data length should be {secondReadLength} bytes, instead of {ret.Value.Length} bytes");
return null;
}
Buffer.BlockCopy(ret.Value, 0, dataBytes, firstReadLength, secondReadLength);
}
return dataBytes;
}
public async ValueTask<List<byte[]>> GetMultiFrames(uint offset, uint[] sizes)
{
var frames = new List<byte[]>();
for (int i = 0; i < sizes.Length; i++)
{
var ret = await GetFrame(offset, sizes[i]);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to get JPEG frame {i} data: {ret.Error}");
continue;
}
if (ret.Value == null)
{
logger.Error($"Frame {i} data is null");
continue;
}
if (ret.Value.Length != sizes[i])
{
logger.Error(
$"Frame {i} data length should be {sizes[i]} bytes, instead of {ret.Value.Length} bytes");
continue;
}
frames.Add(ret.Value);
offset += sizes[i];
}
{
var ret = await AddFrameNum2Process((uint)sizes.Length);
if (!ret) logger.Error($"Failed to update pointer");
}
return frames;
}
public async ValueTask<Result<List<byte[]>?>> GetMultiFrames(uint offset)
{
if (!MsgBus.IsRunning)
{
logger.Error("Message bus is not running");
return new(new Exception("Message bus is not running"));
}
MsgBus.UDPServer.ClearUDPData(this.ep.Address.ToString(), this.ep.Port);
var frameNum = await GetFrameNumber();
if (frameNum == 0) return null;
List<uint>? frameSizes = null;
{
var ret = await GetFrameInfo((int)frameNum);
if (!ret.HasValue || ret.Value.Count == 0)
{
logger.Error($"Failed to get frame info");
return null;
}
frameSizes = ret.Value.Select(x => x.Size).ToList();
}
var frames = await GetMultiFrames(offset, frameSizes.ToArray());
if (frames.Count == 0)
{
logger.Error($"Failed to get frames");
return null;
}
return frames;
}
}

View File

@@ -380,6 +380,7 @@ public class JtagStatusReg
public class Jtag public class Jtag
{ {
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly ProgressTracker _progressTracker = MsgBus.ProgressTracker;
private const int CLOCK_FREQ = 50; // MHz private const int CLOCK_FREQ = 50; // MHz
@@ -392,6 +393,7 @@ public class Jtag
public readonly string address; public readonly string address;
private IPEndPoint ep; private IPEndPoint ep;
/// <summary> /// <summary>
/// Jtag 构造函数 /// Jtag 构造函数
/// </summary> /// </summary>
@@ -444,10 +446,10 @@ public class Jtag
async ValueTask<Result<bool>> WriteFIFO( async ValueTask<Result<bool>> WriteFIFO(
UInt32 devAddr, UInt32 data, UInt32 result, UInt32 devAddr, UInt32 data, UInt32 result,
UInt32 resultMask = 0xFF_FF_FF_FF, UInt32 delayMilliseconds = 0, ProgressReporter? progress = null) UInt32 resultMask = 0xFF_FF_FF_FF, UInt32 delayMilliseconds = 0, string progressId = "")
{ {
{ {
var ret = await UDPClientPool.WriteAddr(this.ep, 0, devAddr, data, this.timeout, progress?.CreateChild(80)); var ret = await UDPClientPool.WriteAddr(this.ep, 0, devAddr, data, this.timeout, progressId);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
if (!ret.Value) return new(new Exception("Write FIFO failed")); if (!ret.Value) return new(new Exception("Write FIFO failed"));
} }
@@ -458,17 +460,18 @@ public class Jtag
{ {
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, 0, JtagAddr.STATE, result, resultMask, 0, this.timeout); var ret = await UDPClientPool.ReadAddrWithWait(this.ep, 0, JtagAddr.STATE, result, resultMask, 0, this.timeout);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
progress?.Finish(); _progressTracker?.AdvanceProgress(progressId, 10);
return ret.Value; return ret.Value;
} }
} }
async ValueTask<Result<bool>> WriteFIFO( async ValueTask<Result<bool>> WriteFIFO(
UInt32 devAddr, byte[] data, UInt32 result, UInt32 devAddr, byte[] data, UInt32 result,
UInt32 resultMask = 0xFF_FF_FF_FF, UInt32 delayMilliseconds = 0, ProgressReporter? progress = null) UInt32 resultMask = 0xFF_FF_FF_FF, UInt32 delayMilliseconds = 0, string progressId = "")
{ {
{ {
var ret = await UDPClientPool.WriteAddr(this.ep, 0, devAddr, data, this.timeout, progress?.CreateChild(80)); var ret = await UDPClientPool.WriteAddr(
this.ep, 0, devAddr, data, this.timeout, progressId);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
if (!ret.Value) return new(new Exception("Write FIFO failed")); if (!ret.Value) return new(new Exception("Write FIFO failed"));
} }
@@ -479,7 +482,7 @@ public class Jtag
{ {
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, 0, JtagAddr.STATE, result, resultMask, 0, this.timeout); var ret = await UDPClientPool.ReadAddrWithWait(this.ep, 0, JtagAddr.STATE, result, resultMask, 0, this.timeout);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
progress?.Finish(); _progressTracker.AdvanceProgress(progressId, 10);
return ret.Value; return ret.Value;
} }
} }
@@ -564,7 +567,7 @@ public class Jtag
} }
async ValueTask<Result<bool>> LoadDRCareInput( async ValueTask<Result<bool>> LoadDRCareInput(
byte[] bytesArray, UInt32 timeout = 10_000, UInt32 cycle = 500, ProgressReporter? progress = null) byte[] bytesArray, UInt32 timeout = 10_000, UInt32 cycle = 500, string progressId = "")
{ {
var bytesLen = ((uint)(bytesArray.Length * 8)); var bytesLen = ((uint)(bytesArray.Length * 8));
if (bytesLen > Math.Pow(2, 28)) return new(new Exception("Length is over 2^(28 - 3)")); if (bytesLen > Math.Pow(2, 28)) return new(new Exception("Length is over 2^(28 - 3)"));
@@ -579,14 +582,15 @@ public class Jtag
else if (!ret.Value) return new(new Exception("Write CMD_JTAG_LOAD_DR_CAREI Failed")); else if (!ret.Value) return new(new Exception("Write CMD_JTAG_LOAD_DR_CAREI Failed"));
} }
progress?.Report(10); _progressTracker.AdvanceProgress(progressId, 10);
{ {
var ret = await WriteFIFO( var ret = await WriteFIFO(
JtagAddr.WRITE_DATA, JtagAddr.WRITE_DATA,
bytesArray, 0x01_00_00_00, bytesArray, 0x01_00_00_00,
JtagState.CMD_EXEC_FINISH, JtagState.CMD_EXEC_FINISH,
progress: progress?.CreateChild(90) 0,
progressId
); );
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
@@ -709,56 +713,55 @@ public class Jtag
/// 下载比特流到 JTAG 设备 /// 下载比特流到 JTAG 设备
/// </summary> /// </summary>
/// <param name="bitstream">比特流数据</param> /// <param name="bitstream">比特流数据</param>
/// <param name="progressId">进度ID</param>
/// <returns>指示下载是否成功的异步结果</returns> /// <returns>指示下载是否成功的异步结果</returns>
public async ValueTask<Result<bool>> DownloadBitstream(byte[] bitstream, ProgressReporter? progress = null) public async ValueTask<Result<bool>> DownloadBitstream(
byte[] bitstream, string progressId = "")
{ {
// Clear Data // Clear Data
MsgBus.UDPServer.ClearUDPData(this.address, 0); MsgBus.UDPServer.ClearUDPData(this.address, 0);
logger.Trace($"Clear up udp server {this.address,0} receive data"); logger.Trace($"Clear up udp server {this.address,0} receive data");
if (progress != null)
{ _progressTracker.AdvanceProgress(progressId, 10);
progress.ExpectedSteps = 25;
progress.Increase();
}
Result<bool> ret; Result<bool> ret;
ret = await CloseTest(); ret = await CloseTest();
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed")); else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
ret = await RunTest(); ret = await RunTest();
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed")); else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
logger.Trace("Jtag initialize"); logger.Trace("Jtag initialize");
ret = await ExecRDCmd(JtagCmd.JTAG_DR_JRST); ret = await ExecRDCmd(JtagCmd.JTAG_DR_JRST);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_JRST Failed")); else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_JRST Failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
ret = await RunTest(); ret = await RunTest();
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed")); else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
ret = await ExecRDCmd(JtagCmd.JTAG_DR_CFGI); ret = await ExecRDCmd(JtagCmd.JTAG_DR_CFGI);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_CFGI Failed")); else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_CFGI Failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
logger.Trace("Jtag ready to write bitstream"); logger.Trace("Jtag ready to write bitstream");
ret = await IdleDelay(100000); ret = await IdleDelay(100000);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed")); else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
ret = await LoadDRCareInput(bitstream, progress: progress?.CreateChild(50)); ret = await LoadDRCareInput(bitstream, progressId: progressId);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Load Data Failed")); else if (!ret.Value) return new(new Exception("Jtag Load Data Failed"));
@@ -767,40 +770,40 @@ public class Jtag
ret = await CloseTest(); ret = await CloseTest();
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed")); else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
ret = await RunTest(); ret = await RunTest();
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed")); else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
ret = await ExecRDCmd(JtagCmd.JTAG_DR_JWAKEUP); ret = await ExecRDCmd(JtagCmd.JTAG_DR_JWAKEUP);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_JWAKEUP Failed")); else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_JWAKEUP Failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
logger.Trace("Jtag reset device"); logger.Trace("Jtag reset device");
ret = await IdleDelay(10000); ret = await IdleDelay(10000);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed")); else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
var retCode = await ReadStatusReg(); var retCode = await ReadStatusReg();
if (!retCode.IsSuccessful) return new(retCode.Error); if (!retCode.IsSuccessful) return new(retCode.Error);
var jtagStatus = new JtagStatusReg(retCode.Value); var jtagStatus = new JtagStatusReg(retCode.Value);
if (!(jtagStatus.done && jtagStatus.wakeup_over && jtagStatus.init_complete)) if (!(jtagStatus.done && jtagStatus.wakeup_over && jtagStatus.init_complete))
return new(new Exception("Jtag download bitstream failed")); return new(new Exception("Jtag download bitstream failed"));
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
ret = await CloseTest(); ret = await CloseTest();
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed")); else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
logger.Trace("Jtag download bitstream successfully"); logger.Trace("Jtag download bitstream successfully");
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 10);
// Finish // Finish
progress?.Finish(); _progressTracker.AdvanceProgress(progressId, 10);
return true; return true;
} }

View File

@@ -12,7 +12,7 @@ static class AnalyzerAddr
const UInt32 DMA1_BASE = 0x7000_0000; const UInt32 DMA1_BASE = 0x7000_0000;
const UInt32 DDR_BASE = 0x0000_0000; const UInt32 DDR_BASE = 0x0000_0000;
/// <summary> /// <summary>
/// 0x0000_0000 R/W [ 0] capture on: 置1开始等待捕获0停止捕获。捕获到信号后该位自动清零。 <br/> /// 0x0000_0000 R/W [ 0] capture on: 置1开始等待捕获0停止捕获。捕获到信号后该位自动清零。 <br/>
/// [ 8] capture force: 置1则强制捕获信号自动置0。 <br/> /// [ 8] capture force: 置1则强制捕获信号自动置0。 <br/>
/// [16] capture busy: 1为逻辑分析仪正在捕获信号。 <br/> /// [16] capture busy: 1为逻辑分析仪正在捕获信号。 <br/>
@@ -21,7 +21,7 @@ static class AnalyzerAddr
/// </summary> /// </summary>
public const UInt32 CAPTURE_MODE = BASE + 0x0000_0000; public const UInt32 CAPTURE_MODE = BASE + 0x0000_0000;
/// <summary> /// <summary>
/// 0x0000_0001 R/W [1:0] global trig mode: 00: 全局与 (&amp;) <br/> /// 0x0000_0001 R/W [1:0] global trig mode: 00: 全局与 (&amp;) <br/>
/// 01: 全局或 (&#124;) <br/> /// 01: 全局或 (&#124;) <br/>
/// 10: 全局非与(~&amp;) <br/> /// 10: 全局非与(~&amp;) <br/>
@@ -29,7 +29,7 @@ static class AnalyzerAddr
/// </summary> /// </summary>
public const UInt32 GLOBAL_TRIG_MODE = BASE + 0x0000_0001; public const UInt32 GLOBAL_TRIG_MODE = BASE + 0x0000_0001;
/// <summary> /// <summary>
/// 0x0000_0010 - 0x0000_0017 R/W [5:0] 信号M的触发操作符共8路 <br/> /// 0x0000_0010 - 0x0000_0017 R/W [5:0] 信号M的触发操作符共8路 <br/>
/// [5:3] M's Operator: 000 == <br/> /// [5:3] M's Operator: 000 == <br/>
/// 001 != <br/> /// 001 != <br/>
@@ -73,7 +73,7 @@ static class AnalyzerAddr
public const UInt32 DMA1_CAPTURE_CTRL_ADDR = DMA1_BASE + 0x0000_0014; public const UInt32 DMA1_CAPTURE_CTRL_ADDR = DMA1_BASE + 0x0000_0014;
public const UInt32 STORE_OFFSET_ADDR = DDR_BASE + 0x0100_0000; public const UInt32 STORE_OFFSET_ADDR = DDR_BASE + 0x0100_0000;
/// <summary> /// <summary>
/// 0x0100_0000 - 0x0100_03FF 只读 32位波形存储得到的32位数据中低八位最先捕获高八位最后捕获。<br/> /// 0x0100_0000 - 0x0100_03FF 只读 32位波形存储得到的32位数据中低八位最先捕获高八位最后捕获。<br/>
/// 共1024个地址每个地址存储4组深度为4096。<br/> /// 共1024个地址每个地址存储4组深度为4096。<br/>
/// </summary> /// </summary>
@@ -87,53 +87,53 @@ static class AnalyzerAddr
[Flags] [Flags]
public enum CaptureStatus public enum CaptureStatus
{ {
/// <summary> /// <summary>
/// 无状态标志 /// 无状态标志
/// </summary> /// </summary>
None = 0, None = 0,
/// <summary> /// <summary>
/// 捕获使能位置1开始等待捕获0停止捕获。捕获到信号后该位自动清零 /// 捕获使能位置1开始等待捕获0停止捕获。捕获到信号后该位自动清零
/// </summary> /// </summary>
CaptureOn = 1 << 0, // [0] 捕获使能 CaptureOn = 1 << 0, // [0] 捕获使能
/// <summary> /// <summary>
/// 强制捕获位置1则强制捕获信号自动置0 /// 强制捕获位置1则强制捕获信号自动置0
/// </summary> /// </summary>
CaptureForce = 1 << 8, // [8] 强制捕获 CaptureForce = 1 << 8, // [8] 强制捕获
/// <summary> /// <summary>
/// 捕获忙碌位1为逻辑分析仪正在捕获信号 /// 捕获忙碌位1为逻辑分析仪正在捕获信号
/// </summary> /// </summary>
CaptureBusy = 1 << 16, // [16] 捕获进行中 CaptureBusy = 1 << 16, // [16] 捕获进行中
/// <summary> /// <summary>
/// 捕获完成位1为逻辑分析仪内存完整存储了此次捕获的信号 /// 捕获完成位1为逻辑分析仪内存完整存储了此次捕获的信号
/// </summary> /// </summary>
CaptureDone = 1 << 24 // [24] 捕获完成 CaptureDone = 1 << 24 // [24] 捕获完成
} }
/// <summary> /// <summary>
/// 全局触发模式枚举,定义多路信号触发条件的逻辑组合方式 /// 全局触发模式枚举,定义多路信号触发条件的逻辑组合方式
/// </summary> /// </summary>
public enum GlobalCaptureMode public enum GlobalCaptureMode
{ {
/// <summary> /// <summary>
/// 全局与模式,所有触发条件都必须满足 /// 全局与模式,所有触发条件都必须满足
/// </summary> /// </summary>
AND = 0b00, AND = 0b00,
/// <summary> /// <summary>
/// 全局或模式,任一触发条件满足即可 /// 全局或模式,任一触发条件满足即可
/// </summary> /// </summary>
OR = 0b01, OR = 0b01,
/// <summary> /// <summary>
/// 全局非与模式,不是所有触发条件都满足 /// 全局非与模式,不是所有触发条件都满足
/// </summary> /// </summary>
NAND = 0b10, NAND = 0b10,
/// <summary> /// <summary>
/// 全局非或模式,所有触发条件都不满足 /// 全局非或模式,所有触发条件都不满足
/// </summary> /// </summary>
NOR = 0b11 NOR = 0b11
@@ -144,32 +144,32 @@ public enum GlobalCaptureMode
/// </summary> /// </summary>
public enum AnalyzerClockDiv public enum AnalyzerClockDiv
{ {
/// <summary> /// <summary>
/// 1分频 /// 1分频
/// </summary> /// </summary>
DIV1 = 0x0000_0000, DIV1 = 0x0000_0000,
/// <summary> /// <summary>
/// 2分频 /// 2分频
/// </summary> /// </summary>
DIV2 = 0x0000_0001, DIV2 = 0x0000_0001,
/// <summary> /// <summary>
/// 4分频 /// 4分频
/// </summary> /// </summary>
DIV4 = 0x0000_0002, DIV4 = 0x0000_0002,
/// <summary> /// <summary>
/// 8分频 /// 8分频
/// </summary> /// </summary>
DIV8 = 0x0000_0003, DIV8 = 0x0000_0003,
/// <summary> /// <summary>
/// 16分频 /// 16分频
/// </summary> /// </summary>
DIV16 = 0x0000_0004, DIV16 = 0x0000_0004,
/// <summary> /// <summary>
/// 32分频 /// 32分频
/// </summary> /// </summary>
DIV32 = 0x0000_0005, DIV32 = 0x0000_0005,
@@ -190,27 +190,27 @@ public enum AnalyzerClockDiv
/// </summary> /// </summary>
public enum SignalOperator : byte public enum SignalOperator : byte
{ {
/// <summary> /// <summary>
/// 等于操作符 /// 等于操作符
/// </summary> /// </summary>
Equal = 0b000, // == Equal = 0b000, // ==
/// <summary> /// <summary>
/// 不等于操作符 /// 不等于操作符
/// </summary> /// </summary>
NotEqual = 0b001, // != NotEqual = 0b001, // !=
/// <summary> /// <summary>
/// 小于操作符 /// 小于操作符
/// </summary> /// </summary>
LessThan = 0b010, // < LessThan = 0b010, // <
/// <summary> /// <summary>
/// 小于等于操作符 /// 小于等于操作符
/// </summary> /// </summary>
LessThanOrEqual = 0b011, // <= LessThanOrEqual = 0b011, // <=
/// <summary> /// <summary>
/// 大于操作符 /// 大于操作符
/// </summary> /// </summary>
GreaterThan = 0b100, // > GreaterThan = 0b100, // >
/// <summary> /// <summary>
/// 大于等于操作符 /// 大于等于操作符
/// </summary> /// </summary>
GreaterThanOrEqual = 0b101 // >= GreaterThanOrEqual = 0b101 // >=
@@ -221,35 +221,35 @@ public enum SignalOperator : byte
/// </summary> /// </summary>
public enum SignalValue : byte public enum SignalValue : byte
{ {
/// <summary> /// <summary>
/// 逻辑0电平 /// 逻辑0电平
/// </summary> /// </summary>
Logic0 = 0b000, // LOGIC 0 Logic0 = 0b000, // LOGIC 0
/// <summary> /// <summary>
/// 逻辑1电平 /// 逻辑1电平
/// </summary> /// </summary>
Logic1 = 0b001, // LOGIC 1 Logic1 = 0b001, // LOGIC 1
/// <summary> /// <summary>
/// 不关心该信号状态 /// 不关心该信号状态
/// </summary> /// </summary>
NotCare = 0b010, // X(not care) NotCare = 0b010, // X(not care)
/// <summary> /// <summary>
/// 上升沿触发 /// 上升沿触发
/// </summary> /// </summary>
Rise = 0b011, // RISE Rise = 0b011, // RISE
/// <summary> /// <summary>
/// 下降沿触发 /// 下降沿触发
/// </summary> /// </summary>
Fall = 0b100, // FALL Fall = 0b100, // FALL
/// <summary> /// <summary>
/// 上升沿或下降沿触发 /// 上升沿或下降沿触发
/// </summary> /// </summary>
RiseOrFall = 0b101, // RISE OR FALL RiseOrFall = 0b101, // RISE OR FALL
/// <summary> /// <summary>
/// 信号无变化 /// 信号无变化
/// </summary> /// </summary>
NoChange = 0b110, // NOCHANGE NoChange = 0b110, // NOCHANGE
/// <summary> /// <summary>
/// 特定数值 /// 特定数值
/// </summary> /// </summary>
SomeNumber = 0b111 // SOME NUMBER SomeNumber = 0b111 // SOME NUMBER
@@ -260,11 +260,11 @@ public enum SignalValue : byte
/// </summary> /// </summary>
public enum AnalyzerChannelDiv public enum AnalyzerChannelDiv
{ {
/// <summary> /// <summary>
/// 1路 /// 1路
/// </summary> /// </summary>
ONE = 0x0000_0000, ONE = 0x0000_0000,
/// <summary> /// <summary>
/// 2路 /// 2路
/// </summary> /// </summary>
TWO = 0x0000_0001, TWO = 0x0000_0001,
@@ -366,7 +366,7 @@ public class Analyzer
/// <returns>操作结果,成功返回寄存器值,否则返回异常信息</returns> /// <returns>操作结果,成功返回寄存器值,否则返回异常信息</returns>
public async ValueTask<Result<CaptureStatus>> ReadCaptureStatus() public async ValueTask<Result<CaptureStatus>> ReadCaptureStatus()
{ {
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, AnalyzerAddr.CAPTURE_MODE, this.timeout); var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, AnalyzerAddr.CAPTURE_MODE, this.timeout);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
{ {
logger.Error($"Failed to read capture status: {ret.Error}"); logger.Error($"Failed to read capture status: {ret.Error}");

View File

@@ -9,57 +9,57 @@ static class OscilloscopeAddr
{ {
const UInt32 BASE = 0x8000_0000; const UInt32 BASE = 0x8000_0000;
/// <summary> /// <summary>
/// 0x0000_0000:R/W[0] wave_run 启动捕获/关闭 /// 0x0000_0000:R/W[0] wave_run 启动捕获/关闭
/// </summary> /// </summary>
public const UInt32 START_CAPTURE = BASE + 0x0000_0000; public const UInt32 START_CAPTURE = BASE + 0x0000_0000;
/// <summary> /// <summary>
/// 0x0000_0001: R/W[7:0] trig_level 触发电平 /// 0x0000_0001: R/W[7:0] trig_level 触发电平
/// </summary> /// </summary>
public const UInt32 TRIG_LEVEL = BASE + 0x0000_0001; public const UInt32 TRIG_LEVEL = BASE + 0x0000_0001;
/// <summary> /// <summary>
/// 0x0000_0002:R/W[0] trig_edge 触发边沿0-下降沿1-上升沿 /// 0x0000_0002:R/W[0] trig_edge 触发边沿0-下降沿1-上升沿
/// </summary> /// </summary>
public const UInt32 TRIG_EDGE = BASE + 0x0000_0002; public const UInt32 TRIG_EDGE = BASE + 0x0000_0002;
/// <summary> /// <summary>
/// 0x0000_0003: R/W[9:0] h shift 水平偏移量 /// 0x0000_0003: R/W[9:0] h shift 水平偏移量
/// </summary> /// </summary>
public const UInt32 H_SHIFT = BASE + 0x0000_0003; public const UInt32 H_SHIFT = BASE + 0x0000_0003;
/// <summary> /// <summary>
/// 0x0000_0004: R/W[9:0] deci rate 抽样率0—1023 /// 0x0000_0004: R/W[9:0] deci rate 抽样率0—1023
/// </summary> /// </summary>
public const UInt32 DECI_RATE = BASE + 0x0000_0004; public const UInt32 DECI_RATE = BASE + 0x0000_0004;
/// <summary> /// <summary>
/// 0x0000_0005:R/W[0] ram refresh RAM刷新 /// 0x0000_0005:R/W[0] ram refresh RAM刷新
/// </summary> /// </summary>
public const UInt32 RAM_FRESH = BASE + 0x0000_0005; public const UInt32 RAM_FRESH = BASE + 0x0000_0005;
/// <summary> /// <summary>
/// 0x0000 0006:R[19: 0] ad_freq AD采样频率 /// 0x0000 0006:R[19: 0] ad_freq AD采样频率
/// </summary> /// </summary>
public const UInt32 AD_FREQ = BASE + 0x0000_0006; public const UInt32 AD_FREQ = BASE + 0x0000_0006;
/// <summary> /// <summary>
/// Ox0000_0007: R[7:0] ad_vpp AD采样幅度 /// Ox0000_0007: R[7:0] ad_vpp AD采样幅度
/// </summary> /// </summary>
public const UInt32 AD_VPP = BASE + 0x0000_0007; public const UInt32 AD_VPP = BASE + 0x0000_0007;
/// <summary> /// <summary>
/// 0x0000_0008: R[7:0] ad max AD采样最大值 /// 0x0000_0008: R[7:0] ad max AD采样最大值
/// </summary> /// </summary>
public const UInt32 AD_MAX = BASE + 0x0000_0008; public const UInt32 AD_MAX = BASE + 0x0000_0008;
/// <summary> /// <summary>
/// 0x0000_0009: R[7:0] ad_min AD采样最小值 /// 0x0000_0009: R[7:0] ad_min AD采样最小值
/// </summary> /// </summary>
public const UInt32 AD_MIN = BASE + 0x0000_0009; public const UInt32 AD_MIN = BASE + 0x0000_0009;
/// <summary> /// <summary>
/// 0x0000_1000-0x0000_13FF:R[7:0] wave_rd_data 共1024个字节 /// 0x0000_1000-0x0000_13FF:R[7:0] wave_rd_data 共1024个字节
/// </summary> /// </summary>
public const UInt32 RD_DATA_ADDR = BASE + 0x0000_1000; public const UInt32 RD_DATA_ADDR = BASE + 0x0000_1000;
@@ -232,7 +232,7 @@ class Oscilloscope
/// <returns>操作结果,成功返回采样频率值,否则返回异常信息</returns> /// <returns>操作结果,成功返回采样频率值,否则返回异常信息</returns>
public async ValueTask<Result<UInt32>> GetADFrequency() public async ValueTask<Result<UInt32>> GetADFrequency()
{ {
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_FREQ, this.timeout); var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, OscilloscopeAddr.AD_FREQ, this.timeout);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
{ {
logger.Error($"Failed to read AD frequency: {ret.Error}"); logger.Error($"Failed to read AD frequency: {ret.Error}");
@@ -255,7 +255,7 @@ class Oscilloscope
/// <returns>操作结果,成功返回采样幅度值,否则返回异常信息</returns> /// <returns>操作结果,成功返回采样幅度值,否则返回异常信息</returns>
public async ValueTask<Result<byte>> GetADVpp() public async ValueTask<Result<byte>> GetADVpp()
{ {
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_VPP, this.timeout); var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, OscilloscopeAddr.AD_VPP, this.timeout);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
{ {
logger.Error($"Failed to read AD VPP: {ret.Error}"); logger.Error($"Failed to read AD VPP: {ret.Error}");
@@ -275,7 +275,7 @@ class Oscilloscope
/// <returns>操作结果,成功返回采样最大值,否则返回异常信息</returns> /// <returns>操作结果,成功返回采样最大值,否则返回异常信息</returns>
public async ValueTask<Result<byte>> GetADMax() public async ValueTask<Result<byte>> GetADMax()
{ {
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_MAX, this.timeout); var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, OscilloscopeAddr.AD_MAX, this.timeout);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
{ {
logger.Error($"Failed to read AD max: {ret.Error}"); logger.Error($"Failed to read AD max: {ret.Error}");
@@ -295,7 +295,7 @@ class Oscilloscope
/// <returns>操作结果,成功返回采样最小值,否则返回异常信息</returns> /// <returns>操作结果,成功返回采样最小值,否则返回异常信息</returns>
public async ValueTask<Result<byte>> GetADMin() public async ValueTask<Result<byte>> GetADMin()
{ {
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_MIN, this.timeout); var ret = await UDPClientPool.ReadAddrByte(this.ep, this.taskID, OscilloscopeAddr.AD_MIN, this.timeout);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
{ {
logger.Error($"Failed to read AD min: {ret.Error}"); logger.Error($"Failed to read AD min: {ret.Error}");

View File

@@ -7,20 +7,20 @@ static class RemoteUpdaterAddr
{ {
public const UInt32 Base = 0x20_00_00_00; public const UInt32 Base = 0x20_00_00_00;
/// <summary> /// <summary>
/// ADDR: 0X00: 写Flash-读写地址——控制位 <br/> /// ADDR: 0X00: 写Flash-读写地址——控制位 <br/>
/// [31:16]: wr_sector_num <br/> /// [31:16]: wr_sector_num <br/>
/// [15: 0]: {flash_wr_en,-,-,-, start_wr_sector} <br/> /// [15: 0]: {flash_wr_en,-,-,-, start_wr_sector} <br/>
/// </summary> /// </summary>
public const UInt32 WriteCtrl = Base + 0x00; public const UInt32 WriteCtrl = Base + 0x00;
/// <summary> /// <summary>
/// ADDR: 0X01: 写Flash-只写地址——FIFO入口 <br/> /// ADDR: 0X01: 写Flash-只写地址——FIFO入口 <br/>
/// [31:0]: 写比特流数据入口 <br/> /// [31:0]: 写比特流数据入口 <br/>
/// </summary> /// </summary>
public const UInt32 WriteFIFO = Base + 0x01; public const UInt32 WriteFIFO = Base + 0x01;
/// <summary> /// <summary>
/// ADDR: 0X02: 写Flash-只读地址——标志位 <br/> /// ADDR: 0X02: 写Flash-只读地址——标志位 <br/>
/// [31:24]: {-, -, -, -, -, -, -, wr_fifo_full} <br/> /// [31:24]: {-, -, -, -, -, -, -, wr_fifo_full} <br/>
/// [23:16]: {-, -, -, -, -, -, -, wr_fifo_empty} <br/> /// [23:16]: {-, -, -, -, -, -, -, wr_fifo_empty} <br/>
@@ -29,14 +29,14 @@ static class RemoteUpdaterAddr
/// </summary> /// </summary>
public const UInt32 WriteSign = Base + 0x02; public const UInt32 WriteSign = Base + 0x02;
/// <summary> /// <summary>
/// ADDR: 0X03: 读Flash-读写地址——控制位1 <br/> /// ADDR: 0X03: 读Flash-读写地址——控制位1 <br/>
/// [31:16]: rd_sector_num <br/> /// [31:16]: rd_sector_num <br/>
/// [15: 0]: {flash_rd_en,-,-,-, start_rd_sub_sector} <br/> /// [15: 0]: {flash_rd_en,-,-,-, start_rd_sub_sector} <br/>
/// </summary> /// </summary>
public const UInt32 ReadCtrl1 = Base + 0x03; public const UInt32 ReadCtrl1 = Base + 0x03;
/// <summary> /// <summary>
/// ADDR: 0X04: 读Flash-读写地址——控制位2 <br/> /// ADDR: 0X04: 读Flash-读写地址——控制位2 <br/>
/// [31:24]: { } <br/> /// [31:24]: { } <br/>
/// [23:16]: {-, -, -, -, -, -,{ bs_crc32_ok }} <br/> /// [23:16]: {-, -, -, -, -, -,{ bs_crc32_ok }} <br/>
@@ -45,19 +45,19 @@ static class RemoteUpdaterAddr
/// </summary> /// </summary>
public const UInt32 ReadCtrl2 = Base + 0x04; public const UInt32 ReadCtrl2 = Base + 0x04;
/// <summary> /// <summary>
/// ADDR: 0X05: 读Flash-只读地址——FIFO出口 <br/> /// ADDR: 0X05: 读Flash-只读地址——FIFO出口 <br/>
/// [31:0]: 读比特流数据出口 <br/> /// [31:0]: 读比特流数据出口 <br/>
/// </summary> /// </summary>
public const UInt32 ReadFIFO = Base + 0x05; public const UInt32 ReadFIFO = Base + 0x05;
/// <summary> /// <summary>
/// ADDR: 0X06: 读Flash-只读地址——CRC校验值 <br/> /// ADDR: 0X06: 读Flash-只读地址——CRC校验值 <br/>
/// [31:0]: CRC校验值 bs_readback_crc <br/> /// [31:0]: CRC校验值 bs_readback_crc <br/>
/// </summary> /// </summary>
public const UInt32 ReadCRC = Base + 0x06; public const UInt32 ReadCRC = Base + 0x06;
/// <summary> /// <summary>
/// ADDR: 0X07: 读Flash-只读地址——标志位 <br/> /// ADDR: 0X07: 读Flash-只读地址——标志位 <br/>
/// [31:24]: {-, -, -, -, -, -, -, rd_fifo_afull} <br/> /// [31:24]: {-, -, -, -, -, -, -, rd_fifo_afull} <br/>
/// [23:16]: {-, -, -, -, -, -, -, rd_fifo_empty} <br/> /// [23:16]: {-, -, -, -, -, -, -, rd_fifo_empty} <br/>
@@ -66,14 +66,14 @@ static class RemoteUpdaterAddr
/// </summary> /// </summary>
public const UInt32 ReadSign = Base + 0x07; public const UInt32 ReadSign = Base + 0x07;
/// <summary> /// <summary>
/// ADDR: 0X08: 热启动开关-读写地址——控制位 <br/> /// ADDR: 0X08: 热启动开关-读写地址——控制位 <br/>
/// [31: 8]: hotreset_addr <br/> /// [31: 8]: hotreset_addr <br/>
/// [ 7: 0]: {-, -, -, -, -, -, -, hotreset_en} <br/> /// [ 7: 0]: {-, -, -, -, -, -, -, hotreset_en} <br/>
/// </summary> /// </summary>
public const UInt32 HotResetCtrl = Base + 0x08; public const UInt32 HotResetCtrl = Base + 0x08;
/// <summary> /// <summary>
/// ADDR: 0X09: 只读地址 版本号 <br/> /// ADDR: 0X09: 只读地址 版本号 <br/>
/// [31: 0]: FPGA_VERSION[31:0] <br/> /// [31: 0]: FPGA_VERSION[31:0] <br/>
/// </summary> /// </summary>
@@ -339,7 +339,7 @@ public class RemoteUpdater
} }
{ {
var ret = await UDPClientPool.ReadAddr(this.ep, 0, RemoteUpdaterAddr.ReadCRC, this.timeout); var ret = await UDPClientPool.ReadAddrByte(this.ep, 0, RemoteUpdaterAddr.ReadCRC, this.timeout);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
var bytes = ret.Value.Options.Data; var bytes = ret.Value.Options.Data;
@@ -543,7 +543,7 @@ public class RemoteUpdater
logger.Trace("Clear udp data finished"); logger.Trace("Clear udp data finished");
{ {
var ret = await UDPClientPool.ReadAddr(this.ep, 0, RemoteUpdaterAddr.Version, this.timeout); var ret = await UDPClientPool.ReadAddrByte(this.ep, 0, RemoteUpdaterAddr.Version, this.timeout);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
var retData = ret.Value.Options.Data; var retData = ret.Value.Options.Data;

View File

@@ -0,0 +1,79 @@
using System.Net;
using DotNext;
using Common;
namespace Peripherals.SevenDigitalTubesClient;
static class SevenDigitalTubesAddr
{
public const UInt32 BASE = 0xB000_0000;
}
public class SevenDigitalTubesCtrl
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
readonly int timeout = 500;
readonly int taskID;
readonly int port;
readonly string address;
private IPEndPoint ep;
/// <summary>
/// 初始化七段数码管控制器
/// </summary>
/// <param name="address">七段数码管控制器IP地址</param>
/// <param name="port">七段数码管控制器端口</param>
/// <param name="taskID">任务ID</param>
/// <param name="timeout">超时时间(毫秒)</param>
public SevenDigitalTubesCtrl(string address, int port, int taskID, int timeout = 500)
{
if (timeout < 0)
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
this.address = address;
this.port = port;
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
this.taskID = taskID;
this.timeout = timeout;
}
public async ValueTask<Result<byte>> ReadTube(int num)
{
if (num < 0 || num > 31)
throw new ArgumentOutOfRangeException(nameof(num), "Tube number must be between 0 and 31");
var ret = await UDPClientPool.ReadAddrByte(
this.ep, this.taskID, SevenDigitalTubesAddr.BASE + (UInt32)num, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Read tubes failed: {ret.Error}");
return new(ret.Error);
}
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 4)
return new(new Exception("Data length is too short"));
var data = Number.BytesToUInt32(ret.Value.Options.Data, 0, 4).Value;
if ((data >> 8) != num)
{
logger.Error($"Read wrong tube number: {num} != {data >> 8}");
return new(new Exception($"Read wrong tube number: {num} != {data >> 8}"));
}
return (byte)(data & 0xFF);
}
public async ValueTask<Result<byte[]>> ScanAllTubes()
{
var tubes = new byte[32];
for (int i = 0; i < 32; i++)
{
var ret = await ReadTube(i);
if (!ret.IsSuccessful)
return new(ret.Error);
tubes[i] = ret.Value;
}
return tubes;
}
}

View File

@@ -0,0 +1,65 @@
using System.Collections;
using System.Net;
using DotNext;
namespace Peripherals.SwitchClient;
class SwitchCtrlAddr
{
public const UInt32 BASE = 0xB0_00_00_20;
public const UInt32 ENABLE = BASE;
}
/// <summary>
/// 矩阵键盘外设类,用于控制和管理矩阵键盘的功能。
/// </summary>
public class SwitchCtrl
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
readonly int timeout = 500;
readonly int taskID;
readonly int port;
readonly string address;
private IPEndPoint ep;
public SwitchCtrl(string address, int port, int taskID, int timeout = 500)
{
if (timeout < 0)
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
this.address = address;
this.port = port;
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
this.taskID = taskID;
this.timeout = timeout;
}
public async ValueTask<Result<bool>> SetEnable(bool enable)
{
if (MsgBus.IsRunning)
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
else return new(new Exception("Message Bus not work!"));
var ret = await UDPClientPool.WriteAddr(
this.ep, this.taskID, SwitchCtrlAddr.ENABLE, enable ? 0x1U : 0x0U, this.timeout);
if (!ret.IsSuccessful) return new(ret.Error);
return ret.Value;
}
public async ValueTask<Result<bool>> SetSwitchOnOff(int num, bool onOff)
{
if (MsgBus.IsRunning)
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
else return new(new Exception("Message Bus not work!"));
var ret = await UDPClientPool.WriteAddr(
this.ep, this.taskID, SwitchCtrlAddr.BASE + (UInt32)num, onOff ? 0x1U : 0x0U, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Set Switch {onOff} failed: {ret.Error}");
return new(ret.Error);
}
return ret.Value;
}
}

View File

@@ -1,6 +1,7 @@
using System.Net; using System.Net;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using Peripherals.HdmiInClient; using Peripherals.HdmiInClient;
using Peripherals.JpegClient;
namespace server.Services; namespace server.Services;
@@ -12,18 +13,32 @@ public class HdmiVideoStreamEndpoint
public string SnapshotUrl { get; set; } = ""; public string SnapshotUrl { get; set; } = "";
} }
public class HdmiVideoStreamClient
{
public required HdmiIn HdmiInClient { get; set; }
public required Jpeg JpegClient { get; set; }
public required CancellationTokenSource CTS { get; set; }
public required int Offset { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
public class HttpHdmiVideoStreamService : BackgroundService public class HttpHdmiVideoStreamService : BackgroundService
{ {
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private HttpListener? _httpListener; private HttpListener? _httpListener;
private readonly int _serverPort = 4322; private readonly int _serverPort = 4322;
private readonly ConcurrentDictionary<string, HdmiIn> _hdmiInDict = new(); private readonly ConcurrentDictionary<string, HdmiVideoStreamClient> _clientDict = new();
private readonly ConcurrentDictionary<string, CancellationTokenSource> _hdmiInCtsDict = new();
public override async Task StartAsync(CancellationToken cancellationToken) public override async Task StartAsync(CancellationToken cancellationToken)
{ {
_httpListener = new HttpListener(); _httpListener = new HttpListener();
_httpListener.Prefixes.Add($"http://*:{_serverPort}/"); _httpListener.Prefixes.Add($"http://{Global.LocalHost}:{_serverPort}/");
_httpListener.Start(); _httpListener.Start();
logger.Info($"HDMI Video Stream Service started on port {_serverPort}"); logger.Info($"HDMI Video Stream Service started on port {_serverPort}");
@@ -32,40 +47,31 @@ public class HttpHdmiVideoStreamService : BackgroundService
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
try while (!stoppingToken.IsCancellationRequested)
{ {
while (!stoppingToken.IsCancellationRequested) if (_httpListener == null) continue;
try
{ {
HttpListenerContext? context = null; logger.Debug("Waiting for HTTP request...");
try var contextTask = _httpListener.GetContextAsync();
var completedTask = await Task.WhenAny(contextTask, Task.Delay(-1, stoppingToken));
if (completedTask == contextTask)
{ {
logger.Debug("Waiting for HTTP request..."); var context = contextTask.Result;
context = await _httpListener.GetContextAsync();
logger.Debug($"Received request: {context.Request.Url?.AbsolutePath}"); logger.Debug($"Received request: {context.Request.Url?.AbsolutePath}");
if (context != null)
_ = HandleRequestAsync(context, stoppingToken);
} }
catch (ObjectDisposedException) else
{ {
// Listener closed, exit loop
break; break;
} }
catch (HttpListenerException)
{
// Listener closed, exit loop
break;
}
catch (Exception ex)
{
logger.Error(ex, "Error in GetContextAsync");
break;
}
if (context != null)
_ = HandleRequestAsync(context, stoppingToken);
} }
} catch (Exception ex)
finally {
{ logger.Error(ex, "Error in GetContextAsync");
_httpListener?.Close(); break;
logger.Info("HDMI Video Stream Service stopped."); }
} }
} }
@@ -75,7 +81,7 @@ public class HttpHdmiVideoStreamService : BackgroundService
// 禁用所有活跃的HDMI传输 // 禁用所有活跃的HDMI传输
var disableTasks = new List<Task>(); var disableTasks = new List<Task>();
foreach (var hdmiKey in _hdmiInDict.Keys) foreach (var hdmiKey in _clientDict.Keys)
{ {
disableTasks.Add(DisableHdmiTransmissionAsync(hdmiKey)); disableTasks.Add(DisableHdmiTransmissionAsync(hdmiKey));
} }
@@ -84,10 +90,8 @@ public class HttpHdmiVideoStreamService : BackgroundService
await Task.WhenAll(disableTasks); await Task.WhenAll(disableTasks);
// 清空字典 // 清空字典
_hdmiInDict.Clear(); _clientDict.Clear();
_hdmiInCtsDict.Clear();
_httpListener?.Close(); // 立即关闭监听器,唤醒阻塞
await base.StopAsync(cancellationToken); await base.StopAsync(cancellationToken);
} }
@@ -95,18 +99,17 @@ public class HttpHdmiVideoStreamService : BackgroundService
{ {
try try
{ {
var cts = _hdmiInCtsDict[key]; var client = _clientDict[key];
cts.Cancel(); client.CTS.Cancel();
var hdmiIn = _hdmiInDict[key]; var disableResult = await client.JpegClient.SetEnable(false);
var disableResult = await hdmiIn.EnableTrans(false); if (disableResult)
if (disableResult.IsSuccessful)
{ {
logger.Info("Successfully disabled HDMI transmission"); logger.Info("Successfully disabled HDMI transmission");
} }
else else
{ {
logger.Error($"Failed to disable HDMI transmission: {disableResult.Error}"); logger.Error($"Failed to disable HDMI transmission");
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -115,40 +118,18 @@ public class HttpHdmiVideoStreamService : BackgroundService
} }
} }
// 获取/创建 HdmiIn 实例 private async Task<HdmiVideoStreamClient?> GetOrCreateClientAsync(string boardId)
private async Task<HdmiIn?> GetOrCreateHdmiInAsync(string boardId)
{ {
if (_hdmiInDict.TryGetValue(boardId, out var hdmiIn)) if (_clientDict.TryGetValue(boardId, out var client))
{ {
try client.Width = client.JpegClient.Width;
{ client.Height = client.JpegClient.Height;
var enableResult = await hdmiIn.EnableTrans(true); return client;
if (!enableResult.IsSuccessful)
{
logger.Error($"Failed to enable HDMI transmission for board {boardId}: {enableResult.Error}");
return null;
}
logger.Info($"Successfully enabled HDMI transmission for board {boardId}");
}
catch (Exception ex)
{
logger.Error(ex, $"Exception occurred while enabling HDMI transmission for board {boardId}");
return null;
}
_hdmiInDict[boardId] = hdmiIn;
_hdmiInCtsDict[boardId] = new CancellationTokenSource();
return hdmiIn;
} }
var db = new Database.AppDataConnection(); var userManager = new Database.UserManager();
if (db == null)
{
logger.Error("Failed to create HdmiIn instance");
return null;
}
var boardRet = db.GetBoardByID(Guid.Parse(boardId)); var boardRet = userManager.GetBoardByID(Guid.Parse(boardId));
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue) if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
{ {
logger.Error($"Failed to get board with ID {boardId}"); logger.Error($"Failed to get board with ID {boardId}");
@@ -157,18 +138,35 @@ public class HttpHdmiVideoStreamService : BackgroundService
var board = boardRet.Value.Value; var board = boardRet.Value.Value;
hdmiIn = new HdmiIn(board.IpAddr, board.Port, 0); // taskID 可根据实际需求调整 client = new HdmiVideoStreamClient()
{
HdmiInClient = new HdmiIn(board.IpAddr, board.Port, 1),
JpegClient = new Jpeg(board.IpAddr, board.Port, 1),
CTS = new CancellationTokenSource(),
Offset = 0
};
// 启用HDMI传输 // 启用HDMI传输
try try
{ {
var enableResult = await hdmiIn.EnableTrans(true); // var hdmiEnableRet = await client.JpegClient.EnableTrans(true);
if (!enableResult.IsSuccessful) // if (!hdmiEnableRet.IsSuccessful)
// {
// logger.Error($"Failed to enable HDMI transmission for board {boardId}: {hdmiEnableRet.Error}");
// return null;
// }
// logger.Info($"Successfully enabled HDMI transmission for board {boardId}");
var jpegEnableRet = await client.JpegClient.Init(true);
if (!jpegEnableRet.IsSuccessful)
{ {
logger.Error($"Failed to enable HDMI transmission for board {boardId}: {enableResult.Error}"); logger.Error($"Failed to enable JPEG transmission for board {boardId}: {jpegEnableRet.Error}");
return null; return null;
} }
logger.Info($"Successfully enabled HDMI transmission for board {boardId}"); logger.Info($"Successfully enabled JPEG transmission for board {boardId}");
client.Width = client.JpegClient.Width;
client.Height = client.JpegClient.Height;
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -176,9 +174,8 @@ public class HttpHdmiVideoStreamService : BackgroundService
return null; return null;
} }
_hdmiInDict[boardId] = hdmiIn; _clientDict[boardId] = client;
_hdmiInCtsDict[boardId] = new CancellationTokenSource(); return client;
return hdmiIn;
} }
private async Task HandleRequestAsync(HttpListenerContext context, CancellationToken cancellationToken) private async Task HandleRequestAsync(HttpListenerContext context, CancellationToken cancellationToken)
@@ -191,27 +188,22 @@ public class HttpHdmiVideoStreamService : BackgroundService
return; return;
} }
var hdmiIn = await GetOrCreateHdmiInAsync(boardId); var client = await GetOrCreateClientAsync(boardId);
if (hdmiIn == null) if (client == null)
{ {
await SendErrorAsync(context.Response, "Invalid boardId or board not available"); await SendErrorAsync(context.Response, "Invalid boardId or board not available");
return; return;
} }
var hdmiInToken = _hdmiInCtsDict[boardId].Token; var hdmiInToken = _clientDict[boardId].CTS.Token;
if (hdmiInToken == null)
{
await SendErrorAsync(context.Response, "HDMI input is not available");
return;
}
if (path == "/snapshot") if (path == "/snapshot")
{ {
await HandleSnapshotRequestAsync(context.Response, hdmiIn, hdmiInToken); await HandleSnapshotRequestAsync(context.Response, client, hdmiInToken);
} }
else if (path == "/mjpeg") else if (path == "/mjpeg")
{ {
await HandleMjpegStreamAsync(context.Response, hdmiIn, hdmiInToken); await HandleMjpegStreamAsync(context.Response, client, hdmiInToken);
} }
else if (path == "/video") else if (path == "/video")
{ {
@@ -223,18 +215,16 @@ public class HttpHdmiVideoStreamService : BackgroundService
} }
} }
private async Task HandleSnapshotRequestAsync(HttpListenerResponse response, HdmiIn hdmiIn, CancellationToken cancellationToken) private async Task HandleSnapshotRequestAsync(
HttpListenerResponse response, HdmiVideoStreamClient client, CancellationToken cancellationToken)
{ {
try try
{ {
logger.Debug("处理HDMI快照请求"); logger.Debug("处理HDMI快照请求");
const int frameWidth = 960; // HDMI输入分辨率
const int frameHeight = 540;
// 从HDMI读取RGB565数据 // 从HDMI读取RGB565数据
var frameResult = await hdmiIn.ReadFrame(); var frameResult = await client.JpegClient.GetMultiFrames((uint)client.Offset);
if (!frameResult.IsSuccessful || frameResult.Value == null) if (!frameResult.IsSuccessful || frameResult.Value == null || frameResult.Value.Count == 0)
{ {
logger.Error("HDMI快照获取失败"); logger.Error("HDMI快照获取失败");
response.StatusCode = 500; response.StatusCode = 500;
@@ -244,51 +234,27 @@ public class HttpHdmiVideoStreamService : BackgroundService
return; return;
} }
var rgb565Data = frameResult.Value; var jpegData = frameResult.Value[0];
var jpegImage = Common.Image.CompleteJpegData(jpegData, client.Width, client.Height);
// 验证数据长度 if (!jpegImage.IsSuccessful)
var expectedLength = frameWidth * frameHeight * 2;
if (rgb565Data.Length != expectedLength)
{ {
logger.Warn("HDMI快照数据长度不匹配期望: {Expected}, 实际: {Actual}", logger.Error("JPEG数据补全失败");
expectedLength, rgb565Data.Length);
}
// 将RGB565转换为RGB24
var rgb24Result = Common.Image.ConvertRGB565ToRGB24(rgb565Data, frameWidth, frameHeight, isLittleEndian: false);
if (!rgb24Result.IsSuccessful)
{
logger.Error("HDMI快照RGB565转RGB24失败: {Error}", rgb24Result.Error);
response.StatusCode = 500; response.StatusCode = 500;
var errorBytes = System.Text.Encoding.UTF8.GetBytes("Failed to process HDMI snapshot"); var errorBytes = System.Text.Encoding.UTF8.GetBytes("Failed to complete JPEG data");
await response.OutputStream.WriteAsync(errorBytes, 0, errorBytes.Length, cancellationToken); await response.OutputStream.WriteAsync(errorBytes, 0, errorBytes.Length, cancellationToken);
response.Close(); response.Close();
return; return;
} }
// 将RGB24转换为JPEG
var jpegResult = Common.Image.ConvertRGB24ToJpeg(rgb24Result.Value, frameWidth, frameHeight, 80);
if (!jpegResult.IsSuccessful)
{
logger.Error("HDMI快照RGB24转JPEG失败: {Error}", jpegResult.Error);
response.StatusCode = 500;
var errorBytes = System.Text.Encoding.UTF8.GetBytes("Failed to encode HDMI snapshot");
await response.OutputStream.WriteAsync(errorBytes, 0, errorBytes.Length, cancellationToken);
response.Close();
return;
}
var jpegData = jpegResult.Value;
// 设置响应头参考Camera版本 // 设置响应头参考Camera版本
response.ContentType = "image/jpeg"; response.ContentType = "image/jpeg";
response.ContentLength64 = jpegData.Length; response.ContentLength64 = jpegImage.Value.Length;
response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate"); response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
await response.OutputStream.WriteAsync(jpegData, 0, jpegData.Length, cancellationToken); await response.OutputStream.WriteAsync(jpegImage.Value, 0, jpegImage.Value.Length, cancellationToken);
await response.OutputStream.FlushAsync(cancellationToken); await response.OutputStream.FlushAsync(cancellationToken);
logger.Debug("已发送HDMI快照图像大小{Size} 字节", jpegData.Length); logger.Debug("已发送HDMI快照图像大小{Size} 字节", jpegImage.Value.Length);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -301,7 +267,8 @@ public class HttpHdmiVideoStreamService : BackgroundService
} }
} }
private async Task HandleMjpegStreamAsync(HttpListenerResponse response, HdmiIn hdmiIn, CancellationToken cancellationToken) private async Task HandleMjpegStreamAsync(
HttpListenerResponse response, HdmiVideoStreamClient client, CancellationToken cancellationToken)
{ {
try try
{ {
@@ -314,70 +281,42 @@ public class HttpHdmiVideoStreamService : BackgroundService
logger.Debug("开始HDMI MJPEG流传输"); logger.Debug("开始HDMI MJPEG流传输");
int frameCounter = 0; int frameCounter = 0;
const int frameWidth = 960; // HDMI输入分辨率
const int frameHeight = 540;
while (!cancellationToken.IsCancellationRequested) while (!cancellationToken.IsCancellationRequested)
{ {
try var frameStartTime = DateTime.UtcNow;
var frameResult =
await client.JpegClient.GetMultiFrames((uint)client.Offset);
if (!frameResult.IsSuccessful || frameResult.Value == null || frameResult.Value.Count == 0)
{ {
var frameStartTime = DateTime.UtcNow; logger.Error("获取HDMI帧失败");
await Task.Delay(100, cancellationToken);
continue;
}
// 从HDMI读取RGB565数据 foreach (var framebytes in frameResult.Value)
var readStartTime = DateTime.UtcNow; {
var frameResult = await hdmiIn.ReadFrame(); var jpegImage = Common.Image.CompleteJpegData(framebytes, client.Width, client.Height);
var readEndTime = DateTime.UtcNow; if (!jpegImage.IsSuccessful)
var readTime = (readEndTime - readStartTime).TotalMilliseconds;
if (!frameResult.IsSuccessful || frameResult.Value == null)
{ {
logger.Warn("HDMI帧读取失败或为空"); logger.Error("JPEG数据不完整");
await Task.Delay(100, cancellationToken);
continue; continue;
} }
var rgb565Data = frameResult.Value; var frameRet = Common.Image.CreateMjpegFrameFromJpeg(jpegImage.Value);
if (!frameRet.IsSuccessful)
// 验证数据长度是否正确 (RGB565为每像素2字节)
var expectedLength = frameWidth * frameHeight * 2;
if (rgb565Data.Length != expectedLength)
{ {
logger.Warn("HDMI数据长度不匹配期望: {Expected}, 实际: {Actual}", logger.Error("创建MJPEG帧失败");
expectedLength, rgb565Data.Length); await Task.Delay(100, cancellationToken);
}
// 将RGB565转换为RGB24参考Camera版本的处理
var convertStartTime = DateTime.UtcNow;
var rgb24Result = Common.Image.ConvertRGB565ToRGB24(rgb565Data, frameWidth, frameHeight, isLittleEndian: false);
var convertEndTime = DateTime.UtcNow;
var convertTime = (convertEndTime - convertStartTime).TotalMilliseconds;
if (!rgb24Result.IsSuccessful)
{
logger.Error("HDMI RGB565转RGB24失败: {Error}", rgb24Result.Error);
continue; continue;
} }
var frame = frameRet.Value;
// 将RGB24转换为JPEG参考Camera版本的处理 await response.OutputStream.WriteAsync(frame.header, 0, frame.header.Length, cancellationToken);
var jpegStartTime = DateTime.UtcNow; await response.OutputStream.WriteAsync(frame.data, 0, frame.data.Length, cancellationToken);
var jpegResult = Common.Image.ConvertRGB24ToJpeg(rgb24Result.Value, frameWidth, frameHeight, 80); await response.OutputStream.WriteAsync(frame.footer, 0, frame.footer.Length, cancellationToken);
var jpegEndTime = DateTime.UtcNow;
var jpegTime = (jpegEndTime - jpegStartTime).TotalMilliseconds;
if (!jpegResult.IsSuccessful)
{
logger.Error("HDMI RGB24转JPEG失败: {Error}", jpegResult.Error);
continue;
}
var jpegData = jpegResult.Value;
// 发送MJPEG帧使用Camera版本的格式
var mjpegFrameHeader = Common.Image.CreateMjpegFrameHeader(jpegData.Length);
var mjpegFrameFooter = Common.Image.CreateMjpegFrameFooter();
await response.OutputStream.WriteAsync(mjpegFrameHeader, 0, mjpegFrameHeader.Length, cancellationToken);
await response.OutputStream.WriteAsync(jpegData, 0, jpegData.Length, cancellationToken);
await response.OutputStream.WriteAsync(mjpegFrameFooter, 0, mjpegFrameFooter.Length, cancellationToken);
await response.OutputStream.FlushAsync(cancellationToken); await response.OutputStream.FlushAsync(cancellationToken);
frameCounter++; frameCounter++;
@@ -387,13 +326,10 @@ public class HttpHdmiVideoStreamService : BackgroundService
// 性能统计日志每30帧记录一次 // 性能统计日志每30帧记录一次
if (frameCounter % 30 == 0) if (frameCounter % 30 == 0)
{ {
logger.Debug("HDMI帧 {FrameNumber} 性能统计 - 读取: {ReadTime:F1}ms, RGB转换: {ConvertTime:F1}ms, JPEG转换: {JpegTime:F1}ms, 总计: {TotalTime:F1}ms, JPEG大小: {JpegSize} 字节", logger.Debug("HDMI帧 {FrameNumber} 性能统计 - 总计: {TotalTime:F1}ms, JPEG大小: {JpegSize} 字节",
frameCounter, readTime, convertTime, jpegTime, totalTime, jpegData.Length); frameCounter, totalTime, frame.data.Length);
} }
}
catch (Exception ex)
{
logger.Error(ex, "处理HDMI帧时发生错误");
} }
} }
} }
@@ -406,7 +342,7 @@ public class HttpHdmiVideoStreamService : BackgroundService
try try
{ {
// 停止传输时禁用HDMI传输 // 停止传输时禁用HDMI传输
await hdmiIn.EnableTrans(false); await client.HdmiInClient.EnableTrans(false);
logger.Info("已禁用HDMI传输"); logger.Info("已禁用HDMI传输");
} }
catch (Exception ex) catch (Exception ex)
@@ -461,8 +397,9 @@ public class HttpHdmiVideoStreamService : BackgroundService
/// <returns>返回所有可用的HDMI视频流终端点列表</returns> /// <returns>返回所有可用的HDMI视频流终端点列表</returns>
public List<HdmiVideoStreamEndpoint>? GetAllVideoEndpoints() public List<HdmiVideoStreamEndpoint>? GetAllVideoEndpoints()
{ {
var db = new Database.AppDataConnection(); var userManager = new Database.UserManager();
var boards = db?.GetAllBoard();
var boards = userManager.GetAllBoard();
if (boards == null) if (boards == null)
return null; return null;
@@ -472,9 +409,9 @@ public class HttpHdmiVideoStreamService : BackgroundService
endpoints.Add(new HdmiVideoStreamEndpoint endpoints.Add(new HdmiVideoStreamEndpoint
{ {
BoardId = board.ID.ToString(), BoardId = board.ID.ToString(),
MjpegUrl = $"http://{Global.localhost}:{_serverPort}/mjpeg?boardId={board.ID}", MjpegUrl = $"http://{Global.LocalHost}:{_serverPort}/mjpeg?boardId={board.ID}",
VideoUrl = $"http://{Global.localhost}:{_serverPort}/video?boardId={board.ID}", VideoUrl = $"http://{Global.LocalHost}:{_serverPort}/video?boardId={board.ID}",
SnapshotUrl = $"http://{Global.localhost}:{_serverPort}/snapshot?boardId={board.ID}" SnapshotUrl = $"http://{Global.LocalHost}:{_serverPort}/snapshot?boardId={board.ID}"
}); });
} }
return endpoints; return endpoints;
@@ -490,9 +427,9 @@ public class HttpHdmiVideoStreamService : BackgroundService
return new HdmiVideoStreamEndpoint return new HdmiVideoStreamEndpoint
{ {
BoardId = boardId, BoardId = boardId,
MjpegUrl = $"http://{Global.localhost}:{_serverPort}/mjpeg?boardId={boardId}", MjpegUrl = $"http://{Global.LocalHost}:{_serverPort}/mjpeg?boardId={boardId}",
VideoUrl = $"http://{Global.localhost}:{_serverPort}/video?boardId={boardId}", VideoUrl = $"http://{Global.LocalHost}:{_serverPort}/video?boardId={boardId}",
SnapshotUrl = $"http://{Global.localhost}:{_serverPort}/snapshot?boardId={boardId}" SnapshotUrl = $"http://{Global.LocalHost}:{_serverPort}/snapshot?boardId={boardId}"
}; };
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,147 @@
using System.Collections.Concurrent;
using Microsoft.AspNetCore.SignalR;
using server.Hubs;
namespace server.Services;
public enum TaskState { Running, Completed, Failed, Cancelled }
public readonly struct TaskProgress
{
public string Id { get; }
public int Current { get; }
public int Total { get; }
public TaskState State { get; }
public long Timestamp { get; }
public string? Error { get; }
public TaskProgress(string id, int current, int total, TaskState state, long timestamp, string? error = null)
{
Id = id;
Current = current;
Total = total;
State = state;
Timestamp = timestamp;
Error = error;
}
public TaskProgress WithUpdate(int? current = null, TaskState? state = null, string? error = null)
{
return new TaskProgress(
Id,
current ?? Current,
Total,
state ?? State,
DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
error ?? Error
);
}
public ProgressInfo ToProgressInfo()
{
return new ProgressInfo
{
TaskId = Id,
Status = State switch
{
TaskState.Running => ProgressStatus.Running,
TaskState.Completed => ProgressStatus.Completed,
TaskState.Failed => ProgressStatus.Failed,
TaskState.Cancelled => ProgressStatus.Canceled,
_ => ProgressStatus.Failed
},
ProgressPercent = Total > 0 ? ((double)Current * 100) / (double)Total : 0,
ErrorMessage = Error ?? string.Empty
};
}
}
public sealed class ProgressTracker
{
private readonly ConcurrentDictionary<string, TaskProgress> _tasks = new();
private readonly Timer _cleaner;
private readonly IHubContext<ProgressHub, IProgressReceiver> _hubContext;
// 构造器支持可选的Hub注入
public ProgressTracker(IHubContext<ProgressHub, IProgressReceiver> hubContext)
{
_hubContext = hubContext;
_cleaner = new Timer(CleanExpiredTasks, null,
TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
}
public void CleanExpiredTasks(object? obj)
{
var cutoff = DateTimeOffset.Now.AddMinutes(-3).ToUnixTimeSeconds();
var expired = _tasks.Where(kvp => kvp.Value.Timestamp < cutoff).Select(kvp => kvp.Key).ToList();
foreach (var id in expired)
{
_tasks.TryRemove(id, out _);
}
}
public string CreateTask(int total)
{
var id = Guid.NewGuid().ToString();
var task = new TaskProgress(id, 0, total, TaskState.Running, DateTimeOffset.UtcNow.ToUnixTimeSeconds());
_tasks[id] = task;
NotifyIfNeeded(task);
return id;
}
// 核心更新方法,现在包含自动通知
public bool UpdateTask(string id, Func<TaskProgress, TaskProgress> updater)
{
if (!_tasks.TryGetValue(id, out var current))
return false;
var updated = updater(current);
if (_tasks.TryUpdate(id, updated, current))
{
NotifyIfNeeded(updated);
return true;
}
return false;
}
// 自动通知逻辑 - 简单直接
private void NotifyIfNeeded(TaskProgress task)
{
_hubContext.Clients.Group(task.Id).OnReceiveProgress(task.ToProgressInfo());
}
public bool UpdateProgress(string id, int current)
{
return UpdateTask(id, p => p.WithUpdate(
current: Math.Min(current, p.Total)));
}
public bool AdvanceProgress(string id, int steps)
{
return UpdateTask(id, p => p.WithUpdate(
current: Math.Min(p.Current + steps, p.Total)));
}
public bool CancelProgress(string id)
{
return UpdateTask(id, p => p.WithUpdate(state: TaskState.Cancelled));
}
public bool CompleteProgress(string id)
{
return UpdateTask(id, p => p.WithUpdate(
current: p.Total, state: TaskState.Completed));
}
public bool FailProgress(string id, string? error)
{
return UpdateTask(id, p => p.WithUpdate(
state: TaskState.Failed, error: error));
}
public TaskProgress? GetTask(string id)
{
_tasks.TryGetValue(id, out var task);
return task.Id == null ? null : task;
}
}

View File

@@ -1,288 +0,0 @@
using Microsoft.AspNetCore.SignalR;
using System.Collections.Concurrent;
using DotNext;
using Common;
using server.Hubs;
namespace server.Services;
public class ProgressReporter : ProgressInfo, IProgress<int>
{
private int _progress = 0;
private int _stepProgress = 1;
private int _expectedSteps = 100;
private int _parentProportion = 100;
public int Progress => _progress;
public int MaxProgress { get; set; } = 100;
public int StepProgress
{
get => _stepProgress;
set
{
_stepProgress = value;
ExpectedSteps = MaxProgress / value;
}
}
public int ExpectedSteps
{
get => _expectedSteps;
set
{
_expectedSteps = value;
MaxProgress = Number.IntPow(10, Number.GetLength(value));
StepProgress = MaxProgress / value;
}
}
public Func<int, Task>? ReporterFunc { get; set; } = null;
public ProgressReporter? Parent { get; set; }
public ProgressReporter? Child { get; set; }
private ProgressStatus _status = ProgressStatus.Pending;
private string _errorMessage;
public string TaskId { get; set; } = new Guid().ToString();
public int ProgressPercent => _progress * 100 / MaxProgress;
public ProgressStatus Status => _status;
public string ErrorMessage => _errorMessage;
public ProgressReporter(Func<int, Task>? reporter = null, int initProgress = 0, int maxProgress = 100, int step = 1)
{
_progress = initProgress;
MaxProgress = maxProgress;
StepProgress = step;
ReporterFunc = reporter;
}
public ProgressReporter(int parentProportion, int expectedSteps = 100, Func<int, Task>? reporter = null)
{
this._parentProportion = parentProportion;
MaxProgress = Number.IntPow(10, Number.GetLength(expectedSteps));
StepProgress = MaxProgress / expectedSteps;
ReporterFunc = reporter;
}
private async void ForceReport(int value)
{
try
{
if (ReporterFunc != null)
await ReporterFunc(value);
if (Parent != null)
Parent.Increase((value - _progress) / StepProgress * _parentProportion / (MaxProgress / StepProgress));
_progress = value;
}
catch (OperationCanceledException ex)
{
_errorMessage = ex.Message;
this._status = ProgressStatus.Canceled;
}
catch (Exception ex)
{
_errorMessage = ex.Message;
this._status = ProgressStatus.Failed;
}
}
public async void Report(int value)
{
if (this._status == ProgressStatus.Pending)
this._status = ProgressStatus.InProgress;
else if (this.Status != ProgressStatus.InProgress)
return;
if (value > MaxProgress) return;
ForceReport(value);
}
public void Increase(int? value = null)
{
if (this._status == ProgressStatus.Pending)
this._status = ProgressStatus.InProgress;
else if (this.Status != ProgressStatus.InProgress)
return;
if (value.HasValue)
{
if (_progress + value.Value >= MaxProgress) return;
this.Report(_progress + value.Value);
}
else
{
if (_progress + StepProgress >= MaxProgress) return;
this.Report(_progress + StepProgress);
}
}
public void Finish()
{
this._status = ProgressStatus.Completed;
this.ForceReport(MaxProgress);
}
public void Cancel()
{
this._status = ProgressStatus.Canceled;
this._errorMessage = "User Cancelled";
this.ForceReport(_progress);
}
public void Error(string message)
{
this._status = ProgressStatus.Failed;
this._errorMessage = message;
this.ForceReport(_progress);
}
public ProgressReporter CreateChild(int proportion, int expectedSteps = 100)
{
var child = new ProgressReporter(proportion, expectedSteps);
child.Parent = this;
this.Child = child;
return child;
}
}
public class ProgressTrackerService : BackgroundService
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly ConcurrentDictionary<string, TaskProgressInfo> _taskMap = new();
private readonly IHubContext<ProgressHub, IProgressReceiver> _hubContext;
private class TaskProgressInfo
{
public ProgressReporter Reporter { get; set; }
public string? ConnectionId { get; set; }
public required CancellationToken CancellationToken { get; set; }
public required CancellationTokenSource CancellationTokenSource { get; set; }
public required DateTime UpdatedAt { get; set; }
}
public ProgressTrackerService(IHubContext<ProgressHub, IProgressReceiver> hubContext)
{
_hubContext = hubContext;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
var now = DateTime.UtcNow;
foreach (var kvp in _taskMap)
{
var info = kvp.Value;
// 超过 1 分钟且任务已完成/失败/取消
if ((now - info.UpdatedAt).TotalMinutes > 1 &&
(info.Reporter.Status == ProgressStatus.Completed ||
info.Reporter.Status == ProgressStatus.Failed ||
info.Reporter.Status == ProgressStatus.Canceled))
{
_taskMap.TryRemove(kvp.Key, out _);
logger.Info($"Cleaned up task {kvp.Key}");
}
}
}
catch (Exception ex)
{
logger.Error(ex, "Error during ProgressTracker cleanup");
}
await Task.Delay(TimeSpan.FromSeconds(30));
}
}
public (string, ProgressReporter) CreateTask(CancellationToken? cancellationToken = null)
{
CancellationTokenSource? cancellationTokenSource;
if (cancellationToken.HasValue)
{
cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken.Value);
}
else
{
cancellationTokenSource = new CancellationTokenSource();
}
var progressInfo = new TaskProgressInfo
{
ConnectionId = null,
UpdatedAt = DateTime.UtcNow,
CancellationToken = cancellationTokenSource.Token,
CancellationTokenSource = cancellationTokenSource,
};
var progress = new ProgressReporter(async value =>
{
cancellationTokenSource.Token.ThrowIfCancellationRequested();
// 通过 SignalR 推送进度
if (progressInfo.ConnectionId != null)
await _hubContext.Clients.Client(progressInfo.ConnectionId).OnReceiveProgress(progressInfo.Reporter);
});
progressInfo.Reporter = progress;
_taskMap.TryAdd(progressInfo.Reporter.TaskId, progressInfo);
return (progressInfo.Reporter.TaskId, progress);
}
public Optional<ProgressReporter> GetReporter(string taskId)
{
if (_taskMap.TryGetValue(taskId, out var info))
{
return info.Reporter;
}
return Optional<ProgressReporter>.None;
}
public Optional<ProgressStatus> GetProgressStatus(string taskId)
{
if (_taskMap.TryGetValue(taskId, out var info))
{
return info.Reporter.Status;
}
return Optional<ProgressStatus>.None;
}
public bool BindTask(string taskId, string connectionId)
{
if (_taskMap.TryGetValue(taskId, out var info) && info != null)
{
lock (info)
{
info.ConnectionId = connectionId;
}
return true;
}
return false;
}
public bool CancelTask(string taskId)
{
try
{
if (_taskMap.TryGetValue(taskId, out var info) && info != null)
{
lock (info)
{
info.CancellationTokenSource.Cancel();
info.Reporter.Cancel();
info.UpdatedAt = DateTime.UtcNow;
}
return true;
}
return false;
}
catch (Exception ex)
{
logger.Error(ex, $"Failed to cancel task {taskId}");
return false;
}
}
}

View File

@@ -8,11 +8,11 @@ using server.Services;
/// <summary> /// <summary>
/// UDP客户端发送池 /// UDP客户端发送池
/// </summary> /// </summary>
public class UDPClientPool public sealed class UDPClientPool
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static IPAddress localhost = IPAddress.Parse("127.0.0.1"); private static ProgressTracker _progressTracker = MsgBus.ProgressTracker;
/// <summary> /// <summary>
/// 发送字符串 /// 发送字符串
@@ -183,62 +183,34 @@ public class UDPClientPool
return await Task.Run(() => { return SendDataPack(endPoint, pkg); }); return await Task.Run(() => { return SendDataPack(endPoint, pkg); });
} }
/// <summary>
/// 发送字符串到本地
/// </summary>
/// <param name="port">端口</param>
/// <param name="stringArray">字符串数组</param>
/// <returns>是否成功</returns>
public static bool SendStringLocalHost(int port, string[] stringArray)
{
return SendString(new IPEndPoint(localhost, port), stringArray);
}
/// <summary>
/// 循环发送字符串到本地
/// </summary>
/// <param name="times">发送总次数</param>
/// <param name="sleepMilliSeconds">间隔时间</param>
/// <param name="port">端口</param>
/// <param name="stringArray">字符串数组</param>
/// <returns>是否成功</returns>
public static bool CycleSendStringLocalHost(int times, int sleepMilliSeconds, int port, string[] stringArray)
{
var isSuccessful = true;
while (times-- >= 0)
{
isSuccessful = SendStringLocalHost(port, stringArray);
if (!isSuccessful) break;
Thread.Sleep(sleepMilliSeconds);
}
return isSuccessful;
}
/// <summary> /// <summary>
/// 读取设备地址数据 /// 读取设备地址数据
/// </summary> /// </summary>
/// <param name="endPoint">IP端点IP地址与端口</param> /// <param name="endPoint">IP端点IP地址与端口</param>
/// <param name="taskID">任务ID</param> /// <param name="taskID">任务ID</param>
/// <param name="devAddr">设备地址</param> /// <param name="devAddr">设备地址</param>
/// <param name="dataLength">数据长度(0~255)</param>
/// <param name="timeout">超时时间(毫秒)</param> /// <param name="timeout">超时时间(毫秒)</param>
/// <returns>读取结果,包含接收到的数据包</returns> /// <returns>读取结果,包含接收到的数据包</returns>
public static async ValueTask<Result<RecvDataPackage>> ReadAddr( public static async ValueTask<Result<RecvDataPackage>> ReadAddr(
IPEndPoint endPoint, int taskID, uint devAddr, int timeout = 1000) IPEndPoint endPoint, int taskID, uint devAddr, int dataLength, int timeout = 1000)
{ {
if (dataLength <= 0) return new(new ArgumentException(
$"Data length must be greater than 0, instead of {dataLength}"));
if (dataLength > 255) return new(new ArgumentException(
$"Data length must be less than or equal to 255, instead of {dataLength}"));
var ret = false; var ret = false;
var opts = new SendAddrPackOptions() var opts = new SendAddrPackOptions()
{ {
BurstType = BurstType.FixedBurst, BurstType = BurstType.FixedBurst,
BurstLength = 0, BurstLength = ((byte)(dataLength - 1)),
CommandID = Convert.ToByte(taskID), CommandID = Convert.ToByte(taskID),
Address = devAddr, Address = devAddr,
IsWrite = false, IsWrite = false,
}; };
// Read Register // Read Register
ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts)); ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts));
if (!ret) return new(new Exception("Send Address Package Failed!")); if (!ret) return new(new Exception("Send Address Package Failed!"));
@@ -260,6 +232,20 @@ public class UDPClientPool
return retPack; return retPack;
} }
/// <summary>
/// 读取设备地址数据
/// </summary>
/// <param name="endPoint">IP端点IP地址与端口</param>
/// <param name="taskID">任务ID</param>
/// <param name="devAddr">设备地址</param>
/// <param name="timeout">超时时间(毫秒)</param>
/// <returns>读取结果,包含接收到的数据包</returns>
public static async ValueTask<Result<RecvDataPackage>> ReadAddrByte(
IPEndPoint endPoint, int taskID, uint devAddr, int timeout = 1000)
{
return await ReadAddr(endPoint, taskID, devAddr, 1, timeout);
}
/// <summary> /// <summary>
/// 读取设备地址数据并校验结果 /// 读取设备地址数据并校验结果
/// </summary> /// </summary>
@@ -271,11 +257,11 @@ public class UDPClientPool
/// <param name="timeout">超时时间(毫秒)</param> /// <param name="timeout">超时时间(毫秒)</param>
/// <returns>校验结果true表示数据匹配期望值</returns> /// <returns>校验结果true表示数据匹配期望值</returns>
public static async ValueTask<Result<bool>> ReadAddr( public static async ValueTask<Result<bool>> ReadAddr(
IPEndPoint endPoint, int taskID, uint devAddr, UInt32 result, UInt32 resultMask, int timeout = 1000) IPEndPoint endPoint, int taskID, uint devAddr, UInt32 result, UInt32 resultMask, int timeout = 1000)
{ {
var address = endPoint.Address.ToString(); var address = endPoint.Address.ToString();
var ret = await ReadAddr(endPoint, taskID, devAddr, timeout); var ret = await ReadAddrByte(endPoint, taskID, devAddr, timeout);
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
if (!ret.Value.IsSuccessful) if (!ret.Value.IsSuccessful)
return new(new Exception($"Read device {address} address {devAddr} failed")); return new(new Exception($"Read device {address} address {devAddr} failed"));
@@ -311,7 +297,9 @@ public class UDPClientPool
/// <param name="timeout">超时时间(毫秒)</param> /// <param name="timeout">超时时间(毫秒)</param>
/// <returns>校验结果true表示在超时前数据匹配期望值</returns> /// <returns>校验结果true表示在超时前数据匹配期望值</returns>
public static async ValueTask<Result<bool>> ReadAddrWithWait( public static async ValueTask<Result<bool>> ReadAddrWithWait(
IPEndPoint endPoint, int taskID, uint devAddr, UInt32 result, UInt32 resultMask, int waittime = 100, int timeout = 1000) IPEndPoint endPoint, int taskID, uint devAddr,
UInt32 result, UInt32 resultMask,
int waittime = 100, int timeout = 1000)
{ {
var address = endPoint.Address.ToString(); var address = endPoint.Address.ToString();
@@ -324,7 +312,7 @@ public class UDPClientPool
await Task.Delay(waittime); await Task.Delay(waittime);
try try
{ {
var ret = await ReadAddr(endPoint, taskID, devAddr, Convert.ToInt32(timeleft.TotalMilliseconds)); var ret = await ReadAddrByte(endPoint, taskID, devAddr, Convert.ToInt32(timeleft.TotalMilliseconds));
if (!ret.IsSuccessful) return new(ret.Error); if (!ret.IsSuccessful) return new(ret.Error);
if (!ret.Value.IsSuccessful) if (!ret.Value.IsSuccessful)
return new(new Exception($"Read device {address} address {devAddr} failed")); return new(new Exception($"Read device {address} address {devAddr} failed"));
@@ -555,7 +543,7 @@ public class UDPClientPool
var resultData = new List<byte>(); var resultData = new List<byte>();
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
var ret = await ReadAddr(endPoint, taskID, addr[i], timeout); var ret = await ReadAddrByte(endPoint, taskID, addr[i], timeout);
if (!ret.IsSuccessful) if (!ret.IsSuccessful)
{ {
logger.Error($"ReadAddrSeq failed at index {i}: {ret.Error}"); logger.Error($"ReadAddrSeq failed at index {i}: {ret.Error}");
@@ -585,10 +573,11 @@ public class UDPClientPool
/// <param name="devAddr">设备地址</param> /// <param name="devAddr">设备地址</param>
/// <param name="data">要写入的32位数据</param> /// <param name="data">要写入的32位数据</param>
/// <param name="timeout">超时时间(毫秒)</param> /// <param name="timeout">超时时间(毫秒)</param>
/// <param name="progressId">进度报告器</param>
/// <returns>写入结果true表示写入成功</returns> /// <returns>写入结果true表示写入成功</returns>
public static async ValueTask<Result<bool>> WriteAddr( public static async ValueTask<Result<bool>> WriteAddr(
IPEndPoint endPoint, int taskID, UInt32 devAddr, IPEndPoint endPoint, int taskID, UInt32 devAddr,
UInt32 data, int timeout = 1000, ProgressReporter? progress = null) UInt32 data, int timeout = 1000, string progressId = "")
{ {
var ret = false; var ret = false;
var opts = new SendAddrPackOptions() var opts = new SendAddrPackOptions()
@@ -599,17 +588,18 @@ public class UDPClientPool
Address = devAddr, Address = devAddr,
IsWrite = true, IsWrite = true,
}; };
progress?.Report(20); _progressTracker.AdvanceProgress(progressId, 10);
// Write Register // Write Register
ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts)); ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts));
if (!ret) return new(new Exception("Send 1st address package failed!")); if (!ret) return new(new Exception("Send 1st address package failed!"));
progress?.Report(40); _progressTracker.AdvanceProgress(progressId, 10);
// Send Data Package // Send Data Package
ret = await UDPClientPool.SendDataPackAsync(endPoint, ret = await UDPClientPool.SendDataPackAsync(endPoint,
new SendDataPackage(Common.Number.NumberToBytes(data, 4).Value)); new SendDataPackage(Common.Number.NumberToBytes(data, 4).Value));
if (!ret) return new(new Exception("Send data package failed!")); if (!ret) return new(new Exception("Send data package failed!"));
progress?.Report(60); _progressTracker.AdvanceProgress(progressId, 10);
// Check Msg Bus // Check Msg Bus
if (!MsgBus.IsRunning) if (!MsgBus.IsRunning)
@@ -619,7 +609,7 @@ public class UDPClientPool
var udpWriteAck = await MsgBus.UDPServer.WaitForAckAsync( var udpWriteAck = await MsgBus.UDPServer.WaitForAckAsync(
endPoint.Address.ToString(), taskID, endPoint.Port, timeout); endPoint.Address.ToString(), taskID, endPoint.Port, timeout);
if (!udpWriteAck.IsSuccessful) return new(udpWriteAck.Error); if (!udpWriteAck.IsSuccessful) return new(udpWriteAck.Error);
progress?.Finish(); _progressTracker.AdvanceProgress(progressId, 10);
return udpWriteAck.Value.IsSuccessful; return udpWriteAck.Value.IsSuccessful;
} }
@@ -632,10 +622,11 @@ public class UDPClientPool
/// <param name="devAddr">设备地址</param> /// <param name="devAddr">设备地址</param>
/// <param name="dataArray">要写入的字节数组</param> /// <param name="dataArray">要写入的字节数组</param>
/// <param name="timeout">超时时间(毫秒)</param> /// <param name="timeout">超时时间(毫秒)</param>
/// <param name="progressId">进度报告器</param>
/// <returns>写入结果true表示写入成功</returns> /// <returns>写入结果true表示写入成功</returns>
public static async ValueTask<Result<bool>> WriteAddr( public static async ValueTask<Result<bool>> WriteAddr(
IPEndPoint endPoint, int taskID, UInt32 devAddr, IPEndPoint endPoint, int taskID, UInt32 devAddr,
byte[] dataArray, int timeout = 1000, ProgressReporter? progress = null) byte[] dataArray, int timeout = 1000, string progressId = "")
{ {
var ret = false; var ret = false;
var opts = new SendAddrPackOptions() var opts = new SendAddrPackOptions()
@@ -657,8 +648,6 @@ public class UDPClientPool
var writeTimes = hasRest ? var writeTimes = hasRest ?
dataArray.Length / (max4BytesPerRead * (32 / 8)) + 1 : dataArray.Length / (max4BytesPerRead * (32 / 8)) + 1 :
dataArray.Length / (max4BytesPerRead * (32 / 8)); dataArray.Length / (max4BytesPerRead * (32 / 8));
if (progress != null)
progress.ExpectedSteps = writeTimes;
for (var i = 0; i < writeTimes; i++) for (var i = 0; i < writeTimes; i++)
{ {
// Sperate Data Array // Sperate Data Array
@@ -688,10 +677,9 @@ public class UDPClientPool
if (!udpWriteAck.Value.IsSuccessful) if (!udpWriteAck.Value.IsSuccessful)
return false; return false;
progress?.Increase(); _progressTracker.AdvanceProgress(progressId, 1);
} }
progress?.Finish();
return true; return true;
} }

File diff suppressed because it is too large Load Diff

View File

@@ -4,13 +4,13 @@ import Dialog from "./components/Dialog.vue";
import { Alert, useAlertProvider } from "./components/Alert"; import { Alert, useAlertProvider } from "./components/Alert";
import { ref, provide, computed, onMounted } from "vue"; import { ref, provide, computed, onMounted } from "vue";
import { useRouter } from "vue-router"; import { useRouter } from "vue-router";
import { useThemeStore } from "./stores/theme";
const router = useRouter(); const router = useRouter();
const theme = useThemeStore();
// 主题切换状态管理 // 主题切换状态管理
const isDarkMode = ref( const isDarkMode = ref(theme.isDarkTheme());
window.matchMedia("(prefers-color-scheme: dark)").matches,
);
// Navbar显示状态管理 // Navbar显示状态管理
const showNavbar = ref(true); const showNavbar = ref(true);
@@ -46,6 +46,7 @@ const applyTheme = () => {
// 切换主题 // 切换主题
const toggleTheme = () => { const toggleTheme = () => {
isDarkMode.value = !isDarkMode.value; isDarkMode.value = !isDarkMode.value;
theme.toggleTheme();
applyTheme(); applyTheme();
}; };

View File

@@ -1,3 +1,6 @@
import { ResourceClient, ResourcePurpose } from "@/APIClient";
import { AuthManager } from "@/utils/AuthManager";
// 定义 diagram.json 的类型结构 // 定义 diagram.json 的类型结构
export interface DiagramData { export interface DiagramData {
version: number; version: number;
@@ -26,40 +29,43 @@ export interface DiagramPart {
// 连接类型定义 - 使用元组类型表示四元素数组 // 连接类型定义 - 使用元组类型表示四元素数组
export type ConnectionArray = [string, string, number, string[]]; export type ConnectionArray = [string, string, number, string[]];
import { AuthManager } from '@/utils/AuthManager';
// 解析连接字符串为组件ID和引脚ID // 解析连接字符串为组件ID和引脚ID
export function parseConnectionPin(connectionPin: string): { componentId: string; pinId: string } { export function parseConnectionPin(connectionPin: string): {
const [componentId, pinId] = connectionPin.split(':'); componentId: string;
pinId: string;
} {
const [componentId, pinId] = connectionPin.split(":");
return { componentId, pinId }; return { componentId, pinId };
} }
// 将连接数组转换为适用于渲染的格式 // 将连接数组转换为适用于渲染的格式
export function connectionArrayToWireItem( export function connectionArrayToWireItem(
connection: ConnectionArray, connection: ConnectionArray,
index: number, index: number,
startPos = { x: 0, y: 0 }, startPos = { x: 0, y: 0 },
endPos = { x: 0, y: 0 } endPos = { x: 0, y: 0 },
): WireItem { ): WireItem {
const [startPinStr, endPinStr, width, path] = connection; const [startPinStr, endPinStr, width, path] = connection;
const { componentId: startComponentId, pinId: startPinId } = parseConnectionPin(startPinStr); const { componentId: startComponentId, pinId: startPinId } =
const { componentId: endComponentId, pinId: endPinId } = parseConnectionPin(endPinStr); parseConnectionPin(startPinStr);
const { componentId: endComponentId, pinId: endPinId } =
parseConnectionPin(endPinStr);
return { return {
id: `wire-${index}`, id: `wire-${index}`,
startX: startPos.x, startX: startPos.x,
startY: startPos.y, startY: startPos.y,
endX: endPos.x, endX: endPos.x,
endY: endPos.y, endY: endPos.y,
startComponentId, startComponentId,
startPinId, startPinId,
endComponentId, endComponentId,
endPinId, endPinId,
strokeWidth: width, strokeWidth: width,
color: '#4a5568', // 默认颜色 color: "#4a5568", // 默认颜色
routingMode: 'path', routingMode: "path",
pathCommands: path, pathCommands: path,
showLabel: false showLabel: false,
}; };
} }
@@ -76,7 +82,7 @@ export interface WireItem {
endPinId?: string; endPinId?: string;
strokeWidth: number; strokeWidth: number;
color: string; color: string;
routingMode: 'orthogonal' | 'path'; routingMode: "orthogonal" | "path";
constraint?: string; constraint?: string;
pathCommands?: string[]; pathCommands?: string[];
showLabel: boolean; showLabel: boolean;
@@ -88,58 +94,64 @@ export async function loadDiagramData(examId?: string): Promise<DiagramData> {
// 如果提供了examId优先从API加载实验的diagram // 如果提供了examId优先从API加载实验的diagram
if (examId) { if (examId) {
try { try {
const resourceClient = AuthManager.createAuthenticatedResourceClient(); const resourceClient = AuthManager.createClient(ResourceClient);
// 获取diagram类型的资源列表 // 获取diagram类型的资源列表
const resources = await resourceClient.getResourceList(examId, 'canvas', 'template'); const resources = await resourceClient.getResourceList(
examId,
"canvas",
ResourcePurpose.Template,
);
if (resources && resources.length > 0) { if (resources && resources.length > 0) {
// 获取第一个diagram资源 // 获取第一个diagram资源
const diagramResource = resources[0]; const diagramResource = resources[0];
// 使用动态API获取资源文件内容 // 使用动态API获取资源文件内容
const response = await resourceClient.getResourceById(diagramResource.id); const response = await resourceClient.getResourceById(
diagramResource.id,
);
if (response && response.data) { if (response && response.data) {
const text = await response.data.text(); const text = await response.data.text();
const data = JSON.parse(text); const data = JSON.parse(text);
// 验证数据格式 // 验证数据格式
const validation = validateDiagramData(data); const validation = validateDiagramData(data);
if (validation.isValid) { if (validation.isValid) {
console.log('成功从API加载实验diagram:', examId); console.log("成功从API加载实验diagram:", examId);
return data; return data;
} else { } else {
console.warn('API返回的diagram数据格式无效:', validation.errors); console.warn("API返回的diagram数据格式无效:", validation.errors);
} }
} }
} else { } else {
console.log('未找到实验diagram资源使用默认加载方式'); console.log("未找到实验diagram资源使用默认加载方式");
} }
} catch (error) { } catch (error) {
console.warn('从API加载实验diagram失败使用默认加载方式:', error); console.warn("从API加载实验diagram失败使用默认加载方式:", error);
} }
} }
// 如果没有examId或API加载失败尝试从静态文件加载不再使用本地存储 // 如果没有examId或API加载失败尝试从静态文件加载不再使用本地存储
// 从静态文件加载(作为备选方案) // 从静态文件加载(作为备选方案)
const response = await fetch('/src/components/diagram.json'); const response = await fetch("/src/components/diagram.json");
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to load diagram.json: ${response.statusText}`); throw new Error(`Failed to load diagram.json: ${response.statusText}`);
} }
const data = await response.json(); const data = await response.json();
// 验证静态文件数据 // 验证静态文件数据
const validation = validateDiagramData(data); const validation = validateDiagramData(data);
if (validation.isValid) { if (validation.isValid) {
return data; return data;
} else { } else {
console.warn('静态diagram文件数据格式无效:', validation.errors); console.warn("静态diagram文件数据格式无效:", validation.errors);
throw new Error('所有diagram数据源都无效'); throw new Error("所有diagram数据源都无效");
} }
} catch (error) { } catch (error) {
console.error('Error loading diagram data:', error); console.error("Error loading diagram data:", error);
// 返回空的默认数据结构 // 返回空的默认数据结构
return createEmptyDiagram(); return createEmptyDiagram();
} }
@@ -149,33 +161,31 @@ export async function loadDiagramData(examId?: string): Promise<DiagramData> {
export function createEmptyDiagram(): DiagramData { export function createEmptyDiagram(): DiagramData {
return { return {
version: 1, version: 1,
author: 'user', author: "user",
editor: 'user', editor: "user",
parts: [], parts: [],
connections: [] connections: [],
}; };
} }
// 保存图表数据(已禁用本地存储) // 保存图表数据(已禁用本地存储)
export function saveDiagramData(data: DiagramData): void { export function saveDiagramData(data: DiagramData): void {
// 本地存储功能已禁用 - 不再保存到localStorage // 本地存储功能已禁用 - 不再保存到localStorage
console.debug('saveDiagramData called but localStorage saving is disabled'); console.debug("saveDiagramData called but localStorage saving is disabled");
} }
// 更新组件位置 // 更新组件位置
export function updatePartPosition( export function updatePartPosition(
data: DiagramData, data: DiagramData,
partId: string, partId: string,
x: number, x: number,
y: number y: number,
): DiagramData { ): DiagramData {
return { return {
...data, ...data,
parts: data.parts.map(part => parts: data.parts.map((part) =>
part.id === partId part.id === partId ? { ...part, x, y } : part,
? { ...part, x, y } ),
: part
)
}; };
} }
@@ -184,21 +194,21 @@ export function updatePartAttribute(
data: DiagramData, data: DiagramData,
partId: string, partId: string,
attrName: string, attrName: string,
value: any value: any,
): DiagramData { ): DiagramData {
return { return {
...data, ...data,
parts: data.parts.map(part => parts: data.parts.map((part) =>
part.id === partId part.id === partId
? { ? {
...part, ...part,
attrs: { attrs: {
...part.attrs, ...part.attrs,
[attrName]: value [attrName]: value,
} },
} }
: part : part,
) ),
}; };
} }
@@ -210,72 +220,79 @@ export function addConnection(
endComponentId: string, endComponentId: string,
endPinId: string, endPinId: string,
width: number = 2, width: number = 2,
path: string[] = [] path: string[] = [],
): DiagramData { ): DiagramData {
const newConnection: ConnectionArray = [ const newConnection: ConnectionArray = [
`${startComponentId}:${startPinId}`, `${startComponentId}:${startPinId}`,
`${endComponentId}:${endPinId}`, `${endComponentId}:${endPinId}`,
width, width,
path path,
]; ];
return { return {
...data, ...data,
connections: [...data.connections, newConnection] connections: [...data.connections, newConnection],
}; };
} }
// 删除连接 // 删除连接
export function deleteConnection( export function deleteConnection(
data: DiagramData, data: DiagramData,
connectionIndex: number connectionIndex: number,
): DiagramData { ): DiagramData {
return { return {
...data, ...data,
connections: data.connections.filter((_, index) => index !== connectionIndex) connections: data.connections.filter(
(_, index) => index !== connectionIndex,
),
}; };
} }
// 查找与组件关联的所有连接 // 查找与组件关联的所有连接
export function findConnectionsByPart( export function findConnectionsByPart(
data: DiagramData, data: DiagramData,
partId: string partId: string,
): { connection: ConnectionArray; index: number }[] { ): { connection: ConnectionArray; index: number }[] {
return data.connections return data.connections
.map((connection, index) => ({ connection, index })) .map((connection, index) => ({ connection, index }))
.filter(({ connection }) => { .filter(({ connection }) => {
const [startPin, endPin] = connection; const [startPin, endPin] = connection;
const startCompId = startPin.split(':')[0]; const startCompId = startPin.split(":")[0];
const endCompId = endPin.split(':')[0]; const endCompId = endPin.split(":")[0];
return startCompId === partId || endCompId === partId; return startCompId === partId || endCompId === partId;
}); });
} }
// 添加验证diagram.json文件的函数 // 添加验证diagram.json文件的函数
export function validateDiagramData(data: any): { isValid: boolean; errors: string[] } { export function validateDiagramData(data: any): {
isValid: boolean;
errors: string[];
} {
const errors: string[] = []; const errors: string[] = [];
// 检查版本号 // 检查版本号
if (!data.version) { if (!data.version) {
errors.push('缺少version字段'); errors.push("缺少version字段");
} }
// 检查parts数组 // 检查parts数组
if (!Array.isArray(data.parts)) { if (!Array.isArray(data.parts)) {
errors.push('parts字段不是数组'); errors.push("parts字段不是数组");
} else { } else {
// 验证parts中的每个对象 // 验证parts中的每个对象
data.parts.forEach((part: any, index: number) => { data.parts.forEach((part: any, index: number) => {
if (!part.id) errors.push(`parts[${index}]缺少id`); if (!part.id) errors.push(`parts[${index}]缺少id`);
if (!part.type) errors.push(`parts[${index}]缺少type`); if (!part.type) errors.push(`parts[${index}]缺少type`);
if (typeof part.x !== 'number') errors.push(`parts[${index}]缺少有效的x坐标`); if (typeof part.x !== "number")
if (typeof part.y !== 'number') errors.push(`parts[${index}]缺少有效的y坐标`); errors.push(`parts[${index}]缺少有效的x坐标`);
if (typeof part.y !== "number")
errors.push(`parts[${index}]缺少有效的y坐标`);
}); });
} }
// 检查connections数组 // 检查connections数组
if (!Array.isArray(data.connections)) { if (!Array.isArray(data.connections)) {
errors.push('connections字段不是数组'); errors.push("connections字段不是数组");
} else { } else {
// 验证connections中的每个数组 // 验证connections中的每个数组
data.connections.forEach((conn: any, index: number) => { data.connections.forEach((conn: any, index: number) => {
@@ -283,25 +300,25 @@ export function validateDiagramData(data: any): { isValid: boolean; errors: stri
errors.push(`connections[${index}]不是有效的连接数组`); errors.push(`connections[${index}]不是有效的连接数组`);
return; return;
} }
const [startPin, endPin, width] = conn; const [startPin, endPin, width] = conn;
if (typeof startPin !== 'string' || !startPin.includes(':')) { if (typeof startPin !== "string" || !startPin.includes(":")) {
errors.push(`connections[${index}]的起始针脚格式无效`); errors.push(`connections[${index}]的起始针脚格式无效`);
} }
if (typeof endPin !== 'string' || !endPin.includes(':')) { if (typeof endPin !== "string" || !endPin.includes(":")) {
errors.push(`connections[${index}]的结束针脚格式无效`); errors.push(`connections[${index}]的结束针脚格式无效`);
} }
if (typeof width !== 'number') { if (typeof width !== "number") {
errors.push(`connections[${index}]的宽度不是有效的数字`); errors.push(`connections[${index}]的宽度不是有效的数字`);
} }
}); });
} }
return { return {
isValid: errors.length === 0, isValid: errors.length === 0,
errors errors,
}; };
} }

View File

@@ -31,7 +31,7 @@ export const previewSizes: Record<string, number> = {
Switch: 0.35, Switch: 0.35,
Pin: 0.8, Pin: 0.8,
SMT_LED: 0.7, SMT_LED: 0.7,
SevenSegmentDisplay: 0.4, SevenSegmentDisplayUltimate: 0.4,
HDMI: 0.5, HDMI: 0.5,
DDR: 0.5, DDR: 0.5,
ETH: 0.5, ETH: 0.5,
@@ -50,7 +50,7 @@ export const availableComponents: ComponentConfig[] = [
{ type: "Switch", name: "开关" }, { type: "Switch", name: "开关" },
{ type: "Pin", name: "引脚" }, { type: "Pin", name: "引脚" },
{ type: "SMT_LED", name: "贴片LED" }, { type: "SMT_LED", name: "贴片LED" },
{ type: "SevenSegmentDisplay", name: "数码管" }, { type: "SevenSegmentDisplayUltimate", name: "数码管" },
{ type: "HDMI", name: "HDMI接口" }, { type: "HDMI", name: "HDMI接口" },
{ type: "DDR", name: "DDR内存" }, { type: "DDR", name: "DDR内存" },
{ type: "ETH", name: "以太网接口" }, { type: "ETH", name: "以太网接口" },

View File

@@ -31,8 +31,16 @@ export type Channel = {
// 全局模式选项 // 全局模式选项
const globalModes = [ const globalModes = [
{value: GlobalCaptureMode.AND,label: "AND",description: "所有条件都满足时触发",}, {
{value: GlobalCaptureMode.OR,label: "OR",description: "任一条件满足时触发",}, value: GlobalCaptureMode.AND,
label: "AND",
description: "所有条件都满足时触发",
},
{
value: GlobalCaptureMode.OR,
label: "OR",
description: "任一条件满足时触发",
},
{ value: GlobalCaptureMode.NAND, label: "NAND", description: "AND的非" }, { value: GlobalCaptureMode.NAND, label: "NAND", description: "AND的非" },
{ value: GlobalCaptureMode.NOR, label: "NOR", description: "OR的非" }, { value: GlobalCaptureMode.NOR, label: "NOR", description: "OR的非" },
]; ];
@@ -70,21 +78,53 @@ const channelDivOptions = [
]; ];
const ClockDivOptions = [ const ClockDivOptions = [
{ value: AnalyzerClockDiv.DIV1, label: "120MHz", description: "采样频率120MHz" }, {
{ value: AnalyzerClockDiv.DIV2, label: "60MHz", description: "采样频率60MHz" }, value: AnalyzerClockDiv.DIV1,
{ value: AnalyzerClockDiv.DIV4, label: "30MHz", description: "采样频率30MHz" }, label: "120MHz",
{ value: AnalyzerClockDiv.DIV8, label: "15MHz", description: "采样频率15MHz" }, description: "采样频率120MHz",
{ value: AnalyzerClockDiv.DIV16, label: "7.5MHz", description: "采样频率7.5MHz" }, },
{ value: AnalyzerClockDiv.DIV32, label: "3.75MHz", description: "采样频率3.75MHz" }, {
{ value: AnalyzerClockDiv.DIV64, label: "1.875MHz", description: "采样频率1.875MHz" }, value: AnalyzerClockDiv.DIV2,
{ value: AnalyzerClockDiv.DIV128, label: "937.5KHz", description: "采样频率937.5KHz" }, label: "60MHz",
description: "采样频率60MHz",
},
{
value: AnalyzerClockDiv.DIV4,
label: "30MHz",
description: "采样频率30MHz",
},
{
value: AnalyzerClockDiv.DIV8,
label: "15MHz",
description: "采样频率15MHz",
},
{
value: AnalyzerClockDiv.DIV16,
label: "7.5MHz",
description: "采样频率7.5MHz",
},
{
value: AnalyzerClockDiv.DIV32,
label: "3.75MHz",
description: "采样频率3.75MHz",
},
{
value: AnalyzerClockDiv.DIV64,
label: "1.875MHz",
description: "采样频率1.875MHz",
},
{
value: AnalyzerClockDiv.DIV128,
label: "937.5KHz",
description: "采样频率937.5KHz",
},
]; ];
// 捕获深度限制常量 // 捕获深度限制常量
const CAPTURE_LENGTH_MIN = 1024; // 最小捕获深度 1024 const CAPTURE_LENGTH_MIN = 1024; // 最小捕获深度 1024
const CAPTURE_LENGTH_MAX = 0x10000000 - 0x01000000; // 最大捕获深度 const CAPTURE_LENGTH_MAX = 0x10000000 - 0x01000000; // 最大捕获深度
// 预捕获深度限制常量 // 预捕获深度限制常量
const PRE_CAPTURE_LENGTH_MIN = 2; // 最小预捕获深度 2 const PRE_CAPTURE_LENGTH_MIN = 2; // 最小预捕获深度 2
// 默认颜色数组 // 默认颜色数组
@@ -170,40 +210,64 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
// 转换通道数字到枚举值 // 转换通道数字到枚举值
const getChannelDivEnum = (channelCount: number): AnalyzerChannelDiv => { const getChannelDivEnum = (channelCount: number): AnalyzerChannelDiv => {
switch (channelCount) { switch (channelCount) {
case 1: return AnalyzerChannelDiv.ONE; case 1:
case 2: return AnalyzerChannelDiv.TWO; return AnalyzerChannelDiv.ONE;
case 4: return AnalyzerChannelDiv.FOUR; case 2:
case 8: return AnalyzerChannelDiv.EIGHT; return AnalyzerChannelDiv.TWO;
case 16: return AnalyzerChannelDiv.XVI; case 4:
case 32: return AnalyzerChannelDiv.XXXII; return AnalyzerChannelDiv.FOUR;
default: return AnalyzerChannelDiv.EIGHT; case 8:
return AnalyzerChannelDiv.EIGHT;
case 16:
return AnalyzerChannelDiv.XVI;
case 32:
return AnalyzerChannelDiv.XXXII;
default:
return AnalyzerChannelDiv.EIGHT;
} }
}; };
// 验证捕获深度 // 验证捕获深度
const validateCaptureLength = (value: number): { valid: boolean; message?: string } => { const validateCaptureLength = (
value: number,
): { valid: boolean; message?: string } => {
if (!Number.isInteger(value)) { if (!Number.isInteger(value)) {
return { valid: false, message: "捕获深度必须是整数" }; return { valid: false, message: "捕获深度必须是整数" };
} }
if (value < CAPTURE_LENGTH_MIN) { if (value < CAPTURE_LENGTH_MIN) {
return { valid: false, message: `捕获深度不能小于 ${CAPTURE_LENGTH_MIN}` }; return {
valid: false,
message: `捕获深度不能小于 ${CAPTURE_LENGTH_MIN}`,
};
} }
if (value > CAPTURE_LENGTH_MAX) { if (value > CAPTURE_LENGTH_MAX) {
return { valid: false, message: `捕获深度不能大于 ${CAPTURE_LENGTH_MAX.toLocaleString()}` }; return {
valid: false,
message: `捕获深度不能大于 ${CAPTURE_LENGTH_MAX.toLocaleString()}`,
};
} }
return { valid: true }; return { valid: true };
}; };
// 验证预捕获深度 // 验证预捕获深度
const validatePreCaptureLength = (value: number, currentCaptureLength: number): { valid: boolean; message?: string } => { const validatePreCaptureLength = (
value: number,
currentCaptureLength: number,
): { valid: boolean; message?: string } => {
if (!Number.isInteger(value)) { if (!Number.isInteger(value)) {
return { valid: false, message: "预捕获深度必须是整数" }; return { valid: false, message: "预捕获深度必须是整数" };
} }
if (value < PRE_CAPTURE_LENGTH_MIN) { if (value < PRE_CAPTURE_LENGTH_MIN) {
return { valid: false, message: `预捕获深度不能小于 ${PRE_CAPTURE_LENGTH_MIN}` }; return {
valid: false,
message: `预捕获深度不能小于 ${PRE_CAPTURE_LENGTH_MIN}`,
};
} }
if (value >= currentCaptureLength) { if (value >= currentCaptureLength) {
return { valid: false, message: `预捕获深度不能大于等于捕获深度 (${currentCaptureLength})` }; return {
valid: false,
message: `预捕获深度不能大于等于捕获深度 (${currentCaptureLength})`,
};
} }
return { valid: true }; return { valid: true };
}; };
@@ -215,13 +279,13 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
alert?.error(validation.message!, 3000); alert?.error(validation.message!, 3000);
return false; return false;
} }
// 检查预捕获深度是否仍然有效 // 检查预捕获深度是否仍然有效
if (preCaptureLength.value >= value) { if (preCaptureLength.value >= value) {
preCaptureLength.value = Math.max(0, value - 1); preCaptureLength.value = Math.max(0, value - 1);
alert?.warn(`预捕获深度已自动调整为 ${preCaptureLength.value}`, 3000); alert?.warn(`预捕获深度已自动调整为 ${preCaptureLength.value}`, 3000);
} }
captureLength.value = value; captureLength.value = value;
return true; return true;
}; };
@@ -233,7 +297,7 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
alert?.error(validation.message!, 3000); alert?.error(validation.message!, 3000);
return false; return false;
} }
preCaptureLength.value = value; preCaptureLength.value = value;
return true; return true;
}; };
@@ -241,12 +305,12 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
// 设置通道组 // 设置通道组
const setChannelDiv = (channelCount: number) => { const setChannelDiv = (channelCount: number) => {
// 验证通道数量是否有效 // 验证通道数量是否有效
if (!channelDivOptions.find(option => option.value === channelCount)) { if (!channelDivOptions.find((option) => option.value === channelCount)) {
console.error(`无效的通道组设置: ${channelCount}`); console.error(`无效的通道组设置: ${channelCount}`);
return; return;
} }
currentChannelDiv.value = channelCount; currentChannelDiv.value = channelCount;
// 禁用所有通道 // 禁用所有通道
channels.forEach((channel) => { channels.forEach((channel) => {
channel.enabled = false; channel.enabled = false;
@@ -257,7 +321,9 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
channels[i].enabled = true; channels[i].enabled = true;
} }
const option = channelDivOptions.find(opt => opt.value === channelCount); const option = channelDivOptions.find(
(opt) => opt.value === channelCount,
);
alert?.success(`已设置为${option?.label}`, 2000); alert?.success(`已设置为${option?.label}`, 2000);
}; };
@@ -294,7 +360,7 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
const getCaptureData = async () => { const getCaptureData = async () => {
try { try {
const client = AuthManager.createAuthenticatedLogicAnalyzerClient(); const client = AuthManager.createClient(LogicAnalyzerClient);
// 获取捕获数据,使用当前设置的捕获长度 // 获取捕获数据,使用当前设置的捕获长度
const base64Data = await client.getCaptureData(captureLength.value); const base64Data = await client.getCaptureData(captureLength.value);
@@ -308,7 +374,7 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
// 根据当前通道数量解析数据 // 根据当前通道数量解析数据
const channelCount = currentChannelDiv.value; const channelCount = currentChannelDiv.value;
const timeStepNs = currentSamplePeriodNs.value; const timeStepNs = currentSamplePeriodNs.value;
let sampleCount: number; let sampleCount: number;
let x: number[]; let x: number[];
let y: number[][]; let y: number[][];
@@ -316,19 +382,16 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
if (channelCount === 1) { if (channelCount === 1) {
// 1通道每个字节包含8个时间单位的数据 // 1通道每个字节包含8个时间单位的数据
sampleCount = bytes.length * 8; sampleCount = bytes.length * 8;
// 创建时间轴 // 创建时间轴
x = Array.from( x = Array.from(
{ length: sampleCount }, { length: sampleCount },
(_, i) => (i * timeStepNs) / 1000, (_, i) => (i * timeStepNs) / 1000,
); // 转换为微秒 ); // 转换为微秒
// 创建通道数据数组 // 创建通道数据数组
y = Array.from( y = Array.from({ length: 1 }, () => new Array(sampleCount));
{ length: 1 },
() => new Array(sampleCount),
);
// 解析数据每个字节的8个位对应8个时间单位 // 解析数据每个字节的8个位对应8个时间单位
for (let byteIndex = 0; byteIndex < bytes.length; byteIndex++) { for (let byteIndex = 0; byteIndex < bytes.length; byteIndex++) {
const byte = bytes[byteIndex]; const byte = bytes[byteIndex];
@@ -340,19 +403,16 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
} else if (channelCount === 2) { } else if (channelCount === 2) {
// 2通道每个字节包含4个时间单位的数据 // 2通道每个字节包含4个时间单位的数据
sampleCount = bytes.length * 4; sampleCount = bytes.length * 4;
// 创建时间轴 // 创建时间轴
x = Array.from( x = Array.from(
{ length: sampleCount }, { length: sampleCount },
(_, i) => (i * timeStepNs) / 1000, (_, i) => (i * timeStepNs) / 1000,
); // 转换为微秒 ); // 转换为微秒
// 创建通道数据数组 // 创建通道数据数组
y = Array.from( y = Array.from({ length: 2 }, () => new Array(sampleCount));
{ length: 2 },
() => new Array(sampleCount),
);
// 解析数据每个字节的8个位对应4个时间单位的2通道数据 // 解析数据每个字节的8个位对应4个时间单位的2通道数据
// 位分布:[T3_CH1, T3_CH0, T2_CH1, T2_CH0, T1_CH1, T1_CH0, T0_CH1, T0_CH0] // 位分布:[T3_CH1, T3_CH0, T2_CH1, T2_CH0, T1_CH1, T1_CH0, T0_CH1, T0_CH0]
for (let byteIndex = 0; byteIndex < bytes.length; byteIndex++) { for (let byteIndex = 0; byteIndex < bytes.length; byteIndex++) {
@@ -360,37 +420,34 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
for (let timeUnit = 0; timeUnit < 4; timeUnit++) { for (let timeUnit = 0; timeUnit < 4; timeUnit++) {
const timeIndex = byteIndex * 4 + timeUnit; const timeIndex = byteIndex * 4 + timeUnit;
const bitOffset = timeUnit * 2; const bitOffset = timeUnit * 2;
y[0][timeIndex] = (byte >> bitOffset) & 1; // CH0 y[0][timeIndex] = (byte >> bitOffset) & 1; // CH0
y[1][timeIndex] = (byte >> (bitOffset + 1)) & 1; // CH1 y[1][timeIndex] = (byte >> (bitOffset + 1)) & 1; // CH1
} }
} }
} else if (channelCount === 4) { } else if (channelCount === 4) {
// 4通道每个字节包含2个时间单位的数据 // 4通道每个字节包含2个时间单位的数据
sampleCount = bytes.length * 2; sampleCount = bytes.length * 2;
// 创建时间轴 // 创建时间轴
x = Array.from( x = Array.from(
{ length: sampleCount }, { length: sampleCount },
(_, i) => (i * timeStepNs) / 1000, (_, i) => (i * timeStepNs) / 1000,
); // 转换为微秒 ); // 转换为微秒
// 创建通道数据数组 // 创建通道数据数组
y = Array.from( y = Array.from({ length: 4 }, () => new Array(sampleCount));
{ length: 4 },
() => new Array(sampleCount),
);
// 解析数据每个字节的8个位对应2个时间单位的4通道数据 // 解析数据每个字节的8个位对应2个时间单位的4通道数据
// 位分布:[T1_CH3, T1_CH2, T1_CH1, T1_CH0, T0_CH3, T0_CH2, T0_CH1, T0_CH0] // 位分布:[T1_CH3, T1_CH2, T1_CH1, T1_CH0, T0_CH3, T0_CH2, T0_CH1, T0_CH0]
for (let byteIndex = 0; byteIndex < bytes.length; byteIndex++) { for (let byteIndex = 0; byteIndex < bytes.length; byteIndex++) {
const byte = bytes[byteIndex]; const byte = bytes[byteIndex];
// 处理第一个时间单位低4位 // 处理第一个时间单位低4位
const timeIndex1 = byteIndex * 2; const timeIndex1 = byteIndex * 2;
for (let channel = 0; channel < 4; channel++) { for (let channel = 0; channel < 4; channel++) {
y[channel][timeIndex1] = (byte >> channel) & 1; y[channel][timeIndex1] = (byte >> channel) & 1;
} }
// 处理第二个时间单位高4位 // 处理第二个时间单位高4位
const timeIndex2 = byteIndex * 2 + 1; const timeIndex2 = byteIndex * 2 + 1;
for (let channel = 0; channel < 4; channel++) { for (let channel = 0; channel < 4; channel++) {
@@ -400,19 +457,16 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
} else if (channelCount === 8) { } else if (channelCount === 8) {
// 8通道每个字节包含1个时间单位的8个通道数据 // 8通道每个字节包含1个时间单位的8个通道数据
sampleCount = bytes.length; sampleCount = bytes.length;
// 创建时间轴 // 创建时间轴
x = Array.from( x = Array.from(
{ length: sampleCount }, { length: sampleCount },
(_, i) => (i * timeStepNs) / 1000, (_, i) => (i * timeStepNs) / 1000,
); // 转换为微秒 ); // 转换为微秒
// 创建8个通道的数据 // 创建8个通道的数据
y = Array.from( y = Array.from({ length: 8 }, () => new Array(sampleCount));
{ length: 8 },
() => new Array(sampleCount),
);
// 解析每个字节的8个位到对应通道 // 解析每个字节的8个位到对应通道
for (let i = 0; i < sampleCount; i++) { for (let i = 0; i < sampleCount; i++) {
const byte = bytes[i]; const byte = bytes[i];
@@ -424,30 +478,27 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
} else if (channelCount === 16) { } else if (channelCount === 16) {
// 16通道每2个字节包含1个时间单位的16个通道数据 // 16通道每2个字节包含1个时间单位的16个通道数据
sampleCount = bytes.length / 2; sampleCount = bytes.length / 2;
// 创建时间轴 // 创建时间轴
x = Array.from( x = Array.from(
{ length: sampleCount }, { length: sampleCount },
(_, i) => (i * timeStepNs) / 1000, (_, i) => (i * timeStepNs) / 1000,
); // 转换为微秒 ); // 转换为微秒
// 创建16个通道的数据 // 创建16个通道的数据
y = Array.from( y = Array.from({ length: 16 }, () => new Array(sampleCount));
{ length: 16 },
() => new Array(sampleCount),
);
// 解析数据每2个字节为一个时间单位 // 解析数据每2个字节为一个时间单位
for (let timeIndex = 0; timeIndex < sampleCount; timeIndex++) { for (let timeIndex = 0; timeIndex < sampleCount; timeIndex++) {
const byteIndex = timeIndex * 2; const byteIndex = timeIndex * 2;
const byte1 = bytes[byteIndex]; // [7:0] const byte1 = bytes[byteIndex]; // [7:0]
const byte2 = bytes[byteIndex + 1]; // [15:8] const byte2 = bytes[byteIndex + 1]; // [15:8]
// 处理低8位通道 [7:0] // 处理低8位通道 [7:0]
for (let channel = 0; channel < 8; channel++) { for (let channel = 0; channel < 8; channel++) {
y[channel][timeIndex] = (byte1 >> channel) & 1; y[channel][timeIndex] = (byte1 >> channel) & 1;
} }
// 处理高8位通道 [15:8] // 处理高8位通道 [15:8]
for (let channel = 0; channel < 8; channel++) { for (let channel = 0; channel < 8; channel++) {
y[channel + 8][timeIndex] = (byte2 >> channel) & 1; y[channel + 8][timeIndex] = (byte2 >> channel) & 1;
@@ -456,42 +507,39 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
} else if (channelCount === 32) { } else if (channelCount === 32) {
// 32通道每4个字节包含1个时间单位的32个通道数据 // 32通道每4个字节包含1个时间单位的32个通道数据
sampleCount = bytes.length / 4; sampleCount = bytes.length / 4;
// 创建时间轴 // 创建时间轴
x = Array.from( x = Array.from(
{ length: sampleCount }, { length: sampleCount },
(_, i) => (i * timeStepNs) / 1000, (_, i) => (i * timeStepNs) / 1000,
); // 转换为微秒 ); // 转换为微秒
// 创建32个通道的数据 // 创建32个通道的数据
y = Array.from( y = Array.from({ length: 32 }, () => new Array(sampleCount));
{ length: 32 },
() => new Array(sampleCount),
);
// 解析数据每4个字节为一个时间单位 // 解析数据每4个字节为一个时间单位
for (let timeIndex = 0; timeIndex < sampleCount; timeIndex++) { for (let timeIndex = 0; timeIndex < sampleCount; timeIndex++) {
const byteIndex = timeIndex * 4; const byteIndex = timeIndex * 4;
const byte1 = bytes[byteIndex]; // [7:0] const byte1 = bytes[byteIndex]; // [7:0]
const byte2 = bytes[byteIndex + 1]; // [15:8] const byte2 = bytes[byteIndex + 1]; // [15:8]
const byte3 = bytes[byteIndex + 2]; // [23:16] const byte3 = bytes[byteIndex + 2]; // [23:16]
const byte4 = bytes[byteIndex + 3]; // [31:24] const byte4 = bytes[byteIndex + 3]; // [31:24]
// 处理 [7:0] // 处理 [7:0]
for (let channel = 0; channel < 8; channel++) { for (let channel = 0; channel < 8; channel++) {
y[channel][timeIndex] = (byte1 >> channel) & 1; y[channel][timeIndex] = (byte1 >> channel) & 1;
} }
// 处理 [15:8] // 处理 [15:8]
for (let channel = 0; channel < 8; channel++) { for (let channel = 0; channel < 8; channel++) {
y[channel + 8][timeIndex] = (byte2 >> channel) & 1; y[channel + 8][timeIndex] = (byte2 >> channel) & 1;
} }
// 处理 [23:16] // 处理 [23:16]
for (let channel = 0; channel < 8; channel++) { for (let channel = 0; channel < 8; channel++) {
y[channel + 16][timeIndex] = (byte3 >> channel) & 1; y[channel + 16][timeIndex] = (byte3 >> channel) & 1;
} }
// 处理 [31:24] // 处理 [31:24]
for (let channel = 0; channel < 8; channel++) { for (let channel = 0; channel < 8; channel++) {
y[channel + 24][timeIndex] = (byte4 >> channel) & 1; y[channel + 24][timeIndex] = (byte4 >> channel) & 1;
@@ -525,11 +573,11 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
isCapturing.value = true; isCapturing.value = true;
const release = await operationMutex.acquire(); const release = await operationMutex.acquire();
try { try {
const client = AuthManager.createAuthenticatedLogicAnalyzerClient(); const client = AuthManager.createClient(LogicAnalyzerClient);
// 1. 先应用配置 // 1. 先应用配置
alert?.info("正在应用配置...", 2000); alert?.info("正在应用配置...", 2000);
// 准备配置数据 - 包含所有32个通道未启用的通道设置为默认值 // 准备配置数据 - 包含所有32个通道未启用的通道设置为默认值
const allSignals = signalConfigs.map((signal, index) => { const allSignals = signalConfigs.map((signal, index) => {
if (channels[index].enabled) { if (channels[index].enabled) {
@@ -632,7 +680,7 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
const release = await operationMutex.acquire(); const release = await operationMutex.acquire();
try { try {
const client = AuthManager.createAuthenticatedLogicAnalyzerClient(); const client = AuthManager.createClient(LogicAnalyzerClient);
// 执行强制捕获来停止当前捕获 // 执行强制捕获来停止当前捕获
const forceSuccess = await client.setCaptureMode(false, false); const forceSuccess = await client.setCaptureMode(false, false);
@@ -661,7 +709,7 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
const release = await operationMutex.acquire(); const release = await operationMutex.acquire();
try { try {
const client = AuthManager.createAuthenticatedLogicAnalyzerClient(); const client = AuthManager.createClient(LogicAnalyzerClient);
// 执行强制捕获来停止当前捕获 // 执行强制捕获来停止当前捕获
const forceSuccess = await client.setCaptureMode(true, true); const forceSuccess = await client.setCaptureMode(true, true);
@@ -677,7 +725,7 @@ const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
`强制捕获失败: ${error instanceof Error ? error.message : "未知错误"}`, `强制捕获失败: ${error instanceof Error ? error.message : "未知错误"}`,
3000, 3000,
); );
} finally{ } finally {
release(); release();
} }
}; };

View File

@@ -0,0 +1,33 @@
<script setup lang="ts">
import { ref } from "vue";
import { MdEditor } from "md-editor-v3";
import "md-editor-v3/lib/style.css";
import { useThemeStore } from "@/stores/theme";
const theme = useThemeStore();
const text = ref("# Hello Editor");
async function handleSaveEvent(v: string, h: Promise<string>) {}
async function loadMarkdownFromString(markdown: string) {
text.value = markdown;
}
async function loadMarkdownFromUrl(url: string) {
const response = await fetch(url);
const markdown = await response.text();
text.value = markdown;
}
defineExpose({
loadMarkdownFromString,
loadMarkdownFromUrl,
});
</script>
<template>
<MdEditor v-model="text" :theme="theme.currentMode" />
</template>
<style lang="postcss" scoped></style>

File diff suppressed because it is too large Load Diff

View File

@@ -44,7 +44,7 @@
</router-link> </router-link>
</li> </li>
<li class="my-1 hover:translate-x-1 transition-all duration-300"> <li class="my-1 hover:translate-x-1 transition-all duration-300">
<router-link to="/markdown-test" class="text-base font-medium"> <router-link to="/markdown" class="text-base font-medium">
<FileText class="icon" /> <FileText class="icon" />
Markdown测试 Markdown测试
</router-link> </router-link>
@@ -145,6 +145,7 @@ import {
ChevronDownIcon, ChevronDownIcon,
} from "lucide-vue-next"; } from "lucide-vue-next";
import { AuthManager } from "@/utils/AuthManager"; import { AuthManager } from "@/utils/AuthManager";
import { DataClient } from "@/APIClient";
const router = useRouter(); const router = useRouter();
@@ -158,7 +159,7 @@ const loadUserInfo = async () => {
try { try {
const authenticated = await AuthManager.isAuthenticated(); const authenticated = await AuthManager.isAuthenticated();
if (authenticated) { if (authenticated) {
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
const userInfo = await client.getUserInfo(); const userInfo = await client.getUserInfo();
userName.value = userInfo.name; userName.value = userInfo.name;
isLoggedIn.value = true; isLoggedIn.value = true;

View File

@@ -4,6 +4,7 @@ import { Mutex } from "async-mutex";
import { import {
OscilloscopeFullConfig, OscilloscopeFullConfig,
OscilloscopeDataResponse, OscilloscopeDataResponse,
OscilloscopeApiClient,
} from "@/APIClient"; } from "@/APIClient";
import { AuthManager } from "@/utils/AuthManager"; import { AuthManager } from "@/utils/AuthManager";
import { useAlertStore } from "@/components/Alert"; import { useAlertStore } from "@/components/Alert";
@@ -31,257 +32,269 @@ const DEFAULT_CONFIG: OscilloscopeFullConfig = new OscilloscopeFullConfig({
}); });
// 采样频率常量(后端返回) // 采样频率常量(后端返回)
const [useProvideOscilloscope, useOscilloscopeState] = createInjectionState(() => { const [useProvideOscilloscope, useOscilloscopeState] = createInjectionState(
const oscData = shallowRef<OscilloscopeDataType>(); () => {
const alert = useRequiredInjection(useAlertStore); const oscData = shallowRef<OscilloscopeDataType>();
const alert = useRequiredInjection(useAlertStore);
// 互斥锁 // 互斥锁
const operationMutex = new Mutex(); const operationMutex = new Mutex();
// 状态 // 状态
const isApplying = ref(false); const isApplying = ref(false);
const isCapturing = ref(false); const isCapturing = ref(false);
// 配置 // 配置
const config = reactive<OscilloscopeFullConfig>(new OscilloscopeFullConfig({ ...DEFAULT_CONFIG })); const config = reactive<OscilloscopeFullConfig>(
new OscilloscopeFullConfig({ ...DEFAULT_CONFIG }),
);
// 采样点数(由后端数据决定) // 采样点数(由后端数据决定)
const sampleCount = ref(0); const sampleCount = ref(0);
// 采样周期ns由adFrequency计算 // 采样周期ns由adFrequency计算
const samplePeriodNs = computed(() => const samplePeriodNs = computed(() =>
oscData.value?.adFrequency ? 1_000_000_000 / oscData.value.adFrequency : 200 oscData.value?.adFrequency
); ? 1_000_000_000 / oscData.value.adFrequency
: 200,
);
// 应用配置 // 应用配置
const applyConfiguration = async () => { const applyConfiguration = async () => {
if (operationMutex.isLocked()) { if (operationMutex.isLocked()) {
alert.warn("有其他操作正在进行中,请稍后再试", 3000); alert.warn("有其他操作正在进行中,请稍后再试", 3000);
return; return;
}
const release = await operationMutex.acquire();
isApplying.value = true;
try {
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
const success = await client.initialize({ ...config });
if (success) {
alert.success("示波器配置已应用", 2000);
} else {
throw new Error("应用失败");
} }
} catch (error) { const release = await operationMutex.acquire();
alert.error("应用配置失败", 3000); isApplying.value = true;
} finally { try {
isApplying.value = false; const client = AuthManager.createClient(OscilloscopeApiClient);
release(); const success = await client.initialize({ ...config });
} if (success) {
}; alert.success("示波器配置已应用", 2000);
} else {
// 重置配置 throw new Error("应用失败");
const resetConfiguration = () => { }
Object.assign(config, { ...DEFAULT_CONFIG }); } catch (error) {
alert.info("配置已重置", 2000); alert.error("应用配置失败", 3000);
}; } finally {
isApplying.value = false;
const clearOscilloscopeData = () => { release();
oscData.value = undefined;
}
// 获取数据
const getOscilloscopeData = async () => {
try {
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
const resp: OscilloscopeDataResponse = await client.getData();
// 解析波形数据
const binaryString = atob(resp.waveformData);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
} }
sampleCount.value = bytes.length; };
// 构建时间轴 // 重置配置
const resetConfiguration = () => {
Object.assign(config, { ...DEFAULT_CONFIG });
alert.info("配置已重置", 2000);
};
const clearOscilloscopeData = () => {
oscData.value = undefined;
};
// 获取数据
const getOscilloscopeData = async () => {
try {
const client = AuthManager.createClient(OscilloscopeApiClient);
const resp: OscilloscopeDataResponse = await client.getData();
// 解析波形数据
const binaryString = atob(resp.waveformData);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
sampleCount.value = bytes.length;
// 构建时间轴
const x = Array.from(
{ length: bytes.length },
(_, i) => (i * samplePeriodNs.value) / 1000, // us
);
const y = Array.from(bytes);
oscData.value = {
x,
y,
xUnit: "us",
yUnit: "V",
adFrequency: resp.adFrequency,
adVpp: resp.adVpp,
adMax: resp.adMax,
adMin: resp.adMin,
};
} catch (error) {
alert.error("获取示波器数据失败", 3000);
}
};
// 定时器引用
let refreshIntervalId: number | undefined;
// 刷新间隔(毫秒),可根据需要调整
const refreshIntervalMs = ref(1000);
// 定时刷新函数
const startAutoRefresh = () => {
if (refreshIntervalId !== undefined) return;
refreshIntervalId = window.setInterval(async () => {
await refreshRAM();
await getOscilloscopeData();
}, refreshIntervalMs.value);
};
const stopAutoRefresh = () => {
if (refreshIntervalId !== undefined) {
clearInterval(refreshIntervalId);
refreshIntervalId = undefined;
isCapturing.value = false;
}
};
// 启动捕获
const startCapture = async () => {
if (operationMutex.isLocked()) {
alert.warn("有其他操作正在进行中,请稍后再试", 3000);
return;
}
isCapturing.value = true;
const release = await operationMutex.acquire();
try {
const client = AuthManager.createClient(OscilloscopeApiClient);
const started = await client.startCapture();
if (!started) throw new Error("无法启动捕获");
alert.info("开始捕获...", 2000);
// 启动定时刷新
startAutoRefresh();
} catch (error) {
alert.error("捕获失败", 3000);
isCapturing.value = false;
stopAutoRefresh();
} finally {
release();
}
};
// 停止捕获
const stopCapture = async () => {
if (!isCapturing.value) {
alert.warn("当前没有正在进行的捕获操作", 2000);
return;
}
isCapturing.value = false;
stopAutoRefresh();
const release = await operationMutex.acquire();
try {
const client = AuthManager.createClient(OscilloscopeApiClient);
const stopped = await client.stopCapture();
if (!stopped) throw new Error("无法停止捕获");
alert.info("捕获已停止", 2000);
} catch (error) {
alert.error("停止捕获失败", 3000);
} finally {
release();
}
};
// 更新触发参数
const updateTrigger = async (level: number, risingEdge: boolean) => {
const client = AuthManager.createClient(OscilloscopeApiClient);
try {
const ok = await client.updateTrigger(level, risingEdge);
if (ok) {
config.triggerLevel = level;
config.triggerRisingEdge = risingEdge;
alert.success("触发参数已更新", 2000);
} else {
throw new Error();
}
} catch {
alert.error("更新触发参数失败", 2000);
}
};
// 更新采样参数
const updateSampling = async (
horizontalShift: number,
decimationRate: number,
) => {
const client = AuthManager.createClient(OscilloscopeApiClient);
try {
const ok = await client.updateSampling(horizontalShift, decimationRate);
if (ok) {
config.horizontalShift = horizontalShift;
config.decimationRate = decimationRate;
alert.success("采样参数已更新", 2000);
} else {
throw new Error();
}
} catch {
alert.error("更新采样参数失败", 2000);
}
};
// 手动刷新RAM
const refreshRAM = async () => {
const client = AuthManager.createClient(OscilloscopeApiClient);
try {
const ok = await client.refreshRAM();
if (ok) {
// alert.success("RAM已刷新", 2000);
} else {
throw new Error();
}
} catch {
alert.error("刷新RAM失败", 2000);
}
};
// 生成测试数据
const generateTestData = () => {
const freq = 5_000_000;
const duration = 0.001; // 1ms
const points = Math.floor(freq * duration);
const x = Array.from( const x = Array.from(
{ length: bytes.length }, { length: points },
(_, i) => (i * samplePeriodNs.value) / 1000 // us (_, i) => (i * 1_000_000_000) / freq / 1000,
);
const y = Array.from({ length: points }, (_, i) =>
Math.floor(Math.sin(i * 0.01) * 127 + 128),
); );
const y = Array.from(bytes);
oscData.value = { oscData.value = {
x, x,
y, y,
xUnit: "us", xUnit: "us",
yUnit: "V", yUnit: "V",
adFrequency: resp.adFrequency, adFrequency: freq,
adVpp: resp.adVpp, adVpp: 2.0,
adMax: resp.adMax, adMax: 255,
adMin: resp.adMin, adMin: 0,
}; };
} catch (error) { alert.success("测试数据生成成功", 2000);
alert.error("获取示波器数据失败", 3000);
}
};
// 定时器引用
let refreshIntervalId: number | undefined;
// 刷新间隔(毫秒),可根据需要调整
const refreshIntervalMs = ref(1000);
// 定时刷新函数
const startAutoRefresh = () => {
if (refreshIntervalId !== undefined) return;
refreshIntervalId = window.setInterval(async () => {
await refreshRAM();
await getOscilloscopeData();
}, refreshIntervalMs.value);
};
const stopAutoRefresh = () => {
if (refreshIntervalId !== undefined) {
clearInterval(refreshIntervalId);
refreshIntervalId = undefined;
isCapturing.value = false;
}
};
// 启动捕获
const startCapture = async () => {
if (operationMutex.isLocked()) {
alert.warn("有其他操作正在进行中,请稍后再试", 3000);
return;
}
isCapturing.value = true;
const release = await operationMutex.acquire();
try {
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
const started = await client.startCapture();
if (!started) throw new Error("无法启动捕获");
alert.info("开始捕获...", 2000);
// 启动定时刷新
startAutoRefresh();
} catch (error) {
alert.error("捕获失败", 3000);
isCapturing.value = false;
stopAutoRefresh();
} finally {
release();
}
};
// 停止捕获
const stopCapture = async () => {
if (!isCapturing.value) {
alert.warn("当前没有正在进行的捕获操作", 2000);
return;
}
isCapturing.value = false;
stopAutoRefresh();
const release = await operationMutex.acquire();
try {
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
const stopped = await client.stopCapture();
if (!stopped) throw new Error("无法停止捕获");
alert.info("捕获已停止", 2000);
} catch (error) {
alert.error("停止捕获失败", 3000);
} finally {
release();
}
};
// 更新触发参数
const updateTrigger = async (level: number, risingEdge: boolean) => {
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
try {
const ok = await client.updateTrigger(level, risingEdge);
if (ok) {
config.triggerLevel = level;
config.triggerRisingEdge = risingEdge;
alert.success("触发参数已更新", 2000);
} else {
throw new Error();
}
} catch {
alert.error("更新触发参数失败", 2000);
}
};
// 更新采样参数
const updateSampling = async (horizontalShift: number, decimationRate: number) => {
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
try {
const ok = await client.updateSampling(horizontalShift, decimationRate);
if (ok) {
config.horizontalShift = horizontalShift;
config.decimationRate = decimationRate;
alert.success("采样参数已更新", 2000);
} else {
throw new Error();
}
} catch {
alert.error("更新采样参数失败", 2000);
}
};
// 手动刷新RAM
const refreshRAM = async () => {
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
try {
const ok = await client.refreshRAM();
if (ok) {
// alert.success("RAM已刷新", 2000);
} else {
throw new Error();
}
} catch {
alert.error("刷新RAM失败", 2000);
}
};
// 生成测试数据
const generateTestData = () => {
const freq = 5_000_000;
const duration = 0.001; // 1ms
const points = Math.floor(freq * duration);
const x = Array.from({ length: points }, (_, i) => (i * 1_000_000_000 / freq) / 1000);
const y = Array.from({ length: points }, (_, i) =>
Math.floor(Math.sin(i * 0.01) * 127 + 128)
);
oscData.value = {
x,
y,
xUnit: "us",
yUnit: "V",
adFrequency: freq,
adVpp: 2.0,
adMax: 255,
adMin: 0,
}; };
alert.success("测试数据生成成功", 2000);
};
return { return {
oscData, oscData,
config, config,
isApplying, isApplying,
isCapturing, isCapturing,
sampleCount, sampleCount,
samplePeriodNs, samplePeriodNs,
refreshIntervalMs, refreshIntervalMs,
applyConfiguration, applyConfiguration,
resetConfiguration, resetConfiguration,
clearOscilloscopeData, clearOscilloscopeData,
getOscilloscopeData, getOscilloscopeData,
startCapture, startCapture,
stopCapture, stopCapture,
updateTrigger, updateTrigger,
updateSampling, updateSampling,
refreshRAM, refreshRAM,
generateTestData, generateTestData,
}; };
}); },
);
export { useProvideOscilloscope, useOscilloscopeState, DEFAULT_CONFIG }; export { useProvideOscilloscope, useOscilloscopeState, DEFAULT_CONFIG };

View File

@@ -1,325 +1,356 @@
<template> <template>
<div <div
class="tutorial-carousel relative" class="tutorial-carousel relative"
@wheel.prevent="handleWheel" @wheel.prevent="handleWheel"
@mouseenter="pauseAutoRotation" @mouseenter="pauseAutoRotation"
@mouseleave="resumeAutoRotation" @mouseleave="resumeAutoRotation"
> <!-- 例程卡片堆叠 --> >
<div class="card-stack relative mx-auto"> <!-- 例程卡片堆叠 -->
<div <div class="card-stack relative mx-auto">
v-for="(tutorial, index) in tutorials" <div
:key="index" v-for="(tutorial, index) in tutorials"
class="tutorial-card absolute transition-all duration-500 ease-in-out rounded-2xl shadow-2xl border-4 border-base-300 overflow-hidden" :key="index"
:class="getCardClass(index)" class="tutorial-card absolute transition-all duration-500 ease-in-out rounded-2xl shadow-2xl border-4 border-base-300 overflow-hidden"
:style="getCardStyle(index)" :class="getCardClass(index)"
@click="handleCardClick(index, tutorial.id)" :style="getCardStyle(index)"
> @click="handleCardClick(index, tutorial.id)"
<!-- 卡片内容 --> >
<div class="relative"> <!-- 卡片内容 -->
<!-- 图片 --> <img <div class="relative">
:src="tutorial.thumbnail || `https://placehold.co/600x400?text=${tutorial.title}`" <!-- 图片 -->
class="w-full object-contain" <img
:alt="tutorial.title" :src="
style="width: 600px; height: 400px;" tutorial.thumbnail ||
/> `https://kaifage.com/api/placeholder/600/400?text=${tutorial.title}&color=000000&bgColor=ffffff&fontSize=72`
"
<!-- 卡片蒙层 --> class="w-full object-contain"
<div :alt="tutorial.title"
class="absolute inset-0 bg-primary opacity-20 transition-opacity duration-300" style="width: 600px; height: 400px"
:class="{'opacity-10': index === currentIndex}" />
></div>
<!-- 卡片蒙层 -->
<!-- 标题覆盖层 --> <div
<div class="absolute bottom-0 left-0 right-0 p-4 bg-gradient-to-t from-base-300 to-transparent"> class="absolute inset-0 bg-primary opacity-20 transition-opacity duration-300"
<div class="flex flex-col gap-2"> :class="{ 'opacity-10': index === currentIndex }"
<h3 class="text-lg font-bold text-base-content">{{ tutorial.title }}</h3> ></div>
<p class="text-sm opacity-80 truncate">{{ tutorial.description }}</p>
<!-- 签显示 --> <!-- 题覆盖层 -->
<div v-if="tutorial.tags && tutorial.tags.length > 0" class="flex flex-wrap gap-1"> <div
<span class="absolute bottom-0 left-0 right-0 p-4 bg-gradient-to-t from-base-300 to-transparent"
v-for="tag in tutorial.tags.slice(0, 3)" >
:key="tag" <div class="flex flex-col gap-2">
class="badge badge-outline badge-xs text-xs" <h3 class="text-lg font-bold text-base-content">
> {{ tutorial.title }}
{{ tag }} </h3>
</span> <p class="text-sm opacity-80 truncate">
</div> {{ tutorial.description }}
</div> </p>
</div> <!-- 标签显示 -->
</div> <div
</div> v-if="tutorial.tags && tutorial.tags.length > 0"
</div> class="flex flex-wrap gap-1"
>
<!-- 导航指示器 --> <span
<div class="indicators flex justify-center gap-2 mt-4"> v-for="tag in tutorial.tags.slice(0, 3)"
<button :key="tag"
v-for="(_, index) in tutorials" class="badge badge-outline badge-xs text-xs"
:key="index" >
@click="setActiveCard(index)" {{ tag }}
class="w-3 h-3 rounded-full transition-all duration-300" </span>
:class="index === currentIndex ? 'bg-primary scale-125' : 'bg-base-300'" </div>
></button> </div>
</div> </div>
</div> </div>
</template> </div>
</div>
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'; <!-- 导航指示器 -->
import { useRouter } from 'vue-router'; <div class="indicators flex justify-center gap-2 mt-4">
import { AuthManager } from '@/utils/AuthManager'; <button
import type { ExamSummary } from '@/APIClient'; v-for="(_, index) in tutorials"
:key="index"
// 接口定义 @click="setActiveCard(index)"
interface Tutorial { class="w-3 h-3 rounded-full transition-all duration-300"
id: string; :class="index === currentIndex ? 'bg-primary scale-125' : 'bg-base-300'"
title: string; ></button>
description: string; </div>
thumbnail?: string; </div>
tags: string[]; </template>
}
<script setup lang="ts">
// Props import { ref, onMounted, onUnmounted } from "vue";
const props = defineProps<{ import { useRouter } from "vue-router";
autoRotationInterval?: number; import { AuthManager } from "@/utils/AuthManager";
}>(); import { ExamClient, ResourceClient, type ExamInfo } from "@/APIClient";
// 配置默认值 // 接口定义
const autoRotationInterval = props.autoRotationInterval || 5000; // 默认5秒 interface Tutorial {
id: string;
// 状态管理 title: string;
const tutorials = ref<Tutorial[]>([]); description: string;
const currentIndex = ref(0); thumbnail?: string;
const router = useRouter(); tags: string[];
let autoRotationTimer: number | null = null; }
// 处理卡片点击 // Props
const handleCardClick = (index: number, tutorialId: string) => { const props = defineProps<{
if (index === currentIndex.value) { autoRotationInterval?: number;
goToExam(tutorialId); }>();
} else {
setActiveCard(index); // 配置默认值
} const autoRotationInterval = props.autoRotationInterval || 5000; // 默认5秒
};
// 状态管理
// 从数据库加载实验数据 const tutorials = ref<Tutorial[]>([]);
onMounted(async () => { const currentIndex = ref(0);
try { const router = useRouter();
console.log('正在从数据库加载实验数据...'); let autoRotationTimer: number | null = null;
// 创建认证客户端 // 处理卡片点击
const client = AuthManager.createAuthenticatedExamClient(); const handleCardClick = (index: number, tutorialId: string) => {
if (index === currentIndex.value) {
// 获取实验列表 goToExam(tutorialId);
const examList: ExamSummary[] = await client.getExamList(); } else {
setActiveCard(index);
// 筛选可见的实验并转换为Tutorial格式 }
const visibleExams = examList };
.filter(exam => exam.isVisibleToUsers)
.slice(0, 6); // 限制轮播显示最多6个实验 // 从数据库加载实验数据
onMounted(async () => {
if (visibleExams.length === 0) { try {
console.warn('没有找到可见的实验'); console.log("正在从数据库加载实验数据...");
return;
} // 创建认证客户端
const client = AuthManager.createClient(ExamClient);
// 转换数据格式并获取封面图片
const tutorialPromises = visibleExams.map(async (exam) => { // 获取实验列表
let thumbnail: string | undefined; const examList: ExamInfo[] = await client.getExamList();
try { // 筛选可见的实验并转换为Tutorial格式
// 获取实验的封面资源(模板资源) const visibleExams = examList
const resourceClient = AuthManager.createAuthenticatedResourceClient(); .filter((exam) => exam.isVisibleToUsers)
const resourceList = await resourceClient.getResourceList(exam.id, 'cover', 'template'); .slice(0, 6); // 限制轮播显示最多6个实验
if (resourceList && resourceList.length > 0) {
// 使用第一个封面资源 if (visibleExams.length === 0) {
const coverResource = resourceList[0]; console.warn("没有找到可见的实验");
const fileResponse = await resourceClient.getResourceById(coverResource.id); return;
// 创建Blob URL作为缩略图 }
thumbnail = URL.createObjectURL(fileResponse.data);
} // 转换数据格式并获取封面图片
} catch (error) { const tutorialPromises = visibleExams.map(async (exam) => {
console.warn(`无法获取实验${exam.id}的封面图片:`, error); let thumbnail: string | undefined;
}
try {
return { // 获取实验的封面资源(模板资源)
id: exam.id, const resourceClient = AuthManager.createClient(ResourceClient);
title: exam.name, const resourceList = await resourceClient.getResourceList(
description: '点击查看实验详情', exam.id,
thumbnail, "cover",
tags: exam.tags || [] "template",
}; );
}); if (resourceList && resourceList.length > 0) {
// 使用第一个封面资源
tutorials.value = await Promise.all(tutorialPromises); const coverResource = resourceList[0];
const fileResponse = await resourceClient.getResourceById(
console.log('成功加载实验数据:', tutorials.value.length, '个实验'); coverResource.id,
);
// 启动自动旋转 // 创建Blob URL作为缩略图
startAutoRotation(); thumbnail = URL.createObjectURL(fileResponse.data);
} catch (error) { }
console.error('加载实验数据失败:', error); } catch (error) {
console.warn(`无法获取实验${exam.id}的封面图片:`, error);
// 如果加载失败,显示默认的占位内容 }
tutorials.value = [{
id: 'placeholder', return {
title: '实验数据加载中...', id: exam.id,
description: '请稍后或刷新页面重试', title: exam.name,
thumbnail: undefined, description: "点击查看实验详情",
tags: [] thumbnail,
}]; tags: exam.tags || [],
} };
}); });
// 在组件销毁时清除计时器和Blob URLs tutorials.value = await Promise.all(tutorialPromises);
onUnmounted(() => {
if (autoRotationTimer) { console.log("成功加载实验数据:", tutorials.value.length, "个实验");
clearInterval(autoRotationTimer);
} // 启动自动旋转
startAutoRotation();
// 清理创建的Blob URLs } catch (error) {
tutorials.value.forEach(tutorial => { console.error("加载实验数据失败:", error);
if (tutorial.thumbnail && tutorial.thumbnail.startsWith('blob:')) {
URL.revokeObjectURL(tutorial.thumbnail); // 如果加载失败,显示默认的占位内容
} tutorials.value = [
}); {
}); id: "placeholder",
title: "实验数据加载中...",
// 鼠标滚轮处理 description: "请稍后或刷新页面重试",
const handleWheel = (event: WheelEvent) => { thumbnail: undefined,
if (event.deltaY > 0) { tags: [],
nextCard(); },
} else { ];
prevCard(); }
} });
};
// 在组件销毁时清除计时器和Blob URLs
// 下一张卡片 onUnmounted(() => {
const nextCard = () => { if (autoRotationTimer) {
currentIndex.value = (currentIndex.value + 1) % tutorials.value.length; clearInterval(autoRotationTimer);
}; }
// 上一张卡片 // 清理创建的Blob URLs
const prevCard = () => { tutorials.value.forEach((tutorial) => {
currentIndex.value = (currentIndex.value - 1 + tutorials.value.length) % tutorials.value.length; if (tutorial.thumbnail && tutorial.thumbnail.startsWith("blob:")) {
}; URL.revokeObjectURL(tutorial.thumbnail);
}
// 设置活动卡片 });
const setActiveCard = (index: number) => { });
currentIndex.value = index;
}; // 鼠标滚轮处理
const handleWheel = (event: WheelEvent) => {
// 自动旋转 if (event.deltaY > 0) {
const startAutoRotation = () => { nextCard();
autoRotationTimer = window.setInterval(() => { } else {
nextCard(); prevCard();
}, autoRotationInterval); }
}; };
// 暂停自动旋转 // 下一张卡片
const pauseAutoRotation = () => { const nextCard = () => {
if (autoRotationTimer) { currentIndex.value = (currentIndex.value + 1) % tutorials.value.length;
clearInterval(autoRotationTimer); };
autoRotationTimer = null;
} // 上一张卡片
}; const prevCard = () => {
currentIndex.value =
// 恢复自动旋转 (currentIndex.value - 1 + tutorials.value.length) % tutorials.value.length;
const resumeAutoRotation = () => { };
if (!autoRotationTimer) {
startAutoRotation(); // 设置活动卡片
} const setActiveCard = (index: number) => {
}; currentIndex.value = index;
};
// 前往实验
const goToExam = (examId: string) => { // 自动旋转
// 跳转到实验列表页面并传递examId参数页面将自动打开对应的实验详情模态框 const startAutoRotation = () => {
router.push({ autoRotationTimer = window.setInterval(() => {
path: '/exam', nextCard();
query: { examId: examId } }, autoRotationInterval);
}); };
};
// 暂停自动旋转
// 计算卡片类和样式 const pauseAutoRotation = () => {
const getCardClass = (index: number) => { if (autoRotationTimer) {
const isActive = index === currentIndex.value; clearInterval(autoRotationTimer);
const isPrev = (index === currentIndex.value - 1) || (currentIndex.value === 0 && index === tutorials.value.length - 1); autoRotationTimer = null;
const isNext = (index === currentIndex.value + 1) || (currentIndex.value === tutorials.value.length - 1 && index === 0); }
};
return {
'z-30': isActive, // 恢复自动旋转
'z-20': isPrev || isNext, const resumeAutoRotation = () => {
'z-10': !isActive && !isPrev && !isNext, if (!autoRotationTimer) {
'hover:scale-105': isActive, startAutoRotation();
'cursor-pointer': true }
}; };
};
// 前往实验
const getCardStyle = (index: number) => { const goToExam = (examId: string) => {
const isActive = index === currentIndex.value; // 跳转到实验列表页面并传递examId参数页面将自动打开对应的实验详情模态框
const isPrev = (index === currentIndex.value - 1) || (currentIndex.value === 0 && index === tutorials.value.length - 1); router.push({
const isNext = (index === currentIndex.value + 1) || (currentIndex.value === tutorials.value.length - 1 && index === 0); path: "/exam",
query: { examId: examId },
// 基本样式 });
let style = { };
transform: 'scale(1) translateY(0) rotate(0deg)',
opacity: '1', // 计算卡片类和样式
filter: 'blur(0)' const getCardClass = (index: number) => {
}; const isActive = index === currentIndex.value;
const isPrev =
// 活动卡片 index === currentIndex.value - 1 ||
if (isActive) { (currentIndex.value === 0 && index === tutorials.value.length - 1);
return style; const isNext =
} index === currentIndex.value + 1 ||
(currentIndex.value === tutorials.value.length - 1 && index === 0);
// 上一张卡片
if (isPrev) { return {
style.transform = 'scale(0.85) translateY(-10%) rotate(-5deg)'; "z-30": isActive,
style.opacity = '0.7'; "z-20": isPrev || isNext,
style.filter = 'blur(1px)'; "z-10": !isActive && !isPrev && !isNext,
return style; "hover:scale-105": isActive,
} "cursor-pointer": true,
};
// 下一张卡片 };
if (isNext) {
style.transform = 'scale(0.85) translateY(10%) rotate(5deg)'; const getCardStyle = (index: number) => {
style.opacity = '0.7'; const isActive = index === currentIndex.value;
style.filter = 'blur(1px)'; const isPrev =
return style; index === currentIndex.value - 1 ||
} (currentIndex.value === 0 && index === tutorials.value.length - 1);
const isNext =
// 其他卡片 index === currentIndex.value + 1 ||
style.transform = 'scale(0.7) translateY(0) rotate(0deg)'; (currentIndex.value === tutorials.value.length - 1 && index === 0);
style.opacity = '0.4';
style.filter = 'blur(2px)'; // 基本样式
return style; let style = {
} transform: "scale(1) translateY(0) rotate(0deg)",
</script> opacity: "1",
filter: "blur(0)",
<style scoped> };
.tutorial-carousel {
width: 100%; // 活动卡片
height: 500px; if (isActive) {
perspective: 1000px; return style;
display: flex; }
flex-direction: column;
align-items: center; // 上一张卡片
} if (isPrev) {
style.transform = "scale(0.85) translateY(-10%) rotate(-5deg)";
.card-stack { style.opacity = "0.7";
width: 600px; style.filter = "blur(1px)";
height: 440px; return style;
position: relative; }
transform-style: preserve-3d;
} // 下一张卡片
if (isNext) {
.tutorial-card { style.transform = "scale(0.85) translateY(10%) rotate(5deg)";
width: 600px; style.opacity = "0.7";
height: 400px; style.filter = "blur(1px)";
background-color: hsl(var(--b2)); return style;
will-change: transform, opacity; }
}
// 其他卡片
.tutorial-card:hover { style.transform = "scale(0.7) translateY(0) rotate(0deg)";
box-shadow: 0 0 15px rgba(var(--p), 0.5); style.opacity = "0.4";
} style.filter = "blur(2px)";
</style> return style;
};
</script>
<style scoped>
.tutorial-carousel {
width: 100%;
height: 500px;
perspective: 1000px;
display: flex;
flex-direction: column;
align-items: center;
}
.card-stack {
width: 600px;
height: 440px;
position: relative;
transform-style: preserve-3d;
}
.tutorial-card {
width: 600px;
height: 400px;
background-color: hsl(var(--b2));
will-change: transform, opacity;
}
.tutorial-card:hover {
box-shadow: 0 0 15px rgba(var(--p), 0.5);
}
</style>

View File

@@ -16,22 +16,32 @@
<span class="text-sm">{{ bitstream.name }}</span> <span class="text-sm">{{ bitstream.name }}</span>
<div class="flex gap-2"> <div class="flex gap-2">
<button <button
@click="downloadExampleBitstream(bitstream)" @click="handleExampleBitstream('download', bitstream)"
class="btn btn-sm btn-secondary" class="btn btn-sm btn-secondary"
:disabled="isDownloading || isProgramming" :disabled="currentTask !== 'none'"
> >
<div v-if="isDownloading"> <div
v-if="
currentTask === 'downloading' &&
currentBitstreamId === bitstream.id
"
>
<span class="loading loading-spinner loading-xs"></span> <span class="loading loading-spinner loading-xs"></span>
{{ downloadProgress }}% 下载中...
</div> </div>
<div v-else>下载示例</div> <div v-else>下载示例</div>
</button> </button>
<button <button
@click="programExampleBitstream(bitstream)" @click="handleExampleBitstream('program', bitstream)"
class="btn btn-sm btn-primary" class="btn btn-sm btn-primary"
:disabled="isDownloading || isProgramming" :disabled="currentTask !== 'none'"
> >
<div v-if="isProgramming"> <div
v-if="
currentTask === 'programming' &&
currentBitstreamId === bitstream.id
"
>
<span class="loading loading-spinner loading-xs"></span> <span class="loading loading-spinner loading-xs"></span>
烧录中... 烧录中...
</div> </div>
@@ -63,14 +73,18 @@
<!-- Upload Button --> <!-- Upload Button -->
<div class="card-actions w-full"> <div class="card-actions w-full">
<button <button
@click="handleClick" @click="handleUploadAndDownload"
class="btn btn-primary grow" class="btn btn-primary grow"
:disabled="isUploading || isProgramming" :disabled="currentTask !== 'none'"
> >
<div v-if="isUploading"> <div v-if="currentTask === 'uploading'">
<span class="loading loading-spinner"></span> <span class="loading loading-spinner"></span>
上传中... 上传中...
</div> </div>
<div v-else-if="currentTask === 'programming'">
<span class="loading loading-spinner"></span>
{{ currentProgressPercent }}% ...
</div>
<div v-else>上传并下载</div> <div v-else>上传并下载</div>
</button> </button>
</div> </div>
@@ -78,24 +92,19 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, ref, useTemplateRef, onMounted } from "vue"; import { ref, useTemplateRef, onMounted } from "vue";
import { AuthManager } from "@/utils/AuthManager"; // Adjust the path based on your project structure import { AuthManager } from "@/utils/AuthManager";
import { useDialogStore } from "@/stores/dialog"; import { useDialogStore } from "@/stores/dialog";
import { isNull, isUndefined } from "lodash";
import { useEquipments } from "@/stores/equipments"; import { useEquipments } from "@/stores/equipments";
import type { HubConnection } from "@microsoft/signalr";
import type {
IProgressHub,
IProgressReceiver,
} from "@/TypedSignalR.Client/server.Hubs";
import { getHubProxyFactory, getReceiverRegister } from "@/TypedSignalR.Client";
import { ProgressStatus } from "@/server.Hubs";
import { useRequiredInjection } from "@/utils/Common"; import { useRequiredInjection } from "@/utils/Common";
import { useAlertStore } from "./Alert"; import { useAlertStore } from "./Alert";
import { ResourceClient, ResourcePurpose } from "@/APIClient";
import { useProgressStore } from "@/stores/progress";
import { ProgressStatus, type ProgressInfo } from "@/utils/signalR/server.Hubs";
interface Props { interface Props {
maxMemory?: number; maxMemory?: number;
examId?: string; // 新增examId属性 examId?: string;
} }
const props = withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
@@ -108,203 +117,166 @@ const emits = defineEmits<{
}>(); }>();
const alert = useRequiredInjection(useAlertStore); const alert = useRequiredInjection(useAlertStore);
const progressTracker = useProgressStore();
const dialog = useDialogStore(); const dialog = useDialogStore();
const eqps = useEquipments(); const eqps = useEquipments();
const isUploading = ref(false); const availableBitstreams = ref<{ id: string; name: string }[]>([]);
const isDownloading = ref(false);
const isProgramming = ref(false);
const availableBitstreams = ref<{ id: number; name: string }[]>([]);
// Progress
const downloadTaskId = ref("");
const downloadProgress = ref(0);
const progressHubConnection = ref<HubConnection>();
const progressHubProxy = ref<IProgressHub>();
const progressHubReceiver: IProgressReceiver = {
onReceiveProgress: async (msg) => {
if (msg.taskId == downloadTaskId.value) {
if (msg.status == ProgressStatus.InProgress) {
downloadProgress.value = msg.progressPercent;
} else if (msg.status == ProgressStatus.Failed) {
dialog.error(msg.errorMessage);
} else if (msg.status == ProgressStatus.Completed) {
alert.info("比特流下载成功");
}
}
},
};
onMounted(async () => {
progressHubConnection.value =
AuthManager.createAuthenticatedProgressHubConnection();
progressHubProxy.value = getHubProxyFactory("IProgressHub").createHubProxy(
progressHubConnection.value,
);
getReceiverRegister("IProgressReceiver").register(
progressHubConnection.value,
progressHubReceiver,
);
});
const fileInput = useTemplateRef("fileInput"); const fileInput = useTemplateRef("fileInput");
const bitstream = defineModel("bitstreamFile", { const bitstream = ref<File | undefined>(undefined);
type: File,
default: undefined, // 用一个状态变量替代多个
}); const currentTask = ref<"none" | "uploading" | "downloading" | "programming">(
"none",
);
const currentBitstreamId = ref<string>("");
const currentProgressId = ref<string>("");
const currentProgressPercent = ref<number>(0);
// 初始化时加载示例比特流
onMounted(async () => { onMounted(async () => {
if (!isUndefined(bitstream.value) && !isNull(fileInput.value)) { if (bitstream.value && fileInput.value) {
let fileList = new DataTransfer(); let fileList = new DataTransfer();
fileList.items.add(bitstream.value); fileList.items.add(bitstream.value);
fileInput.value.files = fileList.files; fileInput.value.files = fileList.files;
} }
await loadAvailableBitstreams(); await loadAvailableBitstreams();
}); });
// 加载可用的比特流文件列表
async function loadAvailableBitstreams() {
console.log("加载可用比特流文件examId:", props.examId);
if (!props.examId) {
availableBitstreams.value = [];
return;
}
try {
const resourceClient = AuthManager.createAuthenticatedResourceClient();
// 使用新的ResourceClient API获取比特流模板资源列表
const resources = await resourceClient.getResourceList(
props.examId,
"bitstream",
"template",
);
availableBitstreams.value =
resources.map((r) => ({ id: r.id, name: r.name })) || [];
} catch (error) {
console.error("加载比特流列表失败:", error);
availableBitstreams.value = [];
}
}
// 下载示例比特流
async function downloadExampleBitstream(bitstream: {
id: number;
name: string;
}) {
if (isDownloading.value) return;
isDownloading.value = true;
try {
const resourceClient = AuthManager.createAuthenticatedResourceClient();
// 使用新的ResourceClient API获取资源文件
const response = await resourceClient.getResourceById(bitstream.id);
if (response && response.data) {
// 创建下载链接
const url = URL.createObjectURL(response.data);
const link = document.createElement("a");
link.href = url;
link.download = response.fileName || bitstream.name;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
dialog.info("示例比特流下载成功");
} else {
dialog.error("下载失败:响应数据为空");
}
} catch (error) {
console.error("下载示例比特流失败:", error);
dialog.error("下载示例比特流失败");
} finally {
isDownloading.value = false;
}
}
// 直接烧录示例比特流
async function programExampleBitstream(bitstream: {
id: number;
name: string;
}) {
if (isProgramming.value) return;
isProgramming.value = true;
try {
const downloadTaskId = await eqps.jtagDownloadBitstream(bitstream.id);
} catch (error) {
console.error("烧录示例比特流失败:", error);
dialog.error("烧录示例比特流失败");
} finally {
isProgramming.value = false;
}
}
function handleFileChange(event: Event): void { function handleFileChange(event: Event): void {
const target = event.target as HTMLInputElement; const target = event.target as HTMLInputElement;
const file = target.files?.[0]; // 获取选中的第一个文件 const file = target.files?.[0];
bitstream.value = file || undefined;
if (!file) {
return;
}
bitstream.value = file;
} }
function checkFile(file: File): boolean { function checkFileInput(): boolean {
const maxBytes = props.maxMemory! * 1024 * 1024; // 将最大容量从 MB 转换为字节 if (!bitstream.value) {
if (file.size > maxBytes) { dialog.error(`未选择文件`);
return false;
}
const maxBytes = props.maxMemory! * 1024 * 1024;
if (bitstream.value.size > maxBytes) {
dialog.error(`文件大小超过最大限制: ${props.maxMemory}MB`); dialog.error(`文件大小超过最大限制: ${props.maxMemory}MB`);
return false; return false;
} }
return true; return true;
} }
async function handleClick(event: Event): Promise<void> { async function downloadBitstream() {
console.log("上传按钮被点击"); currentTask.value = "programming";
if (isNull(bitstream.value) || isUndefined(bitstream.value)) { try {
dialog.error(`未选择文件`); currentProgressId.value = await eqps.jtagDownloadBitstream(
currentBitstreamId.value,
);
progressTracker.register(
currentProgressId.value,
"programBitstream",
handleProgressUpdate,
);
} catch {
dialog.error("比特流烧录失败");
cleanProgressTracker();
}
}
function cleanProgressTracker() {
currentTask.value = "none";
currentProgressId.value = "";
currentBitstreamId.value = "";
currentProgressPercent.value = 0;
progressTracker.unregister(currentProgressId.value, "programBitstream");
}
async function loadAvailableBitstreams() {
if (!props.examId) {
availableBitstreams.value = [];
return; return;
} }
if (!checkFile(bitstream.value)) return;
isUploading.value = true;
let uploadedBitstreamId: number | null = null;
try { try {
console.log("开始上传比特流文件:", bitstream.value.name); const resourceClient = AuthManager.createClient(ResourceClient);
const bitstreamId = await eqps.jtagUploadBitstream( const resources = await resourceClient.getResourceList(
bitstream.value, props.examId,
"bitstream",
ResourcePurpose.Template,
);
availableBitstreams.value =
resources.map((r) => ({ id: r.id, name: r.name })) || [];
} catch (error) {
availableBitstreams.value = [];
}
}
// 统一处理示例比特流的下载/烧录
async function handleExampleBitstream(
action: "download" | "program",
bitstreamObj: { id: string; name: string },
) {
if (currentTask.value !== "none") return;
currentBitstreamId.value = bitstreamObj.id;
if (action === "download") {
currentTask.value = "downloading";
try {
const resourceClient = AuthManager.createClient(ResourceClient);
const response = await resourceClient.getResourceById(bitstreamObj.id);
if (response && response.data) {
const url = URL.createObjectURL(response.data);
const link = document.createElement("a");
link.href = url;
link.download = response.fileName || bitstreamObj.name;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
alert.info("示例比特流下载成功");
} else {
alert.error("下载失败:响应数据为空");
}
} catch {
alert.error("下载示例比特流失败");
} finally {
currentTask.value = "none";
currentBitstreamId.value = "";
}
} else if (action === "program") {
currentBitstreamId.value = bitstreamObj.id;
await downloadBitstream();
}
}
// 上传并下载
async function handleUploadAndDownload() {
if (currentTask.value !== "none") return;
if (!checkFileInput()) return;
currentTask.value = "uploading";
let uploadedBitstreamId: string | null = null;
try {
uploadedBitstreamId = await eqps.jtagUploadBitstream(
bitstream.value!,
props.examId || "", props.examId || "",
); );
console.log("上传结果ID:", bitstreamId); if (!uploadedBitstreamId) throw new Error("上传失败");
if (bitstreamId === null || bitstreamId === undefined) { emits("finishedUpload", bitstream.value!);
isUploading.value = false; } catch {
return;
}
uploadedBitstreamId = bitstreamId;
} catch (e) {
dialog.error("上传失败"); dialog.error("上传失败");
console.error(e); currentTask.value = "none";
return; return;
} }
isUploading.value = false;
// Download currentBitstreamId.value = uploadedBitstreamId;
try {
console.log("开始下载比特流ID:", uploadedBitstreamId); await downloadBitstream();
if (uploadedBitstreamId === null || uploadedBitstreamId === undefined) { }
dialog.error("uploadedBitstreamId is null or undefined");
} else { function handleProgressUpdate(msg: ProgressInfo) {
isDownloading.value = true; // console.log(msg);
downloadTaskId.value = if (msg.status === ProgressStatus.Running)
await eqps.jtagDownloadBitstream(uploadedBitstreamId); currentProgressPercent.value = msg.progressPercent;
} else if (msg.status === ProgressStatus.Failed) {
} catch (e) { dialog.error(`比特流烧录失败: ${msg.errorMessage}`);
dialog.error("下载失败"); cleanProgressTracker();
console.error(e); } else if (msg.status === ProgressStatus.Completed) {
dialog.info("比特流烧录成功");
cleanProgressTracker();
} }
} }
</script> </script>

View File

@@ -212,6 +212,7 @@ import { useEquipments } from "@/stores/equipments";
import { useDialogStore } from "@/stores/dialog"; import { useDialogStore } from "@/stores/dialog";
import { toInteger } from "lodash"; import { toInteger } from "lodash";
import { AuthManager } from "@/utils/AuthManager"; import { AuthManager } from "@/utils/AuthManager";
import { DDSClient } from "@/APIClient";
// Component Attributes // Component Attributes
const props = defineProps<{ const props = defineProps<{
@@ -221,7 +222,7 @@ const props = defineProps<{
const emit = defineEmits(["update:modelValue"]); const emit = defineEmits(["update:modelValue"]);
// Global varibles // Global varibles
const dds = AuthManager.createAuthenticatedDDSClient(); const dds = AuthManager.createClient(DDSClient);
const eqps = useEquipments(); const eqps = useEquipments();
const dialog = useDialogStore(); const dialog = useDialogStore();

View File

@@ -1,65 +1,114 @@
<template> <template>
<div class="seven-segment-display" :style="{ <div
width: width + 'px', class="seven-segment-display"
height: height + 'px', :style="{
position: 'relative', width: width + 'px',
}"> height: height + 'px',
<svg xmlns="http://www.w3.org/2000/svg" :width="width" :height="height" viewBox="0 0 120 220" class="display"> position: 'relative',
}"
>
<svg
xmlns="http://www.w3.org/2000/svg"
:width="width"
:height="height"
viewBox="0 0 120 220"
class="display"
>
<!-- 数码管基座 --> <!-- 数码管基座 -->
<rect width="120" height="180" x="0" y="0" fill="#222" rx="10" ry="10" /> <rect width="120" height="180" x="0" y="0" fill="#222" rx="10" ry="10" />
<rect width="110" height="170" x="5" y="5" fill="#333" rx="5" ry="5" /> <rect width="110" height="170" x="5" y="5" fill="#333" rx="5" ry="5" />
<!-- 7 + 小数点每个段由多边形表示重新设计点位置使其更接近实际数码管 --> <!-- 7 + 小数点每个段由多边形表示重新设计点位置使其更接近实际数码管 -->
<!-- a段 (顶部横线) --> <!-- a段 (顶部横线) -->
<polygon :points="'30,20 90,20 98,28 82,36 38,36 22,28'" <polygon
:points="'30,20 90,20 98,28 82,36 38,36 22,28'"
:fill="isSegmentActive('a') ? segmentColor : inactiveColor" :fill="isSegmentActive('a') ? segmentColor : inactiveColor"
:style="{ opacity: isSegmentActive('a') ? 1 : 0.15 }" class="segment" /> :style="{ opacity: isSegmentActive('a') ? 1 : 0.15 }"
class="segment"
/>
<!-- b段 (右上竖线) --> <!-- b段 (右上竖线) -->
<polygon :points="'100,30 108,38 108,82 100,90 92,82 92,38'" <polygon
:points="'100,30 108,38 108,82 100,90 92,82 92,38'"
:fill="isSegmentActive('b') ? segmentColor : inactiveColor" :fill="isSegmentActive('b') ? segmentColor : inactiveColor"
:style="{ opacity: isSegmentActive('b') ? 1 : 0.15 }" class="segment" /> :style="{ opacity: isSegmentActive('b') ? 1 : 0.15 }"
class="segment"
/>
<!-- c段 (右下竖线) --> <!-- c段 (右下竖线) -->
<polygon :points="'100,90 108,98 108,142 100,150 92,142 92,98'" <polygon
:points="'100,90 108,98 108,142 100,150 92,142 92,98'"
:fill="isSegmentActive('c') ? segmentColor : inactiveColor" :fill="isSegmentActive('c') ? segmentColor : inactiveColor"
:style="{ opacity: isSegmentActive('c') ? 1 : 0.15 }" class="segment" /> :style="{ opacity: isSegmentActive('c') ? 1 : 0.15 }"
class="segment"
/>
<!-- d段 (底部横线) --> <!-- d段 (底部横线) -->
<polygon :points="'30,160 90,160 98,152 82,144 38,144 22,152'" <polygon
:points="'30,160 90,160 98,152 82,144 38,144 22,152'"
:fill="isSegmentActive('d') ? segmentColor : inactiveColor" :fill="isSegmentActive('d') ? segmentColor : inactiveColor"
:style="{ opacity: isSegmentActive('d') ? 1 : 0.15 }" class="segment" /> :style="{ opacity: isSegmentActive('d') ? 1 : 0.15 }"
class="segment"
/>
<!-- e段 (左下竖线) --> <!-- e段 (左下竖线) -->
<polygon :points="'20,90 28,98 28,142 20,150 12,142 12,98'" <polygon
:points="'20,90 28,98 28,142 20,150 12,142 12,98'"
:fill="isSegmentActive('e') ? segmentColor : inactiveColor" :fill="isSegmentActive('e') ? segmentColor : inactiveColor"
:style="{ opacity: isSegmentActive('e') ? 1 : 0.15 }" class="segment" /> :style="{ opacity: isSegmentActive('e') ? 1 : 0.15 }"
class="segment"
/>
<!-- f段 (左上竖线) --> <!-- f段 (左上竖线) -->
<polygon :points="'20,30 28,38 28,82 20,90 12,82 12,38'" <polygon
:points="'20,30 28,38 28,82 20,90 12,82 12,38'"
:fill="isSegmentActive('f') ? segmentColor : inactiveColor" :fill="isSegmentActive('f') ? segmentColor : inactiveColor"
:style="{ opacity: isSegmentActive('f') ? 1 : 0.15 }" class="segment" /> :style="{ opacity: isSegmentActive('f') ? 1 : 0.15 }"
class="segment"
/>
<!-- g段 (中间横线) --> <!-- g段 (中间横线) -->
<polygon :points="'30,90 38,82 82,82 90,90 82,98 38,98'" <polygon
:points="'30,90 38,82 82,82 90,90 82,98 38,98'"
:fill="isSegmentActive('g') ? segmentColor : inactiveColor" :fill="isSegmentActive('g') ? segmentColor : inactiveColor"
:style="{ opacity: isSegmentActive('g') ? 1 : 0.15 }" class="segment" /> :style="{ opacity: isSegmentActive('g') ? 1 : 0.15 }"
class="segment"
/>
<!-- dp段 (小数点) --> <!-- dp段 (小数点) -->
<circle cx="108" cy="154" r="6" :fill="isSegmentActive('dp') ? segmentColor : inactiveColor" <circle
:style="{ opacity: isSegmentActive('dp') ? 1 : 0.15 }" class="segment" /> cx="108"
cy="154"
r="6"
:fill="isSegmentActive('dp') ? segmentColor : inactiveColor"
:style="{ opacity: isSegmentActive('dp') ? 1 : 0.15 }"
class="segment"
/>
</svg> </svg>
<!-- 引脚 --> <!-- 引脚 -->
<div v-for="pin in pins" :key="pin.pinId" :style="{ <div
position: 'absolute', v-for="pin in pins"
left: `${pin.x * props.size}px`, :key="pin.pinId"
top: `${pin.y * props.size}px`, :style="{
transform: 'translate(-50%, -50%)', position: 'absolute',
}" :data-pin-wrapper="`${pin.pinId}`" :data-pin-x="`${pin.x * props.size}`" left: `${pin.x * props.size}px`,
:data-pin-y="`${pin.y * props.size}`"> top: `${pin.y * props.size}px`,
<Pin :ref="(el) => { transform: 'translate(-50%, -50%)',
if (el) pinRefs[pin.pinId] = el; }"
} :data-pin-wrapper="`${pin.pinId}`"
" :label="pin.pinId" :constraint="pin.constraint" :pinId="pin.pinId" @pin-click="$emit('pin-click', $event)" /> :data-pin-x="`${pin.x * props.size}`"
:data-pin-y="`${pin.y * props.size}`"
>
<Pin
:ref="
(el) => {
if (el) pinRefs[pin.pinId] = el;
}
"
:label="pin.pinId"
:constraint="pin.constraint"
:pinId="pin.pinId"
@pin-click="$emit('pin-click', $event)"
/>
</div> </div>
</div> </div>
</template> </template>
@@ -217,12 +266,12 @@ function isSegmentActive(
if (isInAfterglowMode.value) { if (isInAfterglowMode.value) {
return afterglowStates.value[segment]; return afterglowStates.value[segment];
} }
// 如果COM口未激活所有段都不显示 // 如果COM口未激活所有段都不显示
if (!currentComActive.value) { if (!currentComActive.value) {
return false; return false;
} }
// 否则使用稳定状态 // 否则使用稳定状态
return stableSegmentStates.value[segment]; return stableSegmentStates.value[segment];
} }
@@ -232,7 +281,7 @@ function updateSegmentStates() {
// 先获取COM口状态 // 先获取COM口状态
const comPin = props.pins.find((p) => p.pinId === "COM"); const comPin = props.pins.find((p) => p.pinId === "COM");
let comActive = false; // 默认未激活 let comActive = false; // 默认未激活
if (comPin && comPin.constraint) { if (comPin && comPin.constraint) {
const comState = getConstraintState(comPin.constraint); const comState = getConstraintState(comPin.constraint);
if (props.cathodeType === "anode") { if (props.cathodeType === "anode") {
@@ -274,7 +323,8 @@ function updateSegmentStates() {
for (const pin of props.pins) { for (const pin of props.pins) {
if (["a", "b", "c", "d", "e", "f", "g", "dp"].includes(pin.pinId)) { if (["a", "b", "c", "d", "e", "f", "g", "dp"].includes(pin.pinId)) {
if (!pin.constraint) { if (!pin.constraint) {
segmentStates.value[pin.pinId as keyof typeof segmentStates.value] = false; segmentStates.value[pin.pinId as keyof typeof segmentStates.value] =
false;
continue; continue;
} }
const pinState = getConstraintState(pin.constraint); const pinState = getConstraintState(pin.constraint);
@@ -285,7 +335,8 @@ function updateSegmentStates() {
newState = pinState === "low"; newState = pinState === "low";
} }
// 段状态只有在COM激活时才有效 // 段状态只有在COM激活时才有效
segmentStates.value[pin.pinId as keyof typeof segmentStates.value] = newState; segmentStates.value[pin.pinId as keyof typeof segmentStates.value] =
newState;
} }
} }
@@ -328,22 +379,25 @@ function updateAfterglowBuffers() {
// 进入余晖模式 // 进入余晖模式
function enterAfterglowMode() { function enterAfterglowMode() {
isInAfterglowMode.value = true; isInAfterglowMode.value = true;
// 保存当前稳定状态作为余晖状态 // 保存当前稳定状态作为余晖状态
for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) { for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) {
const typedSegmentId = segmentId as keyof typeof stableSegmentStates.value; const typedSegmentId = segmentId as keyof typeof stableSegmentStates.value;
afterglowStates.value[typedSegmentId] = stableSegmentStates.value[typedSegmentId]; afterglowStates.value[typedSegmentId] =
stableSegmentStates.value[typedSegmentId];
// 设置定时器,在余晖持续时间后退出余晖模式 // 设置定时器,在余晖持续时间后退出余晖模式
if (afterglowTimers.value[segmentId]) { if (afterglowTimers.value[segmentId]) {
clearTimeout(afterglowTimers.value[segmentId]!); clearTimeout(afterglowTimers.value[segmentId]!);
} }
afterglowTimers.value[segmentId] = setTimeout(() => { afterglowTimers.value[segmentId] = setTimeout(() => {
afterglowStates.value[typedSegmentId] = false; afterglowStates.value[typedSegmentId] = false;
// 检查是否所有段都已经关闭 // 检查是否所有段都已经关闭
const allSegmentsOff = Object.values(afterglowStates.value).every(state => !state); const allSegmentsOff = Object.values(afterglowStates.value).every(
(state) => !state,
);
if (allSegmentsOff) { if (allSegmentsOff) {
exitAfterglowMode(); exitAfterglowMode();
} }
@@ -354,14 +408,14 @@ function enterAfterglowMode() {
// 退出余晖模式 // 退出余晖模式
function exitAfterglowMode() { function exitAfterglowMode() {
isInAfterglowMode.value = false; isInAfterglowMode.value = false;
// 清除所有定时器 // 清除所有定时器
for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) { for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) {
if (afterglowTimers.value[segmentId]) { if (afterglowTimers.value[segmentId]) {
clearTimeout(afterglowTimers.value[segmentId]!); clearTimeout(afterglowTimers.value[segmentId]!);
afterglowTimers.value[segmentId] = null; afterglowTimers.value[segmentId] = null;
} }
// 重置余晖状态 // 重置余晖状态
const typedSegmentId = segmentId as keyof typeof afterglowStates.value; const typedSegmentId = segmentId as keyof typeof afterglowStates.value;
afterglowStates.value[typedSegmentId] = false; afterglowStates.value[typedSegmentId] = false;
@@ -397,11 +451,6 @@ onUnmounted(() => {
} }
} }
}); });
// 暴露属性和方法
defineExpose({
updateSegmentStates,
});
</script> </script>
<style scoped> <style scoped>
@@ -418,7 +467,8 @@ defineExpose({
/* 数码管发光效果 */ /* 数码管发光效果 */
.segment[style*="opacity: 1"] { .segment[style*="opacity: 1"] {
filter: drop-shadow(0 0 4px v-bind(segmentColor)) drop-shadow(0 0 2px v-bind(segmentColor)); filter: drop-shadow(0 0 4px v-bind(segmentColor))
drop-shadow(0 0 2px v-bind(segmentColor));
} }
</style> </style>

View File

@@ -0,0 +1,413 @@
<template>
<div
class="seven-segment-display"
:style="{
width: width + 'px',
height: height + 'px',
position: 'relative',
}"
>
<svg
xmlns="http://www.w3.org/2000/svg"
:width="width"
:height="height"
viewBox="0 0 120 220"
class="display"
>
<!-- 数码管基座 -->
<rect width="120" height="180" x="0" y="0" fill="#222" rx="10" ry="10" />
<rect width="110" height="170" x="5" y="5" fill="#333" rx="5" ry="5" />
<!-- 7段显示 -->
<polygon
v-for="(segment, id) in segmentPaths"
:key="id"
:points="segment.points"
:fill="isSegmentActive(id) ? segmentColor : inactiveColor"
:style="{ opacity: isSegmentActive(id) ? 1 : 0.15 }"
:class="{ segment: true, active: isSegmentActive(id) }"
/>
<!-- 小数点 -->
<circle
cx="108"
cy="154"
r="6"
:fill="isSegmentActive('dp') ? segmentColor : inactiveColor"
:style="{ opacity: isSegmentActive('dp') ? 1 : 0.15 }"
:class="{ segment: true, active: isSegmentActive('dp') }"
/>
</svg>
<!-- 引脚仅在非数字孪生模式下显示 -->
<div
v-if="!props.enableDigitalTwin"
v-for="pin in props.pins"
:key="pin.pinId"
:style="{
position: 'absolute',
left: `${pin.x * props.size}px`,
top: `${pin.y * props.size}px`,
transform: 'translate(-50%, -50%)',
}"
:data-pin-wrapper="`${pin.pinId}`"
:data-pin-x="`${pin.x * props.size}`"
:data-pin-y="`${pin.y * props.size}`"
>
<Pin
:ref="
(el) => {
if (el) pinRefs[pin.pinId] = el;
}
"
:label="pin.pinId"
:constraint="pin.constraint"
:pinId="pin.pinId"
@pin-click="$emit('pin-click', $event)"
/>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, watch, onMounted, onUnmounted } from "vue";
import { useConstraintsStore } from "../../stores/constraints";
import Pin from "./Pin.vue";
import { useEquipments } from "@/stores/equipments";
// ============================================================================
// Linus式极简数据结构一个byte解决一切
// ============================================================================
interface Props {
size?: number;
color?: string;
enableDigitalTwin?: boolean;
digitalTwinNum?: number;
afterglowDuration?: number;
cathodeType?: "common" | "anode";
pins?: Array<{
pinId: string;
constraint: string;
x: number;
y: number;
}>;
}
const props = withDefaults(defineProps<Props>(), {
size: 1,
color: "red",
enableDigitalTwin: false,
digitalTwinNum: 0,
afterglowDuration: 500,
cathodeType: "common",
pins: () => [
{ pinId: "a", constraint: "", x: 10, y: 170 },
{ pinId: "b", constraint: "", x: 24, y: 170 },
{ pinId: "c", constraint: "", x: 38, y: 170 },
{ pinId: "d", constraint: "", x: 52, y: 170 },
{ pinId: "e", constraint: "", x: 66, y: 170 },
{ pinId: "f", constraint: "", x: 80, y: 170 },
{ pinId: "g", constraint: "", x: 94, y: 170 },
{ pinId: "dp", constraint: "", x: 108, y: 170 },
{ pinId: "COM", constraint: "", x: 60, y: 10 },
],
});
// ============================================================================
// 核心状态:简单到极致
// ============================================================================
// 当前显示状态 - 8bit对应8个段
const displayByte = ref<number>(0);
// 余晖状态
const afterglowByte = ref<number>(0);
const afterglowTimer = ref<number | null>(null);
// 约束系统状态(兼容模式)
const constraintStates = ref<Record<string, boolean>>({
a: false,
b: false,
c: false,
d: false,
e: false,
f: false,
g: false,
dp: false,
});
// ============================================================================
// Bit操作硬件工程师的好品味
// ============================================================================
// 段到bit位的映射 (标准7段数码管编码)
const SEGMENT_BITS = {
a: 0, // bit 0
b: 1, // bit 1
c: 2, // bit 2
d: 3, // bit 3
e: 4, // bit 4
f: 5, // bit 5
g: 6, // bit 6
dp: 7, // bit 7
} as const;
function isBitSet(byte: number, bit: number): boolean {
return (byte & (1 << bit)) !== 0;
}
function isSegmentActive(segmentId: keyof typeof SEGMENT_BITS): boolean {
if (props.enableDigitalTwin) {
// 数字孪生模式余晖优先然后是当前byte
const bit = SEGMENT_BITS[segmentId];
return (
isBitSet(afterglowByte.value, bit) || isBitSet(displayByte.value, bit)
);
} else {
// 约束模式:使用传统逻辑
return constraintStates.value[segmentId] || false;
}
}
// ============================================================================
// SignalR数字孪生集成
// ============================================================================
const eqps = useEquipments();
async function initDigitalTwin() {
if (
!props.enableDigitalTwin ||
props.digitalTwinNum < 0 ||
props.digitalTwinNum > 31
)
return;
try {
eqps.sevenSegmentDisplaySetOnOff(props.enableDigitalTwin);
console.log(
`Digital twin initialized for address: ${props.digitalTwinNum}`,
);
} catch (error) {
console.warn("Failed to initialize digital twin:", error);
}
}
watch(
() => [eqps.sevenSegmentDisplayData],
() => {
if (
!eqps.sevenSegmentDisplayData ||
props.digitalTwinNum < 0 ||
props.digitalTwinNum > 31
)
return;
handleDigitalTwinData(eqps.sevenSegmentDisplayData[props.digitalTwinNum]);
},
);
function handleDigitalTwinData(data: any) {
let newByte = 0;
if (typeof data === "number") {
// 直接是byte数据
newByte = data & 0xff; // 确保只取低8位
} else if (data && typeof data.value === "number") {
// 包装在对象中的byte数据
newByte = data.value & 0xff;
} else if (data && data.segments) {
// 段状态对象格式
Object.keys(SEGMENT_BITS).forEach((segment) => {
if (data.segments[segment]) {
newByte |= 1 << SEGMENT_BITS[segment as keyof typeof SEGMENT_BITS];
}
});
}
updateDisplayByte(newByte);
}
function updateDisplayByte(newByte: number) {
const oldByte = displayByte.value;
displayByte.value = newByte;
// 启动余晖效果
if (oldByte !== 0 && newByte !== oldByte) {
startAfterglow(oldByte);
}
}
function startAfterglow(byte: number) {
afterglowByte.value = byte;
if (afterglowTimer.value) {
clearTimeout(afterglowTimer.value);
}
afterglowTimer.value = setTimeout(() => {
afterglowByte.value = 0;
afterglowTimer.value = null;
}, props.afterglowDuration);
}
function cleanupDigitalTwin() {
eqps.sevenSegmentDisplaySetOnOff(false);
}
// ============================================================================
// 约束系统兼容(传统模式)
// ============================================================================
const { getConstraintState, onConstraintStateChange } = useConstraintsStore();
let constraintUnsubscribe: (() => void) | null = null;
function updateConstraintStates() {
if (props.enableDigitalTwin) return; // 数字孪生模式下忽略约束
// 获取COM状态
const comPin = props.pins.find((p) => p.pinId === "COM");
const comActive = isComActive(comPin);
if (!comActive) {
// COM不活跃所有段关闭
Object.keys(constraintStates.value).forEach((key) => {
constraintStates.value[key] = false;
});
return;
}
// 更新各段状态
props.pins.forEach((pin) => {
if (Object.hasOwnProperty.call(SEGMENT_BITS, pin.pinId)) {
constraintStates.value[pin.pinId] = isPinActive(pin);
}
});
}
function isComActive(comPin: any): boolean {
if (!comPin?.constraint) return true;
const state = getConstraintState(comPin.constraint);
return props.cathodeType === "common" ? state === "low" : state === "low";
}
function isPinActive(pin: any): boolean {
if (!pin.constraint) return false;
const state = getConstraintState(pin.constraint);
return props.cathodeType === "common" ? state === "high" : state === "low";
}
// ============================================================================
// 渲染数据
// ============================================================================
const segmentPaths = {
a: { points: "30,20 90,20 98,28 82,36 38,36 22,28" },
b: { points: "100,30 108,38 108,82 100,90 92,82 92,38" },
c: { points: "100,90 108,98 108,142 100,150 92,142 92,98" },
d: { points: "30,160 90,160 98,152 82,144 38,144 22,152" },
e: { points: "20,90 28,98 28,142 20,150 12,142 12,98" },
f: { points: "20,30 28,38 28,82 20,90 12,82 12,38" },
g: { points: "30,90 38,82 82,82 90,90 82,98 38,98" },
} as const;
// ============================================================================
// 计算属性
// ============================================================================
const width = computed(() => 120 * props.size);
const height = computed(() => 220 * props.size);
const segmentColor = computed(() => props.color);
const inactiveColor = computed(() => "#FFFFFF");
const pinRefs = ref<Record<string, any>>({});
// ============================================================================
// 生命周期
// ============================================================================
onMounted(async () => {
if (props.enableDigitalTwin) {
await initDigitalTwin();
} else {
constraintUnsubscribe = onConstraintStateChange(updateConstraintStates);
updateConstraintStates();
}
});
onUnmounted(() => {
cleanupDigitalTwin();
if (constraintUnsubscribe) {
constraintUnsubscribe();
}
if (afterglowTimer.value) {
clearTimeout(afterglowTimer.value);
}
});
// 监听模式切换
watch(
() => [props.enableDigitalTwin],
async () => {
// 清理旧模式
cleanupDigitalTwin();
if (constraintUnsubscribe) {
constraintUnsubscribe();
constraintUnsubscribe = null;
}
// 初始化新模式
if (props.enableDigitalTwin) {
await initDigitalTwin();
} else {
constraintUnsubscribe = onConstraintStateChange(updateConstraintStates);
updateConstraintStates();
}
},
);
</script>
<style scoped>
.seven-segment-display {
display: inline-block;
position: relative;
}
.segment {
transition:
opacity 0.2s,
fill 0.2s;
}
.segment.active {
filter: drop-shadow(0 0 4px v-bind(segmentColor))
drop-shadow(0 0 2px v-bind(segmentColor));
}
</style>
<script lang="ts">
export function getDefaultProps() {
return {
size: 1,
color: "red",
enableDigitalTwin: false,
digitalTwinNum: 0,
afterglowDuration: 500,
cathodeType: "common",
pins: [
{ pinId: "a", constraint: "", x: 10, y: 170 },
{ pinId: "b", constraint: "", x: 24, y: 170 },
{ pinId: "c", constraint: "", x: 38, y: 170 },
{ pinId: "d", constraint: "", x: 52, y: 170 },
{ pinId: "e", constraint: "", x: 66, y: 170 },
{ pinId: "f", constraint: "", x: 80, y: 170 },
{ pinId: "g", constraint: "", x: 94, y: 170 },
{ pinId: "dp", constraint: "", x: 108, y: 170 },
{ pinId: "COM", constraint: "", x: 60, y: 10 },
],
};
}
</script>

View File

@@ -1,17 +1,30 @@
// filepath: c:\_Project\FPGA_WebLab\FPGA_WebLab\src\components\equipments\Switch.vue
<template> <template>
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
:width="width" :width="width"
:height="height" :height="height"
:viewBox="`4 6 ${props.switchCount + 2} 4`" :viewBox="`4 6 ${switchCount + 2} 4`"
class="dip-switch" class="dip-switch"
> >
<defs> <defs>
<filter id="glow" x="-50%" y="-50%" width="200%" height="200%"> <filter id="glow" x="-50%" y="-50%" width="200%" height="200%">
<feFlood result="flood" flood-color="#f08a5d" flood-opacity="1"></feFlood> <feFlood
<feComposite in="flood" result="mask" in2="SourceGraphic" operator="in"></feComposite> result="flood"
<feMorphology in="mask" result="dilated" operator="dilate" radius="0.02"></feMorphology> flood-color="#f08a5d"
flood-opacity="1"
></feFlood>
<feComposite
in="flood"
result="mask"
in2="SourceGraphic"
operator="in"
></feComposite>
<feMorphology
in="mask"
result="dilated"
operator="dilate"
radius="0.02"
></feMorphology>
<feGaussianBlur in="dilated" stdDeviation="0.05" result="blur1" /> <feGaussianBlur in="dilated" stdDeviation="0.05" result="blur1" />
<feGaussianBlur in="dilated" stdDeviation="0.1" result="blur2" /> <feGaussianBlur in="dilated" stdDeviation="0.1" result="blur2" />
<feGaussianBlur in="dilated" stdDeviation="0.2" result="blur3" /> <feGaussianBlur in="dilated" stdDeviation="0.2" result="blur3" />
@@ -23,29 +36,41 @@
</feMerge> </feMerge>
</filter> </filter>
</defs> </defs>
<g> <g>
<!-- 红色背景随开关数量变化宽度 --> <rect
<rect :width="props.switchCount + 2" height="4" x="4" y="6" fill="#c01401" rx="0.1" /> :width="switchCount + 2"
<text v-if="props.showLabels" fill="white" font-size="0.7" x="4.25" y="6.75">ON</text> height="4"
x="4"
y="6"
fill="#c01401"
rx="0.1"
/>
<text
v-if="props.showLabels"
fill="white"
font-size="0.7"
x="4.25"
y="6.75"
>
ON
</text>
<g> <g>
<template v-for="(_, index) in Array(props.switchCount)" :key="index"> <template v-for="(_, index) in Array(switchCount)" :key="index">
<rect <rect
class="glow interactive" class="glow interactive"
@click="toggleBtnStatus(index)" @click="toggleBtnStatus(index)"
width="0.7" width="0.7"
height="2" height="2"
fill="#68716f" fill="#68716f"
:x="5.15 + index" :x="5.15 + index"
y="7" y="7"
rx="0.1" rx="0.1"
/> />
<text <text
v-if="props.showLabels" v-if="props.showLabels"
:x="5.5 + index" :x="5.5 + index"
y="9.5" y="9.5"
font-size="0.4" font-size="0.4"
text-anchor="middle" text-anchor="middle"
fill="#444" fill="#444"
> >
@@ -53,19 +78,21 @@
</text> </text>
</template> </template>
</g> </g>
<g> <g>
<template v-for="(location, index) in btnLocation" :key="`btn-${index}`"> <template
<rect v-for="(location, index) in btnLocation"
:key="`btn-${index}`"
>
<rect
class="interactive" class="interactive"
@click="toggleBtnStatus(index)" @click="toggleBtnStatus(index)"
width="0.65" width="0.65"
height="0.65" height="0.65"
fill="white" fill="white"
:x="5.175 + index" :x="5.175 + index"
:y="location" :y="location"
rx="0.1" rx="0.1"
opacity="1" opacity="1"
/> />
</template> </template>
</g> </g>
@@ -74,119 +101,112 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, ref, watch } from "vue"; import { SwitchClient } from "@/APIClient";
import { AuthManager } from "@/utils/AuthManager";
import { isUndefined } from "lodash";
import { ref, computed, watch, onMounted } from "vue";
interface Props { interface Props {
size?: number; size?: number;
enableDigitalTwin?: boolean;
switchCount?: number; switchCount?: number;
// 新增属性 initialValues?: string;
initialValues?: boolean[] | string; // 开关的初始状态,可以是布尔数组或逗号分隔的字符串 showLabels?: boolean;
showLabels?: boolean; // 是否显示标签
} }
const props = withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
size: 1, size: 1,
enableDigitalTwin: false,
switchCount: 6, switchCount: 6,
initialValues: () => [], initialValues: "",
showLabels: true showLabels: true,
}); });
// 计算实际宽高 const switchCount = computed(() => {
const width = computed(() => { if (props.enableDigitalTwin) return 5;
// 每个开关占用25px宽度再加上两侧边距(20px) else return props.switchCount;
return (props.switchCount * 25 + 20) * props.size;
}); });
const height = computed(() => 85 * props.size); // 高度保持固定比例
// 定义发出的事件 function getClient() {
const emit = defineEmits(['change', 'switch-toggle']); return AuthManager.createClient(SwitchClient);
}
// 解析初始值,支持字符串和数组两种格式 // 解析初始值
const parseInitialValues = () => { function parseInitialValues(): boolean[] {
if (Array.isArray(props.initialValues)) { if (Array.isArray(props.initialValues)) {
return [...props.initialValues].slice(0, props.switchCount); return [...props.initialValues].slice(0, switchCount.value);
} else if (typeof props.initialValues === 'string' && props.initialValues.trim() !== '') {
// 将逗号分隔的字符串转换为布尔数组
const values = props.initialValues.split(',')
.map(val => val.trim() === '1' || val.trim().toLowerCase() === 'true')
.slice(0, props.switchCount);
// 如果数组长度小于开关数量,用 false 填充
while (values.length < props.switchCount) {
values.push(false);
}
return values;
} }
// 默认返回全部为 false 的数组 if (
return Array(props.switchCount).fill(false); typeof props.initialValues === "string" &&
}; props.initialValues.trim() !== ""
) {
// 初始化按钮状态 const arr = props.initialValues
const btnStatus = ref(parseInitialValues()); .split(",")
.map((val) => val.trim() === "1" || val.trim().toLowerCase() === "true");
// 监听 switchCount 变化,调整开关状态数组 while (arr.length < props.switchCount) arr.push(false);
watch(() => props.switchCount, (newCount) => { return arr.slice(0, props.switchCount);
if (newCount !== btnStatus.value.length) {
// 如果新数量大于当前数量,则扩展数组
if (newCount > btnStatus.value.length) {
btnStatus.value = [
...btnStatus.value,
...Array(newCount - btnStatus.value.length).fill(false)
];
} else {
// 如果新数量小于当前数量,则截断数组
btnStatus.value = btnStatus.value.slice(0, newCount);
}
} }
}, { immediate: true }); return Array(switchCount.value).fill(false);
}
// 监听 initialValues 变化,更新开关状态 // 状态唯一真相
watch(() => props.initialValues, () => { const btnStatus = ref<boolean[]>(parseInitialValues());
btnStatus.value = parseInitialValues();
});
const btnLocation = computed(() => { // 计算宽高
return btnStatus.value.map((status) => { const width = computed(() => (switchCount.value * 25 + 20) * props.size);
return status ? 7.025 : 8.325; const height = computed(() => 85 * props.size);
});
});
function setBtnStatus(btnNum: number, isOn: boolean): void { // 按钮位置
if (btnNum >= 0 && btnNum < btnStatus.value.length) { const btnLocation = computed(() =>
btnStatus.value[btnNum] = isOn; btnStatus.value.map((status) => (status ? 7.025 : 8.325)),
emit('change', { index: btnNum, value: isOn, states: [...btnStatus.value] }); );
// 状态变更统一处理
function updateStatus(newStates: boolean[], index?: number) {
btnStatus.value = newStates.slice(0, switchCount.value);
if (props.enableDigitalTwin) {
try {
const client = getClient();
if (!isUndefined(index))
client.setSwitchOnOff(index + 1, newStates[index]);
else client.setMultiSwitchsOnOff(btnStatus.value);
} catch (error: any) {}
} }
} }
function toggleBtnStatus(btnNum: number): void { // 切换单个
if (btnNum >= 0 && btnNum < btnStatus.value.length) { function toggleBtnStatus(idx: number) {
btnStatus.value[btnNum] = !btnStatus.value[btnNum]; if (idx < 0 || idx >= btnStatus.value.length) return;
emit('switch-toggle', { const newStates = [...btnStatus.value];
index: btnNum, newStates[idx] = !newStates[idx];
value: btnStatus.value[btnNum], updateStatus(newStates, idx);
states: [...btnStatus.value]
});
}
} }
// 一次性设置所有开关状态 // 单个设置
function setAllStates(states: boolean[]): void { function setBtnStatus(idx: number, isOn: boolean) {
const newStates = states.slice(0, props.switchCount); if (idx < 0 || idx >= btnStatus.value.length) return;
while (newStates.length < props.switchCount) { const newStates = [...btnStatus.value];
newStates.push(false); newStates[idx] = isOn;
} updateStatus(newStates, idx);
btnStatus.value = newStates;
emit('change', { states: [...btnStatus.value] });
} }
// 暴露组件方法和状态 // 监听 props 变化只同步一次
defineExpose({ watch(
setBtnStatus, () => props.enableDigitalTwin,
toggleBtnStatus, (newVal) => {
setAllStates, const client = getClient();
getBtnStatus: () => [...btnStatus.value] client.setEnable(newVal);
}); },
{ immediate: true },
);
watch(
() => [switchCount.value, props.initialValues],
() => {
btnStatus.value = parseInitialValues();
updateStatus(btnStatus.value);
},
);
</script> </script>
<style scoped lang="postcss"> <style scoped lang="postcss">
@@ -194,17 +214,27 @@ defineExpose({
display: block; display: block;
padding: 0; padding: 0;
margin: 0; margin: 0;
line-height: 0; /* 移除行高导致的额外间距 */ line-height: 0;
font-size: 0; /* 防止文本节点造成的间距 */ font-size: 0;
box-sizing: content-box; box-sizing: content-box;
overflow: visible; overflow: visible;
} }
rect { rect {
transition: all 100ms ease-in-out; transition: all 100ms ease-in-out;
} }
.interactive { .interactive {
cursor: pointer; cursor: pointer;
} }
</style> </style>
<script lang="ts">
export function getDefaultProps() {
return {
size: 1,
enableDigitalTwin: false,
switchCount: 6,
initialValues: "",
showLabels: true,
};
}
</script>

View File

@@ -1,10 +1,9 @@
import './assets/main.css' import "./assets/main.css";
import { createApp } from 'vue' import { createApp } from "vue";
import { createPinia } from 'pinia' import { createPinia } from "pinia";
import App from '@/App.vue' import App from "@/App.vue";
import router from './router' import router from "./router";
const app = createApp(App).use(router).use(createPinia()).mount('#app')
const app = createApp(App).use(router).use(createPinia()).mount("#app");

View File

@@ -4,7 +4,8 @@ import AuthView from "../views/AuthView.vue";
import ProjectView from "../views/Project/Index.vue"; import ProjectView from "../views/Project/Index.vue";
import TestView from "../views/TestView.vue"; import TestView from "../views/TestView.vue";
import UserView from "@/views/User/Index.vue"; import UserView from "@/views/User/Index.vue";
import ExamView from "@/views/ExamView.vue"; import ExamView from "@/views/Exam/Index.vue";
import MarkdownEditor from "@/components/MarkdownEditor.vue";
const router = createRouter({ const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), history: createWebHistory(import.meta.env.BASE_URL),
@@ -15,6 +16,7 @@ const router = createRouter({
{ path: "/test", name: "test", component: TestView }, { path: "/test", name: "test", component: TestView },
{ path: "/user", name: "user", component: UserView }, { path: "/user", name: "user", component: UserView },
{ path: "/exam", name: "exam", component: ExamView }, { path: "/exam", name: "exam", component: ExamView },
{ path: "/markdown", name: "markdown", component: MarkdownEditor },
], ],
}); });

View File

@@ -7,12 +7,28 @@ import { isNumber } from "mathjs";
import { Mutex, withTimeout } from "async-mutex"; import { Mutex, withTimeout } from "async-mutex";
import { useConstraintsStore } from "@/stores/constraints"; import { useConstraintsStore } from "@/stores/constraints";
import { useDialogStore } from "./dialog"; import { useDialogStore } from "./dialog";
import { toFileParameterOrUndefined } from "@/utils/Common"; import {
base64ToArrayBuffer,
toFileParameterOrUndefined,
} from "@/utils/Common";
import { AuthManager } from "@/utils/AuthManager"; import { AuthManager } from "@/utils/AuthManager";
import { HubConnection, HubConnectionBuilder } from "@microsoft/signalr"; import { HubConnection, HubConnectionState } from "@microsoft/signalr";
import { getHubProxyFactory, getReceiverRegister } from "@/TypedSignalR.Client"; import {
import type { ResourceInfo } from "@/APIClient"; getHubProxyFactory,
import type { IJtagHub } from "@/TypedSignalR.Client/server.Hubs"; getReceiverRegister,
} from "@/utils/signalR/TypedSignalR.Client";
import {
JtagClient,
MatrixKeyClient,
PowerClient,
ResourceClient,
ResourcePurpose,
type ResourceInfo,
} from "@/APIClient";
import type {
IDigitalTubesHub,
IJtagHub,
} from "@/utils/signalR/TypedSignalR.Client/server.Hubs";
export const useEquipments = defineStore("equipments", () => { export const useEquipments = defineStore("equipments", () => {
// Global Stores // Global Stores
@@ -23,6 +39,7 @@ export const useEquipments = defineStore("equipments", () => {
const boardPort = useLocalStorage("fpga-board-port", 1234); const boardPort = useLocalStorage("fpga-board-port", 1234);
// Jtag // Jtag
const enableJtagBoundaryScan = ref(false);
const jtagBitstream = ref<File>(); const jtagBitstream = ref<File>();
const jtagBoundaryScanFreq = ref(100); const jtagBoundaryScanFreq = ref(100);
const jtagUserBitstreams = ref<ResourceInfo[]>([]); const jtagUserBitstreams = ref<ResourceInfo[]>([]);
@@ -36,8 +53,7 @@ export const useEquipments = defineStore("equipments", () => {
onMounted(async () => { onMounted(async () => {
// 每次挂载都重新创建连接 // 每次挂载都重新创建连接
jtagHubConnection.value = jtagHubConnection.value = AuthManager.createHubConnection("JtagHub");
AuthManager.createAuthenticatedJtagHubConnection();
jtagHubProxy.value = getHubProxyFactory("IJtagHub").createHubProxy( jtagHubProxy.value = getHubProxyFactory("IJtagHub").createHubProxy(
jtagHubConnection.value, jtagHubConnection.value,
); );
@@ -59,46 +75,6 @@ export const useEquipments = defineStore("equipments", () => {
} }
}); });
// Matrix Key
const matrixKeyStates = reactive(new Array<boolean>(16).fill(false));
const matrixKeypadClientMutex = withTimeout(
new Mutex(),
1000,
new Error("Matrixkeyclient Mutex Timeout!"),
);
// Power
const powerClientMutex = withTimeout(
new Mutex(),
1000,
new Error("Matrixkeyclient Mutex Timeout!"),
);
// Enable Setting
const enableJtagBoundaryScan = ref(false);
const enableMatrixKey = ref(false);
const enablePower = ref(false);
function setMatrixKey(
keyNum: number | string | undefined,
keyValue: boolean,
): boolean {
let _keyNum: number;
if (isString(keyNum)) {
_keyNum = toNumber(keyNum);
} else if (isNumber(keyNum)) {
_keyNum = keyNum;
} else {
return false;
}
if (z.number().nonnegative().max(16).safeParse(_keyNum).success) {
matrixKeyStates[_keyNum] = keyValue;
return true;
}
return false;
}
async function jtagBoundaryScanSetOnOff(enable: boolean) { async function jtagBoundaryScanSetOnOff(enable: boolean) {
if (isUndefined(jtagHubProxy.value)) { if (isUndefined(jtagHubProxy.value)) {
console.error("JtagHub Not Initialize..."); console.error("JtagHub Not Initialize...");
@@ -126,15 +102,15 @@ export const useEquipments = defineStore("equipments", () => {
async function jtagUploadBitstream( async function jtagUploadBitstream(
bitstream: File, bitstream: File,
examId?: string, examId?: string,
): Promise<number | null> { ): Promise<string | null> {
try { try {
// 自动开启电源 // 自动开启电源
await powerSetOnOff(true); await powerSetOnOff(true);
const resourceClient = AuthManager.createAuthenticatedResourceClient(); const resourceClient = AuthManager.createClient(ResourceClient);
const resp = await resourceClient.addResource( const resp = await resourceClient.addResource(
"bitstream", "bitstream",
"user", ResourcePurpose.User,
examId || null, examId || null,
toFileParameterOrUndefined(bitstream), toFileParameterOrUndefined(bitstream),
); );
@@ -152,7 +128,7 @@ export const useEquipments = defineStore("equipments", () => {
} }
} }
async function jtagDownloadBitstream(bitstreamId?: number): Promise<string> { async function jtagDownloadBitstream(bitstreamId?: string): Promise<string> {
if (bitstreamId === null || bitstreamId === undefined) { if (bitstreamId === null || bitstreamId === undefined) {
dialog.error("请先选择要下载的比特流"); dialog.error("请先选择要下载的比特流");
return ""; return "";
@@ -163,7 +139,7 @@ export const useEquipments = defineStore("equipments", () => {
// 自动开启电源 // 自动开启电源
await powerSetOnOff(true); await powerSetOnOff(true);
const jtagClient = AuthManager.createAuthenticatedJtagClient(); const jtagClient = AuthManager.createClient(JtagClient);
const resp = await jtagClient.downloadBitstream( const resp = await jtagClient.downloadBitstream(
boardAddr.value, boardAddr.value,
boardPort.value, boardPort.value,
@@ -185,7 +161,7 @@ export const useEquipments = defineStore("equipments", () => {
// 自动开启电源 // 自动开启电源
await powerSetOnOff(true); await powerSetOnOff(true);
const jtagClient = AuthManager.createAuthenticatedJtagClient(); const jtagClient = AuthManager.createClient(JtagClient);
const resp = await jtagClient.getDeviceIDCode( const resp = await jtagClient.getDeviceIDCode(
boardAddr.value, boardAddr.value,
boardPort.value, boardPort.value,
@@ -205,7 +181,7 @@ export const useEquipments = defineStore("equipments", () => {
// 自动开启电源 // 自动开启电源
await powerSetOnOff(true); await powerSetOnOff(true);
const jtagClient = AuthManager.createAuthenticatedJtagClient(); const jtagClient = AuthManager.createClient(JtagClient);
const resp = await jtagClient.setSpeed( const resp = await jtagClient.setSpeed(
boardAddr.value, boardAddr.value,
boardPort.value, boardPort.value,
@@ -220,12 +196,38 @@ export const useEquipments = defineStore("equipments", () => {
} }
} }
// Matrix Key
const enableMatrixKey = ref(false);
const matrixKeyStates = reactive(new Array<boolean>(16).fill(false));
const matrixKeypadClientMutex = withTimeout(
new Mutex(),
1000,
new Error("Matrixkeyclient Mutex Timeout!"),
);
function setMatrixKey(
keyNum: number | string | undefined,
keyValue: boolean,
): boolean {
let _keyNum: number;
if (isString(keyNum)) {
_keyNum = toNumber(keyNum);
} else if (isNumber(keyNum)) {
_keyNum = keyNum;
} else {
return false;
}
if (z.number().nonnegative().max(16).safeParse(_keyNum).success) {
matrixKeyStates[_keyNum] = keyValue;
return true;
}
return false;
}
async function matrixKeypadSetKeyStates(keyStates: boolean[]) { async function matrixKeypadSetKeyStates(keyStates: boolean[]) {
const release = await matrixKeypadClientMutex.acquire(); const release = await matrixKeypadClientMutex.acquire();
console.log("set Key !!!!!!!!!!!!");
try { try {
const matrixKeypadClient = const matrixKeypadClient = AuthManager.createClient(MatrixKeyClient);
AuthManager.createAuthenticatedMatrixKeyClient();
const resp = await matrixKeypadClient.setMatrixKeyStatus( const resp = await matrixKeypadClient.setMatrixKeyStatus(
boardAddr.value, boardAddr.value,
boardPort.value, boardPort.value,
@@ -243,9 +245,8 @@ export const useEquipments = defineStore("equipments", () => {
async function matrixKeypadEnable(enable: boolean) { async function matrixKeypadEnable(enable: boolean) {
const release = await matrixKeypadClientMutex.acquire(); const release = await matrixKeypadClientMutex.acquire();
try { try {
const matrixKeypadClient = AuthManager.createClient(MatrixKeyClient);
if (enable) { if (enable) {
const matrixKeypadClient =
AuthManager.createAuthenticatedMatrixKeyClient();
const resp = await matrixKeypadClient.enabelMatrixKey( const resp = await matrixKeypadClient.enabelMatrixKey(
boardAddr.value, boardAddr.value,
boardPort.value, boardPort.value,
@@ -253,8 +254,6 @@ export const useEquipments = defineStore("equipments", () => {
enableMatrixKey.value = resp; enableMatrixKey.value = resp;
return resp; return resp;
} else { } else {
const matrixKeypadClient =
AuthManager.createAuthenticatedMatrixKeyClient();
const resp = await matrixKeypadClient.disableMatrixKey( const resp = await matrixKeypadClient.disableMatrixKey(
boardAddr.value, boardAddr.value,
boardPort.value, boardPort.value,
@@ -271,10 +270,17 @@ export const useEquipments = defineStore("equipments", () => {
} }
} }
// Power
const powerClientMutex = withTimeout(
new Mutex(),
1000,
new Error("Matrixkeyclient Mutex Timeout!"),
);
const enablePower = ref(false);
async function powerSetOnOff(enable: boolean) { async function powerSetOnOff(enable: boolean) {
const release = await powerClientMutex.acquire(); const release = await powerClientMutex.acquire();
try { try {
const powerClient = AuthManager.createAuthenticatedPowerClient(); const powerClient = AuthManager.createClient(PowerClient);
const resp = await powerClient.setPowerOnOff( const resp = await powerClient.setPowerOnOff(
boardAddr.value, boardAddr.value,
boardPort.value, boardPort.value,
@@ -290,6 +296,74 @@ export const useEquipments = defineStore("equipments", () => {
} }
} }
// Seven Segment Display
const enableSevenSegmentDisplay = ref(false);
const sevenSegmentDisplayFrequency = ref(100);
const sevenSegmentDisplayData = ref<Uint8Array>();
const sevenSegmentDisplayHub = ref<HubConnection>();
const sevenSegmentDisplayHubProxy = ref<IDigitalTubesHub>();
async function sevenSegmentDisplaySetOnOff(enable: boolean) {
if (!sevenSegmentDisplayHub.value || !sevenSegmentDisplayHubProxy.value)
return;
if (sevenSegmentDisplayHub.value.state === HubConnectionState.Disconnected)
await sevenSegmentDisplayHub.value.start();
if (enable) {
await sevenSegmentDisplayHubProxy.value.startScan();
} else {
await sevenSegmentDisplayHubProxy.value.stopScan();
}
}
async function sevenSegmentDisplaySetFrequency(frequency: number) {
if (!sevenSegmentDisplayHub.value || !sevenSegmentDisplayHubProxy.value)
return;
if (sevenSegmentDisplayHub.value.state === HubConnectionState.Disconnected)
await sevenSegmentDisplayHub.value.start();
await sevenSegmentDisplayHubProxy.value.setFrequency(frequency);
}
async function sevenSegmentDisplayGetStatus() {
if (!sevenSegmentDisplayHub.value || !sevenSegmentDisplayHubProxy.value)
return;
if (sevenSegmentDisplayHub.value.state === HubConnectionState.Disconnected)
await sevenSegmentDisplayHub.value.start();
return await sevenSegmentDisplayHubProxy.value.getStatus();
}
async function handleSevenSegmentDisplayOnReceive(msg: string) {
const bytes = base64ToArrayBuffer(msg);
sevenSegmentDisplayData.value = new Uint8Array(bytes);
}
onMounted(async () => {
// 每次挂载都重新创建连接
sevenSegmentDisplayHub.value =
AuthManager.createHubConnection("DigitalTubesHub");
sevenSegmentDisplayHubProxy.value = getHubProxyFactory(
"IDigitalTubesHub",
).createHubProxy(sevenSegmentDisplayHub.value);
getReceiverRegister("IDigitalTubesReceiver").register(
sevenSegmentDisplayHub.value,
{
onReceive: handleSevenSegmentDisplayOnReceive,
},
);
});
onUnmounted(() => {
// 断开连接,清理资源
if (sevenSegmentDisplayHub.value) {
sevenSegmentDisplayHub.value.stop();
sevenSegmentDisplayHub.value = undefined;
sevenSegmentDisplayHubProxy.value = undefined;
}
});
return { return {
boardAddr, boardAddr,
boardPort, boardPort,
@@ -317,5 +391,13 @@ export const useEquipments = defineStore("equipments", () => {
enablePower, enablePower,
powerClientMutex, powerClientMutex,
powerSetOnOff, powerSetOnOff,
// Seven Segment Display
enableSevenSegmentDisplay,
sevenSegmentDisplayData,
sevenSegmentDisplayFrequency,
sevenSegmentDisplaySetOnOff,
sevenSegmentDisplaySetFrequency,
sevenSegmentDisplayGetStatus,
}; };
}); });

83
src/stores/progress.ts Normal file
View File

@@ -0,0 +1,83 @@
import type { HubConnection } from "@microsoft/signalr";
import type {
IProgressHub,
IProgressReceiver,
} from "@/utils/signalR/TypedSignalR.Client/server.Hubs";
import {
getHubProxyFactory,
getReceiverRegister,
} from "@/utils/signalR/TypedSignalR.Client";
import { ProgressStatus, type ProgressInfo } from "@/utils/signalR/server.Hubs";
import { onMounted, onUnmounted, ref, shallowRef } from "vue";
import { defineStore } from "pinia";
import { AuthManager } from "@/utils/AuthManager";
import { forEach, isUndefined } from "lodash";
export type ProgressCallback = (msg: ProgressInfo) => void;
export const useProgressStore = defineStore("progress", () => {
// taskId -> name -> callback
const progressCallbackFuncs = shallowRef<
Map<string, Map<string, ProgressCallback>>
>(new Map());
const progressHubConnection = shallowRef<HubConnection>();
const progressHubProxy = shallowRef<IProgressHub>();
const progressHubReceiver: IProgressReceiver = {
onReceiveProgress: async (msg) => {
const taskMap = progressCallbackFuncs.value.get(msg.taskId);
if (taskMap) {
for (const func of taskMap.values()) {
func(msg);
}
}
},
};
onMounted(async () => {
progressHubConnection.value =
AuthManager.createHubConnection("ProgressHub");
progressHubProxy.value = getHubProxyFactory("IProgressHub").createHubProxy(
progressHubConnection.value,
);
getReceiverRegister("IProgressReceiver").register(
progressHubConnection.value,
progressHubReceiver,
);
progressHubConnection.value.start();
});
onUnmounted(() => {
if (progressHubConnection.value) {
progressHubConnection.value.stop();
progressHubConnection.value = undefined;
progressHubProxy.value = undefined;
}
});
function register(progressId: string, name: string, func: ProgressCallback) {
progressHubProxy.value?.join(progressId);
let taskMap = progressCallbackFuncs.value.get(progressId);
if (!taskMap) {
taskMap = new Map();
progressCallbackFuncs.value?.set(progressId, taskMap);
}
taskMap.set(name, func);
}
function unregister(taskId: string, name: string) {
progressHubProxy.value?.leave(taskId);
const taskMap = progressCallbackFuncs.value.get(taskId);
if (taskMap) {
taskMap.delete(name);
if (taskMap.size === 0) {
progressCallbackFuncs.value?.delete(taskId);
}
}
}
return {
register,
unregister,
};
});

View File

@@ -1,67 +1,73 @@
import { ref, computed, watch } from 'vue' import { ref, computed, watch } from "vue";
import { defineStore } from 'pinia' import { defineStore } from "pinia";
// 本地存储主题的键名 // 本地存储主题的键名
const THEME_STORAGE_KEY = 'fpga-weblab-theme' const THEME_STORAGE_KEY = "fpga-weblab-theme";
export const useThemeStore = defineStore('theme', () => { export const useThemeStore = defineStore("theme", () => {
const allTheme = ["winter", "night"] const allTheme = ["winter", "night"];
const darkTheme = "night"; const darkTheme = "night";
const lightTheme = "winter"; const lightTheme = "winter";
// 尝试从本地存储中获取保存的主题 // 尝试从本地存储中获取保存的主题
const getSavedTheme = (): string | null => { const getSavedTheme = (): string | null => {
return localStorage.getItem(THEME_STORAGE_KEY) return localStorage.getItem(THEME_STORAGE_KEY);
} };
// 检测系统主题偏好 // 检测系统主题偏好
const getPreferredTheme = (): string => { const getPreferredTheme = (): string => {
const savedTheme = getSavedTheme() const savedTheme = getSavedTheme();
// 如果有保存的主题设置,优先使用 // 如果有保存的主题设置,优先使用
if (savedTheme && allTheme.includes(savedTheme)) { if (savedTheme && allTheme.includes(savedTheme)) {
return savedTheme return savedTheme;
} }
// 否则检测系统主题模式 // 否则检测系统主题模式
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches return window.matchMedia &&
? darkTheme : lightTheme window.matchMedia("(prefers-color-scheme: dark)").matches
} ? darkTheme
: lightTheme;
};
// 初始化主题为首选主题 // 初始化主题为首选主题
const currentTheme = ref(getPreferredTheme()) const currentTheme = ref(getPreferredTheme());
const currentMode = computed(() =>
currentTheme.value === darkTheme ? "dark" : "light",
);
// 保存主题到本地存储 // 保存主题到本地存储
const saveTheme = (theme: string) => { const saveTheme = (theme: string) => {
localStorage.setItem(THEME_STORAGE_KEY, theme) localStorage.setItem(THEME_STORAGE_KEY, theme);
} };
// 当主题变化时,保存到本地存储 // 当主题变化时,保存到本地存储
watch(currentTheme, (newTheme) => { watch(currentTheme, (newTheme) => {
saveTheme(newTheme) saveTheme(newTheme);
}) });
// 添加系统主题变化的监听 // 添加系统主题变化的监听
const setupThemeListener = () => { const setupThemeListener = () => {
if (window.matchMedia) { if (window.matchMedia) {
const colorSchemeQuery = window.matchMedia('(prefers-color-scheme: dark)') const colorSchemeQuery = window.matchMedia(
"(prefers-color-scheme: dark)",
);
const handler = (e: MediaQueryListEvent) => { const handler = (e: MediaQueryListEvent) => {
// 只有当用户没有手动设置过主题时,才跟随系统变化 // 只有当用户没有手动设置过主题时,才跟随系统变化
if (!getSavedTheme()) { if (!getSavedTheme()) {
currentTheme.value = e.matches ? darkTheme : lightTheme currentTheme.value = e.matches ? darkTheme : lightTheme;
} }
} };
// 添加主题变化监听器 // 添加主题变化监听器
colorSchemeQuery.addEventListener('change', handler) colorSchemeQuery.addEventListener("change", handler);
} }
} };
function setTheme(theme: string) { function setTheme(theme: string) {
const isContained: boolean = allTheme.includes(theme) const isContained: boolean = allTheme.includes(theme);
if (isContained) { if (isContained) {
currentTheme.value = theme currentTheme.value = theme;
saveTheme(theme) // 保存主题到本地存储 saveTheme(theme); // 保存主题到本地存储
} } else {
else { console.error(`Not have such theme: ${theme}`);
console.error(`Not have such theme: ${theme}`)
} }
} }
@@ -77,26 +83,26 @@ export const useThemeStore = defineStore('theme', () => {
} }
function isDarkTheme(): boolean { function isDarkTheme(): boolean {
return currentTheme.value == darkTheme return currentTheme.value == darkTheme;
} }
function isLightTheme(): boolean { function isLightTheme(): boolean {
return currentTheme.value == lightTheme return currentTheme.value == lightTheme;
} }
// 初始化时设置系统主题变化监听器 // 初始化时设置系统主题变化监听器
if (typeof window !== 'undefined') { if (typeof window !== "undefined") {
setupThemeListener() setupThemeListener();
} }
return { return {
allTheme, allTheme,
currentTheme, currentTheme,
currentMode,
setTheme, setTheme,
toggleTheme, toggleTheme,
isDarkTheme, isDarkTheme,
isLightTheme, isLightTheme,
setupThemeListener setupThemeListener,
} };
}) });

View File

@@ -1,325 +1,105 @@
import { import { DataClient } from "@/APIClient";
DataClient,
VideoStreamClient,
BsdlParserClient,
DDSClient,
JtagClient,
MatrixKeyClient,
PowerClient,
RemoteUpdateClient,
TutorialClient,
UDPClient,
LogicAnalyzerClient,
NetConfigClient,
OscilloscopeApiClient,
DebuggerClient,
ExamClient,
ResourceClient,
HdmiVideoStreamClient,
} from "@/APIClient";
import router from "@/router";
import { HubConnectionBuilder } from "@microsoft/signalr"; import { HubConnectionBuilder } from "@microsoft/signalr";
import axios, { type AxiosInstance } from "axios"; import axios, { type AxiosInstance } from "axios";
import { isNull } from "lodash";
// 支持的客户端类型联合类型
type SupportedClient =
| DataClient
| VideoStreamClient
| BsdlParserClient
| DDSClient
| JtagClient
| MatrixKeyClient
| PowerClient
| RemoteUpdateClient
| TutorialClient
| LogicAnalyzerClient
| UDPClient
| NetConfigClient
| OscilloscopeApiClient
| DebuggerClient
| ExamClient
| ResourceClient
| HdmiVideoStreamClient;
// 简单到让人想哭的认证管理器
export class AuthManager { export class AuthManager {
// 存储token到localStorage private static readonly TOKEN_KEY = "authToken";
public static setToken(token: string): void {
localStorage.setItem("authToken", token); // 核心数据:就是个字符串
static getToken(): string | null {
return localStorage.getItem(this.TOKEN_KEY);
} }
// 从localStorage获取token static setToken(token: string): void {
public static getToken(): string | null { localStorage.setItem(this.TOKEN_KEY, token);
return localStorage.getItem("authToken");
} }
// 清除token static clearToken(): void {
public static clearToken(): void { localStorage.removeItem(this.TOKEN_KEY);
localStorage.removeItem("authToken");
} }
// 检查是否已认证 // 核心功能创建带认证的HTTP配置
public static async isAuthenticated(): Promise<boolean> { static getAuthHeaders(): Record<string, string> {
return await AuthManager.verifyToken(); const token = this.getToken();
return token ? { Authorization: `Bearer ${token}` } : {};
} }
// 通用的为HTTP请求添加Authorization header的方法 // 一个方法搞定所有客户端不要17个垃圾方法
public static addAuthHeader(client: SupportedClient): void { static createClient<T>(
const token = AuthManager.getToken(); ClientClass: new (baseUrl?: string, config?: any) => T,
if (token) { baseUrl?: string,
// 创建一个自定义的 http 对象,包装原有的 fetch 方法
const customHttp = {
fetch: (url: RequestInfo, init?: RequestInit) => {
if (!init) init = {};
if (!init.headers) init.headers = {};
// 添加Authorization header
if (typeof init.headers === "object" && init.headers !== null) {
(init.headers as any)["Authorization"] = `Bearer ${token}`;
}
// 使用全局 fetch 或 window.fetch
return (window as any).fetch(url, init);
},
};
// 重新构造客户端,传入自定义的 http 对象
const ClientClass = client.constructor as new (
baseUrl?: string,
http?: any,
) => SupportedClient;
const newClient = new ClientClass(undefined, customHttp);
// 将新客户端的属性复制到原客户端(这是一个 workaround
// 更好的做法是返回新的客户端实例
Object.setPrototypeOf(client, Object.getPrototypeOf(newClient));
Object.assign(client, newClient);
}
}
// 私有方法创建带认证的HTTP客户端
private static createAuthenticatedHttp() {
const token = AuthManager.getToken();
if (!token) {
return null;
}
return {
fetch: (url: RequestInfo, init?: RequestInit) => {
if (!init) init = {};
if (!init.headers) init.headers = {};
if (typeof init.headers === "object" && init.headers !== null) {
(init.headers as any)["Authorization"] = `Bearer ${token}`;
}
return (window as any).fetch(url, init);
},
};
}
// 私有方法创建带认证的Axios实例
private static createAuthenticatedAxiosInstance(): AxiosInstance | null {
const token = AuthManager.getToken();
if (!token) return null;
const instance = axios.create();
instance.interceptors.request.use((config) => {
config.headers = config.headers || {};
(config.headers as any)["Authorization"] = `Bearer ${token}`;
return config;
});
return instance;
}
// 通用的创建已认证客户端的方法(使用泛型)
public static createAuthenticatedClient<T extends SupportedClient>(
ClientClass: new (baseUrl?: string, instance?: AxiosInstance) => T,
): T { ): T {
const axiosInstance = AuthManager.createAuthenticatedAxiosInstance();
return axiosInstance
? new ClientClass(undefined, axiosInstance)
: new ClientClass();
}
// 便捷方法:创建已配置认证的各种客户端
public static createAuthenticatedDataClient(): DataClient {
return AuthManager.createAuthenticatedClient(DataClient);
}
public static createAuthenticatedVideoStreamClient(): VideoStreamClient {
return AuthManager.createAuthenticatedClient(VideoStreamClient);
}
public static createAuthenticatedBsdlParserClient(): BsdlParserClient {
return AuthManager.createAuthenticatedClient(BsdlParserClient);
}
public static createAuthenticatedDDSClient(): DDSClient {
return AuthManager.createAuthenticatedClient(DDSClient);
}
public static createAuthenticatedJtagClient(): JtagClient {
return AuthManager.createAuthenticatedClient(JtagClient);
}
public static createAuthenticatedMatrixKeyClient(): MatrixKeyClient {
return AuthManager.createAuthenticatedClient(MatrixKeyClient);
}
public static createAuthenticatedPowerClient(): PowerClient {
return AuthManager.createAuthenticatedClient(PowerClient);
}
public static createAuthenticatedRemoteUpdateClient(): RemoteUpdateClient {
return AuthManager.createAuthenticatedClient(RemoteUpdateClient);
}
public static createAuthenticatedTutorialClient(): TutorialClient {
return AuthManager.createAuthenticatedClient(TutorialClient);
}
public static createAuthenticatedUDPClient(): UDPClient {
return AuthManager.createAuthenticatedClient(UDPClient);
}
public static createAuthenticatedLogicAnalyzerClient(): LogicAnalyzerClient {
return AuthManager.createAuthenticatedClient(LogicAnalyzerClient);
}
public static createAuthenticatedNetConfigClient(): NetConfigClient {
return AuthManager.createAuthenticatedClient(NetConfigClient);
}
public static createAuthenticatedOscilloscopeApiClient(): OscilloscopeApiClient {
return AuthManager.createAuthenticatedClient(OscilloscopeApiClient);
}
public static createAuthenticatedDebuggerClient(): DebuggerClient {
return AuthManager.createAuthenticatedClient(DebuggerClient);
}
public static createAuthenticatedExamClient(): ExamClient {
return AuthManager.createAuthenticatedClient(ExamClient);
}
public static createAuthenticatedResourceClient(): ResourceClient {
return AuthManager.createAuthenticatedClient(ResourceClient);
}
public static createAuthenticatedHdmiVideoStreamClient(): HdmiVideoStreamClient {
return AuthManager.createAuthenticatedClient(HdmiVideoStreamClient);
}
public static createAuthenticatedJtagHubConnection() {
const token = this.getToken(); const token = this.getToken();
if (isNull(token)) { if (!token) {
router.push("/login"); return new ClientClass(baseUrl);
throw Error("Token Null!");
} }
// 对于axios客户端
const axiosInstance = axios.create({
headers: this.getAuthHeaders(),
});
return new ClientClass(baseUrl, axiosInstance);
}
// SignalR连接 - 简单明了
static createHubConnection(
hubPath: "ProgressHub" | "JtagHub" | "DigitalTubesHub",
) {
return new HubConnectionBuilder() return new HubConnectionBuilder()
.withUrl("http://127.0.0.1:5000/hubs/JtagHub", { .withUrl(`http://127.0.0.1:5000/hubs/${hubPath}`, {
accessTokenFactory: () => token, accessTokenFactory: () => this.getToken() ?? "",
}) })
.withAutomaticReconnect() .withAutomaticReconnect()
.build(); .build();
} }
public static createAuthenticatedProgressHubConnection() { // 认证逻辑 - 去除所有废话
const token = this.getToken(); static async login(username: string, password: string): Promise<boolean> {
if (isNull(token)) {
router.push("/login");
throw Error("Token Null!");
}
return new HubConnectionBuilder()
.withUrl("http://127.0.0.1:5000/hubs/ProgressHub", {
accessTokenFactory: () => token,
})
.withAutomaticReconnect()
.build();
}
// 登录函数
public static async login(
username: string,
password: string,
): Promise<boolean> {
try { try {
const client = new DataClient(); const client = new DataClient();
const token = await client.login(username, password); const token = await client.login(username, password);
if (token) { if (!token) return false;
AuthManager.setToken(token);
// 验证token this.setToken(token);
const authClient = AuthManager.createAuthenticatedDataClient();
await authClient.testAuth();
return true; // 验证token - 如果失败直接抛异常
} await this.createClient(DataClient).testAuth();
return false;
} catch (error) {
AuthManager.clearToken();
throw error;
}
}
// 登出函数
public static logout(): void {
AuthManager.clearToken();
}
// 验证当前token是否有效
public static async verifyToken(): Promise<boolean> {
try {
const token = AuthManager.getToken();
if (!token) {
return false;
}
const client = AuthManager.createAuthenticatedDataClient();
await client.testAuth();
return true; return true;
} catch (error) { } catch {
AuthManager.clearToken(); this.clearToken();
return false; throw new Error("Login failed");
} }
} }
// 验证管理员权限 static logout(): void {
public static async verifyAdminAuth(): Promise<boolean> { this.clearToken();
}
// 简单的验证 - 不要搞复杂
static async isAuthenticated(): Promise<boolean> {
if (!this.getToken()) return false;
try { try {
const token = AuthManager.getToken(); await this.createClient(DataClient).testAuth();
if (!token) {
return false;
}
const client = AuthManager.createAuthenticatedDataClient();
await client.testAdminAuth();
return true; return true;
} catch (error) { } catch {
// 只有在token完全无效的情况下才清除token this.clearToken();
// 401错误表示token有效但权限不足不应清除token
if (error && typeof error === "object" && "status" in error) {
// 如果是403 (Forbidden) 或401 (Unauthorized)说明token有效但权限不足
if (error.status === 401 || error.status === 403) {
return false;
}
// 其他状态码可能表示token无效清除token
AuthManager.clearToken();
} else {
// 网络错误等不清除token
console.error("管理员权限验证失败:", error);
}
return false; return false;
} }
} }
// 检查客户端是否已配置认证 static async isAdminAuthenticated(): Promise<boolean> {
public static isClientAuthenticated(client: SupportedClient): boolean { if (!this.getToken()) return false;
const token = AuthManager.getToken();
return !!token; try {
await this.createClient(DataClient).testAdminAuth();
return true;
} catch {
this.clearToken();
return false;
}
} }
} }

View File

@@ -17,7 +17,7 @@ export interface BoardData extends Board {
const [useProvideBoardManager, useBoardManager] = createInjectionState(() => { const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
// 远程升级相关参数 // 远程升级相关参数
const devPort = 1234; const devPort = 1234;
const remoteUpdater = AuthManager.createAuthenticatedRemoteUpdateClient(); const remoteUpdater = AuthManager.createClient(RemoteUpdateClient);
// 统一的板卡数据 // 统一的板卡数据
const boards = ref<BoardData[]>([]); const boards = ref<BoardData[]>([]);
@@ -35,13 +35,13 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
async function getAllBoards(): Promise<{ success: boolean; error?: string }> { async function getAllBoards(): Promise<{ success: boolean; error?: string }> {
try { try {
// 验证管理员权限 // 验证管理员权限
const hasAdminAuth = await AuthManager.verifyAdminAuth(); const hasAdminAuth = await AuthManager.isAdminAuthenticated();
if (!hasAdminAuth) { if (!hasAdminAuth) {
console.error("权限验证失败"); console.error("权限验证失败");
return { success: false, error: "权限不足" }; return { success: false, error: "权限不足" };
} }
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
const result = await client.getAllBoards(); const result = await client.getAllBoards();
if (result) { if (result) {
@@ -77,7 +77,7 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
): Promise<{ success: boolean; error?: string; boardId?: string }> { ): Promise<{ success: boolean; error?: string; boardId?: string }> {
try { try {
// 验证管理员权限 // 验证管理员权限
const hasAdminAuth = await AuthManager.verifyAdminAuth(); const hasAdminAuth = await AuthManager.isAdminAuthenticated();
if (!hasAdminAuth) { if (!hasAdminAuth) {
console.error("权限验证失败"); console.error("权限验证失败");
return { success: false, error: "权限不足" }; return { success: false, error: "权限不足" };
@@ -89,11 +89,11 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
return { success: false, error: "参数不完整" }; return { success: false, error: "参数不完整" };
} }
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
const boardId = await client.addBoard(name); const boardId = await client.addBoard(name);
if (boardId) { if (boardId) {
console.log("新增板卡成功", { boardId, name}); console.log("新增板卡成功", { boardId, name });
// 刷新板卡列表 // 刷新板卡列表
await getAllBoards(); await getAllBoards();
return { success: true }; return { success: true };
@@ -119,7 +119,7 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
): Promise<{ success: boolean; error?: string }> { ): Promise<{ success: boolean; error?: string }> {
try { try {
// 验证管理员权限 // 验证管理员权限
const hasAdminAuth = await AuthManager.verifyAdminAuth(); const hasAdminAuth = await AuthManager.isAdminAuthenticated();
if (!hasAdminAuth) { if (!hasAdminAuth) {
console.error("权限验证失败"); console.error("权限验证失败");
return { success: false, error: "权限不足" }; return { success: false, error: "权限不足" };
@@ -130,7 +130,7 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
return { success: false, error: "板卡ID不能为空" }; return { success: false, error: "板卡ID不能为空" };
} }
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
const result = await client.deleteBoard(boardId); const result = await client.deleteBoard(boardId);
if (result > 0) { if (result > 0) {

View File

@@ -48,3 +48,23 @@ export function useOptionalInjection<T>(
const value = useFn(); const value = useFn();
return value ?? defaultValue; return value ?? defaultValue;
} }
export function formatDate(date: Date | string) {
const dateObj = typeof date === "string" ? new Date(date) : date;
return dateObj.toLocaleString("zh-CN", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
});
}
export function base64ToArrayBuffer(base64: string) {
var binaryString = atob(base64);
var bytes = new Uint8Array(binaryString.length);
for (var i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}

View File

@@ -3,8 +3,8 @@
/* tslint:disable */ /* tslint:disable */
// @ts-nocheck // @ts-nocheck
import type { HubConnection, IStreamResult, Subject } from '@microsoft/signalr'; import type { HubConnection, IStreamResult, Subject } from '@microsoft/signalr';
import type { IJtagHub, IProgressHub, IJtagReceiver, IProgressReceiver } from './server.Hubs'; import type { IDigitalTubesHub, IJtagHub, IProgressHub, IDigitalTubesReceiver, IJtagReceiver, IProgressReceiver } from './server.Hubs';
import type { ProgressInfo } from '../server.Hubs'; import type { DigitalTubeTaskStatus, ProgressInfo } from '../server.Hubs';
// components // components
@@ -43,11 +43,15 @@ class ReceiverMethodSubscription implements Disposable {
// API // API
export type HubProxyFactoryProvider = { export type HubProxyFactoryProvider = {
(hubType: "IDigitalTubesHub"): HubProxyFactory<IDigitalTubesHub>;
(hubType: "IJtagHub"): HubProxyFactory<IJtagHub>; (hubType: "IJtagHub"): HubProxyFactory<IJtagHub>;
(hubType: "IProgressHub"): HubProxyFactory<IProgressHub>; (hubType: "IProgressHub"): HubProxyFactory<IProgressHub>;
} }
export const getHubProxyFactory = ((hubType: string) => { export const getHubProxyFactory = ((hubType: string) => {
if(hubType === "IDigitalTubesHub") {
return IDigitalTubesHub_HubProxyFactory.Instance;
}
if(hubType === "IJtagHub") { if(hubType === "IJtagHub") {
return IJtagHub_HubProxyFactory.Instance; return IJtagHub_HubProxyFactory.Instance;
} }
@@ -57,11 +61,15 @@ export const getHubProxyFactory = ((hubType: string) => {
}) as HubProxyFactoryProvider; }) as HubProxyFactoryProvider;
export type ReceiverRegisterProvider = { export type ReceiverRegisterProvider = {
(receiverType: "IDigitalTubesReceiver"): ReceiverRegister<IDigitalTubesReceiver>;
(receiverType: "IJtagReceiver"): ReceiverRegister<IJtagReceiver>; (receiverType: "IJtagReceiver"): ReceiverRegister<IJtagReceiver>;
(receiverType: "IProgressReceiver"): ReceiverRegister<IProgressReceiver>; (receiverType: "IProgressReceiver"): ReceiverRegister<IProgressReceiver>;
} }
export const getReceiverRegister = ((receiverType: string) => { export const getReceiverRegister = ((receiverType: string) => {
if(receiverType === "IDigitalTubesReceiver") {
return IDigitalTubesReceiver_Binder.Instance;
}
if(receiverType === "IJtagReceiver") { if(receiverType === "IJtagReceiver") {
return IJtagReceiver_Binder.Instance; return IJtagReceiver_Binder.Instance;
} }
@@ -72,6 +80,39 @@ export const getReceiverRegister = ((receiverType: string) => {
// HubProxy // HubProxy
class IDigitalTubesHub_HubProxyFactory implements HubProxyFactory<IDigitalTubesHub> {
public static Instance = new IDigitalTubesHub_HubProxyFactory();
private constructor() {
}
public readonly createHubProxy = (connection: HubConnection): IDigitalTubesHub => {
return new IDigitalTubesHub_HubProxy(connection);
}
}
class IDigitalTubesHub_HubProxy implements IDigitalTubesHub {
public constructor(private connection: HubConnection) {
}
public readonly startScan = async (): Promise<boolean> => {
return await this.connection.invoke("StartScan");
}
public readonly stopScan = async (): Promise<boolean> => {
return await this.connection.invoke("StopScan");
}
public readonly setFrequency = async (frequency: number): Promise<boolean> => {
return await this.connection.invoke("SetFrequency", frequency);
}
public readonly getStatus = async (): Promise<DigitalTubeTaskStatus> => {
return await this.connection.invoke("GetStatus");
}
}
class IJtagHub_HubProxyFactory implements HubProxyFactory<IJtagHub> { class IJtagHub_HubProxyFactory implements HubProxyFactory<IJtagHub> {
public static Instance = new IJtagHub_HubProxyFactory(); public static Instance = new IJtagHub_HubProxyFactory();
@@ -120,11 +161,40 @@ class IProgressHub_HubProxy implements IProgressHub {
public readonly join = async (taskId: string): Promise<boolean> => { public readonly join = async (taskId: string): Promise<boolean> => {
return await this.connection.invoke("Join", taskId); return await this.connection.invoke("Join", taskId);
} }
public readonly leave = async (taskId: string): Promise<boolean> => {
return await this.connection.invoke("Leave", taskId);
}
public readonly getProgress = async (taskId: string): Promise<ProgressInfo> => {
return await this.connection.invoke("GetProgress", taskId);
}
} }
// Receiver // Receiver
class IDigitalTubesReceiver_Binder implements ReceiverRegister<IDigitalTubesReceiver> {
public static Instance = new IDigitalTubesReceiver_Binder();
private constructor() {
}
public readonly register = (connection: HubConnection, receiver: IDigitalTubesReceiver): Disposable => {
const __onReceive = (...args: [string]) => receiver.onReceive(...args);
connection.on("OnReceive", __onReceive);
const methodList: ReceiverMethod[] = [
{ methodName: "OnReceive", method: __onReceive }
]
return new ReceiverMethodSubscription(connection, methodList);
}
}
class IJtagReceiver_Binder implements ReceiverRegister<IJtagReceiver> { class IJtagReceiver_Binder implements ReceiverRegister<IJtagReceiver> {
public static Instance = new IJtagReceiver_Binder(); public static Instance = new IJtagReceiver_Binder();

View File

@@ -3,7 +3,27 @@
/* tslint:disable */ /* tslint:disable */
// @ts-nocheck // @ts-nocheck
import type { IStreamResult, Subject } from '@microsoft/signalr'; import type { IStreamResult, Subject } from '@microsoft/signalr';
import type { ProgressInfo } from '../server.Hubs'; import type { DigitalTubeTaskStatus, ProgressInfo } from '../server.Hubs';
export type IDigitalTubesHub = {
/**
* @returns Transpiled from System.Threading.Tasks.Task<bool>
*/
startScan(): Promise<boolean>;
/**
* @returns Transpiled from System.Threading.Tasks.Task<bool>
*/
stopScan(): Promise<boolean>;
/**
* @param frequency Transpiled from int
* @returns Transpiled from System.Threading.Tasks.Task<bool>
*/
setFrequency(frequency: number): Promise<boolean>;
/**
* @returns Transpiled from System.Threading.Tasks.Task<server.Hubs.DigitalTubeTaskStatus?>
*/
getStatus(): Promise<DigitalTubeTaskStatus>;
}
export type IJtagHub = { export type IJtagHub = {
/** /**
@@ -28,6 +48,24 @@ export type IProgressHub = {
* @returns Transpiled from System.Threading.Tasks.Task<bool> * @returns Transpiled from System.Threading.Tasks.Task<bool>
*/ */
join(taskId: string): Promise<boolean>; join(taskId: string): Promise<boolean>;
/**
* @param taskId Transpiled from string
* @returns Transpiled from System.Threading.Tasks.Task<bool>
*/
leave(taskId: string): Promise<boolean>;
/**
* @param taskId Transpiled from string
* @returns Transpiled from System.Threading.Tasks.Task<server.Hubs.ProgressInfo?>
*/
getProgress(taskId: string): Promise<ProgressInfo>;
}
export type IDigitalTubesReceiver = {
/**
* @param data Transpiled from byte[]
* @returns Transpiled from System.Threading.Tasks.Task
*/
onReceive(data: string): Promise<void>;
} }
export type IJtagReceiver = { export type IJtagReceiver = {

View File

@@ -2,13 +2,20 @@
/* eslint-disable */ /* eslint-disable */
/* tslint:disable */ /* tslint:disable */
/** Transpiled from server.Hubs.DigitalTubeTaskStatus */
export type DigitalTubeTaskStatus = {
/** Transpiled from int */
frequency: number;
/** Transpiled from bool */
isRunning: boolean;
}
/** Transpiled from server.Hubs.ProgressStatus */ /** Transpiled from server.Hubs.ProgressStatus */
export enum ProgressStatus { export enum ProgressStatus {
Pending = 0, Running = 0,
InProgress = 1, Completed = 1,
Completed = 2, Canceled = 2,
Canceled = 3, Failed = 3,
Failed = 4,
} }
/** Transpiled from server.Hubs.ProgressInfo */ /** Transpiled from server.Hubs.ProgressInfo */
@@ -17,7 +24,7 @@ export type ProgressInfo = {
taskId: string; taskId: string;
/** Transpiled from server.Hubs.ProgressStatus */ /** Transpiled from server.Hubs.ProgressStatus */
status: ProgressStatus; status: ProgressStatus;
/** Transpiled from int */ /** Transpiled from double */
progressPercent: number; progressPercent: number;
/** Transpiled from string */ /** Transpiled from string */
errorMessage: string; errorMessage: string;

View File

@@ -2,7 +2,10 @@
<div class="flex items-center justify-center min-h-screen bg-base-200"> <div class="flex items-center justify-center min-h-screen bg-base-200">
<div class="relative w-full max-w-md"> <div class="relative w-full max-w-md">
<!-- Login Card --> <!-- Login Card -->
<div v-if="!showSignUp" class="card card-dash h-80 w-100 shadow-xl bg-base-100"> <div
v-if="!showSignUp"
class="card card-dash h-80 w-100 shadow-xl bg-base-100"
>
<div class="card-body"> <div class="card-body">
<h1 class="card-title place-self-center my-3 text-2xl">用户登录</h1> <h1 class="card-title place-self-center my-3 text-2xl">用户登录</h1>
<div class="flex flex-col w-full h-full"> <div class="flex flex-col w-full h-full">
@@ -44,7 +47,10 @@
</div> </div>
<!-- Sign Up Card --> <!-- Sign Up Card -->
<div v-if="showSignUp" class="card card-dash h-96 w-100 shadow-xl bg-base-100"> <div
v-if="showSignUp"
class="card card-dash h-96 w-100 shadow-xl bg-base-100"
>
<div class="card-body"> <div class="card-body">
<h1 class="card-title place-self-center my-3 text-2xl">用户注册</h1> <h1 class="card-title place-self-center my-3 text-2xl">用户注册</h1>
<div class="flex flex-col w-full h-full"> <div class="flex flex-col w-full h-full">
@@ -122,7 +128,7 @@ const isSignUpLoading = ref(false);
const signUpData = ref({ const signUpData = ref({
username: "", username: "",
email: "", email: "",
password: "" password: "",
}); });
// 登录处理函数 // 登录处理函数
@@ -149,7 +155,7 @@ const handleLogin = async () => {
// 短暂延迟后跳转到project页面 // 短暂延迟后跳转到project页面
setTimeout(async () => { setTimeout(async () => {
await router.push("/project"); router.go(-1);
}, 1000); }, 1000);
} catch (error: any) { } catch (error: any) {
console.error("Login error:", error); console.error("Login error:", error);
@@ -180,7 +186,7 @@ const handleRegister = () => {
signUpData.value = { signUpData.value = {
username: "", username: "",
email: "", email: "",
password: "" password: "",
}; };
}; };
@@ -227,13 +233,13 @@ const handleSignUp = async () => {
const result = await dataClient.signUpUser( const result = await dataClient.signUpUser(
signUpData.value.username.trim(), signUpData.value.username.trim(),
signUpData.value.email.trim(), signUpData.value.email.trim(),
signUpData.value.password.trim() signUpData.value.password.trim(),
); );
if (result) { if (result) {
// 注册成功 // 注册成功
alertStore?.show("注册成功!请登录", "success", 2000); alertStore?.show("注册成功!请登录", "success", 2000);
// 延迟后返回登录页面 // 延迟后返回登录页面
setTimeout(() => { setTimeout(() => {
backToLogin(); backToLogin();
@@ -268,10 +274,10 @@ const handleSignUp = async () => {
// 页面初始化时检查是否已有有效token // 页面初始化时检查是否已有有效token
const checkExistingToken = async () => { const checkExistingToken = async () => {
try { try {
const isValid = await AuthManager.verifyToken(); const isValid = await AuthManager.isAuthenticated();
if (isValid) { if (isValid) {
// 如果token仍然有效直接跳转到project页面 // 如果token仍然有效直接跳转到project页面
await router.push("/project"); router.go(-1);
} }
} catch (error) { } catch (error) {
// token无效或验证失败继续显示登录页面 // token无效或验证失败继续显示登录页面

View File

@@ -0,0 +1,781 @@
<template>
<div v-if="isShowModal" class="modal modal-open overflow-hidden">
<div class="modal-box w-full max-w-7xl max-h-[90vh] p-0 overflow-hidden">
<div
class="flex justify-between items-center p-6 border-b border-base-300"
>
<h2 class="text-2xl font-bold text-base-content">
{{ mode === "create" ? "新建实验" : "编辑实验" }}
</h2>
<button @click="close" class="btn btn-sm btn-circle btn-ghost">
<svg
class="w-6 h-6"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
<form @submit.prevent="submitCreateExam" class="flex h-[calc(90vh-5rem)]">
<!-- 左侧基本信息 -->
<div class="w-110 p-6 overflow-y-auto border-r border-base-300">
<div class="space-y-6">
<h3 class="text-xl font-semibold text-base-content mb-4">
基本信息
</h3>
<!-- 实验ID -->
<div class="form-control">
<label class="label">
<span class="label-text font-medium">实验ID *</span>
</label>
<input
type="text"
v-model="editExamInfo.id"
class="input input-bordered w-full"
placeholder="例如: EXP001"
required
/>
</div>
<!-- 实验名称 -->
<div class="form-control">
<label class="label">
<span class="label-text font-medium">实验名称 *</span>
</label>
<input
type="text"
v-model="editExamInfo.name"
class="input input-bordered w-full"
placeholder="实验名称"
required
/>
</div>
<!-- 实验描述 -->
<div class="form-control">
<label class="label">
<span class="label-text font-medium">实验描述 *</span>
</label>
<textarea
v-model="editExamInfo.description"
class="textarea textarea-bordered w-full h-32"
placeholder="详细描述实验内容、目标和要求..."
required
></textarea>
</div>
<!-- 标签 -->
<div class="form-control">
<div class="flex flex-wrap gap-2 mb-3 min-h-[2rem]">
<span
v-for="(tag, index) in editExamInfo.tags"
:key="index"
class="badge badge-primary gap-2"
>
{{ tag }}
<button
type="button"
@click="removeTag(index)"
class="text-primary-content hover:text-error"
>
<svg
class="w-3 h-3"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</span>
</div>
<div class="flex gap-2">
<input
type="text"
v-model="newTagInput"
@keydown.enter.prevent="addTag"
class="input input-bordered flex-1"
placeholder="输入标签按回车添加"
/>
</div>
</div>
<!-- 难度等级 -->
<div class="form-control">
<div class="flex items-center justify-between p-4 rounded-lg">
<span class="label-text font-medium">难度等级 *</span>
<div class="flex items-center gap-4">
<div class="rating rating-lg">
<input
v-for="i in 5"
:key="i"
type="radio"
:value="i"
v-model="editExamInfo.difficulty"
class="mask mask-star-2 bg-orange-400"
/>
</div>
<span class="text-lg font-medium text-base-content"
>({{ editExamInfo.difficulty }}/5)</span
>
</div>
</div>
</div>
<!-- 可见性 -->
<div class="form-control">
<div class="p-4 rounded-lg">
<label class="label cursor-pointer justify-start gap-4">
<input
type="checkbox"
v-model="editExamInfo.isVisibleToUsers"
class="checkbox checkbox-primary"
/>
<div>
<span class="label-text font-medium">对学生可见</span>
<div class="text-sm text-base-content/70">
开启后学生可以在实验列表中看到此实验
</div>
</div>
</label>
</div>
</div>
<!-- 提交按钮 -->
<div class="pt-4 border-t border-base-300">
<div class="space-y-3">
<button
type="submit"
:disabled="isUpdating || !canCreateExam"
class="btn btn-primary w-full"
>
<span
v-if="isUpdating"
class="loading loading-spinner loading-sm mr-2"
></span>
{{
mode === "create"
? isUpdating
? "创建中..."
: "创建实验"
: isUpdating
? "更新中..."
: "更新实验"
}}
</button>
</div>
</div>
</div>
</div>
<!-- 右侧文件上传 -->
<div class="flex-1 p-6 overflow-y-auto">
<div class="space-y-6">
<h3 class="text-xl font-semibold text-base-content mb-4">
资源文件
</h3>
<!-- 第一行MD文档 图片资源 -->
<div class="grid grid-cols-2 gap-4">
<!-- MD文档 -->
<div class="space-y-2">
<label class="text-sm font-medium text-base-content"
>MD文档 (必需)</label
>
<div
class="border-2 border-dashed border-base-300 rounded-lg p-6 text-center cursor-pointer hover:border-primary hover:bg-primary/5 transition-colors aspect-square flex items-center justify-center"
@click="mdFileInput?.click()"
@dragover.prevent
@dragenter.prevent
@drop.prevent="(e) => handleFileDrop(e, 'md')"
>
<div
v-if="!uploadFiles.mdFile"
class="flex flex-col items-center gap-3"
>
<FileTextIcon
class="w-12 h-12 text-base-content opacity-40"
/>
<div class="text-sm text-base-content/70 text-center">
<div class="font-medium mb-1">点击或拖拽上传</div>
<div class="text-xs">支持 .md 文件</div>
</div>
</div>
<div v-else class="flex flex-col items-center gap-2">
<FileTextIcon class="w-8 h-8 text-success" />
<div class="text-xs font-medium text-success text-center">
{{ uploadFiles.mdFile.name }}
</div>
<div class="text-xs text-base-content/50">点击重新选择</div>
</div>
</div>
<input
type="file"
ref="mdFileInput"
@change="(e) => handleFileChange(e, 'md')"
accept=".md"
class="hidden"
/>
</div>
<!-- 图片资源 -->
<div class="space-y-2">
<label class="text-sm font-medium text-base-content"
>图片资源 (可选)</label
>
<div
class="border-2 border-dashed border-base-300 rounded-lg p-6 text-center cursor-pointer hover:border-primary hover:bg-primary/5 transition-colors aspect-square flex items-center justify-center"
@click="imageFilesInput?.click()"
@dragover.prevent
@dragenter.prevent
@drop.prevent="(e) => handleFileDrop(e, 'image')"
>
<div
v-if="uploadFiles.imageFiles.length === 0"
class="flex flex-col items-center gap-3"
>
<ImageIcon class="w-12 h-12 text-base-content opacity-40" />
<div class="text-sm text-base-content/70 text-center">
<div class="font-medium mb-1">点击或拖拽上传</div>
<div class="text-xs">支持 PNG, JPG, GIF 等图片格式</div>
</div>
</div>
<div v-else class="flex flex-col items-center gap-2">
<ImageIcon class="w-8 h-8 text-success" />
<div class="text-xs font-medium text-success">
{{ uploadFiles.imageFiles.length }} 个文件
</div>
<div class="text-xs text-base-content/50">点击重新选择</div>
</div>
</div>
<input
type="file"
ref="imageFilesInput"
@change="(e) => handleFileChange(e, 'image')"
accept="image/*"
multiple
class="hidden"
/>
</div>
</div>
<!-- 第二行示例比特流 画布模板 -->
<div class="grid grid-cols-2 gap-4">
<!-- 示例比特流 -->
<div class="space-y-2">
<label class="text-sm font-medium text-base-content"
>示例比特流 (可选)</label
>
<div
class="border-2 border-dashed border-base-300 rounded-lg p-6 text-center cursor-pointer hover:border-primary hover:bg-primary/5 transition-colors aspect-square flex items-center justify-center"
@click="bitstreamFilesInput?.click()"
@dragover.prevent
@dragenter.prevent
@drop.prevent="(e) => handleFileDrop(e, 'bitstream')"
>
<div
v-if="uploadFiles.bitstreamFiles.length === 0"
class="flex flex-col items-center gap-3"
>
<BinaryIcon
class="w-12 h-12 text-base-content opacity-40"
/>
<div class="text-sm text-base-content/70 text-center">
<div class="font-medium mb-1">点击或拖拽上传</div>
<div class="text-xs">支持 .sbit, .bit, .bin 文件</div>
</div>
</div>
<div v-else class="flex flex-col items-center gap-2">
<BinaryIcon class="w-8 h-8 text-success" />
<div class="text-xs font-medium text-success">
{{ uploadFiles.bitstreamFiles.length }} 个文件
</div>
<div class="text-xs text-base-content/50">点击重新选择</div>
</div>
</div>
<input
type="file"
ref="bitstreamFilesInput"
@change="(e) => handleFileChange(e, 'bitstream')"
accept=".sbit,.bit,.bin"
multiple
class="hidden"
/>
</div>
<!-- 画布模板 -->
<div class="space-y-2">
<label class="text-sm font-medium text-base-content"
>画布模板 (可选)</label
>
<div
class="border-2 border-dashed border-base-300 rounded-lg p-6 text-center cursor-pointer hover:border-primary hover:bg-primary/5 transition-colors aspect-square flex items-center justify-center"
@click="canvasFilesInput?.click()"
@dragover.prevent
@dragenter.prevent
@drop.prevent="(e) => handleFileDrop(e, 'canvas')"
>
<div
v-if="uploadFiles.canvasFiles.length === 0"
class="flex flex-col items-center gap-3"
>
<FileJsonIcon
class="w-12 h-12 text-base-content opacity-40"
/>
<div class="text-sm text-base-content/70 text-center">
<div class="font-medium mb-1">点击或拖拽上传</div>
<div class="text-xs">支持 .json 文件</div>
</div>
</div>
<div v-else class="flex flex-col items-center gap-2">
<FileJsonIcon class="w-8 h-8 text-success" />
<div class="text-xs font-medium text-success">
{{ uploadFiles.canvasFiles.length }} 个文件
</div>
<div class="text-xs text-base-content/50">点击重新选择</div>
</div>
</div>
<input
type="file"
ref="canvasFilesInput"
@change="(e) => handleFileChange(e, 'canvas')"
accept=".json"
multiple
class="hidden"
/>
</div>
</div>
<!-- 第三行资源包 (单独一个居中) -->
<div class="flex justify-center">
<div class="w-1/2 space-y-2">
<label class="text-sm font-medium text-base-content"
>资源包 (可选)</label
>
<div
class="border-2 border-dashed border-base-300 rounded-lg p-6 text-center cursor-pointer hover:border-primary hover:bg-primary/5 transition-colors aspect-square flex items-center justify-center"
@click="resourceFileInput?.click()"
@dragover.prevent
@dragenter.prevent
@drop.prevent="(e) => handleFileDrop(e, 'resource')"
>
<div
v-if="!uploadFiles.resourceFile"
class="flex flex-col items-center gap-3"
>
<FileArchiveIcon
class="w-12 h-12 text-base-content opacity-40"
/>
<div class="text-sm text-base-content/70 text-center">
<div class="font-medium mb-1">点击或拖拽上传</div>
<div class="text-xs">支持 .zip, .rar, .7z 文件</div>
</div>
</div>
<div v-else class="flex flex-col items-center gap-2">
<FileArchiveIcon class="w-8 h-8 text-success" />
<div class="text-xs font-medium text-success text-center">
{{ uploadFiles.resourceFile.name }}
</div>
<div class="text-xs text-base-content/50">点击重新选择</div>
</div>
</div>
<input
type="file"
ref="resourceFileInput"
@change="(e) => handleFileChange(e, 'resource')"
accept=".zip,.rar,.7z"
class="hidden"
/>
</div>
</div>
</div>
</div>
</form>
</div>
<div class="modal-backdrop" @click="close"></div>
</div>
</template>
<script setup lang="ts">
import {
FileTextIcon,
ImageIcon,
BinaryIcon,
FileArchiveIcon,
FileJsonIcon,
} from "lucide-vue-next";
import {
ExamClient,
ExamDto,
ResourceClient,
type FileParameter,
} from "@/APIClient";
import { useAlertStore } from "@/components/Alert";
import { AuthManager } from "@/utils/AuthManager";
import { useRequiredInjection } from "@/utils/Common";
import { defineModel, ref, computed } from "vue";
import { mod } from "mathjs";
import type { ExamInfo } from "@/APIClient";
type Mode = "create" | "edit";
const isShowModal = defineModel<boolean>("isShowModal", {
default: false,
});
const emits = defineEmits<{
editFinished: [examId: string];
}>();
const alert = useRequiredInjection(useAlertStore);
const editExamInfo = ref({
id: "",
name: "",
description: "",
tags: [] as string[],
difficulty: 1,
isVisibleToUsers: true,
});
const isUpdating = ref(false);
const mode = ref<Mode>("create");
const newTagInput = ref("");
// 文件上传相关
const uploadFiles = ref({
mdFile: null as File | null,
imageFiles: [] as File[],
bitstreamFiles: [] as File[],
canvasFiles: [] as File[],
resourceFile: null as File | null,
});
// 文件输入引用
const mdFileInput = ref<HTMLInputElement>();
const imageFilesInput = ref<HTMLInputElement>();
const bitstreamFilesInput = ref<HTMLInputElement>();
const canvasFilesInput = ref<HTMLInputElement>();
const resourceFileInput = ref<HTMLInputElement>();
// 计算属性
const canCreateExam = computed(() => {
return (
editExamInfo.value.id.trim() !== "" &&
editExamInfo.value.name.trim() !== "" &&
editExamInfo.value.description.trim() !== "" &&
(uploadFiles.value.mdFile !== null || mode.value === "edit")
);
});
// 文件类型定义
type FileType = "md" | "image" | "bitstream" | "canvas" | "resource";
// 统一文件处理方法
const handleFileChange = (event: Event, fileType: FileType) => {
const target = event.target as HTMLInputElement;
if (!target.files) return;
switch (fileType) {
case "md":
if (target.files.length > 0) {
uploadFiles.value.mdFile = target.files[0];
}
break;
case "image":
uploadFiles.value.imageFiles = Array.from(target.files);
break;
case "bitstream":
uploadFiles.value.bitstreamFiles = Array.from(target.files);
break;
case "canvas":
uploadFiles.value.canvasFiles = Array.from(target.files);
break;
case "resource":
if (target.files.length > 0) {
uploadFiles.value.resourceFile = target.files[0];
}
break;
}
};
const handleFileDrop = (event: DragEvent, fileType: FileType) => {
const files = event.dataTransfer?.files;
if (!files || files.length === 0) return;
switch (fileType) {
case "md":
const mdFile = files[0];
if (mdFile.name.endsWith(".md")) {
uploadFiles.value.mdFile = mdFile;
}
break;
case "image":
const imageFiles = Array.from(files).filter((file) =>
file.type.startsWith("image/"),
);
uploadFiles.value.imageFiles = imageFiles;
break;
case "bitstream":
const bitstreamFiles = Array.from(files).filter(
(file) =>
file.name.endsWith(".sbit") ||
file.name.endsWith(".bit") ||
file.name.endsWith(".bin"),
);
uploadFiles.value.bitstreamFiles = bitstreamFiles;
break;
case "canvas":
const canvasFiles = Array.from(files).filter((file) =>
file.name.endsWith(".json"),
);
uploadFiles.value.canvasFiles = canvasFiles;
break;
case "resource":
const resourceFile = files[0];
if (
resourceFile.name.endsWith(".zip") ||
resourceFile.name.endsWith(".rar") ||
resourceFile.name.endsWith(".7z")
) {
uploadFiles.value.resourceFile = resourceFile;
}
break;
}
};
// 标签管理
const addTag = (event?: Event) => {
if (event) {
event.preventDefault();
event.stopPropagation();
}
const tag = newTagInput.value.trim();
if (tag && !editExamInfo.value.tags.includes(tag)) {
editExamInfo.value.tags.push(tag);
newTagInput.value = "";
}
};
const removeTag = (index: number) => {
editExamInfo.value.tags.splice(index, 1);
};
const resetCreateForm = () => {
editExamInfo.value = {
id: "",
name: "",
description: "",
tags: [],
difficulty: 1,
isVisibleToUsers: true,
};
newTagInput.value = "";
uploadFiles.value = {
mdFile: null,
imageFiles: [],
bitstreamFiles: [],
canvasFiles: [],
resourceFile: null,
};
// 重置文件输入
if (mdFileInput.value) mdFileInput.value.value = "";
if (imageFilesInput.value) imageFilesInput.value.value = "";
if (bitstreamFilesInput.value) bitstreamFilesInput.value.value = "";
if (canvasFilesInput.value) canvasFilesInput.value.value = "";
if (resourceFileInput.value) resourceFileInput.value.value = "";
};
// 提交创建实验
const submitCreateExam = async () => {
if (isUpdating.value) return;
// 验证必填字段
if (
!editExamInfo.value.id ||
!editExamInfo.value.name ||
!editExamInfo.value.description
) {
alert?.error("请填写所有必填字段");
return;
}
if (!uploadFiles.value.mdFile) {
alert.error("请上传MD文档");
return;
}
isUpdating.value = true;
try {
const client = AuthManager.createClient(ExamClient);
let exam: ExamInfo;
if (mode.value === "create") {
// 创建实验请求
const createRequest = new ExamDto({
id: editExamInfo.value.id,
name: editExamInfo.value.name,
description: editExamInfo.value.description,
tags: editExamInfo.value.tags,
difficulty: editExamInfo.value.difficulty,
isVisibleToUsers: editExamInfo.value.isVisibleToUsers,
});
// 创建实验
exam = await client.createExam(createRequest);
console.log("实验创建成功:", exam);
} else if (mode.value === "edit") {
// 编辑实验请求
const editRequest = new ExamDto({
id: editExamInfo.value.id,
name: editExamInfo.value.name,
description: editExamInfo.value.description,
tags: editExamInfo.value.tags,
difficulty: editExamInfo.value.difficulty,
isVisibleToUsers: editExamInfo.value.isVisibleToUsers,
});
// 编辑实验
exam = await client.updateExam(editRequest);
console.log("实验编辑成功:", exam);
} else {
// 处理其他模式
console.error("未知的模式:", mode.value);
throw new Error("未知的模式");
}
// 上传文件
await uploadExamResources(exam.id);
alert.success("实验创建成功");
close();
emits("editFinished", exam.id);
} catch (err: any) {
console.error("创建实验失败:", err);
alert.error(err.message || "创建实验失败");
} finally {
isUpdating.value = false;
}
};
// 上传实验资源
async function uploadExamResources(examId: string) {
const client = AuthManager.createClient(ResourceClient);
try {
// 上传MD文档
if (uploadFiles.value.mdFile) {
const mdFileParam: FileParameter = {
data: uploadFiles.value.mdFile,
fileName: uploadFiles.value.mdFile.name,
};
await client.addResource("doc", "template", examId, mdFileParam);
console.log("MD文档上传成功");
}
// 上传图片资源
for (const imageFile of uploadFiles.value.imageFiles) {
const imageFileParam: FileParameter = {
data: imageFile,
fileName: imageFile.name,
};
await client.addResource("image", "template", examId, imageFileParam);
console.log("图片上传成功:", imageFile.name);
}
// 上传比特流文件
for (const bitstreamFile of uploadFiles.value.bitstreamFiles) {
const bitstreamFileParam: FileParameter = {
data: bitstreamFile,
fileName: bitstreamFile.name,
};
await client.addResource(
"bitstream",
"template",
examId,
bitstreamFileParam,
);
console.log("比特流文件上传成功:", bitstreamFile.name);
}
// 上传画布模板
for (const canvasFile of uploadFiles.value.canvasFiles) {
const canvasFileParam: FileParameter = {
data: canvasFile,
fileName: canvasFile.name,
};
await client.addResource("canvas", "template", examId, canvasFileParam);
console.log("画布模板上传成功:", canvasFile.name);
}
// 上传资源包
if (uploadFiles.value.resourceFile) {
const resourceFileParam: FileParameter = {
data: uploadFiles.value.resourceFile,
fileName: uploadFiles.value.resourceFile.name,
};
await client.addResource(
"resource",
"template",
examId,
resourceFileParam,
);
console.log("资源包上传成功");
}
} catch (err: any) {
console.error("资源上传失败:", err);
alert?.error("部分资源上传失败: " + (err.message || "未知错误"));
}
}
function show() {
isShowModal.value = true;
}
function close() {
isShowModal.value = false;
mode.value = "create";
resetCreateForm();
}
async function editExam(examId: string) {
const client = AuthManager.createClient(ExamClient);
const examInfo = await client.getExam(examId);
editExamInfo.value = {
id: examInfo.id,
name: examInfo.name,
description: examInfo.description,
tags: examInfo.tags,
difficulty: examInfo.difficulty,
isVisibleToUsers: examInfo.isVisibleToUsers,
};
mode.value = "edit";
show();
}
defineExpose({
show,
close,
editExam,
});
</script>
<style lang="postcss" scoped></style>

View File

@@ -0,0 +1,358 @@
<template>
<div v-if="show" class="modal modal-open overflow-hidden">
<div
class="modal-box w-full max-w-6xl h-[90vh] max-h-[90vh] p-0 overflow-hidden"
>
<div
class="flex justify-between items-center p-6 border-b border-base-300"
>
<h2 class="text-2xl font-bold text-base-content">
{{ selectedExam.id }} - {{ selectedExam.name }}
</h2>
<button
@click="closeExamDetail"
class="btn btn-sm btn-circle btn-ghost"
>
<svg
class="w-6 h-6"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
<div class="flex h-[calc(90vh-5rem)]">
<!-- 左侧实验信息和描述 -->
<div class="flex-1 p-6 overflow-y-auto border-r border-base-300">
<div class="space-y-6">
<!-- 实验信息 -->
<div class="card bg-base-200">
<div class="card-body">
<h3 class="card-title text-lg mb-4">实验信息</h3>
<div class="space-y-3">
<div class="flex">
<span class="font-medium text-base-content w-24"
>实验ID</span
>
<span class="text-base-content/70">{{
selectedExam.id
}}</span>
</div>
<div class="flex">
<span class="font-medium text-base-content w-24"
>实验名称</span
>
<span class="text-base-content/70">{{
selectedExam.name
}}</span>
</div>
<div class="flex">
<span class="font-medium text-base-content w-24"
>难度等级</span
>
<div class="flex items-center gap-2">
<div class="rating rating-sm">
<span
v-for="i in 5"
:key="i"
class="mask mask-star-2"
:class="
i <= selectedExam.difficulty
? 'bg-orange-400'
: 'bg-base-300'
"
></span>
</div>
<span class="text-sm text-base-content/50"
>({{ selectedExam.difficulty }}/5)</span
>
</div>
</div>
<div
v-if="selectedExam.tags && selectedExam.tags.length > 0"
class="flex"
>
<span class="font-medium text-base-content w-24"
>标签</span
>
<div class="flex flex-wrap gap-1">
<span
v-for="tag in selectedExam.tags"
:key="tag"
class="badge badge-outline badge-sm"
>{{ tag }}</span
>
</div>
</div>
<div class="flex">
<span class="font-medium text-base-content w-24"
>创建时间</span
>
<span class="text-base-content/70">{{
formatDate(selectedExam.createdTime)
}}</span>
</div>
<div class="flex">
<span class="font-medium text-base-content w-24"
>更新时间</span
>
<span class="text-base-content/70">{{
formatDate(selectedExam.updatedTime)
}}</span>
</div>
<div class="flex">
<span class="font-medium text-base-content w-24"
>可见性</span
>
<span class="text-base-content/70">{{
selectedExam.isVisibleToUsers
? "对学生可见"
: "仅管理员可见"
}}</span>
</div>
</div>
</div>
</div>
<!-- 实验描述 -->
<div class="card bg-base-200">
<div class="card-body">
<h3 class="card-title text-lg mb-4">实验描述</h3>
<div class="prose prose-sm max-w-none">
<p class="text-base-content/70">
{{ selectedExam.description }}
</p>
</div>
</div>
</div>
</div>
</div>
<!-- 右侧完成情况和控制 -->
<div class="w-80 p-6 bg-base-200 overflow-y-auto">
<div class="space-y-6">
<!-- 完成情况 -->
<div class="card bg-base-100">
<div class="card-body">
<h3 class="card-title text-lg mb-4">完成情况</h3>
<div class="space-y-4">
<div class="flex justify-between items-center">
<span class="text-base-content/70">当前状态</span>
<div class="badge badge-error">未完成</div>
</div>
<div class="flex justify-between items-center">
<span class="text-base-content/70">批阅状态</span>
<div class="badge badge-ghost">待提交</div>
</div>
<div class="flex justify-between items-center">
<span class="text-base-content/70">成绩</span>
<span class="text-base-content/50">未评分</span>
</div>
</div>
<div class="divider"></div>
<!-- 提交历史 -->
<div class="space-y-3">
<h4 class="font-medium text-base-content">提交历史</h4>
<div
v-if="isUndefined(commitsList)"
class="text-sm text-base-content/50 text-center py-4"
>
暂无提交记录
</div>
<div v-else class="overflow-y-auto">
<ul class="steps steps-vertical">
<li class="step step-primary">Register</li>
<li class="step step-primary">Choose plan</li>
<li class="step">Purchase</li>
<li class="step">Receive Product</li>
</ul>
</div>
</div>
</div>
</div>
<!-- 操作按钮 -->
<div class="space-y-3">
<button @click="startExam" class="btn btn-primary w-full">
<svg
class="w-5 h-5 mr-2"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M14.828 14.828a4 4 0 01-5.656 0M9 10h1m4 0h1m-6 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
开始实验
</button>
<button
@click="downloadResources"
class="btn btn-outline w-full"
:disabled="downloadingResources"
>
<svg
class="w-5 h-5 mr-2"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
/>
</svg>
<span v-if="downloadingResources">下载中...</span>
<span v-else>下载资源包</span>
</button>
<button class="btn btn-outline w-full">
<svg
class="w-5 h-5 mr-2"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
/>
</svg>
查看记录
</button>
</div>
</div>
</div>
</div>
</div>
<div class="modal-backdrop" @click="closeExamDetail"></div>
</div>
</template>
<script setup lang="ts">
import {
ExamClient,
ResourceClient,
ResourcePurpose,
type ExamInfo,
type ResourceInfo,
} from "@/APIClient";
import { useAlertStore } from "@/components/Alert";
import { AuthManager } from "@/utils/AuthManager";
import { useRequiredInjection } from "@/utils/Common";
import { defineModel, ref } from "vue";
import { useRouter } from "vue-router";
import { formatDate } from "@/utils/Common";
import { computed } from "vue";
import { watch } from "vue";
import { isNull, isUndefined } from "lodash";
const alertStore = useRequiredInjection(useAlertStore);
const router = useRouter();
const show = defineModel<boolean>("show", {
default: false,
});
const props = defineProps<{
selectedExam: ExamInfo;
}>();
const commitsList = ref<ResourceInfo[]>();
async function updateCommits() {
const client = AuthManager.createClient(ExamClient);
const list = await client.getCommitsByExamId(props.selectedExam.id);
commitsList.value = list;
}
watch(() => props.selectedExam, updateCommits);
// Download resources
const downloadingResources = ref(false);
const downloadResources = async () => {
if (!props.selectedExam || downloadingResources.value) return;
downloadingResources.value = true;
try {
const resourceClient = AuthManager.createClient(ResourceClient);
// 获取资源包列表(模板资源)
const resourceList = await resourceClient.getResourceList(
props.selectedExam.id,
"resource",
ResourcePurpose.Template,
);
if (resourceList && resourceList.length > 0) {
// 使用新的ResourceClient API获取第一个资源包
const resourceId = resourceList[0].id;
const fileResponse = await resourceClient.getResourceById(resourceId);
// 创建Blob URL
const blobUrl = URL.createObjectURL(fileResponse.data);
// 创建下载链接
const link = document.createElement("a");
link.href = blobUrl;
link.download =
fileResponse.fileName ||
resourceList[0].name ||
`${props.selectedExam.name}_资源包`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// 清理Blob URL
URL.revokeObjectURL(blobUrl);
alertStore.success("资料下载成功");
console.log("资料下载成功:", props.selectedExam.id);
} else {
alertStore.error("该实验暂无资料包");
}
} catch (err: any) {
alertStore.error(err.message || "下载资料失败");
console.error("下载资料失败:", err);
} finally {
downloadingResources.value = false;
}
};
// 开始实验
const startExam = () => {
if (props.selectedExam) {
// 跳转到项目页面传递实验ID
console.log("开始实验:", props.selectedExam.id);
router.push({
name: "project",
query: { examId: props.selectedExam.id },
});
}
};
const closeExamDetail = () => {
show.value = false;
};
</script>
<style lang="postcss" scoped></style>

312
src/views/Exam/Index.vue Normal file
View File

@@ -0,0 +1,312 @@
<template>
<div class="min-h-screen bg-base-100 p-5">
<div class="max-w-7xl mx-auto">
<div
class="flex justify-between items-center mb-8 pb-6 border-b border-base-300"
>
<h1 class="text-3xl font-bold text-base-content">实验列表</h1>
</div>
<div
v-if="loading"
class="flex flex-col items-center justify-center min-h-[300px]"
>
<div class="loading loading-spinner loading-lg text-primary mb-4"></div>
<p class="text-base-content/70">正在加载实验列表...</p>
</div>
<div
v-else-if="error"
class="flex flex-col items-center justify-center min-h-[300px]"
>
<div class="alert alert-error max-w-md">
<svg
xmlns="http://www.w3.org/2000/svg"
class="stroke-current shrink-0 h-6 w-6"
fill="none"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
<div>
<h3 class="font-bold">加载失败</h3>
<div class="text-xs">{{ error }}</div>
</div>
</div>
<button @click="refreshExams" class="btn btn-primary mt-4">重试</button>
</div>
<div v-else class="space-y-6">
<div
v-if="exams.length === 0 && !isAdmin"
class="flex flex-col items-center justify-center min-h-[300px] text-center"
>
<h3 class="text-xl font-semibold text-base-content/70 mb-2">
暂无实验
</h3>
<p class="text-base-content/50">
当前没有可用的实验请联系管理员添加实验内容
</p>
</div>
<div
v-else
class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"
>
<!-- 管理员添加实验卡片 -->
<div
v-if="isAdmin"
class="card bg-base-200 shadow-lg hover:shadow-xl transition-all duration-200 cursor-pointer hover:scale-[1.02]"
@click="() => examEditModalRef?.show()"
>
<div class="card-body flex items-center justify-center text-center">
<div class="text-primary text-6xl mb-4">+</div>
<h3 class="text-lg font-semibold text-primary">添加新实验</h3>
<p class="text-sm text-primary/70">点击创建新的实验</p>
</div>
</div>
<div
v-for="exam in exams"
:key="exam.id"
class="card bg-base-200 shadow-lg hover:shadow-xl transition-all duration-200 cursor-pointer hover:scale-[1.02] relative overflow-hidden"
@click="handleCardClicked($event, exam.id)"
>
<div class="card-body">
<div class="flex justify-between items-start mb-4">
<h3 class="card-title text-base-content">{{ exam.name }}</h3>
<div class="flex flex-row items-center gap-2">
<button
class="btn btn-ghost text-error hover:underline group"
@click="handleEditExamClicked($event, exam.id)"
>
<EditIcon
class="w-4 h-4 transition-transform duration-200 group-hover:scale-110"
/>
编辑
</button>
<span
class="card-title text-sm text-blue-600/50 dark:text-sky-400/50"
>{{ exam.id }}</span
>
</div>
</div>
<!-- 实验标签 -->
<div
v-if="exam.tags && exam.tags.length > 0"
class="flex flex-wrap gap-1 mb-3"
>
<span
v-for="tag in exam.tags"
:key="tag"
class="badge badge-outline badge-sm"
>{{ tag }}</span
>
</div>
<div class="space-y-2 text-sm text-base-content/70">
<div class="flex items-center gap-2">
<svg
class="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"
/>
</svg>
<span>创建{{ formatDate(exam.createdTime) }}</span>
</div>
<div class="flex items-center gap-2">
<svg
class="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
<span>更新{{ formatDate(exam.updatedTime) }}</span>
</div>
</div>
</div>
<!-- 难度书角标识 -->
<div
class="difficulty-corner"
:class="{
'difficulty-1': exam.difficulty === 1,
'difficulty-2': exam.difficulty === 2,
'difficulty-3': exam.difficulty === 3,
'difficulty-4': exam.difficulty === 4,
'difficulty-5': exam.difficulty === 5,
}"
></div>
</div>
</div>
</div>
</div>
<!-- 实验详情模态框 -->
<ExamInfoModal
v-if="selectedExam"
v-model:show="showInfoModal"
:selectedExam="selectedExam"
/>
<!-- 创建实验模态框 -->
<ExamEditModal
ref="examEditModalRef"
@edit-finished="handleEditExamFinished"
/>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, computed } from "vue";
import { useRoute } from "vue-router";
import { AuthManager } from "@/utils/AuthManager";
import { ExamClient, type ExamInfo } from "@/APIClient";
import { formatDate } from "@/utils/Common";
import ExamInfoModal from "./ExamInfoModal.vue";
import ExamEditModal from "./ExamEditModal.vue";
import router from "@/router";
import { EditIcon } from "lucide-vue-next";
import { templateRef } from "@vueuse/core";
// 响应式数据
const route = useRoute();
const exams = ref<ExamInfo[]>([]);
const selectedExam = ref<ExamInfo | null>(null);
const loading = ref(false);
const error = ref<string>("");
const isAdmin = ref(false);
// Modal
const examEditModalRef = templateRef("examEditModalRef");
const showInfoModal = ref(false);
async function refreshExams() {
loading.value = true;
error.value = "";
try {
const client = AuthManager.createClient(ExamClient);
exams.value = await client.getExamList();
} catch (err: any) {
error.value = err.message || "获取实验列表失败";
console.error("获取实验列表失败:", err);
} finally {
loading.value = false;
}
}
async function viewExam(examId: string) {
try {
const client = AuthManager.createClient(ExamClient);
selectedExam.value = await client.getExam(examId);
showInfoModal.value = true;
} catch (err: any) {
error.value = err.message || "获取实验详情失败";
console.error("获取实验详情失败:", err);
showInfoModal.value = false;
}
}
async function handleEditExamFinished() {
await refreshExams();
}
async function handleCardClicked(event: MouseEvent, examId: string) {
if (event.target instanceof HTMLButtonElement) return;
await viewExam(examId);
}
async function handleEditExamClicked(event: MouseEvent, examId: string) {
examEditModalRef?.value?.editExam(examId);
}
// 生命周期
onMounted(async () => {
const isAuthenticated = await AuthManager.isAuthenticated();
if (!isAuthenticated) {
router.push("/login");
}
isAdmin.value = await AuthManager.isAdminAuthenticated();
await refreshExams();
// 处理路由参数如果有examId则自动打开该实验的详情模态框
const examId = route.query.examId as string;
if (examId) {
await viewExam(examId);
}
});
</script>
<style scoped>
/* 难度书角样式 */
.difficulty-corner {
position: absolute;
bottom: 0;
left: 0;
width: 0;
height: 0;
pointer-events: none;
z-index: 10;
}
.difficulty-corner::before {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 0;
height: 0;
border-style: solid;
border-width: 0 48px 48px 0;
transition: all 0.3s ease;
}
/* 难度颜色渐变:绿色到红色 */
.difficulty-1::before {
border-color: transparent transparent rgba(6, 199, 77, 0.6) transparent; /* 绿色 80% 透明度 */
}
.difficulty-2::before {
border-color: transparent transparent rgba(127, 204, 11, 0.6) transparent; /* 黄绿色 80% 透明度 */
}
.difficulty-3::before {
border-color: transparent transparent rgba(255, 191, 0, 0.6) transparent; /* 黄色 80% 透明度 */
}
.difficulty-4::before {
border-color: transparent transparent rgba(255, 106, 0, 0.6) transparent; /* 橙色 80% 透明度 */
}
.difficulty-5::before {
border-color: transparent transparent rgba(245, 35, 35, 0.6) transparent; /* 红色 80% 透明度 */
}
/* 悬停效果 */
.card:hover .difficulty-corner::before {
filter: brightness(1.1);
}
</style>

File diff suppressed because it is too large Load Diff

View File

@@ -266,7 +266,12 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { CaptureMode, ChannelConfig, DebuggerConfig } from "@/APIClient"; import {
CaptureMode,
ChannelConfig,
DebuggerClient,
DebuggerConfig,
} from "@/APIClient";
import { useAlertStore } from "@/components/Alert"; import { useAlertStore } from "@/components/Alert";
import BaseInputField from "@/components/InputField/BaseInputField.vue"; import BaseInputField from "@/components/InputField/BaseInputField.vue";
import type { LogicDataType } from "@/components/WaveformDisplay"; import type { LogicDataType } from "@/components/WaveformDisplay";
@@ -421,7 +426,7 @@ async function startCapture() {
} }
isCapturing.value = true; isCapturing.value = true;
const client = AuthManager.createAuthenticatedDebuggerClient(); const client = AuthManager.createClient(DebuggerClient);
// 构造API配置 // 构造API配置
const channelConfigs = channels.value const channelConfigs = channels.value

File diff suppressed because it is too large Load Diff

View File

@@ -8,7 +8,7 @@
@layout="handleVerticalSplitterResize" @layout="handleVerticalSplitterResize"
> >
<!-- 使用 v-show 替代 v-if --> <!-- 使用 v-show 替代 v-if -->
<SplitterPanel <SplitterPanel
v-show="!isBottomBarFullscreen" v-show="!isBottomBarFullscreen"
id="splitter-group-v-panel-project" id="splitter-group-v-panel-project"
:default-size="verticalSplitterSize" :default-size="verticalSplitterSize"
@@ -60,8 +60,8 @@
v-show="showDocPanel" v-show="showDocPanel"
class="doc-panel overflow-y-auto h-full" class="doc-panel overflow-y-auto h-full"
> >
<MarkdownRenderer <MarkdownRenderer
:content="documentContent" :content="documentContent"
:examId="(route.query.examId as string) || ''" :examId="(route.query.examId as string) || ''"
/> />
</div> </div>
@@ -80,11 +80,13 @@
<!-- 功能底栏 --> <!-- 功能底栏 -->
<SplitterPanel <SplitterPanel
id="splitter-group-v-panel-bar" id="splitter-group-v-panel-bar"
:default-size="isBottomBarFullscreen ? 100 : (100 - verticalSplitterSize)" :default-size="
isBottomBarFullscreen ? 100 : 100 - verticalSplitterSize
"
:min-size="isBottomBarFullscreen ? 100 : 15" :min-size="isBottomBarFullscreen ? 100 : 15"
class="w-full overflow-hidden pt-3" class="w-full overflow-hidden pt-3"
> >
<BottomBar <BottomBar
:isFullscreen="isBottomBarFullscreen" :isFullscreen="isBottomBarFullscreen"
@toggle-fullscreen="handleToggleBottomBarFullscreen" @toggle-fullscreen="handleToggleBottomBarFullscreen"
/> />
@@ -106,22 +108,48 @@
/> />
<!-- Navbar切换浮动按钮 --> <!-- Navbar切换浮动按钮 -->
<div <div
class="navbar-toggle-btn" class="navbar-toggle-btn"
:class="{ 'with-navbar': navbarControl.showNavbar.value }" :class="{ 'with-navbar': navbarControl.showNavbar.value }"
> >
<button <button
@click="navbarControl.toggleNavbar" @click="navbarControl.toggleNavbar"
class="btn btn-circle btn-primary shadow-lg hover:shadow-xl transition-all duration-300" class="btn btn-circle btn-primary shadow-lg hover:shadow-xl transition-all duration-300"
:class="{ 'btn-outline': navbarControl.showNavbar.value }" :class="{ 'btn-outline': navbarControl.showNavbar.value }"
:title="navbarControl.showNavbar.value ? '隐藏顶部导航栏' : '显示顶部导航栏'" :title="
navbarControl.showNavbar.value ? '隐藏顶部导航栏' : '显示顶部导航栏'
"
> >
<!-- 使用SVG图标表示菜单/关闭状态 --> <!-- 使用SVG图标表示菜单/关闭状态 -->
<svg v-if="navbarControl.showNavbar.value" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"> <svg
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" /> v-if="navbarControl.showNavbar.value"
xmlns="http://www.w3.org/2000/svg"
class="h-6 w-6"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg> </svg>
<svg v-else xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"> <svg
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" /> v-else
xmlns="http://www.w3.org/2000/svg"
class="h-6 w-6"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M4 6h16M4 12h16M4 18h16"
/>
</svg> </svg>
</button> </button>
</div> </div>
@@ -131,7 +159,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, watch, inject, type Ref } from "vue"; import { ref, onMounted, watch, inject, type Ref } from "vue";
import { useRouter } from "vue-router"; import { useRouter } from "vue-router";
import { useLocalStorage } from '@vueuse/core'; // 添加VueUse导入 import { useLocalStorage } from "@vueuse/core"; // 添加VueUse导入
import { SplitterGroup, SplitterPanel, SplitterResizeHandle } from "reka-ui"; import { SplitterGroup, SplitterPanel, SplitterResizeHandle } from "reka-ui";
import DiagramCanvas from "@/components/LabCanvas/DiagramCanvas.vue"; import DiagramCanvas from "@/components/LabCanvas/DiagramCanvas.vue";
import ComponentSelector from "@/components/LabCanvas/ComponentSelector.vue"; import ComponentSelector from "@/components/LabCanvas/ComponentSelector.vue";
@@ -143,7 +171,7 @@ import { useProvideComponentManager } from "@/components/LabCanvas";
import { useAlertStore } from "@/components/Alert"; import { useAlertStore } from "@/components/Alert";
import { AuthManager } from "@/utils/AuthManager"; import { AuthManager } from "@/utils/AuthManager";
import { useEquipments } from "@/stores/equipments"; import { useEquipments } from "@/stores/equipments";
import type { Board } from "@/APIClient"; import { DataClient, ResourceClient, type Board } from "@/APIClient";
import { useRoute } from "vue-router"; import { useRoute } from "vue-router";
const route = useRoute(); const route = useRoute();
@@ -158,20 +186,29 @@ const equipments = useEquipments();
const alert = useAlertStore(); const alert = useAlertStore();
// --- Navbar控制 --- // --- Navbar控制 ---
const navbarControl = inject('navbar') as { const navbarControl = inject("navbar") as {
showNavbar: Ref<boolean>; showNavbar: Ref<boolean>;
toggleNavbar: () => void; toggleNavbar: () => void;
}; };
// --- 使用VueUse保存分栏状态 --- // --- 使用VueUse保存分栏状态 ---
// 左右分栏比例默认60% // 左右分栏比例默认60%
const horizontalSplitterSize = useLocalStorage('project-horizontal-splitter-size', 60); const horizontalSplitterSize = useLocalStorage(
"project-horizontal-splitter-size",
60,
);
// 上下分栏比例默认80% // 上下分栏比例默认80%
const verticalSplitterSize = useLocalStorage('project-vertical-splitter-size', 80); const verticalSplitterSize = useLocalStorage(
"project-vertical-splitter-size",
80,
);
// 底栏全屏状态 // 底栏全屏状态
const isBottomBarFullscreen = useLocalStorage('project-bottom-bar-fullscreen', false); const isBottomBarFullscreen = useLocalStorage(
"project-bottom-bar-fullscreen",
false,
);
// 文档面板显示状态 // 文档面板显示状态
const showDocPanel = useLocalStorage('project-show-doc-panel', false); const showDocPanel = useLocalStorage("project-show-doc-panel", false);
function handleToggleBottomBarFullscreen() { function handleToggleBottomBarFullscreen() {
isBottomBarFullscreen.value = !isBottomBarFullscreen.value; isBottomBarFullscreen.value = !isBottomBarFullscreen.value;
@@ -216,25 +253,25 @@ async function loadDocumentContent() {
const examId = route.query.examId as string; const examId = route.query.examId as string;
if (examId) { if (examId) {
// 如果有实验ID从API加载实验文档 // 如果有实验ID从API加载实验文档
console.log('加载实验文档:', examId); console.log("加载实验文档:", examId);
const client = AuthManager.createAuthenticatedResourceClient(); const client = AuthManager.createClient(ResourceClient);
// 获取markdown类型的模板资源列表 // 获取markdown类型的模板资源列表
const resources = await client.getResourceList(examId, 'doc', 'template'); const resources = await client.getResourceList(examId, "doc", "template");
if (resources && resources.length > 0) { if (resources && resources.length > 0) {
// 获取第一个markdown资源 // 获取第一个markdown资源
const markdownResource = resources[0]; const markdownResource = resources[0];
// 使用新的ResourceClient API获取资源文件内容 // 使用新的ResourceClient API获取资源文件内容
const response = await client.getResourceById(markdownResource.id); const response = await client.getResourceById(markdownResource.id);
if (!response || !response.data) { if (!response || !response.data) {
throw new Error('获取markdown文件失败'); throw new Error("获取markdown文件失败");
} }
const content = await response.data.text(); const content = await response.data.text();
// 更新文档内容暂时不处理图片路径由MarkdownRenderer处理 // 更新文档内容暂时不处理图片路径由MarkdownRenderer处理
documentContent.value = content; documentContent.value = content;
} else { } else {
@@ -279,17 +316,17 @@ function updateComponentDirectProp(
// 检查并初始化用户实验板 // 检查并初始化用户实验板
async function checkAndInitializeBoard() { async function checkAndInitializeBoard() {
try { try {
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
const userInfo = await client.getUserInfo(); const userInfo = await client.getUserInfo();
if (userInfo.boardID && userInfo.boardID.trim() !== '') { if (userInfo.boardID && userInfo.boardID.trim() !== "") {
// 用户已绑定实验板获取实验板信息并更新到equipment // 用户已绑定实验板获取实验板信息并更新到equipment
try { try {
const board = await client.getBoardByID(userInfo.boardID); const board = await client.getBoardByID(userInfo.boardID);
updateEquipmentFromBoard(board); updateEquipmentFromBoard(board);
alert?.show(`实验板 ${board.boardName} 已连接`, "success"); alert?.show(`实验板 ${board.boardName} 已连接`, "success");
} catch (boardError) { } catch (boardError) {
console.error('获取实验板信息失败:', boardError); console.error("获取实验板信息失败:", boardError);
alert?.show("获取实验板信息失败", "error"); alert?.show("获取实验板信息失败", "error");
showRequestBoardDialog.value = true; showRequestBoardDialog.value = true;
} }
@@ -298,7 +335,7 @@ async function checkAndInitializeBoard() {
showRequestBoardDialog.value = true; showRequestBoardDialog.value = true;
} }
} catch (error) { } catch (error) {
console.error('检查用户实验板失败:', error); console.error("检查用户实验板失败:", error);
alert?.show("检查用户信息失败", "error"); alert?.show("检查用户信息失败", "error");
showRequestBoardDialog.value = true; showRequestBoardDialog.value = true;
} }
@@ -308,12 +345,12 @@ async function checkAndInitializeBoard() {
function updateEquipmentFromBoard(board: Board) { function updateEquipmentFromBoard(board: Board) {
equipments.boardAddr = board.ipAddr; equipments.boardAddr = board.ipAddr;
equipments.boardPort = board.port; equipments.boardPort = board.port;
console.log(`实验板信息已更新到equipment store:`, { console.log(`实验板信息已更新到equipment store:`, {
address: board.ipAddr, address: board.ipAddr,
port: board.port, port: board.port,
boardName: board.boardName, boardName: board.boardName,
boardId: board.id boardId: board.id,
}); });
} }
@@ -321,7 +358,7 @@ function updateEquipmentFromBoard(board: Board) {
function handleRequestBoardClose() { function handleRequestBoardClose() {
showRequestBoardDialog.value = false; showRequestBoardDialog.value = false;
// 如果用户取消申请,可以选择返回上一页或显示警告 // 如果用户取消申请,可以选择返回上一页或显示警告
router.push('/'); router.push("/");
} }
// 处理申请实验板成功 // 处理申请实验板成功
@@ -338,12 +375,12 @@ onMounted(async () => {
const isAuthenticated = await AuthManager.isAuthenticated(); const isAuthenticated = await AuthManager.isAuthenticated();
if (!isAuthenticated) { if (!isAuthenticated) {
// 验证失败,跳转到登录页面 // 验证失败,跳转到登录页面
router.push('/login'); router.push("/login");
return; return;
} }
} catch (error) { } catch (error) {
console.error('身份验证失败:', error); console.error("身份验证失败:", error);
router.push('/login'); router.push("/login");
return; return;
} }

View File

@@ -75,7 +75,7 @@ import { ref, watch } from "vue";
import { CheckCircle } from "lucide-vue-next"; import { CheckCircle } from "lucide-vue-next";
import { AuthManager } from "@/utils/AuthManager"; import { AuthManager } from "@/utils/AuthManager";
import { useAlertStore } from "@/components/Alert"; import { useAlertStore } from "@/components/Alert";
import type { Board } from "@/APIClient"; import { DataClient, type Board } from "@/APIClient";
interface Props { interface Props {
open: boolean; open: boolean;
@@ -113,7 +113,7 @@ async function checkUserBoard() {
boardInfo.value = null; boardInfo.value = null;
try { try {
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
const userInfo = await client.getUserInfo(); const userInfo = await client.getUserInfo();
if (userInfo.boardID && userInfo.boardID.trim() !== "") { if (userInfo.boardID && userInfo.boardID.trim() !== "") {
@@ -140,7 +140,7 @@ async function requestBoard() {
requesting.value = true; requesting.value = true;
try { try {
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
const board = await client.getAvailableBoard(undefined); const board = await client.getAvailableBoard(undefined);
if (board) { if (board) {

View File

@@ -8,20 +8,31 @@
控制面板 控制面板
</h2> </h2>
<div class="grid grid-cols-1 gap-4" <div
:class="{ 'md:grid-cols-3': streamType === 'usbCamera', 'md:grid-cols-4': streamType === 'videoStream' }"> class="grid grid-cols-1 gap-4"
:class="{
'md:grid-cols-3': streamType === 'usbCamera',
'md:grid-cols-4': streamType === 'videoStream',
}"
>
<!-- 服务状态 --> <!-- 服务状态 -->
<div class="stats shadow"> <div class="stats shadow">
<div class="stat bg-base-100"> <div class="stat bg-base-100">
<div class="stat-figure text-primary"> <div class="stat-figure text-primary">
<div class="badge" :class="statusInfo.isRunning ? 'badge-success' : 'badge-error' <div
"> class="badge"
{{ statusInfo.isRunning ? "运行中" : "已停止" }} :class="
videoStreamInfo.isRunning ? 'badge-success' : 'badge-error'
"
>
{{ videoStreamInfo.isRunning ? "运行中" : "已停止" }}
</div> </div>
</div> </div>
<div class="stat-title">服务状态</div> <div class="stat-title">服务状态</div>
<div class="stat-value text-primary">HTTP</div> <div class="stat-value text-primary">HTTP</div>
<div class="stat-desc">端口: {{ statusInfo.serverPort }}</div> <div class="stat-desc">
端口: {{ videoStreamInfo.serverPort }}
</div>
</div> </div>
</div> </div>
@@ -33,9 +44,11 @@
</div> </div>
<div class="stat-title">视频规格</div> <div class="stat-title">视频规格</div>
<div class="stat-value text-secondary"> <div class="stat-value text-secondary">
{{ streamInfo.frameWidth }}×{{ streamInfo.frameHeight }} {{ videoStreamInfo.frameWidth }}×{{
videoStreamInfo.frameHeight
}}
</div> </div>
<div class="stat-desc">{{ streamInfo.frameRate }} FPS</div> <div class="stat-desc">{{ videoStreamInfo.frameRate }} FPS</div>
</div> </div>
</div> </div>
@@ -47,17 +60,31 @@
</div> </div>
<div class="stat-title">分辨率设置</div> <div class="stat-title">分辨率设置</div>
<div class="stat-value text-sm"> <div class="stat-value text-sm">
<select class="select select-sm select-bordered max-w-xs" v-model="selectedResolution" <select
@change="changeResolution" :disabled="changingResolution"> class="select select-sm select-bordered max-w-xs"
<option v-for="res in supportedResolutions" :key="`${res.width}x${res.height}`" :value="res"> v-model="selectedResolution"
@change="changeResolution"
:disabled="changingResolution"
>
<option
v-for="res in supportedResolutions"
:key="`${res.width}x${res.height}`"
:value="res"
>
{{ res.width }}×{{ res.height }} {{ res.width }}×{{ res.height }}
</option> </option>
</select> </select>
</div> </div>
<div class="stat-desc"> <div class="stat-desc">
<button class="btn btn-xs btn-outline btn-info mt-1" @click="refreshResolutions" <button
:disabled="loadingResolutions"> class="btn btn-xs btn-outline btn-info mt-1"
<RefreshCw v-if="loadingResolutions" class="animate-spin h-3 w-3" /> @click="refreshResolutions"
:disabled="loadingResolutions"
>
<RefreshCw
v-if="loadingResolutions"
class="animate-spin h-3 w-3"
/>
{{ loadingResolutions ? "刷新中..." : "刷新" }} {{ loadingResolutions ? "刷新中..." : "刷新" }}
</button> </button>
</div> </div>
@@ -72,22 +99,34 @@
</div> </div>
<div class="stat-title">连接数</div> <div class="stat-title">连接数</div>
<div class="stat-value text-accent"> <div class="stat-value text-accent">
{{ statusInfo.connectedClients }} {{ videoStreamInfo.connectedClients }}
</div> </div>
<div class="stat-desc"> <div class="stat-desc">
<div class="dropdown dropdown-hover dropdown-top"> <div class="dropdown dropdown-hover dropdown-top">
<div tabindex="0" role="button" class="text-xs underline cursor-help"> <div
tabindex="0"
role="button"
class="text-xs underline cursor-help"
>
查看客户端 查看客户端
</div> </div>
<ul tabindex="0" <ul
class="dropdown-content z-20 menu p-2 shadow bg-base-200 rounded-box w-64 max-h-48 overflow-y-auto"> tabindex="0"
<li v-for="(client, index) in statusInfo.clientEndpoints" :key="index" class="text-xs"> class="dropdown-content z-20 menu p-2 shadow bg-base-200 rounded-box w-64 max-h-48 overflow-y-auto"
>
<li
v-for="(client, index) in videoStreamInfo.clientEndpoints"
:key="index"
class="text-xs"
>
<a class="break-all">{{ client }}</a> <a class="break-all">{{ client }}</a>
</li> </li>
<li v-if=" <li
!statusInfo.clientEndpoints || v-if="
statusInfo.clientEndpoints.length === 0 !videoStreamInfo.clientEndpoints ||
"> videoStreamInfo.clientEndpoints.length === 0
"
>
<a class="text-xs opacity-50">无活跃连接</a> <a class="text-xs opacity-50">无活跃连接</a>
</li> </li>
</ul> </ul>
@@ -99,21 +138,41 @@
<!-- 操作按钮 --> <!-- 操作按钮 -->
<div class="card-actions justify-end mt-4"> <div class="card-actions justify-end mt-4">
<button class="btn btn-outline btn-warning mr-2" @click="toggleStreamType" :disabled="isSwitchingStreamType"> <button
class="btn btn-outline btn-warning mr-2"
@click="toggleStreamType"
:disabled="isSwitchingStreamType"
>
<SwitchCamera class="h-4 w-4 mr-2" /> <SwitchCamera class="h-4 w-4 mr-2" />
{{ streamType === 'usbCamera' ? '切换到视频流' : '切换到USB摄像头' }} {{
streamType === "usbCamera" ? "切换到视频流" : "切换到USB摄像头"
}}
</button> </button>
<button v-show="streamType === 'videoStream'" class="btn btn-outline btn-primary" @click="configCamera" :disabled="configing"> <button
v-show="streamType === 'videoStream'"
class="btn btn-outline btn-primary"
@click="configCamera"
:disabled="configing"
>
<RefreshCw v-if="configing" class="animate-spin h-4 w-4 mr-2" /> <RefreshCw v-if="configing" class="animate-spin h-4 w-4 mr-2" />
<CogIcon v-else class="h-4 w-4 mr-2" /> <CogIcon v-else class="h-4 w-4 mr-2" />
{{ configing ? "配置中..." : "配置摄像头" }} {{ configing ? "配置中..." : "配置摄像头" }}
</button> </button>
<button class="btn btn-outline btn-primary" @click="refreshStatus" :disabled="loading"> <button
class="btn btn-outline btn-primary"
@click="refreshStatus"
:disabled="loading"
>
<RefreshCw v-if="loading" class="animate-spin h-4 w-4 mr-2" /> <RefreshCw v-if="loading" class="animate-spin h-4 w-4 mr-2" />
<RefreshCw v-else class="h-4 w-4 mr-2" /> <RefreshCw v-else class="h-4 w-4 mr-2" />
{{ loading ? "刷新中..." : "刷新状态" }} {{ loading ? "刷新中..." : "刷新状态" }}
</button> </button>
<button v-show="streamType === 'videoStream'" class="btn btn-primary" @click="testConnection" :disabled="testing"> <button
v-show="streamType === 'videoStream'"
class="btn btn-primary"
@click="testConnection"
:disabled="testing"
>
<RefreshCw v-if="testing" class="animate-spin h-4 w-4 mr-2" /> <RefreshCw v-if="testing" class="animate-spin h-4 w-4 mr-2" />
<TestTube v-else class="h-4 w-4 mr-2" /> <TestTube v-else class="h-4 w-4 mr-2" />
{{ testing ? "测试中..." : "测试连接" }} {{ testing ? "测试中..." : "测试连接" }}
@@ -130,24 +189,42 @@
视频预览 视频预览
</h2> </h2>
<div class="relative bg-black rounded-lg overflow-hidden cursor-pointer" :class="[ <div
focusAnimationClass, class="relative bg-black rounded-lg overflow-hidden cursor-pointer"
{ 'cursor-not-allowed': !isPlaying || hasVideoError } :class="[
]" style="aspect-ratio: 4/3" @click="handleVideoClick"> focusAnimationClass,
{ 'cursor-not-allowed': !isPlaying || hasVideoError },
]"
style="aspect-ratio: 4/3"
@click="handleVideoClick"
>
<!-- 视频播放器 - 使用img标签直接显示MJPEG流 --> <!-- 视频播放器 - 使用img标签直接显示MJPEG流 -->
<div v-show="isPlaying" class="w-full h-full flex items-center justify-center"> <div
<img :src="currentVideoSource" alt="视频流" class="max-w-full max-h-full object-contain" v-show="isPlaying"
@error="handleVideoError" @load="handleVideoLoad" /> class="w-full h-full flex items-center justify-center"
>
<img
:src="currentVideoSource"
alt="视频流"
class="max-w-full max-h-full object-contain"
@error="handleVideoError"
@load="handleVideoLoad"
/>
</div> </div>
<!-- 对焦提示 --> <!-- 对焦提示 -->
<div v-if="isPlaying && !hasVideoError" <div
class="absolute top-4 right-4 text-white text-sm bg-black bg-opacity-50 px-2 py-1 rounded"> v-if="isPlaying && !hasVideoError"
{{ isFocusing ? '对焦中...' : '点击画面对焦' }} class="absolute top-4 right-4 text-white text-sm bg-black bg-opacity-50 px-2 py-1 rounded"
>
{{ isFocusing ? "对焦中..." : "点击画面对焦" }}
</div> </div>
<!-- 错误信息显示 --> <!-- 错误信息显示 -->
<div v-if="hasVideoError" class="absolute inset-0 flex items-center justify-center bg-black bg-opacity-70"> <div
v-if="hasVideoError"
class="absolute inset-0 flex items-center justify-center bg-black bg-opacity-70"
>
<div class="card bg-error text-white shadow-lg w-full max-w-lg"> <div class="card bg-error text-white shadow-lg w-full max-w-lg">
<div class="card-body"> <div class="card-body">
<h3 class="card-title flex items-center gap-2"> <h3 class="card-title flex items-center gap-2">
@@ -158,10 +235,13 @@
<ul class="list-disc list-inside"> <ul class="list-disc list-inside">
<li>视频流服务是否已启动</li> <li>视频流服务是否已启动</li>
<li>网络连接是否正常</li> <li>网络连接是否正常</li>
<li>端口 {{ statusInfo.serverPort }} 是否可访问</li> <li>端口 {{ videoStreamInfo.serverPort }} 是否可访问</li>
</ul> </ul>
<div class="card-actions justify-end mt-2"> <div class="card-actions justify-end mt-2">
<button class="btn btn-sm btn-outline btn-primary" @click="tryReconnect"> <button
class="btn btn-sm btn-outline btn-primary"
@click="tryReconnect"
>
重试连接 重试连接
</button> </button>
</div> </div>
@@ -170,8 +250,10 @@
</div> </div>
<!-- 占位符 --> <!-- 占位符 -->
<div v-show="!isPlaying && !hasVideoError" <div
class="absolute inset-0 flex items-center justify-center text-white"> v-show="!isPlaying && !hasVideoError"
class="absolute inset-0 flex items-center justify-center text-white"
>
<div class="text-center"> <div class="text-center">
<Video class="w-16 h-16 mx-auto mb-4 opacity-50" /> <Video class="w-16 h-16 mx-auto mb-4 opacity-50" />
<p class="text-lg opacity-75">{{ videoStatus }}</p> <p class="text-lg opacity-75">{{ videoStatus }}</p>
@@ -187,18 +269,25 @@
<div class="text-sm text-base-content/70"> <div class="text-sm text-base-content/70">
流地址: 流地址:
<code class="bg-base-300 px-2 py-1 rounded">{{ <code class="bg-base-300 px-2 py-1 rounded">{{
streamInfo.mjpegUrl videoStreamInfo.mjpegUrl
}}</code> }}</code>
</div> </div>
<div class="space-x-2"> <div class="space-x-2">
<div class="dropdown dropdown-hover dropdown-top dropdown-end"> <div class="dropdown dropdown-hover dropdown-top dropdown-end">
<div tabindex="0" role="button" class="btn btn-sm btn-outline btn-accent"> <div
tabindex="0"
role="button"
class="btn btn-sm btn-outline btn-accent"
>
<MoreHorizontal class="w-4 h-4 mr-1" /> <MoreHorizontal class="w-4 h-4 mr-1" />
更多功能 更多功能
</div> </div>
<ul tabindex="0" class="dropdown-content z-[1] menu p-2 shadow bg-base-200 rounded-box w-52"> <ul
tabindex="0"
class="dropdown-content z-[1] menu p-2 shadow bg-base-200 rounded-box w-52"
>
<li> <li>
<a @click="openInNewTab(streamInfo.htmlUrl)"> <a @click="openInNewTab(videoStreamInfo.htmlUrl)">
<ExternalLink class="w-4 h-4" /> <ExternalLink class="w-4 h-4" />
在新标签打开视频页面 在新标签打开视频页面
</a> </a>
@@ -210,18 +299,26 @@
</a> </a>
</li> </li>
<li> <li>
<a @click="copyToClipboard(streamInfo.mjpegUrl)"> <a @click="copyToClipboard(videoStreamInfo.mjpegUrl)">
<Copy class="w-4 h-4" /> <Copy class="w-4 h-4" />
复制MJPEG地址 复制MJPEG地址
</a> </a>
</li> </li>
</ul> </ul>
</div> </div>
<button class="btn btn-success btn-sm" @click="startStream" :disabled="isPlaying"> <button
class="btn btn-success btn-sm"
@click="startStream"
:disabled="isPlaying"
>
<Play class="w-4 h-4 mr-1" /> <Play class="w-4 h-4 mr-1" />
播放视频流 播放视频流
</button> </button>
<button class="btn btn-error btn-sm" @click="stopStream" :disabled="!isPlaying"> <button
class="btn btn-error btn-sm"
@click="stopStream"
:disabled="!isPlaying"
>
<Square class="w-4 h-4 mr-1" /> <Square class="w-4 h-4 mr-1" />
停止视频流 停止视频流
</button> </button>
@@ -239,11 +336,20 @@
</h2> </h2>
<div class="bg-base-300 rounded-lg p-4 h-48 overflow-y-auto"> <div class="bg-base-300 rounded-lg p-4 h-48 overflow-y-auto">
<div v-for="(log, index) in logs" :key="index" class="text-sm font-mono mb-1"> <div
<span class="text-base-content/50">[{{ formatTime(log.time) }}]</span> v-for="(log, index) in logs"
:key="index"
class="text-sm font-mono mb-1"
>
<span class="text-base-content/50"
>[{{ formatTime(log.time) }}]</span
>
<span :class="getLogClass(log.level)">{{ log.message }}</span> <span :class="getLogClass(log.level)">{{ log.message }}</span>
</div> </div>
<div v-if="logs.length === 0" class="text-base-content/50 text-center py-8"> <div
v-if="logs.length === 0"
class="text-base-content/50 text-center py-8"
>
暂无日志记录 暂无日志记录
</div> </div>
</div> </div>
@@ -277,8 +383,9 @@ import {
MoreHorizontal, MoreHorizontal,
SwitchCamera, SwitchCamera,
} from "lucide-vue-next"; } from "lucide-vue-next";
import { VideoStreamClient, CameraConfigRequest, ResolutionConfigRequest, StreamInfoResult } from "@/APIClient"; import { VideoStreamClient, ResolutionConfigRequest } from "@/APIClient";
import { useEquipments } from "@/stores/equipments"; import { useEquipments } from "@/stores/equipments";
import { AuthManager } from "@/utils/AuthManager";
const eqps = useEquipments(); const eqps = useEquipments();
@@ -291,12 +398,12 @@ const hasVideoError = ref(false);
const videoStatus = ref('点击"播放视频流"按钮开始查看实时视频'); const videoStatus = ref('点击"播放视频流"按钮开始查看实时视频');
// 视频流类型切换相关 // 视频流类型切换相关
const streamType = ref<'usbCamera' | 'videoStream'>('videoStream'); const streamType = ref<"usbCamera" | "videoStream">("videoStream");
const isSwitchingStreamType = ref(false); const isSwitchingStreamType = ref(false);
// 对焦相关状态 // 对焦相关状态
const isFocusing = ref(false); const isFocusing = ref(false);
const focusAnimationClass = ref(''); const focusAnimationClass = ref("");
// 分辨率相关状态 // 分辨率相关状态
const changingResolution = ref(false); const changingResolution = ref(false);
@@ -304,36 +411,29 @@ const loadingResolutions = ref(false);
const selectedResolution = ref({ width: 640, height: 480 }); const selectedResolution = ref({ width: 640, height: 480 });
const supportedResolutions = ref([ const supportedResolutions = ref([
{ width: 640, height: 480 }, { width: 640, height: 480 },
{ width: 1280, height: 720 } { width: 1280, height: 720 },
]); ]);
// 数据 // 数据
const statusInfo = ref({ const videoStreamInfo = ref({
frameWidth: 640,
frameHeight: 480,
frameRate: 30,
isRunning: false, isRunning: false,
serverPort: 8080, serverPort: 8080,
streamUrl: "",
mjpegUrl: "", mjpegUrl: "",
snapshotUrl: "", snapshotUrl: "",
htmlUrl: "",
usbCameraUrl: "",
connectedClients: 0, connectedClients: 0,
clientEndpoints: [] as string[], clientEndpoints: [] as string[],
}); });
const streamInfo = ref<StreamInfoResult>(new StreamInfoResult({
frameRate: 30,
frameWidth: 640,
frameHeight: 480,
format: "MJPEG",
htmlUrl: "",
mjpegUrl: "",
snapshotUrl: "",
usbCameraUrl: "",
}));
const currentVideoSource = ref(""); const currentVideoSource = ref("");
const logs = ref<Array<{ time: Date; level: string; message: string }>>([]); const logs = ref<Array<{ time: Date; level: string; message: string }>>([]);
// API 客户端 // API 客户端
const videoClient = new VideoStreamClient(); const videoClient = AuthManager.createClient(VideoStreamClient);
// 添加日志 // 添加日志
const addLog = (level: string, message: string) => { const addLog = (level: string, message: string) => {
@@ -397,16 +497,23 @@ const toggleStreamType = async () => {
isSwitchingStreamType.value = true; isSwitchingStreamType.value = true;
try { try {
// 这里假设后端有API: setStreamType(type: string) // 这里假设后端有API: setStreamType(type: string)
addLog('info', `正在切换视频流类型到${streamType.value === 'usbCamera' ? '视频流' : 'USB摄像头'}...`); addLog(
"info",
`正在切换视频流类型到${streamType.value === "usbCamera" ? "视频流" : "USB摄像头"}...`,
);
refreshStatus(); refreshStatus();
// 设置视频源 // 设置视频源
streamType.value = streamType.value === 'usbCamera' ? 'videoStream' : 'usbCamera'; streamType.value =
addLog('success', `已切换到${streamType.value === 'usbCamera' ? 'USB摄像头' : '视频流'}`); streamType.value === "usbCamera" ? "videoStream" : "usbCamera";
addLog(
"success",
`已切换到${streamType.value === "usbCamera" ? "USB摄像头" : "视频流"}`,
);
stopStream(); stopStream();
} catch (error) { } catch (error) {
addLog('error', `切换视频流类型失败: ${error}`); addLog("error", `切换视频流类型失败: ${error}`);
console.error('切换视频流类型失败:', error); console.error("切换视频流类型失败:", error);
} finally { } finally {
isSwitchingStreamType.value = false; isSwitchingStreamType.value = false;
} }
@@ -418,7 +525,7 @@ const takeSnapshot = async () => {
addLog("info", "正在获取快照..."); addLog("info", "正在获取快照...");
// 使用当前的快照URL // 使用当前的快照URL
const snapshotUrl = streamInfo.value.snapshotUrl; const snapshotUrl = videoStreamInfo.value.snapshotUrl;
if (!snapshotUrl) { if (!snapshotUrl) {
addLog("error", "快照URL不可用"); addLog("error", "快照URL不可用");
return; return;
@@ -446,17 +553,14 @@ async function configCamera() {
configing.value = true; configing.value = true;
try { try {
addLog("info", "正在配置并初始化摄像头..."); addLog("info", "正在配置并初始化摄像头...");
const boardconfig = new CameraConfigRequest({ await videoClient.configureCamera();
address: eqps.boardAddr,
port: eqps.boardPort,
});
await videoClient.configureCamera(boardconfig);
const status = await videoClient.getCameraConfig(); const ret = await videoClient.testConnection();
if (status.isConfigured) {
if (ret) {
addLog("success", "摄像头已配置并初始化"); addLog("success", "摄像头已配置并初始化");
} else { } else {
addLog("error", "摄像头配置失败,请检查地址和端口"); addLog("error", "摄像头配置失败");
} }
} catch (error) { } catch (error) {
addLog("error", `摄像头配置失败: ${error}`); addLog("error", `摄像头配置失败: ${error}`);
@@ -473,11 +577,23 @@ const refreshStatus = async () => {
addLog("info", "正在获取服务状态..."); addLog("info", "正在获取服务状态...");
// 使用新的API方法名称 // 使用新的API方法名称
const status = await videoClient.getStatus(); const serviceStatus = await videoClient.getServiceStatus();
statusInfo.value = status; const endpointInfo = await videoClient.myEndpoint();
videoStreamInfo.value = {
const info = await videoClient.getStreamInfo(); frameWidth: endpointInfo.frameWidth,
streamInfo.value = info; frameHeight: endpointInfo.frameHeight,
frameRate: endpointInfo.frameRate,
isRunning: serviceStatus.isRunning,
serverPort: serviceStatus.serverPort,
mjpegUrl: endpointInfo.mjpegUrl,
snapshotUrl: endpointInfo.snapshotUrl,
htmlUrl: endpointInfo.htmlUrl,
usbCameraUrl: endpointInfo.usbCameraUrl,
connectedClients: serviceStatus.connectedClientsNum,
clientEndpoints: serviceStatus.clientEndpoints.map(
(ep) => `${ep.boardId}`,
),
};
addLog("success", "服务状态获取成功"); addLog("success", "服务状态获取成功");
} catch (error) { } catch (error) {
@@ -527,9 +643,6 @@ const handleVideoLoad = () => {
const tryReconnect = () => { const tryReconnect = () => {
addLog("info", "尝试重新连接视频流..."); addLog("info", "尝试重新连接视频流...");
hasVideoError.value = false; hasVideoError.value = false;
// 重新设置视频源,添加时间戳避免缓存问题
currentVideoSource.value = `${streamInfo.value.mjpegUrl}?t=${new Date().getTime()}`;
}; };
// 执行对焦 // 执行对焦
@@ -538,41 +651,41 @@ const performFocus = async () => {
try { try {
isFocusing.value = true; isFocusing.value = true;
focusAnimationClass.value = 'focus-starting'; focusAnimationClass.value = "focus-starting";
addLog("info", "正在执行自动对焦..."); addLog("info", "正在执行自动对焦...");
// 调用对焦API // 调用对焦API
const response = await fetch('/api/VideoStream/Focus'); const response = await fetch("/api/VideoStream/Focus");
const result = await response.json(); const result = await response.json();
if (result.success) { if (result.success) {
// 对焦成功动画 // 对焦成功动画
focusAnimationClass.value = 'focus-success'; focusAnimationClass.value = "focus-success";
addLog("success", "自动对焦执行成功"); addLog("success", "自动对焦执行成功");
// 2秒后消失 // 2秒后消失
setTimeout(() => { setTimeout(() => {
focusAnimationClass.value = ''; focusAnimationClass.value = "";
}, 2000); }, 2000);
} else { } else {
// 对焦失败动画 // 对焦失败动画
focusAnimationClass.value = 'focus-error'; focusAnimationClass.value = "focus-error";
addLog("error", `自动对焦执行失败: ${result.message || '未知错误'}`); addLog("error", `自动对焦执行失败: ${result.message || "未知错误"}`);
// 2秒后消失 // 2秒后消失
setTimeout(() => { setTimeout(() => {
focusAnimationClass.value = ''; focusAnimationClass.value = "";
}, 2000); }, 2000);
} }
} catch (error) { } catch (error) {
// 对焦失败动画 // 对焦失败动画
focusAnimationClass.value = 'focus-error'; focusAnimationClass.value = "focus-error";
addLog("error", `自动对焦执行失败: ${error}`); addLog("error", `自动对焦执行失败: ${error}`);
console.error("自动对焦执行失败:", error); console.error("自动对焦执行失败:", error);
// 2秒后消失 // 2秒后消失
setTimeout(() => { setTimeout(() => {
focusAnimationClass.value = ''; focusAnimationClass.value = "";
}, 2000); }, 2000);
} finally { } finally {
// 1秒后重置对焦状态 // 1秒后重置对焦状态
@@ -598,13 +711,16 @@ const startStream = async () => {
try { try {
addLog("info", "正在启动视频流..."); addLog("info", "正在启动视频流...");
videoStatus.value = "正在连接视频流..."; videoStatus.value = "正在连接视频流...";
videoClient.setEnabled(true); videoClient.setVideoStreamEnable(true);
// 刷新状态 // 刷新状态
await refreshStatus(); await refreshStatus();
// 设置视频源 // 设置视频源
currentVideoSource.value = streamType.value === 'usbCamera' ? streamInfo.value.usbCameraUrl : streamInfo.value.mjpegUrl; currentVideoSource.value =
streamType.value === "usbCamera"
? videoStreamInfo.value.usbCameraUrl
: videoStreamInfo.value.mjpegUrl;
// 设置播放状态 // 设置播放状态
isPlaying.value = true; isPlaying.value = true;
@@ -625,12 +741,18 @@ const refreshResolutions = async () => {
try { try {
addLog("info", "正在获取支持的分辨率列表..."); addLog("info", "正在获取支持的分辨率列表...");
const resolutions = await videoClient.getSupportedResolutions(); const resolutions = await videoClient.getSupportedResolutions();
supportedResolutions.value = resolutions.resolutions; supportedResolutions.value = resolutions.map((resolution) => ({
width: resolution.width,
height: resolution.height,
}));
console.log("支持的分辨率列表:", supportedResolutions.value); console.log("支持的分辨率列表:", supportedResolutions.value);
// 获取当前分辨率 // 获取当前分辨率
const currentRes = await videoClient.getCurrentResolution(); const endpointInfo = await videoClient.myEndpoint();
selectedResolution.value = currentRes; selectedResolution.value = {
width: endpointInfo.frameWidth,
height: endpointInfo.frameHeight,
};
addLog("success", "分辨率列表获取成功"); addLog("success", "分辨率列表获取成功");
} catch (error) { } catch (error) {
@@ -649,18 +771,21 @@ const changeResolution = async () => {
const wasPlaying = isPlaying.value; const wasPlaying = isPlaying.value;
try { try {
addLog("info", `正在切换分辨率到 ${selectedResolution.value.width}×${selectedResolution.value.height}...`); addLog(
"info",
`正在切换分辨率到 ${selectedResolution.value.width}×${selectedResolution.value.height}...`,
);
// 如果正在播放,先停止视频流 // 如果正在播放,先停止视频流
if (wasPlaying) { if (wasPlaying) {
stopStream(); stopStream();
await new Promise(resolve => setTimeout(resolve, 1000)); // 等待1秒 await new Promise((resolve) => setTimeout(resolve, 1000)); // 等待1秒
} }
// 设置新分辨率 // 设置新分辨率
const resolutionRequest = new ResolutionConfigRequest({ const resolutionRequest = new ResolutionConfigRequest({
width: selectedResolution.value.width, width: selectedResolution.value.width,
height: selectedResolution.value.height height: selectedResolution.value.height,
}); });
const success = await videoClient.setResolution(resolutionRequest); const success = await videoClient.setResolution(resolutionRequest);
@@ -670,11 +795,14 @@ const changeResolution = async () => {
// 如果之前在播放,重新启动视频流 // 如果之前在播放,重新启动视频流
if (wasPlaying) { if (wasPlaying) {
await new Promise(resolve => setTimeout(resolve, 500)); // 短暂延迟 await new Promise((resolve) => setTimeout(resolve, 500)); // 短暂延迟
await startStream(); await startStream();
} }
addLog("success", `分辨率已切换到 ${selectedResolution.value.width}×${selectedResolution.value.height}`); addLog(
"success",
`分辨率已切换到 ${selectedResolution.value.width}×${selectedResolution.value.height}`,
);
} else { } else {
addLog("error", "分辨率切换失败"); addLog("error", "分辨率切换失败");
} }
@@ -690,7 +818,7 @@ const changeResolution = async () => {
const stopStream = () => { const stopStream = () => {
try { try {
addLog("info", "正在停止视频流..."); addLog("info", "正在停止视频流...");
videoClient.setEnabled(false); videoClient.setVideoStreamEnable(false);
// 清除视频源 // 清除视频源
currentVideoSource.value = ""; currentVideoSource.value = "";

View File

@@ -174,7 +174,12 @@
import { ref, reactive, watch } from "vue"; import { ref, reactive, watch } from "vue";
import { AuthManager } from "../../utils/AuthManager"; import { AuthManager } from "../../utils/AuthManager";
import { useAlertStore } from "../../components/Alert"; import { useAlertStore } from "../../components/Alert";
import { BoardStatus, type NetworkConfigDto } from "../../APIClient"; import {
BoardStatus,
DataClient,
NetConfigClient,
type NetworkConfigDto,
} from "../../APIClient";
import { useRequiredInjection } from "@/utils/Common"; import { useRequiredInjection } from "@/utils/Common";
import { useBoardManager } from "@/utils/BoardManager"; import { useBoardManager } from "@/utils/BoardManager";
@@ -267,8 +272,7 @@ async function handleSubmit() {
isSubmitting.value = true; isSubmitting.value = true;
try { try {
// 通过 AuthManager 获取认证的 DataClient const dataClient = AuthManager.createClient(DataClient);
const dataClient = AuthManager.createAuthenticatedDataClient();
// 添加板卡到数据库 // 添加板卡到数据库
const boardId = await dataClient.addBoard(form.name.trim()); const boardId = await dataClient.addBoard(form.name.trim());
@@ -293,8 +297,7 @@ async function handleCancelPairing() {
if (!addedBoardId.value) return; if (!addedBoardId.value) return;
try { try {
// 通过 AuthManager 获取认证的 DataClient const dataClient = AuthManager.createClient(DataClient);
const dataClient = AuthManager.createAuthenticatedDataClient();
// 删除添加的板卡 // 删除添加的板卡
await dataClient.deleteBoard(addedBoardId.value); await dataClient.deleteBoard(addedBoardId.value);
@@ -317,8 +320,8 @@ async function handlePairingConfirm() {
try { try {
// 通过 AuthManager 获取认证的客户端 // 通过 AuthManager 获取认证的客户端
const dataClient = AuthManager.createAuthenticatedDataClient(); const dataClient = AuthManager.createClient(DataClient);
const netConfigClient = AuthManager.createAuthenticatedNetConfigClient(); const netConfigClient = AuthManager.createClient(NetConfigClient);
// 获取数据库中对应分配的板卡信息 // 获取数据库中对应分配的板卡信息
const boardInfo = await dataClient.getBoardByID(addedBoardId.value); const boardInfo = await dataClient.getBoardByID(addedBoardId.value);
@@ -365,7 +368,7 @@ async function handlePairingConfirm() {
// 配置失败,删除数据库中的板卡信息 // 配置失败,删除数据库中的板卡信息
try { try {
const dataClient = AuthManager.createAuthenticatedDataClient(); const dataClient = AuthManager.createClient(DataClient);
await dataClient.deleteBoard(addedBoardId.value); await dataClient.deleteBoard(addedBoardId.value);
} catch (deleteError) { } catch (deleteError) {
console.error("删除板卡失败:", deleteError); console.error("删除板卡失败:", deleteError);

View File

@@ -42,12 +42,12 @@ const isAdmin = ref(false);
function setActivePage(event: Event) { function setActivePage(event: Event) {
const target = event.currentTarget as HTMLLinkElement; const target = event.currentTarget as HTMLLinkElement;
const newPage = toNumber(target.id); const newPage = toNumber(target.id);
// 如果用户不是管理员但试图访问管理员页面,则忽略 // 如果用户不是管理员但试图访问管理员页面,则忽略
if (newPage === 100 && !isAdmin.value) { if (newPage === 100 && !isAdmin.value) {
return; return;
} }
activePage.value = newPage; activePage.value = newPage;
} }
@@ -60,16 +60,16 @@ onMounted(async () => {
// 这里可以使用路由跳转 // 这里可以使用路由跳转
return; return;
} }
// 验证管理员权限 // 验证管理员权限
isAdmin.value = await AuthManager.verifyAdminAuth(); isAdmin.value = await AuthManager.isAdminAuthenticated();
// 如果当前页面是管理员页面但用户不是管理员,切换到用户信息页面 // 如果当前页面是管理员页面但用户不是管理员,切换到用户信息页面
if (activePage.value === 100 && !isAdmin.value) { if (activePage.value === 100 && !isAdmin.value) {
activePage.value = 1; activePage.value = 1;
} }
} catch (error) { } catch (error) {
console.error('用户认证检查失败:', error); console.error("用户认证检查失败:", error);
// 可以在这里处理错误,比如显示错误信息或重定向到登录页面 // 可以在这里处理错误,比如显示错误信息或重定向到登录页面
} }
}); });

View File

@@ -273,7 +273,13 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted } from "vue"; import { ref, onMounted } from "vue";
import { AuthManager } from "@/utils/AuthManager"; import { AuthManager } from "@/utils/AuthManager";
import { UserInfo, Board, BoardStatus } from "@/APIClient"; import {
UserInfo,
Board,
BoardStatus,
DataClient,
JtagClient,
} from "@/APIClient";
import { Alert, useAlertStore } from "@/components/Alert"; import { Alert, useAlertStore } from "@/components/Alert";
import { import {
User, User,
@@ -319,7 +325,7 @@ const loadBoardInfo = async () => {
} }
try { try {
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
boardInfo.value = await client.getBoardByID(userInfo.value.boardID); boardInfo.value = await client.getBoardByID(userInfo.value.boardID);
} catch (err) { } catch (err) {
console.error("加载实验板信息失败:", err); console.error("加载实验板信息失败:", err);
@@ -335,7 +341,7 @@ const loadUserInfo = async (showSuccessMessage = false) => {
try { try {
await new Promise((resolve) => setTimeout(resolve, 200)); await new Promise((resolve) => setTimeout(resolve, 200));
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
userInfo.value = await client.getUserInfo(); userInfo.value = await client.getUserInfo();
// 如果有绑定的实验板ID加载实验板信息 // 如果有绑定的实验板ID加载实验板信息
@@ -370,7 +376,7 @@ const applyBoard = async () => {
alertStore?.info("正在申请实验板..."); alertStore?.info("正在申请实验板...");
try { try {
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
// 获取可用的实验板 // 获取可用的实验板
const availableBoard = await client.getAvailableBoard(undefined); const availableBoard = await client.getAvailableBoard(undefined);
@@ -407,7 +413,7 @@ const testBoardConnection = async () => {
alertStore?.info("正在测试连接..."); alertStore?.info("正在测试连接...");
try { try {
const jtagClient = AuthManager.createAuthenticatedJtagClient(); const jtagClient = AuthManager.createClient(JtagClient);
// 使用JTAG客户端读取设备ID Code // 使用JTAG客户端读取设备ID Code
const idCode = await jtagClient.getDeviceIDCode( const idCode = await jtagClient.getDeviceIDCode(
@@ -444,7 +450,7 @@ const unbindBoard = async () => {
alertStore?.info("正在解绑实验板..."); alertStore?.info("正在解绑实验板...");
try { try {
const client = AuthManager.createAuthenticatedDataClient(); const client = AuthManager.createClient(DataClient);
const success = await client.unbindBoard(); const success = await client.unbindBoard();
if (success) { if (success) {