/// <summary>
        ///  Load COSEM objects from the file.
        /// </summary>
        /// <param name="stream">Stream.</param>
        /// <returns>Collection of serialized COSEM objects.</returns>
        public static GXDLMSObjectCollection Load(Stream stream)
        {
            GXDLMSObjectCollection objects = new GXDLMSObjectCollection();

            Load(stream, objects);
            return(objects);
        }
Пример #2
0
        /// <summary>
        ///  Load COSEM objects from the file.
        /// </summary>
        /// <param name="stream">Stream.</param>
        /// <param name="objects">Collection of COSEM objects.</param>
        public static void Load(Stream stream, GXDLMSObjectCollection objects)
        {
            GXDLMSObject obj = null;
            String       target;
            ObjectType   type;

            using (GXXmlReader reader = new GXXmlReader(stream))
            {
                reader.Objects = objects;
                while (!reader.EOF)
                {
                    if (reader.IsStartElement())
                    {
                        target = reader.Name;
                        if (string.Compare("Objects", target, true) == 0)
                        {
                            //Skip.
                            reader.Read();
                        }
                        else if (string.Compare("Object", target, true) == 0)
                        {
                            int    r   = 0;
                            string str = reader.GetAttribute(0);
                            if (int.TryParse(str, out r))
                            {
                                type = (ObjectType)r;
                            }
                            else
                            {
                                type = (ObjectType)Enum.Parse(typeof(ObjectType), str);
                            }
                            reader.Read();
                            obj = GXDLMSClient.CreateObject(type);
                            reader.Objects.Add(obj);
                        }
                        else if (string.Compare("SN", target, true) == 0)
                        {
                            obj.ShortName = (UInt16)reader.ReadElementContentAsInt("SN");
                        }
                        else if (string.Compare("LN", target, true) == 0)
                        {
                            obj.LogicalName = reader.ReadElementContentAsString("LN");
                        }
                        else if (string.Compare("Description", target, true) == 0)
                        {
                            obj.Description = reader.ReadElementContentAsString("Description");
                        }
                        else
                        {
                            (obj as IGXDLMSBase).Load(reader);
                            obj = null;
                        }
                    }
                    else
                    {
                        reader.Read();
                    }
                }
            }
        }
 /// <summary>
 ///  Load COSEM objects from the file.
 /// </summary>
 /// <param name="filename"> File path.</param>
 /// <returns>Collection of serialized COSEM objects.</returns>
 public static void Load(string filename, GXDLMSObjectCollection objects)
 {
     using (Stream stream = File.OpenRead(filename))
     {
         Load(stream, objects);
     }
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="ln">Logical Name of the object.</param>
 public GXDLMSAssociationLogicalName(string ln)
     : base(ObjectType.AssociationLogicalName, ln, 0)
 {
     ObjectList                  = new GXDLMSObjectCollection();
     ApplicationContextName      = new GXApplicationContextName();
     XDLMSContextInfo            = new GXxDLMSContextType();
     AuthenticationMechanismName = new GXAuthenticationMechanismName();
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 public GXDLMSAssociationLogicalName()
     : this("0.0.40.0.0.255")
 {
     ObjectList                  = new GXDLMSObjectCollection();
     ApplicationContextName      = new GXApplicationContextName();
     XDLMSContextInfo            = new GXxDLMSContextType();
     AuthenticationMechanismName = new GXAuthenticationMechanismName();
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="ln">Logical Name of the object.</param>
 public GXDLMSAssociationLogicalName(string ln)
     : base(ObjectType.AssociationLogicalName, ln, 0)
 {
     ObjectList                  = new GXDLMSObjectCollection();
     ApplicationContextName      = new GXApplicationContextName();
     XDLMSContextInfo            = new GXxDLMSContextType();
     AuthenticationMechanismMame = new GXAuthenticationMechanismName();
     //Default shared secred.
     Secret = ASCIIEncoding.ASCII.GetBytes("Gurux");
 }
Пример #7
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="ln">Logical Name of the object.</param>
 public GXDLMSAssociationLogicalName(string ln)
     : base(ObjectType.AssociationLogicalName, ln, 0)
 {
     Version                     = 3;
     ObjectList                  = new GXDLMSObjectCollection();
     ApplicationContextName      = new GXApplicationContextName();
     XDLMSContextInfo            = new GXxDLMSContextType();
     AuthenticationMechanismName = new GXAuthenticationMechanismName();
     UserList                    = new List <KeyValuePair <byte, string> >();
 }
Пример #8
0
        public GXDLMSObjectCollection GetObjects(ObjectType type)
        {
            GXDLMSObjectCollection items = new GXDLMSObjectCollection();

            foreach (GXDLMSObject it in this)
            {
                if (it.ObjectType == type)
                {
                    items.Add(it);
                }
            }
            return(items);
        }
Пример #9
0
        public GXDLMSObjectCollection GetObjects(ObjectType[] types)
        {
            GXDLMSObjectCollection items = new GXDLMSObjectCollection();

            foreach (GXDLMSObject it in this)
            {
                if (types.Contains(it.ObjectType))
                {
                    items.Add(it);
                }
            }
            return(items);
        }
Пример #10
0
 object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (ObjectList == null)
     {
         ObjectList = new GXDLMSObjectCollection();
     }
     if (e.Index == 1)
     {
         return(this.LogicalName);
     }
     else if (e.Index == 2)
     {
         return(GetObjects(settings, e).Array());
     }
     else if (e.Index == 3)
     {
         bool lnExists = ObjectList.FindBySN(this.ShortName) != null;
         //Add count
         int cnt = ObjectList.Count;
         if (!lnExists)
         {
             ++cnt;
         }
         GXByteBuffer data = new GXByteBuffer();
         data.SetUInt8((byte)DataType.Array);
         GXCommon.SetObjectCount(cnt, data);
         foreach (GXDLMSObject it in ObjectList)
         {
             GetAccessRights(settings, it, data);
         }
         if (!lnExists)
         {
             GetAccessRights(settings, this, data);
         }
         return(data.Array());
     }
     else if (e.Index == 4)
     {
         GXByteBuffer data = new GXByteBuffer();
         GXCommon.SetData(settings, data, DataType.OctetString, SecuritySetupReference);
         return(data.Array());
     }
     e.Error = ErrorCode.ReadWriteDenied;
     return(null);
 }
Пример #11
0
 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)
     {
         if (CaptureObjects == null || CaptureObjects.Count == 0)
         {
             throw new Exception("Read capture objects first.");
         }
         if (value != null && (value as object[]).Length != 0)
         {
             int      index2   = 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)
                 {
                     index2 = CaptureObjects[pos].Value.AttributeIndex;
                     DataType type;
                     //Actaris SL 7000 and ACE 6000 returns 0.
                     if (index2 != 0)
                     {
                         type = CaptureObjects[pos].Key.GetUIDataType(index2);
                     }
                     else
                     {
                         type = DataType.None;
                     }
                     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.LocalDateTime;
                             }
                         }
                     }
                     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.LocalDateTime;
                         }
                         if (lastDate != DateTime.MinValue)
                         {
                             lastDate = lastDate.AddSeconds(CapturePeriod);
                             row[pos] = new GXDateTime(lastDate);
                         }
                     }
                     if (CaptureObjects[pos].Key is GXDLMSRegister && index2 == 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 (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 (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.");
     }
 }
Пример #12
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="filename"></param>
 internal GXXmlReader(string filename)
 {
     reader  = XmlReader.Create(filename);
     Objects = new GXDLMSObjectCollection();
 }
Пример #13
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="stream">Stream.</param>
 internal GXXmlReader(Stream stream)
 {
     reader  = XmlReader.Create(stream);
     Objects = new GXDLMSObjectCollection();
 }
Пример #14
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;
            }
        }
