首次提交:添加src文件夹代码
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
using Cowain.Bake.Common;
|
||||
using Cowain.Bake.Model;
|
||||
using Prism.Regions;
|
||||
using System.Collections.ObjectModel;
|
||||
using Unity;
|
||||
using Cowain.Bake.BLL;
|
||||
using Prism.Commands;
|
||||
using JSON = Newtonsoft.Json.JsonConvert;
|
||||
using System;
|
||||
using Cowain.Bake.Common.Core;
|
||||
using HandyControl.Controls;
|
||||
using System.Windows;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Cowain.Bake.UI.FactoryMaintenance.ViewModels
|
||||
{
|
||||
public class DeviceManagementViewModel : ViewModelBase, INavigationAware
|
||||
{
|
||||
private bool _autoRefresh;
|
||||
public bool AutoRefresh
|
||||
{
|
||||
get { return _autoRefresh; }
|
||||
set { SetProperty(ref _autoRefresh, value); }
|
||||
}
|
||||
|
||||
private TDeviceConfig _editDevice;
|
||||
public TDeviceConfig EditDevice
|
||||
{
|
||||
get => _editDevice ?? (_editDevice = new TDeviceConfig());
|
||||
set
|
||||
{
|
||||
SetProperty(ref _editDevice, value);
|
||||
}
|
||||
}
|
||||
|
||||
private TDeviceConfig selectDevice;
|
||||
public TDeviceConfig SelectDevice
|
||||
{
|
||||
get => selectDevice ?? (selectDevice = new TDeviceConfig());
|
||||
set
|
||||
{
|
||||
SetProperty(ref selectDevice, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ObservableCollection<TDeviceConfig> _deviceList;
|
||||
public ObservableCollection<TDeviceConfig> DeviceList
|
||||
{
|
||||
//get { return _taskList; }
|
||||
get => _deviceList ?? (_deviceList = new ObservableCollection<TDeviceConfig>());
|
||||
set { SetProperty(ref _deviceList, value); }
|
||||
}
|
||||
public DeviceManagementViewModel(IUnityContainer unityContainer, IRegionManager regionManager) : base(unityContainer, regionManager)
|
||||
{
|
||||
AutoRefresh = true;
|
||||
this.PageTitle = "设备管理";
|
||||
_unityContainer = unityContainer;
|
||||
Task.Run(() => AsyncRefreshTask());
|
||||
}
|
||||
|
||||
public DelegateCommand SelectCommand => new DelegateCommand(() =>
|
||||
{
|
||||
if (SelectDevice != null && SelectDevice.Id > 0)
|
||||
{
|
||||
var json = PrettifyJson(SelectDevice.Json);
|
||||
if (json.Item1)
|
||||
{
|
||||
SelectDevice.Json = json.Item2;
|
||||
//深拷贝
|
||||
EditDevice = JSON.DeserializeObject<TDeviceConfig>(JSON.SerializeObject(SelectDevice));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public DelegateCommand VerifyCommand => new DelegateCommand(() =>
|
||||
{
|
||||
IsVerify(true);
|
||||
});
|
||||
|
||||
bool IsVerify(bool b = false)
|
||||
{
|
||||
if (PrettifyJson(EditDevice.Json).Item1)
|
||||
{
|
||||
if (b)
|
||||
{
|
||||
Growl.Success("有效的JSON格式!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Growl.Error("不是有效的JSON格式, 请重新输入!");
|
||||
return false;
|
||||
}
|
||||
public DelegateCommand EditCommand => new DelegateCommand(() =>
|
||||
{
|
||||
if (!IsVerify())
|
||||
{
|
||||
return;
|
||||
}
|
||||
var result = HandyControl.Controls.MessageBox.Ask($@"是否确定要修改设备信息?", "操作提示");
|
||||
if (result == System.Windows.MessageBoxResult.Cancel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var res = _unityContainer.Resolve<DeviceConfigService>().Update(EditDevice);
|
||||
if (res > 0)
|
||||
{
|
||||
Growl.Success("设备信息修改完成!");
|
||||
Refresh();
|
||||
}
|
||||
else
|
||||
{
|
||||
Growl.Error("设备信息修改失败!");
|
||||
}
|
||||
|
||||
if (!EditDevice.Enable)
|
||||
{
|
||||
CommonCoreHelper.Instance.BlockStatusColor.Add(EditDevice); //刷新主界面的PLC状态
|
||||
}
|
||||
});
|
||||
|
||||
public async void AsyncRefreshTask()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
if (AutoRefresh)
|
||||
{
|
||||
Application.Current?.Dispatcher?.Invoke(new Action(() =>
|
||||
{
|
||||
Refresh();
|
||||
}));
|
||||
}
|
||||
|
||||
await Task.Delay(2000);
|
||||
}
|
||||
}
|
||||
|
||||
private (bool, string) PrettifyJson(string raw)
|
||||
{
|
||||
(bool, string) result = (true, "");
|
||||
try
|
||||
{
|
||||
|
||||
result.Item2 = Newtonsoft.Json.Linq.JToken.Parse(raw).ToString(Newtonsoft.Json.Formatting.Indented);
|
||||
// 如果已经是 JSON 字符串,先解析再序列化即可
|
||||
//var doc = System.Text.Json.JsonDocument.Parse(raw);
|
||||
//return System.Text.Json.JsonSerializer.Serialize(doc.RootElement,
|
||||
// new System.Text.Json.JsonSerializerOptions
|
||||
// {
|
||||
// WriteIndented = true
|
||||
// });
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
// 万一不是合法 JSON,直接原样返回
|
||||
LogHelper.Instance.GetCurrentClassWarn($"{raw},{ex.Message}");
|
||||
result.Item1 = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public override void Refresh()
|
||||
{
|
||||
DeviceList.Clear();
|
||||
var models = _unityContainer.Resolve<DeviceConfigService>().GetAll();
|
||||
models.ForEach(x => DeviceList.Add(x));
|
||||
}
|
||||
|
||||
|
||||
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user