You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
281 lines
6.8 KiB
281 lines
6.8 KiB
3 years ago
|
using EC.Entity.CameraInfo;
|
||
|
using EC.Service.CameraInfo;
|
||
|
using EC.UsingEventAggregator;
|
||
|
using ECMonitor.Code.Models;
|
||
|
using ECMonitor.MVVM;
|
||
|
using Prism.Commands;
|
||
|
using Prism.Events;
|
||
|
using Prism.Regions;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Collections.ObjectModel;
|
||
|
using System.Threading;
|
||
|
using System.Windows.Controls;
|
||
|
|
||
|
namespace ECMonitor.Page.SystemSet
|
||
|
{
|
||
|
public class ManageIpcViewModel : BaseModel
|
||
|
{
|
||
|
private ManageIpc ThatPage { get; set; }
|
||
|
|
||
|
public ObservableCollection<IpcInfo> InfoList { get; set; } = new();
|
||
|
|
||
|
private ReaderWriterLockSlim InfoListrwl { get; } = new();
|
||
|
|
||
|
private List<int> AddNumberList { get; } = new();
|
||
|
|
||
|
private List<int> UpdateNumberList { get; } = new();
|
||
|
|
||
|
public ObservableCollection<string> NvrNameList { get; set; } = new();
|
||
|
|
||
|
#region Command
|
||
|
|
||
|
public DelegateCommand<UserControl> LoadedCommand { get; private set; }
|
||
|
|
||
|
public DelegateCommand AddCommand { get; private set; }
|
||
|
|
||
|
public DelegateCommand DeleteCommand { get; private set; }
|
||
|
|
||
|
public ExtendCommand<DataGridCellEditEndingEventArgs> UpdateCommand { get; private set; }
|
||
|
|
||
|
public DelegateCommand SaveCommand { get; private set; }
|
||
|
|
||
|
public DelegateCommand RevokeCommand { get; private set; }
|
||
|
|
||
|
public DelegateCommand RefreshCommand { get; private set; }
|
||
|
|
||
|
public DelegateCommand ImportCommand { get; private set; }
|
||
|
|
||
|
public DelegateCommand ExportCommand { get; private set; }
|
||
|
|
||
|
#endregion Command
|
||
|
|
||
|
public ManageIpcViewModel(IRegionManager regionManager, IEventAggregator ea) : base(regionManager, ea)
|
||
|
{
|
||
|
LoadedCommand = new DelegateCommand<UserControl>(LoadedPageAction);
|
||
|
|
||
|
AddCommand = new DelegateCommand(AddInfoAction);
|
||
|
DeleteCommand = new DelegateCommand(DeleteInfoAction);
|
||
|
UpdateCommand = new ExtendCommand<DataGridCellEditEndingEventArgs>(UpdateInfoAction);
|
||
|
SaveCommand = new DelegateCommand(SaveInfosAction);
|
||
|
RevokeCommand = new DelegateCommand(RevokeInfoAction);
|
||
|
RefreshCommand = new DelegateCommand(RefreshAction);
|
||
|
ImportCommand = new DelegateCommand(ImportInfosAction);
|
||
|
ExportCommand = new DelegateCommand(ExportInfosAction);
|
||
|
_ea.GetEvent<SendIpcInfoListEvent>().Subscribe(SubscribeIpcInfoListAction);
|
||
|
}
|
||
|
|
||
|
#region Event Action
|
||
|
|
||
|
private void LoadedPageAction(UserControl view)
|
||
|
{
|
||
|
if (ThatPage != null) { return; }
|
||
|
ThatPage = (ManageIpc)view;
|
||
|
RefreshAction();
|
||
|
}
|
||
|
|
||
|
#endregion Event Action
|
||
|
|
||
|
#region Operate Action
|
||
|
|
||
|
private void AddInfoAction()
|
||
|
{
|
||
|
IpcInfo info = new();
|
||
|
info.Number = GetInfoNewNumber();
|
||
|
info.IpcName = $"摄像机#{info.Number}";
|
||
|
info.IpcEnableBool = true;
|
||
|
OperateInfoListByLock(() => { AddInfo(info); });
|
||
|
|
||
|
DataGrid infoGrid = ThatPage.InfoGrid;
|
||
|
infoGrid.SelectedIndex = infoGrid.Items.Count - 1;
|
||
|
}
|
||
|
|
||
|
private async void DeleteInfoAction()
|
||
|
{
|
||
|
DataGrid infoGrid = ThatPage.InfoGrid;
|
||
|
IpcInfo info = (IpcInfo)infoGrid.SelectedItem;
|
||
|
int index = ThatPage.InfoGrid.SelectedIndex;
|
||
|
if (info == null) { return; }
|
||
|
|
||
|
if (info.IpcId <= 0)
|
||
|
{
|
||
|
OperateInfoListByLock(() => { RemoveInfo(info); });
|
||
|
infoGrid.SelectedIndex = (index < infoGrid.Items.Count) ? index : infoGrid.Items.Count - 1;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
using IpcInfoContext ctx = new();
|
||
|
_ = ctx.Remove(info);
|
||
|
int ret = await ctx.SaveChangesAsync();
|
||
|
if (ret > 0)
|
||
|
{
|
||
|
OperateInfoListByLock(() => { RemoveInfo(info); });
|
||
|
infoGrid.SelectedIndex = (index < infoGrid.Items.Count) ? index : infoGrid.Items.Count - 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void UpdateInfoAction(DataGridCellEditEndingEventArgs e)
|
||
|
{
|
||
|
if (e.EditAction != DataGridEditAction.Commit) { return; }
|
||
|
|
||
|
var elm = e.EditingElement;
|
||
|
DataGridRow row = e.Row;
|
||
|
DataGridColumn column = e.Column;
|
||
|
IpcInfo info = (IpcInfo)row.Item;
|
||
|
if (info != null && info.IpcId > 0)
|
||
|
{
|
||
|
if (!UpdateNumberList.Exists(item => item.Equals(info.Number)))
|
||
|
{
|
||
|
UpdateNumberList.Add(info.Number);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private async void SaveInfosAction()
|
||
|
{
|
||
|
//ValidationError validationError = WPFAssist.GetDataGridRowsFirstError(ThatPage.InfoGrid);
|
||
|
//if (WPFAssist.GetDataGridRowsHasError(ThatPage.InfoGrid)) { return; }
|
||
|
|
||
|
List<IpcInfo> list = null;
|
||
|
using IpcInfoContext ctx = new();
|
||
|
int ret = 0;
|
||
|
|
||
|
if (AddNumberList.Count > 0)
|
||
|
{
|
||
|
list ??= GetInfoList();
|
||
|
foreach (int number in AddNumberList)
|
||
|
{
|
||
|
IpcInfo info = list.Find(item => item.Number.Equals(number));
|
||
|
if (info != null) { _ = ctx.Add(info); }
|
||
|
}
|
||
|
ret = await ctx.SaveChangesAsync();
|
||
|
if (ret > 0) { AddNumberList.Clear(); }
|
||
|
else { return; }
|
||
|
}
|
||
|
|
||
|
if (UpdateNumberList.Count > 0)
|
||
|
{
|
||
|
list ??= GetInfoList();
|
||
|
foreach (int number in UpdateNumberList)
|
||
|
{
|
||
|
IpcInfo info = list.Find(item => item.Number.Equals(number));
|
||
|
if (info != null) { _ = ctx.Update(info); }
|
||
|
}
|
||
|
ret = await ctx.SaveChangesAsync();
|
||
|
if (ret > 0) { UpdateNumberList.Clear(); }
|
||
|
else { return; }
|
||
|
}
|
||
|
|
||
|
RefreshAction();
|
||
|
}
|
||
|
|
||
|
private void RevokeInfoAction()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
private void RefreshAction()
|
||
|
{
|
||
|
OperateInfoListByLock(() =>
|
||
|
{
|
||
|
AddNumberList.Clear();
|
||
|
UpdateNumberList.Clear();
|
||
|
|
||
|
LoadNvrNameList();
|
||
|
LoadInfoList();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private void ImportInfosAction()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
private void ExportInfosAction()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
#endregion Operate Action
|
||
|
|
||
|
#region Operate InfoList
|
||
|
|
||
|
private void OperateInfoListByLock(Action action)
|
||
|
{
|
||
|
if (InfoListrwl.IsWriteLockHeld) { return; }
|
||
|
if (!InfoListrwl.TryEnterWriteLock(100)) { return; }
|
||
|
try
|
||
|
{
|
||
|
action?.Invoke();
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
InfoListrwl.ExitWriteLock();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private async void LoadInfoList()
|
||
|
{
|
||
|
InfoList.Clear();
|
||
|
using IpcInfoContext ctx = new();
|
||
|
List<IpcInfo> list = await ctx.GetListAsync();
|
||
|
int number = 0;
|
||
|
foreach (IpcInfo info in list)
|
||
|
{
|
||
|
info.Number = ++number;
|
||
|
InfoList.Add(info);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void AddInfo(IpcInfo info)
|
||
|
{
|
||
|
InfoList.Add(info);
|
||
|
if (!AddNumberList.Exists(item => item.Equals(info.Number)))
|
||
|
{
|
||
|
AddNumberList.Add(info.Number);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void RemoveInfo(IpcInfo info)
|
||
|
{
|
||
|
_ = InfoList.Remove(info);
|
||
|
_ = AddNumberList.RemoveAll(item => item.Equals(info.Number));
|
||
|
_ = UpdateNumberList.RemoveAll(item => item.Equals(info.Number));
|
||
|
}
|
||
|
|
||
|
private List<IpcInfo> GetInfoList()
|
||
|
{
|
||
|
return new(InfoList);
|
||
|
}
|
||
|
|
||
|
private int GetInfoNewNumber()
|
||
|
{
|
||
|
int number = InfoList.Count > 0 ? InfoList[^1].Number + 1 : 1;
|
||
|
return number;
|
||
|
}
|
||
|
|
||
|
private void SubscribeIpcInfoListAction(List<IpcInfo> infoList)
|
||
|
{
|
||
|
OperateInfoListByLock(() =>
|
||
|
{
|
||
|
foreach (IpcInfo item in infoList)
|
||
|
{
|
||
|
item.Number = GetInfoNewNumber();
|
||
|
item.IpcName = $"摄像机#{item.Number}";
|
||
|
AddInfo(item);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
#endregion Operate InfoList
|
||
|
|
||
|
#region Operate OtherList
|
||
|
|
||
|
private async void LoadNvrNameList()
|
||
|
{
|
||
|
NvrNameList.Clear();
|
||
|
using NvrInfoContext ctx = new();
|
||
|
List<string> list = await ctx.GetNvrNameListAsync();
|
||
|
list.ForEach(item => { NvrNameList.Add(item); });
|
||
|
}
|
||
|
|
||
|
#endregion Operate OtherList
|
||
|
}
|
||
|
}
|