forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogMetaData.cs
130 lines (113 loc) · 4.61 KB
/
LogMetaData.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using ExifLibrary;
using log4net;
using MissionPlanner.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using SharpCompress.Archives;
using SharpCompress.Compressors.Xz;
using SharpCompress.Readers;
namespace MissionPlanner.ArduPilot
{
public class LogMetaData
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static string[] vehicles = new[] { "Copter", "Plane", "Rover", "Tracker" };
static string url = "https://autotest.ardupilot.org/LogMessages/{0}/LogMessages.xml.xz";
public static Dictionary<string, Dictionary<string, string>> MetaData { get; } = new Dictionary<string, Dictionary<string, string>>();
public static async Task GetMetaData()
{
List<Task> tlist = new List<Task>();
vehicles.ForEach(a =>
{
try
{
var newurl = String.Format(url, a);
var file = Path.Combine(Settings.GetDataDirectory(), "LogMessages" + a + ".xml.xz");
if(File.Exists(file))
if (new FileInfo(file).LastWriteTime.AddDays(7) > DateTime.Now)
return;
var dltask = Download.getFilefromNetAsync(newurl, file);
tlist.Add(dltask);
}
catch (Exception ex) { log.Error(ex); }
});
await Task.WhenAll(tlist);
vehicles.ForEach(a =>
{
try
{
var fileout = Path.Combine(Settings.GetDataDirectory(), "LogMessages" + a + ".xml");
var file = Path.Combine(Settings.GetDataDirectory(), "LogMessages" + a + ".xml.xz");
if (File.Exists(file))
using (var read = File.OpenRead(file))
{
if (XZStream.IsXZStream(read))
{
read.Position = 0;
var stream = new XZStream(read);
using (var outst = File.OpenWrite(fileout))
{
try
{
outst.SetLength(0);
stream.CopyTo(outst);
}
catch (XZIndexMarkerReachedException)
{
// ignore
}
}
}
}
}
catch (Exception ex)
{
log.Error(ex);
}
});
}
public static void ParseMetaData()
{
vehicles.ForEach(a =>
{
var file = Path.Combine(Settings.GetDataDirectory(), "LogMessages" + a + ".xml");
if (!File.Exists(file))
{
return;
}
try
{
var xml = XDocument.Load(file);
xml.Root.Descendants("logformat").ForEach<XElement>(b =>
{
if (b == null)
return;
var type = b.Attribute("name");
var typedesc = b.Descendants("description").FirstOrDefault();
if (!MetaData.ContainsKey(type.Value))
MetaData[type.Value] = new Dictionary<string, string>();
MetaData[type.Value]["description"] = typedesc.Value;
b.Descendants("fields").Descendants("field").ForEach(c =>
{
var name = c.Attribute("name");
var desc = c.Descendants("description").FirstOrDefault();
MetaData[type.Value][name.Value] = desc.Value;
});
});
}
catch (Exception ex)
{
log.Error(ex);
}
});
}
}
}