12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEngine;
- namespace Sensors.Polar
- {
- public class UdpConnection
- {
- //public int port = 9090;
- //public String ipAddress = "192.168.1.7";
- private UdpClient client;
- private bool listening = false;
- private Action<AccData> onAccData;
- private Action<EcgData> onEcgData;
- public UdpConnection(String ipAddress, int port, Action<AccData> onAccData, Action<EcgData> onEcgData)
- {
- this.onAccData = onAccData;
- this.onEcgData = onEcgData;
- var parsedAddress = IPAddress.Parse(ipAddress);
- client = new UdpClient(new IPEndPoint(parsedAddress, port));
- }
- public void Listen() => Task.Run(async () =>
- {
- listening = true;
- while (listening)
- {
- var result = await client.ReceiveAsync();
- string receivedStr = Encoding.UTF8.GetString(result.Buffer);
- var data = receivedStr.Split(':');
- if (data.Length == 2)
- {
- var name = data[0];
- if (name.Equals("ACC"))
- {
- onAccData(AccData.FromString(data[1]));
- }else if (name.Equals("ECG"))
- {
- onEcgData(EcgData.FromString(data[1]));
- }else if (name.Equals("HR"))
- {
- //TODO
- }
-
- }
-
- //Debug.Log($"RECEIVED DATA VIA UDP: {receivedStr}");
- }
- });
- public void StopListening()
- {
- listening = false;
- }
-
- }
- }
|