From 88407c1927faeabcec2b9c2699d657eb5723bc81 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Fri, 6 Aug 2021 13:03:05 +0300 Subject: Refactored data store to use DataStoreMessageType. --- .../Tango.PMR/DataStore/DataStoreItem.cs | 23 ++++++----- .../Tango.PMR/DataStore/DataStoreMessageType.cs | 47 ++++++++++++++++++++++ .../Visual_Studio/Tango.PMR/ExtensionMethods.cs | 14 +++++++ Software/Visual_Studio/Tango.PMR/MessageFactory.cs | 43 ++++++++++++++++++++ Software/Visual_Studio/Tango.PMR/Tango.PMR.csproj | 3 +- 5 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 Software/Visual_Studio/Tango.PMR/DataStore/DataStoreMessageType.cs (limited to 'Software/Visual_Studio/Tango.PMR') diff --git a/Software/Visual_Studio/Tango.PMR/DataStore/DataStoreItem.cs b/Software/Visual_Studio/Tango.PMR/DataStore/DataStoreItem.cs index f560237ae..c6b4e6776 100644 --- a/Software/Visual_Studio/Tango.PMR/DataStore/DataStoreItem.cs +++ b/Software/Visual_Studio/Tango.PMR/DataStore/DataStoreItem.cs @@ -23,15 +23,16 @@ namespace Tango.PMR.DataStore { byte[] descriptorData = global::System.Convert.FromBase64String( string.Concat( "ChNEYXRhU3RvcmVJdGVtLnByb3RvEhNUYW5nby5QTVIuRGF0YVN0b3JlGg5E", - "YXRhVHlwZS5wcm90bxoRTWVzc2FnZVR5cGUucHJvdG8i7gEKDURhdGFTdG9y", - "ZUl0ZW0SLwoIRGF0YVR5cGUYASABKA4yHS5UYW5nby5QTVIuRGF0YVN0b3Jl", - "LkRhdGFUeXBlEhIKCkludDMyVmFsdWUYAiABKAUSEgoKRmxvYXRWYWx1ZRgD", - "IAEoAhITCgtEb3VibGVWYWx1ZRgEIAEoARIUCgxCb29sZWFuVmFsdWUYBSAB", - "KAgSEwoLU3RyaW5nVmFsdWUYBiABKAkSEgoKQnl0ZXNWYWx1ZRgHIAEoDBIw", - "CglQcm90b1R5cGUYFCABKA4yHS5UYW5nby5QTVIuQ29tbW9uLk1lc3NhZ2VU", - "eXBlQh8KHWNvbS50d2luZS50YW5nby5wbXIuZGF0YXN0b3JlYgZwcm90bzM=")); + "YXRhVHlwZS5wcm90bxoaRGF0YVN0b3JlTWVzc2FnZVR5cGUucHJvdG8i+gEK", + "DURhdGFTdG9yZUl0ZW0SLwoIRGF0YVR5cGUYASABKA4yHS5UYW5nby5QTVIu", + "RGF0YVN0b3JlLkRhdGFUeXBlEhIKCkludDMyVmFsdWUYAiABKAUSEgoKRmxv", + "YXRWYWx1ZRgDIAEoAhITCgtEb3VibGVWYWx1ZRgEIAEoARIUCgxCb29sZWFu", + "VmFsdWUYBSABKAgSEwoLU3RyaW5nVmFsdWUYBiABKAkSEgoKQnl0ZXNWYWx1", + "ZRgHIAEoDBI8CglQcm90b1R5cGUYFCABKA4yKS5UYW5nby5QTVIuRGF0YVN0", + "b3JlLkRhdGFTdG9yZU1lc3NhZ2VUeXBlQh8KHWNvbS50d2luZS50YW5nby5w", + "bXIuZGF0YXN0b3JlYgZwcm90bzM=")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, - new pbr::FileDescriptor[] { global::Tango.PMR.DataStore.DataTypeReflection.Descriptor, global::Tango.PMR.Common.MessageTypeReflection.Descriptor, }, + new pbr::FileDescriptor[] { global::Tango.PMR.DataStore.DataTypeReflection.Descriptor, global::Tango.PMR.DataStore.DataStoreMessageTypeReflection.Descriptor, }, new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(typeof(global::Tango.PMR.DataStore.DataStoreItem), global::Tango.PMR.DataStore.DataStoreItem.Parser, new[]{ "DataType", "Int32Value", "FloatValue", "DoubleValue", "BooleanValue", "StringValue", "BytesValue", "ProtoType" }, null, null, null) })); @@ -158,12 +159,12 @@ namespace Tango.PMR.DataStore { /// Field number for the "ProtoType" field. public const int ProtoTypeFieldNumber = 20; - private global::Tango.PMR.Common.MessageType protoType_ = 0; + private global::Tango.PMR.DataStore.DataStoreMessageType protoType_ = 0; /// ///Use only when DataType = Proto. /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public global::Tango.PMR.Common.MessageType ProtoType { + public global::Tango.PMR.DataStore.DataStoreMessageType ProtoType { get { return protoType_; } set { protoType_ = value; @@ -347,7 +348,7 @@ namespace Tango.PMR.DataStore { break; } case 160: { - protoType_ = (global::Tango.PMR.Common.MessageType) input.ReadEnum(); + protoType_ = (global::Tango.PMR.DataStore.DataStoreMessageType) input.ReadEnum(); break; } } diff --git a/Software/Visual_Studio/Tango.PMR/DataStore/DataStoreMessageType.cs b/Software/Visual_Studio/Tango.PMR/DataStore/DataStoreMessageType.cs new file mode 100644 index 000000000..9c9f70ef8 --- /dev/null +++ b/Software/Visual_Studio/Tango.PMR/DataStore/DataStoreMessageType.cs @@ -0,0 +1,47 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: DataStoreMessageType.proto +#pragma warning disable 1591, 0612, 3021 +#region Designer generated code + +using pb = global::Google.Protobuf; +using pbc = global::Google.Protobuf.Collections; +using pbr = global::Google.Protobuf.Reflection; +using scg = global::System.Collections.Generic; +namespace Tango.PMR.DataStore { + + /// Holder for reflection information generated from DataStoreMessageType.proto + public static partial class DataStoreMessageTypeReflection { + + #region Descriptor + /// File descriptor for DataStoreMessageType.proto + public static pbr::FileDescriptor Descriptor { + get { return descriptor; } + } + private static pbr::FileDescriptor descriptor; + + static DataStoreMessageTypeReflection() { + byte[] descriptorData = global::System.Convert.FromBase64String( + string.Concat( + "ChpEYXRhU3RvcmVNZXNzYWdlVHlwZS5wcm90bxITVGFuZ28uUE1SLkRhdGFT", + "dG9yZSpSChREYXRhU3RvcmVNZXNzYWdlVHlwZRINCglVbmRlZmluZWQQABIU", + "ChBDYWxjdWxhdGVSZXF1ZXN0EAESFQoRQ2FsY3VsYXRlUmVzcG9uc2UQAkIf", + "Ch1jb20udHdpbmUudGFuZ28ucG1yLmRhdGFzdG9yZWIGcHJvdG8z")); + descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, + new pbr::FileDescriptor[] { }, + new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Tango.PMR.DataStore.DataStoreMessageType), }, null)); + } + #endregion + + } + #region Enums + public enum DataStoreMessageType { + [pbr::OriginalName("Undefined")] Undefined = 0, + [pbr::OriginalName("CalculateRequest")] CalculateRequest = 1, + [pbr::OriginalName("CalculateResponse")] CalculateResponse = 2, + } + + #endregion + +} + +#endregion Designer generated code diff --git a/Software/Visual_Studio/Tango.PMR/ExtensionMethods.cs b/Software/Visual_Studio/Tango.PMR/ExtensionMethods.cs index d5475ce7b..be0912220 100644 --- a/Software/Visual_Studio/Tango.PMR/ExtensionMethods.cs +++ b/Software/Visual_Studio/Tango.PMR/ExtensionMethods.cs @@ -8,6 +8,7 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using Tango.PMR.Common; +using Tango.PMR.DataStore; using Tango.PMR.Integration; /// @@ -33,6 +34,19 @@ public static class ExtensionMethods return value.ToString(); } + public static String ToOriginalName(this DataStoreMessageType value) + { + FieldInfo fi = value.GetType().GetField(value.ToString()); + + OriginalNameAttribute[] attributes = (OriginalNameAttribute[])fi.GetCustomAttributes(typeof(OriginalNameAttribute), false); + + if (attributes != null && + attributes.Length > 0) + return attributes[0].Name; + else + return value.ToString(); + } + /// /// Gets the message parser. /// diff --git a/Software/Visual_Studio/Tango.PMR/MessageFactory.cs b/Software/Visual_Studio/Tango.PMR/MessageFactory.cs index d23970e88..8aa5283e8 100644 --- a/Software/Visual_Studio/Tango.PMR/MessageFactory.cs +++ b/Software/Visual_Studio/Tango.PMR/MessageFactory.cs @@ -7,6 +7,7 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using Tango.PMR.Common; +using Tango.PMR.DataStore; using Tango.PMR.Integration; namespace Tango.PMR @@ -17,6 +18,7 @@ namespace Tango.PMR public static class MessageFactory { private static Dictionary _pmrTypes; + private static Dictionary _pmrDataStoreTypes; private static MethodInfo _parseTangoMessageMethod; static MessageFactory() @@ -35,6 +37,18 @@ namespace Tango.PMR _pmrTypes.Add(value, type); } } + + _pmrDataStoreTypes = new Dictionary(); + + foreach (var value in Enum.GetValues(typeof(DataStoreMessageType)).OfType()) + { + var type = types.SingleOrDefault(x => x.Name == value.ToOriginalName()); + + if (type != null) + { + _pmrDataStoreTypes.Add(value, type); + } + } } /// @@ -47,6 +61,16 @@ namespace Tango.PMR return _pmrTypes[messageType]; } + /// + /// Returns the matching PMR type from the PMR message type. + /// + /// Type of the message. + /// + public static Type GetDataStorePMRTypeFromDataStoreMessageType(DataStoreMessageType messageType) + { + return _pmrDataStoreTypes[messageType]; + } + /// /// Creates a new . /// @@ -199,6 +223,25 @@ namespace Tango.PMR } } + /// + /// Parses the specified text to a PMR . + /// + /// The text. + /// + /// Could not parse message type " + text + public static DataStoreMessageType ParseDataStoreMessageType(String text) + { + DataStoreMessageType t; + if (Enum.TryParse(text, true, out t)) + { + return t; + } + else + { + throw new InvalidCastException("Data Store Message type " + text + " was not found on PMR DataStoreMessageType enumeration!"); + } + } + /// /// Gets the available request stubs. /// diff --git a/Software/Visual_Studio/Tango.PMR/Tango.PMR.csproj b/Software/Visual_Studio/Tango.PMR/Tango.PMR.csproj index 7e3ec022f..dc2016ae1 100644 --- a/Software/Visual_Studio/Tango.PMR/Tango.PMR.csproj +++ b/Software/Visual_Studio/Tango.PMR/Tango.PMR.csproj @@ -88,6 +88,7 @@ + @@ -508,7 +509,7 @@ - + \ No newline at end of file -- cgit v1.3.1