/
FlightSimApp.xaml.cs
81 lines (78 loc) · 3.23 KB
/
FlightSimApp.xaml.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Flight_Sim.cppToCSharp;
using System.IO;
using Flight_Sim.Model;
namespace Flight_Sim
{
/// <summary>
/// Interaction logic for FlightSimApp.xaml
/// </summary>
public partial class FlightSimApp : Window
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int useDetector(string trainPath, string testPath);
public FlightSimApp()
{
useDll();
InitializeComponent();
}
private void useDll()
{
FlightSimM fsm = Single.SingleFlightSimM();
// creates a new csv like the received one but with the properties in the first row.
string noPropTrainCSVPath = fsm.CSVTrainPath;
string noPropTestCSVPath = fsm.CsvPath;
string csvTrain = "train.csv";
string csvTest = "test.csv";
// adds the first line with properties
File.WriteAllText(csvTrain, fsm.ColDataNames[0]);
File.WriteAllText(csvTest, fsm.ColDataNames[0]);
for (int i = 1; i < fsm.ColDataNames.Count; i++)
{
File.AppendAllText(csvTrain, "," + fsm.ColDataNames[i]);
File.AppendAllText(csvTest, "," + fsm.ColDataNames[i]);
}
File.AppendAllText(csvTrain, "\n");
File.AppendAllText(csvTest, "\n");
// copying the rest of the file as it is
string[] noPropTrainLines = File.ReadAllLines(noPropTrainCSVPath);
File.AppendAllLines(csvTrain, noPropTrainLines);
// copying the rest of the file as it is
string[] noPropTestLines = File.ReadAllLines(noPropTestCSVPath);
File.AppendAllLines(csvTest, noPropTestLines);
// use the dll function, it creates a file named Anomalies.csv that holds all the
// AnomalyReports, each line holds: Timestep,Description.
string dllPath = fsm.DLLPath;
IntPtr pDll = NativeMethods.LoadLibrary(@dllPath);
IntPtr pAddressofFunctionToCall = NativeMethods.GetProcAddress(pDll, "useDetector");
useDetector upload = (useDetector)Marshal.GetDelegateForFunctionPointer(
pAddressofFunctionToCall,
typeof(useDetector));
int theResult = upload(csvTrain, csvTest);
// saves the anomaly reports that in the file Animalies.csv
// locally in a list of AnomalyReports
string anomaliesPath = "Anomalies.csv";
List<AnomalyReport> anomalyReports = Single.SingleDataModel().AnomalyReports;
string[] lines = File.ReadAllLines(anomaliesPath);
foreach (string ln in lines)
{
string[] splitted = ln.Split(',');
anomalyReports.Add(new AnomalyReport(splitted[1], long.Parse(splitted[0])));
}
}
}
}