diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.DataStore/DataStoreProtoObject.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.DataStore/DataStoreProtoObject.cs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.DataStore/DataStoreProtoObject.cs b/Software/Visual_Studio/Tango.DataStore/DataStoreProtoObject.cs new file mode 100644 index 000000000..ec4cf1057 --- /dev/null +++ b/Software/Visual_Studio/Tango.DataStore/DataStoreProtoObject.cs @@ -0,0 +1,71 @@ +using Google.Protobuf; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core.Bson; +using Tango.PMR; +using Tango.PMR.Common; +using Tango.PMR.DataStore; + +namespace Tango.DataStore +{ + public class DataStoreProtoObject + { + public MessageType MessageType { get; set; } + public Type Type { get; set; } + public byte[] Data { get; set; } + + + private IMessage _message; + [JsonIgnore] + public IMessage Message + { + get + { + if (_message == null) + { + _message = MessageFactory.ParseProtoMessage(Data, Type); + } + + return _message; + } + private set { _message = value; } + } + + public byte[] ToBytes() + { + return BsonConvert.Serialize<DataStoreProtoObject>(this); + } + + public static DataStoreProtoObject FromBytes(byte[] data) + { + var instance = BsonConvert.Deserialize<DataStoreProtoObject>(data); + instance.Message = MessageFactory.ParseProtoMessage(instance.Data, instance.Type); + + return instance; + } + + public static DataStoreProtoObject FromMessage(IMessage message) + { + DataStoreProtoObject proto = new DataStoreProtoObject(); + proto.Type = message.GetType(); + proto.MessageType = MessageFactory.ParseMessageType(proto.Type.Name); + proto.Data = message.ToByteArray(); + proto.Message = message; + return proto; + } + + public static DataStoreProtoObject FromPMRDataStoreItem(DataStoreItem item) + { + DataStoreProtoObject proto = new DataStoreProtoObject(); + proto.MessageType = item.ProtoType; + proto.Type = MessageFactory.GetPMRTypeFromMessageType(item.ProtoType); + proto.Data = item.BytesValue.ToByteArray(); + return proto; + } + } +} |
