-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlSystem.cs
327 lines (267 loc) · 14.9 KB
/
ControlSystem.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
using System;
using Crestron.SimplSharp; // For Basic SIMPL# Classes
using Crestron.SimplSharpPro; // For Basic SIMPL#Pro classes
using Crestron.SimplSharpPro.CrestronThread; // For Threading
using Crestron.SimplSharpPro.Diagnostics; // For System Monitor Access
using Crestron.SimplSharpPro.DeviceSupport;
using System.Text.RegularExpressions; // For Generic Device Support
namespace Crestron_TCP_Buttons {
public class ControlSystem : CrestronControlSystem {
private Server server;
private MPC3Basic panel;
private ButtonController buttonController;
public static ControlSystem System;
public ControlSystem() : base() {
Thread.MaxNumberOfUserThreads = 20;
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(ControlSystem_ControllerProgramEventHandler);
CrestronEnvironment.EthernetEventHandler += new EthernetEventHandler(ControlSystem_ControllerEthernetEventHandler);
System = this;
server = new Server();
server.ClientConnectedEvent += new ClientConnected(server_ClientConnectedEvent);
server.ClientDataReceiveEvent += new ClientDataReceived(server_ClientDataReceiveEvent);
}
private static Regex ConfigureCommand = new Regex(
@"CONFIGURE ([^ ]+) (TOGGLE|MOMENTARY|DISABLED|INTERLOCK GROUP (\d+))",
RegexOptions.IgnoreCase | RegexOptions.Compiled
);
private static Regex SetButtonCommand = new Regex(
@"SET ([^ ]+) (TRUE|FALSE)",
RegexOptions.IgnoreCase | RegexOptions.Compiled
);
private static Regex SetGroupCommand = new Regex(
@"SETGROUP (\d+) (.+)",
RegexOptions.IgnoreCase | RegexOptions.Compiled
);
private static Regex SetVolumeCommand = new Regex(
@"SETVOLUME (\d+)",
RegexOptions.IgnoreCase | RegexOptions.Compiled
);
private void server_ClientDataReceiveEvent(Client client, string data) {
if(data.ToUpper() == "POLL") {
client.Send(buttonController.summarize());
return;
}
if(data.ToUpper() == "ENABLE") {
SetBacklight(true);
SetBeep(true);
buttonController.Disabled = false;
if(this.MPC3x30xTouchscreenSlot != null)
this.MPC3x30xTouchscreenSlot.EnableVolumeControl();
client.Send("PANEL ENABLED\n\n");
return;
}
if(data.ToUpper() == "DISABLE") {
SetBacklight(false);
SetBeep(false);
buttonController.Disabled = true;
if(this.MPC3x30xTouchscreenSlot != null)
this.MPC3x30xTouchscreenSlot.DisableVolumeControl();
client.Send("PANEL DISABLED\n\n");
return;
}
var configCmdMatches = ConfigureCommand.Match(data);
if(configCmdMatches.Success) {
String mode = configCmdMatches.Groups[2].ToString().ToUpper();
if(mode == "MOMENTARY" && buttonController.hasButton(configCmdMatches.Groups[1].Value)) {
buttonController.SetButtonMode(configCmdMatches.Groups[1].Value, ButtonMode.Momentary);
client.Send("CONFIGURATION SUCCESS\n\n");
} else if(mode == "TOGGLE" && buttonController.hasButton(configCmdMatches.Groups[1].Value)) {
buttonController.SetButtonMode(configCmdMatches.Groups[1].Value, ButtonMode.Toggle);
client.Send("CONFIGURATION SUCCESS\n\n");
} else if(mode.StartsWith("INTERLOCK GROUP") && buttonController.hasButton(configCmdMatches.Groups[1].Value)) {
buttonController.SetButtonMode(configCmdMatches.Groups[1].Value, ButtonMode.Interlock, uint.Parse(configCmdMatches.Groups[3].Value));
client.Send("CONFIGURATION SUCCESS\n\n");
} else if(mode == "DISABLED" && buttonController.hasButton(configCmdMatches.Groups[1].Value)) {
buttonController.SetButtonMode(configCmdMatches.Groups[1].Value, ButtonMode.Disabled);
client.Send("CONFIGURATION SUCCESS\n\n");
} else {
client.Send("! INVALID COMMAND\n\n");
}
return;
}
var setButtonCmdMatches = SetButtonCommand.Match(data);
if(setButtonCmdMatches.Success && setButtonCmdMatches.Groups.Count > 2) {
String newState = setButtonCmdMatches.Groups[2].Value.ToUpper();
if(newState == "TRUE") {
buttonController.SetButtonState(setButtonCmdMatches.Groups[1].Value, true);
} else {
buttonController.SetButtonState(setButtonCmdMatches.Groups[1].Value, false);
}
client.Send("UPDATED\n\n");
return;
}
var setGroupCmdMatches = SetGroupCommand.Match(data);
if(setGroupCmdMatches.Success && setGroupCmdMatches.Groups.Count > 2) {
String newState = setGroupCmdMatches.Groups[2].Value;
buttonController.SetGroupState(uint.Parse(setGroupCmdMatches.Groups[1].Value), newState);
client.Send("UPDATED\n\n");
return;
}
var setVolumeCmdMatches = SetVolumeCommand.Match(data);
if(setVolumeCmdMatches.Success && setVolumeCmdMatches.Groups.Count > 1) {
if(this.MPC3x30xTouchscreenSlot != null)
this.MPC3x30xTouchscreenSlot.VolumeFeedback.UShortValue = ushort.Parse(setVolumeCmdMatches.Groups[1].Value);
else
this.panel.VolumeBargraph.UShortValue = ushort.Parse(setVolumeCmdMatches.Groups[1].Value);
client.Send("UPDATED\n\n");
return;
}
client.Send("! UNRECOGNIZED COMMAND\n\n");
}
void server_ClientConnectedEvent(Client client) {
client.Send("# Welcome to " + ControlSystem.System.ControllerPrompt + "\n\n");
if(panel == null) {
client.Send("! Device platform not supported.\n");
} else {
client.Send(buttonController.summarize());
}
}
private void SetBacklight(bool on) {
panel.LEDBrightness.UShortValue = on ? ushort.MaxValue : (ushort) 0;
if(this.MPC3x30xTouchscreenSlot != null) {
this.MPC3x30xTouchscreenSlot.LedBrightnessHighLevel.UShortValue = on ? ushort.MaxValue : (ushort) 0;
this.MPC3x30xTouchscreenSlot.LedBrightnessLowLevel.UShortValue = on ? ushort.MaxValue : (ushort) 0;
} else {
panel.ActiveBrightness.UShortValue = on ? ushort.MaxValue : (ushort) 0;
panel.ActiveModeAutoBrightnessHighLevel.UShortValue = on ? ushort.MaxValue : (ushort) 0;
panel.ActiveModeAutoBrightnessLowLevel.UShortValue = on ? ushort.MaxValue : (ushort) 0; ;
panel.StandbyBrightness.UShortValue = on ? ushort.MaxValue : (ushort) 0;
panel.StandbyModeAutoBrightnessHighLevel.UShortValue = on ? ushort.MaxValue : (ushort) 0;
panel.StandbyModeAutoBrightnessLowLevel.UShortValue = on ? ushort.MaxValue : (ushort) 0;
panel.StandbyTimeout.UShortValue = 0;
}
}
private void SetBeep(bool on) {
if(on) {
if(this.MPC3x101TouchscreenSlot != null)
this.MPC3x101TouchscreenSlot.EnableButtonPressBeeping();
if(this.MPC3x102TouchscreenSlot != null)
this.MPC3x102TouchscreenSlot.EnableButtonPressBeeping();
if(this.MPC3x201TouchscreenSlot != null)
this.MPC3x201TouchscreenSlot.EnableButtonPressBeeping();
} else {
if(this.MPC3x101TouchscreenSlot != null)
this.MPC3x101TouchscreenSlot.DisableButtonPressBeeping();
if(this.MPC3x102TouchscreenSlot != null)
this.MPC3x102TouchscreenSlot.DisableButtonPressBeeping();
if(this.MPC3x201TouchscreenSlot != null)
this.MPC3x201TouchscreenSlot.DisableButtonPressBeeping();
}
}
private void ConfigureBasicPanel(MPC3Basic mpc) {
mpc.Register();
mpc.EnableMuteButton();
mpc.EnablePowerButton();
for(uint i = 1; i < 10; i++) {
try { mpc.EnableNumericalButton(i); } catch(IndexOutOfRangeException) { }
}
mpc.AutoBrightnessEnabled.BoolValue = false;
panel = mpc;
SetBacklight(true);
SetBeep(true);
buttonController = new ButtonController(mpc);
buttonController.ButtonStateEvent += new ButtonStateEventHandler(buttonController_ButtonStateEvent);
buttonController.ButtonGroupEvent += new ButtonGroupEventHandler(buttonController_ButtonGroupEvent);
}
//private void ConfigureVolumeButtons(
void buttonController_ButtonGroupEvent(uint groupId, string button) {
server.Broadcast("UPDATE:\n GROUP<" + groupId + "> = " + button + "\n\n");
}
void buttonController_ButtonStateEvent(Button button, bool state) {
server.Broadcast("UPDATE:\n BUTTON<" + button.Name.ToString() + "> = " + state + "\n\n");
}
public override void InitializeSystem() {
try {
if(this.MPC3x101TouchscreenSlot != null) {
ConfigureBasicPanel(this.MPC3x101TouchscreenSlot);
this.MPC3x101TouchscreenSlot.EnableVolumeUpButton();
this.MPC3x101TouchscreenSlot.EnableVolumeDownButton();
this.MPC3x101TouchscreenSlot.EnableProximityWakeup.BoolValue = false;
this.MPC3x101TouchscreenSlot.ButtonStateChange += new ButtonEventHandler(ButtonStateChange);
}
if(this.MPC3x102TouchscreenSlot != null) {
ConfigureBasicPanel(this.MPC3x102TouchscreenSlot);
this.MPC3x102TouchscreenSlot.EnableVolumeUpButton();
this.MPC3x102TouchscreenSlot.EnableVolumeDownButton();
this.MPC3x102TouchscreenSlot.EnableProximityWakeup.BoolValue = false;
this.MPC3x102TouchscreenSlot.ButtonStateChange += new ButtonEventHandler(ButtonStateChange);
}
if(this.MPC3x201TouchscreenSlot != null) {
ConfigureBasicPanel(this.MPC3x201TouchscreenSlot);
this.MPC3x201TouchscreenSlot.EnableVolumeUpButton();
this.MPC3x201TouchscreenSlot.EnableVolumeDownButton();
this.MPC3x201TouchscreenSlot.EnableProximityWakeup.BoolValue = false;
this.MPC3x201TouchscreenSlot.ButtonStateChange += new ButtonEventHandler(ButtonStateChange);
}
if(this.MPC3x30xTouchscreenSlot != null) {
ConfigureBasicPanel(this.MPC3x30xTouchscreenSlot);
this.MPC3x30xTouchscreenSlot.EnableVolumeControl();
this.MPC3x30xTouchscreenSlot.LedBrightnessHighLevel.UShortValue = ushort.MaxValue;
this.MPC3x30xTouchscreenSlot.LedBrightnessLowLevel.UShortValue = ushort.MaxValue;
}
if(panel != null) {
panel.SigChange += new SigEventHandler(SigChange);
}
server.Start();
} catch(Exception e) {
ErrorLog.Error("Error in InitializeSystem: {0}", e.Message);
}
}
private UShortInputSig Volume {
get {
if(this.MPC3x101TouchscreenSlot != null)
return this.MPC3x101TouchscreenSlot.VolumeBargraph;
else if(this.MPC3x102TouchscreenSlot != null)
return this.MPC3x102TouchscreenSlot.VolumeBargraph;
else if(this.MPC3x201TouchscreenSlot != null)
return this.MPC3x201TouchscreenSlot.VolumeBargraph;
return null;
}
}
void ButtonStateChange(GenericBase device, ButtonEventArgs args) {
if(buttonController.Disabled) return;
if(args.Button.Name == eButtonName.VolumeUp) {
if(Volume != null) Volume.UShortValue = (ushort) Math.Min(65535, ((int) Volume.UShortValue) + 5459);
server.Broadcast("UPDATE:\n VOLUME = " + Volume.UShortValue + "\n\n");
} else if(args.Button.Name == eButtonName.VolumeDown) {
if(Volume != null) Volume.UShortValue = (ushort) Math.Max(0, ((int) Volume.UShortValue) - 5459);
server.Broadcast("UPDATE:\n VOLUME = " + Volume.UShortValue + "\n\n");
}
}
private ushort prevVolume = 0;
void SigChange(BasicTriList currentDevice, SigEventArgs args) {
if(this.MPC3x30xTouchscreenSlot != null) {
if(args.Sig.Type == eSigType.UShort && args.Sig.Number == this.MPC3x30xTouchscreenSlot.Volume.Number) {
this.MPC3x30xTouchscreenSlot.VolumeFeedback.UShortValue = this.MPC3x30xTouchscreenSlot.Volume.UShortValue;
if(args.Sig.UShortValue != prevVolume)
server.Broadcast("UPDATE:\n VOLUME = " + args.Sig.UShortValue + "\n\n");
prevVolume = args.Sig.UShortValue;
return;
}
}
}
public String stateSummary() {
if(this.panel == null) { return ""; }
return TriListSerializer.summarize(this.panel);
}
void ControlSystem_ControllerEthernetEventHandler(EthernetEventArgs ethernetEventArgs) {
switch(ethernetEventArgs.EthernetEventType) {
case (eEthernetEventType.LinkDown):
if(ethernetEventArgs.EthernetAdapter == EthernetAdapterType.EthernetLANAdapter) {
server.Stop();
}
break;
case (eEthernetEventType.LinkUp):
if(ethernetEventArgs.EthernetAdapter == EthernetAdapterType.EthernetLANAdapter) {
server.Start();
}
break;
}
}
void ControlSystem_ControllerProgramEventHandler(eProgramStatusEventType programStatusEventType) {
if(programStatusEventType == eProgramStatusEventType.Stopping) {
server.Stop();
}
}
}
}