Skip to content

Commit bbd3c3b

Browse files
committed
client: Dispatch commands (still blocking).
1 parent 1a9c23d commit bbd3c3b

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

VisualPinball.Engine.Mpf/MpfClient.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
using System;
1414
using System.Collections.Generic;
1515
using System.Threading.Tasks;
16-
using Google.Protobuf.Collections;
16+
using Grpc.Core.Utils;
1717
using Mpf.Vpe;
18+
using NLog;
1819

1920
namespace VisualPinball.Engine.Mpf
2021
{
@@ -23,11 +24,22 @@ namespace VisualPinball.Engine.Mpf
2324
/// </summary>
2425
public class MpfClient
2526
{
27+
public event EventHandler<FadeLightRequest> OnFadeLight;
28+
public event EventHandler<PulseCoilRequest> OnPulseCoil;
29+
public event EventHandler<EnableCoilRequest> OnEnableCoil;
30+
public event EventHandler<DisableCoilRequest> OnDisableCoil;
31+
public event EventHandler<ConfigureHardwareRuleRequest> OnConfigureHardwareRule;
32+
public event EventHandler<RemoveHardwareRuleRequest> OnRemoveHardwareRule;
33+
2634
private Channel _channel;
2735
private MpfHardwareService.MpfHardwareServiceClient _client;
2836

37+
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
38+
39+
2940
public void Connect(string serverIpPort = "127.0.0.1:50051")
3041
{
42+
Logger.Info($"Connecting to {serverIpPort}...");
3143
_channel = new Channel(serverIpPort, ChannelCredentials.Insecure);
3244
_client = new MpfHardwareService.MpfHardwareServiceClient(_channel);
3345
}
@@ -38,11 +50,50 @@ public void StartGame(Dictionary<string, bool> initialSwitches)
3850
foreach (var sw in initialSwitches.Keys) {
3951
ms.InitialSwitchStates.Add(sw, initialSwitches[sw]);
4052
}
41-
_client.Start(ms);
53+
54+
Logger.Info("Starting client...");
55+
using (var call = _client.Start(ms)) {
56+
57+
Logger.Info("Client started, retrieving commands...");
58+
var count = 0;
59+
call.ResponseStream.ForEachAsync(commands => {
60+
61+
Logger.Info($"New command: {commands.CommandCase}");
62+
count++;
63+
switch (commands.CommandCase) {
64+
case Commands.CommandOneofCase.None:
65+
break;
66+
case Commands.CommandOneofCase.FadeLight:
67+
OnFadeLight?.Invoke(this, commands.FadeLight);
68+
break;
69+
case Commands.CommandOneofCase.PulseCoil:
70+
OnPulseCoil?.Invoke(this, commands.PulseCoil);
71+
break;
72+
case Commands.CommandOneofCase.EnableCoil:
73+
OnEnableCoil?.Invoke(this, commands.EnableCoil);
74+
break;
75+
case Commands.CommandOneofCase.DisableCoil:
76+
OnDisableCoil?.Invoke(this, commands.DisableCoil);
77+
break;
78+
case Commands.CommandOneofCase.ConfigureHardwareRule:
79+
OnConfigureHardwareRule?.Invoke(this, commands.ConfigureHardwareRule);
80+
break;
81+
case Commands.CommandOneofCase.RemoveHardwareRule:
82+
OnRemoveHardwareRule?.Invoke(this, commands.RemoveHardwareRule);
83+
break;
84+
default:
85+
throw new ArgumentOutOfRangeException();
86+
}
87+
return Task.CompletedTask;
88+
}).Wait();
89+
90+
Logger.Info($"{count} commands dispatched.");
91+
}
4292
}
4393

4494
public MachineDescription GetMachineDescription()
4595
{
96+
Logger.Info($"Getting machine description...");
4697
return _client.GetMachineDescription(new EmptyRequest());
4798
}
4899

0 commit comments

Comments
 (0)