Mqtt服务器添加,编辑,删除调试完成

This commit is contained in:
2025-07-05 21:49:41 +08:00
parent b69acbba6e
commit 1f57a94c03
5 changed files with 56 additions and 40 deletions

View File

@@ -72,7 +72,7 @@ public partial class App : Application
services.AddSingleton<IDialogService, DialogService>(); services.AddSingleton<IDialogService, DialogService>();
services.AddSingleton<GrowlNotificationService>(); services.AddSingleton<GrowlNotificationService>();
services.AddHostedService<S7BackgroundService>(); // Register as HostedService services.AddHostedService<S7BackgroundService>(); // Register as HostedService
services.AddHostedService<MqttBackgroundService>(); // services.AddHostedService<MqttBackgroundService>();
services.AddSingleton<MainViewModel>(); services.AddSingleton<MainViewModel>();
services.AddSingleton<HomeViewModel>(); services.AddSingleton<HomeViewModel>();
services.AddSingleton<DevicesViewModel>(); services.AddSingleton<DevicesViewModel>();

View File

@@ -188,4 +188,11 @@ public class MenuRepository
return result?.CopyTo<MenuBean>(); return result?.CopyTo<MenuBean>();
} }
} }
public async Task<MenuBean> GetMainMenuByName(string name)
{
using var db = DbContext.GetInstance();
var dbMenu= await db.Queryable<DbMenu>().FirstAsync(m => m.Name == name && m.Type == MenuType.MainMenu);
return dbMenu.CopyTo<MenuBean>();
}
} }

View File