Пример #15
0
 object IGXDLMSBase.GetValue(int index, int selector, object parameters)
 {
     if (ObjectList == null)
     {
         ObjectList = new GXDLMSObjectCollection();
     }
     if (index == 1)
     {
         return(this.LogicalName);
     }
     else if (index == 2)
     {
         int         cnt  = ObjectList.Count;
         List <byte> data = new List <byte>();
         data.Add((byte)DataType.Array);
         //Add count
         GXCommon.SetObjectCount(cnt, data);
         if (cnt != 0)
         {
             foreach (GXDLMSObject it in ObjectList)
             {
                 data.Add((byte)DataType.Structure);
                 data.Add((byte)4);                                            //Count
                 GXCommon.SetData(data, DataType.Int16, it.ShortName);         //base address.
                 GXCommon.SetData(data, DataType.UInt16, it.ObjectType);       //ClassID
                 GXCommon.SetData(data, DataType.UInt8, 0);                    //Version
                 GXCommon.SetData(data, DataType.OctetString, it.LogicalName); //LN
             }
             if (ObjectList.FindBySN(this.ShortName) == null)
             {
                 data.Add((byte)DataType.Structure);
                 data.Add((byte)4);                                              //Count
                 GXCommon.SetData(data, DataType.Int16, this.ShortName);         //base address.
                 GXCommon.SetData(data, DataType.UInt16, this.ObjectType);       //ClassID
                 GXCommon.SetData(data, DataType.UInt8, 0);                      //Version
                 GXCommon.SetData(data, DataType.OctetString, this.LogicalName); //LN
             }
         }
         return(data.ToArray());
     }
     else if (index == 3)
     {
         bool lnExists = ObjectList.FindBySN(this.ShortName) != null;
         //Add count
         int cnt = ObjectList.Count;
         if (!lnExists)
         {
             ++cnt;
         }
         List <byte> data = new List <byte>();
         data.Add((byte)DataType.Array);
         GXCommon.SetObjectCount(cnt, data);
         foreach (GXDLMSObject it in ObjectList)
         {
             GetAccessRights(it, data);
         }
         if (!lnExists)
         {
             GetAccessRights(this, data);
         }
         return(data.ToArray());
     }
     else if (index == 4)
     {
         List <byte> data = new List <byte>();
         GXCommon.SetData(data, DataType.OctetString, SecuritySetupReference);
         return(data.ToArray());
     }
     throw new ArgumentException("GetValue failed. Invalid attribute index.");
 }
