From 14e4bd850fa6730eecd7d15bd7044f364b41c986 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Thu, 12 Nov 2020 20:38:57 +0200 Subject: Working on data store view. --- .../Tango.DataStore/DataStoreHelper.cs | 72 +++++++++++++++++++--- 1 file changed, 63 insertions(+), 9 deletions(-) (limited to 'Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs') 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(); } } + + /// + /// Returns a byte array string representation in hex format. + /// + /// The data. + /// + public static String GetByteArrayHexString(byte[] data) + { + StringBuilder hex = new StringBuilder(); + foreach (byte b in data) + { + hex.Append(b.ToString("X2") + " "); + } + return hex.ToString(); + } + + /// + /// Parses a data store value from a string. + /// + /// The type. + /// The string. + /// Type of the proto message (if type is Proto). + /// + /// No PMR message type specified. + /// The specified data store type is not supported. + 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 bytes = new List(); + 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."); + } } } -- cgit v1.3.1