-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtobufCommunication.cs
158 lines (142 loc) · 5.06 KB
/
ProtobufCommunication.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
using Google.Protobuf;
using Naki3D.Common.Protocol;
using nuitrack;
using SensorServer.ProjectorControl;
using System;
using System.Net.Sockets;
namespace SensorServer
{
class ProtobufCommunication
{
private readonly string _ip;
private readonly int _port;
private TcpClient _tcpClient;
private NetworkStream _networkStream;
private IProjectorController _projectorController;
private bool _finished = false;
public IProjectorController ProjectorController { get => _projectorController; set => _projectorController = value; }
public ProtobufCommunication(string ip, int port)
{
_ip = ip;
_port = port;
}
public void Connect()
{
while (true)
{
if (_finished) break;
try
{
_tcpClient = new TcpClient(_ip, _port);
_networkStream = _tcpClient.GetStream();
break;
}
catch (System.Exception e)
{
Console.WriteLine($"Connection failed: {e.Message}\nRetry in 1s.");
}
System.Threading.Thread.Sleep(1000);
}
}
public void Dispose()
{
if (_networkStream != null) _networkStream.Close();
if ( _tcpClient != null) _tcpClient.Close();
}
/// <summary>
/// Send gesture data if gesture is detected
/// </summary>
/// <param name="sensorId">ID of the sensor that detected the gesture</param>
/// <param name="timestamp">Time when the gesture was detected</param>
/// <param name="gesture">Gesture type (swipe left, swipe right, swipe up, swipe down)</param>
public void SendGestureData(string sensorId, ulong timestamp, Gesture gesture)
{
Naki3D.Common.Protocol.GestureData gestureData = new()
{
UserId = gesture.UserID,
Type = (Naki3D.Common.Protocol.GestureType)gesture.Type
};
SensorMessage message = new()
{
SensorId = sensorId,
Timestamp = timestamp,
Gesture = gestureData
};
System.Console.WriteLine(gesture.Type);
SendMessage(message);
}
/// <summary>
/// Send hand position
/// </summary>
/// <param name="sensorId">ID of the sensor that detected the gesture</param>
/// <param name="userId">ID of the user</param>
/// <param name="timestamp">Time when the gesture was detected</param>
/// <param name="handType">Hand type (left, right)</param>
/// <param name="hand">Hand data</param>
public void SendHandMovement(string sensorId, int userId, ulong timestamp, HandType handType, HandContent hand)
{
Naki3D.Common.Protocol.Vector3 vector3 = new()
{
X = hand.X,
Y = hand.Y,
Z = hand.ZReal
};
HandMovementData handMovementData = new()
{
Hand = handType,
ProjPosition = vector3,
OpenHand = !hand.Click,
UserId = userId
};
SensorMessage message = new()
{
SensorId = sensorId,
Timestamp = timestamp,
HandMovement = handMovementData
};
SendMessage(message);
}
private void SendMessage(SensorMessage sensorMessage)
{
sensorMessage.WriteDelimitedTo(_networkStream);
_networkStream.Flush();
}
public void Start()
{
while (!_finished)
{
try
{
if (!_tcpClient.Connected)
{
break;
}
SensorControlMessage message = SensorControlMessage.Parser.ParseDelimitedFrom(_networkStream);
Console.WriteLine(message.CecMessage);
switch (message.CecMessage.Action)
{
case CECAction.PowerOn:
_projectorController?.PowerOn();
break;
case CECAction.PowerOff:
_projectorController?.PowerOff();
break;
}
}
catch(System.Exception e)
{
Console.WriteLine($"Invalid message: {e.Message}\n Assuming disconnected socket...");
break;
}
}
}
public void Stop()
{
_finished = true;
}
public bool IsConnected()
{
return _tcpClient.Connected;
}
}
}