context: //
  // --------------------------------------------------------------------------
  //  Gurux Ltd
  //
  //
  //
  // Filename:        $HeadURL$
  //
  // Version:         $Revision$,
  //                  $Date$
  //                  $Author$
  //
  // Copyright (c) Gurux Ltd
  //
  //---------------------------------------------------------------------------
  //
  //  DESCRIPTION
  //
  // This file is a part of Gurux Device Framework.
  //
  // Gurux Device Framework is Open Source software; you can redistribute it
  // and/or modify it under the terms of the GNU General Public License
  // as published by the Free Software Foundation; version 2 of the License.
  // Gurux Device Framework is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  // See the GNU General Public License for more details.
  //
  // More information of Gurux products: https://www.gurux.org
  //
  // This code is licensed under the GNU General Public License v2.
  // Full text may be retrieved at http://www.gnu.org/licenses/gpl-2.0.txt
  //---------------------------------------------------------------------------
  
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Xml.Serialization;
  using Gurux.DLMS.Internal;
  using Gurux.DLMS.Enums;
  
  namespace Gurux.DLMS.Objects
  {
      /// <summary>
      /// Online help:
      /// https://www.gurux.fi/Gurux.DLMS.Objects.GXDLMSSapAssignment
      /// </summary>
      public class GXDLMSSapAssignment : GXDLMSObject, IGXDLMSBase
      {
          /// <summary>
          /// Constructor.
          /// </summary>
          public GXDLMSSapAssignment()
          : base(ObjectType.SapAssignment, "0.0.41.0.0.255", 0)
          {
              SapAssignmentList = new List<KeyValuePair<UInt16, string>>();
          }
  
          /// <summary>
          /// Constructor.
          /// </summary>
          /// <param name="ln">Logical Name of the object.</param>
          public GXDLMSSapAssignment(string ln)
          : base(ObjectType.SapAssignment, ln, 0)
          {
              SapAssignmentList = new List<KeyValuePair<UInt16, string>>();
          }
  
          /// <summary>
          /// Constructor.
          /// </summary>
          /// <param name="ln">Logical Name of the object.</param>
          /// <param name="sn">Short Name of the object.</param>
          public GXDLMSSapAssignment(string ln, ushort sn)
          : base(ObjectType.SapAssignment, ln, sn)
          {
              SapAssignmentList = new List<KeyValuePair<UInt16, string>>();
          }
  
          [XmlIgnore()]
          public List<KeyValuePair<UInt16, string>> SapAssignmentList
          {
              get;
              set;
          }
  
          /// <inheritdoc cref="GXDLMSObject.GetValues"/>
          public override object[] GetValues()
          {
              return new object[] { LogicalName, SapAssignmentList };
          }
  
          /// <summary>
          /// Add new SAP item.
          /// </summary>
          /// <param name="client">DLMS client.</param>
          /// <param name="id">SAP ID.</param>
          /// <param name="name">Logical device name.</param>
          /// <returns>Generated bytes sent to the meter.</returns>
          public byte[][] AddSap(GXDLMSClient client, UInt16 id, string name)
          {
              GXByteBuffer data = new GXByteBuffer();
              data.SetUInt8((byte)DataType.Structure);
              //Add structure size.
              data.SetUInt8(2);
              GXCommon.SetData(null, data, DataType.UInt16, id);
              GXCommon.SetData(null, data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(name));
              return client.Method(this, 1, data.Array(), DataType.Structure);
          }
  
          /// <summary>
          /// Remove SAP item.
          /// </summary>
          /// <param name="client">DLMS client.</param>
          /// <param name="name">Logical device name.</param>
          /// <returns>Generated bytes sent to the meter.</returns>
          public byte[][] RemoveSap(GXDLMSClient client, string name)
          {
              GXByteBuffer data = new GXByteBuffer();
              data.SetUInt8((byte)DataType.Structure);
              //Add structure size.
              data.SetUInt8(2);
              GXCommon.SetData(null, data, DataType.UInt16, 0);
              GXCommon.SetData(null, data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(name));
              return client.Method(this, 1, data.Array(), DataType.Structure);
          }
  
          #region IGXDLMSBase Members
  
          int[] IGXDLMSBase.GetAttributeIndexToRead(bool all)
          {
              List<int> attributes = new List<int>();
              //LN is static and read only once.
              if (all || string.IsNullOrEmpty(LogicalName))
              {
                  attributes.Add(1);
              }
              //SapAssignmentList
              if (all || !base.IsRead(2))
              {
                  attributes.Add(2);
              }
              return attributes.ToArray();
          }
  
          /// <inheritdoc cref="IGXDLMSBase.GetNames"/>
          string[] IGXDLMSBase.GetNames()
          {
              return new string[] { Internal.GXCommon.GetLogicalNameString(), "Sap Assignment List" };
          }
  
          /// <inheritdoc cref="IGXDLMSBase.GetMethodNames"/>
          string[] IGXDLMSBase.GetMethodNames()
          {
              return new string[] { "Connect logical device" };
          }
  
          int IGXDLMSBase.GetMaxSupportedVersion()
          {
              return 0;
          }
  
          int IGXDLMSBase.GetAttributeCount()
          {
              return 2;
          }
  
          int IGXDLMSBase.GetMethodCount()
          {
              return 1;
          }
  
          /// <inheritdoc cref="IGXDLMSBase.GetDataType"/>
          public override DataType GetDataType(int index)
          {
              if (index == 1)
              {
                  return DataType.OctetString;
              }
              if (index == 2)
              {
                  return DataType.Array;
              }
              throw new ArgumentException("GetDataType failed. Invalid attribute index.");
          }
  
          object IGXDLMSBase.GetValue(GXDLMSSettings settings, ValueEventArgs e)
          {
              if (e.Index == 1)
              {
                  return GXCommon.LogicalNameToBytes(LogicalName);
              }
              if (e.Index == 2)
              {
                  int cnt = 0;
                  if (SapAssignmentList != null)
                  {
                      cnt = SapAssignmentList.Count;
                  }
                  GXByteBuffer data = new GXByteBuffer();
                  data.SetUInt8((byte)DataType.Array);
                  //Add count
                  GXCommon.SetObjectCount(cnt, data);
                  if (cnt != 0)
                  {
                      foreach (var it in SapAssignmentList)
                      {
                          data.SetUInt8((byte)DataType.Structure);
                          data.SetUInt8((byte)2); //Count
                          GXCommon.SetData(settings, data, DataType.UInt16, it.Key);
                          GXCommon.SetData(settings, data, DataType.OctetString, ASCIIEncoding.ASCII.GetBytes(it.Value));
                      }
                  }
                  return data.Array();
              }
              e.Error = ErrorCode.ReadWriteDenied;
              return null;
          }
  
          void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
          {
              if (e.Index == 1)
              {
                  LogicalName = GXCommon.ToLogicalName(e.Value);
              }
              else if (e.Index == 2)
              {
                  SapAssignmentList.Clear();
                  if (e.Value != null)
                  {
                      foreach (object tmp in (IEnumerable<object>)e.Value)
                      {
                          List<object> item;
                          if (tmp is List<object>)
                          {
  
gt:                             item = (List<object>)tmp;