-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
423 lines (383 loc) · 15 KB
/
Form1.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System;
using System.Diagnostics;
using System.Drawing;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
namespace RadioGet
{
public partial class Form1 : Form
{
public bool seen = false;
public Process p;
public int last_message = 0;
public int last_message2 = 0;
public int sent_mic_data = 0;
public int running = 1;
public ProcessStartInfo psi;
public bool isOnline = false;
public Task task;
public bool ready = false;
public int ffplay_pid = 0;
public System.Threading.Timer t;
public System.Threading.Timer t2;
private System.Timers.Timer aTimer;
private System.Timers.Timer aTimer2;
public Form1()
{
InitializeComponent();
Shown += Form1_Shown;
}
private void Form1_Shown(object sender, EventArgs e)
{
PTT.MouseDown += new MouseEventHandler(button1_MouseDown);
PTT.MouseUp += new MouseEventHandler(button1_MouseUp);
aTimer = new System.Timers.Timer();
aTimer2 = new System.Timers.Timer();
aTimer.Interval = 2000;
aTimer2.Interval = 2000;
aTimer.Elapsed += Ping_process;
aTimer2.Elapsed += Get_voltage;
aTimer.AutoReset = true;
aTimer.Enabled = true;
aTimer2.AutoReset = true;
aTimer2.Enabled = true;
task = Task.Run((Action)Recieve_speaker);
if (String.IsNullOrEmpty(Properties.Settings.Default.Audio))
{
MessageBox.Show("Please set mic device in settings!");
}
else
{
ready = true;
}
Thread.Sleep(1000);
Lautstaerke();
try
{
WebRequest request = WebRequest.Create("http://192.168.219.5/get_channel"); // call GPIO PORT to CLOSE
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResponse = reader.ReadToEnd();
response.Close();
MethodInvoker inv = delegate
{
label8.Text = strResponse;
};
Invoke(inv);
} catch(Exception)
{
MessageBox.Show("Cant fetch channel informations, is the api running?");
}
Application.DoEvents();
}
private void Get_voltage(Object source, System.Timers.ElapsedEventArgs e)
{
aTimer2.Interval = 60000;
try
{
WebRequest request = WebRequest.Create("http://192.168.219.5/voltage");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResponse = reader.ReadToEnd();
response.Close();
MethodInvoker inv = delegate
{
label9.Text = "V: " + strResponse;
};
Invoke(inv);
Application.DoEvents();
}
catch (Exception)
{
MessageBox.Show("Cant fetch voltage information, is the api running?");
}
}
public void Recieve_speaker()
{
Process p2 = new Process();
ProcessStartInfo info2 = p2.StartInfo;
info2.FileName = "taskkill";
info2.Arguments = " /F /IM ffplay.exe";
info2.CreateNoWindow = true;
info2.UseShellExecute = false;
p2.Start();
Thread.Sleep(1000);
Process p = new Process();
ProcessStartInfo info = p.StartInfo;
info.FileName = @AppDomain.CurrentDomain.BaseDirectory + @"Resources\ffplay.exe";
//"highpass = f = 350, lowpass = f = 18000\"";
info.Arguments = " -probesize 32 -acodec libfdk_aac -nodisp -autoexit -protocol_whitelist file,udp,rtp -fflags nobuffer -flags low_delay -reorder_queue_size 0 -i " + AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\x.sdp";
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
p.ErrorDataReceived += new DataReceivedEventHandler(Ffmpeg_data2);
p.Start();
p.BeginErrorReadLine();
p.WaitForExit();
}
public void Ping_process(Object source, System.Timers.ElapsedEventArgs e)
{
aTimer.Interval = 5000;
Ping pinger = new Ping();
PingReply reply = pinger.Send("192.168.219.5");
bool pingable = reply.Status == IPStatus.Success;
if(pingable == false)
{
Thread.Sleep(2000);
reply = pinger.Send("192.168.219.5");
pingable = reply.Status == IPStatus.Success;
}
try
{
MethodInvoker inv = delegate
{
if (pingable && ready)
{
isOnline = true;
PTT.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
label1.BackColor = Color.Green;
label1.Text = "ON";
}
else
{
isOnline = false;
PTT.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
label1.BackColor = Color.Red;
label1.Text = "OFF";
}
};
Invoke(inv);
Application.DoEvents();
} catch(Exception)
{
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (seen == false)
{
seen = true;
p = new Process();
ProcessStartInfo info = p.StartInfo;
info.FileName = @AppDomain.CurrentDomain.BaseDirectory + @"Resources\ffmpeg.exe";
info.Arguments = " -audio_buffer_size 50 -f dshow -acodec pcm_s16le -i audio=" + Properties.Settings.Default.Audio + " -hide_banner -v error -stats -xerror -analyzeduration 0 -probesize 32 -c:a libfdk_aac -profile:a aac_eld -b:a 64k -flags +global_header -f nut tcp://192.168.219.5:1337/out.aac";
info.RedirectStandardError = true;
info.CreateNoWindow = true;
info.UseShellExecute = false;
p.ErrorDataReceived += new DataReceivedEventHandler(Ffmpeg_data);
p.Start();
p.BeginErrorReadLine();
Thread.Sleep(500);
PTT.BackColor = Color.Red;
Application.DoEvents();
}
}
void Ffmpeg_data(object sender, DataReceivedEventArgs e)
{
if (!String.IsNullOrEmpty(e.Data))
{
string pattern = @"size=\s*(\d+)(kB|mB|b)";
// Create a Regex
Regex r = new Regex(pattern);
Match m = r.Match(e.Data);
if (m.Success)
{
last_message = Int32.Parse(m.Groups[1].Value);
}
try
{
MethodInvoker inv = delegate
{
label6.Text = "Outgoing: " + e.Data;
};
Invoke(inv);
Application.DoEvents();
} catch(Exception)
{
}
}
}
void Ffmpeg_data2(object sender, DataReceivedEventArgs e)
{
if (!String.IsNullOrEmpty(e.Data))
{
string pattern = @"size=\s*(\d+)(kB|mB|b)";
// Create a Regex
Regex r = new Regex(pattern);
Match m = r.Match(e.Data);
if (m.Success)
{
last_message2 = Int32.Parse(m.Groups[1].Value);
}
try
{
MethodInvoker inv = delegate
{
label2.Text = "Incoming: " + e.Data;
};
Invoke(inv);
Application.DoEvents();
}
catch (Exception) {
}
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (seen == true)
{
Thread.Sleep(500);
PTT.BackColor = default(Color);
PTT.Enabled = true;
running = 0;
seen = false;
try
{
p.Kill();
} catch(InvalidOperationException)
{
MessageBox.Show("FFmpeg was killed unexpected");
}
sent_mic_data += last_message;
Application.DoEvents();
Update_total_label();
}
}
private void Update_total_label()
{
try
{
MethodInvoker inv = delegate
{
label4.Text = "Bandwidth: " + sent_mic_data.ToString() + "kB";
label6.Text = "Outgoing:";
};
Invoke(inv);
Application.DoEvents();
} catch(Exception)
{
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
WebRequest request = WebRequest.Create("http://192.168.219.5/up"); // call GPIO PORT to CLOSE
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResponse = reader.ReadToEnd();
response.Close();
MethodInvoker inv = delegate
{
label8.Text = strResponse;
};
Invoke(inv);
Application.DoEvents();
}catch(Exception)
{
MessageBox.Show("Cant fetch voltage information, is the api running?");
}
}
private void button1_Click_1(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
Lautstaerke();
DialogResult dialogResult = MessageBox.Show("Sure?", "Rebooting?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
WebRequest request = WebRequest.Create("http://192.168.219.5/reboot"); // call GPIO PORT to CLOSE
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResponse = reader.ReadToEnd();
response.Close();
}
}
public void Lautstaerke()
{
foreach (var process in Process.GetProcesses())
{
if (ffplay_pid == 0)
{
if (process.ProcessName == "ffplay")
{
ffplay_pid = process.Id;
break;
}
}
}
VolumeMixer.SetApplicationVolume(ffplay_pid, 50f);
}
private void trackBar1_Scroll(object sender, EventArgs e) {
// MessageBox.Show(trackBar1.Value.ToString());
VolumeMixer.SetApplicationVolume(ffplay_pid, (float)(trackBar1.Value*10));
}
private void label8DoubleClick(object sender, EventArgs e)
{
System.Drawing.Size size = new System.Drawing.Size(100, 70);
Form inputBox = new Form();
inputBox.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
inputBox.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
inputBox.MaximizeBox = false;
inputBox.MinimizeBox = false;
inputBox.ClientSize = size;
inputBox.Text = "Kanal";
System.Windows.Forms.TextBox textBox = new TextBox();
textBox.Text = this.label8.Text;
textBox.Size = new System.Drawing.Size(size.Width - 10, 10);
textBox.Location = new System.Drawing.Point(5, 5);
inputBox.Controls.Add(textBox);
Button okButton = new Button();
okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
okButton.Name = "okButton";
okButton.Size = new System.Drawing.Size(75, 23);
okButton.Text = "&OK";
okButton.Location = new System.Drawing.Point(size.Width - 80 , 39);
inputBox.Controls.Add(okButton);
inputBox.AcceptButton = okButton;
if (inputBox.ShowDialog(this) == DialogResult.OK)
{
try
{
WebRequest request = WebRequest.Create("http://192.168.219.5/set_channel?channel=" + textBox.Text); // call GPIO PORT to CLOSE
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResponse = reader.ReadToEnd();
response.Close();
MethodInvoker inv = delegate
{
label8.Text = textBox.Text;
};
Invoke(inv);
Application.DoEvents();
}
catch
{
}
}
}
}
}