GXDLMSObject provides an interface to DLMS registers.
상속: IGXDLMSColumnObject
        public void Write(GXDLMSObject obj, object target, int index, List<object> UpdatedObjects)
        {
            object value;
            GXReplyData reply = new GXReplyData();
            for (int it = 1; it != (obj as IGXDLMSBase).GetAttributeCount() + 1; ++it)
            {
                reply.Clear();
                if (obj.GetDirty(it, out value))
                {
                    //Read DLMS data type if not known.
                    DataType type = obj.GetDataType(it);
                    if (type == DataType.None)
                    {
                        byte[] data = client.Read(obj, it)[0];
                        ReadDataBlock(data, "Write object type " + obj.ObjectType, reply);
                        type = reply.DataType;
                        if (type == DataType.None)
                        {
                            throw new Exception("Failed to write value. Data type not set.");
                        }
                        obj.SetDataType(it, type);
                    }
                    try
                    {
                        foreach (byte[] tmp in client.Write(obj.Name, value, type, obj.ObjectType, it))
                        {
                            ReadDataBlock(tmp, "Write object", reply);
                        }
                        obj.ClearDirty(it);
                        //Read data once again to make sure it is updated.
                        byte[] data = client.Read(obj, it)[0];
                        ReadDataBlock(data, "Read object " + obj.ObjectType, reply);

                        value = reply.Value;
                        if (value is byte[] && (type = obj.GetUIDataType(it)) != DataType.None)
                        {
                            value = GXDLMSClient.ChangeType((byte[])value, type);
                        }
                        client.UpdateValue(obj, it, value);
                    }
                    catch (GXDLMSException ex)
                    {
                        if (ex.ErrorCode == 3)
                        {
                            throw new Exception("Read/Write Failed.");
                        }
                        else
                        {
                            throw ex;
                        }
                    }
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Returns collection of manufacturer Obis codes to implement custom read.
 /// </summary>
 /// <param name="name">Short or Logical Name.</param>
 /// <param name="type">Interface type.</param>        
 /// <returns>True, if data read is handled.</returns>
 public bool Read(object sender, GXDLMSObject item, GXDLMSObjectCollection columns, int attribute, GXDLMSCommunicator comm)
 {
     MainForm = sender as System.Windows.Forms.Form;
     if (!(item is GXDLMSProfileGeneric))
     {
         return false;
     }
     //Actaris do not support other than index 2.
     if (attribute != 0 && attribute != 2)
     {
         return true;
     }
     if (comm.OnBeforeRead != null)
     {
         comm.OnBeforeRead(item, attribute);
     }
     CurrentProfileGeneric = item as GXDLMSProfileGeneric;
     if (item is GXDLMSProfileGeneric)
     {
         GXDLMSProfileGeneric pg = item as GXDLMSProfileGeneric;
         GXReplyData reply = new GXReplyData();
         byte[][] data;
         try
         {
             comm.OnDataReceived += new GXDLMSCommunicator.DataReceivedEventHandler(this.OnProfileGenericDataReceived);
             //Read profile generic columns.
             if (pg.AccessSelector == AccessRange.Range ||
                 pg.AccessSelector == AccessRange.Last)
             {
                 data = comm.client.ReadRowsByRange(pg, Convert.ToDateTime(pg.From).Date, Convert.ToDateTime(pg.To).Date);
                 comm.ReadDataBlock(data[0], "Reading profile generic data", 1, reply);
             }
             else if (pg.AccessSelector == AccessRange.Entry)
             {
                 data = comm.client.ReadRowsByEntry(pg, Convert.ToInt32(pg.From), Convert.ToInt32(pg.To));
                 comm.ReadDataBlock(data[0], "Reading profile generic data " + pg.Name, 1, reply);
             }
             else //Read All.
             {
                 data = comm.client.Read(pg, 2);
                 comm.ReadDataBlock(data[0], "Reading profile generic data " + pg.Name, 1, reply);
             }
         }
         finally
         {
             CurrentProfileGeneric = null;
             comm.OnDataReceived -= new GXDLMSCommunicator.DataReceivedEventHandler(this.OnProfileGenericDataReceived);
         }
         return true;
     }
     return false;
 }
예제 #3
0
 /// <summary>
 /// Returns collection of manufacturer Obis codes to implement custom read.
 /// </summary>
 /// <param name="name">Short or Logical Name.</param>
 /// <param name="type">Interface type.</param>        
 /// <returns>True, if data read is handled.</returns>
 public bool Read(object sender, GXDLMSObject item, GXDLMSObjectCollection columns, int attribute, GXDLMSCommunicator comm)
 {
     MainForm = sender as System.Windows.Forms.Form;
     if (!(item is GXDLMSProfileGeneric))
     {
         return false;
     }
     //Actaris do not support other than index 2.
     if (attribute != 0 && attribute != 2)
     {
         return true;
     }
     if (comm.OnBeforeRead != null)
     {
         comm.OnBeforeRead(item, attribute);
     }
     CurrentProfileGeneric = item as GXDLMSProfileGeneric;
     if (item is GXDLMSProfileGeneric)
     {
         GXDLMSProfileGeneric pg = item as GXDLMSProfileGeneric;
         byte[] data;
         try
         {
             comm.OnDataReceived += new GXDLMSCommunicator.DataReceivedEventHandler(this.OnProfileGenericDataReceived);
             //Read profile generic columns.
             if (pg.AccessSelector != AccessRange.Entry)
             {
                 data = comm.m_Cosem.ReadRowsByRange(pg.Name, pg.CaptureObjects[0].Key.LogicalName,
                                 pg.CaptureObjects[0].Key.ObjectType, pg.CaptureObjects[0].Key.Version, Convert.ToDateTime(pg.From).Date, Convert.ToDateTime(pg.To).Date);
                 data = comm.ReadDataBlock(data, "Reading profile generic data", 1);
             }
             else
             {
                 data = comm.m_Cosem.ReadRowsByEntry(pg.Name, Convert.ToInt32(pg.From), Convert.ToInt32(pg.To));
                 data = comm.ReadDataBlock(data, "Reading profile generic data " + pg.Name, 1);
             }
         }
         finally
         {
             CurrentProfileGeneric = null;
             comm.OnDataReceived -= new GXDLMSCommunicator.DataReceivedEventHandler(this.OnProfileGenericDataReceived);
         }
         return true;
     }
     return false;
 }
예제 #4
0
 /// <summary>
 /// Generates a read message.
 /// </summary>
 /// <param name="item">DLMS object to read.</param>
 /// <param name="attributeOrdinal">Read attribute index.</param>
 /// <returns>Read request as byte array.</returns>
 public byte[][] Read(GXDLMSObject item, int attributeOrdinal)
 {
     if ((attributeOrdinal < 1))
     {
         throw new ArgumentOutOfRangeException("Invalid parameter");
     }
     //Clear cache
     m_Base.ClearProgress();
     return m_Base.GenerateMessage(item.Name, new byte[0], item.ObjectType, 
         attributeOrdinal, this.UseLogicalNameReferencing ? Command.GetRequest : Command.ReadRequest);
 }
예제 #5
0
 /// <summary>
 /// Generate Method (Action) request.
 /// </summary>
 /// <param name="item">object to write.</param>
 /// <param name="index">Attribute index where data is write.</param>
 /// <param name="data">Additional data.</param>
 /// <param name="type">Additional data type.</param>
 /// <returns></returns>
 public byte[][] Method(GXDLMSObject item, int index, Object data, DataType type)
 {
     return Method(item.Name, item.ObjectType, index, data, 1, type);
 }
예제 #6
0
 /// <summary>
 /// Get Value from byte array received from the meter.
 /// </summary>
 /// <param name="data">Reply byte array received from the meter.</param>
 /// <param name="target">Read COSEM Object.</param>
 /// <param name="attributeIndex"></param>
 /// <returns></returns>
 /// <seealso cref="TryGetValue"/>
 public object GetValue(byte[] data, GXDLMSObject target, int attributeIndex)
 {
     DataType type = target.GetUIDataType(attributeIndex);
     Object value = GetValue(data);
     if (value is byte[] && type != DataType.None)
     {
         return GXDLMSClient.ChangeType((byte[]) value, type);
     }
     return value;
 }
예제 #7
0
 /// <summary>
 /// Reserved for internal use.
 /// </summary>
 /// <param name="objectType"></param>
 /// <param name="version"></param>
 /// <param name="baseName"></param>
 /// <param name="logicalName"></param>
 /// <param name="accessRights"></param>
 /// <param name="attributeIndex"></param>
 /// <param name="dataIndex"></param>
 internal static void UpdateObjectData(GXDLMSObject obj, ObjectType objectType, object version, object baseName, object logicalName, object accessRights)
 {
     obj.ObjectType = objectType;
     // Check access rights...            
     if (accessRights != null && accessRights.GetType().IsArray)
     {
         //access_rights: access_right
         object[] access = (object[])accessRights;
         foreach (object[] attributeAccess in (object[])access[0])
         {
             int id = Convert.ToInt32(attributeAccess[0]);
             AccessMode mode = (AccessMode)Convert.ToInt32(attributeAccess[1]);
             //With some meters id is negative. 
             if (id > 0)
             {
                 obj.SetAccess(id, mode);
             }
         }
         if (((object[])access[1]).Length != 0)
         {
             if (((object[])access[1])[0] is object[])
             {
                 foreach (object[] methodAccess in (object[])access[1])
                 {
                     int id = Convert.ToInt32(methodAccess[0]);
                     int tmp;
                     //If version is 0.
                     if (methodAccess[1] is Boolean)
                     {
                         tmp = ((Boolean)methodAccess[1]) ? 1 : 0;
                     }
                     else//If version is 1.
                     {
                         tmp = Convert.ToInt32(methodAccess[1]);
                     }
                     obj.SetMethodAccess(id, (MethodAccessMode)tmp);
                 }
             }
             else //All versions from Actaris SL 7000 do not return collection as standard says.
             {
                 object[] arr = (object[])access[1];
                 int id = Convert.ToInt32(arr[0]) + 1;
                 int tmp;
                 //If version is 0.
                 if (arr[1] is Boolean)
                 {
                     tmp = ((Boolean)arr[1]) ? 1 : 0;
                 }
                 else//If version is 1.
                 {
                     tmp = Convert.ToInt32(arr[1]);
                 }
                 obj.SetMethodAccess(id, (MethodAccessMode)tmp);
             }
         }
     }           
     if (baseName != null)
     {
         obj.ShortName = Convert.ToUInt16(baseName);
     }
     if (version != null)
     {
         obj.Version = Convert.ToInt32(version);
     }
     if (logicalName is byte[])
     {
         obj.LogicalName = GXDLMSObject.toLogicalName((byte[])logicalName);
     }
     else
     {
         obj.LogicalName = logicalName.ToString();
     }
 }
예제 #8
0
 /// <summary>
 /// Update standard OBIS codes description and type if defined.
 /// </summary>
 /// <param name="target"> COSEM object.</param>
 public void UpdateOBISCodeInformation(GXDLMSObject target)
 {
     lock (codes)
     {
         if (codes.Count == 0)
         {
             ReadStandardObisInfo(codes);
         }
         UpdateOBISCodeInfo(codes, target);
     }
 }
예제 #9
0
 void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         LogicalName = GXCommon.ToLogicalName(e.Value);
     }
     else if (e.Index == 2)
     {
         ObjectList.Clear();
         if (e.Value != null)
         {
             foreach (Object[] item in (Object[])e.Value)
             {
                 GXDLMSObject obj = GetObject(settings, item);
                 //Unknown objects are not shown.
                 if (obj is IGXDLMSBase)
                 {
                     ObjectList.Add(obj);
                 }
             }
         }
     }
     else if (e.Index == 3)
     {
         if (e.Value != null)
         {
             ClientSAP = Convert.ToByte(((Object[])e.Value)[0]);
             ServerSAP = Convert.ToUInt16(((Object[])e.Value)[1]);
         }
     }
     else if (e.Index == 4)
     {
         //Value of the object identifier encoded in BER
         if (e.Value is byte[])
         {
             GXByteBuffer arr = new GXByteBuffer(e.Value as byte[]);
             if (arr.GetUInt8(0) == 0x60)
             {
                 ApplicationContextName.JointIsoCtt            = arr.GetUInt8();
                 ApplicationContextName.Country                = arr.GetUInt8();
                 ApplicationContextName.CountryName            = arr.GetUInt8();
                 ApplicationContextName.IdentifiedOrganization = arr.GetUInt8();
                 ApplicationContextName.DlmsUA             = arr.GetUInt8();
                 ApplicationContextName.ApplicationContext = arr.GetUInt8();
                 ApplicationContextName.ContextId          = (ApplicationContextName)arr.GetUInt8();
             }
             else
             {
                 //Get Tag and Len.
                 if (arr.GetUInt8() != (int)BerType.Integer && arr.GetUInt8() != 7)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.JointIsoCtt = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.Country = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x12)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.CountryName = arr.GetUInt16();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.IdentifiedOrganization = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.DlmsUA = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.ApplicationContext = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.ContextId = (ApplicationContextName)arr.GetUInt8();
             }
         }
         else if (e.Value != null)
         {
             Object[] arr = (Object[])e.Value;
             ApplicationContextName.JointIsoCtt            = Convert.ToByte(arr[0]);
             ApplicationContextName.Country                = Convert.ToByte(arr[1]);
             ApplicationContextName.CountryName            = Convert.ToUInt16(arr[2]);
             ApplicationContextName.IdentifiedOrganization = Convert.ToByte(arr[3]);
             ApplicationContextName.DlmsUA             = Convert.ToByte(arr[4]);
             ApplicationContextName.ApplicationContext = Convert.ToByte(arr[5]);
             ApplicationContextName.ContextId          = (ApplicationContextName)Convert.ToByte(arr[6]);
         }
     }
     else if (e.Index == 5)
     {
         if (e.Value != null)
         {
             Object[] arr = (Object[])e.Value;
             XDLMSContextInfo.Conformance       = (Conformance)Convert.ToUInt32(arr[0]);
             XDLMSContextInfo.MaxReceivePduSize = Convert.ToUInt16(arr[1]);
             XDLMSContextInfo.MaxSendPduSize    = Convert.ToUInt16(arr[2]);
             XDLMSContextInfo.DlmsVersionNumber = Convert.ToByte(arr[3]);
             XDLMSContextInfo.QualityOfService  = Convert.ToSByte(arr[4]);
             XDLMSContextInfo.CypheringInfo     = (byte[])arr[5];
         }
     }
     else if (e.Index == 6)
     {
         //Value of the object identifier encoded in BER
         if (e.Value is byte[])
         {
             GXByteBuffer arr = new GXByteBuffer(e.Value as byte[]);
             if (arr.GetUInt8(0) == 0x60)
             {
                 AuthenticationMechanismName.JointIsoCtt            = arr.GetUInt8();
                 AuthenticationMechanismName.Country                = arr.GetUInt8();
                 AuthenticationMechanismName.CountryName            = arr.GetUInt8();
                 AuthenticationMechanismName.IdentifiedOrganization = arr.GetUInt8();
                 AuthenticationMechanismName.DlmsUA = arr.GetUInt8();
                 AuthenticationMechanismName.AuthenticationMechanismName = arr.GetUInt8();
                 AuthenticationMechanismName.MechanismId = (Authentication)arr.GetUInt8();
             }
             else
             {
                 //Get Tag and Len.
                 if (arr.GetUInt8() != (int)BerType.Integer && arr.GetUInt8() != 7)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.JointIsoCtt = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.Country = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x12)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.CountryName = arr.GetUInt16();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.IdentifiedOrganization = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.DlmsUA = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.AuthenticationMechanismName = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.MechanismId = (Authentication)arr.GetUInt8();
             }
         }
         else if (e.Value != null)
         {
             Object[] arr = (Object[])e.Value;
             AuthenticationMechanismName.JointIsoCtt            = Convert.ToByte(arr[0]);
             AuthenticationMechanismName.Country                = Convert.ToByte(arr[1]);
             AuthenticationMechanismName.CountryName            = Convert.ToUInt16(arr[2]);
             AuthenticationMechanismName.IdentifiedOrganization = Convert.ToByte(arr[3]);
             AuthenticationMechanismName.DlmsUA = Convert.ToByte(arr[4]);
             AuthenticationMechanismName.AuthenticationMechanismName = Convert.ToByte(arr[5]);
             AuthenticationMechanismName.MechanismId = (Authentication)Convert.ToByte(arr[6]);
         }
     }
     else if (e.Index == 7)
     {
         Secret = (byte[])e.Value;
     }
     else if (e.Index == 8)
     {
         if (e.Value == null)
         {
             AssociationStatus = AssociationStatus.NonAssociated;
         }
         else
         {
             AssociationStatus = (AssociationStatus)Convert.ToInt32(e.Value);
         }
     }
     else if (e.Index == 9)
     {
         SecuritySetupReference = GXCommon.ToLogicalName(e.Value);
     }
     else if (e.Index == 10)
     {
         UserList.Clear();
         if (e.Value != null)
         {
             foreach (Object[] item in (Object[])e.Value)
             {
                 UserList.Add(new KeyValuePair <byte, string>(Convert.ToByte(item[0]), Convert.ToString(item[1])));
             }
         }
     }
     else if (e.Index == 11)
     {
         if (e.Value != null)
         {
             Object[] tmp = (Object[])e.Value;
             if (tmp.Length == 1)
             {
                 CurrentUser = new KeyValuePair <byte, string>(Convert.ToByte(tmp[0]), null);
             }
             else
             {
                 CurrentUser = new KeyValuePair <byte, string>(Convert.ToByte(tmp[0]), Convert.ToString(tmp[1]));
             }
         }
         else
         {
             CurrentUser = new KeyValuePair <byte, string>(0, null);
         }
     }
     else
     {
         e.Error = ErrorCode.ReadWriteDenied;
     }
 }
