/// <summary>
 /// Apply access rights to selected objects.
 /// </summary>
 private void AccessRightsApplyBtn_Click(object sender, EventArgs e)
 {
     try
     {
         int access = (int)AccessRightAllCb.SelectedItem;
         int ver    = version;
         if (Target is GXDLMSAssociationLogicalName)
         {
             ver = Target.Version;
         }
         foreach (DataGridViewRow it in Accessrights.SelectedRows)
         {
             //Logical name can't changed.
             if (it.Index != 0)
             {
                 if (ver < 3)
                 {
                     ((GXDLMSAttributeSettings)bindingSource1[it.Index]).Access  = (AccessMode)access;
                     ((GXDLMSAttributeSettings)bindingSource1[it.Index]).Access3 = 0;
                     Target.SetAccess(it.Index, (AccessMode)access);
                 }
                 else
                 {
                     ((GXDLMSAttributeSettings)bindingSource1[it.Index]).Access = 0;
                     if (access == 0)
                     {
                         ((GXDLMSAttributeSettings)bindingSource1[it.Index]).Access3 = 0;
                     }
                     else
                     {
                         ((GXDLMSAttributeSettings)bindingSource1[it.Index]).Access3 |= (AccessMode3)access;
                     }
                     Target.SetAccess3(1 + it.Index, (AccessMode3)((GXDLMSAttributeSettings)bindingSource1[it.Index]).Access3);
                 }
                 Accessrights.UpdateCellValue(2, it.Index);
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
        private static void UpdateError(GXDLMSObject it, int attributeIndex, Exception ex)
        {
            GXDLMSException t = ex as GXDLMSException;

            if (t != null)
            {
                if (t.ErrorCode == 1 || t.ErrorCode == 3)
                {
                    it.SetAccess(attributeIndex, AccessMode.NoAccess);
                }
                else
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                throw ex;
            }
        }
Beispiel #3
0
 /// <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);
         }
     }
 }
 /// <summary>
 /// Read object.
 /// </summary>
 /// <param name="InitialValues"></param>
 /// <param name="obj"></param>
 /// <param name="attribute"></param>
 public void Read(object sender, GXDLMSObject obj, int attribute)
 {
     foreach (int it in (obj as IGXDLMSBase).GetAttributeIndexToRead())
     {
         if (obj is GXDLMSProfileGeneric && it == 2)
         {
             if (OnBeforeRead != null)
             {
                 OnBeforeRead(obj, it);
             }
             try
             {
                 CurrentProfileGeneric = obj as GXDLMSProfileGeneric;
                 OnDataReceived       += new GXDLMSCommunicator.DataReceivedEventHandler(this.OnProfileGenericDataReceived);
                 if (CurrentProfileGeneric.AccessSelector != AccessRange.Entry)
                 {
                     GXDLMSObject obj2 = CurrentProfileGeneric.CaptureObjects[0].Key;
                     byte[]       tmp  = m_Cosem.ReadRowsByRange(CurrentProfileGeneric.Name, obj2.LogicalName,
                                                                 obj2.ObjectType, obj2.Version,
                                                                 Convert.ToDateTime(CurrentProfileGeneric.From), Convert.ToDateTime(CurrentProfileGeneric.To));
                     ReadDataBlock(tmp, "Reading profile generic data", 1);
                 }
                 else
                 {
                     byte[] tmp = m_Cosem.ReadRowsByEntry(CurrentProfileGeneric.Name, Convert.ToInt32(CurrentProfileGeneric.From), Convert.ToInt32(CurrentProfileGeneric.To));
                     ReadDataBlock(tmp, "Reading profile generic data " + CurrentProfileGeneric.Name, 1);
                 }
             }
             finally
             {
                 if (OnAfterRead != null)
                 {
                     OnAfterRead(obj, it);
                 }
                 OnDataReceived -= new GXDLMSCommunicator.DataReceivedEventHandler(OnProfileGenericDataReceived);
             }
             continue;
         }
         else
         {
             if (OnBeforeRead != null)
             {
                 OnBeforeRead(obj, it);
             }
             byte[] data = m_Cosem.Read(obj.Name, obj.ObjectType, it)[0];
             try
             {
                 data = ReadDataBlock(data, "Read object type " + obj.ObjectType + " index: " + it);
             }
             catch (GXDLMSException ex)
             {
                 if (ex.ErrorCode == 3 || //If read is denied.
                     ex.ErrorCode == 4 || // Undefined object.
                     ex.ErrorCode == 13)  //Actaris returns access violation error.
                 {
                     obj.SetAccess(it, AccessMode.NoAccess);
                     continue;
                 }
                 else
                 {
                     throw ex;
                 }
             }
             if (obj is IGXDLMSBase)
             {
                 object   value = m_Cosem.GetValue(data);
                 DataType type;
                 if (value is byte[] && (type = obj.GetUIDataType(it)) != DataType.None)
                 {
                     value = GXDLMS.Common.GXHelpers.ConvertFromDLMS(value, obj.GetDataType(it), type, true);
                 }
                 (obj as IGXDLMSBase).SetValue(it, value);
             }
             if (OnAfterRead != null)
             {
                 OnAfterRead(obj, it);
             }
             obj.SetLastReadTime(it, DateTime.Now);
             //If only selected attribute is read.
             if (attribute != 0)
             {
                 break;
             }
         }
     }
 }
 /// <summary>
 /// After UpdateObjects call objects can be read using Objects property.
 /// </summary>
 public void UpdateObjects()
 {
     try
     {
         GXDLMSObjectCollection objs = Comm.GetObjects();
         objs.Tag = this;
         int pos = 0;
         foreach (GXDLMSObject it in objs)
         {
             ++pos;
             NotifyProgress(this, "Creating object " + it.LogicalName, pos, objs.Count);
             Objects.Add(it);
         }
         GXLogWriter.WriteLog("--- Created " + Objects.Count.ToString() + " objects. ---");
         //Read registers units and scalers.
         int cnt = Objects.Count;
         if (!UseLogicalNameReferencing)
         {
             GXLogWriter.WriteLog("--- Reading Access rights. ---");
             try
             {
                 foreach (GXDLMSAssociationShortName sn in Objects.GetObjects(ObjectType.AssociationShortName))
                 {
                     if (sn.Version > 1)
                     {
                         Comm.ReadValue(sn, 3);
                     }
                 }
             }
             catch (Exception ex)
             {
                 GXLogWriter.WriteLog(ex.Message);
             }
             GXLogWriter.WriteLog("--- Reading Access rights end. ---");
         }
         GXLogWriter.WriteLog("--- Reading scalers and units. ---");
         this.OnProgress(this, "Reading scalers and units.", cnt + pos + 1, 3 * cnt);
         if ((Comm.client.NegotiatedConformance & Gurux.DLMS.Enums.Conformance.MultipleReferences) != 0)
         {
             List <KeyValuePair <GXDLMSObject, int> > list = new List <KeyValuePair <GXDLMSObject, int> >();
             foreach (GXDLMSObject it in Objects)
             {
                 if (it is GXDLMSRegister && (it.GetAccess(3) & AccessMode.Read) != 0)
                 {
                     list.Add(new KeyValuePair <GXDLMSObject, int>(it, 3));
                 }
                 if (it is GXDLMSDemandRegister && (it.GetAccess(4) & AccessMode.Read) != 0)
                 {
                     list.Add(new KeyValuePair <GXDLMSObject, int>(it, 4));
                 }
             }
             if (list.Count != 0)
             {
                 try
                 {
                     Comm.ReadList(list);
                 }
                 catch (Exception)
                 {
                     //Show error.
                 }
             }
         }
         else
         {
             for (pos = 0; pos != cnt; ++pos)
             {
                 GXDLMSObject it = Objects[pos];
                 if (it is GXDLMSRegister)
                 {
                     //Read scaler first.
                     try
                     {
                         if ((it.GetAccess(3) & AccessMode.Read) != 0)
                         {
                             Comm.ReadValue(it, 3);
                         }
                     }
                     catch (Exception ex)
                     {
                         GXLogWriter.WriteLog(ex.Message);
                         it.SetAccess(3, AccessMode.NoAccess);
                         if (ex is GXDLMSException)
                         {
                             continue;
                         }
                         throw ex;
                     }
                 }
                 if (it is GXDLMSDemandRegister)
                 {
                     //Read scaler first.
                     try
                     {
                         if ((it.GetAccess(4) & AccessMode.Read) != 0)
                         {
                             Comm.ReadValue(it, 4);
                         }
                     }
                     catch (Exception ex)
                     {
                         GXLogWriter.WriteLog(ex.Message);
                         UpdateError(it, 4, ex);
                         if (ex is GXDLMSException)
                         {
                             continue;
                         }
                         throw ex;
                     }
                     //Read Period
                     try
                     {
                         Comm.ReadValue(it, 8);
                     }
                     catch (Exception ex)
                     {
                         GXLogWriter.WriteLog(ex.Message);
                         UpdateError(it, 8, ex);
                         if (ex is GXDLMSException)
                         {
                             continue;
                         }
                         throw ex;
                     }
                     //Read number of periods
                     try
                     {
                         Comm.ReadValue(it, 9);
                     }
                     catch (Exception ex)
                     {
                         GXLogWriter.WriteLog(ex.Message);
                         UpdateError(it, 9, ex);
                         if (ex is GXDLMSException)
                         {
                             continue;
                         }
                         throw ex;
                     }
                 }
             }
         }
         GXLogWriter.WriteLog("--- Reading scalers and units end. ---");
         this.OnProgress(this, "Reading profile generic columns.", cnt, cnt);
         foreach (Gurux.DLMS.Objects.GXDLMSProfileGeneric it in objs.GetObjects(ObjectType.ProfileGeneric))
         {
             ++pos;
             //Read Profile Generic Columns.
             try
             {
                 NotifyProgress(this, "Get profile generic columns", (2 * cnt) + pos, 3 * objs.Count);
                 UpdateColumns(it, Manufacturers.FindByIdentification(Manufacturer));
                 if (it.CaptureObjects == null || it.CaptureObjects.Count == 0)
                 {
                     continue;
                 }
             }
             catch
             {
                 GXLogWriter.WriteLog(string.Format("Failed to read Profile Generic {0} columns.", it.LogicalName));
                 continue;
             }
         }
     }
     finally
     {
         NotifyProgress(this, "", 0, 0);
     }
 }
        /// <summary>
        /// Read object.
        /// </summary>
        /// <param name="InitialValues"></param>
        /// <param name="obj"></param>
        /// <param name="attribute">Attribute index to read.</param>
        /// <param name="forceRead">Force all attributes read.</param>
        public void Read(object sender, GXDLMSObject obj, int attribute, bool forceRead)
        {
            GXReplyData reply = new GXReplyData();

            if (forceRead)
            {
                obj.ClearReadTime();
            }
            foreach (int it in (obj as IGXDLMSBase).GetAttributeIndexToRead())
            {
                reply.Clear();
                if (obj is GXDLMSProfileGeneric && it == 2)
                {
                    if (OnBeforeRead != null)
                    {
                        OnBeforeRead(obj, it);
                    }
                    try
                    {
                        byte[][] tmp;
                        CurrentProfileGeneric = obj as GXDLMSProfileGeneric;
                        OnDataReceived       += new GXDLMSCommunicator.DataReceivedEventHandler(this.OnProfileGenericDataReceived);
                        if (CurrentProfileGeneric.AccessSelector == AccessRange.Range ||
                            CurrentProfileGeneric.AccessSelector == AccessRange.Last)
                        {
                            GXDateTime start = CurrentProfileGeneric.From as GXDateTime;
                            if (start == null)
                            {
                                start = Convert.ToDateTime(CurrentProfileGeneric.From);
                            }
                            GXDateTime end = CurrentProfileGeneric.To as GXDateTime;
                            if (end == null)
                            {
                                end = Convert.ToDateTime(CurrentProfileGeneric.To);
                            }
                            tmp = client.ReadRowsByRange(CurrentProfileGeneric, start, end);
                            ReadDataBlock(tmp, "Reading profile generic data", 1, reply);
                        }
                        else if (CurrentProfileGeneric.AccessSelector == AccessRange.Entry)
                        {
                            tmp = client.ReadRowsByEntry(CurrentProfileGeneric, Convert.ToInt32(CurrentProfileGeneric.From), Convert.ToInt32(CurrentProfileGeneric.To));
                            ReadDataBlock(tmp, "Reading profile generic data " + CurrentProfileGeneric.Name, 1, reply);
                        }
                        else //Read all.
                        {
                            tmp = client.Read(CurrentProfileGeneric, 2);
                            ReadDataBlock(tmp, "Reading profile generic data " + CurrentProfileGeneric.Name, 1, reply);
                        }
                    }
                    finally
                    {
                        if (OnAfterRead != null)
                        {
                            OnAfterRead(obj, it);
                        }
                        OnDataReceived -= new GXDLMSCommunicator.DataReceivedEventHandler(OnProfileGenericDataReceived);
                    }
                    continue;
                }
                else
                {
                    if (OnBeforeRead != null)
                    {
                        OnBeforeRead(obj, it);
                    }
                    byte[] data = client.Read(obj.Name, obj.ObjectType, it)[0];
                    try
                    {
                        ReadDataBlock(data, "Read object type " + obj.ObjectType + " index: " + it, reply);
                    }
                    catch (GXDLMSException ex)
                    {
                        if (ex.ErrorCode == (int)ErrorCode.ReadWriteDenied ||
                            ex.ErrorCode == (int)ErrorCode.UndefinedObject ||
                            ex.ErrorCode == (int)ErrorCode.UnavailableObject ||
                            //Actaris returns access violation error.
                            ex.ErrorCode == (int)ErrorCode.AccessViolated)
                        {
                            obj.SetAccess(it, AccessMode.NoAccess);
                            continue;
                        }
                        else
                        {
                            throw ex;
                        }
                    }
                    if (obj is IGXDLMSBase)
                    {
                        object   value = reply.Value;
                        DataType type;
                        if (value is byte[] && (type = obj.GetUIDataType(it)) != DataType.None)
                        {
                            value = GXDLMSClient.ChangeType((byte[])value, type);
                        }
                        client.UpdateValue(obj, it, value);
                    }
                    if (OnAfterRead != null)
                    {
                        OnAfterRead(obj, it);
                    }
                    obj.SetLastReadTime(it, DateTime.Now);
                    //If only selected attribute is read.
                    if (attribute != 0)
                    {
                        break;
                    }
                }
            }
        }