-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form1.cs
341 lines (297 loc) · 13.6 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using Tomidix.CSharpTradFriLibrary;
using Tomidix.CSharpTradFriLibrary.Controllers;
using Tomidix.CSharpTradFriLibrary.Models;
namespace VUmeter
{
public partial class Form1 : Form
{
private double level = 0;
private TradFriCoapConnector gatewayConnection = null;
private DeviceController dc = null;
private GroupController gc = null;
private const double FILTER_CONSTANT = 0.1;
Color[] colors = new Color[] { Color.FromArgb(0x4a, 0x41, 0x8a ),
Color.FromArgb( 0x8f, 0x26, 0x86 ),
Color.FromArgb( 0xdc, 0x4b, 0x31 ),
Color.FromArgb( 0xda, 0x5d, 0x41 ),
Color.FromArgb( 0x6c, 0x83, 0xba ),
Color.FromArgb( 0xd9, 0x33, 0x7c ),
Color.FromArgb( 0xe5, 0x73, 0x45 ),
Color.FromArgb( 0xe7, 0x88, 0x34 ),
Color.FromArgb( 0xa9, 0xd6, 0x2b ),
Color.FromArgb( 0xeb, 0xb6, 0x3e ),
Color.FromArgb( 0xc9, 0x84, 0xbb ),
Color.FromArgb( 0xd6, 0xe4, 0x4b ),
Color.FromArgb( 0xe4, 0x91, 0xaf ),
Color.FromArgb( 0xef, 0xd2, 0x75 ),
Color.FromArgb( 0xe8, 0xbe, 0xdd ),
Color.FromArgb( 0xf1, 0xe0, 0xb5 ),
Color.FromArgb( 0xf2, 0xec, 0xcf ),
Color.FromArgb( 0xdc, 0xf0, 0xf8 ),
Color.FromArgb( 0xea, 0xf6, 0xfb ),
Color.FromArgb( 0xf5, 0xfa, 0xf6 ) };
string[] colorStr = new string[] { "Blue",
"Light Blue",
"Saturated Purple",
"Lime",
"Light Purple",
"Yellow",
"Saturated Pink",
"Dark Peach",
"Saturated Red",
"Cold sky",
"Pink",
"Peach",
"Warm Amber",
"Light Pink",
"Cool daylight",
"Candlelight",
"Warm glow",
"Warm white",
"Sunrise",
"Cool white"
};
string[] colorHex = new string[] { "4a418a",
"8f2686",
"dc4b31",
"da5d41",
"6c83ba",
"d9337c",
"e57345",
"e78834",
"a9d62b",
"ebb63e",
"c984bb",
"d6e44b",
"e491af",
"efd275",
"e8bedd",
"f1e0b5",
"f2eccf",
"dcf0f8",
"eaf6fb",
"f5faf6"
};
public Form1()
{
InitializeComponent();
// Fill the Audio-devices selection
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
var audiodevices = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
comboBox1.Items.AddRange( audiodevices.ToArray() );
// Fill the settings for the tradfri-connection settings
textBox3.Text = Properties.Settings.Default.GatewayIP;
textBox1.Text = Properties.Settings.Default.ConnectionName;
textBox2.Text = Properties.Settings.Default.ConnectionSecret;
}
// On application closing save back the tradfri-connection settings.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
bool SaveSettings = ( Properties.Settings.Default.GatewayIP.Equals(textBox3.Text) ) &
( Properties.Settings.Default.ConnectionName.Equals(textBox1.Text)) &
( Properties.Settings.Default.ConnectionSecret.Equals(textBox2.Text));
if ( !SaveSettings )
{
Properties.Settings.Default.GatewayIP = textBox3.Text;
Properties.Settings.Default.ConnectionName = textBox1.Text;
Properties.Settings.Default.ConnectionSecret = textBox2.Text;
Properties.Settings.Default.Save();
}
}
private void LoadAllDevices()
{
GatewayController gwc = new GatewayController(gatewayConnection.Client);
comboBox2.Enabled = false;
comboBox2.BeginUpdate();
comboBox2.Items.Clear();
// Read the devices
foreach ( long deviceID in gwc.GetDevices() )
{
DeviceController dcl = new DeviceController(deviceID, gatewayConnection.Client);
TradFriDevice device = dcl.GetTradFriDevice();
comboBox2.Items.Add( device );
}
// Read the groups
foreach (long groupID in gwc.GetGroups())
{
GroupController gcl = new GroupController(groupID, gatewayConnection.Client);
TradFriGroup currentGroup = gcl.GetTradFriGroup();
comboBox2.Items.Add( currentGroup );
}
comboBox2.SelectedIndex = -1;
comboBox2.EndUpdate();
comboBox2.Enabled = true;
}
private bool CheckTradfriAddress( string Address )
{
bool alive = false;
Ping pingSender = new Ping();
try
{
PingReply reply = pingSender.Send( Address );
alive = ( reply.Status == IPStatus.Success );
}
catch ( PingException )
{
return false;
}
return alive;
}
private void ConnectTradfri()
{
try
{
if (gatewayConnection == null )
{
if (!CheckTradfriAddress(textBox3.Text))
{
MessageBox.Show("Could not find Tradfri gateway. Check address/DNS name.");
return;
}
gatewayConnection = new TradFriCoapConnector( textBox1.Text, textBox3.Text, textBox2.Text);
gatewayConnection.Connect();
}
// Query all devices and list in the comboBox.
LoadAllDevices();
}
catch (Exception ex)
{
MessageBox.Show($"Couldn't connect to TradFri gateway with provided settings: {ex.Message}");
gatewayConnection = null;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
bool update = false;
var device = (MMDevice)comboBox1.SelectedItem;
double index = Math.Round(device.AudioMeterInformation.MasterPeakValue * (colors.Length - 1));
// Update graphic VU-Meter in panel.
progressBar1.Value = (int)(Math.Round(device.AudioMeterInformation.MasterPeakValue * 100));
// Upodate the panel-background color and the color of the bulb, such that they change
// consistently and at a much lower rate than the VU-meter shown in progressBar1.
// This is necessary, because a too high update-rate crashes the tradfri-gateway and further
// may limit live-expectancy of the bulb.
if ((index - level >= 3) || (level - index >= 3))
{
level = index;
update = true;
}
else
{
int old_level = (int)level;
level = (1 - FILTER_CONSTANT) * level + FILTER_CONSTANT * index;
if (old_level != (int)level) update = true;
}
// If required, then update the Tradfri-bulb color/dimmer and correspondigly the backcolor of the panel.
// For the individual bulb, change the color. For the group change the dimmer.
if (update)
{
if (dc != null)
{
panel1.BackColor = colors[(int)level];
dc.SetColor(colorHex[(int)level]);
}
else if (gc != null)
{
int dimmer = (int)(level * 256 / colors.Length);
gc.SetDimmer(dimmer);
panel1.BackColor = Color.FromArgb(dimmer, dimmer, dimmer);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if ( button1.Text == "Start")
{
button1.Text = "Stop";
timer1.Enabled = true;
}
else
{
timer1.Enabled = false;
level = 0;
progressBar1.Value = 0;
button1.Text = "Start";
}
}
private void button2_Click(object sender, EventArgs e)
{
button2.Text = "Connecting";
button2.Enabled = false;
button2.Update();
ConnectTradfri();
button2.Text = "Connect";
button2.Enabled = true;
button2.Update();
}
private void button3_Click(object sender, EventArgs e)
{
var Form2 = new Form2();
if (!CheckTradfriAddress(textBox3.Text))
{
MessageBox.Show("Could not find Tradfri gateway. Check address/DNS name.");
return;
}
Form2.textBox1.Text = textBox1.Text;
if ( Form2.ShowDialog().Equals(DialogResult.OK) )
{
try
{
if (gatewayConnection == null)
{
gatewayConnection = new TradFriCoapConnector(textBox1.Text, textBox3.Text, textBox2.Text);
}
TradFriAuth ConnectionSecret = gatewayConnection.GeneratePSK(Form2.textBox2.Text, Form2.textBox1.Text);
// Write the connection settings back to the respective text boxes.
textBox1.Text = Form2.textBox1.Text;
textBox2.Text = ConnectionSecret.PSK;
gatewayConnection = null;
}
catch (Exception ex)
{
MessageBox.Show($"Couldn't connect to TradFri gateway with provided settings: {ex.Message}");
gatewayConnection = null;
}
}
}
private void comboBox2_SelectionChangeCommitted(object sender, EventArgs e)
{
if ( comboBox2.SelectedIndex >= 0 )
{
if ( comboBox2.SelectedItem.GetType() == typeof( TradFriDevice ))
{
var deviceToChangeProperties = (TradFriDevice) comboBox2.SelectedItem;
dc = new DeviceController(deviceToChangeProperties.ID, gatewayConnection.Client);
gc = null;
}
else if (comboBox2.SelectedItem.GetType() == typeof( TradFriGroup ))
{
var deviceToChangeProperties = (TradFriGroup) comboBox2.SelectedItem;
gc = new GroupController(deviceToChangeProperties.ID, gatewayConnection.Client);
dc = null;
}
else
{
dc = null;
gc = null;
}
}
}
}
}