예제 #10
0
 void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         LogicalName = GXCommon.ToLogicalName(e.Value);
     }
     else if (e.Index == 2)
     {
         PushObjectList.Clear();
         if (e.Value is Object[])
         {
             foreach (object it in e.Value as Object[])
             {
                 Object[]     tmp  = it as Object[];
                 ObjectType   type = (ObjectType)Convert.ToUInt16(tmp[0]);
                 String       ln   = GXCommon.ToLogicalName(tmp[1]);
                 GXDLMSObject obj  = settings.Objects.FindByLN(type, ln);
                 if (obj == null)
                 {
                     obj             = GXDLMSClient.CreateObject(type);
                     obj.LogicalName = ln;
                 }
                 GXDLMSCaptureObject co = new GXDLMSCaptureObject(Convert.ToInt32(tmp[2]), Convert.ToInt32(tmp[3]));
                 PushObjectList.Add(new KeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(obj, co));
             }
         }
     }
     else if (e.Index == 3)
     {
         object[] tmp = e.Value as object[];
         if (tmp != null)
         {
             Service = (ServiceType)Convert.ToInt32(tmp[0]);
             //LN can be used with HDLC
             if (((byte[])tmp[1]).Length == 6 && ((byte[])tmp[1])[5] == 0xFF)
             {
                 Destination = GXCommon.ToLogicalName((byte[])tmp[1]);
             }
             else
             {
                 Destination = (string)GXDLMSClient.ChangeType((byte[])tmp[1], DataType.String, settings.UseUtc2NormalTime);
             }
             Message = (MessageType)Convert.ToInt32(tmp[2]);
         }
     }
     else if (e.Index == 4)
     {
         CommunicationWindow.Clear();
         if (e.Value is Object[])
         {
             foreach (object it in e.Value as Object[])
             {
                 Object[]   tmp   = it as Object[];
                 GXDateTime start = GXDLMSClient.ChangeType((byte[])tmp[0], DataType.DateTime, settings.UseUtc2NormalTime) as GXDateTime;
                 GXDateTime end   = GXDLMSClient.ChangeType((byte[])tmp[1], DataType.DateTime, settings.UseUtc2NormalTime) as GXDateTime;
                 CommunicationWindow.Add(new KeyValuePair <GXDateTime, GXDateTime>(start, end));
             }
         }
     }
     else if (e.Index == 5)
     {
         RandomisationStartInterval = (UInt16)e.Value;
     }
     else if (e.Index == 6)
     {
         NumberOfRetries = (byte)e.Value;
     }
     else if (e.Index == 7)
     {
         RepetitionDelay = (UInt16)e.Value;
     }
     else
     {
         e.Error = ErrorCode.ReadWriteDenied;
     }
 }
