void UpdateData(DataTable dt)
        {
            Standard standard = Standard.DLMS;

            if (target.Parent != null && target.Parent.Parent is GXDLMSClient)
            {
                standard = ((GXDLMSClient)target.Parent.Parent).Standard;
            }
            List <object> rows = GXDLMSCompactData.GetData(target.TemplateDescription, target.Buffer, standard == Standard.Italy);

            if (structures)
            {
                List <object[]> data = new List <object[]>();
                foreach (List <object> r in rows)
                {
                    List <object> row   = new List <object>();
                    int           index = 0;
                    foreach (var it in target.CaptureObjects)
                    {
                        //If COSEM object is selected.
                        //Only few meters are supporting this.
                        if (it.Value.AttributeIndex == 0 && r[index] is List <object> )
                        {
                            //Values must be update to the list because there might be Register Scaler
                            //and it expects that scaler is read before value is updated.
                            GXDLMSObject obj = GXDLMSClient.CreateObject(it.Key.ObjectType);
                            byte         i2  = 1;
                            Dictionary <byte, object> list = new Dictionary <byte, object>();
                            foreach (object v in (r[index] as object[]))
                            {
                                list.Add(i2, v);
                                ++i2;
                            }
                            //Update values first.
                            for (byte i = 0; i != (obj as IGXDLMSBase).GetAttributeCount(); ++i)
                            {
                                ValueEventArgs ve = new ValueEventArgs(obj, i, 0, null);
                                ve.Value = list[i];
                                (obj as IGXDLMSBase).SetValue(null, ve);
                            }
                            //Show values.
                            for (byte i = 0; i != (obj as IGXDLMSBase).GetAttributeCount(); ++i)
                            {
                                row.Add(GetValue(obj.GetValues()[i]));
                            }
                        }
                        else
                        {
                            row.Add(GetValue(r[index]));
                        }
                        ++index;
                    }
                    data.Add(row.ToArray());
                }
                for (int pos = dt.Rows.Count; pos < data.Count; ++pos)
                {
                    object[] row = data[pos];
                    dt.LoadDataRow(row, true);
                }
            }
            else
            {
                for (int pos = dt.Rows.Count; pos < rows.Count; ++pos)
                {
                    List <object> row = (List <object>)rows[pos];
                    if (row != null)
                    {
                        for (int col = 0; col != row.Count; ++col)
                        {
                            if (row[col] is byte[])
                            {
                                if (pos < target.CaptureObjects.Count && target.CaptureObjects[col].Key.GetUIDataType(target.CaptureObjects[col].Value.AttributeIndex) == DataType.DateTime)
                                {
                                    row[col] = GXDLMSClient.ChangeType(row[col] as byte[], DataType.DateTime);
                                }
                                else
                                {
                                    row[col] = GXDLMSTranslator.ToHex((byte[])row[col], true);
                                }
                            }
                            else if (row[col] is Object[])
                            {
                                row[col] = GXDLMSTranslator.ValueToXml(row[col]);
                            }
                            else if (row[col] is List <Object> )
                            {
                                row[col] = GXDLMSTranslator.ValueToXml(row[col]);
                            }
                            else if (col < target.CaptureObjects.Count)
                            {
                                GXDLMSAttributeSettings att = target.CaptureObjects[col].Key.Attributes.Find(target.CaptureObjects[col].Value.AttributeIndex);
                                if (att != null && att.Values != null)
                                {
                                    if (att.Type == DataType.BitString && row[col] is string)
                                    {
                                        string str = (string)row[col];
                                        if (str.Length != 0 && (str[0] == '0' || str[0] == '1'))
                                        {
                                            StringBuilder sb   = new StringBuilder();
                                            int           pos2 = 0;
                                            foreach (char it in str)
                                            {
                                                if (it == '1')
                                                {
                                                    if (sb.Length != 0)
                                                    {
                                                        sb.Append(',');
                                                    }
                                                    sb.Append(att.Values[pos2].UIValue);
                                                }
                                                ++pos2;
                                                if (pos2 == att.Values.Count)
                                                {
                                                    break;
                                                }
                                            }
                                            row[col] = sb.ToString();
                                        }
                                    }
                                    else
                                    {
                                        foreach (GXObisValueItem it in att.Values)
                                        {
                                            if (IsNumber(row[col]) && it.Value == Convert.ToInt32(row[col]))
                                            {
                                                row[col] = it.UIValue;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (dt.Columns.Count != 0)
                        {
                            try
                            {
                                dt.LoadDataRow(row.ToArray(), true);
                            }
                            catch (Exception)
                            {
                                //It's OK if this fails.
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
 void UpdateData(DataTable dt)
 {
     if (target.CaptureObjects.Count == 0)
     {
         return;
     }
     if (structures)
     {
         List <object[]> data = new List <object[]>();
         foreach (object[] r in target.Buffer)
         {
             List <object> row   = new List <object>();
             int           index = 0;
             foreach (var it in target.CaptureObjects)
             {
                 //If COSEM object is selected.
                 //Only few meters are supporting this.
                 if (it.Value.AttributeIndex == 0 && r[index] is List <object> )
                 {
                     //Values must be update to the list because there might be Register Scaler
                     //and it expects that scaler is read before value is updated.
                     GXDLMSObject obj = GXDLMSClient.CreateObject(it.Key.ObjectType);
                     byte         i2  = 1;
                     Dictionary <byte, object> list = new Dictionary <byte, object>();
                     foreach (object v in (r[index] as List <object>))
                     {
                         list.Add(i2, v);
                         ++i2;
                     }
                     //Update values first.
                     foreach (byte i in (obj as IGXDLMSBase).GetAttributeIndexToRead(true))
                     {
                         ValueEventArgs ve = new ValueEventArgs(obj, i, 0, null);
                         ve.Value = list[i];
                         (obj as IGXDLMSBase).SetValue(null, ve);
                     }
                     //Show values.
                     for (byte i = 0; i != (obj as IGXDLMSBase).GetAttributeCount(); ++i)
                     {
                         row.Add(GetValue(obj.GetValues()[i]));
                     }
                 }
                 else
                 {
                     row.Add(GetValue(r[index]));
                 }
                 ++index;
             }
             data.Add(row.ToArray());
         }
         for (int pos = dt.Rows.Count; pos < data.Count; ++pos)
         {
             object[] row = data[pos];
             dt.LoadDataRow(row, true);
         }
     }
     else
     {
         for (int pos = dt.Rows.Count; pos < target.Buffer.Count; ++pos)
         {
             object[] row = target.Buffer[pos];
             if (row != null)
             {
                 for (int col = 0; col != row.Length; ++col)
                 {
                     if (row[col] is GXDateTime)
                     {
                         if (GXDlmsUi.UseMeterTimeZone)
                         {
                             row[col] = (row[col] as GXDateTime).ToFormatMeterString();
                         }
                         else
                         {
                             row[col] = (row[col] as GXDateTime).ToFormatString();
                         }
                     }
                     else if (row[col] is byte[])
                     {
                         row[col] = GXDLMSTranslator.ToHex((byte[])row[col], true);
                     }
                     else if (row[col] is Object[])
                     {
                         row[col] = GXDLMSTranslator.ValueToXml(row[col]);
                     }
                     else if (row[col] is GXStructure || row[col] is GXArray)
                     {
                         if (target.CaptureObjects[col].Key is GXDLMSRegister && target.CaptureObjects[col].Value.AttributeIndex == 2)
                         {
                             GXDLMSRegister obj = new GXDLMSRegister();
                             ValueEventArgs ve  = new ValueEventArgs(obj, target.CaptureObjects[col].Value.AttributeIndex, 0, null);
                             ve.Value = row[col];
                             (obj as IGXDLMSBase).SetValue(null, ve);
                             row[col] = "{" + obj.Scaler + ", " + obj.Unit + "}";
                         }
                         else
                         {
                             StringBuilder sb = new StringBuilder();
                             GetArrayAsString(sb, row[col]);
                             row[col] = sb.ToString();
                         }
                     }
                     else
                     {
                         GXDLMSAttributeSettings att = target.CaptureObjects[col].Key.Attributes.Find(target.CaptureObjects[col].Value.AttributeIndex);
                         if (att != null && att.Values != null)
                         {
                             if (att.Type == DataType.BitString && row[col] is string)
                             {
                                 string str = (string)row[col];
                                 if (str.Length != 0 && (str[0] == '0' || str[0] == '1'))
                                 {
                                     StringBuilder sb   = new StringBuilder();
                                     int           pos2 = 0;
                                     foreach (char it in str)
                                     {
                                         if (it == '1')
                                         {
                                             if (sb.Length != 0)
                                             {
                                                 sb.Append(',');
                                             }
                                             sb.Append(att.Values[pos2].UIValue);
                                         }
                                         ++pos2;
                                         if (pos2 == att.Values.Count)
                                         {
                                             break;
                                         }
                                     }
                                     row[col] = sb.ToString();
                                 }
                             }
                             else
                             {
                                 foreach (GXObisValueItem it in att.Values)
                                 {
                                     if (IsNumber(row[col]) && it.Value == Convert.ToInt32(row[col]))
                                     {
                                         row[col] = it.UIValue;
                                         break;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 dt.LoadDataRow(row, true);
             }
         }
     }
 }