/// <summary> /// 将初始化参数加载到界面上 /// </summary> void InitParam2UI() { if (null == _method) { return; } if (!(_method is IJFInitializable)) { return; } IJFInitializable initor = _method as IJFInitializable; string[] initName = initor.InitParamNames; if (null == initName) { return; } for (int i = 0; i < initName.Length; i++) { _lstUcParam[i].SetParamValue(initor.GetInitParamValue(initName[i])); } }
void AdjustView() { timerFlush.Enabled = false; tpRealtime.Controls.Clear(); tpInitCfg.Controls.Clear(); _lstUcParam.Clear(); if (null == _method) { Label lb1 = new Label(); lb1.Text = "方法对象未设置"; tpRealtime.Controls.Add(lb1); Label lb2 = new Label(); lb2.Text = "方法对象未设置"; tpInitCfg.Controls.Add(lb1); return; } if (_method is IJFRealtimeUIProvider) { JFRealtimeUI ui = (_method as IJFRealtimeUIProvider).GetRealtimeUI(); if (null != ui) { ui.Dock = DockStyle.Fill; tpRealtime.Controls.Add(ui); timerFlush.Enabled = true; } } else { UcCommonMethodRtUi ui = new UcCommonMethodRtUi(); ui.Dock = DockStyle.Fill; ui.SetMethod(_method); tpRealtime.Controls.Add(ui); timerFlush.Enabled = true; } int locX = 3, locY = 3; if (_method is IJFConfigUIProvider)//提供参数配置界面 { Button btShowCfg = new Button(); btShowCfg.Text = "参数配置"; btShowCfg.Location = new Point(locX, locY); tpInitCfg.Controls.Add(btShowCfg); btShowCfg.Click += OnButtonClick_ShowCfgUI; locY = btShowCfg.Bottom + 2; } if (_method is IJFInitializable)//初始参数可序列化对象 { IJFInitializable initor = _method as IJFInitializable; string[] initNames = initor.InitParamNames; if (null != initNames && initNames.Length > 0) { isInitParamEditting = false; btSetSaveInit = new Button(); btSetSaveInit.Text = "编辑初始化参数"; btSetSaveInit.Location = new Point(locX, locY); btSetSaveInit.Click += OnButtonClick_InitParamSetSave; tpInitCfg.Controls.Add(btSetSaveInit); btCancelInit = new Button(); btCancelInit.Text = "取消"; btCancelInit.Location = new Point(btSetSaveInit.Right + 2, locY); btCancelInit.Click += OnButtonClick_InitParamCancel; tpInitCfg.Controls.Add(btCancelInit); btCancelInit.Enabled = false; locY = btCancelInit.Bottom + 2; for (int i = 0; i < initNames.Length; i++) { string initName = initNames[i]; JFParamDescribe pd = initor.GetInitParamDescribe(initName); UcJFParamEdit ucParam = new UcJFParamEdit(); ucParam.Width = tpInitCfg.Width - 100; ucParam.Location = new Point(locX, locY); ucParam.SetParamDesribe(pd); ucParam.SetParamValue(initor.GetInitParamValue(initName)); ucParam.IsValueReadOnly = true; _lstUcParam.Add(ucParam); tpInitCfg.Controls.Add(ucParam); ucParam.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; locY = ucParam.Bottom + 2; } InitParam2UI(); } } }
/// <summary>添加新设备</summary> private void btAdd_Click(object sender, EventArgs e) { FormCreateInitor fm = new FormCreateInitor(); fm.Text = "创建" + InitorCaption + "对象"; fm.MatchType = InitorType; if (DialogResult.OK == fm.ShowDialog()) { IJFInitializable newDevice = fm.Initor; string devID = fm.ID; JFHubCenter.Instance.InitorManager.Add(devID, newDevice); DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell cellID = new DataGridViewTextBoxCell(); cellID.Value = devID; row.Cells.Add(cellID); DataGridViewTextBoxCell cellModel = new DataGridViewTextBoxCell(); cellModel.Value = JFinitializerHelper.DispalyTypeName(newDevice.GetType()); row.Cells.Add(cellModel); DataGridViewTextBoxCell cellType = new DataGridViewTextBoxCell(); cellType.Value = newDevice.GetType().Name; row.Cells.Add(cellType); dgvDevs.Rows.Add(row); newDevice.Initialize(); if (!newDevice.IsInitOK) { btInit.Enabled = true; row.DefaultCellStyle.ForeColor = Color.Red; } else { btInit.Enabled = false; } /// 更新参数到界面 dgvDevs.Rows[dgvDevs.Rows.Count - 1].Selected = true; RemoveAllPEs(); string[] iniParamNames = newDevice.InitParamNames; if (null == iniParamNames) { btEditSave.Enabled = false; return; } btEditSave.Enabled = true; int locY = btInit.Location.Y + btInit.Size.Height + 5; foreach (string ipName in iniParamNames) { UcJFParamEdit pe = new UcJFParamEdit(); pe.Width = gbParams.Width - 1; pe.Location = new Point(4, locY); pe.SetParamDesribe(newDevice.GetInitParamDescribe(ipName)); locY += pe.Height; pe.SetParamValue(newDevice.GetInitParamValue(ipName)); pe.IsValueReadOnly = true; gbParams.Controls.Add(pe); } Type devType = newDevice.GetType(); if (typeof(IJFDevice_Camera).IsAssignableFrom(devType) || typeof(IJFDevice_LightController).IsAssignableFrom(devType) || typeof(IJFDevice_MotionDaq).IsAssignableFrom(devType) || typeof(IJFDevice_TrigController).IsAssignableFrom(devType) || typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) //提供调试界面 { btDebug.Enabled = true; if (typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) { chkSelfUI.Enabled = true; } else { chkSelfUI.Checked = false; chkSelfUI.Enabled = false; } } else { btDebug.Enabled = false; } if (typeof(IJFConfigUIProvider).IsAssignableFrom(devType)) { btCfg.Enabled = true; } else { btCfg.Enabled = false; } } }
/// <summary>选择设备</summary> private void dgvDevs_CellClick(object sender, DataGridViewCellEventArgs e) { if (dgvDevs.SelectedRows.Count == 0) { return; } btRemove.Enabled = true; RemoveAllPEs(); DataGridViewRow row = dgvDevs.SelectedRows[0]; IJFInitializable dev = JFHubCenter.Instance.InitorManager[row.Cells[0].Value.ToString()]; Type devType = dev.GetType(); if (typeof(IJFDevice_Camera).IsAssignableFrom(devType) || typeof(IJFDevice_LightController).IsAssignableFrom(devType) || typeof(IJFDevice_MotionDaq).IsAssignableFrom(devType) || typeof(IJFDevice_TrigController).IsAssignableFrom(devType) || typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) //提供调试界面 { btDebug.Enabled = true; if (typeof(IJFRealtimeUIProvider).IsAssignableFrom(devType)) { chkSelfUI.Enabled = true; } else { chkSelfUI.Checked = false; chkSelfUI.Enabled = false; } } else { btDebug.Enabled = false; } if (typeof(IJFConfigUIProvider).IsAssignableFrom(devType)) { btCfg.Enabled = true; } else { btCfg.Enabled = false; } tbDevID.Text = row.Cells[0].Value.ToString(); btInit.Enabled = !dev.IsInitOK; string[] iniParamNames = dev.InitParamNames; if (null == iniParamNames) { btEditSave.Enabled = false; return; } btEditSave.Enabled = true; int locY = btInit.Location.Y + btInit.Size.Height + 5; foreach (string ipName in iniParamNames) { UcJFParamEdit pe = new UcJFParamEdit(); pe.Width = gbParams.Width - 1; pe.Location = new Point(4, locY); pe.SetParamDesribe(dev.GetInitParamDescribe(ipName)); locY += pe.Height; pe.SetParamValue(dev.GetInitParamValue(ipName)); pe.IsValueReadOnly = true; gbParams.Controls.Add(pe); } }
//internal bool SaveCfgWhenAdd { get; set; } internal void Add(string id, IJFInitializable dev) { dictInitors.Add(id, dev); JFXmlSortedDictionary <string, List <object> > dictInitorParam = JFHubCenter.Instance.SystemCfg.GetItemValue(JFHubCenter.CK_InitDevParams) as JFXmlSortedDictionary <string, List <object> >; List <object> paramsInCfg = new List <object>(); paramsInCfg.Add(dev.GetType().AssemblyQualifiedName); for (int i = 0; i < dev.InitParamNames.Length; i++) { //paramsInCfg.Add(dev.GetInitParamValue(dev.InitParamNames[i])); 本行代码用以下代码代替,以支持有限的 简单类型 + 数组(列表)类型的组合 //暂时的解决方法:将数组类型转化为字符串存储 object paramVal = dev.GetInitParamValue(dev.InitParamNames[i]); Type paramType = dev.GetInitParamDescribe(dev.InitParamNames[i]).ParamType; SerializableAttribute[] sas = paramType.GetCustomAttributes(typeof(SerializableAttribute), false) as SerializableAttribute[]; if (sas != null && sas.Length > 0) //如果是可序列化的类型,直接保存序列化后的文本 { StringBuilder buffer = new StringBuilder(); XmlSerializer serializer = new XmlSerializer(paramType); using (TextWriter writer = new StringWriter(buffer)) { serializer.Serialize(writer, paramVal); } string xmlTxt = buffer.ToString(); paramsInCfg.Add(xmlTxt); } else { if (paramType.IsValueType || paramType == typeof(string)) //单值对象和字符串对象直接添加 { paramsInCfg.Add(paramVal); } else //目前支持Array 和 List { if (paramType.IsArray) //参数类型是数组 { if (null == paramVal) { paramsInCfg.Add(""); } else { StringBuilder sb = new StringBuilder(); string splitString = "$"; for (int j = 0; j < (paramVal as Array).Length; j++) { sb.Append((paramVal as Array).GetValue(j).ToString()); if (j < (paramVal as Array).Length - 1) { sb.Append(splitString); } } paramsInCfg.Add(sb.ToString());//paramsInCfg.Add(string.Join(splitString, paramVal)); //使用ASC 响铃符作为间隔 } } else if (typeof(IList).IsAssignableFrom(paramType)) //除了数组之外的队列类型 { if (null == paramVal) { paramsInCfg.Add(""); } else { StringBuilder sb = new StringBuilder(); string splitString = "$"; for (int j = 0; j < (paramVal as IList).Count; j++) { sb.Append((paramVal as IList)[j].ToString()); if (j < (paramVal as IList).Count - 1) { sb.Append(splitString); } } paramsInCfg.Add(sb.ToString()); //paramsInCfg.Add(string.Join(System.Text.Encoding.ASCII.GetString(new byte[] { 0x07 }), paramVal)); //使用ASC 响铃符作为间隔 } } } } } dictInitorParam.Add(id, paramsInCfg); JFHubCenter.Instance.SystemCfg.NotifyItemChanged(JFHubCenter.CK_InitDevParams); JFHubCenter.Instance.SystemCfg.Save(); }