예제 #11
0
        void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
        {
            if (e.Index == 1)
            {
                if (e.Value is string)
                {
                    LogicalName = e.Value.ToString();
                }
                else
                {
                    LogicalName = GXDLMSClient.ChangeType((byte[])e.Value, DataType.OctetString, settings.UseUtc2NormalTime).ToString();
                }
            }
            else if (e.Index == 2)
            {
                SetBuffer(settings, e);
            }
            else if (e.Index == 3)
            {
                Reset();
                CaptureObjects.Clear();
                GXDLMSObjectCollection objects = new GXDLMSObjectCollection();
                if (e.Value != null)
                {
                    foreach (object it in e.Value as object[])
                    {
                        object[] tmp = it as object[];
                        if (tmp.Length != 4)
                        {
                            throw new GXDLMSException("Invalid structure format.");
                        }
                        ObjectType   type           = (ObjectType)Convert.ToInt16(tmp[0]);
                        string       ln             = GXDLMSObject.ToLogicalName((byte[])tmp[1]);
                        int          attributeIndex = Convert.ToInt16(tmp[2]);
                        int          dataIndex      = Convert.ToInt16(tmp[3]);
                        GXDLMSObject obj            = null;
                        if (settings != null && settings.Objects != null)
                        {
                            obj = settings.Objects.FindByLN(type, ln);
                        }
                        if (obj == null)
                        {
                            obj = GXDLMSClient.CreateDLMSObject((int)type, null, 0, ln, 0);
                        }
                        CaptureObjects.Add(new GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(obj, new GXDLMSCaptureObject(attributeIndex, dataIndex)));
                        objects.Add(obj);
                    }
                }
            }
            else if (e.Index == 4)
            {
                //Any write access to one of the attributes will automatically call a reset
                //and this call will propagate to all other profiles capturing this profile.
                if (settings.IsServer)
                {
                    Reset();
                }
                CapturePeriod = Convert.ToInt32(e.Value);
            }
            else if (e.Index == 5)
            {
                //Any write access to one of the attributes will automatically call a reset
                //and this call will propagate to all other profiles capturing this profile.
                if (settings.IsServer)
                {
                    Reset();
                }
                SortMethod = (SortMethod)Convert.ToInt32(e.Value);
            }
            else if (e.Index == 6)
            {
                //Any write access to one of the attributes will automatically call a reset
                //and this call will propagate to all other profiles capturing this profile.
                if (settings.IsServer)
                {
                    Reset();
                }

                if (e.Value != null)
                {
                    object[] tmp = e.Value as object[];
                    if (tmp.Length != 4)
                    {
                        throw new GXDLMSException("Invalid structure format.");
                    }
                    ObjectType type = (ObjectType)Convert.ToInt16(tmp[0]);
                    string     ln   = GXDLMSObject.ToLogicalName((byte[])tmp[1]);
                    SortAttributeIndex = Convert.ToInt16(tmp[2]);
                    SortDataIndex      = Convert.ToInt16(tmp[3]);
                    SortObject         = null;
                    foreach (var it in CaptureObjects)
                    {
                        if (it.Key.ObjectType == type && it.Key.LogicalName == ln)
                        {
                            SortObject = it.Key;
                            break;
                        }
                    }
                    if (SortObject == null)
                    {
                        SortObject             = GXDLMSClient.CreateObject(type);
                        SortObject.LogicalName = ln;
                    }
                }
                else
                {
                    SortObject = null;
                }
            }
            else if (e.Index == 7)
            {
                EntriesInUse = Convert.ToInt32(e.Value);
            }
            else if (e.Index == 8)
            {
                //Any write access to one of the attributes will automatically call a reset
                //and this call will propagate to all other profiles capturing this profile.
                if (settings.IsServer)
                {
                    Reset();
                }
                ProfileEntries = Convert.ToInt32(e.Value);
            }
            else
            {
                e.Error = ErrorCode.ReadWriteDenied;
            }
        }
 public void Update(GXDLMSObject value, int attributeIndex)
 {
     ObjectType     = value.ObjectType;
     LogicalName    = value.LogicalName;
     AttributeIndex = attributeIndex;
 }
 void IGXDLMSBase.SetValue(GXDLMSSettings settings, int index, object value)
 {
     if (index == 1)
     {
         if (value is string)
         {
             LogicalName = value.ToString();
         }
         else
         {
             LogicalName = GXDLMSClient.ChangeType((byte[])value, DataType.OctetString).ToString();
         }
     }
     else if (index == 2)
     {
         ObjectList.Clear();
         if (value != null)
         {
             foreach (Object[] item in (Object[])value)
             {
                 ushort       sn      = (ushort)(Convert.ToInt32(item[0]) & 0xFFFF);
                 ObjectType   type    = (ObjectType)Convert.ToInt32(item[1]);
                 int          version = Convert.ToInt32(item[2]);
                 String       ln      = GXDLMSObject.ToLogicalName((byte[])item[3]);
                 GXDLMSObject obj     = null;
                 if (settings.Objects != null)
                 {
                     obj = settings.Objects.FindBySN(sn);
                 }
                 if (obj == null)
                 {
                     obj = Gurux.DLMS.GXDLMSClient.CreateObject(type);
                     if (obj != null)
                     {
                         obj.LogicalName = ln;
                         obj.ShortName   = sn;
                         obj.Version     = version;
                     }
                 }
                 //Unknown objects are not shown.
                 if (obj is IGXDLMSBase)
                 {
                     ObjectList.Add(obj);
                 }
             }
         }
     }
     else if (index == 3)
     {
         if (value == null)
         {
             foreach (GXDLMSObject it in ObjectList)
             {
                 for (int pos = 1; pos != (it as IGXDLMSBase).GetAttributeCount(); ++pos)
                 {
                     it.SetAccess(pos, AccessMode.NoAccess);
                 }
             }
         }
         else
         {
             UpdateAccessRights((Object[])value);
         }
     }
     else if (index == 4)
     {
         if (value is string)
         {
             SecuritySetupReference = value.ToString();
         }
         else
         {
             SecuritySetupReference = GXDLMSClient.ChangeType(value as byte[], DataType.OctetString).ToString();
         }
     }
     else
     {
         throw new ArgumentException("SetValue failed. Invalid attribute index.");
     }
 }
 void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         LogicalName = GXCommon.ToLogicalName(e.Value);
     }
     else if (e.Index == 2)
     {
         ObjectList.Clear();
         if (e.Value != null)
         {
             foreach (List <object> item in (List <object>)e.Value)
             {
                 ushort       sn      = (ushort)(Convert.ToInt32(item[0]) & 0xFFFF);
                 ObjectType   type    = (ObjectType)Convert.ToInt32(item[1]);
                 int          version = Convert.ToInt32(item[2]);
                 String       ln      = GXCommon.ToLogicalName((byte[])item[3]);
                 GXDLMSObject obj     = null;
                 if (settings.Objects != null)
                 {
                     obj = settings.Objects.FindBySN(sn);
                 }
                 if (obj == null)
                 {
                     obj = Gurux.DLMS.GXDLMSClient.CreateObject(type);
                     if (obj != null)
                     {
                         obj.LogicalName = ln;
                         obj.ShortName   = sn;
                         obj.Version     = version;
                     }
                 }
                 //Unknown objects are not shown.
                 if (obj is IGXDLMSBase)
                 {
                     ObjectList.Add(obj);
                 }
             }
         }
     }
     else if (e.Index == 3)
     {
         if (e.Value == null)
         {
             foreach (GXDLMSObject it in ObjectList)
             {
                 for (int pos = 1; pos != (it as IGXDLMSBase).GetAttributeCount(); ++pos)
                 {
                     it.SetAccess(pos, AccessMode.NoAccess);
                 }
             }
         }
         else
         {
             UpdateAccessRights((List <object>)e.Value);
         }
     }
     else if (e.Index == 4)
     {
         SecuritySetupReference = GXCommon.ToLogicalName(e.Value);
     }
     else
     {
         e.Error = ErrorCode.ReadWriteDenied;
     }
 }
 public void MethodRequest(GXDLMSObject target, int methodIndex, object data, GXReplyData reply)
 {
     ReadDataBlock(client.Method(target, methodIndex, data, DataType.None), "", reply);
 }
 /// <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)
 {
     GXReplyData reply = new GXReplyData();
     foreach (int it in (obj as IGXDLMSBase).GetAttributeIndexToRead())
     {
         reply.Clear();
         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.Range ||
                     CurrentProfileGeneric.AccessSelector == AccessRange.Last)
                 {
                     byte[][] tmp = client.ReadRowsByRange(CurrentProfileGeneric, Convert.ToDateTime(CurrentProfileGeneric.From), Convert.ToDateTime(CurrentProfileGeneric.To));
                     ReadDataBlock(tmp[0], "Reading profile generic data", 1, reply);
                 }
                 else if (CurrentProfileGeneric.AccessSelector == AccessRange.Entry)
                 {
                     byte[][] tmp = client.ReadRowsByEntry(CurrentProfileGeneric, Convert.ToInt32(CurrentProfileGeneric.From), Convert.ToInt32(CurrentProfileGeneric.To));
                     ReadDataBlock(tmp[0], "Reading profile generic data " + CurrentProfileGeneric.Name, 1, reply);
                 }
                 else //Read all.
                 {
                     byte[] tmp = client.Read(CurrentProfileGeneric, 2)[0];
                     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 == 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 = 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;
             }
         }
     }
 }