Пример #16
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="ln">Logical Name of the object.</param>
 /// <param name="sn">Short Name of the object.</param>
 public GXDLMSAssociationShortName(string ln, ushort sn)
     : base(ObjectType.AssociationShortName, ln, 0)
 {
     ObjectList = new GXDLMSObjectCollection();
 }
Пример #17
0
        /// <summary>
        ///  Load COSEM objects from the file.
        /// </summary>
        /// <param name="stream">Stream.</param>
        /// <param name="objects">Collection of COSEM objects.</param>
        public static void Load(Stream stream, GXDLMSObjectCollection objects)
        {
            GXDLMSObject obj = null;
            String       target;
            ObjectType   type;

            using (GXXmlReader reader = new GXXmlReader(stream))
            {
                reader.Objects = objects;
                while (!reader.EOF)
                {
                    if (reader.IsStartElement())
                    {
                        target = reader.Name;
                        if (string.Compare("Objects", target, true) == 0)
                        {
                            //Skip.
                            reader.Read();
                        }
                        else if (target.StartsWith("GXDLMS"))
                        {
                            string str = target.Substring(6);
                            reader.Read();
                            type        = (ObjectType)Enum.Parse(typeof(ObjectType), str);
                            obj         = GXDLMSClient.CreateObject(type);
                            obj.Version = 0;
                        }
                        else if (string.Compare("Object", target, true) == 0)
                        {
                            int    r   = 0;
                            string str = reader.GetAttribute(0);
                            if (int.TryParse(str, out r))
                            {
                                type = (ObjectType)r;
                            }
                            else
                            {
                                type = (ObjectType)Enum.Parse(typeof(ObjectType), str);
                            }
                            reader.Read();
                            obj         = GXDLMSClient.CreateObject(type);
                            obj.Version = 0;
                            reader.Objects.Add(obj);
                        }
                        else if (string.Compare("SN", target, true) == 0)
                        {
                            obj.ShortName = (UInt16)reader.ReadElementContentAsInt("SN");
                        }
                        else if (string.Compare("LN", target, true) == 0)
                        {
                            obj.LogicalName = reader.ReadElementContentAsString("LN");
                            GXDLMSObject tmp = reader.Objects.FindByLN(obj.ObjectType, obj.LogicalName);
                            if (tmp == null)
                            {
                                reader.Objects.Add(obj);
                            }
                            else
                            {
                                obj = tmp;
                            }
                        }
                        else if (string.Compare("Description", target, true) == 0)
                        {
                            obj.Description = reader.ReadElementContentAsString("Description");
                        }
                        else if (string.Compare("Version", target, true) == 0)
                        {
                            obj.Version = (UInt16)reader.ReadElementContentAsInt("Version");
                        }
                        else if (string.Compare("Access", target, true) == 0)
                        {
                            int pos = 0;
                            foreach (byte it in reader.ReadElementContentAsString("Access"))
                            {
                                ++pos;
                                obj.SetAccess(pos, (AccessMode)(it - 0x30));
                            }
                        }
                        else if (string.Compare("MethodAccess", target, true) == 0)
                        {
                            int pos = 0;
                            foreach (byte it in reader.ReadElementContentAsString("MethodAccess"))
                            {
                                ++pos;
                                obj.SetMethodAccess(pos, (MethodAccessMode)(it - 0x30));
                            }
                        }
                        else
                        {
                            (obj as IGXDLMSBase).Load(reader);
                            obj = null;
                        }
                    }
                    else
                    {
                        reader.Read();
                    }
                }
                foreach (GXDLMSObject it in reader.Objects)
                {
                    (it as IGXDLMSBase).PostLoad(reader);
                }
            }
        }