blob: 5aa7c534294f12598efdfcf0140d198e398ed581 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
using Google.Protobuf;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
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.Core.ExtensionMethods;
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 FromJObject(JObject obj)
{
return (obj.ToObject<DataStoreProtoObject>());
}
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;
}
public override string ToString()
{
return Message?.ToJsonString();
}
}
}
|