예제 #17
0
 void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         if (e.Value is string)
         {
             LogicalName = e.Value.ToString();
         }
         else
         {
             LogicalName = GXDLMSClient.ChangeType((byte[])e.Value, DataType.OctetString).ToString();
         }
     }
     else if (e.Index == 2)
     {
         ObjectList.Clear();
         if (e.Value != null)
         {
             foreach (Object[] item in (Object[])e.Value)
             {
                 ObjectType   type    = (ObjectType)Convert.ToInt32(item[0]);
                 int          version = Convert.ToInt32(item[1]);
                 String       ln      = GXDLMSObject.ToLogicalName((byte[])item[2]);
                 GXDLMSObject obj     = null;
                 if (settings.Objects != null)
                 {
                     obj = settings.Objects.FindByLN(type, ln);
                 }
                 if (obj == null)
                 {
                     obj             = Gurux.DLMS.GXDLMSClient.CreateObject(type);
                     obj.LogicalName = ln;
                     obj.Version     = version;
                 }
                 //Unknown objects are not shown.
                 if (obj is IGXDLMSBase && item[3] != null)
                 {
                     UpdateAccessRights(obj, (Object[])item[3]);
                     ObjectList.Add(obj);
                 }
             }
         }
     }
     else if (e.Index == 3)
     {
         if (e.Value != null)
         {
             ClientSAP = Convert.ToByte(((Object[])e.Value)[0]);
             ServerSAP = Convert.ToUInt16(((Object[])e.Value)[1]);
         }
     }
     else if (e.Index == 4)
     {
         //Value of the object identifier encoded in BER
         if (e.Value is byte[])
         {
             GXByteBuffer arr = new GXByteBuffer(e.Value as byte[]);
             if (arr.GetUInt8(0) == 0x60)
             {
                 ApplicationContextName.JointIsoCtt = 0;
                 ++arr.Position;
                 ApplicationContextName.Country = 0;
                 ++arr.Position;
                 ApplicationContextName.CountryName = 0;
                 ++arr.Position;
                 ApplicationContextName.IdentifiedOrganization = arr.GetUInt8();
                 ApplicationContextName.DlmsUA             = arr.GetUInt8();
                 ApplicationContextName.ApplicationContext = arr.GetUInt8();
                 ApplicationContextName.ContextId          = arr.GetUInt8();
             }
             else
             {
                 //Get Tag and Len.
                 if (arr.GetUInt8() != (int)BerType.Integer && arr.GetUInt8() != 7)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.JointIsoCtt = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.Country = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x12)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.CountryName = arr.GetUInt16();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.IdentifiedOrganization = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.DlmsUA = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.ApplicationContext = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 ApplicationContextName.ContextId = arr.GetUInt8();
             }
         }
         else if (e.Value != null)
         {
             Object[] arr = (Object[])e.Value;
             ApplicationContextName.JointIsoCtt            = Convert.ToByte(arr[0]);
             ApplicationContextName.Country                = Convert.ToByte(arr[1]);
             ApplicationContextName.CountryName            = Convert.ToUInt16(arr[2]);
             ApplicationContextName.IdentifiedOrganization = Convert.ToByte(arr[3]);
             ApplicationContextName.DlmsUA             = Convert.ToByte(arr[4]);
             ApplicationContextName.ApplicationContext = Convert.ToByte(arr[5]);
             ApplicationContextName.ContextId          = Convert.ToByte(arr[6]);
         }
     }
     else if (e.Index == 5)
     {
         if (e.Value != null)
         {
             Object[]     arr = (Object[])e.Value;
             GXByteBuffer bb  = new GXByteBuffer();
             GXCommon.SetBitString(bb, arr[0]);
             bb.SetUInt8(0, 0);
             XDLMSContextInfo.Conformance       = (Conformance)bb.GetUInt32();
             XDLMSContextInfo.MaxReceivePduSize = Convert.ToUInt16(arr[1]);
             XDLMSContextInfo.MaxSendPpuSize    = Convert.ToUInt16(arr[2]);
             XDLMSContextInfo.DlmsVersionNumber = Convert.ToByte(arr[3]);
             XDLMSContextInfo.QualityOfService  = Convert.ToSByte(arr[4]);
             XDLMSContextInfo.CypheringInfo     = (byte[])arr[5];
         }
     }
     else if (e.Index == 6)
     {
         //Value of the object identifier encoded in BER
         if (e.Value is byte[])
         {
             GXByteBuffer arr = new GXByteBuffer(e.Value as byte[]);
             if (arr.GetUInt8(0) == 0x60)
             {
                 AuthenticationMechanismName.JointIsoCtt = 0;
                 ++arr.Position;
                 AuthenticationMechanismName.Country = 0;
                 ++arr.Position;
                 AuthenticationMechanismName.CountryName = 0;
                 ++arr.Position;
                 AuthenticationMechanismName.IdentifiedOrganization = arr.GetUInt8();
                 AuthenticationMechanismName.DlmsUA = arr.GetUInt8();
                 AuthenticationMechanismName.AuthenticationMechanismName = arr.GetUInt8();
                 AuthenticationMechanismName.MechanismId = (Authentication)arr.GetUInt8();
             }
             else
             {
                 //Get Tag and Len.
                 if (arr.GetUInt8() != (int)BerType.Integer && arr.GetUInt8() != 7)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.JointIsoCtt = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.Country = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x12)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.CountryName = arr.GetUInt16();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.IdentifiedOrganization = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.DlmsUA = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.AuthenticationMechanismName = arr.GetUInt8();
                 //Get tag
                 if (arr.GetUInt8() != 0x11)
                 {
                     throw new ArgumentOutOfRangeException();
                 }
                 AuthenticationMechanismName.MechanismId = (Authentication)arr.GetUInt8();
             }
         }
         else if (e.Value != null)
         {
             Object[] arr = (Object[])e.Value;
             AuthenticationMechanismName.JointIsoCtt            = Convert.ToByte(arr[0]);
             AuthenticationMechanismName.Country                = Convert.ToByte(arr[1]);
             AuthenticationMechanismName.CountryName            = Convert.ToUInt16(arr[2]);
             AuthenticationMechanismName.IdentifiedOrganization = Convert.ToByte(arr[3]);
             AuthenticationMechanismName.DlmsUA = Convert.ToByte(arr[4]);
             AuthenticationMechanismName.AuthenticationMechanismName = Convert.ToByte(arr[5]);
             AuthenticationMechanismName.MechanismId = (Authentication)Convert.ToByte(arr[6]);
         }
     }
     else if (e.Index == 7)
     {
         Secret = (byte[])e.Value;
     }
     else if (e.Index == 8)
     {
         if (e.Value == null)
         {
             AssociationStatus = AssociationStatus.NonAssociated;
         }
         else
         {
             AssociationStatus = (AssociationStatus)Convert.ToInt32(e.Value);
         }
     }
     else if (e.Index == 9)
     {
         SecuritySetupReference = GXDLMSClient.ChangeType((byte[])e.Value, DataType.OctetString).ToString();
     }
     else
     {
         e.Error = ErrorCode.ReadWriteDenied;
     }
 }
