aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Telemetry/TelemetryBase.cs
blob: c2879e9db6797bafa12f2b0cbd4f0c5064254dc0 (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
using LiteDB;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Telemetry
{
    public class TelemetryBase : ITelemetry
    {
        public string ID { get; set; }

        public DateTime Time { get; set; }

        public TelemetryBase()
        {
            ID = Guid.NewGuid().ToString();
            Time = DateTime.UtcNow;
        }

        [BsonIgnore] //This will be used for column mapping in ADX
        public String Type
        {
            get { return this.ToTelemetryName(); }
        }

        public byte[] ToBytes(Formatting format = Formatting.None, bool flatten = true)
        {
            return Encoding.UTF8.GetBytes(ToJson(format, flatten));
        }

        public string ToJson(Formatting format = Formatting.None, bool flatten = true)
        {
            if (flatten)
            {
                return JsonFlattener.FlattenObjectToFlatJson(this, format);
            }
            else
            {
                return JsonConvert.SerializeObject(this, format);
            }
        }
    }
}