数据库 操作

This commit is contained in:
tanglong 2025-10-30 10:41:42 +08:00
parent 5948f4a97d
commit 9f56229c59
12 changed files with 145 additions and 72 deletions

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.10" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,20 @@
using AiSportsMicrospaceDB.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AiSportsMicrospaceDB.DBContext
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<BasicConfig> BasicConfig { get; set; }
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AiSportsMicrospaceDB.Entities
{
public class BasicConfig
{
// ✅ 主键
public int Id { get; set; }
public string Host { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
}

BIN
DB/AiSportsMicrospaceDB.db Normal file

Binary file not shown.

View File

@ -2,9 +2,9 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Wpf_AiSportsMicrospace.Views"
xmlns:conv="clr-namespace:Wpf_AiSportsMicrospace.Converter"
xmlns:conv="clr-namespace:Wpf_AiSportsMicrospace.Converter">
StartupUri="Views/Main.xaml">
<!--StartupUri="Views/Main.xaml">-->
<!--StartupUri="Views/JumpRope/GroupJumpRope.xaml">-->
<Application.Resources>

View File

@ -1,8 +1,17 @@
using System.Configuration;
using AiSportsMicrospaceDB.DBContext;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.ML.Runtime;
using System.Configuration;
using System.Data;
using System.IO;
using System.Windows;
using Yztob.AiSports.Common;
using Yztob.AiSports.Common.Implement;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Wpf_AiSportsMicrospace.Views;
namespace Wpf_AiSportsMicrospace;
@ -11,16 +20,45 @@ namespace Wpf_AiSportsMicrospace;
/// </summary>
public partial class App : Application
{
public static Microsoft.Extensions.Hosting.IHost AppHost { get; private set; }
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
#if DEBUG
// 初始化 DebugTracker
DebugTracker.Enabled = true;
DebugTracker.Channels.Add(new DiagnosisDebugTrackChannel());
#endif
AppHost = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((context, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.ConfigureServices((context, services) =>
{
var connectionString = context.Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<AppDbContext>(options =>
options.UseSqlite(connectionString));
services.AddSingleton<MainWindow>();
})
.Build();
AppHost.Start();
using (var scope = AppHost.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
//db.Database.EnsureDeleted(); // 删除数据库和表
db.Database.EnsureCreated();
}
// 用 DI 创建窗口
var mainWindow = AppHost.Services.GetRequiredService<MainWindow>();
mainWindow.Show();
}
}

View File

@ -1,4 +1,6 @@
using Emgu.CV.Reg;
using AiSportsMicrospaceDB.DBContext;
using Emgu.CV.Reg;
using Microsoft.EntityFrameworkCore;
using SharpDX.Direct3D9;
using SkiaSharp;
using System.Diagnostics;
@ -6,6 +8,7 @@ using System.Drawing.Imaging;
using System.IO;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
@ -15,7 +18,6 @@ using System.Windows.Shapes;
using System.Windows.Threading;
using Wpf_AiSportsMicrospace.Common;
using Wpf_AiSportsMicrospace.Dto;
using Wpf_AiSportsMicrospace.Service;
using Yztob.AiSports.Inferences.Abstractions;
using Yztob.AiSports.Inferences.Things;
using Yztob.AiSports.Postures.Sports;
@ -41,13 +43,13 @@ public partial class MainWindow : Window
private WriteableBitmap _videoBitmap;
private int _lastFrameNumber = -1;
ConfigService configService = new ConfigService();
private readonly AppDbContext _context;
#endregion
public MainWindow()
public MainWindow(AppDbContext context)
{
InitializeComponent();
_context = context;
// 输出到控制台
_humanPredictor = HumanPredictorFactory.Create(HumanPredictorType.SingleHigh);
@ -104,6 +106,15 @@ public partial class MainWindow : Window
private void ping_Click(object sender, RoutedEventArgs e)
{
var config = _context.BasicConfig.FirstOrDefault();
if (config != null)
{
//var ip = config.Ip;
}
var host = this.host.Text.Trim();
if (string.IsNullOrWhiteSpace(host))
{
@ -264,12 +275,11 @@ public partial class MainWindow : Window
//VoiceBroadcast.PlayTick();
}
private void DrawJumpRope3DPointsWithGlow()
private async Task DrawJumpRope3DPointsWithGlow()
{
if (videoImage == null || overlayCanvas == null) return;
configService.LoadAllConfigs(); // 从文件加载 ConfigDic
ConfigSet jumpRopeConfig;
ConfigSet jumpRopeConfig = new ConfigSet();
double imgWidth = videoImage.ActualWidth;
double imgHeight = videoImage.ActualHeight;
@ -282,7 +292,9 @@ public partial class MainWindow : Window
bool needSaveConfig = false;
if (!configService.ConfigDic.TryGetValue("rope-skipping", out jumpRopeConfig) || jumpRopeConfig.Points.Count == 0)
var config = await _context.BasicConfig.FirstOrDefaultAsync();
if (config!=null)
{
// 没有配置,则生成默认配置
jumpRopeConfig = new ConfigSet { Name = "rope-skipping" };
@ -351,8 +363,7 @@ public partial class MainWindow : Window
// 保存配置(如果是新生成的)
if (needSaveConfig)
{
configService.ConfigDic[jumpRopeConfig.Name] = jumpRopeConfig;
configService.SaveAllConfigs();
}
}

View File

@ -1,52 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Wpf_AiSportsMicrospace.Dto;
namespace Wpf_AiSportsMicrospace.Service
{
/// <summary>
/// 配置服务
/// </summary>
public class ConfigService
{
public Dictionary<string, ConfigSet> ConfigDic { get; set; } = new Dictionary<string, ConfigSet>();
public ConfigService()
{
LoadAllConfigs();
}
/// <summary>
/// 保存配置信息
/// </summary>
/// <param name="filePath"></param>
public void SaveAllConfigs(string fileName = "configs.json")
{
string basePath = AppContext.BaseDirectory; // 当前运行目录
string filePath = Path.Combine(basePath, fileName);
var json = JsonSerializer.Serialize(ConfigDic, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(filePath, json);
}
/// <summary>
/// 加载配置信息
/// </summary>
/// <param name="fileName"></param>
public void LoadAllConfigs(string fileName = "configs.json")
{
string basePath = AppContext.BaseDirectory;
string filePath = Path.Combine(basePath, fileName);
if (!File.Exists(filePath)) return;
var json = File.ReadAllText(filePath);
ConfigDic = JsonSerializer.Deserialize<Dictionary<string, ConfigSet>>(json);
}
}
}

View File

@ -29,7 +29,6 @@ using Wpf_AiSportsMicrospace.Common;
using Wpf_AiSportsMicrospace.Dto;
using Wpf_AiSportsMicrospace.Enum;
using Wpf_AiSportsMicrospace.MyUserControl;
using Wpf_AiSportsMicrospace.Service;
using WpfAnimatedGif;
using Yztob.AiSports.Common;
using Yztob.AiSports.Common.Implement;

View File

@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
<None Remove="Resources\Font\myFontFamily.ttf" />
<None Remove="Resources\Img\Album\1.gif" />
<None Remove="Resources\Img\Album\1.jpg" />
@ -97,6 +98,12 @@
<None Remove="Resources\Music\raisehand.mp3" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\Img\Album\leight.gif">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
@ -141,6 +148,11 @@
<PackageReference Include="FFmpeg.AutoGen.Abstractions" Version="7.1.1" />
<PackageReference Include="FFmpeg.AutoGen.Bindings.DynamicallyLoaded" Version="7.1.1" />
<PackageReference Include="HandyControl" Version="3.5.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.10" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.10" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.10" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.10" />
<PackageReference Include="Microsoft.Management.Infrastructure" Version="3.0.0" />
<PackageReference Include="Microsoft.ML" Version="4.0.2" />
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.23.0" />
@ -153,6 +165,10 @@
<PackageReference Include="WpfAnimatedGif" Version="2.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AiSportsMicrospaceDB\AiSportsMicrospaceDB.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Yunzhi.Library">
<HintPath>..\sdks\Yunzhi.Library.dll</HintPath>

View File

@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35828.75 d17.13
# Visual Studio Version 18
VisualStudioVersion = 18.0.11111.16 d18.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wpf_AiSportsMicrospace", "Wpf_AiSportsMicrospace.csproj", "{345052A8-36AD-4739-80F6-991A4DD84ADC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AiSportsMicrospaceDB", "..\AiSportsMicrospaceDB\AiSportsMicrospaceDB.csproj", "{76186799-4B51-4DE3-9F61-E0F4B5A40553}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{345052A8-36AD-4739-80F6-991A4DD84ADC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{345052A8-36AD-4739-80F6-991A4DD84ADC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{345052A8-36AD-4739-80F6-991A4DD84ADC}.Release|Any CPU.Build.0 = Release|Any CPU
{76186799-4B51-4DE3-9F61-E0F4B5A40553}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76186799-4B51-4DE3-9F61-E0F4B5A40553}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76186799-4B51-4DE3-9F61-E0F4B5A40553}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76186799-4B51-4DE3-9F61-E0F4B5A40553}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=AiSportsMicrospaceDB.db"
}
}