/// <summary> /// Add new user action. /// </summary> /// <param name="macro"></param> public void AddAction(GXMacro macro) { if (this.InvokeRequired) { BeginInvoke(new AddActionEventHandler(AddAction), macro); } else { dirty = true; macro.Timestamp = DateTime.Now; Macros2.Add(macro); AddMacro(macro, Filtered); //Serialization will remove \r and comparing result will fail... if (macro.Value != null) { macro.Value = macro.Value.Replace("\r\n", "\n"); } //Serialization will remove \r and comparing result will fail... if (macro.Data != null) { macro.Data = macro.Data.Replace("\r\n", "\n"); } if (Created) { MacrosView.VirtualListSize = GetMacros().Count; if (Properties.Settings.Default.MacroFollowLast) { MacrosView.EnsureVisible(GetMacros().Count - 1); } UpdateTitle(); } } }
private void ActionsList_DragDrop(object sender, DragEventArgs e) { if (MacrosView.SelectedIndices.Count != 0) { Point pt = MacrosView.PointToClient(new Point(e.X, e.Y)); ListViewItem dIt = MacrosView.GetItemAt(pt.X, pt.Y); if (dIt != null) { int index = dIt.Index; foreach (int pos in MacrosView.SelectedIndices) { if (index != pos) { GXMacro old = GetMacros()[pos]; Macros2.Remove(old); Macros2.Insert(index, old); ++index; } } MacrosView.RedrawItems(dIt.Index, index, false); dirty = true; UpdateTitle(); } } }
/// <summary> /// Find Macro. /// </summary> /// <param name="nodes">List of macros to search.</param> /// <param name="text">OBIS code to search</param> /// <param name="first">If selected item found.</param> /// <returns>Is macro found.</returns> private bool FindMacro(List <GXMacro> macros, ref bool first) { int index = 0; if (!first && MacrosView.SelectedIndices.Count == 1) { index = MacrosView.SelectedIndices[0]; ++index; } if (searchText != null) { searchText = searchText.ToLower(); } for (int pos = index; pos < macros.Count; ++pos) { GXMacro it = macros[pos]; if ((searchLn != null && it.LogicalName == searchLn) || (it.Name != null && it.Name.ToLower().Contains(searchText)) || (it.Value != null && it.Value.ToLower().Contains(searchText)) || (searchText != null && it.Type.ToString().ToLower().Contains(searchText))) { MacrosView.SelectedIndices.Clear(); MacrosView.SelectedIndices.Add(pos); return(true); } } return(false); }
private static void AddMacro(GXMacro it, List <GXMacro> list) { if (((Properties.Settings.Default.MacroHideFailed && !string.IsNullOrEmpty(it.Exception)) || (Properties.Settings.Default.MacroHideDisabled && it.Disable) || (Properties.Settings.Default.MacroIHideVerified && it.Verify) || (Properties.Settings.Default.MacroHideSucceeded && string.IsNullOrEmpty(it.Exception)))) { } else { list.Add(it); } }
/// <summary> /// Draw macro on Virtual View mode. /// </summary> private void MacrosView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) { lock (Macros2) { if (e.ItemIndex < GetMacros().Count) { GXMacro action = GetMacros()[e.ItemIndex]; string value = action.Value; if (value != null && value.Length > 200) { value = value.Substring(0, 200) + "..."; } ListViewItem li = new ListViewItem(new string[] { action.Timestamp.ToString(), action.Type.ToString(), action.Name, value }); li.Tag = action; //Update image. if (action.Disable) { li.ImageIndex = 5; } else if (!string.IsNullOrEmpty(action.Exception)) { //Exception that has occurred on recording. li.ImageIndex = 2; li.SubItems[3].Text = action.Exception; } else if (action.Verify && action.Type != UserActionType.Connect && action.Type != UserActionType.Disconnecting) { li.ImageIndex = 3; } else { li.ImageIndex = 0; } if (action.Running) { li.BackColor = Color.Green; } else if (action.LastRunMacro != null && !string.IsNullOrEmpty(action.LastRunMacro.Exception)) { //If macro run fails. li.BackColor = Color.Red; li.SubItems[3].Text = action.LastRunMacro.Exception; } e.Item = li; } } }
private void Run(System.Collections.IList items, List <GXMacro> macros) { try { if (Target == null) { throw new Exception("Failed to run macro. No device is selected."); } UpdateRunMenu(true); UpdateStatus(); GXMacro selectedMacro = null; if (MacrosView.SelectedIndices.Count == 1) { selectedMacro = GetMacros()[MacrosView.SelectedIndices[0]]; } Results.Clear(); foreach (int it in items) { GXMacro orig = macros[it]; GXMacro macro = orig.Clone(); orig.LastRunMacro = macro; Results.Add(orig, macro); if (Properties.Settings.Default.MacroFollowLast) { MacrosView.EnsureVisible(it); } if (!macro.Disable) { macro.Exception = null; try { orig.Running = true; MacrosView.RedrawItems(it, it, false); switch (macro.Type) { case UserActionType.Connect: m_OnConnect(macro); break; case UserActionType.Disconnecting: m_OnDisconnect(macro); break; case UserActionType.Get: macro.Value = macro.Data = macro.Exception = null; macro.DataType = 0; m_OnGet(macro); //Update data if it's updated. if (macro.Verify) { if (macro.Exception != orig.Exception) { macro.Exception = "Different exceptions."; } else if (macro.DataType != orig.DataType) { macro.Exception = "Different data type."; } else if (macro.Data != orig.Data) { macro.Exception = "Different data."; } } break; case UserActionType.Set: m_OnSet(macro); break; case UserActionType.Action: m_OnAction(macro); break; default: break; } } catch (Exception ex) { macro.Exception = ex.Message; if (Properties.Settings.Default.MacroBreakOnError) { throw; } } finally { orig.Running = false; MacrosView.RedrawItems(it, it, false); } if (orig == selectedMacro) { ActionsList_SelectedIndexChanged(null, null); } } } } finally { UpdateRunMenu(false); UpdateStatus(); } }
/// <summary> /// Show action data. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ActionsList_SelectedIndexChanged(object sender, EventArgs e) { if (MacrosView.SelectedIndices.Count == 1) { GXMacro macro = GetMacros()[MacrosView.SelectedIndices[0]]; if (macro.Type == UserActionType.Get || macro.Type == UserActionType.Set) { if (!tabControl1.TabPages.Contains(VisualizedTab)) { tabControl1.TabPages.Insert(0, VisualizedTab); } } else { if (tabControl1.TabPages.Contains(VisualizedTab)) { tabControl1.TabPages.Remove(VisualizedTab); } } string actual, expected; if (Properties.Settings.Default.MacroRaw) { expected = macro.Data; } else { expected = macro.Value; } OriginalDataTb.Text = expected; if (Results.ContainsKey(macro)) { if (Properties.Settings.Default.MacroRaw) { actual = Results[macro].Data; expected = macro.Data; } else { actual = Results[macro].Value; expected = macro.Value; } if (actual != expected) { ReplyDataTb.Text = actual; ActualPanel.Visible = true; } else { ActualPanel.Visible = false; } } else { ActualPanel.Visible = false; ReplyDataTb.Text = null; } try { if (macro.Type == UserActionType.Get || macro.Type == UserActionType.Set) { if (macro.ObjectType != 0) { GXDLMSObject obj = GXDLMSClient.CreateObject((ObjectType)macro.ObjectType); if (obj != null) { obj.LogicalName = macro.LogicalName; for (int pos = 1; pos != (obj as IGXDLMSBase).GetAttributeCount() + 1; ++pos) { obj.SetAccess(pos, AccessMode.NoAccess); } obj.SetAccess(macro.Index, AccessMode.ReadWrite); obj.SetDataType(macro.Index, (DataType)macro.DataType); obj.SetUIDataType(macro.Index, (DataType)macro.UIDataType); if (!string.IsNullOrEmpty(macro.Data) && Target != null) { object value = GXDLMSTranslator.XmlToValue(macro.Data); if (value is byte[] && macro.UIDataType != 0) { value = Target.Comm.client.ChangeType(new GXByteBuffer((byte[])value), (DataType)macro.UIDataType); } else if (value is byte[] && macro.DataType != (int)DataType.None && macro.DataType != (int)DataType.OctetString) { if (macro.DataType == (int)DataType.Array || macro.DataType == (int)DataType.Structure) { GXByteBuffer bb = new GXByteBuffer((byte[])value); //Skip data type. bb.Position = 1; value = Target.Comm.client.ChangeType(bb, (DataType)macro.DataType); } else { GXByteBuffer bb = new GXByteBuffer((byte[])value); value = Target.Comm.client.ChangeType(bb, (DataType)macro.DataType); } } if (macro.ObjectType == (int)ObjectType.ProfileGeneric && macro.Index == 2 && !string.IsNullOrEmpty(macro.External)) { Target.Comm.client.UpdateValue(obj, 3, GXDLMSTranslator.XmlToValue(macro.External)); } Target.Comm.client.UpdateValue(obj, macro.Index, value); } if (SelectedView == null || SelectedView.Target.ObjectType != obj.ObjectType) { //SelectedView must remove from the controls. ObjectPanelFrame.Controls.Clear(); if (Target == null) { SelectedView = GXDlmsUi.GetView(Views, obj, Standard.DLMS); } else { SelectedView = GXDlmsUi.GetView(Views, obj, Target.Comm.client.Standard); } SelectedView.Target = obj; GXDlmsUi.ObjectChanged(SelectedView, obj, false); SelectedView.OnDirtyChange(macro.Index, true); ObjectPanelFrame.Controls.Add(((Form)SelectedView)); ((Form)SelectedView).Show(); } else { SelectedView.Target = obj; GXDlmsUi.ObjectChanged(SelectedView, obj, false); SelectedView.OnDirtyChange(macro.Index, true); } } } } } catch (Exception Ex) { GXDLMS.Common.Error.ShowError(this, Ex); } } else { OriginalDataTb.Text = ""; if (GetMacros().Count == 0 && tabControl1.TabPages.Contains(VisualizedTab)) { tabControl1.TabPages.Remove(VisualizedTab); } } }