using Autofac;
|
using Bro.Common.Base;
|
using Bro.Common.Factory;
|
using Bro.Common.Helper;
|
using Bro.Common.Interface;
|
using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Drawing;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
|
namespace Bro.Common.Model.Forms
|
{
|
public partial class FrmDeviceOpConfigEditor : Form
|
{
|
public FrmDeviceOpConfigEditor()
|
{
|
InitializeComponent();
|
}
|
|
//private DeviceOpBind bind = null;
|
public DeviceOpBind Bind { get; set; }
|
|
IOperationConfig backOpConfig = new OperationConfigBase();
|
List<IDevice> deviceList = null;
|
IDevice currentDevice = null;
|
public FrmDeviceOpConfigEditor(DeviceOpBind bind, bool isEnableSelectDevice = true)
|
{
|
InitializeComponent();
|
|
InitialDeviceCbo();
|
|
Bind = bind;
|
if (!string.IsNullOrWhiteSpace(bind.Device))
|
{
|
cboDevice.SelectedValue = bind.Device;
|
|
currentDevice = deviceList.FirstOrDefault(u => u.Id == bind.Device);
|
if (currentDevice != null)
|
{
|
var attr = currentDevice.GetType().GetCustomAttribute<DeviceAttribute>();
|
if (attr != null)
|
{
|
backOpConfig = ConfigFactory.GetOperationConfig(attr.TypeCode);
|
}
|
}
|
}
|
else
|
{
|
if (cboDevice.Items.Count > 0)
|
cboDevice.SelectedIndex = 0;
|
}
|
|
if (isEnableSelectDevice)
|
{
|
cboDevice.Enabled = true;
|
cboDevice.SelectedIndexChanged += CboDevice_SelectedIndexChanged;
|
}
|
else
|
{
|
cboDevice.Enabled = false;
|
}
|
|
if (bind.OpConfig == null)
|
{
|
bind.OpConfig = new OperationConfigBase();
|
|
if (currentDevice != null)
|
{
|
var attr = currentDevice.GetType().GetCustomAttribute<DeviceAttribute>();
|
if (attr != null)
|
bind.OpConfig = ConfigFactory.GetOperationConfig(attr.TypeCode);
|
}
|
}
|
|
bind.OpConfig.DeviceId = bind.Device;
|
|
backOpConfig.DataFrom(bind.OpConfig);
|
|
propGrid.SelectedObject = bind.OpConfig;
|
|
}
|
|
private void CboDevice_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
ChangeOpConfigByDevice();
|
}
|
|
private void ChangeOpConfigByDevice()
|
{
|
var device = deviceList.FirstOrDefault(u => u.Id == cboDevice.SelectedValue.ToString());
|
if (device != null)
|
{
|
var attr = device.GetType().GetCustomAttribute<DeviceAttribute>();
|
if (attr != null)
|
{
|
var opConfig = ConfigFactory.GetOperationConfig(attr.TypeCode);
|
opConfig.DeviceId = device.Id;
|
propGrid.SelectedObject = opConfig;
|
}
|
}
|
}
|
|
private void InitialDeviceCbo()
|
{
|
using (var scope = GlobalVar.Container.BeginLifetimeScope())
|
{
|
deviceList = scope.Resolve<List<IDevice>>();
|
if (deviceList.Count > 0)
|
{
|
UIHelper.SetCombo(cboDevice, new List<ISimpleDevice>(deviceList), "Name", "Id");
|
}
|
}
|
}
|
|
private void btnCancel_Click(object sender, EventArgs e)
|
{
|
Bind.OpConfig = backOpConfig;
|
this.DialogResult = DialogResult.Cancel;
|
}
|
|
private void btnConfirm_Click(object sender, EventArgs e)
|
{
|
Bind.Device = cboDevice.SelectedValue.ToString();
|
Bind.OpConfig = propGrid.SelectedObject as IOperationConfig;
|
Bind.OpConfig.DeviceId = Bind.Device;
|
this.DialogResult = DialogResult.OK;
|
}
|
|
private void btnReset_Click(object sender, EventArgs e)
|
{
|
ChangeOpConfigByDevice();
|
}
|
}
|
}
|