public WebSocketProxy(string host, int port,
                       IConnectionDelegate connectDelegate, INotifyDelegate notifyDelegate)
 {
     URL                  = $"ws://{host}:{port}/";
     webSocket            = new WebSocketSharp.WebSocket(URL);
     this.connectDelegate = connectDelegate;
     this.notifyDelegate  = notifyDelegate;
     requestAgent         = new RequestAgent();
 }
Beispiel #2
0
 public async Task <bool> ConnectTo(string deviceId, IConnectionDelegate connectionDelegate, INotifyDelegate notifyDelegate)
 {
     BLE.IIMUNotifyDevice device = new BLE.IMUDevice(deviceId);
     try {
         device = await device.ConnectionAsync();
     } catch (BLE.BLEException e) {
         Debug.WriteLine($"{deviceId} connect failed. {e.Message}");
         connectionDelegate?.OnConnectFail(deviceId);
         device.Dispose();
         return(false);
     }
     device.ConnectionLostObservable()
     .Subscribe(_ => { connectionDelegate?.OnConnectLost(deviceId); });
     device.ButtonUpdateObservable()
     .Subscribe(data => {
         bool press = BitConverter.ToBoolean(data, 0);
         char name  = (char)data[1];
         short ms   = BitConverter.ToInt16(data, 2);
         float time = ms / 1000.0F;
         if (press)
         {
             notifyDelegate?.OnButtonPush(deviceId, name.ToString());
         }
         else
         {
             notifyDelegate?.OnButtonRelease(deviceId, name.ToString(), time);
         }
     });
     device.IMUUpdateObservable()
     .Subscribe(data => {
         var acc = new float[3] {
             BitConverter.ToSingle(data, 0),
             BitConverter.ToSingle(data, 4),
             BitConverter.ToSingle(data, 8)
         };
         var gyro = new float[3] {
             BitConverter.ToSingle(data, 12),
             BitConverter.ToSingle(data, 16),
             BitConverter.ToSingle(data, 20)
         };
         var mag = new float[3] {
             BitConverter.ToSingle(data, 24),
             BitConverter.ToSingle(data, 28),
             BitConverter.ToSingle(data, 32)
         };
         var quat = new float[4] {
             BitConverter.ToSingle(data, 36),
             BitConverter.ToSingle(data, 40),
             BitConverter.ToSingle(data, 44),
             BitConverter.ToSingle(data, 48)
         };
         notifyDelegate?.OnIMUDataUpdate(deviceId, acc, gyro, mag, quat);
     });
     connectionDelegate?.OnConnectDone(deviceId);
     if (deviceDict.ContainsKey(deviceId))
     {
         // overwrite
         deviceDict[deviceId].Dispose();
         deviceDict[deviceId] = device;
     }
     else
     {
         deviceDict.Add(deviceId, device);
     }
     return(true);
 }
Beispiel #3
0
 public Task <bool> ConnectTo(string deviceId, IConnectionDelegate connectionDelegate, INotifyDelegate notifyDelegate)
 {
     return(Task.FromResult(false));
 }