Compare commits
120 Commits
c5f0e706a4
...
pyg
| Author | SHA1 | Date | |
|---|---|---|---|
| aff9da2a60 | |||
|
|
e0ac21d141 | ||
| 8396b7aaea | |||
|
|
a331494fde | ||
|
|
e86cd5464e | ||
|
|
04b136117d | ||
|
|
5c87204ef6 | ||
| 35647d21bb | |||
|
|
51b39cee07 | ||
|
|
0bd1ad8a0e | ||
|
|
f2c7c78b64 | ||
|
|
2f23ffe482 | ||
|
|
9904fecbee | ||
| cb229c2a30 | |||
|
|
e5f2be616c | ||
|
|
2e9e378457 | ||
|
|
9fe0ee959f | ||
| 9adc5295f8 | |||
|
|
8047987935 | ||
|
|
2d77706013 | ||
|
|
c564844673 | ||
| 2adeca3b99 | |||
| bafd06162c | |||
| 8c404d4072 | |||
|
|
d27b5d7737 | ||
|
|
4df583e74b | ||
| 1ca9999f15 | |||
|
|
0cc35ce541 | ||
|
|
d7c02ee6c9 | ||
|
|
6b701658d1 | ||
| 2f1be8b0b7 | |||
| 82bc03b9fb | |||
| 3257a68407 | |||
|
|
6dfd275091 | ||
| 6c5250f9c2 | |||
| 912eb625f5 | |||
| 10e4a82e5b | |||
| ef267721fd | |||
|
|
1d35c36da6 | ||
| 3da0f284f3 | |||
| 23d4459406 | |||
| a4192659d1 | |||
| f200d90fc0 | |||
| 6c1bda50ce | |||
| 3535b94123 | |||
| 5da9d9f4e2 | |||
| e7c8d3fb9e | |||
| e872f24936 | |||
| d1c9710afe | |||
|
|
422aaa89d5 | ||
|
|
5103145d01 | ||
|
|
27c8ceb1db | ||
| d30712d0f6 | |||
| a56a65cc0d | |||
| 9c7bde206b | |||
|
|
1fa944f3c7 | ||
|
|
1492f16fdd | ||
| 042ca40998 | |||
| e4a1c34a6c | |||
|
|
ba79a2093b | ||
| 35bad4027d | |||
| 9af2d3e87e | |||
| e9ad1f0256 | |||
| 12cd35edff | |||
| 69c7cbf4d8 | |||
| 80b6dfb38d | |||
| 0f4386457d | |||
| 08a9be543e | |||
| 688fe05b1b | |||
| 2ff735e06a | |||
| 53eaac43e3 | |||
| f5dd474ba0 | |||
|
|
e4ead72d53 | ||
| fb13a5c484 | |||
| 1053d71d29 | |||
| 56dcbf5caa | |||
| dfe279bf37 | |||
| e3b769b24e | |||
|
|
8e19587a16 | ||
| d551cbe793 | |||
| 822091243e | |||
| bcee42d8c1 | |||
| 9165c2e5f4 | |||
| 8070e03496 | |||
| 43e3cce048 | |||
| bcdefb2779 | |||
| 519094b3a0 | |||
| 57cf82b48f | |||
| b08b86dbbe | |||
| 0cfbebf804 | |||
| 446da52515 | |||
|
|
c70cc46aa9 | ||
|
|
0f850c3ae7 | ||
| 99dc7b52cc | |||
| 0410d14d3a | |||
|
|
474151d412 | ||
|
|
4562be2d01 | ||
| ef76f3e9c7 | |||
|
|
a28ae9be97 | ||
| 9f25391540 | |||
|
|
938ee80979 | ||
|
|
4af7da6344 | ||
|
|
4b140ef683 | ||
| b139542c4c | |||
| be8fed995c | |||
| 49cbdc51d9 | |||
|
|
705e322e41 | ||
|
|
0f3754ce99 | ||
| 89cc2291c0 | |||
|
|
683d918d30 | ||
|
|
d901a440a7 | ||
| 6500c1ce2d | |||
| c9fc6961fa | |||
| 4d6c06a0e0 | |||
|
|
533e2561ab | ||
| e8a16fd446 | |||
| ca906489c2 | |||
|
|
2b1ee90af7 | ||
| 2894ee24be | |||
| 6068a10d67 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -18,6 +18,7 @@ coverage
|
|||||||
|
|
||||||
/cypress/videos/
|
/cypress/videos/
|
||||||
/cypress/screenshots/
|
/cypress/screenshots/
|
||||||
|
DebuggerCmd.md
|
||||||
|
|
||||||
# Editor directories and files
|
# Editor directories and files
|
||||||
.vscode/*
|
.vscode/*
|
||||||
@@ -27,10 +28,10 @@ coverage
|
|||||||
*.suo
|
*.suo
|
||||||
*.ntvs*
|
*.ntvs*
|
||||||
*.njsproj
|
*.njsproj
|
||||||
*.sln
|
|
||||||
*.sw?
|
*.sw?
|
||||||
|
|
||||||
*.tsbuildinfo
|
*.tsbuildinfo
|
||||||
|
|
||||||
# Generated Files
|
# Generated Files
|
||||||
*.sqlite
|
*.sqlite
|
||||||
|
components.d.ts
|
||||||
|
|||||||
10
.justfile
10
.justfile
@@ -15,11 +15,15 @@ clean:
|
|||||||
rm -rf "dist"
|
rm -rf "dist"
|
||||||
rm -rf "wwwroot"
|
rm -rf "wwwroot"
|
||||||
|
|
||||||
update:
|
update: update-node update-dotnet
|
||||||
npm install
|
|
||||||
dotnet restore ./server/server.csproj
|
|
||||||
git submodule update --init --remote --recursive
|
git submodule update --init --remote --recursive
|
||||||
|
|
||||||
|
update-node:
|
||||||
|
npm install
|
||||||
|
|
||||||
|
update-dotnet:
|
||||||
|
dotnet restore ./server/server.csproj
|
||||||
|
|
||||||
# 生成Restful API到网页客户端
|
# 生成Restful API到网页客户端
|
||||||
gen-api:
|
gen-api:
|
||||||
npm run gen-api
|
npm run gen-api
|
||||||
|
|||||||
48
FPGAWebLabServer.sln
Normal file
48
FPGAWebLabServer.sln
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.31903.59
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "server", "server\server.csproj", "{F31D6A0D-0407-41CE-A67E-01B847488EFB}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "server.test", "server.test\server.test.csproj", "{CC274582-AC3C-4FD1-977C-96F1BC2760BD}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
|
Debug|x86 = Debug|x86
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
Release|x86 = Release|x86
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
74
components.d.ts
vendored
74
components.d.ts
vendored
@@ -1,74 +0,0 @@
|
|||||||
/* eslint-disable */
|
|
||||||
// @ts-nocheck
|
|
||||||
// Generated by unplugin-vue-components
|
|
||||||
// Read more: https://github.com/vuejs/core/pull/3399
|
|
||||||
// biome-ignore lint: disable
|
|
||||||
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']
|
|
||||||
DDR: typeof import('./src/components/equipments/DDR.vue')['default']
|
|
||||||
DDS: typeof import('./src/components/equipments/DDS.vue')['default']
|
|
||||||
DDSPropertyEditor: typeof import('./src/components/equipments/DDSPropertyEditor.vue')['default']
|
|
||||||
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']
|
|
||||||
LabComponentsDrawerNew: typeof import('./src/components/LabCanvas/LabComponentsDrawerNew.vue')['default']
|
|
||||||
LoginCard: typeof import('./src/components/LoginCard.vue')['default']
|
|
||||||
MarkdownRenderer: typeof import('./src/components/MarkdownRenderer.vue')['default']
|
|
||||||
MechanicalButton: typeof import('./src/components/equipments/MechanicalButton.vue')['default']
|
|
||||||
MotherBoard: typeof import('./src/components/equipments/MotherBoard.vue')['default']
|
|
||||||
MotherBoardCaps: typeof import('./src/components/equipments/MotherBoardCaps.vue')['default']
|
|
||||||
Navbar: typeof import('./src/components/Navbar.vue')['default']
|
|
||||||
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']
|
|
||||||
Sidebar: typeof import('./src/components/Sidebar.vue')['default']
|
|
||||||
SMA: typeof import('./src/components/equipments/SMA.vue')['default']
|
|
||||||
SMT_LED: typeof import('./src/components/equipments/SMT_LED.vue')['default']
|
|
||||||
SplitterGroup: typeof import('reka-ui')['SplitterGroup']
|
|
||||||
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']
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -23,6 +23,7 @@
|
|||||||
sqls
|
sqls
|
||||||
sql-studio
|
sql-studio
|
||||||
zlib
|
zlib
|
||||||
|
bash
|
||||||
# Backend
|
# Backend
|
||||||
(dotnetCorePackages.combinePackages [
|
(dotnetCorePackages.combinePackages [
|
||||||
dotnetCorePackages.sdk_9_0
|
dotnetCorePackages.sdk_9_0
|
||||||
@@ -38,10 +39,10 @@
|
|||||||
typescript-language-server
|
typescript-language-server
|
||||||
];
|
];
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
export PATH=$PATH:
|
export PATH=$PATH:/home/sikongjueluo/.dotnet/tools
|
||||||
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pkgs.zlib}/lib
|
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pkgs.zlib}/lib
|
||||||
|
export DOTNET_ROOT=${pkgs.dotnetCorePackages.sdk_9_0}/share/dotnet
|
||||||
'';
|
'';
|
||||||
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
495
package-lock.json
generated
495
package-lock.json
generated
@@ -8,11 +8,14 @@
|
|||||||
"name": "fpga-weblab",
|
"name": "fpga-weblab",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@microsoft/signalr": "^9.0.6",
|
||||||
"@svgdotjs/svg.js": "^3.2.4",
|
"@svgdotjs/svg.js": "^3.2.4",
|
||||||
"@tanstack/vue-table": "^8.21.3",
|
"@tanstack/vue-table": "^8.21.3",
|
||||||
"@types/lodash": "^4.17.16",
|
"@types/lodash": "^4.17.16",
|
||||||
|
"@types/signalr": "^2.4.3",
|
||||||
"@vueuse/core": "^13.5.0",
|
"@vueuse/core": "^13.5.0",
|
||||||
"async-mutex": "^0.5.0",
|
"async-mutex": "^0.5.0",
|
||||||
|
"axios": "^1.11.0",
|
||||||
"echarts": "^5.6.0",
|
"echarts": "^5.6.0",
|
||||||
"highlight.js": "^11.11.1",
|
"highlight.js": "^11.11.1",
|
||||||
"konva": "^9.3.20",
|
"konva": "^9.3.20",
|
||||||
@@ -1127,6 +1130,39 @@
|
|||||||
"@jridgewell/sourcemap-codec": "^1.4.14"
|
"@jridgewell/sourcemap-codec": "^1.4.14"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@microsoft/signalr": {
|
||||||
|
"version": "9.0.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/@microsoft/signalr/-/signalr-9.0.6.tgz",
|
||||||
|
"integrity": "sha512-DrhgzFWI9JE4RPTsHYRxh4yr+OhnwKz8bnJe7eIi7mLLjqhJpEb62CiUy/YbFvLqLzcGzlzz1QWgVAW0zyipMQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"abort-controller": "^3.0.0",
|
||||||
|
"eventsource": "^2.0.2",
|
||||||
|
"fetch-cookie": "^2.0.3",
|
||||||
|
"node-fetch": "^2.6.7",
|
||||||
|
"ws": "^7.5.10"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@microsoft/signalr/node_modules/node-fetch": {
|
||||||
|
"version": "2.7.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
|
||||||
|
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"whatwg-url": "^5.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "4.x || >=6.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"encoding": "^0.1.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"encoding": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@polka/url": {
|
"node_modules/@polka/url": {
|
||||||
"version": "1.0.0-next.29",
|
"version": "1.0.0-next.29",
|
||||||
"resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.29.tgz",
|
"resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.29.tgz",
|
||||||
@@ -1844,6 +1880,15 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/jquery": {
|
||||||
|
"version": "3.5.32",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.5.32.tgz",
|
||||||
|
"integrity": "sha512-b9Xbf4CkMqS02YH8zACqN1xzdxc3cO735Qe5AbSUFmyOiaWAbcpqh9Wna+Uk0vgACvoQHpWDg2rGdHkYPLmCiQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@types/sizzle": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@types/lodash": {
|
"node_modules/@types/lodash": {
|
||||||
"version": "4.17.16",
|
"version": "4.17.16",
|
||||||
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.16.tgz",
|
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.16.tgz",
|
||||||
@@ -1860,6 +1905,21 @@
|
|||||||
"undici-types": "~6.21.0"
|
"undici-types": "~6.21.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/signalr": {
|
||||||
|
"version": "2.4.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/signalr/-/signalr-2.4.3.tgz",
|
||||||
|
"integrity": "sha512-W6C1wMRIIhJV9nsw19yhw4h9zlkLnJzsu9dYlH35aHUQblPsDF6UpCcAVu4Ljy4RS3c3uJyV88wf2M2SOWqqZg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@types/jquery": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@types/sizzle": {
|
||||||
|
"version": "2.3.9",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/sizzle/-/sizzle-2.3.9.tgz",
|
||||||
|
"integrity": "sha512-xzLEyKB50yqCUPUJkIsrVvoWNfFUbIZI+RspLWt8u+tIW/BetMBZtgV2LY/2o+tYH8dRvQ+eoPf3NdhQCcLE2w==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/@types/web-bluetooth": {
|
"node_modules/@types/web-bluetooth": {
|
||||||
"version": "0.0.21",
|
"version": "0.0.21",
|
||||||
"resolved": "https://registry.npmjs.org/@types/web-bluetooth/-/web-bluetooth-0.0.21.tgz",
|
"resolved": "https://registry.npmjs.org/@types/web-bluetooth/-/web-bluetooth-0.0.21.tgz",
|
||||||
@@ -2256,6 +2316,18 @@
|
|||||||
"url": "https://github.com/sponsors/antfu"
|
"url": "https://github.com/sponsors/antfu"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/abort-controller": {
|
||||||
|
"version": "3.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz",
|
||||||
|
"integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"event-target-shim": "^5.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/acorn": {
|
"node_modules/acorn": {
|
||||||
"version": "8.15.0",
|
"version": "8.15.0",
|
||||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz",
|
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz",
|
||||||
@@ -2357,6 +2429,12 @@
|
|||||||
"tslib": "^2.4.0"
|
"tslib": "^2.4.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/asynckit": {
|
||||||
|
"version": "0.4.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
||||||
|
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/autoprefixer": {
|
"node_modules/autoprefixer": {
|
||||||
"version": "10.4.21",
|
"version": "10.4.21",
|
||||||
"resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.21.tgz",
|
"resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.21.tgz",
|
||||||
@@ -2395,6 +2473,17 @@
|
|||||||
"postcss": "^8.1.0"
|
"postcss": "^8.1.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/axios": {
|
||||||
|
"version": "1.11.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/axios/-/axios-1.11.0.tgz",
|
||||||
|
"integrity": "sha512-1Lx3WLFQWm3ooKDYZD1eXmoGO9fxYQjrycfHFC8P0sCfQVXyROp0p9PFWBehewBOdCwHc+f/b8I0fMto5eSfwA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"follow-redirects": "^1.15.6",
|
||||||
|
"form-data": "^4.0.4",
|
||||||
|
"proxy-from-env": "^1.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/balanced-match": {
|
"node_modules/balanced-match": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
|
||||||
@@ -2496,6 +2585,19 @@
|
|||||||
"url": "https://github.com/sponsors/sindresorhus"
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/call-bind-apply-helpers": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"es-errors": "^1.3.0",
|
||||||
|
"function-bind": "^1.1.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/caniuse-lite": {
|
"node_modules/caniuse-lite": {
|
||||||
"version": "1.0.30001715",
|
"version": "1.0.30001715",
|
||||||
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001715.tgz",
|
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001715.tgz",
|
||||||
@@ -2542,6 +2644,18 @@
|
|||||||
"fsevents": "~2.3.2"
|
"fsevents": "~2.3.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/combined-stream": {
|
||||||
|
"version": "1.0.8",
|
||||||
|
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
||||||
|
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"delayed-stream": "~1.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/complex.js": {
|
"node_modules/complex.js": {
|
||||||
"version": "2.4.2",
|
"version": "2.4.2",
|
||||||
"resolved": "https://registry.npmjs.org/complex.js/-/complex.js-2.4.2.tgz",
|
"resolved": "https://registry.npmjs.org/complex.js/-/complex.js-2.4.2.tgz",
|
||||||
@@ -2735,6 +2849,15 @@
|
|||||||
"integrity": "sha512-mEQCMmwJu317oSz8CwdIOdwf3xMif1ttiM8LTufzc3g6kR+9Pe236twL8j3IYT1F7GfRgGcW6MWxzZjLIkuHIg==",
|
"integrity": "sha512-mEQCMmwJu317oSz8CwdIOdwf3xMif1ttiM8LTufzc3g6kR+9Pe236twL8j3IYT1F7GfRgGcW6MWxzZjLIkuHIg==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/delayed-stream": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
|
||||||
|
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.4.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/detect-libc": {
|
"node_modules/detect-libc": {
|
||||||
"version": "2.0.3",
|
"version": "2.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.3.tgz",
|
||||||
@@ -2755,6 +2878,20 @@
|
|||||||
"node": ">=0.3.1"
|
"node": ">=0.3.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/dunder-proto": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"call-bind-apply-helpers": "^1.0.1",
|
||||||
|
"es-errors": "^1.3.0",
|
||||||
|
"gopd": "^1.2.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/echarts": {
|
"node_modules/echarts": {
|
||||||
"version": "5.6.0",
|
"version": "5.6.0",
|
||||||
"resolved": "https://registry.npmjs.org/echarts/-/echarts-5.6.0.tgz",
|
"resolved": "https://registry.npmjs.org/echarts/-/echarts-5.6.0.tgz",
|
||||||
@@ -2814,6 +2951,51 @@
|
|||||||
"url": "https://github.com/sponsors/antfu"
|
"url": "https://github.com/sponsors/antfu"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/es-define-property": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/es-errors": {
|
||||||
|
"version": "1.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz",
|
||||||
|
"integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/es-object-atoms": {
|
||||||
|
"version": "1.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz",
|
||||||
|
"integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"es-errors": "^1.3.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/es-set-tostringtag": {
|
||||||
|
"version": "2.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz",
|
||||||
|
"integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"es-errors": "^1.3.0",
|
||||||
|
"get-intrinsic": "^1.2.6",
|
||||||
|
"has-tostringtag": "^1.0.2",
|
||||||
|
"hasown": "^2.0.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/esbuild": {
|
"node_modules/esbuild": {
|
||||||
"version": "0.25.2",
|
"version": "0.25.2",
|
||||||
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.2.tgz",
|
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.2.tgz",
|
||||||
@@ -2877,6 +3059,24 @@
|
|||||||
"integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==",
|
"integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/event-target-shim": {
|
||||||
|
"version": "5.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz",
|
||||||
|
"integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/eventsource": {
|
||||||
|
"version": "2.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/eventsource/-/eventsource-2.0.2.tgz",
|
||||||
|
"integrity": "sha512-IzUmBGPR3+oUG9dUeXynyNmf91/3zUSJg1lCktzKw47OXuhco54U3r9B7O4XX+Rb1Itm9OZ2b0RkTs10bICOxA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/execa": {
|
"node_modules/execa": {
|
||||||
"version": "9.5.2",
|
"version": "9.5.2",
|
||||||
"resolved": "https://registry.npmjs.org/execa/-/execa-9.5.2.tgz",
|
"resolved": "https://registry.npmjs.org/execa/-/execa-9.5.2.tgz",
|
||||||
@@ -2950,6 +3150,16 @@
|
|||||||
"node": "^12.20 || >= 14.13"
|
"node": "^12.20 || >= 14.13"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/fetch-cookie": {
|
||||||
|
"version": "2.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/fetch-cookie/-/fetch-cookie-2.2.0.tgz",
|
||||||
|
"integrity": "sha512-h9AgfjURuCgA2+2ISl8GbavpUdR+WGAM2McW/ovn4tVccegp8ZqCKWSBR8uRdM8dDNlx5WdKRWxBYUwteLDCNQ==",
|
||||||
|
"license": "Unlicense",
|
||||||
|
"dependencies": {
|
||||||
|
"set-cookie-parser": "^2.4.8",
|
||||||
|
"tough-cookie": "^4.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/figures": {
|
"node_modules/figures": {
|
||||||
"version": "6.1.0",
|
"version": "6.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/figures/-/figures-6.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/figures/-/figures-6.1.0.tgz",
|
||||||
@@ -2979,6 +3189,42 @@
|
|||||||
"node": ">=8"
|
"node": ">=8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/follow-redirects": {
|
||||||
|
"version": "1.15.9",
|
||||||
|
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz",
|
||||||
|
"integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==",
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"type": "individual",
|
||||||
|
"url": "https://github.com/sponsors/RubenVerborgh"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"debug": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/form-data": {
|
||||||
|
"version": "4.0.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.4.tgz",
|
||||||
|
"integrity": "sha512-KrGhL9Q4zjj0kiUt5OO4Mr/A/jlI2jDYs5eHBpYHPcBEVSiipAvn2Ko2HnPe20rmcuuvMHNdZFp+4IlGTMF0Ow==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"asynckit": "^0.4.0",
|
||||||
|
"combined-stream": "^1.0.8",
|
||||||
|
"es-set-tostringtag": "^2.1.0",
|
||||||
|
"hasown": "^2.0.2",
|
||||||
|
"mime-types": "^2.1.12"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/formdata-polyfill": {
|
"node_modules/formdata-polyfill": {
|
||||||
"version": "4.0.10",
|
"version": "4.0.10",
|
||||||
"resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
|
"resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
|
||||||
@@ -3036,6 +3282,15 @@
|
|||||||
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
|
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/function-bind": {
|
||||||
|
"version": "1.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
|
||||||
|
"integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/gensync": {
|
"node_modules/gensync": {
|
||||||
"version": "1.0.0-beta.2",
|
"version": "1.0.0-beta.2",
|
||||||
"resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz",
|
"resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz",
|
||||||
@@ -3046,6 +3301,43 @@
|
|||||||
"node": ">=6.9.0"
|
"node": ">=6.9.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/get-intrinsic": {
|
||||||
|
"version": "1.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz",
|
||||||
|
"integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"call-bind-apply-helpers": "^1.0.2",
|
||||||
|
"es-define-property": "^1.0.1",
|
||||||
|
"es-errors": "^1.3.0",
|
||||||
|
"es-object-atoms": "^1.1.1",
|
||||||
|
"function-bind": "^1.1.2",
|
||||||
|
"get-proto": "^1.0.1",
|
||||||
|
"gopd": "^1.2.0",
|
||||||
|
"has-symbols": "^1.1.0",
|
||||||
|
"hasown": "^2.0.2",
|
||||||
|
"math-intrinsics": "^1.1.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/get-proto": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"dunder-proto": "^1.0.1",
|
||||||
|
"es-object-atoms": "^1.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/get-stream": {
|
"node_modules/get-stream": {
|
||||||
"version": "9.0.1",
|
"version": "9.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-9.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-9.0.1.tgz",
|
||||||
@@ -3099,6 +3391,18 @@
|
|||||||
"node": ">=4"
|
"node": ">=4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/gopd": {
|
||||||
|
"version": "1.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",
|
||||||
|
"integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/graceful-fs": {
|
"node_modules/graceful-fs": {
|
||||||
"version": "4.2.11",
|
"version": "4.2.11",
|
||||||
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",
|
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",
|
||||||
@@ -3106,6 +3410,45 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
},
|
},
|
||||||
|
"node_modules/has-symbols": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/has-tostringtag": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"has-symbols": "^1.0.3"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/hasown": {
|
||||||
|
"version": "2.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz",
|
||||||
|
"integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"function-bind": "^1.1.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/he": {
|
"node_modules/he": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
|
||||||
@@ -3723,6 +4066,15 @@
|
|||||||
"node": ">= 18"
|
"node": ">= 18"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/math-intrinsics": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/mathjs": {
|
"node_modules/mathjs": {
|
||||||
"version": "14.4.0",
|
"version": "14.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/mathjs/-/mathjs-14.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/mathjs/-/mathjs-14.4.0.tgz",
|
||||||
@@ -3768,6 +4120,27 @@
|
|||||||
"node": ">= 0.10.0"
|
"node": ">= 0.10.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/mime-db": {
|
||||||
|
"version": "1.52.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
|
||||||
|
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/mime-types": {
|
||||||
|
"version": "2.1.35",
|
||||||
|
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
|
||||||
|
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"mime-db": "1.52.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/minimatch": {
|
"node_modules/minimatch": {
|
||||||
"version": "9.0.5",
|
"version": "9.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz",
|
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz",
|
||||||
@@ -4195,6 +4568,33 @@
|
|||||||
"url": "https://github.com/sponsors/sindresorhus"
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/proxy-from-env": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
|
"node_modules/psl": {
|
||||||
|
"version": "1.15.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/psl/-/psl-1.15.0.tgz",
|
||||||
|
"integrity": "sha512-JZd3gMVBAVQkSs6HdNZo9Sdo0LNcQeMNP3CozBJb3JYC/QUYZTnKxP+f8oWRX4rHP5EurWxqAHTSwUCjlNKa1w==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"punycode": "^2.3.1"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/lupomontero"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/punycode": {
|
||||||
|
"version": "2.3.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
|
||||||
|
"integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/quansync": {
|
"node_modules/quansync": {
|
||||||
"version": "0.2.10",
|
"version": "0.2.10",
|
||||||
"resolved": "https://registry.npmjs.org/quansync/-/quansync-0.2.10.tgz",
|
"resolved": "https://registry.npmjs.org/quansync/-/quansync-0.2.10.tgz",
|
||||||
@@ -4212,6 +4612,12 @@
|
|||||||
],
|
],
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/querystringify": {
|
||||||
|
"version": "2.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
|
||||||
|
"integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/read-package-json-fast": {
|
"node_modules/read-package-json-fast": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/read-package-json-fast/-/read-package-json-fast-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/read-package-json-fast/-/read-package-json-fast-4.0.0.tgz",
|
||||||
@@ -4297,6 +4703,12 @@
|
|||||||
"url": "https://github.com/sponsors/antfu"
|
"url": "https://github.com/sponsors/antfu"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/requires-port": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
|
||||||
|
"integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/resolve-pkg-maps": {
|
"node_modules/resolve-pkg-maps": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz",
|
||||||
@@ -4382,6 +4794,12 @@
|
|||||||
"semver": "bin/semver.js"
|
"semver": "bin/semver.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/set-cookie-parser": {
|
||||||
|
"version": "2.7.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.7.1.tgz",
|
||||||
|
"integrity": "sha512-IOc8uWeOZgnb3ptbCURJWNjWUPcO3ZnTTdzsurqERrP6nPyv+paC55vJM0LpOlT2ne+Ix+9+CRG1MNLlyZ4GjQ==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/shebang-command": {
|
"node_modules/shebang-command": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
|
||||||
@@ -4552,6 +4970,36 @@
|
|||||||
"node": ">=6"
|
"node": ">=6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/tough-cookie": {
|
||||||
|
"version": "4.1.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.4.tgz",
|
||||||
|
"integrity": "sha512-Loo5UUvLD9ScZ6jh8beX1T6sO1w2/MpCRpEP7V280GKMVUQ0Jzar2U3UJPsrdbziLEMMhu3Ujnq//rhiFuIeag==",
|
||||||
|
"license": "BSD-3-Clause",
|
||||||
|
"dependencies": {
|
||||||
|
"psl": "^1.1.33",
|
||||||
|
"punycode": "^2.1.1",
|
||||||
|
"universalify": "^0.2.0",
|
||||||
|
"url-parse": "^1.5.3"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/tough-cookie/node_modules/universalify": {
|
||||||
|
"version": "0.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.2.0.tgz",
|
||||||
|
"integrity": "sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 4.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/tr46": {
|
||||||
|
"version": "0.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
|
||||||
|
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/ts-log": {
|
"node_modules/ts-log": {
|
||||||
"version": "2.2.7",
|
"version": "2.2.7",
|
||||||
"resolved": "https://registry.npmjs.org/ts-log/-/ts-log-2.2.7.tgz",
|
"resolved": "https://registry.npmjs.org/ts-log/-/ts-log-2.2.7.tgz",
|
||||||
@@ -4793,6 +5241,16 @@
|
|||||||
"browserslist": ">= 4.21.0"
|
"browserslist": ">= 4.21.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/url-parse": {
|
||||||
|
"version": "1.5.10",
|
||||||
|
"resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz",
|
||||||
|
"integrity": "sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"querystringify": "^2.1.1",
|
||||||
|
"requires-port": "^1.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/v8-compile-cache-lib": {
|
"node_modules/v8-compile-cache-lib": {
|
||||||
"version": "3.0.1",
|
"version": "3.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",
|
||||||
@@ -5112,6 +5570,12 @@
|
|||||||
"node": ">= 8"
|
"node": ">= 8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/webidl-conversions": {
|
||||||
|
"version": "3.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
|
||||||
|
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
|
||||||
|
"license": "BSD-2-Clause"
|
||||||
|
},
|
||||||
"node_modules/webpack-virtual-modules": {
|
"node_modules/webpack-virtual-modules": {
|
||||||
"version": "0.6.2",
|
"version": "0.6.2",
|
||||||
"resolved": "https://registry.npmjs.org/webpack-virtual-modules/-/webpack-virtual-modules-0.6.2.tgz",
|
"resolved": "https://registry.npmjs.org/webpack-virtual-modules/-/webpack-virtual-modules-0.6.2.tgz",
|
||||||
@@ -5119,6 +5583,16 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/whatwg-url": {
|
||||||
|
"version": "5.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
|
||||||
|
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"tr46": "~0.0.3",
|
||||||
|
"webidl-conversions": "^3.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/which": {
|
"node_modules/which": {
|
||||||
"version": "5.0.0",
|
"version": "5.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/which/-/which-5.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/which/-/which-5.0.0.tgz",
|
||||||
@@ -5135,6 +5609,27 @@
|
|||||||
"node": "^18.17.0 || >=20.5.0"
|
"node": "^18.17.0 || >=20.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/ws": {
|
||||||
|
"version": "7.5.10",
|
||||||
|
"resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz",
|
||||||
|
"integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=8.3.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"bufferutil": "^4.0.1",
|
||||||
|
"utf-8-validate": "^5.0.2"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"bufferutil": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"utf-8-validate": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/yallist": {
|
"node_modules/yallist": {
|
||||||
"version": "3.1.1",
|
"version": "3.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz",
|
||||||
|
|||||||
@@ -12,11 +12,14 @@
|
|||||||
"gen-api": "npx tsx scripts/GenerateWebAPI.ts"
|
"gen-api": "npx tsx scripts/GenerateWebAPI.ts"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@microsoft/signalr": "^9.0.6",
|
||||||
"@svgdotjs/svg.js": "^3.2.4",
|
"@svgdotjs/svg.js": "^3.2.4",
|
||||||
"@tanstack/vue-table": "^8.21.3",
|
"@tanstack/vue-table": "^8.21.3",
|
||||||
"@types/lodash": "^4.17.16",
|
"@types/lodash": "^4.17.16",
|
||||||
|
"@types/signalr": "^2.4.3",
|
||||||
"@vueuse/core": "^13.5.0",
|
"@vueuse/core": "^13.5.0",
|
||||||
"async-mutex": "^0.5.0",
|
"async-mutex": "^0.5.0",
|
||||||
|
"axios": "^1.11.0",
|
||||||
"echarts": "^5.6.0",
|
"echarts": "^5.6.0",
|
||||||
"highlight.js": "^11.11.1",
|
"highlight.js": "^11.11.1",
|
||||||
"konva": "^9.3.20",
|
"konva": "^9.3.20",
|
||||||
|
|||||||
@@ -1,22 +1,42 @@
|
|||||||
import { spawn, exec, ChildProcess } from 'child_process';
|
import { spawn, exec, ChildProcess } from "child_process";
|
||||||
import { promisify } from 'util';
|
import { promisify } from "util";
|
||||||
import fetch from 'node-fetch';
|
import fetch from "node-fetch";
|
||||||
|
import * as fs from "fs";
|
||||||
|
|
||||||
const execAsync = promisify(exec);
|
const execAsync = promisify(exec);
|
||||||
|
|
||||||
async function waitForServer(url: string, maxRetries: number = 30, interval: number = 1000): Promise<boolean> {
|
// Windows 支持函数
|
||||||
|
function getCommand(command: string): string {
|
||||||
|
// dotnet 在 Windows 上不需要 .cmd 后缀
|
||||||
|
if (command === "dotnet") {
|
||||||
|
return "dotnet";
|
||||||
|
}
|
||||||
|
return process.platform === "win32" ? `${command}.cmd` : command;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSpawnOptions() {
|
||||||
|
return process.platform === "win32"
|
||||||
|
? { stdio: "pipe", shell: true }
|
||||||
|
: { stdio: "pipe" };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function waitForServer(
|
||||||
|
url: string,
|
||||||
|
maxRetries: number = 30,
|
||||||
|
interval: number = 1000,
|
||||||
|
): Promise<boolean> {
|
||||||
for (let i = 0; i < maxRetries; i++) {
|
for (let i = 0; i < maxRetries; i++) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
console.log('✓ Server is ready');
|
console.log("✓ Server is ready");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Server not ready yet
|
// Server not ready yet
|
||||||
}
|
}
|
||||||
console.log(`Waiting for server... (${i + 1}/${maxRetries})`);
|
console.log(`Waiting for server... (${i + 1}/${maxRetries})`);
|
||||||
await new Promise(resolve => setTimeout(resolve, interval));
|
await new Promise((resolve) => setTimeout(resolve, interval));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -26,34 +46,39 @@ let serverProcess: ChildProcess | null = null;
|
|||||||
let webProcess: ChildProcess | null = null;
|
let webProcess: ChildProcess | null = null;
|
||||||
|
|
||||||
async function startWeb(): Promise<ChildProcess> {
|
async function startWeb(): Promise<ChildProcess> {
|
||||||
console.log('Starting Vite frontend...');
|
console.log("Starting Vite frontend...");
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const process = spawn('npm', ['run', 'dev'], {
|
const process = spawn(
|
||||||
stdio: 'pipe'
|
getCommand("npm"),
|
||||||
});
|
["run", "dev"],
|
||||||
|
getSpawnOptions() as any,
|
||||||
|
);
|
||||||
|
|
||||||
let webStarted = false;
|
let webStarted = false;
|
||||||
|
|
||||||
process.stdout?.on('data', (data) => {
|
process.stdout?.on("data", (data) => {
|
||||||
const output = data.toString();
|
const output = data.toString();
|
||||||
console.log(`Web: ${output}`);
|
console.log(`Web: ${output}`);
|
||||||
|
|
||||||
// 检查 Vite 是否已启动
|
// 检查 Vite 是否已启动
|
||||||
if ((output.includes('Local:') || output.includes('ready in')) && !webStarted) {
|
if (
|
||||||
|
(output.includes("Local:") || output.includes("ready in")) &&
|
||||||
|
!webStarted
|
||||||
|
) {
|
||||||
webStarted = true;
|
webStarted = true;
|
||||||
resolve(process);
|
resolve(process);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
process.stderr?.on('data', (data) => {
|
process.stderr?.on("data", (data) => {
|
||||||
console.error(`Web Error: ${data}`);
|
console.error(`Web Error: ${data}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('error', (error) => {
|
process.on("error", (error) => {
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('exit', (code, signal) => {
|
process.on("exit", (code, signal) => {
|
||||||
console.log(`Web process exited with code ${code} and signal ${signal}`);
|
console.log(`Web process exited with code ${code} and signal ${signal}`);
|
||||||
if (!webStarted) {
|
if (!webStarted) {
|
||||||
reject(new Error(`Web process exited unexpectedly with code ${code}`));
|
reject(new Error(`Web process exited unexpectedly with code ${code}`));
|
||||||
@@ -66,45 +91,53 @@ async function startWeb(): Promise<ChildProcess> {
|
|||||||
// 超时处理
|
// 超时处理
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (!webStarted) {
|
if (!webStarted) {
|
||||||
reject(new Error('Web server failed to start within timeout'));
|
reject(new Error("Web server failed to start within timeout"));
|
||||||
}
|
}
|
||||||
}, 30000); // 30秒超时
|
}, 10000); // 10秒超时
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function startServer(): Promise<ChildProcess> {
|
async function startServer(): Promise<ChildProcess> {
|
||||||
console.log('Starting .NET server...');
|
console.log("Starting .NET server...");
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const process = spawn('dotnet', ['run', '--property:Configuration=Release'], {
|
const process = spawn(
|
||||||
cwd: 'server',
|
getCommand("dotnet"),
|
||||||
stdio: 'pipe'
|
["run", "--property:Configuration=Release"],
|
||||||
});
|
{
|
||||||
|
cwd: "server",
|
||||||
|
...getSpawnOptions(),
|
||||||
|
} as any,
|
||||||
|
);
|
||||||
|
|
||||||
let serverStarted = false;
|
let serverStarted = false;
|
||||||
|
|
||||||
process.stdout?.on('data', (data) => {
|
process.stdout?.on("data", (data) => {
|
||||||
const output = data.toString();
|
const output = data.toString();
|
||||||
console.log(`Server: ${output}`);
|
console.log(`Server: ${output}`);
|
||||||
|
|
||||||
// 检查服务器是否已启动
|
// 检查服务器是否已启动
|
||||||
if (output.includes('Now listening on:') && !serverStarted) {
|
if (output.includes("Now listening on:") && !serverStarted) {
|
||||||
serverStarted = true;
|
serverStarted = true;
|
||||||
resolve(process);
|
resolve(process);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
process.stderr?.on('data', (data) => {
|
process.stderr?.on("data", (data) => {
|
||||||
console.error(`Server Error: ${data}`);
|
console.error(`Server Error: ${data}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('error', (error) => {
|
process.on("error", (error) => {
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('exit', (code, signal) => {
|
process.on("exit", (code, signal) => {
|
||||||
console.log(`Server process exited with code ${code} and signal ${signal}`);
|
console.log(
|
||||||
|
`Server process exited with code ${code} and signal ${signal}`,
|
||||||
|
);
|
||||||
if (!serverStarted) {
|
if (!serverStarted) {
|
||||||
reject(new Error(`Server process exited unexpectedly with code ${code}`));
|
reject(
|
||||||
|
new Error(`Server process exited unexpectedly with code ${code}`),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -114,185 +147,208 @@ async function startServer(): Promise<ChildProcess> {
|
|||||||
// 超时处理
|
// 超时处理
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (!serverStarted) {
|
if (!serverStarted) {
|
||||||
reject(new Error('Server failed to start within timeout'));
|
reject(new Error("Server failed to start within timeout"));
|
||||||
}
|
}
|
||||||
}, 30000); // 30秒超时
|
}, 10000); // 10秒超时
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function stopServer(): Promise<void> {
|
async function stopServer(): Promise<void> {
|
||||||
console.log('Stopping server...');
|
console.log("Stopping server...");
|
||||||
|
|
||||||
if (!serverProcess) {
|
if (!serverProcess) {
|
||||||
console.log('No server process to stop');
|
console.log("No server process to stop");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 检查进程是否还存在
|
// 检查进程是否还存在
|
||||||
if (serverProcess.killed || serverProcess.exitCode !== null) {
|
if (serverProcess.killed || serverProcess.exitCode !== null) {
|
||||||
console.log('✓ Server process already terminated');
|
console.log("✓ Server process already terminated");
|
||||||
serverProcess = null;
|
serverProcess = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送 SIGTERM 信号
|
// 发送 SIGTERM 信号
|
||||||
const killed = serverProcess.kill('SIGTERM');
|
const killed = serverProcess.kill("SIGTERM");
|
||||||
if (!killed) {
|
if (!killed) {
|
||||||
console.warn('Failed to send SIGTERM to server process');
|
console.warn("Failed to send SIGTERM to server process");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 等待进程优雅退出
|
// 设置超时,如果 3 秒内没有退出则强制终止
|
||||||
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) => {
|
const timeoutPromise = new Promise<void>((resolve) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (serverProcess && !serverProcess.killed && serverProcess.exitCode === null) {
|
if (
|
||||||
console.log('Force killing server process...');
|
serverProcess &&
|
||||||
serverProcess.kill('SIGKILL');
|
!serverProcess.killed &&
|
||||||
|
serverProcess.exitCode === null
|
||||||
|
) {
|
||||||
|
console.log("Force killing server process...");
|
||||||
|
serverProcess.kill("SIGKILL");
|
||||||
}
|
}
|
||||||
resolve();
|
resolve();
|
||||||
}, 5000);
|
}, 3000); // 减少超时时间到3秒
|
||||||
});
|
});
|
||||||
|
|
||||||
await Promise.race([exitPromise, timeoutPromise]);
|
await Promise.race([timeoutPromise]);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('Warning: Could not stop server process:', error);
|
console.warn("Warning: Could not stop server process:", error);
|
||||||
} finally {
|
} finally {
|
||||||
serverProcess = null;
|
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> {
|
async function stopWeb(): Promise<void> {
|
||||||
console.log('Stopping web server...');
|
console.log("Stopping web server...");
|
||||||
|
|
||||||
if (!webProcess) {
|
if (!webProcess) {
|
||||||
console.log('No web process to stop');
|
console.log("No web process to stop");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 检查进程是否还存在
|
// 检查进程是否还存在
|
||||||
if (webProcess.killed || webProcess.exitCode !== null) {
|
if (webProcess.killed || webProcess.exitCode !== null) {
|
||||||
console.log('✓ Web process already terminated');
|
console.log("✓ Web process already terminated");
|
||||||
webProcess = null;
|
webProcess = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送 SIGTERM 信号
|
// 发送 SIGTERM 信号
|
||||||
const killed = webProcess.kill('SIGTERM');
|
const killed = webProcess.kill("SIGTERM");
|
||||||
if (!killed) {
|
if (!killed) {
|
||||||
console.warn('Failed to send SIGTERM to web process');
|
console.warn("Failed to send SIGTERM to web process");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 等待进程优雅退出
|
// 设置超时,如果 3 秒内没有退出则强制终止
|
||||||
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) => {
|
const timeoutPromise = new Promise<void>((resolve) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (webProcess && !webProcess.killed && webProcess.exitCode === null) {
|
if (webProcess && !webProcess.killed && webProcess.exitCode === null) {
|
||||||
console.log('Force killing web process...');
|
console.log("Force killing web process...");
|
||||||
webProcess.kill('SIGKILL');
|
webProcess.kill("SIGKILL");
|
||||||
}
|
}
|
||||||
resolve();
|
resolve();
|
||||||
}, 5000);
|
}, 3000);
|
||||||
});
|
});
|
||||||
|
|
||||||
await Promise.race([exitPromise, timeoutPromise]);
|
await Promise.race([timeoutPromise]);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('Warning: Could not stop web process:', error);
|
console.warn("Warning: Could not stop web process:", error);
|
||||||
} finally {
|
} finally {
|
||||||
webProcess = null;
|
webProcess = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 额外清理:确保没有遗留的 npm/node 进程
|
async function postProcessApiClient(): Promise<void> {
|
||||||
|
console.log("Post-processing API client...");
|
||||||
try {
|
try {
|
||||||
if (process.platform !== 'win32') {
|
const filePath = "src/APIClient.ts";
|
||||||
// 清理可能的 vite 进程
|
|
||||||
await execAsync('pkill -f "vite"').catch(() => {
|
// 检查文件是否存在
|
||||||
// 忽略错误,可能没有匹配的进程
|
if (!fs.existsSync(filePath)) {
|
||||||
});
|
throw new Error(`API client file not found: ${filePath}`);
|
||||||
}
|
|
||||||
} catch (cleanupError) {
|
|
||||||
// 忽略清理错误
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 读取文件内容
|
||||||
|
let content = fs.readFileSync(filePath, "utf8");
|
||||||
|
|
||||||
|
// 替换 ArgumentException 中的 message 属性声明
|
||||||
|
content = content.replace(
|
||||||
|
/(\s+)message!:\s*string;/g,
|
||||||
|
"$1declare message: string;",
|
||||||
|
);
|
||||||
|
content = content.replace(
|
||||||
|
"{ AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, CancelToken }",
|
||||||
|
"{ AxiosError, type AxiosInstance, type AxiosRequestConfig, type AxiosResponse, type CancelToken }",
|
||||||
|
);
|
||||||
|
|
||||||
|
// 写回文件
|
||||||
|
fs.writeFileSync(filePath, content, "utf8");
|
||||||
|
|
||||||
|
console.log("✓ API client post-processing completed");
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to post-process API client: ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function generateApiClient(): Promise<void> {
|
async function generateApiClient(): Promise<void> {
|
||||||
console.log('Generating API client...');
|
console.log("Generating API client...");
|
||||||
try {
|
try {
|
||||||
await execAsync('npx nswag openapi2tsclient /input:http://localhost:5000/swagger/v1/swagger.json /output:src/APIClient.ts');
|
const url = "http://127.0.0.1:5000/GetAPIClientCode";
|
||||||
console.log('✓ API client generated successfully');
|
const response = await fetch(url);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed to fetch API client code: ${response.status} ${response.statusText}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const code = await response.text();
|
||||||
|
|
||||||
|
// 写入 APIClient.ts
|
||||||
|
const filePath = "src/APIClient.ts";
|
||||||
|
fs.writeFileSync(filePath, code, "utf8");
|
||||||
|
console.log("✓ API client code fetched and written successfully");
|
||||||
|
|
||||||
|
// 添加后处理步骤
|
||||||
|
await postProcessApiClient();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`Failed to generate API client: ${error}`);
|
throw new Error(`Failed to generate API client: ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function generateSignalRClient(): Promise<void> {
|
||||||
|
console.log("Generating SignalR TypeScript client...");
|
||||||
|
try {
|
||||||
|
const { stdout, stderr } = await execAsync(
|
||||||
|
"dotnet tsrts --project ./server/server.csproj --output ./src/",
|
||||||
|
);
|
||||||
|
if (stdout) console.log(stdout);
|
||||||
|
if (stderr) console.error(stderr);
|
||||||
|
console.log("✓ SignalR TypeScript client generated successfully");
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to generate SignalR client: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function main(): Promise<void> {
|
async function main(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
// Generate SignalR client
|
||||||
|
await generateSignalRClient();
|
||||||
|
console.log("✓ SignalR TypeScript client generated successfully");
|
||||||
|
|
||||||
// Start web frontend first
|
// Start web frontend first
|
||||||
await startWeb();
|
await startWeb();
|
||||||
console.log('✓ Frontend started');
|
console.log("✓ Frontend started");
|
||||||
|
|
||||||
// Wait a bit for frontend to fully initialize
|
// Wait a bit for frontend to fully initialize
|
||||||
await new Promise(resolve => setTimeout(resolve, 3000));
|
await new Promise((resolve) => setTimeout(resolve, 3000));
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
await startServer();
|
await startServer();
|
||||||
console.log('✓ Backend started');
|
console.log("✓ Backend started");
|
||||||
|
|
||||||
// Wait for server to be ready (给服务器额外时间完全启动)
|
// Wait for server to be ready (给服务器额外时间完全启动)
|
||||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
||||||
|
|
||||||
// Check if swagger endpoint is available
|
// Check if swagger endpoint is available
|
||||||
const serverReady = await waitForServer('http://localhost:5000/swagger/v1/swagger.json');
|
const serverReady = await waitForServer(
|
||||||
|
"http://localhost:5000/swagger/v1/swagger.json",
|
||||||
|
);
|
||||||
|
|
||||||
if (!serverReady) {
|
if (!serverReady) {
|
||||||
throw new Error('Server failed to start within the expected time');
|
throw new Error("Server failed to start within the expected time");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate API client
|
// Generate API client
|
||||||
await generateApiClient();
|
await generateApiClient();
|
||||||
|
|
||||||
console.log('✓ API generation completed successfully');
|
console.log("✓ API generation completed successfully");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Error:', error);
|
console.error("❌ Error:", error);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
} finally {
|
} finally {
|
||||||
// Always try to stop processes in order: server first, then web
|
// Always try to stop processes in order: server first, then web
|
||||||
@@ -301,35 +357,73 @@ async function main(): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 改进的进程终止处理
|
// 改进的进程终止处理 - 添加防重复执行
|
||||||
|
let isCleaningUp = false;
|
||||||
|
|
||||||
const cleanup = async (signal: string) => {
|
const cleanup = async (signal: string) => {
|
||||||
|
if (isCleaningUp) {
|
||||||
|
console.log("Cleanup already in progress, ignoring signal");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
isCleaningUp = true;
|
||||||
console.log(`\nReceived ${signal}, cleaning up...`);
|
console.log(`\nReceived ${signal}, cleaning up...`);
|
||||||
await stopServer();
|
|
||||||
await stopWeb();
|
try {
|
||||||
|
await Promise.all([stopServer(), stopWeb()]);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error during cleanup:", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 立即退出,不等待
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
};
|
};
|
||||||
|
|
||||||
process.on('SIGINT', () => cleanup('SIGINT'));
|
process.on("SIGINT", () => cleanup("SIGINT"));
|
||||||
process.on('SIGTERM', () => cleanup('SIGTERM'));
|
process.on("SIGTERM", () => cleanup("SIGTERM"));
|
||||||
|
|
||||||
// 处理未捕获的异常
|
// 处理未捕获的异常
|
||||||
process.on('uncaughtException', async (error) => {
|
process.on("uncaughtException", async (error) => {
|
||||||
console.error('❌ Uncaught exception:', error);
|
if (isCleaningUp) return;
|
||||||
await stopServer();
|
|
||||||
await stopWeb();
|
console.error("❌ Uncaught exception:", error);
|
||||||
|
isCleaningUp = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await Promise.all([stopServer(), stopWeb()]);
|
||||||
|
} catch (cleanupError) {
|
||||||
|
console.error("Error during cleanup:", cleanupError);
|
||||||
|
}
|
||||||
|
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('unhandledRejection', async (reason, promise) => {
|
process.on("unhandledRejection", async (reason, promise) => {
|
||||||
console.error('❌ Unhandled rejection at:', promise, 'reason:', reason);
|
if (isCleaningUp) return;
|
||||||
await stopServer();
|
|
||||||
await stopWeb();
|
console.error("❌ Unhandled rejection at:", promise, "reason:", reason);
|
||||||
|
isCleaningUp = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await Promise.all([stopServer(), stopWeb()]);
|
||||||
|
} catch (cleanupError) {
|
||||||
|
console.error("Error during cleanup:", cleanupError);
|
||||||
|
}
|
||||||
|
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
main().catch(async (error) => {
|
main().catch(async (error) => {
|
||||||
console.error('❌ Unhandled error:', error);
|
if (isCleaningUp) return;
|
||||||
await stopServer();
|
|
||||||
await stopWeb();
|
console.error("❌ Unhandled error:", error);
|
||||||
|
isCleaningUp = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await Promise.all([stopServer(), stopWeb()]);
|
||||||
|
} catch (cleanupError) {
|
||||||
|
console.error("Error during cleanup:", cleanupError);
|
||||||
|
}
|
||||||
|
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
using System.Buffers.Binary;
|
|
||||||
using Common;
|
|
||||||
|
|
||||||
namespace server.test;
|
|
||||||
|
|
||||||
public class CommonTest
|
|
||||||
{
|
|
||||||
[Fact]
|
|
||||||
public void ReverseBytesTest()
|
|
||||||
{
|
|
||||||
var rnd = new Random();
|
|
||||||
var bytesLen = 8;
|
|
||||||
var bytesArray = new byte[bytesLen];
|
|
||||||
rnd.NextBytes(bytesArray);
|
|
||||||
|
|
||||||
var rev2Bytes = new byte[] {
|
|
||||||
bytesArray[1],
|
|
||||||
bytesArray[0],
|
|
||||||
bytesArray[3],
|
|
||||||
bytesArray[2],
|
|
||||||
bytesArray[5],
|
|
||||||
bytesArray[4],
|
|
||||||
bytesArray[7],
|
|
||||||
bytesArray[6],
|
|
||||||
};
|
|
||||||
Assert.Equal(Number.ReverseBytes(bytesArray, 2).Value, rev2Bytes);
|
|
||||||
|
|
||||||
var rev4Bytes = new byte[] {
|
|
||||||
bytesArray[3],
|
|
||||||
bytesArray[2],
|
|
||||||
bytesArray[1],
|
|
||||||
bytesArray[0],
|
|
||||||
bytesArray[7],
|
|
||||||
bytesArray[6],
|
|
||||||
bytesArray[5],
|
|
||||||
bytesArray[4],
|
|
||||||
};
|
|
||||||
Assert.Equal(Number.ReverseBytes(bytesArray, 4).Value, rev4Bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ToBitTest()
|
|
||||||
{
|
|
||||||
Assert.Equal(true, Number.ToBit(0xFF, 3).Value);
|
|
||||||
Assert.Equal(false, Number.ToBit(0x00, 3).Value);
|
|
||||||
Assert.Equal(true, Number.ToBit(0xAA, 3).Value);
|
|
||||||
Assert.Equal(false, Number.ToBit(0xAA, 2).Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ReverseBits()
|
|
||||||
{
|
|
||||||
Assert.Equal((byte)0x05, Common.Number.ReverseBits((byte)0xA0));
|
|
||||||
|
|
||||||
var bytesSrc = new byte[] { 0xAB, 0x00, 0x00, 0x01 };
|
|
||||||
var bytes = new byte[4];
|
|
||||||
for (int i = 0; i < 4; i++)
|
|
||||||
bytes[i] = Common.Number.ReverseBits(bytesSrc[i]);
|
|
||||||
Assert.Equal(new byte[] { 0xD5, 0x00, 0x00, 0x80 }, bytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
310
server.test/NumberTest.cs
Normal file
310
server.test/NumberTest.cs
Normal file
@@ -0,0 +1,310 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using Common;
|
||||||
|
|
||||||
|
namespace CommonTest;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 针对 Common.Number 的单元测试,覆盖所有公开方法
|
||||||
|
/// </summary>
|
||||||
|
public class NumberTest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 NumberToBytes 的正常与异常情况
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_NumberToBytes()
|
||||||
|
{
|
||||||
|
// 测试大端(isLowNumHigh=false)
|
||||||
|
var result1 = Number.NumberToBytes(0x12345678ABCDEF01, 8, false);
|
||||||
|
Assert.True(result1.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x01 }, result1.Value);
|
||||||
|
|
||||||
|
// 测试小端(isLowNumHigh=true)
|
||||||
|
var result2 = Number.NumberToBytes(0x12345678ABCDEF01, 8, true);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0x01, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12 }, result2.Value);
|
||||||
|
|
||||||
|
// 测试长度不足(4字节)
|
||||||
|
var result3 = Number.NumberToBytes(0x12345678, 4, false);
|
||||||
|
Assert.True(result3.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0x12, 0x34, 0x56, 0x78 }, result3.Value);
|
||||||
|
|
||||||
|
// 测试超长
|
||||||
|
var result4 = Number.NumberToBytes(0x1, 9, false);
|
||||||
|
Assert.False(result4.IsSuccessful);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 BytesToUInt64 的正常与异常情况
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_BytesToUInt64()
|
||||||
|
{
|
||||||
|
// 正常大端
|
||||||
|
var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x01 };
|
||||||
|
var result = Number.BytesToUInt64((byte[])bytes.Clone(), false);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal(0x12345678ABCDEF01UL, result.Value);
|
||||||
|
|
||||||
|
// 正常小端
|
||||||
|
var bytes2 = new byte[] { 0x01, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12 };
|
||||||
|
var result2 = Number.BytesToUInt64((byte[])bytes2.Clone(), true);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.Equal(0x12345678ABCDEF01UL, result2.Value);
|
||||||
|
|
||||||
|
// 异常:长度超限
|
||||||
|
var result3 = Number.BytesToUInt64(new byte[9], false);
|
||||||
|
Assert.False(result3.IsSuccessful);
|
||||||
|
|
||||||
|
// 异常:不足8字节
|
||||||
|
var result4 = Number.BytesToUInt64(new byte[] { 0x01, 0x02 }, false);
|
||||||
|
Assert.False(result4.IsSuccessful); // BitConverter.ToUInt64 需要8字节
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 BytesToUInt32 的正常与异常情况
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_BytesToUInt32()
|
||||||
|
{
|
||||||
|
// 正常大端
|
||||||
|
var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78 };
|
||||||
|
var result = Number.BytesToUInt32((byte[])bytes.Clone(), false);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal(0x12345678U, result.Value);
|
||||||
|
|
||||||
|
// 正常小端
|
||||||
|
var bytes2 = new byte[] { 0x78, 0x56, 0x34, 0x12 };
|
||||||
|
var result2 = Number.BytesToUInt32((byte[])bytes2.Clone(), true);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.Equal(0x12345678U, result2.Value);
|
||||||
|
|
||||||
|
// 异常:长度超限
|
||||||
|
var result3 = Number.BytesToUInt32(new byte[5], false);
|
||||||
|
Assert.False(result3.IsSuccessful);
|
||||||
|
|
||||||
|
// 异常:不足4字节
|
||||||
|
var result4 = Number.BytesToUInt32(new byte[] { 0x01, 0x02 }, false);
|
||||||
|
Assert.False(result4.IsSuccessful); // BitConverter.ToUInt32 需要4字节
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 UInt32ArrayToBytes 的正常与异常情况
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_UInt32ArrayToBytes()
|
||||||
|
{
|
||||||
|
// 正常情况
|
||||||
|
var arr = new UInt32[] { 0x12345678, 0xABCDEF01 };
|
||||||
|
var result = Number.UInt32ArrayToBytes(arr);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
// BlockCopy 按小端序
|
||||||
|
Assert.Equal(new byte[] { 0x78, 0x56, 0x34, 0x12, 0x01, 0xEF, 0xCD, 0xAB }, result.Value);
|
||||||
|
|
||||||
|
// 空数组
|
||||||
|
var result2 = Number.UInt32ArrayToBytes(new UInt32[0]);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.Empty(result2.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 MultiBitsToBytes 和 MultiBitsToNumber (ulong)
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_MultiBitsToBytesAndNumber_Ulong()
|
||||||
|
{
|
||||||
|
// 合并两个比特段
|
||||||
|
var result = Number.MultiBitsToNumber(0b101UL, 3, 0b11UL, 2);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal((ulong)0b10111, result.Value);
|
||||||
|
|
||||||
|
// 合并为字节数组
|
||||||
|
var bytesResult = Number.MultiBitsToBytes(0b101UL, 3, 0b11UL, 2);
|
||||||
|
Assert.True(bytesResult.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0b10111 }, bytesResult.Value);
|
||||||
|
|
||||||
|
// 超过64位
|
||||||
|
var failResult = Number.MultiBitsToNumber(0xFFFFFFFFFFFFFFFF, 64, 1, 1);
|
||||||
|
Assert.False(failResult.IsSuccessful);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 MultiBitsToNumber (uint)
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_MultiBitsToNumber_Uint()
|
||||||
|
{
|
||||||
|
var result = Number.MultiBitsToNumber(0b101U, 3, 0b11U, 2);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal((uint)0b10111, result.Value);
|
||||||
|
|
||||||
|
// 超过64位
|
||||||
|
var failResult = Number.MultiBitsToNumber(uint.MaxValue, 64, 1, 1);
|
||||||
|
Assert.False(failResult.IsSuccessful);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 BitsCheck (ulong)
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_BitsCheck_Ulong()
|
||||||
|
{
|
||||||
|
// 完全匹配
|
||||||
|
Assert.True(Number.BitsCheck(0b1101UL, 0b1101UL));
|
||||||
|
// 不匹配
|
||||||
|
Assert.False(Number.BitsCheck(0b1101UL, 0b1001UL));
|
||||||
|
// 掩码
|
||||||
|
Assert.True(Number.BitsCheck(0b1101UL, 0b1001UL, 0b1001UL));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 BitsCheck (uint)
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_BitsCheck_Uint()
|
||||||
|
{
|
||||||
|
Assert.True(Number.BitsCheck(0b1011U, 0b1011U));
|
||||||
|
Assert.False(Number.BitsCheck(0b1011U, 0b1001U));
|
||||||
|
Assert.True(Number.BitsCheck(0b1011U, 0b1001U, 0b1001U));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 ToBit
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_ToBit()
|
||||||
|
{
|
||||||
|
// 取第0位
|
||||||
|
var result = Number.ToBit(0b1010U, 0);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.False(result.Value);
|
||||||
|
|
||||||
|
// 取第1位
|
||||||
|
var result2 = Number.ToBit(0b1010U, 1);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.True(result2.Value);
|
||||||
|
|
||||||
|
// 负数位置
|
||||||
|
var result3 = Number.ToBit(0b1010U, -1);
|
||||||
|
Assert.False(result3.IsSuccessful);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 BitsToNumber
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_BitsToNumber()
|
||||||
|
{
|
||||||
|
// 5位BitArray
|
||||||
|
var bits = new BitArray(new bool[] { true, true, false, true, false }); // 0b01011
|
||||||
|
var result = Number.BitsToNumber(bits);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal((uint)0b01011, result.Value);
|
||||||
|
|
||||||
|
// 超过32位
|
||||||
|
var bits2 = new BitArray(33);
|
||||||
|
Assert.Throws<ArgumentException>(() => Number.BitsToNumber(bits2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 StringToBytes
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_StringToBytes()
|
||||||
|
{
|
||||||
|
// 16进制字符串
|
||||||
|
var bytes = Number.StringToBytes("1234ABCD");
|
||||||
|
Assert.Equal(new byte[] { 0x12, 0x34, 0xAB, 0xCD }, bytes);
|
||||||
|
|
||||||
|
// 8位字符串
|
||||||
|
var bytes2 = Number.StringToBytes("01020304");
|
||||||
|
Assert.Equal(new byte[] { 0x01, 0x02, 0x03, 0x04 }, bytes2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 ReverseBytes
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_ReverseBytes()
|
||||||
|
{
|
||||||
|
// 步长为2
|
||||||
|
var src = new byte[] { 0x01, 0x02, 0x03, 0x04 };
|
||||||
|
var result = Number.ReverseBytes(src, 2);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0x02, 0x01, 0x04, 0x03 }, result.Value);
|
||||||
|
|
||||||
|
// 步长为4
|
||||||
|
var src2 = new byte[] { 0x01, 0x02, 0x03, 0x04 };
|
||||||
|
var result2 = Number.ReverseBytes(src2, 4);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0x04, 0x03, 0x02, 0x01 }, result2.Value);
|
||||||
|
|
||||||
|
// 步长为1(无变化)
|
||||||
|
var src3 = new byte[] { 0x01, 0x02, 0x03, 0x04 };
|
||||||
|
var result3 = Number.ReverseBytes(src3, 1);
|
||||||
|
Assert.True(result3.IsSuccessful);
|
||||||
|
Assert.Equal(src3, result3.Value);
|
||||||
|
|
||||||
|
// 步长为0(异常)
|
||||||
|
var result4 = Number.ReverseBytes(src3, 0);
|
||||||
|
Assert.False(result4.IsSuccessful);
|
||||||
|
|
||||||
|
// 步长不能整除
|
||||||
|
var result5 = Number.ReverseBytes(src3, 3);
|
||||||
|
Assert.False(result5.IsSuccessful);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 ReverseBits (byte)
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_ReverseBits_Byte()
|
||||||
|
{
|
||||||
|
// 0b00010010 -> 0b01001000
|
||||||
|
byte src = 0b00010010;
|
||||||
|
byte reversed = Number.ReverseBits(src);
|
||||||
|
Assert.Equal(0b01001000, reversed);
|
||||||
|
|
||||||
|
// 0b11110000 -> 0b00001111
|
||||||
|
Assert.Equal(0b00001111, Number.ReverseBits(0b11110000));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 ReverseBits (byte[])
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_ReverseBits_ByteArray()
|
||||||
|
{
|
||||||
|
var src = new byte[] { 0b00010010, 0b11110000 };
|
||||||
|
var reversed = Number.ReverseBits(src);
|
||||||
|
Assert.Equal(new byte[] { 0b01001000, 0b00001111 }, reversed);
|
||||||
|
|
||||||
|
// 空数组
|
||||||
|
var reversed2 = Number.ReverseBits(new byte[0]);
|
||||||
|
Assert.Empty(reversed2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 GetLength
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_GetLength()
|
||||||
|
{
|
||||||
|
Assert.Equal(5, Number.GetLength(12345));
|
||||||
|
Assert.Equal(4, Number.GetLength(-123));
|
||||||
|
Assert.Equal(1, Number.GetLength(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 IntPow
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_IntPow()
|
||||||
|
{
|
||||||
|
Assert.Equal(8, Number.IntPow(2, 3));
|
||||||
|
Assert.Equal(1, Number.IntPow(5, 0));
|
||||||
|
Assert.Equal(0, Number.IntPow(0, 5));
|
||||||
|
Assert.Equal(7, Number.IntPow(7, 1));
|
||||||
|
Assert.Equal(81, Number.IntPow(3, 4));
|
||||||
|
}
|
||||||
|
}
|
||||||
99
server.test/ProgressTrackerTest.cs
Normal file
99
server.test/ProgressTrackerTest.cs
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using Moq;
|
||||||
|
using server.Hubs;
|
||||||
|
using server.Services;
|
||||||
|
|
||||||
|
public class ProgressTrackerTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Test_ProgressReporter_Basic()
|
||||||
|
{
|
||||||
|
int reportedValue = -1;
|
||||||
|
var reporter = new ProgressReporter(async v => { reportedValue = v; await Task.CompletedTask; }, 0, 100, 10);
|
||||||
|
|
||||||
|
// Report
|
||||||
|
reporter.Report(50);
|
||||||
|
Assert.Equal(50, reporter.Progress);
|
||||||
|
Assert.Equal(ProgressStatus.InProgress, reporter.Status);
|
||||||
|
Assert.Equal(50, reportedValue);
|
||||||
|
|
||||||
|
// Increase by step
|
||||||
|
reporter.Increase();
|
||||||
|
Assert.Equal(60, reporter.Progress);
|
||||||
|
|
||||||
|
// Increase by value
|
||||||
|
reporter.Increase(20);
|
||||||
|
Assert.Equal(80, reporter.Progress);
|
||||||
|
|
||||||
|
// Finish
|
||||||
|
reporter.Finish();
|
||||||
|
Assert.Equal(ProgressStatus.Completed, reporter.Status);
|
||||||
|
Assert.Equal(100, reporter.Progress);
|
||||||
|
|
||||||
|
// Cancel
|
||||||
|
reporter = new ProgressReporter(async v => { reportedValue = v; await Task.CompletedTask; }, 0, 100, 10);
|
||||||
|
reporter.Cancel();
|
||||||
|
Assert.Equal(ProgressStatus.Canceled, reporter.Status);
|
||||||
|
Assert.Equal("User Cancelled", reporter.ErrorMessage);
|
||||||
|
|
||||||
|
// Error
|
||||||
|
reporter = new ProgressReporter(async v => { reportedValue = v; await Task.CompletedTask; }, 0, 100, 10);
|
||||||
|
reporter.Error("Test Error");
|
||||||
|
Assert.Equal(ProgressStatus.Failed, reporter.Status);
|
||||||
|
Assert.Equal("Test Error", reporter.ErrorMessage);
|
||||||
|
|
||||||
|
// CreateChild
|
||||||
|
var parent = new ProgressReporter(async v => { await Task.CompletedTask; }, 10, 100, 5);
|
||||||
|
var child = parent.CreateChild(50, 5);
|
||||||
|
Assert.Equal(ProgressStatus.Pending, child.Status);
|
||||||
|
Assert.NotNull(child);
|
||||||
|
|
||||||
|
// Child Increase
|
||||||
|
child.Increase();
|
||||||
|
Assert.Equal(ProgressStatus.InProgress, child.Status);
|
||||||
|
Assert.Equal(20, child.ProgressPercent);
|
||||||
|
Assert.Equal(20, parent.Progress);
|
||||||
|
|
||||||
|
// Child Complete
|
||||||
|
child.Finish();
|
||||||
|
Assert.Equal(ProgressStatus.Completed, child.Status);
|
||||||
|
Assert.Equal(100, child.ProgressPercent);
|
||||||
|
Assert.Equal(60, parent.Progress);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_ProgressTrackerService_Basic()
|
||||||
|
{
|
||||||
|
// Mock SignalR HubContext
|
||||||
|
var mockHubContext = new Mock<IHubContext<ProgressHub, IProgressReceiver>>();
|
||||||
|
var service = new ProgressTrackerService(mockHubContext.Object);
|
||||||
|
|
||||||
|
// CreateTask
|
||||||
|
var (taskId, reporter) = service.CreateTask();
|
||||||
|
Assert.NotNull(taskId);
|
||||||
|
Assert.NotNull(reporter);
|
||||||
|
|
||||||
|
// GetReporter
|
||||||
|
var optReporter = service.GetReporter(taskId);
|
||||||
|
Assert.True(optReporter.HasValue);
|
||||||
|
Assert.Equal(reporter, optReporter.Value);
|
||||||
|
|
||||||
|
// GetProgressStatus
|
||||||
|
var optStatus = service.GetProgressStatus(taskId);
|
||||||
|
Assert.True(optStatus.HasValue);
|
||||||
|
Assert.Equal(ProgressStatus.Pending, optStatus.Value);
|
||||||
|
|
||||||
|
// BindTask
|
||||||
|
var bindResult = service.BindTask(taskId, "conn1");
|
||||||
|
Assert.True(bindResult);
|
||||||
|
|
||||||
|
// CancelTask
|
||||||
|
var cancelResult = service.CancelTask(taskId);
|
||||||
|
Assert.True(cancelResult);
|
||||||
|
|
||||||
|
// After cancel, status should be Cancelled
|
||||||
|
var optStatus2 = service.GetProgressStatus(taskId);
|
||||||
|
Assert.True(optStatus2.HasValue);
|
||||||
|
Assert.Equal(ProgressStatus.Canceled, optStatus2.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,138 +0,0 @@
|
|||||||
using System.Net;
|
|
||||||
using System.Text;
|
|
||||||
using Common;
|
|
||||||
using Xunit.Abstractions;
|
|
||||||
|
|
||||||
namespace server.test;
|
|
||||||
|
|
||||||
|
|
||||||
public class UDPServerTest
|
|
||||||
{
|
|
||||||
const string address = "127.0.0.1";
|
|
||||||
const int port = 33000;
|
|
||||||
private static readonly UDPServer udpServer = new UDPServer(port);
|
|
||||||
|
|
||||||
private readonly ITestOutputHelper output;
|
|
||||||
|
|
||||||
public UDPServerTest(ITestOutputHelper output)
|
|
||||||
{
|
|
||||||
this.output = output;
|
|
||||||
udpServer.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void UDPDataDeepClone()
|
|
||||||
{
|
|
||||||
var udpData = new UDPData()
|
|
||||||
{
|
|
||||||
DateTime = DateTime.Now,
|
|
||||||
Address = "127.0.0.1",
|
|
||||||
Port = 1234,
|
|
||||||
Data = new byte[] { 0xf0, 00, 00, 00 },
|
|
||||||
HasRead = false
|
|
||||||
};
|
|
||||||
var cloneUdpData = udpData.DeepClone();
|
|
||||||
|
|
||||||
Assert.Equal(udpData.DateTime, cloneUdpData.DateTime);
|
|
||||||
Assert.Equal(udpData.Address, cloneUdpData.Address);
|
|
||||||
Assert.Equal(udpData.Port, cloneUdpData.Port);
|
|
||||||
Assert.Equal(udpData.Data, cloneUdpData.Data);
|
|
||||||
Assert.Equal(udpData.HasRead, cloneUdpData.HasRead);
|
|
||||||
|
|
||||||
udpData.DateTime = DateTime.Now;
|
|
||||||
udpData.Address = "192.168.1.1";
|
|
||||||
udpData.Port = 33000;
|
|
||||||
udpData.Data = new byte[] { 0xFF, 00, 00, 00 };
|
|
||||||
udpData.HasRead = true;
|
|
||||||
|
|
||||||
Assert.NotNull(cloneUdpData.DateTime);
|
|
||||||
Assert.NotNull(cloneUdpData.Address);
|
|
||||||
Assert.NotNull(cloneUdpData.Port);
|
|
||||||
Assert.NotNull(cloneUdpData.Data);
|
|
||||||
Assert.NotNull(cloneUdpData.HasRead);
|
|
||||||
|
|
||||||
Assert.NotEqual(udpData.DateTime, cloneUdpData.DateTime);
|
|
||||||
Assert.NotEqual(udpData.Address, cloneUdpData.Address);
|
|
||||||
Assert.NotEqual(udpData.Port, cloneUdpData.Port);
|
|
||||||
Assert.NotEqual(udpData.Data, cloneUdpData.Data);
|
|
||||||
Assert.NotEqual(udpData.HasRead, cloneUdpData.HasRead);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(new object[] { new string[] { "Hello World!", "Hello Server!", "What is your problem?" } })]
|
|
||||||
public async Task UDPServerFindString(string[] textArray)
|
|
||||||
{
|
|
||||||
Assert.True(udpServer.IsRunning);
|
|
||||||
|
|
||||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
|
||||||
|
|
||||||
foreach (var text in textArray)
|
|
||||||
{
|
|
||||||
Assert.True(await UDPClientPool.SendStringAsync(serverEP, [text]));
|
|
||||||
var ret = await udpServer.FindDataAsync(address);
|
|
||||||
Assert.True(ret.HasValue);
|
|
||||||
var data = ret.Value;
|
|
||||||
Assert.Equal(address, data.Address);
|
|
||||||
Assert.Equal(text, Encoding.ASCII.GetString(data.Data));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(new object[] { new UInt32[] { 0xF0_00_00_00, 0xFF_00_00_00, 0xFF_FF_FF_FF } })]
|
|
||||||
public async Task UDPServerFindBytes(UInt32[] bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(udpServer.IsRunning);
|
|
||||||
|
|
||||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
|
||||||
|
|
||||||
foreach (var number in bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(await UDPClientPool.SendBytesAsync(serverEP, Number.NumberToBytes(number, 4).Value));
|
|
||||||
var ret = await udpServer.FindDataAsync(address);
|
|
||||||
Assert.True(ret.HasValue);
|
|
||||||
var data = ret.Value;
|
|
||||||
Assert.Equal(address, data.Address);
|
|
||||||
Assert.Equal(number, Number.BytesToUInt32(data.Data).Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(new object[] { new UInt32[] { 0xF0_00_00_00, 0xF0_01_00_00 } })]
|
|
||||||
public async Task UDPServerWaitResp(UInt32[] bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(udpServer.IsRunning);
|
|
||||||
|
|
||||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
|
||||||
|
|
||||||
foreach (var number in bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(await UDPClientPool.SendBytesAsync(serverEP, Number.NumberToBytes(number, 4).Value));
|
|
||||||
|
|
||||||
var ret = await udpServer.WaitForAckAsync(address);
|
|
||||||
Assert.True(ret.IsSuccessful);
|
|
||||||
var data = ret.Value;
|
|
||||||
Assert.True(data.IsSuccessful);
|
|
||||||
Assert.Equal(number, Number.BytesToUInt32(data.ToBytes()).Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(new object[] { new UInt64[] { 0x0F_00_00_00_01_02_02_02, 0x0F_01_00_00_FF_FF_FF_FF } })]
|
|
||||||
public async Task UDPServerWaitData(UInt64[] bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(udpServer.IsRunning);
|
|
||||||
|
|
||||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
|
||||||
|
|
||||||
foreach (var number in bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(await UDPClientPool.SendBytesAsync(serverEP, Number.NumberToBytes(number, 8).Value));
|
|
||||||
|
|
||||||
var ret = await udpServer.WaitForDataAsync(address);
|
|
||||||
Assert.True(ret.IsSuccessful);
|
|
||||||
var data = ret.Value;
|
|
||||||
Assert.True(data.IsSuccessful);
|
|
||||||
Assert.Equal((UInt64)number, Number.BytesToUInt64(data.ToBytes()).Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.4" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.4" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||||
|
<PackageReference Include="Moq" Version="4.20.72" />
|
||||||
<PackageReference Include="xunit" Version="2.9.2" />
|
<PackageReference Include="xunit" Version="2.9.2" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -7,8 +7,11 @@ using Microsoft.IdentityModel.Tokens;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NLog.Web;
|
using NLog.Web;
|
||||||
|
using NSwag;
|
||||||
|
using NSwag.CodeGeneration.TypeScript;
|
||||||
using NSwag.Generation.Processors.Security;
|
using NSwag.Generation.Processors.Security;
|
||||||
using server.Services;
|
using server.Services;
|
||||||
|
using TypedSignalR.Client.DevTools;
|
||||||
|
|
||||||
// Early init of NLog to allow startup and exception logging, before host is built
|
// Early init of NLog to allow startup and exception logging, before host is built
|
||||||
var logger = NLog.LogManager.Setup()
|
var logger = NLog.LogManager.Setup()
|
||||||
@@ -59,7 +62,7 @@ try
|
|||||||
IssuerSigningKey = new SymmetricSecurityKey(
|
IssuerSigningKey = new SymmetricSecurityKey(
|
||||||
Encoding.UTF8.GetBytes("my secret key 1234567890my secret key 1234567890")),
|
Encoding.UTF8.GetBytes("my secret key 1234567890my secret key 1234567890")),
|
||||||
};
|
};
|
||||||
options.Authority = "http://localhost:5000";
|
options.Authority = $"http://{Global.localhost}:5000";
|
||||||
options.RequireHttpsMetadata = false;
|
options.RequireHttpsMetadata = false;
|
||||||
});
|
});
|
||||||
// Add JWT Token Authorization Policy
|
// Add JWT Token Authorization Policy
|
||||||
@@ -92,8 +95,17 @@ try
|
|||||||
.AllowAnyMethod()
|
.AllowAnyMethod()
|
||||||
.AllowAnyHeader()
|
.AllowAnyHeader()
|
||||||
);
|
);
|
||||||
|
options.AddPolicy("SignalR", policy => policy
|
||||||
|
.WithOrigins("http://localhost:5173")
|
||||||
|
.AllowAnyHeader()
|
||||||
|
.AllowAnyMethod()
|
||||||
|
.AllowCredentials()
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Use SignalR
|
||||||
|
builder.Services.AddSignalR();
|
||||||
|
|
||||||
// Add Swagger
|
// Add Swagger
|
||||||
builder.Services.AddSwaggerDocument(options =>
|
builder.Services.AddSwaggerDocument(options =>
|
||||||
{
|
{
|
||||||
@@ -133,6 +145,12 @@ try
|
|||||||
// 添加 HTTP 视频流服务
|
// 添加 HTTP 视频流服务
|
||||||
builder.Services.AddSingleton<HttpVideoStreamService>();
|
builder.Services.AddSingleton<HttpVideoStreamService>();
|
||||||
builder.Services.AddHostedService(provider => provider.GetRequiredService<HttpVideoStreamService>());
|
builder.Services.AddHostedService(provider => provider.GetRequiredService<HttpVideoStreamService>());
|
||||||
|
builder.Services.AddSingleton<HttpHdmiVideoStreamService>();
|
||||||
|
builder.Services.AddHostedService(provider => provider.GetRequiredService<HttpHdmiVideoStreamService>());
|
||||||
|
|
||||||
|
// 添加进度跟踪服务
|
||||||
|
builder.Services.AddSingleton<ProgressTrackerService>();
|
||||||
|
builder.Services.AddHostedService(provider => provider.GetRequiredService<ProgressTrackerService>());
|
||||||
|
|
||||||
// Application Settings
|
// Application Settings
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
@@ -165,6 +183,17 @@ try
|
|||||||
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "log")),
|
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "log")),
|
||||||
RequestPath = "/log"
|
RequestPath = "/log"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Exam Files (实验静态资源)
|
||||||
|
if (Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "exam")))
|
||||||
|
{
|
||||||
|
app.UseStaticFiles(new StaticFileOptions
|
||||||
|
{
|
||||||
|
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "exam")),
|
||||||
|
RequestPath = "/exam"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
app.MapFallbackToFile("index.html");
|
app.MapFallbackToFile("index.html");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,15 +204,57 @@ try
|
|||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
// Swagger
|
// Swagger
|
||||||
app.UseOpenApi();
|
app.UseOpenApi(settings =>
|
||||||
|
{
|
||||||
|
settings.PostProcess = (document, httpRequest) =>
|
||||||
|
{
|
||||||
|
document.Servers.Clear();
|
||||||
|
document.Servers.Add(new NSwag.OpenApiServer { Url = $"http://{Global.localhost}:5000" });
|
||||||
|
};
|
||||||
|
});
|
||||||
app.UseSwaggerUi();
|
app.UseSwaggerUi();
|
||||||
|
|
||||||
|
// SignalR
|
||||||
|
app.UseWebSockets();
|
||||||
|
app.UseSignalRHubSpecification();
|
||||||
|
app.UseSignalRHubDevelopmentUI();
|
||||||
|
|
||||||
// Router
|
// Router
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
app.MapHub<server.Hubs.JtagHub>("hubs/JtagHub");
|
||||||
|
app.MapHub<server.Hubs.ProgressHub>("hubs/ProgressHub");
|
||||||
|
|
||||||
// Setup Program
|
// Setup Program
|
||||||
MsgBus.Init();
|
MsgBus.Init();
|
||||||
|
|
||||||
|
// Generate API Client
|
||||||
|
app.MapGet("GetAPIClientCode", async (HttpContext context) =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var document = await OpenApiDocument.FromUrlAsync($"http://{Global.localhost}:5000/swagger/v1/swagger.json");
|
||||||
|
|
||||||
|
var settings = new TypeScriptClientGeneratorSettings
|
||||||
|
{
|
||||||
|
ClassName = "{controller}Client",
|
||||||
|
UseAbortSignal = false,
|
||||||
|
Template = TypeScriptTemplate.Axios,
|
||||||
|
TypeScriptGeneratorSettings = {
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
var generator = new TypeScriptClientGenerator(document, settings);
|
||||||
|
var code = generator.GenerateFile();
|
||||||
|
|
||||||
|
return Results.Text(code, "text/plain; charset=utf-8", Encoding.UTF8);
|
||||||
|
}
|
||||||
|
catch (Exception err)
|
||||||
|
{
|
||||||
|
logger.Error(err);
|
||||||
|
return Results.Problem(err.ToString());
|
||||||
|
}
|
||||||
|
}).RequireCors("Development");
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
@@ -203,4 +274,3 @@ finally
|
|||||||
// Close Program
|
// Close Program
|
||||||
MsgBus.Exit();
|
MsgBus.Exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"applicationUrl": "http://localhost:5000",
|
"applicationUrl": "http://0.0.0.0:5000",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development",
|
"ASPNETCORE_ENVIRONMENT": "Development",
|
||||||
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
|
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"applicationUrl": "https://localhost:7278;http://localhost:5000",
|
"applicationUrl": "https://0.0.0.0:7278;http://0.0.0.0:5000",
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development",
|
"ASPNETCORE_ENVIRONMENT": "Development",
|
||||||
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
|
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
|
||||||
|
|||||||
@@ -14,8 +14,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DotNext" Version="5.19.1" />
|
<PackageReference Include="ArpLookup" Version="2.0.3" />
|
||||||
<PackageReference Include="DotNext.Threading" Version="5.19.1" />
|
<PackageReference Include="DotNext" Version="5.23.0" />
|
||||||
|
<PackageReference Include="DotNext.Threading" Version="5.23.0" />
|
||||||
<PackageReference Include="Honoo.IO.Hashing.Crc" Version="1.3.3" />
|
<PackageReference Include="Honoo.IO.Hashing.Crc" Version="1.3.3" />
|
||||||
<PackageReference Include="linq2db.AspNet" Version="5.4.1" />
|
<PackageReference Include="linq2db.AspNet" Version="5.4.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.7" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.7" />
|
||||||
@@ -26,8 +27,21 @@
|
|||||||
<PackageReference Include="NLog" Version="5.4.0" />
|
<PackageReference Include="NLog" Version="5.4.0" />
|
||||||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.4.0" />
|
<PackageReference Include="NLog.Web.AspNetCore" Version="5.4.0" />
|
||||||
<PackageReference Include="NSwag.AspNetCore" Version="14.3.0" />
|
<PackageReference Include="NSwag.AspNetCore" Version="14.3.0" />
|
||||||
|
<PackageReference Include="NSwag.CodeGeneration.TypeScript" Version="14.4.0" />
|
||||||
|
<PackageReference Include="OpenCvSharp4" Version="4.11.0.20250507" />
|
||||||
|
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.11.0.20250507" />
|
||||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.10" />
|
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.10" />
|
||||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
|
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
|
||||||
|
<PackageReference Include="Tapper.Analyzer" Version="1.13.1">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="TypedSignalR.Client.DevTools" Version="1.2.4" />
|
||||||
|
<PackageReference Include="TypedSignalR.Client.TypeScript.Analyzer" Version="1.15.0">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="TypedSignalR.Client.TypeScript.Attributes" Version="1.15.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
634
server/src/ArpClient.cs
Normal file
634
server/src/ArpClient.cs
Normal file
@@ -0,0 +1,634 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
using ArpLookup;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ARP 记录管理静态类(跨平台支持)
|
||||||
|
/// </summary>
|
||||||
|
public static class ArpClient
|
||||||
|
{
|
||||||
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 读取所有 ARP 记录
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ARP 记录列表</returns>
|
||||||
|
public static async Task<List<ArpEntry>> GetArpTableAsync()
|
||||||
|
{
|
||||||
|
var entries = new List<ArpEntry>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string command = GetArpListCommand();
|
||||||
|
var result = await ExecuteCommandAsync(command);
|
||||||
|
if (result.IsSuccess)
|
||||||
|
{
|
||||||
|
var lines = result.Output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
foreach (var line in lines)
|
||||||
|
{
|
||||||
|
var entry = ParseArpEntry(line);
|
||||||
|
if (entry != null)
|
||||||
|
{
|
||||||
|
entries.Add(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new Exception($"读取 ARP 表失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 动态更新指定 IP 的 ARP 记录
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ipAddress">要更新的 IP 地址</param>
|
||||||
|
/// <returns>是否成功发送 Ping</returns>
|
||||||
|
public static async Task<bool> UpdateArpEntryAsync(string ipAddress)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(ipAddress))
|
||||||
|
throw new ArgumentException("IP 地址不能为空", nameof(ipAddress));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var ret = await ArpClient.DeleteArpEntryAsync(ipAddress);
|
||||||
|
if (!ret)
|
||||||
|
{
|
||||||
|
logger.Error($"删除 ARP 记录失败: {ipAddress}");
|
||||||
|
}
|
||||||
|
|
||||||
|
PhysicalAddress? mac = await Arp.LookupAsync(IPAddress.Parse(ipAddress));
|
||||||
|
if (mac == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 添加 ARP 记录
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ipAddress">IP 地址</param>
|
||||||
|
/// <param name="macAddress">MAC 地址</param>
|
||||||
|
/// <param name="interfaceName">网络接口名称(可选)</param>
|
||||||
|
/// <returns>是否成功</returns>
|
||||||
|
public static async Task<bool> AddArpEntryAsync(string ipAddress, string macAddress, string? interfaceName = null)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(ipAddress))
|
||||||
|
throw new ArgumentException("IP 地址不能为空", nameof(ipAddress));
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(macAddress))
|
||||||
|
throw new ArgumentException("MAC 地址不能为空", nameof(macAddress));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 格式化 MAC 地址以适配不同操作系统
|
||||||
|
string formattedMac = FormatMacAddress(macAddress);
|
||||||
|
string command = await GetArpAddCommandAsync(ipAddress, formattedMac, interfaceName);
|
||||||
|
var result = await ExecuteCommandAsync(command);
|
||||||
|
return result.IsSuccess;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new Exception($"添加 ARP 记录失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除 ARP 记录
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ipAddress">要删除的 IP 地址</param>
|
||||||
|
/// <param name="interfaceName">网络接口名称(可选)</param>
|
||||||
|
/// <returns>是否成功</returns>
|
||||||
|
public static async Task<bool> DeleteArpEntryAsync(string ipAddress, string? interfaceName = null)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(ipAddress))
|
||||||
|
throw new ArgumentException("IP 地址不能为空", nameof(ipAddress));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string command = GetArpDeleteCommand(ipAddress, interfaceName);
|
||||||
|
var result = await ExecuteCommandAsync(command);
|
||||||
|
return result.IsSuccess;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new Exception($"删除 ARP 记录失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 清空所有 ARP 记录
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>是否成功</returns>
|
||||||
|
public static async Task<bool> ClearArpTableAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string command = GetArpClearCommand();
|
||||||
|
var result = await ExecuteCommandAsync(command);
|
||||||
|
return result.IsSuccess;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new Exception($"清空 ARP 表失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查询特定 IP 的 ARP 记录
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ipAddress">IP 地址</param>
|
||||||
|
/// <returns>ARP 记录,如果不存在则返回 null</returns>
|
||||||
|
public static async Task<ArpEntry?> GetArpEntryAsync(string ipAddress)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(ipAddress))
|
||||||
|
throw new ArgumentException("IP 地址不能为空", nameof(ipAddress));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string command = GetArpQueryCommand(ipAddress);
|
||||||
|
var result = await ExecuteCommandAsync(command);
|
||||||
|
if (result.IsSuccess)
|
||||||
|
{
|
||||||
|
var lines = result.Output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
foreach (var line in lines)
|
||||||
|
{
|
||||||
|
var entry = ParseArpEntry(line);
|
||||||
|
if (entry != null && entry.IpAddress == ipAddress)
|
||||||
|
{
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new Exception($"查询 ARP 记录失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取 ARP 列表命令
|
||||||
|
/// </summary>
|
||||||
|
private static string GetArpListCommand()
|
||||||
|
{
|
||||||
|
return "arp -a";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取 ARP 添加命令
|
||||||
|
/// </summary>
|
||||||
|
private static async Task<string> GetArpAddCommandAsync(string ipAddress, string macAddress, string? interfaceName)
|
||||||
|
{
|
||||||
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(interfaceName))
|
||||||
|
{
|
||||||
|
// 通过 arp -a 获取接口索引
|
||||||
|
var interfaceIdx = await GetWindowsInterfaceIndexAsync(interfaceName);
|
||||||
|
if (interfaceIdx.HasValue)
|
||||||
|
{
|
||||||
|
return $"netsh -c i i add neighbors {interfaceIdx.Value} {ipAddress} {macAddress}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $"arp -s {ipAddress} {macAddress}";
|
||||||
|
}
|
||||||
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||||
|
{
|
||||||
|
return string.IsNullOrWhiteSpace(interfaceName)
|
||||||
|
? $"arp -s {ipAddress} {macAddress}"
|
||||||
|
: $"arp -s {ipAddress} {macAddress} -i {interfaceName}";
|
||||||
|
}
|
||||||
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||||
|
{
|
||||||
|
return string.IsNullOrWhiteSpace(interfaceName)
|
||||||
|
? $"arp -s {ipAddress} {macAddress}"
|
||||||
|
: $"arp -s {ipAddress} {macAddress} ifscope {interfaceName}";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new PlatformNotSupportedException("不支持的操作系统平台");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取 Windows 接口索引
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="interfaceIp">接口IP地址</param>
|
||||||
|
/// <returns>接口索引(十进制),如果未找到则返回null</returns>
|
||||||
|
private static async Task<int?> GetWindowsInterfaceIndexAsync(string interfaceIp)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await ExecuteCommandAsync("arp -a");
|
||||||
|
if (!result.IsSuccess)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var lines = result.Output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
foreach (var line in lines)
|
||||||
|
{
|
||||||
|
// 匹配接口行格式: Interface: 172.6.1.5 --- 0xa
|
||||||
|
var interfacePattern = @"Interface:\s+(\d+\.\d+\.\d+\.\d+)\s+---\s+(0x[a-fA-F0-9]+)";
|
||||||
|
var match = Regex.Match(line, interfacePattern);
|
||||||
|
|
||||||
|
if (match.Success && match.Groups[1].Value == interfaceIp)
|
||||||
|
{
|
||||||
|
// 将十六进制索引转换为十进制
|
||||||
|
var hexIndex = match.Groups[2].Value;
|
||||||
|
// 去掉 "0x" 前缀
|
||||||
|
var hexValue = hexIndex.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
||||||
|
? hexIndex.Substring(2)
|
||||||
|
: hexIndex;
|
||||||
|
|
||||||
|
if (int.TryParse(hexValue, System.Globalization.NumberStyles.HexNumber, null, out int decimalIndex))
|
||||||
|
{
|
||||||
|
logger.Debug($"找到接口 {interfaceIp} 的索引: {hexIndex} -> {decimalIndex}");
|
||||||
|
return decimalIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Warn($"未找到接口 {interfaceIp} 的索引");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, $"获取接口 {interfaceIp} 索引失败");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取 ARP 删除命令
|
||||||
|
/// </summary>
|
||||||
|
private static string GetArpDeleteCommand(string ipAddress, string? interfaceName)
|
||||||
|
{
|
||||||
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
|
{
|
||||||
|
return $"arp -d {ipAddress}";
|
||||||
|
}
|
||||||
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||||
|
{
|
||||||
|
return string.IsNullOrWhiteSpace(interfaceName)
|
||||||
|
? $"arp -d {ipAddress}"
|
||||||
|
: $"arp -d {ipAddress} -i {interfaceName}";
|
||||||
|
}
|
||||||
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||||
|
{
|
||||||
|
return string.IsNullOrWhiteSpace(interfaceName)
|
||||||
|
? $"arp -d {ipAddress}"
|
||||||
|
: $"arp -d {ipAddress} ifscope {interfaceName}";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new PlatformNotSupportedException("不支持的操作系统平台");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取 ARP 清空命令
|
||||||
|
/// </summary>
|
||||||
|
private static string GetArpClearCommand()
|
||||||
|
{
|
||||||
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
|
{
|
||||||
|
return "arp -d *";
|
||||||
|
}
|
||||||
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||||
|
{
|
||||||
|
return "ip neigh flush all";
|
||||||
|
}
|
||||||
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||||
|
{
|
||||||
|
return "arp -d -a";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new PlatformNotSupportedException("不支持的操作系统平台");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取 ARP 查询命令
|
||||||
|
/// </summary>
|
||||||
|
private static string GetArpQueryCommand(string ipAddress)
|
||||||
|
{
|
||||||
|
return $"arp -a {ipAddress}";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行系统命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">命令</param>
|
||||||
|
/// <returns>命令执行结果</returns>
|
||||||
|
private static async Task<CommandResult> ExecuteCommandAsync(string command)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ProcessStartInfo processInfo;
|
||||||
|
|
||||||
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
|
{
|
||||||
|
processInfo = new ProcessStartInfo
|
||||||
|
{
|
||||||
|
FileName = "cmd.exe",
|
||||||
|
Arguments = $"/c {command}",
|
||||||
|
RedirectStandardOutput = true,
|
||||||
|
RedirectStandardError = true,
|
||||||
|
UseShellExecute = false,
|
||||||
|
CreateNoWindow = true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
processInfo = new ProcessStartInfo
|
||||||
|
{
|
||||||
|
FileName = "/bin/bash",
|
||||||
|
Arguments = $"-c \"{command}\"",
|
||||||
|
RedirectStandardOutput = true,
|
||||||
|
RedirectStandardError = true,
|
||||||
|
UseShellExecute = false,
|
||||||
|
CreateNoWindow = true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Debug($"Executing command: {processInfo.FileName} {processInfo.Arguments}");
|
||||||
|
|
||||||
|
using var process = new Process { StartInfo = processInfo };
|
||||||
|
process.Start();
|
||||||
|
|
||||||
|
var output = await process.StandardOutput.ReadToEndAsync();
|
||||||
|
var error = await process.StandardError.ReadToEndAsync();
|
||||||
|
|
||||||
|
await process.WaitForExitAsync();
|
||||||
|
|
||||||
|
logger.Debug($"Command output: {output}");
|
||||||
|
if (!string.IsNullOrWhiteSpace(error))
|
||||||
|
logger.Debug($"Command error: {error}");
|
||||||
|
logger.Debug($"Command exit code: {process.ExitCode}");
|
||||||
|
|
||||||
|
return new CommandResult
|
||||||
|
{
|
||||||
|
IsSuccess = process.ExitCode == 0,
|
||||||
|
Output = output,
|
||||||
|
Error = error,
|
||||||
|
ExitCode = process.ExitCode
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, $"Command execution failed: {command}");
|
||||||
|
return new CommandResult
|
||||||
|
{
|
||||||
|
IsSuccess = false,
|
||||||
|
Error = ex.Message,
|
||||||
|
ExitCode = -1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 解析 ARP 记录行
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="line">ARP 记录行</param>
|
||||||
|
/// <returns>解析后的 ARP 记录</returns>
|
||||||
|
private static ArpEntry? ParseArpEntry(string line)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(line))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
|
{
|
||||||
|
return ParseWindowsArpEntry(line);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return ParseUnixArpEntry(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 解析 Windows ARP 记录
|
||||||
|
/// </summary>
|
||||||
|
private static ArpEntry? ParseWindowsArpEntry(string line)
|
||||||
|
{
|
||||||
|
// 跳过空行和标题行
|
||||||
|
if (string.IsNullOrWhiteSpace(line) ||
|
||||||
|
line.Contains("Interface:") ||
|
||||||
|
line.Contains("Internet Address") ||
|
||||||
|
line.Contains("Physical Address") ||
|
||||||
|
line.Contains("Type"))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Windows arp -a 输出格式: IP地址 物理地址 类型
|
||||||
|
// 示例: 172.6.0.1 e4-3a-6e-29-c3-5b dynamic
|
||||||
|
var pattern = @"^\s*(\d+\.\d+\.\d+\.\d+)\s+([a-fA-F0-9-]{17})\s+(\w+)\s*$";
|
||||||
|
var match = Regex.Match(line, pattern);
|
||||||
|
|
||||||
|
if (match.Success)
|
||||||
|
{
|
||||||
|
return new ArpEntry
|
||||||
|
{
|
||||||
|
IpAddress = match.Groups[1].Value,
|
||||||
|
MacAddress = FormatMacAddress(match.Groups[2].Value), // 格式化 MAC 地址
|
||||||
|
Type = match.Groups[3].Value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 解析 Unix/Linux ARP 记录
|
||||||
|
/// </summary>
|
||||||
|
private static ArpEntry? ParseUnixArpEntry(string line)
|
||||||
|
{
|
||||||
|
// Unix/Linux arp -a 输出格式: hostname (ip) at mac [ether] PERM on interface
|
||||||
|
var pattern = @"(\S+)\s+\((\d+\.\d+\.\d+\.\d+)\)\s+at\s+([a-fA-F0-9:]{17})\s+\[(\w+)\]\s+(\w+)\s+on\s+(\S+)";
|
||||||
|
var match = Regex.Match(line, pattern);
|
||||||
|
|
||||||
|
if (match.Success)
|
||||||
|
{
|
||||||
|
return new ArpEntry
|
||||||
|
{
|
||||||
|
Hostname = match.Groups[1].Value,
|
||||||
|
IpAddress = match.Groups[2].Value,
|
||||||
|
MacAddress = FormatMacAddress(match.Groups[3].Value), // 格式化 MAC 地址
|
||||||
|
Type = match.Groups[5].Value,
|
||||||
|
Interface = match.Groups[6].Value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 匹配简单格式: ip mac interface
|
||||||
|
var simplePattern = @"(\d+\.\d+\.\d+\.\d+)\s+([a-fA-F0-9:]{17})\s+(\S+)";
|
||||||
|
var simpleMatch = Regex.Match(line, simplePattern);
|
||||||
|
|
||||||
|
if (simpleMatch.Success)
|
||||||
|
{
|
||||||
|
return new ArpEntry
|
||||||
|
{
|
||||||
|
IpAddress = simpleMatch.Groups[1].Value,
|
||||||
|
MacAddress = FormatMacAddress(simpleMatch.Groups[2].Value), // 格式化 MAC 地址
|
||||||
|
Interface = simpleMatch.Groups[3].Value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断当前进程是否具有管理员(或 root)权限
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>如果有管理员权限返回 true,否则返回 false</returns>
|
||||||
|
public static bool IsAdministrator()
|
||||||
|
{
|
||||||
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
|
{
|
||||||
|
// Windows: 检查当前用户是否为管理员
|
||||||
|
using var identity = System.Security.Principal.WindowsIdentity.GetCurrent();
|
||||||
|
var principal = new System.Security.Principal.WindowsPrincipal(identity);
|
||||||
|
return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Unix/Linux/macOS: 检查是否为 root 用户
|
||||||
|
return Environment.UserName == "root" || (Environment.GetEnvironmentVariable("USER") == "root");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查指定 IP 是否存在对应的 MAC,如果不存在则删除原有 ARP 记录并新增
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ipAddress">IP 地址</param>
|
||||||
|
/// <param name="macAddress">MAC 地址</param>
|
||||||
|
/// <param name="interfaceName">网络接口名称(可选)</param>
|
||||||
|
/// <returns>是否成功</returns>
|
||||||
|
public static async Task<bool> CheckOrAddAsync(string ipAddress, string macAddress, string? interfaceName = null)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(ipAddress))
|
||||||
|
throw new ArgumentException("IP 地址不能为空", nameof(ipAddress));
|
||||||
|
if (string.IsNullOrWhiteSpace(macAddress))
|
||||||
|
throw new ArgumentException("MAC 地址不能为空", nameof(macAddress));
|
||||||
|
|
||||||
|
// 格式化 MAC 地址以适配不同操作系统
|
||||||
|
string formattedMac = FormatMacAddress(macAddress);
|
||||||
|
|
||||||
|
var entry = await GetArpEntryAsync(ipAddress);
|
||||||
|
if (entry != null && string.Equals(FormatMacAddress(entry.MacAddress), formattedMac, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
// 已存在且 MAC 匹配,无需操作
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 若存在但 MAC 不匹配,先删除
|
||||||
|
if (entry != null)
|
||||||
|
{
|
||||||
|
await DeleteArpEntryAsync(ipAddress, interfaceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 新增 ARP 记录
|
||||||
|
var ret = await AddArpEntryAsync(ipAddress, formattedMac, interfaceName);
|
||||||
|
if (!ret) logger.Error($"添加 ARP 记录失败: {ipAddress} -> {formattedMac} on {interfaceName}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 格式化 MAC 地址为指定平台格式
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="macAddress">原始 MAC 地址</param>
|
||||||
|
/// <returns>格式化后的 MAC 地址</returns>
|
||||||
|
public static string FormatMacAddress(string macAddress)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(macAddress))
|
||||||
|
return string.Empty;
|
||||||
|
|
||||||
|
var cleaned = macAddress.Replace("-", "").Replace(":", "").ToLowerInvariant();
|
||||||
|
if (cleaned.Length != 12)
|
||||||
|
return macAddress;
|
||||||
|
|
||||||
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
|
{
|
||||||
|
// Windows: XX-XX-XX-XX-XX-XX
|
||||||
|
return string.Join("-", Enumerable.Range(0, 6).Select(i => cleaned.Substring(i * 2, 2)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Unix/Linux/macOS: xx:xx:xx:xx:xx:xx
|
||||||
|
return string.Join(":", Enumerable.Range(0, 6).Select(i => cleaned.Substring(i * 2, 2)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// ARP 记录条目
|
||||||
|
/// </summary>
|
||||||
|
public class ArpEntry
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
public string Hostname { get; set; } = string.Empty;
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
public string IpAddress { get; set; } = string.Empty;
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
public string MacAddress { get; set; } = string.Empty;
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
public string Type { get; set; } = string.Empty;
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
public string Interface { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"{IpAddress} -> {MacAddress} ({Interface})";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 命令执行结果
|
||||||
|
/// </summary>
|
||||||
|
public class CommandResult
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSuccess { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
public string Output { get; set; } = string.Empty;
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
public string Error { get; set; } = string.Empty;
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
public int ExitCode { get; set; }
|
||||||
|
}
|
||||||
21
server/src/Common/Global.cs
Normal file
21
server/src/Common/Global.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
|
public static class Global
|
||||||
|
{
|
||||||
|
|
||||||
|
public static readonly string localhost = "127.0.0.1";
|
||||||
|
|
||||||
|
public static string GetLocalIPAddress()
|
||||||
|
{
|
||||||
|
var host = Dns.GetHostEntry(Dns.GetHostName());
|
||||||
|
foreach (var ip in host.AddressList)
|
||||||
|
{
|
||||||
|
if (ip.AddressFamily == AddressFamily.InterNetwork)
|
||||||
|
{
|
||||||
|
return ip.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Exception("No network adapters with an IPv4 address in the system!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -65,7 +65,7 @@ public class Number
|
|||||||
{
|
{
|
||||||
for (var i = 0; i < length; i++)
|
for (var i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
arr[length - 1 - i] = Convert.ToByte((num >> (i << 3)) & (0xFF));
|
arr[i] = Convert.ToByte((num >> (i << 3)) & (0xFF));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -99,20 +99,11 @@ public class Number
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (isLowNumHigh)
|
if (!isLowNumHigh)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < len; i++)
|
Array.Reverse(bytes);
|
||||||
{
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
num = BitConverter.ToUInt64(bytes, 0);
|
||||||
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
@@ -143,20 +134,11 @@ public class Number
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (isLowNumHigh)
|
if (!isLowNumHigh)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < len; i++)
|
Array.Reverse(bytes);
|
||||||
{
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
num = BitConverter.ToUInt32(bytes, 0);
|
||||||
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
@@ -333,10 +315,9 @@ public class Number
|
|||||||
|
|
||||||
for (int i = 0; i < srcBytesLen; i += distance)
|
for (int i = 0; i < srcBytesLen; i += distance)
|
||||||
{
|
{
|
||||||
var end = i + distance;
|
Buffer.BlockCopy(srcBytes, i, buffer, 0, distance);
|
||||||
buffer = srcBytes[i..end];
|
|
||||||
Array.Reverse(buffer);
|
Array.Reverse(buffer);
|
||||||
Array.Copy(buffer, 0, dstBytes, i, distance);
|
Buffer.BlockCopy(buffer, 0, dstBytes, i, distance);
|
||||||
}
|
}
|
||||||
|
|
||||||
return dstBytes;
|
return dstBytes;
|
||||||
@@ -367,4 +348,37 @@ public class Number
|
|||||||
}
|
}
|
||||||
return dstBytes;
|
return dstBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取数字的长度
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="number">数字</param>
|
||||||
|
/// <returns>数字的长度</returns>
|
||||||
|
public static int GetLength(int number)
|
||||||
|
{
|
||||||
|
// 将整数转换为字符串
|
||||||
|
string numberString = number.ToString();
|
||||||
|
|
||||||
|
// 返回字符串的长度
|
||||||
|
return numberString.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 计算整形的幂
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">底数</param>
|
||||||
|
/// <param name="pow">幂</param>
|
||||||
|
/// <returns>计算结果</returns>
|
||||||
|
public static int IntPow(int x, int pow)
|
||||||
|
{
|
||||||
|
int ret = 1;
|
||||||
|
while (pow != 0)
|
||||||
|
{
|
||||||
|
if ((pow & 1) == 1)
|
||||||
|
ret *= x;
|
||||||
|
x *= x;
|
||||||
|
pow >>= 1;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.IdentityModel.Tokens.Jwt;
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Net;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
@@ -16,6 +17,8 @@ namespace server.Controllers;
|
|||||||
public class DataController : ControllerBase
|
public class DataController : ControllerBase
|
||||||
{
|
{
|
||||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
// 固定的实验板IP,端口,MAC地址
|
||||||
|
private const string BOARD_IP = "169.254.109.0";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// [TODO:description]
|
/// [TODO:description]
|
||||||
@@ -48,6 +51,53 @@ public class DataController : ControllerBase
|
|||||||
public DateTime? BoardExpireTime { get; set; }
|
public DateTime? BoardExpireTime { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取本机IP地址(优先选择与实验板同网段的IP)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>本机IP地址</returns>
|
||||||
|
private IPAddress GetLocalIPAddress()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var boardIpSegments = BOARD_IP.Split('.').Take(3).ToArray();
|
||||||
|
|
||||||
|
// 优先选择与实验板IP前三段相同的IP
|
||||||
|
var sameSegmentIP = System.Net.NetworkInformation.NetworkInterface
|
||||||
|
.GetAllNetworkInterfaces()
|
||||||
|
.Where(nic => nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up
|
||||||
|
&& nic.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Loopback)
|
||||||
|
.SelectMany(nic => nic.GetIPProperties().UnicastAddresses)
|
||||||
|
.Where(addr => addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
||||||
|
.Select(addr => addr.Address)
|
||||||
|
.FirstOrDefault(addr =>
|
||||||
|
{
|
||||||
|
var segments = addr.ToString().Split('.');
|
||||||
|
return segments.Length == 4 &&
|
||||||
|
segments[0] == boardIpSegments[0] &&
|
||||||
|
segments[1] == boardIpSegments[1] &&
|
||||||
|
segments[2] == boardIpSegments[2];
|
||||||
|
});
|
||||||
|
|
||||||
|
if (sameSegmentIP != null)
|
||||||
|
return sameSegmentIP;
|
||||||
|
|
||||||
|
// 如果没有找到同网段的IP,返回第一个可用的IP
|
||||||
|
return System.Net.NetworkInformation.NetworkInterface
|
||||||
|
.GetAllNetworkInterfaces()
|
||||||
|
.Where(nic => nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up
|
||||||
|
&& nic.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Loopback)
|
||||||
|
.SelectMany(nic => nic.GetIPProperties().UnicastAddresses)
|
||||||
|
.Where(addr => addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
||||||
|
.Select(addr => addr.Address)
|
||||||
|
.FirstOrDefault() ?? IPAddress.Loopback;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取本机IP地址失败");
|
||||||
|
return IPAddress.Loopback;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户登录,获取 JWT 令牌
|
/// 用户登录,获取 JWT 令牌
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -207,7 +257,7 @@ public class DataController : ControllerBase
|
|||||||
[ProducesResponseType(typeof(Database.Board), StatusCodes.Status200OK)]
|
[ProducesResponseType(typeof(Database.Board), StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
public IActionResult GetAvailableBoard(int durationHours = 1)
|
public async ValueTask<IActionResult> GetAvailableBoard(int durationHours = 1)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -227,7 +277,13 @@ public class DataController : ControllerBase
|
|||||||
if (!boardOpt.HasValue)
|
if (!boardOpt.HasValue)
|
||||||
return NotFound("没有可用的实验板");
|
return NotFound("没有可用的实验板");
|
||||||
|
|
||||||
return Ok(boardOpt.Value);
|
var boardInfo = boardOpt.Value;
|
||||||
|
if (!(await ArpClient.CheckOrAddAsync(boardInfo.IpAddr, boardInfo.MacAddr, GetLocalIPAddress().ToString())))
|
||||||
|
{
|
||||||
|
logger.Error($"无法配置ARP,实验板可能会无法连接");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(boardInfo);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -278,7 +334,7 @@ public class DataController : ControllerBase
|
|||||||
[ProducesResponseType(typeof(Database.Board), StatusCodes.Status200OK)]
|
[ProducesResponseType(typeof(Database.Board), StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
public IActionResult GetBoardByID(Guid id)
|
public async Task<IActionResult> GetBoardByID(Guid id)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -288,7 +344,14 @@ public class DataController : ControllerBase
|
|||||||
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
|
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
|
||||||
if (!ret.Value.HasValue)
|
if (!ret.Value.HasValue)
|
||||||
return NotFound("未找到对应的实验板");
|
return NotFound("未找到对应的实验板");
|
||||||
return Ok(ret.Value.Value);
|
|
||||||
|
var boardInfo = ret.Value.Value;
|
||||||
|
if (!(await ArpClient.CheckOrAddAsync(boardInfo.IpAddr, boardInfo.MacAddr, GetLocalIPAddress().ToString())))
|
||||||
|
{
|
||||||
|
logger.Error($"无法配置ARP,实验板可能会无法连接");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(boardInfo);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -303,21 +366,17 @@ public class DataController : ControllerBase
|
|||||||
[Authorize("Admin")]
|
[Authorize("Admin")]
|
||||||
[HttpPost("AddBoard")]
|
[HttpPost("AddBoard")]
|
||||||
[EnableCors("Users")]
|
[EnableCors("Users")]
|
||||||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
[ProducesResponseType(typeof(Guid), StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
public IActionResult AddBoard(string name, string ipAddr, int port)
|
public IActionResult AddBoard(string name)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(name))
|
if (string.IsNullOrWhiteSpace(name))
|
||||||
return BadRequest("板子名称不能为空");
|
return BadRequest("板子名称不能为空");
|
||||||
if (string.IsNullOrWhiteSpace(ipAddr))
|
|
||||||
return BadRequest("IP地址不能为空");
|
|
||||||
if (port <= 0 || port > 65535)
|
|
||||||
return BadRequest("端口号不合法");
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var db = new Database.AppDataConnection();
|
using var db = new Database.AppDataConnection();
|
||||||
var ret = db.AddBoard(name, ipAddr, port);
|
var ret = db.AddBoard(name);
|
||||||
return Ok(ret);
|
return Ok(ret);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -376,5 +435,58 @@ public class DataController : ControllerBase
|
|||||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新板卡名称(管理员权限)
|
||||||
|
/// </summary>
|
||||||
|
[Authorize("Admin")]
|
||||||
|
[HttpPost("UpdateBoardName")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public IActionResult UpdateBoardName(Guid boardId, string newName)
|
||||||
|
{
|
||||||
|
if (boardId == Guid.Empty)
|
||||||
|
return BadRequest("板子Guid不能为空");
|
||||||
|
if (string.IsNullOrWhiteSpace(newName))
|
||||||
|
return BadRequest("新名称不能为空");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
var result = db.UpdateBoardName(boardId, newName);
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "更新板卡名称时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "更新失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新板卡状态(管理员权限)
|
||||||
|
/// </summary>
|
||||||
|
[Authorize("Admin")]
|
||||||
|
[HttpPost("UpdateBoardStatus")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public IActionResult UpdateBoardStatus(Guid boardId, Database.Board.BoardStatus newStatus)
|
||||||
|
{
|
||||||
|
if (boardId == Guid.Empty)
|
||||||
|
return BadRequest("板子Guid不能为空");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
var result = db.UpdateBoardStatus(boardId, newStatus);
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "更新板卡状态时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "更新失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
467
server/src/Controllers/DebuggerController.cs
Normal file
467
server/src/Controllers/DebuggerController.cs
Normal file
@@ -0,0 +1,467 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Cors;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Peripherals.DebuggerClient;
|
||||||
|
|
||||||
|
namespace server.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// FPGA调试器控制器,提供信号捕获、触发、数据读取等调试相关API
|
||||||
|
/// </summary>
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[Authorize]
|
||||||
|
public class DebuggerController : ControllerBase
|
||||||
|
{
|
||||||
|
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 表示单个信号通道的配置信息
|
||||||
|
/// </summary>
|
||||||
|
public class ChannelConfig
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 通道名称
|
||||||
|
/// </summary>
|
||||||
|
required public string name;
|
||||||
|
/// <summary>
|
||||||
|
/// 通道显示颜色(如前端波形显示用)
|
||||||
|
/// </summary>
|
||||||
|
required public string color;
|
||||||
|
/// <summary>
|
||||||
|
/// 通道信号线宽度(位数)
|
||||||
|
/// </summary>
|
||||||
|
required public UInt32 wireWidth;
|
||||||
|
/// <summary>
|
||||||
|
/// 信号线在父端口中的起始索引(bit)
|
||||||
|
/// </summary>
|
||||||
|
required public UInt32 wireStartIndex;
|
||||||
|
/// <summary>
|
||||||
|
/// 父端口编号
|
||||||
|
/// </summary>
|
||||||
|
required public UInt32 parentPort;
|
||||||
|
/// <summary>
|
||||||
|
/// 捕获模式(如上升沿、下降沿等)
|
||||||
|
/// </summary>
|
||||||
|
required public CaptureMode mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 调试器整体配置信息
|
||||||
|
/// </summary>
|
||||||
|
public class DebuggerConfig
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 时钟频率
|
||||||
|
/// </summary>
|
||||||
|
required public UInt32 clkFreq;
|
||||||
|
/// <summary>
|
||||||
|
/// 总端口数量
|
||||||
|
/// </summary>
|
||||||
|
required public UInt32 totalPortNum;
|
||||||
|
/// <summary>
|
||||||
|
/// 捕获深度(采样点数)
|
||||||
|
/// </summary>
|
||||||
|
required public UInt32 captureDepth;
|
||||||
|
/// <summary>
|
||||||
|
/// 触发器数量
|
||||||
|
/// </summary>
|
||||||
|
required public UInt32 triggerNum;
|
||||||
|
/// <summary>
|
||||||
|
/// 所有信号通道的配置信息
|
||||||
|
/// </summary>
|
||||||
|
required public ChannelConfig[] channelConfigs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 单个通道的捕获数据
|
||||||
|
/// </summary>
|
||||||
|
public class ChannelCaptureData
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 通道名称
|
||||||
|
/// </summary>
|
||||||
|
required public string name;
|
||||||
|
/// <summary>
|
||||||
|
/// 通道捕获到的数据(Base64编码的UInt32数组)
|
||||||
|
/// </summary>
|
||||||
|
required public string data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前用户绑定的调试器实例
|
||||||
|
/// </summary>
|
||||||
|
private DebuggerClient? GetDebugger()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var userName = User.Identity?.Name;
|
||||||
|
if (string.IsNullOrEmpty(userName))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
var userRet = db.GetUserByName(userName);
|
||||||
|
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var user = userRet.Value.Value;
|
||||||
|
if (user.BoardID == Guid.Empty)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var boardRet = db.GetBoardByID(user.BoardID);
|
||||||
|
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var board = boardRet.Value.Value;
|
||||||
|
return new DebuggerClient(board.IpAddr, board.Port, 1);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取调试器实例时发生异常");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置指定信号线的捕获模式
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="wireNum">信号线编号(0~511)</param>
|
||||||
|
/// <param name="mode">捕获模式</param>
|
||||||
|
[HttpPost("SetMode")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> SetMode(UInt32 wireNum, CaptureMode mode)
|
||||||
|
{
|
||||||
|
if (wireNum > 512)
|
||||||
|
{
|
||||||
|
return BadRequest($"最多只能建立512位信号线");
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var debugger = GetDebugger();
|
||||||
|
if (debugger == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
var result = await debugger.SetMode(wireNum, mode);
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置捕获模式失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置捕获模式失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "设置捕获模式时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 为每个通道中的每根线设置捕获模式
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="config">调试器配置信息,包含所有通道的捕获模式设置</param>
|
||||||
|
[HttpPost("SetChannelsMode")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> SetChannelsMode([FromBody] DebuggerConfig config)
|
||||||
|
{
|
||||||
|
if (config == null || config.channelConfigs == null)
|
||||||
|
return BadRequest("配置无效");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var debugger = GetDebugger();
|
||||||
|
if (debugger == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
foreach (var channel in config.channelConfigs)
|
||||||
|
{
|
||||||
|
// 检查每个通道的配置
|
||||||
|
if (channel.wireWidth > 32 ||
|
||||||
|
channel.wireStartIndex > 32 ||
|
||||||
|
channel.wireStartIndex + channel.wireWidth > 32)
|
||||||
|
{
|
||||||
|
return BadRequest($"通道 {channel.name} 配置错误");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint i = 0; i < channel.wireWidth; i++)
|
||||||
|
{
|
||||||
|
var result = await debugger.SetMode(channel.wireStartIndex * (channel.parentPort * 32) + i, channel.mode);
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置通道 {channel.name} 第 {i} 根线捕获模式失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"设置通道 {channel.name} 第 {i} 根线捕获模式失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "为每个通道中的每根线设置捕获模式时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启动触发器,开始信号捕获
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("StartTrigger")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> StartTrigger()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var debugger = GetDebugger();
|
||||||
|
if (debugger == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
var result = await debugger.StartTrigger();
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"启动触发器失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "启动触发器失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "启动触发器时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 读取触发器状态标志
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("ReadFlag")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(byte), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> ReadFlag()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var debugger = GetDebugger();
|
||||||
|
if (debugger == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
var result = await debugger.ReadFlag();
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"读取触发器状态标志失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "读取触发器状态标志失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "读取触发器状态标志时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 清除触发器状态标志
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("ClearFlag")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> ClearFlag()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var debugger = GetDebugger();
|
||||||
|
if (debugger == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
var result = await debugger.ClearFlag();
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"清除触发器状态标志失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "清除触发器状态标志失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "清除触发器状态标志时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 读取捕获数据(等待触发完成后返回各通道采样数据)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="config">调试器配置信息,包含采样深度、端口数、通道配置等</param>
|
||||||
|
/// <param name="cancellationToken">取消操作的令牌</param>
|
||||||
|
[HttpPost("ReadData")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(ChannelCaptureData[]), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> ReadData([FromBody] DebuggerConfig config, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// 检查每个通道的配置
|
||||||
|
foreach (var channel in config.channelConfigs)
|
||||||
|
{
|
||||||
|
if (channel.wireWidth > 32 ||
|
||||||
|
channel.wireStartIndex > 32 ||
|
||||||
|
channel.wireStartIndex + channel.wireWidth > 32)
|
||||||
|
{
|
||||||
|
return BadRequest($"通道 {channel.name} 配置错误");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var debugger = GetDebugger();
|
||||||
|
if (debugger == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
// 等待捕获标志位
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
var flagResult = await debugger.ReadFlag();
|
||||||
|
if (!flagResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"读取捕获标志失败: {flagResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "读取捕获标志失败");
|
||||||
|
}
|
||||||
|
if (flagResult.Value == 1)
|
||||||
|
{
|
||||||
|
var clearResult = await debugger.ClearFlag();
|
||||||
|
if (!clearResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"清除捕获标志失败: {clearResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "清除捕获标志失败");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
await Task.Delay(500, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
var dataResult = await debugger.ReadData(config.totalPortNum);
|
||||||
|
if (!dataResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"读取捕获数据失败: {dataResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "读取捕获数据失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
var freshResult = await debugger.Refresh();
|
||||||
|
if (!freshResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"刷新调试器状态失败: {freshResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "刷新调试器状态失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawData = dataResult.Value;
|
||||||
|
// logger.Debug($"rawData: {BitConverter.ToString(rawData)}");
|
||||||
|
int depth = (int)config.captureDepth;
|
||||||
|
int portDataLen = 4 * depth;
|
||||||
|
int portNum = (int)config.totalPortNum;
|
||||||
|
var channelDataList = new List<ChannelCaptureData>();
|
||||||
|
|
||||||
|
foreach (var channel in config.channelConfigs)
|
||||||
|
{
|
||||||
|
int port = (int)channel.parentPort;
|
||||||
|
int wireStart = (int)channel.wireStartIndex;
|
||||||
|
int wireWidth = (int)channel.wireWidth;
|
||||||
|
|
||||||
|
// 每个port的数据长度
|
||||||
|
int portOffset = port * portDataLen;
|
||||||
|
|
||||||
|
var channelUintArr = new UInt32[depth];
|
||||||
|
for (int i = 0; i < depth; i++)
|
||||||
|
{
|
||||||
|
// 取出该port的第i个采样点的4字节
|
||||||
|
int sampleOffset = portOffset + i * 4;
|
||||||
|
if (sampleOffset + 4 > rawData.Length)
|
||||||
|
{
|
||||||
|
logger.Error($"数据越界: port {port}, sample {i}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "数据越界");
|
||||||
|
}
|
||||||
|
var sampleBytes = rawData[sampleOffset..(sampleOffset + 4)];
|
||||||
|
UInt32 sample = Common.Number.BytesToUInt32(sampleBytes, true).Value;
|
||||||
|
// 提取wireWidth位
|
||||||
|
UInt32 mask = (wireWidth == 32) ? 0xFFFFFFFF : ((1u << wireWidth) - 1u);
|
||||||
|
channelUintArr[i] = (sample >> wireStart) & mask;
|
||||||
|
}
|
||||||
|
var channelBytes = new byte[4 * depth];
|
||||||
|
Buffer.BlockCopy(channelUintArr, 0, channelBytes, 0, channelBytes.Length);
|
||||||
|
// logger.Debug($"{channel.name} HexData: {BitConverter.ToString(channelBytes)}");
|
||||||
|
var base64 = Convert.ToBase64String(channelBytes);
|
||||||
|
channelDataList.Add(new ChannelCaptureData { name = channel.name, data = base64 });
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(channelDataList.ToArray());
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
logger.Info("读取捕获数据请求被取消");
|
||||||
|
return StatusCode(StatusCodes.Status499ClientClosedRequest, "客户端已取消请求");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "读取捕获数据时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 刷新调试器状态(重置采集状态等)
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("Refresh")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> Refresh()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var debugger = GetDebugger();
|
||||||
|
if (debugger == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
var result = await debugger.Refresh();
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"刷新调试器状态失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "刷新调试器状态失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "刷新调试器状态时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
291
server/src/Controllers/ExamController.cs
Normal file
291
server/src/Controllers/ExamController.cs
Normal file
@@ -0,0 +1,291 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Cors;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using DotNext;
|
||||||
|
|
||||||
|
namespace server.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验控制器
|
||||||
|
/// </summary>
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
public class ExamController : ControllerBase
|
||||||
|
{
|
||||||
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验信息类
|
||||||
|
/// </summary>
|
||||||
|
public class ExamInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 实验的唯一标识符
|
||||||
|
/// </summary>
|
||||||
|
public required string ID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验名称
|
||||||
|
/// </summary>
|
||||||
|
public required string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验描述
|
||||||
|
/// </summary>
|
||||||
|
public required string Description { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验创建时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime CreatedTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验最后更新时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime UpdatedTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验标签
|
||||||
|
/// </summary>
|
||||||
|
public string[] Tags { get; set; } = Array.Empty<string>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验难度(1-5)
|
||||||
|
/// </summary>
|
||||||
|
public int Difficulty { get; set; } = 1;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 普通用户是否可见
|
||||||
|
/// </summary>
|
||||||
|
public bool IsVisibleToUsers { get; set; } = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验简要信息类(用于列表显示)
|
||||||
|
/// </summary>
|
||||||
|
public class ExamSummary
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 实验的唯一标识符
|
||||||
|
/// </summary>
|
||||||
|
public required string ID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验名称
|
||||||
|
/// </summary>
|
||||||
|
public required string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验创建时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime CreatedTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验最后更新时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime UpdatedTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验标签
|
||||||
|
/// </summary>
|
||||||
|
public string[] Tags { get; set; } = Array.Empty<string>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验难度(1-5)
|
||||||
|
/// </summary>
|
||||||
|
public int Difficulty { get; set; } = 1;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 普通用户是否可见
|
||||||
|
/// </summary>
|
||||||
|
public bool IsVisibleToUsers { get; set; } = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建实验请求类
|
||||||
|
/// </summary>
|
||||||
|
public class CreateExamRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 实验ID
|
||||||
|
/// </summary>
|
||||||
|
public required string ID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验名称
|
||||||
|
/// </summary>
|
||||||
|
public required string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验描述
|
||||||
|
/// </summary>
|
||||||
|
public required string Description { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验标签
|
||||||
|
/// </summary>
|
||||||
|
public string[] Tags { get; set; } = Array.Empty<string>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验难度(1-5)
|
||||||
|
/// </summary>
|
||||||
|
public int Difficulty { get; set; } = 1;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 普通用户是否可见
|
||||||
|
/// </summary>
|
||||||
|
public bool IsVisibleToUsers { get; set; } = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有实验列表
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>实验列表</returns>
|
||||||
|
[Authorize]
|
||||||
|
[HttpGet("list")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(ExamSummary[]), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public IActionResult GetExamList()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
var exams = db.GetAllExams();
|
||||||
|
|
||||||
|
var examSummaries = exams.Select(exam => new ExamSummary
|
||||||
|
{
|
||||||
|
ID = exam.ID,
|
||||||
|
Name = exam.Name,
|
||||||
|
CreatedTime = exam.CreatedTime,
|
||||||
|
UpdatedTime = exam.UpdatedTime,
|
||||||
|
Tags = exam.GetTagsList(),
|
||||||
|
Difficulty = exam.Difficulty,
|
||||||
|
IsVisibleToUsers = exam.IsVisibleToUsers
|
||||||
|
}).ToArray();
|
||||||
|
|
||||||
|
logger.Info($"成功获取实验列表,共 {examSummaries.Length} 个实验");
|
||||||
|
return Ok(examSummaries);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"获取实验列表时出错: {ex.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取实验列表失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据实验ID获取实验详细信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="examId">实验ID</param>
|
||||||
|
/// <returns>实验详细信息</returns>
|
||||||
|
[Authorize]
|
||||||
|
[HttpGet("{examId}")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(ExamInfo), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public IActionResult GetExam(string examId)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(examId))
|
||||||
|
return BadRequest("实验ID不能为空");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
var result = db.GetExamByID(examId);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取实验时出错: {result.Error.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取实验失败: {result.Error.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!result.Value.HasValue)
|
||||||
|
{
|
||||||
|
logger.Warn($"实验不存在: {examId}");
|
||||||
|
return NotFound($"实验 {examId} 不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
var exam = result.Value.Value;
|
||||||
|
var examInfo = new ExamInfo
|
||||||
|
{
|
||||||
|
ID = exam.ID,
|
||||||
|
Name = exam.Name,
|
||||||
|
Description = exam.Description,
|
||||||
|
CreatedTime = exam.CreatedTime,
|
||||||
|
UpdatedTime = exam.UpdatedTime,
|
||||||
|
Tags = exam.GetTagsList(),
|
||||||
|
Difficulty = exam.Difficulty,
|
||||||
|
IsVisibleToUsers = exam.IsVisibleToUsers
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.Info($"成功获取实验信息: {examId}");
|
||||||
|
return Ok(examInfo);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"获取实验 {examId} 时出错: {ex.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取实验失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建新实验
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request">创建实验请求</param>
|
||||||
|
/// <returns>创建结果</returns>
|
||||||
|
[Authorize("Admin")]
|
||||||
|
[HttpPost]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(ExamInfo), StatusCodes.Status201Created)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public IActionResult CreateExam([FromBody] CreateExamRequest request)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(request.ID) || string.IsNullOrWhiteSpace(request.Name) || string.IsNullOrWhiteSpace(request.Description))
|
||||||
|
return BadRequest("实验ID、名称和描述不能为空");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
var result = db.CreateExam(request.ID, request.Name, request.Description, request.Tags, request.Difficulty, request.IsVisibleToUsers);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
if (result.Error.Message.Contains("已存在"))
|
||||||
|
return Conflict(result.Error.Message);
|
||||||
|
|
||||||
|
logger.Error($"创建实验时出错: {result.Error.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"创建实验失败: {result.Error.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var exam = result.Value;
|
||||||
|
var examInfo = new ExamInfo
|
||||||
|
{
|
||||||
|
ID = exam.ID,
|
||||||
|
Name = exam.Name,
|
||||||
|
Description = exam.Description,
|
||||||
|
CreatedTime = exam.CreatedTime,
|
||||||
|
UpdatedTime = exam.UpdatedTime,
|
||||||
|
Tags = exam.GetTagsList(),
|
||||||
|
Difficulty = exam.Difficulty,
|
||||||
|
IsVisibleToUsers = exam.IsVisibleToUsers
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.Info($"成功创建实验: {request.ID}");
|
||||||
|
return CreatedAtAction(nameof(GetExam), new { examId = request.ID }, examInfo);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"创建实验 {request.ID} 时出错: {ex.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"创建实验失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
97
server/src/Controllers/HdmiVideoStreamController.cs
Normal file
97
server/src/Controllers/HdmiVideoStreamController.cs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Cors;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using server.Services;
|
||||||
|
using Database;
|
||||||
|
|
||||||
|
namespace server.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
public class HdmiVideoStreamController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly HttpHdmiVideoStreamService _videoStreamService;
|
||||||
|
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
public HdmiVideoStreamController(HttpHdmiVideoStreamService videoStreamService)
|
||||||
|
{
|
||||||
|
_videoStreamService = videoStreamService;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 管理员获取所有板子的 endpoints
|
||||||
|
[HttpGet("AllEndpoints")]
|
||||||
|
[Authorize("Admin")]
|
||||||
|
public ActionResult<List<HdmiVideoStreamEndpoint>> GetAllEndpoints()
|
||||||
|
{
|
||||||
|
var endpoints = _videoStreamService.GetAllVideoEndpoints();
|
||||||
|
if (endpoints == null)
|
||||||
|
return NotFound("No boards found.");
|
||||||
|
return Ok(endpoints);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户获取自己板子的 endpoint
|
||||||
|
[HttpGet("MyEndpoint")]
|
||||||
|
[Authorize]
|
||||||
|
public ActionResult<HdmiVideoStreamEndpoint> GetMyEndpoint()
|
||||||
|
{
|
||||||
|
var userName = User.FindFirstValue(ClaimTypes.Name);
|
||||||
|
if (string.IsNullOrEmpty(userName))
|
||||||
|
return Unauthorized("User name not found in claims.");
|
||||||
|
|
||||||
|
var db = new AppDataConnection();
|
||||||
|
if (db == null)
|
||||||
|
return NotFound("Database connection failed.");
|
||||||
|
|
||||||
|
var userRet = db.GetUserByName(userName);
|
||||||
|
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
|
||||||
|
return NotFound("User not found.");
|
||||||
|
|
||||||
|
var user = userRet.Value.Value;
|
||||||
|
var boardId = user.BoardID;
|
||||||
|
if (boardId == Guid.Empty)
|
||||||
|
return NotFound("No board bound to this user.");
|
||||||
|
|
||||||
|
var boardRet = db.GetBoardByID(boardId);
|
||||||
|
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
|
||||||
|
return NotFound("Board not found.");
|
||||||
|
|
||||||
|
var endpoint = _videoStreamService.GetVideoEndpoint(boardId.ToString());
|
||||||
|
return Ok(endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 禁用指定板子的 HDMI 传输
|
||||||
|
[HttpPost("DisableHdmiTransmission")]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<IActionResult> DisableHdmiTransmission()
|
||||||
|
{
|
||||||
|
var userName = User.FindFirstValue(ClaimTypes.Name);
|
||||||
|
if (string.IsNullOrEmpty(userName))
|
||||||
|
return Unauthorized("User name not found in claims.");
|
||||||
|
|
||||||
|
var db = new AppDataConnection();
|
||||||
|
if (db == null)
|
||||||
|
return NotFound("Database connection failed.");
|
||||||
|
|
||||||
|
var userRet = db.GetUserByName(userName);
|
||||||
|
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
|
||||||
|
return NotFound("User not found.");
|
||||||
|
|
||||||
|
var user = userRet.Value.Value;
|
||||||
|
var boardId = user.BoardID;
|
||||||
|
if (boardId == Guid.Empty)
|
||||||
|
return NotFound("No board bound to this user.");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _videoStreamService.DisableHdmiTransmissionAsync(boardId.ToString());
|
||||||
|
return Ok($"HDMI transmission for board {boardId} disabled.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, $"Failed to disable HDMI transmission for board {boardId}");
|
||||||
|
return StatusCode(500, $"Error disabling HDMI transmission: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Cors;
|
using Microsoft.AspNetCore.Cors;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Database;
|
||||||
|
using server.Services;
|
||||||
|
|
||||||
namespace server.Controllers;
|
namespace server.Controllers;
|
||||||
|
|
||||||
@@ -14,8 +16,15 @@ public class JtagController : ControllerBase
|
|||||||
{
|
{
|
||||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
private readonly ProgressTrackerService _tracker;
|
||||||
|
|
||||||
private const string BITSTREAM_PATH = "bitstream/Jtag";
|
private const string BITSTREAM_PATH = "bitstream/Jtag";
|
||||||
|
|
||||||
|
public JtagController(ProgressTrackerService tracker)
|
||||||
|
{
|
||||||
|
_tracker = tracker;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 控制器首页信息
|
/// 控制器首页信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -112,116 +121,96 @@ public class JtagController : ControllerBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 上传比特流文件到服务器
|
|
||||||
/// </summary>
|
|
||||||
/// <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("未选择文件");
|
|
||||||
}
|
|
||||||
|
|
||||||
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>
|
/// <summary>
|
||||||
/// 通过 JTAG 下载比特流文件到 FPGA 设备
|
/// 通过 JTAG 下载比特流文件到 FPGA 设备
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="address">JTAG 设备地址</param>
|
/// <param name="address">JTAG 设备地址</param>
|
||||||
/// <param name="port">JTAG 设备端口</param>
|
/// <param name="port">JTAG 设备端口</param>
|
||||||
/// <returns>下载结果</returns>
|
/// <param name="bitstreamId">比特流ID</param>
|
||||||
|
/// <returns>进度跟踪TaskID</returns>
|
||||||
[HttpPost("DownloadBitstream")]
|
[HttpPost("DownloadBitstream")]
|
||||||
[EnableCors("Users")]
|
[EnableCors("Users")]
|
||||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
public async ValueTask<IResult> DownloadBitstream(string address, int port)
|
public async ValueTask<IResult> DownloadBitstream(string address, int port, int bitstreamId, CancellationToken cancelToken)
|
||||||
{
|
{
|
||||||
logger.Info($"User {User.Identity?.Name} initiating bitstream download to device {address}:{port}");
|
logger.Info($"User {User.Identity?.Name} initiating bitstream download to device {address}:{port} using bitstream ID: {bitstreamId}");
|
||||||
|
|
||||||
// 检查文件
|
|
||||||
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
|
try
|
||||||
{
|
{
|
||||||
// 读取文件
|
// 获取当前用户名
|
||||||
var filePath = Directory.GetFiles(fileDir)[0];
|
var username = User.Identity?.Name;
|
||||||
logger.Info($"User {User.Identity?.Name} reading bitstream file: {filePath}");
|
if (string.IsNullOrEmpty(username))
|
||||||
|
|
||||||
using (var fileStream = System.IO.File.Open(filePath, System.IO.FileMode.Open))
|
|
||||||
{
|
{
|
||||||
if (fileStream is null || fileStream.Length <= 0)
|
logger.Warn("Anonymous user attempted to download bitstream");
|
||||||
{
|
return TypedResults.Unauthorized();
|
||||||
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");
|
// 从数据库获取用户信息
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
var userResult = db.GetUserByName(username);
|
||||||
|
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
|
||||||
|
{
|
||||||
|
logger.Error($"User {username} not found in database");
|
||||||
|
return TypedResults.BadRequest("用户不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
var user = userResult.Value.Value;
|
||||||
|
|
||||||
|
// 从数据库获取比特流
|
||||||
|
var bitstreamResult = db.GetResourceById(bitstreamId);
|
||||||
|
|
||||||
|
if (!bitstreamResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"User {username} failed to get bitstream from database: {bitstreamResult.Error}");
|
||||||
|
return TypedResults.InternalServerError($"数据库查询失败: {bitstreamResult.Error?.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bitstreamResult.Value.HasValue)
|
||||||
|
{
|
||||||
|
logger.Warn($"User {username} attempted to download non-existent bitstream ID: {bitstreamId}");
|
||||||
|
return TypedResults.BadRequest("比特流不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
var bitstream = bitstreamResult.Value.Value;
|
||||||
|
|
||||||
|
// 处理比特流数据
|
||||||
|
var fileBytes = bitstream.Data;
|
||||||
|
if (fileBytes == null || fileBytes.Length == 0)
|
||||||
|
{
|
||||||
|
logger.Warn($"User {username} found empty bitstream data for ID: {bitstreamId}");
|
||||||
|
return TypedResults.BadRequest("比特流数据为空,请重新上传");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info($"User {username} processing bitstream file of size: {fileBytes.Length} bytes");
|
||||||
|
|
||||||
|
// 定义进度跟踪
|
||||||
|
var (taskId, progress) = _tracker.CreateTask(cancelToken);
|
||||||
|
progress.Report(10);
|
||||||
|
|
||||||
|
_ = Task.Run(async () =>
|
||||||
|
{
|
||||||
// 定义缓冲区大小: 32KB
|
// 定义缓冲区大小: 32KB
|
||||||
byte[] buffer = new byte[32 * 1024];
|
byte[] buffer = new byte[32 * 1024];
|
||||||
byte[] revBuffer = new byte[32 * 1024];
|
byte[] revBuffer = new byte[32 * 1024];
|
||||||
long totalBytesRead = 0;
|
long totalBytesProcessed = 0;
|
||||||
|
|
||||||
// 使用异步流读取文件
|
// 使用内存流处理文件
|
||||||
using (var memoryStream = new MemoryStream())
|
using (var inputStream = new MemoryStream(fileBytes))
|
||||||
|
using (var outputStream = new MemoryStream())
|
||||||
{
|
{
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
while ((bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
|
while ((bytesRead = await inputStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
|
||||||
{
|
{
|
||||||
// 反转 32bits
|
// 反转 32bits
|
||||||
var retBuffer = Common.Number.ReverseBytes(buffer, 4);
|
var retBuffer = Common.Number.ReverseBytes(buffer, 4);
|
||||||
if (!retBuffer.IsSuccessful)
|
if (!retBuffer.IsSuccessful)
|
||||||
{
|
{
|
||||||
logger.Error($"User {User.Identity?.Name} failed to reverse bytes: {retBuffer.Error}");
|
logger.Error($"User {username} failed to reverse bytes: {retBuffer.Error}");
|
||||||
return TypedResults.InternalServerError(retBuffer.Error);
|
progress.Error($"User {username} failed to reverse bytes: {retBuffer.Error}");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
revBuffer = retBuffer.Value;
|
revBuffer = retBuffer.Value;
|
||||||
|
|
||||||
@@ -230,34 +219,38 @@ public class JtagController : ControllerBase
|
|||||||
revBuffer[i] = Common.Number.ReverseBits(revBuffer[i]);
|
revBuffer[i] = Common.Number.ReverseBits(revBuffer[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
await memoryStream.WriteAsync(revBuffer, 0, bytesRead);
|
await outputStream.WriteAsync(revBuffer, 0, bytesRead);
|
||||||
totalBytesRead += bytesRead;
|
totalBytesProcessed += bytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将所有数据转换为字节数组(注意:如果文件非常大,可能不适合完全加载到内存)
|
// 获取处理后的数据
|
||||||
var fileBytes = memoryStream.ToArray();
|
var processedBytes = outputStream.ToArray();
|
||||||
logger.Info($"User {User.Identity?.Name} processed {totalBytesRead} bytes for device {address}");
|
logger.Info($"User {username} processed {totalBytesProcessed} bytes for device {address}");
|
||||||
|
|
||||||
|
progress.Report(20);
|
||||||
|
|
||||||
// 下载比特流
|
// 下载比特流
|
||||||
var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port);
|
var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port);
|
||||||
var ret = await jtagCtrl.DownloadBitstream(fileBytes);
|
var ret = await jtagCtrl.DownloadBitstream(processedBytes);
|
||||||
|
|
||||||
if (ret.IsSuccessful)
|
if (ret.IsSuccessful)
|
||||||
{
|
{
|
||||||
logger.Info($"User {User.Identity?.Name} successfully downloaded bitstream to device {address}");
|
logger.Info($"User {username} successfully downloaded bitstream '{bitstream.ResourceName}' to device {address}");
|
||||||
return TypedResults.Ok(ret.Value);
|
progress.Finish();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
logger.Error($"User {User.Identity?.Name} failed to download bitstream to device {address}: {ret.Error}");
|
logger.Error($"User {username} failed to download bitstream to device {address}: {ret.Error}");
|
||||||
return TypedResults.InternalServerError(ret.Error);
|
progress.Error($"User {username} failed to download bitstream to device {address}: {ret.Error}");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return TypedResults.Ok(taskId);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
logger.Error(ex, $"User {User.Identity?.Name} encountered exception while downloading bitstream to device {address}");
|
logger.Error(ex, $"User encountered exception while downloading bitstream to device {address}");
|
||||||
return TypedResults.InternalServerError(ex);
|
return TypedResults.InternalServerError(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,22 @@ public class LogicAnalyzerController : ControllerBase
|
|||||||
/// 全局触发模式
|
/// 全局触发模式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public GlobalCaptureMode GlobalMode { get; set; }
|
public GlobalCaptureMode GlobalMode { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 捕获深度
|
||||||
|
/// </summary>
|
||||||
|
public int CaptureLength { get; set; } = 2048 * 32;
|
||||||
|
/// <summary>
|
||||||
|
/// 预采样深度
|
||||||
|
/// </summary>
|
||||||
|
public int PreCaptureLength { get; set; } = 2048;
|
||||||
|
/// <summary>
|
||||||
|
/// 有效通道
|
||||||
|
/// </summary>
|
||||||
|
public AnalyzerChannelDiv ChannelDiv { get; set; } = AnalyzerChannelDiv.EIGHT;
|
||||||
|
/// <summary>
|
||||||
|
/// 时钟分频系数
|
||||||
|
/// </summary>
|
||||||
|
public AnalyzerClockDiv ClockDiv { get; set; } = AnalyzerClockDiv.DIV1;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 信号触发配置列表
|
/// 信号触发配置列表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -77,7 +92,7 @@ public class LogicAnalyzerController : ControllerBase
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
var board = boardRet.Value.Value;
|
var board = boardRet.Value.Value;
|
||||||
return new Analyzer(board.IpAddr, board.Port, 2);
|
return new Analyzer(board.IpAddr, board.Port, 0);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -208,8 +223,8 @@ public class LogicAnalyzerController : ControllerBase
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (signalIndex < 0 || signalIndex > 7)
|
if (signalIndex < 0 || signalIndex > 31)
|
||||||
return BadRequest("信号索引必须在0-7之间");
|
return BadRequest("信号索引必须在0-31之间");
|
||||||
|
|
||||||
var analyzer = GetAnalyzer();
|
var analyzer = GetAnalyzer();
|
||||||
if (analyzer == null)
|
if (analyzer == null)
|
||||||
@@ -231,6 +246,50 @@ public class LogicAnalyzerController : ControllerBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置深度、预采样深度、有效通道
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="capture_length">深度</param>
|
||||||
|
/// <param name="pre_capture_length">预采样深度</param>
|
||||||
|
/// <param name="channel_div">有效通道(0-[1],1-[2],2-[4],3-[8],4-[16],5-[32])</param>
|
||||||
|
/// <param name="clock_div">采样时钟分频系数</param>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("SetCaptureParams")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> SetCaptureParams(int capture_length, int pre_capture_length, AnalyzerChannelDiv channel_div, AnalyzerClockDiv clock_div)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//DDR深度为 32'h01000000 - 32'h0FFFFFFF
|
||||||
|
if (capture_length < 0 || capture_length > 0x10000000 - 0x01000000)
|
||||||
|
return BadRequest("采样深度设置错误");
|
||||||
|
if (pre_capture_length < 0 || pre_capture_length >= capture_length)
|
||||||
|
return BadRequest("预采样深度必须小于捕获深度");
|
||||||
|
|
||||||
|
var analyzer = GetAnalyzer();
|
||||||
|
if (analyzer == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
var result = await analyzer.SetCaptureParams(capture_length, pre_capture_length, channel_div, clock_div);
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置深度、预采样深度、有效通道、时钟分频失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置深度、预采样深度、有效通道、时钟分频失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "设置深度、预采样深度、有效通道、时钟分频失败时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 批量配置捕获参数
|
/// 批量配置捕获参数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -264,8 +323,8 @@ public class LogicAnalyzerController : ControllerBase
|
|||||||
// 设置信号触发模式
|
// 设置信号触发模式
|
||||||
foreach (var signalConfig in config.SignalConfigs)
|
foreach (var signalConfig in config.SignalConfigs)
|
||||||
{
|
{
|
||||||
if (signalConfig.SignalIndex < 0 || signalConfig.SignalIndex > 7)
|
if (signalConfig.SignalIndex < 0 || signalConfig.SignalIndex > 31)
|
||||||
return BadRequest($"信号索引{signalConfig.SignalIndex}超出范围0-7");
|
return BadRequest($"信号索引{signalConfig.SignalIndex}超出范围0-31");
|
||||||
|
|
||||||
var signalResult = await analyzer.SetSignalTrigMode(
|
var signalResult = await analyzer.SetSignalTrigMode(
|
||||||
signalConfig.SignalIndex, signalConfig.Operator, signalConfig.Value);
|
signalConfig.SignalIndex, signalConfig.Operator, signalConfig.Value);
|
||||||
@@ -276,6 +335,14 @@ public class LogicAnalyzerController : ControllerBase
|
|||||||
$"设置信号{signalConfig.SignalIndex}触发模式失败");
|
$"设置信号{signalConfig.SignalIndex}触发模式失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 设置深度、预采样深度、有效通道
|
||||||
|
var paramsResult = await analyzer.SetCaptureParams(
|
||||||
|
config.CaptureLength, config.PreCaptureLength, config.ChannelDiv, config.ClockDiv);
|
||||||
|
if (!paramsResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置深度、预采样深度、有效通道失败: {paramsResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置深度、预采样深度、有效通道失败");
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
@@ -330,7 +397,7 @@ public class LogicAnalyzerController : ControllerBase
|
|||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
public async Task<IActionResult> GetCaptureData()
|
public async Task<IActionResult> GetCaptureData(int capture_length = 2048 * 32)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -338,7 +405,7 @@ public class LogicAnalyzerController : ControllerBase
|
|||||||
if (analyzer == null)
|
if (analyzer == null)
|
||||||
return BadRequest("用户未绑定有效的实验板");
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
var result = await analyzer.ReadCaptureData();
|
var result = await analyzer.ReadCaptureData(capture_length);
|
||||||
if (!result.IsSuccessful)
|
if (!result.IsSuccessful)
|
||||||
{
|
{
|
||||||
logger.Error($"读取捕获数据失败: {result.Error}");
|
logger.Error($"读取捕获数据失败: {result.Error}");
|
||||||
|
|||||||
776
server/src/Controllers/NetConfigController.cs
Normal file
776
server/src/Controllers/NetConfigController.cs
Normal file
@@ -0,0 +1,776 @@
|
|||||||
|
using System.Net;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Cors;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Peripherals.NetConfigClient;
|
||||||
|
|
||||||
|
namespace server.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 网络配置控制器(仅管理员权限)
|
||||||
|
/// </summary>
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[Authorize("Admin")]
|
||||||
|
public class NetConfigController : ControllerBase
|
||||||
|
{
|
||||||
|
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
// 固定的实验板IP,端口,MAC地址
|
||||||
|
private const string BOARD_IP = "169.254.109.0";
|
||||||
|
private const int BOARD_PORT = 1234;
|
||||||
|
|
||||||
|
// 本机网络信息
|
||||||
|
private readonly IPAddress _localIP;
|
||||||
|
private readonly byte[] _localMAC;
|
||||||
|
private readonly string _localIPString;
|
||||||
|
private readonly string _localMACString;
|
||||||
|
private readonly string _localInterface;
|
||||||
|
|
||||||
|
public NetConfigController()
|
||||||
|
{
|
||||||
|
// 初始化本机IP地址
|
||||||
|
_localIP = GetLocalIPAddress();
|
||||||
|
_localIPString = _localIP?.ToString() ?? "未知";
|
||||||
|
|
||||||
|
// 初始化本机MAC地址
|
||||||
|
_localMAC = GetLocalMACAddress();
|
||||||
|
_localMACString = _localMAC != null ? BitConverter.ToString(_localMAC).Replace("-", ":") : "未知";
|
||||||
|
|
||||||
|
// 获取本机网络接口名称
|
||||||
|
_localInterface = GetLocalNetworkInterface();
|
||||||
|
|
||||||
|
logger.Info($"NetConfigController 初始化完成 - 本机IP: {_localIPString}, 本机MAC: {_localMACString}, 接口: {_localInterface}");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取本机IP地址(优先选择与实验板同网段的IP)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>本机IP地址</returns>
|
||||||
|
private IPAddress GetLocalIPAddress()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var boardIpSegments = BOARD_IP.Split('.').Take(3).ToArray();
|
||||||
|
|
||||||
|
// 优先选择与实验板IP前三段相同的IP
|
||||||
|
var sameSegmentIP = System.Net.NetworkInformation.NetworkInterface
|
||||||
|
.GetAllNetworkInterfaces()
|
||||||
|
.Where(nic => nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up
|
||||||
|
&& nic.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Loopback)
|
||||||
|
.SelectMany(nic => nic.GetIPProperties().UnicastAddresses)
|
||||||
|
.Where(addr => addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
||||||
|
.Select(addr => addr.Address)
|
||||||
|
.FirstOrDefault(addr =>
|
||||||
|
{
|
||||||
|
var segments = addr.ToString().Split('.');
|
||||||
|
return segments.Length == 4 &&
|
||||||
|
segments[0] == boardIpSegments[0] &&
|
||||||
|
segments[1] == boardIpSegments[1] &&
|
||||||
|
segments[2] == boardIpSegments[2];
|
||||||
|
});
|
||||||
|
|
||||||
|
if (sameSegmentIP != null)
|
||||||
|
return sameSegmentIP;
|
||||||
|
|
||||||
|
// 如果没有找到同网段的IP,返回第一个可用的IP
|
||||||
|
return System.Net.NetworkInformation.NetworkInterface
|
||||||
|
.GetAllNetworkInterfaces()
|
||||||
|
.Where(nic => nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up
|
||||||
|
&& nic.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Loopback)
|
||||||
|
.SelectMany(nic => nic.GetIPProperties().UnicastAddresses)
|
||||||
|
.Where(addr => addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
||||||
|
.Select(addr => addr.Address)
|
||||||
|
.FirstOrDefault() ?? IPAddress.Loopback;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取本机IP地址失败");
|
||||||
|
return IPAddress.Loopback;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取本机MAC地址
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>本机MAC地址字节数组</returns>
|
||||||
|
private byte[] GetLocalMACAddress()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return System.Net.NetworkInformation.NetworkInterface
|
||||||
|
.GetAllNetworkInterfaces()
|
||||||
|
.Where(nic => nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up
|
||||||
|
&& nic.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Loopback)
|
||||||
|
.Select(nic => nic.GetPhysicalAddress()?.GetAddressBytes())
|
||||||
|
.FirstOrDefault(bytes => bytes != null && bytes.Length == 6) ?? new byte[6];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取本机MAC地址失败");
|
||||||
|
return new byte[6];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取本机网络接口名称
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>网络接口名称</returns>
|
||||||
|
private string GetLocalNetworkInterface()
|
||||||
|
{
|
||||||
|
return GetLocalIPAddress().ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化ARP记录
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>是否成功</returns>
|
||||||
|
private async Task<bool> InitializeArpAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await ArpClient.UpdateArpEntryAsync(BOARD_IP);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "初始化ARP记录失败");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取主机IP地址
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>主机IP地址</returns>
|
||||||
|
[HttpGet("GetHostIP")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> GetHostIP()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
var result = await netConfig.GetHostIP();
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取主机IP失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取失败: {result.Error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取主机IP时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取板卡IP地址
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>板卡IP地址</returns>
|
||||||
|
[HttpGet("GetBoardIP")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> GetBoardIP()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
var result = await netConfig.GetBoardIP();
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取板卡IP失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取失败: {result.Error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取板卡IP时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取主机MAC地址
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>主机MAC地址</returns>
|
||||||
|
[HttpGet("GetHostMAC")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> GetHostMAC()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
var result = await netConfig.GetHostMAC();
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取主机MAC地址失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取失败: {result.Error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取主机MAC地址时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取板卡MAC地址
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>板卡MAC地址</returns>
|
||||||
|
[HttpGet("GetBoardMAC")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> GetBoardMAC()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
var result = await netConfig.GetBoardMAC();
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取板卡MAC地址失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取失败: {result.Error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取板卡MAC地址时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有网络配置信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>网络配置信息</returns>
|
||||||
|
[HttpGet("GetNetworkConfig")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(NetworkConfigDto), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> GetNetworkConfig()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
|
||||||
|
var hostIPResult = await netConfig.GetHostIP();
|
||||||
|
var boardIPResult = await netConfig.GetBoardIP();
|
||||||
|
var hostMACResult = await netConfig.GetHostMAC();
|
||||||
|
var boardMACResult = await netConfig.GetBoardMAC();
|
||||||
|
|
||||||
|
var config = new NetworkConfigDto
|
||||||
|
{
|
||||||
|
HostIP = hostIPResult.IsSuccessful ? hostIPResult.Value : "获取失败",
|
||||||
|
BoardIP = boardIPResult.IsSuccessful ? boardIPResult.Value : "获取失败",
|
||||||
|
HostMAC = hostMACResult.IsSuccessful ? hostMACResult.Value : "获取失败",
|
||||||
|
BoardMAC = boardMACResult.IsSuccessful ? boardMACResult.Value : "获取失败"
|
||||||
|
};
|
||||||
|
|
||||||
|
return Ok(config);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取网络配置信息时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取本机所有网络接口信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>网络接口信息列表</returns>
|
||||||
|
[HttpGet("GetLocalNetworkInterfaces")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(List<NetworkInterfaceDto>), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public IActionResult GetLocalNetworkInterfaces()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var interfaces = System.Net.NetworkInformation.NetworkInterface
|
||||||
|
.GetAllNetworkInterfaces()
|
||||||
|
.Where(nic => nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up
|
||||||
|
&& nic.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Loopback)
|
||||||
|
.Select(nic => new NetworkInterfaceDto
|
||||||
|
{
|
||||||
|
Name = nic.Name,
|
||||||
|
Description = nic.Description,
|
||||||
|
Type = nic.NetworkInterfaceType.ToString(),
|
||||||
|
Status = nic.OperationalStatus.ToString(),
|
||||||
|
IPAddresses = nic.GetIPProperties().UnicastAddresses
|
||||||
|
.Where(addr => addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
||||||
|
.Select(addr => addr.Address.ToString())
|
||||||
|
.ToList(),
|
||||||
|
MACAddress = nic.GetPhysicalAddress().ToString()
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return Ok(interfaces);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取本机网络接口信息时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置主机IP地址
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostIp">主机IP地址</param>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("SetHostIP")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> SetHostIP(string hostIp)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(hostIp))
|
||||||
|
return BadRequest("主机IP地址不能为空");
|
||||||
|
|
||||||
|
if (!IPAddress.TryParse(hostIp, out var hostIpAddress))
|
||||||
|
return BadRequest("主机IP地址格式不正确");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
var result = await netConfig.SetHostIP(hostIpAddress);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置主机IP失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "设置主机IP时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置板卡IP地址
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="newBoardIp">新的板卡IP地址</param>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("SetBoardIP")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> SetBoardIP(string newBoardIp)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(newBoardIp))
|
||||||
|
return BadRequest("新的板卡IP地址不能为空");
|
||||||
|
|
||||||
|
if (!IPAddress.TryParse(newBoardIp, out var newIpAddress))
|
||||||
|
return BadRequest("新的板卡IP地址格式不正确");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
var result = await netConfig.SetBoardIP(newIpAddress);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置板卡IP失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "设置板卡IP时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置板卡MAC地址
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="boardMac">板卡MAC地址(格式:AA:BB:CC:DD:EE:FF)</param>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("SetBoardMAC")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> SetBoardMAC(string boardMac)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(boardMac))
|
||||||
|
return BadRequest("板卡MAC地址不能为空");
|
||||||
|
|
||||||
|
// 解析MAC地址
|
||||||
|
if (!TryParseMacAddress(boardMac, out var macBytes))
|
||||||
|
return BadRequest("MAC地址格式不正确,请使用格式:AA:BB:CC:DD:EE:FF");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
// 创建网络配置客户端
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
var result = await netConfig.SetBoardMAC(macBytes);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置板卡MAC地址失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "设置板卡MAC地址时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置主机MAC地址
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostMac">主机MAC地址(格式:AA:BB:CC:DD:EE:FF)</param>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("SetHostMAC")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> SetHostMAC(string hostMac)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(hostMac))
|
||||||
|
return BadRequest("主机MAC地址不能为空");
|
||||||
|
|
||||||
|
// 解析MAC地址
|
||||||
|
if (!TryParseMacAddress(hostMac, out var macBytes))
|
||||||
|
return BadRequest("MAC地址格式不正确,请使用格式:AA:BB:CC:DD:EE:FF");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
var result = await netConfig.SetHostMAC(macBytes);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置主机MAC地址失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "设置主机MAC地址时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 自动获取本机IP地址并设置为实验板主机IP
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("UpdateHostIP")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> UpdateHostIP()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_localIP == null)
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "无法获取本机IP地址");
|
||||||
|
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
var result = await netConfig.SetHostIP(_localIP);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"自动设置主机IP失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "自动设置主机IP时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新主机MAC地址
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("UpdateHostMAC")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> UpdateHostMAC()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_localMAC == null || _localMAC.Length != 6)
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "无法获取本机MAC地址");
|
||||||
|
|
||||||
|
if (!(await InitializeArpAsync()))
|
||||||
|
{
|
||||||
|
throw new Exception("无法配置ARP记录");
|
||||||
|
}
|
||||||
|
|
||||||
|
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||||
|
var result = await netConfig.SetHostMAC(_localMAC);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置主机MAC地址失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "设置主机MAC地址时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取本机网络信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>本机网络信息</returns>
|
||||||
|
[HttpGet("GetLocalNetworkInfo")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
||||||
|
public IActionResult GetLocalNetworkInfo()
|
||||||
|
{
|
||||||
|
return Ok(new
|
||||||
|
{
|
||||||
|
LocalIP = _localIPString,
|
||||||
|
LocalMAC = _localMACString,
|
||||||
|
LocalInterface = _localInterface
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 解析MAC地址字符串为字节数组
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="macAddress">MAC地址字符串</param>
|
||||||
|
/// <param name="macBytes">解析后的字节数组</param>
|
||||||
|
/// <returns>是否解析成功</returns>
|
||||||
|
private static bool TryParseMacAddress(string macAddress, out byte[] macBytes)
|
||||||
|
{
|
||||||
|
macBytes = Array.Empty<byte>();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(macAddress))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// 移除可能的分隔符并统一为冒号
|
||||||
|
var cleanMac = macAddress.Replace("-", ":").Replace(" ", "").ToUpper();
|
||||||
|
|
||||||
|
// 验证格式
|
||||||
|
if (cleanMac.Length != 17 || cleanMac.Count(c => c == ':') != 5)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var parts = cleanMac.Split(':');
|
||||||
|
if (parts.Length != 6)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
macBytes = new byte[6];
|
||||||
|
for (int i = 0; i < 6; i++)
|
||||||
|
{
|
||||||
|
macBytes[i] = Convert.ToByte(parts[i], 16);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
macBytes = Array.Empty<byte>();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 网络配置数据传输对象
|
||||||
|
/// </summary>
|
||||||
|
public class NetworkConfigDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 主机IP地址
|
||||||
|
/// </summary>
|
||||||
|
public string? HostIP { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 板卡IP地址
|
||||||
|
/// </summary>
|
||||||
|
public string? BoardIP { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 主机MAC地址
|
||||||
|
/// </summary>
|
||||||
|
public string? HostMAC { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 板卡MAC地址
|
||||||
|
/// </summary>
|
||||||
|
public string? BoardMAC { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 网络配置操作结果
|
||||||
|
/// </summary>
|
||||||
|
public class NetworkConfigResult
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 主机IP设置结果
|
||||||
|
/// </summary>
|
||||||
|
public bool? HostIPResult { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 主机IP设置错误信息
|
||||||
|
/// </summary>
|
||||||
|
public string? HostIPError { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 板卡IP设置结果
|
||||||
|
/// </summary>
|
||||||
|
public bool? BoardIPResult { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 板卡IP设置错误信息
|
||||||
|
/// </summary>
|
||||||
|
public string? BoardIPError { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 主机MAC设置结果
|
||||||
|
/// </summary>
|
||||||
|
public bool? HostMACResult { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 主机MAC设置错误信息
|
||||||
|
/// </summary>
|
||||||
|
public string? HostMACError { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 板卡MAC设置结果
|
||||||
|
/// </summary>
|
||||||
|
public bool? BoardMACResult { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 板卡MAC设置错误信息
|
||||||
|
/// </summary>
|
||||||
|
public string? BoardMACError { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 网络接口信息数据传输对象
|
||||||
|
/// </summary>
|
||||||
|
public class NetworkInterfaceDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 网络接口名称
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 网络接口描述
|
||||||
|
/// </summary>
|
||||||
|
public string Description { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 网络接口类型
|
||||||
|
/// </summary>
|
||||||
|
public string Type { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 网络接口状态
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// IP地址列表
|
||||||
|
/// </summary>
|
||||||
|
public List<string> IPAddresses { get; set; } = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// MAC地址
|
||||||
|
/// </summary>
|
||||||
|
public string MACAddress { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
484
server/src/Controllers/OscilloscopeController.cs
Normal file
484
server/src/Controllers/OscilloscopeController.cs
Normal file
@@ -0,0 +1,484 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Cors;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Peripherals.OscilloscopeClient;
|
||||||
|
|
||||||
|
namespace server.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 示波器API控制器 - 普通用户权限
|
||||||
|
/// </summary>
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[Authorize]
|
||||||
|
public class OscilloscopeApiController : ControllerBase
|
||||||
|
{
|
||||||
|
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 示波器完整配置
|
||||||
|
/// </summary>
|
||||||
|
public class OscilloscopeFullConfig
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 是否启动捕获
|
||||||
|
/// </summary>
|
||||||
|
public bool CaptureEnabled { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发电平(0-255)
|
||||||
|
/// </summary>
|
||||||
|
public byte TriggerLevel { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发边沿(true为上升沿,false为下降沿)
|
||||||
|
/// </summary>
|
||||||
|
public bool TriggerRisingEdge { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 水平偏移量(0-1023)
|
||||||
|
/// </summary>
|
||||||
|
public ushort HorizontalShift { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 抽样率(0-1023)
|
||||||
|
/// </summary>
|
||||||
|
public ushort DecimationRate { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否自动刷新RAM
|
||||||
|
/// </summary>
|
||||||
|
public bool AutoRefreshRAM { get; set; } = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 示波器状态和数据
|
||||||
|
/// </summary>
|
||||||
|
public class OscilloscopeDataResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// AD采样频率
|
||||||
|
/// </summary>
|
||||||
|
public uint ADFrequency { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// AD采样幅度
|
||||||
|
/// </summary>
|
||||||
|
public byte ADVpp { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// AD采样最大值
|
||||||
|
/// </summary>
|
||||||
|
public byte ADMax { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// AD采样最小值
|
||||||
|
/// </summary>
|
||||||
|
public byte ADMin { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 波形数据(Base64编码)
|
||||||
|
/// </summary>
|
||||||
|
public string WaveformData { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取示波器实例
|
||||||
|
/// </summary>
|
||||||
|
private Oscilloscope? GetOscilloscope()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var userName = User.Identity?.Name;
|
||||||
|
if (string.IsNullOrEmpty(userName))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
var userRet = db.GetUserByName(userName);
|
||||||
|
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var user = userRet.Value.Value;
|
||||||
|
if (user.BoardID == Guid.Empty)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var boardRet = db.GetBoardByID(user.BoardID);
|
||||||
|
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var board = boardRet.Value.Value;
|
||||||
|
return new Oscilloscope(board.IpAddr, board.Port);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取示波器实例时发生异常");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化示波器
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="config">示波器配置</param>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("Initialize")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> Initialize([FromBody] OscilloscopeFullConfig config)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (config == null)
|
||||||
|
return BadRequest("配置参数不能为空");
|
||||||
|
|
||||||
|
if (config.HorizontalShift > 1023)
|
||||||
|
return BadRequest("水平偏移量必须在0-1023之间");
|
||||||
|
|
||||||
|
if (config.DecimationRate > 1023)
|
||||||
|
return BadRequest("抽样率必须在0-1023之间");
|
||||||
|
|
||||||
|
var oscilloscope = GetOscilloscope();
|
||||||
|
if (oscilloscope == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
// 首先关闭捕获
|
||||||
|
var stopResult = await oscilloscope.SetCaptureEnable(false);
|
||||||
|
if (!stopResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"关闭捕获失败: {stopResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "关闭捕获失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置触发电平
|
||||||
|
var levelResult = await oscilloscope.SetTriggerLevel(config.TriggerLevel);
|
||||||
|
if (!levelResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置触发电平失败: {levelResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置触发电平失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置触发边沿
|
||||||
|
var edgeResult = await oscilloscope.SetTriggerEdge(config.TriggerRisingEdge);
|
||||||
|
if (!edgeResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置触发边沿失败: {edgeResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置触发边沿失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置水平偏移量
|
||||||
|
var shiftResult = await oscilloscope.SetHorizontalShift(config.HorizontalShift);
|
||||||
|
if (!shiftResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置水平偏移量失败: {shiftResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置水平偏移量失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置抽样率
|
||||||
|
var rateResult = await oscilloscope.SetDecimationRate(config.DecimationRate);
|
||||||
|
if (!rateResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置抽样率失败: {rateResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置抽样率失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 刷新RAM
|
||||||
|
if (config.AutoRefreshRAM)
|
||||||
|
{
|
||||||
|
var refreshResult = await oscilloscope.RefreshRAM();
|
||||||
|
if (!refreshResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"刷新RAM失败: {refreshResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "刷新RAM失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置捕获开关
|
||||||
|
var captureResult = await oscilloscope.SetCaptureEnable(config.CaptureEnabled);
|
||||||
|
if (!captureResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置捕获开关失败: {captureResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置捕获开关失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "初始化示波器时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启动捕获
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("StartCapture")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> StartCapture()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var oscilloscope = GetOscilloscope();
|
||||||
|
if (oscilloscope == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
var result = await oscilloscope.SetCaptureEnable(true);
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"启动捕获失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "启动捕获失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "启动捕获时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 停止捕获
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("StopCapture")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> StopCapture()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var oscilloscope = GetOscilloscope();
|
||||||
|
if (oscilloscope == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
var result = await oscilloscope.SetCaptureEnable(false);
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"停止捕获失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "停止捕获失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "停止捕获时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取示波器数据和状态
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>示波器数据和状态信息</returns>
|
||||||
|
[HttpGet("GetData")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(OscilloscopeDataResponse), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> GetData()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var oscilloscope = GetOscilloscope();
|
||||||
|
if (oscilloscope == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
var freqResult = await oscilloscope.GetADFrequency();
|
||||||
|
var vppResult = await oscilloscope.GetADVpp();
|
||||||
|
var maxResult = await oscilloscope.GetADMax();
|
||||||
|
var minResult = await oscilloscope.GetADMin();
|
||||||
|
var waveformResult = await oscilloscope.GetWaveformData();
|
||||||
|
|
||||||
|
if (!freqResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取AD采样频率失败: {freqResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取AD采样频率失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!vppResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取AD采样幅度失败: {vppResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取AD采样幅度失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!maxResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取AD采样最大值失败: {maxResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取AD采样最大值失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!minResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取AD采样最小值失败: {minResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取AD采样最小值失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!waveformResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取波形数据失败: {waveformResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取波形数据失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = new OscilloscopeDataResponse
|
||||||
|
{
|
||||||
|
ADFrequency = freqResult.Value,
|
||||||
|
ADVpp = vppResult.Value,
|
||||||
|
ADMax = maxResult.Value,
|
||||||
|
ADMin = minResult.Value,
|
||||||
|
WaveformData = Convert.ToBase64String(waveformResult.Value)
|
||||||
|
};
|
||||||
|
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "获取示波器数据时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新触发参数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="level">触发电平(0-255)</param>
|
||||||
|
/// <param name="risingEdge">触发边沿(true为上升沿,false为下降沿)</param>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("UpdateTrigger")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> UpdateTrigger(byte level, bool risingEdge)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var oscilloscope = GetOscilloscope();
|
||||||
|
if (oscilloscope == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
// 设置触发电平
|
||||||
|
var levelResult = await oscilloscope.SetTriggerLevel(level);
|
||||||
|
if (!levelResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置触发电平失败: {levelResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置触发电平失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置触发边沿
|
||||||
|
var edgeResult = await oscilloscope.SetTriggerEdge(risingEdge);
|
||||||
|
if (!edgeResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置触发边沿失败: {edgeResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置触发边沿失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "更新触发参数时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新采样参数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="horizontalShift">水平偏移量(0-1023)</param>
|
||||||
|
/// <param name="decimationRate">抽样率(0-1023)</param>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("UpdateSampling")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> UpdateSampling(ushort horizontalShift, ushort decimationRate)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (horizontalShift > 1023)
|
||||||
|
return BadRequest("水平偏移量必须在0-1023之间");
|
||||||
|
|
||||||
|
if (decimationRate > 1023)
|
||||||
|
return BadRequest("抽样率必须在0-1023之间");
|
||||||
|
|
||||||
|
var oscilloscope = GetOscilloscope();
|
||||||
|
if (oscilloscope == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
// 设置水平偏移量
|
||||||
|
var shiftResult = await oscilloscope.SetHorizontalShift(horizontalShift);
|
||||||
|
if (!shiftResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置水平偏移量失败: {shiftResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置水平偏移量失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置抽样率
|
||||||
|
var rateResult = await oscilloscope.SetDecimationRate(decimationRate);
|
||||||
|
if (!rateResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"设置抽样率失败: {rateResult.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "设置抽样率失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "更新采样参数时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 手动刷新RAM
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果</returns>
|
||||||
|
[HttpPost("RefreshRAM")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public async Task<IActionResult> RefreshRAM()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var oscilloscope = GetOscilloscope();
|
||||||
|
if (oscilloscope == null)
|
||||||
|
return BadRequest("用户未绑定有效的实验板");
|
||||||
|
|
||||||
|
var result = await oscilloscope.RefreshRAM();
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"刷新RAM失败: {result.Error}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "刷新RAM失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.Value);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "刷新RAM时发生异常");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
0
server/src/Controllers/ProgressController.cs
Normal file
0
server/src/Controllers/ProgressController.cs
Normal file
377
server/src/Controllers/ResourceController.cs
Normal file
377
server/src/Controllers/ResourceController.cs
Normal file
@@ -0,0 +1,377 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Cors;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using DotNext;
|
||||||
|
using Database;
|
||||||
|
|
||||||
|
namespace server.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源控制器 - 提供统一的资源管理API
|
||||||
|
/// </summary>
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
public class ResourceController : ControllerBase
|
||||||
|
{
|
||||||
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源信息类
|
||||||
|
/// </summary>
|
||||||
|
public class ResourceInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 资源ID
|
||||||
|
/// </summary>
|
||||||
|
public int ID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源名称
|
||||||
|
/// </summary>
|
||||||
|
public required string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源类型
|
||||||
|
/// </summary>
|
||||||
|
public required string Type { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源用途(template/user)
|
||||||
|
/// </summary>
|
||||||
|
public required string Purpose { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 上传时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime UploadTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 所属实验ID(可选)
|
||||||
|
/// </summary>
|
||||||
|
public string? ExamID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// MIME类型
|
||||||
|
/// </summary>
|
||||||
|
public string? MimeType { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 添加资源请求类
|
||||||
|
/// </summary>
|
||||||
|
public class AddResourceRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 资源类型
|
||||||
|
/// </summary>
|
||||||
|
public required string ResourceType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源用途(template/user)
|
||||||
|
/// </summary>
|
||||||
|
public required string ResourcePurpose { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 所属实验ID(可选)
|
||||||
|
/// </summary>
|
||||||
|
public string? ExamID { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 添加资源(文件上传)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request">添加资源请求</param>
|
||||||
|
/// <param name="file">资源文件</param>
|
||||||
|
/// <returns>添加结果</returns>
|
||||||
|
[Authorize]
|
||||||
|
[HttpPost]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(ResourceInfo), StatusCodes.Status201Created)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> AddResource([FromForm] AddResourceRequest request, IFormFile file)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(request.ResourceType) || string.IsNullOrWhiteSpace(request.ResourcePurpose) || file == null)
|
||||||
|
return BadRequest("资源类型、资源用途和文件不能为空");
|
||||||
|
|
||||||
|
// 验证资源用途
|
||||||
|
if (request.ResourcePurpose != Resource.ResourcePurposes.Template && request.ResourcePurpose != Resource.ResourcePurposes.User)
|
||||||
|
return BadRequest($"无效的资源用途: {request.ResourcePurpose}");
|
||||||
|
|
||||||
|
// 模板资源需要管理员权限
|
||||||
|
if (request.ResourcePurpose == Resource.ResourcePurposes.Template && !User.IsInRole("Admin"))
|
||||||
|
return Forbid("只有管理员可以添加模板资源");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
|
||||||
|
// 获取当前用户ID
|
||||||
|
var userName = User.Identity?.Name;
|
||||||
|
if (string.IsNullOrEmpty(userName))
|
||||||
|
return Unauthorized("无法获取用户信息");
|
||||||
|
|
||||||
|
var userResult = db.GetUserByName(userName);
|
||||||
|
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
|
||||||
|
return Unauthorized("用户不存在");
|
||||||
|
|
||||||
|
var user = userResult.Value.Value;
|
||||||
|
|
||||||
|
// 读取文件数据
|
||||||
|
using var memoryStream = new MemoryStream();
|
||||||
|
await file.CopyToAsync(memoryStream);
|
||||||
|
var fileData = memoryStream.ToArray();
|
||||||
|
|
||||||
|
var result = db.AddResource(user.ID, request.ResourceType, request.ResourcePurpose, file.FileName, fileData, request.ExamID);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
if (result.Error.Message.Contains("不存在"))
|
||||||
|
return NotFound(result.Error.Message);
|
||||||
|
|
||||||
|
logger.Error($"添加资源时出错: {result.Error.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"添加资源失败: {result.Error.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var resource = result.Value;
|
||||||
|
var resourceInfo = new ResourceInfo
|
||||||
|
{
|
||||||
|
ID = resource.ID,
|
||||||
|
Name = resource.ResourceName,
|
||||||
|
Type = resource.ResourceType,
|
||||||
|
Purpose = resource.ResourcePurpose,
|
||||||
|
UploadTime = resource.UploadTime,
|
||||||
|
ExamID = resource.ExamID,
|
||||||
|
MimeType = resource.MimeType
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.Info($"成功添加资源: {request.ResourceType}/{request.ResourcePurpose}/{file.FileName}");
|
||||||
|
return CreatedAtAction(nameof(GetResourceById), new { resourceId = resource.ID }, resourceInfo);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"添加资源 {request.ResourceType}/{request.ResourcePurpose}/{file.FileName} 时出错: {ex.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"添加资源失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取资源列表
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="examId">实验ID(可选)</param>
|
||||||
|
/// <param name="resourceType">资源类型(可选)</param>
|
||||||
|
/// <param name="resourcePurpose">资源用途(可选)</param>
|
||||||
|
/// <returns>资源列表</returns>
|
||||||
|
[Authorize]
|
||||||
|
[HttpGet]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(ResourceInfo[]), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public IActionResult GetResourceList([FromQuery] string? examId = null, [FromQuery] string? resourceType = null, [FromQuery] string? resourcePurpose = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
|
||||||
|
// 获取当前用户ID
|
||||||
|
var userName = User.Identity?.Name;
|
||||||
|
if (string.IsNullOrEmpty(userName))
|
||||||
|
return Unauthorized("无法获取用户信息");
|
||||||
|
|
||||||
|
var userResult = db.GetUserByName(userName);
|
||||||
|
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
|
||||||
|
return Unauthorized("用户不存在");
|
||||||
|
|
||||||
|
var user = userResult.Value.Value;
|
||||||
|
|
||||||
|
// 普通用户只能查看自己的资源和模板资源
|
||||||
|
Guid? userId = null;
|
||||||
|
if (!User.IsInRole("Admin"))
|
||||||
|
{
|
||||||
|
// 如果指定了用户资源用途,则只查看自己的资源
|
||||||
|
if (resourcePurpose == Resource.ResourcePurposes.User)
|
||||||
|
{
|
||||||
|
userId = user.ID;
|
||||||
|
}
|
||||||
|
// 如果指定了模板资源用途,则不限制用户ID
|
||||||
|
else if (resourcePurpose == Resource.ResourcePurposes.Template)
|
||||||
|
{
|
||||||
|
userId = null;
|
||||||
|
}
|
||||||
|
// 如果没有指定用途,则查看自己的用户资源和所有模板资源
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 这种情况下需要分别查询并合并结果
|
||||||
|
var userResourcesResult = db.GetFullResourceList(examId, resourceType, Resource.ResourcePurposes.User, user.ID);
|
||||||
|
var templateResourcesResult = db.GetFullResourceList(examId, resourceType, Resource.ResourcePurposes.Template, null);
|
||||||
|
|
||||||
|
if (!userResourcesResult.IsSuccessful || !templateResourcesResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取资源列表时出错");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, "获取资源列表失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
var allResources = userResourcesResult.Value.Concat(templateResourcesResult.Value)
|
||||||
|
.OrderByDescending(r => r.UploadTime);
|
||||||
|
var mergedResourceInfos = allResources.Select(r => new ResourceInfo
|
||||||
|
{
|
||||||
|
ID = r.ID,
|
||||||
|
Name = r.ResourceName,
|
||||||
|
Type = r.ResourceType,
|
||||||
|
Purpose = r.ResourcePurpose,
|
||||||
|
UploadTime = r.UploadTime,
|
||||||
|
ExamID = r.ExamID,
|
||||||
|
MimeType = r.MimeType
|
||||||
|
}).ToArray();
|
||||||
|
|
||||||
|
logger.Info($"成功获取资源列表,共 {mergedResourceInfos.Length} 个资源");
|
||||||
|
return Ok(mergedResourceInfos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = db.GetFullResourceList(examId, resourceType, resourcePurpose, userId);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取资源列表时出错: {result.Error.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源列表失败: {result.Error.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var resources = result.Value.Select(r => new ResourceInfo
|
||||||
|
{
|
||||||
|
ID = r.ID,
|
||||||
|
Name = r.ResourceName,
|
||||||
|
Type = r.ResourceType,
|
||||||
|
Purpose = r.ResourcePurpose,
|
||||||
|
UploadTime = r.UploadTime,
|
||||||
|
ExamID = r.ExamID,
|
||||||
|
MimeType = r.MimeType
|
||||||
|
}).ToArray();
|
||||||
|
|
||||||
|
logger.Info($"成功获取资源列表,共 {resources.Length} 个资源");
|
||||||
|
return Ok(resources);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"获取资源列表时出错: {ex.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源列表失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据资源ID下载资源
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="resourceId">资源ID</param>
|
||||||
|
/// <returns>资源文件</returns>
|
||||||
|
[HttpGet("{resourceId}")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public IActionResult GetResourceById(int resourceId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
var result = db.GetResourceById(resourceId);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取资源时出错: {result.Error.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源失败: {result.Error.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!result.Value.HasValue)
|
||||||
|
{
|
||||||
|
logger.Warn($"资源不存在: {resourceId}");
|
||||||
|
return NotFound($"资源 {resourceId} 不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
var resource = result.Value.Value;
|
||||||
|
logger.Info($"成功获取资源: {resourceId} ({resource.ResourceName})");
|
||||||
|
return File(resource.Data, resource.MimeType ?? "application/octet-stream", resource.ResourceName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"获取资源 {resourceId} 时出错: {ex.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除资源
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="resourceId">资源ID</param>
|
||||||
|
/// <returns>删除结果</returns>
|
||||||
|
[Authorize]
|
||||||
|
[HttpDelete("{resourceId}")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||||
|
public IActionResult DeleteResource(int resourceId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
|
||||||
|
// 获取当前用户信息
|
||||||
|
var userName = User.Identity?.Name;
|
||||||
|
if (string.IsNullOrEmpty(userName))
|
||||||
|
return Unauthorized("无法获取用户信息");
|
||||||
|
|
||||||
|
var userResult = db.GetUserByName(userName);
|
||||||
|
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
|
||||||
|
return Unauthorized("用户不存在");
|
||||||
|
|
||||||
|
var user = userResult.Value.Value;
|
||||||
|
|
||||||
|
// 先获取资源信息以验证权限
|
||||||
|
var resourceResult = db.GetResourceById(resourceId);
|
||||||
|
if (!resourceResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"获取资源时出错: {resourceResult.Error.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源失败: {resourceResult.Error.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!resourceResult.Value.HasValue)
|
||||||
|
{
|
||||||
|
logger.Warn($"资源不存在: {resourceId}");
|
||||||
|
return NotFound($"资源 {resourceId} 不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
var resource = resourceResult.Value.Value;
|
||||||
|
|
||||||
|
// 权限检查:管理员可以删除所有资源,普通用户只能删除自己的用户资源
|
||||||
|
if (!User.IsInRole("Admin"))
|
||||||
|
{
|
||||||
|
if (resource.ResourcePurpose == Resource.ResourcePurposes.Template)
|
||||||
|
return Forbid("普通用户不能删除模板资源");
|
||||||
|
|
||||||
|
if (resource.UserID != user.ID)
|
||||||
|
return Forbid("只能删除自己的资源");
|
||||||
|
}
|
||||||
|
|
||||||
|
var deleteResult = db.DeleteResource(resourceId);
|
||||||
|
if (!deleteResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"删除资源时出错: {deleteResult.Error.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"删除资源失败: {deleteResult.Error.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info($"成功删除资源: {resourceId} ({resource.ResourceName})");
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"删除资源 {resourceId} 时出错: {ex.Message}");
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, $"删除资源失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,45 @@ public class VideoStreamController : ControllerBase
|
|||||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
private readonly server.Services.HttpVideoStreamService _videoStreamService;
|
private readonly server.Services.HttpVideoStreamService _videoStreamService;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 视频流信息结构体
|
||||||
|
/// </summary>
|
||||||
|
public class StreamInfoResult
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// TODO:
|
||||||
|
/// </summary>
|
||||||
|
public int FrameRate { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// TODO:
|
||||||
|
/// </summary>
|
||||||
|
public int FrameWidth { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// TODO:
|
||||||
|
/// </summary>
|
||||||
|
public int FrameHeight { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// TODO:
|
||||||
|
/// </summary>
|
||||||
|
public string Format { get; set; } = "MJPEG";
|
||||||
|
/// <summary>
|
||||||
|
/// TODO:
|
||||||
|
/// </summary>
|
||||||
|
public string HtmlUrl { get; set; } = "";
|
||||||
|
/// <summary>
|
||||||
|
/// TODO:
|
||||||
|
/// </summary>
|
||||||
|
public string MjpegUrl { get; set; } = "";
|
||||||
|
/// <summary>
|
||||||
|
/// TODO:
|
||||||
|
/// </summary>
|
||||||
|
public string SnapshotUrl { get; set; } = "";
|
||||||
|
/// <summary>
|
||||||
|
/// TODO:
|
||||||
|
/// </summary>
|
||||||
|
public string UsbCameraUrl { get; set; } = "";
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 摄像头配置请求模型
|
/// 摄像头配置请求模型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -96,23 +135,25 @@ public class VideoStreamController : ControllerBase
|
|||||||
/// <returns>流信息</returns>
|
/// <returns>流信息</returns>
|
||||||
[HttpGet("StreamInfo")]
|
[HttpGet("StreamInfo")]
|
||||||
[EnableCors("Users")]
|
[EnableCors("Users")]
|
||||||
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
[ProducesResponseType(typeof(StreamInfoResult), StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||||
public IResult GetStreamInfo()
|
public IResult GetStreamInfo()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
logger.Info("获取 HTTP 视频流信息");
|
logger.Info("获取 HTTP 视频流信息");
|
||||||
return TypedResults.Ok(new
|
var result = new StreamInfoResult
|
||||||
{
|
{
|
||||||
frameRate = _videoStreamService.FrameRate,
|
FrameRate = _videoStreamService.FrameRate,
|
||||||
frameWidth = _videoStreamService.FrameWidth,
|
FrameWidth = _videoStreamService.FrameWidth,
|
||||||
frameHeight = _videoStreamService.FrameHeight,
|
FrameHeight = _videoStreamService.FrameHeight,
|
||||||
format = "MJPEG",
|
Format = "MJPEG",
|
||||||
htmlUrl = $"http://localhost:{_videoStreamService.ServerPort}/video-feed.html",
|
HtmlUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/video-feed.html",
|
||||||
mjpegUrl = $"http://localhost:{_videoStreamService.ServerPort}/video-stream",
|
MjpegUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/video-stream",
|
||||||
snapshotUrl = $"http://localhost:{_videoStreamService.ServerPort}/snapshot",
|
SnapshotUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/snapshot",
|
||||||
});
|
UsbCameraUrl = $"http://{Global.localhost}:{_videoStreamService.ServerPort}/usb-camera"
|
||||||
|
};
|
||||||
|
return TypedResults.Ok(result);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -226,7 +267,7 @@ public class VideoStreamController : ControllerBase
|
|||||||
using (var httpClient = new HttpClient())
|
using (var httpClient = new HttpClient())
|
||||||
{
|
{
|
||||||
httpClient.Timeout = TimeSpan.FromSeconds(2); // 设置较短的超时时间
|
httpClient.Timeout = TimeSpan.FromSeconds(2); // 设置较短的超时时间
|
||||||
var response = await httpClient.GetAsync($"http://localhost:{_videoStreamService.ServerPort}/");
|
var response = await httpClient.GetAsync($"http://{Global.localhost}:{_videoStreamService.ServerPort}/");
|
||||||
|
|
||||||
// 只要能连接上就认为成功,不管返回状态
|
// 只要能连接上就认为成功,不管返回状态
|
||||||
isConnected = response.IsSuccessStatusCode;
|
isConnected = response.IsSuccessStatusCode;
|
||||||
@@ -371,6 +412,10 @@ public class VideoStreamController : ControllerBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>初始化结果</returns>
|
/// <returns>初始化结果</returns>
|
||||||
[HttpPost("InitAutoFocus")]
|
[HttpPost("InitAutoFocus")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
|
||||||
public async Task<IResult> InitAutoFocus()
|
public async Task<IResult> InitAutoFocus()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -412,6 +457,10 @@ public class VideoStreamController : ControllerBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>对焦结果</returns>
|
/// <returns>对焦结果</returns>
|
||||||
[HttpPost("AutoFocus")]
|
[HttpPost("AutoFocus")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
|
||||||
public async Task<IResult> AutoFocus()
|
public async Task<IResult> AutoFocus()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -447,4 +496,61 @@ public class VideoStreamController : ControllerBase
|
|||||||
return TypedResults.InternalServerError($"执行自动对焦失败: {ex.Message}");
|
return TypedResults.InternalServerError($"执行自动对焦失败: {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行一次自动对焦 (GET方式)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>对焦结果</returns>
|
||||||
|
[HttpGet("Focus")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IResult> Focus()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
logger.Info("收到执行一次对焦请求 (GET)");
|
||||||
|
|
||||||
|
// 检查摄像头是否已配置
|
||||||
|
if (!_videoStreamService.IsCameraConfigured())
|
||||||
|
{
|
||||||
|
logger.Warn("摄像头未配置,无法执行对焦");
|
||||||
|
return TypedResults.BadRequest(new
|
||||||
|
{
|
||||||
|
success = false,
|
||||||
|
message = "摄像头未配置,请先配置摄像头连接",
|
||||||
|
timestamp = DateTime.Now
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = await _videoStreamService.PerformAutoFocusAsync();
|
||||||
|
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
logger.Info("对焦执行成功");
|
||||||
|
return TypedResults.Ok(new
|
||||||
|
{
|
||||||
|
success = true,
|
||||||
|
message = "对焦执行成功",
|
||||||
|
timestamp = DateTime.Now
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.Warn("对焦执行失败");
|
||||||
|
return TypedResults.BadRequest(new
|
||||||
|
{
|
||||||
|
success = false,
|
||||||
|
message = "对焦执行失败",
|
||||||
|
timestamp = DateTime.Now
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "执行对焦时发生异常");
|
||||||
|
return TypedResults.InternalServerError($"执行对焦失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,11 +92,17 @@ public class Board
|
|||||||
[NotNull]
|
[NotNull]
|
||||||
public required string IpAddr { get; set; }
|
public required string IpAddr { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// FPGA 板子的MAC地址
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public required string MacAddr { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// FPGA 板子的通信端口
|
/// FPGA 板子的通信端口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[NotNull]
|
[NotNull]
|
||||||
public required int Port { get; set; }
|
public int Port { get; set; } = 1234;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// FPGA 板子的当前状态
|
/// FPGA 板子的当前状态
|
||||||
@@ -127,6 +133,11 @@ public class Board
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public enum BoardStatus
|
public enum BoardStatus
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 未启用状态,无法被使用
|
||||||
|
/// </summary>
|
||||||
|
Disabled,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 繁忙状态,正在被用户使用
|
/// 繁忙状态,正在被用户使用
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -139,6 +150,191 @@ public class Board
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验类,表示实验信息
|
||||||
|
/// </summary>
|
||||||
|
public class Exam
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 实验的唯一标识符
|
||||||
|
/// </summary>
|
||||||
|
[PrimaryKey]
|
||||||
|
public required string ID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验名称
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public required string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验描述
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public required string Description { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验创建时间
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public DateTime CreatedTime { get; set; } = DateTime.Now;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验最后更新时间
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public DateTime UpdatedTime { get; set; } = DateTime.Now;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验标签(以逗号分隔的字符串)
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public string Tags { get; set; } = "";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验难度(1-5,1为最简单)
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public int Difficulty { get; set; } = 1;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 普通用户是否可见
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public bool IsVisibleToUsers { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取标签列表
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>标签数组</returns>
|
||||||
|
public string[] GetTagsList()
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(Tags))
|
||||||
|
return Array.Empty<string>();
|
||||||
|
|
||||||
|
return Tags.Split(',', StringSplitOptions.RemoveEmptyEntries)
|
||||||
|
.Select(tag => tag.Trim())
|
||||||
|
.Where(tag => !string.IsNullOrEmpty(tag))
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置标签列表
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tags">标签数组</param>
|
||||||
|
public void SetTagsList(string[] tags)
|
||||||
|
{
|
||||||
|
Tags = string.Join(",", tags.Where(tag => !string.IsNullOrWhiteSpace(tag)).Select(tag => tag.Trim()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源类,统一管理实验资源、用户比特流等各类资源
|
||||||
|
/// </summary>
|
||||||
|
public class Resource
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 资源的唯一标识符
|
||||||
|
/// </summary>
|
||||||
|
[PrimaryKey, Identity]
|
||||||
|
public int ID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 上传资源的用户ID
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public required Guid UserID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 所属实验ID(可选,如果不属于特定实验则为空)
|
||||||
|
/// </summary>
|
||||||
|
[Nullable]
|
||||||
|
public string? ExamID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源类型(images, markdown, bitstream, diagram, project等)
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public required string ResourceType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源用途:template(模板)或 user(用户上传)
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public required string ResourcePurpose { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源名称(包含文件扩展名)
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public required string ResourceName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源的二进制数据
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public required byte[] Data { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源创建/上传时间
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public DateTime UploadTime { get; set; } = DateTime.Now;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源的MIME类型
|
||||||
|
/// </summary>
|
||||||
|
[NotNull]
|
||||||
|
public string MimeType { get; set; } = "application/octet-stream";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源类型枚举
|
||||||
|
/// </summary>
|
||||||
|
public static class ResourceTypes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 图片资源类型
|
||||||
|
/// </summary>
|
||||||
|
public const string Images = "images";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Markdown文档资源类型
|
||||||
|
/// </summary>
|
||||||
|
public const string Markdown = "markdown";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 比特流文件资源类型
|
||||||
|
/// </summary>
|
||||||
|
public const string Bitstream = "bitstream";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 原理图资源类型
|
||||||
|
/// </summary>
|
||||||
|
public const string Diagram = "diagram";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 项目文件资源类型
|
||||||
|
/// </summary>
|
||||||
|
public const string Project = "project";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源用途枚举
|
||||||
|
/// </summary>
|
||||||
|
public static class ResourcePurposes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 模板资源,通常由管理员上传,供用户参考
|
||||||
|
/// </summary>
|
||||||
|
public const string Template = "template";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户上传的资源
|
||||||
|
/// </summary>
|
||||||
|
public const string User = "user";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 应用程序数据连接类,用于与数据库交互
|
/// 应用程序数据连接类,用于与数据库交互
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -186,6 +382,8 @@ public class AppDataConnection : DataConnection
|
|||||||
logger.Info("正在创建数据库表...");
|
logger.Info("正在创建数据库表...");
|
||||||
this.CreateTable<User>();
|
this.CreateTable<User>();
|
||||||
this.CreateTable<Board>();
|
this.CreateTable<Board>();
|
||||||
|
this.CreateTable<Exam>();
|
||||||
|
this.CreateTable<Resource>();
|
||||||
logger.Info("数据库表创建完成");
|
logger.Info("数据库表创建完成");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,6 +395,8 @@ public class AppDataConnection : DataConnection
|
|||||||
logger.Warn("正在删除所有数据库表...");
|
logger.Warn("正在删除所有数据库表...");
|
||||||
this.DropTable<User>();
|
this.DropTable<User>();
|
||||||
this.DropTable<Board>();
|
this.DropTable<Board>();
|
||||||
|
this.DropTable<Exam>();
|
||||||
|
this.DropTable<Resource>();
|
||||||
logger.Warn("所有数据库表已删除");
|
logger.Warn("所有数据库表已删除");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,25 +571,61 @@ public class AppDataConnection : DataConnection
|
|||||||
return userResult + boardResult;
|
return userResult + boardResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 自动分配一个未被占用的IP地址
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>分配的IP地址字符串</returns>
|
||||||
|
public string AllocateIpAddr()
|
||||||
|
{
|
||||||
|
var usedIps = this.BoardTable.Select(b => b.IpAddr).ToArray();
|
||||||
|
for (int i = 1; i <= 254; i++)
|
||||||
|
{
|
||||||
|
string ip = $"169.254.109.{i}";
|
||||||
|
if (!usedIps.Contains(ip))
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
throw new Exception("没有可用的IP地址");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 自动分配一个未被占用的MAC地址
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>分配的MAC地址字符串</returns>
|
||||||
|
public string AllocateMacAddr()
|
||||||
|
{
|
||||||
|
var usedMacs = this.BoardTable.Select(b => b.MacAddr).ToArray();
|
||||||
|
// 以 02-00-00-xx-xx-xx 格式分配,02 表示本地管理地址
|
||||||
|
for (int i = 1; i <= 0xFFFFFF; i++)
|
||||||
|
{
|
||||||
|
string mac = $"02-00-00-{(i >> 16) & 0xFF:X2}-{(i >> 8) & 0xFF:X2}-{i & 0xFF:X2}";
|
||||||
|
if (!usedMacs.Contains(mac))
|
||||||
|
return mac;
|
||||||
|
}
|
||||||
|
throw new Exception("没有可用的MAC地址");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加一块新的 FPGA 板子到数据库
|
/// 添加一块新的 FPGA 板子到数据库
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="name">FPGA 板子的名称</param>
|
/// <param name="name">FPGA 板子的名称</param>
|
||||||
/// <param name="ipAddr">FPGA 板子的IP地址</param>
|
|
||||||
/// <param name="port">FPGA 板子的通信端口</param>
|
|
||||||
/// <returns>插入的记录数</returns>
|
/// <returns>插入的记录数</returns>
|
||||||
public int AddBoard(string name, string ipAddr, int port)
|
public Guid AddBoard(string name)
|
||||||
{
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(name) || name.Contains('\'') || name.Contains(';'))
|
||||||
|
{
|
||||||
|
logger.Error("实验板名称非法,包含不允许的字符");
|
||||||
|
throw new ArgumentException("实验板名称非法");
|
||||||
|
}
|
||||||
var board = new Board()
|
var board = new Board()
|
||||||
{
|
{
|
||||||
BoardName = name,
|
BoardName = name,
|
||||||
IpAddr = ipAddr,
|
IpAddr = AllocateIpAddr(),
|
||||||
Port = port,
|
MacAddr = AllocateMacAddr(),
|
||||||
Status = Database.Board.BoardStatus.Available,
|
Status = Database.Board.BoardStatus.Disabled,
|
||||||
};
|
};
|
||||||
var result = this.Insert(board);
|
var result = this.Insert(board);
|
||||||
logger.Info($"新实验板已添加: {name} ({ipAddr}:{port})");
|
logger.Info($"新实验板已添加: {name} ({board.IpAddr}:{board.MacAddr})");
|
||||||
return result;
|
return board.ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -479,6 +715,31 @@ public class AppDataConnection : DataConnection
|
|||||||
return new(boards[0]);
|
return new(boards[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据用户名获取实验板信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userName">用户名</param>
|
||||||
|
/// <returns>包含实验板信息的结果,如果未找到则返回空</returns>
|
||||||
|
public Result<Optional<Board>> GetBoardByUserName(string userName)
|
||||||
|
{
|
||||||
|
var boards = this.BoardTable.Where(board => board.OccupiedUserName == userName).ToArray();
|
||||||
|
|
||||||
|
if (boards.Length > 1)
|
||||||
|
{
|
||||||
|
logger.Error($"数据库中存在多个相同用户名的实验板: {userName}");
|
||||||
|
return new(new Exception($"数据库中存在多个相同用户名的实验板: {userName}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (boards.Length == 0)
|
||||||
|
{
|
||||||
|
logger.Info($"未找到用户名对应的实验板: {userName}");
|
||||||
|
return new(Optional<Board>.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Debug($"成功获取实验板信息: {userName}");
|
||||||
|
return new(boards[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取所有实验板信息
|
/// 获取所有实验板信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -542,6 +803,43 @@ public class AppDataConnection : DataConnection
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="boardId">[TODO:parameter]</param>
|
||||||
|
/// <param name="newName">[TODO:parameter]</param>
|
||||||
|
/// <returns>[TODO:return]</returns>
|
||||||
|
public int UpdateBoardName(Guid boardId, string newName)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(newName) || newName.Contains('\'') || newName.Contains(';'))
|
||||||
|
{
|
||||||
|
logger.Error("实验板名称非法,包含不允许的字符");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
var result = this.BoardTable
|
||||||
|
.Where(b => b.ID == boardId)
|
||||||
|
.Set(b => b.BoardName, newName)
|
||||||
|
.Update();
|
||||||
|
logger.Info($"实验板名称已更新: {boardId} -> {newName}");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="boardId">[TODO:parameter]</param>
|
||||||
|
/// <param name="newStatus">[TODO:parameter]</param>
|
||||||
|
/// <returns>[TODO:return]</returns>
|
||||||
|
public int UpdateBoardStatus(Guid boardId, Board.BoardStatus newStatus)
|
||||||
|
{
|
||||||
|
var result = this.BoardTable
|
||||||
|
.Where(b => b.ID == boardId)
|
||||||
|
.Set(b => b.Status, newStatus)
|
||||||
|
.Update();
|
||||||
|
logger.Info($"TODO");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户表
|
/// 用户表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -551,4 +849,418 @@ public class AppDataConnection : DataConnection
|
|||||||
/// FPGA 板子表
|
/// FPGA 板子表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ITable<Board> BoardTable => this.GetTable<Board>();
|
public ITable<Board> BoardTable => this.GetTable<Board>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实验表
|
||||||
|
/// </summary>
|
||||||
|
public ITable<Exam> ExamTable => this.GetTable<Exam>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 资源表(统一管理实验资源、用户比特流等)
|
||||||
|
/// </summary>
|
||||||
|
public ITable<Resource> ResourceTable => this.GetTable<Resource>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建新实验
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">实验ID</param>
|
||||||
|
/// <param name="name">实验名称</param>
|
||||||
|
/// <param name="description">实验描述</param>
|
||||||
|
/// <param name="tags">实验标签</param>
|
||||||
|
/// <param name="difficulty">实验难度</param>
|
||||||
|
/// <param name="isVisibleToUsers">普通用户是否可见</param>
|
||||||
|
/// <returns>创建的实验</returns>
|
||||||
|
public Result<Exam> CreateExam(string id, string name, string description, string[]? tags = null, int difficulty = 1, bool isVisibleToUsers = true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 检查实验ID是否已存在
|
||||||
|
var existingExam = this.ExamTable.Where(e => e.ID == id).FirstOrDefault();
|
||||||
|
if (existingExam != null)
|
||||||
|
{
|
||||||
|
logger.Error($"实验ID已存在: {id}");
|
||||||
|
return new(new Exception($"实验ID已存在: {id}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
var exam = new Exam
|
||||||
|
{
|
||||||
|
ID = id,
|
||||||
|
Name = name,
|
||||||
|
Description = description,
|
||||||
|
Difficulty = Math.Max(1, Math.Min(5, difficulty)),
|
||||||
|
IsVisibleToUsers = isVisibleToUsers,
|
||||||
|
CreatedTime = DateTime.Now,
|
||||||
|
UpdatedTime = DateTime.Now
|
||||||
|
};
|
||||||
|
|
||||||
|
if (tags != null)
|
||||||
|
{
|
||||||
|
exam.SetTagsList(tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.Insert(exam);
|
||||||
|
logger.Info($"新实验已创建: {id} ({name})");
|
||||||
|
return new(exam);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"创建实验时出错: {ex.Message}");
|
||||||
|
return new(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新实验信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">实验ID</param>
|
||||||
|
/// <param name="name">实验名称</param>
|
||||||
|
/// <param name="description">实验描述</param>
|
||||||
|
/// <param name="tags">实验标签</param>
|
||||||
|
/// <param name="difficulty">实验难度</param>
|
||||||
|
/// <param name="isVisibleToUsers">普通用户是否可见</param>
|
||||||
|
/// <returns>更新的记录数</returns>
|
||||||
|
public Result<int> UpdateExam(string id, string? name = null, string? description = null, string[]? tags = null, int? difficulty = null, bool? isVisibleToUsers = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
if (name != null)
|
||||||
|
{
|
||||||
|
result += this.ExamTable.Where(e => e.ID == id).Set(e => e.Name, name).Update();
|
||||||
|
}
|
||||||
|
if (description != null)
|
||||||
|
{
|
||||||
|
result += this.ExamTable.Where(e => e.ID == id).Set(e => e.Description, description).Update();
|
||||||
|
}
|
||||||
|
if (tags != null)
|
||||||
|
{
|
||||||
|
var tagsString = string.Join(",", tags.Where(tag => !string.IsNullOrWhiteSpace(tag)).Select(tag => tag.Trim()));
|
||||||
|
result += this.ExamTable.Where(e => e.ID == id).Set(e => e.Tags, tagsString).Update();
|
||||||
|
}
|
||||||
|
if (difficulty.HasValue)
|
||||||
|
{
|
||||||
|
result += this.ExamTable.Where(e => e.ID == id).Set(e => e.Difficulty, Math.Max(1, Math.Min(5, difficulty.Value))).Update();
|
||||||
|
}
|
||||||
|
if (isVisibleToUsers.HasValue)
|
||||||
|
{
|
||||||
|
result += this.ExamTable.Where(e => e.ID == id).Set(e => e.IsVisibleToUsers, isVisibleToUsers.Value).Update();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新时间
|
||||||
|
this.ExamTable.Where(e => e.ID == id).Set(e => e.UpdatedTime, DateTime.Now).Update();
|
||||||
|
|
||||||
|
logger.Info($"实验已更新: {id},更新记录数: {result}");
|
||||||
|
return new(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"更新实验时出错: {ex.Message}");
|
||||||
|
return new(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 添加资源
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">上传用户ID</param>
|
||||||
|
/// <param name="resourceType">资源类型</param>
|
||||||
|
/// <param name="resourcePurpose">资源用途(template 或 user)</param>
|
||||||
|
/// <param name="resourceName">资源名称</param>
|
||||||
|
/// <param name="data">资源二进制数据</param>
|
||||||
|
/// <param name="examId">所属实验ID(可选)</param>
|
||||||
|
/// <param name="mimeType">MIME类型(可选,将根据文件扩展名自动确定)</param>
|
||||||
|
/// <returns>创建的资源</returns>
|
||||||
|
public Result<Resource> AddResource(Guid userId, string resourceType, string resourcePurpose, string resourceName, byte[] data, string? examId = null, string? mimeType = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 验证用户是否存在
|
||||||
|
var user = this.UserTable.Where(u => u.ID == userId).FirstOrDefault();
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
logger.Error($"用户不存在: {userId}");
|
||||||
|
return new(new Exception($"用户不存在: {userId}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果指定了实验ID,验证实验是否存在
|
||||||
|
if (!string.IsNullOrEmpty(examId))
|
||||||
|
{
|
||||||
|
var exam = this.ExamTable.Where(e => e.ID == examId).FirstOrDefault();
|
||||||
|
if (exam == null)
|
||||||
|
{
|
||||||
|
logger.Error($"实验不存在: {examId}");
|
||||||
|
return new(new Exception($"实验不存在: {examId}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证资源用途
|
||||||
|
if (resourcePurpose != Resource.ResourcePurposes.Template && resourcePurpose != Resource.ResourcePurposes.User)
|
||||||
|
{
|
||||||
|
logger.Error($"无效的资源用途: {resourcePurpose}");
|
||||||
|
return new(new Exception($"无效的资源用途: {resourcePurpose}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果未指定MIME类型,根据文件扩展名自动确定
|
||||||
|
if (string.IsNullOrEmpty(mimeType))
|
||||||
|
{
|
||||||
|
var extension = Path.GetExtension(resourceName).ToLowerInvariant();
|
||||||
|
mimeType = GetMimeTypeFromExtension(extension, resourceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
var resource = new Resource
|
||||||
|
{
|
||||||
|
UserID = userId,
|
||||||
|
ExamID = examId,
|
||||||
|
ResourceType = resourceType,
|
||||||
|
ResourcePurpose = resourcePurpose,
|
||||||
|
ResourceName = resourceName,
|
||||||
|
Data = data,
|
||||||
|
MimeType = mimeType,
|
||||||
|
UploadTime = DateTime.Now
|
||||||
|
};
|
||||||
|
|
||||||
|
var insertedId = this.InsertWithIdentity(resource);
|
||||||
|
resource.ID = Convert.ToInt32(insertedId);
|
||||||
|
|
||||||
|
logger.Info($"新资源已添加: {userId}/{resourceType}/{resourcePurpose}/{resourceName} ({data.Length} bytes)" +
|
||||||
|
(examId != null ? $" [实验: {examId}]" : "") + $" [ID: {resource.ID}]");
|
||||||
|
return new(resource);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"添加资源时出错: {ex.Message}");
|
||||||
|
return new(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取资源信息列表(返回ID和名称)
|
||||||
|
/// <param name="resourceType">资源类型</param>
|
||||||
|
/// <param name="examId">实验ID(可选)</param>
|
||||||
|
/// <param name="resourcePurpose">资源用途(可选)</param>
|
||||||
|
/// <param name="userId">用户ID(可选)</param>
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>资源信息列表</returns>
|
||||||
|
public Result<(int ID, string Name)[]> GetResourceList(string resourceType, string? examId = null, string? resourcePurpose = null, Guid? userId = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = this.ResourceTable.Where(r => r.ResourceType == resourceType);
|
||||||
|
|
||||||
|
if (examId != null)
|
||||||
|
{
|
||||||
|
query = query.Where(r => r.ExamID == examId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resourcePurpose != null)
|
||||||
|
{
|
||||||
|
query = query.Where(r => r.ResourcePurpose == resourcePurpose);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userId != null)
|
||||||
|
{
|
||||||
|
query = query.Where(r => r.UserID == userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
var resources = query
|
||||||
|
.Select(r => new { r.ID, r.ResourceName })
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
var result = resources.Select(r => (r.ID, r.ResourceName)).ToArray();
|
||||||
|
logger.Info($"获取资源列表: {resourceType}" +
|
||||||
|
(examId != null ? $"/{examId}" : "") +
|
||||||
|
(resourcePurpose != null ? $"/{resourcePurpose}" : "") +
|
||||||
|
(userId != null ? $"/{userId}" : "") +
|
||||||
|
$",共 {result.Length} 个资源");
|
||||||
|
return new(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"获取资源列表时出错: {ex.Message}");
|
||||||
|
return new(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取完整的资源列表
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="examId">实验ID(可选)</param>
|
||||||
|
/// <param name="resourceType">资源类型(可选)</param>
|
||||||
|
/// <param name="resourcePurpose">资源用途(可选)</param>
|
||||||
|
/// <param name="userId">用户ID(可选)</param>
|
||||||
|
/// <returns>完整的资源对象列表</returns>
|
||||||
|
public Result<List<Resource>> GetFullResourceList(string? examId = null, string? resourceType = null, string? resourcePurpose = null, Guid? userId = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = this.ResourceTable.AsQueryable();
|
||||||
|
|
||||||
|
if (examId != null)
|
||||||
|
{
|
||||||
|
query = query.Where(r => r.ExamID == examId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resourceType != null)
|
||||||
|
{
|
||||||
|
query = query.Where(r => r.ResourceType == resourceType);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resourcePurpose != null)
|
||||||
|
{
|
||||||
|
query = query.Where(r => r.ResourcePurpose == resourcePurpose);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userId != null)
|
||||||
|
{
|
||||||
|
query = query.Where(r => r.UserID == userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
var resources = query.OrderByDescending(r => r.UploadTime).ToList();
|
||||||
|
logger.Info($"获取完整资源列表" +
|
||||||
|
(examId != null ? $" [实验: {examId}]" : "") +
|
||||||
|
(resourceType != null ? $" [类型: {resourceType}]" : "") +
|
||||||
|
(resourcePurpose != null ? $" [用途: {resourcePurpose}]" : "") +
|
||||||
|
(userId != null ? $" [用户: {userId}]" : "") +
|
||||||
|
$",共 {resources.Count} 个资源");
|
||||||
|
return new(resources);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"获取完整资源列表时出错: {ex.Message}");
|
||||||
|
return new(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据资源ID获取资源
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="resourceId">资源ID</param>
|
||||||
|
/// <returns>资源数据</returns>
|
||||||
|
public Result<Optional<Resource>> GetResourceById(int resourceId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var resource = this.ResourceTable.Where(r => r.ID == resourceId).FirstOrDefault();
|
||||||
|
|
||||||
|
if (resource == null)
|
||||||
|
{
|
||||||
|
logger.Info($"未找到资源: {resourceId}");
|
||||||
|
return new(Optional<Resource>.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info($"成功获取资源: {resourceId} ({resource.ResourceName})");
|
||||||
|
return new(resource);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"获取资源时出错: {ex.Message}");
|
||||||
|
return new(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除资源
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="resourceId">资源ID</param>
|
||||||
|
/// <returns>删除的记录数</returns>
|
||||||
|
public Result<int> DeleteResource(int resourceId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = this.ResourceTable.Where(r => r.ID == resourceId).Delete();
|
||||||
|
logger.Info($"资源已删除: {resourceId},删除记录数: {result}");
|
||||||
|
return new(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"删除资源时出错: {ex.Message}");
|
||||||
|
return new(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据文件扩展名获取MIME类型
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extension">文件扩展名</param>
|
||||||
|
/// <param name="fileName">文件名(可选,用于特殊文件判断)</param>
|
||||||
|
/// <returns>MIME类型</returns>
|
||||||
|
private string GetMimeTypeFromExtension(string extension, string fileName = "")
|
||||||
|
{
|
||||||
|
// 特殊文件名处理
|
||||||
|
if (!string.IsNullOrEmpty(fileName) && fileName.Equals("diagram.json", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return "application/json";
|
||||||
|
}
|
||||||
|
|
||||||
|
return extension.ToLowerInvariant() switch
|
||||||
|
{
|
||||||
|
".png" => "image/png",
|
||||||
|
".jpg" or ".jpeg" => "image/jpeg",
|
||||||
|
".gif" => "image/gif",
|
||||||
|
".bmp" => "image/bmp",
|
||||||
|
".svg" => "image/svg+xml",
|
||||||
|
".sbit" => "application/octet-stream",
|
||||||
|
".bit" => "application/octet-stream",
|
||||||
|
".bin" => "application/octet-stream",
|
||||||
|
".json" => "application/json",
|
||||||
|
".zip" => "application/zip",
|
||||||
|
".md" => "text/markdown",
|
||||||
|
_ => "application/octet-stream"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有实验信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>所有实验的数组</returns>
|
||||||
|
public Exam[] GetAllExams()
|
||||||
|
{
|
||||||
|
var exams = this.ExamTable.OrderBy(e => e.ID).ToArray();
|
||||||
|
logger.Debug($"获取所有实验,共 {exams.Length} 个");
|
||||||
|
return exams;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据实验ID获取实验信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="examId">实验ID</param>
|
||||||
|
/// <returns>包含实验信息的结果,如果未找到则返回空</returns>
|
||||||
|
public Result<Optional<Exam>> GetExamByID(string examId)
|
||||||
|
{
|
||||||
|
var exams = this.ExamTable.Where(exam => exam.ID == examId).ToArray();
|
||||||
|
|
||||||
|
if (exams.Length > 1)
|
||||||
|
{
|
||||||
|
logger.Error($"数据库中存在多个相同ID的实验: {examId}");
|
||||||
|
return new(new Exception($"数据库中存在多个相同ID的实验: {examId}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exams.Length == 0)
|
||||||
|
{
|
||||||
|
logger.Info($"未找到ID对应的实验: {examId}");
|
||||||
|
return new(Optional<Exam>.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Debug($"成功获取实验信息: {examId}");
|
||||||
|
return new(exams[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据文件扩展名获取比特流MIME类型
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extension">文件扩展名</param>
|
||||||
|
/// <returns>MIME类型</returns>
|
||||||
|
private string GetBitstreamMimeType(string extension)
|
||||||
|
{
|
||||||
|
return extension.ToLowerInvariant() switch
|
||||||
|
{
|
||||||
|
".bit" => "application/octet-stream",
|
||||||
|
".sbit" => "application/octet-stream",
|
||||||
|
".bin" => "application/octet-stream",
|
||||||
|
".mcs" => "application/octet-stream",
|
||||||
|
".hex" => "text/plain",
|
||||||
|
_ => "application/octet-stream"
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
195
server/src/Hubs/JtagHub.cs
Normal file
195
server/src/Hubs/JtagHub.cs
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Cors;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using DotNext;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using TypedSignalR.Client;
|
||||||
|
using Tapper;
|
||||||
|
|
||||||
|
namespace server.Hubs;
|
||||||
|
|
||||||
|
[Hub]
|
||||||
|
public interface IJtagHub
|
||||||
|
{
|
||||||
|
Task<bool> SetBoundaryScanFreq(int freq);
|
||||||
|
Task<bool> StartBoundaryScan(int freq = 100);
|
||||||
|
Task<bool> StopBoundaryScan();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Receiver]
|
||||||
|
public interface IJtagReceiver
|
||||||
|
{
|
||||||
|
Task OnReceiveBoundaryScanData(Dictionary<string, bool> msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[EnableCors("SignalR")]
|
||||||
|
public class JtagHub : Hub<IJtagReceiver>, IJtagHub
|
||||||
|
{
|
||||||
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
private static ConcurrentDictionary<string, int> FreqTable = new();
|
||||||
|
private static ConcurrentDictionary<string, CancellationTokenSource> CancellationTokenSourceTable = new();
|
||||||
|
|
||||||
|
private readonly IHubContext<JtagHub, IJtagReceiver> _hubContext;
|
||||||
|
|
||||||
|
public JtagHub(IHubContext<JtagHub, IJtagReceiver> hubContext)
|
||||||
|
{
|
||||||
|
_hubContext = hubContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<Peripherals.JtagClient.Jtag> GetJtagClient(string userName)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var db = new Database.AppDataConnection();
|
||||||
|
var board = db.GetBoardByUserName(userName);
|
||||||
|
if (!board.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Find Board {board.Value.Value.ID} failed because {board.Error}");
|
||||||
|
return new(null);
|
||||||
|
}
|
||||||
|
if (!board.Value.HasValue)
|
||||||
|
{
|
||||||
|
logger.Error($"Board {board.Value.Value.ID} not found");
|
||||||
|
return new(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
var jtag = new Peripherals.JtagClient.Jtag(board.Value.Value.IpAddr, board.Value.Value.Port);
|
||||||
|
return new(jtag);
|
||||||
|
}
|
||||||
|
catch (Exception error)
|
||||||
|
{
|
||||||
|
logger.Error(error);
|
||||||
|
return new(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> SetBoundaryScanFreq(int freq)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var userName = Context.User?.FindFirstValue(ClaimTypes.Name);
|
||||||
|
if (userName is null)
|
||||||
|
{
|
||||||
|
logger.Error("Can't get user info");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
FreqTable.AddOrUpdate(userName, freq, (key, value) => freq);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception error)
|
||||||
|
{
|
||||||
|
logger.Error(error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> StartBoundaryScan(int freq = 100)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var userName = Context.User?.FindFirstValue(ClaimTypes.Name);
|
||||||
|
if (userName is null)
|
||||||
|
{
|
||||||
|
logger.Error("No Such User");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
await SetBoundaryScanFreq(freq);
|
||||||
|
var cts = new CancellationTokenSource();
|
||||||
|
CancellationTokenSourceTable.AddOrUpdate(userName, cts, (key, value) => cts);
|
||||||
|
|
||||||
|
_ = Task.Run(
|
||||||
|
() => BoundaryScanLogicPorts(Context.ConnectionId, userName, cts.Token),
|
||||||
|
cts.Token)
|
||||||
|
.ContinueWith((task) =>
|
||||||
|
{
|
||||||
|
if (task.IsFaulted)
|
||||||
|
{
|
||||||
|
// 遍历所有异常
|
||||||
|
foreach (var ex in task.Exception.InnerExceptions)
|
||||||
|
{
|
||||||
|
if (ex is OperationCanceledException)
|
||||||
|
{
|
||||||
|
logger.Info($"Boundary scan operation cancelled for user {userName}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.Error($"Boundary scan operation failed for user {userName}: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (task.IsCanceled)
|
||||||
|
{
|
||||||
|
logger.Info($"Boundary scan operation cancelled for user {userName}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.Info($"Boundary scan completed successfully for user {userName}");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.Info($"Boundary scan started for user {userName}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception error)
|
||||||
|
{
|
||||||
|
logger.Error(error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> StopBoundaryScan()
|
||||||
|
{
|
||||||
|
var userName = Context.User?.FindFirstValue(ClaimTypes.Name);
|
||||||
|
if (userName is null)
|
||||||
|
{
|
||||||
|
logger.Error("No Such User");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!CancellationTokenSourceTable.TryGetValue(userName, out var cts))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
cts.Cancel();
|
||||||
|
cts.Token.WaitHandle.WaitOne();
|
||||||
|
|
||||||
|
logger.Info($"Boundary scan stopped for user {userName}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task BoundaryScanLogicPorts(string connectionID, string userName, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var jtagCtrl = GetJtagClient(userName).OrThrow(() => new InvalidOperationException("JTAG client not found"));
|
||||||
|
var cntFail = 0;
|
||||||
|
|
||||||
|
while (true && cntFail < 5)
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
var ret = await jtagCtrl.BoundaryScanLogicalPorts();
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"User {userName} boundary scan failed for device {jtagCtrl.address}: {ret.Error}");
|
||||||
|
cntFail++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _hubContext.Clients.Client(connectionID).OnReceiveBoundaryScanData(ret.Value);
|
||||||
|
// logger.Info($"User {userName} successfully completed boundary scan for device {jtagCtrl.address}");
|
||||||
|
|
||||||
|
await Task.Delay(FreqTable.TryGetValue(userName, out var freq) ? 1000 / freq : 1000 / 100, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cntFail >= 5)
|
||||||
|
{
|
||||||
|
logger.Error($"User {userName} boundary scan failed for device {jtagCtrl.address} after 5 attempts");
|
||||||
|
throw new InvalidOperationException("Boundary scan failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
61
server/src/Hubs/ProgressHub.cs
Normal file
61
server/src/Hubs/ProgressHub.cs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using Microsoft.AspNetCore.Cors;
|
||||||
|
using TypedSignalR.Client;
|
||||||
|
using Tapper;
|
||||||
|
using server.Services;
|
||||||
|
|
||||||
|
namespace server.Hubs;
|
||||||
|
|
||||||
|
[Hub]
|
||||||
|
public interface IProgressHub
|
||||||
|
{
|
||||||
|
Task<bool> Join(string taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Receiver]
|
||||||
|
public interface IProgressReceiver
|
||||||
|
{
|
||||||
|
Task OnReceiveProgress(ProgressInfo message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TranspilationSource]
|
||||||
|
public enum ProgressStatus
|
||||||
|
{
|
||||||
|
Pending,
|
||||||
|
InProgress,
|
||||||
|
Completed,
|
||||||
|
Canceled,
|
||||||
|
Failed
|
||||||
|
}
|
||||||
|
|
||||||
|
[TranspilationSource]
|
||||||
|
public class ProgressInfo
|
||||||
|
{
|
||||||
|
public string TaskId { get; }
|
||||||
|
public ProgressStatus Status { get; }
|
||||||
|
public int ProgressPercent { get; }
|
||||||
|
public string ErrorMessage { get; }
|
||||||
|
};
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[EnableCors("SignalR")]
|
||||||
|
public class ProgressHub : Hub<IProgressReceiver>, IProgressHub
|
||||||
|
{
|
||||||
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
private readonly IHubContext<ProgressHub, IProgressReceiver> _hubContext;
|
||||||
|
private readonly ProgressTrackerService _tracker;
|
||||||
|
|
||||||
|
public ProgressHub(IHubContext<ProgressHub, IProgressReceiver> hubContext, ProgressTrackerService tracker)
|
||||||
|
{
|
||||||
|
_hubContext = hubContext;
|
||||||
|
_tracker = tracker;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> Join(string taskId)
|
||||||
|
{
|
||||||
|
return _tracker.BindTask(taskId, Context.ConnectionId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,9 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class MsgBus
|
public static class MsgBus
|
||||||
{
|
{
|
||||||
private static readonly UDPServer udpServer = new UDPServer(1234);
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
private static readonly UDPServer udpServer = new UDPServer(1234, 12);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取UDP服务器
|
/// 获取UDP服务器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -19,8 +21,13 @@ public static class MsgBus
|
|||||||
/// 通信总线初始化
|
/// 通信总线初始化
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>无</returns>
|
/// <returns>无</returns>
|
||||||
public static void Init()
|
public async static void Init()
|
||||||
{
|
{
|
||||||
|
if (!ArpClient.IsAdministrator())
|
||||||
|
{
|
||||||
|
logger.Error($"非管理员运行,ARP无法更新,请用管理员权限运行");
|
||||||
|
// throw new Exception($"非管理员运行,ARP无法更新,请用管理员权限运行");
|
||||||
|
}
|
||||||
udpServer.Start();
|
udpServer.Start();
|
||||||
isRunning = true;
|
isRunning = true;
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -108,7 +108,7 @@ public class DDS
|
|||||||
if (waveNum < 0 || waveNum > 3) return new(new ArgumentException(
|
if (waveNum < 0 || waveNum > 3) return new(new ArgumentException(
|
||||||
$"Wave number should be 0 ~ 3 instead of {waveNum}", nameof(waveNum)));
|
$"Wave number should be 0 ~ 3 instead of {waveNum}", nameof(waveNum)));
|
||||||
|
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||||
logger.Trace("Clear udp data finished");
|
logger.Trace("Clear udp data finished");
|
||||||
|
|
||||||
var ret = await UDPClientPool.WriteAddr(
|
var ret = await UDPClientPool.WriteAddr(
|
||||||
@@ -132,7 +132,7 @@ public class DDS
|
|||||||
if (waveNum < 0 || waveNum > 3) return new(new ArgumentException(
|
if (waveNum < 0 || waveNum > 3) return new(new ArgumentException(
|
||||||
$"Wave number should be 0 ~ 3 instead of {waveNum}", nameof(waveNum)));
|
$"Wave number should be 0 ~ 3 instead of {waveNum}", nameof(waveNum)));
|
||||||
|
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||||
logger.Trace("Clear udp data finished");
|
logger.Trace("Clear udp data finished");
|
||||||
|
|
||||||
var ret = await UDPClientPool.WriteAddr(
|
var ret = await UDPClientPool.WriteAddr(
|
||||||
@@ -158,7 +158,7 @@ public class DDS
|
|||||||
if (phase < 0 || phase > 4096) return new(new ArgumentException(
|
if (phase < 0 || phase > 4096) return new(new ArgumentException(
|
||||||
$"Phase should be 0 ~ 4096 instead of {phase}", nameof(phase)));
|
$"Phase should be 0 ~ 4096 instead of {phase}", nameof(phase)));
|
||||||
|
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||||
logger.Trace("Clear udp data finished");
|
logger.Trace("Clear udp data finished");
|
||||||
|
|
||||||
var ret = await UDPClientPool.WriteAddr(
|
var ret = await UDPClientPool.WriteAddr(
|
||||||
|
|||||||
254
server/src/Peripherals/DebuggerClient.cs
Normal file
254
server/src/Peripherals/DebuggerClient.cs
Normal file
@@ -0,0 +1,254 @@
|
|||||||
|
using System.Net;
|
||||||
|
using DotNext;
|
||||||
|
|
||||||
|
namespace Peripherals.DebuggerClient;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// FPGA调试器的内存地址映射常量
|
||||||
|
/// </summary>
|
||||||
|
class DebuggerAddr
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 触发器启动地址
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 Start = 0x5100_0000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 刷新操作地址
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 Fresh = 0x5100_FFFF;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 信号标志读取地址
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 Signal = 0x5000_0001;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 数据读取基地址
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 Data = 0x5100_0000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 捕获模式设置地址
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 Mode = 0x5101_0000;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// FPGA调试器命令常量
|
||||||
|
/// </summary>
|
||||||
|
class DebuggerCmd
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 启动触发器命令
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 Start = 0xFFFF_FFFF;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 刷新命令
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 Fresh = 0x0000_0000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 清除信号标志命令
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 ClearSignal = 0xFFFF_FFFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 信号捕获模式枚举
|
||||||
|
/// </summary>
|
||||||
|
public enum CaptureMode : byte
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 无捕获模式
|
||||||
|
/// </summary>
|
||||||
|
None = 0,
|
||||||
|
/// <summary>
|
||||||
|
/// 低电平触发模式
|
||||||
|
/// </summary>
|
||||||
|
Logic0 = 1,
|
||||||
|
/// <summary>
|
||||||
|
/// 高电平触发模式
|
||||||
|
/// </summary>
|
||||||
|
Logic1 = 2,
|
||||||
|
/// <summary>
|
||||||
|
/// 上升沿触发模式
|
||||||
|
/// </summary>
|
||||||
|
Rise = 3,
|
||||||
|
/// <summary>
|
||||||
|
/// 下降沿触发模式
|
||||||
|
/// </summary>
|
||||||
|
Fall = 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// FPGA调试器客户端,用于通过UDP协议与FPGA调试器进行通信
|
||||||
|
/// </summary>
|
||||||
|
public class DebuggerClient
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
|
private UInt32 captureDataAddr = 0x5100_0000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化FPGA调试器客户端
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">FPGA设备的IP地址</param>
|
||||||
|
/// <param name="port">通信端口号</param>
|
||||||
|
/// <param name="taskID">任务标识符</param>
|
||||||
|
/// <param name="timeout">通信超时时间(毫秒),默认2000ms</param>
|
||||||
|
/// <exception cref="ArgumentException">当timeout为负数时抛出</exception>
|
||||||
|
public DebuggerClient(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>
|
||||||
|
/// 设置信号捕获模式
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="wireNum">要设置的线</param>
|
||||||
|
/// <param name="mode">要设置的捕获模式</param>
|
||||||
|
/// <returns>操作结果,成功返回true,失败返回错误信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetMode(UInt32 wireNum, CaptureMode mode)
|
||||||
|
{
|
||||||
|
if (wireNum > 512)
|
||||||
|
{
|
||||||
|
return new(new ArgumentException($"Wire Num can't be over 512, but receive num: {wireNum}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
UInt32 data = ((UInt32)mode);
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, DebuggerAddr.Mode + wireNum, data, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set mode: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to SetMode returned false");
|
||||||
|
return new(new Exception("Failed to set mode"));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启动信号触发器开始捕获
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果,成功返回true,失败返回错误信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> StartTrigger()
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, DebuggerAddr.Start, DebuggerCmd.Start, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to start trigger: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to StartTrigger returned false");
|
||||||
|
return new(new Exception("Failed to start trigger"));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 读取触发器状态标志
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果,成功返回状态标志字节,失败返回错误信息</returns>
|
||||||
|
public async ValueTask<Result<byte>> ReadFlag()
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, DebuggerAddr.Signal, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to read flag: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 1)
|
||||||
|
{
|
||||||
|
logger.Error("ReadAddr returned invalid data for flag");
|
||||||
|
return new(new Exception("Failed to read flag"));
|
||||||
|
}
|
||||||
|
return ret.Value.Options.Data[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 清除触发器状态标志
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果,成功返回true,失败返回错误信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> ClearFlag()
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, DebuggerAddr.Signal, DebuggerCmd.ClearSignal, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to clear flag: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to ClearFlag returned false");
|
||||||
|
return new(new Exception("Failed to clear flag"));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从指定偏移地址读取捕获的数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="portNum">Port数量</param>
|
||||||
|
/// <returns>操作结果,成功返回捕获数据,失败返回错误信息</returns>
|
||||||
|
public async ValueTask<Result<byte[]>> ReadData(UInt32 portNum)
|
||||||
|
{
|
||||||
|
var captureData = new byte[1024 * 4 * portNum];
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.ReadAddr4Bytes(this.ep, this.taskID, this.captureDataAddr, captureData.Length / 4, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to read data: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret.Value.Length != captureData.Length)
|
||||||
|
{
|
||||||
|
logger.Error($"Receive capture data length should be {captureData.Length} instead of {ret.Value.Length}");
|
||||||
|
return new(new Exception($"Receive capture data length should be {captureData.Length} instead of {ret.Value.Length}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Buffer.BlockCopy(ret.Value, 0, captureData, 0, captureData.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
return captureData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 刷新调试器状态,重置内部状态机
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果,成功返回true,失败返回错误信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> Refresh()
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, DebuggerAddr.Fresh, DebuggerCmd.Fresh, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to refresh: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to Refresh returned false");
|
||||||
|
return new(new Exception("Failed to refresh"));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
120
server/src/Peripherals/HdmiInClient.cs
Normal file
120
server/src/Peripherals/HdmiInClient.cs
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
using System.Net;
|
||||||
|
using DotNext;
|
||||||
|
using Peripherals.PowerClient;
|
||||||
|
using WebProtocol;
|
||||||
|
|
||||||
|
namespace Peripherals.HdmiInClient;
|
||||||
|
|
||||||
|
static class HdmiInAddr
|
||||||
|
{
|
||||||
|
public const UInt32 BASE = 0xA000_0000;
|
||||||
|
public const UInt32 HdmiIn_CTRL = BASE + 0x0; //[0]: rstn, 0 is reset.
|
||||||
|
public const UInt32 HdmiIn_READFIFO = BASE + 0x1;
|
||||||
|
}
|
||||||
|
|
||||||
|
class HdmiIn
|
||||||
|
{
|
||||||
|
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
readonly int timeout = 500;
|
||||||
|
readonly int taskID;
|
||||||
|
readonly int port;
|
||||||
|
readonly string address;
|
||||||
|
private IPEndPoint ep;
|
||||||
|
|
||||||
|
// 动态分辨率参数
|
||||||
|
private UInt16 _currentWidth = 960;
|
||||||
|
private UInt16 _currentHeight = 540;
|
||||||
|
private UInt32 _currentFrameLength = 960 * 540 * 2 / 4; // RGB565格式,2字节/像素,按4字节对齐
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化HDMI输入客户端
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">HDMI输入设备IP地址</param>
|
||||||
|
/// <param name="port">HDMI输入设备端口</param>
|
||||||
|
/// <param name="taskID">任务ID</param>
|
||||||
|
/// <param name="timeout">超时时间(毫秒)</param>
|
||||||
|
public HdmiIn(string address, int port, int taskID, int timeout = 500)
|
||||||
|
{
|
||||||
|
if (timeout < 0)
|
||||||
|
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
|
||||||
|
this.address = address;
|
||||||
|
this.port = port;
|
||||||
|
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
|
||||||
|
this.taskID = taskID;
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<Result<bool>> EnableTrans(bool isEnable)
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, HdmiInAddr.HdmiIn_CTRL, (isEnable ? 0x00000001u : 0x00000000u));
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to write HdmiIn_CTRL to HdmiIn at {this.address}:{this.port}, error: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error($"HdmiIn_CTRL write returned false for HdmiIn at {this.address}:{this.port}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 读取一帧图像数据
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>包含图像数据的字节数组</returns>
|
||||||
|
public async ValueTask<Result<byte[]>> ReadFrame()
|
||||||
|
{
|
||||||
|
// 只在第一次或出错时清除UDP缓冲区,避免每帧都清除造成延迟
|
||||||
|
// MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
|
logger.Trace($"Reading frame from HdmiIn {this.address}");
|
||||||
|
|
||||||
|
// 使用UDPClientPool读取图像帧数据
|
||||||
|
var result = await UDPClientPool.ReadAddr4BytesAsync(
|
||||||
|
this.ep,
|
||||||
|
this.taskID, // taskID
|
||||||
|
HdmiInAddr.HdmiIn_READFIFO,
|
||||||
|
(int)_currentFrameLength, // 使用当前分辨率的动态大小
|
||||||
|
BurstType.FixedBurst,
|
||||||
|
this.timeout);
|
||||||
|
|
||||||
|
if (!result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to read frame from HdmiIn {this.address}:{this.port}, error: {result.Error}");
|
||||||
|
// 读取失败时清除缓冲区,为下次读取做准备
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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 HdmiIn {this.address}:{this.port}, data length: {result.Value.Length} bytes");
|
||||||
|
return result.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前分辨率
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>当前分辨率(宽度, 高度)</returns>
|
||||||
|
public (int Width, int Height) GetCurrentResolution()
|
||||||
|
{
|
||||||
|
return (_currentWidth, _currentHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前帧长度
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>当前帧长度</returns>
|
||||||
|
public UInt32 GetCurrentFrameLength()
|
||||||
|
{
|
||||||
|
return _currentFrameLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,12 +37,12 @@ static class I2cAddr
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 0x0000_0004: FIFO读出口,仅低8位有效,只读
|
/// 0x0000_0004: FIFO读出口,仅低8位有效,只读
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const UInt32 Read = Base + 0x0000_0003;
|
public const UInt32 Read = Base + 0x0000_0004;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 0x0000_0005: [0] FIFO写入口清空;[8] FIFO读出口清空;
|
/// 0x0000_0005: [0] FIFO写入口清空;[8] FIFO读出口清空;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const UInt32 Clear = Base + 0x0000_0003;
|
public const UInt32 Clear = Base + 0x0000_0005;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -82,7 +82,7 @@ public class I2c
|
|||||||
/// <param name="taskID">[TODO:parameter]</param>
|
/// <param name="taskID">[TODO:parameter]</param>
|
||||||
/// <param name="timeout">[TODO:parameter]</param>
|
/// <param name="timeout">[TODO:parameter]</param>
|
||||||
/// <returns>[TODO:return]</returns>
|
/// <returns>[TODO:return]</returns>
|
||||||
public I2c(string address, int port, int taskID,int timeout = 2000)
|
public I2c(string address, int port, int taskID, int timeout = 2000)
|
||||||
{
|
{
|
||||||
if (timeout < 0)
|
if (timeout < 0)
|
||||||
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
|
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
|
||||||
@@ -109,7 +109,7 @@ public class I2c
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 清除UDP服务器接收缓冲区
|
// 清除UDP服务器接收缓冲区
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
logger.Trace($"Clear up udp server {this.address} receive data");
|
logger.Trace($"Clear up udp server {this.address} receive data");
|
||||||
|
|
||||||
@@ -174,7 +174,7 @@ public class I2c
|
|||||||
|
|
||||||
// 等待I2C命令完成
|
// 等待I2C命令完成
|
||||||
{
|
{
|
||||||
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, this.taskID, I2cAddr.Flag, 0x0000_0001, 0xFFFF_FFFF);
|
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, this.taskID, I2cAddr.Flag, 0x0000_0001, 0xFFFF_FFFF, 10);
|
||||||
if (!ret.IsSuccessful)
|
if (!ret.IsSuccessful)
|
||||||
{
|
{
|
||||||
logger.Error($"Failed to wait for I2C command completion: {ret.Error}");
|
logger.Error($"Failed to wait for I2C command completion: {ret.Error}");
|
||||||
@@ -195,25 +195,59 @@ public class I2c
|
|||||||
/// 从指定I2C设备读取数据
|
/// 从指定I2C设备读取数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="devAddr">I2C设备地址</param>
|
/// <param name="devAddr">I2C设备地址</param>
|
||||||
/// <param name="length">要读取的数据长度</param>
|
/// <param name="data">要写入的数据(dummy数据)</param>
|
||||||
|
/// <param name="dataReadLength">要读取的数据长度</param>
|
||||||
/// <param name="proto">I2C协议类型</param>
|
/// <param name="proto">I2C协议类型</param>
|
||||||
/// <returns>操作结果,成功返回读取到的数据,否则返回异常信息</returns>
|
/// <returns>操作结果,成功返回读取到的数据,否则返回异常信息</returns>
|
||||||
public async ValueTask<Result<byte[]>> ReadData(UInt32 devAddr, int length, I2cProtocol proto)
|
public async ValueTask<Result<byte[]>> ReadData(UInt32 devAddr, byte[] data, int dataReadLength, I2cProtocol proto)
|
||||||
{
|
{
|
||||||
if (length <= 0 || length > 0x0000_FFFF)
|
if (dataReadLength < 1 || dataReadLength > 0x0000_FFFF)
|
||||||
{
|
{
|
||||||
logger.Error($"Read length {length} is invalid or exceeds maximum allowed 0x0000_FFFF");
|
logger.Error($"Read length {dataReadLength} is invalid or exceeds maximum allowed 0x0000_FFFF");
|
||||||
return new(new ArgumentException($"Read length {length} is invalid or exceeds maximum allowed 0x0000_FFFF"));
|
return new(new ArgumentException($"Read length {dataReadLength} is invalid or exceeds maximum allowed 0x0000_FFFF"));
|
||||||
|
}
|
||||||
|
|
||||||
|
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服务器接收缓冲区
|
// 清除UDP服务器接收缓冲区
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
logger.Trace($"Clear up udp server {this.address} receive data");
|
logger.Trace($"Clear up udp server {this.address} receive data");
|
||||||
|
|
||||||
// 配置本次传输数据量
|
// 配置写FIFO内容,内容为data[]
|
||||||
{
|
{
|
||||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, I2cAddr.TranConfig, ((uint)(length - 1)));
|
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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 配置本次传输数据量:[15:0]为读长度(length-1),[31:16]为dummy长度(data.Length-1)
|
||||||
|
{
|
||||||
|
uint tranConfig = ((uint)(dataReadLength - 1)) | (((uint)(data.Length - 1)) << 16);
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, I2cAddr.TranConfig, tranConfig);
|
||||||
if (!ret.IsSuccessful)
|
if (!ret.IsSuccessful)
|
||||||
{
|
{
|
||||||
logger.Error($"Failed to configure transfer length: {ret.Error}");
|
logger.Error($"Failed to configure transfer length: {ret.Error}");
|
||||||
@@ -246,7 +280,7 @@ public class I2c
|
|||||||
|
|
||||||
// 等待I2C命令完成
|
// 等待I2C命令完成
|
||||||
{
|
{
|
||||||
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, this.taskID, I2cAddr.Flag, 0x0000_0001, 0xFFFF_FFFF);
|
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, this.taskID, I2cAddr.Flag, 0x0000_0001, 0xFFFF_FFFF, 10);
|
||||||
if (!ret.IsSuccessful)
|
if (!ret.IsSuccessful)
|
||||||
{
|
{
|
||||||
logger.Error($"Failed to wait for I2C command completion: {ret.Error}");
|
logger.Error($"Failed to wait for I2C command completion: {ret.Error}");
|
||||||
@@ -262,20 +296,20 @@ public class I2c
|
|||||||
|
|
||||||
// 读取数据
|
// 读取数据
|
||||||
{
|
{
|
||||||
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, I2cAddr.Read, length);
|
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, I2cAddr.Read);
|
||||||
if (!ret.IsSuccessful)
|
if (!ret.IsSuccessful)
|
||||||
{
|
{
|
||||||
logger.Error($"Failed to read data from I2C FIFO: {ret.Error}");
|
logger.Error($"Failed to read data from I2C FIFO: {ret.Error}");
|
||||||
return new(ret.Error);
|
return new(ret.Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length != length)
|
if (ret.Value.Options.Data == null)
|
||||||
{
|
{
|
||||||
logger.Error($"ReadAddr returned unexpected data length: {ret.Value.Options.Data?.Length ?? 0}");
|
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 new(new Exception("Failed to read expected amount of data from I2C FIFO"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret.Value.Options.Data;
|
return ret.Value.Options.Data[3..]; // 返回读取到的数据,跳过前3个字节
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ using System.Collections;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using DotNext;
|
using DotNext;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using server;
|
using server.Services;
|
||||||
using WebProtocol;
|
using WebProtocol;
|
||||||
|
|
||||||
namespace Peripherals.JtagClient;
|
namespace Peripherals.JtagClient;
|
||||||
@@ -386,7 +386,10 @@ public class Jtag
|
|||||||
readonly int timeout;
|
readonly int timeout;
|
||||||
|
|
||||||
readonly int port;
|
readonly int port;
|
||||||
readonly string address;
|
/// <summary>
|
||||||
|
/// Jtag控制器IP地址
|
||||||
|
/// </summary>
|
||||||
|
public readonly string address;
|
||||||
private IPEndPoint ep;
|
private IPEndPoint ep;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -406,15 +409,17 @@ public class Jtag
|
|||||||
async ValueTask<Result<uint>> ReadFIFO(uint devAddr)
|
async ValueTask<Result<uint>> ReadFIFO(uint devAddr)
|
||||||
{
|
{
|
||||||
var ret = false;
|
var ret = false;
|
||||||
var opts = new SendAddrPackOptions();
|
var opts = new SendAddrPackOptions()
|
||||||
|
{
|
||||||
|
BurstType = BurstType.FixedBurst,
|
||||||
|
BurstLength = 0,
|
||||||
|
CommandID = 0,
|
||||||
|
Address = devAddr,
|
||||||
|
IsWrite = false,
|
||||||
|
};
|
||||||
|
|
||||||
opts.BurstType = BurstType.FixedBurst;
|
|
||||||
opts.BurstLength = 0;
|
|
||||||
opts.CommandID = 0;
|
|
||||||
opts.Address = devAddr;
|
|
||||||
|
|
||||||
// Read Jtag State Register
|
// Read Jtag State Register
|
||||||
opts.IsWrite = false;
|
|
||||||
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
|
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
|
||||||
if (!ret) return new(new Exception("Send Address Package Failed!"));
|
if (!ret) return new(new Exception("Send Address Package Failed!"));
|
||||||
|
|
||||||
@@ -434,14 +439,15 @@ public class Jtag
|
|||||||
if (retPackLen != 4)
|
if (retPackLen != 4)
|
||||||
return new(new Exception($"RecvDataPackage BodyData Length not Equal to 4: Total {retPackLen} bytes"));
|
return new(new Exception($"RecvDataPackage BodyData Length not Equal to 4: Total {retPackLen} bytes"));
|
||||||
|
|
||||||
return Convert.ToUInt32(Common.Number.BytesToUInt64(retPackOpts.Data).Value);
|
return Convert.ToUInt32(Common.Number.BytesToUInt32(retPackOpts.Data).Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
async ValueTask<Result<bool>> WriteFIFO
|
async ValueTask<Result<bool>> WriteFIFO(
|
||||||
(UInt32 devAddr, UInt32 data, UInt32 result, UInt32 resultMask = 0xFF_FF_FF_FF, UInt32 delayMilliseconds = 0)
|
UInt32 devAddr, UInt32 data, UInt32 result,
|
||||||
|
UInt32 resultMask = 0xFF_FF_FF_FF, UInt32 delayMilliseconds = 0, ProgressReporter? progress = null)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
var ret = await UDPClientPool.WriteAddr(this.ep, 0, devAddr, data, this.timeout);
|
var ret = await UDPClientPool.WriteAddr(this.ep, 0, devAddr, data, this.timeout, progress?.CreateChild(80));
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
if (!ret.Value) return new(new Exception("Write FIFO failed"));
|
if (!ret.Value) return new(new Exception("Write FIFO failed"));
|
||||||
}
|
}
|
||||||
@@ -450,17 +456,19 @@ public class Jtag
|
|||||||
await Task.Delay(TimeSpan.FromMilliseconds(delayMilliseconds));
|
await Task.Delay(TimeSpan.FromMilliseconds(delayMilliseconds));
|
||||||
|
|
||||||
{
|
{
|
||||||
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, 0, JtagAddr.STATE, result, resultMask, this.timeout);
|
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, 0, JtagAddr.STATE, result, resultMask, 0, this.timeout);
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
|
progress?.Finish();
|
||||||
return ret.Value;
|
return ret.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async ValueTask<Result<bool>> WriteFIFO
|
async ValueTask<Result<bool>> WriteFIFO(
|
||||||
(UInt32 devAddr, byte[] data, UInt32 result, UInt32 resultMask = 0xFF_FF_FF_FF, UInt32 delayMilliseconds = 0)
|
UInt32 devAddr, byte[] data, UInt32 result,
|
||||||
|
UInt32 resultMask = 0xFF_FF_FF_FF, UInt32 delayMilliseconds = 0, ProgressReporter? progress = null)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
var ret = await UDPClientPool.WriteAddr(this.ep, 0, devAddr, data, this.timeout);
|
var ret = await UDPClientPool.WriteAddr(this.ep, 0, devAddr, data, this.timeout, progress?.CreateChild(80));
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
if (!ret.Value) return new(new Exception("Write FIFO failed"));
|
if (!ret.Value) return new(new Exception("Write FIFO failed"));
|
||||||
}
|
}
|
||||||
@@ -469,8 +477,9 @@ public class Jtag
|
|||||||
await Task.Delay(TimeSpan.FromMilliseconds(delayMilliseconds));
|
await Task.Delay(TimeSpan.FromMilliseconds(delayMilliseconds));
|
||||||
|
|
||||||
{
|
{
|
||||||
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, 0, JtagAddr.STATE, result, resultMask, this.timeout);
|
var ret = await UDPClientPool.ReadAddrWithWait(this.ep, 0, JtagAddr.STATE, result, resultMask, 0, this.timeout);
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
|
progress?.Finish();
|
||||||
return ret.Value;
|
return ret.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -554,7 +563,8 @@ public class Jtag
|
|||||||
return await ClearWriteDataReg();
|
return await ClearWriteDataReg();
|
||||||
}
|
}
|
||||||
|
|
||||||
async ValueTask<Result<bool>> LoadDRCareInput(byte[] bytesArray, UInt32 timeout = 10_000, UInt32 cycle = 500)
|
async ValueTask<Result<bool>> LoadDRCareInput(
|
||||||
|
byte[] bytesArray, UInt32 timeout = 10_000, UInt32 cycle = 500, ProgressReporter? progress = null)
|
||||||
{
|
{
|
||||||
var bytesLen = ((uint)(bytesArray.Length * 8));
|
var bytesLen = ((uint)(bytesArray.Length * 8));
|
||||||
if (bytesLen > Math.Pow(2, 28)) return new(new Exception("Length is over 2^(28 - 3)"));
|
if (bytesLen > Math.Pow(2, 28)) return new(new Exception("Length is over 2^(28 - 3)"));
|
||||||
@@ -569,11 +579,15 @@ public class Jtag
|
|||||||
else if (!ret.Value) return new(new Exception("Write CMD_JTAG_LOAD_DR_CAREI Failed"));
|
else if (!ret.Value) return new(new Exception("Write CMD_JTAG_LOAD_DR_CAREI Failed"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
progress?.Report(10);
|
||||||
|
|
||||||
{
|
{
|
||||||
var ret = await WriteFIFO(
|
var ret = await WriteFIFO(
|
||||||
JtagAddr.WRITE_DATA,
|
JtagAddr.WRITE_DATA,
|
||||||
bytesArray, 0x01_00_00_00,
|
bytesArray, 0x01_00_00_00,
|
||||||
JtagState.CMD_EXEC_FINISH);
|
JtagState.CMD_EXEC_FINISH,
|
||||||
|
progress: progress?.CreateChild(90)
|
||||||
|
);
|
||||||
|
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
return ret.Value;
|
return ret.Value;
|
||||||
@@ -607,13 +621,10 @@ public class Jtag
|
|||||||
if (ret.Value)
|
if (ret.Value)
|
||||||
{
|
{
|
||||||
var array = new UInt32[UInt32Num];
|
var array = new UInt32[UInt32Num];
|
||||||
for (int i = 0; i < UInt32Num; i++)
|
var retData = await UDPClientPool.ReadAddr4Bytes(ep, 0, JtagAddr.READ_DATA, (int)UInt32Num);
|
||||||
{
|
|
||||||
var retData = await ReadFIFO(JtagAddr.READ_DATA);
|
|
||||||
if (!retData.IsSuccessful)
|
if (!retData.IsSuccessful)
|
||||||
return new(new Exception("Read FIFO failed when Load DR"));
|
return new(new Exception("Read FIFO failed when Load DR"));
|
||||||
array[i] = retData.Value;
|
Buffer.BlockCopy(retData.Value, 0, array, 0, (int)UInt32Num * 4);
|
||||||
}
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -627,7 +638,7 @@ public class Jtag
|
|||||||
public async ValueTask<Result<uint>> ReadIDCode()
|
public async ValueTask<Result<uint>> ReadIDCode()
|
||||||
{
|
{
|
||||||
// Clear Data
|
// Clear Data
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||||
|
|
||||||
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
||||||
|
|
||||||
@@ -665,7 +676,7 @@ public class Jtag
|
|||||||
public async ValueTask<Result<uint>> ReadStatusReg()
|
public async ValueTask<Result<uint>> ReadStatusReg()
|
||||||
{
|
{
|
||||||
// Clear Data
|
// Clear Data
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||||
|
|
||||||
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
||||||
|
|
||||||
@@ -699,44 +710,55 @@ public class Jtag
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="bitstream">比特流数据</param>
|
/// <param name="bitstream">比特流数据</param>
|
||||||
/// <returns>指示下载是否成功的异步结果</returns>
|
/// <returns>指示下载是否成功的异步结果</returns>
|
||||||
public async ValueTask<Result<bool>> DownloadBitstream(byte[] bitstream)
|
public async ValueTask<Result<bool>> DownloadBitstream(byte[] bitstream, ProgressReporter? progress = null)
|
||||||
{
|
{
|
||||||
// Clear Data
|
// Clear Data
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||||
|
|
||||||
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
||||||
|
if (progress != null)
|
||||||
|
{
|
||||||
|
progress.ExpectedSteps = 25;
|
||||||
|
progress.Increase();
|
||||||
|
}
|
||||||
|
|
||||||
Result<bool> ret;
|
Result<bool> ret;
|
||||||
|
|
||||||
ret = await CloseTest();
|
ret = await CloseTest();
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
ret = await RunTest();
|
ret = await RunTest();
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
logger.Trace("Jtag initialize");
|
logger.Trace("Jtag initialize");
|
||||||
|
|
||||||
ret = await ExecRDCmd(JtagCmd.JTAG_DR_JRST);
|
ret = await ExecRDCmd(JtagCmd.JTAG_DR_JRST);
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_JRST Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_JRST Failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
ret = await RunTest();
|
ret = await RunTest();
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
ret = await ExecRDCmd(JtagCmd.JTAG_DR_CFGI);
|
ret = await ExecRDCmd(JtagCmd.JTAG_DR_CFGI);
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_CFGI Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_CFGI Failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
logger.Trace("Jtag ready to write bitstream");
|
logger.Trace("Jtag ready to write bitstream");
|
||||||
|
|
||||||
ret = await IdleDelay(100000);
|
ret = await IdleDelay(100000);
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
ret = await LoadDRCareInput(bitstream);
|
ret = await LoadDRCareInput(bitstream, progress: progress?.CreateChild(50));
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag Load Data Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag Load Data Failed"));
|
||||||
|
|
||||||
@@ -745,32 +767,40 @@ public class Jtag
|
|||||||
ret = await CloseTest();
|
ret = await CloseTest();
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
ret = await RunTest();
|
ret = await RunTest();
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
ret = await ExecRDCmd(JtagCmd.JTAG_DR_JWAKEUP);
|
ret = await ExecRDCmd(JtagCmd.JTAG_DR_JWAKEUP);
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_JWAKEUP Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_JWAKEUP Failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
logger.Trace("Jtag reset device");
|
logger.Trace("Jtag reset device");
|
||||||
|
|
||||||
ret = await IdleDelay(10000);
|
ret = await IdleDelay(10000);
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
var retCode = await ReadStatusReg();
|
var retCode = await ReadStatusReg();
|
||||||
if (!retCode.IsSuccessful) return new(retCode.Error);
|
if (!retCode.IsSuccessful) return new(retCode.Error);
|
||||||
var jtagStatus = new JtagStatusReg(retCode.Value);
|
var jtagStatus = new JtagStatusReg(retCode.Value);
|
||||||
if (!(jtagStatus.done && jtagStatus.wakeup_over && jtagStatus.init_complete))
|
if (!(jtagStatus.done && jtagStatus.wakeup_over && jtagStatus.init_complete))
|
||||||
return new(new Exception("Jtag download bitstream failed"));
|
return new(new Exception("Jtag download bitstream failed"));
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
ret = await CloseTest();
|
ret = await CloseTest();
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
|
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
|
||||||
logger.Trace("Jtag download bitstream successfully");
|
logger.Trace("Jtag download bitstream successfully");
|
||||||
|
progress?.Increase();
|
||||||
|
|
||||||
|
// Finish
|
||||||
|
progress?.Finish();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -783,10 +813,10 @@ public class Jtag
|
|||||||
{
|
{
|
||||||
var paser = new BsdlParser.Parser();
|
var paser = new BsdlParser.Parser();
|
||||||
var portNum = paser.GetBoundaryRegsNum().Value;
|
var portNum = paser.GetBoundaryRegsNum().Value;
|
||||||
logger.Debug($"Get boundar scan registers number: {portNum}");
|
logger.Debug($"Get boundary scan registers number: {portNum}");
|
||||||
|
|
||||||
// Clear Data
|
// Clear Data
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||||
|
|
||||||
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
||||||
|
|
||||||
@@ -853,7 +883,7 @@ public class Jtag
|
|||||||
public async ValueTask<Result<bool>> SetSpeed(UInt32 speed)
|
public async ValueTask<Result<bool>> SetSpeed(UInt32 speed)
|
||||||
{
|
{
|
||||||
// Clear Data
|
// Clear Data
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||||
|
|
||||||
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
logger.Trace($"Clear up udp server {this.address,0} receive data");
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using Common;
|
||||||
using DotNext;
|
using DotNext;
|
||||||
|
using WebProtocol;
|
||||||
|
|
||||||
namespace Peripherals.LogicAnalyzerClient;
|
namespace Peripherals.LogicAnalyzerClient;
|
||||||
|
|
||||||
static class AnalyzerAddr
|
static class AnalyzerAddr
|
||||||
{
|
{
|
||||||
const UInt32 BASE = 0x9000_0000;
|
const UInt32 BASE = 0x9000_0000;
|
||||||
|
const UInt32 DMA1_BASE = 0x7000_0000;
|
||||||
|
const UInt32 DDR_BASE = 0x0000_0000;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 0x0000_0000 R/W [ 0] capture on: 置1开始等待捕获,0停止捕获。捕获到信号后该位自动清零。 <br/>
|
/// 0x0000_0000 R/W [ 0] capture on: 置1开始等待捕获,0停止捕获。捕获到信号后该位自动清零。 <br/>
|
||||||
@@ -23,17 +27,17 @@ static class AnalyzerAddr
|
|||||||
/// 10: 全局非与(~&) <br/>
|
/// 10: 全局非与(~&) <br/>
|
||||||
/// 11: 全局非或(~|) <br/>
|
/// 11: 全局非或(~|) <br/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const UInt32 GLOBAL_TRIG_MODE = BASE + 0x0000_0000;
|
public const UInt32 GLOBAL_TRIG_MODE = BASE + 0x0000_0001;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 0x0000_0010 - 0x0000_0017 R/W [5:0] 信号M的触发操作符,共8路 <br/>
|
/// 0x0000_0010 - 0x0000_0017 R/W [5:0] 信号M的触发操作符,共8路 <br/>
|
||||||
/// [2:0] M's Operator: 000 == <br/>
|
/// [5:3] M's Operator: 000 == <br/>
|
||||||
/// 001 != <br/>
|
/// 001 != <br/>
|
||||||
/// 010 < <br/>
|
/// 010 < <br/>
|
||||||
/// 011 <= <br/>
|
/// 011 <= <br/>
|
||||||
/// 100 > <br/>
|
/// 100 > <br/>
|
||||||
/// 101 >= <br/>
|
/// 101 >= <br/>
|
||||||
/// [5:3] M's Value: 000 LOGIC 0 <br/>
|
/// [2:0] M's Value: 000 LOGIC 0 <br/>
|
||||||
/// 001 LOGIC 1 <br/>
|
/// 001 LOGIC 1 <br/>
|
||||||
/// 010 X(not care) <br/>
|
/// 010 X(not care) <br/>
|
||||||
/// 011 RISE <br/>
|
/// 011 RISE <br/>
|
||||||
@@ -43,22 +47,38 @@ static class AnalyzerAddr
|
|||||||
/// 111 SOME NUMBER <br/>
|
/// 111 SOME NUMBER <br/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly UInt32[] SIGNAL_TRIG_MODE = {
|
public static readonly UInt32[] SIGNAL_TRIG_MODE = {
|
||||||
BASE + 0x0000_0010,
|
BASE + 0x0000_0010, BASE + 0x0000_0011,
|
||||||
BASE + 0x0000_0011,
|
BASE + 0x0000_0012, BASE + 0x0000_0013,
|
||||||
BASE + 0x0000_0012,
|
BASE + 0x0000_0014, BASE + 0x0000_0015,
|
||||||
BASE + 0x0000_0013,
|
BASE + 0x0000_0016, BASE + 0x0000_0017,
|
||||||
BASE + 0x0000_0014,
|
BASE + 0x0000_0018, BASE + 0x0000_0019,
|
||||||
BASE + 0x0000_0015,
|
BASE + 0x0000_001A, BASE + 0x0000_001B,
|
||||||
BASE + 0x0000_0016,
|
BASE + 0x0000_001C, BASE + 0x0000_001D,
|
||||||
BASE + 0x0000_0017,
|
BASE + 0x0000_001E, BASE + 0x0000_001F,
|
||||||
|
BASE + 0x0000_0020, BASE + 0x0000_0021,
|
||||||
|
BASE + 0x0000_0022, BASE + 0x0000_0023,
|
||||||
|
BASE + 0x0000_0024, BASE + 0x0000_0025,
|
||||||
|
BASE + 0x0000_0026, BASE + 0x0000_0027,
|
||||||
|
BASE + 0x0000_0028, BASE + 0x0000_0029,
|
||||||
|
BASE + 0x0000_002A, BASE + 0x0000_002B,
|
||||||
|
BASE + 0x0000_002C, BASE + 0x0000_002D,
|
||||||
|
BASE + 0x0000_002E, BASE + 0x0000_002F
|
||||||
};
|
};
|
||||||
|
public const UInt32 LOAD_NUM_ADDR = BASE + 0x0000_0002;
|
||||||
|
public const UInt32 PRE_LOAD_NUM_ADDR = BASE + 0x0000_0003;
|
||||||
|
public const UInt32 CAHNNEL_DIV_ADDR = BASE + 0x0000_0004;
|
||||||
|
public const UInt32 CLOCK_DIV_ADDR = BASE + 0x0000_0005;
|
||||||
|
public const UInt32 DMA1_START_WRITE_ADDR = DMA1_BASE + 0x0000_0012;
|
||||||
|
public const UInt32 DMA1_END_WRITE_ADDR = DMA1_BASE + 0x0000_0013;
|
||||||
|
public const UInt32 DMA1_CAPTURE_CTRL_ADDR = DMA1_BASE + 0x0000_0014;
|
||||||
|
public const UInt32 STORE_OFFSET_ADDR = DDR_BASE + 0x0100_0000;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 0x0100_0000 - 0x0100_03FF 只读 32位波形存储,得到的32位数据中低八位最先捕获,高八位最后捕获。<br/>
|
/// 0x0100_0000 - 0x0100_03FF 只读 32位波形存储,得到的32位数据中低八位最先捕获,高八位最后捕获。<br/>
|
||||||
/// 共1024个地址,每个地址存储4组,深度为4096。<br/>
|
/// 共1024个地址,每个地址存储4组,深度为4096。<br/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const UInt32 CAPTURE_DATA_ADDR = BASE + 0x0100_0000;
|
|
||||||
public const Int32 CAPTURE_DATA_LENGTH = 1024;
|
public const Int32 CAPTURE_DATA_LENGTH = 1024;
|
||||||
|
public const Int32 CAPTURE_DATA_PRELOAD = 512;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -119,6 +139,52 @@ public enum GlobalCaptureMode
|
|||||||
NOR = 0b11
|
NOR = 0b11
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 逻辑分析仪采样时钟分频系数
|
||||||
|
/// </summary>
|
||||||
|
public enum AnalyzerClockDiv
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 1分频
|
||||||
|
/// </summary>
|
||||||
|
DIV1 = 0x0000_0000,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 2分频
|
||||||
|
/// </summary>
|
||||||
|
DIV2 = 0x0000_0001,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 4分频
|
||||||
|
/// </summary>
|
||||||
|
DIV4 = 0x0000_0002,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 8分频
|
||||||
|
/// </summary>
|
||||||
|
DIV8 = 0x0000_0003,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 16分频
|
||||||
|
/// </summary>
|
||||||
|
DIV16 = 0x0000_0004,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 32分频
|
||||||
|
/// </summary>
|
||||||
|
DIV32 = 0x0000_0005,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 64分频
|
||||||
|
/// </summary>
|
||||||
|
DIV64 = 0x0000_0006,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 128分频
|
||||||
|
/// </summary>
|
||||||
|
DIV128 = 0x0000_0007
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 信号M的操作符枚举
|
/// 信号M的操作符枚举
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -189,6 +255,37 @@ public enum SignalValue : byte
|
|||||||
SomeNumber = 0b111 // SOME NUMBER
|
SomeNumber = 0b111 // SOME NUMBER
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 逻辑分析仪有效通道数
|
||||||
|
/// </summary>
|
||||||
|
public enum AnalyzerChannelDiv
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 1路
|
||||||
|
/// </summary>
|
||||||
|
ONE = 0x0000_0000,
|
||||||
|
/// <summary>
|
||||||
|
/// 2路
|
||||||
|
/// </summary>
|
||||||
|
TWO = 0x0000_0001,
|
||||||
|
/// <summary>
|
||||||
|
/// 4路
|
||||||
|
/// </summary>
|
||||||
|
FOUR = 0x0000_0002,
|
||||||
|
/// <summary>
|
||||||
|
/// 8路
|
||||||
|
/// </summary>
|
||||||
|
EIGHT = 0x0000_0003,
|
||||||
|
/// <summary>
|
||||||
|
/// 16路
|
||||||
|
/// </summary>
|
||||||
|
XVI = 0x0000_0004,
|
||||||
|
/// <summary>
|
||||||
|
/// 32路
|
||||||
|
/// </summary>
|
||||||
|
XXXII = 0x0000_0005
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// FPGA逻辑分析仪客户端,用于控制FPGA上的逻辑分析仪模块进行信号捕获和分析
|
/// FPGA逻辑分析仪客户端,用于控制FPGA上的逻辑分析仪模块进行信号捕获和分析
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -233,8 +330,21 @@ public class Analyzer
|
|||||||
// 构造寄存器值
|
// 构造寄存器值
|
||||||
UInt32 value = 0;
|
UInt32 value = 0;
|
||||||
if (captureOn) value |= 1 << 0;
|
if (captureOn) value |= 1 << 0;
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, AnalyzerAddr.DMA1_CAPTURE_CTRL_ADDR, value, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set DMA1_CAPTURE_CTRL_ADDR: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to DMA1_CAPTURE_CTRL_ADDR returned false");
|
||||||
|
return new(new Exception("Failed to set DMA1_CAPTURE_CTRL_ADDR"));
|
||||||
|
}
|
||||||
|
}
|
||||||
if (force) value |= 1 << 8;
|
if (force) value |= 1 << 8;
|
||||||
|
{
|
||||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, AnalyzerAddr.CAPTURE_MODE, value, this.timeout);
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, AnalyzerAddr.CAPTURE_MODE, value, this.timeout);
|
||||||
if (!ret.IsSuccessful)
|
if (!ret.IsSuccessful)
|
||||||
{
|
{
|
||||||
@@ -246,6 +356,7 @@ public class Analyzer
|
|||||||
logger.Error("WriteAddr to CAPTURE_MODE returned false");
|
logger.Error("WriteAddr to CAPTURE_MODE returned false");
|
||||||
return new(new Exception("Failed to set capture mode"));
|
return new(new Exception("Failed to set capture mode"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,7 +377,7 @@ public class Analyzer
|
|||||||
logger.Error("ReadAddr returned invalid data for capture status");
|
logger.Error("ReadAddr returned invalid data for capture status");
|
||||||
return new(new Exception("Failed to read capture status"));
|
return new(new Exception("Failed to read capture status"));
|
||||||
}
|
}
|
||||||
UInt32 status = BitConverter.ToUInt32(ret.Value.Options.Data, 0);
|
UInt32 status = Number.BytesToUInt32(ret.Value.Options.Data).Value;
|
||||||
return (CaptureStatus)status;
|
return (CaptureStatus)status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,8 +415,8 @@ public class Analyzer
|
|||||||
if (signalIndex < 0 || signalIndex >= AnalyzerAddr.SIGNAL_TRIG_MODE.Length)
|
if (signalIndex < 0 || signalIndex >= AnalyzerAddr.SIGNAL_TRIG_MODE.Length)
|
||||||
return new(new ArgumentException($"Signal index must be 0~{AnalyzerAddr.SIGNAL_TRIG_MODE.Length}"));
|
return new(new ArgumentException($"Signal index must be 0~{AnalyzerAddr.SIGNAL_TRIG_MODE.Length}"));
|
||||||
|
|
||||||
// 计算模式值: [2:0] 操作符, [5:3] 信号值
|
// 计算模式值: [2:0] 信号值, [5:3] 操作符
|
||||||
UInt32 mode = ((UInt32)val << 3) | (UInt32)op;
|
UInt32 mode = ((UInt32)op << 3) | (UInt32)val;
|
||||||
|
|
||||||
var addr = AnalyzerAddr.SIGNAL_TRIG_MODE[signalIndex];
|
var addr = AnalyzerAddr.SIGNAL_TRIG_MODE[signalIndex];
|
||||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, addr, mode, this.timeout);
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, addr, mode, this.timeout);
|
||||||
@@ -322,17 +433,111 @@ public class Analyzer
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置逻辑分析仪的深度、预采样深度、有效通道、分频系数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="capture_length">深度</param>
|
||||||
|
/// <param name="pre_capture_length">预采样深度</param>
|
||||||
|
/// <param name="channel_div">有效通道(0-[1],1-[2],2-[4],3-[8],4-[16],5-[32])</param>
|
||||||
|
/// <param name="clock_div">采样时钟分频系数</param>
|
||||||
|
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetCaptureParams(int capture_length, int pre_capture_length, AnalyzerChannelDiv channel_div, AnalyzerClockDiv clock_div)
|
||||||
|
{
|
||||||
|
if (capture_length == 0) capture_length = 1;
|
||||||
|
if (pre_capture_length == 0) pre_capture_length = 1;
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, AnalyzerAddr.LOAD_NUM_ADDR, (UInt32)(capture_length - 1), this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set LOAD_NUM_ADDR: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to LOAD_NUM_ADDR returned false");
|
||||||
|
return new(new Exception("Failed to set LOAD_NUM_ADDR"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, AnalyzerAddr.PRE_LOAD_NUM_ADDR, (UInt32)(pre_capture_length - 1), this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set PRE_LOAD_NUM_ADDR: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to PRE_LOAD_NUM_ADDR returned false");
|
||||||
|
return new(new Exception("Failed to set PRE_LOAD_NUM_ADDR"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, AnalyzerAddr.DMA1_START_WRITE_ADDR, AnalyzerAddr.STORE_OFFSET_ADDR, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set DMA1_START_WRITE_ADDR: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to DMA1_START_WRITE_ADDR returned false");
|
||||||
|
return new(new Exception("Failed to set DMA1_START_WRITE_ADDR"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, AnalyzerAddr.DMA1_END_WRITE_ADDR, AnalyzerAddr.STORE_OFFSET_ADDR + (UInt32)(capture_length - 1), this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set DMA1_END_WRITE_ADDR: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to DMA1_END_WRITE_ADDR returned false");
|
||||||
|
return new(new Exception("Failed to set DMA1_END_WRITE_ADDR"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, AnalyzerAddr.CAHNNEL_DIV_ADDR, (UInt32)channel_div, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set CAHNNEL_DIV_ADDR: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to CAHNNEL_DIV_ADDR returned false");
|
||||||
|
return new(new Exception("Failed to set CAHNNEL_DIV_ADDR"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, AnalyzerAddr.CLOCK_DIV_ADDR, (UInt32)clock_div, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set CLOCK_DIV_ADDR: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to CLOCK_DIV_ADDR returned false");
|
||||||
|
return new(new Exception("Failed to set CLOCK_DIV_ADDR"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 读取捕获的波形数据
|
/// 读取捕获的波形数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>操作结果,成功返回byte[],否则返回异常信息</returns>
|
/// <returns>操作结果,成功返回byte[],否则返回异常信息</returns>
|
||||||
public async ValueTask<Result<byte[]>> ReadCaptureData()
|
public async ValueTask<Result<byte[]>> ReadCaptureData(int capture_length = 2048 * 32)
|
||||||
{
|
{
|
||||||
var ret = await UDPClientPool.ReadAddr4BytesAsync(
|
var ret = await UDPClientPool.ReadAddr4BytesAsync(
|
||||||
this.ep,
|
this.ep,
|
||||||
this.taskID,
|
this.taskID,
|
||||||
AnalyzerAddr.CAPTURE_DATA_ADDR,
|
AnalyzerAddr.STORE_OFFSET_ADDR,
|
||||||
AnalyzerAddr.CAPTURE_DATA_LENGTH,
|
capture_length,
|
||||||
|
BurstType.ExtendBurst, // 使用扩展突发读取
|
||||||
this.timeout
|
this.timeout
|
||||||
);
|
);
|
||||||
if (!ret.IsSuccessful)
|
if (!ret.IsSuccessful)
|
||||||
@@ -341,7 +546,7 @@ public class Analyzer
|
|||||||
return new(ret.Error);
|
return new(ret.Error);
|
||||||
}
|
}
|
||||||
var data = ret.Value;
|
var data = ret.Value;
|
||||||
if (data == null || data.Length != AnalyzerAddr.CAPTURE_DATA_LENGTH * 4)
|
if (data == null || data.Length != capture_length * 4)
|
||||||
{
|
{
|
||||||
logger.Error($"Capture data length mismatch: {data?.Length}");
|
logger.Error($"Capture data length mismatch: {data?.Length}");
|
||||||
return new(new Exception("Capture data length mismatch"));
|
return new(new Exception("Capture data length mismatch"));
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public class MatrixKey
|
|||||||
public async ValueTask<Result<bool>> EnableControl()
|
public async ValueTask<Result<bool>> EnableControl()
|
||||||
{
|
{
|
||||||
if (MsgBus.IsRunning)
|
if (MsgBus.IsRunning)
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||||
else return new(new Exception("Message Bus not work!"));
|
else return new(new Exception("Message Bus not work!"));
|
||||||
|
|
||||||
var ret = await UDPClientPool.WriteAddr(this.ep, 1, MatrixKeyAddr.KEY_ENABLE, 1, this.timeout);
|
var ret = await UDPClientPool.WriteAddr(this.ep, 1, MatrixKeyAddr.KEY_ENABLE, 1, this.timeout);
|
||||||
@@ -59,7 +59,7 @@ public class MatrixKey
|
|||||||
public async ValueTask<Result<bool>> DisableControl()
|
public async ValueTask<Result<bool>> DisableControl()
|
||||||
{
|
{
|
||||||
if (MsgBus.IsRunning)
|
if (MsgBus.IsRunning)
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||||
else return new(new Exception("Message Bus not work!"));
|
else return new(new Exception("Message Bus not work!"));
|
||||||
|
|
||||||
var ret = await UDPClientPool.WriteAddr(this.ep, 1, MatrixKeyAddr.KEY_ENABLE, 0, this.timeout);
|
var ret = await UDPClientPool.WriteAddr(this.ep, 1, MatrixKeyAddr.KEY_ENABLE, 0, this.timeout);
|
||||||
@@ -75,7 +75,7 @@ public class MatrixKey
|
|||||||
public async ValueTask<Result<bool>> ControlKey(BitArray keyStates)
|
public async ValueTask<Result<bool>> ControlKey(BitArray keyStates)
|
||||||
{
|
{
|
||||||
if (MsgBus.IsRunning)
|
if (MsgBus.IsRunning)
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||||
else return new(new Exception("Message Bus not work!"));
|
else return new(new Exception("Message Bus not work!"));
|
||||||
|
|
||||||
if (keyStates.Length != 16) return new(new ArgumentException(
|
if (keyStates.Length != 16) return new(new ArgumentException(
|
||||||
|
|||||||
392
server/src/Peripherals/NetConfigClient.cs
Normal file
392
server/src/Peripherals/NetConfigClient.cs
Normal file
@@ -0,0 +1,392 @@
|
|||||||
|
using System.Net;
|
||||||
|
using DotNext;
|
||||||
|
|
||||||
|
namespace Peripherals.NetConfigClient;
|
||||||
|
|
||||||
|
static class NetConfigAddr
|
||||||
|
{
|
||||||
|
const UInt32 BASE = 0x30A7_0000;
|
||||||
|
|
||||||
|
public static readonly UInt32[] HOST_IP = { BASE + 0, BASE + 1, BASE + 2, BASE + 3 };
|
||||||
|
public static readonly UInt32[] BOARD_IP = { BASE + 4, BASE + 5, BASE + 6, BASE + 7 };
|
||||||
|
public static readonly UInt32[] HOST_MAC = { BASE + 8, BASE + 9, BASE + 10, BASE + 11, BASE + 12, BASE + 13 };
|
||||||
|
public static readonly UInt32[] BOARD_MAC = { BASE + 14, BASE + 15, BASE + 16, BASE + 17, BASE + 18, BASE + 19 };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Network configuration client for FPGA board communication
|
||||||
|
/// </summary>
|
||||||
|
public class NetConfig
|
||||||
|
{
|
||||||
|
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>
|
||||||
|
/// Initialize NetConfig client
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">Target board address</param>
|
||||||
|
/// <param name="port">Target board port</param>
|
||||||
|
/// <param name="taskID">Task identifier</param>
|
||||||
|
/// <param name="timeout">Timeout in milliseconds</param>
|
||||||
|
public NetConfig(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>
|
||||||
|
/// Set host IP address
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ip">IP address to set</param>
|
||||||
|
/// <returns>Result indicating success or failure</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetHostIP(IPAddress ip)
|
||||||
|
{
|
||||||
|
// 清除UDP服务器接收缓冲区
|
||||||
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
|
// 刷新ARP
|
||||||
|
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||||
|
// if (!refrshRet)
|
||||||
|
// {
|
||||||
|
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
var ipBytes = ip.GetAddressBytes();
|
||||||
|
|
||||||
|
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_IP, ipBytes, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set host IP: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set host IP: operation returned false");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证设置结果
|
||||||
|
var verifyResult = await GetHostIP();
|
||||||
|
if (!verifyResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to verify host IP after setting: {verifyResult.Error}");
|
||||||
|
return new(verifyResult.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
var expectedIP = ip.ToString();
|
||||||
|
if (verifyResult.Value != expectedIP)
|
||||||
|
{
|
||||||
|
logger.Error($"Host IP verification failed: expected {expectedIP}, got {verifyResult.Value}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info($"Successfully set and verified host IP: {expectedIP}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set board IP address
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ip">IP address to set</param>
|
||||||
|
/// <returns>Result indicating success or failure</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetBoardIP(IPAddress ip)
|
||||||
|
{
|
||||||
|
// 清除UDP服务器接收缓冲区
|
||||||
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
|
// 刷新ARP
|
||||||
|
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||||
|
// if (!refrshRet)
|
||||||
|
// {
|
||||||
|
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
var ipBytes = ip.GetAddressBytes();
|
||||||
|
|
||||||
|
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_IP, ipBytes, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set board IP: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set board IP: operation returned false");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证设置结果
|
||||||
|
var verifyResult = await GetBoardIP();
|
||||||
|
if (!verifyResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to verify board IP after setting: {verifyResult.Error}");
|
||||||
|
return new(verifyResult.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
var expectedIP = ip.ToString();
|
||||||
|
if (verifyResult.Value != expectedIP)
|
||||||
|
{
|
||||||
|
logger.Error($"Board IP verification failed: expected {expectedIP}, got {verifyResult.Value}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info($"Successfully set and verified board IP: {expectedIP}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set host MAC address
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="macAddress">MAC address bytes (6 bytes)</param>
|
||||||
|
/// <returns>Result indicating success or failure</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetHostMAC(byte[] macAddress)
|
||||||
|
{
|
||||||
|
if (macAddress == null)
|
||||||
|
throw new ArgumentNullException(nameof(macAddress));
|
||||||
|
if (macAddress.Length != 6)
|
||||||
|
throw new ArgumentException("MAC address must be 6 bytes", nameof(macAddress));
|
||||||
|
|
||||||
|
// 清除UDP服务器接收缓冲区
|
||||||
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
|
// 刷新ARP
|
||||||
|
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||||
|
// if (!refrshRet)
|
||||||
|
// {
|
||||||
|
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_MAC, macAddress, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set host MAC address: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set host MAC address: operation returned false");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证设置结果
|
||||||
|
var verifyResult = await GetHostMAC();
|
||||||
|
if (!verifyResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to verify host MAC after setting: {verifyResult.Error}");
|
||||||
|
return new(verifyResult.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
var expectedMAC = string.Join(":", macAddress.Select(b => $"{b:X2}"));
|
||||||
|
if (verifyResult.Value != expectedMAC)
|
||||||
|
{
|
||||||
|
logger.Error($"Host MAC verification failed: expected {expectedMAC}, got {verifyResult.Value}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info($"Successfully set and verified host MAC: {expectedMAC}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set board MAC address
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="macAddress">MAC address bytes (6 bytes)</param>
|
||||||
|
/// <returns>Result indicating success or failure</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetBoardMAC(byte[] macAddress)
|
||||||
|
{
|
||||||
|
if (macAddress == null)
|
||||||
|
throw new ArgumentNullException(nameof(macAddress));
|
||||||
|
if (macAddress.Length != 6)
|
||||||
|
throw new ArgumentException("MAC address must be 6 bytes", nameof(macAddress));
|
||||||
|
|
||||||
|
// 清除UDP服务器接收缓冲区
|
||||||
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
|
// 刷新ARP
|
||||||
|
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||||
|
// if (!refrshRet)
|
||||||
|
// {
|
||||||
|
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_MAC, macAddress, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set board MAC address: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set board MAC address: operation returned false");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证设置结果
|
||||||
|
var verifyResult = await GetBoardMAC();
|
||||||
|
if (!verifyResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to verify board MAC after setting: {verifyResult.Error}");
|
||||||
|
return new(verifyResult.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
var expectedMAC = string.Join(":", macAddress.Select(b => $"{b:X2}"));
|
||||||
|
if (verifyResult.Value != expectedMAC)
|
||||||
|
{
|
||||||
|
logger.Error($"Board MAC verification failed: expected {expectedMAC}, got {verifyResult.Value}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info($"Successfully set and verified board MAC: {expectedMAC}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get host IP address
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Host IP address as string</returns>
|
||||||
|
public async ValueTask<Result<string>> GetHostIP()
|
||||||
|
{
|
||||||
|
// 清除UDP服务器接收缓冲区
|
||||||
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
|
// 刷新ARP
|
||||||
|
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||||
|
// if (!refrshRet)
|
||||||
|
// {
|
||||||
|
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
var ret = await UDPClientPool.ReadAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_IP, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to get host IP: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
var ip = "";
|
||||||
|
for (int i = 0; i < NetConfigAddr.HOST_IP.Length; i++)
|
||||||
|
{
|
||||||
|
ip += $"{ret.Value[i * 4 + 3]}";
|
||||||
|
if (i != NetConfigAddr.HOST_IP.Length - 1)
|
||||||
|
ip += ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get board IP address
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Board IP address as string</returns>
|
||||||
|
public async ValueTask<Result<string>> GetBoardIP()
|
||||||
|
{
|
||||||
|
// 清除UDP服务器接收缓冲区
|
||||||
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
|
// 刷新ARP
|
||||||
|
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||||
|
// if (!refrshRet)
|
||||||
|
// {
|
||||||
|
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
var ret = await UDPClientPool.ReadAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_IP, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to get board IP: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
var ip = "";
|
||||||
|
for (int i = 0; i < NetConfigAddr.BOARD_IP.Length; i++)
|
||||||
|
{
|
||||||
|
ip += $"{ret.Value[i * 4 + 3]}";
|
||||||
|
if (i != NetConfigAddr.BOARD_IP.Length - 1)
|
||||||
|
ip += ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get host MAC address
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Host MAC address as formatted string (XX:XX:XX:XX:XX:XX)</returns>
|
||||||
|
public async ValueTask<Result<string>> GetHostMAC()
|
||||||
|
{
|
||||||
|
// 清除UDP服务器接收缓冲区
|
||||||
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
|
// 刷新ARP
|
||||||
|
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||||
|
// if (!refrshRet)
|
||||||
|
// {
|
||||||
|
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
var ret = await UDPClientPool.ReadAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_MAC, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to get host MAC address: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
var mac = "";
|
||||||
|
for (int i = 0; i < NetConfigAddr.HOST_MAC.Length; i++)
|
||||||
|
{
|
||||||
|
mac += $"{ret.Value[i * 4 + 3]:X2}";
|
||||||
|
if (i != NetConfigAddr.HOST_MAC.Length - 1)
|
||||||
|
mac += ":";
|
||||||
|
}
|
||||||
|
|
||||||
|
return mac;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get board MAC address
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Board MAC address as formatted string (XX:XX:XX:XX:XX:XX)</returns>
|
||||||
|
public async ValueTask<Result<string>> GetBoardMAC()
|
||||||
|
{
|
||||||
|
// 清除UDP服务器接收缓冲区
|
||||||
|
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||||
|
|
||||||
|
// 刷新ARP
|
||||||
|
// var refrshRet = await Arp.UpdateArpEntryByPingAsync(this.address);
|
||||||
|
// if (!refrshRet)
|
||||||
|
// {
|
||||||
|
// logger.Warn($"Refrash Arp failed, but maybe not a big deal.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
var ret = await UDPClientPool.ReadAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_MAC, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to get board MAC address: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
var mac = "";
|
||||||
|
for (int i = 0; i < NetConfigAddr.BOARD_MAC.Length; i++)
|
||||||
|
{
|
||||||
|
mac += $"{ret.Value[i * 4 + 3]:X2}";
|
||||||
|
if (i != NetConfigAddr.BOARD_MAC.Length - 1)
|
||||||
|
mac += ":";
|
||||||
|
}
|
||||||
|
|
||||||
|
return mac;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,69 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
|
using Common;
|
||||||
using DotNext;
|
using DotNext;
|
||||||
|
using WebProtocol;
|
||||||
|
|
||||||
namespace Peripherals.OscilloscopeClient;
|
namespace Peripherals.OscilloscopeClient;
|
||||||
|
|
||||||
static class OscilloscopeAddr
|
static class OscilloscopeAddr
|
||||||
{
|
{
|
||||||
public const UInt32 Base = 0x0000_0000;
|
const UInt32 BASE = 0x8000_0000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 0x0000_0000:R/W[0] wave_run 启动捕获/关闭
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 START_CAPTURE = BASE + 0x0000_0000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 0x0000_0001: R/W[7:0] trig_level 触发电平
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 TRIG_LEVEL = BASE + 0x0000_0001;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 0x0000_0002:R/W[0] trig_edge 触发边沿,0-下降沿,1-上升沿
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 TRIG_EDGE = BASE + 0x0000_0002;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 0x0000_0003: R/W[9:0] h shift 水平偏移量
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 H_SHIFT = BASE + 0x0000_0003;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 0x0000_0004: R/W[9:0] deci rate 抽样率,0—1023
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 DECI_RATE = BASE + 0x0000_0004;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 0x0000_0005:R/W[0] ram refresh RAM刷新
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 RAM_FRESH = BASE + 0x0000_0005;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 0x0000 0006:R[19: 0] ad_freq AD采样频率
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 AD_FREQ = BASE + 0x0000_0006;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ox0000_0007: R[7:0] ad_vpp AD采样幅度
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 AD_VPP = BASE + 0x0000_0007;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 0x0000_0008: R[7:0] ad max AD采样最大值
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 AD_MAX = BASE + 0x0000_0008;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 0x0000_0009: R[7:0] ad_min AD采样最小值
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 AD_MIN = BASE + 0x0000_0009;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 0x0000_1000-0x0000_13FF:R[7:0] wave_rd_data 共1024个字节
|
||||||
|
/// </summary>
|
||||||
|
public const UInt32 RD_DATA_ADDR = BASE + 0x0000_1000;
|
||||||
|
public const UInt32 RD_DATA_LENGTH = 0x0000_0400;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Oscilloscope
|
class Oscilloscope
|
||||||
@@ -13,6 +71,7 @@ class Oscilloscope
|
|||||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
readonly int timeout = 2000;
|
readonly int timeout = 2000;
|
||||||
|
readonly int taskID = 0;
|
||||||
|
|
||||||
readonly int port;
|
readonly int port;
|
||||||
readonly string address;
|
readonly string address;
|
||||||
@@ -33,4 +92,259 @@ class Oscilloscope
|
|||||||
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
|
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
|
||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 控制示波器的捕获开关
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="enable">是否启动捕获</param>
|
||||||
|
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetCaptureEnable(bool enable)
|
||||||
|
{
|
||||||
|
UInt32 value = enable ? 1u : 0u;
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.START_CAPTURE, value, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set capture enable: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to START_CAPTURE returned false");
|
||||||
|
return new(new Exception("Failed to set capture enable"));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置触发电平
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="level">触发电平值(0-255)</param>
|
||||||
|
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetTriggerLevel(byte level)
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.TRIG_LEVEL, level, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set trigger level: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to TRIG_LEVEL returned false");
|
||||||
|
return new(new Exception("Failed to set trigger level"));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置触发边沿
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="risingEdge">true为上升沿,false为下降沿</param>
|
||||||
|
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetTriggerEdge(bool risingEdge)
|
||||||
|
{
|
||||||
|
UInt32 value = risingEdge ? 1u : 0u;
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.TRIG_EDGE, value, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set trigger edge: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to TRIG_EDGE returned false");
|
||||||
|
return new(new Exception("Failed to set trigger edge"));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置水平偏移量
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="shift">水平偏移量值(0-1023)</param>
|
||||||
|
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetHorizontalShift(UInt16 shift)
|
||||||
|
{
|
||||||
|
if (shift > 1023)
|
||||||
|
return new(new ArgumentException("Horizontal shift must be 0-1023", nameof(shift)));
|
||||||
|
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.H_SHIFT, shift, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set horizontal shift: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to H_SHIFT returned false");
|
||||||
|
return new(new Exception("Failed to set horizontal shift"));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置抽样率
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="rate">抽样率值(0-1023)</param>
|
||||||
|
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> SetDecimationRate(UInt16 rate)
|
||||||
|
{
|
||||||
|
if (rate > 1023)
|
||||||
|
return new(new ArgumentException("Decimation rate must be 0-1023", nameof(rate)));
|
||||||
|
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.DECI_RATE, rate, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to set decimation rate: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to DECI_RATE returned false");
|
||||||
|
return new(new Exception("Failed to set decimation rate"));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 刷新RAM
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果,成功返回true,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<bool>> RefreshRAM()
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, OscilloscopeAddr.RAM_FRESH, 1u, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to refresh RAM: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error("WriteAddr to RAM_FRESH returned false");
|
||||||
|
return new(new Exception("Failed to refresh RAM"));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取AD采样频率
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果,成功返回采样频率值,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<UInt32>> GetADFrequency()
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_FREQ, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to read AD frequency: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 4)
|
||||||
|
{
|
||||||
|
logger.Error("ReadAddr returned invalid data for AD frequency");
|
||||||
|
return new(new Exception("Failed to read AD frequency"));
|
||||||
|
}
|
||||||
|
UInt32 freq = Number.BytesToUInt32(ret.Value.Options.Data).Value;
|
||||||
|
// 取低20位 [19:0]
|
||||||
|
freq &= 0xFFFFF;
|
||||||
|
return freq;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取AD采样幅度
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果,成功返回采样幅度值,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<byte>> GetADVpp()
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_VPP, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to read AD VPP: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 1)
|
||||||
|
{
|
||||||
|
logger.Error("ReadAddr returned invalid data for AD VPP");
|
||||||
|
return new(new Exception("Failed to read AD VPP"));
|
||||||
|
}
|
||||||
|
return ret.Value.Options.Data[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取AD采样最大值
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果,成功返回采样最大值,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<byte>> GetADMax()
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_MAX, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to read AD max: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 1)
|
||||||
|
{
|
||||||
|
logger.Error("ReadAddr returned invalid data for AD max");
|
||||||
|
return new(new Exception("Failed to read AD max"));
|
||||||
|
}
|
||||||
|
return ret.Value.Options.Data[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取AD采样最小值
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果,成功返回采样最小值,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<byte>> GetADMin()
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.ReadAddr(this.ep, this.taskID, OscilloscopeAddr.AD_MIN, this.timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to read AD min: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (ret.Value.Options.Data == null || ret.Value.Options.Data.Length < 1)
|
||||||
|
{
|
||||||
|
logger.Error("ReadAddr returned invalid data for AD min");
|
||||||
|
return new(new Exception("Failed to read AD min"));
|
||||||
|
}
|
||||||
|
return ret.Value.Options.Data[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取波形采样数据
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>操作结果,成功返回采样数据数组,否则返回异常信息</returns>
|
||||||
|
public async ValueTask<Result<byte[]>> GetWaveformData()
|
||||||
|
{
|
||||||
|
var ret = await UDPClientPool.ReadAddr4BytesAsync(
|
||||||
|
this.ep,
|
||||||
|
this.taskID,
|
||||||
|
OscilloscopeAddr.RD_DATA_ADDR,
|
||||||
|
(int)OscilloscopeAddr.RD_DATA_LENGTH / 32,
|
||||||
|
BurstType.ExtendBurst, // 使用扩展突发读取
|
||||||
|
this.timeout
|
||||||
|
);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to read waveform data: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
var data = ret.Value;
|
||||||
|
if (data == null || data.Length != OscilloscopeAddr.RD_DATA_LENGTH / 8)
|
||||||
|
{
|
||||||
|
logger.Error($"Waveform data length mismatch: {data?.Length}");
|
||||||
|
return new(new Exception("Waveform data length mismatch"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理波形数据:从每4个字节中提取第4个字节(索引3)作为有效数据
|
||||||
|
// 数据格式:低八位有效,即[4*i + 3]才是有效数据
|
||||||
|
int sampleCount = data.Length / 4;
|
||||||
|
byte[] waveformData = new byte[sampleCount];
|
||||||
|
|
||||||
|
for (int i = 0; i < sampleCount; i++)
|
||||||
|
{
|
||||||
|
waveformData[i] = data[4 * i + 3];
|
||||||
|
}
|
||||||
|
|
||||||
|
return waveformData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public class Power
|
|||||||
public async ValueTask<Result<bool>> SetPowerOnOff(bool enable)
|
public async ValueTask<Result<bool>> SetPowerOnOff(bool enable)
|
||||||
{
|
{
|
||||||
if (MsgBus.IsRunning)
|
if (MsgBus.IsRunning)
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
MsgBus.UDPServer.ClearUDPData(this.address, 1);
|
||||||
else return new(new Exception("Message Bus not work!"));
|
else return new(new Exception("Message Bus not work!"));
|
||||||
|
|
||||||
var ret = await UDPClientPool.WriteAddr(this.ep, 1, PowerAddr.PowerCtrl, Convert.ToUInt32(enable), this.timeout);
|
var ret = await UDPClientPool.WriteAddr(this.ep, 1, PowerAddr.PowerCtrl, Convert.ToUInt32(enable), this.timeout);
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ public class RemoteUpdater
|
|||||||
const int FLASH_SECTOR_LENGTH = 4 * 1024;
|
const int FLASH_SECTOR_LENGTH = 4 * 1024;
|
||||||
|
|
||||||
readonly int timeout = 2000;
|
readonly int timeout = 2000;
|
||||||
readonly int timeoutForWait = 60 * 1000;
|
readonly int timeoutForWait = 20 * 1000;
|
||||||
|
|
||||||
readonly int port;
|
readonly int port;
|
||||||
readonly string address;
|
readonly string address;
|
||||||
@@ -152,7 +152,7 @@ public class RemoteUpdater
|
|||||||
{
|
{
|
||||||
var ret = await UDPClientPool.ReadAddrWithWait(
|
var ret = await UDPClientPool.ReadAddrWithWait(
|
||||||
this.ep, 0, RemoteUpdaterAddr.WriteSign,
|
this.ep, 0, RemoteUpdaterAddr.WriteSign,
|
||||||
0x00_00_00_01, 0x00_00_00_01, this.timeoutForWait);
|
0x00_00_00_01, 0x00_00_00_01, 100, this.timeoutForWait);
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
if (!ret.Value) return new(new Exception(
|
if (!ret.Value) return new(new Exception(
|
||||||
$"Flash clear failed after {this.timeoutForWait} milliseconds"));
|
$"Flash clear failed after {this.timeoutForWait} milliseconds"));
|
||||||
@@ -167,7 +167,7 @@ public class RemoteUpdater
|
|||||||
{
|
{
|
||||||
var ret = await UDPClientPool.ReadAddrWithWait(
|
var ret = await UDPClientPool.ReadAddrWithWait(
|
||||||
this.ep, 0, RemoteUpdaterAddr.WriteSign,
|
this.ep, 0, RemoteUpdaterAddr.WriteSign,
|
||||||
0x00_00_01_00, 0x00_00_01_00, this.timeoutForWait);
|
0x00_00_01_00, 0x00_00_01_00, 100, this.timeoutForWait);
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
return ret.Value;
|
return ret.Value;
|
||||||
}
|
}
|
||||||
@@ -332,7 +332,7 @@ public class RemoteUpdater
|
|||||||
{
|
{
|
||||||
var ret = await UDPClientPool.ReadAddrWithWait(
|
var ret = await UDPClientPool.ReadAddrWithWait(
|
||||||
this.ep, 0, RemoteUpdaterAddr.ReadSign,
|
this.ep, 0, RemoteUpdaterAddr.ReadSign,
|
||||||
0x00_00_01_00, 0x00_00_01_00, this.timeoutForWait);
|
0x00_00_01_00, 0x00_00_01_00, 10, this.timeoutForWait);
|
||||||
if (!ret.IsSuccessful) return new(ret.Error);
|
if (!ret.IsSuccessful) return new(ret.Error);
|
||||||
if (!ret.Value) return new(new Exception(
|
if (!ret.Value) return new(new Exception(
|
||||||
$"Read bitstream failed after {this.timeoutForWait} milliseconds"));
|
$"Read bitstream failed after {this.timeoutForWait} milliseconds"));
|
||||||
@@ -382,7 +382,7 @@ public class RemoteUpdater
|
|||||||
/// <returns>[TODO:return]</returns>
|
/// <returns>[TODO:return]</returns>
|
||||||
public async ValueTask<Result<bool>> HotResetBitstream(int bitstreamNum)
|
public async ValueTask<Result<bool>> HotResetBitstream(int bitstreamNum)
|
||||||
{
|
{
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||||
logger.Trace("Clear udp data finished");
|
logger.Trace("Clear udp data finished");
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -412,7 +412,7 @@ public class RemoteUpdater
|
|||||||
byte[]? bitstream2,
|
byte[]? bitstream2,
|
||||||
byte[]? bitstream3)
|
byte[]? bitstream3)
|
||||||
{
|
{
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||||
logger.Trace("Clear udp data finished");
|
logger.Trace("Clear udp data finished");
|
||||||
|
|
||||||
for (int bitstreamNum = 0; bitstreamNum < 4; bitstreamNum++)
|
for (int bitstreamNum = 0; bitstreamNum < 4; bitstreamNum++)
|
||||||
@@ -463,7 +463,7 @@ public class RemoteUpdater
|
|||||||
$"The length of data should be divided by 4096, bug given {bytesData.Length}", nameof(bytesData)));
|
$"The length of data should be divided by 4096, bug given {bytesData.Length}", nameof(bytesData)));
|
||||||
var bitstreamBlockNum = bytesData.Length / (4 * 1024);
|
var bitstreamBlockNum = bytesData.Length / (4 * 1024);
|
||||||
|
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||||
logger.Trace("Clear udp data finished");
|
logger.Trace("Clear udp data finished");
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -539,7 +539,7 @@ public class RemoteUpdater
|
|||||||
/// <returns>[TODO:return]</returns>
|
/// <returns>[TODO:return]</returns>
|
||||||
public async ValueTask<Result<UInt32>> GetVersion()
|
public async ValueTask<Result<UInt32>> GetVersion()
|
||||||
{
|
{
|
||||||
await MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||||
logger.Trace("Clear udp data finished");
|
logger.Trace("Clear udp data finished");
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|||||||
498
server/src/Services/HttpHdmiVideoStreamService.cs
Normal file
498
server/src/Services/HttpHdmiVideoStreamService.cs
Normal file
@@ -0,0 +1,498 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using Peripherals.HdmiInClient;
|
||||||
|
|
||||||
|
namespace server.Services;
|
||||||
|
|
||||||
|
public class HdmiVideoStreamEndpoint
|
||||||
|
{
|
||||||
|
public string BoardId { get; set; } = "";
|
||||||
|
public string MjpegUrl { get; set; } = "";
|
||||||
|
public string VideoUrl { get; set; } = "";
|
||||||
|
public string SnapshotUrl { get; set; } = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public class HttpHdmiVideoStreamService : BackgroundService
|
||||||
|
{
|
||||||
|
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
private HttpListener? _httpListener;
|
||||||
|
private readonly int _serverPort = 4322;
|
||||||
|
private readonly ConcurrentDictionary<string, HdmiIn> _hdmiInDict = new();
|
||||||
|
private readonly ConcurrentDictionary<string, CancellationTokenSource> _hdmiInCtsDict = new();
|
||||||
|
|
||||||
|
public override async Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
_httpListener = new HttpListener();
|
||||||
|
_httpListener.Prefixes.Add($"http://*:{_serverPort}/");
|
||||||
|
_httpListener.Start();
|
||||||
|
logger.Info($"HDMI Video Stream Service started on port {_serverPort}");
|
||||||
|
|
||||||
|
await base.StartAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
HttpListenerContext? context = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
logger.Debug("Waiting for HTTP request...");
|
||||||
|
context = await _httpListener.GetContextAsync();
|
||||||
|
logger.Debug($"Received request: {context.Request.Url?.AbsolutePath}");
|
||||||
|
}
|
||||||
|
catch (ObjectDisposedException)
|
||||||
|
{
|
||||||
|
// Listener closed, exit loop
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (HttpListenerException)
|
||||||
|
{
|
||||||
|
// Listener closed, exit loop
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "Error in GetContextAsync");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (context != null)
|
||||||
|
_ = HandleRequestAsync(context, stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_httpListener?.Close();
|
||||||
|
logger.Info("HDMI Video Stream Service stopped.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
logger.Info("Stopping HDMI Video Stream Service...");
|
||||||
|
|
||||||
|
// 禁用所有活跃的HDMI传输
|
||||||
|
var disableTasks = new List<Task>();
|
||||||
|
foreach (var hdmiKey in _hdmiInDict.Keys)
|
||||||
|
{
|
||||||
|
disableTasks.Add(DisableHdmiTransmissionAsync(hdmiKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待所有禁用操作完成
|
||||||
|
await Task.WhenAll(disableTasks);
|
||||||
|
|
||||||
|
// 清空字典
|
||||||
|
_hdmiInDict.Clear();
|
||||||
|
_hdmiInCtsDict.Clear();
|
||||||
|
|
||||||
|
_httpListener?.Close(); // 立即关闭监听器,唤醒阻塞
|
||||||
|
await base.StopAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DisableHdmiTransmissionAsync(string key)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var cts = _hdmiInCtsDict[key];
|
||||||
|
cts.Cancel();
|
||||||
|
|
||||||
|
var hdmiIn = _hdmiInDict[key];
|
||||||
|
var disableResult = await hdmiIn.EnableTrans(false);
|
||||||
|
if (disableResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Info("Successfully disabled HDMI transmission");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to disable HDMI transmission: {disableResult.Error}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "Exception occurred while disabling HDMI transmission");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取/创建 HdmiIn 实例
|
||||||
|
private async Task<HdmiIn?> GetOrCreateHdmiInAsync(string boardId)
|
||||||
|
{
|
||||||
|
if (_hdmiInDict.TryGetValue(boardId, out var hdmiIn))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var enableResult = await hdmiIn.EnableTrans(true);
|
||||||
|
if (!enableResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to enable HDMI transmission for board {boardId}: {enableResult.Error}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
logger.Info($"Successfully enabled HDMI transmission for board {boardId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, $"Exception occurred while enabling HDMI transmission for board {boardId}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
_hdmiInDict[boardId] = hdmiIn;
|
||||||
|
_hdmiInCtsDict[boardId] = new CancellationTokenSource();
|
||||||
|
return hdmiIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
var db = new Database.AppDataConnection();
|
||||||
|
if (db == null)
|
||||||
|
{
|
||||||
|
logger.Error("Failed to create HdmiIn instance");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var boardRet = db.GetBoardByID(Guid.Parse(boardId));
|
||||||
|
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to get board with ID {boardId}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var board = boardRet.Value.Value;
|
||||||
|
|
||||||
|
hdmiIn = new HdmiIn(board.IpAddr, board.Port, 0); // taskID 可根据实际需求调整
|
||||||
|
|
||||||
|
// 启用HDMI传输
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var enableResult = await hdmiIn.EnableTrans(true);
|
||||||
|
if (!enableResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to enable HDMI transmission for board {boardId}: {enableResult.Error}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
logger.Info($"Successfully enabled HDMI transmission for board {boardId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, $"Exception occurred while enabling HDMI transmission for board {boardId}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
_hdmiInDict[boardId] = hdmiIn;
|
||||||
|
_hdmiInCtsDict[boardId] = new CancellationTokenSource();
|
||||||
|
return hdmiIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task HandleRequestAsync(HttpListenerContext context, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var path = context.Request.Url?.AbsolutePath ?? "/";
|
||||||
|
var boardId = context.Request.QueryString["boardId"];
|
||||||
|
if (string.IsNullOrEmpty(boardId))
|
||||||
|
{
|
||||||
|
await SendErrorAsync(context.Response, "Missing boardId");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var hdmiIn = await GetOrCreateHdmiInAsync(boardId);
|
||||||
|
if (hdmiIn == null)
|
||||||
|
{
|
||||||
|
await SendErrorAsync(context.Response, "Invalid boardId or board not available");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var hdmiInToken = _hdmiInCtsDict[boardId].Token;
|
||||||
|
if (hdmiInToken == null)
|
||||||
|
{
|
||||||
|
await SendErrorAsync(context.Response, "HDMI input is not available");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path == "/snapshot")
|
||||||
|
{
|
||||||
|
await HandleSnapshotRequestAsync(context.Response, hdmiIn, hdmiInToken);
|
||||||
|
}
|
||||||
|
else if (path == "/mjpeg")
|
||||||
|
{
|
||||||
|
await HandleMjpegStreamAsync(context.Response, hdmiIn, hdmiInToken);
|
||||||
|
}
|
||||||
|
else if (path == "/video")
|
||||||
|
{
|
||||||
|
await SendVideoHtmlPageAsync(context.Response, boardId);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await SendIndexHtmlPageAsync(context.Response, boardId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task HandleSnapshotRequestAsync(HttpListenerResponse response, HdmiIn hdmiIn, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
logger.Debug("处理HDMI快照请求");
|
||||||
|
|
||||||
|
const int frameWidth = 960; // HDMI输入分辨率
|
||||||
|
const int frameHeight = 540;
|
||||||
|
|
||||||
|
// 从HDMI读取RGB565数据
|
||||||
|
var frameResult = await hdmiIn.ReadFrame();
|
||||||
|
if (!frameResult.IsSuccessful || frameResult.Value == null)
|
||||||
|
{
|
||||||
|
logger.Error("HDMI快照获取失败");
|
||||||
|
response.StatusCode = 500;
|
||||||
|
var errorBytes = System.Text.Encoding.UTF8.GetBytes("Failed to get HDMI snapshot");
|
||||||
|
await response.OutputStream.WriteAsync(errorBytes, 0, errorBytes.Length, cancellationToken);
|
||||||
|
response.Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var rgb565Data = frameResult.Value;
|
||||||
|
|
||||||
|
// 验证数据长度
|
||||||
|
var expectedLength = frameWidth * frameHeight * 2;
|
||||||
|
if (rgb565Data.Length != expectedLength)
|
||||||
|
{
|
||||||
|
logger.Warn("HDMI快照数据长度不匹配,期望: {Expected}, 实际: {Actual}",
|
||||||
|
expectedLength, rgb565Data.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将RGB565转换为RGB24
|
||||||
|
var rgb24Result = Common.Image.ConvertRGB565ToRGB24(rgb565Data, frameWidth, frameHeight, isLittleEndian: false);
|
||||||
|
if (!rgb24Result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error("HDMI快照RGB565转RGB24失败: {Error}", rgb24Result.Error);
|
||||||
|
response.StatusCode = 500;
|
||||||
|
var errorBytes = System.Text.Encoding.UTF8.GetBytes("Failed to process HDMI snapshot");
|
||||||
|
await response.OutputStream.WriteAsync(errorBytes, 0, errorBytes.Length, cancellationToken);
|
||||||
|
response.Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将RGB24转换为JPEG
|
||||||
|
var jpegResult = Common.Image.ConvertRGB24ToJpeg(rgb24Result.Value, frameWidth, frameHeight, 80);
|
||||||
|
if (!jpegResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error("HDMI快照RGB24转JPEG失败: {Error}", jpegResult.Error);
|
||||||
|
response.StatusCode = 500;
|
||||||
|
var errorBytes = System.Text.Encoding.UTF8.GetBytes("Failed to encode HDMI snapshot");
|
||||||
|
await response.OutputStream.WriteAsync(errorBytes, 0, errorBytes.Length, cancellationToken);
|
||||||
|
response.Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var jpegData = jpegResult.Value;
|
||||||
|
|
||||||
|
// 设置响应头(参考Camera版本)
|
||||||
|
response.ContentType = "image/jpeg";
|
||||||
|
response.ContentLength64 = jpegData.Length;
|
||||||
|
response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||||
|
|
||||||
|
await response.OutputStream.WriteAsync(jpegData, 0, jpegData.Length, cancellationToken);
|
||||||
|
await response.OutputStream.FlushAsync(cancellationToken);
|
||||||
|
|
||||||
|
logger.Debug("已发送HDMI快照图像,大小:{Size} 字节", jpegData.Length);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "处理HDMI快照请求时出错");
|
||||||
|
response.StatusCode = 500;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
response.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task HandleMjpegStreamAsync(HttpListenerResponse response, HdmiIn hdmiIn, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 设置MJPEG流的响应头(参考Camera版本)
|
||||||
|
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");
|
||||||
|
|
||||||
|
logger.Debug("开始HDMI MJPEG流传输");
|
||||||
|
|
||||||
|
int frameCounter = 0;
|
||||||
|
const int frameWidth = 960; // HDMI输入分辨率
|
||||||
|
const int frameHeight = 540;
|
||||||
|
|
||||||
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var frameStartTime = DateTime.UtcNow;
|
||||||
|
|
||||||
|
// 从HDMI读取RGB565数据
|
||||||
|
var readStartTime = DateTime.UtcNow;
|
||||||
|
var frameResult = await hdmiIn.ReadFrame();
|
||||||
|
var readEndTime = DateTime.UtcNow;
|
||||||
|
var readTime = (readEndTime - readStartTime).TotalMilliseconds;
|
||||||
|
|
||||||
|
if (!frameResult.IsSuccessful || frameResult.Value == null)
|
||||||
|
{
|
||||||
|
logger.Warn("HDMI帧读取失败或为空");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var rgb565Data = frameResult.Value;
|
||||||
|
|
||||||
|
// 验证数据长度是否正确 (RGB565为每像素2字节)
|
||||||
|
var expectedLength = frameWidth * frameHeight * 2;
|
||||||
|
if (rgb565Data.Length != expectedLength)
|
||||||
|
{
|
||||||
|
logger.Warn("HDMI数据长度不匹配,期望: {Expected}, 实际: {Actual}",
|
||||||
|
expectedLength, rgb565Data.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将RGB565转换为RGB24(参考Camera版本的处理)
|
||||||
|
var convertStartTime = DateTime.UtcNow;
|
||||||
|
var rgb24Result = Common.Image.ConvertRGB565ToRGB24(rgb565Data, frameWidth, frameHeight, isLittleEndian: false);
|
||||||
|
var convertEndTime = DateTime.UtcNow;
|
||||||
|
var convertTime = (convertEndTime - convertStartTime).TotalMilliseconds;
|
||||||
|
|
||||||
|
if (!rgb24Result.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error("HDMI RGB565转RGB24失败: {Error}", rgb24Result.Error);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将RGB24转换为JPEG(参考Camera版本的处理)
|
||||||
|
var jpegStartTime = DateTime.UtcNow;
|
||||||
|
var jpegResult = Common.Image.ConvertRGB24ToJpeg(rgb24Result.Value, frameWidth, frameHeight, 80);
|
||||||
|
var jpegEndTime = DateTime.UtcNow;
|
||||||
|
var jpegTime = (jpegEndTime - jpegStartTime).TotalMilliseconds;
|
||||||
|
|
||||||
|
if (!jpegResult.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error("HDMI RGB24转JPEG失败: {Error}", jpegResult.Error);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var jpegData = jpegResult.Value;
|
||||||
|
|
||||||
|
// 发送MJPEG帧(使用Camera版本的格式)
|
||||||
|
var mjpegFrameHeader = Common.Image.CreateMjpegFrameHeader(jpegData.Length);
|
||||||
|
var mjpegFrameFooter = Common.Image.CreateMjpegFrameFooter();
|
||||||
|
|
||||||
|
await response.OutputStream.WriteAsync(mjpegFrameHeader, 0, mjpegFrameHeader.Length, cancellationToken);
|
||||||
|
await response.OutputStream.WriteAsync(jpegData, 0, jpegData.Length, cancellationToken);
|
||||||
|
await response.OutputStream.WriteAsync(mjpegFrameFooter, 0, mjpegFrameFooter.Length, cancellationToken);
|
||||||
|
await response.OutputStream.FlushAsync(cancellationToken);
|
||||||
|
|
||||||
|
frameCounter++;
|
||||||
|
|
||||||
|
var totalTime = (DateTime.UtcNow - frameStartTime).TotalMilliseconds;
|
||||||
|
|
||||||
|
// 性能统计日志(每30帧记录一次)
|
||||||
|
if (frameCounter % 30 == 0)
|
||||||
|
{
|
||||||
|
logger.Debug("HDMI帧 {FrameNumber} 性能统计 - 读取: {ReadTime:F1}ms, RGB转换: {ConvertTime:F1}ms, JPEG转换: {JpegTime:F1}ms, 总计: {TotalTime:F1}ms, JPEG大小: {JpegSize} 字节",
|
||||||
|
frameCounter, readTime, convertTime, jpegTime, totalTime, jpegData.Length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "处理HDMI帧时发生错误");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "HDMI MJPEG流处理异常");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 停止传输时禁用HDMI传输
|
||||||
|
await hdmiIn.EnableTrans(false);
|
||||||
|
logger.Info("已禁用HDMI传输");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "禁用HDMI传输时出错");
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
response.Close();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// 忽略关闭时的错误
|
||||||
|
}
|
||||||
|
logger.Debug("HDMI MJPEG流连接已关闭");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SendVideoHtmlPageAsync(HttpListenerResponse response, string boardId)
|
||||||
|
{
|
||||||
|
string html = $@"<html><body>
|
||||||
|
<h1>HDMI Video Stream for Board {boardId}</h1>
|
||||||
|
<img src='/mjpeg?boardId={boardId}' />
|
||||||
|
</body></html>";
|
||||||
|
response.ContentType = "text/html";
|
||||||
|
await response.OutputStream.WriteAsync(System.Text.Encoding.UTF8.GetBytes(html));
|
||||||
|
response.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SendIndexHtmlPageAsync(HttpListenerResponse response, string boardId)
|
||||||
|
{
|
||||||
|
string html = $@"<html><body>
|
||||||
|
<h1>Welcome to HDMI Video Stream Service</h1>
|
||||||
|
<a href='/video?boardId={boardId}'>View Video Stream for Board {boardId}</a>
|
||||||
|
</body></html>";
|
||||||
|
response.ContentType = "text/html";
|
||||||
|
await response.OutputStream.WriteAsync(System.Text.Encoding.UTF8.GetBytes(html));
|
||||||
|
response.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SendErrorAsync(HttpListenerResponse response, string message)
|
||||||
|
{
|
||||||
|
response.StatusCode = 400;
|
||||||
|
await response.OutputStream.WriteAsync(System.Text.Encoding.UTF8.GetBytes(message));
|
||||||
|
response.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取所有可用的HDMI视频流终端点
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>返回所有可用的HDMI视频流终端点列表</returns>
|
||||||
|
public List<HdmiVideoStreamEndpoint>? GetAllVideoEndpoints()
|
||||||
|
{
|
||||||
|
var db = new Database.AppDataConnection();
|
||||||
|
var boards = db?.GetAllBoard();
|
||||||
|
if (boards == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var endpoints = new List<HdmiVideoStreamEndpoint>();
|
||||||
|
foreach (var board in boards)
|
||||||
|
{
|
||||||
|
endpoints.Add(new HdmiVideoStreamEndpoint
|
||||||
|
{
|
||||||
|
BoardId = board.ID.ToString(),
|
||||||
|
MjpegUrl = $"http://{Global.localhost}:{_serverPort}/mjpeg?boardId={board.ID}",
|
||||||
|
VideoUrl = $"http://{Global.localhost}:{_serverPort}/video?boardId={board.ID}",
|
||||||
|
SnapshotUrl = $"http://{Global.localhost}:{_serverPort}/snapshot?boardId={board.ID}"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return endpoints;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取指定板卡ID的HDMI视频流终端点
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="boardId">板卡ID</param>
|
||||||
|
/// <returns>返回指定板卡的HDMI视频流终端点</returns>
|
||||||
|
public HdmiVideoStreamEndpoint GetVideoEndpoint(string boardId)
|
||||||
|
{
|
||||||
|
return new HdmiVideoStreamEndpoint
|
||||||
|
{
|
||||||
|
BoardId = boardId,
|
||||||
|
MjpegUrl = $"http://{Global.localhost}:{_serverPort}/mjpeg?boardId={boardId}",
|
||||||
|
VideoUrl = $"http://{Global.localhost}:{_serverPort}/video?boardId={boardId}",
|
||||||
|
SnapshotUrl = $"http://{Global.localhost}:{_serverPort}/snapshot?boardId={boardId}"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,14 @@
|
|||||||
|
#define USB_CAMERA
|
||||||
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Peripherals.CameraClient; // 添加摄像头客户端引用
|
using Peripherals.CameraClient; // 添加摄像头客户端引用
|
||||||
|
|
||||||
|
#if USB_CAMERA
|
||||||
|
using OpenCvSharp;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace server.Services;
|
namespace server.Services;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -80,7 +86,7 @@ public class HttpVideoStreamService : BackgroundService
|
|||||||
{
|
{
|
||||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
private HttpListener? _httpListener;
|
private HttpListener? _httpListener;
|
||||||
private readonly int _serverPort = 8080;
|
private readonly int _serverPort = 4321;
|
||||||
private readonly int _frameRate = 30; // 30 FPS
|
private readonly int _frameRate = 30; // 30 FPS
|
||||||
|
|
||||||
// 动态分辨率配置
|
// 动态分辨率配置
|
||||||
@@ -95,6 +101,13 @@ public class HttpVideoStreamService : BackgroundService
|
|||||||
private int _cameraPort = 8888; // 默认端口
|
private int _cameraPort = 8888; // 默认端口
|
||||||
private readonly object _cameraLock = new object();
|
private readonly object _cameraLock = new object();
|
||||||
|
|
||||||
|
// USB Camera 相关
|
||||||
|
#if USB_CAMERA
|
||||||
|
private VideoCapture? _usbCamera;
|
||||||
|
private bool _usbCameraEnable = false;
|
||||||
|
private readonly object _usbCameraLock = new object();
|
||||||
|
#endif
|
||||||
|
|
||||||
// 模拟 FPGA 图像数据
|
// 模拟 FPGA 图像数据
|
||||||
private int _frameCounter = 0;
|
private int _frameCounter = 0;
|
||||||
private readonly List<HttpListenerResponse> _activeClients = new List<HttpListenerResponse>();
|
private readonly List<HttpListenerResponse> _activeClients = new List<HttpListenerResponse>();
|
||||||
@@ -156,8 +169,9 @@ public class HttpVideoStreamService : BackgroundService
|
|||||||
throw new Exception("Please config camera first");
|
throw new Exception("Please config camera first");
|
||||||
}
|
}
|
||||||
_cameraEnable = isEnabled;
|
_cameraEnable = isEnabled;
|
||||||
await _camera.EnableCamera(_cameraEnable);
|
// if (_cameraEnable) await _camera.WakeUp();
|
||||||
await _camera.SleepCameraHardware(!_cameraEnable);
|
// else await _camera.Sleep();
|
||||||
|
await _camera.EnableHardwareTrans(_cameraEnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -297,7 +311,7 @@ public class HttpVideoStreamService : BackgroundService
|
|||||||
|
|
||||||
// 创建 HTTP 监听器
|
// 创建 HTTP 监听器
|
||||||
_httpListener = new HttpListener();
|
_httpListener = new HttpListener();
|
||||||
_httpListener.Prefixes.Add($"http://localhost:{_serverPort}/");
|
_httpListener.Prefixes.Add($"http://{Global.localhost}:{_serverPort}/");
|
||||||
_httpListener.Start();
|
_httpListener.Start();
|
||||||
|
|
||||||
logger.Info("HTTP 视频流服务已启动,监听端口: {Port}", _serverPort);
|
logger.Info("HTTP 视频流服务已启动,监听端口: {Port}", _serverPort);
|
||||||
@@ -345,9 +359,16 @@ public class HttpVideoStreamService : BackgroundService
|
|||||||
|
|
||||||
if (requestPath == "/video-stream")
|
if (requestPath == "/video-stream")
|
||||||
{
|
{
|
||||||
// MJPEG 流请求
|
// MJPEG 流请求(FPGA)
|
||||||
_ = Task.Run(() => HandleMjpegStreamAsync(response, cancellationToken), cancellationToken);
|
_ = Task.Run(() => HandleMjpegStreamAsync(response, cancellationToken), cancellationToken);
|
||||||
}
|
}
|
||||||
|
#if USB_CAMERA
|
||||||
|
else if (requestPath == "/usb-camera")
|
||||||
|
{
|
||||||
|
// USB Camera MJPEG流请求
|
||||||
|
_ = Task.Run(() => HandleUsbCameraStreamAsync(response, cancellationToken), cancellationToken);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
else if (requestPath == "/snapshot")
|
else if (requestPath == "/snapshot")
|
||||||
{
|
{
|
||||||
// 单帧图像请求
|
// 单帧图像请求
|
||||||
@@ -381,6 +402,87 @@ public class HttpVideoStreamService : BackgroundService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// USB Camera MJPEG流处理
|
||||||
|
#if USB_CAMERA
|
||||||
|
private async Task HandleUsbCameraStreamAsync(HttpListenerResponse response, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
lock (_usbCameraLock)
|
||||||
|
{
|
||||||
|
if (_usbCamera == null)
|
||||||
|
{
|
||||||
|
_usbCamera = new VideoCapture(1);
|
||||||
|
_usbCamera.Fps = _frameRate;
|
||||||
|
_usbCamera.FrameWidth = _frameWidth;
|
||||||
|
_usbCamera.FrameHeight = _frameHeight;
|
||||||
|
_usbCameraEnable = _usbCamera.IsOpened();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!_usbCameraEnable || _usbCamera == null || !_usbCamera.IsOpened())
|
||||||
|
{
|
||||||
|
response.StatusCode = 500;
|
||||||
|
await response.OutputStream.FlushAsync(cancellationToken);
|
||||||
|
response.Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
using (var mat = new Mat())
|
||||||
|
{
|
||||||
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
bool grabbed;
|
||||||
|
lock (_usbCameraLock)
|
||||||
|
{
|
||||||
|
grabbed = _usbCamera.Read(mat);
|
||||||
|
}
|
||||||
|
if (!grabbed || mat.Empty())
|
||||||
|
{
|
||||||
|
await Task.Delay(50, cancellationToken);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 编码为JPEG
|
||||||
|
byte[]? jpegData = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
jpegData = mat.ToBytes(".jpg", new int[] { (int)ImwriteFlags.JpegQuality, 80 });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "USB Camera帧编码JPEG失败");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (jpegData == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// MJPEG帧头
|
||||||
|
var header = Encoding.ASCII.GetBytes("--boundary\r\nContent-Type: image/jpeg\r\nContent-Length: " + jpegData.Length + "\r\n\r\n");
|
||||||
|
await response.OutputStream.WriteAsync(header, 0, header.Length, cancellationToken);
|
||||||
|
await response.OutputStream.WriteAsync(jpegData, 0, jpegData.Length, cancellationToken);
|
||||||
|
await response.OutputStream.WriteAsync(new byte[] { 0x0D, 0x0A }, 0, 2, cancellationToken); // \r\n
|
||||||
|
await response.OutputStream.FlushAsync(cancellationToken);
|
||||||
|
|
||||||
|
await Task.Delay(1000 / _frameRate, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "USB Camera MJPEG流处理异常");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
try { response.Close(); } catch { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
private async Task HandleMjpegStreamAsync(HttpListenerResponse response, CancellationToken cancellationToken)
|
private async Task HandleMjpegStreamAsync(HttpListenerResponse response, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -945,14 +1047,6 @@ public class HttpVideoStreamService : BackgroundService
|
|||||||
{
|
{
|
||||||
logger.Info($"正在设置视频流分辨率为 {width}x{height}");
|
logger.Info($"正在设置视频流分辨率为 {width}x{height}");
|
||||||
|
|
||||||
// 验证分辨率
|
|
||||||
if (!IsSupportedResolution(width, height))
|
|
||||||
{
|
|
||||||
var message = $"不支持的分辨率: {width}x{height},支持的分辨率: 640x480, 1280x720";
|
|
||||||
logger.Error(message);
|
|
||||||
return (false, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
Camera? currentCamera = null;
|
Camera? currentCamera = null;
|
||||||
lock (_cameraLock)
|
lock (_cameraLock)
|
||||||
{
|
{
|
||||||
@@ -1006,18 +1100,6 @@ public class HttpVideoStreamService : BackgroundService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 检查是否支持该分辨率
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="width">宽度</param>
|
|
||||||
/// <param name="height">高度</param>
|
|
||||||
/// <returns>是否支持</returns>
|
|
||||||
private bool IsSupportedResolution(int width, int height)
|
|
||||||
{
|
|
||||||
var resolution = $"{width}x{height}";
|
|
||||||
return resolution == "640x480" || resolution == "1280x720";
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取支持的分辨率列表
|
/// 获取支持的分辨率列表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -1027,7 +1109,10 @@ public class HttpVideoStreamService : BackgroundService
|
|||||||
return new List<(int, int, string)>
|
return new List<(int, int, string)>
|
||||||
{
|
{
|
||||||
(640, 480, "640x480 (VGA)"),
|
(640, 480, "640x480 (VGA)"),
|
||||||
(1280, 720, "1280x720 (HD)")
|
(960, 540, "960x540 (qHD)"),
|
||||||
|
(1280, 720, "1280x720 (HD)"),
|
||||||
|
(1280, 960, "1280x960 (SXGA)"),
|
||||||
|
(1920, 1080, "1920x1080 (Full HD)")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
288
server/src/Services/ProgressTrackerService.cs
Normal file
288
server/src/Services/ProgressTrackerService.cs
Normal file
@@ -0,0 +1,288 @@
|
|||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using DotNext;
|
||||||
|
using Common;
|
||||||
|
using server.Hubs;
|
||||||
|
|
||||||
|
namespace server.Services;
|
||||||
|
|
||||||
|
public class ProgressReporter : ProgressInfo, IProgress<int>
|
||||||
|
{
|
||||||
|
private int _progress = 0;
|
||||||
|
private int _stepProgress = 1;
|
||||||
|
private int _expectedSteps = 100;
|
||||||
|
private int _parentProportion = 100;
|
||||||
|
|
||||||
|
public int Progress => _progress;
|
||||||
|
public int MaxProgress { get; set; } = 100;
|
||||||
|
public int StepProgress
|
||||||
|
{
|
||||||
|
get => _stepProgress;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_stepProgress = value;
|
||||||
|
ExpectedSteps = MaxProgress / value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int ExpectedSteps
|
||||||
|
{
|
||||||
|
get => _expectedSteps;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_expectedSteps = value;
|
||||||
|
MaxProgress = Number.IntPow(10, Number.GetLength(value));
|
||||||
|
StepProgress = MaxProgress / value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public Func<int, Task>? ReporterFunc { get; set; } = null;
|
||||||
|
public ProgressReporter? Parent { get; set; }
|
||||||
|
public ProgressReporter? Child { get; set; }
|
||||||
|
|
||||||
|
private ProgressStatus _status = ProgressStatus.Pending;
|
||||||
|
private string _errorMessage;
|
||||||
|
|
||||||
|
public string TaskId { get; set; } = new Guid().ToString();
|
||||||
|
public int ProgressPercent => _progress * 100 / MaxProgress;
|
||||||
|
public ProgressStatus Status => _status;
|
||||||
|
public string ErrorMessage => _errorMessage;
|
||||||
|
|
||||||
|
public ProgressReporter(Func<int, Task>? reporter = null, int initProgress = 0, int maxProgress = 100, int step = 1)
|
||||||
|
{
|
||||||
|
_progress = initProgress;
|
||||||
|
MaxProgress = maxProgress;
|
||||||
|
StepProgress = step;
|
||||||
|
ReporterFunc = reporter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProgressReporter(int parentProportion, int expectedSteps = 100, Func<int, Task>? reporter = null)
|
||||||
|
{
|
||||||
|
this._parentProportion = parentProportion;
|
||||||
|
MaxProgress = Number.IntPow(10, Number.GetLength(expectedSteps));
|
||||||
|
StepProgress = MaxProgress / expectedSteps;
|
||||||
|
ReporterFunc = reporter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void ForceReport(int value)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (ReporterFunc != null)
|
||||||
|
await ReporterFunc(value);
|
||||||
|
|
||||||
|
if (Parent != null)
|
||||||
|
Parent.Increase((value - _progress) / StepProgress * _parentProportion / (MaxProgress / StepProgress));
|
||||||
|
|
||||||
|
_progress = value;
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException ex)
|
||||||
|
{
|
||||||
|
_errorMessage = ex.Message;
|
||||||
|
this._status = ProgressStatus.Canceled;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_errorMessage = ex.Message;
|
||||||
|
this._status = ProgressStatus.Failed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void Report(int value)
|
||||||
|
{
|
||||||
|
if (this._status == ProgressStatus.Pending)
|
||||||
|
this._status = ProgressStatus.InProgress;
|
||||||
|
else if (this.Status != ProgressStatus.InProgress)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (value > MaxProgress) return;
|
||||||
|
ForceReport(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Increase(int? value = null)
|
||||||
|
{
|
||||||
|
if (this._status == ProgressStatus.Pending)
|
||||||
|
this._status = ProgressStatus.InProgress;
|
||||||
|
else if (this.Status != ProgressStatus.InProgress)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (value.HasValue)
|
||||||
|
{
|
||||||
|
if (_progress + value.Value >= MaxProgress) return;
|
||||||
|
this.Report(_progress + value.Value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (_progress + StepProgress >= MaxProgress) return;
|
||||||
|
this.Report(_progress + StepProgress);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Finish()
|
||||||
|
{
|
||||||
|
this._status = ProgressStatus.Completed;
|
||||||
|
this.ForceReport(MaxProgress);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Cancel()
|
||||||
|
{
|
||||||
|
this._status = ProgressStatus.Canceled;
|
||||||
|
this._errorMessage = "User Cancelled";
|
||||||
|
this.ForceReport(_progress);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Error(string message)
|
||||||
|
{
|
||||||
|
this._status = ProgressStatus.Failed;
|
||||||
|
this._errorMessage = message;
|
||||||
|
this.ForceReport(_progress);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProgressReporter CreateChild(int proportion, int expectedSteps = 100)
|
||||||
|
{
|
||||||
|
var child = new ProgressReporter(proportion, expectedSteps);
|
||||||
|
child.Parent = this;
|
||||||
|
this.Child = child;
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ProgressTrackerService : BackgroundService
|
||||||
|
{
|
||||||
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
private readonly ConcurrentDictionary<string, TaskProgressInfo> _taskMap = new();
|
||||||
|
private readonly IHubContext<ProgressHub, IProgressReceiver> _hubContext;
|
||||||
|
|
||||||
|
private class TaskProgressInfo
|
||||||
|
{
|
||||||
|
public ProgressReporter Reporter { get; set; }
|
||||||
|
public string? ConnectionId { get; set; }
|
||||||
|
public required CancellationToken CancellationToken { get; set; }
|
||||||
|
public required CancellationTokenSource CancellationTokenSource { get; set; }
|
||||||
|
public required DateTime UpdatedAt { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProgressTrackerService(IHubContext<ProgressHub, IProgressReceiver> hubContext)
|
||||||
|
{
|
||||||
|
_hubContext = hubContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
foreach (var kvp in _taskMap)
|
||||||
|
{
|
||||||
|
var info = kvp.Value;
|
||||||
|
// 超过 1 分钟且任务已完成/失败/取消
|
||||||
|
if ((now - info.UpdatedAt).TotalMinutes > 1 &&
|
||||||
|
(info.Reporter.Status == ProgressStatus.Completed ||
|
||||||
|
info.Reporter.Status == ProgressStatus.Failed ||
|
||||||
|
info.Reporter.Status == ProgressStatus.Canceled))
|
||||||
|
{
|
||||||
|
_taskMap.TryRemove(kvp.Key, out _);
|
||||||
|
logger.Info($"Cleaned up task {kvp.Key}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, "Error during ProgressTracker cleanup");
|
||||||
|
}
|
||||||
|
await Task.Delay(TimeSpan.FromSeconds(30));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public (string, ProgressReporter) CreateTask(CancellationToken? cancellationToken = null)
|
||||||
|
{
|
||||||
|
CancellationTokenSource? cancellationTokenSource;
|
||||||
|
if (cancellationToken.HasValue)
|
||||||
|
{
|
||||||
|
cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken.Value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cancellationTokenSource = new CancellationTokenSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
var progressInfo = new TaskProgressInfo
|
||||||
|
{
|
||||||
|
ConnectionId = null,
|
||||||
|
UpdatedAt = DateTime.UtcNow,
|
||||||
|
CancellationToken = cancellationTokenSource.Token,
|
||||||
|
CancellationTokenSource = cancellationTokenSource,
|
||||||
|
};
|
||||||
|
|
||||||
|
var progress = new ProgressReporter(async value =>
|
||||||
|
{
|
||||||
|
cancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
// 通过 SignalR 推送进度
|
||||||
|
if (progressInfo.ConnectionId != null)
|
||||||
|
await _hubContext.Clients.Client(progressInfo.ConnectionId).OnReceiveProgress(progressInfo.Reporter);
|
||||||
|
});
|
||||||
|
|
||||||
|
progressInfo.Reporter = progress;
|
||||||
|
|
||||||
|
_taskMap.TryAdd(progressInfo.Reporter.TaskId, progressInfo);
|
||||||
|
|
||||||
|
return (progressInfo.Reporter.TaskId, progress);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<ProgressReporter> GetReporter(string taskId)
|
||||||
|
{
|
||||||
|
if (_taskMap.TryGetValue(taskId, out var info))
|
||||||
|
{
|
||||||
|
return info.Reporter;
|
||||||
|
}
|
||||||
|
return Optional<ProgressReporter>.None;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<ProgressStatus> GetProgressStatus(string taskId)
|
||||||
|
{
|
||||||
|
if (_taskMap.TryGetValue(taskId, out var info))
|
||||||
|
{
|
||||||
|
return info.Reporter.Status;
|
||||||
|
}
|
||||||
|
return Optional<ProgressStatus>.None;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool BindTask(string taskId, string connectionId)
|
||||||
|
{
|
||||||
|
if (_taskMap.TryGetValue(taskId, out var info) && info != null)
|
||||||
|
{
|
||||||
|
lock (info)
|
||||||
|
{
|
||||||
|
info.ConnectionId = connectionId;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CancelTask(string taskId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_taskMap.TryGetValue(taskId, out var info) && info != null)
|
||||||
|
{
|
||||||
|
lock (info)
|
||||||
|
{
|
||||||
|
info.CancellationTokenSource.Cancel();
|
||||||
|
info.Reporter.Cancel();
|
||||||
|
info.UpdatedAt = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, $"Failed to cancel task {taskId}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ using System.Net.Sockets;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using DotNext;
|
using DotNext;
|
||||||
using WebProtocol;
|
using WebProtocol;
|
||||||
|
using server.Services;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// UDP客户端发送池
|
/// UDP客户端发送池
|
||||||
@@ -228,15 +229,17 @@ public class UDPClientPool
|
|||||||
IPEndPoint endPoint, int taskID, uint devAddr, int timeout = 1000)
|
IPEndPoint endPoint, int taskID, uint devAddr, int timeout = 1000)
|
||||||
{
|
{
|
||||||
var ret = false;
|
var ret = false;
|
||||||
var opts = new SendAddrPackOptions();
|
var opts = new SendAddrPackOptions()
|
||||||
|
{
|
||||||
|
BurstType = BurstType.FixedBurst,
|
||||||
|
BurstLength = 0,
|
||||||
|
CommandID = Convert.ToByte(taskID),
|
||||||
|
Address = devAddr,
|
||||||
|
IsWrite = false,
|
||||||
|
};
|
||||||
|
|
||||||
opts.BurstType = BurstType.FixedBurst;
|
|
||||||
opts.BurstLength = 0;
|
|
||||||
opts.CommandID = Convert.ToByte(taskID);
|
|
||||||
opts.Address = devAddr;
|
|
||||||
|
|
||||||
// Read Jtag State Register
|
// Read Register
|
||||||
opts.IsWrite = false;
|
|
||||||
ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts));
|
ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts));
|
||||||
if (!ret) return new(new Exception("Send Address Package Failed!"));
|
if (!ret) return new(new Exception("Send Address Package Failed!"));
|
||||||
|
|
||||||
@@ -304,10 +307,11 @@ public class UDPClientPool
|
|||||||
/// <param name="devAddr">设备地址</param>
|
/// <param name="devAddr">设备地址</param>
|
||||||
/// <param name="result">期望的结果值</param>
|
/// <param name="result">期望的结果值</param>
|
||||||
/// <param name="resultMask">结果掩码,用于位校验</param>
|
/// <param name="resultMask">结果掩码,用于位校验</param>
|
||||||
|
/// <param name="waittime">等待间隔时间(毫秒)</param>
|
||||||
/// <param name="timeout">超时时间(毫秒)</param>
|
/// <param name="timeout">超时时间(毫秒)</param>
|
||||||
/// <returns>校验结果,true表示在超时前数据匹配期望值</returns>
|
/// <returns>校验结果,true表示在超时前数据匹配期望值</returns>
|
||||||
public static async ValueTask<Result<bool>> ReadAddrWithWait(
|
public static async ValueTask<Result<bool>> ReadAddrWithWait(
|
||||||
IPEndPoint endPoint, int taskID, uint devAddr, UInt32 result, UInt32 resultMask, int timeout = 1000)
|
IPEndPoint endPoint, int taskID, uint devAddr, UInt32 result, UInt32 resultMask, int waittime = 100, int timeout = 1000)
|
||||||
{
|
{
|
||||||
var address = endPoint.Address.ToString();
|
var address = endPoint.Address.ToString();
|
||||||
|
|
||||||
@@ -317,7 +321,7 @@ public class UDPClientPool
|
|||||||
var elapsed = DateTime.Now - startTime;
|
var elapsed = DateTime.Now - startTime;
|
||||||
if (elapsed >= TimeSpan.FromMilliseconds(timeout)) break;
|
if (elapsed >= TimeSpan.FromMilliseconds(timeout)) break;
|
||||||
var timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
var timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
||||||
|
await Task.Delay(waittime);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var ret = await ReadAddr(endPoint, taskID, devAddr, Convert.ToInt32(timeleft.TotalMilliseconds));
|
var ret = await ReadAddr(endPoint, taskID, devAddr, Convert.ToInt32(timeleft.TotalMilliseconds));
|
||||||
@@ -333,7 +337,7 @@ public class UDPClientPool
|
|||||||
$"Device {address} receive data is {retData.Length} bytes instead of 4 bytes"));
|
$"Device {address} receive data is {retData.Length} bytes instead of 4 bytes"));
|
||||||
|
|
||||||
// Check result
|
// Check result
|
||||||
var retCode = Convert.ToUInt32(Common.Number.BytesToUInt64(retData).Value);
|
var retCode = Convert.ToUInt32(Common.Number.BytesToUInt32(retData).Value);
|
||||||
if (Common.Number.BitsCheck(retCode, result, resultMask)) return true;
|
if (Common.Number.BitsCheck(retCode, result, resultMask)) return true;
|
||||||
}
|
}
|
||||||
catch (Exception error)
|
catch (Exception error)
|
||||||
@@ -358,12 +362,16 @@ public class UDPClientPool
|
|||||||
IPEndPoint endPoint, int taskID, UInt32 devAddr, int dataLength, int timeout = 1000)
|
IPEndPoint endPoint, int taskID, UInt32 devAddr, int dataLength, int timeout = 1000)
|
||||||
{
|
{
|
||||||
var ret = false;
|
var ret = false;
|
||||||
var opts = new SendAddrPackOptions();
|
var opts = new SendAddrPackOptions()
|
||||||
|
{
|
||||||
|
BurstLength = 0,
|
||||||
|
Address = 0,
|
||||||
|
BurstType = BurstType.FixedBurst,
|
||||||
|
CommandID = Convert.ToByte(taskID),
|
||||||
|
IsWrite = false,
|
||||||
|
};
|
||||||
var resultData = new List<byte>();
|
var resultData = new List<byte>();
|
||||||
|
|
||||||
opts.BurstType = BurstType.FixedBurst;
|
|
||||||
opts.CommandID = Convert.ToByte(taskID);
|
|
||||||
opts.IsWrite = false;
|
|
||||||
|
|
||||||
// Check Msg Bus
|
// Check Msg Bus
|
||||||
if (!MsgBus.IsRunning)
|
if (!MsgBus.IsRunning)
|
||||||
@@ -426,11 +434,12 @@ public class UDPClientPool
|
|||||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||||
/// <param name="taskID">任务ID</param>
|
/// <param name="taskID">任务ID</param>
|
||||||
/// <param name="devAddr">设备地址</param>
|
/// <param name="devAddr">设备地址</param>
|
||||||
|
/// <param name="burstType">突发类型</param>
|
||||||
/// <param name="dataLength">要读取的数据长度(4字节)</param>
|
/// <param name="dataLength">要读取的数据长度(4字节)</param>
|
||||||
/// <param name="timeout">超时时间(毫秒)</param>
|
/// <param name="timeout">超时时间(毫秒)</param>
|
||||||
/// <returns>读取结果,包含接收到的字节数组</returns>
|
/// <returns>读取结果,包含接收到的字节数组</returns>
|
||||||
public static async ValueTask<Result<byte[]>> ReadAddr4BytesAsync(
|
public static async ValueTask<Result<byte[]>> ReadAddr4BytesAsync(
|
||||||
IPEndPoint endPoint, int taskID, UInt32 devAddr, int dataLength, int timeout = 1000)
|
IPEndPoint endPoint, int taskID, UInt32 devAddr, int dataLength, BurstType burstType, int timeout = 1000)
|
||||||
{
|
{
|
||||||
var pkgList = new List<SendAddrPackage>();
|
var pkgList = new List<SendAddrPackage>();
|
||||||
var resultData = new List<byte>();
|
var resultData = new List<byte>();
|
||||||
@@ -453,11 +462,12 @@ public class UDPClientPool
|
|||||||
|
|
||||||
var opts = new SendAddrPackOptions
|
var opts = new SendAddrPackOptions
|
||||||
{
|
{
|
||||||
BurstType = BurstType.FixedBurst,
|
BurstType = burstType,
|
||||||
CommandID = Convert.ToByte(taskID),
|
CommandID = Convert.ToByte(taskID),
|
||||||
IsWrite = false,
|
IsWrite = false,
|
||||||
BurstLength = (byte)(currentSegmentSize - 1),
|
BurstLength = (byte)(currentSegmentSize - 1),
|
||||||
Address = devAddr + (uint)(i * max4BytesPerRead)
|
Address = (burstType == BurstType.ExtendBurst) ? (devAddr + (uint)(i * max4BytesPerRead)) : (devAddr),
|
||||||
|
// Address = devAddr + (uint)(i * max4BytesPerRead),
|
||||||
};
|
};
|
||||||
pkgList.Add(new SendAddrPackage(opts));
|
pkgList.Add(new SendAddrPackage(opts));
|
||||||
}
|
}
|
||||||
@@ -465,7 +475,7 @@ public class UDPClientPool
|
|||||||
// Send address packages in batches of 128, control outstanding
|
// Send address packages in batches of 128, control outstanding
|
||||||
int sentCount = 0;
|
int sentCount = 0;
|
||||||
var startTime = DateTime.Now;
|
var startTime = DateTime.Now;
|
||||||
const int batchSize = 64;
|
const int batchSize = 32;
|
||||||
while (sentCount < pkgList.Count)
|
while (sentCount < pkgList.Count)
|
||||||
{
|
{
|
||||||
var elapsed = DateTime.Now - startTime;
|
var elapsed = DateTime.Now - startTime;
|
||||||
@@ -477,7 +487,7 @@ public class UDPClientPool
|
|||||||
int outstanding = sentCount - (found.HasValue ? found.Value : 0);
|
int outstanding = sentCount - (found.HasValue ? found.Value : 0);
|
||||||
|
|
||||||
// If outstanding >= 512 - batchSize, wait for some data to be received
|
// If outstanding >= 512 - batchSize, wait for some data to be received
|
||||||
if (outstanding >= 512 - batchSize)
|
if (outstanding >= 128 - batchSize)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
||||||
@@ -519,9 +529,9 @@ public class UDPClientPool
|
|||||||
{
|
{
|
||||||
var bytes = udpDatas[i].Data;
|
var bytes = udpDatas[i].Data;
|
||||||
var expectedLen = ((pkgList[i].Options.BurstLength + 1) * 4);
|
var expectedLen = ((pkgList[i].Options.BurstLength + 1) * 4);
|
||||||
if ((bytes.Length - 4) != expectedLen)
|
if ((bytes.Length - 8) != expectedLen)
|
||||||
return new(new Exception($"Expected {expectedLen} bytes but received {bytes.Length - 4} bytes at segment {i}"));
|
return new(new Exception($"Expected {expectedLen} bytes but received {bytes.Length - 8} bytes at segment {i}"));
|
||||||
resultData.AddRange(bytes[4..]);
|
resultData.AddRange(bytes[8..]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate total data length
|
// Validate total data length
|
||||||
@@ -531,6 +541,42 @@ public class UDPClientPool
|
|||||||
return resultData.ToArray();
|
return resultData.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 顺序读取多个地址的数据,并合并BodyData后返回
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||||
|
/// <param name="taskID">任务ID</param>
|
||||||
|
/// <param name="addr">地址数组</param>
|
||||||
|
/// <param name="timeout">超时时间(毫秒)</param>
|
||||||
|
/// <returns>合并后的BodyData字节数组</returns>
|
||||||
|
public static async ValueTask<Result<byte[]>> ReadAddrSeq(IPEndPoint endPoint, int taskID, UInt32[] addr, int timeout = 1000)
|
||||||
|
{
|
||||||
|
var length = addr.Length;
|
||||||
|
var resultData = new List<byte>();
|
||||||
|
for (int i = 0; i < length; i++)
|
||||||
|
{
|
||||||
|
var ret = await ReadAddr(endPoint, taskID, addr[i], timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"ReadAddrSeq failed at index {i}: {ret.Error}");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"ReadAddrSeq failed at index {i}: Read not successful");
|
||||||
|
return new(new Exception($"ReadAddrSeq failed at index {i}"));
|
||||||
|
}
|
||||||
|
var data = ret.Value.Options.Data;
|
||||||
|
if (data is null)
|
||||||
|
{
|
||||||
|
logger.Error($"ReadAddrSeq got null data at index {i}");
|
||||||
|
return new(new Exception($"ReadAddrSeq got null data at index {i}"));
|
||||||
|
}
|
||||||
|
resultData.AddRange(data);
|
||||||
|
}
|
||||||
|
return resultData.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 向设备地址写入32位数据
|
/// 向设备地址写入32位数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -541,24 +587,29 @@ public class UDPClientPool
|
|||||||
/// <param name="timeout">超时时间(毫秒)</param>
|
/// <param name="timeout">超时时间(毫秒)</param>
|
||||||
/// <returns>写入结果,true表示写入成功</returns>
|
/// <returns>写入结果,true表示写入成功</returns>
|
||||||
public static async ValueTask<Result<bool>> WriteAddr(
|
public static async ValueTask<Result<bool>> WriteAddr(
|
||||||
IPEndPoint endPoint, int taskID, UInt32 devAddr, UInt32 data, int timeout = 1000)
|
IPEndPoint endPoint, int taskID, UInt32 devAddr,
|
||||||
|
UInt32 data, int timeout = 1000, ProgressReporter? progress = null)
|
||||||
{
|
{
|
||||||
var ret = false;
|
var ret = false;
|
||||||
var opts = new SendAddrPackOptions();
|
var opts = new SendAddrPackOptions()
|
||||||
|
{
|
||||||
|
BurstType = BurstType.FixedBurst,
|
||||||
|
BurstLength = 0,
|
||||||
|
CommandID = Convert.ToByte(taskID),
|
||||||
|
Address = devAddr,
|
||||||
|
IsWrite = true,
|
||||||
|
};
|
||||||
|
progress?.Report(20);
|
||||||
|
|
||||||
opts.BurstType = BurstType.FixedBurst;
|
// Write Register
|
||||||
opts.BurstLength = 0;
|
|
||||||
opts.CommandID = Convert.ToByte(taskID);
|
|
||||||
opts.Address = devAddr;
|
|
||||||
|
|
||||||
// Write Jtag State Register
|
|
||||||
opts.IsWrite = true;
|
|
||||||
ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts));
|
ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts));
|
||||||
if (!ret) return new(new Exception("Send 1st address package failed!"));
|
if (!ret) return new(new Exception("Send 1st address package failed!"));
|
||||||
|
progress?.Report(40);
|
||||||
// Send Data Package
|
// Send Data Package
|
||||||
ret = await UDPClientPool.SendDataPackAsync(endPoint,
|
ret = await UDPClientPool.SendDataPackAsync(endPoint,
|
||||||
new SendDataPackage(Common.Number.NumberToBytes(data, 4).Value));
|
new SendDataPackage(Common.Number.NumberToBytes(data, 4).Value));
|
||||||
if (!ret) return new(new Exception("Send data package failed!"));
|
if (!ret) return new(new Exception("Send data package failed!"));
|
||||||
|
progress?.Report(60);
|
||||||
|
|
||||||
// Check Msg Bus
|
// Check Msg Bus
|
||||||
if (!MsgBus.IsRunning)
|
if (!MsgBus.IsRunning)
|
||||||
@@ -568,6 +619,7 @@ public class UDPClientPool
|
|||||||
var udpWriteAck = await MsgBus.UDPServer.WaitForAckAsync(
|
var udpWriteAck = await MsgBus.UDPServer.WaitForAckAsync(
|
||||||
endPoint.Address.ToString(), taskID, endPoint.Port, timeout);
|
endPoint.Address.ToString(), taskID, endPoint.Port, timeout);
|
||||||
if (!udpWriteAck.IsSuccessful) return new(udpWriteAck.Error);
|
if (!udpWriteAck.IsSuccessful) return new(udpWriteAck.Error);
|
||||||
|
progress?.Finish();
|
||||||
|
|
||||||
return udpWriteAck.Value.IsSuccessful;
|
return udpWriteAck.Value.IsSuccessful;
|
||||||
}
|
}
|
||||||
@@ -582,32 +634,38 @@ public class UDPClientPool
|
|||||||
/// <param name="timeout">超时时间(毫秒)</param>
|
/// <param name="timeout">超时时间(毫秒)</param>
|
||||||
/// <returns>写入结果,true表示写入成功</returns>
|
/// <returns>写入结果,true表示写入成功</returns>
|
||||||
public static async ValueTask<Result<bool>> WriteAddr(
|
public static async ValueTask<Result<bool>> WriteAddr(
|
||||||
IPEndPoint endPoint, int taskID, UInt32 devAddr, byte[] dataArray, int timeout = 1000)
|
IPEndPoint endPoint, int taskID, UInt32 devAddr,
|
||||||
|
byte[] dataArray, int timeout = 1000, ProgressReporter? progress = null)
|
||||||
{
|
{
|
||||||
var ret = false;
|
var ret = false;
|
||||||
var opts = new SendAddrPackOptions();
|
var opts = new SendAddrPackOptions()
|
||||||
|
{
|
||||||
|
BurstType = BurstType.FixedBurst,
|
||||||
|
CommandID = Convert.ToByte(taskID),
|
||||||
|
Address = devAddr,
|
||||||
|
BurstLength = 0,
|
||||||
|
IsWrite = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
var max4BytesPerRead = 128; // 1024 bytes per read
|
||||||
opts.BurstType = BurstType.FixedBurst;
|
|
||||||
opts.CommandID = Convert.ToByte(taskID);
|
|
||||||
opts.Address = devAddr;
|
|
||||||
|
|
||||||
// Check Msg Bus
|
// Check Msg Bus
|
||||||
if (!MsgBus.IsRunning)
|
if (!MsgBus.IsRunning)
|
||||||
return new(new Exception("Message bus not working!"));
|
return new(new Exception("Message bus not working!"));
|
||||||
|
|
||||||
opts.IsWrite = true;
|
var hasRest = dataArray.Length % (max4BytesPerRead * (32 / 8)) != 0;
|
||||||
var hasRest = dataArray.Length % (256 * (32 / 8)) != 0;
|
|
||||||
var writeTimes = hasRest ?
|
var writeTimes = hasRest ?
|
||||||
dataArray.Length / (256 * (32 / 8)) + 1 :
|
dataArray.Length / (max4BytesPerRead * (32 / 8)) + 1 :
|
||||||
dataArray.Length / (256 * (32 / 8));
|
dataArray.Length / (max4BytesPerRead * (32 / 8));
|
||||||
|
if (progress != null)
|
||||||
|
progress.ExpectedSteps = writeTimes;
|
||||||
for (var i = 0; i < writeTimes; i++)
|
for (var i = 0; i < writeTimes; i++)
|
||||||
{
|
{
|
||||||
// Sperate Data Array
|
// Sperate Data Array
|
||||||
var isLastData = i == writeTimes - 1;
|
var isLastData = i == writeTimes - 1;
|
||||||
var sendDataArray = isLastData ?
|
var sendDataArray = isLastData ?
|
||||||
dataArray[(i * (256 * (32 / 8)))..] :
|
dataArray[(i * (max4BytesPerRead * (32 / 8)))..] :
|
||||||
dataArray[(i * (256 * (32 / 8)))..((i + 1) * (256 * (32 / 8)))];
|
dataArray[(i * (max4BytesPerRead * (32 / 8)))..((i + 1) * (max4BytesPerRead * (32 / 8)))];
|
||||||
|
|
||||||
// Calculate BurstLength
|
// Calculate BurstLength
|
||||||
opts.BurstLength = ((byte)(
|
opts.BurstLength = ((byte)(
|
||||||
@@ -629,6 +687,45 @@ public class UDPClientPool
|
|||||||
|
|
||||||
if (!udpWriteAck.Value.IsSuccessful)
|
if (!udpWriteAck.Value.IsSuccessful)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
progress?.Increase();
|
||||||
|
}
|
||||||
|
|
||||||
|
progress?.Finish();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="endPoint">[TODO:parameter]</param>
|
||||||
|
/// <param name="taskID">[TODO:parameter]</param>
|
||||||
|
/// <param name="addr">[TODO:parameter]</param>
|
||||||
|
/// <param name="data">[TODO:parameter]</param>
|
||||||
|
/// <param name="timeout">[TODO:parameter]</param>
|
||||||
|
/// <returns>[TODO:return]</returns>
|
||||||
|
public static async ValueTask<Result<bool>> WriteAddrSeq(IPEndPoint endPoint, int taskID, UInt32[] addr, byte[] data, int timeout = 1000)
|
||||||
|
{
|
||||||
|
var length = addr.Length;
|
||||||
|
if (length != data.Length)
|
||||||
|
{
|
||||||
|
logger.Error($"TODO");
|
||||||
|
return new(new ArgumentException($"TODO"));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++)
|
||||||
|
{
|
||||||
|
var ret = await WriteAddr(endPoint, taskID, addr[i], (UInt32)data[i], timeout);
|
||||||
|
if (!ret.IsSuccessful)
|
||||||
|
{
|
||||||
|
logger.Error($"TODO");
|
||||||
|
return new(ret.Error);
|
||||||
|
}
|
||||||
|
if (!ret.Value)
|
||||||
|
{
|
||||||
|
logger.Error($"TODO");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.NetworkInformation; // 添加这个引用
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Common;
|
||||||
using DotNext;
|
using DotNext;
|
||||||
using DotNext.Threading;
|
using DotNext.Threading;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
@@ -15,6 +18,10 @@ public class UDPData
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public required DateTime DateTime { get; set; }
|
public required DateTime DateTime { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// 数据包时间戳
|
||||||
|
/// </summary>
|
||||||
|
public required UInt32 Timestamp { get; set; }
|
||||||
|
/// <summary>
|
||||||
/// 发送来源的IP地址
|
/// 发送来源的IP地址
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public required string Address { get; set; }
|
public required string Address { get; set; }
|
||||||
@@ -47,6 +54,7 @@ public class UDPData
|
|||||||
return new UDPData()
|
return new UDPData()
|
||||||
{
|
{
|
||||||
DateTime = this.DateTime,
|
DateTime = this.DateTime,
|
||||||
|
Timestamp = this.Timestamp,
|
||||||
Address = new string(this.Address),
|
Address = new string(this.Address),
|
||||||
Port = this.Port,
|
Port = this.Port,
|
||||||
TaskID = this.TaskID,
|
TaskID = this.TaskID,
|
||||||
@@ -72,19 +80,22 @@ public class UDPServer
|
|||||||
{
|
{
|
||||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
private Dictionary<string, Queue<UDPData>> udpData = new Dictionary<string, Queue<UDPData>>();
|
private ConcurrentDictionary<string, SortedList<UInt32, UDPData>> udpData
|
||||||
|
= new ConcurrentDictionary<string, SortedList<UInt32, UDPData>>();
|
||||||
private Semaphore taskPool = new Semaphore(3, 3);
|
private readonly AsyncReaderWriterLock udpDataLock = new AsyncReaderWriterLock();
|
||||||
|
|
||||||
private int listenPort;
|
private int listenPort;
|
||||||
private UdpClient listener;
|
private List<UdpClient> listeners = new List<UdpClient>();
|
||||||
|
private List<Task> tasks = new List<Task>();
|
||||||
private IPEndPoint groupEP;
|
private IPEndPoint groupEP;
|
||||||
|
|
||||||
private bool isRunning = false;
|
private CancellationTokenSource? cancellationTokenSource;
|
||||||
|
private bool disposed = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否正在工作
|
/// 是否正在工作
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsRunning { get { return isRunning; } }
|
public bool IsRunning => cancellationTokenSource?.Token.IsCancellationRequested == false;
|
||||||
|
|
||||||
/// <summary> UDP 服务器的错误代码 </summary>
|
/// <summary> UDP 服务器的错误代码 </summary>
|
||||||
public enum ErrorCode
|
public enum ErrorCode
|
||||||
@@ -103,15 +114,27 @@ public class UDPServer
|
|||||||
/// Construct a udp server with fixed port
|
/// Construct a udp server with fixed port
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="port"> Device UDP Port </param>
|
/// <param name="port"> Device UDP Port </param>
|
||||||
|
/// <param name="num"> UDP Client Num </param>
|
||||||
/// <returns> UDPServer class </returns>
|
/// <returns> UDPServer class </returns>
|
||||||
public UDPServer(int port)
|
public UDPServer(int port, int num)
|
||||||
{
|
{
|
||||||
// Construction
|
// Construction
|
||||||
listenPort = port;
|
this.listenPort = port;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
listener = new UdpClient(listenPort);
|
for (int i = 0; i < num; i++)
|
||||||
groupEP = new IPEndPoint(IPAddress.Any, listenPort);
|
{
|
||||||
|
int currentPort = this.listenPort + i;
|
||||||
|
if (IsPortInUse(currentPort))
|
||||||
|
{
|
||||||
|
throw new ArgumentException(
|
||||||
|
$"端口{currentPort}已被占用,无法启动UDP Server",
|
||||||
|
nameof(port)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
listeners.Add(new UdpClient(currentPort));
|
||||||
|
}
|
||||||
|
this.groupEP = new IPEndPoint(IPAddress.Any, listenPort);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@@ -123,6 +146,29 @@ public class UDPServer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool IsPortInUse(int port)
|
||||||
|
{
|
||||||
|
bool inUse = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
|
||||||
|
var udpListeners = ipGlobalProperties.GetActiveUdpListeners();
|
||||||
|
foreach (var ep in udpListeners)
|
||||||
|
{
|
||||||
|
if (ep.Port == port)
|
||||||
|
{
|
||||||
|
inUse = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Warn($"Failed to check port usage for port {port}: {ex.Message}");
|
||||||
|
}
|
||||||
|
return inUse;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步寻找目标发送的内容
|
/// 异步寻找目标发送的内容
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -144,33 +190,37 @@ public class UDPServer
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
UDPData? data = null;
|
UDPData? data = null;
|
||||||
|
var key = $"{ipAddr}-{taskID}";
|
||||||
// logger.Debug($"Caller \"{callerName}|{callerLineNum}\": Try to find {ipAddr}-{taskID} UDP Data");
|
|
||||||
|
|
||||||
var startTime = DateTime.Now;
|
var startTime = DateTime.Now;
|
||||||
var isTimeout = false;
|
var isTimeout = false;
|
||||||
var timeleft = TimeSpan.FromMilliseconds(timeout);
|
|
||||||
while (!isTimeout)
|
while (!isTimeout)
|
||||||
{
|
{
|
||||||
var elapsed = DateTime.Now - startTime;
|
var elapsed = DateTime.Now - startTime;
|
||||||
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
|
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
|
||||||
if (isTimeout) break;
|
if (isTimeout) break;
|
||||||
|
|
||||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
try
|
||||||
using (await udpData.AcquireWriteLockAsync(timeleft))
|
|
||||||
{
|
{
|
||||||
if (udpData.ContainsKey($"{ipAddr}-{taskID}") &&
|
using (await udpDataLock.AcquireWriteLockAsync(TimeSpan.FromMilliseconds(timeout)))
|
||||||
udpData.TryGetValue($"{ipAddr}-{taskID}", out var dataQueue) &&
|
|
||||||
dataQueue.Count > 0)
|
|
||||||
{
|
{
|
||||||
data = dataQueue.Dequeue();
|
if (udpData.TryGetValue(key, out var sortedList) && sortedList.Count > 0)
|
||||||
// logger.Debug($"Find UDP Data: {data.ToString()}");
|
{
|
||||||
|
// 获取最早的数据(第一个元素)
|
||||||
|
var firstKey = sortedList.Keys[0];
|
||||||
|
data = sortedList[firstKey];
|
||||||
|
sortedList.RemoveAt(0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.Delay(cycle);
|
|
||||||
}
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
logger.Trace("Get nothing even after time out");
|
||||||
|
return new(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (data is null)
|
if (data is null)
|
||||||
{
|
{
|
||||||
@@ -193,29 +243,37 @@ public class UDPServer
|
|||||||
public async ValueTask<Optional<List<UDPData>>> FindDataArrayAsync(string ipAddr, int taskID, int timeout = 1000)
|
public async ValueTask<Optional<List<UDPData>>> FindDataArrayAsync(string ipAddr, int taskID, int timeout = 1000)
|
||||||
{
|
{
|
||||||
List<UDPData>? data = null;
|
List<UDPData>? data = null;
|
||||||
|
var key = $"{ipAddr}-{taskID}";
|
||||||
|
|
||||||
|
|
||||||
var startTime = DateTime.Now;
|
var startTime = DateTime.Now;
|
||||||
var isTimeout = false;
|
var isTimeout = false;
|
||||||
var timeleft = TimeSpan.FromMilliseconds(timeout);
|
|
||||||
while (!isTimeout)
|
while (!isTimeout)
|
||||||
{
|
{
|
||||||
var elapsed = DateTime.Now - startTime;
|
var elapsed = DateTime.Now - startTime;
|
||||||
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
|
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
|
||||||
if (isTimeout) break;
|
if (isTimeout) break;
|
||||||
|
|
||||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
try
|
||||||
using (await udpData.AcquireWriteLockAsync(timeleft))
|
|
||||||
{
|
{
|
||||||
if (udpData.ContainsKey($"{ipAddr}-{taskID}") &&
|
using (await udpDataLock.AcquireWriteLockAsync(TimeSpan.FromMilliseconds(timeout)))
|
||||||
udpData.TryGetValue($"{ipAddr}-{taskID}", out var dataQueue) &&
|
|
||||||
dataQueue.Count > 0)
|
|
||||||
{
|
{
|
||||||
data = dataQueue.ToList();
|
if (udpData.TryGetValue(key, out var sortedList) && sortedList.Count > 0)
|
||||||
dataQueue.Clear();
|
{
|
||||||
|
data = new List<UDPData>(sortedList.Values);
|
||||||
|
// 输出数据
|
||||||
|
// PrintDataArray(data);
|
||||||
|
sortedList.Clear();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
logger.Trace("Get nothing even after time out");
|
||||||
|
return new(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (data is null)
|
if (data is null)
|
||||||
{
|
{
|
||||||
@@ -239,28 +297,22 @@ public class UDPServer
|
|||||||
{
|
{
|
||||||
List<UDPData>? data = null;
|
List<UDPData>? data = null;
|
||||||
|
|
||||||
var startTime = DateTime.Now;
|
try
|
||||||
var isTimeout = false;
|
|
||||||
var timeleft = TimeSpan.FromMilliseconds(timeout);
|
|
||||||
while (!isTimeout)
|
|
||||||
{
|
{
|
||||||
var elapsed = DateTime.Now - startTime;
|
using (await udpDataLock.AcquireReadLockAsync(TimeSpan.FromMilliseconds(timeout)))
|
||||||
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
|
|
||||||
if (isTimeout) break;
|
|
||||||
|
|
||||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
|
||||||
using (await udpData.AcquireReadLockAsync(timeleft))
|
|
||||||
{
|
{
|
||||||
if (udpData.ContainsKey($"{ipAddr}-{taskID}") &&
|
var key = $"{ipAddr}-{taskID}";
|
||||||
udpData.TryGetValue($"{ipAddr}-{taskID}", out var dataQueue) &&
|
if (udpData.TryGetValue(key, out var sortedList) && sortedList.Count > 0)
|
||||||
dataQueue.Count > 0)
|
|
||||||
{
|
{
|
||||||
data = dataQueue.ToList();
|
data = new List<UDPData>(sortedList.Values);
|
||||||
// logger.Debug($"Find UDP Data Array: {JsonConvert.SerializeObject(data)}");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (TimeoutException)
|
||||||
|
{
|
||||||
|
logger.Trace("Failed to acquire read lock within timeout");
|
||||||
|
return new(null);
|
||||||
|
}
|
||||||
|
|
||||||
if (data is null)
|
if (data is null)
|
||||||
{
|
{
|
||||||
@@ -284,26 +336,22 @@ public class UDPServer
|
|||||||
{
|
{
|
||||||
int? count = null;
|
int? count = null;
|
||||||
|
|
||||||
var startTime = DateTime.Now;
|
try
|
||||||
var isTimeout = false;
|
|
||||||
var timeleft = TimeSpan.FromMilliseconds(timeout);
|
|
||||||
while (!isTimeout)
|
|
||||||
{
|
{
|
||||||
var elapsed = DateTime.Now - startTime;
|
using (await udpDataLock.AcquireReadLockAsync(TimeSpan.FromMilliseconds(timeout)))
|
||||||
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
|
|
||||||
if (isTimeout) break;
|
|
||||||
|
|
||||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
|
||||||
using (await udpData.AcquireReadLockAsync(timeleft))
|
|
||||||
{
|
{
|
||||||
if (udpData.ContainsKey($"{ipAddr}-{taskID}") &&
|
var key = $"{ipAddr}-{taskID}";
|
||||||
udpData.TryGetValue($"{ipAddr}-{taskID}", out var dataQueue))
|
if (udpData.TryGetValue(key, out var sortedList))
|
||||||
{
|
{
|
||||||
count = dataQueue.Count;
|
count = sortedList.Count;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (TimeoutException)
|
||||||
|
{
|
||||||
|
logger.Trace("Failed to acquire read lock within timeout");
|
||||||
|
return Optional<int>.None;
|
||||||
|
}
|
||||||
|
|
||||||
if (count is null)
|
if (count is null)
|
||||||
{
|
{
|
||||||
@@ -367,82 +415,74 @@ public class UDPServer
|
|||||||
|
|
||||||
return retPack.Value;
|
return retPack.Value;
|
||||||
}
|
}
|
||||||
static int ReceiveHandleCcount = 0;
|
|
||||||
|
|
||||||
private void ReceiveHandler(IAsyncResult res)
|
private async Task ReceiveHandler(byte[] data, IPEndPoint endPoint, DateTime time)
|
||||||
|
{
|
||||||
|
// 异步锁保护 udpData
|
||||||
|
await Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var remoteEP = new IPEndPoint(IPAddress.Any, listenPort);
|
|
||||||
byte[] bytes = listener.EndReceive(res, ref remoteEP);
|
|
||||||
|
|
||||||
// 提前开始接收下一个包
|
|
||||||
listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);
|
|
||||||
logger.Debug($"Test ReceiveHandler Count = {ReceiveHandleCcount}");
|
|
||||||
ReceiveHandleCcount++;
|
|
||||||
|
|
||||||
// Handle RemoteEP
|
// Handle RemoteEP
|
||||||
if (remoteEP is null)
|
if (endPoint is null)
|
||||||
{
|
{
|
||||||
logger.Debug($"Receive Data from Unknown at {DateTime.Now.ToString()}:");
|
logger.Debug($"Receive Data from Unknown at {DateTime.Now.ToString()}:");
|
||||||
logger.Debug($" Original Data : {BitConverter.ToString(bytes).Replace("-", " ")}");
|
logger.Debug($" Original Data : {BitConverter.ToString(data).Replace("-", " ")}");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 异步处理数据包
|
var udpDataObj = await RecordUDPData(data, endPoint, time, Convert.ToInt32(data[1 + 4]));
|
||||||
Task.Run(() =>
|
// PrintData(udpDataObj);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
var udpData = RecordUDPData(bytes, remoteEP, Convert.ToInt32(bytes[1]));
|
logger.Error($"Got Error when handle receive:{e}");
|
||||||
PrintData(udpData);
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool SendBytes(IPEndPoint endPoint, byte[] buf)
|
private async Task<UDPData> RecordUDPData(byte[] bytes, IPEndPoint remoteEP, DateTime time, int taskID)
|
||||||
{
|
|
||||||
var sendLen = listener.Send(buf, endPoint);
|
|
||||||
|
|
||||||
if (sendLen == buf.Length) { return true; }
|
|
||||||
else { return false; }
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool SendString(IPEndPoint endPoint, string text)
|
|
||||||
{
|
|
||||||
byte[] buf = Encoding.ASCII.GetBytes(text);
|
|
||||||
var sendLen = listener.Send(buf, endPoint);
|
|
||||||
|
|
||||||
if (sendLen == buf.Length) { return true; }
|
|
||||||
else { return false; }
|
|
||||||
}
|
|
||||||
|
|
||||||
private UDPData RecordUDPData(byte[] bytes, IPEndPoint remoteEP, int taskID)
|
|
||||||
{
|
{
|
||||||
var remoteAddress = remoteEP.Address.ToString();
|
var remoteAddress = remoteEP.Address.ToString();
|
||||||
var remotePort = remoteEP.Port;
|
var remotePort = remoteEP.Port;
|
||||||
var data = new UDPData()
|
var data = new UDPData()
|
||||||
{
|
{
|
||||||
|
DateTime = time,
|
||||||
|
Timestamp = Number.BytesToUInt32(bytes[..4]).Value,
|
||||||
Address = remoteAddress,
|
Address = remoteAddress,
|
||||||
Port = remotePort,
|
Port = remotePort,
|
||||||
TaskID = taskID,
|
TaskID = taskID,
|
||||||
Data = bytes,
|
Data = bytes,
|
||||||
DateTime = DateTime.Now,
|
|
||||||
HasRead = false,
|
HasRead = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
using (udpData.AcquireWriteLock())
|
var key = $"{remoteAddress}-{taskID}";
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
// Record UDP Receive Data
|
using (await udpDataLock.AcquireWriteLockAsync(TimeSpan.FromMilliseconds(5000)))
|
||||||
if (udpData.ContainsKey($"{remoteAddress}-{taskID}") &&
|
|
||||||
udpData.TryGetValue($"{remoteAddress}-{taskID}", out var dataQueue))
|
|
||||||
{
|
{
|
||||||
dataQueue.Enqueue(data);
|
var sortedList = udpData.GetOrAdd(key, _ => new SortedList<UInt32, UDPData>());
|
||||||
logger.Debug($"Test Lock dataQueue.Count = {dataQueue.Count}");
|
|
||||||
|
// 处理相同时间戳的情况,添加微小的时间差
|
||||||
|
var uniqueTime = data.Timestamp;
|
||||||
|
while (sortedList.ContainsKey(uniqueTime))
|
||||||
|
{
|
||||||
|
logger.Warn(
|
||||||
|
$"Duplicate timestamp detected for {remoteAddress}:{remotePort} at {uniqueTime}.");
|
||||||
|
uniqueTime += 1;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
sortedList.Add(uniqueTime, data);
|
||||||
var queue = new Queue<UDPData>();
|
// 输出单个数据
|
||||||
queue.Enqueue(data);
|
// PrintData(data);
|
||||||
udpData.Add($"{remoteAddress}-{taskID}", queue);
|
|
||||||
logger.Trace("Receive data from new client");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (TimeoutException)
|
||||||
|
{
|
||||||
|
logger.Error($"Failed to acquire write lock for recording UDP data from {remoteAddress}:{remotePort}");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@@ -451,21 +491,12 @@ public class UDPServer
|
|||||||
/// 输出UDP Data到log中
|
/// 输出UDP Data到log中
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">UDP数据</param>
|
/// <param name="data">UDP数据</param>
|
||||||
public void PrintData(UDPData data)
|
public string PrintData(UDPData data)
|
||||||
{
|
{
|
||||||
var bytes = data.Data;
|
var bytes = data.Data;
|
||||||
var sign = bytes[0];
|
var sign = bytes[4];
|
||||||
string recvData = "";
|
string recvData = "";
|
||||||
if (sign == (byte)WebProtocol.PackSign.SendAddr)
|
if (sign == (byte)WebProtocol.PackSign.RecvData)
|
||||||
{
|
|
||||||
var resData = WebProtocol.SendAddrPackage.FromBytes(bytes);
|
|
||||||
if (resData.IsSuccessful)
|
|
||||||
recvData = resData.Value.ToString();
|
|
||||||
else
|
|
||||||
recvData = resData.Error.ToString();
|
|
||||||
}
|
|
||||||
else if (sign == (byte)WebProtocol.PackSign.SendData) { }
|
|
||||||
else if (sign == (byte)WebProtocol.PackSign.RecvData)
|
|
||||||
{
|
{
|
||||||
var resData = WebProtocol.RecvDataPackage.FromBytes(bytes);
|
var resData = WebProtocol.RecvDataPackage.FromBytes(bytes);
|
||||||
if (resData.IsSuccessful)
|
if (resData.IsSuccessful)
|
||||||
@@ -486,30 +517,55 @@ public class UDPServer
|
|||||||
recvData = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
|
recvData = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
// logger.Debug($"Receive Data from {data.Address}:{data.Port} at {data.DateTime.ToString()}:");
|
logger.Debug($"Receive Data from {data.Address}:{data.Port} at {data.DateTime.ToString()} - {data.Timestamp}:");
|
||||||
// logger.Debug($" Original Data : {BitConverter.ToString(bytes).Replace("-", " ")}");
|
logger.Debug($" Original Data : {BitConverter.ToString(bytes).Replace("-", " ")}");
|
||||||
// if (recvData.Length != 0) logger.Debug($" Decoded Data : {recvData}");
|
if (recvData.Length != 0) logger.Debug($" Decoded Data : {recvData}");
|
||||||
|
return $@"
|
||||||
|
Receive Data from {data.Address}:{data.Port} at {data.DateTime.ToString()}:
|
||||||
|
Original Data : {BitConverter.ToString(bytes).Replace("-", " ")}
|
||||||
|
Decoded Data : {recvData}
|
||||||
|
";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 输出UDP Data数组到log中
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataArray">UDP数据列表</param>
|
||||||
|
public void PrintDataArray(IEnumerable<UDPData> dataArray)
|
||||||
|
{
|
||||||
|
foreach (var data in dataArray)
|
||||||
|
{
|
||||||
|
logger.Debug(PrintData(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将所有数据输出到log中
|
/// 将所有数据输出到log中
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns> void </returns>
|
/// <returns> void </returns>
|
||||||
public void PrintAllData()
|
public async Task PrintAllDataAsync()
|
||||||
{
|
{
|
||||||
using (udpData.AcquireReadLock())
|
logger.Debug("Ready Data:");
|
||||||
{
|
|
||||||
// logger.Debug("Ready Data:");
|
|
||||||
|
|
||||||
foreach (var ip in udpData)
|
try
|
||||||
{
|
{
|
||||||
foreach (var data in ip.Value)
|
using (await udpDataLock.AcquireReadLockAsync(TimeSpan.FromMilliseconds(5000)))
|
||||||
{
|
{
|
||||||
// logger.Debug(data.ToString());
|
foreach (var kvp in udpData)
|
||||||
|
{
|
||||||
|
foreach (var data in kvp.Value.Values)
|
||||||
|
{
|
||||||
|
logger.Debug(PrintData(data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (TimeoutException)
|
||||||
|
{
|
||||||
|
logger.Error("Failed to acquire read lock for printing all data");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 清空指定IP地址的数据
|
/// 清空指定IP地址的数据
|
||||||
@@ -517,19 +573,19 @@ public class UDPServer
|
|||||||
/// <param name="ipAddr">IP地址</param>
|
/// <param name="ipAddr">IP地址</param>
|
||||||
/// <param name="taskID">[TODO:parameter]</param>
|
/// <param name="taskID">[TODO:parameter]</param>
|
||||||
/// <returns>无</returns>
|
/// <returns>无</returns>
|
||||||
public async Task ClearUDPData(string ipAddr, int taskID)
|
public void ClearUDPData(string ipAddr, int taskID)
|
||||||
{
|
{
|
||||||
using (await udpData.AcquireWriteLockAsync())
|
var key = $"{ipAddr}-{taskID}";
|
||||||
|
|
||||||
|
using (udpDataLock.AcquireWriteLock())
|
||||||
{
|
{
|
||||||
if (udpData.ContainsKey($"{ipAddr}-{taskID}") &&
|
if (udpData.TryGetValue(key, out var sortedList))
|
||||||
udpData.TryGetValue($"{ipAddr}-{taskID}", out var dataQueue) &&
|
|
||||||
dataQueue.Count > 0)
|
|
||||||
{
|
{
|
||||||
dataQueue.Clear();
|
sortedList.Clear();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Start UDP Server
|
/// Start UDP Server
|
||||||
@@ -537,17 +593,57 @@ public class UDPServer
|
|||||||
/// <returns>None</returns>
|
/// <returns>None</returns>
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
|
if (cancellationTokenSource != null && !cancellationTokenSource.Token.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
logger.Warn("UDP Server is already running");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cancellationTokenSource = new CancellationTokenSource();
|
||||||
|
var cancellationToken = cancellationTokenSource.Token;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
this.listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);
|
foreach (var client in listeners)
|
||||||
|
{
|
||||||
|
tasks.Add(Task.Run(async () =>
|
||||||
|
{
|
||||||
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 使用 CancellationToken 来取消接收操作
|
||||||
|
var result = await client.ReceiveAsync(cancellationToken);
|
||||||
|
_ = ReceiveHandler(result.Buffer, result.RemoteEndPoint, DateTime.Now);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
logger.Debug("UDP receive operation was cancelled");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (ObjectDisposedException)
|
||||||
|
{
|
||||||
|
logger.Debug("UDP client was disposed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
if (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
logger.Error($"Error in UDP receive: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, cancellationToken));
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info("UDP Server started successfully");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e.ToString());
|
logger.Error($"Failed to start UDP server: {e}");
|
||||||
}
|
cancellationTokenSource?.Cancel();
|
||||||
finally
|
throw;
|
||||||
{
|
|
||||||
this.isRunning = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -557,8 +653,71 @@ public class UDPServer
|
|||||||
/// <returns>None</returns>
|
/// <returns>None</returns>
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
this.listener.Close();
|
if (cancellationTokenSource == null || cancellationTokenSource.Token.IsCancellationRequested)
|
||||||
this.isRunning = false;
|
{
|
||||||
|
logger.Warn("UDP Server is not running or already stopped");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
logger.Info("Stopping UDP Server...");
|
||||||
|
|
||||||
|
// 取消所有操作
|
||||||
|
cancellationTokenSource.Cancel();
|
||||||
|
|
||||||
|
// 等待所有任务完成,设置超时时间
|
||||||
|
var waitTasks = Task.WhenAll(tasks);
|
||||||
|
if (!waitTasks.Wait(TimeSpan.FromSeconds(5)))
|
||||||
|
{
|
||||||
|
logger.Warn("Some tasks did not complete within timeout period");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关闭所有UDP客户端
|
||||||
|
foreach (var client in listeners)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.Close();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Warn($"Error closing UDP client: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清理任务列表
|
||||||
|
tasks.Clear();
|
||||||
|
|
||||||
|
logger.Info("UDP Server stopped successfully");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error($"Error stopping UDP server: {ex.Message}");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
cancellationTokenSource?.Dispose();
|
||||||
|
cancellationTokenSource = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 实现IDisposable接口,确保资源正确释放
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (!disposed)
|
||||||
|
{
|
||||||
|
Stop();
|
||||||
|
|
||||||
|
foreach (var client in listeners)
|
||||||
|
{
|
||||||
|
client?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
udpDataLock?.Dispose();
|
||||||
|
disposed = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using Common;
|
||||||
using DotNext;
|
using DotNext;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
@@ -35,32 +36,32 @@ namespace WebProtocol
|
|||||||
/// 突发类型
|
/// 突发类型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <example>0</example>
|
/// <example>0</example>
|
||||||
public BurstType BurstType { get; set; }
|
public required BurstType BurstType { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 任务ID
|
/// 任务ID
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <example>1</example>
|
/// <example>1</example>
|
||||||
public byte CommandID { get; set; }
|
public required byte CommandID { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 标识写入还是读取
|
/// 标识写入还是读取
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <example>true</example>
|
/// <example>true</example>
|
||||||
public bool IsWrite { get; set; }
|
public required bool IsWrite { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 突发长度:0是32bits,255是32bits x 256
|
/// 突发长度:0是32bits,255是32bits x 256
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <example>255</example>
|
/// <example>255</example>
|
||||||
public byte BurstLength { get; set; }
|
public required byte BurstLength { get; set; }
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 目标地址
|
/// 目标地址
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <example>0</example>
|
/// <example>0</example>
|
||||||
public UInt32 Address { get; set; }
|
public required UInt32 Address { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 转换为Json格式字符串
|
/// 转换为Json格式字符串
|
||||||
@@ -84,23 +85,29 @@ namespace WebProtocol
|
|||||||
WriteResp
|
WriteResp
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 时间戳
|
||||||
|
/// </summary>
|
||||||
|
/// <example>1234567</example>
|
||||||
|
public required UInt32 Timestamp { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 数据包类型
|
/// 数据包类型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <example>0</example>
|
/// <example>0</example>
|
||||||
public PackType Type { get; set; }
|
public required PackType Type { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Task ID
|
/// Task ID
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <example>0</example>
|
/// <example>0</example>
|
||||||
public byte CommandID { get; set; }
|
public required byte CommandID { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether is succeed to finish command
|
/// Whether is succeed to finish command
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <example>true</example>
|
/// <example>true</example>
|
||||||
public bool IsSuccess { get; set; }
|
public required bool IsSuccess { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Return Data
|
/// Return Data
|
||||||
@@ -214,12 +221,14 @@ namespace WebProtocol
|
|||||||
/// <returns> 字符串 </returns>
|
/// <returns> 字符串 </returns>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
var opts = new SendAddrPackOptions();
|
var opts = new SendAddrPackOptions()
|
||||||
opts.BurstType = (BurstType)(commandType >> 6);
|
{
|
||||||
opts.CommandID = Convert.ToByte((commandType >> 4) & 0b0011);
|
BurstType = (BurstType)(commandType >> 6),
|
||||||
opts.IsWrite = Convert.ToBoolean(commandType & 0x01);
|
CommandID = Convert.ToByte((commandType >> 4) & 0b0011),
|
||||||
opts.BurstLength = burstLength;
|
IsWrite = Convert.ToBoolean(commandType & 0x01),
|
||||||
opts.Address = address;
|
BurstLength = burstLength,
|
||||||
|
Address = address,
|
||||||
|
};
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(opts);
|
return JsonConvert.SerializeObject(opts);
|
||||||
}
|
}
|
||||||
@@ -297,8 +306,9 @@ namespace WebProtocol
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> FPGA->Server 读响应包 </summary>
|
/// <summary> FPGA->Server 读响应包 </summary>
|
||||||
public struct RecvDataPackage
|
public class RecvDataPackage
|
||||||
{
|
{
|
||||||
|
readonly UInt32 timestamp;
|
||||||
readonly byte sign = (byte)PackSign.RecvData;
|
readonly byte sign = (byte)PackSign.RecvData;
|
||||||
readonly byte commandID;
|
readonly byte commandID;
|
||||||
readonly byte resp;
|
readonly byte resp;
|
||||||
@@ -309,11 +319,13 @@ namespace WebProtocol
|
|||||||
/// FPGA->Server 读响应包
|
/// FPGA->Server 读响应包
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="timestamp"> 时间戳 </param>
|
||||||
/// <param name="commandID"> 任务ID号 </param>
|
/// <param name="commandID"> 任务ID号 </param>
|
||||||
/// <param name="resp"> 读响应包响应 </param>
|
/// <param name="resp"> 读响应包响应 </param>
|
||||||
/// <param name="bodyData"> 数据 </param>
|
/// <param name="bodyData"> 数据 </param>
|
||||||
public RecvDataPackage(byte commandID, byte resp, byte[] bodyData)
|
public RecvDataPackage(UInt32 timestamp, byte commandID, byte resp, byte[] bodyData)
|
||||||
{
|
{
|
||||||
|
this.timestamp = timestamp;
|
||||||
this.commandID = commandID;
|
this.commandID = commandID;
|
||||||
this.resp = resp;
|
this.resp = resp;
|
||||||
this.bodyData = bodyData;
|
this.bodyData = bodyData;
|
||||||
@@ -322,26 +334,13 @@ namespace WebProtocol
|
|||||||
_ = this._reserved;
|
_ = this._reserved;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// FPGA->Server 读响应包
|
|
||||||
/// 构造函数
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="commandID"> 任务ID号 </param>
|
|
||||||
/// <param name="isSuccess">是否读取成功</param>
|
|
||||||
/// <param name="bodyData"> 数据 </param>
|
|
||||||
public RecvDataPackage(byte commandID, bool isSuccess, byte[] bodyData)
|
|
||||||
{
|
|
||||||
this.commandID = commandID;
|
|
||||||
this.resp = Convert.ToByte(isSuccess);
|
|
||||||
this.bodyData = bodyData;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 通过接受包选项构建读响应包
|
/// 通过接受包选项构建读响应包
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="opts">接收包(读响应包和写响应包)选项</param>
|
/// <param name="opts">接收包(读响应包和写响应包)选项</param>
|
||||||
public RecvDataPackage(RecvPackOptions opts)
|
public RecvDataPackage(RecvPackOptions opts)
|
||||||
{
|
{
|
||||||
|
this.timestamp = opts.Timestamp;
|
||||||
this.commandID = opts.CommandID;
|
this.commandID = opts.CommandID;
|
||||||
this.resp = Convert.ToByte(opts.IsSuccess ? 0b10 : 0b00);
|
this.resp = Convert.ToByte(opts.IsSuccess ? 0b10 : 0b00);
|
||||||
this.bodyData = opts.Data ?? (byte[])[0, 0, 0, 0];
|
this.bodyData = opts.Data ?? (byte[])[0, 0, 0, 0];
|
||||||
@@ -354,11 +353,14 @@ namespace WebProtocol
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
var opts = new RecvPackOptions();
|
var opts = new RecvPackOptions()
|
||||||
opts.Type = RecvPackOptions.PackType.ReadResp;
|
{
|
||||||
opts.CommandID = commandID;
|
Timestamp = this.timestamp,
|
||||||
opts.IsSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? false : true);
|
Type = RecvPackOptions.PackType.ReadResp,
|
||||||
opts.Data = bodyData;
|
CommandID = this.commandID,
|
||||||
|
IsSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? false : true),
|
||||||
|
Data = this.bodyData,
|
||||||
|
};
|
||||||
|
|
||||||
return opts;
|
return opts;
|
||||||
}
|
}
|
||||||
@@ -369,7 +371,7 @@ namespace WebProtocol
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsSuccessful
|
public bool IsSuccessful
|
||||||
{
|
{
|
||||||
get { return Convert.ToBoolean((resp >> 1) == 0b01 ? false : true); }
|
get { return Convert.ToBoolean((this.resp >> 1) == 0b01 ? false : true); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -379,12 +381,26 @@ namespace WebProtocol
|
|||||||
/// <returns>读响应包</returns>
|
/// <returns>读响应包</returns>
|
||||||
public static Result<RecvDataPackage> FromBytes(byte[] bytes)
|
public static Result<RecvDataPackage> FromBytes(byte[] bytes)
|
||||||
{
|
{
|
||||||
if (bytes[0] != (byte)PackSign.RecvData)
|
if (bytes[4] != (byte)PackSign.RecvData)
|
||||||
return new(new ArgumentException(
|
return new(new ArgumentException(
|
||||||
$"The sign of bytes is not RecvData Package, Sign: 0x{BitConverter.ToString([bytes[0]])}",
|
$"The sign of bytes is not RecvData Package, Sign: 0x{BitConverter.ToString([bytes[0]])}",
|
||||||
nameof(bytes)
|
nameof(bytes)
|
||||||
));
|
));
|
||||||
return new RecvDataPackage(bytes[1], bytes[2], bytes[4..]);
|
return new RecvDataPackage(
|
||||||
|
Number.BytesToUInt32(bytes[..4]).Value,
|
||||||
|
bytes[5],
|
||||||
|
bytes[6],
|
||||||
|
bytes[8..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bytes">[TODO:parameter]</param>
|
||||||
|
/// <returns>[TODO:return]</returns>
|
||||||
|
public static bool IsRecvDataPackage(byte[] bytes)
|
||||||
|
{
|
||||||
|
return bytes[4] == (byte)PackSign.RecvData;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -394,13 +410,16 @@ namespace WebProtocol
|
|||||||
public byte[] ToBytes()
|
public byte[] ToBytes()
|
||||||
{
|
{
|
||||||
var bodyDataLen = bodyData.Length;
|
var bodyDataLen = bodyData.Length;
|
||||||
var arr = new byte[4 + bodyDataLen];
|
var arr = new byte[8 + bodyDataLen];
|
||||||
|
|
||||||
arr[0] = this.sign;
|
Buffer.BlockCopy(
|
||||||
arr[1] = this.commandID;
|
Number.UInt32ArrayToBytes([this.timestamp]).Value, 0, arr, 0, 4);
|
||||||
arr[2] = this.resp;
|
arr[4] = this.sign;
|
||||||
|
arr[5] = this.commandID;
|
||||||
|
arr[6] = this.resp;
|
||||||
|
arr[7] = this.resp;
|
||||||
|
|
||||||
Array.Copy(bodyData, 0, arr, 4, bodyDataLen);
|
Array.Copy(bodyData, 0, arr, 8, bodyDataLen);
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
@@ -408,8 +427,9 @@ namespace WebProtocol
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> 写响应包 </summary>
|
/// <summary> 写响应包 </summary>
|
||||||
public struct RecvRespPackage
|
public class RecvRespPackage
|
||||||
{
|
{
|
||||||
|
readonly UInt32 timestamp;
|
||||||
readonly byte sign = (byte)PackSign.RecvResp;
|
readonly byte sign = (byte)PackSign.RecvResp;
|
||||||
readonly byte commandID;
|
readonly byte commandID;
|
||||||
readonly byte resp;
|
readonly byte resp;
|
||||||
@@ -418,10 +438,12 @@ namespace WebProtocol
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构建写响应包
|
/// 构建写响应包
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="timestamp">时间戳</param>
|
||||||
/// <param name="commandID">任务ID</param>
|
/// <param name="commandID">任务ID</param>
|
||||||
/// <param name="resp">写响应</param>
|
/// <param name="resp">写响应</param>
|
||||||
public RecvRespPackage(byte commandID, byte resp)
|
public RecvRespPackage(UInt32 timestamp, byte commandID, byte resp)
|
||||||
{
|
{
|
||||||
|
this.timestamp = timestamp;
|
||||||
this.commandID = commandID;
|
this.commandID = commandID;
|
||||||
this.resp = resp;
|
this.resp = resp;
|
||||||
|
|
||||||
@@ -429,23 +451,13 @@ namespace WebProtocol
|
|||||||
_ = this._reserved;
|
_ = this._reserved;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 构建写响应包
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="commandID">任务ID</param>
|
|
||||||
/// <param name="isSuccess">是否写成功</param>
|
|
||||||
public RecvRespPackage(byte commandID, bool isSuccess)
|
|
||||||
{
|
|
||||||
this.commandID = commandID;
|
|
||||||
this.resp = Convert.ToByte(isSuccess);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 通过接受包选项构建写响应包
|
/// 通过接受包选项构建写响应包
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="opts">接收包(读响应包和写响应包)选项</param>
|
/// <param name="opts">接收包(读响应包和写响应包)选项</param>
|
||||||
public RecvRespPackage(RecvPackOptions opts)
|
public RecvRespPackage(RecvPackOptions opts)
|
||||||
{
|
{
|
||||||
|
this.timestamp = opts.Timestamp;
|
||||||
this.commandID = opts.CommandID;
|
this.commandID = opts.CommandID;
|
||||||
this.resp = Convert.ToByte(opts.IsSuccess ? 0b10 : 0b00);
|
this.resp = Convert.ToByte(opts.IsSuccess ? 0b10 : 0b00);
|
||||||
}
|
}
|
||||||
@@ -457,11 +469,14 @@ namespace WebProtocol
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
var opts = new RecvPackOptions();
|
var opts = new RecvPackOptions()
|
||||||
opts.Type = RecvPackOptions.PackType.WriteResp;
|
{
|
||||||
opts.CommandID = commandID;
|
Timestamp = this.timestamp,
|
||||||
opts.IsSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? false : true);
|
Type = RecvPackOptions.PackType.WriteResp,
|
||||||
opts.Data = null;
|
CommandID = commandID,
|
||||||
|
IsSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? false : true),
|
||||||
|
Data = null,
|
||||||
|
};
|
||||||
|
|
||||||
return opts;
|
return opts;
|
||||||
}
|
}
|
||||||
@@ -472,7 +487,7 @@ namespace WebProtocol
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsSuccessful
|
public bool IsSuccessful
|
||||||
{
|
{
|
||||||
get { return Convert.ToBoolean((resp >> 1) == 0b01 ? false : true); }
|
get { return Convert.ToBoolean((this.resp >> 1) == 0b01 ? false : true); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -482,12 +497,23 @@ namespace WebProtocol
|
|||||||
/// <returns>写响应包</returns>
|
/// <returns>写响应包</returns>
|
||||||
public static Result<RecvRespPackage> FromBytes(byte[] bytes)
|
public static Result<RecvRespPackage> FromBytes(byte[] bytes)
|
||||||
{
|
{
|
||||||
if (bytes[0] != (byte)PackSign.RecvResp)
|
if (bytes[4] != (byte)PackSign.RecvResp)
|
||||||
return new(new ArgumentException(
|
return new(new ArgumentException(
|
||||||
$"The sign of bytes is not RecvResp Package, Sign: 0x{BitConverter.ToString([bytes[0]])}",
|
$"The sign of bytes is not RecvResp Package, Sign: 0x{BitConverter.ToString([bytes[4]])}",
|
||||||
nameof(bytes)
|
nameof(bytes)
|
||||||
));
|
));
|
||||||
return new RecvRespPackage(bytes[1], bytes[2]);
|
var timestamp = Number.BytesToUInt32(bytes[..4]).Value;
|
||||||
|
return new RecvRespPackage(timestamp, bytes[5], bytes[6]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// [TODO:description]
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bytes">[TODO:parameter]</param>
|
||||||
|
/// <returns>[TODO:return]</returns>
|
||||||
|
public static bool IsRecvRespPackage(byte[] bytes)
|
||||||
|
{
|
||||||
|
return bytes[4] == (byte)PackSign.RecvResp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -496,11 +522,13 @@ namespace WebProtocol
|
|||||||
/// <returns>字节数组</returns>
|
/// <returns>字节数组</returns>
|
||||||
public byte[] ToBytes()
|
public byte[] ToBytes()
|
||||||
{
|
{
|
||||||
var arr = new byte[4];
|
var arr = new byte[8];
|
||||||
|
Buffer.BlockCopy(
|
||||||
arr[0] = this.sign;
|
Number.UInt32ArrayToBytes([this.timestamp]).Value, 0, arr, 0, 4);
|
||||||
arr[1] = this.commandID;
|
arr[4] = this.sign;
|
||||||
arr[2] = this.resp;
|
arr[5] = this.commandID;
|
||||||
|
arr[6] = this.resp;
|
||||||
|
arr[7] = this._reserved;
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|||||||
7303
src/APIClient.ts
7303
src/APIClient.ts
File diff suppressed because it is too large
Load Diff
39
src/App.vue
39
src/App.vue
@@ -12,6 +12,14 @@ const isDarkMode = ref(
|
|||||||
window.matchMedia("(prefers-color-scheme: dark)").matches,
|
window.matchMedia("(prefers-color-scheme: dark)").matches,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Navbar显示状态管理
|
||||||
|
const showNavbar = ref(true);
|
||||||
|
|
||||||
|
// 切换Navbar显示状态
|
||||||
|
const toggleNavbar = () => {
|
||||||
|
showNavbar.value = !showNavbar.value;
|
||||||
|
};
|
||||||
|
|
||||||
// 初始化主题设置
|
// 初始化主题设置
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
// 应用初始主题
|
// 应用初始主题
|
||||||
@@ -47,6 +55,12 @@ provide("theme", {
|
|||||||
toggleTheme,
|
toggleTheme,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 提供Navbar控制给子组件
|
||||||
|
provide("navbar", {
|
||||||
|
showNavbar,
|
||||||
|
toggleNavbar,
|
||||||
|
});
|
||||||
|
|
||||||
const currentRoutePath = computed(() => {
|
const currentRoutePath = computed(() => {
|
||||||
return router.currentRoute.value.path;
|
return router.currentRoute.value.path;
|
||||||
});
|
});
|
||||||
@@ -56,8 +70,8 @@ useAlertProvider();
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<header class="relative">
|
<header class="relative" :class="{ 'navbar-hidden': !showNavbar }">
|
||||||
<Navbar />
|
<Navbar v-show="showNavbar" />
|
||||||
<Dialog />
|
<Dialog />
|
||||||
<Alert />
|
<Alert />
|
||||||
</header>
|
</header>
|
||||||
@@ -79,4 +93,25 @@ useAlertProvider();
|
|||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
/* 特定于App.vue的样式 */
|
/* 特定于App.vue的样式 */
|
||||||
|
header {
|
||||||
|
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
||||||
|
transform-origin: top;
|
||||||
|
}
|
||||||
|
|
||||||
|
.navbar-hidden {
|
||||||
|
transform: scaleY(0);
|
||||||
|
height: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Navbar显示/隐藏动画 */
|
||||||
|
header .navbar {
|
||||||
|
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
||||||
|
transform-origin: top;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 当header被隐藏时,确保navbar也相应变化 */
|
||||||
|
.navbar-hidden .navbar {
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
169
src/TypedSignalR.Client/index.ts
Normal file
169
src/TypedSignalR.Client/index.ts
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
/* THIS (.ts) FILE IS GENERATED BY TypedSignalR.Client.TypeScript */
|
||||||
|
/* eslint-disable */
|
||||||
|
/* tslint:disable */
|
||||||
|
// @ts-nocheck
|
||||||
|
import type { HubConnection, IStreamResult, Subject } from '@microsoft/signalr';
|
||||||
|
import type { IJtagHub, IProgressHub, IJtagReceiver, IProgressReceiver } from './server.Hubs';
|
||||||
|
import type { ProgressInfo } from '../server.Hubs';
|
||||||
|
|
||||||
|
|
||||||
|
// components
|
||||||
|
|
||||||
|
export type Disposable = {
|
||||||
|
dispose(): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type HubProxyFactory<T> = {
|
||||||
|
createHubProxy(connection: HubConnection): T;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ReceiverRegister<T> = {
|
||||||
|
register(connection: HubConnection, receiver: T): Disposable;
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReceiverMethod = {
|
||||||
|
methodName: string,
|
||||||
|
method: (...args: any[]) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
class ReceiverMethodSubscription implements Disposable {
|
||||||
|
|
||||||
|
public constructor(
|
||||||
|
private connection: HubConnection,
|
||||||
|
private receiverMethod: ReceiverMethod[]) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly dispose = () => {
|
||||||
|
for (const it of this.receiverMethod) {
|
||||||
|
this.connection.off(it.methodName, it.method);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
export type HubProxyFactoryProvider = {
|
||||||
|
(hubType: "IJtagHub"): HubProxyFactory<IJtagHub>;
|
||||||
|
(hubType: "IProgressHub"): HubProxyFactory<IProgressHub>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getHubProxyFactory = ((hubType: string) => {
|
||||||
|
if(hubType === "IJtagHub") {
|
||||||
|
return IJtagHub_HubProxyFactory.Instance;
|
||||||
|
}
|
||||||
|
if(hubType === "IProgressHub") {
|
||||||
|
return IProgressHub_HubProxyFactory.Instance;
|
||||||
|
}
|
||||||
|
}) as HubProxyFactoryProvider;
|
||||||
|
|
||||||
|
export type ReceiverRegisterProvider = {
|
||||||
|
(receiverType: "IJtagReceiver"): ReceiverRegister<IJtagReceiver>;
|
||||||
|
(receiverType: "IProgressReceiver"): ReceiverRegister<IProgressReceiver>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getReceiverRegister = ((receiverType: string) => {
|
||||||
|
if(receiverType === "IJtagReceiver") {
|
||||||
|
return IJtagReceiver_Binder.Instance;
|
||||||
|
}
|
||||||
|
if(receiverType === "IProgressReceiver") {
|
||||||
|
return IProgressReceiver_Binder.Instance;
|
||||||
|
}
|
||||||
|
}) as ReceiverRegisterProvider;
|
||||||
|
|
||||||
|
// HubProxy
|
||||||
|
|
||||||
|
class IJtagHub_HubProxyFactory implements HubProxyFactory<IJtagHub> {
|
||||||
|
public static Instance = new IJtagHub_HubProxyFactory();
|
||||||
|
|
||||||
|
private constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly createHubProxy = (connection: HubConnection): IJtagHub => {
|
||||||
|
return new IJtagHub_HubProxy(connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class IJtagHub_HubProxy implements IJtagHub {
|
||||||
|
|
||||||
|
public constructor(private connection: HubConnection) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly setBoundaryScanFreq = async (freq: number): Promise<boolean> => {
|
||||||
|
return await this.connection.invoke("SetBoundaryScanFreq", freq);
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly startBoundaryScan = async (freq: number): Promise<boolean> => {
|
||||||
|
return await this.connection.invoke("StartBoundaryScan", freq);
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly stopBoundaryScan = async (): Promise<boolean> => {
|
||||||
|
return await this.connection.invoke("StopBoundaryScan");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class IProgressHub_HubProxyFactory implements HubProxyFactory<IProgressHub> {
|
||||||
|
public static Instance = new IProgressHub_HubProxyFactory();
|
||||||
|
|
||||||
|
private constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly createHubProxy = (connection: HubConnection): IProgressHub => {
|
||||||
|
return new IProgressHub_HubProxy(connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class IProgressHub_HubProxy implements IProgressHub {
|
||||||
|
|
||||||
|
public constructor(private connection: HubConnection) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly join = async (taskId: string): Promise<boolean> => {
|
||||||
|
return await this.connection.invoke("Join", taskId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Receiver
|
||||||
|
|
||||||
|
class IJtagReceiver_Binder implements ReceiverRegister<IJtagReceiver> {
|
||||||
|
|
||||||
|
public static Instance = new IJtagReceiver_Binder();
|
||||||
|
|
||||||
|
private constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly register = (connection: HubConnection, receiver: IJtagReceiver): Disposable => {
|
||||||
|
|
||||||
|
const __onReceiveBoundaryScanData = (...args: [Partial<Record<string, boolean>>]) => receiver.onReceiveBoundaryScanData(...args);
|
||||||
|
|
||||||
|
connection.on("OnReceiveBoundaryScanData", __onReceiveBoundaryScanData);
|
||||||
|
|
||||||
|
const methodList: ReceiverMethod[] = [
|
||||||
|
{ methodName: "OnReceiveBoundaryScanData", method: __onReceiveBoundaryScanData }
|
||||||
|
]
|
||||||
|
|
||||||
|
return new ReceiverMethodSubscription(connection, methodList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class IProgressReceiver_Binder implements ReceiverRegister<IProgressReceiver> {
|
||||||
|
|
||||||
|
public static Instance = new IProgressReceiver_Binder();
|
||||||
|
|
||||||
|
private constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly register = (connection: HubConnection, receiver: IProgressReceiver): Disposable => {
|
||||||
|
|
||||||
|
const __onReceiveProgress = (...args: [ProgressInfo]) => receiver.onReceiveProgress(...args);
|
||||||
|
|
||||||
|
connection.on("OnReceiveProgress", __onReceiveProgress);
|
||||||
|
|
||||||
|
const methodList: ReceiverMethod[] = [
|
||||||
|
{ methodName: "OnReceiveProgress", method: __onReceiveProgress }
|
||||||
|
]
|
||||||
|
|
||||||
|
return new ReceiverMethodSubscription(connection, methodList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
48
src/TypedSignalR.Client/server.Hubs.ts
Normal file
48
src/TypedSignalR.Client/server.Hubs.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/* THIS (.ts) FILE IS GENERATED BY TypedSignalR.Client.TypeScript */
|
||||||
|
/* eslint-disable */
|
||||||
|
/* tslint:disable */
|
||||||
|
// @ts-nocheck
|
||||||
|
import type { IStreamResult, Subject } from '@microsoft/signalr';
|
||||||
|
import type { ProgressInfo } from '../server.Hubs';
|
||||||
|
|
||||||
|
export type IJtagHub = {
|
||||||
|
/**
|
||||||
|
* @param freq Transpiled from int
|
||||||
|
* @returns Transpiled from System.Threading.Tasks.Task<bool>
|
||||||
|
*/
|
||||||
|
setBoundaryScanFreq(freq: number): Promise<boolean>;
|
||||||
|
/**
|
||||||
|
* @param freq Transpiled from int
|
||||||
|
* @returns Transpiled from System.Threading.Tasks.Task<bool>
|
||||||
|
*/
|
||||||
|
startBoundaryScan(freq: number): Promise<boolean>;
|
||||||
|
/**
|
||||||
|
* @returns Transpiled from System.Threading.Tasks.Task<bool>
|
||||||
|
*/
|
||||||
|
stopBoundaryScan(): Promise<boolean>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type IProgressHub = {
|
||||||
|
/**
|
||||||
|
* @param taskId Transpiled from string
|
||||||
|
* @returns Transpiled from System.Threading.Tasks.Task<bool>
|
||||||
|
*/
|
||||||
|
join(taskId: string): Promise<boolean>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type IJtagReceiver = {
|
||||||
|
/**
|
||||||
|
* @param msg Transpiled from System.Collections.Generic.Dictionary<string, bool>
|
||||||
|
* @returns Transpiled from System.Threading.Tasks.Task
|
||||||
|
*/
|
||||||
|
onReceiveBoundaryScanData(msg: Partial<Record<string, boolean>>): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type IProgressReceiver = {
|
||||||
|
/**
|
||||||
|
* @param message Transpiled from server.Hubs.ProgressInfo
|
||||||
|
* @returns Transpiled from System.Threading.Tasks.Task
|
||||||
|
*/
|
||||||
|
onReceiveProgress(message: ProgressInfo): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="fixed left-1/2 top-30 z-999 -translate-x-1/2">
|
<div class="fixed left-1/2 top-30 z-[9999] -translate-x-1/2">
|
||||||
<transition
|
<transition
|
||||||
name="alert"
|
name="alert"
|
||||||
enter-active-class="alert-enter-active"
|
enter-active-class="alert-enter-active"
|
||||||
@@ -48,14 +48,15 @@
|
|||||||
import { computed } from "vue";
|
import { computed } from "vue";
|
||||||
import { CheckCircle, XCircle, AlertTriangle, Info, X } from "lucide-vue-next";
|
import { CheckCircle, XCircle, AlertTriangle, Info, X } from "lucide-vue-next";
|
||||||
import { useAlertStore } from ".";
|
import { useAlertStore } from ".";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
|
||||||
const alertStore = useAlertStore();
|
const alertStore = useRequiredInjection(useAlertStore);
|
||||||
|
|
||||||
// Computed classes for different alert types
|
// Computed classes for different alert types
|
||||||
const alertClasses = computed(() => {
|
const alertClasses = computed(() => {
|
||||||
const baseClasses = "shadow-lg max-w-sm";
|
const baseClasses = "shadow-lg max-w-sm";
|
||||||
|
|
||||||
switch (alertStore?.alertState.value.type) {
|
switch (alertStore.alertState.value.type) {
|
||||||
case "success":
|
case "success":
|
||||||
return `${baseClasses} alert-success`;
|
return `${baseClasses} alert-success`;
|
||||||
case "error":
|
case "error":
|
||||||
|
|||||||
@@ -119,6 +119,7 @@
|
|||||||
componentManager.prepareComponentProps(
|
componentManager.prepareComponentProps(
|
||||||
component.attrs || {},
|
component.attrs || {},
|
||||||
component.id,
|
component.id,
|
||||||
|
props.examId,
|
||||||
)
|
)
|
||||||
"
|
"
|
||||||
@update:bindKey="
|
@update:bindKey="
|
||||||
@@ -175,9 +176,7 @@ import {
|
|||||||
ref,
|
ref,
|
||||||
reactive,
|
reactive,
|
||||||
onMounted,
|
onMounted,
|
||||||
onUnmounted,
|
|
||||||
computed,
|
computed,
|
||||||
watch,
|
|
||||||
provide,
|
provide,
|
||||||
} from "vue";
|
} from "vue";
|
||||||
import { useEventListener } from "@vueuse/core";
|
import { useEventListener } from "@vueuse/core";
|
||||||
@@ -188,7 +187,6 @@ import { useAlertStore } from "@/components/Alert";
|
|||||||
// 导入 diagram 管理器
|
// 导入 diagram 管理器
|
||||||
import {
|
import {
|
||||||
loadDiagramData,
|
loadDiagramData,
|
||||||
saveDiagramData,
|
|
||||||
updatePartPosition,
|
updatePartPosition,
|
||||||
updatePartAttribute,
|
updatePartAttribute,
|
||||||
parseConnectionPin,
|
parseConnectionPin,
|
||||||
@@ -217,6 +215,7 @@ const emit = defineEmits(["toggle-doc-panel", "open-components"]);
|
|||||||
// 定义组件接受的属性
|
// 定义组件接受的属性
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
showDocPanel?: boolean; // 添加属性接收文档面板的显示状态
|
showDocPanel?: boolean; // 添加属性接收文档面板的显示状态
|
||||||
|
examId?: string; // 新增examId属性
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
// 获取componentManager实例
|
// 获取componentManager实例
|
||||||
@@ -606,14 +605,13 @@ function onComponentDrag(e: MouseEvent) {
|
|||||||
|
|
||||||
// 停止拖拽组件
|
// 停止拖拽组件
|
||||||
function stopComponentDrag() {
|
function stopComponentDrag() {
|
||||||
// 如果有组件被拖拽,保存当前状态
|
// 如果有组件被拖拽,仅清除拖拽状态(不保存)
|
||||||
if (draggingComponentId.value) {
|
if (draggingComponentId.value) {
|
||||||
draggingComponentId.value = null;
|
draggingComponentId.value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
isComponentDragEventActive.value = false;
|
isComponentDragEventActive.value = false;
|
||||||
|
// 移除自动保存功能 - 不再自动保存到localStorage
|
||||||
saveDiagramData(diagramData.value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新组件属性
|
// 更新组件属性
|
||||||
@@ -977,7 +975,8 @@ function exportDiagram() {
|
|||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
// 加载图表数据
|
// 加载图表数据
|
||||||
try {
|
try {
|
||||||
diagramData.value = await loadDiagramData();
|
// 传入examId参数,让diagramManager处理动态加载
|
||||||
|
diagramData.value = await loadDiagramData(props.examId);
|
||||||
|
|
||||||
// 预加载所有组件模块
|
// 预加载所有组件模块
|
||||||
const componentTypes = new Set<string>();
|
const componentTypes = new Set<string>();
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { ref, shallowRef, computed, reactive } from "vue";
|
import { ref, shallowRef, computed, reactive } from "vue";
|
||||||
import { createInjectionState } from "@vueuse/core";
|
import { createInjectionState } from "@vueuse/core";
|
||||||
import {
|
import {
|
||||||
saveDiagramData,
|
|
||||||
type DiagramData,
|
type DiagramData,
|
||||||
type DiagramPart,
|
type DiagramPart,
|
||||||
} from "./diagramManager";
|
} from "./diagramManager";
|
||||||
@@ -302,7 +301,7 @@ const [useProvideComponentManager, useComponentManager] = createInjectionState(
|
|||||||
|
|
||||||
// 使用 updateDiagramDataDirectly 避免触发加载状态
|
// 使用 updateDiagramDataDirectly 避免触发加载状态
|
||||||
canvasInstance.updateDiagramDataDirectly(currentData);
|
canvasInstance.updateDiagramDataDirectly(currentData);
|
||||||
saveDiagramData(currentData);
|
// 移除自动保存功能
|
||||||
|
|
||||||
console.log("组件添加完成:", newComponent);
|
console.log("组件添加完成:", newComponent);
|
||||||
|
|
||||||
@@ -431,7 +430,7 @@ const [useProvideComponentManager, useComponentManager] = createInjectionState(
|
|||||||
"=== 更新图表数据完成,新组件数量:",
|
"=== 更新图表数据完成,新组件数量:",
|
||||||
currentData.parts.length,
|
currentData.parts.length,
|
||||||
);
|
);
|
||||||
saveDiagramData(currentData);
|
// 移除自动保存功能
|
||||||
|
|
||||||
return { success: true, message: `已添加 ${templateData.name} 模板` };
|
return { success: true, message: `已添加 ${templateData.name} 模板` };
|
||||||
} else {
|
} else {
|
||||||
@@ -504,7 +503,7 @@ const [useProvideComponentManager, useComponentManager] = createInjectionState(
|
|||||||
|
|
||||||
canvasInstance.updateDiagramDataDirectly(currentData);
|
canvasInstance.updateDiagramDataDirectly(currentData);
|
||||||
|
|
||||||
saveDiagramData(currentData);
|
// 移除自动保存功能
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -763,11 +762,15 @@ const [useProvideComponentManager, useComponentManager] = createInjectionState(
|
|||||||
function prepareComponentProps(
|
function prepareComponentProps(
|
||||||
attrs: Record<string, any>,
|
attrs: Record<string, any>,
|
||||||
componentId?: string,
|
componentId?: string,
|
||||||
|
examId?: string,
|
||||||
): Record<string, any> {
|
): Record<string, any> {
|
||||||
const result: Record<string, any> = { ...attrs };
|
const result: Record<string, any> = { ...attrs };
|
||||||
if (componentId) {
|
if (componentId) {
|
||||||
result.componentId = componentId;
|
result.componentId = componentId;
|
||||||
}
|
}
|
||||||
|
if (examId) {
|
||||||
|
result.examId = examId;
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ export interface DiagramPart {
|
|||||||
// 连接类型定义 - 使用元组类型表示四元素数组
|
// 连接类型定义 - 使用元组类型表示四元素数组
|
||||||
export type ConnectionArray = [string, string, number, string[]];
|
export type ConnectionArray = [string, string, number, string[]];
|
||||||
|
|
||||||
|
import { AuthManager } from '@/utils/AuthManager';
|
||||||
|
|
||||||
// 解析连接字符串为组件ID和引脚ID
|
// 解析连接字符串为组件ID和引脚ID
|
||||||
export function parseConnectionPin(connectionPin: string): { componentId: string; pinId: string } {
|
export function parseConnectionPin(connectionPin: string): { componentId: string; pinId: string } {
|
||||||
const [componentId, pinId] = connectionPin.split(':');
|
const [componentId, pinId] = connectionPin.split(':');
|
||||||
@@ -80,22 +82,62 @@ export interface WireItem {
|
|||||||
showLabel: boolean;
|
showLabel: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从本地存储加载图表数据
|
// 从本地存储或动态API加载图表数据
|
||||||
export async function loadDiagramData(): Promise<DiagramData> {
|
export async function loadDiagramData(examId?: string): Promise<DiagramData> {
|
||||||
try {
|
try {
|
||||||
// 先尝试从本地存储加载
|
// 如果提供了examId,优先从API加载实验的diagram
|
||||||
const savedData = localStorage.getItem('diagramData');
|
if (examId) {
|
||||||
if (savedData) {
|
try {
|
||||||
return JSON.parse(savedData);
|
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||||
|
|
||||||
|
// 获取diagram类型的资源列表
|
||||||
|
const resources = await resourceClient.getResourceList(examId, 'canvas', 'template');
|
||||||
|
|
||||||
|
if (resources && resources.length > 0) {
|
||||||
|
// 获取第一个diagram资源
|
||||||
|
const diagramResource = resources[0];
|
||||||
|
|
||||||
|
// 使用动态API获取资源文件内容
|
||||||
|
const response = await resourceClient.getResourceById(diagramResource.id);
|
||||||
|
|
||||||
|
if (response && response.data) {
|
||||||
|
const text = await response.data.text();
|
||||||
|
const data = JSON.parse(text);
|
||||||
|
|
||||||
|
// 验证数据格式
|
||||||
|
const validation = validateDiagramData(data);
|
||||||
|
if (validation.isValid) {
|
||||||
|
console.log('成功从API加载实验diagram:', examId);
|
||||||
|
return data;
|
||||||
|
} else {
|
||||||
|
console.warn('API返回的diagram数据格式无效:', validation.errors);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('未找到实验diagram资源,使用默认加载方式');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('从API加载实验diagram失败,使用默认加载方式:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果本地存储没有,从文件加载
|
// 如果没有examId或API加载失败,尝试从静态文件加载(不再使用本地存储)
|
||||||
|
|
||||||
|
// 从静态文件加载(作为备选方案)
|
||||||
const response = await fetch('/src/components/diagram.json');
|
const response = await fetch('/src/components/diagram.json');
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Failed to load diagram.json: ${response.statusText}`);
|
throw new Error(`Failed to load diagram.json: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
|
// 验证静态文件数据
|
||||||
|
const validation = validateDiagramData(data);
|
||||||
|
if (validation.isValid) {
|
||||||
return data;
|
return data;
|
||||||
|
} else {
|
||||||
|
console.warn('静态diagram文件数据格式无效:', validation.errors);
|
||||||
|
throw new Error('所有diagram数据源都无效');
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error loading diagram data:', error);
|
console.error('Error loading diagram data:', error);
|
||||||
// 返回空的默认数据结构
|
// 返回空的默认数据结构
|
||||||
@@ -114,13 +156,10 @@ export function createEmptyDiagram(): DiagramData {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存图表数据到本地存储
|
// 保存图表数据(已禁用本地存储)
|
||||||
export function saveDiagramData(data: DiagramData): void {
|
export function saveDiagramData(data: DiagramData): void {
|
||||||
try {
|
// 本地存储功能已禁用 - 不再保存到localStorage
|
||||||
localStorage.setItem('diagramData', JSON.stringify(data));
|
console.debug('saveDiagramData called but localStorage saving is disabled');
|
||||||
} catch (error) {
|
|
||||||
console.error('Error saving diagram data:', error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新组件位置
|
// 更新组件位置
|
||||||
|
|||||||
813
src/components/LogicAnalyzer/LogicAnalyzerManager.ts
Normal file
813
src/components/LogicAnalyzer/LogicAnalyzerManager.ts
Normal file
@@ -0,0 +1,813 @@
|
|||||||
|
import { createInjectionState } from "@vueuse/core";
|
||||||
|
import { shallowRef, reactive, ref, computed } from "vue";
|
||||||
|
import { Mutex } from "async-mutex";
|
||||||
|
import {
|
||||||
|
CaptureConfig,
|
||||||
|
CaptureStatus,
|
||||||
|
LogicAnalyzerClient,
|
||||||
|
GlobalCaptureMode,
|
||||||
|
SignalOperator,
|
||||||
|
SignalTriggerConfig,
|
||||||
|
SignalValue,
|
||||||
|
AnalyzerChannelDiv,
|
||||||
|
AnalyzerClockDiv,
|
||||||
|
} from "@/APIClient";
|
||||||
|
import { AuthManager } from "@/utils/AuthManager";
|
||||||
|
import { useAlertStore } from "@/components/Alert";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
|
||||||
|
export type LogicDataType = {
|
||||||
|
x: number[];
|
||||||
|
y: number[][]; // 8 channels of digital data (0 or 1)
|
||||||
|
xUnit: "s" | "ms" | "us" | "ns";
|
||||||
|
};
|
||||||
|
|
||||||
|
// 通道接口定义
|
||||||
|
export type Channel = {
|
||||||
|
enabled: boolean;
|
||||||
|
label: string;
|
||||||
|
color: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 全局模式选项
|
||||||
|
const globalModes = [
|
||||||
|
{value: GlobalCaptureMode.AND,label: "AND",description: "所有条件都满足时触发",},
|
||||||
|
{value: GlobalCaptureMode.OR,label: "OR",description: "任一条件满足时触发",},
|
||||||
|
{ value: GlobalCaptureMode.NAND, label: "NAND", description: "AND的非" },
|
||||||
|
{ value: GlobalCaptureMode.NOR, label: "NOR", description: "OR的非" },
|
||||||
|
];
|
||||||
|
|
||||||
|
// 操作符选项
|
||||||
|
const operators = [
|
||||||
|
{ value: SignalOperator.Equal, label: "=" },
|
||||||
|
{ value: SignalOperator.NotEqual, label: "≠" },
|
||||||
|
{ value: SignalOperator.LessThan, label: "<" },
|
||||||
|
{ value: SignalOperator.LessThanOrEqual, label: "≤" },
|
||||||
|
{ value: SignalOperator.GreaterThan, label: ">" },
|
||||||
|
{ value: SignalOperator.GreaterThanOrEqual, label: "≥" },
|
||||||
|
];
|
||||||
|
|
||||||
|
// 信号值选项
|
||||||
|
const signalValues = [
|
||||||
|
{ value: SignalValue.Logic0, label: "0" },
|
||||||
|
{ value: SignalValue.Logic1, label: "1" },
|
||||||
|
{ value: SignalValue.NotCare, label: "X" },
|
||||||
|
{ value: SignalValue.Rise, label: "↑" },
|
||||||
|
{ value: SignalValue.Fall, label: "↓" },
|
||||||
|
{ value: SignalValue.RiseOrFall, label: "↕" },
|
||||||
|
{ value: SignalValue.NoChange, label: "—" },
|
||||||
|
{ value: SignalValue.SomeNumber, label: "#" },
|
||||||
|
];
|
||||||
|
|
||||||
|
// 通道组选项
|
||||||
|
const channelDivOptions = [
|
||||||
|
{ value: 1, label: "1通道", description: "启用1个通道 (CH0)" },
|
||||||
|
{ value: 2, label: "2通道", description: "启用2个通道 (CH0-CH1)" },
|
||||||
|
{ value: 4, label: "4通道", description: "启用4个通道 (CH0-CH3)" },
|
||||||
|
{ value: 8, label: "8通道", description: "启用8个通道 (CH0-CH7)" },
|
||||||
|
{ value: 16, label: "16通道", description: "启用16个通道 (CH0-CH15)" },
|
||||||
|
{ value: 32, label: "32通道", description: "启用32个通道 (CH0-CH31)" },
|
||||||
|
];
|
||||||
|
|
||||||
|
const ClockDivOptions = [
|
||||||
|
{ value: AnalyzerClockDiv.DIV1, label: "120MHz", description: "采样频率120MHz" },
|
||||||
|
{ value: AnalyzerClockDiv.DIV2, label: "60MHz", description: "采样频率60MHz" },
|
||||||
|
{ value: AnalyzerClockDiv.DIV4, label: "30MHz", description: "采样频率30MHz" },
|
||||||
|
{ value: AnalyzerClockDiv.DIV8, label: "15MHz", description: "采样频率15MHz" },
|
||||||
|
{ value: AnalyzerClockDiv.DIV16, label: "7.5MHz", description: "采样频率7.5MHz" },
|
||||||
|
{ value: AnalyzerClockDiv.DIV32, label: "3.75MHz", description: "采样频率3.75MHz" },
|
||||||
|
{ value: AnalyzerClockDiv.DIV64, label: "1.875MHz", description: "采样频率1.875MHz" },
|
||||||
|
{ value: AnalyzerClockDiv.DIV128, label: "937.5KHz", description: "采样频率937.5KHz" },
|
||||||
|
];
|
||||||
|
|
||||||
|
// 捕获深度限制常量
|
||||||
|
const CAPTURE_LENGTH_MIN = 1024; // 最小捕获深度 1024
|
||||||
|
const CAPTURE_LENGTH_MAX = 0x10000000 - 0x01000000; // 最大捕获深度
|
||||||
|
|
||||||
|
// 预捕获深度限制常量
|
||||||
|
const PRE_CAPTURE_LENGTH_MIN = 2; // 最小预捕获深度 2
|
||||||
|
|
||||||
|
// 默认颜色数组
|
||||||
|
const defaultColors = [
|
||||||
|
"#FF5733",
|
||||||
|
"#33FF57",
|
||||||
|
"#3357FF",
|
||||||
|
"#FF33F5",
|
||||||
|
"#F5FF33",
|
||||||
|
"#33FFF5",
|
||||||
|
"#FF8C33",
|
||||||
|
"#8C33FF",
|
||||||
|
];
|
||||||
|
|
||||||
|
// 添加逻辑分析仪基础频率常量
|
||||||
|
const BASE_LOGIC_ANALYZER_FREQUENCY = 120_000_000; // 120MHz基础频率
|
||||||
|
|
||||||
|
const [useProvideLogicAnalyzer, useLogicAnalyzerState] = createInjectionState(
|
||||||
|
() => {
|
||||||
|
const logicData = shallowRef<LogicDataType>();
|
||||||
|
const alert = useRequiredInjection(useAlertStore);
|
||||||
|
|
||||||
|
// 添加互斥锁
|
||||||
|
const operationMutex = new Mutex();
|
||||||
|
|
||||||
|
// 触发设置相关状态
|
||||||
|
const currentGlobalMode = ref<GlobalCaptureMode>(GlobalCaptureMode.AND);
|
||||||
|
const currentChannelDiv = ref<number>(8); // 默认启用8个通道
|
||||||
|
const captureLength = ref<number>(CAPTURE_LENGTH_MIN); // 捕获深度,默认为最小值
|
||||||
|
const preCaptureLength = ref<number>(PRE_CAPTURE_LENGTH_MIN); // 预捕获深度,默认0
|
||||||
|
const currentclockDiv = ref<AnalyzerClockDiv>(AnalyzerClockDiv.DIV1); // 默认时钟分频为1
|
||||||
|
const isApplying = ref(false);
|
||||||
|
const isCapturing = ref(false); // 添加捕获状态标识
|
||||||
|
|
||||||
|
// 通道配置
|
||||||
|
const channels = reactive<Channel[]>(
|
||||||
|
Array.from({ length: 32 }, (_, index) => ({
|
||||||
|
enabled: index < 8, // 默认启用前8个通道
|
||||||
|
label: `CH${index}`,
|
||||||
|
color: defaultColors[index % defaultColors.length], // 使用模运算避免数组越界
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 32个信号通道的配置
|
||||||
|
const signalConfigs = reactive<SignalTriggerConfig[]>(
|
||||||
|
Array.from(
|
||||||
|
{ length: 32 },
|
||||||
|
(_, index) =>
|
||||||
|
new SignalTriggerConfig({
|
||||||
|
signalIndex: index,
|
||||||
|
operator: SignalOperator.Equal,
|
||||||
|
value: SignalValue.NotCare,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 计算启用的通道数量
|
||||||
|
const enabledChannelCount = computed(
|
||||||
|
() => channels.filter((channel) => channel.enabled).length,
|
||||||
|
);
|
||||||
|
|
||||||
|
// 添加计算属性:获取通道名称数组
|
||||||
|
const channelNames = computed(() =>
|
||||||
|
channels.map((channel) => channel.label),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 添加计算属性:获取启用通道的名称数组
|
||||||
|
const enabledChannels = computed(() =>
|
||||||
|
channels.filter((channel) => channel.enabled),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 计算属性:根据当前时钟分频获取实际采样频率
|
||||||
|
const currentSampleFrequency = computed(() => {
|
||||||
|
const divValue = Math.pow(2, currentclockDiv.value);
|
||||||
|
return BASE_LOGIC_ANALYZER_FREQUENCY / divValue;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 计算属性:获取当前采样周期(纳秒)
|
||||||
|
const currentSamplePeriodNs = computed(() => {
|
||||||
|
return 1_000_000_000 / currentSampleFrequency.value;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 转换通道数字到枚举值
|
||||||
|
const getChannelDivEnum = (channelCount: number): AnalyzerChannelDiv => {
|
||||||
|
switch (channelCount) {
|
||||||
|
case 1: return AnalyzerChannelDiv.ONE;
|
||||||
|
case 2: return AnalyzerChannelDiv.TWO;
|
||||||
|
case 4: return AnalyzerChannelDiv.FOUR;
|
||||||
|
case 8: return AnalyzerChannelDiv.EIGHT;
|
||||||
|
case 16: return AnalyzerChannelDiv.XVI;
|
||||||
|
case 32: return AnalyzerChannelDiv.XXXII;
|
||||||
|
default: return AnalyzerChannelDiv.EIGHT;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 验证捕获深度
|
||||||
|
const validateCaptureLength = (value: number): { valid: boolean; message?: string } => {
|
||||||
|
if (!Number.isInteger(value)) {
|
||||||
|
return { valid: false, message: "捕获深度必须是整数" };
|
||||||
|
}
|
||||||
|
if (value < CAPTURE_LENGTH_MIN) {
|
||||||
|
return { valid: false, message: `捕获深度不能小于 ${CAPTURE_LENGTH_MIN}` };
|
||||||
|
}
|
||||||
|
if (value > CAPTURE_LENGTH_MAX) {
|
||||||
|
return { valid: false, message: `捕获深度不能大于 ${CAPTURE_LENGTH_MAX.toLocaleString()}` };
|
||||||
|
}
|
||||||
|
return { valid: true };
|
||||||
|
};
|
||||||
|
|
||||||
|
// 验证预捕获深度
|
||||||
|
const validatePreCaptureLength = (value: number, currentCaptureLength: number): { valid: boolean; message?: string } => {
|
||||||
|
if (!Number.isInteger(value)) {
|
||||||
|
return { valid: false, message: "预捕获深度必须是整数" };
|
||||||
|
}
|
||||||
|
if (value < PRE_CAPTURE_LENGTH_MIN) {
|
||||||
|
return { valid: false, message: `预捕获深度不能小于 ${PRE_CAPTURE_LENGTH_MIN}` };
|
||||||
|
}
|
||||||
|
if (value >= currentCaptureLength) {
|
||||||
|
return { valid: false, message: `预捕获深度不能大于等于捕获深度 (${currentCaptureLength})` };
|
||||||
|
}
|
||||||
|
return { valid: true };
|
||||||
|
};
|
||||||
|
|
||||||
|
// 设置捕获深度
|
||||||
|
const setCaptureLength = (value: number) => {
|
||||||
|
const validation = validateCaptureLength(value);
|
||||||
|
if (!validation.valid) {
|
||||||
|
alert?.error(validation.message!, 3000);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查预捕获深度是否仍然有效
|
||||||
|
if (preCaptureLength.value >= value) {
|
||||||
|
preCaptureLength.value = Math.max(0, value - 1);
|
||||||
|
alert?.warn(`预捕获深度已自动调整为 ${preCaptureLength.value}`, 3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
captureLength.value = value;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 设置预捕获深度
|
||||||
|
const setPreCaptureLength = (value: number) => {
|
||||||
|
const validation = validatePreCaptureLength(value, captureLength.value);
|
||||||
|
if (!validation.valid) {
|
||||||
|
alert?.error(validation.message!, 3000);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
preCaptureLength.value = value;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 设置通道组
|
||||||
|
const setChannelDiv = (channelCount: number) => {
|
||||||
|
// 验证通道数量是否有效
|
||||||
|
if (!channelDivOptions.find(option => option.value === channelCount)) {
|
||||||
|
console.error(`无效的通道组设置: ${channelCount}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
currentChannelDiv.value = channelCount;
|
||||||
|
|
||||||
|
// 禁用所有通道
|
||||||
|
channels.forEach((channel) => {
|
||||||
|
channel.enabled = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 启用指定数量的通道(从CH0开始)
|
||||||
|
for (let i = 0; i < channelCount && i < channels.length; i++) {
|
||||||
|
channels[i].enabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const option = channelDivOptions.find(opt => opt.value === channelCount);
|
||||||
|
alert?.success(`已设置为${option?.label}`, 2000);
|
||||||
|
};
|
||||||
|
|
||||||
|
const setGlobalMode = (mode: GlobalCaptureMode) => {
|
||||||
|
currentGlobalMode.value = mode;
|
||||||
|
const modeOption = globalModes.find((m) => m.value === mode);
|
||||||
|
alert?.info(`全局触发模式已设置为 ${modeOption?.label}`, 2000);
|
||||||
|
};
|
||||||
|
|
||||||
|
const setClockDiv = (mode: AnalyzerClockDiv) => {
|
||||||
|
currentclockDiv.value = mode;
|
||||||
|
const modeOption = ClockDivOptions.find((m) => m.value === mode);
|
||||||
|
alert?.info(`时钟分频已设置为 ${modeOption?.label}`, 2000);
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetConfiguration = () => {
|
||||||
|
currentGlobalMode.value = GlobalCaptureMode.AND;
|
||||||
|
currentChannelDiv.value = 8; // 重置为默认的8通道
|
||||||
|
currentclockDiv.value = AnalyzerClockDiv.DIV1; // 重置为默认采样频率
|
||||||
|
setChannelDiv(8); // 重置为默认的8通道
|
||||||
|
|
||||||
|
signalConfigs.forEach((signal) => {
|
||||||
|
signal.operator = SignalOperator.Equal;
|
||||||
|
signal.value = SignalValue.NotCare;
|
||||||
|
});
|
||||||
|
|
||||||
|
alert?.info("配置已重置", 2000);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 添加设置逻辑数据的方法
|
||||||
|
const setLogicData = (data: LogicDataType) => {
|
||||||
|
logicData.value = data;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getCaptureData = async () => {
|
||||||
|
try {
|
||||||
|
const client = AuthManager.createAuthenticatedLogicAnalyzerClient();
|
||||||
|
// 获取捕获数据,使用当前设置的捕获长度
|
||||||
|
const base64Data = await client.getCaptureData(captureLength.value);
|
||||||
|
|
||||||
|
// 将base64数据转换为bytes
|
||||||
|
const binaryString = atob(base64Data);
|
||||||
|
const bytes = new Uint8Array(binaryString.length);
|
||||||
|
for (let i = 0; i < binaryString.length; i++) {
|
||||||
|
bytes[i] = binaryString.charCodeAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据当前通道数量解析数据
|
||||||
|
const channelCount = currentChannelDiv.value;
|
||||||
|
const timeStepNs = currentSamplePeriodNs.value;
|
||||||
|
|
||||||
|
let sampleCount: number;
|
||||||
|
let x: number[];
|
||||||
|
let y: number[][];
|
||||||
|
|
||||||
|
if (channelCount === 1) {
|
||||||
|
// 1通道:每个字节包含8个时间单位的数据
|
||||||
|
sampleCount = bytes.length * 8;
|
||||||
|
|
||||||
|
// 创建时间轴
|
||||||
|
x = Array.from(
|
||||||
|
{ length: sampleCount },
|
||||||
|
(_, i) => (i * timeStepNs) / 1000,
|
||||||
|
); // 转换为微秒
|
||||||
|
|
||||||
|
// 创建通道数据数组
|
||||||
|
y = Array.from(
|
||||||
|
{ length: 1 },
|
||||||
|
() => new Array(sampleCount),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 解析数据:每个字节的8个位对应8个时间单位
|
||||||
|
for (let byteIndex = 0; byteIndex < bytes.length; byteIndex++) {
|
||||||
|
const byte = bytes[byteIndex];
|
||||||
|
for (let bitIndex = 0; bitIndex < 8; bitIndex++) {
|
||||||
|
const timeIndex = byteIndex * 8 + bitIndex;
|
||||||
|
y[0][timeIndex] = (byte >> bitIndex) & 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (channelCount === 2) {
|
||||||
|
// 2通道:每个字节包含4个时间单位的数据
|
||||||
|
sampleCount = bytes.length * 4;
|
||||||
|
|
||||||
|
// 创建时间轴
|
||||||
|
x = Array.from(
|
||||||
|
{ length: sampleCount },
|
||||||
|
(_, i) => (i * timeStepNs) / 1000,
|
||||||
|
); // 转换为微秒
|
||||||
|
|
||||||
|
// 创建通道数据数组
|
||||||
|
y = Array.from(
|
||||||
|
{ length: 2 },
|
||||||
|
() => new Array(sampleCount),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 解析数据:每个字节的8个位对应4个时间单位的2通道数据
|
||||||
|
// 位分布:[T3_CH1, T3_CH0, T2_CH1, T2_CH0, T1_CH1, T1_CH0, T0_CH1, T0_CH0]
|
||||||
|
for (let byteIndex = 0; byteIndex < bytes.length; byteIndex++) {
|
||||||
|
const byte = bytes[byteIndex];
|
||||||
|
for (let timeUnit = 0; timeUnit < 4; timeUnit++) {
|
||||||
|
const timeIndex = byteIndex * 4 + timeUnit;
|
||||||
|
const bitOffset = timeUnit * 2;
|
||||||
|
y[0][timeIndex] = (byte >> bitOffset) & 1; // CH0
|
||||||
|
y[1][timeIndex] = (byte >> (bitOffset + 1)) & 1; // CH1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (channelCount === 4) {
|
||||||
|
// 4通道:每个字节包含2个时间单位的数据
|
||||||
|
sampleCount = bytes.length * 2;
|
||||||
|
|
||||||
|
// 创建时间轴
|
||||||
|
x = Array.from(
|
||||||
|
{ length: sampleCount },
|
||||||
|
(_, i) => (i * timeStepNs) / 1000,
|
||||||
|
); // 转换为微秒
|
||||||
|
|
||||||
|
// 创建通道数据数组
|
||||||
|
y = Array.from(
|
||||||
|
{ length: 4 },
|
||||||
|
() => new Array(sampleCount),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 解析数据:每个字节的8个位对应2个时间单位的4通道数据
|
||||||
|
// 位分布:[T1_CH3, T1_CH2, T1_CH1, T1_CH0, T0_CH3, T0_CH2, T0_CH1, T0_CH0]
|
||||||
|
for (let byteIndex = 0; byteIndex < bytes.length; byteIndex++) {
|
||||||
|
const byte = bytes[byteIndex];
|
||||||
|
|
||||||
|
// 处理第一个时间单位(低4位)
|
||||||
|
const timeIndex1 = byteIndex * 2;
|
||||||
|
for (let channel = 0; channel < 4; channel++) {
|
||||||
|
y[channel][timeIndex1] = (byte >> channel) & 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理第二个时间单位(高4位)
|
||||||
|
const timeIndex2 = byteIndex * 2 + 1;
|
||||||
|
for (let channel = 0; channel < 4; channel++) {
|
||||||
|
y[channel][timeIndex2] = (byte >> (channel + 4)) & 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (channelCount === 8) {
|
||||||
|
// 8通道:每个字节包含1个时间单位的8个通道数据
|
||||||
|
sampleCount = bytes.length;
|
||||||
|
|
||||||
|
// 创建时间轴
|
||||||
|
x = Array.from(
|
||||||
|
{ length: sampleCount },
|
||||||
|
(_, i) => (i * timeStepNs) / 1000,
|
||||||
|
); // 转换为微秒
|
||||||
|
|
||||||
|
// 创建8个通道的数据
|
||||||
|
y = Array.from(
|
||||||
|
{ length: 8 },
|
||||||
|
() => new Array(sampleCount),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 解析每个字节的8个位到对应通道
|
||||||
|
for (let i = 0; i < sampleCount; i++) {
|
||||||
|
const byte = bytes[i];
|
||||||
|
for (let channel = 0; channel < 8; channel++) {
|
||||||
|
// bit0对应ch0, bit1对应ch1, ..., bit7对应ch7
|
||||||
|
y[channel][i] = (byte >> channel) & 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (channelCount === 16) {
|
||||||
|
// 16通道:每2个字节包含1个时间单位的16个通道数据
|
||||||
|
sampleCount = bytes.length / 2;
|
||||||
|
|
||||||
|
// 创建时间轴
|
||||||
|
x = Array.from(
|
||||||
|
{ length: sampleCount },
|
||||||
|
(_, i) => (i * timeStepNs) / 1000,
|
||||||
|
); // 转换为微秒
|
||||||
|
|
||||||
|
// 创建16个通道的数据
|
||||||
|
y = Array.from(
|
||||||
|
{ length: 16 },
|
||||||
|
() => new Array(sampleCount),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 解析数据:每2个字节为一个时间单位
|
||||||
|
for (let timeIndex = 0; timeIndex < sampleCount; timeIndex++) {
|
||||||
|
const byteIndex = timeIndex * 2;
|
||||||
|
const byte1 = bytes[byteIndex]; // [7:0]
|
||||||
|
const byte2 = bytes[byteIndex + 1]; // [15:8]
|
||||||
|
|
||||||
|
// 处理低8位通道 [7:0]
|
||||||
|
for (let channel = 0; channel < 8; channel++) {
|
||||||
|
y[channel][timeIndex] = (byte1 >> channel) & 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理高8位通道 [15:8]
|
||||||
|
for (let channel = 0; channel < 8; channel++) {
|
||||||
|
y[channel + 8][timeIndex] = (byte2 >> channel) & 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (channelCount === 32) {
|
||||||
|
// 32通道:每4个字节包含1个时间单位的32个通道数据
|
||||||
|
sampleCount = bytes.length / 4;
|
||||||
|
|
||||||
|
// 创建时间轴
|
||||||
|
x = Array.from(
|
||||||
|
{ length: sampleCount },
|
||||||
|
(_, i) => (i * timeStepNs) / 1000,
|
||||||
|
); // 转换为微秒
|
||||||
|
|
||||||
|
// 创建32个通道的数据
|
||||||
|
y = Array.from(
|
||||||
|
{ length: 32 },
|
||||||
|
() => new Array(sampleCount),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 解析数据:每4个字节为一个时间单位
|
||||||
|
for (let timeIndex = 0; timeIndex < sampleCount; timeIndex++) {
|
||||||
|
const byteIndex = timeIndex * 4;
|
||||||
|
const byte1 = bytes[byteIndex]; // [7:0]
|
||||||
|
const byte2 = bytes[byteIndex + 1]; // [15:8]
|
||||||
|
const byte3 = bytes[byteIndex + 2]; // [23:16]
|
||||||
|
const byte4 = bytes[byteIndex + 3]; // [31:24]
|
||||||
|
|
||||||
|
// 处理 [7:0]
|
||||||
|
for (let channel = 0; channel < 8; channel++) {
|
||||||
|
y[channel][timeIndex] = (byte1 >> channel) & 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理 [15:8]
|
||||||
|
for (let channel = 0; channel < 8; channel++) {
|
||||||
|
y[channel + 8][timeIndex] = (byte2 >> channel) & 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理 [23:16]
|
||||||
|
for (let channel = 0; channel < 8; channel++) {
|
||||||
|
y[channel + 16][timeIndex] = (byte3 >> channel) & 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理 [31:24]
|
||||||
|
for (let channel = 0; channel < 8; channel++) {
|
||||||
|
y[channel + 24][timeIndex] = (byte4 >> channel) & 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error(`不支持的通道数量: ${channelCount}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置逻辑数据
|
||||||
|
const logicData: LogicDataType = {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
xUnit: "us", // 微秒单位
|
||||||
|
};
|
||||||
|
|
||||||
|
setLogicData(logicData);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("获取捕获数据失败:", error);
|
||||||
|
alert?.error("获取捕获数据失败", 3000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const startCapture = async () => {
|
||||||
|
// 检查是否有其他操作正在进行
|
||||||
|
if (operationMutex.isLocked()) {
|
||||||
|
alert.warn("有其他操作正在进行中,请稍后再试", 3000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
isCapturing.value = true;
|
||||||
|
const release = await operationMutex.acquire();
|
||||||
|
try {
|
||||||
|
const client = AuthManager.createAuthenticatedLogicAnalyzerClient();
|
||||||
|
|
||||||
|
// 1. 先应用配置
|
||||||
|
alert?.info("正在应用配置...", 2000);
|
||||||
|
|
||||||
|
// 准备配置数据 - 包含所有32个通道,未启用的通道设置为默认值
|
||||||
|
const allSignals = signalConfigs.map((signal, index) => {
|
||||||
|
if (channels[index].enabled) {
|
||||||
|
// 启用的通道使用用户配置的触发条件
|
||||||
|
return signal;
|
||||||
|
} else {
|
||||||
|
// 未启用的通道设置为默认触发条件
|
||||||
|
return new SignalTriggerConfig({
|
||||||
|
signalIndex: index,
|
||||||
|
operator: SignalOperator.Equal,
|
||||||
|
value: SignalValue.NotCare,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const config = new CaptureConfig({
|
||||||
|
globalMode: currentGlobalMode.value,
|
||||||
|
channelDiv: getChannelDivEnum(currentChannelDiv.value),
|
||||||
|
captureLength: captureLength.value,
|
||||||
|
preCaptureLength: preCaptureLength.value,
|
||||||
|
clockDiv: currentclockDiv.value,
|
||||||
|
signalConfigs: allSignals,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 发送配置
|
||||||
|
const configSuccess = await client.configureCapture(config);
|
||||||
|
if (!configSuccess) {
|
||||||
|
throw new Error("配置应用失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
const enabledChannelCount = channels.filter((ch) => ch.enabled).length;
|
||||||
|
alert?.success(
|
||||||
|
`配置已应用,启用了 ${enabledChannelCount} 个通道,捕获深度: ${captureLength.value}`,
|
||||||
|
2000,
|
||||||
|
);
|
||||||
|
|
||||||
|
// 2. 设置捕获模式为开始捕获
|
||||||
|
const captureStarted = await client.setCaptureMode(true, false);
|
||||||
|
if (!captureStarted) {
|
||||||
|
throw new Error("无法启动捕获");
|
||||||
|
}
|
||||||
|
|
||||||
|
alert?.info("开始捕获信号...", 2000);
|
||||||
|
|
||||||
|
// 3. 轮询捕获状态
|
||||||
|
let captureCompleted = false;
|
||||||
|
while (isCapturing.value) {
|
||||||
|
const status = await client.getCaptureStatus();
|
||||||
|
|
||||||
|
// 检查是否捕获完成
|
||||||
|
if (status === CaptureStatus.CaptureDone) {
|
||||||
|
captureCompleted = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否仍在捕获中
|
||||||
|
if (
|
||||||
|
status === CaptureStatus.CaptureBusy ||
|
||||||
|
status === CaptureStatus.CaptureOn ||
|
||||||
|
status === CaptureStatus.CaptureForce
|
||||||
|
) {
|
||||||
|
// 等待500毫秒后继续轮询
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其他状态视为错误
|
||||||
|
// throw new Error(`捕获状态异常: ${status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果捕获被停止,不继续处理数据
|
||||||
|
if (!captureCompleted) {
|
||||||
|
alert?.info("捕获已停止", 2000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await getCaptureData();
|
||||||
|
alert.success(`捕获完成!`, 3000);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("捕获失败:", error);
|
||||||
|
alert?.error(
|
||||||
|
`捕获失败: ${error instanceof Error ? error.message : "未知错误"}`,
|
||||||
|
3000,
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
isCapturing.value = false;
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const stopCapture = async () => {
|
||||||
|
// 检查是否正在捕获
|
||||||
|
if (!isCapturing.value) {
|
||||||
|
alert.warn("当前没有正在进行的捕获操作", 2000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置捕获状态为false,这会使轮询停止
|
||||||
|
isCapturing.value = false;
|
||||||
|
|
||||||
|
const release = await operationMutex.acquire();
|
||||||
|
try {
|
||||||
|
const client = AuthManager.createAuthenticatedLogicAnalyzerClient();
|
||||||
|
|
||||||
|
// 执行强制捕获来停止当前捕获
|
||||||
|
const forceSuccess = await client.setCaptureMode(false, false);
|
||||||
|
if (!forceSuccess) {
|
||||||
|
throw new Error("无法停止捕获");
|
||||||
|
}
|
||||||
|
|
||||||
|
alert.info("已停止强制捕获...", 2000);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("停止捕获失败:", error);
|
||||||
|
alert.error(
|
||||||
|
`停止捕获失败: ${error instanceof Error ? error.message : "未知错误"}`,
|
||||||
|
3000,
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const forceCapture = async () => {
|
||||||
|
// 检查是否正在捕获
|
||||||
|
if (!isCapturing.value) {
|
||||||
|
alert.warn("当前没有正在进行的捕获操作", 2000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const release = await operationMutex.acquire();
|
||||||
|
try {
|
||||||
|
const client = AuthManager.createAuthenticatedLogicAnalyzerClient();
|
||||||
|
|
||||||
|
// 执行强制捕获来停止当前捕获
|
||||||
|
const forceSuccess = await client.setCaptureMode(true, true);
|
||||||
|
if (!forceSuccess) {
|
||||||
|
throw new Error("无法执行强制捕获");
|
||||||
|
}
|
||||||
|
|
||||||
|
await getCaptureData();
|
||||||
|
alert.success(`强制捕获完成!`, 3000);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("强制捕获失败:", error);
|
||||||
|
alert.error(
|
||||||
|
`强制捕获失败: ${error instanceof Error ? error.message : "未知错误"}`,
|
||||||
|
3000,
|
||||||
|
);
|
||||||
|
} finally{
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 添加检查操作状态的计算属性
|
||||||
|
const isOperationInProgress = computed(
|
||||||
|
() => isApplying.value || isCapturing.value || operationMutex.isLocked(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 添加生成测试数据的方法
|
||||||
|
const generateTestData = () => {
|
||||||
|
const sampleRate = currentSampleFrequency.value; // 使用当前设置的采样频率
|
||||||
|
const duration = 0.001; // 1ms的数据
|
||||||
|
const points = Math.floor(sampleRate * duration);
|
||||||
|
|
||||||
|
const x = Array.from(
|
||||||
|
{ length: points },
|
||||||
|
(_, i) => (i * currentSamplePeriodNs.value) / 1000, // 时间轴,单位:微秒
|
||||||
|
);
|
||||||
|
|
||||||
|
// Generate 8 channels with different digital patterns
|
||||||
|
const y = [
|
||||||
|
// Channel 0: Clock signal 1MHz
|
||||||
|
Array.from(
|
||||||
|
{ length: points },
|
||||||
|
(_, i) => Math.floor((1_000_000 * i) / sampleRate) % 2,
|
||||||
|
),
|
||||||
|
// Channel 1: Clock/2 signal 500kHz
|
||||||
|
Array.from(
|
||||||
|
{ length: points },
|
||||||
|
(_, i) => Math.floor((500_000 * i) / sampleRate) % 2,
|
||||||
|
),
|
||||||
|
// Channel 2: Clock/4 signal 250kHz
|
||||||
|
Array.from(
|
||||||
|
{ length: points },
|
||||||
|
(_, i) => Math.floor((250_000 * i) / sampleRate) % 2,
|
||||||
|
),
|
||||||
|
// Channel 3: Clock/8 signal 125kHz
|
||||||
|
Array.from(
|
||||||
|
{ length: points },
|
||||||
|
(_, i) => Math.floor((125_000 * i) / sampleRate) % 2,
|
||||||
|
),
|
||||||
|
// Channel 4: Data signal (pseudo-random pattern)
|
||||||
|
Array.from({ length: points }, (_, i) =>
|
||||||
|
Math.abs(Math.floor(Math.sin(i * 0.001) * 10) % 2),
|
||||||
|
),
|
||||||
|
// Channel 5: Enable signal (periodic pulse)
|
||||||
|
Array.from({ length: points }, (_, i) =>
|
||||||
|
Math.floor(i / 250) % 10 < 3 ? 1 : 0,
|
||||||
|
),
|
||||||
|
// Channel 6: Reset signal (occasional pulse)
|
||||||
|
Array.from({ length: points }, (_, i) =>
|
||||||
|
Math.floor(i / 1000) % 20 === 0 ? 1 : 0,
|
||||||
|
),
|
||||||
|
// Channel 7: Status signal (slow changing)
|
||||||
|
Array.from({ length: points }, (_, i) => Math.floor(i / 5000) % 2),
|
||||||
|
];
|
||||||
|
|
||||||
|
// 同时更新通道标签为更有意义的名称
|
||||||
|
const testChannelNames = [
|
||||||
|
"CLK",
|
||||||
|
"CLK/2",
|
||||||
|
"CLK/4",
|
||||||
|
"CLK/8",
|
||||||
|
"PWM",
|
||||||
|
"ENABLE",
|
||||||
|
"RESET",
|
||||||
|
"STATUS",
|
||||||
|
];
|
||||||
|
|
||||||
|
channels.forEach((channel, index) => {
|
||||||
|
channel.label = testChannelNames[index];
|
||||||
|
});
|
||||||
|
|
||||||
|
// 设置逻辑数据
|
||||||
|
setChannelDiv(8);
|
||||||
|
setLogicData({ x, y, xUnit: "us" }); // 改为微秒单位
|
||||||
|
|
||||||
|
alert?.success("测试数据生成成功", 2000);
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
// 原有的逻辑数据
|
||||||
|
logicData,
|
||||||
|
|
||||||
|
// 触发设置状态
|
||||||
|
currentGlobalMode,
|
||||||
|
currentChannelDiv, // 导出当前通道组状态
|
||||||
|
captureLength, // 导出捕获深度
|
||||||
|
preCaptureLength, // 导出预捕获深度
|
||||||
|
currentclockDiv, // 导出当前采样频率状态
|
||||||
|
isApplying,
|
||||||
|
isCapturing, // 导出捕获状态
|
||||||
|
isOperationInProgress, // 导出操作进行状态
|
||||||
|
channels,
|
||||||
|
signalConfigs,
|
||||||
|
enabledChannelCount,
|
||||||
|
channelNames,
|
||||||
|
enabledChannels,
|
||||||
|
currentSampleFrequency, // 导出当前采样频率
|
||||||
|
currentSamplePeriodNs, // 导出当前采样周期
|
||||||
|
|
||||||
|
// 选项数据
|
||||||
|
globalModes,
|
||||||
|
operators,
|
||||||
|
signalValues,
|
||||||
|
channelDivOptions, // 导出通道组选项
|
||||||
|
ClockDivOptions, // 导出采样频率选项
|
||||||
|
|
||||||
|
// 捕获深度常量和验证
|
||||||
|
CAPTURE_LENGTH_MIN,
|
||||||
|
CAPTURE_LENGTH_MAX,
|
||||||
|
PRE_CAPTURE_LENGTH_MIN,
|
||||||
|
validateCaptureLength,
|
||||||
|
validatePreCaptureLength,
|
||||||
|
setCaptureLength,
|
||||||
|
setPreCaptureLength,
|
||||||
|
|
||||||
|
// 触发设置方法
|
||||||
|
setChannelDiv, // 导出设置通道组方法
|
||||||
|
setGlobalMode,
|
||||||
|
setClockDiv, // 导出设置采样频率方法
|
||||||
|
resetConfiguration,
|
||||||
|
setLogicData,
|
||||||
|
startCapture,
|
||||||
|
forceCapture,
|
||||||
|
stopCapture,
|
||||||
|
generateTestData,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export { useProvideLogicAnalyzer, useLogicAnalyzerState };
|
||||||
238
src/components/LogicAnalyzer/LogicalWaveFormDisplay.vue
Normal file
238
src/components/LogicAnalyzer/LogicalWaveFormDisplay.vue
Normal file
@@ -0,0 +1,238 @@
|
|||||||
|
<template>
|
||||||
|
<div
|
||||||
|
class="w-full"
|
||||||
|
:class="{
|
||||||
|
'h-48': !analyzer.logicData.value,
|
||||||
|
'h-150': analyzer.logicData.value,
|
||||||
|
}"
|
||||||
|
>
|
||||||
|
<v-chart
|
||||||
|
v-if="analyzer.logicData.value"
|
||||||
|
class="w-full h-full"
|
||||||
|
:option="option"
|
||||||
|
autoresize
|
||||||
|
:update-options="updateOptions"
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
v-else
|
||||||
|
class="w-full h-full flex flex-col gap-6 items-center justify-center"
|
||||||
|
>
|
||||||
|
<div class="text-center">
|
||||||
|
<h3 class="text-xl font-semibold text-slate-600 mb-2">
|
||||||
|
暂无逻辑分析数据
|
||||||
|
</h3>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, shallowRef } from "vue";
|
||||||
|
import VChart from "vue-echarts";
|
||||||
|
|
||||||
|
// Echarts
|
||||||
|
import { use } from "echarts/core";
|
||||||
|
import { LineChart } from "echarts/charts";
|
||||||
|
import {
|
||||||
|
TooltipComponent,
|
||||||
|
GridComponent,
|
||||||
|
DataZoomComponent,
|
||||||
|
AxisPointerComponent,
|
||||||
|
ToolboxComponent,
|
||||||
|
} from "echarts/components";
|
||||||
|
import { CanvasRenderer } from "echarts/renderers";
|
||||||
|
import type { ComposeOption } from "echarts/core";
|
||||||
|
import type { LineSeriesOption } from "echarts/charts";
|
||||||
|
import type {
|
||||||
|
AxisPointerComponentOption,
|
||||||
|
TooltipComponentOption,
|
||||||
|
GridComponentOption,
|
||||||
|
DataZoomComponentOption,
|
||||||
|
} from "echarts/components";
|
||||||
|
import type {
|
||||||
|
ToolboxComponentOption,
|
||||||
|
XAXisOption,
|
||||||
|
YAXisOption,
|
||||||
|
} from "echarts/types/dist/shared";
|
||||||
|
import { useLogicAnalyzerState } from "./LogicAnalyzerManager";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
import { isUndefined } from "lodash";
|
||||||
|
|
||||||
|
use([
|
||||||
|
TooltipComponent,
|
||||||
|
ToolboxComponent,
|
||||||
|
GridComponent,
|
||||||
|
AxisPointerComponent,
|
||||||
|
DataZoomComponent,
|
||||||
|
LineChart,
|
||||||
|
CanvasRenderer,
|
||||||
|
]);
|
||||||
|
|
||||||
|
type EChartsOption = ComposeOption<
|
||||||
|
| AxisPointerComponentOption
|
||||||
|
| TooltipComponentOption
|
||||||
|
| ToolboxComponentOption
|
||||||
|
| GridComponentOption
|
||||||
|
| DataZoomComponentOption
|
||||||
|
| LineSeriesOption
|
||||||
|
>;
|
||||||
|
|
||||||
|
const analyzer = useRequiredInjection(useLogicAnalyzerState);
|
||||||
|
|
||||||
|
// 添加更新选项来减少重绘
|
||||||
|
const updateOptions = shallowRef({
|
||||||
|
notMerge: false,
|
||||||
|
lazyUpdate: true,
|
||||||
|
silent: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const option = computed((): EChartsOption => {
|
||||||
|
if (isUndefined(analyzer.logicData.value)) return {};
|
||||||
|
|
||||||
|
// 只获取启用的通道
|
||||||
|
const enabledChannels = analyzer.enabledChannels.value;
|
||||||
|
const enabledChannelIndices = analyzer.channels
|
||||||
|
.map((channel, index) => (channel.enabled ? index : -1))
|
||||||
|
.filter((index) => index !== -1);
|
||||||
|
|
||||||
|
const channelCount = enabledChannels.length;
|
||||||
|
const channelSpacing = 2; // 每个通道之间的间距
|
||||||
|
|
||||||
|
// 如果没有启用的通道,返回空配置
|
||||||
|
if (channelCount === 0) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用单个网格
|
||||||
|
const grids: GridComponentOption[] = [
|
||||||
|
{
|
||||||
|
left: "5%",
|
||||||
|
right: "5%",
|
||||||
|
top: "5%",
|
||||||
|
bottom: "15%",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// 单个X轴
|
||||||
|
const xAxis: XAXisOption[] = [
|
||||||
|
{
|
||||||
|
type: "category",
|
||||||
|
boundaryGap: false,
|
||||||
|
data: analyzer.logicData.value.x.map((x) => x.toFixed(3)),
|
||||||
|
axisLabel: {
|
||||||
|
formatter: (value: string) =>
|
||||||
|
analyzer.logicData.value
|
||||||
|
? `${value}${analyzer.logicData.value.xUnit}`
|
||||||
|
: `${value}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// 单个Y轴,范围根据启用通道数量调整
|
||||||
|
const yAxis: YAXisOption[] = [
|
||||||
|
{
|
||||||
|
type: "value",
|
||||||
|
min: -0.5,
|
||||||
|
max: channelCount * channelSpacing - 0.5,
|
||||||
|
interval: channelSpacing,
|
||||||
|
axisLabel: {
|
||||||
|
formatter: (value: number) => {
|
||||||
|
const channelIndex = Math.round(value / channelSpacing);
|
||||||
|
return channelIndex < channelCount
|
||||||
|
? enabledChannels[channelIndex].label
|
||||||
|
: "";
|
||||||
|
},
|
||||||
|
},
|
||||||
|
splitLine: { show: false },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// 创建系列数据,只包含启用的通道
|
||||||
|
const series: LineSeriesOption[] = enabledChannelIndices.map(
|
||||||
|
(originalIndex: number, displayIndex: number) => ({
|
||||||
|
name: enabledChannels[displayIndex].label,
|
||||||
|
type: "line",
|
||||||
|
data: analyzer.logicData.value!.y[originalIndex].map(
|
||||||
|
(value: number) => value + displayIndex * channelSpacing + 0.2,
|
||||||
|
),
|
||||||
|
step: "end",
|
||||||
|
lineStyle: {
|
||||||
|
width: 2,
|
||||||
|
color: enabledChannels[displayIndex].color,
|
||||||
|
},
|
||||||
|
areaStyle: {
|
||||||
|
opacity: 0.3,
|
||||||
|
origin: displayIndex * channelSpacing,
|
||||||
|
color: enabledChannels[displayIndex].color,
|
||||||
|
},
|
||||||
|
symbol: "none",
|
||||||
|
// 优化性能配置
|
||||||
|
sampling: "lttb",
|
||||||
|
// 减少动画以避免闪烁
|
||||||
|
animation: false,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
// 全局动画配置
|
||||||
|
animation: false,
|
||||||
|
tooltip: {
|
||||||
|
trigger: "axis",
|
||||||
|
axisPointer: {
|
||||||
|
type: "line",
|
||||||
|
label: {
|
||||||
|
backgroundColor: "#6a7985",
|
||||||
|
},
|
||||||
|
// 减少axisPointer的动画
|
||||||
|
animation: false,
|
||||||
|
},
|
||||||
|
formatter: (params: any) => {
|
||||||
|
if (Array.isArray(params) && params.length > 0) {
|
||||||
|
const timeValue = analyzer.logicData.value!.x[params[0].dataIndex];
|
||||||
|
const dataIndex = params[0].dataIndex;
|
||||||
|
|
||||||
|
let tooltip = `Time: ${timeValue.toFixed(3)}${analyzer.logicData.value!.xUnit}<br/>`;
|
||||||
|
|
||||||
|
// 只显示启用通道在当前时间点的原始数值(0或1)
|
||||||
|
enabledChannelIndices.forEach(
|
||||||
|
(originalIndex: number, displayIndex: number) => {
|
||||||
|
const channelName = enabledChannels[displayIndex].label;
|
||||||
|
const originalValue =
|
||||||
|
analyzer.logicData.value!.y[originalIndex][dataIndex];
|
||||||
|
tooltip += `${channelName}: ${originalValue}<br/>`;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return tooltip;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
},
|
||||||
|
// 优化tooltip性能
|
||||||
|
hideDelay: 100,
|
||||||
|
},
|
||||||
|
toolbox: {
|
||||||
|
feature: {
|
||||||
|
restore: {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
grid: grids,
|
||||||
|
xAxis: xAxis,
|
||||||
|
yAxis: yAxis,
|
||||||
|
dataZoom: [
|
||||||
|
{
|
||||||
|
show: true,
|
||||||
|
realtime: true,
|
||||||
|
start: 0,
|
||||||
|
end: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "inside",
|
||||||
|
realtime: true,
|
||||||
|
start: 0,
|
||||||
|
end: 100,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
series: series,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
</script>
|
||||||
478
src/components/LogicAnalyzer/TriggerSettings.vue
Normal file
478
src/components/LogicAnalyzer/TriggerSettings.vue
Normal file
@@ -0,0 +1,478 @@
|
|||||||
|
<template>
|
||||||
|
<div class="space-y-6">
|
||||||
|
<!-- 通道配置 -->
|
||||||
|
<div class="form-control">
|
||||||
|
<!-- 全局触发模式选择和通道组配置 -->
|
||||||
|
<div class="flex flex-col gap-6 my-4 mx-2">
|
||||||
|
<div class="flex flex-col lg:flex-row gap-6">
|
||||||
|
<div class="flex flex-col gap-2">
|
||||||
|
<label class="block text-sm font-semibold antialiased">
|
||||||
|
全局触发逻辑
|
||||||
|
</label>
|
||||||
|
<div class="relative w-[200px]">
|
||||||
|
<button
|
||||||
|
tabindex="0"
|
||||||
|
type="button"
|
||||||
|
class="flex items-center gap-4 justify-between h-max outline-none focus:outline-none bg-transparent ring-transparent border border-slate-200 transition-all duration-300 ease-in disabled:opacity-50 disabled:pointer-events-none select-none text-start text-sm rounded-md py-2 px-2.5 ring shadow-sm hover:border-slate-800 hover:ring-slate-800/10 focus:border-slate-800 focus:ring-slate-800/10 w-full"
|
||||||
|
@click="toggleGlobalModeDropdown"
|
||||||
|
:aria-expanded="showGlobalModeDropdown"
|
||||||
|
aria-haspopup="listbox"
|
||||||
|
role="combobox"
|
||||||
|
>
|
||||||
|
<span>{{ currentGlobalModeLabel }}</span>
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor" class="h-[1em] w-[1em] translate-x-0.5 stroke-[1.5]">
|
||||||
|
<path d="M17 8L12 3L7 8" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path>
|
||||||
|
<path d="M17 16L12 21L7 16" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<input readonly style="display:none" :value="currentGlobalMode" />
|
||||||
|
<!-- 下拉菜单 -->
|
||||||
|
<div v-if="showGlobalModeDropdown" class="absolute top-full left-0 right-0 mt-1 bg-white border border-slate-200 rounded-md shadow-lg z-50">
|
||||||
|
<div
|
||||||
|
v-for="mode in globalModes"
|
||||||
|
:key="mode.value"
|
||||||
|
@click="selectGlobalMode(mode.value)"
|
||||||
|
class="px-3 py-2 text-sm text-slate-700 hover:bg-slate-100 cursor-pointer"
|
||||||
|
:class="{ 'bg-slate-100': mode.value === currentGlobalMode }"
|
||||||
|
>
|
||||||
|
{{ mode.label }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p class="flex items-center text-xs text-slate-400">
|
||||||
|
{{ currentGlobalModeDescription }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-col gap-2">
|
||||||
|
<label class="block text-sm font-semibold antialiased">
|
||||||
|
通道组
|
||||||
|
</label>
|
||||||
|
<div class="relative w-[200px]">
|
||||||
|
<button
|
||||||
|
tabindex="0"
|
||||||
|
type="button"
|
||||||
|
class="flex items-center gap-4 justify-between h-max outline-none focus:outline-none bg-transparent ring-transparent border border-slate-200 transition-all duration-300 ease-in disabled:opacity-50 disabled:pointer-events-none select-none text-start text-sm rounded-md py-2 px-2.5 ring shadow-sm hover:border-slate-800 hover:ring-slate-800/10 focus:border-slate-800 focus:ring-slate-800/10 w-full"
|
||||||
|
@click="toggleChannelDivDropdown"
|
||||||
|
:aria-expanded="showChannelDivDropdown"
|
||||||
|
aria-haspopup="listbox"
|
||||||
|
role="combobox"
|
||||||
|
>
|
||||||
|
<span>{{ currentChannelDivLabel }}</span>
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor" class="h-[1em] w-[1em] translate-x-0.5 stroke-[1.5]">
|
||||||
|
<path d="M17 8L12 3L7 8" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path>
|
||||||
|
<path d="M17 16L12 21L7 16" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<input readonly style="display:none" :value="currentChannelDiv" />
|
||||||
|
<!-- 下拉菜单 -->
|
||||||
|
<div v-if="showChannelDivDropdown" class="absolute top-full left-0 right-0 mt-1 bg-white border border-slate-200 rounded-md shadow-lg z-50">
|
||||||
|
<div
|
||||||
|
v-for="option in channelDivOptions"
|
||||||
|
:key="option.value"
|
||||||
|
@click="selectChannelDiv(option.value)"
|
||||||
|
class="px-3 py-2 text-sm text-slate-700 hover:bg-slate-100 cursor-pointer"
|
||||||
|
:class="{ 'bg-slate-100': option.value === currentChannelDiv }"
|
||||||
|
>
|
||||||
|
{{ option.label }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p class="flex items-center text-xs text-slate-400">
|
||||||
|
{{ currentChannelDivDescription }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-col gap-2">
|
||||||
|
<label class="block text-sm font-semibold antialiased text-slate-800">
|
||||||
|
采样频率
|
||||||
|
</label>
|
||||||
|
<div class="relative w-[200px]">
|
||||||
|
<button
|
||||||
|
tabindex="0"
|
||||||
|
type="button"
|
||||||
|
class="flex items-center gap-4 justify-between h-max outline-none focus:outline-none text-slate-600 bg-transparent ring-transparent border border-slate-200 transition-all duration-300 ease-in disabled:opacity-50 disabled:pointer-events-none select-none text-start text-sm rounded-md py-2 px-2.5 ring shadow-sm hover:border-slate-800 hover:ring-slate-800/10 focus:border-slate-800 focus:ring-slate-800/10 w-full"
|
||||||
|
@click="toggleClockDivDropdown"
|
||||||
|
:aria-expanded="showClockDivDropdown"
|
||||||
|
aria-haspopup="listbox"
|
||||||
|
role="combobox"
|
||||||
|
>
|
||||||
|
<span>{{ currentClockDivLabel }}</span>
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor" class="h-[1em] w-[1em] translate-x-0.5 stroke-[1.5]">
|
||||||
|
<path d="M17 8L12 3L7 8" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path>
|
||||||
|
<path d="M17 16L12 21L7 16" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<input readonly style="display:none" :value="currentclockDiv" />
|
||||||
|
<!-- 下拉菜单 -->
|
||||||
|
<div v-if="showClockDivDropdown" class="absolute top-full left-0 right-0 mt-1 bg-white border border-slate-200 rounded-md shadow-lg z-50">
|
||||||
|
<div
|
||||||
|
v-for="option in ClockDivOptions"
|
||||||
|
:key="option.value"
|
||||||
|
@click="selectClockDiv(option.value)"
|
||||||
|
class="px-3 py-2 text-sm text-slate-700 hover:bg-slate-100 cursor-pointer"
|
||||||
|
:class="{ 'bg-slate-100': option.value === currentclockDiv }"
|
||||||
|
>
|
||||||
|
{{ option.label }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p class="flex items-center text-xs text-slate-400">
|
||||||
|
{{ currentClockDivDescription }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-col gap-2">
|
||||||
|
<label class="block text-sm font-semibold antialiased">
|
||||||
|
捕获深度
|
||||||
|
</label>
|
||||||
|
<div class="relative w-[200px]">
|
||||||
|
<button
|
||||||
|
@click="decreaseCaptureLength"
|
||||||
|
class="absolute right-9 top-1 rounded-md border border-transparent p-1.5 text-center text-sm transition-all hover:bg-slate-100 focus:bg-slate-100 active:bg-slate-100 disabled:pointer-events-none disabled:opacity-50 disabled:shadow-none"
|
||||||
|
type="button"
|
||||||
|
:disabled="captureLength <= CAPTURE_LENGTH_MIN"
|
||||||
|
>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="w-4 h-4">
|
||||||
|
<path d="M3.75 7.25a.75.75 0 0 0 0 1.5h8.5a.75.75 0 0 0 0-1.5h-8.5Z" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<input
|
||||||
|
v-model.number="captureLength"
|
||||||
|
@change="handleCaptureLengthChange"
|
||||||
|
type="number"
|
||||||
|
:min="CAPTURE_LENGTH_MIN"
|
||||||
|
:max="CAPTURE_LENGTH_MAX"
|
||||||
|
class="w-full bg-transparent placeholder:text-sm border border-slate-200 rounded-md pl-3 pr-20 py-2 transition duration-300 ease focus:outline-none focus:border-slate-400 ring ring-transparent hover:ring-slate-800/10 focus:ring-slate-800/10 hover:border-slate-800 shadow-sm focus:shadow appearance-none [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"
|
||||||
|
:placeholder="CAPTURE_LENGTH_MIN.toString()"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
@click="increaseCaptureLength"
|
||||||
|
class="absolute right-1 top-1 rounded-md border border-transparent p-1.5 text-center text-sm transition-all hover:bg-slate-100 focus:bg-slate-100 active:bg-slate-100 disabled:pointer-events-none disabled:opacity-50 disabled:shadow-none"
|
||||||
|
type="button"
|
||||||
|
:disabled="captureLength >= CAPTURE_LENGTH_MAX"
|
||||||
|
>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="w-4 h-4">
|
||||||
|
<path d="M8.75 3.75a.75.75 0 0 0-1.5 0v3.5h-3.5a.75.75 0 0 0 0 1.5h3.5v3.5a.75.75 0 0 0 1.5 0v-3.5h3.5a.75.75 0 0 0 0-1.5h-3.5v-3.5Z" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<p class="flex items-center text-xs text-slate-400">
|
||||||
|
范围: {{ CAPTURE_LENGTH_MIN.toLocaleString() }} - {{ CAPTURE_LENGTH_MAX.toLocaleString() }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-col gap-2">
|
||||||
|
<label class="block text-sm font-semibold antialiased">
|
||||||
|
预捕获深度
|
||||||
|
</label>
|
||||||
|
<div class="relative w-[200px]">
|
||||||
|
<button
|
||||||
|
@click="decreasePreCaptureLength"
|
||||||
|
class="absolute right-9 top-1 rounded-md border border-transparent p-1.5 text-center text-sm transition-all hover:bg-slate-100 focus:bg-slate-100 active:bg-slate-100 disabled:pointer-events-none disabled:opacity-50 disabled:shadow-none"
|
||||||
|
type="button"
|
||||||
|
:disabled="preCaptureLength <= PRE_CAPTURE_LENGTH_MIN"
|
||||||
|
>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="w-4 h-4">
|
||||||
|
<path d="M3.75 7.25a.75.75 0 0 0 0 1.5h8.5a.75.75 0 0 0 0-1.5h-8.5Z" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<input
|
||||||
|
v-model.number="preCaptureLength"
|
||||||
|
@change="handlePreCaptureLengthChange"
|
||||||
|
type="number"
|
||||||
|
:min="PRE_CAPTURE_LENGTH_MIN"
|
||||||
|
:max="Math.max(0, captureLength - 1)"
|
||||||
|
class="w-full bg-transparent placeholder:text-sm border border-slate-200 rounded-md pl-3 pr-20 py-2 transition duration-300 ease focus:outline-none focus:border-slate-400 ring ring-transparent hover:ring-slate-800/10 focus:ring-slate-800/10 hover:border-slate-800 shadow-sm focus:shadow appearance-none [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"
|
||||||
|
:placeholder="PRE_CAPTURE_LENGTH_MIN.toString()"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
@click="increasePreCaptureLength"
|
||||||
|
class="absolute right-1 top-1 rounded-md border border-transparent p-1.5 text-center text-sm transition-all hover:bg-slate-100 focus:bg-slate-100 active:bg-slate-100 disabled:pointer-events-none disabled:opacity-50 disabled:shadow-none"
|
||||||
|
type="button"
|
||||||
|
:disabled="preCaptureLength >= Math.max(0, captureLength - 1)"
|
||||||
|
>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" class="w-4 h-4">
|
||||||
|
<path d="M8.75 3.75a.75.75 0 0 0-1.5 0v3.5h-3.5a.75.75 0 0 0 0 1.5h3.5v3.5a.75.75 0 0 0 1.5 0v-3.5h3.5a.75.75 0 0 0 0-1.5h-3.5v-3.5Z" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<p class="flex items-center text-xs text-slate-400">
|
||||||
|
范围: {{ PRE_CAPTURE_LENGTH_MIN }} - {{ Math.max(0, captureLength - 1) }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-col gap-2">
|
||||||
|
<label class="block text-sm font-semibold antialiased">
|
||||||
|
重置配置
|
||||||
|
</label>
|
||||||
|
<div class="relative w-[200px]">
|
||||||
|
<button
|
||||||
|
@click="resetConfiguration"
|
||||||
|
class="w-10 h-10 bg-transparent text-red-600 text-sm border border-red-200 rounded-md py-2 px-2.5 transition duration-300 ease ring ring-transparent hover:ring-red-600/10 focus:ring-red-600/10 hover:border-red-600 shadow-sm focus:shadow flex items-center justify-center"
|
||||||
|
type="button"
|
||||||
|
title="重置配置"
|
||||||
|
>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" class="w-4 h-4">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<p class="flex items-center text-xs text-slate-400">
|
||||||
|
恢复所有设置到默认值
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- 通道列表 -->
|
||||||
|
<div class="space-y-2">
|
||||||
|
<!-- 表头 -->
|
||||||
|
<div
|
||||||
|
class="flex items-center gap-2 p-2 bg-base-300 rounded-lg text-sm font-medium"
|
||||||
|
>
|
||||||
|
<span class="w-16">通道</span>
|
||||||
|
<span class="w-32">标签</span>
|
||||||
|
<span class="w-16">颜色</span>
|
||||||
|
<span class="w-32">触发操作</span>
|
||||||
|
<span class="w-32">触发值</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 通道配置网格 - 根据当前通道组动态显示 -->
|
||||||
|
<div class="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4 gap-4">
|
||||||
|
<div
|
||||||
|
v-for="(channel, index) in channels.filter(ch => ch.enabled)"
|
||||||
|
:key="index"
|
||||||
|
class="flex items-center gap-2 p-3 bg-base-200 rounded-lg hover:bg-base-300 transition-colors"
|
||||||
|
>
|
||||||
|
<!-- 通道编号和颜色指示 -->
|
||||||
|
<div class="flex items-center gap-2 w-16">
|
||||||
|
<span class="font-mono font-medium">CH{{ channels.indexOf(channel) }}</span>
|
||||||
|
<div
|
||||||
|
class="w-3 h-3 rounded-full border-2 border-white shadow-sm"
|
||||||
|
:style="{ backgroundColor: channel.color }"
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 通道标签 -->
|
||||||
|
<div class="form-control w-32">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
v-model="channel.label"
|
||||||
|
:placeholder="`通道 ${channels.indexOf(channel)}`"
|
||||||
|
class="input input-sm input-bordered w-full"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 颜色选择 -->
|
||||||
|
<div class="form-control w-16">
|
||||||
|
<input
|
||||||
|
type="color"
|
||||||
|
v-model="channel.color"
|
||||||
|
class="w-8 h-8 rounded border-2 border-base-300 cursor-pointer"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 触发操作符选择 -->
|
||||||
|
<select
|
||||||
|
v-model="signalConfigs[channels.indexOf(channel)].operator"
|
||||||
|
class="select select-sm select-bordered w-32"
|
||||||
|
>
|
||||||
|
<option
|
||||||
|
v-for="op in operators"
|
||||||
|
:key="op.value"
|
||||||
|
:value="op.value"
|
||||||
|
>
|
||||||
|
{{ op.label }}
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<!-- 触发信号值选择 -->
|
||||||
|
<select
|
||||||
|
v-model="signalConfigs[channels.indexOf(channel)].value"
|
||||||
|
class="select select-sm select-bordered w-32"
|
||||||
|
>
|
||||||
|
<option
|
||||||
|
v-for="val in signalValues"
|
||||||
|
:key="val.value"
|
||||||
|
:value="val.value"
|
||||||
|
>
|
||||||
|
{{ val.label }}
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 当没有启用通道时的提示 -->
|
||||||
|
<div v-if="enabledChannelCount === 0" class="text-center py-8 text-base-content/60">
|
||||||
|
<p class="text-lg font-medium">未启用任何通道</p>
|
||||||
|
<p class="text-sm">请选择通道组来配置逻辑分析仪</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, ref, onMounted, onUnmounted } from "vue";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
import { useLogicAnalyzerState } from "./LogicAnalyzerManager";
|
||||||
|
|
||||||
|
const {
|
||||||
|
currentGlobalMode,
|
||||||
|
currentChannelDiv,
|
||||||
|
currentclockDiv,
|
||||||
|
captureLength,
|
||||||
|
preCaptureLength,
|
||||||
|
isApplying,
|
||||||
|
channels,
|
||||||
|
signalConfigs,
|
||||||
|
enabledChannelCount,
|
||||||
|
globalModes,
|
||||||
|
operators,
|
||||||
|
signalValues,
|
||||||
|
channelDivOptions,
|
||||||
|
ClockDivOptions,
|
||||||
|
CAPTURE_LENGTH_MIN,
|
||||||
|
CAPTURE_LENGTH_MAX,
|
||||||
|
PRE_CAPTURE_LENGTH_MIN,
|
||||||
|
validateCaptureLength,
|
||||||
|
validatePreCaptureLength,
|
||||||
|
setCaptureLength,
|
||||||
|
setPreCaptureLength,
|
||||||
|
setChannelDiv,
|
||||||
|
setGlobalMode,
|
||||||
|
setClockDiv,
|
||||||
|
resetConfiguration,
|
||||||
|
} = useRequiredInjection(useLogicAnalyzerState);
|
||||||
|
|
||||||
|
// 下拉菜单状态
|
||||||
|
const showGlobalModeDropdown = ref(false);
|
||||||
|
const showChannelDivDropdown = ref(false);
|
||||||
|
const showClockDivDropdown = ref(false);
|
||||||
|
|
||||||
|
// 处理捕获深度变化
|
||||||
|
const handleCaptureLengthChange = () => {
|
||||||
|
setCaptureLength(captureLength.value);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 处理预捕获深度变化
|
||||||
|
const handlePreCaptureLengthChange = () => {
|
||||||
|
setPreCaptureLength(preCaptureLength.value);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 增加捕获深度
|
||||||
|
const increaseCaptureLength = () => {
|
||||||
|
const newValue = Math.min(captureLength.value + 1024, CAPTURE_LENGTH_MAX);
|
||||||
|
setCaptureLength(newValue);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 减少捕获深度
|
||||||
|
const decreaseCaptureLength = () => {
|
||||||
|
const newValue = Math.max(captureLength.value - 1024, CAPTURE_LENGTH_MIN);
|
||||||
|
setCaptureLength(newValue);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 增加预捕获深度
|
||||||
|
const increasePreCaptureLength = () => {
|
||||||
|
const maxValue = Math.max(0, captureLength.value - 1);
|
||||||
|
const newValue = Math.min(preCaptureLength.value + 64, maxValue);
|
||||||
|
setPreCaptureLength(newValue);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 减少预捕获深度
|
||||||
|
const decreasePreCaptureLength = () => {
|
||||||
|
const newValue = Math.max(preCaptureLength.value - 64, PRE_CAPTURE_LENGTH_MIN);
|
||||||
|
setPreCaptureLength(newValue);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 计算属性:获取当前全局模式的标签
|
||||||
|
const currentGlobalModeLabel = computed(() => {
|
||||||
|
const mode = globalModes.find(m => m.value === currentGlobalMode.value);
|
||||||
|
return mode ? mode.label : '';
|
||||||
|
});
|
||||||
|
|
||||||
|
// 计算属性:获取当前全局模式的描述
|
||||||
|
const currentGlobalModeDescription = computed(() => {
|
||||||
|
const mode = globalModes.find(m => m.value === currentGlobalMode.value);
|
||||||
|
return mode ? mode.description : '';
|
||||||
|
});
|
||||||
|
|
||||||
|
// 计算属性:获取当前通道组的标签
|
||||||
|
const currentChannelDivLabel = computed(() => {
|
||||||
|
const option = channelDivOptions.find(opt => opt.value === currentChannelDiv.value);
|
||||||
|
return option ? option.label : '';
|
||||||
|
});
|
||||||
|
|
||||||
|
// 计算属性:获取当前通道组的描述
|
||||||
|
const currentChannelDivDescription = computed(() => {
|
||||||
|
const option = channelDivOptions.find(opt => opt.value === currentChannelDiv.value);
|
||||||
|
return option ? option.description : '';
|
||||||
|
});
|
||||||
|
|
||||||
|
// 计算属性:获取当前采样频率的标签
|
||||||
|
const currentClockDivLabel = computed(() => {
|
||||||
|
const option = ClockDivOptions.find(opt => opt.value === currentclockDiv.value);
|
||||||
|
return option ? option.label : '';
|
||||||
|
});
|
||||||
|
|
||||||
|
// 计算属性:获取当前采样频率的描述
|
||||||
|
const currentClockDivDescription = computed(() => {
|
||||||
|
const option = ClockDivOptions.find(opt => opt.value === currentclockDiv.value);
|
||||||
|
return option ? option.description : '';
|
||||||
|
});
|
||||||
|
|
||||||
|
// 全局模式下拉菜单相关函数
|
||||||
|
const toggleGlobalModeDropdown = () => {
|
||||||
|
showGlobalModeDropdown.value = !showGlobalModeDropdown.value;
|
||||||
|
if (showGlobalModeDropdown.value) {
|
||||||
|
showChannelDivDropdown.value = false;
|
||||||
|
showClockDivDropdown.value = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const selectGlobalMode = (mode: any) => {
|
||||||
|
setGlobalMode(mode);
|
||||||
|
showGlobalModeDropdown.value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 通道组下拉菜单相关函数
|
||||||
|
const toggleChannelDivDropdown = () => {
|
||||||
|
showChannelDivDropdown.value = !showChannelDivDropdown.value;
|
||||||
|
if (showChannelDivDropdown.value) {
|
||||||
|
showGlobalModeDropdown.value = false;
|
||||||
|
showClockDivDropdown.value = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const selectChannelDiv = (value: number) => {
|
||||||
|
setChannelDiv(value);
|
||||||
|
showChannelDivDropdown.value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 采样频率下拉菜单相关函数
|
||||||
|
const toggleClockDivDropdown = () => {
|
||||||
|
showClockDivDropdown.value = !showClockDivDropdown.value;
|
||||||
|
if (showClockDivDropdown.value) {
|
||||||
|
showGlobalModeDropdown.value = false;
|
||||||
|
showChannelDivDropdown.value = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const selectClockDiv = (value: any) => {
|
||||||
|
setClockDiv(value);
|
||||||
|
showClockDivDropdown.value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 点击其他地方关闭下拉菜单
|
||||||
|
const handleClickOutside = (event: MouseEvent) => {
|
||||||
|
const target = event.target as HTMLElement;
|
||||||
|
if (!target.closest('.relative')) {
|
||||||
|
showGlobalModeDropdown.value = false;
|
||||||
|
showChannelDivDropdown.value = false;
|
||||||
|
showClockDivDropdown.value = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
document.addEventListener('click', handleClickOutside);
|
||||||
|
});
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
document.removeEventListener('click', handleClickOutside);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
5
src/components/LogicAnalyzer/index.ts
Normal file
5
src/components/LogicAnalyzer/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import LogicalWaveFormDisplay from "./LogicalWaveFormDisplay.vue";
|
||||||
|
|
||||||
|
export { LogicalWaveFormDisplay };
|
||||||
|
export { default as TriggerSettings } from './TriggerSettings.vue'
|
||||||
|
export { useProvideLogicAnalyzer , useLogicAnalyzerState } from './LogicAnalyzerManager.ts'
|
||||||
@@ -6,6 +6,7 @@ import hljs from 'highlight.js';
|
|||||||
import 'highlight.js/styles/github.css'; // 亮色主题
|
import 'highlight.js/styles/github.css'; // 亮色主题
|
||||||
// 导入主题存储
|
// 导入主题存储
|
||||||
import { useThemeStore } from '@/stores/theme';
|
import { useThemeStore } from '@/stores/theme';
|
||||||
|
import { AuthManager } from '@/utils/AuthManager';
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
content: {
|
content: {
|
||||||
@@ -15,6 +16,10 @@ const props = defineProps({
|
|||||||
removeFirstH1: {
|
removeFirstH1: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false
|
default: false
|
||||||
|
},
|
||||||
|
examId: {
|
||||||
|
type: String,
|
||||||
|
default: ''
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -23,6 +28,42 @@ const themeStore = useThemeStore();
|
|||||||
// 使用 isDarkTheme 函数来检查当前是否为暗色主题
|
// 使用 isDarkTheme 函数来检查当前是否为暗色主题
|
||||||
const isDarkMode = computed(() => themeStore.isDarkTheme());
|
const isDarkMode = computed(() => themeStore.isDarkTheme());
|
||||||
|
|
||||||
|
// 图片资源缓存
|
||||||
|
const imageResourceCache = ref<Map<string, string>>(new Map());
|
||||||
|
|
||||||
|
// 获取图片资源ID的函数
|
||||||
|
async function getImageResourceId(examId: string, imagePath: string): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const client = AuthManager.createAuthenticatedResourceClient();
|
||||||
|
const resources = await client.getResourceList(examId, 'images', 'template');
|
||||||
|
|
||||||
|
// 查找匹配的图片资源
|
||||||
|
const imageResource = resources.find(r => r.name === imagePath || r.name.endsWith(imagePath));
|
||||||
|
|
||||||
|
return imageResource ? imageResource.id.toString() : null;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取图片资源ID失败:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通过资源ID获取图片数据URL
|
||||||
|
async function getImageDataUrl(resourceId: string): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const client = AuthManager.createAuthenticatedResourceClient();
|
||||||
|
const response = await client.getResourceById(parseInt(resourceId));
|
||||||
|
|
||||||
|
if (response && response.data) {
|
||||||
|
return URL.createObjectURL(response.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取图片数据失败:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 监听主题变化
|
// 监听主题变化
|
||||||
watch(() => themeStore.currentTheme, () => {
|
watch(() => themeStore.currentTheme, () => {
|
||||||
// 主题变化时更新代码高亮样式
|
// 主题变化时更新代码高亮样式
|
||||||
@@ -54,6 +95,27 @@ const renderedContent = computed(() => {
|
|||||||
// 创建自定义渲染器
|
// 创建自定义渲染器
|
||||||
const renderer = new marked.Renderer();
|
const renderer = new marked.Renderer();
|
||||||
|
|
||||||
|
// 重写图片渲染方法,处理相对路径
|
||||||
|
renderer.image = (href, title, text) => {
|
||||||
|
let src = href;
|
||||||
|
|
||||||
|
console.log(`原始图片路径: ${href}, examId: ${props.examId}`);
|
||||||
|
|
||||||
|
// 如果是相对路径且有实验ID,需要通过动态API获取
|
||||||
|
if (props.examId && href && href.startsWith('./')) {
|
||||||
|
// 对于相对路径的图片,我们需要先获取图片资源ID,然后通过动态API获取
|
||||||
|
// 暂时保留原始路径,在后处理中进行替换
|
||||||
|
src = href;
|
||||||
|
console.log(`保留原始路径用于后处理: ${src}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const titleAttr = title ? ` title="${title}"` : '';
|
||||||
|
const altAttr = text ? ` alt="${text}"` : '';
|
||||||
|
const dataOriginal = href && href.startsWith('./') ? ` data-original-src="${href}"` : '';
|
||||||
|
console.log(`最终渲染的HTML: <img src="${src}"${titleAttr}${altAttr}${dataOriginal} />`);
|
||||||
|
return `<img src="${src}"${titleAttr}${altAttr}${dataOriginal} />`;
|
||||||
|
};
|
||||||
|
|
||||||
// 重写代码块渲染方法,添加语言信息
|
// 重写代码块渲染方法,添加语言信息
|
||||||
renderer.code = (code, incomingLanguage) => {
|
renderer.code = (code, incomingLanguage) => {
|
||||||
// 确保语言参数是字符串
|
// 确保语言参数是字符串
|
||||||
@@ -67,14 +129,60 @@ const renderedContent = computed(() => {
|
|||||||
return `<pre class="hljs" data-language="${validLanguage}"><code class="language-${validLanguage}">${highlightedCode}</code></pre>`;
|
return `<pre class="hljs" data-language="${validLanguage}"><code class="language-${validLanguage}">${highlightedCode}</code></pre>`;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 设置 marked 选项
|
// 设置 marked 选项并解析内容
|
||||||
marked.use({
|
let html = marked.parse(processedContent, {
|
||||||
renderer: renderer,
|
renderer: renderer,
|
||||||
gfm: true,
|
gfm: true,
|
||||||
breaks: true
|
breaks: true
|
||||||
});
|
}) as string;
|
||||||
|
|
||||||
return marked(processedContent);
|
// 后处理HTML,异步处理图片
|
||||||
|
if (props.examId) {
|
||||||
|
// 查找所有需要处理的图片
|
||||||
|
const imgMatches = Array.from(html.matchAll(/(<img[^>]+data-original-src=["'])\.\/([^"']+)(["'][^>]*>)/g));
|
||||||
|
|
||||||
|
// 异步处理每个图片
|
||||||
|
imgMatches.forEach(async (match) => {
|
||||||
|
const [fullMatch, prefix, path, suffix] = match;
|
||||||
|
const imagePath = path.replace('images/', '');
|
||||||
|
|
||||||
|
// 检查缓存
|
||||||
|
if (imageResourceCache.value.has(imagePath)) {
|
||||||
|
const cachedUrl = imageResourceCache.value.get(imagePath)!;
|
||||||
|
html = html.replace(fullMatch, `${prefix}${cachedUrl}${suffix.replace(' data-original-src="./'+path+'"', '')}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 获取图片资源ID
|
||||||
|
const resourceId = await getImageResourceId(props.examId, imagePath);
|
||||||
|
if (resourceId) {
|
||||||
|
// 获取图片数据URL
|
||||||
|
const dataUrl = await getImageDataUrl(resourceId);
|
||||||
|
if (dataUrl) {
|
||||||
|
// 缓存URL
|
||||||
|
imageResourceCache.value.set(imagePath, dataUrl);
|
||||||
|
|
||||||
|
// 更新HTML中的图片src
|
||||||
|
const updatedHtml = html.replace(fullMatch, `${prefix}${dataUrl}${suffix.replace(' data-original-src="./'+path+'"', '')}`);
|
||||||
|
|
||||||
|
// 触发重新渲染
|
||||||
|
setTimeout(() => {
|
||||||
|
const imgElements = document.querySelectorAll(`img[data-original-src="./${path}"]`);
|
||||||
|
imgElements.forEach(img => {
|
||||||
|
(img as HTMLImageElement).src = dataUrl;
|
||||||
|
img.removeAttribute('data-original-src');
|
||||||
|
});
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`处理图片 ${imagePath} 失败:`, error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return html;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 页面挂载后,确保应用正确的主题样式
|
// 页面挂载后,确保应用正确的主题样式
|
||||||
|
|||||||
@@ -31,6 +31,12 @@
|
|||||||
工程界面
|
工程界面
|
||||||
</router-link>
|
</router-link>
|
||||||
</li>
|
</li>
|
||||||
|
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
||||||
|
<router-link to="/exam" class="text-base font-medium">
|
||||||
|
<FileText class="icon" />
|
||||||
|
实验列表
|
||||||
|
</router-link>
|
||||||
|
</li>
|
||||||
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
<li class="my-1 hover:translate-x-1 transition-all duration-300">
|
||||||
<router-link to="/test" class="text-base font-medium">
|
<router-link to="/test" class="text-base font-medium">
|
||||||
<FlaskConical class="icon" />
|
<FlaskConical class="icon" />
|
||||||
|
|||||||
287
src/components/Oscilloscope/OscilloscopeManager.ts
Normal file
287
src/components/Oscilloscope/OscilloscopeManager.ts
Normal file
@@ -0,0 +1,287 @@
|
|||||||
|
import { autoResetRef, createInjectionState } from "@vueuse/core";
|
||||||
|
import { shallowRef, reactive, ref, computed } from "vue";
|
||||||
|
import { Mutex } from "async-mutex";
|
||||||
|
import {
|
||||||
|
OscilloscopeFullConfig,
|
||||||
|
OscilloscopeDataResponse,
|
||||||
|
} from "@/APIClient";
|
||||||
|
import { AuthManager } from "@/utils/AuthManager";
|
||||||
|
import { useAlertStore } from "@/components/Alert";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
|
||||||
|
export type OscilloscopeDataType = {
|
||||||
|
x: number[];
|
||||||
|
y: number[] | number[][];
|
||||||
|
xUnit: "s" | "ms" | "us" | "ns";
|
||||||
|
yUnit: "V" | "mV" | "uV";
|
||||||
|
adFrequency: number;
|
||||||
|
adVpp: number;
|
||||||
|
adMax: number;
|
||||||
|
adMin: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 默认配置
|
||||||
|
const DEFAULT_CONFIG: OscilloscopeFullConfig = new OscilloscopeFullConfig({
|
||||||
|
captureEnabled: false,
|
||||||
|
triggerLevel: 128,
|
||||||
|
triggerRisingEdge: true,
|
||||||
|
horizontalShift: 0,
|
||||||
|
decimationRate: 50,
|
||||||
|
autoRefreshRAM: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 采样频率常量(后端返回)
|
||||||
|
const [useProvideOscilloscope, useOscilloscopeState] = createInjectionState(() => {
|
||||||
|
const oscData = shallowRef<OscilloscopeDataType>();
|
||||||
|
const alert = useRequiredInjection(useAlertStore);
|
||||||
|
|
||||||
|
// 互斥锁
|
||||||
|
const operationMutex = new Mutex();
|
||||||
|
|
||||||
|
// 状态
|
||||||
|
const isApplying = ref(false);
|
||||||
|
const isCapturing = ref(false);
|
||||||
|
|
||||||
|
// 配置
|
||||||
|
const config = reactive<OscilloscopeFullConfig>(new OscilloscopeFullConfig({ ...DEFAULT_CONFIG }));
|
||||||
|
|
||||||
|
// 采样点数(由后端数据决定)
|
||||||
|
const sampleCount = ref(0);
|
||||||
|
|
||||||
|
// 采样周期(ns),由adFrequency计算
|
||||||
|
const samplePeriodNs = computed(() =>
|
||||||
|
oscData.value?.adFrequency ? 1_000_000_000 / oscData.value.adFrequency : 200
|
||||||
|
);
|
||||||
|
|
||||||
|
// 应用配置
|
||||||
|
const applyConfiguration = async () => {
|
||||||
|
if (operationMutex.isLocked()) {
|
||||||
|
alert.warn("有其他操作正在进行中,请稍后再试", 3000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const release = await operationMutex.acquire();
|
||||||
|
isApplying.value = true;
|
||||||
|
try {
|
||||||
|
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
|
||||||
|
const success = await client.initialize({ ...config });
|
||||||
|
if (success) {
|
||||||
|
alert.success("示波器配置已应用", 2000);
|
||||||
|
} else {
|
||||||
|
throw new Error("应用失败");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
alert.error("应用配置失败", 3000);
|
||||||
|
} finally {
|
||||||
|
isApplying.value = false;
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 重置配置
|
||||||
|
const resetConfiguration = () => {
|
||||||
|
Object.assign(config, { ...DEFAULT_CONFIG });
|
||||||
|
alert.info("配置已重置", 2000);
|
||||||
|
};
|
||||||
|
|
||||||
|
const clearOscilloscopeData = () => {
|
||||||
|
oscData.value = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取数据
|
||||||
|
const getOscilloscopeData = async () => {
|
||||||
|
try {
|
||||||
|
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
|
||||||
|
const resp: OscilloscopeDataResponse = await client.getData();
|
||||||
|
|
||||||
|
// 解析波形数据
|
||||||
|
const binaryString = atob(resp.waveformData);
|
||||||
|
const bytes = new Uint8Array(binaryString.length);
|
||||||
|
for (let i = 0; i < binaryString.length; i++) {
|
||||||
|
bytes[i] = binaryString.charCodeAt(i);
|
||||||
|
}
|
||||||
|
sampleCount.value = bytes.length;
|
||||||
|
|
||||||
|
// 构建时间轴
|
||||||
|
const x = Array.from(
|
||||||
|
{ length: bytes.length },
|
||||||
|
(_, i) => (i * samplePeriodNs.value) / 1000 // us
|
||||||
|
);
|
||||||
|
const y = Array.from(bytes);
|
||||||
|
|
||||||
|
oscData.value = {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
xUnit: "us",
|
||||||
|
yUnit: "V",
|
||||||
|
adFrequency: resp.adFrequency,
|
||||||
|
adVpp: resp.adVpp,
|
||||||
|
adMax: resp.adMax,
|
||||||
|
adMin: resp.adMin,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
alert.error("获取示波器数据失败", 3000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 定时器引用
|
||||||
|
let refreshIntervalId: number | undefined;
|
||||||
|
// 刷新间隔(毫秒),可根据需要调整
|
||||||
|
const refreshIntervalMs = ref(1000);
|
||||||
|
|
||||||
|
// 定时刷新函数
|
||||||
|
const startAutoRefresh = () => {
|
||||||
|
if (refreshIntervalId !== undefined) return;
|
||||||
|
refreshIntervalId = window.setInterval(async () => {
|
||||||
|
await refreshRAM();
|
||||||
|
await getOscilloscopeData();
|
||||||
|
}, refreshIntervalMs.value);
|
||||||
|
};
|
||||||
|
|
||||||
|
const stopAutoRefresh = () => {
|
||||||
|
if (refreshIntervalId !== undefined) {
|
||||||
|
clearInterval(refreshIntervalId);
|
||||||
|
refreshIntervalId = undefined;
|
||||||
|
isCapturing.value = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 启动捕获
|
||||||
|
const startCapture = async () => {
|
||||||
|
if (operationMutex.isLocked()) {
|
||||||
|
alert.warn("有其他操作正在进行中,请稍后再试", 3000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
isCapturing.value = true;
|
||||||
|
const release = await operationMutex.acquire();
|
||||||
|
try {
|
||||||
|
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
|
||||||
|
const started = await client.startCapture();
|
||||||
|
if (!started) throw new Error("无法启动捕获");
|
||||||
|
alert.info("开始捕获...", 2000);
|
||||||
|
|
||||||
|
// 启动定时刷新
|
||||||
|
startAutoRefresh();
|
||||||
|
} catch (error) {
|
||||||
|
alert.error("捕获失败", 3000);
|
||||||
|
isCapturing.value = false;
|
||||||
|
stopAutoRefresh();
|
||||||
|
} finally {
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 停止捕获
|
||||||
|
const stopCapture = async () => {
|
||||||
|
if (!isCapturing.value) {
|
||||||
|
alert.warn("当前没有正在进行的捕获操作", 2000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
isCapturing.value = false;
|
||||||
|
stopAutoRefresh();
|
||||||
|
const release = await operationMutex.acquire();
|
||||||
|
try {
|
||||||
|
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
|
||||||
|
const stopped = await client.stopCapture();
|
||||||
|
if (!stopped) throw new Error("无法停止捕获");
|
||||||
|
alert.info("捕获已停止", 2000);
|
||||||
|
} catch (error) {
|
||||||
|
alert.error("停止捕获失败", 3000);
|
||||||
|
} finally {
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 更新触发参数
|
||||||
|
const updateTrigger = async (level: number, risingEdge: boolean) => {
|
||||||
|
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
|
||||||
|
try {
|
||||||
|
const ok = await client.updateTrigger(level, risingEdge);
|
||||||
|
if (ok) {
|
||||||
|
config.triggerLevel = level;
|
||||||
|
config.triggerRisingEdge = risingEdge;
|
||||||
|
alert.success("触发参数已更新", 2000);
|
||||||
|
} else {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
alert.error("更新触发参数失败", 2000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 更新采样参数
|
||||||
|
const updateSampling = async (horizontalShift: number, decimationRate: number) => {
|
||||||
|
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
|
||||||
|
try {
|
||||||
|
const ok = await client.updateSampling(horizontalShift, decimationRate);
|
||||||
|
if (ok) {
|
||||||
|
config.horizontalShift = horizontalShift;
|
||||||
|
config.decimationRate = decimationRate;
|
||||||
|
alert.success("采样参数已更新", 2000);
|
||||||
|
} else {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
alert.error("更新采样参数失败", 2000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 手动刷新RAM
|
||||||
|
const refreshRAM = async () => {
|
||||||
|
const client = AuthManager.createAuthenticatedOscilloscopeApiClient();
|
||||||
|
try {
|
||||||
|
const ok = await client.refreshRAM();
|
||||||
|
if (ok) {
|
||||||
|
// alert.success("RAM已刷新", 2000);
|
||||||
|
} else {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
alert.error("刷新RAM失败", 2000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 生成测试数据
|
||||||
|
const generateTestData = () => {
|
||||||
|
const freq = 5_000_000;
|
||||||
|
const duration = 0.001; // 1ms
|
||||||
|
const points = Math.floor(freq * duration);
|
||||||
|
const x = Array.from({ length: points }, (_, i) => (i * 1_000_000_000 / freq) / 1000);
|
||||||
|
const y = Array.from({ length: points }, (_, i) =>
|
||||||
|
Math.floor(Math.sin(i * 0.01) * 127 + 128)
|
||||||
|
);
|
||||||
|
oscData.value = {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
xUnit: "us",
|
||||||
|
yUnit: "V",
|
||||||
|
adFrequency: freq,
|
||||||
|
adVpp: 2.0,
|
||||||
|
adMax: 255,
|
||||||
|
adMin: 0,
|
||||||
|
};
|
||||||
|
alert.success("测试数据生成成功", 2000);
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
oscData,
|
||||||
|
config,
|
||||||
|
isApplying,
|
||||||
|
isCapturing,
|
||||||
|
sampleCount,
|
||||||
|
samplePeriodNs,
|
||||||
|
refreshIntervalMs,
|
||||||
|
|
||||||
|
applyConfiguration,
|
||||||
|
resetConfiguration,
|
||||||
|
clearOscilloscopeData,
|
||||||
|
getOscilloscopeData,
|
||||||
|
startCapture,
|
||||||
|
stopCapture,
|
||||||
|
updateTrigger,
|
||||||
|
updateSampling,
|
||||||
|
refreshRAM,
|
||||||
|
generateTestData,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
export { useProvideOscilloscope, useOscilloscopeState, DEFAULT_CONFIG };
|
||||||
214
src/components/Oscilloscope/OscilloscopeWaveformDisplay.vue
Normal file
214
src/components/Oscilloscope/OscilloscopeWaveformDisplay.vue
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
<template>
|
||||||
|
<div class="w-full h-100 flex flex-col">
|
||||||
|
<!-- 原有内容 -->
|
||||||
|
<v-chart v-if="hasData" class="w-full h-full" :option="option" autoresize />
|
||||||
|
<div v-else class="w-full h-full flex flex-col gap-4 items-center justify-center text-gray-500">
|
||||||
|
<span> 暂无数据 </span>
|
||||||
|
<!-- 采集控制按钮 -->
|
||||||
|
<div class="flex justify-center items-center mb-2">
|
||||||
|
<button
|
||||||
|
class="group relative px-8 py-3 bg-gradient-to-r text-white font-medium rounded-lg shadow-lg hover:shadow-xl transform hover:scale-105 transition-all duration-200 ease-in-out focus:outline-none focus:ring-4 active:scale-95"
|
||||||
|
:class="{
|
||||||
|
'from-blue-500 to-purple-600 hover:from-blue-600 hover:to-purple-700 focus:ring-blue-300':
|
||||||
|
!oscManager.isCapturing.value,
|
||||||
|
'from-red-500 to-red-600 hover:from-red-600 hover:to-red-700 focus:ring-red-300':
|
||||||
|
oscManager.isCapturing.value,
|
||||||
|
}" @click="
|
||||||
|
oscManager.isCapturing.value
|
||||||
|
? oscManager.stopCapture()
|
||||||
|
: oscManager.startCapture()
|
||||||
|
">
|
||||||
|
<span class="flex items-center gap-2">
|
||||||
|
<template v-if="oscManager.isCapturing.value">
|
||||||
|
<Square class="w-5 h-5" />
|
||||||
|
停止采集
|
||||||
|
</template>
|
||||||
|
<template v-else>
|
||||||
|
<Play class="w-5 h-5" />
|
||||||
|
开始采集
|
||||||
|
</template>
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { computed } from "vue";
|
||||||
|
import { forEach } from "lodash";
|
||||||
|
import VChart from "vue-echarts";
|
||||||
|
import { useOscilloscopeState } from "./OscilloscopeManager";
|
||||||
|
|
||||||
|
// Echarts
|
||||||
|
import { use } from "echarts/core";
|
||||||
|
import { LineChart } from "echarts/charts";
|
||||||
|
import {
|
||||||
|
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 {
|
||||||
|
TooltipComponentOption,
|
||||||
|
LegendComponentOption,
|
||||||
|
ToolboxComponentOption,
|
||||||
|
DataZoomComponentOption,
|
||||||
|
GridComponentOption,
|
||||||
|
} from "echarts/components";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
import { Play, Square } from "lucide-vue-next";
|
||||||
|
|
||||||
|
use([
|
||||||
|
TooltipComponent,
|
||||||
|
LegendComponent,
|
||||||
|
ToolboxComponent,
|
||||||
|
DataZoomComponent,
|
||||||
|
GridComponent,
|
||||||
|
LineChart,
|
||||||
|
CanvasRenderer,
|
||||||
|
]);
|
||||||
|
|
||||||
|
type EChartsOption = ComposeOption<
|
||||||
|
| TooltipComponentOption
|
||||||
|
| LegendComponentOption
|
||||||
|
| ToolboxComponentOption
|
||||||
|
| DataZoomComponentOption
|
||||||
|
| GridComponentOption
|
||||||
|
| LineSeriesOption
|
||||||
|
>;
|
||||||
|
|
||||||
|
// 使用 manager 获取 oscilloscope 数据
|
||||||
|
const oscManager = useRequiredInjection(useOscilloscopeState);
|
||||||
|
|
||||||
|
const oscData = computed(() => oscManager.oscData.value);
|
||||||
|
|
||||||
|
const hasData = computed(() => {
|
||||||
|
return (
|
||||||
|
oscData.value &&
|
||||||
|
oscData.value.x &&
|
||||||
|
oscData.value.y &&
|
||||||
|
oscData.value.x.length > 0 &&
|
||||||
|
(Array.isArray(oscData.value.y[0])
|
||||||
|
? oscData.value.y.some((channel: any) => channel.length > 0)
|
||||||
|
: oscData.value.y.length > 0)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
const option = computed((): EChartsOption => {
|
||||||
|
if (!oscData.value || !oscData.value.x || !oscData.value.y) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const isCapturing = oscManager.isCapturing.value;
|
||||||
|
|
||||||
|
const series: LineSeriesOption[] = [];
|
||||||
|
|
||||||
|
// 兼容单通道和多通道,确保 yChannels 为 number[]
|
||||||
|
const yChannels: number[][] = Array.isArray(oscData.value.y[0])
|
||||||
|
? (oscData.value.y as number[][])
|
||||||
|
: [oscData.value.y as number[]];
|
||||||
|
|
||||||
|
forEach(yChannels, (yData, index) => {
|
||||||
|
if (!oscData.value || !yData) return;
|
||||||
|
const seriesData = oscData.value.x.map((xValue, i) => [
|
||||||
|
xValue,
|
||||||
|
yData && yData[i] !== undefined ? yData[i] : 0,
|
||||||
|
]);
|
||||||
|
series.push({
|
||||||
|
type: "line",
|
||||||
|
name: `通道 ${index + 1}`,
|
||||||
|
data: seriesData,
|
||||||
|
smooth: false,
|
||||||
|
symbol: "none",
|
||||||
|
lineStyle: {
|
||||||
|
width: 2,
|
||||||
|
},
|
||||||
|
// 关闭系列动画
|
||||||
|
animation: !isCapturing,
|
||||||
|
animationDuration: isCapturing ? 0 : 1000,
|
||||||
|
animationEasing: isCapturing ? "linear" : "cubicOut",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
grid: {
|
||||||
|
left: "10%",
|
||||||
|
right: "10%",
|
||||||
|
top: "15%",
|
||||||
|
bottom: "25%",
|
||||||
|
},
|
||||||
|
tooltip: {
|
||||||
|
trigger: "axis",
|
||||||
|
formatter: (params: any) => {
|
||||||
|
if (!oscData.value) return "";
|
||||||
|
let result = `时间: ${params[0].data[0].toFixed(2)} ${oscData.value.xUnit}<br/>`;
|
||||||
|
params.forEach((param: any) => {
|
||||||
|
result += `${param.seriesName}: ${param.data[1].toFixed(3)} ${oscData.value?.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: oscData.value ? `时间 (${oscData.value.xUnit})` : "时间",
|
||||||
|
nameLocation: "middle",
|
||||||
|
nameGap: 30,
|
||||||
|
axisLine: {
|
||||||
|
show: true,
|
||||||
|
},
|
||||||
|
axisTick: {
|
||||||
|
show: true,
|
||||||
|
},
|
||||||
|
splitLine: {
|
||||||
|
show: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
yAxis: {
|
||||||
|
type: "value",
|
||||||
|
name: oscData.value ? `电压 (${oscData.value.yUnit})` : "电压",
|
||||||
|
nameLocation: "middle",
|
||||||
|
nameGap: 40,
|
||||||
|
axisLine: {
|
||||||
|
show: true,
|
||||||
|
},
|
||||||
|
axisTick: {
|
||||||
|
show: true,
|
||||||
|
},
|
||||||
|
splitLine: {
|
||||||
|
show: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// 全局动画开关
|
||||||
|
animation: !isCapturing,
|
||||||
|
animationDuration: isCapturing ? 0 : 1000,
|
||||||
|
animationEasing: isCapturing ? "linear" : "cubicOut",
|
||||||
|
series: series,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
</script>
|
||||||
@@ -1,178 +0,0 @@
|
|||||||
<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>
|
|
||||||
@@ -1,42 +1,3 @@
|
|||||||
import WaveformDisplay from "./WaveformDisplay.vue";
|
import OscilloscopeWaveformDisplay from "./OscilloscopeWaveformDisplay.vue";
|
||||||
|
|
||||||
type WaveformDataType = {
|
export { OscilloscopeWaveformDisplay };
|
||||||
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 };
|
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
<template>
|
|
||||||
<button class="btn transition-transform duration-150 ease-in-out hover:scale-120">
|
|
||||||
Button A
|
|
||||||
</button>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script lang="ts" setup></script>
|
|
||||||
|
|
||||||
<style scoped lang="postcss">
|
|
||||||
@import "@/assets/main.css";
|
|
||||||
</style>
|
|
||||||
@@ -1,162 +0,0 @@
|
|||||||
<template>
|
|
||||||
<div
|
|
||||||
class="card card-dash shadow-xl h-screen"
|
|
||||||
:class="[sidebar.isClose ? 'w-31' : 'w-80']"
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
class="card-body flex relative transition-all duration-500 ease-in-out"
|
|
||||||
>
|
|
||||||
<!-- Avatar and Name -->
|
|
||||||
<div class="relative" :class="sidebar.isClose ? 'h-50' : 'h-20'">
|
|
||||||
<!-- Img -->
|
|
||||||
<div
|
|
||||||
class="avatar h-10 fixed top-10"
|
|
||||||
:class="sidebar.isClose ? 'left-10' : 'left-7'"
|
|
||||||
>
|
|
||||||
<div class="rounded-full">
|
|
||||||
<img src="../assets/user.svg" alt="User" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<!-- Text -->
|
|
||||||
<Transition>
|
|
||||||
<div v-if="!sidebar.isClose" class="mx-5 grow fixed left-20 top-11">
|
|
||||||
<label class="text-2xl">用户名</label>
|
|
||||||
</div>
|
|
||||||
</Transition>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Toggle Button -->
|
|
||||||
<button
|
|
||||||
class="btn btn-square rounded-lg p-2 m-3 fixed"
|
|
||||||
:class="sidebar.isClose ? 'left-7 top-23' : 'left-60 top-7'"
|
|
||||||
@click="sidebar.toggleSidebar"
|
|
||||||
>
|
|
||||||
<svg
|
|
||||||
t="1741694970690"
|
|
||||||
:class="sidebar.isClose ? 'rotate-0' : 'rotate-540'"
|
|
||||||
class="icon"
|
|
||||||
viewBox="0 0 1024 1024"
|
|
||||||
version="1.1"
|
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
|
||||||
p-id="4546"
|
|
||||||
xmlns:xlink="http://www.w3.org/1999/xlink"
|
|
||||||
width="200"
|
|
||||||
height="200"
|
|
||||||
>
|
|
||||||
<path
|
|
||||||
d="M803.758 514.017c-0.001-0.311-0.013-0.622-0.018-0.933-0.162-23.974-9.386-47.811-27.743-65.903-0.084-0.082-0.172-0.157-0.256-0.239-0.154-0.154-0.296-0.315-0.451-0.468L417.861 94.096c-37.685-37.153-99.034-37.476-136.331-0.718-37.297 36.758-36.979 97.231 0.707 134.384l290.361 286.257-290.362 286.257c-37.685 37.153-38.004 97.625-0.707 134.383 37.297 36.758 98.646 36.435 136.331-0.718l357.43-352.378c0.155-0.153 0.297-0.314 0.451-0.468 0.084-0.082 0.172-0.157 0.256-0.239 18.354-18.089 27.578-41.922 27.743-65.892 0.004-0.315 0.017-0.631 0.018-0.947z"
|
|
||||||
:fill="theme.isLightTheme() ? '#828282' : '#C0C3C8'"
|
|
||||||
p-id="4547"
|
|
||||||
></path>
|
|
||||||
</svg>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<div class="divider"></div>
|
|
||||||
|
|
||||||
<ul class="menu h-full w-full">
|
|
||||||
<li v-for="item in props.items" class="text-xl my-1">
|
|
||||||
<a @click="router.push(item.page)">
|
|
||||||
<svg
|
|
||||||
t="1741694797806"
|
|
||||||
class="icon h-[1.5em] w-[1.5em] opacity-50 mx-1"
|
|
||||||
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>
|
|
||||||
<Transition>
|
|
||||||
<p class="break-keep" v-if="!sidebar.isClose">{{ item.text }}</p>
|
|
||||||
</Transition>
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<div class="divider"></div>
|
|
||||||
|
|
||||||
<ul class="menu w-full">
|
|
||||||
<li>
|
|
||||||
<a @click="theme.toggleTheme" class="text-xl">
|
|
||||||
<ThemeControlButton />
|
|
||||||
<Transition>
|
|
||||||
<p v-if="!sidebar.isClose" class="break-keep">改变主题</p>
|
|
||||||
</Transition>
|
|
||||||
<Transition>
|
|
||||||
<ThemeControlToggle v-if="!sidebar.isClose" />
|
|
||||||
</Transition>
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script setup lang="ts">
|
|
||||||
import iconMenu from "../assets/menu.svg";
|
|
||||||
import "../router";
|
|
||||||
import { useThemeStore } from "@/stores/theme";
|
|
||||||
import { useSidebarStore } from "@/stores/sidebar";
|
|
||||||
import ThemeControlButton from "./ThemeControlButton.vue";
|
|
||||||
import ThemeControlToggle from "./ThemeControlToggle.vue";
|
|
||||||
import router from "../router";
|
|
||||||
|
|
||||||
const theme = useThemeStore();
|
|
||||||
const sidebar = useSidebarStore();
|
|
||||||
|
|
||||||
type Item = {
|
|
||||||
id: number;
|
|
||||||
icon: string;
|
|
||||||
text: string;
|
|
||||||
page: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
items?: Array<Item>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const props = withDefaults(defineProps<Props>(), {
|
|
||||||
items: () => [
|
|
||||||
{ id: 1, icon: iconMenu, text: "btn1", page: "/" },
|
|
||||||
{ id: 2, icon: iconMenu, text: "btn2", page: "/" },
|
|
||||||
{ id: 3, icon: iconMenu, text: "btn3", page: "/" },
|
|
||||||
{ id: 4, icon: iconMenu, text: "btn4", page: "/" },
|
|
||||||
],
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style scoped lang="postcss">
|
|
||||||
@reference "../assets/main.css";
|
|
||||||
|
|
||||||
* {
|
|
||||||
@apply transition-all duration-500 ease-in-out;
|
|
||||||
}
|
|
||||||
|
|
||||||
.v-enter-active,
|
|
||||||
.v-leave-active {
|
|
||||||
transition: opacity 0.3s ease;
|
|
||||||
}
|
|
||||||
|
|
||||||
.v-enter-from,
|
|
||||||
.v-leave-to {
|
|
||||||
opacity: 0;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
@@ -31,8 +31,20 @@
|
|||||||
|
|
||||||
<!-- 标题覆盖层 -->
|
<!-- 标题覆盖层 -->
|
||||||
<div class="absolute bottom-0 left-0 right-0 p-4 bg-gradient-to-t from-base-300 to-transparent">
|
<div class="absolute bottom-0 left-0 right-0 p-4 bg-gradient-to-t from-base-300 to-transparent">
|
||||||
|
<div class="flex flex-col gap-2">
|
||||||
<h3 class="text-lg font-bold text-base-content">{{ tutorial.title }}</h3>
|
<h3 class="text-lg font-bold text-base-content">{{ tutorial.title }}</h3>
|
||||||
<p class="text-sm opacity-80 truncate">{{ tutorial.description }}</p>
|
<p class="text-sm opacity-80 truncate">{{ tutorial.description }}</p>
|
||||||
|
<!-- 标签显示 -->
|
||||||
|
<div v-if="tutorial.tags && tutorial.tags.length > 0" class="flex flex-wrap gap-1">
|
||||||
|
<span
|
||||||
|
v-for="tag in tutorial.tags.slice(0, 3)"
|
||||||
|
:key="tag"
|
||||||
|
class="badge badge-outline badge-xs text-xs"
|
||||||
|
>
|
||||||
|
{{ tag }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -54,6 +66,8 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, onMounted, onUnmounted } from 'vue';
|
import { ref, onMounted, onUnmounted } from 'vue';
|
||||||
import { useRouter } from 'vue-router';
|
import { useRouter } from 'vue-router';
|
||||||
|
import { AuthManager } from '@/utils/AuthManager';
|
||||||
|
import type { ExamSummary } from '@/APIClient';
|
||||||
|
|
||||||
// 接口定义
|
// 接口定义
|
||||||
interface Tutorial {
|
interface Tutorial {
|
||||||
@@ -61,7 +75,7 @@ interface Tutorial {
|
|||||||
title: string;
|
title: string;
|
||||||
description: string;
|
description: string;
|
||||||
thumbnail?: string;
|
thumbnail?: string;
|
||||||
docPath: string;
|
tags: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Props
|
// Props
|
||||||
@@ -81,87 +95,93 @@ let autoRotationTimer: number | null = null;
|
|||||||
// 处理卡片点击
|
// 处理卡片点击
|
||||||
const handleCardClick = (index: number, tutorialId: string) => {
|
const handleCardClick = (index: number, tutorialId: string) => {
|
||||||
if (index === currentIndex.value) {
|
if (index === currentIndex.value) {
|
||||||
goToTutorial(tutorialId);
|
goToExam(tutorialId);
|
||||||
} else {
|
} else {
|
||||||
setActiveCard(index);
|
setActiveCard(index);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 从 public/doc 目录加载例程信息
|
// 从数据库加载实验数据
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
try {
|
try {
|
||||||
// 尝试从API获取教程目录
|
console.log('正在从数据库加载实验数据...');
|
||||||
let tutorialIds: string[] = [];
|
|
||||||
try {
|
// 创建认证客户端
|
||||||
const response = await fetch('/api/tutorial');
|
const client = AuthManager.createAuthenticatedExamClient();
|
||||||
if (response.ok) {
|
|
||||||
const data = await response.json();
|
// 获取实验列表
|
||||||
tutorialIds = data.tutorials || [];
|
const examList: ExamSummary[] = await client.getExamList();
|
||||||
}
|
|
||||||
} catch (error) {
|
// 筛选可见的实验并转换为Tutorial格式
|
||||||
console.warn('无法从API获取教程目录,使用默认值:', error);
|
const visibleExams = examList
|
||||||
|
.filter(exam => exam.isVisibleToUsers)
|
||||||
|
.slice(0, 6); // 限制轮播显示最多6个实验
|
||||||
|
|
||||||
|
if (visibleExams.length === 0) {
|
||||||
|
console.warn('没有找到可见的实验');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果API调用失败或返回空列表,使用默认值
|
// 转换数据格式并获取封面图片
|
||||||
if (tutorialIds.length === 0) {
|
const tutorialPromises = visibleExams.map(async (exam) => {
|
||||||
console.log('使用默认教程列表');
|
let thumbnail: string | undefined;
|
||||||
tutorialIds = ['01', '02', '03', '04', '05', '06', '11', '12', '13']; // 默认例程
|
|
||||||
} else {
|
|
||||||
console.log('使用API获取的教程列表:', tutorialIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 为每个例程创建对象并尝试获取文档标题
|
|
||||||
const tutorialPromises = tutorialIds.map(async (id) => {
|
|
||||||
// 尝试读取doc.md获取标题
|
|
||||||
let title = `例程 ${id}`;
|
|
||||||
let description = "点击加载此例程";
|
|
||||||
let thumbnail = `/doc/${id}/cover.png`; // 默认使用第一张图片作为缩略图
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 尝试读取文档内容获取标题
|
// 获取实验的封面资源(模板资源)
|
||||||
const response = await fetch(`/doc/${id}/doc.md`);
|
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||||
if (response.ok) {
|
const resourceList = await resourceClient.getResourceList(exam.id, 'cover', 'template');
|
||||||
const text = await response.text();
|
if (resourceList && resourceList.length > 0) {
|
||||||
// 从Markdown提取标题
|
// 使用第一个封面资源
|
||||||
const titleMatch = text.match(/^#\s+(.+)$/m);
|
const coverResource = resourceList[0];
|
||||||
if (titleMatch && titleMatch[1]) {
|
const fileResponse = await resourceClient.getResourceById(coverResource.id);
|
||||||
title = titleMatch[1].trim();
|
// 创建Blob URL作为缩略图
|
||||||
}
|
thumbnail = URL.createObjectURL(fileResponse.data);
|
||||||
|
|
||||||
// 提取第一段作为描述
|
|
||||||
const descMatch = text.match(/\n\n([^#\n][^\n]+)/);
|
|
||||||
if (descMatch && descMatch[1]) {
|
|
||||||
description = descMatch[1].substring(0, 100).trim();
|
|
||||||
if (description.length === 100) description += '...';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(`无法读取例程${id}的文档内容:`, error);
|
console.warn(`无法获取实验${exam.id}的封面图片:`, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id,
|
id: exam.id,
|
||||||
title,
|
title: exam.name,
|
||||||
description,
|
description: '点击查看实验详情',
|
||||||
thumbnail,
|
thumbnail,
|
||||||
docPath: `/doc/${id}/doc.md`
|
tags: exam.tags || []
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
tutorials.value = await Promise.all(tutorialPromises);
|
tutorials.value = await Promise.all(tutorialPromises);
|
||||||
|
|
||||||
|
console.log('成功加载实验数据:', tutorials.value.length, '个实验');
|
||||||
|
|
||||||
// 启动自动旋转
|
// 启动自动旋转
|
||||||
startAutoRotation();
|
startAutoRotation();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('加载例程失败:', error);
|
console.error('加载实验数据失败:', error);
|
||||||
|
|
||||||
|
// 如果加载失败,显示默认的占位内容
|
||||||
|
tutorials.value = [{
|
||||||
|
id: 'placeholder',
|
||||||
|
title: '实验数据加载中...',
|
||||||
|
description: '请稍后或刷新页面重试',
|
||||||
|
thumbnail: undefined,
|
||||||
|
tags: []
|
||||||
|
}];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 在组件销毁时清除计时器
|
// 在组件销毁时清除计时器和Blob URLs
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
if (autoRotationTimer) {
|
if (autoRotationTimer) {
|
||||||
clearInterval(autoRotationTimer);
|
clearInterval(autoRotationTimer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 清理创建的Blob URLs
|
||||||
|
tutorials.value.forEach(tutorial => {
|
||||||
|
if (tutorial.thumbnail && tutorial.thumbnail.startsWith('blob:')) {
|
||||||
|
URL.revokeObjectURL(tutorial.thumbnail);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// 鼠标滚轮处理
|
// 鼠标滚轮处理
|
||||||
@@ -210,12 +230,12 @@ const resumeAutoRotation = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 前往例程
|
// 前往实验
|
||||||
const goToTutorial = (tutorialId: string) => {
|
const goToExam = (examId: string) => {
|
||||||
// 跳转到工程页面,并通过 query 参数传递文档路径
|
// 跳转到实验列表页面并传递examId参数,页面将自动打开对应的实验详情模态框
|
||||||
router.push({
|
router.push({
|
||||||
path: '/project',
|
path: '/exam',
|
||||||
query: { tutorial: tutorialId }
|
query: { examId: examId }
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +1,77 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="flex flex-col bg-base-100 justify-center items-center">
|
<div class="flex flex-col bg-base-100 justify-center items-center gap-4">
|
||||||
<!-- Title -->
|
<!-- Title -->
|
||||||
<h1 class="font-bold text-2xl">上传比特流文件</h1>
|
<h1 class="font-bold text-2xl">比特流文件</h1>
|
||||||
|
|
||||||
|
<!-- 示例比特流下载区域 (仅在有examId时显示) -->
|
||||||
|
<div v-if="examId && availableBitstreams.length > 0" class="w-full">
|
||||||
|
<fieldset class="fieldset w-full">
|
||||||
|
<legend class="fieldset-legend text-sm">示例比特流文件</legend>
|
||||||
|
<div class="space-y-2">
|
||||||
|
<div
|
||||||
|
v-for="bitstream in availableBitstreams"
|
||||||
|
:key="bitstream.id"
|
||||||
|
class="flex items-center justify-between p-2 border-2 border-base-300 rounded-lg"
|
||||||
|
>
|
||||||
|
<span class="text-sm">{{ bitstream.name }}</span>
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<button
|
||||||
|
@click="downloadExampleBitstream(bitstream)"
|
||||||
|
class="btn btn-sm btn-secondary"
|
||||||
|
:disabled="isDownloading || isProgramming"
|
||||||
|
>
|
||||||
|
<div v-if="isDownloading">
|
||||||
|
<span class="loading loading-spinner loading-xs"></span>
|
||||||
|
{{ downloadProgress }}%
|
||||||
|
</div>
|
||||||
|
<div v-else>下载示例</div>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
@click="programExampleBitstream(bitstream)"
|
||||||
|
class="btn btn-sm btn-primary"
|
||||||
|
:disabled="isDownloading || isProgramming"
|
||||||
|
>
|
||||||
|
<div v-if="isProgramming">
|
||||||
|
<span class="loading loading-spinner loading-xs"></span>
|
||||||
|
烧录中...
|
||||||
|
</div>
|
||||||
|
<div v-else>直接烧录</div>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 分割线 -->
|
||||||
|
<div v-if="examId && availableBitstreams.length > 0" class="divider">
|
||||||
|
或
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Input File -->
|
<!-- Input File -->
|
||||||
<fieldset class="fieldset w-full">
|
<fieldset class="fieldset w-full">
|
||||||
<legend class="fieldset-legend text-sm">选择或拖拽上传文件</legend>
|
<legend class="fieldset-legend text-sm">上传自定义比特流文件</legend>
|
||||||
<input type="file" ref="fileInput" class="file-input w-full" @change="handleFileChange" />
|
<input
|
||||||
|
type="file"
|
||||||
|
ref="fileInput"
|
||||||
|
class="file-input w-full"
|
||||||
|
@change="handleFileChange"
|
||||||
|
/>
|
||||||
<label class="fieldset-label">文件最大容量: {{ maxMemory }}MB</label>
|
<label class="fieldset-label">文件最大容量: {{ maxMemory }}MB</label>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|
||||||
<!-- Upload Button -->
|
<!-- Upload Button -->
|
||||||
<div class="card-actions w-full">
|
<div class="card-actions w-full">
|
||||||
<button @click="handleClick" class="btn btn-primary grow" :disabled="isUploading">
|
<button
|
||||||
|
@click="handleClick"
|
||||||
|
class="btn btn-primary grow"
|
||||||
|
:disabled="isUploading || isProgramming"
|
||||||
|
>
|
||||||
<div v-if="isUploading">
|
<div v-if="isUploading">
|
||||||
<span class="loading loading-spinner"></span>
|
<span class="loading loading-spinner"></span>
|
||||||
下载中...
|
上传中...
|
||||||
</div>
|
|
||||||
<div v-else>
|
|
||||||
{{ buttonText }}
|
|
||||||
</div>
|
</div>
|
||||||
|
<div v-else>上传并下载</div>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -27,28 +79,71 @@
|
|||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { computed, ref, useTemplateRef, onMounted } from "vue";
|
import { computed, ref, useTemplateRef, onMounted } from "vue";
|
||||||
|
import { AuthManager } from "@/utils/AuthManager"; // Adjust the path based on your project structure
|
||||||
import { useDialogStore } from "@/stores/dialog";
|
import { useDialogStore } from "@/stores/dialog";
|
||||||
import { isNull, isUndefined } from "lodash";
|
import { isNull, isUndefined } from "lodash";
|
||||||
|
import { useEquipments } from "@/stores/equipments";
|
||||||
|
import type { HubConnection } from "@microsoft/signalr";
|
||||||
|
import type {
|
||||||
|
IProgressHub,
|
||||||
|
IProgressReceiver,
|
||||||
|
} from "@/TypedSignalR.Client/server.Hubs";
|
||||||
|
import { getHubProxyFactory, getReceiverRegister } from "@/TypedSignalR.Client";
|
||||||
|
import { ProgressStatus } from "@/server.Hubs";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
import { useAlertStore } from "./Alert";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
uploadEvent?: (file: File) => Promise<boolean>;
|
|
||||||
downloadEvent?: () => Promise<boolean>;
|
|
||||||
maxMemory?: number;
|
maxMemory?: number;
|
||||||
|
examId?: string; // 新增examId属性
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = withDefaults(defineProps<Props>(), {
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
maxMemory: 4,
|
maxMemory: 4,
|
||||||
|
examId: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
const emits = defineEmits<{
|
const emits = defineEmits<{
|
||||||
finishedUpload: [file: File];
|
finishedUpload: [file: File];
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
|
const alert = useRequiredInjection(useAlertStore);
|
||||||
const dialog = useDialogStore();
|
const dialog = useDialogStore();
|
||||||
|
const eqps = useEquipments();
|
||||||
|
|
||||||
const isUploading = ref(false);
|
const isUploading = ref(false);
|
||||||
const buttonText = computed(() => {
|
const isDownloading = ref(false);
|
||||||
return isUndefined(props.downloadEvent) ? "上传" : "上传并下载";
|
const isProgramming = ref(false);
|
||||||
|
const availableBitstreams = ref<{ id: number; name: string }[]>([]);
|
||||||
|
|
||||||
|
// Progress
|
||||||
|
const downloadTaskId = ref("");
|
||||||
|
const downloadProgress = ref(0);
|
||||||
|
const progressHubConnection = ref<HubConnection>();
|
||||||
|
const progressHubProxy = ref<IProgressHub>();
|
||||||
|
const progressHubReceiver: IProgressReceiver = {
|
||||||
|
onReceiveProgress: async (msg) => {
|
||||||
|
if (msg.taskId == downloadTaskId.value) {
|
||||||
|
if (msg.status == ProgressStatus.InProgress) {
|
||||||
|
downloadProgress.value = msg.progressPercent;
|
||||||
|
} else if (msg.status == ProgressStatus.Failed) {
|
||||||
|
dialog.error(msg.errorMessage);
|
||||||
|
} else if (msg.status == ProgressStatus.Completed) {
|
||||||
|
alert.info("比特流下载成功");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
onMounted(async () => {
|
||||||
|
progressHubConnection.value =
|
||||||
|
AuthManager.createAuthenticatedProgressHubConnection();
|
||||||
|
progressHubProxy.value = getHubProxyFactory("IProgressHub").createHubProxy(
|
||||||
|
progressHubConnection.value,
|
||||||
|
);
|
||||||
|
getReceiverRegister("IProgressReceiver").register(
|
||||||
|
progressHubConnection.value,
|
||||||
|
progressHubReceiver,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
const fileInput = useTemplateRef("fileInput");
|
const fileInput = useTemplateRef("fileInput");
|
||||||
@@ -56,14 +151,97 @@ const bitstream = defineModel("bitstreamFile", {
|
|||||||
type: File,
|
type: File,
|
||||||
default: undefined,
|
default: undefined,
|
||||||
});
|
});
|
||||||
onMounted(() => {
|
|
||||||
|
// 初始化时加载示例比特流
|
||||||
|
onMounted(async () => {
|
||||||
if (!isUndefined(bitstream.value) && !isNull(fileInput.value)) {
|
if (!isUndefined(bitstream.value) && !isNull(fileInput.value)) {
|
||||||
let fileList = new DataTransfer();
|
let fileList = new DataTransfer();
|
||||||
fileList.items.add(bitstream.value);
|
fileList.items.add(bitstream.value);
|
||||||
fileInput.value.files = fileList.files;
|
fileInput.value.files = fileList.files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await loadAvailableBitstreams();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 加载可用的比特流文件列表
|
||||||
|
async function loadAvailableBitstreams() {
|
||||||
|
console.log("加载可用比特流文件,examId:", props.examId);
|
||||||
|
if (!props.examId) {
|
||||||
|
availableBitstreams.value = [];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||||
|
// 使用新的ResourceClient API获取比特流模板资源列表
|
||||||
|
const resources = await resourceClient.getResourceList(
|
||||||
|
props.examId,
|
||||||
|
"bitstream",
|
||||||
|
"template",
|
||||||
|
);
|
||||||
|
availableBitstreams.value =
|
||||||
|
resources.map((r) => ({ id: r.id, name: r.name })) || [];
|
||||||
|
} catch (error) {
|
||||||
|
console.error("加载比特流列表失败:", error);
|
||||||
|
availableBitstreams.value = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 下载示例比特流
|
||||||
|
async function downloadExampleBitstream(bitstream: {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}) {
|
||||||
|
if (isDownloading.value) return;
|
||||||
|
|
||||||
|
isDownloading.value = true;
|
||||||
|
try {
|
||||||
|
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||||
|
|
||||||
|
// 使用新的ResourceClient API获取资源文件
|
||||||
|
const response = await resourceClient.getResourceById(bitstream.id);
|
||||||
|
|
||||||
|
if (response && response.data) {
|
||||||
|
// 创建下载链接
|
||||||
|
const url = URL.createObjectURL(response.data);
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = url;
|
||||||
|
link.download = response.fileName || bitstream.name;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
|
||||||
|
dialog.info("示例比特流下载成功");
|
||||||
|
} else {
|
||||||
|
dialog.error("下载失败:响应数据为空");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("下载示例比特流失败:", error);
|
||||||
|
dialog.error("下载示例比特流失败");
|
||||||
|
} finally {
|
||||||
|
isDownloading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 直接烧录示例比特流
|
||||||
|
async function programExampleBitstream(bitstream: {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}) {
|
||||||
|
if (isProgramming.value) return;
|
||||||
|
|
||||||
|
isProgramming.value = true;
|
||||||
|
try {
|
||||||
|
const downloadTaskId = await eqps.jtagDownloadBitstream(bitstream.id);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("烧录示例比特流失败:", error);
|
||||||
|
dialog.error("烧录示例比特流失败");
|
||||||
|
} finally {
|
||||||
|
isProgramming.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function handleFileChange(event: Event): void {
|
function handleFileChange(event: Event): void {
|
||||||
const target = event.target as HTMLInputElement;
|
const target = event.target as HTMLInputElement;
|
||||||
const file = target.files?.[0]; // 获取选中的第一个文件
|
const file = target.files?.[0]; // 获取选中的第一个文件
|
||||||
@@ -85,48 +263,49 @@ function checkFile(file: File): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function handleClick(event: Event): Promise<void> {
|
async function handleClick(event: Event): Promise<void> {
|
||||||
|
console.log("上传按钮被点击");
|
||||||
if (isNull(bitstream.value) || isUndefined(bitstream.value)) {
|
if (isNull(bitstream.value) || isUndefined(bitstream.value)) {
|
||||||
dialog.error(`未选择文件`);
|
dialog.error(`未选择文件`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!checkFile(bitstream.value)) return;
|
if (!checkFile(bitstream.value)) return;
|
||||||
if (isUndefined(props.uploadEvent)) {
|
|
||||||
dialog.error("无法上传");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
isUploading.value = true;
|
isUploading.value = true;
|
||||||
|
let uploadedBitstreamId: number | null = null;
|
||||||
try {
|
try {
|
||||||
const ret = await props.uploadEvent(bitstream.value);
|
console.log("开始上传比特流文件:", bitstream.value.name);
|
||||||
if (isUndefined(props.downloadEvent)) {
|
const bitstreamId = await eqps.jtagUploadBitstream(
|
||||||
if (ret) {
|
bitstream.value,
|
||||||
dialog.info("上传成功");
|
props.examId || "",
|
||||||
emits("finishedUpload", bitstream.value);
|
);
|
||||||
} else dialog.error("上传失败");
|
console.log("上传结果,ID:", bitstreamId);
|
||||||
return;
|
if (bitstreamId === null || bitstreamId === undefined) {
|
||||||
}
|
|
||||||
if (!ret) {
|
|
||||||
isUploading.value = false;
|
isUploading.value = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
uploadedBitstreamId = bitstreamId;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
dialog.error("上传失败");
|
dialog.error("上传失败");
|
||||||
console.error(e);
|
console.error(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
isUploading.value = false;
|
||||||
|
|
||||||
// Download
|
// Download
|
||||||
try {
|
try {
|
||||||
const ret = await props.downloadEvent();
|
console.log("开始下载比特流,ID:", uploadedBitstreamId);
|
||||||
if (ret) dialog.info("下载成功");
|
if (uploadedBitstreamId === null || uploadedBitstreamId === undefined) {
|
||||||
else dialog.error("下载失败");
|
dialog.error("uploadedBitstreamId is null or undefined");
|
||||||
|
} else {
|
||||||
|
isDownloading.value = true;
|
||||||
|
downloadTaskId.value =
|
||||||
|
await eqps.jtagDownloadBitstream(uploadedBitstreamId);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
dialog.error("下载失败");
|
dialog.error("下载失败");
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
isUploading.value = false;
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
373
src/components/WaveformDisplay/WaveformDisplay.vue
Normal file
373
src/components/WaveformDisplay/WaveformDisplay.vue
Normal file
@@ -0,0 +1,373 @@
|
|||||||
|
<template>
|
||||||
|
<div
|
||||||
|
class="w-full"
|
||||||
|
:class="{
|
||||||
|
'h-48': !props.data,
|
||||||
|
'h-150': props.data,
|
||||||
|
}"
|
||||||
|
>
|
||||||
|
<v-chart
|
||||||
|
v-if="props.data"
|
||||||
|
class="w-full h-full"
|
||||||
|
:option="option"
|
||||||
|
autoresize
|
||||||
|
:update-options="updateOptions"
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
v-else
|
||||||
|
class="w-full h-full flex flex-col gap-6 items-center justify-center"
|
||||||
|
>
|
||||||
|
<template v-if="hasSlot">
|
||||||
|
<slot />
|
||||||
|
</template>
|
||||||
|
<template v-else>
|
||||||
|
<div class="text-center">
|
||||||
|
<h3 class="text-xl font-semibold text-slate-600 mb-2">暂无数据</h3>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, shallowRef, useSlots } from "vue";
|
||||||
|
import VChart from "vue-echarts";
|
||||||
|
|
||||||
|
// Echarts
|
||||||
|
import { use } from "echarts/core";
|
||||||
|
import { LineChart } from "echarts/charts";
|
||||||
|
import {
|
||||||
|
TooltipComponent,
|
||||||
|
GridComponent,
|
||||||
|
DataZoomComponent,
|
||||||
|
AxisPointerComponent,
|
||||||
|
ToolboxComponent,
|
||||||
|
MarkLineComponent,
|
||||||
|
} from "echarts/components";
|
||||||
|
import { CanvasRenderer } from "echarts/renderers";
|
||||||
|
import type { ComposeOption } from "echarts/core";
|
||||||
|
import type { LineSeriesOption } from "echarts/charts";
|
||||||
|
import type {
|
||||||
|
AxisPointerComponentOption,
|
||||||
|
TooltipComponentOption,
|
||||||
|
GridComponentOption,
|
||||||
|
DataZoomComponentOption,
|
||||||
|
MarkLineComponentOption,
|
||||||
|
} from "echarts/components";
|
||||||
|
import type {
|
||||||
|
ToolboxComponentOption,
|
||||||
|
XAXisOption,
|
||||||
|
YAXisOption,
|
||||||
|
} from "echarts/types/dist/shared";
|
||||||
|
import { isUndefined } from "lodash";
|
||||||
|
import type { LogicDataType } from ".";
|
||||||
|
|
||||||
|
use([
|
||||||
|
TooltipComponent,
|
||||||
|
ToolboxComponent,
|
||||||
|
GridComponent,
|
||||||
|
AxisPointerComponent,
|
||||||
|
DataZoomComponent,
|
||||||
|
LineChart,
|
||||||
|
CanvasRenderer,
|
||||||
|
MarkLineComponent,
|
||||||
|
]);
|
||||||
|
|
||||||
|
type EChartsOption = ComposeOption<
|
||||||
|
| AxisPointerComponentOption
|
||||||
|
| TooltipComponentOption
|
||||||
|
| ToolboxComponentOption
|
||||||
|
| GridComponentOption
|
||||||
|
| DataZoomComponentOption
|
||||||
|
| LineSeriesOption
|
||||||
|
| MarkLineComponentOption
|
||||||
|
>;
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
data?: LogicDataType;
|
||||||
|
}>();
|
||||||
|
|
||||||
|
const slots = useSlots();
|
||||||
|
const hasSlot = computed(() => !!slots.default && slots.default().length > 0);
|
||||||
|
|
||||||
|
// 添加更新选项来减少重绘
|
||||||
|
const updateOptions = shallowRef({
|
||||||
|
notMerge: false,
|
||||||
|
lazyUpdate: true,
|
||||||
|
silent: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const option = computed((): EChartsOption => {
|
||||||
|
if (isUndefined(props.data)) return {};
|
||||||
|
|
||||||
|
// 只获取启用的通道,使用y数据结构
|
||||||
|
const enabledChannels = props.data.y.filter(
|
||||||
|
(channel) => channel.enabled,
|
||||||
|
);
|
||||||
|
const enabledChannelIndices = props.data.y
|
||||||
|
.map((channel, index) => (channel.enabled ? index : -1))
|
||||||
|
.filter((index) => index !== -1);
|
||||||
|
|
||||||
|
// 统计启用通道数量
|
||||||
|
const channelCount = enabledChannels.length;
|
||||||
|
const channelSpacing = 2; // 每个通道之间的间距
|
||||||
|
|
||||||
|
if (channelCount === 0) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单个网格
|
||||||
|
const grids: GridComponentOption[] = [
|
||||||
|
{
|
||||||
|
left: "5%",
|
||||||
|
right: "5%",
|
||||||
|
top: "5%",
|
||||||
|
bottom: "15%",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// 单个X轴
|
||||||
|
const xAxis: XAXisOption[] = [
|
||||||
|
{
|
||||||
|
type: "category",
|
||||||
|
boundaryGap: true,
|
||||||
|
data: props.data.x.map((x) => x.toFixed(3)),
|
||||||
|
axisLabel: {
|
||||||
|
formatter: (value: string) =>
|
||||||
|
props.data
|
||||||
|
? `${value}${props.data.xUnit}`
|
||||||
|
: `${value}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// 单个Y轴,范围根据启用通道数量调整
|
||||||
|
const yAxis: YAXisOption[] = [
|
||||||
|
{
|
||||||
|
type: "value",
|
||||||
|
min: -0.5,
|
||||||
|
max: channelCount * channelSpacing - 0.5,
|
||||||
|
interval: channelSpacing,
|
||||||
|
axisLabel: {
|
||||||
|
formatter: (value: number) => {
|
||||||
|
const channelIndex = Math.round(value / channelSpacing);
|
||||||
|
return channelIndex < channelCount
|
||||||
|
? enabledChannels[channelIndex].name
|
||||||
|
: "";
|
||||||
|
},
|
||||||
|
},
|
||||||
|
splitLine: { show: false },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// 创建系列数据
|
||||||
|
const series: LineSeriesOption[] = [];
|
||||||
|
enabledChannelIndices.forEach(
|
||||||
|
(originalIndex: number, displayIndex: number) => {
|
||||||
|
const channel = props.data!.y[originalIndex];
|
||||||
|
if (channel.type === "logic") {
|
||||||
|
// logic类型,原样显示
|
||||||
|
series.push({
|
||||||
|
name: channel.name,
|
||||||
|
type: "line",
|
||||||
|
data: channel.value.map(
|
||||||
|
(value: number) => value + displayIndex * channelSpacing + 0.2,
|
||||||
|
),
|
||||||
|
step: "end",
|
||||||
|
lineStyle: {
|
||||||
|
width: 2,
|
||||||
|
color: channel.color,
|
||||||
|
},
|
||||||
|
areaStyle: {
|
||||||
|
opacity: 0.3,
|
||||||
|
origin: displayIndex * channelSpacing,
|
||||||
|
color: channel.color,
|
||||||
|
},
|
||||||
|
symbol: "none",
|
||||||
|
sampling: "lttb",
|
||||||
|
animation: false,
|
||||||
|
});
|
||||||
|
} else if (channel.type === "number") {
|
||||||
|
const values = channel.value;
|
||||||
|
const xArr = props.data!.x;
|
||||||
|
// 构造带过渡的点序列
|
||||||
|
function buildVcdLine(valArr: number[], high: number, low: number) {
|
||||||
|
const points: { x: number; y: number }[] = [];
|
||||||
|
let lastValue = high;
|
||||||
|
points.push({ x: xArr[0], y: lastValue });
|
||||||
|
for (let i = 1; i < valArr.length; i++) {
|
||||||
|
const v =
|
||||||
|
valArr[i] !== valArr[i - 1]
|
||||||
|
? lastValue === high
|
||||||
|
? low
|
||||||
|
: high
|
||||||
|
: lastValue;
|
||||||
|
points.push({ x: xArr[i], y: v });
|
||||||
|
lastValue = v;
|
||||||
|
}
|
||||||
|
return points.map((p) => p.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算变化点区间
|
||||||
|
function buildMarkLines(valArr: number[], yBase: number) {
|
||||||
|
const markLines: any[] = [];
|
||||||
|
let lastValue = valArr[0];
|
||||||
|
let lastIdx = 0;
|
||||||
|
// 格式化函数
|
||||||
|
function formatValue(val: number) {
|
||||||
|
if (channel.base === "hex")
|
||||||
|
return "0x" + val.toString(16).toUpperCase();
|
||||||
|
if (channel.base === "bin") return "0b" + val.toString(2);
|
||||||
|
return val.toString();
|
||||||
|
}
|
||||||
|
for (let i = 1; i <= valArr.length; i++) {
|
||||||
|
if (i === valArr.length || valArr[i] !== lastValue) {
|
||||||
|
markLines.push([
|
||||||
|
{
|
||||||
|
xAxis: lastIdx,
|
||||||
|
yAxis: yBase,
|
||||||
|
label: {
|
||||||
|
formatter: formatValue(lastValue),
|
||||||
|
position: "insideMiddle",
|
||||||
|
color: channel.color,
|
||||||
|
fontSize: 18,
|
||||||
|
opacity: 1,
|
||||||
|
},
|
||||||
|
lineStyle: {
|
||||||
|
opacity: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
xAxis: i - 1,
|
||||||
|
yAxis: yBase,
|
||||||
|
lineStyle: {
|
||||||
|
opacity: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
lastValue = valArr[i];
|
||||||
|
lastIdx = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return markLines;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1线条
|
||||||
|
series.push({
|
||||||
|
name: channel.name + "_1",
|
||||||
|
type: "line",
|
||||||
|
data: buildVcdLine(
|
||||||
|
values,
|
||||||
|
displayIndex * channelSpacing + 1,
|
||||||
|
displayIndex * channelSpacing,
|
||||||
|
),
|
||||||
|
step: false,
|
||||||
|
lineStyle: {
|
||||||
|
width: 2,
|
||||||
|
color: channel.color,
|
||||||
|
},
|
||||||
|
areaStyle: {
|
||||||
|
opacity: 0.3,
|
||||||
|
origin: displayIndex * channelSpacing + 0.5,
|
||||||
|
color: channel.color,
|
||||||
|
},
|
||||||
|
symbol: "none",
|
||||||
|
sampling: "lttb",
|
||||||
|
animation: false,
|
||||||
|
// 添加markLine
|
||||||
|
markLine: {
|
||||||
|
data: buildMarkLines(values, displayIndex * channelSpacing + 0.5),
|
||||||
|
emphasis: {
|
||||||
|
disabled: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// 0线条
|
||||||
|
series.push({
|
||||||
|
name: channel.name + "_0",
|
||||||
|
type: "line",
|
||||||
|
data: buildVcdLine(
|
||||||
|
values,
|
||||||
|
displayIndex * channelSpacing,
|
||||||
|
displayIndex * channelSpacing + 1,
|
||||||
|
),
|
||||||
|
step: false,
|
||||||
|
lineStyle: {
|
||||||
|
width: 2,
|
||||||
|
color: channel.color,
|
||||||
|
},
|
||||||
|
areaStyle: {
|
||||||
|
opacity: 0.3,
|
||||||
|
origin: displayIndex * channelSpacing + 0.5,
|
||||||
|
color: channel.color,
|
||||||
|
},
|
||||||
|
symbol: "none",
|
||||||
|
sampling: "lttb",
|
||||||
|
animation: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
animation: false,
|
||||||
|
tooltip: {
|
||||||
|
trigger: "axis",
|
||||||
|
axisPointer: {
|
||||||
|
type: "line",
|
||||||
|
label: {
|
||||||
|
backgroundColor: "#6a7985",
|
||||||
|
},
|
||||||
|
animation: false,
|
||||||
|
},
|
||||||
|
formatter: (params: any) => {
|
||||||
|
if (Array.isArray(params) && params.length > 0) {
|
||||||
|
const timeValue = props.data!.x[params[0].dataIndex];
|
||||||
|
const dataIndex = params[0].dataIndex;
|
||||||
|
let tooltip = `Time: ${timeValue.toFixed(3)}${props.data!.xUnit}<br/>`;
|
||||||
|
enabledChannelIndices.forEach(
|
||||||
|
(originalIndex: number, displayIndex: number) => {
|
||||||
|
const channel = props.data!.y[originalIndex];
|
||||||
|
if (channel.type === "logic") {
|
||||||
|
const channelName = channel.name;
|
||||||
|
const originalValue = channel.value[dataIndex];
|
||||||
|
tooltip += `${channelName}: ${originalValue}<br/>`;
|
||||||
|
} else if (channel.type === "number") {
|
||||||
|
const channelName = channel.name;
|
||||||
|
const originalValue = channel.value[dataIndex];
|
||||||
|
tooltip += `${channelName}: ${originalValue}<br/>`;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return tooltip;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
},
|
||||||
|
hideDelay: 100,
|
||||||
|
},
|
||||||
|
toolbox: {
|
||||||
|
feature: {
|
||||||
|
restore: {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
grid: grids,
|
||||||
|
xAxis: xAxis,
|
||||||
|
yAxis: yAxis,
|
||||||
|
dataZoom: [
|
||||||
|
{
|
||||||
|
show: true,
|
||||||
|
realtime: true,
|
||||||
|
start: 0,
|
||||||
|
end: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "inside",
|
||||||
|
realtime: true,
|
||||||
|
start: 0,
|
||||||
|
end: 100,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
series: series,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
</script>
|
||||||
61
src/components/WaveformDisplay/index.ts
Normal file
61
src/components/WaveformDisplay/index.ts
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import WaveformDisplay from "./WaveformDisplay.vue";
|
||||||
|
|
||||||
|
export type LogicDataType = {
|
||||||
|
x: number[];
|
||||||
|
y: {
|
||||||
|
enabled: boolean;
|
||||||
|
type: "logic" | "number";
|
||||||
|
name: string;
|
||||||
|
color: string;
|
||||||
|
value: number[];
|
||||||
|
base: "bin" | "dec" | "hex";
|
||||||
|
}[];
|
||||||
|
xUnit: "s" | "ms" | "us" | "ns";
|
||||||
|
};
|
||||||
|
|
||||||
|
// 生成4路测试数据的函数
|
||||||
|
export function generateTestData(): LogicDataType {
|
||||||
|
// 生成时间轴数据 (0-100ns,每1ns一个采样点)
|
||||||
|
const timePoints = Array.from({ length: 101 }, (_, i) => i);
|
||||||
|
|
||||||
|
return {
|
||||||
|
x: timePoints,
|
||||||
|
y: [
|
||||||
|
{
|
||||||
|
enabled: true,
|
||||||
|
type: "logic",
|
||||||
|
name: "CLK",
|
||||||
|
color: "#ff0000",
|
||||||
|
value: timePoints.map((t) => t % 2), // 时钟信号,每1ns翻转
|
||||||
|
base: "bin",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
enabled: true,
|
||||||
|
type: "logic",
|
||||||
|
name: "RESET",
|
||||||
|
color: "#00ff00",
|
||||||
|
value: timePoints.map((t) => (t < 10 ? 1 : 0)), // 复位信号,前10ns为高电平
|
||||||
|
base: "bin",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
enabled: true,
|
||||||
|
type: "number",
|
||||||
|
name: "DATA",
|
||||||
|
color: "#0000ff",
|
||||||
|
value: timePoints.map((t) => Math.floor(t / 4) % 16), // 计数器,每4ns递增
|
||||||
|
base: "hex",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
enabled: true,
|
||||||
|
type: "logic",
|
||||||
|
name: "ENABLE",
|
||||||
|
color: "#ff8800",
|
||||||
|
value: timePoints.map((t) => (t >= 20 && t < 80 ? 1 : 0)), // 使能信号,20-80ns为高电平
|
||||||
|
base: "bin",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
xUnit: "ns",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export { WaveformDisplay };
|
||||||
@@ -7,7 +7,12 @@
|
|||||||
<div class="waveform-display">
|
<div class="waveform-display">
|
||||||
<svg width="100%" height="120" viewBox="0 0 300 120">
|
<svg width="100%" height="120" viewBox="0 0 300 120">
|
||||||
<rect width="300" height="120" fill="#1a1f25" />
|
<rect width="300" height="120" fill="#1a1f25" />
|
||||||
<path :d="currentWaveformPath" stroke="lime" stroke-width="2" fill="none" />
|
<path
|
||||||
|
:d="currentWaveformPath"
|
||||||
|
stroke="lime"
|
||||||
|
stroke-width="2"
|
||||||
|
fill="none"
|
||||||
|
/>
|
||||||
|
|
||||||
<!-- 频率和相位显示 -->
|
<!-- 频率和相位显示 -->
|
||||||
<text x="20" y="25" fill="#0f0" font-size="14">
|
<text x="20" y="25" fill="#0f0" font-size="14">
|
||||||
@@ -16,7 +21,13 @@
|
|||||||
<text x="200" y="25" fill="#0f0" font-size="14">
|
<text x="200" y="25" fill="#0f0" font-size="14">
|
||||||
φ: {{ phase }}°
|
φ: {{ phase }}°
|
||||||
</text>
|
</text>
|
||||||
<text x="150" y="110" fill="#0f0" font-size="14" text-anchor="middle">
|
<text
|
||||||
|
x="150"
|
||||||
|
y="110"
|
||||||
|
fill="#0f0"
|
||||||
|
font-size="14"
|
||||||
|
text-anchor="middle"
|
||||||
|
>
|
||||||
{{ displayTimebase }}
|
{{ displayTimebase }}
|
||||||
</text>
|
</text>
|
||||||
</svg>
|
</svg>
|
||||||
@@ -35,10 +46,15 @@
|
|||||||
|
|
||||||
<!-- 波形选择区 -->
|
<!-- 波形选择区 -->
|
||||||
<div class="waveform-selector">
|
<div class="waveform-selector">
|
||||||
<div v-for="(name, index) in waveformNames" :key="`wave-${index}`" :class="[
|
<div
|
||||||
|
v-for="(name, index) in waveformNames"
|
||||||
|
:key="`wave-${index}`"
|
||||||
|
:class="[
|
||||||
'waveform-option',
|
'waveform-option',
|
||||||
{ active: currentWaveformIndex === index },
|
{ active: currentWaveformIndex === index },
|
||||||
]" @click="selectWaveform(index)">
|
]"
|
||||||
|
@click="selectWaveform(index)"
|
||||||
|
>
|
||||||
{{ name }}
|
{{ name }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -51,8 +67,13 @@
|
|||||||
<button class="control-button" @click="decreaseFrequency">
|
<button class="control-button" @click="decreaseFrequency">
|
||||||
-
|
-
|
||||||
</button>
|
</button>
|
||||||
<input v-model="frequencyInput" @blur="applyFrequencyInput" @keyup.enter="applyFrequencyInput"
|
<input
|
||||||
class="control-input" type="text" />
|
v-model="frequencyInput"
|
||||||
|
@blur="applyFrequencyInput"
|
||||||
|
@keyup.enter="applyFrequencyInput"
|
||||||
|
class="control-input"
|
||||||
|
type="text"
|
||||||
|
/>
|
||||||
<button class="control-button" @click="increaseFrequency">
|
<button class="control-button" @click="increaseFrequency">
|
||||||
+
|
+
|
||||||
</button>
|
</button>
|
||||||
@@ -63,8 +84,13 @@
|
|||||||
<span class="control-label">相位:</span>
|
<span class="control-label">相位:</span>
|
||||||
<div class="control-buttons">
|
<div class="control-buttons">
|
||||||
<button class="control-button" @click="decreasePhase">-</button>
|
<button class="control-button" @click="decreasePhase">-</button>
|
||||||
<input v-model="phaseInput" @blur="applyPhaseInput" @keyup.enter="applyPhaseInput" class="control-input"
|
<input
|
||||||
type="text" />
|
v-model="phaseInput"
|
||||||
|
@blur="applyPhaseInput"
|
||||||
|
@keyup.enter="applyPhaseInput"
|
||||||
|
class="control-input"
|
||||||
|
type="text"
|
||||||
|
/>
|
||||||
<button class="control-button" @click="increasePhase">+</button>
|
<button class="control-button" @click="increasePhase">+</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -75,9 +101,12 @@
|
|||||||
<div class="section-heading">自定义波形</div>
|
<div class="section-heading">自定义波形</div>
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<label class="input-label">函数表达式:</label>
|
<label class="input-label">函数表达式:</label>
|
||||||
<input v-model="customWaveformExpression" class="function-input"
|
<input
|
||||||
|
v-model="customWaveformExpression"
|
||||||
|
class="function-input"
|
||||||
placeholder="例如: sin(t) 或 x^(2/3)+0.9*sqrt(3.3-x^2)*sin(a*PI*x) [a=7.8]"
|
placeholder="例如: sin(t) 或 x^(2/3)+0.9*sqrt(3.3-x^2)*sin(a*PI*x) [a=7.8]"
|
||||||
@keyup.enter="applyCustomWaveform" />
|
@keyup.enter="applyCustomWaveform"
|
||||||
|
/>
|
||||||
<button class="apply-button" @click="applyCustomWaveform">
|
<button class="apply-button" @click="applyCustomWaveform">
|
||||||
应用
|
应用
|
||||||
</button>
|
</button>
|
||||||
@@ -86,17 +115,26 @@
|
|||||||
<div class="example-functions">
|
<div class="example-functions">
|
||||||
<div class="example-label">示例函数:</div>
|
<div class="example-label">示例函数:</div>
|
||||||
<div class="example-buttons">
|
<div class="example-buttons">
|
||||||
<button class="example-button" @click="applyExampleFunction('sin(t)')">
|
<button
|
||||||
|
class="example-button"
|
||||||
|
@click="applyExampleFunction('sin(t)')"
|
||||||
|
>
|
||||||
正弦波
|
正弦波
|
||||||
</button>
|
</button>
|
||||||
<button class="example-button" @click="applyExampleFunction('sin(t)^3')">
|
<button
|
||||||
|
class="example-button"
|
||||||
|
@click="applyExampleFunction('sin(t)^3')"
|
||||||
|
>
|
||||||
立方正弦
|
立方正弦
|
||||||
</button>
|
</button>
|
||||||
<button class="example-button" @click="
|
<button
|
||||||
|
class="example-button"
|
||||||
|
@click="
|
||||||
applyExampleFunction(
|
applyExampleFunction(
|
||||||
'((x)^(2/3)+0.9*sqrt(3.3-(x)^2)*sin(10*PI*(x)))*0.75',
|
'((x)^(2/3)+0.9*sqrt(3.3-(x)^2)*sin(10*PI*(x)))*0.75',
|
||||||
)
|
)
|
||||||
">
|
"
|
||||||
|
>
|
||||||
心形函数
|
心形函数
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -105,8 +143,16 @@
|
|||||||
<div class="drawing-area">
|
<div class="drawing-area">
|
||||||
<div class="section-heading">波形绘制</div>
|
<div class="section-heading">波形绘制</div>
|
||||||
<div class="waveform-canvas-container" ref="canvasContainer">
|
<div class="waveform-canvas-container" ref="canvasContainer">
|
||||||
<canvas ref="drawingCanvas" class="drawing-canvas" width="280" height="100" @mousedown="startDrawing"
|
<canvas
|
||||||
@mousemove="draw" @mouseup="stopDrawing" @mouseleave="stopDrawing"></canvas>
|
ref="drawingCanvas"
|
||||||
|
class="drawing-canvas"
|
||||||
|
width="280"
|
||||||
|
height="100"
|
||||||
|
@mousedown="startDrawing"
|
||||||
|
@mousemove="draw"
|
||||||
|
@mouseup="stopDrawing"
|
||||||
|
@mouseleave="stopDrawing"
|
||||||
|
></canvas>
|
||||||
<div class="canvas-actions">
|
<div class="canvas-actions">
|
||||||
<button class="canvas-button" @click="clearCanvas">
|
<button class="canvas-button" @click="clearCanvas">
|
||||||
清除
|
清除
|
||||||
@@ -123,19 +169,30 @@
|
|||||||
<div class="saved-waveforms">
|
<div class="saved-waveforms">
|
||||||
<div class="section-heading">波形存储槽</div>
|
<div class="section-heading">波形存储槽</div>
|
||||||
<div class="slot-container">
|
<div class="slot-container">
|
||||||
<div v-for="(slot, index) in waveformSlots" :key="`slot-${index}`"
|
<div
|
||||||
:class="['waveform-slot', { empty: !slot.name }]" @click="loadWaveformSlot(index)">
|
v-for="(slot, index) in waveformSlots"
|
||||||
|
:key="`slot-${index}`"
|
||||||
|
:class="['waveform-slot', { empty: !slot.name }]"
|
||||||
|
@click="loadWaveformSlot(index)"
|
||||||
|
>
|
||||||
<span class="slot-name">{{
|
<span class="slot-name">{{
|
||||||
slot.name || `槽 ${index + 1}`
|
slot.name || `槽 ${index + 1}`
|
||||||
}}</span>
|
}}</span>
|
||||||
<button class="save-button" @click.stop="saveCurrentToSlot(index)">
|
<button
|
||||||
|
class="save-button"
|
||||||
|
@click.stop="saveCurrentToSlot(index)"
|
||||||
|
>
|
||||||
保存
|
保存
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button class="btn btn-primary text-primary-content w-full" :disabled="isApplying" @click="applyOutputWave">
|
<button
|
||||||
|
class="btn btn-primary text-primary-content w-full"
|
||||||
|
:disabled="isApplying"
|
||||||
|
@click="applyOutputWave"
|
||||||
|
>
|
||||||
<div v-if="isApplying">
|
<div v-if="isApplying">
|
||||||
<span class="loading loading-spinner"></span>
|
<span class="loading loading-spinner"></span>
|
||||||
应用中...
|
应用中...
|
||||||
@@ -151,10 +208,10 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed, watch, onMounted } from "vue";
|
import { ref, computed, watch, onMounted } from "vue";
|
||||||
import CollapsibleSection from "../CollapsibleSection.vue";
|
import CollapsibleSection from "../CollapsibleSection.vue";
|
||||||
import { DDSClient } from "@/APIClient";
|
|
||||||
import { useEquipments } from "@/stores/equipments";
|
import { useEquipments } from "@/stores/equipments";
|
||||||
import { useDialogStore } from "@/stores/dialog";
|
import { useDialogStore } from "@/stores/dialog";
|
||||||
import { toInteger } from "lodash";
|
import { toInteger } from "lodash";
|
||||||
|
import { AuthManager } from "@/utils/AuthManager";
|
||||||
|
|
||||||
// Component Attributes
|
// Component Attributes
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
@@ -164,7 +221,7 @@ const props = defineProps<{
|
|||||||
const emit = defineEmits(["update:modelValue"]);
|
const emit = defineEmits(["update:modelValue"]);
|
||||||
|
|
||||||
// Global varibles
|
// Global varibles
|
||||||
const dds = new DDSClient();
|
const dds = AuthManager.createAuthenticatedDDSClient();
|
||||||
const eqps = useEquipments();
|
const eqps = useEquipments();
|
||||||
const dialog = useDialogStore();
|
const dialog = useDialogStore();
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,47 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="motherboard-container" v-bind="$attrs" :style="{
|
<div
|
||||||
|
class="motherboard-container"
|
||||||
|
v-bind="$attrs"
|
||||||
|
:style="{
|
||||||
width: width + 'px',
|
width: width + 'px',
|
||||||
height: height + 'px',
|
height: height + 'px',
|
||||||
position: 'relative',
|
position: 'relative',
|
||||||
}">
|
}"
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" :width="width" :height="height" :viewBox="`0 0 800 600`"
|
>
|
||||||
class="motherboard-svg">
|
<svg
|
||||||
<image href="../equipments/svg/motherboard.svg" width="100%" height="100%" preserveAspectRatio="xMidYMid meet" />
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
:width="width"
|
||||||
|
:height="height"
|
||||||
|
:viewBox="`0 0 800 600`"
|
||||||
|
class="motherboard-svg"
|
||||||
|
>
|
||||||
|
<image
|
||||||
|
href="../equipments/svg/motherboard.svg"
|
||||||
|
width="100%"
|
||||||
|
height="100%"
|
||||||
|
preserveAspectRatio="xMidYMid meet"
|
||||||
|
/>
|
||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
<Teleport to="#ComponentCapabilities" v-if="selectecComponentID === props.componentId">
|
<Teleport
|
||||||
<MotherBoardCaps :jtagAddr="props.boardAddr" :jtagPort="toNumber(props.boardPort)" :jtagFreq="jtagFreq"
|
to="#ComponentCapabilities"
|
||||||
@change-jtag-freq="changeJtagFreq" />
|
v-if="selectecComponentID === props.componentId"
|
||||||
|
>
|
||||||
|
<MotherBoardCaps :jtagFreq="jtagFreq" :exam-id="examId" @change-jtag-freq="changeJtagFreq" />
|
||||||
</Teleport>
|
</Teleport>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="tsx">
|
<script setup lang="tsx">
|
||||||
import MotherBoardCaps from "./MotherBoardCaps.vue";
|
import MotherBoardCaps from "./MotherBoardCaps.vue";
|
||||||
import { ref, computed, watchEffect, inject } from "vue";
|
import { ref, computed, inject } from "vue";
|
||||||
import { CanvasCurrentSelectedComponentID } from "../InjectKeys";
|
import { CanvasCurrentSelectedComponentID } from "../InjectKeys";
|
||||||
import { toNumber } from "lodash";
|
import { toNumber } from "lodash";
|
||||||
|
|
||||||
// 主板特有属性
|
// 主板特有属性
|
||||||
export interface MotherBoardProps {
|
export interface MotherBoardProps {
|
||||||
size: number;
|
size: number;
|
||||||
boardAddr?: string;
|
|
||||||
boardPort?: string;
|
|
||||||
componentId?: string;
|
componentId?: string;
|
||||||
|
examId?: string; // 新增examId属性
|
||||||
}
|
}
|
||||||
|
|
||||||
const emit = defineEmits<{
|
const emit = defineEmits<{
|
||||||
@@ -61,8 +76,6 @@ defineExpose({
|
|||||||
export function getDefaultProps(): MotherBoardProps {
|
export function getDefaultProps(): MotherBoardProps {
|
||||||
return {
|
return {
|
||||||
size: 1,
|
size: 1,
|
||||||
boardAddr: "127.0.0.1",
|
|
||||||
boardPort: "1234",
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -8,29 +8,22 @@
|
|||||||
<p>
|
<p>
|
||||||
IDCode: 0x{{ jtagIDCode.toString(16).padStart(8, "0").toUpperCase() }}
|
IDCode: 0x{{ jtagIDCode.toString(16).padStart(8, "0").toUpperCase() }}
|
||||||
</p>
|
</p>
|
||||||
<button class="btn btn-circle w-6 h-6" :onclick="getIDCode">
|
<button
|
||||||
<svg
|
class="btn btn-circle w-6 h-6"
|
||||||
class="icon opacity-70 fill-primary"
|
:disabled="isGettingIDCode"
|
||||||
viewBox="0 0 1024 1024"
|
:onclick="getIDCode"
|
||||||
version="1.1"
|
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
|
||||||
p-id="4865"
|
|
||||||
width="200"
|
|
||||||
height="200"
|
|
||||||
>
|
>
|
||||||
<path
|
<RefreshCcwIcon
|
||||||
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"
|
class="icon"
|
||||||
p-id="4866"
|
:class="{ 'animate-spin': isGettingIDCode }"
|
||||||
></path>
|
/>
|
||||||
</svg>
|
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="divider"></div>
|
<div class="divider"></div>
|
||||||
<UploadCard
|
<UploadCard
|
||||||
class="bg-base-200"
|
:exam-id="props.examId"
|
||||||
:upload-event="eqps.jtagUploadBitstream"
|
:upload-event="eqps.jtagUploadBitstream"
|
||||||
:download-event="eqps.jtagDownloadBitstream"
|
|
||||||
:bitstream-file="eqps.jtagBitstream"
|
:bitstream-file="eqps.jtagBitstream"
|
||||||
@update:bitstream-file="handleBitstreamChange"
|
@update:bitstream-file="handleBitstreamChange"
|
||||||
>
|
>
|
||||||
@@ -73,7 +66,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="divider"></div>
|
<div class="divider"></div>
|
||||||
<h1 class="font-bold text-center text-2xl">外设</h1>
|
<h1 class="font-bold text-center text-2xl">外设</h1>
|
||||||
<div class="flex flex-row justify-around">
|
<div class="flex flex-row justify-center">
|
||||||
<div class="flex flex-row">
|
<div class="flex flex-row">
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
@@ -83,15 +76,6 @@
|
|||||||
/>
|
/>
|
||||||
<p class="mx-2">启用矩阵键盘</p>
|
<p class="mx-2">启用矩阵键盘</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex flex-row">
|
|
||||||
<input
|
|
||||||
type="checkbox"
|
|
||||||
class="checkbox"
|
|
||||||
:checked="eqps.enablePower"
|
|
||||||
@change="handlePowerCheckboxChange"
|
|
||||||
/>
|
|
||||||
<p class="mx-2">启用电源</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -102,11 +86,11 @@ import UploadCard from "@/components/UploadCard.vue";
|
|||||||
import { useDialogStore } from "@/stores/dialog";
|
import { useDialogStore } from "@/stores/dialog";
|
||||||
import { useEquipments } from "@/stores/equipments";
|
import { useEquipments } from "@/stores/equipments";
|
||||||
import { computed, ref, watchEffect } from "vue";
|
import { computed, ref, watchEffect } from "vue";
|
||||||
|
import { RefreshCcwIcon } from "lucide-vue-next";
|
||||||
|
|
||||||
interface CapsProps {
|
interface CapsProps {
|
||||||
jtagAddr?: string;
|
|
||||||
jtagPort?: number;
|
|
||||||
jtagFreq?: string;
|
jtagFreq?: string;
|
||||||
|
examId?: string; // 新增examId属性
|
||||||
}
|
}
|
||||||
|
|
||||||
const emits = defineEmits<{
|
const emits = defineEmits<{
|
||||||
@@ -162,28 +146,16 @@ async function handleMatrixkeyCheckboxChange(event: Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handlePowerCheckboxChange(event: Event) {
|
|
||||||
const target = event.target as HTMLInputElement;
|
|
||||||
const ret = await eqps.powerSetOnOff(target.checked);
|
|
||||||
if (target.checked) {
|
|
||||||
eqps.enablePower = ret;
|
|
||||||
} else {
|
|
||||||
eqps.enablePower = !ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function toggleJtagBoundaryScan() {
|
async function toggleJtagBoundaryScan() {
|
||||||
eqps.enableJtagBoundaryScan = !eqps.enableJtagBoundaryScan;
|
eqps.jtagBoundaryScanSetOnOff(!eqps.enableJtagBoundaryScan);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isGettingIDCode = ref(false);
|
||||||
async function getIDCode(isQuiet: boolean = false) {
|
async function getIDCode(isQuiet: boolean = false) {
|
||||||
|
isGettingIDCode.value = true;
|
||||||
jtagIDCode.value = await eqps.jtagGetIDCode(isQuiet);
|
jtagIDCode.value = await eqps.jtagGetIDCode(isQuiet);
|
||||||
|
isGettingIDCode.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
watchEffect(async () => {
|
|
||||||
if (eqps.setAddr(props.jtagAddr) && eqps.setPort(props.jtagPort))
|
|
||||||
getIDCode(true);
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped lang="postcss">
|
<style scoped lang="postcss">
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ interface SevenSegmentDisplayProps {
|
|||||||
size?: number;
|
size?: number;
|
||||||
color?: string;
|
color?: string;
|
||||||
AFTERGLOW_BUFFER_SIZE?: number; // 余晖存储槽大小
|
AFTERGLOW_BUFFER_SIZE?: number; // 余晖存储槽大小
|
||||||
AFTERGLOW_UPDATE_INTERVAL?: number; // 余晖更新间隔,单位毫秒
|
AFTERGLOW_DURATION?: number; // 余晖持续时间(毫秒)
|
||||||
pins?: {
|
pins?: {
|
||||||
pinId: string;
|
pinId: string;
|
||||||
constraint: string;
|
constraint: string;
|
||||||
@@ -93,7 +93,7 @@ const props = withDefaults(defineProps<SevenSegmentDisplayProps>(), {
|
|||||||
size: 1,
|
size: 1,
|
||||||
color: "red",
|
color: "red",
|
||||||
AFTERGLOW_BUFFER_SIZE: 1, // 默认存储槽大小为100
|
AFTERGLOW_BUFFER_SIZE: 1, // 默认存储槽大小为100
|
||||||
AFTERGLOW_UPDATE_INTERVAL: 1, // 默认更新间隔为2毫秒
|
AFTERGLOW_DURATION: 2000, // 默认余晖持续时间500毫秒
|
||||||
cathodeType: "common", // 默认为共阴极
|
cathodeType: "common", // 默认为共阴极
|
||||||
pins: () => [
|
pins: () => [
|
||||||
{ pinId: "a", constraint: "", x: 10, y: 170 }, // a段
|
{ pinId: "a", constraint: "", x: 10, y: 170 }, // a段
|
||||||
@@ -149,55 +149,160 @@ const afterglowBuffers = ref<Record<string, boolean[]>>({
|
|||||||
dp: [],
|
dp: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
// 更新间隔计时器
|
// 余晖判定阈值(比如持续10帧才算稳定,可根据刷新间隔和人眼余晖调节)
|
||||||
let updateIntervalTimer: number | null = null;
|
const STABLE_THRESHOLD = 3;
|
||||||
|
|
||||||
// 判断段是否激活 - 如果当前状态或任一历史状态为true,则视为激活
|
// 实际显示的段状态(只有稳定后才改变)
|
||||||
|
const stableSegmentStates = ref({
|
||||||
|
a: false,
|
||||||
|
b: false,
|
||||||
|
c: false,
|
||||||
|
d: false,
|
||||||
|
e: false,
|
||||||
|
f: false,
|
||||||
|
g: false,
|
||||||
|
dp: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 每段的稳定计数器
|
||||||
|
const segmentStableCounters = ref<Record<string, number>>({
|
||||||
|
a: 0,
|
||||||
|
b: 0,
|
||||||
|
c: 0,
|
||||||
|
d: 0,
|
||||||
|
e: 0,
|
||||||
|
f: 0,
|
||||||
|
g: 0,
|
||||||
|
dp: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 段选关闭时的余晖状态保持
|
||||||
|
const afterglowStates = ref({
|
||||||
|
a: false,
|
||||||
|
b: false,
|
||||||
|
c: false,
|
||||||
|
d: false,
|
||||||
|
e: false,
|
||||||
|
f: false,
|
||||||
|
g: false,
|
||||||
|
dp: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 余晖计时器
|
||||||
|
const afterglowTimers = ref<Record<string, number | null>>({
|
||||||
|
a: null,
|
||||||
|
b: null,
|
||||||
|
c: null,
|
||||||
|
d: null,
|
||||||
|
e: null,
|
||||||
|
f: null,
|
||||||
|
g: null,
|
||||||
|
dp: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 余晖持续时间(毫秒)
|
||||||
|
const AFTERGLOW_DURATION = computed(() => props.AFTERGLOW_DURATION || 500);
|
||||||
|
|
||||||
|
// 当前COM口状态
|
||||||
|
const currentComActive = ref(false); // 初始化为false,等待第一次状态检查
|
||||||
|
|
||||||
|
// 是否处于余晖模式
|
||||||
|
const isInAfterglowMode = ref(false);
|
||||||
|
|
||||||
|
// 判断段是否激活(用稳定状态或余晖状态)
|
||||||
function isSegmentActive(
|
function isSegmentActive(
|
||||||
segment: "a" | "b" | "c" | "d" | "e" | "f" | "g" | "dp",
|
segment: "a" | "b" | "c" | "d" | "e" | "f" | "g" | "dp",
|
||||||
): boolean {
|
): boolean {
|
||||||
return segmentStates.value[segment] || afterglowBuffers.value[segment].some(state => state);
|
// 如果处于余晖模式,使用余晖状态
|
||||||
|
if (isInAfterglowMode.value) {
|
||||||
|
return afterglowStates.value[segment];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果COM口未激活,所有段都不显示
|
||||||
|
if (!currentComActive.value) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 否则使用稳定状态
|
||||||
|
return stableSegmentStates.value[segment];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新引脚状态的函数
|
// 更新引脚状态的函数
|
||||||
function updateSegmentStates() {
|
function updateSegmentStates() {
|
||||||
// 先获取COM口状态
|
// 先获取COM口状态
|
||||||
const comPin = props.pins.find(p => p.pinId === "COM");
|
const comPin = props.pins.find((p) => p.pinId === "COM");
|
||||||
let comActive = true;
|
let comActive = false; // 默认未激活
|
||||||
|
|
||||||
if (comPin && comPin.constraint) {
|
if (comPin && comPin.constraint) {
|
||||||
const comState = getConstraintState(comPin.constraint);
|
const comState = getConstraintState(comPin.constraint);
|
||||||
if (props.cathodeType === "anode") {
|
if (props.cathodeType === "anode") {
|
||||||
// anode模式下,COM为高电平则所有段都熄灭
|
// 共阳极模式下,COM为低电平才激活
|
||||||
comActive = comState !== "high";
|
comActive = comState === "low";
|
||||||
|
} else {
|
||||||
|
// 共阴极模式下,COM为低电平才激活
|
||||||
|
comActive = comState === "low";
|
||||||
}
|
}
|
||||||
// 可扩展其他模式
|
} else if (!comPin || !comPin.constraint) {
|
||||||
|
// 如果没有COM引脚或者COM引脚没有约束,则认为始终激活
|
||||||
|
comActive = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果COM口激活,更新所有段的状态到存储槽
|
// 检查COM口状态是否发生变化
|
||||||
if (comActive) {
|
const comStateChanged = currentComActive.value !== comActive;
|
||||||
|
currentComActive.value = comActive;
|
||||||
|
|
||||||
|
// 如果COM从激活变为非激活,进入余晖模式
|
||||||
|
if (comStateChanged && !comActive) {
|
||||||
|
enterAfterglowMode();
|
||||||
|
return; // 在余晖模式下,不处理其他引脚变化
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果COM从非激活变为激活,退出余晖模式
|
||||||
|
if (comStateChanged && comActive) {
|
||||||
|
exitAfterglowMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关键修复:如果COM口未激活或处于余晖模式,不处理任何引脚状态变化
|
||||||
|
if (!comActive || isInAfterglowMode.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只有当COM口激活时,才更新段的状态
|
||||||
updateAfterglowBuffers();
|
updateAfterglowBuffers();
|
||||||
}
|
|
||||||
|
|
||||||
|
// 先更新 segmentStates
|
||||||
for (const pin of props.pins) {
|
for (const pin of props.pins) {
|
||||||
if (["a", "b", "c", "d", "e", "f", "g", "dp"].includes(pin.pinId)) {
|
if (["a", "b", "c", "d", "e", "f", "g", "dp"].includes(pin.pinId)) {
|
||||||
// 如果constraint为空,则默认为未激活状态
|
|
||||||
if (!pin.constraint) {
|
if (!pin.constraint) {
|
||||||
segmentStates.value[pin.pinId as keyof typeof segmentStates.value] = false;
|
segmentStates.value[pin.pinId as keyof typeof segmentStates.value] = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const pinState = getConstraintState(pin.constraint);
|
const pinState = getConstraintState(pin.constraint);
|
||||||
let newState: boolean;
|
let newState: boolean;
|
||||||
if (props.cathodeType === "common") {
|
if (props.cathodeType === "common") {
|
||||||
// 共阴极: 高电平激活段
|
|
||||||
newState = pinState === "high";
|
newState = pinState === "high";
|
||||||
} else {
|
} else {
|
||||||
// 共阳极: 低电平激活段
|
|
||||||
newState = pinState === "low";
|
newState = pinState === "low";
|
||||||
}
|
}
|
||||||
|
// 段状态只有在COM激活时才有效
|
||||||
|
segmentStates.value[pin.pinId as keyof typeof segmentStates.value] = newState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 更新当前状态
|
// 余晖判定:只有新状态持续 STABLE_THRESHOLD 次才更新 stableSegmentStates
|
||||||
segmentStates.value[pin.pinId as keyof typeof segmentStates.value] = newState && comActive;
|
for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) {
|
||||||
|
const typedSegmentId = segmentId as keyof typeof segmentStates.value;
|
||||||
|
const current = segmentStates.value[typedSegmentId];
|
||||||
|
const stable = stableSegmentStates.value[typedSegmentId];
|
||||||
|
|
||||||
|
if (current === stable) {
|
||||||
|
segmentStableCounters.value[segmentId] = 0; // 状态一致,计数器清零
|
||||||
|
} else {
|
||||||
|
segmentStableCounters.value[segmentId]++;
|
||||||
|
if (segmentStableCounters.value[segmentId] >= STABLE_THRESHOLD) {
|
||||||
|
stableSegmentStates.value[typedSegmentId] = current;
|
||||||
|
segmentStableCounters.value[segmentId] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -212,26 +317,54 @@ function updateAfterglowBuffers() {
|
|||||||
afterglowBuffers.value[segmentId].unshift(currentState);
|
afterglowBuffers.value[segmentId].unshift(currentState);
|
||||||
|
|
||||||
// 如果存储槽超过了最大容量,移除最旧的状态
|
// 如果存储槽超过了最大容量,移除最旧的状态
|
||||||
if (afterglowBuffers.value[segmentId].length > props.AFTERGLOW_BUFFER_SIZE) {
|
if (
|
||||||
|
afterglowBuffers.value[segmentId].length > props.AFTERGLOW_BUFFER_SIZE
|
||||||
|
) {
|
||||||
afterglowBuffers.value[segmentId].pop();
|
afterglowBuffers.value[segmentId].pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 开始余晖更新间隔
|
// 进入余晖模式
|
||||||
function startAfterglowUpdates() {
|
function enterAfterglowMode() {
|
||||||
if (updateIntervalTimer) return;
|
isInAfterglowMode.value = true;
|
||||||
|
|
||||||
updateIntervalTimer = window.setInterval(() => {
|
// 保存当前稳定状态作为余晖状态
|
||||||
updateSegmentStates();
|
for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) {
|
||||||
}, props.AFTERGLOW_UPDATE_INTERVAL);
|
const typedSegmentId = segmentId as keyof typeof stableSegmentStates.value;
|
||||||
|
afterglowStates.value[typedSegmentId] = stableSegmentStates.value[typedSegmentId];
|
||||||
|
|
||||||
|
// 设置定时器,在余晖持续时间后退出余晖模式
|
||||||
|
if (afterglowTimers.value[segmentId]) {
|
||||||
|
clearTimeout(afterglowTimers.value[segmentId]!);
|
||||||
|
}
|
||||||
|
|
||||||
|
afterglowTimers.value[segmentId] = setTimeout(() => {
|
||||||
|
afterglowStates.value[typedSegmentId] = false;
|
||||||
|
|
||||||
|
// 检查是否所有段都已经关闭
|
||||||
|
const allSegmentsOff = Object.values(afterglowStates.value).every(state => !state);
|
||||||
|
if (allSegmentsOff) {
|
||||||
|
exitAfterglowMode();
|
||||||
|
}
|
||||||
|
}, AFTERGLOW_DURATION.value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 停止余晖更新间隔
|
// 退出余晖模式
|
||||||
function stopAfterglowUpdates() {
|
function exitAfterglowMode() {
|
||||||
if (updateIntervalTimer) {
|
isInAfterglowMode.value = false;
|
||||||
window.clearInterval(updateIntervalTimer);
|
|
||||||
updateIntervalTimer = null;
|
// 清除所有定时器
|
||||||
|
for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) {
|
||||||
|
if (afterglowTimers.value[segmentId]) {
|
||||||
|
clearTimeout(afterglowTimers.value[segmentId]!);
|
||||||
|
afterglowTimers.value[segmentId] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重置余晖状态
|
||||||
|
const typedSegmentId = segmentId as keyof typeof afterglowStates.value;
|
||||||
|
afterglowStates.value[typedSegmentId] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,17 +380,22 @@ function onConstraintChange(constraint: string, level: string) {
|
|||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
// 初始化余晖存储槽
|
// 初始化余晖存储槽
|
||||||
for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) {
|
for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) {
|
||||||
afterglowBuffers.value[segmentId] = Array(props.AFTERGLOW_BUFFER_SIZE).fill(false);
|
afterglowBuffers.value[segmentId] = Array(props.AFTERGLOW_BUFFER_SIZE).fill(
|
||||||
|
false,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateSegmentStates();
|
updateSegmentStates();
|
||||||
onConstraintStateChange(onConstraintChange);
|
onConstraintStateChange(onConstraintChange);
|
||||||
startAfterglowUpdates();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
// 清理约束状态监听
|
// 清理所有余晖定时器
|
||||||
stopAfterglowUpdates();
|
for (const segmentId of ["a", "b", "c", "d", "e", "f", "g", "dp"]) {
|
||||||
|
if (afterglowTimers.value[segmentId]) {
|
||||||
|
clearTimeout(afterglowTimers.value[segmentId]!);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 暴露属性和方法
|
// 暴露属性和方法
|
||||||
@@ -291,6 +429,7 @@ export function getDefaultProps() {
|
|||||||
size: 1,
|
size: 1,
|
||||||
color: "red",
|
color: "red",
|
||||||
cathodeType: "common",
|
cathodeType: "common",
|
||||||
|
AFTERGLOW_DURATION: 500, // 默认余晖持续时间500毫秒
|
||||||
pins: [
|
pins: [
|
||||||
{ pinId: "a", constraint: "", x: 10, y: 170 },
|
{ pinId: "a", constraint: "", x: 10, y: 170 },
|
||||||
{ pinId: "b", constraint: "", x: 25 - 1, y: 170 },
|
{ pinId: "b", constraint: "", x: 25 - 1, y: 170 },
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import AuthView from "../views/AuthView.vue";
|
|||||||
import ProjectView from "../views/Project/Index.vue";
|
import ProjectView from "../views/Project/Index.vue";
|
||||||
import TestView from "../views/TestView.vue";
|
import TestView from "../views/TestView.vue";
|
||||||
import UserView from "@/views/User/Index.vue";
|
import UserView from "@/views/User/Index.vue";
|
||||||
|
import ExamView from "@/views/ExamView.vue";
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(import.meta.env.BASE_URL),
|
history: createWebHistory(import.meta.env.BASE_URL),
|
||||||
@@ -13,6 +14,7 @@ const router = createRouter({
|
|||||||
{ path: "/project", name: "project", component: ProjectView },
|
{ path: "/project", name: "project", component: ProjectView },
|
||||||
{ path: "/test", name: "test", component: TestView },
|
{ path: "/test", name: "test", component: TestView },
|
||||||
{ path: "/user", name: "user", component: UserView },
|
{ path: "/user", name: "user", component: UserView },
|
||||||
|
{ path: "/exam", name: "exam", component: ExamView },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
25
src/server.Hubs.ts
Normal file
25
src/server.Hubs.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/* THIS (.ts) FILE IS GENERATED BY Tapper */
|
||||||
|
/* eslint-disable */
|
||||||
|
/* tslint:disable */
|
||||||
|
|
||||||
|
/** Transpiled from server.Hubs.ProgressStatus */
|
||||||
|
export enum ProgressStatus {
|
||||||
|
Pending = 0,
|
||||||
|
InProgress = 1,
|
||||||
|
Completed = 2,
|
||||||
|
Canceled = 3,
|
||||||
|
Failed = 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Transpiled from server.Hubs.ProgressInfo */
|
||||||
|
export type ProgressInfo = {
|
||||||
|
/** Transpiled from string */
|
||||||
|
taskId: string;
|
||||||
|
/** Transpiled from server.Hubs.ProgressStatus */
|
||||||
|
status: ProgressStatus;
|
||||||
|
/** Transpiled from int */
|
||||||
|
progressPercent: number;
|
||||||
|
/** Transpiled from string */
|
||||||
|
errorMessage: string;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,26 +1,25 @@
|
|||||||
import { ref, computed, reactive } from 'vue'
|
import { ref, computed, reactive } from "vue";
|
||||||
import { defineStore } from 'pinia'
|
import { defineStore } from "pinia";
|
||||||
import { isBoolean } from 'lodash';
|
import { isBoolean } from "lodash";
|
||||||
|
|
||||||
// 约束电平状态类型
|
// 约束电平状态类型
|
||||||
export type ConstraintLevel = 'high' | 'low' | 'undefined';
|
export type ConstraintLevel = "high" | "low" | "undefined";
|
||||||
|
|
||||||
export const useConstraintsStore = defineStore('constraints', () => {
|
|
||||||
|
|
||||||
|
export const useConstraintsStore = defineStore("constraints", () => {
|
||||||
// 约束状态存储
|
// 约束状态存储
|
||||||
const constraintStates = reactive<Record<string, ConstraintLevel>>({});
|
const constraintStates = reactive<Record<string, ConstraintLevel>>({});
|
||||||
|
|
||||||
// 约束颜色映射
|
// 约束颜色映射
|
||||||
const constraintColors = {
|
const constraintColors = {
|
||||||
high: '#ff3333', // 高电平为红色
|
high: "#ff3333", // 高电平为红色
|
||||||
low: '#3333ff', // 低电平为蓝色
|
low: "#3333ff", // 低电平为蓝色
|
||||||
undefined: '#999999' // 未定义为灰色
|
undefined: "#999999", // 未定义为灰色
|
||||||
};
|
};
|
||||||
|
|
||||||
// 获取约束状态
|
// 获取约束状态
|
||||||
function getConstraintState(constraint: string): ConstraintLevel {
|
function getConstraintState(constraint: string): ConstraintLevel {
|
||||||
if (!constraint) return 'undefined';
|
if (!constraint) return "undefined";
|
||||||
return constraintStates[constraint] || 'undefined';
|
return constraintStates[constraint] || "undefined";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置约束状态
|
// 设置约束状态
|
||||||
@@ -30,7 +29,9 @@ export const useConstraintsStore = defineStore('constraints', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 批量设置约束状态
|
// 批量设置约束状态
|
||||||
function batchSetConstraintStates(states: Record<string, ConstraintLevel> | Record<string, boolean>) {
|
function batchSetConstraintStates(
|
||||||
|
states: Record<string, ConstraintLevel> | Partial<Record<string, boolean>>,
|
||||||
|
) {
|
||||||
// 收集发生变化的约束
|
// 收集发生变化的约束
|
||||||
const changedConstraints: [string, ConstraintLevel][] = [];
|
const changedConstraints: [string, ConstraintLevel][] = [];
|
||||||
|
|
||||||
@@ -38,6 +39,8 @@ export const useConstraintsStore = defineStore('constraints', () => {
|
|||||||
Object.entries(states).forEach(([constraint, level]) => {
|
Object.entries(states).forEach(([constraint, level]) => {
|
||||||
if (isBoolean(level)) {
|
if (isBoolean(level)) {
|
||||||
level = level ? "high" : "low";
|
level = level ? "high" : "low";
|
||||||
|
} else {
|
||||||
|
level = "low";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (constraintStates[constraint] !== level) {
|
if (constraintStates[constraint] !== level) {
|
||||||
@@ -48,7 +51,7 @@ export const useConstraintsStore = defineStore('constraints', () => {
|
|||||||
|
|
||||||
// 通知所有变化
|
// 通知所有变化
|
||||||
changedConstraints.forEach(([constraint, level]) => {
|
changedConstraints.forEach(([constraint, level]) => {
|
||||||
stateChangeCallbacks.forEach(callback => callback(constraint, level));
|
stateChangeCallbacks.forEach((callback) => callback(constraint, level));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +63,7 @@ export const useConstraintsStore = defineStore('constraints', () => {
|
|||||||
|
|
||||||
// 清除所有约束状态
|
// 清除所有约束状态
|
||||||
function clearAllConstraintStates() {
|
function clearAllConstraintStates() {
|
||||||
Object.keys(constraintStates).forEach(key => {
|
Object.keys(constraintStates).forEach((key) => {
|
||||||
delete constraintStates[key];
|
delete constraintStates[key];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -71,9 +74,14 @@ export const useConstraintsStore = defineStore('constraints', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 注册约束状态变化回调
|
// 注册约束状态变化回调
|
||||||
const stateChangeCallbacks: ((constraint: string, level: ConstraintLevel) => void)[] = [];
|
const stateChangeCallbacks: ((
|
||||||
|
constraint: string,
|
||||||
|
level: ConstraintLevel,
|
||||||
|
) => void)[] = [];
|
||||||
|
|
||||||
function onConstraintStateChange(callback: (constraint: string, level: ConstraintLevel) => void) {
|
function onConstraintStateChange(
|
||||||
|
callback: (constraint: string, level: ConstraintLevel) => void,
|
||||||
|
) {
|
||||||
stateChangeCallbacks.push(callback);
|
stateChangeCallbacks.push(callback);
|
||||||
return () => {
|
return () => {
|
||||||
const index = stateChangeCallbacks.indexOf(callback);
|
const index = stateChangeCallbacks.indexOf(callback);
|
||||||
@@ -86,7 +94,7 @@ export const useConstraintsStore = defineStore('constraints', () => {
|
|||||||
// 触发约束变化
|
// 触发约束变化
|
||||||
function notifyConstraintChange(constraint: string, level: ConstraintLevel) {
|
function notifyConstraintChange(constraint: string, level: ConstraintLevel) {
|
||||||
setConstraintState(constraint, level);
|
setConstraintState(constraint, level);
|
||||||
stateChangeCallbacks.forEach(callback => callback(constraint, level));
|
stateChangeCallbacks.forEach((callback) => callback(constraint, level));
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -98,6 +106,5 @@ export const useConstraintsStore = defineStore('constraints', () => {
|
|||||||
getAllConstraintStates,
|
getAllConstraintStates,
|
||||||
onConstraintStateChange,
|
onConstraintStateChange,
|
||||||
notifyConstraintChange,
|
notifyConstraintChange,
|
||||||
}
|
};
|
||||||
})
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,76 +1,88 @@
|
|||||||
import { ref, reactive, watchPostEffect } from 'vue'
|
import { ref, reactive, watchPostEffect, onMounted, onUnmounted } from "vue";
|
||||||
import { defineStore } from 'pinia'
|
import { defineStore } from "pinia";
|
||||||
import { useLocalStorage } from '@vueuse/core'
|
import { useLocalStorage } from "@vueuse/core";
|
||||||
import { isString, toNumber } from 'lodash';
|
import { isString, toNumber, isUndefined, type Dictionary } from "lodash";
|
||||||
import { Common } from '@/utils/Common';
|
import z from "zod";
|
||||||
import z from "zod"
|
import { isNumber } from "mathjs";
|
||||||
import { isNumber } from 'mathjs';
|
import { Mutex, withTimeout } from "async-mutex";
|
||||||
import { JtagClient, MatrixKeyClient, PowerClient } from "@/APIClient";
|
|
||||||
import { Mutex, withTimeout } from 'async-mutex';
|
|
||||||
import { useConstraintsStore } from "@/stores/constraints";
|
import { useConstraintsStore } from "@/stores/constraints";
|
||||||
import { useDialogStore } from './dialog';
|
import { useDialogStore } from "./dialog";
|
||||||
|
import { toFileParameterOrUndefined } from "@/utils/Common";
|
||||||
|
import { AuthManager } from "@/utils/AuthManager";
|
||||||
|
import { HubConnection, HubConnectionBuilder } from "@microsoft/signalr";
|
||||||
|
import { getHubProxyFactory, getReceiverRegister } from "@/TypedSignalR.Client";
|
||||||
|
import type { ResourceInfo } from "@/APIClient";
|
||||||
|
import type { IJtagHub } from "@/TypedSignalR.Client/server.Hubs";
|
||||||
|
|
||||||
export const useEquipments = defineStore('equipments', () => {
|
export const useEquipments = defineStore("equipments", () => {
|
||||||
// Global Stores
|
// Global Stores
|
||||||
const constrainsts = useConstraintsStore();
|
const constrainsts = useConstraintsStore();
|
||||||
const dialog = useDialogStore();
|
const dialog = useDialogStore();
|
||||||
|
|
||||||
const boardAddr = useLocalStorage('fpga-board-addr', "127.0.0.1");
|
const boardAddr = useLocalStorage("fpga-board-addr", "127.0.0.1");
|
||||||
const boardPort = useLocalStorage('fpga-board-port', 1234);
|
const boardPort = useLocalStorage("fpga-board-port", 1234);
|
||||||
|
|
||||||
// Jtag
|
// Jtag
|
||||||
const jtagBitstream = ref<File>();
|
const jtagBitstream = ref<File>();
|
||||||
const jtagBoundaryScanFreq = ref(100);
|
const jtagBoundaryScanFreq = ref(100);
|
||||||
const jtagClientMutex = withTimeout(new Mutex(), 1000, new Error("JtagClient Mutex Timeout!"))
|
const jtagUserBitstreams = ref<ResourceInfo[]>([]);
|
||||||
const jtagClient = new JtagClient();
|
const jtagClientMutex = withTimeout(
|
||||||
|
new Mutex(),
|
||||||
|
1000,
|
||||||
|
new Error("JtagClient Mutex Timeout!"),
|
||||||
|
);
|
||||||
|
const jtagHubConnection = ref<HubConnection>();
|
||||||
|
const jtagHubProxy = ref<IJtagHub>();
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
// 每次挂载都重新创建连接
|
||||||
|
jtagHubConnection.value =
|
||||||
|
AuthManager.createAuthenticatedJtagHubConnection();
|
||||||
|
jtagHubProxy.value = getHubProxyFactory("IJtagHub").createHubProxy(
|
||||||
|
jtagHubConnection.value,
|
||||||
|
);
|
||||||
|
|
||||||
|
getReceiverRegister("IJtagReceiver").register(jtagHubConnection.value, {
|
||||||
|
onReceiveBoundaryScanData: async (msg) => {
|
||||||
|
constrainsts.batchSetConstraintStates(msg);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await jtagHubConnection.value.start();
|
||||||
|
});
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
// 断开连接,清理资源
|
||||||
|
if (jtagHubConnection.value) {
|
||||||
|
jtagHubConnection.value.stop();
|
||||||
|
jtagHubConnection.value = undefined;
|
||||||
|
jtagHubProxy.value = undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Matrix Key
|
// Matrix Key
|
||||||
const matrixKeyStates = reactive(new Array<boolean>(16).fill(false))
|
const matrixKeyStates = reactive(new Array<boolean>(16).fill(false));
|
||||||
const matrixKeypadClientMutex = withTimeout(new Mutex(), 1000, new Error("Matrixkeyclient Mutex Timeout!"));
|
const matrixKeypadClientMutex = withTimeout(
|
||||||
const matrixKeypadClient = new MatrixKeyClient();
|
new Mutex(),
|
||||||
|
1000,
|
||||||
|
new Error("Matrixkeyclient Mutex Timeout!"),
|
||||||
|
);
|
||||||
|
|
||||||
// Power
|
// Power
|
||||||
const powerClientMutex = withTimeout(new Mutex(), 1000, new Error("Matrixkeyclient Mutex Timeout!"));
|
const powerClientMutex = withTimeout(
|
||||||
const powerClient = new PowerClient();
|
new Mutex(),
|
||||||
|
1000,
|
||||||
|
new Error("Matrixkeyclient Mutex Timeout!"),
|
||||||
|
);
|
||||||
|
|
||||||
// Enable Setting
|
// Enable Setting
|
||||||
const enableJtagBoundaryScan = ref(false);
|
const enableJtagBoundaryScan = ref(false);
|
||||||
const enableMatrixKey = ref(false);
|
const enableMatrixKey = ref(false);
|
||||||
const enablePower = ref(false)
|
const enablePower = ref(false);
|
||||||
|
|
||||||
// Watch
|
function setMatrixKey(
|
||||||
watchPostEffect(async () => {
|
keyNum: number | string | undefined,
|
||||||
if (true === enableJtagBoundaryScan.value) jtagBoundaryScan();
|
keyValue: boolean,
|
||||||
});
|
): boolean {
|
||||||
|
|
||||||
// Parse and Set
|
|
||||||
function setAddr(address: string | undefined): boolean {
|
|
||||||
if (isString(address) && z.string().ip("4").safeParse(address).success) {
|
|
||||||
boardAddr.value = address;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setPort(port: string | number | undefined): boolean {
|
|
||||||
if (isString(port) && port.length != 0) {
|
|
||||||
const portNumber = toNumber(port);
|
|
||||||
if (z.number().nonnegative().max(65535).safeParse(portNumber).success) {
|
|
||||||
boardPort.value = portNumber;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (isNumber(port)) {
|
|
||||||
if (z.number().nonnegative().max(65535).safeParse(port).success) {
|
|
||||||
boardPort.value = port;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setMatrixKey(keyNum: number | string | undefined, keyValue: boolean): boolean {
|
|
||||||
let _keyNum: number;
|
let _keyNum: number;
|
||||||
if (isString(keyNum)) {
|
if (isString(keyNum)) {
|
||||||
_keyNum = toNumber(keyNum);
|
_keyNum = toNumber(keyNum);
|
||||||
@@ -87,53 +99,81 @@ export const useEquipments = defineStore('equipments', () => {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function jtagBoundaryScan() {
|
async function jtagBoundaryScanSetOnOff(enable: boolean) {
|
||||||
const release = await jtagClientMutex.acquire();
|
if (isUndefined(jtagHubProxy.value)) {
|
||||||
try {
|
console.error("JtagHub Not Initialize...");
|
||||||
const portStates = await jtagClient.boundaryScanLogicalPorts(
|
return;
|
||||||
boardAddr.value,
|
|
||||||
boardPort.value,
|
|
||||||
);
|
|
||||||
|
|
||||||
constrainsts.batchSetConstraintStates(portStates);
|
|
||||||
} catch (error) {
|
|
||||||
dialog.error("边界扫描发生错误");
|
|
||||||
console.error(error);
|
|
||||||
enableJtagBoundaryScan.value = false;
|
|
||||||
} finally {
|
|
||||||
release();
|
|
||||||
|
|
||||||
if (enableJtagBoundaryScan.value)
|
|
||||||
setTimeout(jtagBoundaryScan, 1000 / jtagBoundaryScanFreq.value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function jtagUploadBitstream(bitstream: File): Promise<boolean> {
|
if (enable) {
|
||||||
try {
|
const ret = await jtagHubProxy.value.startBoundaryScan(
|
||||||
const resp = await jtagClient.uploadBitstream(
|
jtagBoundaryScanFreq.value,
|
||||||
boardAddr.value,
|
|
||||||
Common.toFileParameterOrNull(bitstream),
|
|
||||||
);
|
);
|
||||||
return resp;
|
if (!ret) {
|
||||||
|
console.error("Failed to start boundary scan");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const ret = await jtagHubProxy.value.stopBoundaryScan();
|
||||||
|
if (!ret) {
|
||||||
|
console.error("Failed to stop boundary scan");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
enableJtagBoundaryScan.value = enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function jtagUploadBitstream(
|
||||||
|
bitstream: File,
|
||||||
|
examId?: string,
|
||||||
|
): Promise<number | null> {
|
||||||
|
try {
|
||||||
|
// 自动开启电源
|
||||||
|
await powerSetOnOff(true);
|
||||||
|
|
||||||
|
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||||
|
const resp = await resourceClient.addResource(
|
||||||
|
"bitstream",
|
||||||
|
"user",
|
||||||
|
examId || null,
|
||||||
|
toFileParameterOrUndefined(bitstream),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 如果上传成功,设置为当前选中的比特流
|
||||||
|
if (resp && resp.id !== undefined && resp.id !== null) {
|
||||||
|
return resp.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
dialog.error("上传错误");
|
dialog.error("上传错误");
|
||||||
console.error(e);
|
console.error(e);
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function jtagDownloadBitstream(): Promise<boolean> {
|
async function jtagDownloadBitstream(bitstreamId?: number): Promise<string> {
|
||||||
|
if (bitstreamId === null || bitstreamId === undefined) {
|
||||||
|
dialog.error("请先选择要下载的比特流");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
const release = await jtagClientMutex.acquire();
|
const release = await jtagClientMutex.acquire();
|
||||||
try {
|
try {
|
||||||
|
// 自动开启电源
|
||||||
|
await powerSetOnOff(true);
|
||||||
|
|
||||||
|
const jtagClient = AuthManager.createAuthenticatedJtagClient();
|
||||||
const resp = await jtagClient.downloadBitstream(
|
const resp = await jtagClient.downloadBitstream(
|
||||||
boardAddr.value,
|
boardAddr.value,
|
||||||
boardPort.value
|
boardPort.value,
|
||||||
|
bitstreamId,
|
||||||
);
|
);
|
||||||
return resp;
|
return resp;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
dialog.error("上传错误");
|
dialog.error("下载错误");
|
||||||
console.error(e);
|
console.error(e);
|
||||||
return false;
|
throw e;
|
||||||
} finally {
|
} finally {
|
||||||
release();
|
release();
|
||||||
}
|
}
|
||||||
@@ -142,9 +182,13 @@ export const useEquipments = defineStore('equipments', () => {
|
|||||||
async function jtagGetIDCode(isQuiet: boolean = false): Promise<number> {
|
async function jtagGetIDCode(isQuiet: boolean = false): Promise<number> {
|
||||||
const release = await jtagClientMutex.acquire();
|
const release = await jtagClientMutex.acquire();
|
||||||
try {
|
try {
|
||||||
|
// 自动开启电源
|
||||||
|
await powerSetOnOff(true);
|
||||||
|
|
||||||
|
const jtagClient = AuthManager.createAuthenticatedJtagClient();
|
||||||
const resp = await jtagClient.getDeviceIDCode(
|
const resp = await jtagClient.getDeviceIDCode(
|
||||||
boardAddr.value,
|
boardAddr.value,
|
||||||
boardPort.value
|
boardPort.value,
|
||||||
);
|
);
|
||||||
return resp;
|
return resp;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -158,10 +202,14 @@ export const useEquipments = defineStore('equipments', () => {
|
|||||||
async function jtagSetSpeed(speed: number): Promise<boolean> {
|
async function jtagSetSpeed(speed: number): Promise<boolean> {
|
||||||
const release = await jtagClientMutex.acquire();
|
const release = await jtagClientMutex.acquire();
|
||||||
try {
|
try {
|
||||||
|
// 自动开启电源
|
||||||
|
await powerSetOnOff(true);
|
||||||
|
|
||||||
|
const jtagClient = AuthManager.createAuthenticatedJtagClient();
|
||||||
const resp = await jtagClient.setSpeed(
|
const resp = await jtagClient.setSpeed(
|
||||||
boardAddr.value,
|
boardAddr.value,
|
||||||
boardPort.value,
|
boardPort.value,
|
||||||
speed
|
speed,
|
||||||
);
|
);
|
||||||
return resp;
|
return resp;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -176,10 +224,12 @@ export const useEquipments = defineStore('equipments', () => {
|
|||||||
const release = await matrixKeypadClientMutex.acquire();
|
const release = await matrixKeypadClientMutex.acquire();
|
||||||
console.log("set Key !!!!!!!!!!!!");
|
console.log("set Key !!!!!!!!!!!!");
|
||||||
try {
|
try {
|
||||||
|
const matrixKeypadClient =
|
||||||
|
AuthManager.createAuthenticatedMatrixKeyClient();
|
||||||
const resp = await matrixKeypadClient.setMatrixKeyStatus(
|
const resp = await matrixKeypadClient.setMatrixKeyStatus(
|
||||||
boardAddr.value,
|
boardAddr.value,
|
||||||
boardPort.value,
|
boardPort.value,
|
||||||
keyStates
|
keyStates,
|
||||||
);
|
);
|
||||||
return resp;
|
return resp;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -194,6 +244,8 @@ export const useEquipments = defineStore('equipments', () => {
|
|||||||
const release = await matrixKeypadClientMutex.acquire();
|
const release = await matrixKeypadClientMutex.acquire();
|
||||||
try {
|
try {
|
||||||
if (enable) {
|
if (enable) {
|
||||||
|
const matrixKeypadClient =
|
||||||
|
AuthManager.createAuthenticatedMatrixKeyClient();
|
||||||
const resp = await matrixKeypadClient.enabelMatrixKey(
|
const resp = await matrixKeypadClient.enabelMatrixKey(
|
||||||
boardAddr.value,
|
boardAddr.value,
|
||||||
boardPort.value,
|
boardPort.value,
|
||||||
@@ -201,6 +253,8 @@ export const useEquipments = defineStore('equipments', () => {
|
|||||||
enableMatrixKey.value = resp;
|
enableMatrixKey.value = resp;
|
||||||
return resp;
|
return resp;
|
||||||
} else {
|
} else {
|
||||||
|
const matrixKeypadClient =
|
||||||
|
AuthManager.createAuthenticatedMatrixKeyClient();
|
||||||
const resp = await matrixKeypadClient.disableMatrixKey(
|
const resp = await matrixKeypadClient.disableMatrixKey(
|
||||||
boardAddr.value,
|
boardAddr.value,
|
||||||
boardPort.value,
|
boardPort.value,
|
||||||
@@ -220,10 +274,11 @@ export const useEquipments = defineStore('equipments', () => {
|
|||||||
async function powerSetOnOff(enable: boolean) {
|
async function powerSetOnOff(enable: boolean) {
|
||||||
const release = await powerClientMutex.acquire();
|
const release = await powerClientMutex.acquire();
|
||||||
try {
|
try {
|
||||||
|
const powerClient = AuthManager.createAuthenticatedPowerClient();
|
||||||
const resp = await powerClient.setPowerOnOff(
|
const resp = await powerClient.setPowerOnOff(
|
||||||
boardAddr.value,
|
boardAddr.value,
|
||||||
boardPort.value,
|
boardPort.value,
|
||||||
enable
|
enable,
|
||||||
);
|
);
|
||||||
return resp;
|
return resp;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -238,16 +293,14 @@ export const useEquipments = defineStore('equipments', () => {
|
|||||||
return {
|
return {
|
||||||
boardAddr,
|
boardAddr,
|
||||||
boardPort,
|
boardPort,
|
||||||
setAddr,
|
|
||||||
setPort,
|
|
||||||
setMatrixKey,
|
setMatrixKey,
|
||||||
|
|
||||||
// Jtag
|
// Jtag
|
||||||
enableJtagBoundaryScan,
|
enableJtagBoundaryScan,
|
||||||
|
jtagBoundaryScanSetOnOff,
|
||||||
jtagBitstream,
|
jtagBitstream,
|
||||||
jtagBoundaryScanFreq,
|
jtagBoundaryScanFreq,
|
||||||
jtagClientMutex,
|
jtagUserBitstreams,
|
||||||
jtagClient,
|
|
||||||
jtagUploadBitstream,
|
jtagUploadBitstream,
|
||||||
jtagDownloadBitstream,
|
jtagDownloadBitstream,
|
||||||
jtagGetIDCode,
|
jtagGetIDCode,
|
||||||
@@ -257,15 +310,12 @@ export const useEquipments = defineStore('equipments', () => {
|
|||||||
enableMatrixKey,
|
enableMatrixKey,
|
||||||
matrixKeyStates,
|
matrixKeyStates,
|
||||||
matrixKeypadClientMutex,
|
matrixKeypadClientMutex,
|
||||||
matrixKeypadClient,
|
|
||||||
matrixKeypadEnable,
|
matrixKeypadEnable,
|
||||||
matrixKeypadSetKeyStates,
|
matrixKeypadSetKeyStates,
|
||||||
|
|
||||||
// Power
|
// Power
|
||||||
enablePower,
|
enablePower,
|
||||||
powerClient,
|
|
||||||
powerClientMutex,
|
powerClientMutex,
|
||||||
powerSetOnOff,
|
powerSetOnOff,
|
||||||
}
|
};
|
||||||
})
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
import { ref, computed } from 'vue'
|
|
||||||
import { defineStore } from 'pinia'
|
|
||||||
|
|
||||||
export const useSidebarStore = defineStore('sidebar', () => {
|
|
||||||
const isClose = ref(false);
|
|
||||||
|
|
||||||
function closeSidebar() {
|
|
||||||
isClose.value = true;
|
|
||||||
console.info("Close sidebar");
|
|
||||||
}
|
|
||||||
|
|
||||||
function openSidebar() {
|
|
||||||
isClose.value = false;
|
|
||||||
console.info("Open sidebar");
|
|
||||||
}
|
|
||||||
|
|
||||||
function toggleSidebar() {
|
|
||||||
if (isClose.value) {
|
|
||||||
openSidebar();
|
|
||||||
// themeSidebar.value = "card-dash sidebar-base sidebar-open"
|
|
||||||
} else {
|
|
||||||
closeSidebar();
|
|
||||||
// themeSidebar.value = "card-dash sidebar-base sidebar-close"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { isClose, closeSidebar, openSidebar, toggleSidebar }
|
|
||||||
})
|
|
||||||
|
|
||||||
@@ -9,7 +9,18 @@ import {
|
|||||||
RemoteUpdateClient,
|
RemoteUpdateClient,
|
||||||
TutorialClient,
|
TutorialClient,
|
||||||
UDPClient,
|
UDPClient,
|
||||||
|
LogicAnalyzerClient,
|
||||||
|
NetConfigClient,
|
||||||
|
OscilloscopeApiClient,
|
||||||
|
DebuggerClient,
|
||||||
|
ExamClient,
|
||||||
|
ResourceClient,
|
||||||
|
HdmiVideoStreamClient,
|
||||||
} from "@/APIClient";
|
} from "@/APIClient";
|
||||||
|
import router from "@/router";
|
||||||
|
import { HubConnectionBuilder } from "@microsoft/signalr";
|
||||||
|
import axios, { type AxiosInstance } from "axios";
|
||||||
|
import { isNull } from "lodash";
|
||||||
|
|
||||||
// 支持的客户端类型联合类型
|
// 支持的客户端类型联合类型
|
||||||
type SupportedClient =
|
type SupportedClient =
|
||||||
@@ -22,7 +33,14 @@ type SupportedClient =
|
|||||||
| PowerClient
|
| PowerClient
|
||||||
| RemoteUpdateClient
|
| RemoteUpdateClient
|
||||||
| TutorialClient
|
| TutorialClient
|
||||||
| UDPClient;
|
| LogicAnalyzerClient
|
||||||
|
| UDPClient
|
||||||
|
| NetConfigClient
|
||||||
|
| OscilloscopeApiClient
|
||||||
|
| DebuggerClient
|
||||||
|
| ExamClient
|
||||||
|
| ResourceClient
|
||||||
|
| HdmiVideoStreamClient;
|
||||||
|
|
||||||
export class AuthManager {
|
export class AuthManager {
|
||||||
// 存储token到localStorage
|
// 存储token到localStorage
|
||||||
@@ -100,13 +118,27 @@ export class AuthManager {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 私有方法:创建带认证的Axios实例
|
||||||
|
private static createAuthenticatedAxiosInstance(): AxiosInstance | null {
|
||||||
|
const token = AuthManager.getToken();
|
||||||
|
if (!token) return null;
|
||||||
|
|
||||||
|
const instance = axios.create();
|
||||||
|
instance.interceptors.request.use((config) => {
|
||||||
|
config.headers = config.headers || {};
|
||||||
|
(config.headers as any)["Authorization"] = `Bearer ${token}`;
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
// 通用的创建已认证客户端的方法(使用泛型)
|
// 通用的创建已认证客户端的方法(使用泛型)
|
||||||
public static createAuthenticatedClient<T extends SupportedClient>(
|
public static createAuthenticatedClient<T extends SupportedClient>(
|
||||||
ClientClass: new (baseUrl?: string, http?: any) => T,
|
ClientClass: new (baseUrl?: string, instance?: AxiosInstance) => T,
|
||||||
): T {
|
): T {
|
||||||
const customHttp = AuthManager.createAuthenticatedHttp();
|
const axiosInstance = AuthManager.createAuthenticatedAxiosInstance();
|
||||||
return customHttp
|
return axiosInstance
|
||||||
? new ClientClass(undefined, customHttp)
|
? new ClientClass(undefined, axiosInstance)
|
||||||
: new ClientClass();
|
: new ClientClass();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,6 +183,64 @@ export class AuthManager {
|
|||||||
return AuthManager.createAuthenticatedClient(UDPClient);
|
return AuthManager.createAuthenticatedClient(UDPClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static createAuthenticatedLogicAnalyzerClient(): LogicAnalyzerClient {
|
||||||
|
return AuthManager.createAuthenticatedClient(LogicAnalyzerClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createAuthenticatedNetConfigClient(): NetConfigClient {
|
||||||
|
return AuthManager.createAuthenticatedClient(NetConfigClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createAuthenticatedOscilloscopeApiClient(): OscilloscopeApiClient {
|
||||||
|
return AuthManager.createAuthenticatedClient(OscilloscopeApiClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createAuthenticatedDebuggerClient(): DebuggerClient {
|
||||||
|
return AuthManager.createAuthenticatedClient(DebuggerClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createAuthenticatedExamClient(): ExamClient {
|
||||||
|
return AuthManager.createAuthenticatedClient(ExamClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createAuthenticatedResourceClient(): ResourceClient {
|
||||||
|
return AuthManager.createAuthenticatedClient(ResourceClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createAuthenticatedHdmiVideoStreamClient(): HdmiVideoStreamClient {
|
||||||
|
return AuthManager.createAuthenticatedClient(HdmiVideoStreamClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createAuthenticatedJtagHubConnection() {
|
||||||
|
const token = this.getToken();
|
||||||
|
if (isNull(token)) {
|
||||||
|
router.push("/login");
|
||||||
|
throw Error("Token Null!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HubConnectionBuilder()
|
||||||
|
.withUrl("http://127.0.0.1:5000/hubs/JtagHub", {
|
||||||
|
accessTokenFactory: () => token,
|
||||||
|
})
|
||||||
|
.withAutomaticReconnect()
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createAuthenticatedProgressHubConnection() {
|
||||||
|
const token = this.getToken();
|
||||||
|
if (isNull(token)) {
|
||||||
|
router.push("/login");
|
||||||
|
throw Error("Token Null!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HubConnectionBuilder()
|
||||||
|
.withUrl("http://127.0.0.1:5000/hubs/ProgressHub", {
|
||||||
|
accessTokenFactory: () => token,
|
||||||
|
})
|
||||||
|
.withAutomaticReconnect()
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
// 登录函数
|
// 登录函数
|
||||||
public static async login(
|
public static async login(
|
||||||
username: string,
|
username: string,
|
||||||
@@ -212,11 +302,7 @@ export class AuthManager {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 只有在token完全无效的情况下才清除token
|
// 只有在token完全无效的情况下才清除token
|
||||||
// 401错误表示token有效但权限不足,不应清除token
|
// 401错误表示token有效但权限不足,不应清除token
|
||||||
if (
|
if (error && typeof error === "object" && "status" in error) {
|
||||||
error &&
|
|
||||||
typeof error === "object" &&
|
|
||||||
"status" in error
|
|
||||||
) {
|
|
||||||
// 如果是403 (Forbidden) 或401 (Unauthorized),说明token有效但权限不足
|
// 如果是403 (Forbidden) 或401 (Unauthorized),说明token有效但权限不足
|
||||||
if (error.status === 401 || error.status === 403) {
|
if (error.status === 401 || error.status === 403) {
|
||||||
return false;
|
return false;
|
||||||
@@ -225,7 +311,7 @@ export class AuthManager {
|
|||||||
AuthManager.clearToken();
|
AuthManager.clearToken();
|
||||||
} else {
|
} else {
|
||||||
// 网络错误等,不清除token
|
// 网络错误等,不清除token
|
||||||
console.error('管理员权限验证失败:', error);
|
console.error("管理员权限验证失败:", error);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { ref } from "vue";
|
import { ref } from "vue";
|
||||||
import { createInjectionState } from "@vueuse/core";
|
import { createInjectionState } from "@vueuse/core";
|
||||||
import { RemoteUpdateClient, DataClient, Board } from "@/APIClient";
|
import { RemoteUpdateClient, DataClient, Board } from "@/APIClient";
|
||||||
import { Common } from "@/utils/Common";
|
|
||||||
import { isUndefined } from "lodash";
|
import { isUndefined } from "lodash";
|
||||||
import { AuthManager } from "@/utils/AuthManager";
|
import { AuthManager } from "@/utils/AuthManager";
|
||||||
|
import { toFileParameterOrNull } from "./Common";
|
||||||
|
|
||||||
// 统一的板卡数据接口,扩展原有的Board类型
|
// 统一的板卡数据接口,扩展原有的Board类型
|
||||||
export interface BoardData extends Board {
|
export interface BoardData extends Board {
|
||||||
@@ -17,7 +17,7 @@ export interface BoardData extends Board {
|
|||||||
const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
|
const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
|
||||||
// 远程升级相关参数
|
// 远程升级相关参数
|
||||||
const devPort = 1234;
|
const devPort = 1234;
|
||||||
const remoteUpdater = new RemoteUpdateClient();
|
const remoteUpdater = AuthManager.createAuthenticatedRemoteUpdateClient();
|
||||||
|
|
||||||
// 统一的板卡数据
|
// 统一的板卡数据
|
||||||
const boards = ref<BoardData[]>([]);
|
const boards = ref<BoardData[]>([]);
|
||||||
@@ -74,8 +74,6 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
|
|||||||
// 新增板卡(管理员权限)
|
// 新增板卡(管理员权限)
|
||||||
async function addBoard(
|
async function addBoard(
|
||||||
name: string,
|
name: string,
|
||||||
ipAddr: string,
|
|
||||||
port: number,
|
|
||||||
): Promise<{ success: boolean; error?: string; boardId?: string }> {
|
): Promise<{ success: boolean; error?: string; boardId?: string }> {
|
||||||
try {
|
try {
|
||||||
// 验证管理员权限
|
// 验证管理员权限
|
||||||
@@ -86,19 +84,19 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 验证输入参数
|
// 验证输入参数
|
||||||
if (!name || !ipAddr || !port) {
|
if (!name) {
|
||||||
console.error("参数验证失败", { name, ipAddr, port });
|
console.error("参数验证失败", { name });
|
||||||
return { success: false, error: "参数不完整" };
|
return { success: false, error: "参数不完整" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = AuthManager.createAuthenticatedDataClient();
|
const client = AuthManager.createAuthenticatedDataClient();
|
||||||
const boardId = await client.addBoard(name, ipAddr, port);
|
const boardId = await client.addBoard(name);
|
||||||
|
|
||||||
if (boardId) {
|
if (boardId) {
|
||||||
console.log("新增板卡成功", { boardId, name, ipAddr, port });
|
console.log("新增板卡成功", { boardId, name});
|
||||||
// 刷新板卡列表
|
// 刷新板卡列表
|
||||||
await getAllBoards();
|
await getAllBoards();
|
||||||
return { success: true};
|
return { success: true };
|
||||||
} else {
|
} else {
|
||||||
console.error("新增板卡失败:返回ID为空");
|
console.error("新增板卡失败:返回ID为空");
|
||||||
return { success: false, error: "新增板卡失败" };
|
return { success: false, error: "新增板卡失败" };
|
||||||
@@ -116,7 +114,9 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 删除板卡(管理员权限)
|
// 删除板卡(管理员权限)
|
||||||
async function deleteBoard(boardId: string): Promise<{ success: boolean; error?: string }> {
|
async function deleteBoard(
|
||||||
|
boardId: string,
|
||||||
|
): Promise<{ success: boolean; error?: string }> {
|
||||||
try {
|
try {
|
||||||
// 验证管理员权限
|
// 验证管理员权限
|
||||||
const hasAdminAuth = await AuthManager.verifyAdminAuth();
|
const hasAdminAuth = await AuthManager.verifyAdminAuth();
|
||||||
@@ -178,10 +178,10 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
|
|||||||
|
|
||||||
const uploadResult = await remoteUpdater.uploadBitstreams(
|
const uploadResult = await remoteUpdater.uploadBitstreams(
|
||||||
board.ipAddr,
|
board.ipAddr,
|
||||||
Common.toFileParameterOrNull(goldBitstream),
|
toFileParameterOrNull(goldBitstream),
|
||||||
Common.toFileParameterOrNull(appBitstream1),
|
toFileParameterOrNull(appBitstream1),
|
||||||
Common.toFileParameterOrNull(appBitstream2),
|
toFileParameterOrNull(appBitstream2),
|
||||||
Common.toFileParameterOrNull(appBitstream3),
|
toFileParameterOrNull(appBitstream3),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!uploadResult) {
|
if (!uploadResult) {
|
||||||
@@ -198,7 +198,10 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (downloadResult != cnt) {
|
if (downloadResult != cnt) {
|
||||||
console.error("固化比特流失败", { expected: cnt, actual: downloadResult });
|
console.error("固化比特流失败", {
|
||||||
|
expected: cnt,
|
||||||
|
actual: downloadResult,
|
||||||
|
});
|
||||||
return { success: false, error: "固化比特流失败" };
|
return { success: false, error: "固化比特流失败" };
|
||||||
} else {
|
} else {
|
||||||
console.log("固化比特流成功", { count: downloadResult });
|
console.log("固化比特流成功", { count: downloadResult });
|
||||||
@@ -213,7 +216,7 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
|
|||||||
// 热启动位流
|
// 热启动位流
|
||||||
async function hotresetBitstream(
|
async function hotresetBitstream(
|
||||||
board: BoardData,
|
board: BoardData,
|
||||||
bitstreamNum: number
|
bitstreamNum: number,
|
||||||
): Promise<{ success: boolean; error?: string }> {
|
): Promise<{ success: boolean; error?: string }> {
|
||||||
try {
|
try {
|
||||||
console.log("开始热启动比特流", { boardIp: board.ipAddr, bitstreamNum });
|
console.log("开始热启动比特流", { boardIp: board.ipAddr, bitstreamNum });
|
||||||
@@ -253,7 +256,11 @@ const [useProvideBoardManager, useBoardManager] = createInjectionState(() => {
|
|||||||
const file = target.files?.[0];
|
const file = target.files?.[0];
|
||||||
if (file) {
|
if (file) {
|
||||||
(board as any)[fileKey] = file;
|
(board as any)[fileKey] = file;
|
||||||
console.log(`文件选择成功`, { boardIp: board.ipAddr, fileKey, fileName: file.name });
|
console.log(`文件选择成功`, {
|
||||||
|
boardIp: board.ipAddr,
|
||||||
|
fileKey,
|
||||||
|
fileName: file.name,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,50 @@
|
|||||||
import { type FileParameter } from "@/APIClient";
|
import { type FileParameter } from "@/APIClient";
|
||||||
import { isNull, isUndefined } from "lodash";
|
import { isNull, isUndefined } from "lodash";
|
||||||
|
|
||||||
export namespace Common {
|
export function toFileParameter(object: File): FileParameter {
|
||||||
export function toFileParameter(object: File): FileParameter {
|
|
||||||
if (isNull(object) || isUndefined(object))
|
if (isNull(object) || isUndefined(object))
|
||||||
throw new Error("File is Null or Undefined");
|
throw new Error("File is Null or Undefined");
|
||||||
return {
|
return {
|
||||||
data: object,
|
data: object,
|
||||||
fileName: object.name
|
fileName: object.name,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toFileParameterOrNull(object?: File | null): FileParameter | null {
|
export function toFileParameterOrNull(
|
||||||
if (isNull(object) || isUndefined(object)) return null;
|
object?: File | null,
|
||||||
else return {
|
): FileParameter | null {
|
||||||
data: object,
|
if (isNull(object) || isUndefined(object)) return null;
|
||||||
fileName: object.name
|
else
|
||||||
}
|
return {
|
||||||
}
|
data: object,
|
||||||
|
fileName: object.name,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toFileParameterOrUndefined(
|
||||||
|
object?: File | undefined,
|
||||||
|
): FileParameter | undefined {
|
||||||
|
if (isNull(object) || isUndefined(object)) return undefined;
|
||||||
|
else
|
||||||
|
return {
|
||||||
|
data: object,
|
||||||
|
fileName: object.name,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 自定义 Hook:检查依赖注入值是否为空
|
||||||
|
export function useRequiredInjection<T>(useFn: () => T | undefined): T {
|
||||||
|
const value = useFn();
|
||||||
|
if (value === undefined) {
|
||||||
|
throw new Error("Missing required injection");
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useOptionalInjection<T>(
|
||||||
|
useFn: () => T | undefined,
|
||||||
|
defaultValue: T,
|
||||||
|
): T {
|
||||||
|
const value = useFn();
|
||||||
|
return value ?? defaultValue;
|
||||||
}
|
}
|
||||||
|
|||||||
1069
src/views/ExamView.vue
Normal file
1069
src/views/ExamView.vue
Normal file
File diff suppressed because it is too large
Load Diff
@@ -25,26 +25,37 @@
|
|||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<p class="py-6 text-lg opacity-80 leading-relaxed">
|
<p class="py-6 text-lg opacity-80 leading-relaxed">
|
||||||
Prototype and simulate electronic circuits in your browser with our
|
在浏览器中进行FPGA原型设计和电路仿真,使用现代直观的界面。创建、测试和分享您的FPGA设计,体验从基础学习到高级项目的完整开发流程。
|
||||||
modern, intuitive interface. Create, test, and share your FPGA
|
|
||||||
designs seamlessly.
|
|
||||||
</p>
|
</p>
|
||||||
<div class="flex flex-wrap gap-4 actions-container">
|
<div class="flex flex-col sm:flex-row gap-4 actions-container">
|
||||||
<router-link
|
<router-link
|
||||||
to="/project"
|
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"
|
class="btn btn-primary text-base-100 shadow-lg transform transition-all duration-300 hover:scale-105 hover:shadow-xl hover:-translate-y-1 flex-1 sm:flex-none"
|
||||||
>
|
>
|
||||||
<BookOpen class="h-5 w-5 mr-2" />
|
<BookOpen class="h-5 w-5 mr-2" />
|
||||||
进入工程界面
|
进入工程界面
|
||||||
</router-link>
|
</router-link>
|
||||||
|
<router-link
|
||||||
|
to="/exam"
|
||||||
|
class="btn btn-secondary text-base-100 shadow-lg transform transition-all duration-300 hover:scale-105 hover:shadow-xl hover:-translate-y-1 flex-1 sm:flex-none"
|
||||||
|
>
|
||||||
|
<GraduationCap class="h-5 w-5 mr-2" />
|
||||||
|
实验列表
|
||||||
|
</router-link>
|
||||||
</div>
|
</div>
|
||||||
<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"
|
||||||
>
|
>
|
||||||
|
<div class="space-y-2">
|
||||||
<p class="text-sm">
|
<p class="text-sm">
|
||||||
<span class="font-semibold text-primary">提示:</span>
|
<span class="font-semibold text-primary">工程界面:</span>
|
||||||
您可以在工程界面中创建、编辑和测试您的FPGA项目,使用我们简洁直观的界面轻松进行硬件设计。
|
自由创建和编辑FPGA项目,使用可视化画布进行电路设计和仿真测试。
|
||||||
</p>
|
</p>
|
||||||
|
<p class="text-sm">
|
||||||
|
<span class="font-semibold text-secondary">实验列表:</span>
|
||||||
|
浏览结构化的学习实验,从基础概念到高级应用的系统性学习路径。
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -55,7 +66,7 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import "@/router";
|
import "@/router";
|
||||||
import TutorialCarousel from "@/components/TutorialCarousel.vue";
|
import TutorialCarousel from "@/components/TutorialCarousel.vue";
|
||||||
import { BookOpen } from "lucide-vue-next";
|
import { BookOpen, GraduationCap } from "lucide-vue-next";
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped lang="postcss">
|
<style scoped lang="postcss">
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="h-full flex flex-col gap-7">
|
<div class="h-full flex flex-col gap-7">
|
||||||
<div class="tabs tabs-box flex-shrink-0 shadow-xl">
|
<div class="tabs tabs-lift flex-shrink-0 mx-5">
|
||||||
<label class="tab">
|
<label class="tab">
|
||||||
<input
|
<input
|
||||||
type="radio"
|
type="radio"
|
||||||
name="function-bar"
|
name="function-bar"
|
||||||
id="1"
|
id="1"
|
||||||
checked
|
:checked="checkID === 1"
|
||||||
@change="handleTabChange"
|
@change="handleTabChange"
|
||||||
/>
|
/>
|
||||||
<TerminalIcon class="icon" />
|
<TerminalIcon class="icon" />
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
type="radio"
|
type="radio"
|
||||||
name="function-bar"
|
name="function-bar"
|
||||||
id="2"
|
id="2"
|
||||||
|
:checked="checkID === 2"
|
||||||
@change="handleTabChange"
|
@change="handleTabChange"
|
||||||
/>
|
/>
|
||||||
<VideoIcon class="icon" />
|
<VideoIcon class="icon" />
|
||||||
@@ -27,6 +28,18 @@
|
|||||||
type="radio"
|
type="radio"
|
||||||
name="function-bar"
|
name="function-bar"
|
||||||
id="3"
|
id="3"
|
||||||
|
:checked="checkID === 3"
|
||||||
|
@change="handleTabChange"
|
||||||
|
/>
|
||||||
|
<Monitor class="icon" />
|
||||||
|
HDMI视频流
|
||||||
|
</label>
|
||||||
|
<label class="tab">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="function-bar"
|
||||||
|
id="4"
|
||||||
|
:checked="checkID === 4"
|
||||||
@change="handleTabChange"
|
@change="handleTabChange"
|
||||||
/>
|
/>
|
||||||
<SquareActivityIcon class="icon" />
|
<SquareActivityIcon class="icon" />
|
||||||
@@ -36,12 +49,24 @@
|
|||||||
<input
|
<input
|
||||||
type="radio"
|
type="radio"
|
||||||
name="function-bar"
|
name="function-bar"
|
||||||
id="4"
|
id="5"
|
||||||
|
:checked="checkID === 5"
|
||||||
@change="handleTabChange"
|
@change="handleTabChange"
|
||||||
/>
|
/>
|
||||||
<Zap class="icon" />
|
<Binary class="icon" />
|
||||||
逻辑分析仪
|
逻辑分析仪
|
||||||
</label>
|
</label>
|
||||||
|
<label class="tab">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="function-bar"
|
||||||
|
id="6"
|
||||||
|
:checked="checkID === 6"
|
||||||
|
@change="handleTabChange"
|
||||||
|
/>
|
||||||
|
<Hand class="icon" />
|
||||||
|
嵌入式逻辑分析仪
|
||||||
|
</label>
|
||||||
<!-- 全屏按钮 -->
|
<!-- 全屏按钮 -->
|
||||||
<button
|
<button
|
||||||
class="fullscreen-btn ml-auto btn btn-ghost btn-sm"
|
class="fullscreen-btn ml-auto btn btn-ghost btn-sm"
|
||||||
@@ -59,11 +84,17 @@
|
|||||||
<VideoStreamView />
|
<VideoStreamView />
|
||||||
</div>
|
</div>
|
||||||
<div v-else-if="checkID === 3" class="h-full overflow-y-auto">
|
<div v-else-if="checkID === 3" class="h-full overflow-y-auto">
|
||||||
<OscilloscopeView />
|
<HdmiVideoStreamView />
|
||||||
</div>
|
</div>
|
||||||
<div v-else-if="checkID === 4" class="h-full overflow-y-auto">
|
<div v-else-if="checkID === 4" class="h-full overflow-y-auto">
|
||||||
|
<OscilloscopeView />
|
||||||
|
</div>
|
||||||
|
<div v-else-if="checkID === 5" class="h-full overflow-y-auto">
|
||||||
<LogicAnalyzerView />
|
<LogicAnalyzerView />
|
||||||
</div>
|
</div>
|
||||||
|
<div v-else-if="checkID === 6" class="h-full overflow-y-auto">
|
||||||
|
<Debugger />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -75,14 +106,23 @@ import {
|
|||||||
TerminalIcon,
|
TerminalIcon,
|
||||||
MaximizeIcon,
|
MaximizeIcon,
|
||||||
MinimizeIcon,
|
MinimizeIcon,
|
||||||
Zap,
|
Binary,
|
||||||
|
Hand,
|
||||||
|
Monitor,
|
||||||
} from "lucide-vue-next";
|
} from "lucide-vue-next";
|
||||||
import { useLocalStorage } from "@vueuse/core";
|
import { useLocalStorage } from "@vueuse/core";
|
||||||
import VideoStreamView from "@/views/Project/VideoStream.vue";
|
import VideoStreamView from "@/views/Project/VideoStream.vue";
|
||||||
|
import HdmiVideoStreamView from "@/views/Project/HdmiVideoStream.vue";
|
||||||
import OscilloscopeView from "@/views/Project/Oscilloscope.vue";
|
import OscilloscopeView from "@/views/Project/Oscilloscope.vue";
|
||||||
import LogicAnalyzerView from "@/views/Project/LogicAnalyzer.vue";
|
import LogicAnalyzerView from "@/views/Project/LogicAnalyzer.vue";
|
||||||
import { isNull, toNumber } from "lodash";
|
import { isNull, toNumber } from "lodash";
|
||||||
import { onMounted, ref, watch } from "vue";
|
import { onMounted, ref, watch } from "vue";
|
||||||
|
import Debugger from "./Debugger.vue";
|
||||||
|
import { useProvideLogicAnalyzer } from "@/components/LogicAnalyzer";
|
||||||
|
import { useProvideOscilloscope } from "@/components/Oscilloscope/OscilloscopeManager";
|
||||||
|
|
||||||
|
const analyzer = useProvideLogicAnalyzer();
|
||||||
|
const oscilloscopeManager = useProvideOscilloscope();
|
||||||
|
|
||||||
const checkID = useLocalStorage("checkID", 1);
|
const checkID = useLocalStorage("checkID", 1);
|
||||||
|
|
||||||
|
|||||||
527
src/views/Project/Debugger.vue
Normal file
527
src/views/Project/Debugger.vue
Normal file
@@ -0,0 +1,527 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<div class="card m-5 bg-base-200 shadow-2xl">
|
||||||
|
<div class="card-body">
|
||||||
|
<h2 class="card-title flex justify-between items-center">
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<Zap class="w-5 h-5" />
|
||||||
|
调试器波形捕获
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<button
|
||||||
|
class="btn btn-sm btn-primary"
|
||||||
|
@click="
|
||||||
|
() => {
|
||||||
|
handleDeleteData();
|
||||||
|
startCapture();
|
||||||
|
}
|
||||||
|
"
|
||||||
|
:disabled="!captureData"
|
||||||
|
>
|
||||||
|
重新捕获
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="btn btn-sm btn-error"
|
||||||
|
@click="handleDeleteData"
|
||||||
|
:disabled="!captureData"
|
||||||
|
>
|
||||||
|
清空
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</h2>
|
||||||
|
<WaveformDisplay :data="captureData">
|
||||||
|
<div class="text-center">
|
||||||
|
<h3 class="text-xl font-semibold text-slate-600 mb-2">
|
||||||
|
暂无逻辑分析数据
|
||||||
|
</h3>
|
||||||
|
<p class="text-sm text-slate-500">点击下方按钮开始捕获</p>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
class="group relative px-8 py-3 bg-gradient-to-r text-white font-medium rounded-lg shadow-lg hover:shadow-xl transform hover:scale-105 transition-all duration-200 ease-in-out focus:outline-none focus:ring-4 active:scale-95"
|
||||||
|
:class="{
|
||||||
|
'from-blue-500 to-purple-600 hover:from-blue-600 hover:to-purple-700 focus:ring-blue-300':
|
||||||
|
!isCapturing,
|
||||||
|
'from-red-500 to-red-600 hover:from-red-600 hover:to-red-700 focus:ring-red-300':
|
||||||
|
isCapturing,
|
||||||
|
}"
|
||||||
|
@click="isCapturing ? stopCapture() : startCapture()"
|
||||||
|
>
|
||||||
|
<span class="flex items-center gap-2">
|
||||||
|
<template v-if="isCapturing">
|
||||||
|
<Square class="w-5 h-5" />
|
||||||
|
停止捕获
|
||||||
|
</template>
|
||||||
|
<template v-else>
|
||||||
|
<Play class="w-5 h-5" />
|
||||||
|
开始捕获
|
||||||
|
</template>
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</WaveformDisplay>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- Debugger 通道配置 -->
|
||||||
|
<div class="card m-5 bg-base-200 shadow-2xl">
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="flex justify-between">
|
||||||
|
<h2 class="card-title mb-4">调试器通道配置</h2>
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<button
|
||||||
|
class="btn btn-sm btn-primary"
|
||||||
|
@click="addChannel"
|
||||||
|
:disabled="!configInited"
|
||||||
|
>
|
||||||
|
添加通道
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="btn btn-sm btn-secondary"
|
||||||
|
@click="showConfigDialog = true"
|
||||||
|
>
|
||||||
|
配置
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- 配置未初始化时 -->
|
||||||
|
<div
|
||||||
|
v-if="!configInited"
|
||||||
|
class="flex flex-col items-center justify-center py-10"
|
||||||
|
>
|
||||||
|
<div class="text-lg text-slate-500 mb-4">请先进行调试器基本配置</div>
|
||||||
|
<button class="btn btn-primary" @click="showConfigDialog = true">
|
||||||
|
配置调试器
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="configInited" class="overflow-x-auto flex flex-col gap-10">
|
||||||
|
<!-- 状态概览 -->
|
||||||
|
<div
|
||||||
|
class="stats stats-horizontal bg-base-100 shadow flex justify-between"
|
||||||
|
>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-title">启用端口数</div>
|
||||||
|
<div class="stat-value text-primary">
|
||||||
|
{{ config.totalPortNum }}
|
||||||
|
</div>
|
||||||
|
<div class="stat-desc">每端口最大32线</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-title">最大线宽数</div>
|
||||||
|
<div class="stat-value text-info">
|
||||||
|
{{ config.totalPortNum * 32 }}
|
||||||
|
</div>
|
||||||
|
<div class="stat-desc">启用端口数 × 32</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-title">已用线宽数</div>
|
||||||
|
<div class="stat-value text-success">
|
||||||
|
{{ channels.reduce((sum, ch) => sum + ch.width, 0) }}
|
||||||
|
</div>
|
||||||
|
<div class="stat-desc">所有通道线宽总和</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-title">采样深度</div>
|
||||||
|
<div class="stat-value text-warning">
|
||||||
|
{{ config.captureDepth }}
|
||||||
|
</div>
|
||||||
|
<div class="stat-desc">每通道采样点数</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="stat-title">时钟频率</div>
|
||||||
|
<div class="stat-value text-accent">{{ config.clkFreq }} MHz</div>
|
||||||
|
<div class="stat-desc">采样时钟</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 通道表格 -->
|
||||||
|
<div class="space-y-2">
|
||||||
|
<!-- 表头 -->
|
||||||
|
<div
|
||||||
|
class="grid grid-cols-7 justify-items-center gap-2 p-2 bg-base-300 rounded-lg text-sm font-medium"
|
||||||
|
>
|
||||||
|
<span>名称</span>
|
||||||
|
<span>显示</span>
|
||||||
|
<span>颜色</span>
|
||||||
|
<span>触发模式</span>
|
||||||
|
<span>数据位宽(起始:结尾)</span>
|
||||||
|
<span>父端口编号</span>
|
||||||
|
<span>操作</span>
|
||||||
|
</div>
|
||||||
|
<!-- 通道列表 -->
|
||||||
|
<div
|
||||||
|
v-for="(ch, idx) in channels"
|
||||||
|
:key="idx"
|
||||||
|
class="grid grid-cols-7 place-items-center gap-4 p-4 bg-base-200 rounded-lg hover:bg-base-300 transition-colors"
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
v-model="ch.name"
|
||||||
|
class="input input-bordered w-full"
|
||||||
|
:placeholder="`通道${idx + 1}`"
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
v-model="ch.visible"
|
||||||
|
class="toggle toggle-primary"
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
type="color"
|
||||||
|
v-model="ch.color"
|
||||||
|
class="w-8 h-8 rounded border-2 border-base-300 cursor-pointer"
|
||||||
|
/>
|
||||||
|
<select
|
||||||
|
v-model="ch.trigger"
|
||||||
|
class="select select-bordered w-full"
|
||||||
|
>
|
||||||
|
<option
|
||||||
|
v-for="mode in triggerModes"
|
||||||
|
:key="mode.value"
|
||||||
|
:value="mode.value"
|
||||||
|
>
|
||||||
|
{{ mode.label }}
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
<input
|
||||||
|
v-model="ch.widthStr"
|
||||||
|
class="input input-bordered w-full"
|
||||||
|
placeholder="如0:7"
|
||||||
|
@change="parseWidthStr(idx)"
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min="0"
|
||||||
|
:max="config.totalPortNum - 1"
|
||||||
|
v-model.number="ch.parentPort"
|
||||||
|
class="input input-bordered w-full"
|
||||||
|
/>
|
||||||
|
<button class="btn btn-error" @click="removeChannel(idx)">
|
||||||
|
删除
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<!-- 添加通道按钮 -->
|
||||||
|
<div class="flex justify-center mt-2">
|
||||||
|
<button class="btn btn-primary w-100" @click="addChannel">
|
||||||
|
添加通道
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 配置Dialog -->
|
||||||
|
<dialog v-if="showConfigDialog" class="modal modal-open">
|
||||||
|
<form
|
||||||
|
method="dialog"
|
||||||
|
class="modal-box max-w-fit"
|
||||||
|
@submit.prevent="onConfigSubmit"
|
||||||
|
>
|
||||||
|
<h3 class="font-bold text-lg mb-4">调试器基本配置</h3>
|
||||||
|
<div class="flex flex-col gap-4 w-80">
|
||||||
|
<BaseInputField
|
||||||
|
v-model="config.clkFreq"
|
||||||
|
label="时钟频率 (MHz)"
|
||||||
|
type="number"
|
||||||
|
min="1"
|
||||||
|
max="200"
|
||||||
|
:error="
|
||||||
|
config.clkFreq < 1 || config.clkFreq > 500 ? '范围1~200' : ''
|
||||||
|
"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<BaseInputField
|
||||||
|
v-model="config.totalPortNum"
|
||||||
|
label="启用端口数"
|
||||||
|
type="number"
|
||||||
|
min="1"
|
||||||
|
max="16"
|
||||||
|
:error="
|
||||||
|
config.totalPortNum < 1 || config.totalPortNum > 16
|
||||||
|
? '范围1~16'
|
||||||
|
: ''
|
||||||
|
"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<BaseInputField
|
||||||
|
v-model="config.captureDepth"
|
||||||
|
label="采样深度"
|
||||||
|
type="number"
|
||||||
|
min="1"
|
||||||
|
max="1048576"
|
||||||
|
:error="
|
||||||
|
config.captureDepth < 1 || config.captureDepth > 1048576
|
||||||
|
? '范围1~1048576'
|
||||||
|
: ''
|
||||||
|
"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="modal-action mt-6">
|
||||||
|
<button class="btn btn-primary" type="submit">确定</button>
|
||||||
|
<button class="btn" type="button" @click="showConfigDialog = false">
|
||||||
|
取消
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</dialog>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { CaptureMode, ChannelConfig, DebuggerConfig } from "@/APIClient";
|
||||||
|
import { useAlertStore } from "@/components/Alert";
|
||||||
|
import BaseInputField from "@/components/InputField/BaseInputField.vue";
|
||||||
|
import type { LogicDataType } from "@/components/WaveformDisplay";
|
||||||
|
import WaveformDisplay from "@/components/WaveformDisplay/WaveformDisplay.vue";
|
||||||
|
import { AuthManager } from "@/utils/AuthManager";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
import { useLocalStorage } from "@vueuse/core";
|
||||||
|
import axios, { type CancelTokenSource } from "axios";
|
||||||
|
import { isNull } from "lodash";
|
||||||
|
import { Play, Square, Zap } from "lucide-vue-next";
|
||||||
|
import { ref, reactive, computed } from "vue";
|
||||||
|
|
||||||
|
interface DebugChannel {
|
||||||
|
name: string;
|
||||||
|
visible: boolean;
|
||||||
|
color: string;
|
||||||
|
trigger: CaptureMode;
|
||||||
|
width: number;
|
||||||
|
widthStr: string;
|
||||||
|
start: number;
|
||||||
|
parentPort: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DebuggerSettings {
|
||||||
|
clkFreq: number;
|
||||||
|
totalPortNum: number;
|
||||||
|
captureDepth: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const triggerModes = [
|
||||||
|
{ value: CaptureMode.None, label: "x (无关)" },
|
||||||
|
{ value: CaptureMode.Logic0, label: "0 (低电平)" },
|
||||||
|
{ value: CaptureMode.Logic1, label: "1 (高电平)" },
|
||||||
|
{ value: CaptureMode.Rise, label: "↑ (上升沿)" },
|
||||||
|
{ value: CaptureMode.Fall, label: "↓ (下降沿)" },
|
||||||
|
];
|
||||||
|
|
||||||
|
// 基本配置
|
||||||
|
const config = reactive<DebuggerSettings>({
|
||||||
|
clkFreq: 50,
|
||||||
|
totalPortNum: 1,
|
||||||
|
captureDepth: 1024,
|
||||||
|
});
|
||||||
|
const configInited = ref(false);
|
||||||
|
const showConfigDialog = ref(false);
|
||||||
|
|
||||||
|
function onConfigSubmit() {
|
||||||
|
configInited.value = true;
|
||||||
|
showConfigDialog.value = false;
|
||||||
|
// 清空通道
|
||||||
|
channels.value = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通道配置
|
||||||
|
const channels = useLocalStorage<DebugChannel[]>("debugger-channels", []);
|
||||||
|
const captureData = ref<LogicDataType>();
|
||||||
|
const alert = useRequiredInjection(useAlertStore);
|
||||||
|
|
||||||
|
const isCapturing = ref(false);
|
||||||
|
const readCancelTokenSource = ref<CancelTokenSource | null>(null);
|
||||||
|
|
||||||
|
// 解析widthStr为start/width
|
||||||
|
function parseWidthStr(idx: number) {
|
||||||
|
const ch = channels.value[idx];
|
||||||
|
const match = /^(\d+)\s*:\s*(\d+)$/.exec(ch.widthStr);
|
||||||
|
if (isNull(match)) {
|
||||||
|
alert.error("格式错误,应为 起始位:宽度,如 0:7");
|
||||||
|
ch.widthStr = `${ch.start}:${ch.width}`;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const min = Math.min(parseInt(match[1]), parseInt(match[2]));
|
||||||
|
const max = Math.max(parseInt(match[1]), parseInt(match[2]));
|
||||||
|
|
||||||
|
ch.start = min;
|
||||||
|
ch.width = max - min + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function addChannel() {
|
||||||
|
if (!configInited.value) {
|
||||||
|
alert.error("请先配置调试器基本参数");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (channels.value.length >= config.totalPortNum * 32) {
|
||||||
|
alert.error("通道数已达最大线宽数");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
channels.value.push({
|
||||||
|
name: `CH${channels.value.length + 1}`,
|
||||||
|
visible: true,
|
||||||
|
color: "#00bcd4",
|
||||||
|
trigger: CaptureMode.None,
|
||||||
|
width: 1,
|
||||||
|
widthStr: "0:0",
|
||||||
|
start: 0,
|
||||||
|
parentPort: 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeChannel(idx: number) {
|
||||||
|
channels.value.splice(idx, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleDeleteData() {
|
||||||
|
captureData.value = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopCapture() {
|
||||||
|
isCapturing.value = false;
|
||||||
|
if (readCancelTokenSource.value) {
|
||||||
|
readCancelTokenSource.value.cancel("用户手动停止捕获");
|
||||||
|
readCancelTokenSource.value = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function startCapture() {
|
||||||
|
if (!configInited.value) {
|
||||||
|
alert.error("请先配置调试器基本参数");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (channels.value.length === 0) {
|
||||||
|
alert.error("请至少添加一个通道");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验通道参数
|
||||||
|
let usedWires = 0;
|
||||||
|
for (let i = 0; i < channels.value.length; i++) {
|
||||||
|
const ch = channels.value[i];
|
||||||
|
if (!ch.visible) continue;
|
||||||
|
if (!ch.name) {
|
||||||
|
alert.error(`通道 ${i + 1} 名称不能为空`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ch.width < 1 || ch.width > 32) {
|
||||||
|
alert.error(`通道 ${i + 1} 数据位宽必须在1到32之间`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ch.start < 0 || ch.start + ch.width > 32) {
|
||||||
|
alert.error(`通道 ${i + 1} 起始位+宽度不能超过32`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ch.parentPort < 0 || ch.parentPort >= config.totalPortNum) {
|
||||||
|
alert.error(`通道 ${i + 1} 父端口编号超出范围`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
usedWires += ch.width;
|
||||||
|
}
|
||||||
|
if (usedWires > config.totalPortNum * 32) {
|
||||||
|
alert.error("所有通道线宽总和不能超过最大线宽数");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
isCapturing.value = true;
|
||||||
|
const client = AuthManager.createAuthenticatedDebuggerClient();
|
||||||
|
|
||||||
|
// 构造API配置
|
||||||
|
const channelConfigs = channels.value
|
||||||
|
.filter((ch) => ch.visible)
|
||||||
|
.map(
|
||||||
|
(ch) =>
|
||||||
|
new ChannelConfig({
|
||||||
|
name: ch.name,
|
||||||
|
color: ch.color,
|
||||||
|
wireWidth: ch.width,
|
||||||
|
wireStartIndex: ch.start,
|
||||||
|
parentPort: ch.parentPort,
|
||||||
|
mode: ch.trigger,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const apiConfig = new DebuggerConfig({
|
||||||
|
clkFreq: config.clkFreq,
|
||||||
|
totalPortNum: config.totalPortNum,
|
||||||
|
captureDepth: config.captureDepth,
|
||||||
|
triggerNum: 0,
|
||||||
|
channelConfigs: channelConfigs,
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 设置通道模式
|
||||||
|
let ret = await client.setChannelsMode(apiConfig);
|
||||||
|
if (!ret) {
|
||||||
|
alert.error("设置通道模式失败");
|
||||||
|
isCapturing.value = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 启动捕获
|
||||||
|
ret = await client.startTrigger();
|
||||||
|
if (!ret) {
|
||||||
|
alert.error("开始捕获失败,请检查连接");
|
||||||
|
isCapturing.value = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取数据
|
||||||
|
readCancelTokenSource.value = axios.CancelToken.source();
|
||||||
|
const readDataPromise = client
|
||||||
|
.readData(apiConfig, readCancelTokenSource.value.token)
|
||||||
|
.then((data) => {
|
||||||
|
const enabledChannels = channelConfigs;
|
||||||
|
const sampleCount = config.captureDepth;
|
||||||
|
|
||||||
|
// 解析数据
|
||||||
|
const y = data.map((cd, idx) => {
|
||||||
|
const ch = enabledChannels[idx];
|
||||||
|
const bin = atob(cd.data);
|
||||||
|
// UInt32数组
|
||||||
|
const arr = [];
|
||||||
|
for (let i = 0; i < bin.length; i += 4) {
|
||||||
|
arr.push(
|
||||||
|
bin.charCodeAt(i) |
|
||||||
|
(bin.charCodeAt(i + 1) << 8) |
|
||||||
|
(bin.charCodeAt(i + 2) << 16) |
|
||||||
|
(bin.charCodeAt(i + 3) << 24),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// 截取采样深度
|
||||||
|
return {
|
||||||
|
enabled: true,
|
||||||
|
type: ch.wireWidth === 1 ? ("logic" as const) : ("number" as const),
|
||||||
|
name: ch.name,
|
||||||
|
color: ch.color,
|
||||||
|
value: arr.slice(0, sampleCount),
|
||||||
|
base: ch.wireWidth === 1 ? ("bin" as const) : ("hex" as const),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const x: number[] = [];
|
||||||
|
for (let i = 0; i < sampleCount; i++) {
|
||||||
|
x.push(i * (1 / config.clkFreq)); // us
|
||||||
|
}
|
||||||
|
|
||||||
|
captureData.value = {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
xUnit: "us",
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
if (axios.isCancel(error)) {
|
||||||
|
alert.info("捕获已取消");
|
||||||
|
} else {
|
||||||
|
alert.error(`读取数据失败: ${error.message}`);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
isCapturing.value = false;
|
||||||
|
readCancelTokenSource.value = null;
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
alert.error(`开始捕获失败: ${error.message}`);
|
||||||
|
isCapturing.value = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
490
src/views/Project/HdmiVideoStream.vue
Normal file
490
src/views/Project/HdmiVideoStream.vue
Normal file
@@ -0,0 +1,490 @@
|
|||||||
|
<template>
|
||||||
|
<div class="bg-base-100 flex flex-col gap-7">
|
||||||
|
<!-- 控制面板 -->
|
||||||
|
<div class="card bg-base-200 shadow-xl mx-5">
|
||||||
|
<div class="card-body">
|
||||||
|
<h2 class="card-title text-primary">
|
||||||
|
<Settings class="w-6 h-6" />
|
||||||
|
HDMI视频流控制面板
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||||
|
<!-- 板卡信息 -->
|
||||||
|
<div class="stats shadow">
|
||||||
|
<div class="stat bg-base-100">
|
||||||
|
<div class="stat-figure text-primary">
|
||||||
|
<div class="badge" :class="endpoint ? 'badge-success' : 'badge-warning'">
|
||||||
|
{{ endpoint ? "已连接" : "未配置" }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat-title">板卡状态</div>
|
||||||
|
<div class="stat-value text-primary">HDMI</div>
|
||||||
|
<div class="stat-desc">{{ endpoint ? `板卡: ${endpoint.boardId.substring(0, 8)}...` : "请先连接板卡" }}</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">
|
||||||
|
{{ isPlaying ? "播放中" : "未播放" }}
|
||||||
|
</div>
|
||||||
|
<div class="stat-desc">{{ videoStatus }}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 操作按钮 -->
|
||||||
|
<div class="card-actions justify-end mt-4">
|
||||||
|
<button class="btn btn-outline btn-primary" @click="refreshEndpoint" :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 || !endpoint">
|
||||||
|
<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 mx-5">
|
||||||
|
<div class="card-body">
|
||||||
|
<h2 class="card-title text-primary">
|
||||||
|
<Video class="w-6 h-6" />
|
||||||
|
HDMI视频预览
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<div class="relative bg-black rounded-lg overflow-hidden cursor-pointer" :class="[
|
||||||
|
{ 'cursor-not-allowed': !isPlaying || hasVideoError || !endpoint }
|
||||||
|
]" style="aspect-ratio: 16/9" @click="handleVideoClick">
|
||||||
|
<!-- 视频播放器 - 使用img标签直接显示MJPEG流 -->
|
||||||
|
<div v-show="isPlaying && endpoint" class="w-full h-full flex items-center justify-center">
|
||||||
|
<img :src="currentVideoSource" alt="HDMI视频流" 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" />
|
||||||
|
HDMI视频流加载失败
|
||||||
|
</h3>
|
||||||
|
<p>无法连接到HDMI视频服务器,请检查以下内容:</p>
|
||||||
|
<ul class="list-disc list-inside">
|
||||||
|
<li>HDMI输入设备是否已连接</li>
|
||||||
|
<li>板卡是否正常工作</li>
|
||||||
|
<li>网络连接是否正常</li>
|
||||||
|
<li>HDMI视频流服务是否已启动</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) || !endpoint"
|
||||||
|
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">
|
||||||
|
{{ endpoint ? '点击"播放HDMI视频流"按钮开始查看实时视频' : '请先刷新连接以获取板卡信息' }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 视频控制 -->
|
||||||
|
<div class="flex justify-between items-center mt-4" v-if="endpoint">
|
||||||
|
<div class="text-sm text-base-content/70">
|
||||||
|
MJPEG地址:
|
||||||
|
<code class="bg-base-300 px-2 py-1 rounded text-xs">{{
|
||||||
|
endpoint.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(endpoint.videoUrl)">
|
||||||
|
<ExternalLink class="w-4 h-4" />
|
||||||
|
在新标签打开视频页面
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a @click="takeSnapshot">
|
||||||
|
<Camera class="w-4 h-4" />
|
||||||
|
获取并下载快照
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a @click="copyToClipboard(endpoint.mjpegUrl)">
|
||||||
|
<Copy class="w-4 h-4" />
|
||||||
|
复制MJPEG地址
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-success btn-sm" @click="startStream" :disabled="isPlaying || !endpoint">
|
||||||
|
<Play class="w-4 h-4 mr-1" />
|
||||||
|
播放HDMI视频流
|
||||||
|
</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 mx-5">
|
||||||
|
<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 {
|
||||||
|
Settings,
|
||||||
|
Video,
|
||||||
|
RefreshCw,
|
||||||
|
TestTube,
|
||||||
|
Play,
|
||||||
|
Square,
|
||||||
|
ExternalLink,
|
||||||
|
Camera,
|
||||||
|
Copy,
|
||||||
|
FileText,
|
||||||
|
AlertTriangle,
|
||||||
|
MoreHorizontal,
|
||||||
|
} from "lucide-vue-next";
|
||||||
|
import { HdmiVideoStreamClient, type HdmiVideoStreamEndpoint } from "@/APIClient";
|
||||||
|
import { AuthManager } from "@/utils/AuthManager";
|
||||||
|
import { useAlertStore } from "@/components/Alert";
|
||||||
|
|
||||||
|
// Alert系统
|
||||||
|
const alert = useAlertStore();
|
||||||
|
|
||||||
|
// 状态管理
|
||||||
|
const loading = ref(false);
|
||||||
|
const testing = ref(false);
|
||||||
|
const isPlaying = ref(false);
|
||||||
|
const hasVideoError = ref(false);
|
||||||
|
const videoStatus = ref('未连接');
|
||||||
|
|
||||||
|
// HDMI视频流数据
|
||||||
|
const endpoint = ref<HdmiVideoStreamEndpoint | null>(null);
|
||||||
|
const currentVideoSource = ref('');
|
||||||
|
|
||||||
|
// 日志系统
|
||||||
|
interface LogEntry {
|
||||||
|
time: Date;
|
||||||
|
level: 'info' | 'success' | 'warning' | 'error';
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const logs = ref<LogEntry[]>([]);
|
||||||
|
|
||||||
|
// 添加日志
|
||||||
|
function addLog(level: LogEntry['level'], message: string) {
|
||||||
|
logs.value.unshift({
|
||||||
|
time: new Date(),
|
||||||
|
level,
|
||||||
|
message
|
||||||
|
});
|
||||||
|
|
||||||
|
// 保持最近100条日志
|
||||||
|
if (logs.value.length > 100) {
|
||||||
|
logs.value = logs.value.slice(0, 100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 格式化时间
|
||||||
|
function formatTime(date: Date): string {
|
||||||
|
return date.toLocaleTimeString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取日志样式类
|
||||||
|
function getLogClass(level: LogEntry['level']): string {
|
||||||
|
switch (level) {
|
||||||
|
case 'success':
|
||||||
|
return 'text-success';
|
||||||
|
case 'warning':
|
||||||
|
return 'text-warning';
|
||||||
|
case 'error':
|
||||||
|
return 'text-error';
|
||||||
|
default:
|
||||||
|
return 'text-base-content';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清空日志
|
||||||
|
function clearLogs() {
|
||||||
|
logs.value = [];
|
||||||
|
addLog('info', '日志已清空');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 刷新HDMI视频流端点
|
||||||
|
async function refreshEndpoint() {
|
||||||
|
loading.value = true;
|
||||||
|
try {
|
||||||
|
addLog('info', '正在获取HDMI视频流端点...');
|
||||||
|
|
||||||
|
const client = AuthManager.createAuthenticatedHdmiVideoStreamClient();
|
||||||
|
const result = await client.getMyEndpoint();
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
endpoint.value = result;
|
||||||
|
videoStatus.value = '已连接板卡,可以播放视频流';
|
||||||
|
addLog('success', `成功获取HDMI视频流端点,板卡ID: ${result.boardId.substring(0, 8)}...`);
|
||||||
|
alert?.success('HDMI视频流连接成功');
|
||||||
|
} else {
|
||||||
|
endpoint.value = null;
|
||||||
|
videoStatus.value = '无法获取板卡信息';
|
||||||
|
addLog('error', '未找到绑定的板卡或板卡未配置HDMI输入');
|
||||||
|
alert?.error('未找到绑定的板卡');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取HDMI视频流端点失败:', error);
|
||||||
|
endpoint.value = null;
|
||||||
|
videoStatus.value = '连接失败';
|
||||||
|
addLog('error', `获取HDMI视频流端点失败: ${error}`);
|
||||||
|
alert?.error('获取HDMI视频流信息失败');
|
||||||
|
} finally {
|
||||||
|
loading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试连接
|
||||||
|
async function testConnection() {
|
||||||
|
if (!endpoint.value) {
|
||||||
|
alert?.warn('请先刷新连接获取板卡信息');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
testing.value = true;
|
||||||
|
try {
|
||||||
|
addLog('info', '正在测试HDMI视频流连接...');
|
||||||
|
|
||||||
|
// 尝试获取快照来测试连接
|
||||||
|
const response = await fetch(endpoint.value.snapshotUrl, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Cache-Control': 'no-cache'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
addLog('success', 'HDMI视频流连接测试成功');
|
||||||
|
alert?.success('HDMI连接测试成功');
|
||||||
|
videoStatus.value = '连接正常,可以播放视频流';
|
||||||
|
} else {
|
||||||
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('HDMI视频流连接测试失败:', error);
|
||||||
|
addLog('error', `连接测试失败: ${error}`);
|
||||||
|
alert?.error('HDMI连接测试失败');
|
||||||
|
videoStatus.value = '连接测试失败';
|
||||||
|
} finally {
|
||||||
|
testing.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始播放视频流
|
||||||
|
function startStream() {
|
||||||
|
if (!endpoint.value) {
|
||||||
|
alert?.warn('请先刷新连接获取板卡信息');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 添加时间戳防止缓存
|
||||||
|
const timestamp = new Date().getTime();
|
||||||
|
currentVideoSource.value = `${endpoint.value.mjpegUrl}&t=${timestamp}`;
|
||||||
|
isPlaying.value = true;
|
||||||
|
hasVideoError.value = false;
|
||||||
|
videoStatus.value = '正在加载视频流...';
|
||||||
|
|
||||||
|
addLog('info', '开始播放HDMI视频流');
|
||||||
|
alert?.success('开始播放HDMI视频流');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('启动HDMI视频流失败:', error);
|
||||||
|
addLog('error', `启动视频流失败: ${error}`);
|
||||||
|
alert?.error('启动HDMI视频流失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止播放视频流
|
||||||
|
function stopStream() {
|
||||||
|
isPlaying.value = false;
|
||||||
|
currentVideoSource.value = '';
|
||||||
|
videoStatus.value = '已停止播放';
|
||||||
|
|
||||||
|
const client = AuthManager.createAuthenticatedHdmiVideoStreamClient();
|
||||||
|
client.disableHdmiTransmission();
|
||||||
|
|
||||||
|
addLog('info', '停止播放HDMI视频流');
|
||||||
|
alert?.info('已停止播放HDMI视频流');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理视频加载错误
|
||||||
|
function handleVideoError() {
|
||||||
|
hasVideoError.value = true;
|
||||||
|
videoStatus.value = '视频流加载失败';
|
||||||
|
addLog('error', 'HDMI视频流加载失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理视频加载成功
|
||||||
|
function handleVideoLoad() {
|
||||||
|
hasVideoError.value = false;
|
||||||
|
videoStatus.value = '视频流播放中';
|
||||||
|
addLog('success', 'HDMI视频流加载成功');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理视频点击
|
||||||
|
function handleVideoClick() {
|
||||||
|
if (!isPlaying.value || hasVideoError.value || !endpoint.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 可以在这里添加点击视频的交互逻辑
|
||||||
|
addLog('info', '视频画面被点击');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重试连接
|
||||||
|
function tryReconnect() {
|
||||||
|
hasVideoError.value = false;
|
||||||
|
if (endpoint.value) {
|
||||||
|
startStream();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 在新标签页打开视频
|
||||||
|
function openInNewTab(url: string) {
|
||||||
|
window.open(url, '_blank');
|
||||||
|
addLog('info', '在新标签页打开HDMI视频页面');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取快照
|
||||||
|
async function takeSnapshot() {
|
||||||
|
if (!endpoint.value) {
|
||||||
|
alert?.warn('请先刷新连接获取板卡信息');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
addLog('info', '正在获取HDMI视频快照...');
|
||||||
|
|
||||||
|
const response = await fetch(endpoint.value.snapshotUrl, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Cache-Control': 'no-cache'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const blob = await response.blob();
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
|
a.download = `hdmi_snapshot_${new Date().toISOString().replace(/:/g, '-')}.jpg`;
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
document.body.removeChild(a);
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
|
||||||
|
addLog('success', '快照下载成功');
|
||||||
|
alert?.success('HDMI快照下载成功');
|
||||||
|
} else {
|
||||||
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取HDMI快照失败:', error);
|
||||||
|
addLog('error', `获取快照失败: ${error}`);
|
||||||
|
alert?.error('获取HDMI快照失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 复制到剪贴板
|
||||||
|
async function copyToClipboard(text: string) {
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(text);
|
||||||
|
addLog('success', '地址已复制到剪贴板');
|
||||||
|
alert?.success('地址已复制到剪贴板');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('复制到剪贴板失败:', error);
|
||||||
|
addLog('error', '复制到剪贴板失败');
|
||||||
|
alert?.error('复制到剪贴板失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 组件挂载时初始化
|
||||||
|
onMounted(() => {
|
||||||
|
addLog('info', 'HDMI视频流界面已初始化');
|
||||||
|
refreshEndpoint();
|
||||||
|
});
|
||||||
|
|
||||||
|
// 组件卸载时清理
|
||||||
|
onUnmounted(() => {
|
||||||
|
stopStream();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
/* 对焦动画效果 */
|
||||||
|
@keyframes focus-pulse {
|
||||||
|
0%, 100% {
|
||||||
|
box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.7);
|
||||||
|
}
|
||||||
|
50% {
|
||||||
|
box-shadow: 0 0 0 10px rgba(59, 130, 246, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.focus-animation {
|
||||||
|
animation: focus-pulse 1s ease-out;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -29,6 +29,7 @@
|
|||||||
<DiagramCanvas
|
<DiagramCanvas
|
||||||
ref="diagramCanvas"
|
ref="diagramCanvas"
|
||||||
:showDocPanel="showDocPanel"
|
:showDocPanel="showDocPanel"
|
||||||
|
:exam-id="(route.query.examId as string) || ''"
|
||||||
@open-components="openComponentsMenu"
|
@open-components="openComponentsMenu"
|
||||||
@toggle-doc-panel="toggleDocPanel"
|
@toggle-doc-panel="toggleDocPanel"
|
||||||
/>
|
/>
|
||||||
@@ -36,13 +37,13 @@
|
|||||||
<!-- 拖拽分割线 -->
|
<!-- 拖拽分割线 -->
|
||||||
<SplitterResizeHandle
|
<SplitterResizeHandle
|
||||||
id="splitter-group-h-resize-handle"
|
id="splitter-group-h-resize-handle"
|
||||||
class="w-2 bg-base-100 hover:bg-primary hover:opacity-70 transition-colors"
|
class="w-1 bg-base-300"
|
||||||
/>
|
/>
|
||||||
<!-- 右侧编辑区域 -->
|
<!-- 右侧编辑区域 -->
|
||||||
<SplitterPanel
|
<SplitterPanel
|
||||||
id="splitter-group-h-panel-properties"
|
id="splitter-group-h-panel-properties"
|
||||||
:min-size="20"
|
:min-size="20"
|
||||||
class="bg-base-200 h-full overflow-hidden flex flex-col"
|
class="bg-base-100 h-full overflow-hidden flex flex-col"
|
||||||
>
|
>
|
||||||
<div class="overflow-y-auto flex-1">
|
<div class="overflow-y-auto flex-1">
|
||||||
<!-- 使用条件渲染显示不同的面板 -->
|
<!-- 使用条件渲染显示不同的面板 -->
|
||||||
@@ -59,7 +60,10 @@
|
|||||||
v-show="showDocPanel"
|
v-show="showDocPanel"
|
||||||
class="doc-panel overflow-y-auto h-full"
|
class="doc-panel overflow-y-auto h-full"
|
||||||
>
|
>
|
||||||
<MarkdownRenderer :content="documentContent" />
|
<MarkdownRenderer
|
||||||
|
:content="documentContent"
|
||||||
|
:examId="(route.query.examId as string) || ''"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</SplitterPanel>
|
</SplitterPanel>
|
||||||
@@ -70,7 +74,7 @@
|
|||||||
<SplitterResizeHandle
|
<SplitterResizeHandle
|
||||||
v-show="!isBottomBarFullscreen"
|
v-show="!isBottomBarFullscreen"
|
||||||
id="splitter-group-v-resize-handle"
|
id="splitter-group-v-resize-handle"
|
||||||
class="h-2 bg-base-100 hover:bg-primary hover:opacity-70 transition-colors"
|
class="h-1 bg-base-300"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<!-- 功能底栏 -->
|
<!-- 功能底栏 -->
|
||||||
@@ -78,7 +82,7 @@
|
|||||||
id="splitter-group-v-panel-bar"
|
id="splitter-group-v-panel-bar"
|
||||||
:default-size="isBottomBarFullscreen ? 100 : (100 - verticalSplitterSize)"
|
:default-size="isBottomBarFullscreen ? 100 : (100 - verticalSplitterSize)"
|
||||||
:min-size="isBottomBarFullscreen ? 100 : 15"
|
:min-size="isBottomBarFullscreen ? 100 : 15"
|
||||||
class="w-full overflow-hidden px-5 pt-3"
|
class="w-full overflow-hidden pt-3"
|
||||||
>
|
>
|
||||||
<BottomBar
|
<BottomBar
|
||||||
:isFullscreen="isBottomBarFullscreen"
|
:isFullscreen="isBottomBarFullscreen"
|
||||||
@@ -100,11 +104,32 @@
|
|||||||
@close="handleRequestBoardClose"
|
@close="handleRequestBoardClose"
|
||||||
@success="handleRequestBoardSuccess"
|
@success="handleRequestBoardSuccess"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<!-- Navbar切换浮动按钮 -->
|
||||||
|
<div
|
||||||
|
class="navbar-toggle-btn"
|
||||||
|
:class="{ 'with-navbar': navbarControl.showNavbar.value }"
|
||||||
|
>
|
||||||
|
<button
|
||||||
|
@click="navbarControl.toggleNavbar"
|
||||||
|
class="btn btn-circle btn-primary shadow-lg hover:shadow-xl transition-all duration-300"
|
||||||
|
:class="{ 'btn-outline': navbarControl.showNavbar.value }"
|
||||||
|
:title="navbarControl.showNavbar.value ? '隐藏顶部导航栏' : '显示顶部导航栏'"
|
||||||
|
>
|
||||||
|
<!-- 使用SVG图标表示菜单/关闭状态 -->
|
||||||
|
<svg v-if="navbarControl.showNavbar.value" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
|
||||||
|
</svg>
|
||||||
|
<svg v-else xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, onMounted, watch } from "vue";
|
import { ref, onMounted, watch, inject, type Ref } from "vue";
|
||||||
import { useRouter } from "vue-router";
|
import { useRouter } from "vue-router";
|
||||||
import { useLocalStorage } from '@vueuse/core'; // 添加VueUse导入
|
import { useLocalStorage } from '@vueuse/core'; // 添加VueUse导入
|
||||||
import { SplitterGroup, SplitterPanel, SplitterResizeHandle } from "reka-ui";
|
import { SplitterGroup, SplitterPanel, SplitterResizeHandle } from "reka-ui";
|
||||||
@@ -115,7 +140,6 @@ import MarkdownRenderer from "@/components/MarkdownRenderer.vue";
|
|||||||
import BottomBar from "@/views/Project/BottomBar.vue";
|
import BottomBar from "@/views/Project/BottomBar.vue";
|
||||||
import RequestBoardDialog from "@/views/Project/RequestBoardDialog.vue";
|
import RequestBoardDialog from "@/views/Project/RequestBoardDialog.vue";
|
||||||
import { useProvideComponentManager } from "@/components/LabCanvas";
|
import { useProvideComponentManager } from "@/components/LabCanvas";
|
||||||
import type { DiagramData } from "@/components/LabCanvas";
|
|
||||||
import { useAlertStore } from "@/components/Alert";
|
import { useAlertStore } from "@/components/Alert";
|
||||||
import { AuthManager } from "@/utils/AuthManager";
|
import { AuthManager } from "@/utils/AuthManager";
|
||||||
import { useEquipments } from "@/stores/equipments";
|
import { useEquipments } from "@/stores/equipments";
|
||||||
@@ -133,6 +157,12 @@ const equipments = useEquipments();
|
|||||||
|
|
||||||
const alert = useAlertStore();
|
const alert = useAlertStore();
|
||||||
|
|
||||||
|
// --- Navbar控制 ---
|
||||||
|
const navbarControl = inject('navbar') as {
|
||||||
|
showNavbar: Ref<boolean>;
|
||||||
|
toggleNavbar: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
// --- 使用VueUse保存分栏状态 ---
|
// --- 使用VueUse保存分栏状态 ---
|
||||||
// 左右分栏比例(默认60%)
|
// 左右分栏比例(默认60%)
|
||||||
const horizontalSplitterSize = useLocalStorage('project-horizontal-splitter-size', 60);
|
const horizontalSplitterSize = useLocalStorage('project-horizontal-splitter-size', 60);
|
||||||
@@ -182,29 +212,37 @@ async function toggleDocPanel() {
|
|||||||
// 加载文档内容
|
// 加载文档内容
|
||||||
async function loadDocumentContent() {
|
async function loadDocumentContent() {
|
||||||
try {
|
try {
|
||||||
// 从路由参数中获取教程ID
|
// 检查是否有实验ID参数
|
||||||
const tutorialId = (route.query.tutorial as string) || "02"; // 默认加载02例程
|
const examId = route.query.examId as string;
|
||||||
|
if (examId) {
|
||||||
|
// 如果有实验ID,从API加载实验文档
|
||||||
|
console.log('加载实验文档:', examId);
|
||||||
|
const client = AuthManager.createAuthenticatedResourceClient();
|
||||||
|
|
||||||
// 构建文档路径
|
// 获取markdown类型的模板资源列表
|
||||||
let docPath = `/doc/${tutorialId}/doc.md`;
|
const resources = await client.getResourceList(examId, 'doc', 'template');
|
||||||
|
|
||||||
// 检查当前路径是否包含下划线(例如 02_key 格式)
|
if (resources && resources.length > 0) {
|
||||||
// 如果不包含,那么使用更新的命名格式
|
// 获取第一个markdown资源
|
||||||
if (!tutorialId.includes("_")) {
|
const markdownResource = resources[0];
|
||||||
docPath = `/doc/${tutorialId}/doc.md`;
|
|
||||||
|
// 使用新的ResourceClient API获取资源文件内容
|
||||||
|
const response = await client.getResourceById(markdownResource.id);
|
||||||
|
|
||||||
|
if (!response || !response.data) {
|
||||||
|
throw new Error('获取markdown文件失败');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取文档内容
|
const content = await response.data.text();
|
||||||
const response = await fetch(docPath);
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to load document: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新文档内容,并替换图片路径
|
// 更新文档内容,暂时不处理图片路径,由MarkdownRenderer处理
|
||||||
documentContent.value = (await response.text()).replace(
|
documentContent.value = content;
|
||||||
/.\/images/gi,
|
} else {
|
||||||
`/doc/${tutorialId}/images`,
|
documentContent.value = "# 暂无实验文档\n\n该实验尚未提供文档内容。";
|
||||||
);
|
}
|
||||||
|
} else {
|
||||||
|
documentContent.value = "# 无文档";
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("加载文档失败:", error);
|
console.error("加载文档失败:", error);
|
||||||
documentContent.value = "# 文档加载失败\n\n无法加载请求的文档。";
|
documentContent.value = "# 文档加载失败\n\n无法加载请求的文档。";
|
||||||
@@ -268,8 +306,8 @@ async function checkAndInitializeBoard() {
|
|||||||
|
|
||||||
// 根据实验板信息更新equipment store
|
// 根据实验板信息更新equipment store
|
||||||
function updateEquipmentFromBoard(board: Board) {
|
function updateEquipmentFromBoard(board: Board) {
|
||||||
equipments.setAddr(board.ipAddr);
|
equipments.boardAddr = board.ipAddr;
|
||||||
equipments.setPort(board.port);
|
equipments.boardPort = board.port;
|
||||||
|
|
||||||
console.log(`实验板信息已更新到equipment store:`, {
|
console.log(`实验板信息已更新到equipment store:`, {
|
||||||
address: board.ipAddr,
|
address: board.ipAddr,
|
||||||
@@ -312,8 +350,8 @@ onMounted(async () => {
|
|||||||
// 检查并初始化用户实验板
|
// 检查并初始化用户实验板
|
||||||
await checkAndInitializeBoard();
|
await checkAndInitializeBoard();
|
||||||
|
|
||||||
// 检查是否有例程参数,如果有则自动打开文档面板
|
// 检查是否有例程参数或实验ID参数,如果有则自动打开文档面板
|
||||||
if (route.query.tutorial) {
|
if (route.query.tutorial || route.query.examId) {
|
||||||
showDocPanel.value = true;
|
showDocPanel.value = true;
|
||||||
await loadDocumentContent();
|
await loadDocumentContent();
|
||||||
}
|
}
|
||||||
@@ -344,7 +382,7 @@ onMounted(async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 确保滚动行为仅在需要时出现 */
|
/* 确保整个页面禁止滚动 */
|
||||||
html,
|
html,
|
||||||
body {
|
body {
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
@@ -376,7 +414,42 @@ body {
|
|||||||
:deep(.markdown-content) {
|
:deep(.markdown-content) {
|
||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
background-color: hsl(var(--b1));
|
background-color: hsl(var(--b1));
|
||||||
border-radius: 0.5rem;
|
}
|
||||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
|
|
||||||
|
/* Navbar切换浮动按钮样式 */
|
||||||
|
.navbar-toggle-btn {
|
||||||
|
position: fixed;
|
||||||
|
top: 20px;
|
||||||
|
left: 20px;
|
||||||
|
z-index: 1000;
|
||||||
|
transition: all 0.3s ease-in-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 当Navbar显示时,调整按钮位置 */
|
||||||
|
.navbar-toggle-btn.with-navbar {
|
||||||
|
top: 80px; /* 调整到Navbar下方 */
|
||||||
|
}
|
||||||
|
|
||||||
|
.navbar-toggle-btn button {
|
||||||
|
backdrop-filter: blur(10px);
|
||||||
|
background: rgba(var(--p), 0.9);
|
||||||
|
border: 2px solid rgba(var(--p), 0.3);
|
||||||
|
transition: all 0.3s ease-in-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
.navbar-toggle-btn button:hover {
|
||||||
|
transform: scale(1.1);
|
||||||
|
background: rgba(var(--p), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navbar-toggle-btn button.btn-outline {
|
||||||
|
background: rgba(var(--b1), 0.9);
|
||||||
|
color: hsl(var(--p));
|
||||||
|
border: 2px solid rgba(var(--p), 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navbar-toggle-btn button.btn-outline:hover {
|
||||||
|
background: rgba(var(--p), 0.1);
|
||||||
|
border: 2px solid rgba(var(--p), 0.8);
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -1,42 +1,115 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="bg-base-100 flex flex-col">
|
<div class="bg-base-100 flex flex-col gap-10 mb-5">
|
||||||
<!-- 逻辑信号展示 -->
|
<!-- 逻辑信号展示 -->
|
||||||
<div class="card bg-base-200 shadow-xl">
|
<div class="card bg-base-200 shadow-xl mx-5">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h2 class="card-title">
|
<h2 class="card-title flex justify-between items-center">
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
<Zap class="w-5 h-5" />
|
<Zap class="w-5 h-5" />
|
||||||
逻辑信号分析
|
逻辑信号分析
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<!-- 空闲状态:只显示开始捕获按钮 -->
|
||||||
|
<button
|
||||||
|
v-if="!analyzer.isCapturing.value"
|
||||||
|
@click="analyzer.startCapture"
|
||||||
|
:disabled="analyzer.isApplying.value"
|
||||||
|
class="btn btn-sm btn-primary"
|
||||||
|
>
|
||||||
|
开始捕获
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<!-- 捕获状态:显示停止捕获和强制捕获按钮 -->
|
||||||
|
<button
|
||||||
|
v-if="analyzer.isCapturing.value"
|
||||||
|
@click="analyzer.stopCapture"
|
||||||
|
class="btn btn-sm btn-warning"
|
||||||
|
>
|
||||||
|
<span class="loading loading-spinner loading-sm"></span>
|
||||||
|
停止捕获
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
v-if="analyzer.isCapturing.value"
|
||||||
|
@click="analyzer.forceCapture"
|
||||||
|
class="btn btn-sm btn-secondary"
|
||||||
|
>
|
||||||
|
强制捕获
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<!-- 其他按钮保持不变 -->
|
||||||
|
<button
|
||||||
|
@click="analyzer.generateTestData"
|
||||||
|
class="btn btn-sm btn-info"
|
||||||
|
>
|
||||||
|
测试数据
|
||||||
|
</button>
|
||||||
|
<button class="btn btn-sm btn-error" @click="handleDeleteData">
|
||||||
|
清空数据
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</h2>
|
</h2>
|
||||||
|
<LogicalWaveFormDisplay />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 触发设置 -->
|
<!-- 触发设置 -->
|
||||||
<div class="card bg-base-200 shadow-xl mt-4">
|
<div class="card bg-base-200 shadow-xl mx-5">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h2 class="card-title">
|
<h2 class="card-title flex justify-between items-center">
|
||||||
|
<div class="flex gap-8">
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
<Settings class="w-5 h-5" />
|
<Settings class="w-5 h-5" />
|
||||||
触发设置
|
触发设置
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center gap-4">
|
||||||
|
<!-- 状态指示 -->
|
||||||
|
<div class="flex items-center gap-2 text-sm">
|
||||||
|
<span
|
||||||
|
v-if="analyzer.isCapturing.value"
|
||||||
|
class="flex items-center gap-1 text-warning"
|
||||||
|
>
|
||||||
|
<span class="loading loading-spinner loading-xs"></span>
|
||||||
|
捕获中
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
v-else-if="analyzer.isApplying.value"
|
||||||
|
class="flex items-center gap-1 text-info"
|
||||||
|
>
|
||||||
|
<span class="loading loading-spinner loading-xs"></span>
|
||||||
|
配置中
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
v-else
|
||||||
|
class="text-success"
|
||||||
|
>
|
||||||
|
就绪
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</h2>
|
</h2>
|
||||||
<TriggerSettings />
|
<TriggerSettings />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 通道配置 -->
|
|
||||||
<div class="card bg-base-200 shadow-xl mt-4">
|
|
||||||
<div class="card-body">
|
|
||||||
<h2 class="card-title">
|
|
||||||
<Layers class="w-5 h-5" />
|
|
||||||
通道配置
|
|
||||||
</h2>
|
|
||||||
<ChannelConfig />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { Zap, Settings, Layers } from "lucide-vue-next";
|
import { Zap, Settings, Layers } from "lucide-vue-next";
|
||||||
import { useEquipments } from "@/stores/equipments";
|
import { useEquipments } from "@/stores/equipments";
|
||||||
|
import {
|
||||||
|
LogicalWaveFormDisplay,
|
||||||
|
TriggerSettings,
|
||||||
|
useLogicAnalyzerState,
|
||||||
|
} from "@/components/LogicAnalyzer";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
|
||||||
|
const analyzer = useRequiredInjection(useLogicAnalyzerState);
|
||||||
|
|
||||||
|
|
||||||
|
function handleDeleteData() {
|
||||||
|
analyzer.logicData.value = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
// 使用全局设备配置
|
// 使用全局设备配置
|
||||||
const equipments = useEquipments();
|
const equipments = useEquipments();
|
||||||
|
|||||||
@@ -1,13 +1,98 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="bg-base-100 flex flex-col">
|
<div class="bg-base-100 flex flex-col gap-4">
|
||||||
<!-- 波形展示 -->
|
<!-- 波形展示 -->
|
||||||
<div class="card bg-base-200 shadow-xl">
|
<div class="card bg-base-200 shadow-xl mx-5">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h2 class="card-title">
|
<h2 class="card-title flex flex-row justify-between">
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
<Activity class="w-5 h-5" />
|
<Activity class="w-5 h-5" />
|
||||||
波形显示
|
波形显示
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<button class="btn btn-sm btn-warning" @click="osc.stopCapture" :disabled="!osc.isCapturing.value">
|
||||||
|
停止捕获
|
||||||
|
</button>
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<button class="btn btn-sm btn-error" @click="osc.clearOscilloscopeData">
|
||||||
|
清空
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</h2>
|
</h2>
|
||||||
<WaveformDisplay :data="generateTestData()" />
|
<OscilloscopeWaveformDisplay />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 示波器配置 -->
|
||||||
|
<div class="card bg-base-200 shadow-xl mx-5">
|
||||||
|
<div class="card-body">
|
||||||
|
<h2 class="card-title">示波器配置</h2>
|
||||||
|
<form class="flex flex-col gap-2" @submit.prevent="applyConfiguration">
|
||||||
|
<div class="flex flex-row items-center justify-between gap-4">
|
||||||
|
<label>
|
||||||
|
边沿触发:
|
||||||
|
<select v-model="osc.config.triggerRisingEdge" class="select select-bordered w-24">
|
||||||
|
<option :value="true">上升沿</option>
|
||||||
|
<option :value="false">下降沿</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
触发电平:
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<input type="range" min="0" max="255" step="1" v-model="osc.config.triggerLevel"
|
||||||
|
class="range range-sm w-50" />
|
||||||
|
<input type="number" v-model="osc.config.triggerLevel" min="0" max="255"
|
||||||
|
class="input input-bordered w-24" />
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
水平偏移:
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<input type="range" min="0" max="1000" step="1" v-model="osc.config.horizontalShift"
|
||||||
|
class="range range-sm w-50" />
|
||||||
|
<input type="number" v-model="osc.config.horizontalShift" min="0" max="1000"
|
||||||
|
class="input input-bordered w-24" />
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
抽取率:
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<input type="range" min="0" max="100" step="1" v-model="osc.config.decimationRate"
|
||||||
|
class="range range-sm w-50" />
|
||||||
|
<input type="number" v-model="osc.config.decimationRate" min="0" max="100"
|
||||||
|
class="input input-bordered w-24" />
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div class="flex gap-4">
|
||||||
|
</div>
|
||||||
|
<div class="flex items-center justify-between gap-2 mt-2">
|
||||||
|
<label>
|
||||||
|
刷新间隔(ms):
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<input type="range" min="100" max="3000" step="100" v-model="osc.refreshIntervalMs.value"
|
||||||
|
class="range range-sm w-50" />
|
||||||
|
<input type="number" min="100" max="3000" step="100" v-model="osc.refreshIntervalMs.value"
|
||||||
|
class="input input-bordered w-24" />
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<button class="btn btn-primary" type="submit" :disabled="osc.isApplying.value || osc.isCapturing.value">
|
||||||
|
应用配置
|
||||||
|
</button>
|
||||||
|
<button class="btn btn-secondary" type="button" @click="osc.resetConfiguration"
|
||||||
|
:disabled="osc.isApplying.value || osc.isCapturing.value">
|
||||||
|
重置
|
||||||
|
</button>
|
||||||
|
<button class="btn btn-outline" @click="osc.refreshRAM" :disabled="osc.isApplying.value || osc.isCapturing.value">
|
||||||
|
刷新RAM
|
||||||
|
</button>
|
||||||
|
<!-- <button class="btn btn-accent" @click="osc.generateTestData" :disabled="osc.isOperationInProgress.value">
|
||||||
|
生成测试数据
|
||||||
|
</button> -->
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -15,9 +100,17 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { Activity } from "lucide-vue-next";
|
import { Activity } from "lucide-vue-next";
|
||||||
import { WaveformDisplay, generateTestData } from "@/components/Oscilloscope";
|
import { OscilloscopeWaveformDisplay } from "@/components/Oscilloscope";
|
||||||
import { useEquipments } from "@/stores/equipments";
|
import { useEquipments } from "@/stores/equipments";
|
||||||
|
import { useOscilloscopeState } from "@/components/Oscilloscope/OscilloscopeManager";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
|
||||||
// 使用全局设备配置
|
// 使用全局设备配置
|
||||||
const equipments = useEquipments();
|
const equipments = useEquipments();
|
||||||
|
|
||||||
|
// 获取示波器状态和操作
|
||||||
|
const osc = useRequiredInjection(useOscilloscopeState);
|
||||||
|
|
||||||
|
// 应用配置
|
||||||
|
const applyConfiguration = () => osc.applyConfiguration();
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -1,24 +1,21 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="bg-base-100 flex flex-col gap-7">
|
<div class="bg-base-100 flex flex-col gap-7">
|
||||||
<!-- 控制面板 -->
|
<!-- 控制面板 -->
|
||||||
<div class="card bg-base-200 shadow-xl">
|
<div class="card bg-base-200 shadow-xl mx-5">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h2 class="card-title text-primary">
|
<h2 class="card-title text-primary">
|
||||||
<Settings class="w-6 h-6" />
|
<Settings class="w-6 h-6" />
|
||||||
控制面板
|
控制面板
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
|
<div class="grid grid-cols-1 gap-4"
|
||||||
|
:class="{ 'md:grid-cols-3': streamType === 'usbCamera', 'md:grid-cols-4': streamType === 'videoStream' }">
|
||||||
<!-- 服务状态 -->
|
<!-- 服务状态 -->
|
||||||
<div class="stats shadow">
|
<div class="stats shadow">
|
||||||
<div class="stat bg-base-100">
|
<div class="stat bg-base-100">
|
||||||
<div class="stat-figure text-primary">
|
<div class="stat-figure text-primary">
|
||||||
<div
|
<div class="badge" :class="statusInfo.isRunning ? 'badge-success' : 'badge-error'
|
||||||
class="badge"
|
">
|
||||||
:class="
|
|
||||||
statusInfo.isRunning ? 'badge-success' : 'badge-error'
|
|
||||||
"
|
|
||||||
>
|
|
||||||
{{ statusInfo.isRunning ? "运行中" : "已停止" }}
|
{{ statusInfo.isRunning ? "运行中" : "已停止" }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -43,30 +40,23 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 分辨率控制 -->
|
<!-- 分辨率控制 -->
|
||||||
<div class="stats shadow">
|
<div v-show="streamType === 'videoStream'" class="stats shadow">
|
||||||
<div class="stat bg-base-100">
|
<div class="stat bg-base-100">
|
||||||
<div class="stat-figure text-info">
|
<div class="stat-figure text-info">
|
||||||
<Settings class="w-8 h-8" />
|
<Settings class="w-8 h-8" />
|
||||||
</div>
|
</div>
|
||||||
<div class="stat-title">分辨率设置</div>
|
<div class="stat-title">分辨率设置</div>
|
||||||
<div class="stat-value text-sm">
|
<div class="stat-value text-sm">
|
||||||
<select
|
<select class="select select-sm select-bordered max-w-xs" v-model="selectedResolution"
|
||||||
class="select select-sm select-bordered max-w-xs"
|
@change="changeResolution" :disabled="changingResolution">
|
||||||
v-model="selectedResolution"
|
|
||||||
@change="changeResolution"
|
|
||||||
:disabled="changingResolution"
|
|
||||||
>
|
|
||||||
<option v-for="res in supportedResolutions" :key="`${res.width}x${res.height}`" :value="res">
|
<option v-for="res in supportedResolutions" :key="`${res.width}x${res.height}`" :value="res">
|
||||||
{{ res.width }}×{{ res.height }}
|
{{ res.width }}×{{ res.height }}
|
||||||
</option>
|
</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="stat-desc">
|
<div class="stat-desc">
|
||||||
<button
|
<button class="btn btn-xs btn-outline btn-info mt-1" @click="refreshResolutions"
|
||||||
class="btn btn-xs btn-outline btn-info mt-1"
|
:disabled="loadingResolutions">
|
||||||
@click="refreshResolutions"
|
|
||||||
:disabled="loadingResolutions"
|
|
||||||
>
|
|
||||||
<RefreshCw v-if="loadingResolutions" class="animate-spin h-3 w-3" />
|
<RefreshCw v-if="loadingResolutions" class="animate-spin h-3 w-3" />
|
||||||
{{ loadingResolutions ? "刷新中..." : "刷新" }}
|
{{ loadingResolutions ? "刷新中..." : "刷新" }}
|
||||||
</button>
|
</button>
|
||||||
@@ -86,30 +76,18 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="stat-desc">
|
<div class="stat-desc">
|
||||||
<div class="dropdown dropdown-hover dropdown-top">
|
<div class="dropdown dropdown-hover dropdown-top">
|
||||||
<div
|
<div tabindex="0" role="button" class="text-xs underline cursor-help">
|
||||||
tabindex="0"
|
|
||||||
role="button"
|
|
||||||
class="text-xs underline cursor-help"
|
|
||||||
>
|
|
||||||
查看客户端
|
查看客户端
|
||||||
</div>
|
</div>
|
||||||
<ul
|
<ul tabindex="0"
|
||||||
tabindex="0"
|
class="dropdown-content z-20 menu p-2 shadow bg-base-200 rounded-box w-64 max-h-48 overflow-y-auto">
|
||||||
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">
|
||||||
>
|
|
||||||
<li
|
|
||||||
v-for="(client, index) in statusInfo.clientEndpoints"
|
|
||||||
:key="index"
|
|
||||||
class="text-xs"
|
|
||||||
>
|
|
||||||
<a class="break-all">{{ client }}</a>
|
<a class="break-all">{{ client }}</a>
|
||||||
</li>
|
</li>
|
||||||
<li
|
<li v-if="
|
||||||
v-if="
|
|
||||||
!statusInfo.clientEndpoints ||
|
!statusInfo.clientEndpoints ||
|
||||||
statusInfo.clientEndpoints.length === 0
|
statusInfo.clientEndpoints.length === 0
|
||||||
"
|
">
|
||||||
>
|
|
||||||
<a class="text-xs opacity-50">无活跃连接</a>
|
<a class="text-xs opacity-50">无活跃连接</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
@@ -121,29 +99,21 @@
|
|||||||
|
|
||||||
<!-- 操作按钮 -->
|
<!-- 操作按钮 -->
|
||||||
<div class="card-actions justify-end mt-4">
|
<div class="card-actions justify-end mt-4">
|
||||||
<button
|
<button class="btn btn-outline btn-warning mr-2" @click="toggleStreamType" :disabled="isSwitchingStreamType">
|
||||||
class="btn btn-outline btn-primary"
|
<SwitchCamera class="h-4 w-4 mr-2" />
|
||||||
@click="configCamera"
|
{{ streamType === 'usbCamera' ? '切换到视频流' : '切换到USB摄像头' }}
|
||||||
:dsiabled="configing"
|
</button>
|
||||||
>
|
<button v-show="streamType === 'videoStream'" class="btn btn-outline btn-primary" @click="configCamera" :disabled="configing">
|
||||||
<RefreshCw v-if="configing" class="animate-spin h-4 w-4 mr-2" />
|
<RefreshCw v-if="configing" class="animate-spin h-4 w-4 mr-2" />
|
||||||
<CogIcon v-else class="h-4 w-4 mr-2" />
|
<CogIcon v-else class="h-4 w-4 mr-2" />
|
||||||
{{ configing ? "配置中..." : "配置摄像头" }}
|
{{ configing ? "配置中..." : "配置摄像头" }}
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button class="btn btn-outline btn-primary" @click="refreshStatus" :disabled="loading">
|
||||||
class="btn btn-outline btn-primary"
|
|
||||||
@click="refreshStatus"
|
|
||||||
:disabled="loading"
|
|
||||||
>
|
|
||||||
<RefreshCw v-if="loading" class="animate-spin h-4 w-4 mr-2" />
|
<RefreshCw v-if="loading" class="animate-spin h-4 w-4 mr-2" />
|
||||||
<RefreshCw v-else class="h-4 w-4 mr-2" />
|
<RefreshCw v-else class="h-4 w-4 mr-2" />
|
||||||
{{ loading ? "刷新中..." : "刷新状态" }}
|
{{ loading ? "刷新中..." : "刷新状态" }}
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button v-show="streamType === 'videoStream'" class="btn btn-primary" @click="testConnection" :disabled="testing">
|
||||||
class="btn btn-primary"
|
|
||||||
@click="testConnection"
|
|
||||||
:disabled="testing"
|
|
||||||
>
|
|
||||||
<RefreshCw v-if="testing" class="animate-spin h-4 w-4 mr-2" />
|
<RefreshCw v-if="testing" class="animate-spin h-4 w-4 mr-2" />
|
||||||
<TestTube v-else class="h-4 w-4 mr-2" />
|
<TestTube v-else class="h-4 w-4 mr-2" />
|
||||||
{{ testing ? "测试中..." : "测试连接" }}
|
{{ testing ? "测试中..." : "测试连接" }}
|
||||||
@@ -153,36 +123,31 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 视频预览区域 -->
|
<!-- 视频预览区域 -->
|
||||||
<div class="card bg-base-200 shadow-xl">
|
<div class="card bg-base-200 shadow-xl mx-5">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h2 class="card-title text-primary">
|
<h2 class="card-title text-primary">
|
||||||
<Video class="w-6 h-6" />
|
<Video class="w-6 h-6" />
|
||||||
视频预览
|
视频预览
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<div
|
<div class="relative bg-black rounded-lg overflow-hidden cursor-pointer" :class="[
|
||||||
class="relative bg-black rounded-lg overflow-hidden"
|
focusAnimationClass,
|
||||||
style="aspect-ratio: 4/3"
|
{ 'cursor-not-allowed': !isPlaying || hasVideoError }
|
||||||
>
|
]" style="aspect-ratio: 4/3" @click="handleVideoClick">
|
||||||
<!-- 视频播放器 - 使用img标签直接显示MJPEG流 -->
|
<!-- 视频播放器 - 使用img标签直接显示MJPEG流 -->
|
||||||
<div
|
<div v-show="isPlaying" class="w-full h-full flex items-center justify-center">
|
||||||
v-show="isPlaying"
|
<img :src="currentVideoSource" alt="视频流" class="max-w-full max-h-full object-contain"
|
||||||
class="w-full h-full flex items-center justify-center"
|
@error="handleVideoError" @load="handleVideoLoad" />
|
||||||
>
|
</div>
|
||||||
<img
|
|
||||||
:src="currentVideoSource"
|
<!-- 对焦提示 -->
|
||||||
alt="视频流"
|
<div v-if="isPlaying && !hasVideoError"
|
||||||
class="max-w-full max-h-full object-contain"
|
class="absolute top-4 right-4 text-white text-sm bg-black bg-opacity-50 px-2 py-1 rounded">
|
||||||
@error="handleVideoError"
|
{{ isFocusing ? '对焦中...' : '点击画面对焦' }}
|
||||||
@load="handleVideoLoad"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 错误信息显示 -->
|
<!-- 错误信息显示 -->
|
||||||
<div
|
<div v-if="hasVideoError" class="absolute inset-0 flex items-center justify-center bg-black bg-opacity-70">
|
||||||
v-if="hasVideoError"
|
|
||||||
class="absolute inset-0 flex items-center justify-center bg-black bg-opacity-70"
|
|
||||||
>
|
|
||||||
<div class="card bg-error text-white shadow-lg w-full max-w-lg">
|
<div class="card bg-error text-white shadow-lg w-full max-w-lg">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h3 class="card-title flex items-center gap-2">
|
<h3 class="card-title flex items-center gap-2">
|
||||||
@@ -196,10 +161,7 @@
|
|||||||
<li>端口 {{ statusInfo.serverPort }} 是否可访问</li>
|
<li>端口 {{ statusInfo.serverPort }} 是否可访问</li>
|
||||||
</ul>
|
</ul>
|
||||||
<div class="card-actions justify-end mt-2">
|
<div class="card-actions justify-end mt-2">
|
||||||
<button
|
<button class="btn btn-sm btn-outline btn-primary" @click="tryReconnect">
|
||||||
class="btn btn-sm btn-outline btn-primary"
|
|
||||||
@click="tryReconnect"
|
|
||||||
>
|
|
||||||
重试连接
|
重试连接
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -208,10 +170,8 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 占位符 -->
|
<!-- 占位符 -->
|
||||||
<div
|
<div v-show="!isPlaying && !hasVideoError"
|
||||||
v-show="!isPlaying && !hasVideoError"
|
class="absolute inset-0 flex items-center justify-center text-white">
|
||||||
class="absolute inset-0 flex items-center justify-center text-white"
|
|
||||||
>
|
|
||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
<Video class="w-16 h-16 mx-auto mb-4 opacity-50" />
|
<Video class="w-16 h-16 mx-auto mb-4 opacity-50" />
|
||||||
<p class="text-lg opacity-75">{{ videoStatus }}</p>
|
<p class="text-lg opacity-75">{{ videoStatus }}</p>
|
||||||
@@ -232,18 +192,11 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="space-x-2">
|
<div class="space-x-2">
|
||||||
<div class="dropdown dropdown-hover dropdown-top dropdown-end">
|
<div class="dropdown dropdown-hover dropdown-top dropdown-end">
|
||||||
<div
|
<div tabindex="0" role="button" class="btn btn-sm btn-outline btn-accent">
|
||||||
tabindex="0"
|
|
||||||
role="button"
|
|
||||||
class="btn btn-sm btn-outline btn-accent"
|
|
||||||
>
|
|
||||||
<MoreHorizontal class="w-4 h-4 mr-1" />
|
<MoreHorizontal class="w-4 h-4 mr-1" />
|
||||||
更多功能
|
更多功能
|
||||||
</div>
|
</div>
|
||||||
<ul
|
<ul tabindex="0" class="dropdown-content z-[1] menu p-2 shadow bg-base-200 rounded-box w-52">
|
||||||
tabindex="0"
|
|
||||||
class="dropdown-content z-[1] menu p-2 shadow bg-base-200 rounded-box w-52"
|
|
||||||
>
|
|
||||||
<li>
|
<li>
|
||||||
<a @click="openInNewTab(streamInfo.htmlUrl)">
|
<a @click="openInNewTab(streamInfo.htmlUrl)">
|
||||||
<ExternalLink class="w-4 h-4" />
|
<ExternalLink class="w-4 h-4" />
|
||||||
@@ -264,19 +217,11 @@
|
|||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<button
|
<button class="btn btn-success btn-sm" @click="startStream" :disabled="isPlaying">
|
||||||
class="btn btn-success btn-sm"
|
|
||||||
@click="startStream"
|
|
||||||
:disabled="isPlaying"
|
|
||||||
>
|
|
||||||
<Play class="w-4 h-4 mr-1" />
|
<Play class="w-4 h-4 mr-1" />
|
||||||
播放视频流
|
播放视频流
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button class="btn btn-error btn-sm" @click="stopStream" :disabled="!isPlaying">
|
||||||
class="btn btn-error btn-sm"
|
|
||||||
@click="stopStream"
|
|
||||||
:disabled="!isPlaying"
|
|
||||||
>
|
|
||||||
<Square class="w-4 h-4 mr-1" />
|
<Square class="w-4 h-4 mr-1" />
|
||||||
停止视频流
|
停止视频流
|
||||||
</button>
|
</button>
|
||||||
@@ -286,7 +231,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 日志区域 -->
|
<!-- 日志区域 -->
|
||||||
<div class="card bg-base-200 shadow-xl">
|
<div class="card bg-base-200 shadow-xl mx-5">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h2 class="card-title text-primary">
|
<h2 class="card-title text-primary">
|
||||||
<FileText class="w-6 h-6" />
|
<FileText class="w-6 h-6" />
|
||||||
@@ -294,20 +239,11 @@
|
|||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<div class="bg-base-300 rounded-lg p-4 h-48 overflow-y-auto">
|
<div class="bg-base-300 rounded-lg p-4 h-48 overflow-y-auto">
|
||||||
<div
|
<div v-for="(log, index) in logs" :key="index" class="text-sm font-mono mb-1">
|
||||||
v-for="(log, index) in logs"
|
<span class="text-base-content/50">[{{ formatTime(log.time) }}]</span>
|
||||||
:key="index"
|
|
||||||
class="text-sm font-mono mb-1"
|
|
||||||
>
|
|
||||||
<span class="text-base-content/50"
|
|
||||||
>[{{ formatTime(log.time) }}]</span
|
|
||||||
>
|
|
||||||
<span :class="getLogClass(log.level)">{{ log.message }}</span>
|
<span :class="getLogClass(log.level)">{{ log.message }}</span>
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div v-if="logs.length === 0" class="text-base-content/50 text-center py-8">
|
||||||
v-if="logs.length === 0"
|
|
||||||
class="text-base-content/50 text-center py-8"
|
|
||||||
>
|
|
||||||
暂无日志记录
|
暂无日志记录
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -339,8 +275,9 @@ import {
|
|||||||
FileText,
|
FileText,
|
||||||
AlertTriangle,
|
AlertTriangle,
|
||||||
MoreHorizontal,
|
MoreHorizontal,
|
||||||
|
SwitchCamera,
|
||||||
} from "lucide-vue-next";
|
} from "lucide-vue-next";
|
||||||
import { VideoStreamClient, CameraConfigRequest, ResolutionConfigRequest } from "@/APIClient";
|
import { VideoStreamClient, CameraConfigRequest, ResolutionConfigRequest, StreamInfoResult } from "@/APIClient";
|
||||||
import { useEquipments } from "@/stores/equipments";
|
import { useEquipments } from "@/stores/equipments";
|
||||||
|
|
||||||
const eqps = useEquipments();
|
const eqps = useEquipments();
|
||||||
@@ -353,6 +290,14 @@ const isPlaying = ref(false);
|
|||||||
const hasVideoError = ref(false);
|
const hasVideoError = ref(false);
|
||||||
const videoStatus = ref('点击"播放视频流"按钮开始查看实时视频');
|
const videoStatus = ref('点击"播放视频流"按钮开始查看实时视频');
|
||||||
|
|
||||||
|
// 视频流类型切换相关
|
||||||
|
const streamType = ref<'usbCamera' | 'videoStream'>('videoStream');
|
||||||
|
const isSwitchingStreamType = ref(false);
|
||||||
|
|
||||||
|
// 对焦相关状态
|
||||||
|
const isFocusing = ref(false);
|
||||||
|
const focusAnimationClass = ref('');
|
||||||
|
|
||||||
// 分辨率相关状态
|
// 分辨率相关状态
|
||||||
const changingResolution = ref(false);
|
const changingResolution = ref(false);
|
||||||
const loadingResolutions = ref(false);
|
const loadingResolutions = ref(false);
|
||||||
@@ -373,7 +318,7 @@ const statusInfo = ref({
|
|||||||
clientEndpoints: [] as string[],
|
clientEndpoints: [] as string[],
|
||||||
});
|
});
|
||||||
|
|
||||||
const streamInfo = ref({
|
const streamInfo = ref<StreamInfoResult>(new StreamInfoResult({
|
||||||
frameRate: 30,
|
frameRate: 30,
|
||||||
frameWidth: 640,
|
frameWidth: 640,
|
||||||
frameHeight: 480,
|
frameHeight: 480,
|
||||||
@@ -381,7 +326,8 @@ const streamInfo = ref({
|
|||||||
htmlUrl: "",
|
htmlUrl: "",
|
||||||
mjpegUrl: "",
|
mjpegUrl: "",
|
||||||
snapshotUrl: "",
|
snapshotUrl: "",
|
||||||
});
|
usbCameraUrl: "",
|
||||||
|
}));
|
||||||
|
|
||||||
const currentVideoSource = ref("");
|
const currentVideoSource = ref("");
|
||||||
const logs = ref<Array<{ time: Date; level: string; message: string }>>([]);
|
const logs = ref<Array<{ time: Date; level: string; message: string }>>([]);
|
||||||
@@ -445,6 +391,27 @@ const openInNewTab = (url: string) => {
|
|||||||
addLog("info", `已在新标签打开视频页面: ${url}`);
|
addLog("info", `已在新标签打开视频页面: ${url}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 切换视频流类型
|
||||||
|
const toggleStreamType = async () => {
|
||||||
|
if (isSwitchingStreamType.value) return;
|
||||||
|
isSwitchingStreamType.value = true;
|
||||||
|
try {
|
||||||
|
// 这里假设后端有API: setStreamType(type: string)
|
||||||
|
addLog('info', `正在切换视频流类型到${streamType.value === 'usbCamera' ? '视频流' : 'USB摄像头'}...`);
|
||||||
|
refreshStatus();
|
||||||
|
|
||||||
|
// 设置视频源
|
||||||
|
streamType.value = streamType.value === 'usbCamera' ? 'videoStream' : 'usbCamera';
|
||||||
|
addLog('success', `已切换到${streamType.value === 'usbCamera' ? 'USB摄像头' : '视频流'}`);
|
||||||
|
stopStream();
|
||||||
|
} catch (error) {
|
||||||
|
addLog('error', `切换视频流类型失败: ${error}`);
|
||||||
|
console.error('切换视频流类型失败:', error);
|
||||||
|
} finally {
|
||||||
|
isSwitchingStreamType.value = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 获取并下载快照
|
// 获取并下载快照
|
||||||
const takeSnapshot = async () => {
|
const takeSnapshot = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -565,6 +532,67 @@ const tryReconnect = () => {
|
|||||||
currentVideoSource.value = `${streamInfo.value.mjpegUrl}?t=${new Date().getTime()}`;
|
currentVideoSource.value = `${streamInfo.value.mjpegUrl}?t=${new Date().getTime()}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 执行对焦
|
||||||
|
const performFocus = async () => {
|
||||||
|
if (isFocusing.value || !isPlaying.value) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
isFocusing.value = true;
|
||||||
|
focusAnimationClass.value = 'focus-starting';
|
||||||
|
addLog("info", "正在执行自动对焦...");
|
||||||
|
|
||||||
|
// 调用对焦API
|
||||||
|
const response = await fetch('/api/VideoStream/Focus');
|
||||||
|
const result = await response.json();
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
// 对焦成功动画
|
||||||
|
focusAnimationClass.value = 'focus-success';
|
||||||
|
addLog("success", "自动对焦执行成功");
|
||||||
|
|
||||||
|
// 2秒后消失
|
||||||
|
setTimeout(() => {
|
||||||
|
focusAnimationClass.value = '';
|
||||||
|
}, 2000);
|
||||||
|
} else {
|
||||||
|
// 对焦失败动画
|
||||||
|
focusAnimationClass.value = 'focus-error';
|
||||||
|
addLog("error", `自动对焦执行失败: ${result.message || '未知错误'}`);
|
||||||
|
|
||||||
|
// 2秒后消失
|
||||||
|
setTimeout(() => {
|
||||||
|
focusAnimationClass.value = '';
|
||||||
|
}, 2000);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// 对焦失败动画
|
||||||
|
focusAnimationClass.value = 'focus-error';
|
||||||
|
addLog("error", `自动对焦执行失败: ${error}`);
|
||||||
|
console.error("自动对焦执行失败:", error);
|
||||||
|
|
||||||
|
// 2秒后消失
|
||||||
|
setTimeout(() => {
|
||||||
|
focusAnimationClass.value = '';
|
||||||
|
}, 2000);
|
||||||
|
} finally {
|
||||||
|
// 1秒后重置对焦状态
|
||||||
|
setTimeout(() => {
|
||||||
|
isFocusing.value = false;
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 处理视频点击事件
|
||||||
|
const handleVideoClick = (event: MouseEvent) => {
|
||||||
|
// 只在播放状态下才允许对焦
|
||||||
|
if (!isPlaying.value || hasVideoError.value) return;
|
||||||
|
|
||||||
|
// 防止重复点击
|
||||||
|
if (isFocusing.value) return;
|
||||||
|
|
||||||
|
performFocus();
|
||||||
|
};
|
||||||
|
|
||||||
// 启动视频流
|
// 启动视频流
|
||||||
const startStream = async () => {
|
const startStream = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -576,7 +604,7 @@ const startStream = async () => {
|
|||||||
await refreshStatus();
|
await refreshStatus();
|
||||||
|
|
||||||
// 设置视频源
|
// 设置视频源
|
||||||
currentVideoSource.value = streamInfo.value.mjpegUrl;
|
currentVideoSource.value = streamType.value === 'usbCamera' ? streamInfo.value.usbCameraUrl : streamInfo.value.mjpegUrl;
|
||||||
|
|
||||||
// 设置播放状态
|
// 设置播放状态
|
||||||
isPlaying.value = true;
|
isPlaying.value = true;
|
||||||
@@ -711,4 +739,79 @@ img {
|
|||||||
* {
|
* {
|
||||||
transition: all 500ms ease-in-out;
|
transition: all 500ms ease-in-out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 对焦动画样式 */
|
||||||
|
.focus-starting {
|
||||||
|
border: 3px solid transparent;
|
||||||
|
animation: focus-starting-animation 0.5s ease-in-out forwards;
|
||||||
|
}
|
||||||
|
|
||||||
|
.focus-success {
|
||||||
|
border: 3px solid transparent;
|
||||||
|
animation: focus-success-animation 2s ease-in-out forwards;
|
||||||
|
}
|
||||||
|
|
||||||
|
.focus-error {
|
||||||
|
border: 3px solid transparent;
|
||||||
|
animation: focus-error-animation 2s ease-in-out forwards;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes focus-starting-animation {
|
||||||
|
0% {
|
||||||
|
border-color: transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
border-color: #fbbf24;
|
||||||
|
/* 黄色 */
|
||||||
|
box-shadow: 0 0 20px rgba(251, 191, 36, 0.5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes focus-success-animation {
|
||||||
|
0% {
|
||||||
|
border-color: #fbbf24;
|
||||||
|
/* 黄色 */
|
||||||
|
box-shadow: 0 0 20px rgba(251, 191, 36, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
50% {
|
||||||
|
border-color: #10b981;
|
||||||
|
/* 绿色 */
|
||||||
|
box-shadow: 0 0 20px rgba(16, 185, 129, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
border-color: transparent;
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes focus-error-animation {
|
||||||
|
0% {
|
||||||
|
border-color: #fbbf24;
|
||||||
|
/* 黄色 */
|
||||||
|
box-shadow: 0 0 20px rgba(251, 191, 36, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
50% {
|
||||||
|
border-color: #ef4444;
|
||||||
|
/* 红色 */
|
||||||
|
box-shadow: 0 0 20px rgba(239, 68, 68, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
border-color: transparent;
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 对焦状态下的鼠标指针 */
|
||||||
|
.cursor-pointer {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.cursor-not-allowed {
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -3,11 +3,15 @@
|
|||||||
<div class="modal-box w-96 max-w-md">
|
<div class="modal-box w-96 max-w-md">
|
||||||
<h3 class="text-lg font-bold mb-4">新增实验板</h3>
|
<h3 class="text-lg font-bold mb-4">新增实验板</h3>
|
||||||
|
|
||||||
<form @submit.prevent="handleSubmit" class="space-y-4">
|
<!-- 步骤1: 输入板卡名称 -->
|
||||||
|
<div v-if="currentStep === 'input'" class="space-y-4">
|
||||||
|
<form @submit.prevent="handleSubmit">
|
||||||
<!-- 实验板名称 -->
|
<!-- 实验板名称 -->
|
||||||
<div class="form-control">
|
<div class="form-control">
|
||||||
<label class="label">
|
<label class="label">
|
||||||
<span class="label-text">实验板名称 <span class="text-error">*</span></span>
|
<span class="label-text"
|
||||||
|
>实验板名称 <span class="text-error">*</span></span
|
||||||
|
>
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
v-model="form.name"
|
v-model="form.name"
|
||||||
@@ -22,44 +26,6 @@
|
|||||||
</label>
|
</label>
|
||||||
</div>
|
</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">
|
<div class="modal-action">
|
||||||
<button
|
<button
|
||||||
@@ -73,15 +39,130 @@
|
|||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
class="btn btn-primary"
|
class="btn btn-primary"
|
||||||
:class="{ 'loading': isSubmitting }"
|
:class="{ loading: isSubmitting }"
|
||||||
:disabled="isSubmitting"
|
:disabled="isSubmitting"
|
||||||
>
|
>
|
||||||
{{ isSubmitting ? '添加中...' : '确认添加' }}
|
{{ isSubmitting ? "添加中..." : "确认添加" }}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- 步骤2: 等待配对 -->
|
||||||
|
<div v-else-if="currentStep === 'pairing'" class="space-y-4">
|
||||||
|
<div class="text-center">
|
||||||
|
<div class="alert alert-info">
|
||||||
|
<div class="flex items-center">
|
||||||
|
<svg
|
||||||
|
class="w-5 h-5 mr-2"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
stroke-linecap="round"
|
||||||
|
stroke-linejoin="round"
|
||||||
|
stroke-width="2"
|
||||||
|
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
<span>请打开实验板配对模式</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 操作按钮 -->
|
||||||
|
<div class="modal-action">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="btn btn-ghost"
|
||||||
|
@click="handleCancelPairing"
|
||||||
|
:disabled="isConfiguring"
|
||||||
|
>
|
||||||
|
取消
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="btn btn-primary"
|
||||||
|
@click="handlePairingConfirm"
|
||||||
|
:disabled="isConfiguring"
|
||||||
|
>
|
||||||
|
已开启
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 步骤3: 配置网络 -->
|
||||||
|
<div v-else-if="currentStep === 'configuring'" class="space-y-4">
|
||||||
|
<div class="text-center">
|
||||||
|
<div class="alert alert-warning">
|
||||||
|
<div class="flex items-center">
|
||||||
|
<div class="loading loading-spinner loading-sm mr-2"></div>
|
||||||
|
<span>正在分配网络...</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 步骤4: 显示配置结果 -->
|
||||||
|
<div v-else-if="currentStep === 'result'" class="space-y-4">
|
||||||
|
<div class="text-center">
|
||||||
|
<div class="alert alert-success">
|
||||||
|
<div class="flex items-center">
|
||||||
|
<svg
|
||||||
|
class="w-5 h-5 mr-2"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
stroke-linecap="round"
|
||||||
|
stroke-linejoin="round"
|
||||||
|
stroke-width="2"
|
||||||
|
d="M5 13l4 4L19 7"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
<span>实验板配置成功</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 网络配置信息 -->
|
||||||
|
<div v-if="networkConfig" class="space-y-2">
|
||||||
|
<h4 class="font-semibold">网络配置信息:</h4>
|
||||||
|
<div class="bg-base-200 p-3 rounded">
|
||||||
|
<div class="text-sm space-y-1">
|
||||||
|
<div>
|
||||||
|
<span class="font-medium">主机IP:</span>
|
||||||
|
{{ networkConfig.hostIP }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<span class="font-medium">板卡IP:</span>
|
||||||
|
{{ networkConfig.boardIP }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<span class="font-medium">主机MAC:</span>
|
||||||
|
{{ networkConfig.hostMAC }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<span class="font-medium">板卡MAC:</span>
|
||||||
|
{{ networkConfig.boardMAC }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 操作按钮 -->
|
||||||
|
<div class="modal-action">
|
||||||
|
<button type="button" class="btn btn-primary" @click="handleSuccess">
|
||||||
|
确认
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- 点击背景关闭 -->
|
<!-- 点击背景关闭 -->
|
||||||
<form method="dialog" class="modal-backdrop">
|
<form method="dialog" class="modal-backdrop">
|
||||||
<button type="button" @click="handleCancel">close</button>
|
<button type="button" @click="handleCancel">close</button>
|
||||||
@@ -90,8 +171,12 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref, reactive, watch } from 'vue';
|
import { ref, reactive, watch } from "vue";
|
||||||
import { useBoardManager } from '../../utils/BoardManager';
|
import { AuthManager } from "../../utils/AuthManager";
|
||||||
|
import { useAlertStore } from "../../components/Alert";
|
||||||
|
import { BoardStatus, type NetworkConfigDto } from "../../APIClient";
|
||||||
|
import { useRequiredInjection } from "@/utils/Common";
|
||||||
|
import { useBoardManager } from "@/utils/BoardManager";
|
||||||
|
|
||||||
// Props 和 Emits
|
// Props 和 Emits
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -99,75 +184,57 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Emits {
|
interface Emits {
|
||||||
(e: 'update:visible', value: boolean): void;
|
(e: "update:visible", value: boolean): void;
|
||||||
(e: 'success'): void;
|
(e: "success"): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = defineProps<Props>();
|
const props = defineProps<Props>();
|
||||||
const emit = defineEmits<Emits>();
|
const emit = defineEmits<Emits>();
|
||||||
|
|
||||||
// 使用 BoardManager
|
// 使用 Alert
|
||||||
const boardManager = useBoardManager()!;
|
const alertStore = useAlertStore();
|
||||||
|
|
||||||
|
const boardManager = useRequiredInjection(useBoardManager);
|
||||||
|
|
||||||
|
// 当前步骤
|
||||||
|
const currentStep = ref<"input" | "pairing" | "configuring" | "result">(
|
||||||
|
"input",
|
||||||
|
);
|
||||||
|
|
||||||
// 表单数据
|
// 表单数据
|
||||||
const form = reactive({
|
const form = reactive({
|
||||||
name: '',
|
name: "Board1",
|
||||||
ipAddr: '',
|
|
||||||
port: 1234
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 表单错误
|
// 表单错误
|
||||||
const errors = reactive({
|
const errors = reactive({
|
||||||
name: '',
|
name: "",
|
||||||
ipAddr: '',
|
|
||||||
port: ''
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 提交状态
|
// 状态
|
||||||
const isSubmitting = ref(false);
|
const isSubmitting = ref(false);
|
||||||
|
const isConfiguring = 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]?)$/;
|
const addedBoardId = ref<string>("");
|
||||||
|
const networkConfig = ref<NetworkConfigDto | null>(null);
|
||||||
|
|
||||||
// 验证表单
|
// 验证表单
|
||||||
function validateForm(): boolean {
|
function validateForm(): boolean {
|
||||||
// 清空之前的错误
|
// 清空之前的错误
|
||||||
errors.name = '';
|
errors.name = "";
|
||||||
errors.ipAddr = '';
|
|
||||||
errors.port = '';
|
|
||||||
|
|
||||||
let isValid = true;
|
let isValid = true;
|
||||||
|
|
||||||
// 验证名称
|
// 验证名称
|
||||||
if (!form.name.trim()) {
|
if (!form.name.trim()) {
|
||||||
errors.name = '请输入实验板名称';
|
errors.name = "请输入实验板名称";
|
||||||
isValid = false;
|
isValid = false;
|
||||||
} else if (form.name.trim().length < 2) {
|
} else if (form.name.trim().length < 2) {
|
||||||
errors.name = '实验板名称至少需要2个字符';
|
errors.name = "实验板名称至少需要2个字符";
|
||||||
isValid = false;
|
isValid = false;
|
||||||
} else if (form.name.trim().length > 50) {
|
} else if (form.name.trim().length > 50) {
|
||||||
errors.name = '实验板名称不能超过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;
|
isValid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,18 +243,17 @@ function validateForm(): boolean {
|
|||||||
|
|
||||||
// 重置表单
|
// 重置表单
|
||||||
function resetForm() {
|
function resetForm() {
|
||||||
form.name = '';
|
form.name = "Board1";
|
||||||
form.ipAddr = '';
|
errors.name = "";
|
||||||
form.port = 1234;
|
currentStep.value = "input";
|
||||||
errors.name = '';
|
addedBoardId.value = "";
|
||||||
errors.ipAddr = '';
|
networkConfig.value = null;
|
||||||
errors.port = '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理取消
|
// 处理取消
|
||||||
function handleCancel() {
|
function handleCancel() {
|
||||||
if (!isSubmitting.value) {
|
if (!isSubmitting.value && !isConfiguring.value) {
|
||||||
emit('update:visible', false);
|
emit("update:visible", false);
|
||||||
resetForm();
|
resetForm();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,29 +267,133 @@ async function handleSubmit() {
|
|||||||
isSubmitting.value = true;
|
isSubmitting.value = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const success = await boardManager.addBoard(
|
// 通过 AuthManager 获取认证的 DataClient
|
||||||
form.name.trim(),
|
const dataClient = AuthManager.createAuthenticatedDataClient();
|
||||||
form.ipAddr.trim(),
|
|
||||||
form.port
|
|
||||||
);
|
|
||||||
|
|
||||||
if (success) {
|
// 添加板卡到数据库
|
||||||
emit('success');
|
const boardId = await dataClient.addBoard(form.name.trim());
|
||||||
resetForm();
|
|
||||||
|
if (boardId) {
|
||||||
|
addedBoardId.value = boardId;
|
||||||
|
currentStep.value = "pairing";
|
||||||
|
alertStore?.success("板卡添加成功,请开启配对模式");
|
||||||
|
} else {
|
||||||
|
alertStore?.error("板卡添加失败");
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('添加实验板失败:', error);
|
console.error("添加实验板失败:", error);
|
||||||
|
alertStore?.error("添加实验板失败");
|
||||||
} finally {
|
} finally {
|
||||||
isSubmitting.value = false;
|
isSubmitting.value = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理取消配对
|
||||||
|
async function handleCancelPairing() {
|
||||||
|
if (!addedBoardId.value) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 通过 AuthManager 获取认证的 DataClient
|
||||||
|
const dataClient = AuthManager.createAuthenticatedDataClient();
|
||||||
|
|
||||||
|
// 删除添加的板卡
|
||||||
|
await dataClient.deleteBoard(addedBoardId.value);
|
||||||
|
|
||||||
|
alertStore?.info("已取消添加实验板");
|
||||||
|
emit("update:visible", false);
|
||||||
|
resetForm();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("删除板卡失败:", error);
|
||||||
|
alertStore?.error("删除板卡失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理配对确认
|
||||||
|
async function handlePairingConfirm() {
|
||||||
|
if (!addedBoardId.value) return;
|
||||||
|
|
||||||
|
isConfiguring.value = true;
|
||||||
|
currentStep.value = "configuring";
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 通过 AuthManager 获取认证的客户端
|
||||||
|
const dataClient = AuthManager.createAuthenticatedDataClient();
|
||||||
|
const netConfigClient = AuthManager.createAuthenticatedNetConfigClient();
|
||||||
|
|
||||||
|
// 获取数据库中对应分配的板卡信息
|
||||||
|
const boardInfo = await dataClient.getBoardByID(addedBoardId.value);
|
||||||
|
|
||||||
|
if (!boardInfo) {
|
||||||
|
throw new Error("无法获取板卡信息");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新主机IP和主机MAC
|
||||||
|
await netConfigClient.updateHostIP();
|
||||||
|
await netConfigClient.updateHostMAC();
|
||||||
|
|
||||||
|
// 设置板卡IP和MAC
|
||||||
|
await netConfigClient.setBoardIP(boardInfo.ipAddr);
|
||||||
|
await netConfigClient.setBoardMAC(boardInfo.macAddr);
|
||||||
|
|
||||||
|
// 更新板卡状态为可用
|
||||||
|
if (
|
||||||
|
(await dataClient.updateBoardStatus(
|
||||||
|
boardInfo.id,
|
||||||
|
BoardStatus.Available,
|
||||||
|
)) != 1
|
||||||
|
) {
|
||||||
|
throw new Error("无法更新板卡状态");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(await boardManager.getAllBoards()).success) {
|
||||||
|
alertStore?.error("无法获取板卡列表");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取实验板网络信息
|
||||||
|
const networkInfo = await netConfigClient.getNetworkConfig();
|
||||||
|
|
||||||
|
if (networkInfo) {
|
||||||
|
networkConfig.value = networkInfo;
|
||||||
|
currentStep.value = "result";
|
||||||
|
alertStore?.success("实验板配置成功");
|
||||||
|
} else {
|
||||||
|
throw new Error("无法获取网络配置信息");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("配置实验板失败:", error);
|
||||||
|
alertStore?.error("配置实验板失败");
|
||||||
|
|
||||||
|
// 配置失败,删除数据库中的板卡信息
|
||||||
|
try {
|
||||||
|
const dataClient = AuthManager.createAuthenticatedDataClient();
|
||||||
|
await dataClient.deleteBoard(addedBoardId.value);
|
||||||
|
} catch (deleteError) {
|
||||||
|
console.error("删除板卡失败:", deleteError);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回输入步骤
|
||||||
|
currentStep.value = "input";
|
||||||
|
} finally {
|
||||||
|
isConfiguring.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理成功
|
||||||
|
function handleSuccess() {
|
||||||
|
emit("success");
|
||||||
|
emit("update:visible", false);
|
||||||
|
resetForm();
|
||||||
|
}
|
||||||
|
|
||||||
// 监听对话框显示状态,重置表单
|
// 监听对话框显示状态,重置表单
|
||||||
watch(() => props.visible, (newVisible) => {
|
watch(
|
||||||
|
() => props.visible,
|
||||||
|
(newVisible) => {
|
||||||
if (newVisible) {
|
if (newVisible) {
|
||||||
resetForm();
|
resetForm();
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped lang="postcss">
|
<style scoped lang="postcss">
|
||||||
@@ -248,4 +418,20 @@ watch(() => props.visible, (newVisible) => {
|
|||||||
.loading {
|
.loading {
|
||||||
@apply opacity-50 cursor-not-allowed;
|
@apply opacity-50 cursor-not-allowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.alert {
|
||||||
|
@apply rounded-lg p-4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.alert-info {
|
||||||
|
@apply bg-blue-50 text-blue-800 border border-blue-200;
|
||||||
|
}
|
||||||
|
|
||||||
|
.alert-warning {
|
||||||
|
@apply bg-yellow-50 text-yellow-800 border border-yellow-200;
|
||||||
|
}
|
||||||
|
|
||||||
|
.alert-success {
|
||||||
|
@apply bg-green-50 text-green-800 border border-green-200;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
@@ -37,7 +37,9 @@ const [useProvideBoardTableManager, useBoardTableManager] =
|
|||||||
port: false,
|
port: false,
|
||||||
id: false,
|
id: false,
|
||||||
status: false,
|
status: false,
|
||||||
version: false,
|
firmVersion: false,
|
||||||
|
macAddr: false,
|
||||||
|
occupiedUserName: false,
|
||||||
});
|
});
|
||||||
const rowSelection = ref({});
|
const rowSelection = ref({});
|
||||||
const expanded = ref<ExpandedState>({});
|
const expanded = ref<ExpandedState>({});
|
||||||
@@ -88,20 +90,12 @@ const [useProvideBoardTableManager, useBoardTableManager] =
|
|||||||
enableHiding: true,
|
enableHiding: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
accessorKey: "devAddr",
|
accessorKey: "ipAddr",
|
||||||
header: "IP 地址",
|
header: "IP 地址",
|
||||||
cell: ({ row }) => {
|
cell: ({ row }) => {
|
||||||
const device = row.original;
|
const device = row.original;
|
||||||
return isEditMode.value
|
// IP地址设置为不可更改
|
||||||
? h("input", {
|
return h("span", { class: "font-mono" }, device.ipAddr);
|
||||||
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);
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -109,16 +103,28 @@ const [useProvideBoardTableManager, useBoardTableManager] =
|
|||||||
header: "端口",
|
header: "端口",
|
||||||
cell: ({ row }) => {
|
cell: ({ row }) => {
|
||||||
const device = row.original;
|
const device = row.original;
|
||||||
return isEditMode.value
|
// 端口设置为不可更改
|
||||||
? h("input", {
|
return h("span", { class: "font-mono" }, device.port.toString());
|
||||||
type: "number",
|
|
||||||
class: "input input-sm w-full",
|
|
||||||
value: device.port,
|
|
||||||
onInput: (e: Event) => {
|
|
||||||
device.port = parseInt((e.target as HTMLInputElement).value);
|
|
||||||
},
|
},
|
||||||
})
|
enableHiding: true,
|
||||||
: h("span", { class: "font-mono" }, device.port.toString());
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "macAddr",
|
||||||
|
header: "MAC 地址",
|
||||||
|
cell: ({ row }) => {
|
||||||
|
const device = row.original;
|
||||||
|
return h("span", { class: "font-mono text-sm" }, device.macAddr);
|
||||||
|
},
|
||||||
|
enableHiding: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "occupiedUserName",
|
||||||
|
header: "占用用户",
|
||||||
|
cell: ({ row }) => {
|
||||||
|
const device = row.original;
|
||||||
|
const userName = device.occupiedUserName || "未占用";
|
||||||
|
const userClass = device.occupiedUserName ? "text-warning" : "text-success";
|
||||||
|
return h("span", { class: `font-medium ${userClass}` }, userName);
|
||||||
},
|
},
|
||||||
enableHiding: true,
|
enableHiding: true,
|
||||||
},
|
},
|
||||||
@@ -134,9 +140,27 @@ const [useProvideBoardTableManager, useBoardTableManager] =
|
|||||||
header: "状态",
|
header: "状态",
|
||||||
cell: ({ row }) => {
|
cell: ({ row }) => {
|
||||||
const device = row.original;
|
const device = row.original;
|
||||||
const statusText = device.status === 0 ? "忙碌" : "可用";
|
let statusText = "";
|
||||||
const statusClass =
|
let statusClass = "";
|
||||||
device.status === 0 ? "badge-warning" : "badge-success";
|
|
||||||
|
switch (device.status) {
|
||||||
|
case 0: // Disabled
|
||||||
|
statusText = "禁用";
|
||||||
|
statusClass = "badge-error";
|
||||||
|
break;
|
||||||
|
case 1: // Busy
|
||||||
|
statusText = "忙碌";
|
||||||
|
statusClass = "badge-warning";
|
||||||
|
break;
|
||||||
|
case 2: // Available
|
||||||
|
statusText = "可用";
|
||||||
|
statusClass = "badge-success";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
statusText = "未知";
|
||||||
|
statusClass = "badge-neutral";
|
||||||
|
}
|
||||||
|
|
||||||
return h(
|
return h(
|
||||||
"span",
|
"span",
|
||||||
{
|
{
|
||||||
@@ -148,10 +172,11 @@ const [useProvideBoardTableManager, useBoardTableManager] =
|
|||||||
enableHiding: true,
|
enableHiding: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
accessorKey: "version",
|
accessorKey: "firmVersion",
|
||||||
header: "版本号",
|
header: "固件版本",
|
||||||
cell: ({ row }) =>
|
cell: ({ row }) =>
|
||||||
h("span", { class: "font-mono" }, row.original.firmVersion),
|
h("span", { class: "font-mono text-sm" }, row.original.firmVersion),
|
||||||
|
enableHiding: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
accessorKey: "defaultBitstream",
|
accessorKey: "defaultBitstream",
|
||||||
@@ -248,7 +273,7 @@ const [useProvideBoardTableManager, useBoardTableManager] =
|
|||||||
class: "btn btn-error btn-sm",
|
class: "btn btn-error btn-sm",
|
||||||
onClick: async () => {
|
onClick: async () => {
|
||||||
const confirmed = confirm(
|
const confirmed = confirm(
|
||||||
`确定要删除设备 ${device.ipAddr} 吗?`,
|
`确定要删除设备 ${device.boardName || device.ipAddr} 吗?`,
|
||||||
);
|
);
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
await deleteBoard(device.id);
|
await deleteBoard(device.id);
|
||||||
@@ -378,8 +403,8 @@ const [useProvideBoardTableManager, useBoardTableManager] =
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 新增板卡
|
// 新增板卡
|
||||||
async function addBoard(name: string, ipAddr: string, port: number): Promise<boolean> {
|
async function addBoard(name: string): Promise<boolean> {
|
||||||
const result = await boardManager.addBoard(name, ipAddr, port);
|
const result = await boardManager.addBoard(name);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
dialog?.info("新增板卡成功");
|
dialog?.info("新增板卡成功");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div
|
<div
|
||||||
class="min-h-screen bg-base-100 container mx-auto p-6 space-y-6 flex flex-row"
|
class="min-h-screen bg-base-100 container-md mx-auto p-6 space-y-6 flex flex-row"
|
||||||
>
|
>
|
||||||
<ul class="menu bg-base-200 w-56 gap-2 rounded-2xl p-5">
|
<ul class="menu bg-base-200 w-56 gap-2 rounded-2xl p-5">
|
||||||
<li id="1" @click="setActivePage">
|
<li id="1" @click="setActivePage">
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<div class="divider divider-horizontal h-full"></div>
|
<div class="divider divider-horizontal h-full"></div>
|
||||||
<div class="card bg-base-300 w-300 rounded-2xl p-7">
|
<div class="card bg-base-300 flex-1 rounded-2xl p-7">
|
||||||
<div v-if="activePage === 1">
|
<div v-if="activePage === 1">
|
||||||
<UserInfo />
|
<UserInfo />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user