-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerConnect.cs
221 lines (198 loc) · 7 KB
/
ServerConnect.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
using Microsoft.AspNetCore.SignalR.Client;
using NetCoreAudio;
using System;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace busylight_client
{
class ServerConnect
{
private static Busylight.SDK busy = new Busylight.SDK();
HubConnection connection;
public ContextMenuStrip _menu;
private static Settings _settings;
private static Busylight.BusylightColor _idleColor;
public ServerConnect(Settings settings, ContextMenuStrip menu)
{
_settings = settings;
_menu = menu;
_idleColor = GetColorFromString(_settings.Idle_Color);
connection = new HubConnectionBuilder()
.WithUrl(new Uri(_settings.SignalR_Uri), options =>
{
options.Headers.Add(_settings.KeyName, _settings.ApiKey);
})
.Build();
}
public async Task Connect()
{
connection.Closed += async error =>
{
ToggleColorMenu(false);
await Task.Delay(new Random().Next(0, 5) * 1000);
await ConnectWithRetryAsync();
ToggleColorMenu(true);
};
connection.Reconnected += async error =>
{
await JoinGroup(_settings.Location);
};
connection.On("Police", async () =>
{
for (int i = 0; i < 400; i++)
{
busy.Light(i % 2 == 0 ? Busylight.BusylightColor.Red : Busylight.BusylightColor.Blue);
await Task.Delay(75);
}
});
connection.On("SetColor", (string colorAsString) =>
{
colorAsString = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(colorAsString?.ToLower());
var color = GetColorFromString(colorAsString);
busy.Light(color);
});
connection.On("Ring", async () =>
{
if (!string.IsNullOrEmpty(_settings.Custom_Sound))
{
var soundIsPlaying = true;
Player player = new Player();
await player.SetVolume((byte)_settings.Ring_Volume);
player.PlaybackFinished += (sender, args) =>
{
// This event is triggered even if the player still has 1 or 2 seconds to play.
busy.Light(_idleColor);
soundIsPlaying = false;
};
await player.Play(_settings.Custom_Sound);
var redOrBlue = true;
while (soundIsPlaying)
{
busy.Light(redOrBlue ? Busylight.BusylightColor.Red : Busylight.BusylightColor.Blue);
redOrBlue = !redOrBlue;
await Task.Delay(75);
}
}
else
{
var tune = GetTuneFromString(_settings.Ring_Tune);
var color = GetColorFromString(_settings.Ring_Color);
busy.Alert(color, tune, GetVolumeFromInt(_settings.Ring_Volume));
await Task.Delay(_settings.Ring_Time);
busy.Light(_idleColor);
}
});
try
{
await ConnectWithRetryAsync();
AddClickEvent();
busy.Light(_idleColor);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
async Task<bool> ConnectWithRetryAsync()
{
while (true)
{
try
{
await connection.StartAsync();
await JoinGroup(_settings.Location);
return true;
}
catch (Exception)
{
await Task.Delay(5000);
}
}
}
return;
}
private async Task JoinGroup(string groupName)
{
await connection.InvokeAsync("JoinGroup", groupName);
}
private async Task SendToSelf(string color)
{
await connection.InvokeAsync("SendToSelf", color);
}
private static Busylight.BusylightColor GetColorFromString(string colorAsString)
{
var propInfo = typeof(Busylight.BusylightColor).GetProperty(colorAsString);
if (propInfo != null)
{
var color = (Busylight.BusylightColor)propInfo.GetValue(propInfo, null);
return color;
}
return Busylight.BusylightColor.Red;
}
private static Busylight.BusylightSoundClip GetTuneFromString(string tuneAsString)
{
try
{
return (Busylight.BusylightSoundClip)Enum.Parse(typeof(Busylight.BusylightSoundClip), tuneAsString, true);
}
catch (Exception)
{
return Busylight.BusylightSoundClip.KuandoTrain;
}
}
private static Busylight.BusylightVolume GetVolumeFromInt(int volumeAsInt)
{
try
{
return (Busylight.BusylightVolume)volumeAsInt;
}
catch (Exception)
{
return Busylight.BusylightVolume.High;
}
}
private void AddClickEvent()
{
foreach (var item in _menu.Items)
{
if (item is ToolStripMenuItem)
{
var aa = item as ToolStripMenuItem;
if ((string)aa.Tag == "Color")
{
aa.Enabled = true;
aa.Click += async (s, e) =>
{
await SendToSelf(aa.Name);
};
}
}
}
}
private void ToggleColorMenu(bool enable)
{
foreach (var item in _menu.Items)
{
if (item is ToolStripMenuItem)
{
var aa = item as ToolStripMenuItem;
if ((string)aa.Tag == "Color")
{
try
{
void enableAction() => aa.Enabled = enable;
_menu.BeginInvoke(enableAction);
}
catch (Exception)
{
// var jjaa = e;
// throw;
}
}
}
}
}
}
}