private static void GetCaptureObjects(GXDLMSSettings settings, List <GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> > list, object[] array)
 {
     foreach (object it in array)
     {
         object[] tmp = it as object[];
         if (tmp.Length != 4)
         {
             throw new GXDLMSException("Invalid structure format.");
         }
         ObjectType   type           = (ObjectType)Convert.ToInt16(tmp[0]);
         string       ln             = GXCommon.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);
         }
         list.Add(new GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(obj, new GXDLMSCaptureObject(attributeIndex, dataIndex)));
     }
 }
        private static void SetCaptureObjects(GXDLMSSettings settings, List <GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> > list, object[] array)
        {
            GXDLMSConverter c = null;

            list.Clear();
            try
            {
                if (array != null)
                {
                    foreach (object it in array)
                    {
                        object[] tmp = it as object[];
                        if (tmp.Length != 4)
                        {
                            throw new GXDLMSException("Invalid structure format.");
                        }
                        int v = Convert.ToInt16(tmp[0]);
                        if (Enum.GetName(typeof(ObjectType), v) == null)
                        {
                            list.Clear();
                            return;
                        }
                        ObjectType type           = (ObjectType)v;
                        string     ln             = GXCommon.ToLogicalName((byte[])tmp[1]);
                        int        attributeIndex = Convert.ToInt16(tmp[2]);
                        //If profile generic selective access is used.
                        if (attributeIndex < 0)
                        {
                            attributeIndex = 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);
                            if (c == null)
                            {
                                c = new GXDLMSConverter();
                            }
                            c.UpdateOBISCodeInformation(obj);
                        }
                        list.Add(new GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(obj, new GXDLMSCaptureObject(attributeIndex, dataIndex)));
                    }
                }
            }
            catch (Exception ex)
            {
                list.Clear();
                throw ex;
            }
        }
        private static void SetCaptureObjects(GXDLMSProfileGeneric parent, GXDLMSSettings settings, List <GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject> > list, IEnumerable <object> array)
        {
            GXDLMSConverter c = null;

            try
            {
                foreach (object it in array)
                {
                    List <object> tmp;
                    if (it is List <object> )
                    {
                        tmp = (List <object>)it;
                    }
                    else
                    {
                        tmp = new List <object>((object[])it);
                    }
                    if (tmp.Count != 4)
                    {
                        throw new GXDLMSException("Invalid structure format.");
                    }
                    int v = Convert.ToInt16(tmp[0]);
                    if (Enum.GetName(typeof(ObjectType), v) == null)
                    {
                        list.Clear();
                        return;
                    }
                    ObjectType   type           = (ObjectType)v;
                    string       ln             = GXCommon.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);
                    }
                    //Create a new instance to avoid circular references.
                    if (obj == null || obj == parent)
                    {
                        obj = GXDLMSClient.CreateDLMSObject((int)type, null, 0, ln, 0);
                        if (c == null)
                        {
                            c = new GXDLMSConverter(settings == null ? Standard.DLMS : settings.Standard);
                        }
                        c.UpdateOBISCodeInformation(obj);
                    }
                    list.Add(new GXKeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(obj, new GXDLMSCaptureObject(attributeIndex, dataIndex)));
                }
            }
            catch (Exception)
            {
                list.Clear();
            }
        }
Esempio n. 4
0
        ParsePush(Object[] data)
        {
            int          index;
            GXDLMSObject obj;
            object       value;
            DataType     dt;
            List <KeyValuePair <GXDLMSObject, int> > items = new List <KeyValuePair <GXDLMSObject, int> >();

            if (data != null)
            {
                GXDLMSConverter        c       = new GXDLMSConverter();
                GXDLMSObjectCollection objects = new GXDLMSObjectCollection();
                foreach (Object it in (Object[])data[0])
                {
                    Object[] tmp     = (Object[])it;
                    int      classID = ((UInt16)(tmp[0])) & 0xFFFF;
                    if (classID > 0)
                    {
                        GXDLMSObject comp;
                        comp = this.Objects.FindByLN((ObjectType)classID, GXCommon.ToLogicalName(tmp[1] as byte[]));
                        if (comp == null)
                        {
                            comp = GXDLMSClient.CreateDLMSObject(classID, 0, 0, tmp[1], null);
                            c.UpdateOBISCodeInformation(comp);
                            objects.Add(comp);
                        }
                        if ((comp is IGXDLMSBase))
                        {
                            items.Add(new KeyValuePair <GXDLMSObject, int>(comp, (sbyte)tmp[2]));
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine(string.Format("Unknown object : {0} {1}",
                                                                             classID, GXCommon.ToLogicalName((byte[])tmp[1])));
                        }
                    }
                }
                for (int pos = 0; pos < data.Length; ++pos)
                {
                    obj   = items[pos].Key as GXDLMSObject;
                    value = data[pos];
                    index = items[pos].Value;
                    if (value is byte[] && (dt = obj.GetUIDataType(index)) != DataType.None)
                    {
                        value = GXDLMSClient.ChangeType(value as byte[], dt, Settings.UseUtc2NormalTime);
                    }
                    ValueEventArgs e = new ValueEventArgs(Settings, obj, index, 0, null);
                    e.Value = value;
                    (obj as IGXDLMSBase).SetValue(Settings, e);
                }
            }
            return(items);
        }
