-
Notifications
You must be signed in to change notification settings - Fork 3
/
Panel.cs
113 lines (110 loc) · 3.13 KB
/
Panel.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
namespace DesktopApp
{
//Panel Class (Model)
public class Panel : FlightGearClient
{
private static readonly EventWaitHandle Wait = new ManualResetEvent(initialState: true);
private int _linesN; //number of rows.
private IEnumerable<string> _lines;
public void SetPath(string path)
{
_lines = File.ReadLines(path);
M.WaitOne();
_LinesN = _lines.Count();
M.ReleaseMutex();
_NumLine = 0;
}
public int _LinesN
{
get => _linesN;
set
{
_linesN = value;
NotifyPropertyChanged("_LinesN");
}
}
private bool _needToClose;
//Constructor
public Panel()
{
_needToClose = false;
_LinesN = 500;
}
//Constructor
public Panel(int port) : base(port)
{
_needToClose = false;
_linesN = 500;
}
//send the csv file line-by-line to the FlightGear server.
public void CSV_Sender(string path)
{
try {
_lines = File.ReadLines(path);
M.WaitOne();
var lines = _lines.ToList();
_LinesN = lines.Count();
M.ReleaseMutex();
var line = lines.ElementAt(this._NumLine);
while (line != null && !_needToClose)
{
var data = Encoding.ASCII.GetBytes(line + "\r\n");
Stream.Write(data, 0, data.Length);
while (SendSpeed == 0)
{
}
Thread.Sleep((int)(1000 / this.SendSpeed));
M.WaitOne();
if (_NumLine > _linesN)
Pause();
this._NumLine++;
M.ReleaseMutex();
if (this._NumLine < lines.Count())
{
line = lines.ElementAt(this._NumLine);
}
Wait.WaitOne();
}
}
catch (Exception e) { Console.WriteLine(e.Message); }
}
//Pause the sending.
public void Pause()
{
if (IsRunning)
{
Wait.Reset();
IsRunning = false;
}
}
//Play the sending.
public void Play()
{
if (!IsRunning)
{
Wait.Set();
IsRunning = true;
}
}
public void Close()
{
IsRunning = false;
try
{
Stream.Close();
TcpClient.Close();
}
catch (Exception)
{
// ignored
}
_needToClose = true;
}
}
}