diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-11-12 20:38:57 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-11-12 20:38:57 +0200 |
| commit | 14e4bd850fa6730eecd7d15bd7044f364b41c986 (patch) | |
| tree | 4e1945a18def6d1c91f239b2b4dbf6e11c0d51db /Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs | |
| parent | 775f0015321f91aa5be3aca079e289e89d4719f5 (diff) | |
| download | Tango-14e4bd850fa6730eecd7d15bd7044f364b41c986.tar.gz Tango-14e4bd850fa6730eecd7d15bd7044f364b41c986.zip | |
Working on data store view.
Diffstat (limited to 'Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs | 72 |
1 files changed, 63 insertions, 9 deletions
diff --git a/Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs b/Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs index 7167648b4..0ca8e484e 100644 --- a/Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs +++ b/Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs @@ -84,23 +84,77 @@ namespace Tango.DataStore { if (item.Type == DataType.Bytes) { - byte[] bytes = (byte[])item.Value; - - StringBuilder hex = new StringBuilder(); - foreach (byte b in bytes) - { - hex.AppendFormat("{0:x2} ", b); - } - return hex.ToString(); + return GetByteArrayHexString((byte[])item.Value); } else if (item.Type == DataType.Proto) { - return (item.Value as DataStoreProtoObject).Message.ToString(); + return (item.Value as DataStoreProtoObject).Message.ToJsonString(); } else { return item.Value.ToStringSafe(); } } + + /// <summary> + /// Returns a byte array string representation in hex format. + /// </summary> + /// <param name="data">The data.</param> + /// <returns></returns> + public static String GetByteArrayHexString(byte[] data) + { + StringBuilder hex = new StringBuilder(); + foreach (byte b in data) + { + hex.Append(b.ToString("X2") + " "); + } + return hex.ToString(); + } + + /// <summary> + /// Parses a data store value from a string. + /// </summary> + /// <param name="type">The type.</param> + /// <param name="text">The string.</param> + /// <param name="protoMessageType">Type of the proto message (if type is Proto).</param> + /// <returns></returns> + /// <exception cref="ArgumentNullException">No PMR message type specified.</exception> + /// <exception cref="NotSupportedException">The specified data store type is not supported.</exception> + public static Object ParseDataStoreValue(DataType type, String text, MessageType? protoMessageType = null) + { + switch (type) + { + case DataType.String: + return text; + case DataType.Int32: + return int.Parse(text); + case DataType.Float: + return float.Parse(text); + case DataType.Double: + return double.Parse(text); + case DataType.Boolean: + return bool.Parse(text); + case DataType.Proto: + if (protoMessageType == null) throw new ArgumentNullException("No PMR message type specified."); + var messageType = MessageFactory.GetPMRTypeFromMessageType(protoMessageType.Value); + var instance = Activator.CreateInstance(messageType) as IMessage; + instance = instance.GetParser().ParseJson(text); + return DataStoreProtoObject.FromMessage(instance); + case DataType.Bytes: + string[] hexValuesSplit = text.Split(' '); + List<byte> bytes = new List<byte>(); + foreach (string hex in hexValuesSplit) + { + if (hex.IsNotNullOrEmpty()) + { + byte b = (byte)Convert.ToInt32(hex.Trim(), 16); + bytes.Add(b); + } + } + return bytes.ToArray(); + } + + throw new NotSupportedException("The specified data store type is not supported."); + } } } |