Esempio n. 5
0
        /// <summary>
        /// Returns collection of push objects.
        /// </summary>
        /// <param name="data">Received data.</param>
        /// <returns>Array of objects and called indexes.</returns>
        public List <KeyValuePair <GXDLMSObject, int> > ParsePushObjects(GXByteBuffer data)
        {
            int          index;
            GXDLMSObject obj;
            object       value;
            DataType     dt;
            GXReplyData  reply = new GXReplyData();

            reply.Data = data;
            List <KeyValuePair <GXDLMSObject, int> > items = new List <KeyValuePair <GXDLMSObject, int> >();

            GXDLMS.GetValueFromData(Settings, reply);
            Object[] list = (Object[])reply.Value;
            GXDLMSObjectCollection objects = new GXDLMSObjectCollection();

            foreach (Object it in (Object[])list[0])
            {
                Object[] tmp     = (Object[])it;
                int      classID = ((UInt16)(tmp[0])) & 0xFFFF;
                if (classID > 0)
                {
                    GXDLMSObject comp;
                    comp = this.Objects.FindByLN((ObjectType)classID, GXDLMSObject.ToLogicalName(tmp[1] as byte[]));
                    if (comp == null)
                    {
                        comp = GXDLMSClient.CreateDLMSObject(classID, 0, 0, tmp[1], null);
                        objects.Add(comp);
                    }
                    if ((comp is IGXDLMSBase))
                    {
                        items.Add(new KeyValuePair <GXDLMSObject, int>(comp, (sbyte)tmp[2]));
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine(string.Format("Unknown object : {0} {1}",
                                                                         classID, GXDLMSObject.ToLogicalName((byte[])tmp[1])));
                    }
                }
            }
            for (int pos = 0; pos < list.Length; ++pos)
            {
                obj   = items[pos].Key as GXDLMSObject;
                value = list[pos];
                index = items[pos].Value;
                if (value is byte[] && (dt = obj.GetUIDataType(index)) != DataType.None)
                {
                    value = GXDLMSClient.ChangeType(value as byte[], dt);
                }
                (obj as IGXDLMSBase).SetValue(Settings, index, value);
            }
            return(items);
        }
Esempio n. 6
0
        /// <summary>
        /// Get logical names from client request.
        /// </summary>
        /// <param name="nodes">XML nodes.</param>
        /// <returns>Collection of logical names.</returns>
        private static void GetLN(GXDLMSObjectCollection objects, List <ValueEventArgs> targets, XmlNodeList nodes)
        {
            List <ValueEventArgs> list = new List <ValueEventArgs>();

            foreach (XmlNode node in nodes)
            {
                ObjectType ot;
                string     classId, instanceId;
                int        attributeId;
                if (node.Name == "cosem-attribute-descriptor")
                {
                    classId     = node.ChildNodes[0].ChildNodes[0].InnerText;
                    instanceId  = node.ChildNodes[1].ChildNodes[0].InnerText;
                    instanceId  = GXDLMSObject.ToLogicalName(GXCommon.HexToBytes(instanceId));
                    attributeId = int.Parse(node.ChildNodes[2].ChildNodes[0].InnerText);
                    ot          = (ObjectType)int.Parse(classId);
                    GXDLMSObject t = objects.FindByLN(ot, instanceId);
                    if (t == null)
                    {
                        t = GXDLMSClient.CreateDLMSObject((int)ot, 0, 0, instanceId, null);
                    }
                    ValueEventArgs ve = new ValueEventArgs(t, attributeId, 0, null);
                    targets.Add(ve);
                    System.Diagnostics.Debug.WriteLine(ot + " " + instanceId);
                }
                else if ("AttributeDescriptorList".Equals(node.Name))
                {
                    foreach (XmlNode it in node.ChildNodes[1].ChildNodes)
                    {
                        classId     = it.ChildNodes[0].InnerText;
                        instanceId  = it.ChildNodes[1].InnerText;
                        instanceId  = GXDLMSObject.ToLogicalName(GXCommon.HexToBytes(instanceId));
                        attributeId = int.Parse(it.ChildNodes[2].InnerText);
                        ot          = (ObjectType)int.Parse(classId);
                        ValueEventArgs ve = new ValueEventArgs(objects.FindByLN(ot, instanceId), attributeId, 0, null);
                        targets.Add(ve);
                        System.Diagnostics.Debug.WriteLine(ot + " " + instanceId);
                    }
                }
            }
        }
 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.");
     }
 }
        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;
            }
        }