예제 #18
0
 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;
     }
 }
예제 #19
0
 void IGXDLMSBase.SetValue(int index, object value)
 {
     if (index == 1)
     {
         if (value is string)
         {
             LogicalName = value.ToString();
         }
         else
         {
             LogicalName = GXDLMSClient.ChangeType((byte[])value, DataType.OctetString).ToString();
         }
     }
     else if (index == 2)
     {
         if (CaptureObjects == null || CaptureObjects.Count == 0)
         {
             throw new Exception("Read capture objects first.");
         }
         if (value != null && (value as object[]).Length != 0)
         {
             DateTime lastDate = DateTime.MinValue;
             foreach (object[] row in (value as object[]))
             {
                 if ((row as object[]).Length != CaptureObjects.Count)
                 {
                     throw new Exception("Number of columns do not match.");
                 }
                 for (int pos = 0; pos != row.Length; ++pos)
                 {
                     DataType type = CaptureObjects[pos].Key.GetUIDataType(CaptureObjects[pos].Value.AttributeIndex);
                     if (row[pos] is byte[])
                     {
                         if (type != DataType.None && row[pos] is byte[])
                         {
                             row[pos] = GXDLMSClient.ChangeType(row[pos] as byte[], type);
                             if (row[pos] is GXDateTime)
                             {
                                 GXDateTime dt = (GXDateTime)row[pos];
                                 lastDate = dt.Value;
                             }
                         }
                     }
                     else if (type == DataType.DateTime && row[pos] == null && CapturePeriod != 0)
                     {
                         if (lastDate == DateTime.MinValue && Buffer.Count != 0)
                         {
                             lastDate = ((GXDateTime)Buffer[Buffer.Count - 1].GetValue(pos)).Value;
                         }
                         if (lastDate != DateTime.MinValue)
                         {
                             lastDate = lastDate.AddSeconds(CapturePeriod);
                             row[pos] = new GXDateTime(lastDate);
                         }
                     }
                     if (CaptureObjects[pos].Key is GXDLMSRegister && CaptureObjects[pos].Value.AttributeIndex == 2)
                     {
                         double scaler = (CaptureObjects[pos].Key as GXDLMSRegister).Scaler;
                         if (scaler != 1)
                         {
                             try
                             {
                                 row[pos] = Convert.ToDouble(row[pos]) * scaler;
                             }
                             catch
                             {
                                 //Skip error
                             }
                         }
                     }
                 }
                 Buffer.Add(row);
             }
             EntriesInUse = Buffer.Count;
         }
     }
     else if (index == 3)
     {
         Buffer.Clear();
         EntriesInUse = 0;
         CaptureObjects.Clear();
         GXDLMSObjectCollection objects = new GXDLMSObjectCollection();
         foreach (object it in value as object[])
         {
             object[] tmp = it as object[];
             if (tmp.Length != 4)
             {
                 throw new GXDLMSException("Invalid structure format.");
             }
             ObjectType   type           = (ObjectType)Convert.ToInt16(tmp[0]);
             string       ln             = GXDLMSObject.toLogicalName((byte[])tmp[1]);
             int          attributeIndex = Convert.ToInt16(tmp[2]);
             int          dataIndex      = Convert.ToInt16(tmp[3]);
             GXDLMSObject obj            = null;
             if (Parent != null)
             {
                 obj = Parent.FindByLN(type, ln);
             }
             if (obj == null)
             {
                 obj = GXDLMSClient.CreateDLMSObject((int)type, null, 0, ln, 0);
             }
             CaptureObjects.Add(new GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(obj, new GXDLMSCaptureObject(attributeIndex, dataIndex)));
             objects.Add(obj);
         }
         GXDLMSClient.UpdateOBISCodes(objects);
     }
     else if (index == 4)
     {
         CapturePeriod = Convert.ToInt32(value);
     }
     else if (index == 5)
     {
         SortMethod = (SortMethod)Convert.ToInt32(value);
     }
     else if (index == 6)
     {
         if (value != null)
         {
             object[] tmp = value as object[];
             if (tmp.Length != 4)
             {
                 throw new GXDLMSException("Invalid structure format.");
             }
             ObjectType type = (ObjectType)Convert.ToInt16(tmp[0]);
             string     ln   = GXDLMSObject.toLogicalName((byte[])tmp[1]);
             SortAttributeIndex = Convert.ToInt16(tmp[2]);
             SortDataIndex      = Convert.ToInt16(tmp[3]);
             SortObject         = null;
             foreach (var it in CaptureObjects)
             {
                 if (it.Key.ObjectType == type && it.Key.LogicalName == ln)
                 {
                     SortObject = it.Key;
                     break;
                 }
             }
         }
         else
         {
             SortObject = null;
         }
     }
     else if (index == 7)
     {
         EntriesInUse = Convert.ToInt32(value);
     }
     else if (index == 8)
     {
         ProfileEntries = Convert.ToInt32(value);
     }
     else
     {
         throw new ArgumentException("SetValue failed. Invalid attribute index.");
     }
 }
