Exemplo n.º 1
0
 static public object ConvertFromDLMS(object data, DataType from, DataType type, bool arrayAsString)
 {
     if (type == DataType.Array)
     {
         return(data);
     }
     if (type == DataType.None)
     {
         if (arrayAsString && data != null && data.GetType().IsArray)
         {
             data = GXHelpers.GetArrayAsString(data);
         }
         return(data);
     }
     //Show Octet string...
     if (from == DataType.OctetString && type == DataType.OctetString)
     {
         if (data is byte[])
         {
             string str = "";
             byte[] arr = (byte[])data;
             if (arr.Length == 0)
             {
                 data = string.Empty;
             }
             else
             {
                 foreach (byte it in arr)
                 {
                     str += it.ToString() + ".";
                 }
                 data = str.Substring(0, str.Length - 1);
             }
         }
     }
     //Convert DLMS octect string to Windows string.
     else if (from == DataType.OctetString && type == DataType.String)
     {
         if (data is string)
         {
             return(data);
         }
         else if (data is byte[])
         {
             byte[] arr = (byte[])data;
             data = System.Text.Encoding.ASCII.GetString(arr);
         }
     }
     //Convert DLMS date time to Windows Time.
     else if (type == DataType.DateTime)
     {
         if (data is byte[])
         {
             if (((byte[])data).Length == 5)
             {
                 return(GXDLMSClient.ChangeType((byte[])data, DataType.Date));
             }
             return(GXDLMSClient.ChangeType((byte[])data, DataType.DateTime));
         }
         return(data);
     }
     //Convert DLMS date time to Windows Date.
     else if (type == DataType.Date)
     {
         if (data is DateTime)
         {
             return(data);
         }
         if (data is string)
         {
             return(data);
         }
         if (!data.GetType().IsArray || ((Array)data).Length < 5)
         {
             throw new Exception("DateTime conversion failed. Invalid DLMS format.");
         }
         return(GXDLMSClient.ChangeType((byte[])data, DataType.Date));
     }
     //Convert DLMS date time to Windows Time.
     else if (type == DataType.Time)
     {
         if (data is byte[])
         {
             return(GXDLMSClient.ChangeType((byte[])data, type));
         }
         return(data);
     }
     else if (data is byte[])
     {
         if (type == DataType.String)
         {
             data = System.Text.Encoding.ASCII.GetString((byte[])data);
         }
         else
         {
             data = ToHexString(data);
         }
     }
     else if (data is Array)
     {
         data = ArrayToString(data);
     }
     return(data);
 }