Beispiel #1
0
 public override string ToString()
 {
     if (HasGliFormat)
     {
         return(GliFormat.ToString());
     }
     if (IsCompressed)
     {
         return(InternalFormat.ToString().ToUpper());
     }
     return(ExternalFormat.ToString().ToUpper() + "_" + Type.ToString().ToUpper());
 }
Beispiel #2
0
        } // ctor

        public Thing(string csvString, char separator = ',')
        {
            m_stringSeparatorChar = separator;
            string[] attributes = csvString.Split(m_stringSeparatorChar);

            ExternalFormat ef = ExternalFormat.Unknown;

            // Read format specifier:
            switch (attributes[0])
            {
            case "T":
                ef = ExternalFormat.Tiny;           // T,TypeAsNumber,Id
                break;

            case "S":
                ef = ExternalFormat.Standard;       // S,TypeAsString,Id,CreatedWhen
                break;

            case "D":
                ef = ExternalFormat.Display;        // D,TypeAsNumber,TypeAsString,Id,CreatedWhen
                break;

            default:
                DeviceServerApp.Logger.Error($"Invalid format specifier <{attributes[ 0 ]}>");
                throw new FormatException("Invalid CSV format");
            } // determine format.

            try
            {
                // Deserialize type of the thing:
                if (ef == ExternalFormat.Tiny || ef == ExternalFormat.Display)
                {
                    // Type has integer format, e.g. "9000":
                    Type = (ThingType)Convert.ToUInt64(attributes[1]);
                }
                else
                if (ef == ExternalFormat.Standard)
                {
                    // Type has string format, e.g. "Digger":
                    Type = (ThingType)Enum.Parse(typeof(ThingType), attributes[1]);
                }

                TypeAsString = Type.ToString();

                // Deserialize the GUID of the thing:
                if (ef == ExternalFormat.Tiny || ef == ExternalFormat.Standard)
                {
                    Id = Guid.Parse(attributes[2]);
                }
                else
                if (ef == ExternalFormat.Display)
                {
                    Id = Guid.Parse(attributes[3]);
                }

                // Deserialize the birthday, if present:
                if (ef == ExternalFormat.Standard)
                {
                    CreatedWhen = DateTime.Parse(attributes[3]);
                }
                else
                if (ef == ExternalFormat.Display)
                {
                    CreatedWhen = DateTime.Parse(attributes[4]);
                }
            }
            catch (Exception ex)
            {
                DeviceServerApp.Logger.Error(ex.Message);
                throw;
            }
        } // ctor