예제 #20
0
 public static GXDLMSObject CreateObject(Gurux.DLMS.ObjectType type)
 {
     lock (AvailableObjectTypes)
     {
         //Update objects.
         if (AvailableObjectTypes.Count == 0)
         {
             GetObjectTypes();
         }
         if (AvailableObjectTypes.ContainsKey(type))
         {
             return Activator.CreateInstance(AvailableObjectTypes[type]) as GXDLMSObject;
         }
     }
     GXDLMSObject obj = new GXDLMSObject();
     obj.ObjectType = type;
     return obj;
 }
        void OnUpdateTarget(GXDLMSObject value)
        {
            m_Target = (GXDLMSProfileGeneric)value;
            m_MyPane.GraphPane.CurveList.Clear();
            GXDLMSObject obj;
            int index = 0;
            if (m_Target != null)
            {
                m_MyPane.GraphPane.Title.Text = m_Target.Description;
                DataTable table = ProfileGenericView.DataSource as DataTable;
                ProfileGenericView.DataSource = null;
                ProfileGenericView.Columns.Clear();
                DataTable dt = new DataTable();
                foreach (var it in m_Target.CaptureObjects)
                {
                    DataColumn dc = dt.Columns.Add(index.ToString());
                    dc.Caption = it.Key.Description;
                    int pos = ProfileGenericView.Columns.Add(index.ToString(), it.Key.Description);
                    ProfileGenericView.Columns[pos].DataPropertyName = index.ToString();
                    ++index;
                }
                foreach(object[] it in m_Target.Buffer)
                {
                    dt.LoadDataRow(it, true);
                }

                ProfileGenericView.DataSource = dt;
                if (m_Target.CaptureObjects.Count != 0 && m_Target.CaptureObjects[0].Value.AttributeIndex != 0)
                {
                    //We can show graph only tables that are date based.
                    if (m_Target.CaptureObjects[0].Key.GetUIDataType(m_Target.CaptureObjects[0].Value.AttributeIndex) == DataType.DateTime)
                    {
                        for (int col = 0; col < m_Target.CaptureObjects.Count; ++col)
                        {
                            //Do not shown Status' or Events
                            index = m_Target.CaptureObjects[col].Value.AttributeIndex;
                            if (index > 0 && ((index & 0x8) != 0 || (m_Target.CaptureObjects[col].Value.AttributeIndex & 0x10) != 0))
                            {
                                continue;
                            }
                            obj = m_Target.CaptureObjects[col].Key;
                            GXGraphItem item = GraphItems.Find(obj.LogicalName, index);
                            if (item != null && item.Enabled && GXHelpers.IsNumeric(obj.GetUIDataType(index)))
                            {
                                ZedGraph.DataSourcePointList dspl = new ZedGraph.DataSourcePointList();
                                dspl.DataSource = m_Target.Buffer;
                                dspl.XDataMember = m_Target.CaptureObjects[0].Key.Description;
                                dspl.YDataMember = obj.Description;
                                ZedGraph.LineItem myCurve = m_MyPane.GraphPane.AddCurve(obj.Description, dspl, item.Color);
                            }
                        }
                        m_MyPane.GraphPane.XAxis.Title.Text = m_Target.CaptureObjects[0].Key.LogicalName;
                        // Tell ZedGraph to refigure the axes since the data have changed
                        m_MyPane.AxisChange();
                    }
                }
            }
            else
            {
                ProfileGenericView.DataSource = null;
            }

            //Set initial values...
            ReadFromRB.Enabled = ReadLastRB.Enabled = ReadEntryBtn.Enabled = m_Target.CaptureObjects.Count != 0;
            ReadFromRB.Checked = ReadLastRB.Checked = ReadEntryBtn.Checked = false;
            StartEntry.Value = 0;
            EndEntry.Value = 1;
            ReadLastTB.Value = 0;
            StartPick.Value = ToPick.Value = DateTime.Now;
            if (!ReadFromRB.Enabled)
            {
                return;
            }
            index = m_Target.CaptureObjects[0].Value.AttributeIndex;
            obj = m_Target.CaptureObjects[0].Key;
            if (index != 0 &&
                obj.GetUIDataType(index) != DataType.DateTime)
            {
                ReadFromRB.Enabled = ReadLastRB.Enabled = false;
                m_Target.AccessSelector = AccessRange.Entry;
                m_Target.From = 0;
                m_Target.To = 1;
            }
            else
            {
                ReadFromRB.Enabled = ReadLastRB.Enabled = true;
            }
            if (m_Target.AccessSelector == AccessRange.Entry)
            {
                StartEntry.Value = Convert.ToInt32(m_Target.From);
                EndEntry.Value = Convert.ToInt32(m_Target.To);
                ReadEntryBtn.Checked = true;
            }
            else if (m_Target.AccessSelector == AccessRange.Last)
            {
                TimeSpan diff = (DateTime)m_Target.To - (DateTime)m_Target.From;
                ReadLastTB.Value = diff.Days - 1;
                ReadLastRB.Checked = true;
            }
            else if (m_Target.AccessSelector == AccessRange.Range)
            {
                if ((DateTime)m_Target.From == DateTime.MinValue)
                {
                    StartPick.Checked = false;
                }
                else
                {
                    StartPick.Value = (DateTime)m_Target.From;
                }
                if ((DateTime)m_Target.To == DateTime.MaxValue)
                {
                    ToPick.Checked = false;
                }
                else
                {
                    ToPick.Value = (DateTime)m_Target.To;
                }
                ReadFromRB.Checked = true;
            }
            else //All is checked.
            {
                ReadAllRB.Checked = true;
            }
        }
