13
13
using System ;
14
14
using System . Collections . Generic ;
15
15
using System . Threading . Tasks ;
16
- using Google . Protobuf . Collections ;
16
+ using Grpc . Core . Utils ;
17
17
using Mpf . Vpe ;
18
+ using NLog ;
18
19
19
20
namespace VisualPinball . Engine . Mpf
20
21
{
@@ -23,11 +24,22 @@ namespace VisualPinball.Engine.Mpf
23
24
/// </summary>
24
25
public class MpfClient
25
26
{
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
+
26
34
private Channel _channel ;
27
35
private MpfHardwareService . MpfHardwareServiceClient _client ;
28
36
37
+ private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
38
+
39
+
29
40
public void Connect ( string serverIpPort = "127.0.0.1:50051" )
30
41
{
42
+ Logger . Info ( $ "Connecting to { serverIpPort } ...") ;
31
43
_channel = new Channel ( serverIpPort , ChannelCredentials . Insecure ) ;
32
44
_client = new MpfHardwareService . MpfHardwareServiceClient ( _channel ) ;
33
45
}
@@ -38,11 +50,50 @@ public void StartGame(Dictionary<string, bool> initialSwitches)
38
50
foreach ( var sw in initialSwitches . Keys ) {
39
51
ms . InitialSwitchStates . Add ( sw , initialSwitches [ sw ] ) ;
40
52
}
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
+ }
42
92
}
43
93
44
94
public MachineDescription GetMachineDescription ( )
45
95
{
96
+ Logger . Info ( $ "Getting machine description...") ;
46
97
return _client . GetMachineDescription ( new EmptyRequest ( ) ) ;
47
98
}
48
99
0 commit comments