@@ -36,7 +36,9 @@ public class MqttRepository
stopwatch.Start(); stopwatch.Start();
using (var _db = DbContext.GetInstance()) using (var _db = DbContext.GetInstance())
{ {
var result = await _db.Queryable<DbMqtt>().In(id).SingleAsync(); var result = await _db.Queryable<DbMqtt>()
.In(id)
.SingleAsync();
stopwatch.Stop(); stopwatch.Stop();
Logger.Info($"根据ID '{id}' 获取Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms"); Logger.Info($"根据ID '{id}' 获取Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms");
return result.CopyTo<Mqtt>(); return result.CopyTo<Mqtt>();
@@ -53,10 +55,12 @@ public class MqttRepository
stopwatch.Start(); stopwatch.Start();
using (var _db = DbContext.GetInstance()) using (var _db = DbContext.GetInstance())
{ {
var result = await _db.Queryable<DbMqtt>().ToListAsync(); var result = await _db.Queryable<DbMqtt>()
.ToListAsync();
stopwatch.Stop(); stopwatch.Stop();
Logger.Info($"获取所有Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms"); Logger.Info($"获取所有Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms");
return result.Select(m=>m.CopyTo<Mqtt>()).ToList(); return result.Select(m => m.CopyTo<Mqtt>())
.ToList();
} }
} }
@@ -69,19 +73,21 @@ public class MqttRepository
{ {
Stopwatch stopwatch = new Stopwatch(); Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
using (var db = DbContext.GetInstance()) using var db = DbContext.GetInstance();
{
await db.BeginTranAsync(); await db.BeginTranAsync();
try try
{ {
var result = await db.Insertable(mqtt.CopyTo<DbMqtt>()).ExecuteReturnIdentityAsync(); var result = await db.Insertable(mqtt.CopyTo<DbMqtt>())
.ExecuteReturnIdentityAsync();
var mqttMenu = await _menuRepository.GetMainMenuByName("Mqtt服务器");
// Add menu entry // Add menu entry
var menu = new MenuBean() var menu = new MenuBean()
{ {
Name = mqtt.Name, Name = mqtt.Name,
Icon = SegoeFluentIcons.Wifi.Glyph, Icon = SegoeFluentIcons.Wifi.Glyph,
Type = MenuType.MqttMenu, Type = MenuType.MqttMenu,
DataId = result DataId = result,
ParentId = mqttMenu.Id,
}; };
await _menuRepository.Add(menu, db); await _menuRepository.Add(menu, db);
await db.CommitTranAsync(); await db.CommitTranAsync();
@@ -96,7 +102,6 @@ public class MqttRepository
throw; throw;
} }
} }
}
/// <summary> /// <summary>
/// 更新Mqtt配置 /// 更新Mqtt配置
@@ -112,7 +117,8 @@ public class MqttRepository
await db.BeginTranAsync(); await db.BeginTranAsync();
try try
{ {
var result = await db.Updateable(mqtt.CopyTo<DbMqtt>()).ExecuteCommandAsync(); var result = await db.Updateable(mqtt.CopyTo<DbMqtt>())
.ExecuteCommandAsync();
// Update menu entry // Update menu entry
var menu = await _menuRepository.GetMenuByDataId(mqtt.Id, MenuType.MqttMenu); var menu = await _menuRepository.GetMenuByDataId(mqtt.Id, MenuType.MqttMenu);
if (menu != null) if (menu != null)
@@ -120,6 +126,7 @@ public class MqttRepository
menu.Name = mqtt.Name; menu.Name = mqtt.Name;
await _menuRepository.Edit(menu, db); await _menuRepository.Edit(menu, db);
} }
await db.CommitTranAsync(); await db.CommitTranAsync();
stopwatch.Stop(); stopwatch.Stop();
Logger.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); Logger.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
@@ -148,13 +155,12 @@ public class MqttRepository
await db.BeginTranAsync(); await db.BeginTranAsync();
try try
{ {
var result = await db.Deleteable<DbMqtt>().In(mqtt.Id).ExecuteCommandAsync(); var result = await db.Deleteable<DbMqtt>()
.In(mqtt.Id)
.ExecuteCommandAsync();
// Delete menu entry // Delete menu entry
var menu = await _menuRepository.GetMenuByDataId(mqtt.Id, MenuType.MqttMenu); var menu = await _menuRepository.GetMenuByDataId(mqtt.Id, MenuType.MqttMenu);
if (menu != null)
{
await _menuRepository.DeleteMenu(menu, db); await _menuRepository.DeleteMenu(menu, db);
}
await db.CommitTranAsync(); await db.CommitTranAsync();
stopwatch.Stop(); stopwatch.Stop();
Logger.Info($"删除Mqtt配置ID '{mqtt.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms"); Logger.Info($"删除Mqtt配置ID '{mqtt.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");

View File

@@ -150,13 +150,16 @@ public partial class MainViewModel : ViewModelBase
{ {
switch (menu.Type) switch (menu.Type)
{ {
// 导航到一级菜单
case MenuType.MainMenu: case MenuType.MainMenu:
menu.ViewModel = DataServicesHelper.GetMainViewModel(menu.Name); menu.ViewModel = DataServicesHelper.GetMainViewModel(menu.Name);
break; break;
// 导航到设备下面的菜单
case MenuType.DeviceMenu: case MenuType.DeviceMenu:
menu.ViewModel = App.Current.Services.GetRequiredService<DeviceDetailViewModel>(); menu.ViewModel = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
menu.Data = _dataServices.Devices.FirstOrDefault(d => d.Id == menu.DataId); menu.Data = _dataServices.Devices.FirstOrDefault(d => d.Id == menu.DataId);
break; break;
// 导航到变量表菜单
case MenuType.VariableTableMenu: case MenuType.VariableTableMenu:
VariableTableViewModel varTableVM = VariableTableViewModel varTableVM =
App.Current.Services.GetRequiredService<VariableTableViewModel>(); App.Current.Services.GetRequiredService<VariableTableViewModel>();
@@ -166,16 +169,15 @@ public partial class MainViewModel : ViewModelBase
varTableVM.IsLoadCompletion = false; varTableVM.IsLoadCompletion = false;
menu.ViewModel = varTableVM; menu.ViewModel = varTableVM;
menu.Data = varTableVM.VariableTable; menu.Data = varTableVM.VariableTable;
break; break;
// 导航到添加变量表的菜单
case MenuType.AddVariableTableMenu: case MenuType.AddVariableTableMenu:
await AddVariableTable(menu); await AddVariableTable(menu);
return;
break; break;
} }
if (menu.Type == MenuType.AddVariableTableMenu)
return;
if (menu.ViewModel != null) if (menu.ViewModel != null)
{ {

View File

@@ -63,6 +63,7 @@ public partial class MqttsViewModel : ViewModelBase
await _mqttRepository.Add(mqtt); await _mqttRepository.Add(mqtt);
MessageHelper.SendLoadMessage(LoadTypes.Mqtts); MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
MessageHelper.SendLoadMessage(LoadTypes.Menu);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -87,8 +88,8 @@ public partial class MqttsViewModel : ViewModelBase
if (isDel) if (isDel)
{ {
await _mqttRepository.Delete(SelectedMqtt); await _mqttRepository.Delete(SelectedMqtt);
MessageHelper.SendLoadMessage(LoadTypes.Mqtts); MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
MessageHelper.SendLoadMessage(LoadTypes.Menu);
NotificationHelper.ShowMessage($"删除MQTT成功,MQTT名{SelectedMqtt.Name}", NotificationType.Success); NotificationHelper.ShowMessage($"删除MQTT成功,MQTT名{SelectedMqtt.Name}", NotificationType.Success);
} }
} }