예제 #22
0
 private static void UpdateOBISCodeInfo(GXStandardObisCodeCollection codes, GXDLMSObject it)
 {
     if (!string.IsNullOrEmpty(it.Description))
     {
         return;
     }
     GXStandardObisCode code = codes.Find(it.LogicalName, it.ObjectType)[0];
     it.Description = code.Description;
     //If string is used
     if (code.DataType.Contains("10"))
     {
         code.UIDataType = "10";
     }
     //If date time is used.
     else if (code.DataType.Contains("25") || code.DataType.Contains("26"))
     {
         code.UIDataType = code.DataType = "25";
     }
     //Time stamps of the billing periods objects (first scheme if there are two)
     else if (code.DataType.Contains("9"))
     {
         if ((GXStandardObisCodeCollection.EqualsMask("0.0-64.96.7.10-14.255", it.LogicalName) ||
                 //Time stamps of the billing periods objects (second scheme)
                 GXStandardObisCodeCollection.EqualsMask("0.0-64.0.1.5.0-99,255", it.LogicalName) ||
                 //Time of power failure
                 GXStandardObisCodeCollection.EqualsMask("0.0-64.0.1.2.0-99,255", it.LogicalName) ||
                 //Time stamps of the billing periods objects (first scheme if there are two)
                 GXStandardObisCodeCollection.EqualsMask("1.0-64.0.1.2.0-99,255", it.LogicalName) ||
                 //Time stamps of the billing periods objects (second scheme)
                 GXStandardObisCodeCollection.EqualsMask("1.0-64.0.1.5.0-99,255", it.LogicalName) ||
                 //Time expired since last end of billing period
                 GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.0.255", it.LogicalName) ||
                 //Time of last reset
                 GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.6.255", it.LogicalName) ||
                 //Date of last reset
                 GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.7.255", it.LogicalName) ||
                 //Time expired since last end of billing period (Second billing period scheme)
                 GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.13.255", it.LogicalName) ||
                 //Time of last reset (Second billing period scheme)
                 GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.14.255", it.LogicalName) ||
                 //Date of last reset (Second billing period scheme)
                 GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.15.255", it.LogicalName)))
         {
             code.UIDataType = "25";
         }
         //Local time
         else if (GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.1.255", it.LogicalName))
         {
             code.UIDataType = "27";
         }
         //Local date
         else if (GXStandardObisCodeCollection.EqualsMask("1.0-64.0.9.2.255", it.LogicalName))
         {
             code.UIDataType = "26";
         }
         //Active firmware identifier
         else if (GXStandardObisCodeCollection.EqualsMask("1.0.0.2.0.255", it.LogicalName))
         {
             code.UIDataType = "10";
         }
     }
     if (code.DataType != "*" && code.DataType != string.Empty && !code.DataType.Contains(","))
     {
         DataType type = (DataType)int.Parse(code.DataType);
         switch (it.ObjectType)
         {
             case ObjectType.Data:
             case ObjectType.Register:
             case ObjectType.RegisterActivation:
             case ObjectType.ExtendedRegister:
                 it.SetDataType(2, type);
                 break;
             default:
                 break;
         }
     }
     if (!string.IsNullOrEmpty(code.UIDataType))
     {
         DataType uiType = (DataType)int.Parse(code.UIDataType);
         switch (it.ObjectType)
         {
             case ObjectType.Data:
             case ObjectType.Register:
             case ObjectType.RegisterActivation:
             case ObjectType.ExtendedRegister:
                 it.SetUIDataType(2, uiType);
                 break;
             default:
                 break;
         }
     }
 }
 /// <summary>
 /// Method attribute value.
 /// </summary>
 public void Method(GXDLMSObject it, int attributeIndex, object value, DataType type)
 {
     ReadDataBlock(Client.Method(it, attributeIndex, value, type)[0]);
 }
예제 #24
0
 /// <summary>
 /// Get Value from byte array received from the meter.
 /// </summary>
 public object UpdateValue(byte[] data, GXDLMSObject target, int attributeIndex)
 {
     target.SetValue(attributeIndex, GetValue(data, target, attributeIndex));
     return target.GetValues()[attributeIndex - 1];
 }
 /// <summary>
 /// Read attribute value.
 /// </summary>
 public object Read(GXDLMSObject it, int attributeIndex)
 {
     byte[] reply = ReadDataBlock(Client.Read(it.Name, it.ObjectType, attributeIndex)[0]);
     //Update data type.
     if (it.GetDataType(attributeIndex) == DataType.None)
     {
         it.SetDataType(attributeIndex, Client.GetDLMSDataType(reply));
     }
     return Client.UpdateValue(reply, it, attributeIndex);
 }
예제 #26
0
 public static GXDLMSAttributeSettings GetAttributeInfo(GXDLMSObject item, int index)
 {
     GXDLMSAttributeSettings att = item.Attributes.Find(index);
     return att;
 }
 /// <summary>
 /// Read Profile Generic Columns by entry.
 /// </summary>
 public object[] ReadRowsByEntry(GXDLMSObject it, int index, int count)
 {
     byte[] reply = ReadDataBlock(Client.ReadRowsByEntry(it.Name, index, count));
     return (object[]) Client.UpdateValue(reply, it, 2);
 }
예제 #28
0
 /// <summary>
 /// Generates a write message.
 /// </summary>
 /// <param name="item">object to write.</param>
 /// <param name="index">Attribute index where data is write.</param>
 /// <returns></returns>
 public byte[][] Write(GXDLMSObject item, int index)
 {
     if (item == null || index < 1)
     {
         throw new GXDLMSException("Invalid parameter");
     }
     Object value = (item as IGXDLMSBase).GetValue(index, 0, null);
     DataType type = item.GetDataType(index);
     if (type == DataType.None)
     {
         type = Gurux.DLMS.Internal.GXCommon.GetValueType(value);
     }
     return Write(item.Name, value, type, item.ObjectType, index);
 }
 /// <summary>
 /// Read Profile Generic Columns by range.
 /// </summary>
 public object[] ReadRowsByRange(GXDLMSObject it, DateTime start, DateTime end, List<GXKeyValuePair<GXDLMSObject, GXDLMSCaptureObject>> columns)
 {
     GXDLMSObject col = columns[0].Key;
     byte[] reply = ReadDataBlock(Client.ReadRowsByRange(it.Name, col.LogicalName, col.ObjectType, col.Version, start, end));
     return (object[])Client.UpdateValue(reply, it, 2);
 }
예제 #30
0
 /// <summary>
 /// Reserved for internal use.
 /// </summary>
 /// <param name="ClassID"></param>
 /// <param name="Version"></param>
 /// <param name="BaseName"></param>
 /// <param name="LN"></param>
 /// <param name="AccessRights"></param>
 /// <param name="AttributeIndex"></param>
 /// <param name="dataIndex"></param>
 /// <returns></returns>
 internal static GXDLMSObject CreateDLMSObject(int ClassID, object Version, int BaseName, object LN, object AccessRights)
 {
     GXDLMSObject obj = null;
     ObjectType type = (ObjectType)ClassID;
     if (GXDLMS.AvailableObjectTypes.ContainsKey(type))
     {
         Type tmp = GXDLMS.AvailableObjectTypes[type];
         obj = Activator.CreateInstance(tmp) as GXDLMSObject;
     }
     else
     {
         obj = new GXDLMSObject();
     }
     UpdateObjectData(obj, type, Version, BaseName, LN, AccessRights);
     return obj;
 }
 /// <summary>
 /// Write attribute value.
 /// </summary>
 public void Write(GXDLMSObject it, int attributeIndex)
 {
     ReadDataBlock(Client.Write(it, attributeIndex));
 }
 public byte[] Read(GXDLMSObject it, int attributeOrdinal)
 {
     lastTransaction = DateTime.Now;
     byte[] tmp = client.Read(it, attributeOrdinal)[0];
     GXLogWriter.WriteLog(string.Format("Reading object {0} from interface {1}", it.LogicalName, it.ObjectType), tmp);
     return tmp;
 }
 /// <summary>
 /// Read data type of selected attribute index.
 /// </summary>
 public DataType GetDLMSDataType(GXDLMSObject it, int attributeIndex)
 {
     byte[] reply = ReadDataBlock(Client.Read(it.Name, it.ObjectType, attributeIndex)[0]);
     return Client.GetDLMSDataType(reply);
 }
 public void ReadValue(GXDLMSObject it, int attributeOrdinal)
 {
     GXReplyData reply = new GXReplyData();
     ReadDataBlock(Read(it, attributeOrdinal), "Read object", reply);
     //If data type is unknown
     if (it.GetDataType(attributeOrdinal) == DataType.None)
     {
         it.SetDataType(attributeOrdinal, reply.DataType);
     }
     client.UpdateValue(it, attributeOrdinal, reply.Value);
 }
예제 #35
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public ValueEventArgs(GXDLMSObject target, int index, int selector, object parameters)
 {
     Target = target;
     Index = index;
     Selector = selector;
     Parameters = parameters;
 }
예제 #36
0
 /// <summary>
 /// This method is used to solve Column's data type in Profile Generic table.
 /// </summary>
 /// <param name="component"></param>
 /// <param name="attributeIndex"></param>
 /// <returns></returns>
 internal static DataType GetAttributeType(GXDLMSObject component, int attributeIndex)
 {
     if (attributeIndex != 0)
     {
         if (attributeIndex > 0x10)
         {
             attributeIndex = 2;
         }
         GXDLMSAttributeSettings att2 = component.Attributes.Find(attributeIndex);
         if (att2 != null && att2.Type != DataType.None)
         {
             return att2.Type;
         }
         PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(component);
         foreach (PropertyDescriptor pd in pdc)
         {
             GXDLMSAttributeSettings att = (GXDLMSAttributeSettings)pd.Attributes[typeof(GXDLMSAttributeSettings)];
             if (att != null)
             {
                 if (att.Index == attributeIndex)
                 {
                     if (att.UIType != DataType.None)
                     {
                         return att.UIType;
                     }
                     //If expected type is not given return property type.
                     if (pd.PropertyType == typeof(Int32))
                     {
                         return DataType.Int32;
                     }
                     if (pd.PropertyType == typeof(UInt32))
                     {
                         return DataType.UInt32;
                     }
                     if (pd.PropertyType == typeof(String))
                     {
                         return DataType.String;
                     }
                     if (pd.PropertyType == typeof(byte))
                     {
                         return DataType.UInt8;
                     }
                     if (pd.PropertyType == typeof(sbyte))
                     {
                         return DataType.Int8;
                     }
                     if (pd.PropertyType == typeof(Int16))
                     {
                         return DataType.Int16;
                     }
                     if (pd.PropertyType == typeof(UInt16))
                     {
                         return DataType.UInt16;
                     }
                     if (pd.PropertyType == typeof(Int64))
                     {
                         return DataType.Int64;
                     }
                     if (pd.PropertyType == typeof(UInt64))
                     {
                         return DataType.UInt64;
                     }
                     if (pd.PropertyType == typeof(float))
                     {
                         return DataType.Float32;
                     }
                     if (pd.PropertyType == typeof(double))
                     {
                         return DataType.Float64;
                     }
                     if (pd.PropertyType == typeof(DateTime))
                     {
                         return DataType.DateTime;
                     }
                     if (pd.PropertyType == typeof(Boolean) || pd.PropertyType == typeof(bool))
                     {
                         return DataType.Boolean;
                     }
                     if (pd.PropertyType == typeof(object))
                     {
                         return DataType.None;
                     }
                 }
             }
         }
     }
     return DataType.None;
 }
예제 #37
0
 private byte[] GetValue(object name, GXDLMSObject item, int index, int selector, object parameters)
 {
     IGXDLMSBase tmp = item as IGXDLMSBase;
     object value = null;             
     if (tmp != null)
     {
         value = tmp.GetValue(index, selector, parameters);
     }
     else
     {
         object[] values = item.GetValues();
         if (index <= values.Length)
         {
             value = values[index - 1];
         }
     }
     Gurux.DLMS.DataType tp = item.GetDataType(index);
     if (tp == DataType.None)
     {
         tp = Gurux.DLMS.Internal.GXCommon.GetValueType(value);
     }
     //If data is shown as string, but it's OCTECT String.
     if (tp == DataType.OctetString && value is string && item.GetUIDataType(index) == DataType.String)
     {
         value = ASCIIEncoding.ASCII.GetBytes((string)value);
     }
     if (tp != DataType.None || (value == null && tp == DataType.None))
     {
         SendData.AddRange(ReadReply(name, item.ObjectType, index, value, tp));
         return SendData[FrameIndex];
     }
     //Return HW error.
     throw new ArgumentOutOfRangeException();
 }
예제 #38
0
 bool ShouldSkip(GXDLMSObject it, int index)
 {
     //Skip Scaler and unit.
     if (it is GXDLMSRegister && index == 3)
     {
         return true;
     }
     return false;
 }
예제 #39
0
 byte[] IGXDLMSBase.Invoke(GXDLMSSettings settings, ValueEventArgs e)
 {
     //Check reply_to_HLS_authentication
     if (e.Index == 1)
     {
         UInt32 ic = 0;
         byte[] secret;
         if (settings.Authentication == Authentication.HighGMAC)
         {
             secret = settings.SourceSystemTitle;
             GXByteBuffer bb = new GXByteBuffer(e.Parameters as byte[]);
             bb.GetUInt8();
             ic = bb.GetUInt32();
         }
         else
         {
             secret = Secret;
         }
         byte[] serverChallenge = GXSecure.Secure(settings, settings.Cipher, ic, settings.StoCChallenge, secret);
         byte[] clientChallenge = (byte[])e.Parameters;
         if (serverChallenge != null && clientChallenge != null && GXCommon.Compare(serverChallenge, clientChallenge))
         {
             if (settings.Authentication == Authentication.HighGMAC)
             {
                 secret = settings.Cipher.SystemTitle;
                 ic     = settings.Cipher.InvocationCounter;
             }
             else
             {
                 secret = Secret;
             }
             AssociationStatus = AssociationStatus.Associated;
             return(GXSecure.Secure(settings, settings.Cipher, ic, settings.CtoSChallenge, secret));
         }
         else //If the password does not match.
         {
             AssociationStatus = AssociationStatus.NonAssociated;
             return(null);
         }
     }
     else if (e.Index == 2)
     {
         byte[] tmp = e.Parameters as byte[];
         if (tmp == null || tmp.Length == 0)
         {
             e.Error = ErrorCode.ReadWriteDenied;
         }
         else
         {
             Secret = tmp;
         }
     }
     else if (e.Index == 3)
     {
         //Add COSEM object.
         GXDLMSObject obj = GetObject(settings, e.Parameters as object[]);
         //Unknown objects are not add.
         if (obj is IGXDLMSBase)
         {
             if (ObjectList.FindByLN(obj.ObjectType, obj.LogicalName) == null)
             {
                 ObjectList.Add(obj);
             }
             if (settings.Objects.FindByLN(obj.ObjectType, obj.LogicalName) == null)
             {
                 settings.Objects.Add(obj);
             }
         }
     }
     else if (e.Index == 4)
     {
         //Remove COSEM object.
         GXDLMSObject obj = GetObject(settings, e.Parameters as object[]);
         //Unknown objects are not removed.
         if (obj is IGXDLMSBase)
         {
             GXDLMSObject t = ObjectList.FindByLN(obj.ObjectType, obj.LogicalName);
             if (t != null)
             {
                 ObjectList.Remove(t);
             }
             //Item is not removed from all objects. It might be that use wants remove object only from association view.
         }
     }
     else if (e.Index == 5)
     {
         object[] tmp = e.Parameters as object[];
         if (tmp == null || tmp.Length != 2)
         {
             e.Error = ErrorCode.ReadWriteDenied;
         }
         else
         {
             UserList.Add(new KeyValuePair <byte, string>(Convert.ToByte(tmp[0]), Convert.ToString(tmp[1])));
         }
     }
     else if (e.Index == 6)
     {
         object[] tmp = e.Parameters as object[];
         if (tmp == null || tmp.Length != 2)
         {
             e.Error = ErrorCode.ReadWriteDenied;
         }
         else
         {
             UserList.Remove(new KeyValuePair <byte, string>(Convert.ToByte(tmp[0]), Convert.ToString(tmp[1])));
         }
     }
     else
     {
         e.Error = ErrorCode.ReadWriteDenied;
     }
     return(null);
 }