Compare commits
64 Commits
refactor
...
32b126b93f
| Author | SHA1 | Date | |
|---|---|---|---|
| 32b126b93f | |||
| b913f58f13 | |||
|
|
0350ce8829 | ||
|
|
229e6e70ed | ||
| eebc5105a0 | |||
| 15c6eefe30 | |||
| 28af2df093 | |||
|
|
e25f08739a | ||
| f253a33c83 | |||
| 0fb0c4e395 | |||
| 44e357b887 | |||
| 50ffd491fe | |||
| e0619eb9a3 | |||
| da6386c6f0 | |||
| 8789d6f9ee | |||
| 546b9250fa | |||
| 3f2c772eeb | |||
| fae07d9eae | |||
| eedec80927 | |||
| b4bb563782 | |||
| d88c710606 | |||
| bdffba7576 | |||
|
|
d83bc250bd | ||
| 285d3e8585 | |||
|
|
8a1d6e52cb | ||
| 33a2dbf437 | |||
| 4a5709a783 | |||
| d6167ac286 | |||
| c6c3f1cc41 | |||
| 540f5c788d | |||
| 558a139593 | |||
| fad37ba922 | |||
| c7c8cbaeb8 | |||
| 15f9b68e7d | |||
| 48501d79e2 | |||
| bbad7388d8 | |||
| cbb3543c4a | |||
| 53027470fe | |||
| 2a766c3f6b | |||
| 497fa731ca | |||
| 443aea5e3e | |||
| 67bdec8570 | |||
| 1af3fa3a8f | |||
| dd7efe3c84 | |||
|
|
23236b22bd | ||
|
|
ef1a6c8208 | ||
| ff7f7b5a76 | |||
| da7b3f4a4b | |||
| a9ab5926ed | |||
| 2e084bfb58 | |||
| 221d598a6e | |||
| 287c416247 | |||
| e84a784517 | |||
| 178ac0de67 | |||
| bed0158a5f | |||
| 7ffb15c722 | |||
|
|
5ba71d220f | ||
|
|
4c14ada97b | ||
| 81f91b2b71 | |||
|
|
bbfe06822d | ||
|
|
d73166187a | ||
|
|
2eabb79d0f | ||
|
|
a865cfc950 | ||
| fa7c947351 |
13
TODO.md
Normal file
13
TODO.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# TODO
|
||||
|
||||
1. 后端HTTP视频流
|
||||
|
||||
640*480, RGB565
|
||||
0x0000_0000 + 25800
|
||||
|
||||
|
||||
2. 信号发生器界面导入.dat文件
|
||||
3. 示波器后端交互、前端界面
|
||||
4. 逻辑分析仪后端交互、前端界面
|
||||
5. 前端重构
|
||||
6. 数据库 —— 用户登录、板卡资源分配、板卡IP地址分配
|
||||
18
components.d.ts
vendored
18
components.d.ts
vendored
@@ -8,7 +8,10 @@ export {}
|
||||
/* prettier-ignore */
|
||||
declare module 'vue' {
|
||||
export interface GlobalComponents {
|
||||
Alert: typeof import('./src/components/Alert/Alert.vue')['default']
|
||||
AlertDemo: typeof import('./src/components/AlertDemo.vue')['default']
|
||||
BaseBoard: typeof import('./src/components/equipments/BaseBoard.vue')['default']
|
||||
BaseInputField: typeof import('./src/components/InputField/BaseInputField.vue')['default']
|
||||
Canvas: typeof import('./src/components/Canvas.vue')['default']
|
||||
CollapsibleSection: typeof import('./src/components/CollapsibleSection.vue')['default']
|
||||
ComponentSelector: typeof import('./src/components/LabCanvas/ComponentSelector.vue')['default']
|
||||
@@ -18,7 +21,10 @@ declare module 'vue' {
|
||||
DiagramCanvas: typeof import('./src/components/LabCanvas/DiagramCanvas.vue')['default']
|
||||
Dialog: typeof import('./src/components/Dialog.vue')['default']
|
||||
ETH: typeof import('./src/components/equipments/ETH.vue')['default']
|
||||
FunctionBar: typeof import('./src/components/FunctionBar.vue')['default']
|
||||
HDMI: typeof import('./src/components/equipments/HDMI.vue')['default']
|
||||
IpInputField: typeof import('./src/components/InputField/IpInputField.vue')['default']
|
||||
ItemList: typeof import('./src/components/LabCanvas/ItemList.vue')['default']
|
||||
LabCanvas: typeof import('./src/components/LabCanvasNew/LabCanvas.vue')['default']
|
||||
LabCanvasNew: typeof import('./src/components/LabCanvas/LabCanvasNew.vue')['default']
|
||||
LabComponentsDrawer: typeof import('./src/components/LabCanvasNew/LabComponentsDrawer.vue')['default']
|
||||
@@ -32,11 +38,17 @@ declare module 'vue' {
|
||||
PG2L100H_FBG676: typeof import('./src/components/equipments/PG2L100H_FBG676.vue')['default']
|
||||
Pin: typeof import('./src/components/equipments/Pin.vue')['default']
|
||||
PopButton: typeof import('./src/components/PopButton.vue')['default']
|
||||
PortInputField: typeof import('./src/components/InputField/PortInputField.vue')['default']
|
||||
PropertyEditor: typeof import('./src/components/PropertyEditor.vue')['default']
|
||||
PropertyPanel: typeof import('./src/components/PropertyPanel.vue')['default']
|
||||
RekaSplitterGroup: typeof import('reka-ui')['SplitterGroup']
|
||||
RouterLink: typeof import('vue-router')['RouterLink']
|
||||
RouterView: typeof import('vue-router')['RouterView']
|
||||
ScrollAreaCorner: typeof import('reka-ui')['ScrollAreaCorner']
|
||||
ScrollAreaRoot: typeof import('reka-ui')['ScrollAreaRoot']
|
||||
ScrollAreaScrollbar: typeof import('reka-ui')['ScrollAreaScrollbar']
|
||||
ScrollAreaThumb: typeof import('reka-ui')['ScrollAreaThumb']
|
||||
ScrollAreaViewport: typeof import('reka-ui')['ScrollAreaViewport']
|
||||
SD: typeof import('./src/components/equipments/SD.vue')['default']
|
||||
SevenSegmentDisplay: typeof import('./src/components/equipments/SevenSegmentDisplay.vue')['default']
|
||||
SFP: typeof import('./src/components/equipments/SFP.vue')['default']
|
||||
@@ -47,10 +59,16 @@ declare module 'vue' {
|
||||
SplitterPanel: typeof import('reka-ui')['SplitterPanel']
|
||||
SplitterResizeHandle: typeof import('reka-ui')['SplitterResizeHandle']
|
||||
Switch: typeof import('./src/components/equipments/Switch.vue')['default']
|
||||
TabsContent: typeof import('reka-ui')['TabsContent']
|
||||
TabsIndicator: typeof import('reka-ui')['TabsIndicator']
|
||||
TabsList: typeof import('reka-ui')['TabsList']
|
||||
TabsRoot: typeof import('reka-ui')['TabsRoot']
|
||||
TabsTrigger: typeof import('reka-ui')['TabsTrigger']
|
||||
ThemeControlButton: typeof import('./src/components/ThemeControlButton.vue')['default']
|
||||
ThemeControlToggle: typeof import('./src/components/ThemeControlToggle.vue')['default']
|
||||
TutorialCarousel: typeof import('./src/components/TutorialCarousel.vue')['default']
|
||||
UploadCard: typeof import('./src/components/UploadCard.vue')['default']
|
||||
WaveformDisplay: typeof import('./src/components/Oscilloscope/WaveformDisplay.vue')['default']
|
||||
Wire: typeof import('./src/components/equipments/Wire.vue')['default']
|
||||
}
|
||||
}
|
||||
|
||||
461
package-lock.json
generated
461
package-lock.json
generated
@@ -9,10 +9,13 @@
|
||||
"version": "0.1.0",
|
||||
"dependencies": {
|
||||
"@svgdotjs/svg.js": "^3.2.4",
|
||||
"@tanstack/vue-table": "^8.21.3",
|
||||
"@types/lodash": "^4.17.16",
|
||||
"@vueuse/core": "^13.5.0",
|
||||
"async-mutex": "^0.5.0",
|
||||
"echarts": "^5.6.0",
|
||||
"highlight.js": "^11.11.1",
|
||||
"konva": "^9.3.20",
|
||||
"lodash": "^4.17.21",
|
||||
"log-symbols": "^7.0.0",
|
||||
"lucide-vue-next": "^0.525.0",
|
||||
@@ -23,6 +26,8 @@
|
||||
"ts-log": "^2.2.7",
|
||||
"ts-results-es": "^5.0.1",
|
||||
"vue": "^3.5.13",
|
||||
"vue-echarts": "^7.0.3",
|
||||
"vue-konva": "^3.2.1",
|
||||
"vue-router": "4",
|
||||
"yocto-queue": "^1.2.1",
|
||||
"zod": "^3.24.2"
|
||||
@@ -36,10 +41,13 @@
|
||||
"@vue/tsconfig": "^0.7.0",
|
||||
"autoprefixer": "^10.4.20",
|
||||
"daisyui": "^5.0.0",
|
||||
"node-fetch": "^3.3.2",
|
||||
"npm-run-all2": "^7.0.2",
|
||||
"nswag": "^14.3.0",
|
||||
"postcss": "^8.5.3",
|
||||
"tailwindcss": "^4.0.12",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsx": "^4.20.3",
|
||||
"typescript": "~5.7.3",
|
||||
"unplugin-vue-components": "^28.8.0",
|
||||
"vite": "^6.1.0",
|
||||
@@ -538,6 +546,30 @@
|
||||
"node": ">=6.9.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@cspotcode/source-map-support": {
|
||||
"version": "0.8.1",
|
||||
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz",
|
||||
"integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@jridgewell/trace-mapping": "0.3.9"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/@cspotcode/source-map-support/node_modules/@jridgewell/trace-mapping": {
|
||||
"version": "0.3.9",
|
||||
"resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz",
|
||||
"integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@jridgewell/resolve-uri": "^3.0.3",
|
||||
"@jridgewell/sourcemap-codec": "^1.4.10"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/aix-ppc64": {
|
||||
"version": "0.25.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.2.tgz",
|
||||
@@ -1712,6 +1744,19 @@
|
||||
"tailwindcss": "4.1.4"
|
||||
}
|
||||
},
|
||||
"node_modules/@tanstack/table-core": {
|
||||
"version": "8.21.3",
|
||||
"resolved": "https://registry.npmjs.org/@tanstack/table-core/-/table-core-8.21.3.tgz",
|
||||
"integrity": "sha512-ldZXEhOBb8Is7xLs01fR3YEc3DERiz5silj8tnGkFZytt1abEvl/GhUmCE0PMLaMPTa3Jk4HbKmRlHmu+gCftg==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/tannerlinsley"
|
||||
}
|
||||
},
|
||||
"node_modules/@tanstack/virtual-core": {
|
||||
"version": "3.13.12",
|
||||
"resolved": "https://registry.npmjs.org/@tanstack/virtual-core/-/virtual-core-3.13.12.tgz",
|
||||
@@ -1722,6 +1767,25 @@
|
||||
"url": "https://github.com/sponsors/tannerlinsley"
|
||||
}
|
||||
},
|
||||
"node_modules/@tanstack/vue-table": {
|
||||
"version": "8.21.3",
|
||||
"resolved": "https://registry.npmjs.org/@tanstack/vue-table/-/vue-table-8.21.3.tgz",
|
||||
"integrity": "sha512-rusRyd77c5tDPloPskctMyPLFEQUeBzxdQ+2Eow4F7gDPlPOB1UnnhzfpdvqZ8ZyX2rRNGmqNnQWm87OI2OQPw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@tanstack/table-core": "8.21.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/tannerlinsley"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"vue": ">=3.2"
|
||||
}
|
||||
},
|
||||
"node_modules/@tanstack/vue-virtual": {
|
||||
"version": "3.13.12",
|
||||
"resolved": "https://registry.npmjs.org/@tanstack/vue-virtual/-/vue-virtual-3.13.12.tgz",
|
||||
@@ -1738,6 +1802,34 @@
|
||||
"vue": "^2.7.0 || ^3.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@tsconfig/node10": {
|
||||
"version": "1.0.11",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.11.tgz",
|
||||
"integrity": "sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@tsconfig/node12": {
|
||||
"version": "1.0.11",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz",
|
||||
"integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@tsconfig/node14": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz",
|
||||
"integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@tsconfig/node16": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz",
|
||||
"integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@tsconfig/node22": {
|
||||
"version": "22.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node22/-/node22-22.0.1.tgz",
|
||||
@@ -2177,6 +2269,19 @@
|
||||
"node": ">=0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/acorn-walk": {
|
||||
"version": "8.3.4",
|
||||
"resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.4.tgz",
|
||||
"integrity": "sha512-ueEepnujpqee2o5aIYnvHU6C0A42MNdsIDeqy5BydrkuC5R1ZuUFnm27EeFJGoEHJQgn3uleRvmTXaJgfXbt4g==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"acorn": "^8.11.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/alien-signals": {
|
||||
"version": "1.0.13",
|
||||
"resolved": "https://registry.npmjs.org/alien-signals/-/alien-signals-1.0.13.tgz",
|
||||
@@ -2224,6 +2329,13 @@
|
||||
"url": "https://github.com/sponsors/jonschlinkert"
|
||||
}
|
||||
},
|
||||
"node_modules/arg": {
|
||||
"version": "4.1.3",
|
||||
"resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
|
||||
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/aria-hidden": {
|
||||
"version": "1.2.6",
|
||||
"resolved": "https://registry.npmjs.org/aria-hidden/-/aria-hidden-1.2.6.tgz",
|
||||
@@ -2472,6 +2584,13 @@
|
||||
"url": "https://github.com/sponsors/mesqueeb"
|
||||
}
|
||||
},
|
||||
"node_modules/create-require": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz",
|
||||
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/cross-spawn": {
|
||||
"version": "7.0.6",
|
||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
||||
@@ -2526,6 +2645,16 @@
|
||||
"url": "https://github.com/saadeghi/daisyui?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/data-uri-to-buffer": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",
|
||||
"integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 12"
|
||||
}
|
||||
},
|
||||
"node_modules/de-indent": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/de-indent/-/de-indent-1.0.2.tgz",
|
||||
@@ -2616,6 +2745,32 @@
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/diff": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
|
||||
"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
|
||||
"dev": true,
|
||||
"license": "BSD-3-Clause",
|
||||
"engines": {
|
||||
"node": ">=0.3.1"
|
||||
}
|
||||
},
|
||||
"node_modules/echarts": {
|
||||
"version": "5.6.0",
|
||||
"resolved": "https://registry.npmjs.org/echarts/-/echarts-5.6.0.tgz",
|
||||
"integrity": "sha512-oTbVTsXfKuEhxftHqL5xprgLoc0k7uScAwtryCgWF6hPYFLRwOUHiFmHGCBKP5NPFNkDVopOieyUqYGH8Fa3kA==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"tslib": "2.3.0",
|
||||
"zrender": "5.6.1"
|
||||
}
|
||||
},
|
||||
"node_modules/echarts/node_modules/tslib": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz",
|
||||
"integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==",
|
||||
"license": "0BSD"
|
||||
},
|
||||
"node_modules/electron-to-chromium": {
|
||||
"version": "1.5.140",
|
||||
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.140.tgz",
|
||||
@@ -2771,6 +2926,30 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/fetch-blob": {
|
||||
"version": "3.2.0",
|
||||
"resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz",
|
||||
"integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==",
|
||||
"dev": true,
|
||||
"funding": [
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/jimmywarting"
|
||||
},
|
||||
{
|
||||
"type": "paypal",
|
||||
"url": "https://paypal.me/jimmywarting"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"node-domexception": "^1.0.0",
|
||||
"web-streams-polyfill": "^3.0.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^12.20 || >= 14.13"
|
||||
}
|
||||
},
|
||||
"node_modules/figures": {
|
||||
"version": "6.1.0",
|
||||
"resolved": "https://registry.npmjs.org/figures/-/figures-6.1.0.tgz",
|
||||
@@ -2800,6 +2979,19 @@
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/formdata-polyfill": {
|
||||
"version": "4.0.10",
|
||||
"resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
|
||||
"integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"fetch-blob": "^3.1.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12.20.0"
|
||||
}
|
||||
},
|
||||
"node_modules/fraction.js": {
|
||||
"version": "4.3.7",
|
||||
"resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-4.3.7.tgz",
|
||||
@@ -2871,6 +3063,19 @@
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/get-tsconfig": {
|
||||
"version": "4.10.1",
|
||||
"resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.10.1.tgz",
|
||||
"integrity": "sha512-auHyJ4AgMz7vgS8Hp3N6HXSmlMdUyhSUrfBF16w153rxtLIEOE+HGqaBppczZvnHLqQJfiHotCYpNhl0lUROFQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"resolve-pkg-maps": "^1.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/privatenumber/get-tsconfig?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/glob-parent": {
|
||||
"version": "5.1.2",
|
||||
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",
|
||||
@@ -3172,6 +3377,26 @@
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/konva": {
|
||||
"version": "9.3.20",
|
||||
"resolved": "https://registry.npmjs.org/konva/-/konva-9.3.20.tgz",
|
||||
"integrity": "sha512-7XPD/YtgfzC8b1c7z0hhY5TF1IO/pBYNa29zMTA2PeBaqI0n5YplUeo4JRuRcljeAF8lWtW65jePZZF7064c8w==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "patreon",
|
||||
"url": "https://www.patreon.com/lavrton"
|
||||
},
|
||||
{
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/konva"
|
||||
},
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/lavrton"
|
||||
}
|
||||
],
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/lightningcss": {
|
||||
"version": "1.29.2",
|
||||
"resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.29.2.tgz",
|
||||
@@ -3479,6 +3704,13 @@
|
||||
"@jridgewell/sourcemap-codec": "^1.5.0"
|
||||
}
|
||||
},
|
||||
"node_modules/make-error": {
|
||||
"version": "1.3.6",
|
||||
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
|
||||
"integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
|
||||
"dev": true,
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/marked": {
|
||||
"version": "12.0.2",
|
||||
"resolved": "https://registry.npmjs.org/marked/-/marked-12.0.2.tgz",
|
||||
@@ -3632,6 +3864,46 @@
|
||||
"node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
|
||||
}
|
||||
},
|
||||
"node_modules/node-domexception": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
|
||||
"integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==",
|
||||
"deprecated": "Use your platform's native DOMException instead",
|
||||
"dev": true,
|
||||
"funding": [
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/jimmywarting"
|
||||
},
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://paypal.me/jimmywarting"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=10.5.0"
|
||||
}
|
||||
},
|
||||
"node_modules/node-fetch": {
|
||||
"version": "3.3.2",
|
||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz",
|
||||
"integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"data-uri-to-buffer": "^4.0.0",
|
||||
"fetch-blob": "^3.1.4",
|
||||
"formdata-polyfill": "^4.0.10"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/node-fetch"
|
||||
}
|
||||
},
|
||||
"node_modules/node-releases": {
|
||||
"version": "2.0.19",
|
||||
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.19.tgz",
|
||||
@@ -4025,6 +4297,16 @@
|
||||
"url": "https://github.com/sponsors/antfu"
|
||||
}
|
||||
},
|
||||
"node_modules/resolve-pkg-maps": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz",
|
||||
"integrity": "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"url": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/rfdc": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.4.1.tgz",
|
||||
@@ -4276,6 +4558,50 @@
|
||||
"integrity": "sha512-320x5Ggei84AxzlXp91QkIGSw5wgaLT6GeAH0KsqDmRZdVWW2OiSeVvElVoatk3f7nicwXlElXsoFkARiGE2yg==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/ts-node": {
|
||||
"version": "10.9.2",
|
||||
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz",
|
||||
"integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@cspotcode/source-map-support": "^0.8.0",
|
||||
"@tsconfig/node10": "^1.0.7",
|
||||
"@tsconfig/node12": "^1.0.7",
|
||||
"@tsconfig/node14": "^1.0.0",
|
||||
"@tsconfig/node16": "^1.0.2",
|
||||
"acorn": "^8.4.1",
|
||||
"acorn-walk": "^8.1.1",
|
||||
"arg": "^4.1.0",
|
||||
"create-require": "^1.1.0",
|
||||
"diff": "^4.0.1",
|
||||
"make-error": "^1.1.1",
|
||||
"v8-compile-cache-lib": "^3.0.1",
|
||||
"yn": "3.1.1"
|
||||
},
|
||||
"bin": {
|
||||
"ts-node": "dist/bin.js",
|
||||
"ts-node-cwd": "dist/bin-cwd.js",
|
||||
"ts-node-esm": "dist/bin-esm.js",
|
||||
"ts-node-script": "dist/bin-script.js",
|
||||
"ts-node-transpile-only": "dist/bin-transpile.js",
|
||||
"ts-script": "dist/bin-script-deprecated.js"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@swc/core": ">=1.2.50",
|
||||
"@swc/wasm": ">=1.2.50",
|
||||
"@types/node": "*",
|
||||
"typescript": ">=2.7"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@swc/core": {
|
||||
"optional": true
|
||||
},
|
||||
"@swc/wasm": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/ts-results-es": {
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/ts-results-es/-/ts-results-es-5.0.1.tgz",
|
||||
@@ -4288,6 +4614,26 @@
|
||||
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
|
||||
"license": "0BSD"
|
||||
},
|
||||
"node_modules/tsx": {
|
||||
"version": "4.20.3",
|
||||
"resolved": "https://registry.npmjs.org/tsx/-/tsx-4.20.3.tgz",
|
||||
"integrity": "sha512-qjbnuR9Tr+FJOMBqJCW5ehvIo/buZq7vH7qD7JziU98h6l3qGy0a/yPFjwO+y0/T7GFpNgNAvEcPPVfyT8rrPQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"esbuild": "~0.25.0",
|
||||
"get-tsconfig": "^4.7.5"
|
||||
},
|
||||
"bin": {
|
||||
"tsx": "dist/cli.mjs"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"fsevents": "~2.3.3"
|
||||
}
|
||||
},
|
||||
"node_modules/typed-function": {
|
||||
"version": "4.2.1",
|
||||
"resolved": "https://registry.npmjs.org/typed-function/-/typed-function-4.2.1.tgz",
|
||||
@@ -4447,6 +4793,13 @@
|
||||
"browserslist": ">= 4.21.0"
|
||||
}
|
||||
},
|
||||
"node_modules/v8-compile-cache-lib": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",
|
||||
"integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/vite": {
|
||||
"version": "6.3.5",
|
||||
"resolved": "https://registry.npmjs.org/vite/-/vite-6.3.5.tgz",
|
||||
@@ -4638,6 +4991,79 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/vue-demi": {
|
||||
"version": "0.13.11",
|
||||
"resolved": "https://registry.npmjs.org/vue-demi/-/vue-demi-0.13.11.tgz",
|
||||
"integrity": "sha512-IR8HoEEGM65YY3ZJYAjMlKygDQn25D5ajNFNoKh9RSDMQtlzCxtfQjdQgv9jjK+m3377SsJXY8ysq8kLCZL25A==",
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"vue-demi-fix": "bin/vue-demi-fix.js",
|
||||
"vue-demi-switch": "bin/vue-demi-switch.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/antfu"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@vue/composition-api": "^1.0.0-rc.1",
|
||||
"vue": "^3.0.0-0 || ^2.6.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@vue/composition-api": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/vue-echarts": {
|
||||
"version": "7.0.3",
|
||||
"resolved": "https://registry.npmjs.org/vue-echarts/-/vue-echarts-7.0.3.tgz",
|
||||
"integrity": "sha512-/jSxNwOsw5+dYAUcwSfkLwKPuzTQ0Cepz1LxCOpj2QcHrrmUa/Ql0eQqMmc1rTPQVrh2JQ29n2dhq75ZcHvRDw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"vue-demi": "^0.13.11"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@vue/runtime-core": "^3.0.0",
|
||||
"echarts": "^5.5.1",
|
||||
"vue": "^2.7.0 || ^3.1.1"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@vue/runtime-core": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/vue-konva": {
|
||||
"version": "3.2.1",
|
||||
"resolved": "https://registry.npmjs.org/vue-konva/-/vue-konva-3.2.1.tgz",
|
||||
"integrity": "sha512-gLF+VYnlrBfwtaN3NkgzzEqlj9nyCll80VZv2DdvLUM3cisUsdcRJJuMwGTBJOTebcnn6MB22r33IFd2m+m/ig==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "patreon",
|
||||
"url": "https://www.patreon.com/lavrton"
|
||||
},
|
||||
{
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/konva"
|
||||
},
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/lavrton"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 4.0.0",
|
||||
"npm": ">= 3.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"konva": ">7",
|
||||
"vue": "^3"
|
||||
}
|
||||
},
|
||||
"node_modules/vue-router": {
|
||||
"version": "4.5.0",
|
||||
"resolved": "https://registry.npmjs.org/vue-router/-/vue-router-4.5.0.tgz",
|
||||
@@ -4676,6 +5102,16 @@
|
||||
"typescript": ">=5.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/web-streams-polyfill": {
|
||||
"version": "3.3.3",
|
||||
"resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",
|
||||
"integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/webpack-virtual-modules": {
|
||||
"version": "0.6.2",
|
||||
"resolved": "https://registry.npmjs.org/webpack-virtual-modules/-/webpack-virtual-modules-0.6.2.tgz",
|
||||
@@ -4706,6 +5142,16 @@
|
||||
"dev": true,
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/yn": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
|
||||
"integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/yocto-queue": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-1.2.1.tgz",
|
||||
@@ -4738,6 +5184,21 @@
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/colinhacks"
|
||||
}
|
||||
},
|
||||
"node_modules/zrender": {
|
||||
"version": "5.6.1",
|
||||
"resolved": "https://registry.npmjs.org/zrender/-/zrender-5.6.1.tgz",
|
||||
"integrity": "sha512-OFXkDJKcrlx5su2XbzJvj/34Q3m6PvyCZkVPHGYpcCJ52ek4U/ymZyfuV1nKE23AyBJ51E/6Yr0mhZ7xGTO4ag==",
|
||||
"license": "BSD-3-Clause",
|
||||
"dependencies": {
|
||||
"tslib": "2.3.0"
|
||||
}
|
||||
},
|
||||
"node_modules/zrender/node_modules/tslib": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz",
|
||||
"integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==",
|
||||
"license": "0BSD"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
12
package.json
12
package.json
@@ -9,16 +9,17 @@
|
||||
"preview": "vite preview",
|
||||
"build-only": "vite build",
|
||||
"type-check": "vue-tsc --build",
|
||||
"pregen-api": "cd server && dotnet run --property:Configuration=Release &",
|
||||
"gen-api": "npx nswag openapi2tsclient /input:http://localhost:5000/swagger/v1/swagger.json /output:src/APIClient.ts",
|
||||
"postgen-api": "pkill server"
|
||||
"gen-api": "npx tsx scripts/GenerateWebAPI.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@svgdotjs/svg.js": "^3.2.4",
|
||||
"@tanstack/vue-table": "^8.21.3",
|
||||
"@types/lodash": "^4.17.16",
|
||||
"@vueuse/core": "^13.5.0",
|
||||
"async-mutex": "^0.5.0",
|
||||
"echarts": "^5.6.0",
|
||||
"highlight.js": "^11.11.1",
|
||||
"konva": "^9.3.20",
|
||||
"lodash": "^4.17.21",
|
||||
"log-symbols": "^7.0.0",
|
||||
"lucide-vue-next": "^0.525.0",
|
||||
@@ -29,6 +30,8 @@
|
||||
"ts-log": "^2.2.7",
|
||||
"ts-results-es": "^5.0.1",
|
||||
"vue": "^3.5.13",
|
||||
"vue-echarts": "^7.0.3",
|
||||
"vue-konva": "^3.2.1",
|
||||
"vue-router": "4",
|
||||
"yocto-queue": "^1.2.1",
|
||||
"zod": "^3.24.2"
|
||||
@@ -42,10 +45,13 @@
|
||||
"@vue/tsconfig": "^0.7.0",
|
||||
"autoprefixer": "^10.4.20",
|
||||
"daisyui": "^5.0.0",
|
||||
"node-fetch": "^3.3.2",
|
||||
"npm-run-all2": "^7.0.2",
|
||||
"nswag": "^14.3.0",
|
||||
"postcss": "^8.5.3",
|
||||
"tailwindcss": "^4.0.12",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsx": "^4.20.3",
|
||||
"typescript": "~5.7.3",
|
||||
"unplugin-vue-components": "^28.8.0",
|
||||
"vite": "^6.1.0",
|
||||
|
||||
335
scripts/GenerateWebAPI.ts
Normal file
335
scripts/GenerateWebAPI.ts
Normal file
@@ -0,0 +1,335 @@
|
||||
import { spawn, exec, ChildProcess } from 'child_process';
|
||||
import { promisify } from 'util';
|
||||
import fetch from 'node-fetch';
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
async function waitForServer(url: string, maxRetries: number = 30, interval: number = 1000): Promise<boolean> {
|
||||
for (let i = 0; i < maxRetries; i++) {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (response.ok) {
|
||||
console.log('✓ Server is ready');
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
// Server not ready yet
|
||||
}
|
||||
console.log(`Waiting for server... (${i + 1}/${maxRetries})`);
|
||||
await new Promise(resolve => setTimeout(resolve, interval));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 改进全局变量类型
|
||||
let serverProcess: ChildProcess | null = null;
|
||||
let webProcess: ChildProcess | null = null;
|
||||
|
||||
async function startWeb(): Promise<ChildProcess> {
|
||||
console.log('Starting Vite frontend...');
|
||||
return new Promise((resolve, reject) => {
|
||||
const process = spawn('npm', ['run', 'dev'], {
|
||||
stdio: 'pipe'
|
||||
});
|
||||
|
||||
let webStarted = false;
|
||||
|
||||
process.stdout?.on('data', (data) => {
|
||||
const output = data.toString();
|
||||
console.log(`Web: ${output}`);
|
||||
|
||||
// 检查 Vite 是否已启动
|
||||
if ((output.includes('Local:') || output.includes('ready in')) && !webStarted) {
|
||||
webStarted = true;
|
||||
resolve(process);
|
||||
}
|
||||
});
|
||||
|
||||
process.stderr?.on('data', (data) => {
|
||||
console.error(`Web Error: ${data}`);
|
||||
});
|
||||
|
||||
process.on('error', (error) => {
|
||||
reject(error);
|
||||
});
|
||||
|
||||
process.on('exit', (code, signal) => {
|
||||
console.log(`Web process exited with code ${code} and signal ${signal}`);
|
||||
if (!webStarted) {
|
||||
reject(new Error(`Web process exited unexpectedly with code ${code}`));
|
||||
}
|
||||
});
|
||||
|
||||
// 存储进程引用
|
||||
webProcess = process;
|
||||
|
||||
// 超时处理
|
||||
setTimeout(() => {
|
||||
if (!webStarted) {
|
||||
reject(new Error('Web server failed to start within timeout'));
|
||||
}
|
||||
}, 30000); // 30秒超时
|
||||
});
|
||||
}
|
||||
|
||||
async function startServer(): Promise<ChildProcess> {
|
||||
console.log('Starting .NET server...');
|
||||
return new Promise((resolve, reject) => {
|
||||
const process = spawn('dotnet', ['run', '--property:Configuration=Release'], {
|
||||
cwd: 'server',
|
||||
stdio: 'pipe'
|
||||
});
|
||||
|
||||
let serverStarted = false;
|
||||
|
||||
process.stdout?.on('data', (data) => {
|
||||
const output = data.toString();
|
||||
console.log(`Server: ${output}`);
|
||||
|
||||
// 检查服务器是否已启动
|
||||
if (output.includes('Now listening on:') && !serverStarted) {
|
||||
serverStarted = true;
|
||||
resolve(process);
|
||||
}
|
||||
});
|
||||
|
||||
process.stderr?.on('data', (data) => {
|
||||
console.error(`Server Error: ${data}`);
|
||||
});
|
||||
|
||||
process.on('error', (error) => {
|
||||
reject(error);
|
||||
});
|
||||
|
||||
process.on('exit', (code, signal) => {
|
||||
console.log(`Server process exited with code ${code} and signal ${signal}`);
|
||||
if (!serverStarted) {
|
||||
reject(new Error(`Server process exited unexpectedly with code ${code}`));
|
||||
}
|
||||
});
|
||||
|
||||
// 存储进程引用
|
||||
serverProcess = process;
|
||||
|
||||
// 超时处理
|
||||
setTimeout(() => {
|
||||
if (!serverStarted) {
|
||||
reject(new Error('Server failed to start within timeout'));
|
||||
}
|
||||
}, 30000); // 30秒超时
|
||||
});
|
||||
}
|
||||
|
||||
async function stopServer(): Promise<void> {
|
||||
console.log('Stopping server...');
|
||||
|
||||
if (!serverProcess) {
|
||||
console.log('No server process to stop');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查进程是否还存在
|
||||
if (serverProcess.killed || serverProcess.exitCode !== null) {
|
||||
console.log('✓ Server process already terminated');
|
||||
serverProcess = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// 发送 SIGTERM 信号
|
||||
const killed = serverProcess.kill('SIGTERM');
|
||||
if (!killed) {
|
||||
console.warn('Failed to send SIGTERM to server process');
|
||||
return;
|
||||
}
|
||||
|
||||
// 等待进程优雅退出
|
||||
const exitPromise = new Promise<void>((resolve) => {
|
||||
if (serverProcess) {
|
||||
serverProcess.on('exit', () => {
|
||||
console.log('✓ Server stopped gracefully');
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
|
||||
// 设置超时,如果 5 秒内没有退出则强制终止
|
||||
const timeoutPromise = new Promise<void>((resolve) => {
|
||||
setTimeout(() => {
|
||||
if (serverProcess && !serverProcess.killed && serverProcess.exitCode === null) {
|
||||
console.log('Force killing server process...');
|
||||
serverProcess.kill('SIGKILL');
|
||||
}
|
||||
resolve();
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
await Promise.race([exitPromise, timeoutPromise]);
|
||||
|
||||
} catch (error) {
|
||||
console.warn('Warning: Could not stop server process:', error);
|
||||
} finally {
|
||||
serverProcess = null;
|
||||
|
||||
// 额外清理:确保没有遗留的 dotnet 进程
|
||||
try {
|
||||
if (process.platform !== 'win32') {
|
||||
// 只清理与我们项目相关的进程
|
||||
await execAsync('pkill -f "dotnet.*run.*--property:Configuration=Release"').catch(() => {
|
||||
// 忽略错误,可能没有匹配的进程
|
||||
});
|
||||
}
|
||||
} catch (cleanupError) {
|
||||
// 忽略清理错误
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function stopWeb(): Promise<void> {
|
||||
console.log('Stopping web server...');
|
||||
|
||||
if (!webProcess) {
|
||||
console.log('No web process to stop');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查进程是否还存在
|
||||
if (webProcess.killed || webProcess.exitCode !== null) {
|
||||
console.log('✓ Web process already terminated');
|
||||
webProcess = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// 发送 SIGTERM 信号
|
||||
const killed = webProcess.kill('SIGTERM');
|
||||
if (!killed) {
|
||||
console.warn('Failed to send SIGTERM to web process');
|
||||
return;
|
||||
}
|
||||
|
||||
// 等待进程优雅退出
|
||||
const exitPromise = new Promise<void>((resolve) => {
|
||||
if (webProcess) {
|
||||
webProcess.on('exit', () => {
|
||||
console.log('✓ Web server stopped gracefully');
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
|
||||
// 设置超时,如果 5 秒内没有退出则强制终止
|
||||
const timeoutPromise = new Promise<void>((resolve) => {
|
||||
setTimeout(() => {
|
||||
if (webProcess && !webProcess.killed && webProcess.exitCode === null) {
|
||||
console.log('Force killing web process...');
|
||||
webProcess.kill('SIGKILL');
|
||||
}
|
||||
resolve();
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
await Promise.race([exitPromise, timeoutPromise]);
|
||||
|
||||
} catch (error) {
|
||||
console.warn('Warning: Could not stop web process:', error);
|
||||
} finally {
|
||||
webProcess = null;
|
||||
|
||||
// 额外清理:确保没有遗留的 npm/node 进程
|
||||
try {
|
||||
if (process.platform !== 'win32') {
|
||||
// 清理可能的 vite 进程
|
||||
await execAsync('pkill -f "vite"').catch(() => {
|
||||
// 忽略错误,可能没有匹配的进程
|
||||
});
|
||||
}
|
||||
} catch (cleanupError) {
|
||||
// 忽略清理错误
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function generateApiClient(): Promise<void> {
|
||||
console.log('Generating API client...');
|
||||
try {
|
||||
await execAsync('npx nswag openapi2tsclient /input:http://localhost:5000/swagger/v1/swagger.json /output:src/APIClient.ts');
|
||||
console.log('✓ API client generated successfully');
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to generate API client: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
try {
|
||||
// Start web frontend first
|
||||
await startWeb();
|
||||
console.log('✓ Frontend started');
|
||||
|
||||
// Wait a bit for frontend to fully initialize
|
||||
await new Promise(resolve => setTimeout(resolve, 3000));
|
||||
|
||||
// Start server
|
||||
await startServer();
|
||||
console.log('✓ Backend started');
|
||||
|
||||
// Wait for server to be ready (给服务器额外时间完全启动)
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
|
||||
// Check if swagger endpoint is available
|
||||
const serverReady = await waitForServer('http://localhost:5000/swagger/v1/swagger.json');
|
||||
|
||||
if (!serverReady) {
|
||||
throw new Error('Server failed to start within the expected time');
|
||||
}
|
||||
|
||||
// Generate API client
|
||||
await generateApiClient();
|
||||
|
||||
console.log('✓ API generation completed successfully');
|
||||
} catch (error) {
|
||||
console.error('❌ Error:', error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
// Always try to stop processes in order: server first, then web
|
||||
await stopServer();
|
||||
await stopWeb();
|
||||
}
|
||||
}
|
||||
|
||||
// 改进的进程终止处理
|
||||
const cleanup = async (signal: string) => {
|
||||
console.log(`\nReceived ${signal}, cleaning up...`);
|
||||
await stopServer();
|
||||
await stopWeb();
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
process.on('SIGINT', () => cleanup('SIGINT'));
|
||||
process.on('SIGTERM', () => cleanup('SIGTERM'));
|
||||
|
||||
// 处理未捕获的异常
|
||||
process.on('uncaughtException', async (error) => {
|
||||
console.error('❌ Uncaught exception:', error);
|
||||
await stopServer();
|
||||
await stopWeb();
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', async (reason, promise) => {
|
||||
console.error('❌ Unhandled rejection at:', promise, 'reason:', reason);
|
||||
await stopServer();
|
||||
await stopWeb();
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
main().catch(async (error) => {
|
||||
console.error('❌ Unhandled error:', error);
|
||||
await stopServer();
|
||||
await stopWeb();
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -1,8 +1,14 @@
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using NLog.Web;
|
||||
using NSwag.Generation.Processors.Security;
|
||||
using server.Services;
|
||||
|
||||
// Early init of NLog to allow startup and exception logging, before host is built
|
||||
var logger = NLog.LogManager.Setup()
|
||||
@@ -36,6 +42,37 @@ try
|
||||
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
|
||||
});
|
||||
|
||||
// Add JWT Token Authorization
|
||||
builder.Services
|
||||
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
RequireExpirationTime = true,
|
||||
ValidIssuer = "dlut.edu.cn",
|
||||
ValidAudience = "dlut.edu.cn",
|
||||
IssuerSigningKey = new SymmetricSecurityKey(
|
||||
Encoding.UTF8.GetBytes("my secret key 1234567890my secret key 1234567890")),
|
||||
};
|
||||
options.Authority = "http://localhost:5000";
|
||||
options.RequireHttpsMetadata = false;
|
||||
});
|
||||
// Add JWT Token Authorization Policy
|
||||
builder.Services.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy("Admin", policy =>
|
||||
{
|
||||
policy.RequireClaim(ClaimTypes.Role, new string[] {
|
||||
Database.User.UserPermission.Admin.ToString(),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Add CORS policy
|
||||
if (builder.Environment.IsDevelopment())
|
||||
{
|
||||
@@ -52,13 +89,13 @@ try
|
||||
{
|
||||
options.AddPolicy("Users", policy => policy
|
||||
.AllowAnyOrigin()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader()
|
||||
);
|
||||
});
|
||||
|
||||
// Add Swagger
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddOpenApiDocument(options =>
|
||||
builder.Services.AddSwaggerDocument(options =>
|
||||
{
|
||||
options.PostProcess = document =>
|
||||
{
|
||||
@@ -80,8 +117,23 @@ try
|
||||
// }
|
||||
};
|
||||
};
|
||||
|
||||
// Authorization
|
||||
options.AddSecurity("Bearer", new NSwag.OpenApiSecurityScheme
|
||||
{
|
||||
Description = "请输入token,格式为 Bearer xxxxxxxx(注意中间必须有空格)",
|
||||
Name = "Authorization",
|
||||
In = NSwag.OpenApiSecurityApiKeyLocation.Header,
|
||||
Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
|
||||
});
|
||||
options.OperationProcessors.Add(new OperationSecurityScopeProcessor("Bearer"));
|
||||
});
|
||||
|
||||
|
||||
// 添加 HTTP 视频流服务
|
||||
builder.Services.AddSingleton<HttpVideoStreamService>();
|
||||
builder.Services.AddHostedService(provider => provider.GetRequiredService<HttpVideoStreamService>());
|
||||
|
||||
// Application Settings
|
||||
var app = builder.Build();
|
||||
// Configure the HTTP request pipeline.
|
||||
@@ -95,12 +147,14 @@ try
|
||||
logger.Info($"Use Static Files : {Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")}");
|
||||
app.UseDefaultFiles();
|
||||
app.UseStaticFiles(); // Serves files from wwwroot by default
|
||||
|
||||
// Assets Files
|
||||
app.UseStaticFiles(new StaticFileOptions
|
||||
{
|
||||
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "assets")),
|
||||
RequestPath = "/assets"
|
||||
});
|
||||
|
||||
// Log Files
|
||||
if (!Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "log")))
|
||||
{
|
||||
@@ -113,10 +167,11 @@ try
|
||||
});
|
||||
app.MapFallbackToFile("index.html");
|
||||
}
|
||||
// Add logs
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseRouting();
|
||||
app.UseCors();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
// Swagger
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
<PackageReference Include="DotNext.Threading" Version="5.19.1" />
|
||||
<PackageReference Include="Honoo.IO.Hashing.Crc" Version="1.3.3" />
|
||||
<PackageReference Include="linq2db.AspNet" Version="5.4.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.7" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.4" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="9.0.4" />
|
||||
<PackageReference Include="Microsoft.OpenApi" Version="1.6.23" />
|
||||
@@ -25,6 +26,7 @@
|
||||
<PackageReference Include="NLog" Version="5.4.0" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.4.0" />
|
||||
<PackageReference Include="NSwag.AspNetCore" Version="14.3.0" />
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.10" />
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,392 +0,0 @@
|
||||
using System.Collections;
|
||||
using DotNext;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
/// <summary>
|
||||
/// 数字处理工具
|
||||
/// </summary>
|
||||
public class Number
|
||||
{
|
||||
private static readonly byte[] BitReverseTable = new byte[] {
|
||||
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
|
||||
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
|
||||
0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
|
||||
0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
|
||||
0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
|
||||
0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
|
||||
0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
|
||||
0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
|
||||
0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
|
||||
0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
|
||||
0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
|
||||
0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
|
||||
0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
|
||||
0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
|
||||
0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
|
||||
0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
|
||||
0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
|
||||
0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
|
||||
0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
|
||||
0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
|
||||
0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
|
||||
0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
|
||||
0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
|
||||
0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
|
||||
0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
|
||||
0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
|
||||
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
|
||||
0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
|
||||
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
|
||||
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
|
||||
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
|
||||
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 整数转成二进制字节数组
|
||||
/// </summary>
|
||||
/// <param name="num">整数</param>
|
||||
/// <param name="length">整数长度</param>
|
||||
/// <param name="isLowNumHigh">是否高位在数组索引低</param>
|
||||
/// <returns>二进制字节数组</returns>
|
||||
public static Result<byte[]> NumberToBytes(ulong num, uint length, bool isLowNumHigh = false)
|
||||
{
|
||||
if (length > 8)
|
||||
{
|
||||
return new(new ArgumentException(
|
||||
"Unsigned long number can't over 8 bytes(64 bits).",
|
||||
nameof(length)
|
||||
));
|
||||
}
|
||||
|
||||
var arr = new byte[length];
|
||||
|
||||
if (isLowNumHigh)
|
||||
{
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
arr[length - 1 - i] = Convert.ToByte((num >> (i << 3)) & (0xFF));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
arr[i] = Convert.ToByte((num >> ((int)(length - 1 - i) << 3)) & (0xFF));
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 二进制字节数组转成64bits整数
|
||||
/// </summary>
|
||||
/// <param name="bytes">二进制字节数组</param>
|
||||
/// <param name="isLowNumHigh">是否高位在数组索引低</param>
|
||||
/// <returns>整数</returns>
|
||||
public static Result<UInt64> BytesToUInt64(byte[] bytes, bool isLowNumHigh = false)
|
||||
{
|
||||
if (bytes.Length > 8)
|
||||
{
|
||||
return new(new ArgumentException(
|
||||
"Unsigned long number can't over 8 bytes(64 bits).",
|
||||
nameof(bytes)
|
||||
));
|
||||
}
|
||||
|
||||
UInt64 num = 0;
|
||||
int len = bytes.Length;
|
||||
|
||||
try
|
||||
{
|
||||
if (isLowNumHigh)
|
||||
{
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
num += Convert.ToUInt64((UInt64)bytes[len - 1 - i] << (i << 3));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
num += Convert.ToUInt64((UInt64)bytes[i] << ((int)(len - 1 - i) << 3));
|
||||
}
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return new(error);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 二进制字节数组转成32bits整数
|
||||
/// </summary>
|
||||
/// <param name="bytes">二进制字节数组</param>
|
||||
/// <param name="isLowNumHigh">是否高位在数组索引低</param>
|
||||
/// <returns>整数</returns>
|
||||
public static Result<UInt32> BytesToUInt32(byte[] bytes, bool isLowNumHigh = false)
|
||||
{
|
||||
if (bytes.Length > 4)
|
||||
{
|
||||
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)
|
||||
{
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
num += Convert.ToUInt32((UInt32)bytes[len - 1 - i] << (i << 3));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
num += Convert.ToUInt32((UInt32)bytes[i] << ((int)(len - 1 - i) << 3));
|
||||
}
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return new(error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <param name="uintArray">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public static Result<byte[]> UInt32ArrayToBytes(UInt32[] uintArray)
|
||||
{
|
||||
byte[] byteArray = new byte[uintArray.Length * 4];
|
||||
try
|
||||
{
|
||||
Buffer.BlockCopy(uintArray, 0, byteArray, 0, uintArray.Length * 4);
|
||||
return byteArray;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return new(error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 比特合并成二进制字节
|
||||
/// </summary>
|
||||
/// <param name="bits1">第一个比特值</param>
|
||||
/// <param name="bits1Len">第一个比特值的长度(位数)</param>
|
||||
/// <param name="bits2">第二个比特值</param>
|
||||
/// <param name="bits2Len">第二个比特值的长度(位数)</param>
|
||||
/// <returns>合并后的二进制字节数组</returns>
|
||||
public static Result<byte[]> MultiBitsToBytes(ulong bits1, uint bits1Len, ulong bits2, uint bits2Len)
|
||||
{
|
||||
return NumberToBytes(MultiBitsToNumber(bits1, bits1Len, bits2, bits2Len).Value,
|
||||
(bits1Len + bits2Len) % 8 != 0 ? (bits1Len + bits2Len) / 8 + 1 : (bits1Len + bits2Len) / 8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比特合并成整型
|
||||
/// </summary>
|
||||
/// <param name="bits1">第一个比特值</param>
|
||||
/// <param name="bits1Len">第一个比特值的长度(位数)</param>
|
||||
/// <param name="bits2">第二个比特值</param>
|
||||
/// <param name="bits2Len">第二个比特值的长度(位数)</param>
|
||||
/// <returns>合并后的整型值</returns>
|
||||
public static Result<ulong> MultiBitsToNumber(ulong bits1, uint bits1Len, ulong bits2, uint bits2Len)
|
||||
{
|
||||
if (bits1Len + bits2Len > 64) return new(new ArgumentException("Two Bits is more than 64 bits"));
|
||||
|
||||
ulong num = (bits1 << Convert.ToInt32(bits2Len)) | bits2;
|
||||
return num;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比特合并成整型
|
||||
/// </summary>
|
||||
/// <param name="bits1">第一个比特值</param>
|
||||
/// <param name="bits1Len">第一个比特值的长度(位数)</param>
|
||||
/// <param name="bits2">第二个比特值</param>
|
||||
/// <param name="bits2Len">第二个比特值的长度(位数)</param>
|
||||
/// <returns>合并后的整型值</returns>
|
||||
public static Result<uint> MultiBitsToNumber(uint bits1, uint bits1Len, uint bits2, uint bits2Len)
|
||||
{
|
||||
if (bits1Len + bits2Len > 64) return new(new ArgumentException("Two Bits is more than 64 bits"));
|
||||
|
||||
uint num = (bits1 << Convert.ToInt32(bits2Len)) | bits2;
|
||||
return num;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比特位检查
|
||||
/// </summary>
|
||||
/// <param name="srcBits">源比特值</param>
|
||||
/// <param name="dstBits">目标比特值</param>
|
||||
/// <param name="mask">掩码(默认为全1)</param>
|
||||
/// <returns>检查结果(是否匹配)</returns>
|
||||
public static bool BitsCheck(ulong srcBits, ulong dstBits, ulong mask = 0xFFFF_FFFF_FFFF_FFFF)
|
||||
{
|
||||
return (srcBits & mask) == dstBits;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比特位检查
|
||||
/// </summary>
|
||||
/// <param name="srcBits">源比特值</param>
|
||||
/// <param name="dstBits">目标比特值</param>
|
||||
/// <param name="mask">掩码(默认为全1)</param>
|
||||
/// <returns>检查结果(是否匹配)</returns>
|
||||
public static bool BitsCheck(uint srcBits, uint dstBits, uint mask = 0xFFFF_FFFF)
|
||||
{
|
||||
return (srcBits & mask) == dstBits;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取整型对应位置的比特
|
||||
/// </summary>
|
||||
/// <param name="srcBits">整型数字</param>
|
||||
/// <param name="location">位置</param>
|
||||
/// <returns>比特</returns>
|
||||
public static Result<bool> ToBit(UInt32 srcBits, int location)
|
||||
{
|
||||
if (location < 0)
|
||||
return new(new ArgumentException(
|
||||
"Location can't be negetive", nameof(location)));
|
||||
|
||||
return ((srcBits >> location) & ((UInt32)0b1)) == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将BitArray转化为32bits无符号整型
|
||||
/// </summary>
|
||||
/// <param name="bits">BitArray比特数组</param>
|
||||
/// <returns>32bits无符号整型</returns>
|
||||
public static Result<UInt32> BitsToNumber(BitArray bits)
|
||||
{
|
||||
if (bits.Length > 32)
|
||||
throw new ArgumentException("Argument length shall be at most 32 bits.");
|
||||
|
||||
var array = new UInt32[1];
|
||||
bits.CopyTo(array, 0);
|
||||
return array[0];
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 字符串转二进制字节数组
|
||||
/// </summary>
|
||||
/// <param name="str">输入的字符串</param>
|
||||
/// <param name="numBase">进制(默认为16进制)</param>
|
||||
/// <returns>转换后的二进制字节数组</returns>
|
||||
public static byte[] StringToBytes(string str, int numBase = 16)
|
||||
{
|
||||
var len = str.Length;
|
||||
var bytesLen = len / 2;
|
||||
var bytes = new byte[bytesLen];
|
||||
|
||||
for (var i = 0; i < bytesLen; i++)
|
||||
{
|
||||
bytes[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反转字节数组中的子数组
|
||||
/// </summary>
|
||||
/// <param name="srcBytes">源字节数组</param>
|
||||
/// <param name="distance">子数组的长度(反转的步长)</param>
|
||||
/// <returns>反转后的字节数组</returns>
|
||||
public static Result<byte[]> ReverseBytes(byte[] srcBytes, int distance)
|
||||
{
|
||||
if (distance <= 0)
|
||||
return new(new ArgumentException("Distance can't be negetive", nameof(distance)));
|
||||
|
||||
var srcBytesLen = srcBytes.Length;
|
||||
if (distance > srcBytesLen)
|
||||
return new(new ArgumentException(
|
||||
"Distance is larger than bytesArray", nameof(distance)));
|
||||
if (srcBytesLen % distance != 0)
|
||||
return new(new ArgumentException(
|
||||
"The length of bytes can't be divided by distance without reminder", nameof(distance)));
|
||||
|
||||
var dstBytes = new byte[srcBytesLen];
|
||||
var buffer = new byte[distance];
|
||||
|
||||
for (int i = 0; i < srcBytesLen; i += distance)
|
||||
{
|
||||
var end = i + distance;
|
||||
buffer = srcBytes[i..end];
|
||||
Array.Reverse(buffer);
|
||||
Array.Copy(buffer, 0, dstBytes, i, distance);
|
||||
}
|
||||
|
||||
return dstBytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反转字节内比特顺序(使用查找表的方法)
|
||||
/// </summary>
|
||||
/// <param name="srcByte">字节</param>
|
||||
/// <returns>反转后的字节</returns>
|
||||
public static byte ReverseBits(byte srcByte)
|
||||
{
|
||||
return BitReverseTable[srcByte];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反转字节数组的字节内比特顺序(使用查找表的方法)
|
||||
/// </summary>
|
||||
/// <param name="srcBytes">字节数组</param>
|
||||
/// <returns>反转后的字节字节数组</returns>
|
||||
public static byte[] ReverseBits(byte[] srcBytes)
|
||||
{
|
||||
var bytesLen = srcBytes.Length;
|
||||
var dstBytes = new byte[bytesLen];
|
||||
for (int i = 0; i < bytesLen; i++)
|
||||
{
|
||||
dstBytes[i] = BitReverseTable[srcBytes[i]];
|
||||
}
|
||||
return dstBytes;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字符串处理工具
|
||||
/// </summary>
|
||||
public class String
|
||||
{
|
||||
/// <summary>
|
||||
/// 反转字符串
|
||||
/// </summary>
|
||||
/// <param name="s">输入的字符串</param>
|
||||
/// <returns>反转后的字符串</returns>
|
||||
public static string Reverse(string s)
|
||||
{
|
||||
char[] charArray = s.ToCharArray();
|
||||
Array.Reverse(charArray);
|
||||
return new string(charArray);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
358
server/src/Common/Image.cs
Normal file
358
server/src/Common/Image.cs
Normal file
@@ -0,0 +1,358 @@
|
||||
using System.Text;
|
||||
using DotNext;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Formats.Jpeg;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
|
||||
namespace Common;
|
||||
|
||||
/// <summary>
|
||||
/// 图像处理工具
|
||||
/// </summary>
|
||||
public class Image
|
||||
{
|
||||
/// <summary>
|
||||
/// 将 RGB565 格式转换为 RGB24 格式
|
||||
/// RGB565: 5位红色 + 6位绿色 + 5位蓝色 = 16位 (2字节)
|
||||
/// RGB24: 8位红色 + 8位绿色 + 8位蓝色 = 24位 (3字节)
|
||||
/// </summary>
|
||||
/// <param name="rgb565Data">RGB565格式的原始数据</param>
|
||||
/// <param name="width">图像宽度</param>
|
||||
/// <param name="height">图像高度</param>
|
||||
/// <param name="isLittleEndian">是否为小端序(默认为true)</param>
|
||||
/// <returns>RGB24格式的转换后数据</returns>
|
||||
public static Result<byte[]> ConvertRGB565ToRGB24(byte[] rgb565Data, int width, int height, bool isLittleEndian = true)
|
||||
{
|
||||
if (rgb565Data == null)
|
||||
return new(new ArgumentNullException(nameof(rgb565Data)));
|
||||
|
||||
if (width <= 0 || height <= 0)
|
||||
return new(new ArgumentException("Width and height must be positive"));
|
||||
|
||||
// 计算像素数量
|
||||
var expectedPixelCount = width * height;
|
||||
var actualPixelCount = rgb565Data.Length / 2;
|
||||
|
||||
if (actualPixelCount < expectedPixelCount)
|
||||
{
|
||||
return new(new ArgumentException(
|
||||
$"RGB565 data length insufficient. Expected: {expectedPixelCount * 2} bytes, Actual: {rgb565Data.Length} bytes"));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var pixelCount = Math.Min(actualPixelCount, expectedPixelCount);
|
||||
var rgb24Data = new byte[pixelCount * 3];
|
||||
|
||||
for (int i = 0; i < pixelCount; i++)
|
||||
{
|
||||
// 读取 RGB565 数据
|
||||
var rgb565Index = i * 2;
|
||||
if (rgb565Index + 1 >= rgb565Data.Length) break;
|
||||
|
||||
// 组合成16位值
|
||||
UInt16 rgb565;
|
||||
if (isLittleEndian)
|
||||
{
|
||||
rgb565 = (UInt16)(rgb565Data[rgb565Index] | (rgb565Data[rgb565Index + 1] << 8));
|
||||
}
|
||||
else
|
||||
{
|
||||
rgb565 = (UInt16)((rgb565Data[rgb565Index] << 8) | rgb565Data[rgb565Index + 1]);
|
||||
}
|
||||
|
||||
// 提取各颜色分量
|
||||
var r5 = (rgb565 >> 11) & 0x1F; // 高5位为红色
|
||||
var g6 = (rgb565 >> 5) & 0x3F; // 中间6位为绿色
|
||||
var b5 = rgb565 & 0x1F; // 低5位为蓝色
|
||||
|
||||
// 转换为8位颜色值
|
||||
var r8 = (byte)((r5 * 255) / 31); // 5位扩展到8位
|
||||
var g8 = (byte)((g6 * 255) / 63); // 6位扩展到8位
|
||||
var b8 = (byte)((b5 * 255) / 31); // 5位扩展到8位
|
||||
|
||||
// 存储到 RGB24 数组
|
||||
var rgb24Index = (i%2 == 0)?((i+1) * 3):((i-1) * 3);
|
||||
rgb24Data[rgb24Index] = r8; // R
|
||||
rgb24Data[rgb24Index + 1] = g8; // G
|
||||
rgb24Data[rgb24Index + 2] = b8; // B
|
||||
}
|
||||
|
||||
return rgb24Data;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 RGB24 格式转换为 RGB565 格式
|
||||
/// RGB24: 8位红色 + 8位绿色 + 8位蓝色 = 24位 (3字节)
|
||||
/// RGB565: 5位红色 + 6位绿色 + 5位蓝色 = 16位 (2字节)
|
||||
/// </summary>
|
||||
/// <param name="rgb24Data">RGB24格式的原始数据</param>
|
||||
/// <param name="width">图像宽度</param>
|
||||
/// <param name="height">图像高度</param>
|
||||
/// <param name="isLittleEndian">是否为小端序(默认为true)</param>
|
||||
/// <returns>RGB565格式的转换后数据</returns>
|
||||
public static Result<byte[]> ConvertRGB24ToRGB565(byte[] rgb24Data, int width, int height, bool isLittleEndian = true)
|
||||
{
|
||||
if (rgb24Data == null)
|
||||
return new(new ArgumentNullException(nameof(rgb24Data)));
|
||||
|
||||
if (width <= 0 || height <= 0)
|
||||
return new(new ArgumentException("Width and height must be positive"));
|
||||
|
||||
var expectedPixelCount = width * height;
|
||||
var actualPixelCount = rgb24Data.Length / 3;
|
||||
|
||||
if (actualPixelCount < expectedPixelCount)
|
||||
{
|
||||
return new(new ArgumentException(
|
||||
$"RGB24 data length insufficient. Expected: {expectedPixelCount * 3} bytes, Actual: {rgb24Data.Length} bytes"));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var pixelCount = Math.Min(actualPixelCount, expectedPixelCount);
|
||||
var rgb565Data = new byte[pixelCount * 2];
|
||||
|
||||
for (int i = 0; i < pixelCount; i++)
|
||||
{
|
||||
var rgb24Index = i * 3;
|
||||
if (rgb24Index + 2 >= rgb24Data.Length) break;
|
||||
|
||||
// 读取 RGB24 数据
|
||||
var r8 = rgb24Data[rgb24Index];
|
||||
var g8 = rgb24Data[rgb24Index + 1];
|
||||
var b8 = rgb24Data[rgb24Index + 2];
|
||||
|
||||
// 转换为5位、6位、5位
|
||||
var r5 = (UInt16)((r8 * 31) / 255);
|
||||
var g6 = (UInt16)((g8 * 63) / 255);
|
||||
var b5 = (UInt16)((b8 * 31) / 255);
|
||||
|
||||
// 组合成16位值
|
||||
var rgb565 = (UInt16)((r5 << 11) | (g6 << 5) | b5);
|
||||
|
||||
// 存储到 RGB565 数组
|
||||
var rgb565Index = i * 2;
|
||||
if (isLittleEndian)
|
||||
{
|
||||
rgb565Data[rgb565Index] = (byte)(rgb565 & 0xFF);
|
||||
rgb565Data[rgb565Index + 1] = (byte)(rgb565 >> 8);
|
||||
}
|
||||
else
|
||||
{
|
||||
rgb565Data[rgb565Index] = (byte)(rgb565 >> 8);
|
||||
rgb565Data[rgb565Index + 1] = (byte)(rgb565 & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
return rgb565Data;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 RGB24 数据转换为 JPEG 格式
|
||||
/// </summary>
|
||||
/// <param name="rgb24Data">RGB24格式的图像数据</param>
|
||||
/// <param name="width">图像宽度</param>
|
||||
/// <param name="height">图像高度</param>
|
||||
/// <param name="quality">JPEG质量(1-100,默认80)</param>
|
||||
/// <returns>JPEG格式的字节数组</returns>
|
||||
public static Result<byte[]> ConvertRGB24ToJpeg(byte[] rgb24Data, int width, int height, int quality = 80)
|
||||
{
|
||||
if (rgb24Data == null)
|
||||
return new(new ArgumentNullException(nameof(rgb24Data)));
|
||||
|
||||
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"));
|
||||
|
||||
var expectedDataLength = width * height * 3;
|
||||
if (rgb24Data.Length < expectedDataLength)
|
||||
{
|
||||
return new(new ArgumentException(
|
||||
$"RGB24 data length insufficient. Expected: {expectedDataLength} bytes, Actual: {rgb24Data.Length} bytes"));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var image = new SixLabors.ImageSharp.Image<Rgb24>(width, height);
|
||||
|
||||
// 将 RGB 数据复制到 ImageSharp 图像
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
int index = (y * width + x) * 3;
|
||||
if (index + 2 < rgb24Data.Length)
|
||||
{
|
||||
var pixel = new Rgb24(rgb24Data[index], rgb24Data[index + 1], rgb24Data[index + 2]);
|
||||
image[x, y] = pixel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
using var stream = new MemoryStream();
|
||||
image.SaveAsJpeg(stream, new JpegEncoder { Quality = quality });
|
||||
return stream.ToArray();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 RGB565 数据直接转换为 JPEG 格式
|
||||
/// </summary>
|
||||
/// <param name="rgb565Data">RGB565格式的图像数据</param>
|
||||
/// <param name="width">图像宽度</param>
|
||||
/// <param name="height">图像高度</param>
|
||||
/// <param name="quality">JPEG质量(1-100,默认80)</param>
|
||||
/// <param name="isLittleEndian">是否为小端序(默认为true)</param>
|
||||
/// <returns>JPEG格式的字节数组</returns>
|
||||
public static Result<byte[]> ConvertRGB565ToJpeg(byte[] rgb565Data, int width, int height, int quality = 80, bool isLittleEndian = true)
|
||||
{
|
||||
// 先转换为RGB24
|
||||
var rgb24Result = ConvertRGB565ToRGB24(rgb565Data, width, height, isLittleEndian);
|
||||
if (!rgb24Result.IsSuccessful)
|
||||
{
|
||||
return new(rgb24Result.Error);
|
||||
}
|
||||
|
||||
// 再转换为JPEG
|
||||
return ConvertRGB24ToJpeg(rgb24Result.Value, width, height, quality);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建 MJPEG 帧头部
|
||||
/// </summary>
|
||||
/// <param name="frameDataLength">帧数据长度</param>
|
||||
/// <param name="boundary">边界字符串(默认为"--boundary")</param>
|
||||
/// <returns>MJPEG帧头部字节数组</returns>
|
||||
public static byte[] CreateMjpegFrameHeader(int frameDataLength, string boundary = "--boundary")
|
||||
{
|
||||
var header = $"{boundary}\r\nContent-Type: image/jpeg\r\nContent-Length: {frameDataLength}\r\n\r\n";
|
||||
return Encoding.ASCII.GetBytes(header);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建 MJPEG 帧尾部
|
||||
/// </summary>
|
||||
/// <returns>MJPEG帧尾部字节数组</returns>
|
||||
public static byte[] CreateMjpegFrameFooter()
|
||||
{
|
||||
return Encoding.ASCII.GetBytes("\r\n");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建完整的 MJPEG 帧数据
|
||||
/// </summary>
|
||||
/// <param name="jpegData">JPEG数据</param>
|
||||
/// <param name="boundary">边界字符串(默认为"--boundary")</param>
|
||||
/// <returns>完整的MJPEG帧数据</returns>
|
||||
public static Result<byte[]> CreateMjpegFrame(byte[] jpegData, string boundary = "--boundary")
|
||||
{
|
||||
if (jpegData == null)
|
||||
return new(new ArgumentNullException(nameof(jpegData)));
|
||||
|
||||
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 frameData;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证图像数据长度是否正确
|
||||
/// </summary>
|
||||
/// <param name="data">图像数据</param>
|
||||
/// <param name="width">图像宽度</param>
|
||||
/// <param name="height">图像高度</param>
|
||||
/// <param name="bytesPerPixel">每像素字节数</param>
|
||||
/// <returns>验证结果</returns>
|
||||
public static bool ValidateImageDataLength(byte[] data, int width, int height, int bytesPerPixel)
|
||||
{
|
||||
if (data == null || width <= 0 || height <= 0 || bytesPerPixel <= 0)
|
||||
return false;
|
||||
|
||||
var expectedLength = width * height * bytesPerPixel;
|
||||
return data.Length >= expectedLength;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取图像格式信息
|
||||
/// </summary>
|
||||
/// <param name="format">图像格式枚举</param>
|
||||
/// <returns>格式信息</returns>
|
||||
public static ImageFormatInfo GetImageFormatInfo(ImageFormat format)
|
||||
{
|
||||
return format switch
|
||||
{
|
||||
ImageFormat.RGB565 => new ImageFormatInfo("RGB565", 2, "16-bit RGB format (5R+6G+5B)"),
|
||||
ImageFormat.RGB24 => new ImageFormatInfo("RGB24", 3, "24-bit RGB format (8R+8G+8B)"),
|
||||
ImageFormat.RGBA32 => new ImageFormatInfo("RGBA32", 4, "32-bit RGBA format (8R+8G+8B+8A)"),
|
||||
ImageFormat.Grayscale8 => new ImageFormatInfo("Grayscale8", 1, "8-bit grayscale format"),
|
||||
_ => new ImageFormatInfo("Unknown", 0, "Unknown image format")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 图像格式枚举
|
||||
/// </summary>
|
||||
public enum ImageFormat
|
||||
{
|
||||
/// <summary>
|
||||
/// RGB565
|
||||
/// </summary>
|
||||
RGB565,
|
||||
|
||||
/// <summary>
|
||||
/// RGB888 / RGB24
|
||||
/// </summary>
|
||||
RGB24,
|
||||
|
||||
/// <summary>
|
||||
/// RGBA8888 / RGBA32
|
||||
/// </summary>
|
||||
RGBA32,
|
||||
|
||||
/// <summary>
|
||||
/// 灰度图
|
||||
/// </summary>
|
||||
Grayscale8
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 图像格式信息
|
||||
/// </summary>
|
||||
public record ImageFormatInfo(string Name, int BytesPerPixel, string Description);
|
||||
370
server/src/Common/Number.cs
Normal file
370
server/src/Common/Number.cs
Normal file
@@ -0,0 +1,370 @@
|
||||
using System.Collections;
|
||||
using DotNext;
|
||||
|
||||
namespace Common;
|
||||
/// <summary>
|
||||
/// 数字处理工具
|
||||
/// </summary>
|
||||
public class Number
|
||||
{
|
||||
private static readonly byte[] BitReverseTable = new byte[] {
|
||||
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
|
||||
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
|
||||
0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
|
||||
0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
|
||||
0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
|
||||
0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
|
||||
0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
|
||||
0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
|
||||
0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
|
||||
0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
|
||||
0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
|
||||
0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
|
||||
0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
|
||||
0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
|
||||
0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
|
||||
0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
|
||||
0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
|
||||
0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
|
||||
0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
|
||||
0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
|
||||
0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
|
||||
0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
|
||||
0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
|
||||
0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
|
||||
0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
|
||||
0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
|
||||
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
|
||||
0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
|
||||
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
|
||||
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
|
||||
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
|
||||
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 整数转成二进制字节数组
|
||||
/// </summary>
|
||||
/// <param name="num">整数</param>
|
||||
/// <param name="length">整数长度</param>
|
||||
/// <param name="isLowNumHigh">是否高位在数组索引低</param>
|
||||
/// <returns>二进制字节数组</returns>
|
||||
public static Result<byte[]> NumberToBytes(ulong num, uint length, bool isLowNumHigh = false)
|
||||
{
|
||||
if (length > 8)
|
||||
{
|
||||
return new(new ArgumentException(
|
||||
"Unsigned long number can't over 8 bytes(64 bits).",
|
||||
nameof(length)
|
||||
));
|
||||
}
|
||||
|
||||
var arr = new byte[length];
|
||||
|
||||
if (isLowNumHigh)
|
||||
{
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
arr[length - 1 - i] = Convert.ToByte((num >> (i << 3)) & (0xFF));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
arr[i] = Convert.ToByte((num >> ((int)(length - 1 - i) << 3)) & (0xFF));
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 二进制字节数组转成64bits整数
|
||||
/// </summary>
|
||||
/// <param name="bytes">二进制字节数组</param>
|
||||
/// <param name="isLowNumHigh">是否高位在数组索引低</param>
|
||||
/// <returns>整数</returns>
|
||||
public static Result<UInt64> BytesToUInt64(byte[] bytes, bool isLowNumHigh = false)
|
||||
{
|
||||
if (bytes.Length > 8)
|
||||
{
|
||||
return new(new ArgumentException(
|
||||
"Unsigned long number can't over 8 bytes(64 bits).",
|
||||
nameof(bytes)
|
||||
));
|
||||
}
|
||||
|
||||
UInt64 num = 0;
|
||||
int len = bytes.Length;
|
||||
|
||||
try
|
||||
{
|
||||
if (isLowNumHigh)
|
||||
{
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
num += Convert.ToUInt64((UInt64)bytes[len - 1 - i] << (i << 3));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
num += Convert.ToUInt64((UInt64)bytes[i] << ((int)(len - 1 - i) << 3));
|
||||
}
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return new(error);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 二进制字节数组转成32bits整数
|
||||
/// </summary>
|
||||
/// <param name="bytes">二进制字节数组</param>
|
||||
/// <param name="isLowNumHigh">是否高位在数组索引低</param>
|
||||
/// <returns>整数</returns>
|
||||
public static Result<UInt32> BytesToUInt32(byte[] bytes, bool isLowNumHigh = false)
|
||||
{
|
||||
if (bytes.Length > 4)
|
||||
{
|
||||
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)
|
||||
{
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
num += Convert.ToUInt32((UInt32)bytes[len - 1 - i] << (i << 3));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
num += Convert.ToUInt32((UInt32)bytes[i] << ((int)(len - 1 - i) << 3));
|
||||
}
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return new(error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <param name="uintArray">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public static Result<byte[]> UInt32ArrayToBytes(UInt32[] uintArray)
|
||||
{
|
||||
byte[] byteArray = new byte[uintArray.Length * 4];
|
||||
try
|
||||
{
|
||||
Buffer.BlockCopy(uintArray, 0, byteArray, 0, uintArray.Length * 4);
|
||||
return byteArray;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return new(error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 比特合并成二进制字节
|
||||
/// </summary>
|
||||
/// <param name="bits1">第一个比特值</param>
|
||||
/// <param name="bits1Len">第一个比特值的长度(位数)</param>
|
||||
/// <param name="bits2">第二个比特值</param>
|
||||
/// <param name="bits2Len">第二个比特值的长度(位数)</param>
|
||||
/// <returns>合并后的二进制字节数组</returns>
|
||||
public static Result<byte[]> MultiBitsToBytes(ulong bits1, uint bits1Len, ulong bits2, uint bits2Len)
|
||||
{
|
||||
return NumberToBytes(MultiBitsToNumber(bits1, bits1Len, bits2, bits2Len).Value,
|
||||
(bits1Len + bits2Len) % 8 != 0 ? (bits1Len + bits2Len) / 8 + 1 : (bits1Len + bits2Len) / 8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比特合并成整型
|
||||
/// </summary>
|
||||
/// <param name="bits1">第一个比特值</param>
|
||||
/// <param name="bits1Len">第一个比特值的长度(位数)</param>
|
||||
/// <param name="bits2">第二个比特值</param>
|
||||
/// <param name="bits2Len">第二个比特值的长度(位数)</param>
|
||||
/// <returns>合并后的整型值</returns>
|
||||
public static Result<ulong> MultiBitsToNumber(ulong bits1, uint bits1Len, ulong bits2, uint bits2Len)
|
||||
{
|
||||
if (bits1Len + bits2Len > 64) return new(new ArgumentException("Two Bits is more than 64 bits"));
|
||||
|
||||
ulong num = (bits1 << Convert.ToInt32(bits2Len)) | bits2;
|
||||
return num;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比特合并成整型
|
||||
/// </summary>
|
||||
/// <param name="bits1">第一个比特值</param>
|
||||
/// <param name="bits1Len">第一个比特值的长度(位数)</param>
|
||||
/// <param name="bits2">第二个比特值</param>
|
||||
/// <param name="bits2Len">第二个比特值的长度(位数)</param>
|
||||
/// <returns>合并后的整型值</returns>
|
||||
public static Result<uint> MultiBitsToNumber(uint bits1, uint bits1Len, uint bits2, uint bits2Len)
|
||||
{
|
||||
if (bits1Len + bits2Len > 64) return new(new ArgumentException("Two Bits is more than 64 bits"));
|
||||
|
||||
uint num = (bits1 << Convert.ToInt32(bits2Len)) | bits2;
|
||||
return num;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比特位检查
|
||||
/// </summary>
|
||||
/// <param name="srcBits">源比特值</param>
|
||||
/// <param name="dstBits">目标比特值</param>
|
||||
/// <param name="mask">掩码(默认为全1)</param>
|
||||
/// <returns>检查结果(是否匹配)</returns>
|
||||
public static bool BitsCheck(ulong srcBits, ulong dstBits, ulong mask = 0xFFFF_FFFF_FFFF_FFFF)
|
||||
{
|
||||
return (srcBits & mask) == dstBits;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比特位检查
|
||||
/// </summary>
|
||||
/// <param name="srcBits">源比特值</param>
|
||||
/// <param name="dstBits">目标比特值</param>
|
||||
/// <param name="mask">掩码(默认为全1)</param>
|
||||
/// <returns>检查结果(是否匹配)</returns>
|
||||
public static bool BitsCheck(uint srcBits, uint dstBits, uint mask = 0xFFFF_FFFF)
|
||||
{
|
||||
return (srcBits & mask) == dstBits;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取整型对应位置的比特
|
||||
/// </summary>
|
||||
/// <param name="srcBits">整型数字</param>
|
||||
/// <param name="location">位置</param>
|
||||
/// <returns>比特</returns>
|
||||
public static Result<bool> ToBit(UInt32 srcBits, int location)
|
||||
{
|
||||
if (location < 0)
|
||||
return new(new ArgumentException(
|
||||
"Location can't be negetive", nameof(location)));
|
||||
|
||||
return ((srcBits >> location) & ((UInt32)0b1)) == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将BitArray转化为32bits无符号整型
|
||||
/// </summary>
|
||||
/// <param name="bits">BitArray比特数组</param>
|
||||
/// <returns>32bits无符号整型</returns>
|
||||
public static Result<UInt32> BitsToNumber(BitArray bits)
|
||||
{
|
||||
if (bits.Length > 32)
|
||||
throw new ArgumentException("Argument length shall be at most 32 bits.");
|
||||
|
||||
var array = new UInt32[1];
|
||||
bits.CopyTo(array, 0);
|
||||
return array[0];
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 字符串转二进制字节数组
|
||||
/// </summary>
|
||||
/// <param name="str">输入的字符串</param>
|
||||
/// <param name="numBase">进制(默认为16进制)</param>
|
||||
/// <returns>转换后的二进制字节数组</returns>
|
||||
public static byte[] StringToBytes(string str, int numBase = 16)
|
||||
{
|
||||
var len = str.Length;
|
||||
var bytesLen = len / 2;
|
||||
var bytes = new byte[bytesLen];
|
||||
|
||||
for (var i = 0; i < bytesLen; i++)
|
||||
{
|
||||
bytes[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反转字节数组中的子数组
|
||||
/// </summary>
|
||||
/// <param name="srcBytes">源字节数组</param>
|
||||
/// <param name="distance">子数组的长度(反转的步长)</param>
|
||||
/// <returns>反转后的字节数组</returns>
|
||||
public static Result<byte[]> ReverseBytes(byte[] srcBytes, int distance)
|
||||
{
|
||||
if (distance <= 0)
|
||||
return new(new ArgumentException("Distance can't be negetive", nameof(distance)));
|
||||
|
||||
var srcBytesLen = srcBytes.Length;
|
||||
if (distance > srcBytesLen)
|
||||
return new(new ArgumentException(
|
||||
"Distance is larger than bytesArray", nameof(distance)));
|
||||
if (srcBytesLen % distance != 0)
|
||||
return new(new ArgumentException(
|
||||
"The length of bytes can't be divided by distance without reminder", nameof(distance)));
|
||||
|
||||
var dstBytes = new byte[srcBytesLen];
|
||||
var buffer = new byte[distance];
|
||||
|
||||
for (int i = 0; i < srcBytesLen; i += distance)
|
||||
{
|
||||
var end = i + distance;
|
||||
buffer = srcBytes[i..end];
|
||||
Array.Reverse(buffer);
|
||||
Array.Copy(buffer, 0, dstBytes, i, distance);
|
||||
}
|
||||
|
||||
return dstBytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反转字节内比特顺序(使用查找表的方法)
|
||||
/// </summary>
|
||||
/// <param name="srcByte">字节</param>
|
||||
/// <returns>反转后的字节</returns>
|
||||
public static byte ReverseBits(byte srcByte)
|
||||
{
|
||||
return BitReverseTable[srcByte];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反转字节数组的字节内比特顺序(使用查找表的方法)
|
||||
/// </summary>
|
||||
/// <param name="srcBytes">字节数组</param>
|
||||
/// <returns>反转后的字节字节数组</returns>
|
||||
public static byte[] ReverseBits(byte[] srcBytes)
|
||||
{
|
||||
var bytesLen = srcBytes.Length;
|
||||
var dstBytes = new byte[bytesLen];
|
||||
for (int i = 0; i < bytesLen; i++)
|
||||
{
|
||||
dstBytes[i] = BitReverseTable[srcBytes[i]];
|
||||
}
|
||||
return dstBytes;
|
||||
}
|
||||
}
|
||||
116
server/src/Common/SemaphorePool.cs
Normal file
116
server/src/Common/SemaphorePool.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using System.Collections.Concurrent;
|
||||
using DotNext;
|
||||
|
||||
namespace Common;
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
public class SemaphorePool
|
||||
{
|
||||
private SemaphoreSlim semaphore;
|
||||
private ConcurrentQueue<int> queue;
|
||||
private int beginNum;
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
public int RemainingCount { get; }
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
public int MaxCount { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <param name="initialCount">[TODO:parameter]</param>
|
||||
/// <param name="beginNum">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public SemaphorePool(int initialCount, int beginNum = 0)
|
||||
{
|
||||
semaphore = new SemaphoreSlim(initialCount);
|
||||
queue = new ConcurrentQueue<int>();
|
||||
this.beginNum = beginNum;
|
||||
this.RemainingCount = initialCount;
|
||||
this.MaxCount = initialCount;
|
||||
for (int i = 0; i < initialCount; i++)
|
||||
{
|
||||
queue.Enqueue(beginNum + i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <param name="initialCount">[TODO:parameter]</param>
|
||||
/// <param name="maxCount">[TODO:parameter]</param>
|
||||
/// <param name="beginNum">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public SemaphorePool(int initialCount, int maxCount, int beginNum = 0)
|
||||
{
|
||||
semaphore = new SemaphoreSlim(initialCount, maxCount);
|
||||
queue = new ConcurrentQueue<int>();
|
||||
this.beginNum = beginNum;
|
||||
this.RemainingCount = initialCount;
|
||||
this.MaxCount = maxCount;
|
||||
for (int i = 0; i < initialCount; i++)
|
||||
{
|
||||
queue.Enqueue(beginNum + i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public Result<int> Wait()
|
||||
{
|
||||
semaphore.Wait();
|
||||
|
||||
int pop;
|
||||
if (queue.TryDequeue(out pop))
|
||||
{
|
||||
return pop;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new(new Exception($"TODO"));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public async ValueTask<Result<int>> WaitAsync()
|
||||
{
|
||||
await semaphore.WaitAsync();
|
||||
|
||||
int pop;
|
||||
if (queue.TryDequeue(out pop))
|
||||
{
|
||||
return pop;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new(new Exception($"TODO"));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public void Release()
|
||||
{
|
||||
semaphore.Release();
|
||||
queue.Clear();
|
||||
for (int i = 0; i < MaxCount; i++)
|
||||
{
|
||||
queue.Enqueue(beginNum + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
20
server/src/Common/String.cs
Normal file
20
server/src/Common/String.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace Common;
|
||||
|
||||
/// <summary>
|
||||
/// 字符串处理工具
|
||||
/// </summary>
|
||||
public class String
|
||||
{
|
||||
/// <summary>
|
||||
/// 反转字符串
|
||||
/// </summary>
|
||||
/// <param name="s">输入的字符串</param>
|
||||
/// <returns>反转后的字符串</returns>
|
||||
public static string Reverse(string s)
|
||||
{
|
||||
char[] charArray = s.ToCharArray();
|
||||
Array.Reverse(charArray);
|
||||
return new string(charArray);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
@@ -13,57 +18,363 @@ public class DataController : ControllerBase
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
/// <summary>
|
||||
/// 创建数据库表
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <returns>插入的记录数</returns>
|
||||
[EnableCors("Development")]
|
||||
[HttpPost("CreateTable")]
|
||||
public IResult CreateTables()
|
||||
public class UserInfo
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
db.CreateAllTables();
|
||||
return TypedResults.Ok();
|
||||
/// <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>
|
||||
/// 删除数据库表
|
||||
/// 用户登录,获取 JWT 令牌
|
||||
/// </summary>
|
||||
/// <returns>插入的记录数</returns>
|
||||
[EnableCors("Development")]
|
||||
[HttpDelete("DropTables")]
|
||||
public IResult DropTables()
|
||||
/// <param name="name">用户名</param>
|
||||
/// <param name="password">用户密码</param>
|
||||
/// <returns>JWT 令牌字符串</returns>
|
||||
[HttpPost("Login")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult Login(string name, string password)
|
||||
{
|
||||
// 验证用户密码
|
||||
using var db = new Database.AppDataConnection();
|
||||
db.DropAllTables();
|
||||
return TypedResults.Ok();
|
||||
var ret = db.CheckUserPassword(name, password);
|
||||
if (!ret.IsSuccessful) return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
|
||||
if (!ret.Value.HasValue) return BadRequest("用户名或密码错误");
|
||||
var user = ret.Value.Value;
|
||||
|
||||
// 生成 JWT
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes("my secret key 1234567890my secret key 1234567890");
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new Claim[]
|
||||
{
|
||||
new Claim(ClaimTypes.Name, user.Name),
|
||||
new Claim(ClaimTypes.Email, user.EMail),
|
||||
new Claim(ClaimTypes.Role, user.Permission.ToString())
|
||||
}),
|
||||
Expires = DateTime.UtcNow.AddHours(1),
|
||||
SigningCredentials = new SigningCredentials(
|
||||
new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature),
|
||||
Audience = "dlut.edu.cn",
|
||||
Issuer = "dlut.edu.cn",
|
||||
};
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
var jwt = tokenHandler.WriteToken(token);
|
||||
|
||||
return Ok(jwt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有用户
|
||||
/// 测试用户认证,需携带有效 JWT
|
||||
/// </summary>
|
||||
/// <returns>用户列表</returns>
|
||||
[HttpGet("AllUsers")]
|
||||
public IResult AllUsers()
|
||||
/// <returns>认证成功信息</returns>
|
||||
[Authorize]
|
||||
[HttpGet("TestAuth")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public IActionResult TestAuth()
|
||||
{
|
||||
return Ok("认证成功!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试管理员用户认证,需携带有效 JWT
|
||||
/// </summary>
|
||||
/// <returns>认证成功信息</returns>
|
||||
[Authorize("Admin")]
|
||||
[HttpGet("TestAdminAuth")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public IActionResult TestAdminAuth()
|
||||
{
|
||||
return Ok("认证成功!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前用户信息
|
||||
/// </summary>
|
||||
/// <returns>用户信息,包括ID、用户名、邮箱和板卡ID</returns>
|
||||
[Authorize]
|
||||
[HttpGet("GetUserInfo")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(UserInfo), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public IActionResult GetUserInfo()
|
||||
{
|
||||
// Get User Name
|
||||
var userName = User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(userName))
|
||||
return Unauthorized("未找到用户名信息");
|
||||
|
||||
// Get User Info
|
||||
using var db = new Database.AppDataConnection();
|
||||
var ret = db.User.ToList();
|
||||
return TypedResults.Ok(ret);
|
||||
var ret = db.GetUserByName(userName);
|
||||
if (!ret.IsSuccessful)
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
|
||||
|
||||
if (!ret.Value.HasValue)
|
||||
return BadRequest("用户不存在");
|
||||
|
||||
var user = ret.Value.Value;
|
||||
return Ok(new UserInfo
|
||||
{
|
||||
ID = user.ID,
|
||||
Name = user.Name,
|
||||
EMail = user.EMail,
|
||||
BoardID = user.BoardID,
|
||||
BoardExpireTime = user.BoardExpireTime,
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册新用户
|
||||
/// </summary>
|
||||
/// <param name="name">用户名</param>
|
||||
/// <returns>操作结果</returns>
|
||||
/// <param name="name">用户名(不超过255个字符)</param>
|
||||
/// <param name="email">邮箱地址</param>
|
||||
/// <param name="password">用户密码</param>
|
||||
/// <returns>操作结果,成功返回 true,失败返回错误信息</returns>
|
||||
[HttpPost("SignUpUser")]
|
||||
public IResult SignUpUser(string name)
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult SignUpUser(string name, string email, string password)
|
||||
{
|
||||
if (name.Length > 255)
|
||||
return TypedResults.BadRequest("Name Couln't over 255 characters");
|
||||
// 验证输入参数
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return BadRequest("用户名不能为空");
|
||||
|
||||
using var db = new Database.AppDataConnection();
|
||||
var ret = db.AddUser(name);
|
||||
return TypedResults.Ok(ret);
|
||||
if (name.Length > 255)
|
||||
return BadRequest("用户名不能超过255个字符");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(email))
|
||||
return BadRequest("邮箱不能为空");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(password))
|
||||
return BadRequest("密码不能为空");
|
||||
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var ret = db.AddUser(name, email, password);
|
||||
return Ok(ret);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "注册用户时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "注册失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个空闲的实验板(普通用户权限)
|
||||
/// </summary>
|
||||
/// <param name="durationHours">绑定持续时间(小时),默认为1小时</param>
|
||||
[Authorize]
|
||||
[HttpGet("GetAvailableBoard")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(Database.Board), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetAvailableBoard(int durationHours = 1)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userName = User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(userName))
|
||||
return Unauthorized("未找到用户名信息");
|
||||
|
||||
using var db = new Database.AppDataConnection();
|
||||
var userRet = db.GetUserByName(userName);
|
||||
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
|
||||
return BadRequest("用户不存在");
|
||||
|
||||
var user = userRet.Value.Value;
|
||||
var expireTime = DateTime.UtcNow.AddHours(durationHours);
|
||||
|
||||
var boardOpt = db.GetAvailableBoard(user.ID, expireTime);
|
||||
if (!boardOpt.HasValue)
|
||||
return NotFound("没有可用的实验板");
|
||||
|
||||
return Ok(boardOpt.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取空闲实验板时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解除当前用户绑定的实验板(普通用户权限)
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[HttpPost("UnbindBoard")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult UnbindBoard()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userName = User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(userName))
|
||||
return Unauthorized("未找到用户名信息");
|
||||
|
||||
using var db = new Database.AppDataConnection();
|
||||
var userRet = db.GetUserByName(userName);
|
||||
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
|
||||
return BadRequest("用户不存在");
|
||||
|
||||
var user = userRet.Value.Value;
|
||||
var result = db.UnbindUserFromBoard(user.ID);
|
||||
return Ok(result > 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "解除实验板绑定时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "解除失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户根据实验板ID获取实验板信息(普通用户权限)
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[HttpGet("GetBoardByID")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(Database.Board), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetBoardByID(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var ret = db.GetBoardByID(id);
|
||||
if (!ret.IsSuccessful)
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
|
||||
if (!ret.Value.HasValue)
|
||||
return NotFound("未找到对应的实验板");
|
||||
return Ok(ret.Value.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取实验板信息时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增板子(管理员权限)
|
||||
/// </summary>
|
||||
[Authorize("Admin")]
|
||||
[HttpPost("AddBoard")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult AddBoard(string name, string ipAddr, int port)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return BadRequest("板子名称不能为空");
|
||||
if (string.IsNullOrWhiteSpace(ipAddr))
|
||||
return BadRequest("IP地址不能为空");
|
||||
if (port <= 0 || port > 65535)
|
||||
return BadRequest("端口号不合法");
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var ret = db.AddBoard(name, ipAddr, port);
|
||||
return Ok(ret);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "新增板子时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "新增失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除板子(管理员权限)
|
||||
/// </summary>
|
||||
[Authorize("Admin")]
|
||||
[HttpDelete("DeleteBoard")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult DeleteBoard(Guid id)
|
||||
{
|
||||
if (id == Guid.Empty)
|
||||
return BadRequest("板子Guid不能为空");
|
||||
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var ret = db.DeleteBoardByID(id);
|
||||
return Ok(ret);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "删除板子时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "删除失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取全部板子(管理员权限)
|
||||
/// </summary>
|
||||
[Authorize("Admin")]
|
||||
[HttpGet("GetAllBoards")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(Database.Board[]), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetAllBoards()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var boards = db.GetAllBoard();
|
||||
return Ok(boards);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取全部板子时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// Jtag API
|
||||
/// JTAG 控制器 - 提供 JTAG 相关的 API 操作
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize] // 添加用户认证要求
|
||||
public class JtagController : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
@@ -15,134 +17,193 @@ public class JtagController : ControllerBase
|
||||
private const string BITSTREAM_PATH = "bitstream/Jtag";
|
||||
|
||||
/// <summary>
|
||||
/// 页面
|
||||
/// 控制器首页信息
|
||||
/// </summary>
|
||||
/// <returns>控制器描述信息</returns>
|
||||
[HttpGet]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||
public string Index()
|
||||
{
|
||||
logger.Info($"User {User.Identity?.Name} accessed Jtag controller index");
|
||||
return "This is Jtag Controller";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取Jtag ID Code
|
||||
/// 获取 JTAG 设备的 ID Code
|
||||
/// </summary>
|
||||
/// <param name="address"> 设备地址 </param>
|
||||
/// <param name="port"> 设备端口 </param>
|
||||
/// <param name="address">JTAG 设备地址</param>
|
||||
/// <param name="port">JTAG 设备端口</param>
|
||||
/// <returns>设备的 ID Code</returns>
|
||||
[HttpGet("GetDeviceIDCode")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(uint), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async ValueTask<IResult> GetDeviceIDCode(string address, int port)
|
||||
{
|
||||
var jtagCtrl = new JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.ReadIDCode();
|
||||
logger.Info($"User {User.Identity?.Name} requesting device ID code from {address}:{port}");
|
||||
|
||||
if (ret.IsSuccessful)
|
||||
try
|
||||
{
|
||||
logger.Info($"Get device {address} ID code: 0x{ret.Value:X4}");
|
||||
return TypedResults.Ok(ret.Value);
|
||||
var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.ReadIDCode();
|
||||
|
||||
if (ret.IsSuccessful)
|
||||
{
|
||||
logger.Info($"User {User.Identity?.Name} successfully got device {address} ID code: 0x{ret.Value:X8}");
|
||||
return TypedResults.Ok(ret.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Error($"User {User.Identity?.Name} failed to get device {address} ID code: {ret.Error}");
|
||||
return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ret.Error);
|
||||
return TypedResults.InternalServerError(ret.Error);
|
||||
logger.Error(ex, $"User {User.Identity?.Name} encountered exception while getting device {address} ID code");
|
||||
return TypedResults.InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取状态寄存器
|
||||
/// 读取 JTAG 设备的状态寄存器
|
||||
/// </summary>
|
||||
/// <param name="address"> 设备地址 </param>
|
||||
/// <param name="port"> 设备端口 </param>
|
||||
/// <param name="address">JTAG 设备地址</param>
|
||||
/// <param name="port">JTAG 设备端口</param>
|
||||
/// <returns>状态寄存器的原始值、二进制表示和解码值</returns>
|
||||
[HttpGet("ReadStatusReg")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async ValueTask<IResult> ReadStatusReg(string address, int port)
|
||||
{
|
||||
var jtagCtrl = new JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.ReadStatusReg();
|
||||
logger.Info($"User {User.Identity?.Name} requesting status register from {address}:{port}");
|
||||
|
||||
if (ret.IsSuccessful)
|
||||
try
|
||||
{
|
||||
var binaryValue = Common.String.Reverse(Convert.ToString(ret.Value, 2).PadLeft(32, '0'));
|
||||
var decodeValue = new JtagClient.JtagStatusReg(ret.Value);
|
||||
logger.Info($"Read device {address} Status Register: \n\t 0b{binaryValue} \n\t {decodeValue}");
|
||||
return TypedResults.Ok(new
|
||||
var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.ReadStatusReg();
|
||||
|
||||
if (ret.IsSuccessful)
|
||||
{
|
||||
original = ret.Value,
|
||||
binaryValue,
|
||||
decodeValue,
|
||||
});
|
||||
var binaryValue = Common.String.Reverse(Convert.ToString(ret.Value, 2).PadLeft(32, '0'));
|
||||
var decodeValue = new Peripherals.JtagClient.JtagStatusReg(ret.Value);
|
||||
logger.Info($"User {User.Identity?.Name} successfully read device {address} Status Register: \n\t 0b{binaryValue} \n\t {decodeValue}");
|
||||
return TypedResults.Ok(new
|
||||
{
|
||||
original = ret.Value,
|
||||
binaryValue,
|
||||
decodeValue,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Error($"User {User.Identity?.Name} failed to read device {address} status register: {ret.Error}");
|
||||
return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ret.Error);
|
||||
return TypedResults.InternalServerError(ret.Error);
|
||||
logger.Error(ex, $"User {User.Identity?.Name} encountered exception while reading device {address} status register");
|
||||
return TypedResults.InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 上传比特流文件
|
||||
/// 上传比特流文件到服务器
|
||||
/// </summary>
|
||||
/// <param name="address"> 设备地址 </param>
|
||||
/// <param name="address">目标设备地址</param>
|
||||
/// <param name="file">比特流文件</param>
|
||||
/// <returns>上传结果</returns>
|
||||
[HttpPost("UploadBitstream")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async ValueTask<IResult> UploadBitstream(string address, IFormFile file)
|
||||
{
|
||||
logger.Info($"User {User.Identity?.Name} uploading bitstream for device {address}");
|
||||
|
||||
if (file == null || file.Length == 0)
|
||||
{
|
||||
logger.Warn($"User {User.Identity?.Name} attempted to upload empty file for device {address}");
|
||||
return TypedResults.BadRequest("未选择文件");
|
||||
|
||||
// 生成安全的文件名(避免路径遍历攻击)
|
||||
var fileName = Path.GetRandomFileName();
|
||||
var uploadsFolder = Path.Combine(Environment.CurrentDirectory, $"{BITSTREAM_PATH}/{address}");
|
||||
|
||||
// 如果存在文件,则删除原文件再上传
|
||||
if (Directory.Exists(uploadsFolder))
|
||||
{
|
||||
Directory.Delete(uploadsFolder, true);
|
||||
}
|
||||
Directory.CreateDirectory(uploadsFolder);
|
||||
|
||||
var filePath = Path.Combine(uploadsFolder, fileName);
|
||||
|
||||
using (var stream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
|
||||
logger.Info($"Device {address} Upload Bitstream Successfully");
|
||||
return TypedResults.Ok(true);
|
||||
try
|
||||
{
|
||||
// 生成安全的文件名(避免路径遍历攻击)
|
||||
var fileName = Path.GetRandomFileName();
|
||||
var uploadsFolder = Path.Combine(Environment.CurrentDirectory, $"{BITSTREAM_PATH}/{address}");
|
||||
|
||||
// 如果存在文件,则删除原文件再上传
|
||||
if (Directory.Exists(uploadsFolder))
|
||||
{
|
||||
Directory.Delete(uploadsFolder, true);
|
||||
logger.Info($"User {User.Identity?.Name} removed existing bitstream folder for device {address}");
|
||||
}
|
||||
Directory.CreateDirectory(uploadsFolder);
|
||||
|
||||
var filePath = Path.Combine(uploadsFolder, fileName);
|
||||
|
||||
using (var stream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
|
||||
logger.Info($"User {User.Identity?.Name} successfully uploaded bitstream for device {address}, file size: {file.Length} bytes");
|
||||
return TypedResults.Ok(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, $"User {User.Identity?.Name} failed to upload bitstream for device {address}");
|
||||
return TypedResults.InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过Jtag下载比特流文件
|
||||
/// 通过 JTAG 下载比特流文件到 FPGA 设备
|
||||
/// </summary>
|
||||
/// <param name="address"> 设备地址 </param>
|
||||
/// <param name="port"> 设备端口 </param>
|
||||
/// <param name="address">JTAG 设备地址</param>
|
||||
/// <param name="port">JTAG 设备端口</param>
|
||||
/// <returns>下载结果</returns>
|
||||
[HttpPost("DownloadBitstream")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async ValueTask<IResult> DownloadBitstream(string address, int port)
|
||||
{
|
||||
logger.Info($"User {User.Identity?.Name} initiating bitstream download to device {address}:{port}");
|
||||
|
||||
// 检查文件
|
||||
var fileDir = Path.Combine(Environment.CurrentDirectory, $"{BITSTREAM_PATH}/{address}");
|
||||
if (!Directory.Exists(fileDir))
|
||||
{
|
||||
logger.Warn($"User {User.Identity?.Name} attempted to download non-existent bitstream for device {address}");
|
||||
return TypedResults.BadRequest("Empty bitstream, Please upload it first");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 读取文件
|
||||
var filePath = Directory.GetFiles(fileDir)[0];
|
||||
logger.Info($"User {User.Identity?.Name} reading bitstream file: {filePath}");
|
||||
|
||||
using (var fileStream = System.IO.File.Open(filePath, System.IO.FileMode.Open))
|
||||
{
|
||||
if (fileStream is null || fileStream.Length <= 0)
|
||||
{
|
||||
logger.Warn($"User {User.Identity?.Name} found invalid bitstream file for device {address}");
|
||||
return TypedResults.BadRequest("Wrong bitstream, Please upload it again");
|
||||
}
|
||||
|
||||
logger.Info($"User {User.Identity?.Name} processing bitstream file of size: {fileStream.Length} bytes");
|
||||
|
||||
// 定义缓冲区大小: 32KB
|
||||
byte[] buffer = new byte[32 * 1024];
|
||||
@@ -158,7 +219,10 @@ public class JtagController : ControllerBase
|
||||
// 反转 32bits
|
||||
var retBuffer = Common.Number.ReverseBytes(buffer, 4);
|
||||
if (!retBuffer.IsSuccessful)
|
||||
{
|
||||
logger.Error($"User {User.Identity?.Name} failed to reverse bytes: {retBuffer.Error}");
|
||||
return TypedResults.InternalServerError(retBuffer.Error);
|
||||
}
|
||||
revBuffer = retBuffer.Value;
|
||||
|
||||
for (int i = 0; i < revBuffer.Length; i++)
|
||||
@@ -172,107 +236,148 @@ public class JtagController : ControllerBase
|
||||
|
||||
// 将所有数据转换为字节数组(注意:如果文件非常大,可能不适合完全加载到内存)
|
||||
var fileBytes = memoryStream.ToArray();
|
||||
logger.Info($"User {User.Identity?.Name} processed {totalBytesRead} bytes for device {address}");
|
||||
|
||||
// 下载比特流
|
||||
var jtagCtrl = new JtagClient.Jtag(address, port);
|
||||
var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.DownloadBitstream(fileBytes);
|
||||
|
||||
if (ret.IsSuccessful)
|
||||
{
|
||||
logger.Info($"Device {address} dowload bitstream successfully");
|
||||
logger.Info($"User {User.Identity?.Name} successfully downloaded bitstream to device {address}");
|
||||
return TypedResults.Ok(ret.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Error(ret.Error);
|
||||
logger.Error($"User {User.Identity?.Name} failed to download bitstream to device {address}: {ret.Error}");
|
||||
return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception error)
|
||||
catch (Exception ex)
|
||||
{
|
||||
return TypedResults.InternalServerError(error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
logger.Error(ex, $"User {User.Identity?.Name} encountered exception while downloading bitstream to device {address}");
|
||||
return TypedResults.InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// 执行边界扫描,获取所有端口状态
|
||||
/// </summary>
|
||||
/// <param name="address">[TODO:parameter]</param>
|
||||
/// <param name="port">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
/// <param name="address">JTAG 设备地址</param>
|
||||
/// <param name="port">JTAG 设备端口</param>
|
||||
/// <returns>边界扫描结果</returns>
|
||||
[HttpPost("BoundaryScanAllPorts")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async ValueTask<IResult> BoundaryScanAllPorts(string address, int port)
|
||||
{
|
||||
var jtagCtrl = new JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.BoundaryScan();
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
if (ret.Error is ArgumentException)
|
||||
return TypedResults.BadRequest(ret.Error);
|
||||
else return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
logger.Info($"User {User.Identity?.Name} initiating boundary scan for all ports on device {address}:{port}");
|
||||
|
||||
return TypedResults.Ok(ret.Value);
|
||||
try
|
||||
{
|
||||
var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.BoundaryScan();
|
||||
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"User {User.Identity?.Name} boundary scan failed for device {address}: {ret.Error}");
|
||||
if (ret.Error is ArgumentException)
|
||||
return TypedResults.BadRequest(ret.Error);
|
||||
else
|
||||
return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
|
||||
logger.Info($"User {User.Identity?.Name} successfully completed boundary scan for device {address}");
|
||||
return TypedResults.Ok(ret.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, $"User {User.Identity?.Name} encountered exception during boundary scan for device {address}");
|
||||
return TypedResults.InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// 执行逻辑端口边界扫描
|
||||
/// </summary>
|
||||
/// <param name="address">[TODO:parameter]</param>
|
||||
/// <param name="port">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
/// <param name="address">JTAG 设备地址</param>
|
||||
/// <param name="port">JTAG 设备端口</param>
|
||||
/// <returns>逻辑端口状态字典</returns>
|
||||
[HttpPost("BoundaryScanLogicalPorts")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(Dictionary<string, bool>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async ValueTask<IResult> BoundaryScanLogicalPorts(string address, int port)
|
||||
{
|
||||
var jtagCtrl = new JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.BoundaryScanLogicalPorts();
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
if (ret.Error is ArgumentException)
|
||||
return TypedResults.BadRequest(ret.Error);
|
||||
else return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
logger.Info($"User {User.Identity?.Name} initiating logical ports boundary scan on device {address}:{port}");
|
||||
|
||||
return TypedResults.Ok(ret.Value);
|
||||
try
|
||||
{
|
||||
var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.BoundaryScanLogicalPorts();
|
||||
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"User {User.Identity?.Name} logical ports boundary scan failed for device {address}: {ret.Error}");
|
||||
if (ret.Error is ArgumentException)
|
||||
return TypedResults.BadRequest(ret.Error);
|
||||
else
|
||||
return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
|
||||
logger.Info($"User {User.Identity?.Name} successfully completed logical ports boundary scan for device {address}, found {ret.Value?.Count} ports");
|
||||
return TypedResults.Ok(ret.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, $"User {User.Identity?.Name} encountered exception during logical ports boundary scan for device {address}");
|
||||
return TypedResults.InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// 设置 JTAG 时钟速度
|
||||
/// </summary>
|
||||
/// <param name="address">[TODO:parameter]</param>
|
||||
/// <param name="port">[TODO:parameter]</param>
|
||||
/// <param name="speed">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
/// <param name="address">JTAG 设备地址</param>
|
||||
/// <param name="port">JTAG 设备端口</param>
|
||||
/// <param name="speed">时钟速度 (Hz)</param>
|
||||
/// <returns>设置结果</returns>
|
||||
[HttpPost("SetSpeed")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async ValueTask<IResult> SetSpeed(string address, int port, UInt32 speed)
|
||||
{
|
||||
var jtagCtrl = new JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.SetSpeed(speed);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
if (ret.Error is ArgumentException)
|
||||
return TypedResults.BadRequest(ret.Error);
|
||||
else return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
logger.Info($"User {User.Identity?.Name} setting JTAG speed to {speed} Hz for device {address}:{port}");
|
||||
|
||||
return TypedResults.Ok(ret.Value);
|
||||
try
|
||||
{
|
||||
var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.SetSpeed(speed);
|
||||
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"User {User.Identity?.Name} failed to set speed for device {address}: {ret.Error}");
|
||||
if (ret.Error is ArgumentException)
|
||||
return TypedResults.BadRequest(ret.Error);
|
||||
else
|
||||
return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
|
||||
logger.Info($"User {User.Identity?.Name} successfully set JTAG speed to {speed} Hz for device {address}");
|
||||
return TypedResults.Ok(ret.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, $"User {User.Identity?.Name} encountered exception while setting speed for device {address}");
|
||||
return TypedResults.InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using DotNext;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -24,6 +25,7 @@ public class RemoteUpdateController : ControllerBase
|
||||
/// <param name="bitstream2">比特流文件2</param>
|
||||
/// <param name="bitstream3">比特流文件3</param>
|
||||
/// <returns>上传结果</returns>
|
||||
[Authorize("Admin")]
|
||||
[HttpPost("UploadBitstream")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
@@ -129,6 +131,7 @@ public class RemoteUpdateController : ControllerBase
|
||||
/// <param name="address"> 设备地址 </param>
|
||||
/// <param name="port"> 设备端口 </param>
|
||||
/// <param name="bitstreamNum"> 比特流位号 </param>
|
||||
[Authorize("Admin")]
|
||||
[HttpPost("DownloadBitstream")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
@@ -150,7 +153,7 @@ public class RemoteUpdateController : ControllerBase
|
||||
if (!fileBytes.IsSuccessful) return TypedResults.InternalServerError(fileBytes.Error);
|
||||
|
||||
// 下载比特流
|
||||
var remoteUpdater = new RemoteUpdateClient.RemoteUpdater(address, port);
|
||||
var remoteUpdater = new Peripherals.RemoteUpdateClient.RemoteUpdater(address, port);
|
||||
var ret = await remoteUpdater.UpdateBitstream(bitstreamNum, fileBytes.Value);
|
||||
|
||||
if (ret.IsSuccessful)
|
||||
@@ -179,6 +182,7 @@ public class RemoteUpdateController : ControllerBase
|
||||
/// <param name="port">设备端口</param>
|
||||
/// <param name="bitstreamNum">比特流编号</param>
|
||||
/// <returns>总共上传比特流的数量</returns>
|
||||
[Authorize("Admin")]
|
||||
[HttpPost("DownloadMultiBitstreams")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||||
@@ -210,7 +214,7 @@ public class RemoteUpdateController : ControllerBase
|
||||
}
|
||||
|
||||
// 下载比特流
|
||||
var remoteUpdater = new RemoteUpdateClient.RemoteUpdater(address, port);
|
||||
var remoteUpdater = new Peripherals.RemoteUpdateClient.RemoteUpdater(address, port);
|
||||
{
|
||||
var ret = await remoteUpdater.UploadBitstreams(bitstreams[0], bitstreams[1], bitstreams[2], bitstreams[3]);
|
||||
if (!ret.IsSuccessful) return TypedResults.InternalServerError(ret.Error);
|
||||
@@ -239,6 +243,7 @@ public class RemoteUpdateController : ControllerBase
|
||||
/// <param name="port">设备端口</param>
|
||||
/// <param name="bitstreamNum">比特流编号</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[Authorize("Admin")]
|
||||
[HttpPost("HotResetBitstream")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
@@ -246,7 +251,7 @@ public class RemoteUpdateController : ControllerBase
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
public async ValueTask<IResult> HotResetBitstream(string address, int port, int bitstreamNum)
|
||||
{
|
||||
var remoteUpdater = new RemoteUpdateClient.RemoteUpdater(address, port);
|
||||
var remoteUpdater = new Peripherals.RemoteUpdateClient.RemoteUpdater(address, port);
|
||||
var ret = await remoteUpdater.HotResetBitstream(bitstreamNum);
|
||||
|
||||
if (ret.IsSuccessful)
|
||||
@@ -267,6 +272,7 @@ public class RemoteUpdateController : ControllerBase
|
||||
/// <param name="address">[TODO:parameter]</param>
|
||||
/// <param name="port">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
[Authorize("Admin")]
|
||||
[HttpPost("GetFirmwareVersion")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(UInt32), StatusCodes.Status200OK)]
|
||||
@@ -274,7 +280,7 @@ public class RemoteUpdateController : ControllerBase
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
public async ValueTask<IResult> GetFirmwareVersion(string address, int port)
|
||||
{
|
||||
var remoteUpdater = new RemoteUpdateClient.RemoteUpdater(address, port);
|
||||
var remoteUpdater = new Peripherals.RemoteUpdateClient.RemoteUpdater(address, port);
|
||||
var ret = await remoteUpdater.GetVersion();
|
||||
|
||||
if (ret.IsSuccessful)
|
||||
|
||||
@@ -14,6 +14,11 @@ public class TutorialController : ControllerBase
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private readonly IWebHostEnvironment _environment;
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <param name="environment">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public TutorialController(IWebHostEnvironment environment)
|
||||
{
|
||||
_environment = environment;
|
||||
@@ -106,15 +106,16 @@ public class UDPController : ControllerBase
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定IP地址接受的数据列表
|
||||
/// 获取指定IP地址接收的数据列表
|
||||
/// </summary>
|
||||
/// <param name="address">IP地址</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
[HttpGet("GetRecvDataArray")]
|
||||
[ProducesResponseType(typeof(List<UDPData>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async ValueTask<IResult> GetRecvDataArray(string address)
|
||||
public async ValueTask<IResult> GetRecvDataArray(string address, int taskID)
|
||||
{
|
||||
var ret = await MsgBus.UDPServer.GetDataArrayAsync(address);
|
||||
var ret = await MsgBus.UDPServer.GetDataArrayAsync(address, taskID);
|
||||
|
||||
if (ret.HasValue)
|
||||
{
|
||||
|
||||
236
server/src/Controllers/VideoStreamController.cs
Normal file
236
server/src/Controllers/VideoStreamController.cs
Normal file
@@ -0,0 +1,236 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
/// <summary>
|
||||
/// 视频流控制器,支持动态配置摄像头连接
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class VideoStreamController : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private readonly server.Services.HttpVideoStreamService _videoStreamService;
|
||||
|
||||
/// <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>
|
||||
/// 初始化HTTP视频流控制器
|
||||
/// </summary>
|
||||
/// <param name="videoStreamService">HTTP视频流服务</param>
|
||||
public VideoStreamController(server.Services.HttpVideoStreamService videoStreamService)
|
||||
{
|
||||
logger.Info("创建VideoStreamController,命名空间:{Namespace}", this.GetType().Namespace);
|
||||
_videoStreamService = videoStreamService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 HTTP 视频流服务状态
|
||||
/// </summary>
|
||||
/// <returns>服务状态信息</returns>
|
||||
[HttpGet("Status")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
public IResult GetStatus()
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.Info("GetStatus方法被调用,控制器:{Controller},路径:api/VideoStream/Status", this.GetType().Name);
|
||||
|
||||
// 使用HttpVideoStreamService提供的状态信息
|
||||
var status = _videoStreamService.GetServiceStatus();
|
||||
|
||||
// 转换为小写首字母的JSON属性(符合前端惯例)
|
||||
return TypedResults.Ok(status);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取 HTTP 视频流服务状态失败");
|
||||
return TypedResults.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 HTTP 视频流信息
|
||||
/// </summary>
|
||||
/// <returns>流信息</returns>
|
||||
[HttpGet("StreamInfo")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
public IResult GetStreamInfo()
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.Info("获取 HTTP 视频流信息");
|
||||
return TypedResults.Ok(new
|
||||
{
|
||||
frameRate = _videoStreamService.FrameRate,
|
||||
frameWidth = _videoStreamService.FrameWidth,
|
||||
frameHeight = _videoStreamService.FrameHeight,
|
||||
format = "MJPEG",
|
||||
htmlUrl = $"http://localhost:{_videoStreamService.ServerPort}/video-feed.html",
|
||||
mjpegUrl = $"http://localhost:{_videoStreamService.ServerPort}/video-stream",
|
||||
snapshotUrl = $"http://localhost:{_videoStreamService.ServerPort}/snapshot",
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取 HTTP 视频流信息失败");
|
||||
return TypedResults.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置摄像头连接参数
|
||||
/// </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)
|
||||
{
|
||||
logger.Error(ex, "获取摄像头配置失败");
|
||||
return TypedResults.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// 测试 HTTP 视频流连接
|
||||
/// </summary>
|
||||
/// <returns>连接测试结果</returns>
|
||||
[HttpPost("TestConnection")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IResult> TestConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.Info("测试 HTTP 视频流连接");
|
||||
|
||||
// 尝试通过HTTP请求检查视频流服务是否可访问
|
||||
bool isConnected = false;
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
httpClient.Timeout = TimeSpan.FromSeconds(2); // 设置较短的超时时间
|
||||
var response = await httpClient.GetAsync($"http://localhost:{_videoStreamService.ServerPort}/");
|
||||
|
||||
// 只要能连接上就认为成功,不管返回状态
|
||||
isConnected = response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
logger.Info("测试摄像头连接");
|
||||
|
||||
var (isSuccess, message) = await _videoStreamService.TestCameraConnectionAsync();
|
||||
|
||||
return TypedResults.Ok(new
|
||||
{
|
||||
isConnected = isConnected,
|
||||
success = isSuccess,
|
||||
message = message,
|
||||
cameraAddress = _videoStreamService.CameraAddress,
|
||||
cameraPort = _videoStreamService.CameraPort,
|
||||
timestamp = DateTime.Now
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "HTTP 视频流连接测试失败");
|
||||
// 连接失败但不抛出异常,而是返回连接失败的结果
|
||||
return TypedResults.Ok(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using DotNext;
|
||||
using LinqToDB;
|
||||
using LinqToDB.Data;
|
||||
using LinqToDB.Mapping;
|
||||
@@ -20,6 +21,52 @@ public class User
|
||||
/// </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>
|
||||
/// 用户权限枚举
|
||||
/// </summary>
|
||||
public enum UserPermission
|
||||
{
|
||||
/// <summary>
|
||||
/// 管理员权限,可以管理用户和实验板
|
||||
/// </summary>
|
||||
Admin,
|
||||
|
||||
/// <summary>
|
||||
/// 普通用户权限,只能使用实验板
|
||||
/// </summary>
|
||||
Normal,
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -31,13 +78,65 @@ public class Board
|
||||
/// FPGA 板子的唯一标识符
|
||||
/// </summary>
|
||||
[PrimaryKey]
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
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 板子的通信端口
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public required int Port { get; set; }
|
||||
|
||||
/// <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>
|
||||
/// FPGA 板子状态枚举
|
||||
/// </summary>
|
||||
public enum BoardStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// 繁忙状态,正在被用户使用
|
||||
/// </summary>
|
||||
Busy,
|
||||
|
||||
/// <summary>
|
||||
/// 可用状态,可以被分配给用户
|
||||
/// </summary>
|
||||
Available,
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -45,14 +144,38 @@ public class Board
|
||||
/// </summary>
|
||||
public class AppDataConnection : DataConnection
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
static readonly string DATABASE_FILEPATH = $"{Environment.CurrentDirectory}/Database.sqlite";
|
||||
|
||||
static readonly LinqToDB.DataOptions options =
|
||||
new LinqToDB.DataOptions()
|
||||
.UseSQLite($"Data Source={Environment.CurrentDirectory}/Database.sqlite");
|
||||
new LinqToDB.DataOptions().UseSQLite($"Data Source={DATABASE_FILEPATH}");
|
||||
|
||||
/// <summary>
|
||||
/// 初始化应用程序数据连接
|
||||
/// </summary>
|
||||
public AppDataConnection() : base(options) { }
|
||||
public AppDataConnection() : base(options)
|
||||
{
|
||||
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.User.UserPermission.Admin,
|
||||
};
|
||||
this.Insert(user);
|
||||
logger.Info("默认管理员用户已创建");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Info($"数据库连接已建立: {DATABASE_FILEPATH}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -60,8 +183,10 @@ public class AppDataConnection : DataConnection
|
||||
/// </summary>
|
||||
public void CreateAllTables()
|
||||
{
|
||||
logger.Info("正在创建数据库表...");
|
||||
this.CreateTable<User>();
|
||||
this.CreateTable<Board>();
|
||||
logger.Info("数据库表创建完成");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -69,45 +194,361 @@ public class AppDataConnection : DataConnection
|
||||
/// </summary>
|
||||
public void DropAllTables()
|
||||
{
|
||||
logger.Warn("正在删除所有数据库表...");
|
||||
this.DropTable<User>();
|
||||
this.DropTable<Board>();
|
||||
logger.Warn("所有数据库表已删除");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加一个新的用户到数据库
|
||||
/// </summary>
|
||||
/// <param name="name">用户的名称</param>
|
||||
/// <param name="email">用户的电子邮箱地址</param>
|
||||
/// <param name="password">用户的密码</param>
|
||||
/// <returns>插入的记录数</returns>
|
||||
public int AddUser(string name)
|
||||
public int AddUser(string name, string email, string password)
|
||||
{
|
||||
var user = new User()
|
||||
{
|
||||
Name = name
|
||||
Name = name,
|
||||
EMail = email,
|
||||
Password = password,
|
||||
Permission = Database.User.UserPermission.Normal,
|
||||
};
|
||||
return this.Insert(user);
|
||||
var result = this.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 = this.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 = this.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 = this.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 = this.UserTable.Where(u => u.ID == userId).FirstOrDefault();
|
||||
if (user == null)
|
||||
{
|
||||
logger.Error($"未找到用户: {userId}");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 更新用户的板子绑定信息
|
||||
var userResult = this.UserTable
|
||||
.Where(u => u.ID == userId)
|
||||
.Set(u => u.BoardID, boardId)
|
||||
.Set(u => u.BoardExpireTime, expireTime)
|
||||
.Update();
|
||||
|
||||
// 更新板子的用户绑定信息
|
||||
var boardResult = this.BoardTable
|
||||
.Where(b => b.ID == boardId)
|
||||
.Set(b => b.Status, Board.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 = this.UserTable.Where(u => u.ID == userId).FirstOrDefault();
|
||||
Guid boardId = user?.BoardID ?? Guid.Empty;
|
||||
|
||||
// 清空用户的板子绑定信息
|
||||
var userResult = this.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 = this.BoardTable
|
||||
.Where(b => b.ID == boardId)
|
||||
.Set(b => b.Status, Board.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>
|
||||
/// 添加一块新的 FPGA 板子到数据库
|
||||
/// </summary>
|
||||
/// <param name="name">FPGA 板子的名称</param>
|
||||
/// <param name="ipAddr">FPGA 板子的IP地址</param>
|
||||
/// <param name="port">FPGA 板子的通信端口</param>
|
||||
/// <returns>插入的记录数</returns>
|
||||
public int AddBoard(string name)
|
||||
public int AddBoard(string name, string ipAddr, int port)
|
||||
{
|
||||
var board = new Board()
|
||||
{
|
||||
BoardName = name
|
||||
BoardName = name,
|
||||
IpAddr = ipAddr,
|
||||
Port = port,
|
||||
Status = Database.Board.BoardStatus.Available,
|
||||
};
|
||||
return this.Insert(board);
|
||||
var result = this.Insert(board);
|
||||
logger.Info($"新实验板已添加: {name} ({ipAddr}:{port})");
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据名称删除实验板
|
||||
/// </summary>
|
||||
/// <param name="name">实验板的名称</param>
|
||||
/// <returns>删除的记录数</returns>
|
||||
public int DeleteBoardByName(string name)
|
||||
{
|
||||
// 先获取要删除的板子信息
|
||||
var board = this.BoardTable.Where(b => b.BoardName == name).FirstOrDefault();
|
||||
if (board == null)
|
||||
{
|
||||
logger.Warn($"未找到名称为 {name} 的实验板");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 如果板子被占用,先解除绑定
|
||||
if (board.OccupiedUserID != Guid.Empty)
|
||||
{
|
||||
this.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 = this.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 = this.BoardTable.Where(b => b.ID == id).FirstOrDefault();
|
||||
if (board == null)
|
||||
{
|
||||
logger.Warn($"未找到ID为 {id} 的实验板");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 如果板子被占用,先解除绑定
|
||||
if (board.OccupiedUserID != Guid.Empty)
|
||||
{
|
||||
this.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 = this.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 = this.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>
|
||||
/// <returns>所有实验板的数组</returns>
|
||||
public Board[] GetAllBoard()
|
||||
{
|
||||
var boards = this.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 = this.BoardTable.Where(
|
||||
(board) => board.Status == Database.Board.BoardStatus.Available
|
||||
).ToArray();
|
||||
|
||||
if (boards.Length == 0)
|
||||
{
|
||||
logger.Warn("没有可用的实验板");
|
||||
return new(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
var board = boards[0];
|
||||
var user = this.UserTable.Where(u => u.ID == userId).FirstOrDefault();
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
logger.Error($"未找到用户: {userId}");
|
||||
return new(null);
|
||||
}
|
||||
|
||||
// 更新板子状态和用户绑定信息
|
||||
this.BoardTable
|
||||
.Where(target => target.ID == board.ID)
|
||||
.Set(target => target.Status, Board.BoardStatus.Busy)
|
||||
.Set(target => target.OccupiedUserID, userId)
|
||||
.Set(target => target.OccupiedUserName, user.Name)
|
||||
.Update();
|
||||
|
||||
// 更新用户的板子绑定信息
|
||||
this.UserTable
|
||||
.Where(u => u.ID == userId)
|
||||
.Set(u => u.BoardID, board.ID)
|
||||
.Set(u => u.BoardExpireTime, expireTime)
|
||||
.Update();
|
||||
|
||||
board.Status = Database.Board.BoardStatus.Busy;
|
||||
board.OccupiedUserID = userId;
|
||||
board.OccupiedUserName = user.Name;
|
||||
|
||||
logger.Info($"实验板 {board.BoardName} ({board.ID}) 已分配给用户 {user.Name} ({userId}),过期时间: {expireTime}");
|
||||
return new(board);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户表
|
||||
/// </summary>
|
||||
public ITable<User> User => this.GetTable<User>();
|
||||
public ITable<User> UserTable => this.GetTable<User>();
|
||||
|
||||
/// <summary>
|
||||
/// FPGA 板子表
|
||||
/// </summary>
|
||||
public ITable<Board> Board => this.GetTable<Board>();
|
||||
public ITable<Board> BoardTable => this.GetTable<Board>();
|
||||
}
|
||||
|
||||
839
server/src/Peripherals/CameraClient.cs
Normal file
839
server/src/Peripherals/CameraClient.cs
Normal file
@@ -0,0 +1,839 @@
|
||||
using System.Net;
|
||||
using DotNext;
|
||||
|
||||
namespace Peripherals.CameraClient;
|
||||
|
||||
static class CameraAddr
|
||||
{
|
||||
public const UInt32 BASE = 0x7000_0000;
|
||||
|
||||
public const UInt32 STORE_ADDR = BASE + 0x12;
|
||||
public const UInt32 STORE_NUM = BASE + 0x13;
|
||||
public const UInt32 EXPECTED_VH = BASE + 0x14;
|
||||
public const UInt32 CAPTURE_ON = BASE + 0x15;
|
||||
}
|
||||
|
||||
class Camera
|
||||
{
|
||||
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;
|
||||
|
||||
|
||||
const uint CAM_I2C_ADDR = 0x3C;
|
||||
const Peripherals.I2cClient.I2cProtocol CAM_PROTO = Peripherals.I2cClient.I2cProtocol.SCCB;
|
||||
const UInt16 H_START = 0; //default: 0
|
||||
const UInt16 V_START = 0; //default: 0
|
||||
const UInt16 DVPHO = 640; //default: 2592 (0xA20)
|
||||
const UInt16 DVPVO = 480; //default: 1944 (0x798)
|
||||
const UInt16 H_END = H_START + 1500 - 1; //default: 2624-1 (0xA3F)
|
||||
const UInt16 V_END = V_START + 1300 - 1; //default: 1951-1 (0x79F)
|
||||
const UInt16 HTS = 1700; //default: 2844 (0xB1C)
|
||||
const UInt16 VTS = 1500; //default: 1968 (0x7B0)
|
||||
const UInt16 H_OFFSET = 16; //default: 16 (0x10)
|
||||
const UInt16 V_OFFSET = 4; //default: 4 (0x04)
|
||||
const byte PLL_MUX = 10;
|
||||
const UInt32 FrameAddr = 0x00;
|
||||
const UInt32 FrameLength = DVPHO * DVPVO * 16 / 32;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 初始化摄像头客户端
|
||||
/// </summary>
|
||||
/// <param name="address">摄像头设备IP地址</param>
|
||||
/// <param name="port">摄像头设备端口</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
public Camera(string address, int port, int timeout = 2000)
|
||||
{
|
||||
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.timeout = timeout;
|
||||
}
|
||||
|
||||
public async ValueTask<Result<bool>> Init()
|
||||
{
|
||||
// 步骤1: 复位
|
||||
var resetResult = await Reset();
|
||||
if (!resetResult.IsSuccessful) return resetResult;
|
||||
|
||||
// 步骤2: 休眠
|
||||
var sleepResult = await Sleep();
|
||||
if (!sleepResult.IsSuccessful) return sleepResult;
|
||||
|
||||
// 步骤3: 配置基础寄存器
|
||||
var basicResult = await ConfigureBasicRegisters();
|
||||
if (!basicResult.IsSuccessful) return basicResult;
|
||||
|
||||
// 步骤4: 配置传感器控制
|
||||
var sensorResult = await ConfigureSensorControl();
|
||||
if (!sensorResult.IsSuccessful) return sensorResult;
|
||||
|
||||
// 步骤5: 配置模拟控制
|
||||
var analogResult = await ConfigureAnalogControl();
|
||||
if (!analogResult.IsSuccessful) return analogResult;
|
||||
|
||||
// 步骤6: 配置时钟控制
|
||||
var clockResult = await ConfigureClockControl();
|
||||
if (!clockResult.IsSuccessful) return clockResult;
|
||||
|
||||
// 步骤7: 配置PSRAM控制
|
||||
var psramResult = await ConfigurePSRAMControl();
|
||||
if (!psramResult.IsSuccessful) return psramResult;
|
||||
|
||||
// 步骤8: 配置DVP时序
|
||||
var dvpResult = await ConfigureDVPTiming();
|
||||
if (!dvpResult.IsSuccessful) return dvpResult;
|
||||
|
||||
// 步骤9: 配置基础控制
|
||||
var baseResult = await ConfigureBaseControl();
|
||||
if (!baseResult.IsSuccessful) return baseResult;
|
||||
|
||||
// 步骤10: 配置图像格式
|
||||
var formatResult = await ConfigureImageFormat();
|
||||
if (!formatResult.IsSuccessful) return formatResult;
|
||||
|
||||
// 步骤11: 配置ISP控制
|
||||
var ispResult = await ConfigureISPControl();
|
||||
if (!ispResult.IsSuccessful) return ispResult;
|
||||
|
||||
// 步骤12: 配置AEC
|
||||
var aecResult = await ConfigureAEC();
|
||||
if (!aecResult.IsSuccessful) return aecResult;
|
||||
|
||||
// 步骤13: 配置LENC
|
||||
var lencResult = await ConfigureLENC();
|
||||
if (!lencResult.IsSuccessful) return lencResult;
|
||||
|
||||
// 步骤14: 配置AWB
|
||||
var awbResult = await ConfigureAWB();
|
||||
if (!awbResult.IsSuccessful) return awbResult;
|
||||
|
||||
// 步骤15: 配置Gamma
|
||||
var gammaResult = await ConfigureGamma();
|
||||
if (!gammaResult.IsSuccessful) return gammaResult;
|
||||
|
||||
// 步骤16: 配置CMX
|
||||
var cmxResult = await ConfigureCMX();
|
||||
if (!cmxResult.IsSuccessful) return cmxResult;
|
||||
|
||||
// 步骤17: 配置SDE
|
||||
var sdeResult = await ConfigureSDE();
|
||||
if (!sdeResult.IsSuccessful) return sdeResult;
|
||||
|
||||
// 步骤18: 配置CIP
|
||||
var cipResult = await ConfigureCIP();
|
||||
if (!cipResult.IsSuccessful) return cipResult;
|
||||
|
||||
// 步骤19: 配置时序控制
|
||||
var timingResult = await ConfigureTimingControl();
|
||||
if (!timingResult.IsSuccessful) return timingResult;
|
||||
|
||||
// 步骤20: 配置测试和闪光灯
|
||||
var testResult = await ConfigureTestAndFlash();
|
||||
if (!testResult.IsSuccessful) return testResult;
|
||||
|
||||
// 步骤21: 配置分辨率(默认640x480)
|
||||
// var resolutionResult = await ConfigureResolution640x480();
|
||||
var resolutionResult = await ConfigureResolution1280x720();
|
||||
if (!resolutionResult.IsSuccessful) return resolutionResult;
|
||||
|
||||
// 步骤22: 开始流
|
||||
var startResult = await StartStreaming();
|
||||
if (!startResult.IsSuccessful) return startResult;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async ValueTask<Result<bool>> EnableCamera(bool isEnable)
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, CameraAddr.CAPTURE_ON, Convert.ToUInt32(isEnable));
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to write CAPTURE_ON to camera at {this.address}:{this.port}, error: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error($"CAPTURE_ON write returned false for camera at {this.address}:{this.port}");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取一帧图像数据
|
||||
/// </summary>
|
||||
/// <returns>包含图像数据的字节数组</returns>
|
||||
public async ValueTask<Result<byte[]>> ReadFrame()
|
||||
{
|
||||
// 只在第一次或出错时清除UDP缓冲区,避免每帧都清除造成延迟
|
||||
// await MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
logger.Trace($"Reading frame from camera {this.address}");
|
||||
|
||||
// 使用UDPClientPool读取图像帧数据
|
||||
var result = await UDPClientPool.ReadAddr4BytesAsync(
|
||||
this.ep,
|
||||
this.taskID, // taskID
|
||||
FrameAddr,
|
||||
// ((int)FrameLength),
|
||||
1280 * 720 / 2,
|
||||
this.timeout);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to read frame from camera {this.address}:{this.port}, error: {result.Error}");
|
||||
// 读取失败时清除缓冲区,为下次读取做准备
|
||||
try
|
||||
{
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Warn($"Failed to clear UDP data after read error: {ex.Message}");
|
||||
}
|
||||
return new(result.Error);
|
||||
}
|
||||
|
||||
logger.Trace($"Successfully read frame from camera {this.address}:{this.port}, data length: {result.Value.Length} bytes");
|
||||
return result.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量配置I2C寄存器
|
||||
/// </summary>
|
||||
/// <param name="registerTable">寄存器配置表,每个元素包含3个字节:[地址高位, 地址低位, 数据]</param>
|
||||
/// <param name="customDelayMs">自定义延时时间(毫秒),如果为null则使用默认延时逻辑</param>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureRegisters(byte[][] registerTable, int? customDelayMs = null)
|
||||
{
|
||||
var i2c = new Peripherals.I2cClient.I2c(this.address, this.port, this.taskID, this.timeout);
|
||||
|
||||
foreach (var cmd in registerTable)
|
||||
{
|
||||
if (cmd.Length != 3)
|
||||
{
|
||||
logger.Error($"Invalid register command length: {cmd.Length}, expected 3 bytes");
|
||||
return new(new ArgumentException($"Invalid register command length: {cmd.Length}, expected 3 bytes"));
|
||||
}
|
||||
|
||||
var ret = await i2c.WriteData(CAM_I2C_ADDR, cmd, CAM_PROTO);
|
||||
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"I2C write 0x{CAM_I2C_ADDR.ToString("X")} failed: {BitConverter.ToString(cmd)} error: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error($"I2C write 0x{CAM_I2C_ADDR.ToString("X")} returned false: {BitConverter.ToString(cmd)}");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 处理延时逻辑
|
||||
if (customDelayMs.HasValue)
|
||||
{
|
||||
await Task.Delay(customDelayMs.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 使用默认延时逻辑
|
||||
if (cmd[0] == 0x30 && cmd[1] == 0x08 && cmd[2] == 0x82)
|
||||
{
|
||||
// 复位命令,等待5MS
|
||||
await Task.Delay(5);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Task.Delay(3); // 其他命令延时3ms
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置摄像头分辨率和相关参数
|
||||
/// </summary>
|
||||
/// <param name="hStart">水平起始位置</param>
|
||||
/// <param name="vStart">垂直起始位置</param>
|
||||
/// <param name="dvpHo">输出水平像素数</param>
|
||||
/// <param name="dvpVo">输出垂直像素数</param>
|
||||
/// <param name="hts">水平总像素数</param>
|
||||
/// <param name="vts">垂直总像素数</param>
|
||||
/// <param name="hOffset">水平偏移</param>
|
||||
/// <param name="vOffset">垂直偏移</param>
|
||||
/// <param name="hWindow">水平窗口大小(默认1500)</param>
|
||||
/// <param name="vWindow">垂直窗口大小(默认1300)</param>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureResolution(
|
||||
UInt16 hStart, UInt16 vStart,
|
||||
UInt16 dvpHo, UInt16 dvpVo,
|
||||
UInt16 hts, UInt16 vts,
|
||||
UInt16 hOffset, UInt16 vOffset,
|
||||
UInt16 hWindow = 1500, UInt16 vWindow = 1300)
|
||||
{
|
||||
// 计算结束位置
|
||||
UInt16 hEnd = (UInt16)(hStart + hWindow - 1);
|
||||
UInt16 vEnd = (UInt16)(vStart + vWindow - 1);
|
||||
|
||||
// 计算帧长度
|
||||
UInt32 frameLength = (UInt32)(dvpHo * dvpVo * 16 / 32);
|
||||
|
||||
// 1. 配置UDP相关寄存器
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, CameraAddr.STORE_ADDR, FrameAddr);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to write STORE_ADDR: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("STORE_ADDR write returned false");
|
||||
return new(new Exception("STORE_ADDR write returned false"));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, CameraAddr.STORE_NUM, frameLength);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to write STORE_NUM: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("STORE_NUM write returned false");
|
||||
return new(new Exception("STORE_NUM write returned false"));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, CameraAddr.EXPECTED_VH, ((uint)dvpVo) << 16 | ((uint)dvpHo * 2));
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to write EXPECTED_VH: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("EXPECTED_VH write returned false");
|
||||
return new(new Exception("EXPECTED_VH write returned false"));
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 配置I2C寄存器
|
||||
var resolutionRegisters = new byte[][]
|
||||
{
|
||||
// H_OFFSET/V_OFFSET
|
||||
new byte[] { 0x38, 0x10, unchecked((byte)((hOffset >> 8) & 0xFF)) },
|
||||
new byte[] { 0x38, 0x11, unchecked((byte)(hOffset & 0xFF)) },
|
||||
new byte[] { 0x38, 0x12, unchecked((byte)((vOffset >> 8) & 0xFF)) },
|
||||
|
||||
// H_START/V_START
|
||||
new byte[] { 0x38, 0x00, unchecked((byte)((hStart >> 8) & 0xFF)) },
|
||||
new byte[] { 0x38, 0x01, unchecked((byte)(hStart & 0xFF)) },
|
||||
new byte[] { 0x38, 0x02, unchecked((byte)((vStart >> 8) & 0xFF)) },
|
||||
new byte[] { 0x38, 0x03, unchecked((byte)(vStart & 0xFF)) },
|
||||
|
||||
// H_END/V_END
|
||||
new byte[] { 0x38, 0x04, unchecked((byte)((hEnd >> 8) & 0xFF)) },
|
||||
new byte[] { 0x38, 0x05, unchecked((byte)(hEnd & 0xFF)) },
|
||||
new byte[] { 0x38, 0x06, unchecked((byte)((vEnd >> 8) & 0xFF)) },
|
||||
new byte[] { 0x38, 0x07, unchecked((byte)(vEnd & 0xFF)) },
|
||||
|
||||
// 输出像素个数
|
||||
new byte[] { 0x38, 0x08, unchecked((byte)((dvpHo >> 8) & 0xFF)) },
|
||||
new byte[] { 0x38, 0x09, unchecked((byte)(dvpHo & 0xFF)) },
|
||||
new byte[] { 0x38, 0x0A, unchecked((byte)((dvpVo >> 8) & 0xFF)) },
|
||||
new byte[] { 0x38, 0x0B, unchecked((byte)(dvpVo & 0xFF)) },
|
||||
|
||||
// 总像素
|
||||
new byte[] { 0x38, 0x0C, unchecked((byte)((hts >> 8) & 0xFF)) },
|
||||
new byte[] { 0x38, 0x0D, unchecked((byte)(hts & 0xFF)) },
|
||||
new byte[] { 0x38, 0x0E, unchecked((byte)((vts >> 8) & 0xFF)) },
|
||||
new byte[] { 0x38, 0x0F, unchecked((byte)(vts & 0xFF)) },
|
||||
|
||||
// Timing Voffset
|
||||
new byte[] { 0x38, 0x13, unchecked((byte)(vOffset & 0xFF)) }
|
||||
};
|
||||
|
||||
var configResult = await ConfigureRegisters(resolutionRegisters, customDelayMs: 1);
|
||||
if (!configResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to configure resolution registers: {configResult.Error}");
|
||||
return configResult;
|
||||
}
|
||||
|
||||
logger.Info($"Successfully configured resolution: {dvpHo}x{dvpVo}, HTS={hts}, VTS={vts}");
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置为640x480分辨率(默认配置)
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureResolution640x480()
|
||||
{
|
||||
return await ConfigureResolution(
|
||||
hStart: 0, vStart: 0,
|
||||
dvpHo: 640, dvpVo: 480,
|
||||
hts: 1700, vts: 1500,
|
||||
hOffset: 16, vOffset: 4
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置为320x240分辨率
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureResolution320x240()
|
||||
{
|
||||
return await ConfigureResolution(
|
||||
hStart: 0, vStart: 0,
|
||||
dvpHo: 320, dvpVo: 240,
|
||||
hts: 850, vts: 750,
|
||||
hOffset: 16, vOffset: 4,
|
||||
hWindow: 750, vWindow: 650
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置为1280x720分辨率
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureResolution1280x720()
|
||||
{
|
||||
return await ConfigureResolution(
|
||||
hStart: 0, vStart: 0,
|
||||
dvpHo: 1280, dvpVo: 720,
|
||||
hts: 2844, vts: 1968,
|
||||
hOffset: 16, vOffset: 4,
|
||||
hWindow: 2592, vWindow: 1944
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 复位摄像头
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> Reset()
|
||||
{
|
||||
var resetRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x30, 0x08, 0x82 } // 复位命令
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(resetRegisters, customDelayMs: 5); // 复位后等待5ms
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置摄像头为休眠模式
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> Sleep()
|
||||
{
|
||||
var sleepRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x30, 0x08, 0x42 } // 休眠命令
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(sleepRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置基础寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureBasicRegisters()
|
||||
{
|
||||
var basicRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x31, 0x03, 0x02 },
|
||||
new byte[] { 0x30, 0x17, 0xff },
|
||||
new byte[] { 0x30, 0x18, 0xff },
|
||||
new byte[] { 0x30, 0x37, 0x13 },
|
||||
new byte[] { 0x31, 0x08, 0x01 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(basicRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置传感器控制寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureSensorControl()
|
||||
{
|
||||
var sensorRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x36, 0x30, 0x36 },
|
||||
new byte[] { 0x36, 0x31, 0x0e },
|
||||
new byte[] { 0x36, 0x32, 0xe2 },
|
||||
new byte[] { 0x36, 0x33, 0x12 },
|
||||
new byte[] { 0x36, 0x21, 0xe0 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(sensorRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置模拟控制寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureAnalogControl()
|
||||
{
|
||||
var analogRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x37, 0x04, 0xa0 },
|
||||
new byte[] { 0x37, 0x03, 0x5a },
|
||||
new byte[] { 0x37, 0x15, 0x78 },
|
||||
new byte[] { 0x37, 0x17, 0x01 },
|
||||
new byte[] { 0x37, 0x0b, 0x60 },
|
||||
new byte[] { 0x37, 0x05, 0x1a }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(analogRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置时钟控制寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureClockControl()
|
||||
{
|
||||
var clockRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x39, 0x05, 0x02 },
|
||||
new byte[] { 0x39, 0x06, 0x10 },
|
||||
new byte[] { 0x39, 0x01, 0x0a },
|
||||
new byte[] { 0x30, 0x35, 0x11 }, // 30fps
|
||||
new byte[] { 0x30, 0x36, PLL_MUX } // PLL倍频
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(clockRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置PSRAM控制寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigurePSRAMControl()
|
||||
{
|
||||
var psramRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x37, 0x31, 0x12 },
|
||||
new byte[] { 0x36, 0x00, 0x08 },
|
||||
new byte[] { 0x36, 0x01, 0x33 },
|
||||
new byte[] { 0x30, 0x2d, 0x60 },
|
||||
new byte[] { 0x36, 0x20, 0x52 },
|
||||
new byte[] { 0x37, 0x1b, 0x20 },
|
||||
new byte[] { 0x47, 0x1c, 0x50 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(psramRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置DVP时序寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureDVPTiming()
|
||||
{
|
||||
var dvpRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x3a, 0x13, 0x43 },
|
||||
new byte[] { 0x3a, 0x18, 0x00 },
|
||||
new byte[] { 0x3a, 0x19, 0xf8 },
|
||||
new byte[] { 0x36, 0x35, 0x13 },
|
||||
new byte[] { 0x36, 0x36, 0x03 },
|
||||
new byte[] { 0x36, 0x34, 0x40 },
|
||||
new byte[] { 0x36, 0x22, 0x01 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(dvpRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置基础控制寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureBaseControl()
|
||||
{
|
||||
var baseRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x3c, 0x01, 0x34 },
|
||||
new byte[] { 0x3c, 0x04, 0x28 },
|
||||
new byte[] { 0x3c, 0x05, 0x98 },
|
||||
new byte[] { 0x3c, 0x06, 0x00 },
|
||||
new byte[] { 0x3c, 0x07, 0x08 },
|
||||
new byte[] { 0x3c, 0x08, 0x00 },
|
||||
new byte[] { 0x3c, 0x09, 0x1c },
|
||||
new byte[] { 0x3c, 0x0a, 0x9c },
|
||||
new byte[] { 0x3c, 0x0b, 0x40 },
|
||||
new byte[] { 0x37, 0x08, 0x64 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(baseRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置图像格式寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureImageFormat()
|
||||
{
|
||||
var formatRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x40, 0x01, 0x02 },
|
||||
new byte[] { 0x40, 0x05, 0x1a },
|
||||
new byte[] { 0x30, 0x00, 0x00 },
|
||||
new byte[] { 0x30, 0x04, 0xff },
|
||||
new byte[] { 0x43, 0x00, 0x6F }, // RGB565:first byte:{g[2:0],b[4:0]],second byte:{r[4:0],g[5:3]}
|
||||
new byte[] { 0x50, 0x1f, 0x01 }, // Format: ISP RGB
|
||||
new byte[] { 0x44, 0x0e, 0x00 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(formatRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置ISP控制寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureISPControl()
|
||||
{
|
||||
var ispRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x50, 0x00, 0xA7 }, // ISP控制
|
||||
new byte[] { 0x50, 0x01, 0xA3 } // ISP控制
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(ispRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置AEC(自动曝光控制)寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureAEC()
|
||||
{
|
||||
var aecRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x3a, 0x0f, 0x30 }, // AEC控制;stable range in high
|
||||
new byte[] { 0x3a, 0x10, 0x28 }, // AEC控制;stable range in low
|
||||
new byte[] { 0x3a, 0x1b, 0x30 }, // AEC控制;stable range out high
|
||||
new byte[] { 0x3a, 0x1e, 0x26 }, // AEC控制;stable range out low
|
||||
new byte[] { 0x3a, 0x11, 0x60 }, // AEC控制; fast zone high
|
||||
new byte[] { 0x3a, 0x1f, 0x14 }, // AEC控制; fast zone low
|
||||
new byte[] { 0x3a, 0x02, 0x17 }, // 60Hz max exposure
|
||||
new byte[] { 0x3a, 0x03, 0x10 }, // 60Hz max exposure
|
||||
new byte[] { 0x3a, 0x14, 0x17 }, // 50Hz max exposure
|
||||
new byte[] { 0x3a, 0x15, 0x10 }, // 50Hz max exposure
|
||||
new byte[] { 0x3b, 0x07, 0x0a } // 帧曝光模式
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(aecRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置LENC(镜头校正)寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureLENC()
|
||||
{
|
||||
var lencRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x58, 0x00, 0x23 }, new byte[] { 0x58, 0x01, 0x14 }, new byte[] { 0x58, 0x02, 0x0f },
|
||||
new byte[] { 0x58, 0x03, 0x0f }, new byte[] { 0x58, 0x04, 0x12 }, new byte[] { 0x58, 0x05, 0x26 },
|
||||
new byte[] { 0x58, 0x06, 0x0c }, new byte[] { 0x58, 0x07, 0x08 }, new byte[] { 0x58, 0x08, 0x05 },
|
||||
new byte[] { 0x58, 0x09, 0x05 }, new byte[] { 0x58, 0x0a, 0x08 }, new byte[] { 0x58, 0x0b, 0x0d },
|
||||
new byte[] { 0x58, 0x0c, 0x08 }, new byte[] { 0x58, 0x0d, 0x03 }, new byte[] { 0x58, 0x0e, 0x00 },
|
||||
new byte[] { 0x58, 0x0f, 0x00 }, new byte[] { 0x58, 0x10, 0x03 }, new byte[] { 0x58, 0x11, 0x09 },
|
||||
new byte[] { 0x58, 0x12, 0x07 }, new byte[] { 0x58, 0x13, 0x03 }, new byte[] { 0x58, 0x14, 0x00 },
|
||||
new byte[] { 0x58, 0x15, 0x01 }, new byte[] { 0x58, 0x16, 0x03 }, new byte[] { 0x58, 0x17, 0x08 },
|
||||
new byte[] { 0x58, 0x18, 0x0d }, new byte[] { 0x58, 0x19, 0x08 }, new byte[] { 0x58, 0x1a, 0x05 },
|
||||
new byte[] { 0x58, 0x1b, 0x06 }, new byte[] { 0x58, 0x1c, 0x08 }, new byte[] { 0x58, 0x1d, 0x0e },
|
||||
new byte[] { 0x58, 0x1e, 0x29 }, new byte[] { 0x58, 0x1f, 0x17 }, new byte[] { 0x58, 0x20, 0x11 },
|
||||
new byte[] { 0x58, 0x21, 0x11 }, new byte[] { 0x58, 0x22, 0x15 }, new byte[] { 0x58, 0x23, 0x28 },
|
||||
new byte[] { 0x58, 0x24, 0x46 }, new byte[] { 0x58, 0x25, 0x26 }, new byte[] { 0x58, 0x26, 0x08 },
|
||||
new byte[] { 0x58, 0x27, 0x26 }, new byte[] { 0x58, 0x28, 0x64 }, new byte[] { 0x58, 0x29, 0x26 },
|
||||
new byte[] { 0x58, 0x2a, 0x24 }, new byte[] { 0x58, 0x2b, 0x22 }, new byte[] { 0x58, 0x2c, 0x24 },
|
||||
new byte[] { 0x58, 0x2d, 0x24 }, new byte[] { 0x58, 0x2e, 0x06 }, new byte[] { 0x58, 0x2f, 0x22 },
|
||||
new byte[] { 0x58, 0x30, 0x40 }, new byte[] { 0x58, 0x31, 0x42 }, new byte[] { 0x58, 0x32, 0x24 },
|
||||
new byte[] { 0x58, 0x33, 0x26 }, new byte[] { 0x58, 0x34, 0x24 }, new byte[] { 0x58, 0x35, 0x22 },
|
||||
new byte[] { 0x58, 0x36, 0x22 }, new byte[] { 0x58, 0x37, 0x26 }, new byte[] { 0x58, 0x38, 0x44 },
|
||||
new byte[] { 0x58, 0x39, 0x24 }, new byte[] { 0x58, 0x3a, 0x26 }, new byte[] { 0x58, 0x3b, 0x28 },
|
||||
new byte[] { 0x58, 0x3c, 0x42 }, new byte[] { 0x58, 0x3d, 0xce }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(lencRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置摄像头AWB(自动白平衡)寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureAWB()
|
||||
{
|
||||
var awbRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x51, 0x80, 0xff }, new byte[] { 0x51, 0x81, 0xf2 }, new byte[] { 0x51, 0x82, 0x00 },
|
||||
new byte[] { 0x51, 0x83, 0x14 }, new byte[] { 0x51, 0x84, 0x25 }, new byte[] { 0x51, 0x85, 0x24 },
|
||||
new byte[] { 0x51, 0x86, 0x09 }, new byte[] { 0x51, 0x87, 0x09 }, new byte[] { 0x51, 0x88, 0x09 },
|
||||
new byte[] { 0x51, 0x89, 0x75 }, new byte[] { 0x51, 0x8a, 0x54 }, new byte[] { 0x51, 0x8b, 0xe0 },
|
||||
new byte[] { 0x51, 0x8c, 0xb2 }, new byte[] { 0x51, 0x8d, 0x42 }, new byte[] { 0x51, 0x8e, 0x3d },
|
||||
new byte[] { 0x51, 0x8f, 0x56 }, new byte[] { 0x51, 0x90, 0x46 }, new byte[] { 0x51, 0x91, 0xf8 },
|
||||
new byte[] { 0x51, 0x92, 0x04 }, new byte[] { 0x51, 0x93, 0x70 }, new byte[] { 0x51, 0x94, 0xf0 },
|
||||
new byte[] { 0x51, 0x95, 0xf0 }, new byte[] { 0x51, 0x96, 0x03 }, new byte[] { 0x51, 0x97, 0x01 },
|
||||
new byte[] { 0x51, 0x98, 0x04 }, new byte[] { 0x51, 0x99, 0x12 }, new byte[] { 0x51, 0x9a, 0x04 },
|
||||
new byte[] { 0x51, 0x9b, 0x00 }, new byte[] { 0x51, 0x9c, 0x06 }, new byte[] { 0x51, 0x9d, 0x82 },
|
||||
new byte[] { 0x51, 0x9e, 0x38 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(awbRegisters, customDelayMs: 2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置摄像头Gamma校正寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureGamma()
|
||||
{
|
||||
var gammaRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x54, 0x80, 0x01 }, new byte[] { 0x54, 0x81, 0x08 }, new byte[] { 0x54, 0x82, 0x14 },
|
||||
new byte[] { 0x54, 0x83, 0x28 }, new byte[] { 0x54, 0x84, 0x51 }, new byte[] { 0x54, 0x85, 0x65 },
|
||||
new byte[] { 0x54, 0x86, 0x71 }, new byte[] { 0x54, 0x87, 0x7d }, new byte[] { 0x54, 0x88, 0x87 },
|
||||
new byte[] { 0x54, 0x89, 0x91 }, new byte[] { 0x54, 0x8a, 0x9a }, new byte[] { 0x54, 0x8b, 0xaa },
|
||||
new byte[] { 0x54, 0x8c, 0xb8 }, new byte[] { 0x54, 0x8d, 0xcd }, new byte[] { 0x54, 0x8e, 0xdd },
|
||||
new byte[] { 0x54, 0x8f, 0xea }, new byte[] { 0x54, 0x90, 0x1d }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(gammaRegisters);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 配置CMX(色彩矩阵)寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureCMX()
|
||||
{
|
||||
var cmxRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x53, 0x81, 0x1e }, new byte[] { 0x53, 0x82, 0x5b }, new byte[] { 0x53, 0x83, 0x08 },
|
||||
new byte[] { 0x53, 0x84, 0x0a }, new byte[] { 0x53, 0x85, 0x7e }, new byte[] { 0x53, 0x86, 0x88 },
|
||||
new byte[] { 0x53, 0x87, 0x7c }, new byte[] { 0x53, 0x88, 0x6c }, new byte[] { 0x53, 0x89, 0x10 },
|
||||
new byte[] { 0x53, 0x8a, 0x01 }, new byte[] { 0x53, 0x8b, 0x98 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(cmxRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置SDE(特殊数字效果)寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureSDE()
|
||||
{
|
||||
var sdeRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x55, 0x80, 0x06 }, new byte[] { 0x55, 0x83, 0x40 }, new byte[] { 0x55, 0x84, 0x10 },
|
||||
new byte[] { 0x55, 0x89, 0x10 }, new byte[] { 0x55, 0x8a, 0x00 }, new byte[] { 0x55, 0x8b, 0xf8 },
|
||||
new byte[] { 0x50, 0x1d, 0x40 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(sdeRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置CIP(颜色插值处理)寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureCIP()
|
||||
{
|
||||
var cipRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x53, 0x00, 0x08 }, new byte[] { 0x53, 0x01, 0x30 }, new byte[] { 0x53, 0x02, 0x10 },
|
||||
new byte[] { 0x53, 0x03, 0x00 }, new byte[] { 0x53, 0x04, 0x08 }, new byte[] { 0x53, 0x05, 0x30 },
|
||||
new byte[] { 0x53, 0x06, 0x08 }, new byte[] { 0x53, 0x07, 0x16 }, new byte[] { 0x53, 0x09, 0x08 },
|
||||
new byte[] { 0x53, 0x0a, 0x30 }, new byte[] { 0x53, 0x0b, 0x04 }, new byte[] { 0x53, 0x0c, 0x06 },
|
||||
new byte[] { 0x50, 0x25, 0x00 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(cipRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置时序控制寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureTimingControl()
|
||||
{
|
||||
var timingRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x38, 0x20, 0x46 }, // vflip
|
||||
new byte[] { 0x38, 0x21, 0x01 }, // mirror
|
||||
new byte[] { 0x38, 0x14, 0x31 }, // timing X inc
|
||||
new byte[] { 0x38, 0x15, 0x31 }, // timing Y inc
|
||||
new byte[] { 0x36, 0x18, 0x00 },
|
||||
new byte[] { 0x36, 0x12, 0x29 },
|
||||
new byte[] { 0x37, 0x09, 0x52 },
|
||||
new byte[] { 0x37, 0x0c, 0x03 }
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(timingRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置测试模式和闪光灯寄存器
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureTestAndFlash()
|
||||
{
|
||||
var testRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x40, 0x04, 0x02 }, // BLC(背光) 2 lines
|
||||
new byte[] { 0x47, 0x13, 0x03 }, // JPEG mode 3
|
||||
new byte[] { 0x44, 0x07, 0x04 }, // 量化标度
|
||||
new byte[] { 0x46, 0x0c, 0x20 },
|
||||
new byte[] { 0x48, 0x37, 0x22 }, // DVP CLK divider
|
||||
new byte[] { 0x38, 0x24, 0x02 }, // DVP CLK divider
|
||||
// 彩条测试禁用
|
||||
new byte[] { 0x50, 0x3d, 0x00 },
|
||||
new byte[] { 0x47, 0x41, 0x00 },
|
||||
// 闪光灯配置
|
||||
new byte[] { 0x30, 0x16, 0x02 },
|
||||
new byte[] { 0x30, 0x1c, 0x02 },
|
||||
new byte[] { 0x30, 0x19, 0x02 }, // 开启闪光灯
|
||||
new byte[] { 0x30, 0x19, 0x00 } // 关闭闪光灯
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(testRegisters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始流媒体传输
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> StartStreaming()
|
||||
{
|
||||
var startRegisters = new byte[][]
|
||||
{
|
||||
new byte[] { 0x30, 0x08, 0x02 } // 开始流
|
||||
};
|
||||
|
||||
return await ConfigureRegisters(startRegisters);
|
||||
}
|
||||
}
|
||||
@@ -108,11 +108,11 @@ public class DDS
|
||||
if (waveNum < 0 || waveNum > 3) return new(new ArgumentException(
|
||||
$"Wave number should be 0 ~ 3 instead of {waveNum}", nameof(waveNum)));
|
||||
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||
logger.Trace("Clear udp data finished");
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, DDSAddr.Channel[channelNum].WaveSelect, (UInt32)waveNum, this.timeout);
|
||||
this.ep, 1, DDSAddr.Channel[channelNum].WaveSelect, (UInt32)waveNum, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
return new(ret.Error);
|
||||
return ret.Value;
|
||||
@@ -132,11 +132,11 @@ public class DDS
|
||||
if (waveNum < 0 || waveNum > 3) return new(new ArgumentException(
|
||||
$"Wave number should be 0 ~ 3 instead of {waveNum}", nameof(waveNum)));
|
||||
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||
logger.Trace("Clear udp data finished");
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, DDSAddr.Channel[channelNum].FreqCtrl[waveNum], step, this.timeout);
|
||||
this.ep, 1, DDSAddr.Channel[channelNum].FreqCtrl[waveNum], step, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
return new(ret.Error);
|
||||
return ret.Value;
|
||||
@@ -158,11 +158,11 @@ public class DDS
|
||||
if (phase < 0 || phase > 4096) return new(new ArgumentException(
|
||||
$"Phase should be 0 ~ 4096 instead of {phase}", nameof(phase)));
|
||||
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||
logger.Trace("Clear udp data finished");
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, DDSAddr.Channel[channelNum].PhaseCtrl[waveNum], (UInt32)phase, this.timeout);
|
||||
this.ep, 1, DDSAddr.Channel[channelNum].PhaseCtrl[waveNum], (UInt32)phase, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
return new(ret.Error);
|
||||
return ret.Value;
|
||||
|
||||
281
server/src/Peripherals/I2cClient.cs
Normal file
281
server/src/Peripherals/I2cClient.cs
Normal file
@@ -0,0 +1,281 @@
|
||||
using System.Net;
|
||||
using DotNext;
|
||||
|
||||
namespace Peripherals.I2cClient;
|
||||
|
||||
static class I2cAddr
|
||||
{
|
||||
|
||||
const UInt32 Base = 0x6000_0000;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0000:
|
||||
/// [7:0] 本次传输的i2c地址(最高位总为0);
|
||||
/// [8] 1为读,0为写;
|
||||
/// [16] 1为SCCB协议,0为I2C协议;
|
||||
/// [24] 1为开启本次传输,自动置零
|
||||
/// </summary>
|
||||
public const UInt32 BaseConfig = Base + 0x0000_0000;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0001:
|
||||
/// [15:0] 本次传输的数据量(以字节为单位,0为传1个字节);
|
||||
/// [31:16] 若本次传输为读的DUMMY数据量(字节为单位,0为传1个字节)
|
||||
/// </summary>
|
||||
public const UInt32 TranConfig = Base + 0x0000_0001;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0002: [0] cmd_done; [8] cmd_error;
|
||||
/// </summary>
|
||||
public const UInt32 Flag = Base + 0x0000_0002;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0003: FIFO写入口,仅低8位有效,只写
|
||||
/// </summary>
|
||||
public const UInt32 Write = Base + 0x0000_0003;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0004: FIFO读出口,仅低8位有效,只读
|
||||
/// </summary>
|
||||
public const UInt32 Read = Base + 0x0000_0003;
|
||||
|
||||
/// <summary>
|
||||
/// 0x0000_0005: [0] FIFO写入口清空;[8] FIFO读出口清空;
|
||||
/// </summary>
|
||||
public const UInt32 Clear = Base + 0x0000_0003;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:Enum]
|
||||
/// </summary>
|
||||
public enum I2cProtocol
|
||||
{
|
||||
/// <summary>
|
||||
/// [TODO:Enum]
|
||||
/// </summary>
|
||||
I2c = 0,
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:Enum]
|
||||
/// </summary>
|
||||
SCCB = 1
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
public class I2c
|
||||
{
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <param name="address">[TODO:parameter]</param>
|
||||
/// <param name="port">[TODO:parameter]</param>
|
||||
/// <param name="taskID">[TODO:parameter]</param>
|
||||
/// <param name="timeout">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public I2c(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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向指定I2C设备写入数据
|
||||
/// </summary>
|
||||
/// <param name="devAddr">I2C设备地址</param>
|
||||
/// <param name="data">要写入的数据</param>
|
||||
/// <param name="proto">I2C协议类型</param>
|
||||
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<bool>> WriteData(UInt32 devAddr, byte[] data, I2cProtocol proto)
|
||||
{
|
||||
if (data.Length > 0x0000_FFFF)
|
||||
{
|
||||
logger.Error($"Data length {data.Length} exceeds maximum allowed 0x0000_FFFF");
|
||||
return new(new ArgumentException($"Data length {data.Length} exceeds maximum allowed 0x0000_FFFF"));
|
||||
}
|
||||
|
||||
// 清除UDP服务器接收缓冲区
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
logger.Trace($"Clear up udp server {this.address} receive data");
|
||||
|
||||
// 写入数据到I2C FIFO写入口
|
||||
{
|
||||
var i2cData = new byte[data.Length * 4];
|
||||
int i = 0;
|
||||
foreach (var item in data)
|
||||
{
|
||||
i2cData[i++] = 0x00;
|
||||
i2cData[i++] = 0x00;
|
||||
i2cData[i++] = 0x00;
|
||||
i2cData[i++] = item;
|
||||
}
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, I2cAddr.Write, i2cData);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to write data to I2C FIFO: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to I2C FIFO returned false");
|
||||
return new(new Exception("Failed to write data to I2C FIFO"));
|
||||
}
|
||||
}
|
||||
|
||||
// 配置本次传输数据量
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, I2cAddr.TranConfig, ((uint)(data.Length - 1)));
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to configure transfer length: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to TranConfig returned false");
|
||||
return new(new Exception("Failed to configure transfer length"));
|
||||
}
|
||||
}
|
||||
|
||||
// 配置I2C地址、协议及启动传输
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, this.taskID, I2cAddr.BaseConfig, (devAddr) | (((uint)proto) << 16) | (1 << 24));
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to configure I2C address/protocol/start: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to BaseConfig returned false");
|
||||
return new(new Exception("Failed to configure I2C address/protocol/start"));
|
||||
}
|
||||
}
|
||||
|
||||
// 等待I2C命令完成
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, this.taskID, I2cAddr.Flag, 0x0000_0001, 0xFFFF_FFFF);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to wait for I2C command completion: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("ReadAddrWithWait for I2C command completion returned false");
|
||||
return new(new Exception("I2C command did not complete successfully"));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从指定I2C设备读取数据
|
||||
/// </summary>
|
||||
/// <param name="devAddr">I2C设备地址</param>
|
||||
/// <param name="length">要读取的数据长度</param>
|
||||
/// <param name="proto">I2C协议类型</param>
|
||||
/// <returns>操作结果,成功返回读取到的数据,否则返回异常信息</returns>
|
||||
public async ValueTask<Result<byte[]>> ReadData(UInt32 devAddr, int length, I2cProtocol proto)
|
||||
{
|
||||
if (length <= 0 || length > 0x0000_FFFF)
|
||||
{
|
||||
logger.Error($"Read length {length} is invalid or exceeds maximum allowed 0x0000_FFFF");
|
||||
return new(new ArgumentException($"Read length {length} is invalid or exceeds maximum allowed 0x0000_FFFF"));
|
||||
}
|
||||
|
||||
// 清除UDP服务器接收缓冲区
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
logger.Trace($"Clear up udp server {this.address} receive data");
|
||||
|
||||
// 配置本次传输数据量
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, I2cAddr.TranConfig, ((uint)(length - 1)));
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to configure transfer length: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to TranConfig returned false");
|
||||
return new(new Exception("Failed to configure transfer length"));
|
||||
}
|
||||
}
|
||||
|
||||
// 配置I2C地址、协议及启动传输(读操作)
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, this.taskID, I2cAddr.BaseConfig, (devAddr) | (1 << 8) | (((uint)proto) << 16) | (1 << 24));
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to configure I2C address/protocol/start: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("WriteAddr to BaseConfig returned false");
|
||||
return new(new Exception("Failed to configure I2C address/protocol/start"));
|
||||
}
|
||||
}
|
||||
|
||||
// 等待I2C命令完成
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, this.taskID, I2cAddr.Flag, 0x0000_0001, 0xFFFF_FFFF);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to wait for I2C command completion: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error("ReadAddrWithWait for I2C command completion returned false");
|
||||
return new(new Exception("I2C command did not complete successfully"));
|
||||
}
|
||||
}
|
||||
|
||||
// 读取数据
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, I2cAddr.Read, length);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to read data from I2C FIFO: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
|
||||
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length != length)
|
||||
{
|
||||
logger.Error($"ReadAddr returned unexpected data length: {ret.Value.Options.Data?.Length ?? 0}");
|
||||
return new(new Exception("Failed to read expected amount of data from I2C FIFO"));
|
||||
}
|
||||
|
||||
return ret.Value.Options.Data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Net;
|
||||
using BsdlParser;
|
||||
using DotNext;
|
||||
using Newtonsoft.Json;
|
||||
using server;
|
||||
using WebProtocol;
|
||||
|
||||
namespace JtagClient;
|
||||
namespace Peripherals.JtagClient;
|
||||
|
||||
/// <summary>
|
||||
/// Global Constant Jtag Address
|
||||
@@ -422,7 +422,7 @@ public class Jtag
|
||||
if (!MsgBus.IsRunning)
|
||||
return new(new Exception("Message Bus not Working!"));
|
||||
|
||||
var retPack = await MsgBus.UDPServer.WaitForDataAsync(address, port);
|
||||
var retPack = await MsgBus.UDPServer.WaitForDataAsync(address, 0, port);
|
||||
if (!retPack.IsSuccessful || !retPack.Value.IsSuccessful)
|
||||
return new(new Exception("Send address package failed"));
|
||||
|
||||
@@ -441,7 +441,7 @@ public class Jtag
|
||||
(UInt32 devAddr, UInt32 data, UInt32 result, UInt32 resultMask = 0xFF_FF_FF_FF, UInt32 delayMilliseconds = 0)
|
||||
{
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, devAddr, data, this.timeout);
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, 0, devAddr, data, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
if (!ret.Value) return new(new Exception("Write FIFO failed"));
|
||||
}
|
||||
@@ -450,7 +450,7 @@ public class Jtag
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(delayMilliseconds));
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, JtagAddr.STATE, result, resultMask, this.timeout);
|
||||
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, 0, JtagAddr.STATE, result, resultMask, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
return ret.Value;
|
||||
}
|
||||
@@ -460,7 +460,7 @@ public class Jtag
|
||||
(UInt32 devAddr, byte[] data, UInt32 result, UInt32 resultMask = 0xFF_FF_FF_FF, UInt32 delayMilliseconds = 0)
|
||||
{
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, devAddr, data, this.timeout);
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, 0, devAddr, data, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
if (!ret.Value) return new(new Exception("Write FIFO failed"));
|
||||
}
|
||||
@@ -469,7 +469,7 @@ public class Jtag
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(delayMilliseconds));
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, JtagAddr.STATE, result, resultMask, this.timeout);
|
||||
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, 0, JtagAddr.STATE, result, resultMask, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
return ret.Value;
|
||||
}
|
||||
@@ -627,9 +627,9 @@ public class Jtag
|
||||
public async ValueTask<Result<uint>> ReadIDCode()
|
||||
{
|
||||
// Clear Data
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||
|
||||
logger.Trace($"Clear up udp server {this.address} receive data");
|
||||
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
||||
|
||||
Result<bool> ret;
|
||||
|
||||
@@ -665,9 +665,9 @@ public class Jtag
|
||||
public async ValueTask<Result<uint>> ReadStatusReg()
|
||||
{
|
||||
// Clear Data
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||
|
||||
logger.Trace($"Clear up udp server {this.address} receive data");
|
||||
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
||||
|
||||
Result<bool> ret;
|
||||
|
||||
@@ -702,9 +702,9 @@ public class Jtag
|
||||
public async ValueTask<Result<bool>> DownloadBitstream(byte[] bitstream)
|
||||
{
|
||||
// Clear Data
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||
|
||||
logger.Trace($"Clear up udp server {this.address} receive data");
|
||||
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
||||
|
||||
Result<bool> ret;
|
||||
|
||||
@@ -786,9 +786,9 @@ public class Jtag
|
||||
logger.Debug($"Get boundar scan registers number: {portNum}");
|
||||
|
||||
// Clear Data
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||
|
||||
logger.Trace($"Clear up udp server {this.address} receive data");
|
||||
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
||||
|
||||
Result<bool> ret;
|
||||
|
||||
@@ -853,9 +853,9 @@ public class Jtag
|
||||
public async ValueTask<Result<bool>> SetSpeed(UInt32 speed)
|
||||
{
|
||||
// Clear Data
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||
|
||||
logger.Trace($"Clear up udp server {this.address} receive data");
|
||||
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
||||
|
||||
var ret = await WriteFIFO(
|
||||
JtagAddr.SPEED_CTRL, (speed << 16) | speed,
|
||||
@@ -44,10 +44,10 @@ public class MatrixKey
|
||||
public async ValueTask<Result<bool>> EnableControl()
|
||||
{
|
||||
if (MsgBus.IsRunning)
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||
else return new(new Exception("Message Bus not work!"));
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, MatrixKeyAddr.KEY_ENABLE, 1, this.timeout);
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, 1, MatrixKeyAddr.KEY_ENABLE, 1, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
return ret.Value;
|
||||
}
|
||||
@@ -59,10 +59,10 @@ public class MatrixKey
|
||||
public async ValueTask<Result<bool>> DisableControl()
|
||||
{
|
||||
if (MsgBus.IsRunning)
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||
else return new(new Exception("Message Bus not work!"));
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, MatrixKeyAddr.KEY_ENABLE, 0, this.timeout);
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, 1, MatrixKeyAddr.KEY_ENABLE, 0, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
return ret.Value;
|
||||
}
|
||||
@@ -75,14 +75,14 @@ public class MatrixKey
|
||||
public async ValueTask<Result<bool>> ControlKey(BitArray keyStates)
|
||||
{
|
||||
if (MsgBus.IsRunning)
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||
else return new(new Exception("Message Bus not work!"));
|
||||
|
||||
if (keyStates.Length != 16) return new(new ArgumentException(
|
||||
$"The number of key should be 16 instead of {keyStates.Length}", nameof(keyStates)));
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, MatrixKeyAddr.KEY_CTRL, Common.Number.BitsToNumber(keyStates).Value, this.timeout);
|
||||
this.ep, 1, MatrixKeyAddr.KEY_CTRL, Common.Number.BitsToNumber(keyStates).Value, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
return ret.Value;
|
||||
}
|
||||
|
||||
36
server/src/Peripherals/OscilloscopeClient.cs
Normal file
36
server/src/Peripherals/OscilloscopeClient.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Net;
|
||||
using DotNext;
|
||||
|
||||
namespace Peripherals.OscilloscopeClient;
|
||||
|
||||
static class OscilloscopeAddr
|
||||
{
|
||||
public const UInt32 Base = 0x0000_0000;
|
||||
}
|
||||
|
||||
class Oscilloscope
|
||||
{
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
readonly int timeout = 2000;
|
||||
|
||||
readonly int port;
|
||||
readonly string address;
|
||||
private IPEndPoint ep;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化示波器客户端
|
||||
/// </summary>
|
||||
/// <param name="address">示波器设备IP地址</param>
|
||||
/// <param name="port">示波器设备端口</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
public Oscilloscope(string address, int port, int timeout = 2000)
|
||||
{
|
||||
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.timeout = timeout;
|
||||
}
|
||||
}
|
||||
@@ -45,10 +45,10 @@ public class Power
|
||||
public async ValueTask<Result<bool>> SetPowerOnOff(bool enable)
|
||||
{
|
||||
if (MsgBus.IsRunning)
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||
else return new(new Exception("Message Bus not work!"));
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, PowerAddr.PowerCtrl, Convert.ToUInt32(enable), this.timeout);
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, 1, PowerAddr.PowerCtrl, Convert.ToUInt32(enable), this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
return ret.Value;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Net;
|
||||
using DotNext;
|
||||
namespace RemoteUpdateClient;
|
||||
|
||||
namespace Peripherals.RemoteUpdateClient;
|
||||
|
||||
static class RemoteUpdaterAddr
|
||||
{
|
||||
@@ -142,7 +143,7 @@ public class RemoteUpdater
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, RemoteUpdaterAddr.WriteCtrl,
|
||||
this.ep, 0, RemoteUpdaterAddr.WriteCtrl,
|
||||
Convert.ToUInt32((writeSectorNum << 16) | (1 << 15) | Convert.ToInt32(flashAddr / 4096)), this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
if (!ret.Value) return new(new Exception("Enable write flash failed"));
|
||||
@@ -150,7 +151,7 @@ public class RemoteUpdater
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddrWithWait(
|
||||
this.ep, RemoteUpdaterAddr.WriteSign,
|
||||
this.ep, 0, RemoteUpdaterAddr.WriteSign,
|
||||
0x00_00_00_01, 0x00_00_00_01, this.timeoutForWait);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
if (!ret.Value) return new(new Exception(
|
||||
@@ -158,14 +159,14 @@ public class RemoteUpdater
|
||||
}
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, RemoteUpdaterAddr.WriteFIFO, bytesData, this.timeout);
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, 0, RemoteUpdaterAddr.WriteFIFO, bytesData, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
if (!ret.Value) return new(new Exception("Send data to flash failed"));
|
||||
}
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddrWithWait(
|
||||
this.ep, RemoteUpdaterAddr.WriteSign,
|
||||
this.ep, 0, RemoteUpdaterAddr.WriteSign,
|
||||
0x00_00_01_00, 0x00_00_01_00, this.timeoutForWait);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
return ret.Value;
|
||||
@@ -314,14 +315,14 @@ public class RemoteUpdater
|
||||
private async ValueTask<Result<bool>> CheckBitstreamCRC(int bitstreamNum, int bitstreamLen, UInt32 checkSum)
|
||||
{
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, RemoteUpdaterAddr.ReadCtrl2, 0x00_00_00_00, this.timeout);
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, 0, RemoteUpdaterAddr.ReadCtrl2, 0x00_00_00_00, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
if (!ret.Value) return new(new Exception("Write read control 2 failed"));
|
||||
}
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, RemoteUpdaterAddr.ReadCtrl1,
|
||||
this.ep, 0, RemoteUpdaterAddr.ReadCtrl1,
|
||||
Convert.ToUInt32((bitstreamLen << 16) | (1 << 15) | Convert.ToInt32(FlashAddr.Bitstream[bitstreamNum] / 4096)),
|
||||
this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
@@ -330,7 +331,7 @@ public class RemoteUpdater
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddrWithWait(
|
||||
this.ep, RemoteUpdaterAddr.ReadSign,
|
||||
this.ep, 0, RemoteUpdaterAddr.ReadSign,
|
||||
0x00_00_01_00, 0x00_00_01_00, this.timeoutForWait);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
if (!ret.Value) return new(new Exception(
|
||||
@@ -338,7 +339,7 @@ public class RemoteUpdater
|
||||
}
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddr(this.ep, RemoteUpdaterAddr.ReadCRC, this.timeout);
|
||||
var ret = await UDPClientPool.ReadAddr(this.ep, 0, RemoteUpdaterAddr.ReadCRC, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
|
||||
var bytes = ret.Value.Options.Data;
|
||||
@@ -368,7 +369,7 @@ public class RemoteUpdater
|
||||
$"Bitsteam num should be 0 ~ 3 for HotRest, but given {bitstreamNum}", nameof(bitstreamNum)));
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, RemoteUpdaterAddr.HotResetCtrl,
|
||||
this.ep, 0, RemoteUpdaterAddr.HotResetCtrl,
|
||||
((FlashAddr.Bitstream[bitstreamNum] << 8) | 1), this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
return ret.Value;
|
||||
@@ -381,7 +382,7 @@ public class RemoteUpdater
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public async ValueTask<Result<bool>> HotResetBitstream(int bitstreamNum)
|
||||
{
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||
logger.Trace("Clear udp data finished");
|
||||
|
||||
{
|
||||
@@ -411,7 +412,7 @@ public class RemoteUpdater
|
||||
byte[]? bitstream2,
|
||||
byte[]? bitstream3)
|
||||
{
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||
logger.Trace("Clear udp data finished");
|
||||
|
||||
for (int bitstreamNum = 0; bitstreamNum < 4; bitstreamNum++)
|
||||
@@ -462,7 +463,7 @@ public class RemoteUpdater
|
||||
$"The length of data should be divided by 4096, bug given {bytesData.Length}", nameof(bytesData)));
|
||||
var bitstreamBlockNum = bytesData.Length / (4 * 1024);
|
||||
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||
logger.Trace("Clear udp data finished");
|
||||
|
||||
{
|
||||
@@ -538,11 +539,11 @@ public class RemoteUpdater
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public async ValueTask<Result<UInt32>> GetVersion()
|
||||
{
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||
logger.Trace("Clear udp data finished");
|
||||
|
||||
{
|
||||
var ret = await UDPClientPool.ReadAddr(this.ep, RemoteUpdaterAddr.Version, this.timeout);
|
||||
var ret = await UDPClientPool.ReadAddr(this.ep, 0, RemoteUpdaterAddr.Version, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
|
||||
var retData = ret.Value.Options.Data;
|
||||
907
server/src/Services/HttpVideoStreamService.cs
Normal file
907
server/src/Services/HttpVideoStreamService.cs
Normal file
@@ -0,0 +1,907 @@
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Peripherals.CameraClient; // 添加摄像头客户端引用
|
||||
|
||||
namespace server.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 表示摄像头连接状态信息
|
||||
/// </summary>
|
||||
public class CameraStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// 摄像头的IP地址
|
||||
/// </summary>
|
||||
public string Address { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 摄像头的端口号
|
||||
/// </summary>
|
||||
public int Port { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已配置摄像头
|
||||
/// </summary>
|
||||
public bool IsConfigured { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 摄像头连接字符串(IP:端口)
|
||||
/// </summary>
|
||||
public string ConnectionString { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 表示视频流服务的运行状态
|
||||
/// </summary>
|
||||
public class ServiceStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// 服务是否正在运行
|
||||
/// </summary>
|
||||
public bool IsRunning { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 服务监听的端口号
|
||||
/// </summary>
|
||||
public int ServerPort { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 视频流的帧率(FPS)
|
||||
/// </summary>
|
||||
public int FrameRate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 视频分辨率(如 640x480)
|
||||
/// </summary>
|
||||
public string Resolution { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 当前连接的客户端数量
|
||||
/// </summary>
|
||||
public int ConnectedClients { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前连接的客户端端点列表
|
||||
/// </summary>
|
||||
public List<string> ClientEndpoints { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 摄像头连接状态信息
|
||||
/// </summary>
|
||||
public CameraStatus CameraStatus { get; set; } = new();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// HTTP 视频流服务,用于从 FPGA 获取图像数据并推送到前端网页
|
||||
/// 支持动态配置摄像头地址和端口
|
||||
/// </summary>
|
||||
public class HttpVideoStreamService : BackgroundService
|
||||
{
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private HttpListener? _httpListener;
|
||||
private readonly int _serverPort = 8080;
|
||||
private readonly int _frameRate = 30; // 30 FPS
|
||||
private readonly int _frameWidth = 1280;
|
||||
private readonly int _frameHeight = 720;
|
||||
|
||||
// 摄像头客户端
|
||||
private Camera? _camera;
|
||||
private bool _cameraEnable = false;
|
||||
private string _cameraAddress = "192.168.1.100"; // 默认FPGA地址
|
||||
private int _cameraPort = 8888; // 默认端口
|
||||
private readonly object _cameraLock = new object();
|
||||
|
||||
// 模拟 FPGA 图像数据
|
||||
private int _frameCounter = 0;
|
||||
private readonly List<HttpListenerResponse> _activeClients = new List<HttpListenerResponse>();
|
||||
private readonly object _clientsLock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前连接的客户端数量
|
||||
/// </summary>
|
||||
public int ConnectedClientsCount { get { return _activeClients.Count; } }
|
||||
|
||||
/// <summary>
|
||||
/// 获取服务端口
|
||||
/// </summary>
|
||||
public int ServerPort => _serverPort;
|
||||
|
||||
/// <summary>
|
||||
/// 获取帧宽度
|
||||
/// </summary>
|
||||
public int FrameWidth => _frameWidth;
|
||||
|
||||
/// <summary>
|
||||
/// 获取帧高度
|
||||
/// </summary>
|
||||
public int FrameHeight => _frameHeight;
|
||||
|
||||
/// <summary>
|
||||
/// 获取帧率
|
||||
/// </summary>
|
||||
public int FrameRate => _frameRate;
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前摄像头地址
|
||||
/// </summary>
|
||||
public string CameraAddress { get { return _cameraAddress; } }
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前摄像头端口
|
||||
/// </summary>
|
||||
public int CameraPort { get { return _cameraPort; } }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化 HttpVideoStreamService
|
||||
/// </summary>
|
||||
public HttpVideoStreamService()
|
||||
{
|
||||
// 延迟初始化摄像头客户端,直到配置完成
|
||||
logger.Info("HttpVideoStreamService 初始化完成,默认摄像头地址: {Address}:{Port}", _cameraAddress, _cameraPort);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <param name="isEnabled">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public async Task SetEnable(bool isEnabled)
|
||||
{
|
||||
if (_camera == null)
|
||||
{
|
||||
throw new Exception("Please config camera first");
|
||||
}
|
||||
_cameraEnable = isEnabled;
|
||||
await _camera.EnableCamera(_cameraEnable);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置摄像头连接参数
|
||||
/// </summary>
|
||||
/// <param name="address">摄像头IP地址</param>
|
||||
/// <param name="port">摄像头端口</param>
|
||||
/// <returns>配置是否成功</returns>
|
||||
public async Task<bool> ConfigureCameraAsync(string address, int port)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(address))
|
||||
{
|
||||
logger.Error("摄像头地址不能为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (port <= 0 || port > 65535)
|
||||
{
|
||||
logger.Error("摄像头端口必须在1-65535范围内");
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
lock (_cameraLock)
|
||||
{
|
||||
// 关闭现有连接
|
||||
if (_camera != null)
|
||||
{
|
||||
logger.Info("关闭现有摄像头连接");
|
||||
// Camera doesn't have Dispose method, set to null
|
||||
_camera = null;
|
||||
}
|
||||
|
||||
// 更新配置
|
||||
_cameraAddress = address;
|
||||
_cameraPort = port;
|
||||
|
||||
// 创建新的摄像头客户端
|
||||
_camera = new Camera(_cameraAddress, _cameraPort);
|
||||
|
||||
logger.Info("摄像头配置已更新: {Address}:{Port}", _cameraAddress, _cameraPort);
|
||||
}
|
||||
|
||||
// Init Camera
|
||||
{
|
||||
var ret = await _camera.Init();
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error(ret.Error);
|
||||
throw ret.Error;
|
||||
}
|
||||
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error($"Camera Init Failed!");
|
||||
throw new Exception($"Camera Init Failed!");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "配置摄像头连接时发生错误");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试摄像头连接
|
||||
/// </summary>
|
||||
/// <returns>连接测试结果</returns>
|
||||
public async Task<(bool IsSuccess, string Message)> TestCameraConnectionAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
Camera? testCamera = null;
|
||||
|
||||
lock (_cameraLock)
|
||||
{
|
||||
if (_camera == null)
|
||||
{
|
||||
return (false, "摄像头未配置");
|
||||
}
|
||||
testCamera = _camera;
|
||||
}
|
||||
|
||||
// 尝试读取一帧数据来测试连接
|
||||
var result = await testCamera.ReadFrame();
|
||||
|
||||
if (result.IsSuccessful)
|
||||
{
|
||||
logger.Info("摄像头连接测试成功: {Address}:{Port}", _cameraAddress, _cameraPort);
|
||||
return (true, "连接成功");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Warn("摄像头连接测试失败: {Error}", result.Error);
|
||||
return (false, result.Error.ToString());
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "摄像头连接测试出错");
|
||||
return (false, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取摄像头连接状态
|
||||
/// </summary>
|
||||
/// <returns>连接状态信息</returns>
|
||||
public CameraStatus GetCameraStatus()
|
||||
{
|
||||
return new CameraStatus
|
||||
{
|
||||
Address = _cameraAddress,
|
||||
Port = _cameraPort,
|
||||
IsConfigured = _camera != null,
|
||||
ConnectionString = $"{_cameraAddress}:{_cameraPort}"
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行 HTTP 视频流服务
|
||||
/// </summary>
|
||||
/// <param name="stoppingToken">取消令牌</param>
|
||||
/// <returns>任务</returns>
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.Info("启动 HTTP 视频流服务,端口: {Port}", _serverPort);
|
||||
|
||||
// 初始化默认摄像头连接
|
||||
await ConfigureCameraAsync(_cameraAddress, _cameraPort);
|
||||
|
||||
// 创建 HTTP 监听器
|
||||
_httpListener = new HttpListener();
|
||||
_httpListener.Prefixes.Add($"http://localhost:{_serverPort}/");
|
||||
_httpListener.Start();
|
||||
|
||||
logger.Info("HTTP 视频流服务已启动,监听端口: {Port}", _serverPort);
|
||||
|
||||
// 开始接受客户端连接
|
||||
_ = Task.Run(() => AcceptClientsAsync(stoppingToken), stoppingToken);
|
||||
|
||||
// 开始生成视频帧
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (_cameraEnable)
|
||||
{
|
||||
await GenerateVideoFrames(stoppingToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Task.Delay(500, stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (HttpListenerException ex)
|
||||
{
|
||||
logger.Error(ex, "HTTP 视频流服务启动失败,请确保您有管理员权限或使用netsh配置URL前缀权限");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "HTTP 视频流服务启动失败");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AcceptClientsAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested && _httpListener != null && _httpListener.IsListening)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 等待客户端连接
|
||||
var context = await _httpListener.GetContextAsync();
|
||||
var request = context.Request;
|
||||
var response = context.Response;
|
||||
|
||||
logger.Info("新HTTP客户端连接: {RemoteEndPoint}", request.RemoteEndPoint);
|
||||
// 处理不同的请求路径
|
||||
var requestPath = request.Url?.AbsolutePath ?? "/";
|
||||
|
||||
if (requestPath == "/video-stream")
|
||||
{
|
||||
// MJPEG 流请求
|
||||
_ = Task.Run(() => HandleMjpegStreamAsync(response, cancellationToken), cancellationToken);
|
||||
}
|
||||
else if (requestPath == "/snapshot")
|
||||
{
|
||||
// 单帧图像请求
|
||||
await HandleSnapshotRequestAsync(response, cancellationToken);
|
||||
}
|
||||
else if (requestPath == "/video-feed.html")
|
||||
{
|
||||
// HTML页面请求
|
||||
await SendVideoHtmlPageAsync(response);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 默认返回简单的HTML页面,提供链接到视频页面
|
||||
await SendIndexHtmlPageAsync(response);
|
||||
}
|
||||
}
|
||||
catch (HttpListenerException)
|
||||
{
|
||||
// HTTP监听器可能已停止
|
||||
break;
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// 对象可能已被释放
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "接受HTTP客户端连接时发生错误");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleMjpegStreamAsync(HttpListenerResponse response, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 设置MJPEG流的响应头
|
||||
response.ContentType = "multipart/x-mixed-replace; boundary=--boundary";
|
||||
response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
response.Headers.Add("Pragma", "no-cache");
|
||||
response.Headers.Add("Expires", "0");
|
||||
|
||||
// 跟踪活跃的客户端
|
||||
lock (_clientsLock)
|
||||
{
|
||||
_activeClients.Add(response);
|
||||
}
|
||||
|
||||
logger.Debug("已启动MJPEG流,客户端: {RemoteEndPoint}", response.OutputStream?.GetHashCode() ?? 0);
|
||||
|
||||
// 保持连接直到取消或出错
|
||||
try
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(100, cancellationToken); // 简单的保活循环
|
||||
}
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
// 预期的取消
|
||||
}
|
||||
|
||||
logger.Debug("MJPEG流已结束,客户端: {ClientId}", response.OutputStream?.GetHashCode() ?? 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "处理MJPEG流时出错");
|
||||
}
|
||||
finally
|
||||
{
|
||||
lock (_clientsLock)
|
||||
{
|
||||
_activeClients.Remove(response);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
response.Close();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略关闭时的错误
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleSnapshotRequestAsync(HttpListenerResponse response, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取当前帧
|
||||
var imageData = await GetFPGAImageData();
|
||||
|
||||
// 直接使用Common.Image.ConvertRGB24ToJpeg进行转换
|
||||
var jpegResult = Common.Image.ConvertRGB24ToJpeg(imageData, _frameWidth, _frameHeight, 80);
|
||||
if (!jpegResult.IsSuccessful)
|
||||
{
|
||||
logger.Error("RGB24转JPEG失败: {Error}", jpegResult.Error);
|
||||
response.StatusCode = 500;
|
||||
response.Close();
|
||||
return;
|
||||
}
|
||||
|
||||
var jpegData = jpegResult.Value;
|
||||
|
||||
// 设置响应头
|
||||
response.ContentType = "image/jpeg";
|
||||
response.ContentLength64 = jpegData.Length;
|
||||
response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
|
||||
// 发送JPEG数据
|
||||
await response.OutputStream.WriteAsync(jpegData, 0, jpegData.Length, cancellationToken);
|
||||
await response.OutputStream.FlushAsync(cancellationToken);
|
||||
|
||||
logger.Debug("已发送快照图像,大小:{Size} 字节", jpegData.Length);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "处理快照请求时出错");
|
||||
}
|
||||
finally
|
||||
{
|
||||
response.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendVideoHtmlPageAsync(HttpListenerResponse response)
|
||||
{
|
||||
string html = $@"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>FPGA 视频流</title>
|
||||
<meta charset=""utf-8"">
|
||||
<style>
|
||||
body {{ font-family: Arial, sans-serif; text-align: center; margin: 20px; }}
|
||||
h1 {{ color: #333; }}
|
||||
.video-container {{ margin: 20px auto; max-width: 800px; }}
|
||||
.controls {{ margin: 10px 0; }}
|
||||
img {{ max-width: 100%; border: 1px solid #ddd; }}
|
||||
button {{ padding: 8px 16px; margin: 0 5px; cursor: pointer; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>FPGA 实时视频流</h1>
|
||||
<div class=""video-container"">
|
||||
<img id=""videoStream"" src=""/video-stream"" alt=""FPGA视频流"" />
|
||||
</div>
|
||||
<div class=""controls"">
|
||||
<button onclick=""document.getElementById('videoStream').src='/snapshot?t=' + new Date().getTime()"">刷新快照</button>
|
||||
<button onclick=""document.getElementById('videoStream').src='/video-stream'"">开始流媒体</button>
|
||||
<span id=""status"">状态: 连接中...</span>
|
||||
</div>
|
||||
<script>
|
||||
document.getElementById('videoStream').onload = function() {{
|
||||
document.getElementById('status').textContent = '状态: 已连接';
|
||||
}};
|
||||
document.getElementById('videoStream').onerror = function() {{
|
||||
document.getElementById('status').textContent = '状态: 连接错误';
|
||||
}};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
";
|
||||
|
||||
response.ContentType = "text/html";
|
||||
response.ContentEncoding = Encoding.UTF8;
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(html);
|
||||
response.ContentLength64 = buffer.Length;
|
||||
|
||||
await response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||
response.Close();
|
||||
}
|
||||
|
||||
private async Task SendIndexHtmlPageAsync(HttpListenerResponse response)
|
||||
{
|
||||
string html = $@"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>FPGA WebLab 视频服务</title>
|
||||
<meta charset=""utf-8"">
|
||||
<style>
|
||||
body {{ font-family: Arial, sans-serif; text-align: center; margin: 20px; }}
|
||||
h1 {{ color: #333; }}
|
||||
.links {{ margin: 20px; }}
|
||||
a {{ padding: 10px 15px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 4px; margin: 5px; display: inline-block; }}
|
||||
a:hover {{ background-color: #45a049; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>FPGA WebLab 视频服务</h1>
|
||||
<div class=""links"">
|
||||
<a href=""/video-feed.html"">观看实时视频</a>
|
||||
<a href=""/snapshot"" target=""_blank"">获取当前快照</a>
|
||||
</div>
|
||||
<p>HTTP流媒体服务端口: {_serverPort}</p>
|
||||
</body>
|
||||
</html>
|
||||
";
|
||||
|
||||
response.ContentType = "text/html";
|
||||
response.ContentEncoding = Encoding.UTF8;
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(html);
|
||||
response.ContentLength64 = buffer.Length;
|
||||
|
||||
await response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||
response.Close();
|
||||
}
|
||||
|
||||
private async Task GenerateVideoFrames(CancellationToken cancellationToken)
|
||||
{
|
||||
var frameInterval = TimeSpan.FromMilliseconds(1000.0 / _frameRate);
|
||||
var lastFrameTime = DateTime.UtcNow;
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested && _cameraEnable)
|
||||
{
|
||||
try
|
||||
{
|
||||
var frameStartTime = DateTime.UtcNow;
|
||||
|
||||
// 从 FPGA 获取图像数据
|
||||
var imageData = await GetFPGAImageData();
|
||||
|
||||
var imageAcquireTime = DateTime.UtcNow;
|
||||
|
||||
// 如果有图像数据,立即开始广播(不等待)
|
||||
if (imageData != null && imageData.Length > 0)
|
||||
{
|
||||
// 异步广播帧,不阻塞下一帧的获取
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await BroadcastFrameAsync(imageData, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "异步广播帧时发生错误");
|
||||
}
|
||||
}, cancellationToken);
|
||||
|
||||
_frameCounter++;
|
||||
|
||||
var frameEndTime = DateTime.UtcNow;
|
||||
var frameProcessingTime = (frameEndTime - frameStartTime).TotalMilliseconds;
|
||||
var imageAcquireElapsed = (imageAcquireTime - frameStartTime).TotalMilliseconds;
|
||||
|
||||
if (_frameCounter % 30 == 0) // 每秒记录一次性能信息
|
||||
{
|
||||
logger.Debug("帧 {FrameNumber} 性能统计 - 图像获取: {AcquireTime:F1}ms, 总处理: {ProcessTime:F1}ms",
|
||||
_frameCounter, imageAcquireElapsed, frameProcessingTime);
|
||||
}
|
||||
}
|
||||
|
||||
// 动态调整延迟 - 基于实际处理时间
|
||||
var elapsed = (DateTime.UtcNow - lastFrameTime).TotalMilliseconds;
|
||||
var targetInterval = frameInterval.TotalMilliseconds;
|
||||
var remainingDelay = Math.Max(0, targetInterval - elapsed);
|
||||
|
||||
if (remainingDelay > 0)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(remainingDelay), cancellationToken);
|
||||
}
|
||||
|
||||
lastFrameTime = DateTime.UtcNow;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "生成视频帧时发生错误");
|
||||
await Task.Delay(100, cancellationToken); // 减少错误恢复延迟
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 FPGA 获取图像数据
|
||||
/// 实际从摄像头读取 RGB565 格式数据并转换为 RGB24
|
||||
/// </summary>
|
||||
private async Task<byte[]> GetFPGAImageData()
|
||||
{
|
||||
var startTime = DateTime.UtcNow;
|
||||
Camera? currentCamera = null;
|
||||
|
||||
lock (_cameraLock)
|
||||
{
|
||||
currentCamera = _camera;
|
||||
}
|
||||
|
||||
if (currentCamera == null)
|
||||
{
|
||||
logger.Error("摄像头客户端未初始化");
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 从摄像头读取帧数据
|
||||
var readStartTime = DateTime.UtcNow;
|
||||
var result = await currentCamera.ReadFrame();
|
||||
var readEndTime = DateTime.UtcNow;
|
||||
var readTime = (readEndTime - readStartTime).TotalMilliseconds;
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error("读取摄像头帧数据失败: {Error}", result.Error);
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
var rgb565Data = result.Value;
|
||||
|
||||
// 验证数据长度是否正确
|
||||
if (!Common.Image.ValidateImageDataLength(rgb565Data, _frameWidth, _frameHeight, 2))
|
||||
{
|
||||
logger.Warn("摄像头数据长度不匹配,期望: {Expected}, 实际: {Actual}",
|
||||
_frameWidth * _frameHeight * 2, rgb565Data.Length);
|
||||
}
|
||||
|
||||
// 将 RGB565 转换为 RGB24
|
||||
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("RGB565转RGB24失败: {Error}", rgb24Result.Error);
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
var totalTime = (DateTime.UtcNow - startTime).TotalMilliseconds;
|
||||
|
||||
if (_frameCounter % 30 == 0) // 每秒更新一次日志
|
||||
{
|
||||
logger.Debug("帧 {FrameNumber} 数据获取性能 - 读取: {ReadTime:F1}ms, 转换: {ConvertTime:F1}ms, 总计: {TotalTime:F1}ms, RGB565: {RGB565Size} 字节, RGB24: {RGB24Size} 字节",
|
||||
_frameCounter, readTime, convertTime, totalTime, rgb565Data.Length, rgb24Result.Value.Length);
|
||||
}
|
||||
|
||||
return rgb24Result.Value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取FPGA图像数据时发生错误");
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向所有连接的客户端广播帧数据
|
||||
/// </summary>
|
||||
private async Task BroadcastFrameAsync(byte[] frameData, CancellationToken cancellationToken)
|
||||
{
|
||||
if (frameData == null || frameData.Length == 0)
|
||||
{
|
||||
logger.Warn("尝试广播空帧数据");
|
||||
return;
|
||||
}
|
||||
|
||||
// 直接使用Common.Image.ConvertRGB24ToJpeg进行转换
|
||||
var jpegResult = Common.Image.ConvertRGB24ToJpeg(frameData, _frameWidth, _frameHeight, 80);
|
||||
if (!jpegResult.IsSuccessful)
|
||||
{
|
||||
logger.Error("RGB24转JPEG失败: {Error}", jpegResult.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
var jpegData = jpegResult.Value;
|
||||
|
||||
// 使用Common中的方法准备MJPEG帧数据
|
||||
var mjpegFrameHeader = Common.Image.CreateMjpegFrameHeader(jpegData.Length);
|
||||
var mjpegFrameFooter = Common.Image.CreateMjpegFrameFooter();
|
||||
|
||||
var clientsToRemove = new List<HttpListenerResponse>();
|
||||
var clientsToProcess = new List<HttpListenerResponse>();
|
||||
|
||||
// 获取当前连接的客户端列表
|
||||
lock (_clientsLock)
|
||||
{
|
||||
clientsToProcess.AddRange(_activeClients);
|
||||
}
|
||||
|
||||
if (clientsToProcess.Count == 0)
|
||||
{
|
||||
return; // 没有活跃客户端
|
||||
}
|
||||
|
||||
// 向每个活跃的客户端并行发送帧
|
||||
var sendTasks = clientsToProcess.Select(async client =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// 发送帧头部
|
||||
await client.OutputStream.WriteAsync(mjpegFrameHeader, 0, mjpegFrameHeader.Length, cancellationToken);
|
||||
|
||||
// 发送JPEG数据
|
||||
await client.OutputStream.WriteAsync(jpegData, 0, jpegData.Length, cancellationToken);
|
||||
|
||||
// 发送结尾换行符
|
||||
await client.OutputStream.WriteAsync(mjpegFrameFooter, 0, mjpegFrameFooter.Length, cancellationToken);
|
||||
|
||||
// 确保数据立即发送
|
||||
await client.OutputStream.FlushAsync(cancellationToken);
|
||||
|
||||
return (client, success: true, error: (Exception?)null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return (client, success: false, error: ex);
|
||||
}
|
||||
});
|
||||
|
||||
// 等待所有发送任务完成
|
||||
var results = await Task.WhenAll(sendTasks);
|
||||
|
||||
// 处理发送结果
|
||||
foreach (var (client, success, error) in results)
|
||||
{
|
||||
if (!success)
|
||||
{
|
||||
logger.Debug("发送帧数据时出错: {Error}", error?.Message ?? "未知错误");
|
||||
clientsToRemove.Add(client);
|
||||
}
|
||||
}
|
||||
|
||||
if (_frameCounter % 30 == 0 && clientsToProcess.Count > 0) // 每秒记录一次日志
|
||||
{
|
||||
logger.Debug("已向 {ClientCount} 个客户端发送第 {FrameNumber} 帧,大小:{Size} 字节",
|
||||
clientsToProcess.Count, _frameCounter, jpegData.Length);
|
||||
}
|
||||
|
||||
// 移除断开连接的客户端
|
||||
if (clientsToRemove.Count > 0)
|
||||
{
|
||||
lock (_clientsLock)
|
||||
{
|
||||
foreach (var client in clientsToRemove)
|
||||
{
|
||||
_activeClients.Remove(client);
|
||||
try { client.Close(); }
|
||||
catch { /* 忽略关闭错误 */ }
|
||||
}
|
||||
}
|
||||
|
||||
logger.Info("已移除 {Count} 个断开连接的客户端,当前连接数: {ActiveCount}",
|
||||
clientsToRemove.Count, _activeClients.Count);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取连接的客户端端点列表
|
||||
/// </summary>
|
||||
public List<string> GetConnectedClientEndpoints()
|
||||
{
|
||||
List<string> endpoints = new List<string>();
|
||||
|
||||
lock (_clientsLock)
|
||||
{
|
||||
foreach (var client in _activeClients)
|
||||
{
|
||||
endpoints.Add($"Client-{client.OutputStream?.GetHashCode() ?? 0}");
|
||||
}
|
||||
}
|
||||
|
||||
return endpoints;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取服务状态信息
|
||||
/// </summary>
|
||||
public ServiceStatus GetServiceStatus()
|
||||
{
|
||||
var cameraStatus = GetCameraStatus();
|
||||
|
||||
return new ServiceStatus
|
||||
{
|
||||
IsRunning = (_httpListener?.IsListening ?? false) && _cameraEnable,
|
||||
ServerPort = _serverPort,
|
||||
FrameRate = _frameRate,
|
||||
Resolution = $"{_frameWidth}x{_frameHeight}",
|
||||
ConnectedClients = ConnectedClientsCount,
|
||||
ClientEndpoints = GetConnectedClientEndpoints(),
|
||||
CameraStatus = cameraStatus
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 停止 HTTP 视频流服务
|
||||
/// </summary>
|
||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
logger.Info("正在停止 HTTP 视频流服务...");
|
||||
|
||||
_cameraEnable = false;
|
||||
|
||||
if (_httpListener != null && _httpListener.IsListening)
|
||||
{
|
||||
_httpListener.Stop();
|
||||
_httpListener.Close();
|
||||
}
|
||||
|
||||
// 关闭所有客户端连接
|
||||
lock (_clientsLock)
|
||||
{
|
||||
foreach (var client in _activeClients)
|
||||
{
|
||||
try { client.Close(); }
|
||||
catch { /* 忽略关闭错误 */ }
|
||||
}
|
||||
_activeClients.Clear();
|
||||
}
|
||||
|
||||
// 关闭摄像头连接
|
||||
lock (_cameraLock)
|
||||
{
|
||||
_camera = null;
|
||||
}
|
||||
|
||||
await base.StopAsync(cancellationToken);
|
||||
|
||||
logger.Info("HTTP 视频流服务已停止");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源
|
||||
/// </summary>
|
||||
public override void Dispose()
|
||||
{
|
||||
if (_httpListener != null)
|
||||
{
|
||||
if (_httpListener.IsListening)
|
||||
{
|
||||
_httpListener.Stop();
|
||||
}
|
||||
_httpListener.Close();
|
||||
}
|
||||
|
||||
lock (_clientsLock)
|
||||
{
|
||||
foreach (var client in _activeClients)
|
||||
{
|
||||
try { client.Close(); }
|
||||
catch { /* 忽略关闭错误 */ }
|
||||
}
|
||||
_activeClients.Clear();
|
||||
}
|
||||
|
||||
lock (_cameraLock)
|
||||
{
|
||||
_camera = null;
|
||||
}
|
||||
|
||||
base.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
// 此接口提供获取例程目录服务
|
||||
// GET /api/tutorials 返回所有可用的例程目录
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { Request, Response } from 'express';
|
||||
|
||||
// 获取当前文件的目录
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const publicDir = path.resolve(__dirname, '../public');
|
||||
|
||||
export function getTutorials(req: Request, res: Response) {
|
||||
try {
|
||||
const docDir = path.join(publicDir, 'doc');
|
||||
|
||||
// 读取doc目录下的所有文件夹
|
||||
const entries = fs.readdirSync(docDir, { withFileTypes: true });
|
||||
const dirs = entries
|
||||
.filter(dirent => dirent.isDirectory())
|
||||
.map(dirent => dirent.name);
|
||||
|
||||
// 返回文件夹列表
|
||||
res.json({ tutorials: dirs });
|
||||
} catch (error) {
|
||||
console.error('获取例程目录失败:', error);
|
||||
res.status(500).json({ error: '无法读取例程目录' });
|
||||
}
|
||||
}
|
||||
@@ -60,8 +60,8 @@ public class UDPClientPool
|
||||
var sendLen = socket.SendTo(buf, endPoint);
|
||||
socket.Close();
|
||||
|
||||
logger.Debug($"UDP socket send bytes to device {endPoint.Address.ToString()}:{endPoint.Port.ToString()}:");
|
||||
logger.Debug($" Original Data: {BitConverter.ToString(buf).Replace("-", " ")}");
|
||||
// logger.Debug($"UDP socket send bytes to device {endPoint.Address.ToString()}:{endPoint.Port.ToString()}:");
|
||||
// logger.Debug($" Original Data: {BitConverter.ToString(buf).Replace("-", " ")}");
|
||||
|
||||
if (sendLen == buf.Length) { return true; }
|
||||
else { return false; }
|
||||
@@ -91,9 +91,9 @@ public class UDPClientPool
|
||||
var sendLen = socket.SendTo(sendBytes, endPoint);
|
||||
socket.Close();
|
||||
|
||||
logger.Debug($"UDP socket send address package to device {endPoint.Address.ToString()}:{endPoint.Port.ToString()}:");
|
||||
logger.Debug($" Original Data: {BitConverter.ToString(pkg.ToBytes()).Replace("-", " ")}");
|
||||
logger.Debug($" Decoded Data: {pkg.ToString()}");
|
||||
// logger.Debug($"UDP socket send address package to device {endPoint.Address.ToString()}:{endPoint.Port.ToString()}:");
|
||||
// logger.Debug($" Original Data: {BitConverter.ToString(pkg.ToBytes()).Replace("-", " ")}");
|
||||
// logger.Debug($" Decoded Data: {pkg.ToString()}");
|
||||
|
||||
if (sendLen == sendBytes.Length) { return true; }
|
||||
else { return false; }
|
||||
@@ -124,8 +124,8 @@ public class UDPClientPool
|
||||
var sendLen = socket.SendTo(sendBytes, endPoint);
|
||||
socket.Close();
|
||||
|
||||
logger.Debug($"UDP socket send data package to device {endPoint.Address.ToString()}:{endPoint.Port.ToString()}:");
|
||||
logger.Debug($" Original Data: {BitConverter.ToString(pkg.ToBytes()).Replace("-", " ")}");
|
||||
// logger.Debug($"UDP socket send data package to device {endPoint.Address.ToString()}:{endPoint.Port.ToString()}:");
|
||||
// logger.Debug($" Original Data: {BitConverter.ToString(pkg.ToBytes()).Replace("-", " ")}");
|
||||
|
||||
if (sendLen == sendBytes.Length) { return true; }
|
||||
else { return false; }
|
||||
@@ -177,21 +177,22 @@ public class UDPClientPool
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// 读取设备地址数据
|
||||
/// </summary>
|
||||
/// <param name="endPoint">[TODO:parameter]</param>
|
||||
/// <param name="devAddr">[TODO:parameter]</param>
|
||||
/// <param name="timeout">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
/// <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>> ReadAddr(
|
||||
IPEndPoint endPoint, uint devAddr, int timeout = 1000)
|
||||
IPEndPoint endPoint, int taskID, uint devAddr, int timeout = 1000)
|
||||
{
|
||||
var ret = false;
|
||||
var opts = new SendAddrPackOptions();
|
||||
|
||||
opts.BurstType = BurstType.FixedBurst;
|
||||
opts.BurstLength = 0;
|
||||
opts.CommandID = 0;
|
||||
opts.CommandID = Convert.ToByte(taskID);
|
||||
opts.Address = devAddr;
|
||||
|
||||
// Read Jtag State Register
|
||||
@@ -204,7 +205,7 @@ public class UDPClientPool
|
||||
return new(new Exception("Message Bus not Working!"));
|
||||
|
||||
var retPack = await MsgBus.UDPServer.WaitForDataAsync(
|
||||
endPoint.Address.ToString(), endPoint.Port, timeout);
|
||||
endPoint.Address.ToString(), taskID, endPoint.Port, timeout);
|
||||
if (!retPack.IsSuccessful) return new(retPack.Error);
|
||||
else if (!retPack.Value.IsSuccessful)
|
||||
return new(new Exception("Send address package failed"));
|
||||
@@ -217,20 +218,21 @@ public class UDPClientPool
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// 读取设备地址数据并校验结果
|
||||
/// </summary>
|
||||
/// <param name="endPoint">[TODO:parameter]</param>
|
||||
/// <param name="devAddr">[TODO:parameter]</param>
|
||||
/// <param name="result">[TODO:parameter]</param>
|
||||
/// <param name="resultMask">[TODO:parameter]</param>
|
||||
/// <param name="timeout">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="devAddr">设备地址</param>
|
||||
/// <param name="result">期望的结果值</param>
|
||||
/// <param name="resultMask">结果掩码,用于位校验</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns>校验结果,true表示数据匹配期望值</returns>
|
||||
public static async ValueTask<Result<bool>> ReadAddr(
|
||||
IPEndPoint endPoint, 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 ret = await ReadAddr(endPoint, devAddr, timeout);
|
||||
var ret = await ReadAddr(endPoint, taskID, devAddr, timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
if (!ret.Value.IsSuccessful)
|
||||
return new(new Exception($"Read device {address} address {devAddr} failed"));
|
||||
@@ -255,16 +257,17 @@ public class UDPClientPool
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// 读取设备地址数据并等待直到结果匹配或超时
|
||||
/// </summary>
|
||||
/// <param name="endPoint">[TODO:parameter]</param>
|
||||
/// <param name="devAddr">[TODO:parameter]</param>
|
||||
/// <param name="result">[TODO:parameter]</param>
|
||||
/// <param name="resultMask">[TODO:parameter]</param>
|
||||
/// <param name="timeout">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="devAddr">设备地址</param>
|
||||
/// <param name="result">期望的结果值</param>
|
||||
/// <param name="resultMask">结果掩码,用于位校验</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns>校验结果,true表示在超时前数据匹配期望值</returns>
|
||||
public static async ValueTask<Result<bool>> ReadAddrWithWait(
|
||||
IPEndPoint endPoint, 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();
|
||||
|
||||
@@ -277,7 +280,7 @@ public class UDPClientPool
|
||||
|
||||
try
|
||||
{
|
||||
var ret = await ReadAddr(endPoint, devAddr, Convert.ToInt32(timeleft.TotalMilliseconds));
|
||||
var ret = await ReadAddr(endPoint, taskID, devAddr, Convert.ToInt32(timeleft.TotalMilliseconds));
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
if (!ret.Value.IsSuccessful)
|
||||
return new(new Exception($"Read device {address} address {devAddr} failed"));
|
||||
@@ -302,24 +305,206 @@ public class UDPClientPool
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从设备地址读取字节数组数据(支持大数据量分段传输)
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="devAddr">设备地址</param>
|
||||
/// <param name="dataLength">要读取的数据长度(4字节)</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns>读取结果,包含接收到的字节数组</returns>
|
||||
public static async ValueTask<Result<byte[]>> ReadAddr4Bytes(
|
||||
IPEndPoint endPoint, int taskID, UInt32 devAddr, int dataLength, int timeout = 1000)
|
||||
{
|
||||
var ret = false;
|
||||
var opts = new SendAddrPackOptions();
|
||||
var resultData = new List<byte>();
|
||||
|
||||
opts.BurstType = BurstType.FixedBurst;
|
||||
opts.CommandID = Convert.ToByte(taskID);
|
||||
opts.IsWrite = false;
|
||||
|
||||
// Check Msg Bus
|
||||
if (!MsgBus.IsRunning)
|
||||
return new(new Exception("Message bus not working!"));
|
||||
|
||||
// Calculate read times and segments
|
||||
var max4BytesPerRead = 0x80; // 512 bytes per read
|
||||
var rest4Bytes = dataLength % max4BytesPerRead;
|
||||
var readTimes = (rest4Bytes != 0) ?
|
||||
(dataLength / max4BytesPerRead + 1) :
|
||||
(dataLength / max4BytesPerRead);
|
||||
|
||||
for (var i = 0; i < readTimes; i++)
|
||||
{
|
||||
// Calculate current segment size
|
||||
var isLastSegment = i == readTimes - 1;
|
||||
var currentSegmentSize = (isLastSegment && rest4Bytes != 0) ? rest4Bytes : max4BytesPerRead;
|
||||
|
||||
// Set burst length (in 32-bit words)
|
||||
opts.BurstLength = (byte)(currentSegmentSize - 1);
|
||||
|
||||
// Update address for current segment
|
||||
opts.Address = devAddr + (uint)(i * max4BytesPerRead);
|
||||
|
||||
// Send read address package
|
||||
ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts));
|
||||
if (!ret) return new(new Exception($"Send address package failed at segment {i}!"));
|
||||
|
||||
// Wait for data response
|
||||
var retPack = await MsgBus.UDPServer.WaitForDataAsync(
|
||||
endPoint.Address.ToString(), taskID, endPoint.Port, timeout);
|
||||
if (!retPack.IsSuccessful) return new(retPack.Error);
|
||||
|
||||
if (!retPack.Value.IsSuccessful)
|
||||
return new(new Exception($"Read address package failed at segment {i}"));
|
||||
|
||||
var retPackOpts = retPack.Value.Options;
|
||||
if (retPackOpts.Data is null)
|
||||
return new(new Exception($"Data is null at segment {i}, package: {retPackOpts.ToString()}"));
|
||||
|
||||
// Validate received data length
|
||||
if (retPackOpts.Data.Length != currentSegmentSize * 4)
|
||||
return new(new Exception($"Expected {currentSegmentSize * 4} bytes but received {retPackOpts.Data.Length} bytes at segment {i}"));
|
||||
|
||||
// Add received data to result
|
||||
resultData.AddRange(retPackOpts.Data);
|
||||
}
|
||||
|
||||
// Validate total data length
|
||||
if (resultData.Count != dataLength * 4)
|
||||
return new(new Exception($"Expected total {dataLength * 4} bytes but received {resultData.Count} bytes"));
|
||||
|
||||
return resultData.ToArray();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// 从设备地址读取字节数组数据(支持大数据量分段传输,先发送所有地址包再接收所有数据包)
|
||||
/// </summary>
|
||||
/// <param name="endPoint">[TODO:parameter]</param>
|
||||
/// <param name="devAddr">[TODO:parameter]</param>
|
||||
/// <param name="data">[TODO:parameter]</param>
|
||||
/// <param name="timeout">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="devAddr">设备地址</param>
|
||||
/// <param name="dataLength">要读取的数据长度(4字节)</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns>读取结果,包含接收到的字节数组</returns>
|
||||
public static async ValueTask<Result<byte[]>> ReadAddr4BytesAsync(
|
||||
IPEndPoint endPoint, int taskID, UInt32 devAddr, int dataLength, int timeout = 1000)
|
||||
{
|
||||
var optsList = new List<SendAddrPackOptions>();
|
||||
var resultData = new List<byte>();
|
||||
|
||||
// Check Msg Bus
|
||||
if (!MsgBus.IsRunning)
|
||||
return new(new Exception("Message bus not working!"));
|
||||
|
||||
// Prepare options for each segment
|
||||
var max4BytesPerRead = 0x80; // 512 bytes per read
|
||||
var rest4Bytes = dataLength % max4BytesPerRead;
|
||||
var readTimes = (rest4Bytes != 0) ?
|
||||
(dataLength / max4BytesPerRead + 1) :
|
||||
(dataLength / max4BytesPerRead);
|
||||
|
||||
for (var i = 0; i < readTimes; i++)
|
||||
{
|
||||
var isLastSegment = i == readTimes - 1;
|
||||
var currentSegmentSize = (isLastSegment && rest4Bytes != 0) ? rest4Bytes : max4BytesPerRead;
|
||||
|
||||
var opts = new SendAddrPackOptions
|
||||
{
|
||||
BurstType = BurstType.FixedBurst,
|
||||
CommandID = Convert.ToByte(taskID),
|
||||
IsWrite = false,
|
||||
BurstLength = (byte)(currentSegmentSize - 1),
|
||||
Address = devAddr + (uint)(i * max4BytesPerRead)
|
||||
};
|
||||
optsList.Add(opts);
|
||||
}
|
||||
|
||||
// Send all address packages first, but keep outstanding < 512
|
||||
int sentCount = 0;
|
||||
var startTime = DateTime.Now;
|
||||
while (sentCount < optsList.Count)
|
||||
{
|
||||
// Check how many data packets have been received
|
||||
var elapsed = DateTime.Now - startTime;
|
||||
if (elapsed >= TimeSpan.FromMilliseconds(timeout))
|
||||
break;
|
||||
var timeleft = timeout - (int)elapsed.TotalMilliseconds;
|
||||
|
||||
var found = await MsgBus.UDPServer.GetDataCountAsync(endPoint.Address.ToString(), taskID, timeleft);
|
||||
int outstanding = sentCount - (found.HasValue ? found.Value : 0);
|
||||
|
||||
// If outstanding >= 512, wait for some data to be received
|
||||
if (outstanding >= 512)
|
||||
continue;
|
||||
|
||||
// Send next address package
|
||||
var ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(optsList[sentCount]));
|
||||
if (!ret) return new(new Exception($"Send address package failed at segment {sentCount}!"));
|
||||
sentCount++;
|
||||
}
|
||||
|
||||
// Wait until enough data is received or timeout
|
||||
startTime = DateTime.Now;
|
||||
List<UDPData>? udpDatas = null;
|
||||
while (true)
|
||||
{
|
||||
var elapsed = DateTime.Now - startTime;
|
||||
if (elapsed >= TimeSpan.FromMilliseconds(timeout)) break;
|
||||
var timeleft = timeout - (int)elapsed.TotalMilliseconds;
|
||||
|
||||
var found = await MsgBus.UDPServer.GetDataCountAsync(endPoint.Address.ToString(), taskID, timeleft);
|
||||
if (found.HasValue && found.Value >= readTimes)
|
||||
{
|
||||
var dataArr = await MsgBus.UDPServer.FindDataArrayAsync(endPoint.Address.ToString(), taskID, timeleft);
|
||||
if (dataArr.HasValue)
|
||||
{
|
||||
udpDatas = dataArr.Value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (udpDatas is null || udpDatas.Count < readTimes)
|
||||
return new(new Exception($"Expected {readTimes} UDP data packets but received {udpDatas?.Count ?? 0}"));
|
||||
|
||||
// Collect and validate all received data
|
||||
for (var i = 0; i < udpDatas.Count; i++)
|
||||
{
|
||||
var bytes = udpDatas[i].Data;
|
||||
var expectedLen = ((optsList[i].BurstLength + 1) * 4);
|
||||
if (bytes.Length != expectedLen)
|
||||
return new(new Exception($"Expected {expectedLen} bytes but received {bytes.Length} bytes at segment {i}"));
|
||||
resultData.AddRange(bytes);
|
||||
}
|
||||
|
||||
// Validate total data length
|
||||
if (resultData.Count != dataLength * 4)
|
||||
return new(new Exception($"Expected total {dataLength * 4} bytes but received {resultData.Count} bytes"));
|
||||
|
||||
return resultData.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向设备地址写入32位数据
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="devAddr">设备地址</param>
|
||||
/// <param name="data">要写入的32位数据</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns>写入结果,true表示写入成功</returns>
|
||||
public static async ValueTask<Result<bool>> WriteAddr(
|
||||
IPEndPoint endPoint, UInt32 devAddr, UInt32 data, int timeout = 1000)
|
||||
IPEndPoint endPoint, int taskID, UInt32 devAddr, UInt32 data, int timeout = 1000)
|
||||
{
|
||||
var ret = false;
|
||||
var opts = new SendAddrPackOptions();
|
||||
|
||||
opts.BurstType = BurstType.FixedBurst;
|
||||
opts.BurstLength = 0;
|
||||
opts.CommandID = 0;
|
||||
opts.CommandID = Convert.ToByte(taskID);
|
||||
opts.Address = devAddr;
|
||||
|
||||
// Write Jtag State Register
|
||||
@@ -337,28 +522,30 @@ public class UDPClientPool
|
||||
|
||||
// Wait for Write Ack
|
||||
var udpWriteAck = await MsgBus.UDPServer.WaitForAckAsync(
|
||||
endPoint.Address.ToString(), endPoint.Port, timeout);
|
||||
endPoint.Address.ToString(), taskID, endPoint.Port, timeout);
|
||||
if (!udpWriteAck.IsSuccessful) return new(udpWriteAck.Error);
|
||||
|
||||
return udpWriteAck.Value.IsSuccessful;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// 向设备地址写入字节数组数据(支持大数据量分段传输)
|
||||
/// </summary>
|
||||
/// <param name="endPoint">[TODO:parameter]</param>
|
||||
/// <param name="devAddr">[TODO:parameter]</param>
|
||||
/// <param name="dataArray">[TODO:parameter]</param>
|
||||
/// <param name="timeout">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public static async ValueTask<Result<bool>> WriteAddr(IPEndPoint endPoint, UInt32 devAddr, byte[] dataArray, int timeout = 1000)
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="devAddr">设备地址</param>
|
||||
/// <param name="dataArray">要写入的字节数组</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns>写入结果,true表示写入成功</returns>
|
||||
public static async ValueTask<Result<bool>> WriteAddr(
|
||||
IPEndPoint endPoint, int taskID, UInt32 devAddr, byte[] dataArray, int timeout = 1000)
|
||||
{
|
||||
var ret = false;
|
||||
var opts = new SendAddrPackOptions();
|
||||
|
||||
|
||||
opts.BurstType = BurstType.FixedBurst;
|
||||
opts.CommandID = 0;
|
||||
opts.CommandID = Convert.ToByte(taskID);
|
||||
opts.Address = devAddr;
|
||||
|
||||
// Check Msg Bus
|
||||
@@ -374,13 +561,17 @@ public class UDPClientPool
|
||||
{
|
||||
// Sperate Data Array
|
||||
var isLastData = i == writeTimes - 1;
|
||||
var sendDataArray =
|
||||
isLastData ?
|
||||
var sendDataArray = isLastData ?
|
||||
dataArray[(i * (256 * (32 / 8)))..] :
|
||||
dataArray[(i * (256 * (32 / 8)))..((i + 1) * (256 * (32 / 8)))];
|
||||
|
||||
// Write Jtag State Register
|
||||
opts.BurstLength = ((byte)(sendDataArray.Length / 4 - 1));
|
||||
// Calculate BurstLength
|
||||
opts.BurstLength = ((byte)(
|
||||
sendDataArray.Length % 4 == 0 ?
|
||||
(sendDataArray.Length / 4 - 1) :
|
||||
(sendDataArray.Length / 4)
|
||||
));
|
||||
|
||||
ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts));
|
||||
if (!ret) return new(new Exception("Send 1st address package failed!"));
|
||||
|
||||
@@ -389,7 +580,7 @@ public class UDPClientPool
|
||||
if (!ret) return new(new Exception("Send data package failed!"));
|
||||
|
||||
// Wait for Write Ack
|
||||
var udpWriteAck = await MsgBus.UDPServer.WaitForAckAsync(endPoint.Address.ToString(), endPoint.Port, timeout);
|
||||
var udpWriteAck = await MsgBus.UDPServer.WaitForAckAsync(endPoint.Address.ToString(), taskID, endPoint.Port, timeout);
|
||||
if (!udpWriteAck.IsSuccessful) return new(udpWriteAck.Error);
|
||||
|
||||
if (!udpWriteAck.Value.IsSuccessful)
|
||||
@@ -398,5 +589,4 @@ public class UDPClientPool
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,11 @@ public class UDPData
|
||||
/// 发送来源的端口号
|
||||
/// </summary>
|
||||
public required int Port { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 任务ID
|
||||
/// </summary>
|
||||
public required int TaskID { get; set; }
|
||||
/// <summary>
|
||||
/// 接受到的数据
|
||||
/// </summary>
|
||||
@@ -44,6 +49,7 @@ public class UDPData
|
||||
DateTime = this.DateTime,
|
||||
Address = new string(this.Address),
|
||||
Port = this.Port,
|
||||
TaskID = this.TaskID,
|
||||
Data = cloneData,
|
||||
HasRead = this.HasRead
|
||||
};
|
||||
@@ -66,7 +72,9 @@ public class UDPServer
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
private static Dictionary<string, Queue<UDPData>> udpData = new Dictionary<string, Queue<UDPData>>();
|
||||
private Dictionary<string, Queue<UDPData>> udpData = new Dictionary<string, Queue<UDPData>>();
|
||||
|
||||
private Semaphore taskPool = new Semaphore(3, 3);
|
||||
|
||||
private int listenPort;
|
||||
private UdpClient listener;
|
||||
@@ -119,6 +127,7 @@ public class UDPServer
|
||||
/// 异步寻找目标发送的内容
|
||||
/// </summary>
|
||||
/// <param name="ipAddr"> 目标IP地址 </param>
|
||||
/// <param name="taskID">[TODO:parameter]</param>
|
||||
/// <param name="timeout">超时时间</param>
|
||||
/// <param name="cycle">延迟时间</param>
|
||||
/// <param name="callerName">调用函数名称</param>
|
||||
@@ -129,13 +138,14 @@ public class UDPServer
|
||||
/// Optional 存在时,为最先收到的数据
|
||||
/// </returns>
|
||||
public async ValueTask<Optional<UDPData>> FindDataAsync(
|
||||
string ipAddr, int timeout = 1000, int cycle = 0,
|
||||
string ipAddr, int taskID, int timeout = 1000, int cycle = 0,
|
||||
[CallerMemberName] string callerName = "",
|
||||
[CallerLineNumber] int callerLineNum = 0)
|
||||
[CallerLineNumber] int callerLineNum = 0
|
||||
)
|
||||
{
|
||||
UDPData? data = null;
|
||||
|
||||
logger.Debug($"Caller \"{callerName}|{callerLineNum}\": Try to find {ipAddr} UDP Data");
|
||||
// logger.Debug($"Caller \"{callerName}|{callerLineNum}\": Try to find {ipAddr}-{taskID} UDP Data");
|
||||
|
||||
var startTime = DateTime.Now;
|
||||
var isTimeout = false;
|
||||
@@ -149,12 +159,12 @@ public class UDPServer
|
||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
||||
using (await udpData.AcquireWriteLockAsync(timeleft))
|
||||
{
|
||||
if (udpData.ContainsKey(ipAddr) &&
|
||||
udpData.TryGetValue(ipAddr, out var dataQueue) &&
|
||||
if (udpData.ContainsKey($"{ipAddr}-{taskID}") &&
|
||||
udpData.TryGetValue($"{ipAddr}-{taskID}", out var dataQueue) &&
|
||||
dataQueue.Count > 0)
|
||||
{
|
||||
data = dataQueue.Dequeue();
|
||||
logger.Debug($"Find UDP Data: {data.ToString()}");
|
||||
// logger.Debug($"Find UDP Data: {data.ToString()}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -174,13 +184,13 @@ public class UDPServer
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取还未被读取的数据列表
|
||||
/// 异步寻找目标发送的所有内容,并清空队列
|
||||
/// </summary>
|
||||
/// <param name="ipAddr">IP地址</param>
|
||||
/// <param name="ipAddr">目标IP地址</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="timeout">超时时间</param>
|
||||
/// <param name="cycle">延迟时间</param>
|
||||
/// <returns>数据列表</returns>
|
||||
public async ValueTask<Optional<List<UDPData>>> GetDataArrayAsync(string ipAddr, int timeout = 1000, int cycle = 0)
|
||||
/// <returns>异步Optional 数据包列表</returns>
|
||||
public async ValueTask<Optional<List<UDPData>>> FindDataArrayAsync(string ipAddr, int taskID, int timeout = 1000)
|
||||
{
|
||||
List<UDPData>? data = null;
|
||||
|
||||
@@ -194,14 +204,14 @@ public class UDPServer
|
||||
if (isTimeout) break;
|
||||
|
||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
||||
using (await udpData.AcquireReadLockAsync(timeleft))
|
||||
using (await udpData.AcquireWriteLockAsync(timeleft))
|
||||
{
|
||||
if (udpData.ContainsKey(ipAddr) &&
|
||||
udpData.TryGetValue(ipAddr, out var dataQueue) &&
|
||||
if (udpData.ContainsKey($"{ipAddr}-{taskID}") &&
|
||||
udpData.TryGetValue($"{ipAddr}-{taskID}", out var dataQueue) &&
|
||||
dataQueue.Count > 0)
|
||||
{
|
||||
data = dataQueue.ToList();
|
||||
logger.Debug($"Find UDP Data Array: {JsonConvert.SerializeObject(data)}");
|
||||
dataQueue.Clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -218,17 +228,106 @@ public class UDPServer
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取还未被读取的数据列表
|
||||
/// </summary>
|
||||
/// <param name="ipAddr">IP地址</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="timeout">超时时间</param>
|
||||
/// <returns>数据列表</returns>
|
||||
public async ValueTask<Optional<List<UDPData>>> GetDataArrayAsync(string ipAddr, int taskID, int timeout = 1000)
|
||||
{
|
||||
List<UDPData>? data = null;
|
||||
|
||||
var startTime = DateTime.Now;
|
||||
var isTimeout = false;
|
||||
var timeleft = TimeSpan.FromMilliseconds(timeout);
|
||||
while (!isTimeout)
|
||||
{
|
||||
var elapsed = DateTime.Now - startTime;
|
||||
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
|
||||
if (isTimeout) break;
|
||||
|
||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
||||
using (await udpData.AcquireReadLockAsync(timeleft))
|
||||
{
|
||||
if (udpData.ContainsKey($"{ipAddr}-{taskID}") &&
|
||||
udpData.TryGetValue($"{ipAddr}-{taskID}", out var dataQueue) &&
|
||||
dataQueue.Count > 0)
|
||||
{
|
||||
data = dataQueue.ToList();
|
||||
// logger.Debug($"Find UDP Data Array: {JsonConvert.SerializeObject(data)}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (data is null)
|
||||
{
|
||||
logger.Trace("Get nothing even after time out");
|
||||
return new(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new(data);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取指定IP和任务ID的数据队列长度
|
||||
/// </summary>
|
||||
/// <param name="ipAddr">IP地址</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="timeout">超时时间</param>
|
||||
/// <returns>数据队列长度</returns>
|
||||
public async ValueTask<Optional<int>> GetDataCountAsync(string ipAddr, int taskID, int timeout = 1000)
|
||||
{
|
||||
int? count = null;
|
||||
|
||||
var startTime = DateTime.Now;
|
||||
var isTimeout = false;
|
||||
var timeleft = TimeSpan.FromMilliseconds(timeout);
|
||||
while (!isTimeout)
|
||||
{
|
||||
var elapsed = DateTime.Now - startTime;
|
||||
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
|
||||
if (isTimeout) break;
|
||||
|
||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
||||
using (await udpData.AcquireReadLockAsync(timeleft))
|
||||
{
|
||||
if (udpData.ContainsKey($"{ipAddr}-{taskID}") &&
|
||||
udpData.TryGetValue($"{ipAddr}-{taskID}", out var dataQueue))
|
||||
{
|
||||
count = dataQueue.Count;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count is null)
|
||||
{
|
||||
logger.Trace("Get nothing even after time out");
|
||||
return Optional<int>.None;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new(count.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步等待写响应
|
||||
/// </summary>
|
||||
/// <param name="address">IP地址</param>
|
||||
/// <param name="taskID">[TODO:parameter]</param>
|
||||
/// <param name="port">UDP 端口</param>
|
||||
/// <param name="timeout">超时时间范围</param>
|
||||
/// <returns>接收响应包</returns>
|
||||
public async ValueTask<Result<WebProtocol.RecvRespPackage>> WaitForAckAsync
|
||||
(string address, int port = -1, int timeout = 1000)
|
||||
(string address, int taskID, int port = -1, int timeout = 1000)
|
||||
{
|
||||
var data = await FindDataAsync(address, timeout);
|
||||
var data = await FindDataAsync(address, taskID, timeout);
|
||||
if (!data.HasValue)
|
||||
return new(new Exception("Get None even after time out!"));
|
||||
|
||||
@@ -247,13 +346,14 @@ public class UDPServer
|
||||
/// 异步等待数据
|
||||
/// </summary>
|
||||
/// <param name="address">IP地址</param>
|
||||
/// <param name="taskID">[TODO:parameter]</param>
|
||||
/// <param name="port">UDP 端口</param>
|
||||
/// <param name="timeout">超时时间范围</param>
|
||||
/// <returns>接收数据包</returns>
|
||||
public async ValueTask<Result<RecvDataPackage>> WaitForDataAsync
|
||||
(string address, int port = -1, int timeout = 1000)
|
||||
(string address, int taskID, int port = -1, int timeout = 1000)
|
||||
{
|
||||
var data = await FindDataAsync(address, timeout);
|
||||
var data = await FindDataAsync(address, taskID, timeout);
|
||||
if (!data.HasValue)
|
||||
return new(new Exception("Get None even after time out!"));
|
||||
|
||||
@@ -274,21 +374,23 @@ public class UDPServer
|
||||
var remoteEP = new IPEndPoint(IPAddress.Any, listenPort);
|
||||
byte[] bytes = listener.EndReceive(res, ref remoteEP);
|
||||
|
||||
// 提前开始接收下一个包
|
||||
listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);
|
||||
|
||||
// Handle RemoteEP
|
||||
if (remoteEP is null)
|
||||
{
|
||||
logger.Debug($"Receive Data from Unknown at {DateTime.Now.ToString()}:");
|
||||
logger.Debug($" Original Data : {BitConverter.ToString(bytes).Replace("-", " ")}");
|
||||
goto BEGIN_RECEIVE;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Handle Package
|
||||
var udpData = RecordUDPData(bytes, remoteEP);
|
||||
PrintData(udpData);
|
||||
|
||||
BEGIN_RECEIVE:
|
||||
listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);
|
||||
// 异步处理数据包
|
||||
Task.Run(() =>
|
||||
{
|
||||
var udpData = RecordUDPData(bytes, remoteEP, Convert.ToInt32(bytes[1]));
|
||||
PrintData(udpData);
|
||||
});
|
||||
}
|
||||
|
||||
private bool SendBytes(IPEndPoint endPoint, byte[] buf)
|
||||
@@ -308,7 +410,7 @@ public class UDPServer
|
||||
else { return false; }
|
||||
}
|
||||
|
||||
private UDPData RecordUDPData(byte[] bytes, IPEndPoint remoteEP)
|
||||
private UDPData RecordUDPData(byte[] bytes, IPEndPoint remoteEP, int taskID)
|
||||
{
|
||||
var remoteAddress = remoteEP.Address.ToString();
|
||||
var remotePort = remoteEP.Port;
|
||||
@@ -316,6 +418,7 @@ public class UDPServer
|
||||
{
|
||||
Address = remoteAddress,
|
||||
Port = remotePort,
|
||||
TaskID = taskID,
|
||||
Data = bytes,
|
||||
DateTime = DateTime.Now,
|
||||
HasRead = false,
|
||||
@@ -324,7 +427,8 @@ public class UDPServer
|
||||
using (udpData.AcquireWriteLock())
|
||||
{
|
||||
// Record UDP Receive Data
|
||||
if (udpData.ContainsKey(remoteAddress) && udpData.TryGetValue(remoteAddress, out var dataQueue))
|
||||
if (udpData.ContainsKey($"{remoteAddress}-{taskID}") &&
|
||||
udpData.TryGetValue($"{remoteAddress}-{taskID}", out var dataQueue))
|
||||
{
|
||||
dataQueue.Enqueue(data);
|
||||
logger.Trace("Receive data from old client");
|
||||
@@ -333,7 +437,7 @@ public class UDPServer
|
||||
{
|
||||
var queue = new Queue<UDPData>();
|
||||
queue.Enqueue(data);
|
||||
udpData.Add(remoteAddress, queue);
|
||||
udpData.Add($"{remoteAddress}-{taskID}", queue);
|
||||
logger.Trace("Receive data from new client");
|
||||
}
|
||||
}
|
||||
@@ -380,9 +484,9 @@ public class UDPServer
|
||||
recvData = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
logger.Debug($"Receive Data from {data.Address}:{data.Port} at {data.DateTime.ToString()}:");
|
||||
logger.Debug($" Original Data : {BitConverter.ToString(bytes).Replace("-", " ")}");
|
||||
if (recvData.Length != 0) logger.Debug($" Decoded Data : {recvData}");
|
||||
// logger.Debug($"Receive Data from {data.Address}:{data.Port} at {data.DateTime.ToString()}:");
|
||||
// logger.Debug($" Original Data : {BitConverter.ToString(bytes).Replace("-", " ")}");
|
||||
// if (recvData.Length != 0) logger.Debug($" Decoded Data : {recvData}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -393,13 +497,13 @@ public class UDPServer
|
||||
{
|
||||
using (udpData.AcquireReadLock())
|
||||
{
|
||||
logger.Debug("Ready Data:");
|
||||
// logger.Debug("Ready Data:");
|
||||
|
||||
foreach (var ip in udpData)
|
||||
{
|
||||
foreach (var data in ip.Value)
|
||||
{
|
||||
logger.Debug(data.ToString());
|
||||
// logger.Debug(data.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -409,13 +513,14 @@ public class UDPServer
|
||||
/// 清空指定IP地址的数据
|
||||
/// </summary>
|
||||
/// <param name="ipAddr">IP地址</param>
|
||||
/// <param name="taskID">[TODO:parameter]</param>
|
||||
/// <returns>无</returns>
|
||||
public async Task ClearUDPData(string ipAddr)
|
||||
public async Task ClearUDPData(string ipAddr, int taskID)
|
||||
{
|
||||
using (await udpData.AcquireWriteLockAsync())
|
||||
{
|
||||
if (udpData.ContainsKey(ipAddr) &&
|
||||
udpData.TryGetValue(ipAddr, out var dataQueue) &&
|
||||
if (udpData.ContainsKey($"{ipAddr}-{taskID}") &&
|
||||
udpData.TryGetValue($"{ipAddr}-{taskID}", out var dataQueue) &&
|
||||
dataQueue.Count > 0)
|
||||
{
|
||||
dataQueue.Clear();
|
||||
|
||||
@@ -269,6 +269,9 @@ namespace WebProtocol
|
||||
if (bodyData.Length > 256 * (32 / 8))
|
||||
throw new Exception("The data of SendDataPackage can't over 256 * 32bits");
|
||||
|
||||
if (bodyData.Length % 4 != 0)
|
||||
throw new Exception("The data of SendDataPackage should be divided by 4");
|
||||
|
||||
this.bodyData = bodyData;
|
||||
|
||||
_ = _reserved;
|
||||
|
||||
1491
src/APIClient.ts
1491
src/APIClient.ts
File diff suppressed because it is too large
Load Diff
14
src/App.vue
14
src/App.vue
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import Navbar from "./components/Navbar.vue";
|
||||
import Dialog from "./components/Dialog.vue";
|
||||
import { Alert, useAlertProvider } from "./components/Alert";
|
||||
import { ref, provide, computed, onMounted } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
|
||||
@@ -49,19 +50,26 @@ provide("theme", {
|
||||
const currentRoutePath = computed(() => {
|
||||
return router.currentRoute.value.path;
|
||||
});
|
||||
|
||||
useAlertProvider();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<header class="relative">
|
||||
<Navbar></Navbar>
|
||||
<Dialog></Dialog>
|
||||
<Navbar />
|
||||
<Dialog />
|
||||
<Alert />
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<RouterView />
|
||||
</main>
|
||||
<footer v-if="currentRoutePath != '/project'" class="footer footer-center p-4 bg-base-300 text-base-content">
|
||||
|
||||
<footer
|
||||
v-if="currentRoutePath != '/project'"
|
||||
class="footer footer-center p-4 bg-base-300 text-base-content"
|
||||
>
|
||||
<div>
|
||||
<p>Copyright © 2023 - All right reserved by OurEDA</p>
|
||||
</div>
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1741694797806" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2622" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M192 192m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0Z" p-id="2623"></path><path d="M192 512m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0Z" p-id="2624"></path><path d="M192 832m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0Z" p-id="2625"></path><path d="M864 160H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32zM864 480H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32zM864 800H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32z" p-id="2626"></path></svg>
|
||||
|
Before Width: | Height: | Size: 870 B |
@@ -1 +0,0 @@
|
||||
<svg t="1741522876251" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3628" width="200" height="200"><path d="M327.04 85.333333h369.92C841.472 85.333333 938.666667 186.794667 938.666667 337.749333v348.501334C938.666667 837.205333 841.472 938.666667 696.874667 938.666667h-229.546667a32 32 0 0 1 0-64h229.546667c107.989333 0 177.792-73.941333 177.792-188.416V337.749333c0-114.474667-69.802667-188.416-177.749334-188.416H327.04C219.093333 149.333333 149.333333 223.274667 149.333333 337.749333v348.501334c0 114.474667 69.76 188.416 177.706667 188.416a32 32 0 0 1 0 64C182.442667 938.666667 85.333333 837.205333 85.333333 686.250667V337.749333C85.333333 186.794667 182.442667 85.333333 327.04 85.333333z m-51.114667 381.098667a31.914667 31.914667 0 0 1 42.325334-16.042667 31.914667 31.914667 0 0 1 16.042666 42.282667A47.061333 47.061333 0 1 0 424.192 512c0-25.898667-21.077333-46.933333-47.018667-46.933333a32 32 0 0 1 0-64c50.048 0 91.904 33.408 105.728 78.933333h242.858667a32 32 0 0 1 32 32v79.018667a32 32 0 0 1-64 0V544h-56.704v47.018667a32 32 0 0 1-64 0V544h-90.154667a110.72 110.72 0 0 1-105.728 79.018667 111.104 111.104 0 0 1-101.248-156.544z" fill="#200E32" p-id="3629"></path></svg>
|
||||
|
Before Width: | Height: | Size: 1.2 KiB |
@@ -1 +0,0 @@
|
||||
<svg t="1741522263287" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2626" width="200" height="200"><path d="M511.913993 941.605241c-255.612968 0-385.311608-57.452713-385.311608-170.810012 0-80.846632 133.654964-133.998992 266.621871-151.88846L393.224257 602.049387c-79.986561-55.904586-118.86175-153.436587-118.86175-297.240383 0-139.33143 87.211154-222.586259 233.423148-222.586259l7.912649 0c146.211994 0 233.423148 83.254829 233.423148 222.586259 0 54.184445 0 214.67361-117.829666 297.412397l-0.344028 16.685369c132.966907 18.061482 266.105829 71.041828 266.105829 151.716445C897.225601 884.152528 767.526961 941.605241 511.913993 941.605241zM507.957668 141.567613c-79.470519 0-174.250294 28.382328-174.250294 163.241391 0 129.698639 34.230808 213.469511 104.584579 255.784982 8.944734 5.332437 14.277171 14.965228 14.277171 25.286074l0 59.344868c0 15.309256-11.524945 28.0383-26.662187 29.414413-144.319839 14.449185-239.959684 67.429531-239.959684 95.983874 0 92.199563 177.346548 111.637158 325.966739 111.637158 148.792206 0 325.966739-19.26558 325.966739-111.637158 0-28.726356-95.639845-81.534688-239.959684-95.983874-15.48127-1.548127-27.006215-14.621199-26.662187-30.102469l1.376113-59.344868c0.172014-10.148833 5.676466-19.437594 14.277171-24.770032 70.525785-42.487485 103.208466-123.678145 103.208466-255.784982 0-135.031077-94.779775-163.241391-174.250294-163.241391L507.957668 141.567613 507.957668 141.567613z" fill="#575B66" p-id="2627"></path></svg>
|
||||
|
Before Width: | Height: | Size: 1.5 KiB |
102
src/components/Alert/Alert.vue
Normal file
102
src/components/Alert/Alert.vue
Normal file
@@ -0,0 +1,102 @@
|
||||
<template>
|
||||
<div class="fixed left-1/2 top-30 z-50 -translate-x-1/2">
|
||||
<transition
|
||||
name="alert"
|
||||
enter-active-class="alert-enter-active"
|
||||
leave-active-class="alert-leave-active"
|
||||
enter-from-class="alert-enter-from"
|
||||
enter-to-class="alert-enter-to"
|
||||
leave-from-class="alert-leave-from"
|
||||
leave-to-class="alert-leave-to"
|
||||
>
|
||||
<div
|
||||
v-if="alertStore?.alertState.value.visible"
|
||||
:class="alertClasses"
|
||||
class="alert"
|
||||
>
|
||||
<div class="flex items-center gap-2">
|
||||
<!-- Icons for different alert types -->
|
||||
<CheckCircle
|
||||
v-if="alertStore?.alertState.value.type === 'success'"
|
||||
class="h-6 w-6 shrink-0 stroke-current"
|
||||
/>
|
||||
<XCircle
|
||||
v-else-if="alertStore?.alertState.value.type === 'error'"
|
||||
class="h-6 w-6 shrink-0 stroke-current"
|
||||
/>
|
||||
<AlertTriangle
|
||||
v-else-if="alertStore?.alertState.value.type === 'warning'"
|
||||
class="h-6 w-6 shrink-0 stroke-current"
|
||||
/>
|
||||
<Info v-else class="h-6 w-6 shrink-0 stroke-current" />
|
||||
<span>{{ alertStore?.alertState.value.message }}</span>
|
||||
</div>
|
||||
<div class="flex-none">
|
||||
<button
|
||||
class="btn btn-sm btn-circle btn-ghost"
|
||||
@click="alertStore?.hide"
|
||||
>
|
||||
<X class="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</transition>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
import { CheckCircle, XCircle, AlertTriangle, Info, X } from "lucide-vue-next";
|
||||
import { useAlertStore } from ".";
|
||||
|
||||
const alertStore = useAlertStore();
|
||||
|
||||
// Computed classes for different alert types
|
||||
const alertClasses = computed(() => {
|
||||
const baseClasses = "shadow-lg max-w-sm";
|
||||
|
||||
switch (alertStore?.alertState.value.type) {
|
||||
case "success":
|
||||
return `${baseClasses} alert-success`;
|
||||
case "error":
|
||||
return `${baseClasses} alert-error`;
|
||||
case "warning":
|
||||
return `${baseClasses} alert-warning`;
|
||||
case "info":
|
||||
default:
|
||||
return `${baseClasses} alert-info`;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
/* 进入和离开的过渡动画持续时间 */
|
||||
.alert-enter-active,
|
||||
.alert-leave-active {
|
||||
transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
|
||||
}
|
||||
|
||||
/* 进入的起始状态 */
|
||||
.alert-enter-from {
|
||||
opacity: 0;
|
||||
transform: translateY(-20px) scale(0.95);
|
||||
}
|
||||
|
||||
/* 进入的结束状态 */
|
||||
.alert-enter-to {
|
||||
opacity: 1;
|
||||
transform: translateY(0) scale(1);
|
||||
}
|
||||
|
||||
/* 离开的起始状态 */
|
||||
.alert-leave-from {
|
||||
opacity: 1;
|
||||
transform: translateY(0) scale(1);
|
||||
}
|
||||
|
||||
/* 离开的结束状态 */
|
||||
.alert-leave-to {
|
||||
opacity: 0;
|
||||
transform: translateY(-10px) scale(0.98);
|
||||
}
|
||||
</style>
|
||||
82
src/components/Alert/index.ts
Normal file
82
src/components/Alert/index.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { ref, computed } from "vue";
|
||||
import Alert from "./Alert.vue";
|
||||
import { createInjectionState } from "@vueuse/core";
|
||||
|
||||
export interface AlertState {
|
||||
visible: boolean;
|
||||
message: string;
|
||||
type: "success" | "error" | "warning" | "info";
|
||||
}
|
||||
|
||||
// create injectivon state using vueuse
|
||||
const [useAlertProvider, useAlertStore] = createInjectionState(() => {
|
||||
const alertState = ref<AlertState>({
|
||||
visible: false,
|
||||
message: "",
|
||||
type: "info",
|
||||
});
|
||||
|
||||
let timeoutId: number | null = null;
|
||||
|
||||
function show(
|
||||
message: string,
|
||||
type: AlertState["type"] = "info",
|
||||
duration = 2000,
|
||||
) {
|
||||
// Clear existing timeout
|
||||
if (timeoutId) {
|
||||
window.clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
alertState.value = {
|
||||
visible: true,
|
||||
message,
|
||||
type,
|
||||
};
|
||||
|
||||
// Auto hide after duration
|
||||
if (duration > 0) {
|
||||
timeoutId = window.setTimeout(() => {
|
||||
hide();
|
||||
}, duration);
|
||||
}
|
||||
}
|
||||
|
||||
function hide() {
|
||||
alertState.value.visible = false;
|
||||
if (timeoutId) {
|
||||
window.clearTimeout(timeoutId);
|
||||
timeoutId = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Convenience methods for different alert types
|
||||
function error(message: string, duration = 2000) {
|
||||
show(message, "error", duration);
|
||||
}
|
||||
|
||||
function info(message: string, duration = 2000) {
|
||||
show(message, "info", duration);
|
||||
}
|
||||
|
||||
function warn(message: string, duration = 2000) {
|
||||
show(message, "warning", duration);
|
||||
}
|
||||
|
||||
function success(message: string, duration = 2000) {
|
||||
show(message, "success", duration);
|
||||
}
|
||||
|
||||
return {
|
||||
alertState,
|
||||
show,
|
||||
hide,
|
||||
error,
|
||||
info,
|
||||
warn,
|
||||
success,
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
export { Alert, useAlertProvider, useAlertStore };
|
||||
94
src/components/InputField/BaseInputField.vue
Normal file
94
src/components/InputField/BaseInputField.vue
Normal file
@@ -0,0 +1,94 @@
|
||||
<template>
|
||||
<div class="form-control">
|
||||
<label class="label" v-if="label || icon">
|
||||
<component :is="icon" class="w-4 h-4" v-if="icon" />
|
||||
<span class="label-text" v-if="label">{{ label }}</span>
|
||||
</label>
|
||||
|
||||
<div class="input-group">
|
||||
<input
|
||||
:type="type"
|
||||
:placeholder="placeholder"
|
||||
:class="inputClasses"
|
||||
:value="modelValue"
|
||||
@input="handleInput"
|
||||
@blur="handleBlur"
|
||||
v-bind="$attrs"
|
||||
/>
|
||||
<slot name="suffix"></slot>
|
||||
</div>
|
||||
|
||||
<label class="label" v-if="error">
|
||||
<span class="label-text-alt text-error">{{ error }}</span>
|
||||
</label>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
|
||||
interface Props {
|
||||
modelValue?: string | number
|
||||
label?: string
|
||||
placeholder?: string
|
||||
error?: string
|
||||
type?: 'text' | 'number' | 'email' | 'password'
|
||||
size?: 'xs' | 'sm' | 'md' | 'lg'
|
||||
variant?: 'default' | 'bordered' | 'ghost'
|
||||
icon?: any
|
||||
disabled?: boolean
|
||||
readonly?: boolean
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
type: 'text',
|
||||
size: 'md',
|
||||
variant: 'bordered'
|
||||
})
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: string | number]
|
||||
'blur': [event: FocusEvent]
|
||||
}>()
|
||||
|
||||
defineOptions({
|
||||
inheritAttrs: false
|
||||
})
|
||||
|
||||
const inputClasses = computed(() => {
|
||||
const baseClasses = ['input', 'flex-1']
|
||||
|
||||
// 添加变体样式
|
||||
if (props.variant === 'bordered') baseClasses.push('input-bordered')
|
||||
else if (props.variant === 'ghost') baseClasses.push('input-ghost')
|
||||
|
||||
// 添加尺寸样式
|
||||
if (props.size === 'xs') baseClasses.push('input-xs')
|
||||
else if (props.size === 'sm') baseClasses.push('input-sm')
|
||||
else if (props.size === 'lg') baseClasses.push('input-lg')
|
||||
|
||||
// 添加错误样式
|
||||
if (props.error) baseClasses.push('input-error')
|
||||
|
||||
// 添加状态样式
|
||||
if (props.disabled) baseClasses.push('input-disabled')
|
||||
|
||||
return baseClasses
|
||||
})
|
||||
|
||||
const handleInput = (event: Event) => {
|
||||
const target = event.target as HTMLInputElement
|
||||
let value: string | number = target.value
|
||||
|
||||
// 如果是数字类型,转换为数字
|
||||
if (props.type === 'number' && value !== '') {
|
||||
value = Number(value)
|
||||
}
|
||||
|
||||
emit('update:modelValue', value)
|
||||
}
|
||||
|
||||
const handleBlur = (event: FocusEvent) => {
|
||||
emit('blur', event)
|
||||
}
|
||||
</script>
|
||||
75
src/components/InputField/IpInputField.vue
Normal file
75
src/components/InputField/IpInputField.vue
Normal file
@@ -0,0 +1,75 @@
|
||||
<template>
|
||||
<BaseInputField
|
||||
v-model="value"
|
||||
:label="label"
|
||||
:placeholder="placeholder || '192.168.1.100'"
|
||||
:error="validationError"
|
||||
:icon="icon || Globe"
|
||||
type="text"
|
||||
v-bind="$attrs"
|
||||
@blur="validateOnBlur"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import { z } from 'zod'
|
||||
import { Globe } from 'lucide-vue-next'
|
||||
import BaseInputField from './BaseInputField.vue'
|
||||
|
||||
interface Props {
|
||||
modelValue: string
|
||||
label?: string
|
||||
placeholder?: string
|
||||
icon?: any
|
||||
required?: boolean
|
||||
validateOnBlur?: boolean
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
label: 'IP 地址',
|
||||
validateOnBlur: true
|
||||
})
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: string]
|
||||
}>()
|
||||
|
||||
defineOptions({
|
||||
inheritAttrs: false
|
||||
})
|
||||
|
||||
const hasBlurred = ref(false)
|
||||
|
||||
const value = computed({
|
||||
get: () => props.modelValue,
|
||||
set: (val) => emit('update:modelValue', val)
|
||||
})
|
||||
|
||||
// IP地址验证模式
|
||||
const ipSchema = z.string().ip({
|
||||
version: 'v4',
|
||||
message: '请输入有效的IPv4地址'
|
||||
})
|
||||
|
||||
const validationError = computed(() => {
|
||||
// 如果是必填且为空
|
||||
if (props.required && !props.modelValue) {
|
||||
return '请输入IP地址'
|
||||
}
|
||||
|
||||
// 如果有值但格式不正确,并且设置了在失焦时验证且已经失焦过
|
||||
if (props.modelValue && (!props.validateOnBlur || hasBlurred.value)) {
|
||||
const result = ipSchema.safeParse(props.modelValue)
|
||||
return result.success ? '' : result.error.errors[0]?.message || '无效的IP地址'
|
||||
}
|
||||
|
||||
return ''
|
||||
})
|
||||
|
||||
const validateOnBlur = () => {
|
||||
if (props.validateOnBlur) {
|
||||
hasBlurred.value = true
|
||||
}
|
||||
}
|
||||
</script>
|
||||
81
src/components/InputField/PortInputField.vue
Normal file
81
src/components/InputField/PortInputField.vue
Normal file
@@ -0,0 +1,81 @@
|
||||
<template>
|
||||
<BaseInputField
|
||||
v-model="value"
|
||||
:label="label"
|
||||
:placeholder="placeholder || '8080'"
|
||||
:error="validationError"
|
||||
:icon="icon || Network"
|
||||
type="number"
|
||||
v-bind="$attrs"
|
||||
@blur="validateOnBlur"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import { z } from 'zod'
|
||||
import { Network } from 'lucide-vue-next'
|
||||
import BaseInputField from './BaseInputField.vue'
|
||||
|
||||
interface Props {
|
||||
modelValue: number
|
||||
label?: string
|
||||
placeholder?: string
|
||||
icon?: any
|
||||
required?: boolean
|
||||
validateOnBlur?: boolean
|
||||
min?: number
|
||||
max?: number
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
label: '端口',
|
||||
validateOnBlur: true,
|
||||
min: 1,
|
||||
max: 65535
|
||||
})
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: number]
|
||||
}>()
|
||||
|
||||
defineOptions({
|
||||
inheritAttrs: false
|
||||
})
|
||||
|
||||
const hasBlurred = ref(false)
|
||||
|
||||
const value = computed({
|
||||
get: () => props.modelValue,
|
||||
set: (val) => emit('update:modelValue', Number(val))
|
||||
})
|
||||
|
||||
// 端口验证模式
|
||||
const portSchema = computed(() =>
|
||||
z.number()
|
||||
.int('端口必须是整数')
|
||||
.min(props.min, `端口必须大于等于${props.min}`)
|
||||
.max(props.max, `端口必须小于等于${props.max}`)
|
||||
)
|
||||
|
||||
const validationError = computed(() => {
|
||||
// 如果是必填且为空
|
||||
if (props.required && (!props.modelValue && props.modelValue !== 0)) {
|
||||
return '请输入端口号'
|
||||
}
|
||||
|
||||
// 如果有值但格式不正确,并且设置了在失焦时验证且已经失焦过
|
||||
if ((props.modelValue || props.modelValue === 0) && (!props.validateOnBlur || hasBlurred.value)) {
|
||||
const result = portSchema.value.safeParse(props.modelValue)
|
||||
return result.success ? '' : result.error.errors[0]?.message || '无效的端口号'
|
||||
}
|
||||
|
||||
return ''
|
||||
})
|
||||
|
||||
const validateOnBlur = () => {
|
||||
if (props.validateOnBlur) {
|
||||
hasBlurred.value = true
|
||||
}
|
||||
}
|
||||
</script>
|
||||
3
src/components/InputField/index.ts
Normal file
3
src/components/InputField/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { default as BaseInputField } from './BaseInputField.vue'
|
||||
export { default as IpInputField } from './IpInputField.vue'
|
||||
export { default as PortInputField } from './PortInputField.vue'
|
||||
@@ -2,177 +2,106 @@
|
||||
<div>
|
||||
<!-- 元器件选择菜单 (Drawer) -->
|
||||
<div class="drawer drawer-end z-50">
|
||||
<input id="component-drawer" type="checkbox" class="drawer-toggle" v-model="showComponentsMenu" />
|
||||
<input
|
||||
id="component-drawer"
|
||||
type="checkbox"
|
||||
class="drawer-toggle"
|
||||
v-model="showComponentsMenu"
|
||||
/>
|
||||
<div class="drawer-side">
|
||||
<label for="component-drawer" aria-label="close sidebar" class="drawer-overlay !bg-opacity-50"></label>
|
||||
<div class="menu p-0 w-[460px] min-h-full bg-base-100 shadow-xl flex flex-col">
|
||||
<label
|
||||
for="component-drawer"
|
||||
aria-label="close sidebar"
|
||||
class="drawer-overlay !bg-opacity-50"
|
||||
></label>
|
||||
<div
|
||||
class="menu p-0 w-[460px] min-h-full bg-base-100 shadow-xl flex flex-col"
|
||||
>
|
||||
<!-- 菜单头部 -->
|
||||
<div class="p-6 border-b border-base-300 flex justify-between items-center">
|
||||
<div
|
||||
class="p-6 border-b bg-base-200 border-base-300 flex justify-between items-center"
|
||||
>
|
||||
<h3 class="text-xl font-bold text-primary flex items-center gap-2">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
|
||||
class="text-primary">
|
||||
<circle cx="12" cy="12" r="10"></circle>
|
||||
<path d="M12 8v8"></path>
|
||||
<path d="M8 12h8"></path>
|
||||
</svg>
|
||||
<Plus :size="20" class="text-primary" />
|
||||
添加元器件
|
||||
</h3>
|
||||
<label for="component-drawer" class="btn btn-ghost btn-sm btn-circle" @click="closeMenu">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<line x1="18" y1="6" x2="6" y2="18"></line>
|
||||
<line x1="6" y1="6" x2="18" y2="18"></line>
|
||||
</svg>
|
||||
<label
|
||||
for="component-drawer"
|
||||
class="btn btn-ghost btn-sm btn-circle"
|
||||
@click="closeMenu"
|
||||
>
|
||||
<X :size="20" />
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<!-- 导航栏 -->
|
||||
<div class="tabs tabs-boxed bg-base-200 mx-6 mt-4 rounded-box">
|
||||
<a class="tab" :class="{ 'tab-active': activeTab === 'components' }"
|
||||
@click="activeTab = 'components'">元器件</a>
|
||||
<a class="tab" :class="{ 'tab-active': activeTab === 'templates' }" @click="activeTab = 'templates'">模板</a>
|
||||
<a class="tab" :class="{ 'tab-active': activeTab === 'virtual' }" @click="activeTab = 'virtual'">虚拟外设</a>
|
||||
<a
|
||||
class="tab"
|
||||
:class="{ 'tab-active': activeTab === 'components' }"
|
||||
@click="activeTab = 'components'"
|
||||
>元器件</a
|
||||
>
|
||||
<a
|
||||
class="tab"
|
||||
:class="{ 'tab-active': activeTab === 'templates' }"
|
||||
@click="activeTab = 'templates'"
|
||||
>模板</a
|
||||
>
|
||||
<a
|
||||
class="tab"
|
||||
:class="{ 'tab-active': activeTab === 'virtual' }"
|
||||
@click="activeTab = 'virtual'"
|
||||
>虚拟外设</a
|
||||
>
|
||||
</div>
|
||||
|
||||
<!-- 搜索框 -->
|
||||
<div class="px-6 py-4 border-b border-base-300">
|
||||
<div class="join w-full">
|
||||
<div class="join-item flex-1 relative">
|
||||
<input type="text" placeholder="搜索..." class="input input-bordered input-sm w-full pl-10"
|
||||
v-model="searchQuery" @keyup.enter="searchComponents" />
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
|
||||
class="absolute left-3 top-1/2 -translate-y-1/2 text-base-content opacity-60">
|
||||
<circle cx="11" cy="11" r="8"></circle>
|
||||
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
|
||||
</svg>
|
||||
</div>
|
||||
<button class="btn btn-sm join-item" @click="searchComponents">
|
||||
搜索
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 元器件列表 (组件选项卡) -->
|
||||
<div v-if="activeTab === 'components'" class="px-6 py-4 overflow-auto flex-1">
|
||||
<div v-if="filteredComponents.length > 0" class="grid grid-cols-2 gap-4">
|
||||
<div v-for="(component, index) in filteredComponents" :key="index"
|
||||
class="card bg-base-200 hover:bg-base-300 transition-all duration-300 hover:shadow-md cursor-pointer"
|
||||
@click="addComponent(component)">
|
||||
<div class="card-body p-3 items-center text-center">
|
||||
<div
|
||||
class="bg-base-100 rounded-lg w-full h-[90px] flex items-center justify-center overflow-hidden p-2">
|
||||
<!-- 直接使用组件作为预览 -->
|
||||
<component v-if="componentModules[component.type]" :is="componentModules[component.type].default"
|
||||
class="component-preview" :size="getPreviewSize(component.type)" />
|
||||
<!-- 加载中状态 -->
|
||||
<span v-else class="text-xs text-gray-400">加载中...</span>
|
||||
</div>
|
||||
<h3 class="card-title text-sm mt-2">{{ component.name }}</h3>
|
||||
<p class="text-xs opacity-70">{{ component.type }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 无搜索结果 -->
|
||||
<div v-else class="py-16 text-center">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"
|
||||
class="mx-auto text-base-300 mb-3">
|
||||
<circle cx="11" cy="11" r="8"></circle>
|
||||
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
|
||||
<line x1="8" y1="11" x2="14" y2="11"></line>
|
||||
</svg>
|
||||
<p class="text-base-content opacity-70">没有找到匹配的元器件</p>
|
||||
<button class="btn btn-sm btn-ghost mt-3" @click="searchQuery = ''">
|
||||
清除搜索
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 模板列表 (模板选项卡) -->
|
||||
<div v-if="activeTab === 'templates'" class="px-6 py-4 overflow-auto flex-1">
|
||||
<div v-if="filteredTemplates.length > 0" class="grid grid-cols-2 gap-4">
|
||||
<div v-for="(template, index) in filteredTemplates" :key="index"
|
||||
class="card bg-base-200 hover:bg-base-300 transition-all duration-300 hover:shadow-md cursor-pointer"
|
||||
@click="addTemplate(template)">
|
||||
<div class="card-body p-3 items-center text-center">
|
||||
<div
|
||||
class="bg-base-100 rounded-lg w-full h-[90px] flex items-center justify-center overflow-hidden p-2">
|
||||
<img :src="template.thumbnailUrl || '/placeholder-template.png'
|
||||
" alt="Template thumbnail" class="max-h-full max-w-full object-contain" />
|
||||
</div>
|
||||
<h3 class="card-title text-sm mt-2">{{ template.name }}</h3>
|
||||
<p class="text-xs opacity-70">
|
||||
{{ template.description || "模板" }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 无搜索结果 -->
|
||||
<div v-else class="py-16 text-center">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"
|
||||
class="mx-auto text-base-300 mb-3">
|
||||
<circle cx="11" cy="11" r="8"></circle>
|
||||
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
|
||||
<line x1="8" y1="11" x2="14" y2="11"></line>
|
||||
</svg>
|
||||
<p class="text-base-content opacity-70">没有找到匹配的模板</p>
|
||||
<button class="btn btn-sm btn-ghost mt-3" @click="searchQuery = ''">
|
||||
清除搜索
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 虚拟外设列表 (虚拟外设选项卡) -->
|
||||
<div v-if="activeTab === 'virtual'" class="px-6 py-4 overflow-auto flex-1">
|
||||
<div v-if="filteredVirtualDevices.length > 0" class="grid grid-cols-2 gap-4">
|
||||
<div v-for="(device, index) in filteredVirtualDevices" :key="index"
|
||||
class="card bg-base-200 hover:bg-base-300 transition-all duration-300 hover:shadow-md cursor-pointer"
|
||||
@click="addComponent(device)">
|
||||
<div class="card-body p-3 items-center text-center">
|
||||
<div
|
||||
class="bg-base-100 rounded-lg w-full h-[90px] flex items-center justify-center overflow-hidden p-2">
|
||||
<!-- 直接使用组件作为预览 -->
|
||||
<component v-if="componentModules[device.type]" :is="componentModules[device.type].default"
|
||||
class="component-preview" :size="getPreviewSize(device.type)" />
|
||||
<!-- 加载中状态 -->
|
||||
<span v-else class="text-xs text-gray-400">加载中...</span>
|
||||
</div>
|
||||
<h3 class="card-title text-sm mt-2">{{ device.name }}</h3>
|
||||
<p class="text-xs opacity-70">{{ device.type }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 无搜索结果 -->
|
||||
<div v-else class="py-16 text-center">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"
|
||||
class="mx-auto text-base-300 mb-3">
|
||||
<circle cx="11" cy="11" r="8"></circle>
|
||||
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
|
||||
<line x1="8" y1="11" x2="14" y2="11"></line>
|
||||
</svg>
|
||||
<p class="text-base-content opacity-70">没有找到匹配的虚拟外设</p>
|
||||
<button class="btn btn-sm btn-ghost mt-3" @click="searchQuery = ''">
|
||||
清除搜索
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 底部操作区 -->
|
||||
<div class="p-4 border-t border-base-300 bg-base-200 flex justify-between">
|
||||
<label for="component-drawer" class="btn btn-sm btn-ghost" @click="closeMenu">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="mr-1">
|
||||
<path d="M19 12H5M12 19l-7-7 7-7"></path>
|
||||
</svg>
|
||||
返回
|
||||
</label>
|
||||
<label for="component-drawer" class="btn btn-sm btn-primary" @click="closeMenu">
|
||||
完成
|
||||
<div class="px-6 py-4 w-full">
|
||||
<label class="input w-full">
|
||||
<Search :size="16" class="h-[1em] opacity-50" />
|
||||
<input
|
||||
type="text"
|
||||
placeholder="搜索..."
|
||||
class="grow"
|
||||
v-model="searchQuery"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<!-- 统一的项目列表 -->
|
||||
<ItemList
|
||||
v-if="activeTab === 'components'"
|
||||
:items="availableComponents"
|
||||
:search-query="searchQuery"
|
||||
:component-modules="componentModules"
|
||||
:no-results-message="'没有找到匹配的元器件'"
|
||||
item-type="component"
|
||||
@item-click="addComponent"
|
||||
@clear-search="searchQuery = ''"
|
||||
/>
|
||||
|
||||
<ItemList
|
||||
v-if="activeTab === 'templates'"
|
||||
:items="availableTemplates"
|
||||
:search-query="searchQuery"
|
||||
:component-modules="componentModules"
|
||||
:no-results-message="'没有找到匹配的模板'"
|
||||
item-type="template"
|
||||
@item-click="addTemplate"
|
||||
@clear-search="searchQuery = ''"
|
||||
/>
|
||||
|
||||
<ItemList
|
||||
v-if="activeTab === 'virtual'"
|
||||
:items="availableVirtualDevices"
|
||||
:search-query="searchQuery"
|
||||
:component-modules="componentModules"
|
||||
:no-results-message="'没有找到匹配的虚拟外设'"
|
||||
item-type="virtual"
|
||||
@item-click="addComponent"
|
||||
@clear-search="searchQuery = ''"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -181,8 +110,18 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, shallowRef, onMounted } from "vue";
|
||||
import motherboardSvg from "@/components/equipments/svg/motherboard.svg";
|
||||
import buttonSvg from "@/components//equipments/svg/button.svg";
|
||||
import { Plus, X, Search } from "lucide-vue-next";
|
||||
import ItemList from "./ItemList.vue";
|
||||
import {
|
||||
useComponentManager,
|
||||
availableComponents,
|
||||
availableVirtualDevices,
|
||||
availableTemplates,
|
||||
getAllComponentTypes,
|
||||
type ComponentConfig,
|
||||
type VirtualDeviceConfig,
|
||||
type TemplateConfig,
|
||||
} from "./index.ts";
|
||||
|
||||
// Props 定义
|
||||
interface Props {
|
||||
@@ -191,6 +130,8 @@ interface Props {
|
||||
|
||||
const props = defineProps<Props>();
|
||||
|
||||
const componentManager = useComponentManager();
|
||||
|
||||
// 定义组件发出的事件
|
||||
const emit = defineEmits([
|
||||
"close",
|
||||
@@ -205,45 +146,6 @@ const activeTab = ref("components");
|
||||
// --- 搜索功能 ---
|
||||
const searchQuery = ref("");
|
||||
|
||||
// --- 可用元器件列表 ---
|
||||
const availableComponents = [
|
||||
{ type: "MechanicalButton", name: "机械按钮" },
|
||||
{ type: "Switch", name: "开关" },
|
||||
{ type: "Pin", name: "引脚" },
|
||||
{ type: "SMT_LED", name: "贴片LED" },
|
||||
{ type: "SevenSegmentDisplay", name: "数码管" },
|
||||
{ type: "HDMI", name: "HDMI接口" },
|
||||
{ type: "DDR", name: "DDR内存" },
|
||||
{ type: "ETH", name: "以太网接口" },
|
||||
{ type: "SD", name: "SD卡插槽" },
|
||||
{ type: "SFP", name: "SFP光纤模块" },
|
||||
{ type: "SMA", name: "SMA连接器" },
|
||||
{ type: "MotherBoard", name: "主板" },
|
||||
{ type: "PG2L100H_FBG676", name: "PG2L100H FBG676芯片" },
|
||||
{ type: "BaseBoard", name: "通用底板" },
|
||||
];
|
||||
|
||||
// --- 可用虚拟外设列表 ---
|
||||
const availableVirtualDevices = [{ type: "DDS", name: "信号发生器" }];
|
||||
|
||||
// --- 可用模板列表 ---
|
||||
const availableTemplates = ref([
|
||||
{
|
||||
name: "PG2L100H 基础开发板",
|
||||
id: "PG2L100H_Pango100pro",
|
||||
description: "包含主板和两个LED的基本设置",
|
||||
path: "/EquipmentTemplates/PG2L100H_Pango100pro.json",
|
||||
thumbnailUrl: motherboardSvg,
|
||||
},
|
||||
{
|
||||
name: "矩阵键盘",
|
||||
id: "MatrixKey",
|
||||
description: "包含4x4,共16个按键的矩阵键盘",
|
||||
path: "/EquipmentTemplates/MatrixKey.json",
|
||||
thumbnailUrl: buttonSvg,
|
||||
},
|
||||
]);
|
||||
|
||||
// 显示/隐藏组件菜单
|
||||
const showComponentsMenu = computed({
|
||||
get: () => props.open,
|
||||
@@ -277,52 +179,15 @@ async function loadComponentModule(type: string) {
|
||||
|
||||
// 预加载组件模块
|
||||
async function preloadComponentModules() {
|
||||
// 加载基础组件
|
||||
for (const component of availableComponents) {
|
||||
const allTypes = getAllComponentTypes();
|
||||
|
||||
for (const type of allTypes) {
|
||||
try {
|
||||
await loadComponentModule(component.type);
|
||||
await loadComponentModule(type);
|
||||
} catch (error) {
|
||||
console.error(`Failed to preload component ${component.type}:`, error);
|
||||
console.error(`Failed to preload component ${type}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// 加载虚拟外设组件
|
||||
for (const device of availableVirtualDevices) {
|
||||
try {
|
||||
await loadComponentModule(device.type);
|
||||
} catch (error) {
|
||||
console.error(`Failed to preload virtual device ${device.type}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取组件预览时适合的尺寸
|
||||
function getPreviewSize(componentType: string): number {
|
||||
// 根据组件类型返回适当的预览尺寸
|
||||
const previewSizes: Record<string, number> = {
|
||||
MechanicalButton: 0.4, // 按钮较大,需要更小尺寸
|
||||
Switch: 0.35, // 开关较大,需要更小尺寸
|
||||
Pin: 0.8, // 引脚较小,可以大一些
|
||||
SMT_LED: 0.7, // LED可以保持适中
|
||||
SevenSegmentDisplay: 0.4, // 数码管较大,需要较小尺寸
|
||||
HDMI: 0.5, // HDMI接口较大
|
||||
DDR: 0.5, // DDR内存较大
|
||||
ETH: 0.5, // 以太网接口较大
|
||||
SD: 0.6, // SD卡插槽适中
|
||||
SFP: 0.4, // SFP光纤模块较大
|
||||
SMA: 0.7, // SMA连接器可以适中
|
||||
MotherBoard: 0.13, // 主板最大,需要最小尺寸
|
||||
DDS: 0.3, // 信号发生器较大,需要较小尺寸
|
||||
};
|
||||
|
||||
// 返回对应尺寸,如果没有特定配置则返回默认值0.5
|
||||
return previewSizes[componentType] || 0.5;
|
||||
}
|
||||
|
||||
// 搜索组件
|
||||
function searchComponents() {
|
||||
// 根据用户输入过滤可用组件列表
|
||||
// 实际逻辑已经在 filteredComponents 计算属性中实现
|
||||
}
|
||||
|
||||
// 关闭菜单
|
||||
@@ -332,7 +197,9 @@ function closeMenu() {
|
||||
}
|
||||
|
||||
// 添加新元器件
|
||||
async function addComponent(componentTemplate: { type: string; name: string }) {
|
||||
async function addComponent(
|
||||
componentTemplate: ComponentConfig | VirtualDeviceConfig,
|
||||
) {
|
||||
// 先加载组件模块
|
||||
const moduleRef = await loadComponentModule(componentTemplate.type);
|
||||
let defaultProps: Record<string, any> = {};
|
||||
@@ -365,7 +232,7 @@ async function addComponent(componentTemplate: { type: string; name: string }) {
|
||||
}
|
||||
|
||||
// 添加模板
|
||||
async function addTemplate(template: any) {
|
||||
async function addTemplate(template: TemplateConfig) {
|
||||
try {
|
||||
// 加载模板JSON文件
|
||||
const response = await fetch(template.path);
|
||||
@@ -381,7 +248,7 @@ async function addTemplate(template: any) {
|
||||
id: template.id,
|
||||
name: template.name,
|
||||
template: templateData,
|
||||
capsPage: template.capsPage
|
||||
capsPage: template.capsPage,
|
||||
});
|
||||
|
||||
// 关闭菜单
|
||||
@@ -392,46 +259,6 @@ async function addTemplate(template: any) {
|
||||
}
|
||||
}
|
||||
|
||||
// 过滤后的元器件列表 (用于菜单)
|
||||
const filteredComponents = computed(() => {
|
||||
if (!searchQuery.value || activeTab.value !== "components") {
|
||||
return availableComponents;
|
||||
}
|
||||
const query = searchQuery.value.toLowerCase();
|
||||
return availableComponents.filter(
|
||||
(component) =>
|
||||
component.name.toLowerCase().includes(query) ||
|
||||
component.type.toLowerCase().includes(query),
|
||||
);
|
||||
});
|
||||
|
||||
// 过滤后的模板列表 (用于菜单)
|
||||
const filteredTemplates = computed(() => {
|
||||
if (!searchQuery.value || activeTab.value !== "templates") {
|
||||
return availableTemplates.value;
|
||||
}
|
||||
const query = searchQuery.value.toLowerCase();
|
||||
return availableTemplates.value.filter(
|
||||
(template) =>
|
||||
template.name.toLowerCase().includes(query) ||
|
||||
(template.description &&
|
||||
template.description.toLowerCase().includes(query)),
|
||||
);
|
||||
});
|
||||
|
||||
// 过滤后的虚拟外设列表 (用于菜单)
|
||||
const filteredVirtualDevices = computed(() => {
|
||||
if (!searchQuery.value || activeTab.value !== "virtual") {
|
||||
return availableVirtualDevices;
|
||||
}
|
||||
const query = searchQuery.value.toLowerCase();
|
||||
return availableVirtualDevices.filter(
|
||||
(device) =>
|
||||
device.name.toLowerCase().includes(query) ||
|
||||
device.type.toLowerCase().includes(query),
|
||||
);
|
||||
});
|
||||
|
||||
// 生命周期钩子
|
||||
onMounted(() => {
|
||||
// 预加载组件模块
|
||||
@@ -440,13 +267,6 @@ onMounted(() => {
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
/* 组件预览样式 */
|
||||
.component-preview {
|
||||
max-width: 100%;
|
||||
max-height: 100%;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
/* 动画效果 */
|
||||
.animate-slideUp {
|
||||
animation: slideUp 0.3s ease-out forwards;
|
||||
|
||||
@@ -1,70 +1,96 @@
|
||||
<template>
|
||||
<div class="flex-1 h-full w-full bg-base-200 relative overflow-hidden diagram-container" ref="canvasContainer"
|
||||
@mousedown="handleCanvasMouseDown" @mousedown.middle.prevent="startMiddleDrag" @wheel.prevent="onZoom"
|
||||
@contextmenu.prevent="handleContextMenu">
|
||||
<div
|
||||
class="flex-1 h-full w-full bg-base-200 relative overflow-hidden diagram-container"
|
||||
ref="canvasContainer"
|
||||
@mousedown="handleCanvasMouseDown"
|
||||
@mousedown.middle.prevent="startMiddleDrag"
|
||||
@wheel.prevent="onZoom"
|
||||
@contextmenu.prevent="handleContextMenu"
|
||||
>
|
||||
<!-- 工具栏 -->
|
||||
<div class="absolute top-2 right-2 flex gap-2 z-30">
|
||||
<button class="btn btn-sm btn-primary" @click="openDiagramFileSelector">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24"
|
||||
stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M5 19a2 2 0 01-2-2V7a2 2 0 012-2h4l2 2h4a2 2 0 012 2v1M5 19h14a2 2 0 002-2v-5a2 2 0 00-2-2H9a2 2 0 00-2 2v5a2 2 0 01-2 2z" />
|
||||
</svg>
|
||||
<FolderOpen class="h-4 w-4 mr-1" />
|
||||
导入
|
||||
</button>
|
||||
<button class="btn btn-sm btn-primary" @click="exportDiagram">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24"
|
||||
stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" />
|
||||
</svg>
|
||||
<Download class="h-4 w-4 mr-1" />
|
||||
导出
|
||||
</button>
|
||||
<button class="btn btn-sm btn-primary" @click="emit('open-components')">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24"
|
||||
stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
|
||||
</svg>
|
||||
<Plus class="h-4 w-4 mr-1" />
|
||||
添加组件
|
||||
</button>
|
||||
<button class="btn btn-sm btn-primary" @click="emit('toggle-doc-panel')">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24"
|
||||
stroke="currentColor">
|
||||
<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>
|
||||
<FileText class="h-4 w-4 mr-1" />
|
||||
{{ props.showDocPanel ? "属性面板" : "文档" }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 隐藏的文件输入 -->
|
||||
<input type="file" ref="fileInput" class="hidden" accept=".json" @change="handleFileSelected" />
|
||||
<input
|
||||
type="file"
|
||||
ref="fileInput"
|
||||
class="hidden"
|
||||
accept=".json"
|
||||
@change="handleFileSelected"
|
||||
/>
|
||||
|
||||
<div ref="canvas" class="diagram-canvas" :style="{
|
||||
transform: `translate(${position.x}px, ${position.y}px) scale(${scale})`,
|
||||
}">
|
||||
<div
|
||||
ref="canvas"
|
||||
class="diagram-canvas"
|
||||
:style="{
|
||||
transform: `translate(${position.x}px, ${position.y}px) scale(${scale})`,
|
||||
}"
|
||||
>
|
||||
<!-- 渲染连线 -->
|
||||
<svg class="wires-layer" width="10000" height="10000">
|
||||
<!-- 已完成的连线 -->
|
||||
<WireComponent v-for="(wire, index) in wireItems" :key="wire.id" :id="wire.id" :start-x="wire.startX"
|
||||
:start-y="wire.startY" :end-x="wire.endX" :end-y="wire.endY" :stroke-color="wire.color || '#4a5568'"
|
||||
:stroke-width="wire.strokeWidth" :is-active="false" :start-component-id="wire.startComponentId"
|
||||
:start-pin-id="wire.startPinId" :end-component-id="wire.endComponentId" :end-pin-id="wire.endPinId"
|
||||
:routing-mode="wire.routingMode" :path-commands="wire.pathCommands" />
|
||||
<WireComponent
|
||||
v-for="(wire, index) in wireItems"
|
||||
:key="wire.id"
|
||||
:id="wire.id"
|
||||
:start-x="wire.startX"
|
||||
:start-y="wire.startY"
|
||||
:end-x="wire.endX"
|
||||
:end-y="wire.endY"
|
||||
:stroke-color="wire.color || '#4a5568'"
|
||||
:stroke-width="wire.strokeWidth"
|
||||
:is-active="false"
|
||||
:start-component-id="wire.startComponentId"
|
||||
:start-pin-id="wire.startPinId"
|
||||
:end-component-id="wire.endComponentId"
|
||||
:end-pin-id="wire.endPinId"
|
||||
:routing-mode="wire.routingMode"
|
||||
:path-commands="wire.pathCommands"
|
||||
/>
|
||||
|
||||
<!-- 正在创建的连线 -->
|
||||
<WireComponent v-if="isCreatingWire" id="temp-wire" :start-x="creatingWireStart.x"
|
||||
:start-y="creatingWireStart.y" :end-x="mousePosition.x" :end-y="mousePosition.y" stroke-color="#3182ce"
|
||||
:stroke-width="2" :is-active="true" />
|
||||
<WireComponent
|
||||
v-if="isCreatingWire"
|
||||
id="temp-wire"
|
||||
:start-x="creatingWireStart.x"
|
||||
:start-y="creatingWireStart.y"
|
||||
:end-x="mousePosition.x"
|
||||
:end-y="mousePosition.y"
|
||||
stroke-color="#3182ce"
|
||||
:stroke-width="2"
|
||||
:is-active="true"
|
||||
/>
|
||||
</svg>
|
||||
|
||||
<!-- 渲染画布上的组件 -->
|
||||
<div v-for="component in diagramParts" :key="component.id" class="component-wrapper" :class="{
|
||||
'component-hover': hoveredComponent === component.id,
|
||||
'component-selected': selectedComponentId === component.id,
|
||||
'component-disabled': !component.isOn,
|
||||
'component-hidepins': component.hidepins,
|
||||
}" :style="{
|
||||
<div
|
||||
v-for="component in diagramParts"
|
||||
:key="component.id"
|
||||
class="component-wrapper"
|
||||
:class="{
|
||||
'component-hover': hoveredComponent === component.id,
|
||||
'component-selected': selectedComponentId === component.id,
|
||||
'component-disabled': !component.isOn,
|
||||
'component-hidepins': component.hidepins,
|
||||
}"
|
||||
:style="{
|
||||
top: component.y + 'px',
|
||||
left: component.x + 'px',
|
||||
zIndex: component.index ?? 0,
|
||||
@@ -73,56 +99,74 @@
|
||||
: 'none',
|
||||
opacity: component.isOn ? 1 : 0.6,
|
||||
display: 'block',
|
||||
}" @mousedown.left.stop="startComponentDrag($event, component)" @mouseover="
|
||||
}"
|
||||
@mousedown.left.stop="startComponentDrag($event, component)"
|
||||
@mouseover="
|
||||
(event) => {
|
||||
hoveredComponent = component.id;
|
||||
}
|
||||
" @mouseleave="
|
||||
"
|
||||
@mouseleave="
|
||||
(event) => {
|
||||
hoveredComponent = null;
|
||||
}
|
||||
">
|
||||
"
|
||||
>
|
||||
<!-- 动态渲染组件 -->
|
||||
<component :is="componentManager.getComponentDefinition(component.type)"
|
||||
v-if="componentManager.componentModules.value[component.type] && componentManager.getComponentDefinition(component.type)"
|
||||
v-bind="componentManager.prepareComponentProps(component.attrs || {}, component.id)" @update:bindKey="
|
||||
<component
|
||||
:is="componentManager.getComponentDefinition(component.type)"
|
||||
v-if="
|
||||
componentManager.componentModules.value[component.type] &&
|
||||
componentManager.getComponentDefinition(component.type)
|
||||
"
|
||||
v-bind="
|
||||
componentManager.prepareComponentProps(
|
||||
component.attrs || {},
|
||||
component.id,
|
||||
)
|
||||
"
|
||||
@update:bindKey="
|
||||
(value: string) =>
|
||||
updateComponentProp(component.id, 'bindKey', value)
|
||||
" @pin-click="
|
||||
"
|
||||
@pin-click="
|
||||
(pinInfo: any) =>
|
||||
handlePinClick(component.id, pinInfo, pinInfo.originalEvent)
|
||||
" :ref="(el: any) => componentManager.setComponentRef(component.id, el)" />
|
||||
"
|
||||
:ref="(el: any) => componentManager.setComponentRef(component.id, el)"
|
||||
/>
|
||||
|
||||
<!-- Fallback if component module not loaded yet -->
|
||||
<div v-else
|
||||
class="p-2 text-xs text-gray-400 border border-dashed border-gray-400 flex items-center justify-center">
|
||||
<div
|
||||
v-else
|
||||
class="p-2 text-xs text-gray-400 border border-dashed border-gray-400 flex items-center justify-center"
|
||||
>
|
||||
<div class="flex flex-col items-center">
|
||||
<div class="loading loading-spinner loading-xs mb-1"></div>
|
||||
<span>Loading {{ component.type }}...</span>
|
||||
<small class="mt-1 text-xs">{{ componentManager.componentModules.value[component.type] ? 'Module loaded but invalid' : 'Module not found' }}</small>
|
||||
<small class="mt-1 text-xs">{{
|
||||
componentManager.componentModules.value[component.type]
|
||||
? "Module loaded but invalid"
|
||||
: "Module not found"
|
||||
}}</small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 通知组件 -->
|
||||
<div v-if="showNotification" class="toast toast-top toast-center z-50 w-fit-content">
|
||||
<div :class="`alert ${notificationType === 'success'
|
||||
? 'alert-success'
|
||||
: notificationType === 'error'
|
||||
? 'alert-error'
|
||||
: 'alert-info'
|
||||
}`">
|
||||
<span>{{ notificationMessage }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 加载指示器 -->
|
||||
<div v-if="isLoading" class="absolute inset-0 bg-black bg-opacity-30 flex items-center justify-center z-50">
|
||||
<div
|
||||
v-if="isLoading"
|
||||
class="absolute inset-0 bg-black bg-opacity-30 flex items-center justify-center z-50"
|
||||
>
|
||||
<div class="loading loading-spinner loading-lg text-primary"></div>
|
||||
</div>
|
||||
|
||||
<!-- 缩放指示器 -->
|
||||
<div class="absolute bottom-4 right-4 bg-base-100 px-3 py-1.5 rounded-md shadow-md z-20" style="opacity: 0.9">
|
||||
<div
|
||||
class="absolute bottom-4 right-4 bg-base-100 px-3 py-1.5 rounded-md shadow-md z-20"
|
||||
style="opacity: 0.9"
|
||||
>
|
||||
<span class="text-sm font-medium">{{ Math.round(scale * 100) }}%</span>
|
||||
</div>
|
||||
</div>
|
||||
@@ -139,7 +183,9 @@ import {
|
||||
provide,
|
||||
} from "vue";
|
||||
import { useEventListener } from "@vueuse/core";
|
||||
import { FolderOpen, Download, Plus, FileText } from "lucide-vue-next";
|
||||
import WireComponent from "@/components/equipments/Wire.vue";
|
||||
import { useAlertStore } from "@/components/Alert";
|
||||
|
||||
// 导入 diagram 管理器
|
||||
import {
|
||||
@@ -186,9 +232,14 @@ const props = defineProps<{
|
||||
// 获取componentManager实例
|
||||
const componentManager = useComponentManager();
|
||||
if (!componentManager) {
|
||||
throw new Error("DiagramCanvas must be used within a component manager provider");
|
||||
throw new Error(
|
||||
"DiagramCanvas must be used within a component manager provider",
|
||||
);
|
||||
}
|
||||
|
||||
// 获取Alert store实例
|
||||
const alertStore = useAlertStore();
|
||||
|
||||
// --- 画布状态 ---
|
||||
const canvasContainer = ref<HTMLElement | null>(null);
|
||||
const canvas = ref<HTMLElement | null>(null);
|
||||
@@ -215,7 +266,9 @@ const diagramData = ref<DiagramData>({
|
||||
});
|
||||
|
||||
// 组件引用跟踪(保留以便向后兼容)
|
||||
const componentRefs = computed(() => componentManager?.componentRefs.value || {});
|
||||
const componentRefs = computed(
|
||||
() => componentManager?.componentRefs.value || {},
|
||||
);
|
||||
|
||||
// 计算属性:从 diagramData 中提取组件列表,并按index属性排序
|
||||
const diagramParts = computed<DiagramPart[]>(() => {
|
||||
@@ -320,13 +373,6 @@ const mousePosition = reactive({ x: 0, y: 0 });
|
||||
// 加载状态
|
||||
const isLoading = ref(false);
|
||||
|
||||
// 通知状态
|
||||
const showNotification = ref(false);
|
||||
const notificationMessage = ref("");
|
||||
const notificationType = ref<"success" | "error" | "info">("info");
|
||||
// 保存toast定时器ID
|
||||
const toastTimers: number[] = [];
|
||||
|
||||
// 文件选择引用
|
||||
const fileInput = ref<HTMLInputElement | null>(null);
|
||||
|
||||
@@ -337,7 +383,7 @@ const isWireCreationEventActive = ref(false);
|
||||
|
||||
// 使用VueUse设置事件监听器
|
||||
// 画布拖拽事件
|
||||
useEventListener(document, 'mousemove', (e: MouseEvent) => {
|
||||
useEventListener(document, "mousemove", (e: MouseEvent) => {
|
||||
if (isDragEventActive.value) {
|
||||
onDrag(e);
|
||||
}
|
||||
@@ -349,7 +395,7 @@ useEventListener(document, 'mousemove', (e: MouseEvent) => {
|
||||
}
|
||||
});
|
||||
|
||||
useEventListener(document, 'mouseup', () => {
|
||||
useEventListener(document, "mouseup", () => {
|
||||
if (isDragEventActive.value) {
|
||||
stopDrag();
|
||||
}
|
||||
@@ -359,7 +405,7 @@ useEventListener(document, 'mouseup', () => {
|
||||
});
|
||||
|
||||
// 键盘事件
|
||||
useEventListener(window, 'keydown', handleKeyDown);
|
||||
useEventListener(window, "keydown", handleKeyDown);
|
||||
|
||||
// --- 缩放功能 ---
|
||||
const MIN_SCALE = 0.2;
|
||||
@@ -586,7 +632,7 @@ function onComponentDrag(e: MouseEvent) {
|
||||
function stopComponentDrag() {
|
||||
// 如果有组件被拖拽,保存当前状态
|
||||
if (draggingComponentId.value) {
|
||||
console.log(`组件拖拽结束: ${draggingComponentId.value}`);
|
||||
// console.log(`组件拖拽结束: ${draggingComponentId.value}`);
|
||||
|
||||
// 保存图表数据
|
||||
saveDiagramData(diagramData.value);
|
||||
@@ -893,7 +939,7 @@ function handleFileSelected(event: Event) {
|
||||
const validation = validateDiagramData(parsed);
|
||||
|
||||
if (!validation.isValid) {
|
||||
showToast(
|
||||
alertStore?.show(
|
||||
`不是有效的diagram.json格式: ${validation.errors.join("; ")}`,
|
||||
"error",
|
||||
);
|
||||
@@ -910,11 +956,11 @@ function handleFileSelected(event: Event) {
|
||||
// 发出更新事件
|
||||
emit("diagram-updated", diagramData.value);
|
||||
|
||||
showToast(`成功导入diagram文件`, "success");
|
||||
alertStore?.show(`成功导入diagram文件`, "success");
|
||||
} catch (error) {
|
||||
console.error("解析JSON文件出错:", error);
|
||||
if (document.body.contains(canvasContainer.value)) {
|
||||
showToast("解析文件出错,请确认是有效的JSON格式", "error");
|
||||
alertStore?.show("解析文件出错,请确认是有效的JSON格式", "error");
|
||||
}
|
||||
} finally {
|
||||
// 检查组件是否仍然挂载
|
||||
@@ -930,7 +976,7 @@ function handleFileSelected(event: Event) {
|
||||
reader.onerror = () => {
|
||||
// 检查组件是否仍然挂载
|
||||
if (document.body.contains(canvasContainer.value)) {
|
||||
showToast("读取文件时出错", "error");
|
||||
alertStore?.show("读取文件时出错", "error");
|
||||
isLoading.value = false;
|
||||
}
|
||||
// 清除文件输入
|
||||
@@ -956,46 +1002,21 @@ function exportDiagram() {
|
||||
a.download = "diagram.json";
|
||||
a.click();
|
||||
// 释放URL对象
|
||||
const timerId = setTimeout(() => {
|
||||
setTimeout(() => {
|
||||
URL.revokeObjectURL(url);
|
||||
// 检查组件是否仍然挂载
|
||||
if (document.body.contains(canvasContainer.value)) {
|
||||
isLoading.value = false;
|
||||
showToast("成功导出diagram文件", "success");
|
||||
alertStore?.show("成功导出diagram文件", "success");
|
||||
}
|
||||
}, 100);
|
||||
|
||||
// 将定时器ID保存起来,以便在组件卸载时清除
|
||||
toastTimers.push(timerId);
|
||||
} catch (error) {
|
||||
console.error("导出diagram文件时出错:", error);
|
||||
showToast("导出diagram文件时出错", "error");
|
||||
alertStore?.show("导出diagram文件时出错", "error");
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 显示通知
|
||||
function showToast(
|
||||
message: string,
|
||||
type: "success" | "error" | "info" = "info",
|
||||
duration = 3000,
|
||||
) {
|
||||
notificationMessage.value = message;
|
||||
notificationType.value = type;
|
||||
showNotification.value = true;
|
||||
|
||||
// 保存定时器ID以便清除
|
||||
const timerId = setTimeout(() => {
|
||||
// 检查组件是否仍然挂载
|
||||
if (document.body.contains(canvasContainer.value)) {
|
||||
showNotification.value = false;
|
||||
}
|
||||
}, duration);
|
||||
|
||||
// 将定时器ID保存起来,以便在组件卸载时清除
|
||||
toastTimers.push(timerId);
|
||||
}
|
||||
|
||||
// --- 生命周期钩子 ---
|
||||
onMounted(async () => {
|
||||
// 设置componentManager的画布引用
|
||||
@@ -1011,7 +1032,6 @@ onMounted(async () => {
|
||||
getCanvasPosition: () => ({ x: position.x, y: position.y }),
|
||||
getScale: () => scale.value,
|
||||
$el: canvasContainer.value,
|
||||
showToast
|
||||
};
|
||||
componentManager.setCanvasRef(canvasAPI);
|
||||
}
|
||||
@@ -1033,7 +1053,9 @@ onMounted(async () => {
|
||||
|
||||
// 直接通过componentManager预加载组件模块
|
||||
if (componentManager) {
|
||||
await componentManager.preloadComponentModules(Array.from(componentTypes));
|
||||
await componentManager.preloadComponentModules(
|
||||
Array.from(componentTypes),
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("加载图表数据失败:", error);
|
||||
@@ -1061,16 +1083,6 @@ function handleKeyDown(e: KeyboardEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
// 清除所有toast定时器
|
||||
toastTimers.forEach((timerId) => clearTimeout(timerId));
|
||||
|
||||
// 重置事件状态
|
||||
isDragEventActive.value = false;
|
||||
isComponentDragEventActive.value = false;
|
||||
isWireCreationEventActive.value = false;
|
||||
});
|
||||
|
||||
// 无加载动画的数据更新方法
|
||||
function updateDiagramDataDirectly(data: DiagramData) {
|
||||
// 检查组件是否仍然挂载
|
||||
@@ -1085,7 +1097,7 @@ function updateDiagramDataDirectly(data: DiagramData) {
|
||||
emit("diagram-updated", data);
|
||||
}
|
||||
|
||||
// 暴露方法给父组件 - 简化版本,主要用于数据访问
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
// 基本数据操作
|
||||
getDiagramData: () => diagramData.value,
|
||||
@@ -1112,24 +1124,18 @@ defineExpose({
|
||||
emit("diagram-updated", data);
|
||||
|
||||
// 短暂延迟后结束加载状态,以便UI能更新
|
||||
const timerId = setTimeout(() => {
|
||||
setTimeout(() => {
|
||||
// 检查组件是否仍然挂载
|
||||
if (document.body.contains(canvasContainer.value)) {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}, 200);
|
||||
|
||||
// 将定时器ID保存起来,以便在组件卸载时清除
|
||||
toastTimers.push(timerId);
|
||||
});
|
||||
},
|
||||
|
||||
// 画布状态
|
||||
getCanvasPosition: () => ({ x: position.x, y: position.y }),
|
||||
getScale: () => scale.value,
|
||||
|
||||
// 通知系统
|
||||
showToast,
|
||||
});
|
||||
|
||||
// 监视器 - 当图表数据更改时保存
|
||||
@@ -1254,7 +1260,13 @@ watch(
|
||||
-ms-user-select: none;
|
||||
}
|
||||
|
||||
.component-wrapper :deep(svg *:not([class*="interactive"]):not(rect.glow):not(circle[fill-opacity]):not([fill-opacity])) {
|
||||
.component-wrapper
|
||||
:deep(
|
||||
svg
|
||||
*:not([class*="interactive"]):not(rect.glow):not(
|
||||
circle[fill-opacity]
|
||||
):not([fill-opacity])
|
||||
) {
|
||||
pointer-events: none;
|
||||
/* 非交互元素不接收鼠标事件 */
|
||||
}
|
||||
|
||||
100
src/components/LabCanvas/ItemList.vue
Normal file
100
src/components/LabCanvas/ItemList.vue
Normal file
@@ -0,0 +1,100 @@
|
||||
<template>
|
||||
<div class="px-6 py-4 overflow-auto flex-1">
|
||||
<div v-if="filteredItems.length > 0" class="grid grid-cols-2 gap-4">
|
||||
<div
|
||||
v-for="(item, index) in filteredItems"
|
||||
:key="index"
|
||||
class="card bg-base-200 hover:bg-base-300 transition-all duration-300 hover:shadow-md cursor-pointer"
|
||||
@click="handleItemClick(item)"
|
||||
>
|
||||
<div class="card-body p-3 items-center text-center">
|
||||
<div
|
||||
class="bg-base-100 rounded-lg w-full h-[90px] flex items-center justify-center overflow-hidden p-2"
|
||||
>
|
||||
<!-- 组件预览 -->
|
||||
<component
|
||||
v-if="item.type && componentModules[item.type]"
|
||||
:is="componentModules[item.type].default"
|
||||
class="component-preview"
|
||||
:size="getPreviewSize(item.type)"
|
||||
/>
|
||||
<!-- 模板预览 -->
|
||||
<img
|
||||
v-else-if="item.thumbnailUrl"
|
||||
:src="item.thumbnailUrl || '/placeholder-template.png'"
|
||||
alt="Template thumbnail"
|
||||
class="max-h-full max-w-full object-contain"
|
||||
/>
|
||||
<!-- 加载中状态 -->
|
||||
<span v-else class="text-xs text-gray-400">加载中...</span>
|
||||
</div>
|
||||
<h3 class="card-title text-sm mt-2">{{ item.name }}</h3>
|
||||
<p class="text-xs opacity-70">
|
||||
{{ item.description || item.type || getItemSubtitle(item) }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 无搜索结果 -->
|
||||
<div v-else class="py-16 text-center">
|
||||
<SearchX :size="48" class="mx-auto text-base-300 mb-3" />
|
||||
<p class="text-base-content opacity-70">{{ noResultsMessage }}</p>
|
||||
<button class="btn btn-sm btn-ghost mt-3" @click="$emit('clear-search')">
|
||||
清除搜索
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
import { SearchX } from "lucide-vue-next";
|
||||
import { getPreviewSize } from "./index.ts";
|
||||
|
||||
interface Props {
|
||||
items: any[];
|
||||
searchQuery: string;
|
||||
componentModules: Record<string, any>;
|
||||
noResultsMessage: string;
|
||||
itemType: "component" | "template" | "virtual";
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
|
||||
const emit = defineEmits(["item-click", "clear-search"]);
|
||||
|
||||
// 过滤后的项目列表
|
||||
const filteredItems = computed(() => {
|
||||
if (!props.searchQuery) {
|
||||
return props.items;
|
||||
}
|
||||
const query = props.searchQuery.toLowerCase();
|
||||
return props.items.filter(
|
||||
(item) =>
|
||||
item.name.toLowerCase().includes(query) ||
|
||||
(item.type && item.type.toLowerCase().includes(query)) ||
|
||||
(item.description && item.description.toLowerCase().includes(query)),
|
||||
);
|
||||
});
|
||||
|
||||
// 获取项目副标题
|
||||
function getItemSubtitle(item: any): string {
|
||||
if (props.itemType === "template") {
|
||||
return "模板";
|
||||
}
|
||||
return item.type || "";
|
||||
}
|
||||
|
||||
// 处理项目点击
|
||||
function handleItemClick(item: any) {
|
||||
emit("item-click", item);
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.component-preview {
|
||||
max-width: 100%;
|
||||
max-height: 100%;
|
||||
object-fit: contain;
|
||||
}
|
||||
</style>
|
||||
@@ -1,8 +1,110 @@
|
||||
import motherboardSvg from "@/components/equipments/svg/motherboard.svg";
|
||||
import buttonSvg from "@/components/equipments/svg/button.svg";
|
||||
|
||||
// 元器件配置接口
|
||||
export interface ComponentConfig {
|
||||
type: string;
|
||||
name: string;
|
||||
previewSize?: number;
|
||||
}
|
||||
|
||||
// 虚拟外设配置接口
|
||||
export interface VirtualDeviceConfig {
|
||||
type: string;
|
||||
name: string;
|
||||
previewSize?: number;
|
||||
}
|
||||
|
||||
// 模板配置接口
|
||||
export interface TemplateConfig {
|
||||
name: string;
|
||||
id: string;
|
||||
description: string;
|
||||
path: string;
|
||||
thumbnailUrl: string;
|
||||
capsPage?: string;
|
||||
}
|
||||
|
||||
// 预览尺寸配置
|
||||
export const previewSizes: Record<string, number> = {
|
||||
MechanicalButton: 0.4,
|
||||
Switch: 0.35,
|
||||
Pin: 0.8,
|
||||
SMT_LED: 0.7,
|
||||
SevenSegmentDisplay: 0.4,
|
||||
HDMI: 0.5,
|
||||
DDR: 0.5,
|
||||
ETH: 0.5,
|
||||
SD: 0.6,
|
||||
SFP: 0.4,
|
||||
SMA: 0.7,
|
||||
MotherBoard: 0.13,
|
||||
PG2L100H_FBG676: 0.2,
|
||||
BaseBoard: 0.15,
|
||||
DDS: 0.3,
|
||||
};
|
||||
|
||||
// 可用元器件列表
|
||||
export const availableComponents: ComponentConfig[] = [
|
||||
{ type: "MechanicalButton", name: "机械按钮" },
|
||||
{ type: "Switch", name: "开关" },
|
||||
{ type: "Pin", name: "引脚" },
|
||||
{ type: "SMT_LED", name: "贴片LED" },
|
||||
{ type: "SevenSegmentDisplay", name: "数码管" },
|
||||
{ type: "HDMI", name: "HDMI接口" },
|
||||
{ type: "DDR", name: "DDR内存" },
|
||||
{ type: "ETH", name: "以太网接口" },
|
||||
{ type: "SD", name: "SD卡插槽" },
|
||||
{ type: "SFP", name: "SFP光纤模块" },
|
||||
{ type: "SMA", name: "SMA连接器" },
|
||||
{ type: "MotherBoard", name: "主板" },
|
||||
{ type: "PG2L100H_FBG676", name: "PG2L100H FBG676芯片" },
|
||||
{ type: "BaseBoard", name: "通用底板" },
|
||||
];
|
||||
|
||||
// 可用虚拟外设列表
|
||||
export const availableVirtualDevices: VirtualDeviceConfig[] = [
|
||||
{ type: "DDS", name: "信号发生器" },
|
||||
];
|
||||
|
||||
// 可用模板列表
|
||||
export const availableTemplates: TemplateConfig[] = [
|
||||
{
|
||||
name: "PG2L100H 基础开发板",
|
||||
id: "PG2L100H_Pango100pro",
|
||||
description: "包含主板和两个LED的基本设置",
|
||||
path: "/EquipmentTemplates/PG2L100H_Pango100pro.json",
|
||||
thumbnailUrl: motherboardSvg,
|
||||
},
|
||||
{
|
||||
name: "矩阵键盘",
|
||||
id: "MatrixKey",
|
||||
description: "包含4x4,共16个按键的矩阵键盘",
|
||||
path: "/EquipmentTemplates/MatrixKey.json",
|
||||
thumbnailUrl: buttonSvg,
|
||||
},
|
||||
];
|
||||
|
||||
// 获取组件预览尺寸的工具函数
|
||||
export function getPreviewSize(componentType: string): number {
|
||||
return previewSizes[componentType] || 0.5;
|
||||
}
|
||||
|
||||
// 获取所有组件类型(用于预加载)
|
||||
export function getAllComponentTypes(): string[] {
|
||||
const componentTypes = availableComponents.map((c) => c.type);
|
||||
const virtualDeviceTypes = availableVirtualDevices.map((d) => d.type);
|
||||
return [...componentTypes, ...virtualDeviceTypes];
|
||||
}
|
||||
|
||||
// 导出组件管理器服务
|
||||
export { useProvideComponentManager, useComponentManager } from './composable/componentManager';
|
||||
export {
|
||||
useProvideComponentManager,
|
||||
useComponentManager,
|
||||
} from "./composable/componentManager";
|
||||
|
||||
// 导出图表管理器
|
||||
export type { DiagramData, DiagramPart } from './composable/diagramManager';
|
||||
export type { DiagramData, DiagramPart } from "./composable/diagramManager";
|
||||
|
||||
// 导出连线管理器
|
||||
export type { WireItem } from './composable/wireManager';
|
||||
export type { WireItem } from "./composable/wireManager";
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
<template>
|
||||
<div class="card card-dash h-80 w-100 shadow-xl">
|
||||
<div class="card-body">
|
||||
<h1 class="card-title place-self-center my-3 text-2xl">User Login</h1>
|
||||
<div class="flex flex-col w-full h-full">
|
||||
<label class="input w-full my-3">
|
||||
<img class="h-[1em] opacity-50" src="@/assets/user.svg" alt="User img" />
|
||||
<input type="text" class="grow" placeholder="用户名" />
|
||||
</label>
|
||||
<label class="input w-full my-3">
|
||||
<img class="h-[1em] opacity-50" src="@/assets/pwd.svg" alt="User img" />
|
||||
<input type="text" class="grow" placeholder="密码" />
|
||||
</label>
|
||||
</div>
|
||||
<div>
|
||||
<RouterLink class="flex justify-end mx-3" to="/">忘记密码?</RouterLink>
|
||||
</div>
|
||||
<div class="card-actions flex items-end my-3">
|
||||
<button class="btn flex-1">注册</button>
|
||||
<button class="btn btn-primary flex-3">登录</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts"></script>
|
||||
|
||||
<style scoped>
|
||||
@import "@/assets/main.css";
|
||||
</style>
|
||||
@@ -2,74 +2,55 @@
|
||||
<div class="navbar bg-base-100 shadow-xl">
|
||||
<div class="navbar-start">
|
||||
<div class="dropdown">
|
||||
<div tabindex="0" role="button"
|
||||
class="btn btn-ghost hover:bg-primary hover:bg-opacity-20 transition-all duration-300">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h8m-8 6h16" />
|
||||
</svg>
|
||||
<div
|
||||
tabindex="0"
|
||||
role="button"
|
||||
class="btn btn-ghost hover:bg-primary hover:bg-opacity-20 transition-all duration-300"
|
||||
>
|
||||
<MenuIcon />
|
||||
</div>
|
||||
<ul tabindex="0"
|
||||
class="menu menu-sm dropdown-content bg-base-100 rounded-lg z-50 mt-3 w-52 p-2 shadow-lg transition-all duration-300 ease-in-out">
|
||||
<ul
|
||||
tabindex="0"
|
||||
class="menu menu-sm dropdown-content bg-base-200 rounded-lg z-50 mt-3 w-52 p-2 shadow-lg transition-all duration-300 ease-in-out"
|
||||
>
|
||||
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
||||
<router-link to="/" class="text-base font-medium">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 opacity-70" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" />
|
||||
<polyline points="9 22 9 12 15 12 15 22" />
|
||||
</svg>
|
||||
<House class="icon" />
|
||||
首页
|
||||
</router-link>
|
||||
</li>
|
||||
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
||||
<router-link to="/user" class="text-base font-medium">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 opacity-70" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
|
||||
<circle cx="12" cy="7" r="4"></circle>
|
||||
</svg>
|
||||
<User class="icon" />
|
||||
用户界面
|
||||
</router-link>
|
||||
</li>
|
||||
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
||||
<router-link to="/project" class="text-base font-medium">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 opacity-70" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"></path>
|
||||
<path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"></path>
|
||||
</svg>
|
||||
<PencilRuler class="icon" />
|
||||
工程界面
|
||||
</router-link>
|
||||
</li>
|
||||
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
||||
<router-link to="/test" class="text-base font-medium">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 opacity-70" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z"></path>
|
||||
</svg>
|
||||
<FlaskConical class="icon" />
|
||||
测试功能
|
||||
</router-link>
|
||||
</li>
|
||||
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
||||
<router-link to="/markdown-test" class="text-base font-medium">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 opacity-70" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
|
||||
<polyline points="14 2 14 8 20 8"></polyline>
|
||||
<line x1="16" y1="13" x2="8" y2="13"></line>
|
||||
<line x1="16" y1="17" x2="8" y2="17"></line>
|
||||
<polyline points="10 9 9 9 8 9"></polyline>
|
||||
</svg>
|
||||
<FileText class="icon" />
|
||||
Markdown测试
|
||||
</router-link>
|
||||
</li>
|
||||
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
||||
<a href="http://localhost:5000/swagger" target="_self" rel="noopener noreferrer"
|
||||
class="text-base font-medium">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 opacity-70" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<path d="M3 3h18v18H3z"></path>
|
||||
<path d="M8 8h8v8H8z" fill="currentColor"></path>
|
||||
</svg>
|
||||
<a
|
||||
href="http://localhost:5000/swagger"
|
||||
target="_self"
|
||||
rel="noopener noreferrer"
|
||||
class="text-base font-medium"
|
||||
>
|
||||
<BookOpenText class="icon" />
|
||||
OpenAPI文档
|
||||
</a>
|
||||
</li>
|
||||
@@ -77,15 +58,64 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="navbar-center lg:flex">
|
||||
<router-link to="/" class="btn btn-ghost text-xl font-bold transition-all duration-300 hover:scale-105">
|
||||
<router-link
|
||||
to="/"
|
||||
class="btn btn-ghost text-xl font-bold transition-all duration-300 hover:scale-105"
|
||||
>
|
||||
<span class="text-primary">FPGA</span> Web Lab
|
||||
</router-link>
|
||||
</div>
|
||||
<div class="navbar-end">
|
||||
<router-link to="/login"
|
||||
class="btn btn-primary text-base-100 transition-all duration-300 hover:scale-105 hover:shadow-lg mr-3">
|
||||
登录
|
||||
</router-link>
|
||||
<!-- 未登录状态 -->
|
||||
<template v-if="!isLoggedIn">
|
||||
<router-link
|
||||
to="/login"
|
||||
class="btn btn-primary text-base-100 transition-all duration-300 hover:scale-105 hover:shadow-lg mr-3"
|
||||
>
|
||||
登录
|
||||
</router-link>
|
||||
</template>
|
||||
|
||||
<!-- 已登录状态 -->
|
||||
<template v-else>
|
||||
<div class="dropdown dropdown-end mr-3">
|
||||
<div
|
||||
tabindex="0"
|
||||
role="button"
|
||||
class="btn btn-ghost hover:bg-primary hover:bg-opacity-20 transition-all duration-300 flex items-center gap-2"
|
||||
>
|
||||
<User class="h-5 w-5" />
|
||||
<span class="font-medium">{{ userName }}</span>
|
||||
<ChevronDownIcon
|
||||
class="icon transition-transform duration-300 dropdown-icon"
|
||||
/>
|
||||
</div>
|
||||
<ul
|
||||
tabindex="0"
|
||||
class="menu menu-sm dropdown-content bg-base-200 rounded-lg z-50 mt-3 w-48 p-2 shadow-lg transition-all duration-300 ease-in-out"
|
||||
>
|
||||
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
||||
<router-link
|
||||
to="/user"
|
||||
class="text-base font-medium flex items-center gap-2"
|
||||
>
|
||||
<User class="icon" />
|
||||
用户中心
|
||||
</router-link>
|
||||
</li>
|
||||
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
||||
<button
|
||||
@click="handleLogout"
|
||||
class="text-base font-medium flex items-center gap-2 w-full text-left hover:bg-error hover:text-error-content"
|
||||
>
|
||||
<LogOutIcon class="icon" />
|
||||
退出登录
|
||||
</button>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="ml-2 transition-all duration-500 hover:rotate-12">
|
||||
<ThemeControlButton />
|
||||
</div>
|
||||
@@ -94,9 +124,78 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from "vue";
|
||||
import { onBeforeRouteUpdate, useRouter } from "vue-router";
|
||||
import ThemeControlButton from "./ThemeControlButton.vue";
|
||||
import {
|
||||
MenuIcon,
|
||||
FileText,
|
||||
BookOpenText,
|
||||
FlaskConical,
|
||||
House,
|
||||
User,
|
||||
PencilRuler,
|
||||
LogOutIcon,
|
||||
ChevronDownIcon,
|
||||
} from "lucide-vue-next";
|
||||
import { AuthManager } from "@/utils/AuthManager";
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
// 响应式数据
|
||||
const userName = ref<string>("");
|
||||
const isUserMenuOpen = ref<boolean>(false);
|
||||
const isLoggedIn = ref<boolean>(false); // 改为响应式变量
|
||||
|
||||
// 方法
|
||||
const loadUserInfo = async () => {
|
||||
try {
|
||||
const authenticated = await AuthManager.isAuthenticated();
|
||||
if (authenticated) {
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
const userInfo = await client.getUserInfo();
|
||||
userName.value = userInfo.name;
|
||||
isLoggedIn.value = true;
|
||||
} else {
|
||||
userName.value = "";
|
||||
isLoggedIn.value = false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to load user info:", error);
|
||||
// 如果获取用户信息失败,清除token
|
||||
AuthManager.clearToken();
|
||||
userName.value = "";
|
||||
isLoggedIn.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
AuthManager.logout();
|
||||
userName.value = "";
|
||||
isLoggedIn.value = false;
|
||||
router.push("/");
|
||||
};
|
||||
|
||||
// 生命周期钩子
|
||||
onMounted(() => {
|
||||
loadUserInfo();
|
||||
|
||||
// 监听路由变化
|
||||
router.afterEach(() => {
|
||||
loadUserInfo();
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
<style scoped lang="postcss">
|
||||
@import "../assets/main.css";
|
||||
|
||||
.icon {
|
||||
@apply h-5 w-5 opacity-70;
|
||||
}
|
||||
|
||||
.dropdown[open] .dropdown-icon,
|
||||
.dropdown:focus-within .dropdown-icon {
|
||||
transform: rotate(180deg);
|
||||
}
|
||||
</style>
|
||||
|
||||
178
src/components/Oscilloscope/WaveformDisplay.vue
Normal file
178
src/components/Oscilloscope/WaveformDisplay.vue
Normal file
@@ -0,0 +1,178 @@
|
||||
<template>
|
||||
<div class="w-full h-100">
|
||||
<v-chart v-if="true" class="w-full h-full" :option="option" autoresize />
|
||||
<div
|
||||
v-else
|
||||
class="w-full h-full flex items-center justify-center text-gray-500"
|
||||
>
|
||||
暂无数据
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, withDefaults } from "vue";
|
||||
import { forEach } from "lodash";
|
||||
import VChart from "vue-echarts";
|
||||
import { type WaveformDataType } from "./index";
|
||||
|
||||
// Echarts
|
||||
import { use } from "echarts/core";
|
||||
import { LineChart } from "echarts/charts";
|
||||
import {
|
||||
TitleComponent,
|
||||
TooltipComponent,
|
||||
LegendComponent,
|
||||
ToolboxComponent,
|
||||
DataZoomComponent,
|
||||
GridComponent,
|
||||
} from "echarts/components";
|
||||
import { CanvasRenderer } from "echarts/renderers";
|
||||
import type { ComposeOption } from "echarts/core";
|
||||
import type { LineSeriesOption } from "echarts/charts";
|
||||
import type {
|
||||
TitleComponentOption,
|
||||
TooltipComponentOption,
|
||||
LegendComponentOption,
|
||||
ToolboxComponentOption,
|
||||
DataZoomComponentOption,
|
||||
GridComponentOption,
|
||||
} from "echarts/components";
|
||||
|
||||
use([
|
||||
TitleComponent,
|
||||
TooltipComponent,
|
||||
LegendComponent,
|
||||
ToolboxComponent,
|
||||
DataZoomComponent,
|
||||
GridComponent,
|
||||
LineChart,
|
||||
CanvasRenderer,
|
||||
]);
|
||||
|
||||
type EChartsOption = ComposeOption<
|
||||
| TitleComponentOption
|
||||
| TooltipComponentOption
|
||||
| LegendComponentOption
|
||||
| ToolboxComponentOption
|
||||
| DataZoomComponentOption
|
||||
| GridComponentOption
|
||||
| LineSeriesOption
|
||||
>;
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
data?: WaveformDataType;
|
||||
}>(),
|
||||
{
|
||||
data: () => ({
|
||||
x: [],
|
||||
y: [],
|
||||
xUnit: "s",
|
||||
yUnit: "V",
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
const hasData = computed(() => {
|
||||
return (
|
||||
props.data &&
|
||||
props.data.x &&
|
||||
props.data.y &&
|
||||
props.data.x.length > 0 &&
|
||||
props.data.y.length > 0 &&
|
||||
props.data.y.some((channel) => channel.length > 0)
|
||||
);
|
||||
});
|
||||
|
||||
const option = computed((): EChartsOption => {
|
||||
const series: LineSeriesOption[] = [];
|
||||
|
||||
forEach(props.data.y, (yData, index) => {
|
||||
// 将 x 和 y 数据组合成 [x, y] 格式
|
||||
const seriesData = props.data.x.map((xValue, i) => [xValue, yData[i] || 0]);
|
||||
|
||||
series.push({
|
||||
type: "line",
|
||||
name: `通道 ${index + 1}`,
|
||||
data: seriesData,
|
||||
smooth: false, // 示波器通常显示原始波形
|
||||
symbol: "none", // 不显示数据点标记
|
||||
lineStyle: {
|
||||
width: 2,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
grid: {
|
||||
left: "10%",
|
||||
right: "10%",
|
||||
top: "15%",
|
||||
bottom: "25%",
|
||||
},
|
||||
tooltip: {
|
||||
trigger: "axis",
|
||||
formatter: (params: any) => {
|
||||
let result = `时间: ${params[0].data[0].toFixed(2)} ${props.data.xUnit}<br/>`;
|
||||
params.forEach((param: any) => {
|
||||
result += `${param.seriesName}: ${param.data[1].toFixed(3)} ${props.data.yUnit}<br/>`;
|
||||
});
|
||||
return result;
|
||||
},
|
||||
},
|
||||
legend: {
|
||||
top: "5%",
|
||||
data: series.map((s) => s.name) as string[],
|
||||
},
|
||||
toolbox: {
|
||||
feature: {
|
||||
restore: {},
|
||||
saveAsImage: {},
|
||||
},
|
||||
},
|
||||
dataZoom: [
|
||||
{
|
||||
type: "inside",
|
||||
start: 0,
|
||||
end: 100,
|
||||
},
|
||||
{
|
||||
start: 0,
|
||||
end: 100,
|
||||
},
|
||||
],
|
||||
xAxis: {
|
||||
type: "value",
|
||||
name: `时间 (${props.data.xUnit})`,
|
||||
nameLocation: "middle",
|
||||
nameGap: 30,
|
||||
axisLine: {
|
||||
show: true,
|
||||
},
|
||||
axisTick: {
|
||||
show: true,
|
||||
},
|
||||
splitLine: {
|
||||
show: false,
|
||||
},
|
||||
},
|
||||
yAxis: {
|
||||
type: "value",
|
||||
name: `电压 (${props.data.yUnit})`,
|
||||
nameLocation: "middle",
|
||||
nameGap: 40,
|
||||
axisLine: {
|
||||
show: true,
|
||||
},
|
||||
axisTick: {
|
||||
show: true,
|
||||
},
|
||||
splitLine: {
|
||||
show: false,
|
||||
},
|
||||
},
|
||||
series: series,
|
||||
};
|
||||
});
|
||||
</script>
|
||||
42
src/components/Oscilloscope/index.ts
Normal file
42
src/components/Oscilloscope/index.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import WaveformDisplay from "./WaveformDisplay.vue";
|
||||
|
||||
type WaveformDataType = {
|
||||
x: number[];
|
||||
y: number[][];
|
||||
xUnit: "s" | "ms" | "us";
|
||||
yUnit: "V" | "mV" | "uV";
|
||||
};
|
||||
|
||||
// Test data generator
|
||||
function generateTestData(): WaveformDataType {
|
||||
const sampleRate = 1000; // 1kHz
|
||||
const duration = 0.1; // 10ms
|
||||
const points = Math.floor(sampleRate * duration);
|
||||
|
||||
const x = Array.from({ length: points }, (_, i) => (i / sampleRate) * 1000); // time in ms
|
||||
|
||||
// Generate multiple channels with different waveforms
|
||||
const y = [
|
||||
// Channel 1: Sine wave 50Hz
|
||||
Array.from(
|
||||
{ length: points },
|
||||
(_, i) => Math.sin((2 * Math.PI * 50 * i) / sampleRate) * 3.3,
|
||||
),
|
||||
// Channel 2: Square wave 25Hz
|
||||
Array.from(
|
||||
{ length: points },
|
||||
(_, i) => Math.sign(Math.sin((2 * Math.PI * 25 * i) / sampleRate)) * 5,
|
||||
),
|
||||
// Channel 3: Sawtooth wave 33Hz
|
||||
Array.from(
|
||||
{ length: points },
|
||||
(_, i) => (2 * (((33 * i) / sampleRate) % 1) - 1) * 2.5,
|
||||
),
|
||||
// Channel 4: Noise + DC offset
|
||||
Array.from({ length: points }, () => Math.random() * 0.5 + 1.5),
|
||||
];
|
||||
|
||||
return { x, y, xUnit: "ms", yUnit: "V" };
|
||||
}
|
||||
|
||||
export { WaveformDisplay, generateTestData , type WaveformDataType };
|
||||
@@ -385,6 +385,7 @@ const currentWaveformPath = computed(() => {
|
||||
function selectWaveform(index: number) {
|
||||
currentWaveformIndex.value = index;
|
||||
updateModelValue();
|
||||
applyOutputWave();
|
||||
}
|
||||
|
||||
async function applyOutputWave() {
|
||||
@@ -408,7 +409,7 @@ async function applyOutputWave() {
|
||||
eqps.boardPort,
|
||||
0,
|
||||
currentWaveformIndex.value,
|
||||
toInteger(frequency.value * Math.pow(2, 32 - 20)),
|
||||
toInteger((frequency.value * Math.pow(2, 32 - 20)) / 10),
|
||||
);
|
||||
if (!ret) {
|
||||
dialog.error("应用失败");
|
||||
@@ -424,7 +425,7 @@ async function applyOutputWave() {
|
||||
toInteger((phase.value * 4096) / 360),
|
||||
);
|
||||
if (ret) {
|
||||
dialog.info("应用成功");
|
||||
// dialog.info("应用成功");
|
||||
} else {
|
||||
dialog.error("应用失败");
|
||||
}
|
||||
@@ -455,6 +456,7 @@ function increaseFrequency() {
|
||||
frequency.value = parseFloat(frequency.value.toFixed(1)); // 修复浮点数精度问题
|
||||
frequencyInput.value = formatFrequency(frequency.value);
|
||||
updateModelValue();
|
||||
applyOutputWave();
|
||||
}
|
||||
|
||||
function decreaseFrequency() {
|
||||
@@ -475,6 +477,7 @@ function decreaseFrequency() {
|
||||
frequency.value = parseFloat(frequency.value.toFixed(1)); // 修复浮点数精度问题
|
||||
frequencyInput.value = formatFrequency(frequency.value);
|
||||
updateModelValue();
|
||||
applyOutputWave();
|
||||
}
|
||||
|
||||
function applyFrequencyInput() {
|
||||
@@ -505,6 +508,7 @@ function increasePhase() {
|
||||
}
|
||||
phaseInput.value = phase.value.toString();
|
||||
updateModelValue();
|
||||
applyOutputWave();
|
||||
}
|
||||
|
||||
function decreasePhase() {
|
||||
@@ -514,6 +518,7 @@ function decreasePhase() {
|
||||
}
|
||||
phaseInput.value = phase.value.toString();
|
||||
updateModelValue();
|
||||
applyOutputWave();
|
||||
}
|
||||
|
||||
function applyPhaseInput() {
|
||||
|
||||
@@ -9,24 +9,40 @@
|
||||
IDCode: 0x{{ jtagIDCode.toString(16).padStart(8, "0").toUpperCase() }}
|
||||
</p>
|
||||
<button class="btn btn-circle w-6 h-6" :onclick="getIDCode">
|
||||
<svg class="icon opacity-70 fill-primary" viewBox="0 0 1024 1024" version="1.1"
|
||||
xmlns="http://www.w3.org/2000/svg" p-id="4865" width="200" height="200">
|
||||
<svg
|
||||
class="icon opacity-70 fill-primary"
|
||||
viewBox="0 0 1024 1024"
|
||||
version="1.1"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
p-id="4865"
|
||||
width="200"
|
||||
height="200"
|
||||
>
|
||||
<path
|
||||
d="M894.481158 505.727133c0 49.589418-9.711176 97.705276-28.867468 143.007041-18.501376 43.74634-44.98454 83.031065-78.712713 116.759237-33.728172 33.728172-73.012897 60.211337-116.759237 78.712713-45.311998 19.156292-93.417623 28.877701-143.007041 28.877701s-97.695043-9.721409-142.996808-28.877701c-43.756573-18.501376-83.031065-44.98454-116.76947-78.712713-33.728172-33.728172-60.211337-73.012897-78.712713-116.759237-19.156292-45.301765-28.867468-93.417623-28.867468-143.007041 0-49.579185 9.711176-97.695043 28.867468-142.996808 18.501376-43.74634 44.98454-83.031065 78.712713-116.759237 33.738405-33.728172 73.012897-60.211337 116.76947-78.712713 45.301765-19.166525 93.40739-28.877701 142.996808-28.877701 52.925397 0 104.008842 11.010775 151.827941 32.745798 46.192042 20.977777 86.909395 50.79692 121.016191 88.608084 4.389984 4.860704 8.646937 9.854439 12.781094 14.97097l0-136.263453c0-11.307533 9.168824-20.466124 20.466124-20.466124 11.307533 0 20.466124 9.15859 20.466124 20.466124l0 183.64253c0 5.433756-2.148943 10.632151-5.986341 14.46955-3.847631 3.837398-9.046027 5.996574-14.479783 5.996574l-183.64253-0.020466c-11.307533 0-20.466124-9.168824-20.466124-20.466124 0-11.307533 9.168824-20.466124 20.466124-20.466124l132.293025 0.020466c-3.960195-4.952802-8.063653-9.782807-12.289907-14.479783-30.320563-33.605376-66.514903-60.098773-107.549481-78.753645-42.467207-19.289322-87.850837-29.072129-134.902456-29.072129-87.195921 0-169.172981 33.9533-230.816946 95.597265-61.654198 61.654198-95.597265 143.621025-95.597265 230.816946s33.943067 169.172981 95.597265 230.816946c61.643965 61.654198 143.621025 95.607498 230.816946 95.607498s169.172981-33.9533 230.816946-95.607498c61.654198-61.643965 95.597265-143.621025 95.597265-230.816946 0-11.2973 9.168824-20.466124 20.466124-20.466124C885.322567 485.261009 894.481158 494.429833 894.481158 505.727133z"
|
||||
p-id="4866"></path>
|
||||
p-id="4866"
|
||||
></path>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="divider"></div>
|
||||
<UploadCard class="bg-base-200" :upload-event="eqps.jtagUploadBitstream"
|
||||
:download-event="eqps.jtagDownloadBitstream" :bitstream-file="eqps.jtagBitstream"
|
||||
@update:bitstream-file="handleBitstreamChange">
|
||||
<UploadCard
|
||||
class="bg-base-200"
|
||||
:upload-event="eqps.jtagUploadBitstream"
|
||||
:download-event="eqps.jtagDownloadBitstream"
|
||||
:bitstream-file="eqps.jtagBitstream"
|
||||
@update:bitstream-file="handleBitstreamChange"
|
||||
>
|
||||
</UploadCard>
|
||||
<div class="divider"></div>
|
||||
<div class="w-full">
|
||||
<legend class="fieldset-legend text-sm mb-0.3">Jtag运行频率</legend>
|
||||
<select class="select w-full" @change="handleSelectJtagSpeed" :value="props.jtagFreq">
|
||||
<select
|
||||
class="select w-full"
|
||||
@change="handleSelectJtagSpeed"
|
||||
:value="props.jtagFreq"
|
||||
>
|
||||
<option v-for="option in selectJtagSpeedOptions" :value="option.id">
|
||||
{{ option.text }}
|
||||
</option>
|
||||
@@ -35,12 +51,23 @@
|
||||
<div class="flex flex-row items-center">
|
||||
<fieldset class="fieldset w-70">
|
||||
<legend class="fieldset-legend text-sm">边界扫描刷新率 / Hz</legend>
|
||||
<input type="number" class="input validator" required placeholder="Type a number between 1 to 1000" min="1"
|
||||
max="1000" v-model="jtagBoundaryScanFreq" title="Type a number between 1 to 1000" />
|
||||
<input
|
||||
type="number"
|
||||
class="input validator"
|
||||
required
|
||||
placeholder="Type a number between 1 to 1000"
|
||||
min="1"
|
||||
max="1000"
|
||||
v-model="jtagBoundaryScanFreq"
|
||||
title="Type a number between 1 to 1000"
|
||||
/>
|
||||
<p class="validator-hint">输入一个1 ~ 1000的数</p>
|
||||
</fieldset>
|
||||
<button class="btn btn-primary grow mx-4" :class="eqps.enableJtagBoundaryScan ? '' : 'btn-soft'"
|
||||
:onclick="toggleJtagBoundaryScan">
|
||||
<button
|
||||
class="btn btn-primary grow mx-4"
|
||||
:class="eqps.enableJtagBoundaryScan ? '' : 'btn-soft'"
|
||||
:onclick="toggleJtagBoundaryScan"
|
||||
>
|
||||
{{ eqps.enableJtagBoundaryScan ? "关闭边界扫描" : "启动边界扫描" }}
|
||||
</button>
|
||||
</div>
|
||||
@@ -48,12 +75,21 @@
|
||||
<h1 class="font-bold text-center text-2xl">外设</h1>
|
||||
<div class="flex flex-row justify-around">
|
||||
<div class="flex flex-row">
|
||||
<input type="checkbox" class="checkbox" :checked="eqps.enableMatrixKey"
|
||||
@change="handleMatrixkeyCheckboxChange" />
|
||||
<input
|
||||
type="checkbox"
|
||||
class="checkbox"
|
||||
:checked="eqps.enableMatrixKey"
|
||||
@change="handleMatrixkeyCheckboxChange"
|
||||
/>
|
||||
<p class="mx-2">启用矩阵键盘</p>
|
||||
</div>
|
||||
<div class="flex flex-row">
|
||||
<input type="checkbox" class="checkbox" :checked="eqps.enablePower" @change="handlePowerCheckboxChange" />
|
||||
<input
|
||||
type="checkbox"
|
||||
class="checkbox"
|
||||
:checked="eqps.enablePower"
|
||||
@change="handlePowerCheckboxChange"
|
||||
/>
|
||||
<p class="mx-2">启用电源</p>
|
||||
</div>
|
||||
</div>
|
||||
@@ -65,7 +101,7 @@ import z from "zod";
|
||||
import UploadCard from "@/components/UploadCard.vue";
|
||||
import { useDialogStore } from "@/stores/dialog";
|
||||
import { useEquipments } from "@/stores/equipments";
|
||||
import { computed, ref, watchEffect, watchPostEffect } from "vue";
|
||||
import { computed, ref, watchEffect } from "vue";
|
||||
|
||||
interface CapsProps {
|
||||
jtagAddr?: string;
|
||||
@@ -137,11 +173,6 @@ async function handlePowerCheckboxChange(event: Event) {
|
||||
}
|
||||
|
||||
async function toggleJtagBoundaryScan() {
|
||||
if (eqps.jtagClientMutex.isLocked()) {
|
||||
dialog.warn("Jtag正在被占用");
|
||||
return;
|
||||
}
|
||||
|
||||
eqps.enableJtagBoundaryScan = !eqps.enableJtagBoundaryScan;
|
||||
}
|
||||
|
||||
|
||||
@@ -177,26 +177,26 @@ defineExpose({
|
||||
getPinPosition: (pinId: string) => {
|
||||
// 如果是自定义的引脚ID
|
||||
if (props.pins && props.pins.length > 0) {
|
||||
console.log('SMT_LED查找Pin ID:', pinId);
|
||||
console.log('SMT_LED组件尺寸:', props.size, '宽高:', width.value, 'x', height.value);
|
||||
// console.log('SMT_LED查找Pin ID:', pinId);
|
||||
// console.log('SMT_LED组件尺寸:', props.size, '宽高:', width.value, 'x', height.value);
|
||||
const customPin = props.pins.find(p => p.pinId === pinId);
|
||||
console.log('找到的引脚配置:', customPin);
|
||||
// console.log('找到的引脚配置:', customPin);
|
||||
|
||||
if (customPin) {
|
||||
// 考虑组件尺寸的缩放
|
||||
const scaledX = customPin.x * props.size;
|
||||
const scaledY = customPin.y * props.size;
|
||||
|
||||
console.log('使用Pin缩放后的坐标:', scaledX, scaledY);
|
||||
// console.log('使用Pin缩放后的坐标:', scaledX, scaledY);
|
||||
return {
|
||||
x: scaledX,
|
||||
y: scaledY
|
||||
};
|
||||
}
|
||||
console.log('未找到匹配的引脚');
|
||||
// console.log('未找到匹配的引脚');
|
||||
return null;
|
||||
}
|
||||
console.log('没有引脚配置');
|
||||
// console.log('没有引脚配置');
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -2,10 +2,9 @@ import './assets/main.css'
|
||||
|
||||
import { createApp } from 'vue'
|
||||
import { createPinia } from 'pinia'
|
||||
import VueKonva from "vue-konva"
|
||||
|
||||
import App from '@/App.vue'
|
||||
import router from './router'
|
||||
|
||||
const app = createApp(App).use(router).use(createPinia()).use(VueKonva).mount('#app')
|
||||
const app = createApp(App).use(router).use(createPinia()).mount('#app')
|
||||
|
||||
|
||||
@@ -1,22 +1,19 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import HomeView from '../views/HomeView.vue'
|
||||
import LoginView from '../views/LoginView.vue'
|
||||
import LabView from '../views/LabView.vue'
|
||||
import ProjectView from '../views/ProjectView.vue'
|
||||
import TestView from '../views/TestView.vue'
|
||||
import UserView from '../views/UserView.vue'
|
||||
import AdminView from '../views/AdminView.vue'
|
||||
import { createRouter, createWebHistory } from "vue-router";
|
||||
import HomeView from "../views/HomeView.vue";
|
||||
import AuthView from "../views/AuthView.vue";
|
||||
import ProjectView from "../views/Project/Index.vue";
|
||||
import TestView from "../views/TestView.vue";
|
||||
import UserView from "@/views/User/Index.vue";
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
routes: [
|
||||
{path: '/', name: 'home', component: HomeView},
|
||||
{path: '/login', name: 'login', component: LoginView},
|
||||
{path: '/lab/:id',name: 'lab', component: LabView},
|
||||
{path: '/project',name: 'project',component: ProjectView},
|
||||
{path: '/test', name: 'test', component: TestView},
|
||||
{path: '/user', name: 'user', component: UserView},
|
||||
{path: '/admin', name: 'admin', component: AdminView}]
|
||||
})
|
||||
{ path: "/", name: "home", component: HomeView },
|
||||
{ path: "/login", name: "login", component: AuthView },
|
||||
{ path: "/project", name: "project", component: ProjectView },
|
||||
{ path: "/test", name: "test", component: TestView },
|
||||
{ path: "/user", name: "user", component: UserView },
|
||||
],
|
||||
});
|
||||
|
||||
export default router
|
||||
export default router;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { ref, reactive, watchPostEffect } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { useLocalStorage } from '@vueuse/core'
|
||||
import { isString, toNumber } from 'lodash';
|
||||
import { Common } from '@/utils/Common';
|
||||
import z from "zod"
|
||||
@@ -14,9 +15,8 @@ export const useEquipments = defineStore('equipments', () => {
|
||||
const constrainsts = useConstraintsStore();
|
||||
const dialog = useDialogStore();
|
||||
|
||||
// Basic Info
|
||||
const boardAddr = ref("127.0.0.1");
|
||||
const boardPort = ref(1234);
|
||||
const boardAddr = useLocalStorage('fpga-board-addr', "127.0.0.1");
|
||||
const boardPort = useLocalStorage('fpga-board-port', 1234);
|
||||
|
||||
// Jtag
|
||||
const jtagBitstream = ref<File>();
|
||||
|
||||
239
src/utils/AuthManager.ts
Normal file
239
src/utils/AuthManager.ts
Normal file
@@ -0,0 +1,239 @@
|
||||
import {
|
||||
DataClient,
|
||||
VideoStreamClient,
|
||||
BsdlParserClient,
|
||||
DDSClient,
|
||||
JtagClient,
|
||||
MatrixKeyClient,
|
||||
PowerClient,
|
||||
RemoteUpdateClient,
|
||||
TutorialClient,
|
||||
UDPClient,
|
||||
} from "@/APIClient";
|
||||
|
||||
// 支持的客户端类型联合类型
|
||||
type SupportedClient =
|
||||
| DataClient
|
||||
| VideoStreamClient
|
||||
| BsdlParserClient
|
||||
| DDSClient
|
||||
| JtagClient
|
||||
| MatrixKeyClient
|
||||
| PowerClient
|
||||
| RemoteUpdateClient
|
||||
| TutorialClient
|
||||
| UDPClient;
|
||||
|
||||
export class AuthManager {
|
||||
// 存储token到localStorage
|
||||
public static setToken(token: string): void {
|
||||
localStorage.setItem("authToken", token);
|
||||
}
|
||||
|
||||
// 从localStorage获取token
|
||||
public static getToken(): string | null {
|
||||
return localStorage.getItem("authToken");
|
||||
}
|
||||
|
||||
// 清除token
|
||||
public static clearToken(): void {
|
||||
localStorage.removeItem("authToken");
|
||||
}
|
||||
|
||||
// 检查是否已认证
|
||||
public static async isAuthenticated(): Promise<boolean> {
|
||||
return await AuthManager.verifyToken();
|
||||
}
|
||||
|
||||
// 通用的为HTTP请求添加Authorization header的方法
|
||||
public static addAuthHeader(client: SupportedClient): void {
|
||||
const token = AuthManager.getToken();
|
||||
if (token) {
|
||||
// 创建一个自定义的 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);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// 通用的创建已认证客户端的方法(使用泛型)
|
||||
public static createAuthenticatedClient<T extends SupportedClient>(
|
||||
ClientClass: new (baseUrl?: string, http?: any) => T,
|
||||
): T {
|
||||
const customHttp = AuthManager.createAuthenticatedHttp();
|
||||
return customHttp
|
||||
? new ClientClass(undefined, customHttp)
|
||||
: 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 async login(
|
||||
username: string,
|
||||
password: string,
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const client = new DataClient();
|
||||
const token = await client.login(username, password);
|
||||
|
||||
if (token) {
|
||||
AuthManager.setToken(token);
|
||||
|
||||
// 验证token
|
||||
const authClient = AuthManager.createAuthenticatedDataClient();
|
||||
await authClient.testAuth();
|
||||
|
||||
return true;
|
||||
}
|
||||
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;
|
||||
} catch (error) {
|
||||
AuthManager.clearToken();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 验证管理员权限
|
||||
public static async verifyAdminAuth(): Promise<boolean> {
|
||||
try {
|
||||
const token = AuthManager.getToken();
|
||||
if (!token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
await client.testAdminAuth();
|
||||
return true;
|
||||
} catch (error) {
|
||||
// 只有在token完全无效的情况下才清除token
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查客户端是否已配置认证
|
||||
public static isClientAuthenticated(client: SupportedClient): boolean {
|
||||
const token = AuthManager.getToken();
|
||||
return !!token;
|
||||
}
|
||||
}
|
||||
272
src/utils/BoardManager.ts
Normal file
272
src/utils/BoardManager.ts
Normal file
@@ -0,0 +1,272 @@
|
||||
import { ref } from "vue";
|
||||
import { createInjectionState } from "@vueuse/core";
|
||||
import { RemoteUpdateClient, DataClient, Board } from "@/APIClient";
|
||||
import { Common } from "@/utils/Common";
|
||||
import { isUndefined } from "lodash";
|
||||
import { AuthManager } from "@/utils/AuthManager";
|
||||
|
||||
// 统一的板卡数据接口,扩展原有的Board类型
|
||||
export interface BoardData extends Board {
|
||||
defaultBitstream: string;
|
||||
goldBitstreamFile?: File;
|
||||
appBitstream1File?: File;
|
||||
appBitstream2File?: File;
|
||||
appBitstream3File?: File;
|
||||
}
|
||||
|
||||
const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
|
||||
// 远程升级相关参数
|
||||
const devPort = 1234;
|
||||
const remoteUpdater = new RemoteUpdateClient();
|
||||
|
||||
// 统一的板卡数据
|
||||
const boards = ref<BoardData[]>([]);
|
||||
|
||||
// 获取位流编号
|
||||
function getSelectedBitstreamNum(bitstreamName: string): number {
|
||||
if (bitstreamName === "黄金位流") return 0;
|
||||
if (bitstreamName === "应用位流1") return 1;
|
||||
if (bitstreamName === "应用位流2") return 2;
|
||||
if (bitstreamName === "应用位流3") return 3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 获取所有板卡信息(管理员权限)
|
||||
async function getAllBoards(): Promise<{ success: boolean; error?: string }> {
|
||||
try {
|
||||
// 验证管理员权限
|
||||
const hasAdminAuth = await AuthManager.verifyAdminAuth();
|
||||
if (!hasAdminAuth) {
|
||||
console.error("权限验证失败");
|
||||
return { success: false, error: "权限不足" };
|
||||
}
|
||||
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
const result = await client.getAllBoards();
|
||||
|
||||
if (result) {
|
||||
// 将Board类型转换为BoardData类型,添加默认值
|
||||
boards.value = result.map((board) => {
|
||||
return {
|
||||
...board,
|
||||
defaultBitstream: "黄金位流", // 设置默认位流
|
||||
goldBitstreamFile: undefined,
|
||||
appBitstream1File: undefined,
|
||||
appBitstream2File: undefined,
|
||||
appBitstream3File: undefined,
|
||||
// Ensure methods from Board are present
|
||||
init: board.init?.bind(board),
|
||||
toJSON: board.toJSON?.bind(board),
|
||||
};
|
||||
});
|
||||
console.log("获取板卡信息成功", result.length);
|
||||
return { success: true };
|
||||
} else {
|
||||
console.error("获取板卡信息失败:返回结果为空");
|
||||
return { success: false, error: "获取板卡信息失败" };
|
||||
}
|
||||
} catch (e: any) {
|
||||
console.error("获取板卡信息异常:", e);
|
||||
return { success: false, error: e.message || "获取板卡信息异常" };
|
||||
}
|
||||
}
|
||||
|
||||
// 新增板卡(管理员权限)
|
||||
async function addBoard(
|
||||
name: string,
|
||||
ipAddr: string,
|
||||
port: number,
|
||||
): Promise<{ success: boolean; error?: string; boardId?: string }> {
|
||||
try {
|
||||
// 验证管理员权限
|
||||
const hasAdminAuth = await AuthManager.verifyAdminAuth();
|
||||
if (!hasAdminAuth) {
|
||||
console.error("权限验证失败");
|
||||
return { success: false, error: "权限不足" };
|
||||
}
|
||||
|
||||
// 验证输入参数
|
||||
if (!name || !ipAddr || !port) {
|
||||
console.error("参数验证失败", { name, ipAddr, port });
|
||||
return { success: false, error: "参数不完整" };
|
||||
}
|
||||
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
const boardId = await client.addBoard(name, ipAddr, port);
|
||||
|
||||
if (boardId) {
|
||||
console.log("新增板卡成功", { boardId, name, ipAddr, port });
|
||||
// 刷新板卡列表
|
||||
await getAllBoards();
|
||||
return { success: true};
|
||||
} else {
|
||||
console.error("新增板卡失败:返回ID为空");
|
||||
return { success: false, error: "新增板卡失败" };
|
||||
}
|
||||
} catch (e: any) {
|
||||
console.error("新增板卡异常:", e);
|
||||
if (e.status === 401) {
|
||||
return { success: false, error: "权限不足" };
|
||||
} else if (e.status === 400) {
|
||||
return { success: false, error: "输入参数错误" };
|
||||
} else {
|
||||
return { success: false, error: e.message || "新增板卡异常" };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除板卡(管理员权限)
|
||||
async function deleteBoard(boardId: string): Promise<{ success: boolean; error?: string }> {
|
||||
try {
|
||||
// 验证管理员权限
|
||||
const hasAdminAuth = await AuthManager.verifyAdminAuth();
|
||||
if (!hasAdminAuth) {
|
||||
console.error("权限验证失败");
|
||||
return { success: false, error: "权限不足" };
|
||||
}
|
||||
|
||||
if (!boardId) {
|
||||
console.error("板卡ID为空");
|
||||
return { success: false, error: "板卡ID不能为空" };
|
||||
}
|
||||
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
const result = await client.deleteBoard(boardId);
|
||||
|
||||
if (result > 0) {
|
||||
console.log("删除板卡成功", { boardId, deletedCount: result });
|
||||
// 刷新板卡列表
|
||||
await getAllBoards();
|
||||
return { success: true };
|
||||
} else {
|
||||
console.error("删除板卡失败:影响行数为0");
|
||||
return { success: false, error: "删除板卡失败" };
|
||||
}
|
||||
} catch (e: any) {
|
||||
console.error("删除板卡异常:", e);
|
||||
if (e.status === 401) {
|
||||
return { success: false, error: "权限不足" };
|
||||
} else if (e.status === 400) {
|
||||
return { success: false, error: "输入参数错误" };
|
||||
} else {
|
||||
return { success: false, error: e.message || "删除板卡异常" };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 上传并固化位流
|
||||
async function uploadAndDownloadBitstreams(
|
||||
board: BoardData,
|
||||
goldBitstream?: File,
|
||||
appBitstream1?: File,
|
||||
appBitstream2?: File,
|
||||
appBitstream3?: File,
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
let cnt = 0;
|
||||
if (!isUndefined(goldBitstream)) cnt++;
|
||||
if (!isUndefined(appBitstream1)) cnt++;
|
||||
if (!isUndefined(appBitstream2)) cnt++;
|
||||
if (!isUndefined(appBitstream3)) cnt++;
|
||||
|
||||
if (cnt === 0) {
|
||||
console.error("未选择比特流文件");
|
||||
return { success: false, error: "未选择比特流文件" };
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("开始上传比特流", { boardIp: board.ipAddr, fileCount: cnt });
|
||||
|
||||
const uploadResult = await remoteUpdater.uploadBitstreams(
|
||||
board.ipAddr,
|
||||
Common.toFileParameterOrNull(goldBitstream),
|
||||
Common.toFileParameterOrNull(appBitstream1),
|
||||
Common.toFileParameterOrNull(appBitstream2),
|
||||
Common.toFileParameterOrNull(appBitstream3),
|
||||
);
|
||||
|
||||
if (!uploadResult) {
|
||||
console.error("上传比特流失败");
|
||||
return { success: false, error: "上传比特流失败" };
|
||||
}
|
||||
|
||||
console.log("比特流上传成功,开始固化");
|
||||
|
||||
const downloadResult = await remoteUpdater.downloadMultiBitstreams(
|
||||
board.ipAddr,
|
||||
board.port,
|
||||
getSelectedBitstreamNum(board.defaultBitstream),
|
||||
);
|
||||
|
||||
if (downloadResult != cnt) {
|
||||
console.error("固化比特流失败", { expected: cnt, actual: downloadResult });
|
||||
return { success: false, error: "固化比特流失败" };
|
||||
} else {
|
||||
console.log("固化比特流成功", { count: downloadResult });
|
||||
return { success: true };
|
||||
}
|
||||
} catch (e: any) {
|
||||
console.error("比特流操作异常:", e);
|
||||
return { success: false, error: e.message || "比特流操作异常" };
|
||||
}
|
||||
}
|
||||
|
||||
// 热启动位流
|
||||
async function hotresetBitstream(
|
||||
board: BoardData,
|
||||
bitstreamNum: number
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
try {
|
||||
console.log("开始热启动比特流", { boardIp: board.ipAddr, bitstreamNum });
|
||||
|
||||
const ret = await remoteUpdater.hotResetBitstream(
|
||||
board.ipAddr,
|
||||
board.port,
|
||||
bitstreamNum,
|
||||
);
|
||||
|
||||
if (ret) {
|
||||
console.log("热启动比特流成功");
|
||||
return { success: true };
|
||||
} else {
|
||||
console.error("热启动比特流失败");
|
||||
return { success: false, error: "热启动比特流失败" };
|
||||
}
|
||||
} catch (e: any) {
|
||||
console.error("热启动比特流异常:", e);
|
||||
return { success: false, error: e.message || "热启动比特流异常" };
|
||||
}
|
||||
}
|
||||
|
||||
// 处理文件上传
|
||||
function handleFileChange(
|
||||
event: Event,
|
||||
board: BoardData,
|
||||
fileKey: keyof Pick<
|
||||
BoardData,
|
||||
| "goldBitstreamFile"
|
||||
| "appBitstream1File"
|
||||
| "appBitstream2File"
|
||||
| "appBitstream3File"
|
||||
>,
|
||||
) {
|
||||
const target = event.target as HTMLInputElement;
|
||||
const file = target.files?.[0];
|
||||
if (file) {
|
||||
(board as any)[fileKey] = file;
|
||||
console.log(`文件选择成功`, { boardIp: board.ipAddr, fileKey, fileName: file.name });
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
boards,
|
||||
uploadAndDownloadBitstreams,
|
||||
hotresetBitstream,
|
||||
handleFileChange,
|
||||
getSelectedBitstreamNum,
|
||||
getAllBoards,
|
||||
addBoard,
|
||||
deleteBoard,
|
||||
};
|
||||
});
|
||||
|
||||
export { useProvideBoardManager, useBoardManager };
|
||||
@@ -1,30 +0,0 @@
|
||||
import Konva from "konva";
|
||||
import type { VueElement } from "vue";
|
||||
|
||||
interface VNode extends VueElement {
|
||||
getNode(): Konva.Node
|
||||
}
|
||||
|
||||
interface VLayer extends VueElement {
|
||||
getNode(): Konva.Layer
|
||||
}
|
||||
|
||||
interface VGroup extends VueElement {
|
||||
getNode(): Konva.Group
|
||||
}
|
||||
|
||||
interface VStage extends VueElement {
|
||||
getNode(): Konva.Stage
|
||||
}
|
||||
|
||||
interface VTransformer extends VueElement {
|
||||
getNode(): Konva.Transformer
|
||||
}
|
||||
|
||||
export type {
|
||||
VNode,
|
||||
VLayer,
|
||||
VGroup,
|
||||
VStage,
|
||||
VTransformer,
|
||||
}
|
||||
@@ -1,253 +0,0 @@
|
||||
<template>
|
||||
<div class="bg-base-200 min-h-screen p-6">
|
||||
<div class="flex flex-row justify-between items-center">
|
||||
<h1 class="text-3xl font-bold mb-6">FPGA 设备管理</h1>
|
||||
<button class="btn btn-ghost text-error hover:underline" @click="
|
||||
() => {
|
||||
isEditMode = !isEditMode;
|
||||
}
|
||||
">
|
||||
编辑
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="card bg-base-100 shadow-xl">
|
||||
<div class="card-body">
|
||||
<div class="flex flex-row justify-between items-center">
|
||||
<h2 class="card-title mb-4">IP 地址列表</h2>
|
||||
<button class="btn btn-ghost" @click="">刷新</button>
|
||||
</div>
|
||||
|
||||
<div class="overflow-x-auto">
|
||||
<table class="table w-full">
|
||||
<!-- 表头 -->
|
||||
<thead>
|
||||
<tr class="bg-base-300">
|
||||
<th class="w-50">IP 地址</th>
|
||||
<th class="w-30">版本号</th>
|
||||
<th class="w-50">默认启动位流</th>
|
||||
<th class="w-80">黄金位流</th>
|
||||
<th class="w-80">应用位流1</th>
|
||||
<th class="w-80">应用位流2</th>
|
||||
<th class="w-80">应用位流3</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<!-- 表格内容 -->
|
||||
<tbody>
|
||||
<tr class="hover">
|
||||
<td class="font-medium">
|
||||
<input v-if="isEditMode" type="text" placeholder="Type here" class="input m-0" v-model="devAddr" />
|
||||
<span v-else>{{ devAddr }}</span>
|
||||
</td>
|
||||
<td>v1.2.3</td>
|
||||
<td>
|
||||
<select class="select select-bordered w-full max-w-xs" v-model="selectBitstream">
|
||||
<option selected>黄金位流</option>
|
||||
<option>应用位流1</option>
|
||||
<option>应用位流2</option>
|
||||
<option>应用位流3</option>
|
||||
</select>
|
||||
</td>
|
||||
<!-- 黄金位流上传区 -->
|
||||
<td>
|
||||
<div class="flex flex-col items-center gap-2">
|
||||
<input type="file" class="file-input file-input-primary" @change="handleFileChange($event, 0)" />
|
||||
</div>
|
||||
</td>
|
||||
<!-- 应用位流1上传区 -->
|
||||
<td>
|
||||
<div class="flex flex-col items-center gap-2">
|
||||
<input type="file" class="file-input file-input-secondary" @change="handleFileChange($event, 1)" />
|
||||
</div>
|
||||
</td>
|
||||
<!-- 应用位流2上传区 -->
|
||||
<td>
|
||||
<div class="flex flex-col items-center gap-2">
|
||||
<input type="file" class="file-input file-input-accent" @change="handleFileChange($event, 2)" />
|
||||
</div>
|
||||
</td>
|
||||
<!-- 应用位流3上传区 -->
|
||||
<td>
|
||||
<div class="flex flex-col items-center gap-2">
|
||||
<input type="file" class="file-input file-input-info" @change="handleFileChange($event, 3)" />
|
||||
</div>
|
||||
</td>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<td class="flex gap-2">
|
||||
<button class="btn grow btn-warning" @click="
|
||||
uploadAndDownloadBitstreams(
|
||||
devAddr,
|
||||
goldBitstreamFile,
|
||||
appBitstream1File,
|
||||
appBitstream2File,
|
||||
appBitstream3File,
|
||||
)
|
||||
">
|
||||
固化
|
||||
</button>
|
||||
<button class="btn grow btn-success" @click="
|
||||
hotresetBitstream(devAddr, getSelectedBitstreamNum())
|
||||
">
|
||||
切换并热启动
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="mt-6 bg-base-300 p-4 rounded-lg">
|
||||
<p class="text-sm opacity-80">
|
||||
<span class="font-semibold text-error">提示:</span>
|
||||
请谨慎操作FPGA固化和热启动功能,确保上传的位流文件无误,以避免设备损坏。
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { isNull, isUndefined } from "lodash";
|
||||
import { ref } from "vue";
|
||||
import { RemoteUpdateClient } from "@/APIClient";
|
||||
import { useDialogStore } from "@/stores/dialog";
|
||||
import { Common } from "@/utils/Common";
|
||||
|
||||
const dialog = useDialogStore();
|
||||
|
||||
// 编辑状态
|
||||
const isEditMode = ref(false);
|
||||
|
||||
// 选择热切换的比特流
|
||||
const selectBitstream = ref("黄金位流");
|
||||
|
||||
// 存储上传文件的信息
|
||||
const goldBitstreamFile = ref<File>();
|
||||
const appBitstream1File = ref<File>();
|
||||
const appBitstream2File = ref<File>();
|
||||
const appBitstream3File = ref<File>();
|
||||
|
||||
// 远程升级相关参数
|
||||
const devAddr = ref("192.168.1.100");
|
||||
const devPort = 1234;
|
||||
const remoteUpdater = new RemoteUpdateClient();
|
||||
|
||||
// 处理文件上传
|
||||
function handleFileChange(event: Event, bistreamNum: number) {
|
||||
const target = event.target as HTMLInputElement;
|
||||
const file = target.files?.[0]; // 获取选中的第一个文件
|
||||
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (bistreamNum === 0) {
|
||||
goldBitstreamFile.value = file;
|
||||
} else if (bistreamNum === 1) {
|
||||
appBitstream1File.value = file;
|
||||
} else if (bistreamNum === 2) {
|
||||
appBitstream2File.value = file;
|
||||
} else if (bistreamNum === 3) {
|
||||
appBitstream3File.value = file;
|
||||
} else {
|
||||
goldBitstreamFile.value = file;
|
||||
}
|
||||
}
|
||||
|
||||
function getSelectedBitstreamNum(): number {
|
||||
if (selectBitstream.value == "黄金位流") {
|
||||
return 0;
|
||||
} else if (selectBitstream.value == "应用位流1") {
|
||||
return 1;
|
||||
} else if (selectBitstream.value == "应用位流2") {
|
||||
return 2;
|
||||
} else if (selectBitstream.value == "应用位流3") {
|
||||
return 3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
async function uploadAndDownloadBitstreams(
|
||||
devAddr: string,
|
||||
goldBitstream?: File,
|
||||
appBitstream1?: File,
|
||||
appBitstream2?: File,
|
||||
appBitstream3?: File,
|
||||
) {
|
||||
let cnt = 0;
|
||||
if (!isUndefined(goldBitstream)) cnt++;
|
||||
if (!isUndefined(appBitstream1)) cnt++;
|
||||
if (!isUndefined(appBitstream2)) cnt++;
|
||||
if (!isUndefined(appBitstream3)) cnt++;
|
||||
if (cnt === 0) {
|
||||
dialog.error("未选择比特流");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
{
|
||||
const ret = await remoteUpdater.uploadBitstreams(
|
||||
devAddr,
|
||||
Common.toFileParameterOrNull(goldBitstream),
|
||||
Common.toFileParameterOrNull(appBitstream1),
|
||||
Common.toFileParameterOrNull(appBitstream2),
|
||||
Common.toFileParameterOrNull(appBitstream3),
|
||||
);
|
||||
if (!ret) {
|
||||
dialog.warn("上传比特流出错");
|
||||
return;
|
||||
}
|
||||
}
|
||||
{
|
||||
const ret = await remoteUpdater.downloadMultiBitstreams(
|
||||
devAddr,
|
||||
devPort,
|
||||
getSelectedBitstreamNum(),
|
||||
);
|
||||
if (ret != cnt) {
|
||||
dialog.warn("固化比特流出错");
|
||||
} else {
|
||||
dialog.info("固化比特流成功");
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
dialog.error("比特流上传错误");
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function hotresetBitstream(devAddr: string, bitstreamNum: number) {
|
||||
try {
|
||||
const ret = await remoteUpdater.hotResetBitstream(
|
||||
devAddr,
|
||||
devPort,
|
||||
bitstreamNum,
|
||||
);
|
||||
if (ret) {
|
||||
dialog.info("切换比特流成功");
|
||||
} else {
|
||||
dialog.error("切换比特流失败");
|
||||
}
|
||||
} catch (e) {
|
||||
dialog.error("切换比特流失败");
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshData() {
|
||||
try {
|
||||
const ret = await remoteUpdater.getFirmwareVersion(devAddr.value, devPort);
|
||||
} catch (e) {
|
||||
dialog.error("获取数据失败");
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="postcss">
|
||||
@import "../assets/main.css";
|
||||
</style>
|
||||
288
src/views/AuthView.vue
Normal file
288
src/views/AuthView.vue
Normal file
@@ -0,0 +1,288 @@
|
||||
<template>
|
||||
<div class="flex items-center justify-center min-h-screen bg-base-200">
|
||||
<div class="relative w-full max-w-md">
|
||||
<!-- Login Card -->
|
||||
<div v-if="!showSignUp" class="card card-dash h-80 w-100 shadow-xl bg-base-100">
|
||||
<div class="card-body">
|
||||
<h1 class="card-title place-self-center my-3 text-2xl">用户登录</h1>
|
||||
<div class="flex flex-col w-full h-full">
|
||||
<label class="input w-full my-3">
|
||||
<User class="h-[1em] opacity-50" />
|
||||
<input
|
||||
type="text"
|
||||
class="grow"
|
||||
placeholder="用户名"
|
||||
v-model="username"
|
||||
@keyup.enter="handleLogin"
|
||||
/>
|
||||
</label>
|
||||
<label class="input w-full my-3">
|
||||
<Lock class="h-[1em] opacity-50" />
|
||||
<input
|
||||
type="password"
|
||||
class="grow"
|
||||
placeholder="密码"
|
||||
v-model="password"
|
||||
@keyup.enter="handleLogin"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<div class="flex justify-end mx-3">
|
||||
<RouterLink to="/">忘记密码?</RouterLink>
|
||||
</div>
|
||||
<div class="card-actions flex items-end my-3">
|
||||
<button class="btn flex-1" @click="handleRegister">注册</button>
|
||||
<button
|
||||
class="btn btn-primary flex-3"
|
||||
@click="handleLogin"
|
||||
:disabled="isLoading"
|
||||
>
|
||||
{{ isLoading ? "登录中..." : "登录" }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Sign Up Card -->
|
||||
<div v-if="showSignUp" class="card card-dash h-96 w-100 shadow-xl bg-base-100">
|
||||
<div class="card-body">
|
||||
<h1 class="card-title place-self-center my-3 text-2xl">用户注册</h1>
|
||||
<div class="flex flex-col w-full h-full">
|
||||
<label class="input w-full my-2">
|
||||
<User class="h-[1em] opacity-50" />
|
||||
<input
|
||||
type="text"
|
||||
class="grow"
|
||||
placeholder="用户名"
|
||||
v-model="signUpData.username"
|
||||
@keyup.enter="handleSignUp"
|
||||
/>
|
||||
</label>
|
||||
<label class="input w-full my-2">
|
||||
<Mail class="h-[1em] opacity-50" />
|
||||
<input
|
||||
type="email"
|
||||
class="grow"
|
||||
placeholder="邮箱"
|
||||
v-model="signUpData.email"
|
||||
@keyup.enter="handleSignUp"
|
||||
/>
|
||||
</label>
|
||||
<label class="input w-full my-2">
|
||||
<Lock class="h-[1em] opacity-50" />
|
||||
<input
|
||||
type="password"
|
||||
class="grow"
|
||||
placeholder="密码"
|
||||
v-model="signUpData.password"
|
||||
@keyup.enter="handleSignUp"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<div class="card-actions flex items-end my-3">
|
||||
<button class="btn flex-1" @click="backToLogin">返回登录</button>
|
||||
<button
|
||||
class="btn btn-primary flex-3"
|
||||
@click="handleSignUp"
|
||||
:disabled="isSignUpLoading"
|
||||
>
|
||||
{{ isSignUpLoading ? "注册中..." : "注册" }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import { AuthManager } from "@/utils/AuthManager";
|
||||
import { useAlertStore } from "@/components/Alert";
|
||||
import { User, Lock, Mail } from "lucide-vue-next";
|
||||
import { DataClient } from "@/APIClient";
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
// 获取Alert store
|
||||
const alertStore = useAlertStore();
|
||||
|
||||
// 创建API客户端实例
|
||||
const dataClient = new DataClient();
|
||||
|
||||
// 响应式数据
|
||||
const username = ref("");
|
||||
const password = ref("");
|
||||
const isLoading = ref(false);
|
||||
|
||||
// 注册相关数据
|
||||
const showSignUp = ref(false);
|
||||
const isSignUpLoading = ref(false);
|
||||
const signUpData = ref({
|
||||
username: "",
|
||||
email: "",
|
||||
password: ""
|
||||
});
|
||||
|
||||
// 登录处理函数
|
||||
const handleLogin = async () => {
|
||||
// 验证输入
|
||||
if (!username.value.trim()) {
|
||||
alertStore?.show("请输入用户名", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!password.value.trim()) {
|
||||
alertStore?.show("请输入密码", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
isLoading.value = true;
|
||||
|
||||
try {
|
||||
// 调用AuthManager的登录函数
|
||||
await AuthManager.login(username.value.trim(), password.value.trim());
|
||||
|
||||
// 登录成功,显示成功消息并跳转
|
||||
alertStore?.show("登录成功", "success", 1000);
|
||||
|
||||
// 短暂延迟后跳转到project页面
|
||||
setTimeout(async () => {
|
||||
await router.push("/project");
|
||||
}, 1000);
|
||||
} catch (error: any) {
|
||||
console.error("Login error:", error);
|
||||
|
||||
// 处理不同类型的错误
|
||||
let errorMessage = "登录失败,请检查网络连接";
|
||||
|
||||
if (error.status === 400) {
|
||||
errorMessage = "用户名或密码错误";
|
||||
} else if (error.status === 401) {
|
||||
errorMessage = "用户名或密码错误";
|
||||
} else if (error.status === 500) {
|
||||
errorMessage = "服务器错误,请稍后重试";
|
||||
} else if (error.message) {
|
||||
errorMessage = error.message;
|
||||
}
|
||||
|
||||
alertStore?.show(errorMessage, "error");
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 注册处理函数
|
||||
const handleRegister = () => {
|
||||
showSignUp.value = true;
|
||||
// 清空注册表单
|
||||
signUpData.value = {
|
||||
username: "",
|
||||
email: "",
|
||||
password: ""
|
||||
};
|
||||
};
|
||||
|
||||
// 返回登录页面
|
||||
const backToLogin = () => {
|
||||
showSignUp.value = false;
|
||||
};
|
||||
|
||||
// 注册提交处理函数
|
||||
const handleSignUp = async () => {
|
||||
// 验证输入
|
||||
if (!signUpData.value.username.trim()) {
|
||||
alertStore?.show("请输入用户名", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!signUpData.value.email.trim()) {
|
||||
alertStore?.show("请输入邮箱", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
// 简单的邮箱格式验证
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailRegex.test(signUpData.value.email.trim())) {
|
||||
alertStore?.show("请输入有效的邮箱地址", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!signUpData.value.password.trim()) {
|
||||
alertStore?.show("请输入密码", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
// 密码长度验证
|
||||
if (signUpData.value.password.length < 6) {
|
||||
alertStore?.show("密码长度至少6位", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
isSignUpLoading.value = true;
|
||||
|
||||
try {
|
||||
// 调用注册API
|
||||
const result = await dataClient.signUpUser(
|
||||
signUpData.value.username.trim(),
|
||||
signUpData.value.email.trim(),
|
||||
signUpData.value.password.trim()
|
||||
);
|
||||
|
||||
if (result) {
|
||||
// 注册成功
|
||||
alertStore?.show("注册成功!请登录", "success", 2000);
|
||||
|
||||
// 延迟后返回登录页面
|
||||
setTimeout(() => {
|
||||
backToLogin();
|
||||
}, 2000);
|
||||
} else {
|
||||
alertStore?.show("注册失败,请重试", "error");
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Sign up error:", error);
|
||||
|
||||
let errorMessage = "注册失败,请检查网络连接";
|
||||
|
||||
if (error.status === 400) {
|
||||
// 检查是否有详细的错误信息
|
||||
if (error.result && error.result.detail) {
|
||||
errorMessage = error.result.detail;
|
||||
} else {
|
||||
errorMessage = "注册信息无效,请检查输入";
|
||||
}
|
||||
} else if (error.status === 500) {
|
||||
errorMessage = "服务器错误,请稍后重试";
|
||||
} else if (error.message) {
|
||||
errorMessage = error.message;
|
||||
}
|
||||
|
||||
alertStore?.show(errorMessage, "error");
|
||||
} finally {
|
||||
isSignUpLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 页面初始化时检查是否已有有效token
|
||||
const checkExistingToken = async () => {
|
||||
try {
|
||||
const isValid = await AuthManager.verifyToken();
|
||||
if (isValid) {
|
||||
// 如果token仍然有效,直接跳转到project页面
|
||||
await router.push("/project");
|
||||
}
|
||||
} catch (error) {
|
||||
// token无效或验证失败,继续显示登录页面
|
||||
console.log("Token verification failed, showing login page");
|
||||
}
|
||||
};
|
||||
|
||||
// 组件挂载时检查已存在的token
|
||||
onMounted(() => {
|
||||
checkExistingToken();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped></style>
|
||||
@@ -1,19 +1,27 @@
|
||||
<template>
|
||||
<div class="bg-base-200 min-h-screen">
|
||||
<main class="hero min-h-screen bg-base-200">
|
||||
<div class="hero-content flex-col xl:flex-row-reverse gap-8 xl:gap-12 py-10 px-4"> <!-- 例程轮播容器 -->
|
||||
<div class="w-full flex justify-center" style="min-width: 650px;">
|
||||
<div
|
||||
class="hero-content flex-col xl:flex-row-reverse gap-8 xl:gap-12 py-10 px-4"
|
||||
>
|
||||
<!-- 例程轮播容器 -->
|
||||
<div class="w-full flex justify-center" style="min-width: 650px">
|
||||
<TutorialCarousel :autoRotationInterval="3000" />
|
||||
</div>
|
||||
<!-- 内容容器 -->
|
||||
<div class="content-container max-w-md lg:max-w-2xl transform transition-all duration-500 ease-in-out">
|
||||
<div
|
||||
class="content-container max-w-md lg:max-w-2xl transform transition-all duration-500 ease-in-out"
|
||||
>
|
||||
<h1 class="text-4xl md:text-5xl font-bold mb-3 relative group">
|
||||
<span class="relative z-10 bg-gradient-to-r from-primary to-secondary bg-clip-text text-transparent">
|
||||
<span
|
||||
class="relative z-10 bg-gradient-to-r from-primary to-secondary bg-clip-text text-transparent"
|
||||
>
|
||||
Welcome to
|
||||
</span>
|
||||
<span class="text-base-content">FPGA Web Lab!</span>
|
||||
<span
|
||||
class="absolute bottom-0 left-0 w-0 h-1 bg-primary transition-all duration-500 ease-in-out group-hover:w-3/4"></span>
|
||||
class="absolute bottom-0 left-0 w-0 h-1 bg-primary transition-all duration-500 ease-in-out group-hover:w-3/4"
|
||||
></span>
|
||||
</h1>
|
||||
|
||||
<p class="py-6 text-lg opacity-80 leading-relaxed">
|
||||
@@ -22,59 +30,17 @@
|
||||
designs seamlessly.
|
||||
</p>
|
||||
<div class="flex flex-wrap gap-4 actions-container">
|
||||
<router-link to="/project"
|
||||
class="btn btn-primary text-base-100 shadow-lg transform transition-all duration-300 hover:scale-105 hover:shadow-xl hover:-translate-y-1">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"></path>
|
||||
<path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"></path>
|
||||
</svg>
|
||||
<router-link
|
||||
to="/project"
|
||||
class="btn btn-primary text-base-100 shadow-lg transform transition-all duration-300 hover:scale-105 hover:shadow-xl hover:-translate-y-1"
|
||||
>
|
||||
<BookOpen class="h-5 w-5 mr-2" />
|
||||
进入工程界面
|
||||
</router-link>
|
||||
|
||||
<router-link to="/login"
|
||||
class="btn btn-secondary text-base-100 shadow-lg transform transition-all duration-300 hover:scale-105 hover:shadow-xl hover:-translate-y-1">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
|
||||
<path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
|
||||
</svg>
|
||||
登录
|
||||
</router-link>
|
||||
|
||||
<router-link to="/user"
|
||||
class="btn btn-accent text-base-100 shadow-lg transform transition-all duration-300 hover:scale-105 hover:shadow-xl hover:-translate-y-1">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
|
||||
<circle cx="12" cy="7" r="4"></circle>
|
||||
</svg>
|
||||
用户中心
|
||||
</router-link>
|
||||
<router-link to="/test"
|
||||
class="btn btn-info text-base-100 shadow-lg transform transition-all duration-300 hover:scale-105 hover:shadow-xl hover:-translate-y-1">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"></path>
|
||||
<polyline points="17 21 17 13 7 13 7 21"></polyline>
|
||||
<polyline points="7 3 7 8 15 8"></polyline>
|
||||
</svg>
|
||||
测试功能
|
||||
</router-link>
|
||||
|
||||
<router-link to="/admin"
|
||||
class="btn btn-error text-base-100 shadow-lg transform transition-all duration-300 hover:scale-105 hover:shadow-xl hover:-translate-y-1">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2">
|
||||
<path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5z">
|
||||
</path>
|
||||
<circle cx="12" cy="12" r="3"></circle>
|
||||
</svg>
|
||||
管理控制台
|
||||
</router-link>
|
||||
</div>
|
||||
<div
|
||||
class="mt-8 p-4 bg-base-300 rounded-lg shadow-inner opacity-80 transition-all duration-300 hover:opacity-100 hover:shadow-md">
|
||||
class="mt-8 p-4 bg-base-300 rounded-lg shadow-inner opacity-80 transition-all duration-300 hover:opacity-100 hover:shadow-md"
|
||||
>
|
||||
<p class="text-sm">
|
||||
<span class="font-semibold text-primary">提示:</span>
|
||||
您可以在工程界面中创建、编辑和测试您的FPGA项目,使用我们简洁直观的界面轻松进行硬件设计。
|
||||
@@ -89,6 +55,7 @@
|
||||
<script lang="ts" setup>
|
||||
import "@/router";
|
||||
import TutorialCarousel from "@/components/TutorialCarousel.vue";
|
||||
import { BookOpen } from "lucide-vue-next";
|
||||
</script>
|
||||
|
||||
<style scoped lang="postcss">
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
<template>
|
||||
<div>
|
||||
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
|
||||
</script>
|
||||
|
||||
<style scoped lang="postcss"></style>
|
||||
@@ -1,15 +0,0 @@
|
||||
<template>
|
||||
<main>
|
||||
<div class="flex items-center justify-center min-h-screen">
|
||||
<div class="relative w-full max-w-md">
|
||||
<LoginCard />
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import LoginCard from "@/components/LoginCard.vue";
|
||||
</script>
|
||||
|
||||
<style scoped></style>
|
||||
123
src/views/Project/BottomBar.vue
Normal file
123
src/views/Project/BottomBar.vue
Normal file
@@ -0,0 +1,123 @@
|
||||
<template>
|
||||
<div class="h-full flex flex-col gap-7">
|
||||
<div class="tabs tabs-box flex-shrink-0 shadow-xl">
|
||||
<label class="tab">
|
||||
<input
|
||||
type="radio"
|
||||
name="function-bar"
|
||||
id="1"
|
||||
checked
|
||||
@change="handleTabChange"
|
||||
/>
|
||||
<TerminalIcon class="icon" />
|
||||
日志终端
|
||||
</label>
|
||||
<label class="tab">
|
||||
<input
|
||||
type="radio"
|
||||
name="function-bar"
|
||||
id="2"
|
||||
@change="handleTabChange"
|
||||
/>
|
||||
<VideoIcon class="icon" />
|
||||
HTTP视频流
|
||||
</label>
|
||||
<label class="tab">
|
||||
<input
|
||||
type="radio"
|
||||
name="function-bar"
|
||||
id="3"
|
||||
@change="handleTabChange"
|
||||
/>
|
||||
<SquareActivityIcon class="icon" />
|
||||
示波器
|
||||
</label>
|
||||
<!-- 全屏按钮 -->
|
||||
<button
|
||||
class="fullscreen-btn ml-auto btn btn-ghost btn-sm"
|
||||
@click="toggleFullscreen"
|
||||
:title="isFullscreen ? '退出全屏' : '全屏'"
|
||||
>
|
||||
<MaximizeIcon v-if="!isFullscreen" class="icon" />
|
||||
<MinimizeIcon v-else class="icon" />
|
||||
</button>
|
||||
</div>
|
||||
<!-- 主页面 -->
|
||||
<div class="flex-1 overflow-hidden">
|
||||
<div v-if="checkID === 1" class="h-full overflow-y-auto"></div>
|
||||
<div v-else-if="checkID === 2" class="h-full overflow-y-auto">
|
||||
<VideoStreamView />
|
||||
</div>
|
||||
<div v-else-if="checkID === 3" class="h-full overflow-y-auto">
|
||||
<OscilloscopeView />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
VideoIcon,
|
||||
SquareActivityIcon,
|
||||
TerminalIcon,
|
||||
MaximizeIcon,
|
||||
MinimizeIcon,
|
||||
} from "lucide-vue-next";
|
||||
import VideoStreamView from "@/views/Project/VideoStream.vue";
|
||||
import OscilloscopeView from "@/views/Project/Oscilloscope.vue";
|
||||
import { isNull, toNumber } from "lodash";
|
||||
import { ref, watch } from "vue";
|
||||
|
||||
const checkID = ref(1);
|
||||
|
||||
// 定义事件
|
||||
const emit = defineEmits<{
|
||||
toggleFullscreen: [];
|
||||
}>();
|
||||
|
||||
// 接收父组件传递的全屏状态
|
||||
const props = defineProps<{
|
||||
isFullscreen?: boolean;
|
||||
}>();
|
||||
|
||||
const isFullscreen = ref(props.isFullscreen || false);
|
||||
|
||||
// 监听props变化
|
||||
watch(
|
||||
() => props.isFullscreen,
|
||||
(newVal) => {
|
||||
isFullscreen.value = newVal || false;
|
||||
},
|
||||
);
|
||||
|
||||
function handleTabChange(event: Event) {
|
||||
const target = event.currentTarget as HTMLInputElement;
|
||||
if (isNull(target)) return;
|
||||
|
||||
checkID.value = toNumber(target.id);
|
||||
}
|
||||
|
||||
function toggleFullscreen() {
|
||||
emit("toggleFullscreen");
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="postcss">
|
||||
@import "@/assets/main.css";
|
||||
|
||||
.icon {
|
||||
@apply h-4 w-4 opacity-70 mr-1.5;
|
||||
}
|
||||
|
||||
.tabs {
|
||||
@apply relative flex items-center;
|
||||
}
|
||||
|
||||
.fullscreen-btn {
|
||||
@apply flex items-center justify-center p-2 rounded-lg transition-colors;
|
||||
}
|
||||
|
||||
.fullscreen-btn .icon {
|
||||
@apply mr-0;
|
||||
}
|
||||
</style>
|
||||
408
src/views/Project/Index.vue
Normal file
408
src/views/Project/Index.vue
Normal file
@@ -0,0 +1,408 @@
|
||||
<template>
|
||||
<div class="h-screen flex flex-col overflow-hidden">
|
||||
<div class="flex flex-1 overflow-hidden relative">
|
||||
<SplitterGroup
|
||||
id="splitter-group-v"
|
||||
direction="vertical"
|
||||
class="w-full h-full"
|
||||
@layout="handleVerticalSplitterResize"
|
||||
>
|
||||
<!-- 使用 v-show 替代 v-if -->
|
||||
<SplitterPanel
|
||||
v-show="!isBottomBarFullscreen"
|
||||
id="splitter-group-v-panel-project"
|
||||
:default-size="verticalSplitterSize"
|
||||
>
|
||||
<SplitterGroup
|
||||
id="splitter-group-h"
|
||||
direction="horizontal"
|
||||
class="w-full h-full"
|
||||
@layout="handleHorizontalSplitterResize"
|
||||
>
|
||||
<!-- 左侧图形化区域 -->
|
||||
<SplitterPanel
|
||||
id="splitter-group-h-panel-canvas"
|
||||
:default-size="horizontalSplitterSize"
|
||||
:min-size="30"
|
||||
class="relative bg-base-200 overflow-hidden h-full"
|
||||
>
|
||||
<DiagramCanvas
|
||||
ref="diagramCanvas"
|
||||
:showDocPanel="showDocPanel"
|
||||
@diagram-updated="handleDiagramUpdated"
|
||||
@open-components="openComponentsMenu"
|
||||
@toggle-doc-panel="toggleDocPanel"
|
||||
/>
|
||||
</SplitterPanel>
|
||||
<!-- 拖拽分割线 -->
|
||||
<SplitterResizeHandle
|
||||
id="splitter-group-h-resize-handle"
|
||||
class="w-2 bg-base-100 hover:bg-primary hover:opacity-70 transition-colors"
|
||||
/>
|
||||
<!-- 右侧编辑区域 -->
|
||||
<SplitterPanel
|
||||
id="splitter-group-h-panel-properties"
|
||||
:min-size="20"
|
||||
class="bg-base-200 h-full overflow-hidden flex flex-col"
|
||||
>
|
||||
<div class="overflow-y-auto flex-1">
|
||||
<!-- 使用条件渲染显示不同的面板 -->
|
||||
<PropertyPanel
|
||||
v-show="!showDocPanel"
|
||||
:componentData="componentManager.selectedComponentData.value"
|
||||
:componentConfig="
|
||||
componentManager.selectedComponentConfig.value
|
||||
"
|
||||
@updateProp="updateComponentProp"
|
||||
@updateDirectProp="updateComponentDirectProp"
|
||||
/>
|
||||
<div
|
||||
v-show="showDocPanel"
|
||||
class="doc-panel overflow-y-auto h-full"
|
||||
>
|
||||
<MarkdownRenderer :content="documentContent" />
|
||||
</div>
|
||||
</div>
|
||||
</SplitterPanel>
|
||||
</SplitterGroup>
|
||||
</SplitterPanel>
|
||||
|
||||
<!-- 分割线也使用 v-show -->
|
||||
<SplitterResizeHandle
|
||||
v-show="!isBottomBarFullscreen"
|
||||
id="splitter-group-v-resize-handle"
|
||||
class="h-2 bg-base-100 hover:bg-primary hover:opacity-70 transition-colors"
|
||||
/>
|
||||
|
||||
<!-- 功能底栏 -->
|
||||
<SplitterPanel
|
||||
id="splitter-group-v-panel-bar"
|
||||
:default-size="isBottomBarFullscreen ? 100 : (100 - verticalSplitterSize)"
|
||||
:min-size="isBottomBarFullscreen ? 100 : 15"
|
||||
class="w-full overflow-hidden px-5 pt-3"
|
||||
>
|
||||
<BottomBar
|
||||
:isFullscreen="isBottomBarFullscreen"
|
||||
@toggle-fullscreen="handleToggleBottomBarFullscreen"
|
||||
/>
|
||||
</SplitterPanel>
|
||||
</SplitterGroup>
|
||||
</div>
|
||||
<!-- 元器件选择组件 -->
|
||||
<ComponentSelector
|
||||
:open="showComponentsMenu"
|
||||
@update:open="showComponentsMenu = $event"
|
||||
@add-component="handleAddComponent"
|
||||
@add-template="handleAddTemplate"
|
||||
@close="showComponentsMenu = false"
|
||||
/>
|
||||
|
||||
<!-- 实验板申请对话框 -->
|
||||
<RequestBoardDialog
|
||||
:open="showRequestBoardDialog"
|
||||
@close="handleRequestBoardClose"
|
||||
@success="handleRequestBoardSuccess"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, watch } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import { useLocalStorage } from '@vueuse/core'; // 添加VueUse导入
|
||||
import { SplitterGroup, SplitterPanel, SplitterResizeHandle } from "reka-ui";
|
||||
import DiagramCanvas from "@/components/LabCanvas/DiagramCanvas.vue";
|
||||
import ComponentSelector from "@/components/LabCanvas/ComponentSelector.vue";
|
||||
import PropertyPanel from "@/components/PropertyPanel.vue";
|
||||
import MarkdownRenderer from "@/components/MarkdownRenderer.vue";
|
||||
import BottomBar from "@/views/Project/BottomBar.vue";
|
||||
import RequestBoardDialog from "@/views/Project/RequestBoardDialog.vue";
|
||||
import { useProvideComponentManager } from "@/components/LabCanvas";
|
||||
import type { DiagramData } from "@/components/LabCanvas";
|
||||
import { useAlertStore } from "@/components/Alert";
|
||||
import { AuthManager } from "@/utils/AuthManager";
|
||||
import { useEquipments } from "@/stores/equipments";
|
||||
import type { Board } from "@/APIClient";
|
||||
|
||||
import { useRoute } from "vue-router";
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
|
||||
// 提供组件管理服务
|
||||
const componentManager = useProvideComponentManager();
|
||||
|
||||
// 设备管理store
|
||||
const equipments = useEquipments();
|
||||
|
||||
const alert = useAlertStore();
|
||||
|
||||
// --- 使用VueUse保存分栏状态 ---
|
||||
// 左右分栏比例(默认60%)
|
||||
const horizontalSplitterSize = useLocalStorage('project-horizontal-splitter-size', 60);
|
||||
// 上下分栏比例(默认80%)
|
||||
const verticalSplitterSize = useLocalStorage('project-vertical-splitter-size', 80);
|
||||
// 底栏全屏状态
|
||||
const isBottomBarFullscreen = useLocalStorage('project-bottom-bar-fullscreen', false);
|
||||
// 文档面板显示状态
|
||||
const showDocPanel = useLocalStorage('project-show-doc-panel', false);
|
||||
|
||||
function handleToggleBottomBarFullscreen() {
|
||||
isBottomBarFullscreen.value = !isBottomBarFullscreen.value;
|
||||
}
|
||||
|
||||
// --- 处理分栏大小变化 ---
|
||||
function handleHorizontalSplitterResize(sizes: number[]) {
|
||||
if (sizes && sizes.length > 0) {
|
||||
horizontalSplitterSize.value = sizes[0];
|
||||
}
|
||||
}
|
||||
|
||||
function handleVerticalSplitterResize(sizes: number[]) {
|
||||
if (sizes && sizes.length > 0) {
|
||||
verticalSplitterSize.value = sizes[0];
|
||||
}
|
||||
}
|
||||
|
||||
// --- 实验板申请对话框 ---
|
||||
const showRequestBoardDialog = ref(false);
|
||||
|
||||
// --- 文档面板控制 ---
|
||||
const documentContent = ref("");
|
||||
|
||||
// 切换文档面板和属性面板
|
||||
async function toggleDocPanel() {
|
||||
showDocPanel.value = !showDocPanel.value;
|
||||
|
||||
// 如果切换到文档面板,则获取文档内容
|
||||
if (showDocPanel.value) {
|
||||
await loadDocumentContent();
|
||||
}
|
||||
}
|
||||
|
||||
// 加载文档内容
|
||||
async function loadDocumentContent() {
|
||||
try {
|
||||
// 从路由参数中获取教程ID
|
||||
const tutorialId = (route.query.tutorial as string) || "02"; // 默认加载02例程
|
||||
|
||||
// 构建文档路径
|
||||
let docPath = `/doc/${tutorialId}/doc.md`;
|
||||
|
||||
// 检查当前路径是否包含下划线(例如 02_key 格式)
|
||||
// 如果不包含,那么使用更新的命名格式
|
||||
if (!tutorialId.includes("_")) {
|
||||
docPath = `/doc/${tutorialId}/doc.md`;
|
||||
}
|
||||
|
||||
// 获取文档内容
|
||||
const response = await fetch(docPath);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load document: ${response.status}`);
|
||||
}
|
||||
|
||||
// 更新文档内容,并替换图片路径
|
||||
documentContent.value = (await response.text()).replace(
|
||||
/.\/images/gi,
|
||||
`/doc/${tutorialId}/images`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("加载文档失败:", error);
|
||||
documentContent.value = "# 文档加载失败\n\n无法加载请求的文档。";
|
||||
}
|
||||
}
|
||||
|
||||
// --- UI 状态管理 ---
|
||||
const showComponentsMenu = ref(false);
|
||||
const diagramCanvas = ref(null);
|
||||
|
||||
function openComponentsMenu() {
|
||||
showComponentsMenu.value = true;
|
||||
}
|
||||
|
||||
// 处理 ComponentSelector 组件添加元器件事件
|
||||
async function handleAddComponent(componentData: {
|
||||
type: string;
|
||||
name: string;
|
||||
props: Record<string, any>;
|
||||
}) {
|
||||
await componentManager.addComponent(componentData);
|
||||
}
|
||||
|
||||
// 处理模板添加事件
|
||||
async function handleAddTemplate(templateData: {
|
||||
id: string;
|
||||
name: string;
|
||||
template: any;
|
||||
}) {
|
||||
const result = await componentManager.addTemplate(templateData);
|
||||
if (result) {
|
||||
alert?.show(result.message, result.success ? "success" : "error");
|
||||
}
|
||||
}
|
||||
|
||||
// 处理图表数据更新事件
|
||||
function handleDiagramUpdated(data: DiagramData) {
|
||||
console.log("Diagram data updated:", data);
|
||||
}
|
||||
|
||||
// 更新组件属性的方法 - 委托给componentManager
|
||||
function updateComponentProp(
|
||||
componentId: string,
|
||||
propName: string,
|
||||
value: any,
|
||||
) {
|
||||
componentManager.updateComponentProp(componentId, propName, value);
|
||||
}
|
||||
|
||||
// 更新组件的直接属性 - 委托给componentManager
|
||||
function updateComponentDirectProp(
|
||||
componentId: string,
|
||||
propName: string,
|
||||
value: any,
|
||||
) {
|
||||
componentManager.updateComponentDirectProp(componentId, propName, value);
|
||||
}
|
||||
|
||||
// --- 实验板管理 ---
|
||||
// 检查并初始化用户实验板
|
||||
async function checkAndInitializeBoard() {
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
const userInfo = await client.getUserInfo();
|
||||
|
||||
if (userInfo.boardID && userInfo.boardID.trim() !== '') {
|
||||
// 用户已绑定实验板,获取实验板信息并更新到equipment
|
||||
try {
|
||||
const board = await client.getBoardByID(userInfo.boardID);
|
||||
updateEquipmentFromBoard(board);
|
||||
alert?.show(`实验板 ${board.boardName} 已连接`, "success");
|
||||
} catch (boardError) {
|
||||
console.error('获取实验板信息失败:', boardError);
|
||||
alert?.show("获取实验板信息失败", "error");
|
||||
showRequestBoardDialog.value = true;
|
||||
}
|
||||
} else {
|
||||
// 用户未绑定实验板,显示申请对话框
|
||||
showRequestBoardDialog.value = true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('检查用户实验板失败:', error);
|
||||
alert?.show("检查用户信息失败", "error");
|
||||
showRequestBoardDialog.value = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 根据实验板信息更新equipment store
|
||||
function updateEquipmentFromBoard(board: Board) {
|
||||
equipments.setAddr(board.ipAddr);
|
||||
equipments.setPort(board.port);
|
||||
|
||||
console.log(`实验板信息已更新到equipment store:`, {
|
||||
address: board.ipAddr,
|
||||
port: board.port,
|
||||
boardName: board.boardName,
|
||||
boardId: board.id
|
||||
});
|
||||
}
|
||||
|
||||
// 处理申请实验板对话框关闭
|
||||
function handleRequestBoardClose() {
|
||||
showRequestBoardDialog.value = false;
|
||||
// 如果用户取消申请,可以选择返回上一页或显示警告
|
||||
router.push('/');
|
||||
}
|
||||
|
||||
// 处理申请实验板成功
|
||||
function handleRequestBoardSuccess(board: Board) {
|
||||
showRequestBoardDialog.value = false;
|
||||
updateEquipmentFromBoard(board);
|
||||
alert?.show(`实验板 ${board.boardName} 申请成功!`, "success");
|
||||
}
|
||||
|
||||
// --- 生命周期钩子 ---
|
||||
onMounted(async () => {
|
||||
// 验证用户身份
|
||||
try {
|
||||
const isAuthenticated = await AuthManager.isAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
// 验证失败,跳转到登录页面
|
||||
router.push('/login');
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('身份验证失败:', error);
|
||||
router.push('/login');
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查并初始化用户实验板
|
||||
await checkAndInitializeBoard();
|
||||
|
||||
// 检查是否有例程参数,如果有则自动打开文档面板
|
||||
if (route.query.tutorial) {
|
||||
showDocPanel.value = true;
|
||||
await loadDocumentContent();
|
||||
}
|
||||
|
||||
// 设置画布引用并初始化组件管理器
|
||||
componentManager.setCanvasRef(diagramCanvas.value);
|
||||
await componentManager.initialize();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="postcss">
|
||||
/* 样式保持不变 */
|
||||
@import "@/assets/main.css";
|
||||
|
||||
.animate-slideRight {
|
||||
animation: slideRight 0.3s ease-out forwards;
|
||||
}
|
||||
|
||||
@keyframes slideRight {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateX(30px);
|
||||
}
|
||||
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateX(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* 确保滚动行为仅在需要时出现 */
|
||||
html,
|
||||
body {
|
||||
overflow: hidden;
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
/* 文档面板样式 */
|
||||
.doc-panel {
|
||||
padding: 1.5rem;
|
||||
max-width: 100%;
|
||||
margin: 0;
|
||||
background-color: transparent;
|
||||
/* 使用透明背景 */
|
||||
border: none;
|
||||
/* 确保没有边框 */
|
||||
}
|
||||
|
||||
/* 文档切换按钮样式 */
|
||||
.doc-toggle-btn {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
right: 10px;
|
||||
z-index: 50;
|
||||
}
|
||||
|
||||
/* Markdown渲染样式调整 */
|
||||
:deep(.markdown-content) {
|
||||
padding: 1rem;
|
||||
background-color: hsl(var(--b1));
|
||||
border-radius: 0.5rem;
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
</style>
|
||||
23
src/views/Project/Oscilloscope.vue
Normal file
23
src/views/Project/Oscilloscope.vue
Normal file
@@ -0,0 +1,23 @@
|
||||
<template>
|
||||
<div class="bg-base-100 flex flex-col">
|
||||
<!-- 波形展示 -->
|
||||
<div class="card bg-base-200 shadow-xl">
|
||||
<div class="card-body">
|
||||
<h2 class="card-title">
|
||||
<Activity class="w-5 h-5" />
|
||||
波形显示
|
||||
</h2>
|
||||
<WaveformDisplay :data="generateTestData()" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { Activity } from "lucide-vue-next";
|
||||
import { WaveformDisplay, generateTestData } from "@/components/Oscilloscope";
|
||||
import { useEquipments } from "@/stores/equipments";
|
||||
|
||||
// 使用全局设备配置
|
||||
const equipments = useEquipments();
|
||||
</script>
|
||||
163
src/views/Project/RequestBoardDialog.vue
Normal file
163
src/views/Project/RequestBoardDialog.vue
Normal file
@@ -0,0 +1,163 @@
|
||||
<template>
|
||||
<div
|
||||
v-if="open"
|
||||
class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50"
|
||||
>
|
||||
<div class="bg-base-100 rounded-lg shadow-xl max-w-md w-full mx-4">
|
||||
<div class="p-6">
|
||||
<h2 class="text-xl font-bold mb-4">申请实验板</h2>
|
||||
|
||||
<div v-if="!loading && !hasBoard" class="space-y-4">
|
||||
<p class="text-base-content">
|
||||
检测到您尚未绑定实验板,请申请一个可用的实验板以继续实验。
|
||||
</p>
|
||||
|
||||
<div class="flex justify-end space-x-2">
|
||||
<button
|
||||
@click="$emit('close')"
|
||||
class="btn btn-ghost"
|
||||
:disabled="requesting"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
@click="requestBoard"
|
||||
class="btn btn-primary"
|
||||
:disabled="requesting"
|
||||
>
|
||||
<span
|
||||
v-if="requesting"
|
||||
class="loading loading-spinner loading-sm"
|
||||
></span>
|
||||
{{ requesting ? "申请中..." : "申请实验板" }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-else-if="loading" class="text-center py-8">
|
||||
<span class="loading loading-spinner loading-lg"></span>
|
||||
<p class="mt-2">检查实验板状态中...</p>
|
||||
</div>
|
||||
|
||||
<div v-else-if="hasBoard" class="space-y-4">
|
||||
<div class="bg-base-200 p-4 rounded">
|
||||
<h3 class="font-semibold mb-2">实验板信息</h3>
|
||||
<div class="space-y-1 text-sm">
|
||||
<p>
|
||||
<span class="font-medium">名称:</span>
|
||||
{{ boardInfo?.boardName }}
|
||||
</p>
|
||||
<p><span class="font-medium">ID:</span> {{ boardInfo?.id }}</p>
|
||||
<p>
|
||||
<span class="font-medium">地址:</span>
|
||||
{{ boardInfo?.ipAddr }}:{{ boardInfo?.port }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex justify-end">
|
||||
<button
|
||||
v-if="boardInfo"
|
||||
@click="$emit('success', boardInfo)"
|
||||
class="btn btn-primary"
|
||||
>
|
||||
开始实验
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, watch } from "vue";
|
||||
import { CheckCircle } from "lucide-vue-next";
|
||||
import { AuthManager } from "@/utils/AuthManager";
|
||||
import { useAlertStore } from "@/components/Alert";
|
||||
import type { Board } from "@/APIClient";
|
||||
|
||||
interface Props {
|
||||
open: boolean;
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: "close"): void;
|
||||
(e: "success", board: Board): void;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
const alert = useAlertStore();
|
||||
|
||||
const loading = ref(false);
|
||||
const requesting = ref(false);
|
||||
const hasBoard = ref(false);
|
||||
const boardInfo = ref<Board | null>(null);
|
||||
|
||||
// 监听对话框打开状态,自动检查用户信息
|
||||
watch(
|
||||
() => props.open,
|
||||
async (newOpen) => {
|
||||
if (newOpen) {
|
||||
await checkUserBoard();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// 检查用户是否已绑定实验板
|
||||
async function checkUserBoard() {
|
||||
loading.value = true;
|
||||
hasBoard.value = false;
|
||||
boardInfo.value = null;
|
||||
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
const userInfo = await client.getUserInfo();
|
||||
|
||||
if (userInfo.boardID && userInfo.boardID.trim() !== "") {
|
||||
// 用户已绑定实验板,获取实验板信息
|
||||
try {
|
||||
const board = await client.getBoardByID(userInfo.boardID);
|
||||
boardInfo.value = board;
|
||||
hasBoard.value = true;
|
||||
} catch (boardError) {
|
||||
console.error("获取实验板信息失败:", boardError);
|
||||
alert?.error("获取实验板信息失败,请重试");
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("检查用户信息失败:", err);
|
||||
alert?.error("检查用户信息失败,请重试");
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 申请实验板
|
||||
async function requestBoard() {
|
||||
requesting.value = true;
|
||||
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
const board = await client.getAvailableBoard(undefined);
|
||||
|
||||
if (board) {
|
||||
boardInfo.value = board;
|
||||
hasBoard.value = true;
|
||||
} else {
|
||||
alert?.error("当前没有可用的实验板,请稍后重试");
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error("申请实验板失败:", err);
|
||||
if (err.status === 404) {
|
||||
alert?.error("当前没有可用的实验板,请稍后重试");
|
||||
} else {
|
||||
alert?.error("申请实验板失败,请重试");
|
||||
}
|
||||
} finally {
|
||||
requesting.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
604
src/views/Project/VideoStream.vue
Normal file
604
src/views/Project/VideoStream.vue
Normal file
@@ -0,0 +1,604 @@
|
||||
<template>
|
||||
<div class="bg-base-100 flex flex-col gap-7">
|
||||
<!-- 控制面板 -->
|
||||
<div class="card bg-base-200 shadow-xl">
|
||||
<div class="card-body">
|
||||
<h2 class="card-title text-primary">
|
||||
<Settings class="w-6 h-6" />
|
||||
控制面板
|
||||
</h2>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||
<!-- 服务状态 -->
|
||||
<div class="stats shadow">
|
||||
<div class="stat bg-base-100">
|
||||
<div class="stat-figure text-primary">
|
||||
<div
|
||||
class="badge"
|
||||
:class="
|
||||
statusInfo.isRunning ? 'badge-success' : 'badge-error'
|
||||
"
|
||||
>
|
||||
{{ statusInfo.isRunning ? "运行中" : "已停止" }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="stat-title">服务状态</div>
|
||||
<div class="stat-value text-primary">HTTP</div>
|
||||
<div class="stat-desc">端口: {{ statusInfo.serverPort }}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 流信息 -->
|
||||
<div class="stats shadow">
|
||||
<div class="stat bg-base-100">
|
||||
<div class="stat-figure text-secondary">
|
||||
<Video class="w-8 h-8" />
|
||||
</div>
|
||||
<div class="stat-title">视频规格</div>
|
||||
<div class="stat-value text-secondary">
|
||||
{{ streamInfo.frameWidth }}×{{ streamInfo.frameHeight }}
|
||||
</div>
|
||||
<div class="stat-desc">{{ streamInfo.frameRate }} FPS</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 连接数 -->
|
||||
<div class="stats shadow">
|
||||
<div class="stat bg-base-100 relative">
|
||||
<div class="stat-figure text-accent">
|
||||
<Users class="w-8 h-8" />
|
||||
</div>
|
||||
<div class="stat-title">连接数</div>
|
||||
<div class="stat-value text-accent">
|
||||
{{ statusInfo.connectedClients }}
|
||||
</div>
|
||||
<div class="stat-desc">
|
||||
<div class="dropdown dropdown-hover dropdown-top">
|
||||
<div
|
||||
tabindex="0"
|
||||
role="button"
|
||||
class="text-xs underline cursor-help"
|
||||
>
|
||||
查看客户端
|
||||
</div>
|
||||
<ul
|
||||
tabindex="0"
|
||||
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 statusInfo.clientEndpoints"
|
||||
:key="index"
|
||||
class="text-xs"
|
||||
>
|
||||
<a class="break-all">{{ client }}</a>
|
||||
</li>
|
||||
<li
|
||||
v-if="
|
||||
!statusInfo.clientEndpoints ||
|
||||
statusInfo.clientEndpoints.length === 0
|
||||
"
|
||||
>
|
||||
<a class="text-xs opacity-50">无活跃连接</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<div class="card-actions justify-end mt-4">
|
||||
<button
|
||||
class="btn btn-outline btn-primary"
|
||||
@click="configCamera"
|
||||
:dsiabled="configing"
|
||||
>
|
||||
<RefreshCw v-if="configing" class="animate-spin h-4 w-4 mr-2" />
|
||||
<CogIcon v-else class="h-4 w-4 mr-2" />
|
||||
{{ configing ? "配置中..." : "配置摄像头" }}
|
||||
</button>
|
||||
<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-else class="h-4 w-4 mr-2" />
|
||||
{{ loading ? "刷新中..." : "刷新状态" }}
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-primary"
|
||||
@click="testConnection"
|
||||
:disabled="testing"
|
||||
>
|
||||
<RefreshCw v-if="testing" class="animate-spin h-4 w-4 mr-2" />
|
||||
<TestTube v-else class="h-4 w-4 mr-2" />
|
||||
{{ testing ? "测试中..." : "测试连接" }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 视频预览区域 -->
|
||||
<div class="card bg-base-200 shadow-xl">
|
||||
<div class="card-body">
|
||||
<h2 class="card-title text-primary">
|
||||
<Video class="w-6 h-6" />
|
||||
视频预览
|
||||
</h2>
|
||||
|
||||
<div
|
||||
class="relative bg-black rounded-lg overflow-hidden"
|
||||
style="aspect-ratio: 4/3"
|
||||
>
|
||||
<!-- 视频播放器 - 使用img标签直接显示MJPEG流 -->
|
||||
<div
|
||||
v-show="isPlaying"
|
||||
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
|
||||
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-body">
|
||||
<h3 class="card-title flex items-center gap-2">
|
||||
<AlertTriangle class="h-6 w-6" />
|
||||
视频流加载失败
|
||||
</h3>
|
||||
<p>无法连接到视频服务器,请检查以下内容:</p>
|
||||
<ul class="list-disc list-inside">
|
||||
<li>视频流服务是否已启动</li>
|
||||
<li>网络连接是否正常</li>
|
||||
<li>端口 {{ statusInfo.serverPort }} 是否可访问</li>
|
||||
</ul>
|
||||
<div class="card-actions justify-end mt-2">
|
||||
<button
|
||||
class="btn btn-sm btn-outline btn-primary"
|
||||
@click="tryReconnect"
|
||||
>
|
||||
重试连接
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 占位符 -->
|
||||
<div
|
||||
v-show="!isPlaying && !hasVideoError"
|
||||
class="absolute inset-0 flex items-center justify-center text-white"
|
||||
>
|
||||
<div class="text-center">
|
||||
<Video class="w-16 h-16 mx-auto mb-4 opacity-50" />
|
||||
<p class="text-lg opacity-75">{{ videoStatus }}</p>
|
||||
<p class="text-sm opacity-60 mt-2">
|
||||
点击"播放视频流"按钮开始查看实时视频
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 视频控制 -->
|
||||
<div class="flex justify-between items-center mt-4">
|
||||
<div class="text-sm text-base-content/70">
|
||||
流地址:
|
||||
<code class="bg-base-300 px-2 py-1 rounded">{{
|
||||
streamInfo.mjpegUrl
|
||||
}}</code>
|
||||
</div>
|
||||
<div class="space-x-2">
|
||||
<div class="dropdown dropdown-hover dropdown-top dropdown-end">
|
||||
<div
|
||||
tabindex="0"
|
||||
role="button"
|
||||
class="btn btn-sm btn-outline btn-accent"
|
||||
>
|
||||
<MoreHorizontal class="w-4 h-4 mr-1" />
|
||||
更多功能
|
||||
</div>
|
||||
<ul
|
||||
tabindex="0"
|
||||
class="dropdown-content z-[1] menu p-2 shadow bg-base-200 rounded-box w-52"
|
||||
>
|
||||
<li>
|
||||
<a @click="openInNewTab(streamInfo.htmlUrl)">
|
||||
<ExternalLink class="w-4 h-4" />
|
||||
在新标签打开视频页面
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a @click="takeSnapshot">
|
||||
<Camera class="w-4 h-4" />
|
||||
获取并下载快照
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a @click="copyToClipboard(streamInfo.mjpegUrl)">
|
||||
<Copy class="w-4 h-4" />
|
||||
复制MJPEG地址
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<button
|
||||
class="btn btn-success btn-sm"
|
||||
@click="startStream"
|
||||
:disabled="isPlaying"
|
||||
>
|
||||
<Play class="w-4 h-4 mr-1" />
|
||||
播放视频流
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-error btn-sm"
|
||||
@click="stopStream"
|
||||
:disabled="!isPlaying"
|
||||
>
|
||||
<Square class="w-4 h-4 mr-1" />
|
||||
停止视频流
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 日志区域 -->
|
||||
<div class="card bg-base-200 shadow-xl">
|
||||
<div class="card-body">
|
||||
<h2 class="card-title text-primary">
|
||||
<FileText class="w-6 h-6" />
|
||||
操作日志
|
||||
</h2>
|
||||
|
||||
<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"
|
||||
>
|
||||
<span class="text-base-content/50"
|
||||
>[{{ formatTime(log.time) }}]</span
|
||||
>
|
||||
<span :class="getLogClass(log.level)">{{ log.message }}</span>
|
||||
</div>
|
||||
<div
|
||||
v-if="logs.length === 0"
|
||||
class="text-base-content/50 text-center py-8"
|
||||
>
|
||||
暂无日志记录
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card-actions justify-end mt-2">
|
||||
<button class="btn btn-outline btn-sm" @click="clearLogs">
|
||||
清空日志
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, onUnmounted } from "vue";
|
||||
import {
|
||||
CogIcon,
|
||||
Settings,
|
||||
Video,
|
||||
Users,
|
||||
RefreshCw,
|
||||
TestTube,
|
||||
Play,
|
||||
Square,
|
||||
ExternalLink,
|
||||
Camera,
|
||||
Copy,
|
||||
FileText,
|
||||
AlertTriangle,
|
||||
MoreHorizontal,
|
||||
} from "lucide-vue-next";
|
||||
import { VideoStreamClient, CameraConfigRequest } from "@/APIClient";
|
||||
import { useEquipments } from "@/stores/equipments";
|
||||
|
||||
const eqps = useEquipments();
|
||||
|
||||
// 状态管理
|
||||
const loading = ref(false);
|
||||
const configing = ref(false);
|
||||
const testing = ref(false);
|
||||
const isPlaying = ref(false);
|
||||
const hasVideoError = ref(false);
|
||||
const videoStatus = ref('点击"播放视频流"按钮开始查看实时视频');
|
||||
|
||||
// 数据
|
||||
const statusInfo = ref({
|
||||
isRunning: false,
|
||||
serverPort: 8080,
|
||||
streamUrl: "",
|
||||
mjpegUrl: "",
|
||||
snapshotUrl: "",
|
||||
connectedClients: 0,
|
||||
clientEndpoints: [] as string[],
|
||||
});
|
||||
|
||||
const streamInfo = ref({
|
||||
frameRate: 30,
|
||||
frameWidth: 640,
|
||||
frameHeight: 480,
|
||||
format: "MJPEG",
|
||||
htmlUrl: "",
|
||||
mjpegUrl: "",
|
||||
snapshotUrl: "",
|
||||
});
|
||||
|
||||
const currentVideoSource = ref("");
|
||||
const logs = ref<Array<{ time: Date; level: string; message: string }>>([]);
|
||||
|
||||
// API 客户端
|
||||
const videoClient = new VideoStreamClient();
|
||||
|
||||
// 添加日志
|
||||
const addLog = (level: string, message: string) => {
|
||||
logs.value.push({
|
||||
time: new Date(),
|
||||
level,
|
||||
message,
|
||||
});
|
||||
// 限制日志数量
|
||||
if (logs.value.length > 100) {
|
||||
logs.value.shift();
|
||||
}
|
||||
};
|
||||
|
||||
// 格式化时间
|
||||
const formatTime = (time: Date) => {
|
||||
return time.toLocaleTimeString();
|
||||
};
|
||||
|
||||
// 获取日志样式
|
||||
const getLogClass = (level: string) => {
|
||||
switch (level) {
|
||||
case "error":
|
||||
return "text-error";
|
||||
case "warning":
|
||||
return "text-warning";
|
||||
case "success":
|
||||
return "text-success";
|
||||
default:
|
||||
return "text-base-content";
|
||||
}
|
||||
};
|
||||
|
||||
// 清空日志
|
||||
const clearLogs = () => {
|
||||
logs.value = [];
|
||||
addLog("info", "日志已清空");
|
||||
};
|
||||
|
||||
// 复制到剪贴板
|
||||
const copyToClipboard = (text: string) => {
|
||||
navigator.clipboard
|
||||
.writeText(text)
|
||||
.then(() => {
|
||||
addLog("success", "已复制到剪贴板");
|
||||
})
|
||||
.catch((err) => {
|
||||
addLog("error", `复制失败: ${err}`);
|
||||
});
|
||||
};
|
||||
|
||||
// 在新标签中打开视频页面
|
||||
const openInNewTab = (url: string) => {
|
||||
window.open(url, "_blank");
|
||||
addLog("info", `已在新标签打开视频页面: ${url}`);
|
||||
};
|
||||
|
||||
// 获取并下载快照
|
||||
const takeSnapshot = async () => {
|
||||
try {
|
||||
addLog("info", "正在获取快照...");
|
||||
|
||||
// 使用当前的快照URL
|
||||
const snapshotUrl = streamInfo.value.snapshotUrl;
|
||||
if (!snapshotUrl) {
|
||||
addLog("error", "快照URL不可用");
|
||||
return;
|
||||
}
|
||||
|
||||
// 添加时间戳防止缓存
|
||||
const urlWithTimestamp = `${snapshotUrl}?t=${new Date().getTime()}`;
|
||||
|
||||
// 创建一个临时链接下载图片
|
||||
const a = document.createElement("a");
|
||||
a.href = urlWithTimestamp;
|
||||
a.download = `fpga-snapshot-${new Date().toISOString().replace(/:/g, "-")}.jpg`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
|
||||
addLog("success", "快照已下载");
|
||||
} catch (error) {
|
||||
addLog("error", `获取快照失败: ${error}`);
|
||||
console.error("获取快照失败:", error);
|
||||
}
|
||||
};
|
||||
|
||||
async function configCamera() {
|
||||
configing.value = true;
|
||||
try {
|
||||
addLog("info", "正在配置并初始化摄像头...");
|
||||
const boardconfig = new CameraConfigRequest({
|
||||
address: eqps.boardAddr,
|
||||
port: eqps.boardPort,
|
||||
});
|
||||
await videoClient.configureCamera(boardconfig);
|
||||
|
||||
const status = await videoClient.getCameraConfig();
|
||||
if (status.isConfigured) {
|
||||
addLog("success", "摄像头已配置并初始化");
|
||||
} else {
|
||||
addLog("error", "摄像头配置失败,请检查地址和端口");
|
||||
}
|
||||
} catch (error) {
|
||||
addLog("error", `摄像头配置失败: ${error}`);
|
||||
console.error("摄像头配置失败:", error);
|
||||
} finally {
|
||||
configing.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新状态
|
||||
const refreshStatus = async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
addLog("info", "正在获取服务状态...");
|
||||
|
||||
// 使用新的API方法名称
|
||||
const status = await videoClient.getStatus();
|
||||
statusInfo.value = status;
|
||||
|
||||
const info = await videoClient.getStreamInfo();
|
||||
streamInfo.value = info;
|
||||
|
||||
addLog("success", "服务状态获取成功");
|
||||
} catch (error) {
|
||||
addLog("error", `获取状态失败: ${error}`);
|
||||
console.error("获取状态失败:", error);
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 测试连接
|
||||
const testConnection = async () => {
|
||||
testing.value = true;
|
||||
try {
|
||||
addLog("info", "正在测试视频流连接...");
|
||||
|
||||
const result = await videoClient.testConnection();
|
||||
|
||||
if (result) {
|
||||
addLog("success", "视频流连接测试成功");
|
||||
} else {
|
||||
addLog("error", "视频流连接测试失败");
|
||||
}
|
||||
} catch (error) {
|
||||
addLog("error", `连接测试失败: ${error}`);
|
||||
console.error("连接测试失败:", error);
|
||||
} finally {
|
||||
testing.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 视频错误处理
|
||||
const handleVideoError = () => {
|
||||
if (isPlaying.value) {
|
||||
hasVideoError.value = true;
|
||||
addLog("error", "视频流加载失败");
|
||||
}
|
||||
};
|
||||
|
||||
// 视频加载成功处理
|
||||
const handleVideoLoad = () => {
|
||||
hasVideoError.value = false;
|
||||
addLog("success", "视频流加载成功");
|
||||
};
|
||||
|
||||
// 尝试重新连接
|
||||
const tryReconnect = () => {
|
||||
addLog("info", "尝试重新连接视频流...");
|
||||
hasVideoError.value = false;
|
||||
|
||||
// 重新设置视频源,添加时间戳避免缓存问题
|
||||
currentVideoSource.value = `${streamInfo.value.mjpegUrl}?t=${new Date().getTime()}`;
|
||||
};
|
||||
|
||||
// 启动视频流
|
||||
const startStream = async () => {
|
||||
try {
|
||||
addLog("info", "正在启动视频流...");
|
||||
videoStatus.value = "正在连接视频流...";
|
||||
videoClient.setEnabled(true);
|
||||
|
||||
// 刷新状态
|
||||
await refreshStatus();
|
||||
|
||||
// 设置视频源
|
||||
currentVideoSource.value = streamInfo.value.mjpegUrl;
|
||||
|
||||
// 设置播放状态
|
||||
isPlaying.value = true;
|
||||
hasVideoError.value = false;
|
||||
|
||||
addLog("success", "视频流已启动");
|
||||
} catch (error) {
|
||||
addLog("error", `启动视频流失败: ${error}`);
|
||||
videoStatus.value = "启动视频流失败";
|
||||
console.error("启动视频流失败:", error);
|
||||
}
|
||||
};
|
||||
|
||||
// 停止视频流
|
||||
const stopStream = () => {
|
||||
try {
|
||||
addLog("info", "正在停止视频流...");
|
||||
videoClient.setEnabled(false);
|
||||
|
||||
// 清除视频源
|
||||
currentVideoSource.value = "";
|
||||
|
||||
// 更新状态
|
||||
isPlaying.value = false;
|
||||
hasVideoError.value = false;
|
||||
videoStatus.value = '点击"播放视频流"按钮开始查看实时视频';
|
||||
|
||||
addLog("success", "视频流已停止");
|
||||
} catch (error) {
|
||||
addLog("error", `停止视频流失败: ${error}`);
|
||||
console.error("停止视频流失败:", error);
|
||||
}
|
||||
};
|
||||
|
||||
// 生命周期
|
||||
onMounted(async () => {
|
||||
addLog("info", "HTTP 视频流页面已加载");
|
||||
await refreshStatus();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
stopStream();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
/* 自定义样式 */
|
||||
.stats {
|
||||
background-color: var(--b1);
|
||||
color: var(--bc);
|
||||
/* 添加适配文本颜色 */
|
||||
}
|
||||
|
||||
code {
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
img {
|
||||
/* 确保视频流居中显示 */
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
* {
|
||||
transition: all 500ms ease-in-out;
|
||||
}
|
||||
</style>
|
||||
@@ -1,254 +0,0 @@
|
||||
<template>
|
||||
<div class="h-screen flex flex-col overflow-hidden">
|
||||
<div class="flex flex-1 overflow-hidden relative">
|
||||
<SplitterGroup id="splitter-group" direction="horizontal" class="w-full h-full">
|
||||
<!-- 左侧图形化区域 -->
|
||||
<SplitterPanel
|
||||
id="splitter-group-panel-canvas"
|
||||
:default-size="60"
|
||||
:min-size="30"
|
||||
class="relative bg-base-200 overflow-hidden h-full"
|
||||
>
|
||||
<DiagramCanvas ref="diagramCanvas" :showDocPanel="showDocPanel"
|
||||
@diagram-updated="handleDiagramUpdated" @open-components="openComponentsMenu"
|
||||
@toggle-doc-panel="toggleDocPanel" />
|
||||
</SplitterPanel>
|
||||
<!-- 拖拽分割线 -->
|
||||
<SplitterResizeHandle id="splitter-group-resize-handle" class="w-2 bg-base-100 hover:bg-primary hover:opacity-70 transition-colors" />
|
||||
<!-- 右侧编辑区域 -->
|
||||
<SplitterPanel
|
||||
id="splitter-group-panel-properties"
|
||||
:min-size="20"
|
||||
class="bg-base-200 h-full overflow-hidden flex flex-col"
|
||||
>
|
||||
<div class="overflow-y-auto flex-1">
|
||||
<!-- 使用条件渲染显示不同的面板 -->
|
||||
<PropertyPanel v-show="!showDocPanel" :componentData="componentManager.selectedComponentData.value"
|
||||
:componentConfig="componentManager.selectedComponentConfig.value" @updateProp="updateComponentProp"
|
||||
@updateDirectProp="updateComponentDirectProp" />
|
||||
<div v-show="showDocPanel" class="doc-panel overflow-y-auto h-full">
|
||||
<MarkdownRenderer :content="documentContent" />
|
||||
</div>
|
||||
</div>
|
||||
</SplitterPanel>
|
||||
</SplitterGroup>
|
||||
</div>
|
||||
<!-- 元器件选择组件 -->
|
||||
<ComponentSelector :open="showComponentsMenu" @update:open="showComponentsMenu = $event"
|
||||
@add-component="handleAddComponent" @add-template="handleAddTemplate" @close="showComponentsMenu = false" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, watch } from "vue";
|
||||
import { SplitterGroup, SplitterPanel, SplitterResizeHandle } from "reka-ui";
|
||||
import DiagramCanvas from "@/components/LabCanvas/DiagramCanvas.vue";
|
||||
import ComponentSelector from "@/components/LabCanvas/ComponentSelector.vue";
|
||||
import PropertyPanel from "@/components/PropertyPanel.vue";
|
||||
import MarkdownRenderer from "@/components/MarkdownRenderer.vue";
|
||||
import { useProvideComponentManager } from "@/components/LabCanvas";
|
||||
import type { DiagramData, DiagramPart } from "@/components/LabCanvas";
|
||||
|
||||
// 获取路由参数
|
||||
import { useRoute } from "vue-router";
|
||||
const route = useRoute();
|
||||
|
||||
// 提供组件管理服务
|
||||
const componentManager = useProvideComponentManager();
|
||||
|
||||
// --- 文档面板控制 ---
|
||||
const showDocPanel = ref(false);
|
||||
const documentContent = ref("");
|
||||
|
||||
// 切换文档面板和属性面板
|
||||
async function toggleDocPanel() {
|
||||
showDocPanel.value = !showDocPanel.value;
|
||||
|
||||
// 如果切换到文档面板,则获取文档内容
|
||||
if (showDocPanel.value) {
|
||||
await loadDocumentContent();
|
||||
}
|
||||
}
|
||||
|
||||
// 加载文档内容
|
||||
async function loadDocumentContent() {
|
||||
try {
|
||||
// 从路由参数中获取教程ID
|
||||
const tutorialId = (route.query.tutorial as string) || "02"; // 默认加载02例程
|
||||
|
||||
// 构建文档路径
|
||||
let docPath = `/doc/${tutorialId}/doc.md`;
|
||||
|
||||
// 检查当前路径是否包含下划线(例如 02_key 格式)
|
||||
// 如果不包含,那么使用更新的命名格式
|
||||
if (!tutorialId.includes("_")) {
|
||||
docPath = `/doc/${tutorialId}/doc.md`;
|
||||
}
|
||||
|
||||
// 获取文档内容
|
||||
const response = await fetch(docPath);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load document: ${response.status}`);
|
||||
}
|
||||
|
||||
// 更新文档内容,并替换图片路径
|
||||
documentContent.value = (await response.text()).replace(
|
||||
/.\/images/gi,
|
||||
`/doc/${tutorialId}/images`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("加载文档失败:", error);
|
||||
documentContent.value = "# 文档加载失败\n\n无法加载请求的文档。";
|
||||
}
|
||||
}
|
||||
|
||||
// --- UI 状态管理 ---
|
||||
const showComponentsMenu = ref(false);
|
||||
const diagramCanvas = ref(null);
|
||||
|
||||
// --- 页面动画和通知 ---
|
||||
const showNotification = ref(false);
|
||||
const notificationMessage = ref("");
|
||||
const notificationType = ref<"success" | "error" | "info">("info");
|
||||
|
||||
function showToast(
|
||||
message: string,
|
||||
type: "success" | "error" | "info" = "info",
|
||||
duration = 3000,
|
||||
) {
|
||||
const canvasInstance = diagramCanvas.value as any;
|
||||
if (canvasInstance && canvasInstance.showToast) {
|
||||
canvasInstance.showToast(message, type, duration);
|
||||
} else {
|
||||
// 后备方案:使用原来的通知系统
|
||||
notificationMessage.value = message;
|
||||
notificationType.value = type;
|
||||
showNotification.value = true;
|
||||
|
||||
// 设置自动消失
|
||||
setTimeout(() => {
|
||||
showNotification.value = false;
|
||||
}, duration);
|
||||
}
|
||||
}
|
||||
|
||||
// --- 事件处理器(委托给组件管理器) ---
|
||||
|
||||
function openComponentsMenu() {
|
||||
showComponentsMenu.value = true;
|
||||
}
|
||||
|
||||
// 处理 ComponentSelector 组件添加元器件事件
|
||||
async function handleAddComponent(componentData: {
|
||||
type: string;
|
||||
name: string;
|
||||
props: Record<string, any>;
|
||||
}) {
|
||||
await componentManager.addComponent(componentData);
|
||||
}
|
||||
|
||||
// 处理模板添加事件
|
||||
async function handleAddTemplate(templateData: {
|
||||
id: string;
|
||||
name: string;
|
||||
template: any;
|
||||
}) {
|
||||
const result = await componentManager.addTemplate(templateData);
|
||||
if (result) {
|
||||
showToast(result.message, result.success ? "success" : "error");
|
||||
}
|
||||
}
|
||||
|
||||
// 处理图表数据更新事件
|
||||
function handleDiagramUpdated(data: DiagramData) {
|
||||
console.log("Diagram data updated:", data);
|
||||
}
|
||||
|
||||
// 更新组件属性的方法 - 委托给componentManager
|
||||
function updateComponentProp(
|
||||
componentId: string,
|
||||
propName: string,
|
||||
value: any,
|
||||
) {
|
||||
componentManager.updateComponentProp(componentId, propName, value);
|
||||
}
|
||||
|
||||
// 更新组件的直接属性 - 委托给componentManager
|
||||
function updateComponentDirectProp(
|
||||
componentId: string,
|
||||
propName: string,
|
||||
value: any,
|
||||
) {
|
||||
componentManager.updateComponentDirectProp(componentId, propName, value);
|
||||
}
|
||||
|
||||
// --- 生命周期钩子 ---
|
||||
onMounted(async () => {
|
||||
// 检查是否有例程参数,如果有则自动打开文档面板
|
||||
if (route.query.tutorial) {
|
||||
showDocPanel.value = true;
|
||||
await loadDocumentContent();
|
||||
}
|
||||
|
||||
// 设置画布引用并初始化组件管理器
|
||||
componentManager.setCanvasRef(diagramCanvas.value);
|
||||
await componentManager.initialize();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="postcss">
|
||||
/* 样式保持不变 */
|
||||
@import "../assets/main.css";
|
||||
|
||||
.animate-slideRight {
|
||||
animation: slideRight 0.3s ease-out forwards;
|
||||
}
|
||||
|
||||
@keyframes slideRight {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateX(30px);
|
||||
}
|
||||
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateX(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* 确保滚动行为仅在需要时出现 */
|
||||
html,
|
||||
body {
|
||||
overflow: hidden;
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
/* 文档面板样式 */
|
||||
.doc-panel {
|
||||
padding: 1.5rem;
|
||||
max-width: 100%;
|
||||
margin: 0;
|
||||
background-color: transparent;
|
||||
/* 使用透明背景 */
|
||||
border: none;
|
||||
/* 确保没有边框 */
|
||||
}
|
||||
|
||||
/* 文档切换按钮样式 */
|
||||
.doc-toggle-btn {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
right: 10px;
|
||||
z-index: 50;
|
||||
}
|
||||
|
||||
/* Markdown渲染样式调整 */
|
||||
:deep(.markdown-content) {
|
||||
padding: 1rem;
|
||||
background-color: hsl(var(--b1));
|
||||
border-radius: 0.5rem;
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
</style>
|
||||
251
src/views/User/AddBoardDialog.vue
Normal file
251
src/views/User/AddBoardDialog.vue
Normal file
@@ -0,0 +1,251 @@
|
||||
<template>
|
||||
<dialog class="modal" :class="{ 'modal-open': visible }">
|
||||
<div class="modal-box w-96 max-w-md">
|
||||
<h3 class="text-lg font-bold mb-4">新增实验板</h3>
|
||||
|
||||
<form @submit.prevent="handleSubmit" class="space-y-4">
|
||||
<!-- 实验板名称 -->
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="label-text">实验板名称 <span class="text-error">*</span></span>
|
||||
</label>
|
||||
<input
|
||||
v-model="form.name"
|
||||
type="text"
|
||||
placeholder="请输入实验板名称"
|
||||
class="input input-bordered"
|
||||
:class="{ 'input-error': errors.name }"
|
||||
required
|
||||
/>
|
||||
<label v-if="errors.name" class="label">
|
||||
<span class="label-text-alt text-error">{{ errors.name }}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<!-- IP 地址 -->
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="label-text">IP 地址 <span class="text-error">*</span></span>
|
||||
</label>
|
||||
<input
|
||||
v-model="form.ipAddr"
|
||||
type="text"
|
||||
placeholder="例如:192.168.1.100"
|
||||
class="input input-bordered"
|
||||
:class="{ 'input-error': errors.ipAddr }"
|
||||
required
|
||||
/>
|
||||
<label v-if="errors.ipAddr" class="label">
|
||||
<span class="label-text-alt text-error">{{ errors.ipAddr }}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<!-- 端口号 -->
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="label-text">端口号 <span class="text-error">*</span></span>
|
||||
</label>
|
||||
<input
|
||||
v-model.number="form.port"
|
||||
type="number"
|
||||
placeholder="例如:1234"
|
||||
min="1"
|
||||
max="65535"
|
||||
class="input input-bordered"
|
||||
:class="{ 'input-error': errors.port }"
|
||||
required
|
||||
/>
|
||||
<label v-if="errors.port" class="label">
|
||||
<span class="label-text-alt text-error">{{ errors.port }}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<div class="modal-action">
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-ghost"
|
||||
@click="handleCancel"
|
||||
:disabled="isSubmitting"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
class="btn btn-primary"
|
||||
:class="{ 'loading': isSubmitting }"
|
||||
:disabled="isSubmitting"
|
||||
>
|
||||
{{ isSubmitting ? '添加中...' : '确认添加' }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- 点击背景关闭 -->
|
||||
<form method="dialog" class="modal-backdrop">
|
||||
<button type="button" @click="handleCancel">close</button>
|
||||
</form>
|
||||
</dialog>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref, reactive, watch } from 'vue';
|
||||
import { useBoardManager } from '../../utils/BoardManager';
|
||||
|
||||
// Props 和 Emits
|
||||
interface Props {
|
||||
visible: boolean;
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'update:visible', value: boolean): void;
|
||||
(e: 'success'): void;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 使用 BoardManager
|
||||
const boardManager = useBoardManager()!;
|
||||
|
||||
// 表单数据
|
||||
const form = reactive({
|
||||
name: '',
|
||||
ipAddr: '',
|
||||
port: 1234
|
||||
});
|
||||
|
||||
// 表单错误
|
||||
const errors = reactive({
|
||||
name: '',
|
||||
ipAddr: '',
|
||||
port: ''
|
||||
});
|
||||
|
||||
// 提交状态
|
||||
const isSubmitting = ref(false);
|
||||
|
||||
// IP地址验证正则
|
||||
const IP_REGEX = /^((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]?)$/;
|
||||
|
||||
// 验证表单
|
||||
function validateForm(): boolean {
|
||||
// 清空之前的错误
|
||||
errors.name = '';
|
||||
errors.ipAddr = '';
|
||||
errors.port = '';
|
||||
|
||||
let isValid = true;
|
||||
|
||||
// 验证名称
|
||||
if (!form.name.trim()) {
|
||||
errors.name = '请输入实验板名称';
|
||||
isValid = false;
|
||||
} else if (form.name.trim().length < 2) {
|
||||
errors.name = '实验板名称至少需要2个字符';
|
||||
isValid = false;
|
||||
} else if (form.name.trim().length > 50) {
|
||||
errors.name = '实验板名称不能超过50个字符';
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
// 验证IP地址
|
||||
if (!form.ipAddr.trim()) {
|
||||
errors.ipAddr = '请输入IP地址';
|
||||
isValid = false;
|
||||
} else if (!IP_REGEX.test(form.ipAddr.trim())) {
|
||||
errors.ipAddr = '请输入有效的IP地址格式';
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
// 验证端口号
|
||||
if (!form.port) {
|
||||
errors.port = '请输入端口号';
|
||||
isValid = false;
|
||||
} else if (form.port < 1 || form.port > 65535) {
|
||||
errors.port = '端口号必须在1-65535之间';
|
||||
isValid = false;
|
||||
} else if (!Number.isInteger(form.port)) {
|
||||
errors.port = '端口号必须是整数';
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
return isValid;
|
||||
}
|
||||
|
||||
// 重置表单
|
||||
function resetForm() {
|
||||
form.name = '';
|
||||
form.ipAddr = '';
|
||||
form.port = 1234;
|
||||
errors.name = '';
|
||||
errors.ipAddr = '';
|
||||
errors.port = '';
|
||||
}
|
||||
|
||||
// 处理取消
|
||||
function handleCancel() {
|
||||
if (!isSubmitting.value) {
|
||||
emit('update:visible', false);
|
||||
resetForm();
|
||||
}
|
||||
}
|
||||
|
||||
// 处理提交
|
||||
async function handleSubmit() {
|
||||
if (!validateForm()) {
|
||||
return;
|
||||
}
|
||||
|
||||
isSubmitting.value = true;
|
||||
|
||||
try {
|
||||
const success = await boardManager.addBoard(
|
||||
form.name.trim(),
|
||||
form.ipAddr.trim(),
|
||||
form.port
|
||||
);
|
||||
|
||||
if (success) {
|
||||
emit('success');
|
||||
resetForm();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('添加实验板失败:', error);
|
||||
} finally {
|
||||
isSubmitting.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 监听对话框显示状态,重置表单
|
||||
watch(() => props.visible, (newVisible) => {
|
||||
if (newVisible) {
|
||||
resetForm();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="postcss">
|
||||
@import "@/assets/main.css";
|
||||
|
||||
.form-control {
|
||||
@apply w-full;
|
||||
}
|
||||
|
||||
.label-text {
|
||||
@apply font-medium;
|
||||
}
|
||||
|
||||
.input-error {
|
||||
@apply border-error;
|
||||
}
|
||||
|
||||
.text-error {
|
||||
@apply text-red-500;
|
||||
}
|
||||
|
||||
.loading {
|
||||
@apply opacity-50 cursor-not-allowed;
|
||||
}
|
||||
</style>
|
||||
232
src/views/User/BoardTable.vue
Normal file
232
src/views/User/BoardTable.vue
Normal file
@@ -0,0 +1,232 @@
|
||||
<template>
|
||||
<div class="flex flex-row justify-between items-center">
|
||||
<h1 class="text-3xl font-bold mb-6">FPGA 设备管理</h1>
|
||||
<div>
|
||||
<button
|
||||
class="btn btn-ghost group"
|
||||
@click="tableManager.getAllBoards"
|
||||
>
|
||||
<RefreshCw class="w-4 h-4 mr-2 transition-transform duration-300 group-hover:rotate-180" />
|
||||
刷新
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-ghost text-error hover:underline group"
|
||||
@click="tableManager.toggleEditMode"
|
||||
>
|
||||
<Edit class="w-4 h-4 mr-2 transition-transform duration-200 group-hover:scale-110" />
|
||||
编辑
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card bg-base-100 shadow-xl">
|
||||
<div class="card-body">
|
||||
<!-- 搜索和列控制 -->
|
||||
<div class="flex items-center my-2 gap-4">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="筛选 IP 地址..."
|
||||
class="input input-bordered max-w-sm"
|
||||
:value="
|
||||
tableManager.getColumnByKey('devAddr')?.getFilterValue() as string
|
||||
"
|
||||
@input="
|
||||
tableManager
|
||||
.getColumnByKey('devAddr')
|
||||
?.setFilterValue(($event.target as HTMLInputElement).value)
|
||||
"
|
||||
/>
|
||||
|
||||
<div class="dropdown dropdown-end">
|
||||
<div tabindex="0" role="button" class="btn btn-outline">
|
||||
列显示
|
||||
<svg
|
||||
class="w-4 h-4 ml-2"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="m19 9-7 7-7-7"
|
||||
></path>
|
||||
</svg>
|
||||
</div>
|
||||
<ul
|
||||
tabindex="0"
|
||||
class="dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-52"
|
||||
>
|
||||
<li
|
||||
v-for="column in tableManager.getAllHideableColumns()"
|
||||
:key="column.id"
|
||||
>
|
||||
<label class="label cursor-pointer">
|
||||
<span class="label-text capitalize">{{ column.id }}</span>
|
||||
<input
|
||||
type="checkbox"
|
||||
class="checkbox checkbox-sm"
|
||||
:checked="column.getIsVisible()"
|
||||
@change="
|
||||
column.toggleVisibility(
|
||||
!!($event.target as HTMLInputElement).checked,
|
||||
)
|
||||
"
|
||||
/>
|
||||
</label>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="flex gap-2 ml-auto">
|
||||
<button
|
||||
class="btn btn-primary group"
|
||||
:disabled="!tableManager.isEditMode.value"
|
||||
@click="showAddBoardDialog = true"
|
||||
>
|
||||
<Plus class="w-4 h-4 mr-2 transition-transform duration-200 group-hover:scale-110" />
|
||||
新增实验板
|
||||
</button>
|
||||
|
||||
<button
|
||||
class="btn btn-error group"
|
||||
:disabled="!tableManager.isEditMode.value"
|
||||
@click="tableManager.deleteSelectedBoards"
|
||||
>
|
||||
<Trash2 class="w-4 h-4 mr-2 transition-transform duration-200 group-hover:scale-110 group-hover:animate-pulse" />
|
||||
删除选中
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 表格 -->
|
||||
<div class="overflow-x-auto border border-base-300 rounded-lg">
|
||||
<table class="table w-full">
|
||||
<thead>
|
||||
<tr
|
||||
v-for="headerGroup in tableManager.getHeaderGroups()"
|
||||
:key="headerGroup.id"
|
||||
class="bg-base-300"
|
||||
>
|
||||
<th v-for="header in headerGroup.headers" :key="header.id">
|
||||
<FlexRender
|
||||
v-if="!header.isPlaceholder"
|
||||
:render="header.column.columnDef.header"
|
||||
:props="header.getContext()"
|
||||
/>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<template v-if="tableManager.getRowModel().rows?.length">
|
||||
<template
|
||||
v-for="row in tableManager.getRowModel().rows"
|
||||
:key="row.id"
|
||||
>
|
||||
<tr
|
||||
class="hover"
|
||||
:class="{ 'bg-primary/10': row.getIsSelected() }"
|
||||
>
|
||||
<td v-for="cell in row.getVisibleCells()" :key="cell.id">
|
||||
<FlexRender
|
||||
:render="cell.column.columnDef.cell"
|
||||
:props="cell.getContext()"
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
<tr v-if="row.getIsExpanded()">
|
||||
<td :colspan="row.getAllCells().length" class="bg-base-200">
|
||||
<div class="p-4">
|
||||
<pre class="text-sm">{{
|
||||
JSON.stringify(row.original, null, 2)
|
||||
}}</pre>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</template>
|
||||
</template>
|
||||
<tr v-else>
|
||||
<td
|
||||
:colspan="tableManager.columns.length"
|
||||
class="h-24 text-center text-base-content/60"
|
||||
>
|
||||
暂无数据
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- 分页控制 -->
|
||||
<div class="flex items-center justify-between py-4">
|
||||
<div class="text-sm text-base-content/60">
|
||||
已选择 {{ tableManager.getSelectedRows().length }} /
|
||||
{{ tableManager.getAllRows().length }} 行
|
||||
</div>
|
||||
<div class="flex gap-2">
|
||||
<button
|
||||
class="btn btn-outline btn-sm"
|
||||
:disabled="!tableManager.canPreviousPage()"
|
||||
@click="tableManager.previousPage()"
|
||||
>
|
||||
上一页
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-outline btn-sm"
|
||||
:disabled="!tableManager.canNextPage()"
|
||||
@click="tableManager.nextPage()"
|
||||
>
|
||||
下一页
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-6 bg-base-300 p-4 rounded-lg">
|
||||
<p class="text-sm opacity-80">
|
||||
<span class="font-semibold text-error">提示:</span>
|
||||
请谨慎操作FPGA固化和热启动功能,确保上传的位流文件无误,以避免设备损坏。
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 新增实验板对话框 -->
|
||||
<AddBoardDialog
|
||||
v-model:visible="showAddBoardDialog"
|
||||
@success="handleAddBoardSuccess"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { FlexRender } from "@tanstack/vue-table";
|
||||
import { onMounted, ref } from "vue";
|
||||
import { RefreshCw, Edit, Plus, Trash2 } from "lucide-vue-next";
|
||||
import { useProvideBoardManager } from "../../utils/BoardManager";
|
||||
import { useProvideBoardTableManager } from "./BoardTableManager";
|
||||
import AddBoardDialog from "./AddBoardDialog.vue";
|
||||
|
||||
// 使用 BoardManager
|
||||
const boardManager = useProvideBoardManager()!;
|
||||
|
||||
// 使用表格管理器(不再需要参数)
|
||||
const tableManager = useProvideBoardTableManager()!;
|
||||
|
||||
// 新增实验板对话框显示状态
|
||||
const showAddBoardDialog = ref(false);
|
||||
|
||||
// 处理新增实验板成功事件
|
||||
const handleAddBoardSuccess = () => {
|
||||
showAddBoardDialog.value = false;
|
||||
// 刷新数据在 BoardManager.addBoard 中已经处理
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
// 初始化数据
|
||||
boardManager.getAllBoards();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="postcss">
|
||||
@import "@/assets/main.css";
|
||||
</style>
|
||||
544
src/views/User/BoardTableManager.ts
Normal file
544
src/views/User/BoardTableManager.ts
Normal file
@@ -0,0 +1,544 @@
|
||||
import type {
|
||||
ColumnDef,
|
||||
ColumnFiltersState,
|
||||
ExpandedState,
|
||||
SortingState,
|
||||
VisibilityState,
|
||||
} from "@tanstack/vue-table";
|
||||
import {
|
||||
getCoreRowModel,
|
||||
getExpandedRowModel,
|
||||
getFilteredRowModel,
|
||||
getPaginationRowModel,
|
||||
getSortedRowModel,
|
||||
useVueTable,
|
||||
} from "@tanstack/vue-table";
|
||||
import { h, ref, computed, version } from "vue";
|
||||
import { createInjectionState } from "@vueuse/core";
|
||||
import type { BoardData } from "../../utils/BoardManager";
|
||||
import { useBoardManager } from "../../utils/BoardManager";
|
||||
import { useDialogStore } from "@/stores/dialog";
|
||||
|
||||
const [useProvideBoardTableManager, useBoardTableManager] =
|
||||
createInjectionState(() => {
|
||||
// 从BoardManager获取数据和方法
|
||||
const boardManager = useBoardManager()!;
|
||||
|
||||
const dialog = useDialogStore();
|
||||
|
||||
// 编辑状态
|
||||
const isEditMode = ref(false);
|
||||
|
||||
// 表格状态管理
|
||||
const sorting = ref<SortingState>([]);
|
||||
const columnFilters = ref<ColumnFiltersState>([]);
|
||||
const columnVisibility = ref<VisibilityState>({
|
||||
// 默认隐藏端口、ID、状态列和板卡名称列
|
||||
port: false,
|
||||
id: false,
|
||||
status: false,
|
||||
version: false,
|
||||
});
|
||||
const rowSelection = ref({});
|
||||
const expanded = ref<ExpandedState>({});
|
||||
|
||||
// 表格列定义
|
||||
const columns: ColumnDef<BoardData>[] = [
|
||||
{
|
||||
id: "select",
|
||||
header: ({ table }) =>
|
||||
h("input", {
|
||||
type: "checkbox",
|
||||
class: "checkbox",
|
||||
checked:
|
||||
table.getIsAllPageRowsSelected() ||
|
||||
(table.getIsSomePageRowsSelected() ? "indeterminate" : false),
|
||||
onChange: (event: Event) =>
|
||||
table.toggleAllPageRowsSelected(
|
||||
!!(event.target as HTMLInputElement).checked,
|
||||
),
|
||||
}),
|
||||
cell: ({ row }) =>
|
||||
h("input", {
|
||||
type: "checkbox",
|
||||
class: "checkbox",
|
||||
checked: row.getIsSelected(),
|
||||
onChange: (event: Event) =>
|
||||
row.toggleSelected(!!(event.target as HTMLInputElement).checked),
|
||||
}),
|
||||
enableSorting: false,
|
||||
enableHiding: false,
|
||||
},
|
||||
{
|
||||
accessorKey: "boardName",
|
||||
header: "板卡名称",
|
||||
cell: ({ row }) => {
|
||||
const device = row.original;
|
||||
return isEditMode.value
|
||||
? h("input", {
|
||||
type: "text",
|
||||
class: "input input-sm w-full",
|
||||
value: device.boardName,
|
||||
onInput: (e: Event) => {
|
||||
device.boardName = (e.target as HTMLInputElement).value;
|
||||
},
|
||||
})
|
||||
: h("span", { class: "font-medium" }, device.boardName);
|
||||
},
|
||||
enableHiding: true,
|
||||
},
|
||||
{
|
||||
accessorKey: "devAddr",
|
||||
header: "IP 地址",
|
||||
cell: ({ row }) => {
|
||||
const device = row.original;
|
||||
return isEditMode.value
|
||||
? h("input", {
|
||||
type: "text",
|
||||
class: "input input-sm w-full",
|
||||
value: device.ipAddr,
|
||||
onInput: (e: Event) => {
|
||||
device.ipAddr = (e.target as HTMLInputElement).value;
|
||||
},
|
||||
})
|
||||
: h("span", { class: "font-medium" }, device.ipAddr);
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "port",
|
||||
header: "端口",
|
||||
cell: ({ row }) => {
|
||||
const device = row.original;
|
||||
return isEditMode.value
|
||||
? h("input", {
|
||||
type: "number",
|
||||
class: "input input-sm w-full",
|
||||
value: device.port,
|
||||
onInput: (e: Event) => {
|
||||
device.port = parseInt((e.target as HTMLInputElement).value);
|
||||
},
|
||||
})
|
||||
: h("span", { class: "font-mono" }, device.port.toString());
|
||||
},
|
||||
enableHiding: true,
|
||||
},
|
||||
{
|
||||
accessorKey: "id",
|
||||
header: "设备ID",
|
||||
cell: ({ row }) =>
|
||||
h("span", { class: "font-mono text-xs" }, row.original.id),
|
||||
enableHiding: true,
|
||||
},
|
||||
{
|
||||
accessorKey: "status",
|
||||
header: "状态",
|
||||
cell: ({ row }) => {
|
||||
const device = row.original;
|
||||
const statusText = device.status === 0 ? "忙碌" : "可用";
|
||||
const statusClass =
|
||||
device.status === 0 ? "badge-warning" : "badge-success";
|
||||
return h(
|
||||
"span",
|
||||
{
|
||||
class: `badge ${statusClass} min-w-15`,
|
||||
},
|
||||
statusText,
|
||||
);
|
||||
},
|
||||
enableHiding: true,
|
||||
},
|
||||
{
|
||||
accessorKey: "version",
|
||||
header: "版本号",
|
||||
cell: ({ row }) =>
|
||||
h("span", { class: "font-mono" }, row.original.firmVersion),
|
||||
},
|
||||
{
|
||||
accessorKey: "defaultBitstream",
|
||||
header: "默认启动位流",
|
||||
cell: ({ row }) => {
|
||||
const device = row.original;
|
||||
return h(
|
||||
"select",
|
||||
{
|
||||
class: "select select-bordered select-sm w-full",
|
||||
value: device.defaultBitstream,
|
||||
onChange: (e: Event) => {
|
||||
device.defaultBitstream = (e.target as HTMLSelectElement).value;
|
||||
},
|
||||
},
|
||||
[
|
||||
h("option", { value: "黄金位流" }, "黄金位流"),
|
||||
h("option", { value: "应用位流1" }, "应用位流1"),
|
||||
h("option", { value: "应用位流2" }, "应用位流2"),
|
||||
h("option", { value: "应用位流3" }, "应用位流3"),
|
||||
],
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "goldBitstream",
|
||||
header: "黄金位流",
|
||||
cell: ({ row }) => {
|
||||
const device = row.original;
|
||||
return h("input", {
|
||||
type: "file",
|
||||
class: "file-input file-input-primary file-input-sm",
|
||||
onChange: (e: Event) =>
|
||||
boardManager.handleFileChange(e, device, "goldBitstreamFile"),
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "appBitstream1",
|
||||
header: "应用位流1",
|
||||
cell: ({ row }) => {
|
||||
const device = row.original;
|
||||
return h("input", {
|
||||
type: "file",
|
||||
class: "file-input file-input-secondary file-input-sm",
|
||||
onChange: (e: Event) =>
|
||||
boardManager.handleFileChange(e, device, "appBitstream1File"),
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "appBitstream2",
|
||||
header: "应用位流2",
|
||||
cell: ({ row }) => {
|
||||
const device = row.original;
|
||||
return h("input", {
|
||||
type: "file",
|
||||
class: "file-input file-input-accent file-input-sm",
|
||||
onChange: (e: Event) =>
|
||||
boardManager.handleFileChange(e, device, "appBitstream2File"),
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "appBitstream3",
|
||||
header: "应用位流3",
|
||||
cell: ({ row }) => {
|
||||
const device = row.original;
|
||||
return h("input", {
|
||||
type: "file",
|
||||
class: "file-input file-input-info file-input-sm",
|
||||
onChange: (e: Event) =>
|
||||
boardManager.handleFileChange(e, device, "appBitstream3File"),
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "actions",
|
||||
header: "操作",
|
||||
cell: ({ row }) => {
|
||||
const device = row.original;
|
||||
|
||||
// 根据编辑模式显示不同的按钮
|
||||
if (isEditMode.value) {
|
||||
return h(
|
||||
"div",
|
||||
{
|
||||
class: ["flex gap-2", { "min-w-30": !isEditMode.value }],
|
||||
},
|
||||
[
|
||||
h(
|
||||
"button",
|
||||
{
|
||||
class: "btn btn-error btn-sm",
|
||||
onClick: async () => {
|
||||
const confirmed = confirm(
|
||||
`确定要删除设备 ${device.ipAddr} 吗?`,
|
||||
);
|
||||
if (confirmed) {
|
||||
await deleteBoard(device.id);
|
||||
}
|
||||
},
|
||||
},
|
||||
"删除",
|
||||
),
|
||||
],
|
||||
);
|
||||
} else {
|
||||
return h("div", { class: "flex gap-2 min-w-30" }, [
|
||||
h(
|
||||
"button",
|
||||
{
|
||||
class: "btn btn-warning btn-sm",
|
||||
onClick: () =>
|
||||
uploadAndDownloadBitstreams(
|
||||
device,
|
||||
device.goldBitstreamFile,
|
||||
device.appBitstream1File,
|
||||
device.appBitstream2File,
|
||||
device.appBitstream3File,
|
||||
),
|
||||
},
|
||||
"固化",
|
||||
),
|
||||
h(
|
||||
"button",
|
||||
{
|
||||
class: "btn btn-success btn-sm",
|
||||
onClick: () =>
|
||||
hotresetBitstream(
|
||||
device,
|
||||
boardManager.getSelectedBitstreamNum(
|
||||
device.defaultBitstream,
|
||||
),
|
||||
),
|
||||
},
|
||||
"热启动",
|
||||
),
|
||||
]);
|
||||
}
|
||||
},
|
||||
enableHiding: false,
|
||||
},
|
||||
];
|
||||
|
||||
// 创建表格实例
|
||||
const table = useVueTable({
|
||||
get data() {
|
||||
return boardManager.boards.value;
|
||||
},
|
||||
get columns() {
|
||||
return columns;
|
||||
},
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
getPaginationRowModel: getPaginationRowModel(),
|
||||
getSortedRowModel: getSortedRowModel(),
|
||||
getFilteredRowModel: getFilteredRowModel(),
|
||||
getExpandedRowModel: getExpandedRowModel(),
|
||||
onSortingChange: (updaterOrValue) => {
|
||||
if (typeof updaterOrValue === "function") {
|
||||
sorting.value = updaterOrValue(sorting.value);
|
||||
} else {
|
||||
sorting.value = updaterOrValue;
|
||||
}
|
||||
},
|
||||
onColumnFiltersChange: (updaterOrValue) => {
|
||||
if (typeof updaterOrValue === "function") {
|
||||
columnFilters.value = updaterOrValue(columnFilters.value);
|
||||
} else {
|
||||
columnFilters.value = updaterOrValue;
|
||||
}
|
||||
},
|
||||
onColumnVisibilityChange: (updaterOrValue) => {
|
||||
if (typeof updaterOrValue === "function") {
|
||||
columnVisibility.value = updaterOrValue(columnVisibility.value);
|
||||
} else {
|
||||
columnVisibility.value = updaterOrValue;
|
||||
}
|
||||
},
|
||||
onRowSelectionChange: (updaterOrValue) => {
|
||||
if (typeof updaterOrValue === "function") {
|
||||
rowSelection.value = updaterOrValue(rowSelection.value);
|
||||
} else {
|
||||
rowSelection.value = updaterOrValue;
|
||||
}
|
||||
},
|
||||
onExpandedChange: (updaterOrValue) => {
|
||||
if (typeof updaterOrValue === "function") {
|
||||
expanded.value = updaterOrValue(expanded.value);
|
||||
} else {
|
||||
expanded.value = updaterOrValue;
|
||||
}
|
||||
},
|
||||
state: {
|
||||
get sorting() {
|
||||
return sorting.value;
|
||||
},
|
||||
get columnFilters() {
|
||||
return columnFilters.value;
|
||||
},
|
||||
get columnVisibility() {
|
||||
return columnVisibility.value;
|
||||
},
|
||||
get rowSelection() {
|
||||
return rowSelection.value;
|
||||
},
|
||||
get expanded() {
|
||||
return expanded.value;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// UI层的API封装方法 - 添加dialog提示
|
||||
|
||||
// 获取所有板卡信息
|
||||
async function getAllBoards(): Promise<boolean> {
|
||||
const result = await boardManager.getAllBoards();
|
||||
if (result.success) {
|
||||
dialog?.info("获取板卡信息成功");
|
||||
} else {
|
||||
dialog?.error(result.error || "获取板卡信息失败");
|
||||
}
|
||||
return result.success;
|
||||
}
|
||||
|
||||
// 新增板卡
|
||||
async function addBoard(name: string, ipAddr: string, port: number): Promise<boolean> {
|
||||
const result = await boardManager.addBoard(name, ipAddr, port);
|
||||
if (result.success) {
|
||||
dialog?.info("新增板卡成功");
|
||||
} else {
|
||||
dialog?.error(result.error || "新增板卡失败");
|
||||
}
|
||||
return result.success;
|
||||
}
|
||||
|
||||
// 删除板卡
|
||||
async function deleteBoard(boardId: string): Promise<boolean> {
|
||||
const result = await boardManager.deleteBoard(boardId);
|
||||
if (result.success) {
|
||||
dialog?.info("删除板卡成功");
|
||||
} else {
|
||||
dialog?.error(result.error || "删除板卡失败");
|
||||
}
|
||||
return result.success;
|
||||
}
|
||||
|
||||
// 上传并固化位流
|
||||
async function uploadAndDownloadBitstreams(
|
||||
board: BoardData,
|
||||
goldBitstream?: File,
|
||||
appBitstream1?: File,
|
||||
appBitstream2?: File,
|
||||
appBitstream3?: File,
|
||||
): Promise<boolean> {
|
||||
const result = await boardManager.uploadAndDownloadBitstreams(
|
||||
board,
|
||||
goldBitstream,
|
||||
appBitstream1,
|
||||
appBitstream2,
|
||||
appBitstream3,
|
||||
);
|
||||
if (result.success) {
|
||||
dialog?.info("固化比特流成功");
|
||||
} else {
|
||||
dialog?.error(result.error || "固化比特流失败");
|
||||
}
|
||||
return result.success;
|
||||
}
|
||||
|
||||
// 热启动位流
|
||||
async function hotresetBitstream(board: BoardData, bitstreamNum: number): Promise<boolean> {
|
||||
const result = await boardManager.hotresetBitstream(board, bitstreamNum);
|
||||
if (result.success) {
|
||||
dialog?.info("切换比特流成功");
|
||||
} else {
|
||||
dialog?.error(result.error || "切换比特流失败");
|
||||
}
|
||||
return result.success;
|
||||
}
|
||||
|
||||
// 表格操作方法
|
||||
const getSelectedRows = () => table.getFilteredSelectedRowModel().rows;
|
||||
const getAllRows = () => table.getFilteredRowModel().rows;
|
||||
const getColumnByKey = (key: string) => table.getColumn(key);
|
||||
const getAllHideableColumns = () =>
|
||||
table.getAllColumns().filter((column) => column.getCanHide());
|
||||
const getHeaderGroups = () => table.getHeaderGroups();
|
||||
const getRowModel = () => table.getRowModel();
|
||||
const canPreviousPage = () => table.getCanPreviousPage();
|
||||
const canNextPage = () => table.getCanNextPage();
|
||||
const previousPage = () => table.previousPage();
|
||||
const nextPage = () => table.nextPage();
|
||||
|
||||
// 编辑模式控制
|
||||
const toggleEditMode = () => {
|
||||
isEditMode.value = !isEditMode.value;
|
||||
};
|
||||
|
||||
// 删除选中的实验板
|
||||
const deleteSelectedBoards = async () => {
|
||||
const selectedRows = getSelectedRows();
|
||||
|
||||
if (selectedRows.length === 0) {
|
||||
dialog?.warn("请先选择要删除的实验板");
|
||||
return false;
|
||||
}
|
||||
|
||||
const boardNames = selectedRows
|
||||
.map((row) => row.original.boardName || row.original.ipAddr)
|
||||
.join("、");
|
||||
const confirmed = confirm(
|
||||
`确定要删除以下 ${selectedRows.length} 个实验板吗?\n${boardNames}`,
|
||||
);
|
||||
|
||||
if (!confirmed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let successCount = 0;
|
||||
let failCount = 0;
|
||||
|
||||
// 批量删除
|
||||
for (const row of selectedRows) {
|
||||
const board = row.original;
|
||||
const result = await boardManager.deleteBoard(board.id);
|
||||
if (result.success) {
|
||||
successCount++;
|
||||
} else {
|
||||
failCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 清空选择状态
|
||||
rowSelection.value = {};
|
||||
|
||||
// 显示结果提示
|
||||
if (failCount === 0) {
|
||||
dialog?.info(`成功删除 ${successCount} 个实验板`);
|
||||
} else if (successCount === 0) {
|
||||
dialog?.error(
|
||||
`删除失败,共 ${failCount} 个实验板删除失败`,
|
||||
);
|
||||
} else {
|
||||
dialog?.warn(
|
||||
`部分删除成功:成功 ${successCount} 个,失败 ${failCount} 个`,
|
||||
);
|
||||
}
|
||||
|
||||
return successCount > 0;
|
||||
};
|
||||
|
||||
return {
|
||||
// 表格实例
|
||||
table,
|
||||
// 列定义
|
||||
columns,
|
||||
// 表格操作方法
|
||||
getSelectedRows,
|
||||
getAllRows,
|
||||
getColumnByKey,
|
||||
getAllHideableColumns,
|
||||
getHeaderGroups,
|
||||
getRowModel,
|
||||
canPreviousPage,
|
||||
canNextPage,
|
||||
previousPage,
|
||||
nextPage,
|
||||
deleteSelectedBoards,
|
||||
// 状态
|
||||
sorting,
|
||||
columnFilters,
|
||||
columnVisibility,
|
||||
rowSelection,
|
||||
expanded,
|
||||
// 编辑模式
|
||||
isEditMode,
|
||||
toggleEditMode,
|
||||
// UI层封装的API方法
|
||||
getAllBoards,
|
||||
addBoard,
|
||||
deleteBoard,
|
||||
uploadAndDownloadBitstreams,
|
||||
hotresetBitstream,
|
||||
// BoardManager 的引用
|
||||
boardManager,
|
||||
};
|
||||
});
|
||||
|
||||
export { useProvideBoardTableManager, useBoardTableManager };
|
||||
94
src/views/User/Index.vue
Normal file
94
src/views/User/Index.vue
Normal file
@@ -0,0 +1,94 @@
|
||||
<template>
|
||||
<div
|
||||
class="min-h-screen bg-base-100 container mx-auto p-6 space-y-6 flex flex-row"
|
||||
>
|
||||
<ul class="menu bg-base-200 w-56 gap-2 rounded-2xl p-5">
|
||||
<li id="1" @click="setActivePage">
|
||||
<a :class="{ 'menu-active': activePage === 1 }">用户信息</a>
|
||||
</li>
|
||||
<li id="2" @click="setActivePage">
|
||||
<a :class="{ 'menu-active': activePage === 2 }">Item 2</a>
|
||||
</li>
|
||||
<li v-if="isAdmin" id="100" @click="setActivePage">
|
||||
<a :class="{ 'menu-active': activePage === 100 }">实验板控制台</a>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="divider divider-horizontal h-full"></div>
|
||||
<div class="card bg-base-300 w-300 rounded-2xl p-7">
|
||||
<div v-if="activePage === 1">
|
||||
<UserInfo />
|
||||
</div>
|
||||
<div v-else-if="activePage === 2">
|
||||
<!-- 添加对应的组件或内容 -->
|
||||
<div>Item 2 内容</div>
|
||||
</div>
|
||||
<div v-else-if="activePage === 100">
|
||||
<BoardTable />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import BoardTable from "./BoardTable.vue";
|
||||
import { toNumber } from "lodash";
|
||||
import { onMounted, ref } from "vue";
|
||||
import { AuthManager } from "@/utils/AuthManager";
|
||||
import UserInfo from "./UserInfo.vue";
|
||||
|
||||
const activePage = ref(1);
|
||||
const isAdmin = ref(false);
|
||||
|
||||
function setActivePage(event: Event) {
|
||||
const target = event.currentTarget as HTMLLinkElement;
|
||||
const newPage = toNumber(target.id);
|
||||
|
||||
// 如果用户不是管理员但试图访问管理员页面,则忽略
|
||||
if (newPage === 100 && !isAdmin.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
activePage.value = newPage;
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
// 首先验证用户是否已登录
|
||||
const isAuthenticated = await AuthManager.isAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
// 如果未登录,重定向到登录页面
|
||||
// 这里可以使用路由跳转
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证管理员权限
|
||||
isAdmin.value = await AuthManager.verifyAdminAuth();
|
||||
|
||||
// 如果当前页面是管理员页面但用户不是管理员,切换到用户信息页面
|
||||
if (activePage.value === 100 && !isAdmin.value) {
|
||||
activePage.value = 1;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('用户认证检查失败:', error);
|
||||
// 可以在这里处理错误,比如显示错误信息或重定向到登录页面
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.menu-active {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.menu-active::before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
left: -12px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
width: 4px;
|
||||
height: 80%;
|
||||
background-color: var(--color-primary);
|
||||
border-radius: 2px;
|
||||
}
|
||||
</style>
|
||||
590
src/views/User/UserInfo.vue
Normal file
590
src/views/User/UserInfo.vue
Normal file
@@ -0,0 +1,590 @@
|
||||
<template>
|
||||
<div class="space-y-6">
|
||||
<!-- 页面标题 -->
|
||||
<div class="flex items-center gap-3">
|
||||
<User class="w-8 h-8 text-primary" />
|
||||
<h1 class="text-3xl font-bold">用户信息</h1>
|
||||
<!-- 刷新按钮图标 -->
|
||||
<button
|
||||
@click="refreshAllInfo"
|
||||
class="btn btn-ghost btn-sm ml-auto"
|
||||
:disabled="loading"
|
||||
title="刷新信息"
|
||||
>
|
||||
<RefreshCw class="w-5 h-5" :class="{ 'animate-spin': loading }" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 全局加载状态 -->
|
||||
<div v-if="loading" class="flex justify-center items-center py-12">
|
||||
<span class="loading loading-spinner loading-lg text-primary m-2"> </span>
|
||||
加载中...
|
||||
</div>
|
||||
|
||||
<!-- 错误状态 -->
|
||||
<div v-else-if="error" class="alert alert-error">
|
||||
<AlertCircle class="w-5 h-5" />
|
||||
<span>{{ error }}</span>
|
||||
<button @click="refreshAllInfo" class="btn btn-sm btn-outline">
|
||||
<RefreshCw class="w-4 h-4" />
|
||||
重试
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 用户信息内容 -->
|
||||
<div v-else class="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
||||
<!-- 用户基本信息卡片 -->
|
||||
<div class="card bg-base-200 shadow-lg">
|
||||
<div class="card-body">
|
||||
<div class="flex items-center gap-3 mb-4">
|
||||
<UserCircle class="w-6 h-6 text-primary" />
|
||||
<h2 class="card-title">基本信息</h2>
|
||||
</div>
|
||||
|
||||
<div class="space-y-4">
|
||||
<!-- 用户ID -->
|
||||
<div class="flex items-center gap-3 p-3 bg-base-100 rounded-lg">
|
||||
<IdCard class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">用户ID</div>
|
||||
<div class="font-mono text-sm">{{ userInfo?.id || "N/A" }}</div>
|
||||
</div>
|
||||
<button
|
||||
@click="copyToClipboard(userInfo?.id)"
|
||||
class="btn btn-ghost btn-sm"
|
||||
title="复制ID"
|
||||
>
|
||||
<Copy class="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 用户名 -->
|
||||
<div class="flex items-center gap-3 p-3 bg-base-100 rounded-lg">
|
||||
<User class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">用户名</div>
|
||||
<div class="font-semibold">{{ userInfo?.name || "N/A" }}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 邮箱 -->
|
||||
<div class="flex items-center gap-3 p-3 bg-base-100 rounded-lg">
|
||||
<Mail class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">邮箱地址</div>
|
||||
<div class="font-mono text-sm">
|
||||
{{ userInfo?.eMail || "N/A" }}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
@click="copyToClipboard(userInfo?.eMail)"
|
||||
class="btn btn-ghost btn-sm"
|
||||
title="复制邮箱"
|
||||
>
|
||||
<Copy class="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 账户状态 -->
|
||||
<div class="flex items-center gap-3 p-3 bg-base-100 rounded-lg">
|
||||
<Shield class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">账户状态</div>
|
||||
<div class="badge badge-success">已认证</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 绑定过期时间 -->
|
||||
<div
|
||||
v-if="userInfo?.boardExpireTime"
|
||||
class="flex items-center gap-3 p-3 bg-base-100 rounded-lg"
|
||||
>
|
||||
<Clock class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">绑定过期时间</div>
|
||||
<div class="font-mono text-sm">
|
||||
{{ formatExpireTime(userInfo.boardExpireTime) }}
|
||||
</div>
|
||||
<div
|
||||
class="text-xs mt-1"
|
||||
:class="getExpireTimeStatusClass(userInfo.boardExpireTime)"
|
||||
>
|
||||
{{ getExpireTimeStatus(userInfo.boardExpireTime) }}
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
class="badge badge-sm"
|
||||
:class="getExpireTimeBadgeClass(userInfo.boardExpireTime)"
|
||||
>
|
||||
{{ getTimeRemaining(userInfo.boardExpireTime) }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 实验板信息卡片 -->
|
||||
<div class="card bg-base-200 shadow-lg">
|
||||
<div class="card-body">
|
||||
<div class="flex items-center justify-between gap-3 mb-4">
|
||||
<div class="flex items-center gap-3">
|
||||
<Cpu class="w-6 h-6 text-primary" />
|
||||
<h2 class="card-title">绑定实验板</h2>
|
||||
</div>
|
||||
<!-- 操作按钮 - 只有在有绑定实验板时才显示 -->
|
||||
<div v-if="boardInfo" class="flex items-center gap-3">
|
||||
<button
|
||||
@click="testBoardConnection"
|
||||
class="btn btn-secondary btn-sm"
|
||||
:disabled="testingConnection"
|
||||
>
|
||||
<Zap
|
||||
class="w-4 h-4"
|
||||
:class="{ 'animate-pulse': testingConnection }"
|
||||
/>
|
||||
{{ testingConnection ? "测试中..." : "测试连接" }}
|
||||
</button>
|
||||
<button
|
||||
@click="unbindBoard"
|
||||
class="btn btn-error btn-outline btn-sm"
|
||||
:disabled="unbindingBoard"
|
||||
>
|
||||
<Unlink2
|
||||
class="w-4 h-4"
|
||||
:class="{ 'animate-pulse': unbindingBoard }"
|
||||
/>
|
||||
{{ unbindingBoard ? "解绑中..." : "解绑实验板" }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 无实验板绑定 -->
|
||||
<div v-if="!boardInfo" class="text-center py-8">
|
||||
<Unlink class="w-12 h-12 text-base-content/50 mx-auto mb-4" />
|
||||
<div class="text-base-content/70 mb-4">暂无绑定的实验板</div>
|
||||
<!-- 申请实验板按钮 -->
|
||||
<button
|
||||
@click="applyBoard"
|
||||
class="btn btn-primary"
|
||||
:disabled="applyingBoard"
|
||||
>
|
||||
<Plus
|
||||
class="w-4 h-4"
|
||||
:class="{ 'animate-pulse': applyingBoard }"
|
||||
/>
|
||||
{{ applyingBoard ? "申请中..." : "申请实验板" }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 实验板信息 -->
|
||||
<div v-else class="space-y-4">
|
||||
<!-- 实验板ID -->
|
||||
<div class="flex items-center gap-3 p-3 bg-base-100 rounded-lg">
|
||||
<IdCard class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">实验板ID</div>
|
||||
<div class="font-mono text-sm">
|
||||
{{ boardInfo?.id || "N/A" }}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
@click="copyToClipboard(boardInfo?.id)"
|
||||
class="btn btn-ghost btn-sm"
|
||||
title="复制ID"
|
||||
>
|
||||
<Copy class="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 实验板名称 -->
|
||||
<div class="flex items-center gap-3 p-3 bg-base-100 rounded-lg">
|
||||
<Tag class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">实验板名称</div>
|
||||
<div class="font-semibold">
|
||||
{{ boardInfo?.boardName || "N/A" }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- IP地址 -->
|
||||
<div class="flex items-center gap-3 p-3 bg-base-100 rounded-lg">
|
||||
<Globe class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">IP地址</div>
|
||||
<div class="font-mono text-sm">
|
||||
{{ boardInfo?.ipAddr || "N/A" }}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
@click="copyToClipboard(boardInfo?.ipAddr)"
|
||||
class="btn btn-ghost btn-sm"
|
||||
title="复制IP地址"
|
||||
>
|
||||
<Copy class="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 端口 -->
|
||||
<div class="flex items-center gap-3 p-3 bg-base-100 rounded-lg">
|
||||
<Server class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">端口</div>
|
||||
<div class="font-mono text-sm">
|
||||
{{ boardInfo?.port || "N/A" }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 状态 -->
|
||||
<div class="flex items-center gap-3 p-3 bg-base-100 rounded-lg">
|
||||
<Activity class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">状态</div>
|
||||
<div
|
||||
class="badge"
|
||||
:class="getBoardStatusClass(boardInfo?.status)"
|
||||
>
|
||||
{{ getBoardStatusText(boardInfo?.status) }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 固件版本 -->
|
||||
<div class="flex items-center gap-3 p-3 bg-base-100 rounded-lg">
|
||||
<Settings class="w-5 h-5 text-secondary" />
|
||||
<div class="flex-1">
|
||||
<div class="text-sm text-base-content/70">固件版本</div>
|
||||
<div class="font-mono text-sm">
|
||||
{{ boardInfo?.firmVersion || "N/A" }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 使用自定义 Alert 组件 -->
|
||||
<Alert />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from "vue";
|
||||
import { AuthManager } from "@/utils/AuthManager";
|
||||
import { UserInfo, Board, BoardStatus } from "@/APIClient";
|
||||
import { Alert, useAlertStore } from "@/components/Alert";
|
||||
import {
|
||||
User,
|
||||
UserCircle,
|
||||
Mail,
|
||||
IdCard,
|
||||
Copy,
|
||||
Shield,
|
||||
Cpu,
|
||||
Globe,
|
||||
Server,
|
||||
Activity,
|
||||
Settings,
|
||||
Tag,
|
||||
RefreshCw,
|
||||
AlertCircle,
|
||||
Unlink,
|
||||
Unlink2,
|
||||
Zap,
|
||||
Plus,
|
||||
Clock,
|
||||
} from "lucide-vue-next";
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const error = ref("");
|
||||
const userInfo = ref<UserInfo | null>(null);
|
||||
const boardInfo = ref<Board | null>(null);
|
||||
|
||||
// 操作状态
|
||||
const testingConnection = ref(false);
|
||||
const unbindingBoard = ref(false);
|
||||
const applyingBoard = ref(false);
|
||||
|
||||
// 使用自定义 Alert
|
||||
const alertStore = useAlertStore();
|
||||
|
||||
// 加载实验板信息
|
||||
const loadBoardInfo = async () => {
|
||||
if (!userInfo.value?.boardID) {
|
||||
boardInfo.value = null;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
boardInfo.value = await client.getBoardByID(userInfo.value.boardID);
|
||||
} catch (err) {
|
||||
console.error("加载实验板信息失败:", err);
|
||||
boardInfo.value = null;
|
||||
}
|
||||
};
|
||||
|
||||
// 统一的信息加载函数(合并了原来的 loadUserInfo 和 refreshAllInfo)
|
||||
const loadUserInfo = async (showSuccessMessage = false) => {
|
||||
loading.value = true;
|
||||
error.value = "";
|
||||
|
||||
try {
|
||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
||||
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
userInfo.value = await client.getUserInfo();
|
||||
|
||||
// 如果有绑定的实验板ID,加载实验板信息
|
||||
if (userInfo.value?.boardID) {
|
||||
await loadBoardInfo();
|
||||
} else {
|
||||
boardInfo.value = null;
|
||||
}
|
||||
|
||||
if (showSuccessMessage) {
|
||||
alertStore?.success("信息刷新成功");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("加载用户信息失败:", err);
|
||||
error.value = "加载用户信息失败,请检查网络连接或重新登录";
|
||||
if (showSuccessMessage) {
|
||||
alertStore?.error("刷新信息失败,请检查网络连接");
|
||||
}
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 刷新所有信息(调用统一的加载函数,显示成功消息)
|
||||
const refreshAllInfo = async () => {
|
||||
await loadUserInfo(true);
|
||||
};
|
||||
|
||||
// 申请实验板
|
||||
const applyBoard = async () => {
|
||||
applyingBoard.value = true;
|
||||
alertStore?.info("正在申请实验板...");
|
||||
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
|
||||
// 获取可用的实验板
|
||||
const availableBoard = await client.getAvailableBoard(undefined);
|
||||
|
||||
if (availableBoard) {
|
||||
alertStore?.success(`成功申请到实验板: ${availableBoard.boardName}`);
|
||||
|
||||
// 重新加载用户信息以获取最新的绑定状态
|
||||
await loadUserInfo();
|
||||
} else {
|
||||
alertStore?.warn("当前没有可用的实验板,请稍后再试");
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error("申请实验板失败:", err);
|
||||
|
||||
// 根据错误状态码提供更友好的错误信息
|
||||
if (err?.status === 404) {
|
||||
alertStore?.warn("当前没有可用的实验板,请稍后再试");
|
||||
} else if (err?.status === 400) {
|
||||
alertStore?.error("您已经绑定了实验板,无需重复申请");
|
||||
} else {
|
||||
alertStore?.error("申请实验板失败,请检查网络连接或稍后重试");
|
||||
}
|
||||
} finally {
|
||||
applyingBoard.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 测试实验板连接
|
||||
const testBoardConnection = async () => {
|
||||
if (!boardInfo.value) return;
|
||||
|
||||
testingConnection.value = true;
|
||||
alertStore?.info("正在测试连接...");
|
||||
|
||||
try {
|
||||
const jtagClient = AuthManager.createAuthenticatedJtagClient();
|
||||
|
||||
// 使用JTAG客户端读取设备ID Code
|
||||
const idCode = await jtagClient.getDeviceIDCode(
|
||||
boardInfo.value.ipAddr,
|
||||
boardInfo.value.port,
|
||||
);
|
||||
|
||||
// 检查ID Code是否有效(非0xFFFFFFFF表示连接成功)
|
||||
if (idCode !== 0xffffffff && idCode !== 0) {
|
||||
alertStore?.success(
|
||||
`连接测试成功,设备ID: 0x${idCode.toString(16).toUpperCase()}`,
|
||||
);
|
||||
} else {
|
||||
alertStore?.warn("连接测试失败,未检测到有效设备");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("连接测试失败:", err);
|
||||
alertStore?.error("连接测试失败,请检查实验板是否在线");
|
||||
} finally {
|
||||
testingConnection.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 解绑实验板
|
||||
const unbindBoard = async () => {
|
||||
if (!boardInfo.value) return;
|
||||
|
||||
// 确认对话框
|
||||
if (!confirm("确定要解绑当前实验板吗?解绑后需要重新绑定才能使用。")) {
|
||||
return;
|
||||
}
|
||||
|
||||
unbindingBoard.value = true;
|
||||
alertStore?.info("正在解绑实验板...");
|
||||
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedDataClient();
|
||||
const success = await client.unbindBoard();
|
||||
|
||||
if (success) {
|
||||
alertStore?.success("实验板解绑成功");
|
||||
// 清空实验板信息并重新加载用户信息
|
||||
boardInfo.value = null;
|
||||
await loadUserInfo();
|
||||
} else {
|
||||
alertStore?.error("实验板解绑失败");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("解绑实验板失败:", err);
|
||||
alertStore?.error("解绑实验板失败,请稍后重试");
|
||||
} finally {
|
||||
unbindingBoard.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 复制到剪贴板
|
||||
const copyToClipboard = async (text?: string) => {
|
||||
if (!text) return;
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
alertStore?.success("已复制到剪贴板");
|
||||
} catch (err) {
|
||||
alertStore?.error("复制失败");
|
||||
}
|
||||
};
|
||||
|
||||
// 时间相关的工具函数
|
||||
const formatExpireTime = (expireTime: Date) => {
|
||||
return new Date(expireTime).toLocaleString("zh-CN", {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
});
|
||||
};
|
||||
|
||||
const getTimeRemaining = (expireTime: Date) => {
|
||||
const now = new Date();
|
||||
const expire = new Date(expireTime);
|
||||
const timeDiff = expire.getTime() - now.getTime();
|
||||
|
||||
if (timeDiff <= 0) {
|
||||
return "已过期";
|
||||
}
|
||||
|
||||
const hours = Math.floor(timeDiff / (1000 * 60 * 60));
|
||||
const minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
|
||||
|
||||
if (hours > 0) {
|
||||
return `剩余 ${hours}小时${minutes}分钟`;
|
||||
} else {
|
||||
return `剩余 ${minutes}分钟`;
|
||||
}
|
||||
};
|
||||
|
||||
// 获取过期时间相关状态的统一函数
|
||||
const getExpireTimeInfo = (expireTime: Date) => {
|
||||
const now = new Date();
|
||||
const expire = new Date(expireTime);
|
||||
const timeDiff = expire.getTime() - now.getTime();
|
||||
|
||||
if (timeDiff <= 0) {
|
||||
return {
|
||||
status: "已过期",
|
||||
statusClass: "text-error",
|
||||
badgeClass: "badge-error",
|
||||
};
|
||||
} else if (timeDiff <= 30 * 60 * 1000) {
|
||||
return {
|
||||
status: "即将过期",
|
||||
statusClass: "text-warning",
|
||||
badgeClass: "badge-warning",
|
||||
};
|
||||
} else if (timeDiff <= 60 * 60 * 1000) {
|
||||
return {
|
||||
status: "临近过期",
|
||||
statusClass: "text-warning",
|
||||
badgeClass: "badge-warning",
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: "正常",
|
||||
statusClass: "text-success",
|
||||
badgeClass: "badge-success",
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// 使用统一函数的便捷方法
|
||||
const getExpireTimeStatus = (expireTime: Date) =>
|
||||
getExpireTimeInfo(expireTime).status;
|
||||
const getExpireTimeStatusClass = (expireTime: Date) =>
|
||||
getExpireTimeInfo(expireTime).statusClass;
|
||||
const getExpireTimeBadgeClass = (expireTime: Date) =>
|
||||
getExpireTimeInfo(expireTime).badgeClass;
|
||||
|
||||
// 获取实验板状态相关信息的统一函数
|
||||
const getBoardStatusInfo = (status?: BoardStatus) => {
|
||||
switch (status) {
|
||||
case BoardStatus.Available:
|
||||
return { text: "可用", class: "badge-success" };
|
||||
case BoardStatus.Busy:
|
||||
return { text: "使用中", class: "badge-warning" };
|
||||
default:
|
||||
return { text: "未知", class: "badge-neutral" };
|
||||
}
|
||||
};
|
||||
|
||||
// 使用统一函数的便捷方法
|
||||
const getBoardStatusClass = (status?: BoardStatus) =>
|
||||
getBoardStatusInfo(status).class;
|
||||
const getBoardStatusText = (status?: BoardStatus) =>
|
||||
getBoardStatusInfo(status).text;
|
||||
|
||||
// 组件挂载时加载数据
|
||||
onMounted(() => {
|
||||
loadUserInfo();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
/* 添加一些自定义样式优化 */
|
||||
.card {
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.card:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
/* 响应式优化 */
|
||||
@media (max-width: 768px) {
|
||||
.grid-cols-1.lg\:grid-cols-2 {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -1,16 +0,0 @@
|
||||
<template>
|
||||
<header></header>
|
||||
<main class="relative">
|
||||
<div class="w-screen h-screen flex items-center justify-center">
|
||||
<UploadCard />
|
||||
</div>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import UploadCard from "@/components/UploadCard.vue";
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
@import "../assets/main.css";
|
||||
</style>
|
||||
@@ -2,7 +2,8 @@
|
||||
"extends": "@vue/tsconfig/tsconfig.dom.json",
|
||||
"include": [
|
||||
"env.d.ts",
|
||||
"src/**/*",
|
||||
"src/**/*.ts",
|
||||
"src/**/*.tsx",
|
||||
"src/**/*.vue"
|
||||
],
|
||||
"exclude": [
|
||||
|
||||
Reference in New Issue
Block a user