コード例 #1
0
        private void OnWebSocketMessageReceive(object sender, MessageEventArgs e)
        {
            if (!e.IsText)
            {
                return;
            }
            var typeJson = JsonUtility.FromJson <JsonData.GrayBlueJson <object> >(e.Data);

            switch (typeJson.Type)
            {
            case JsonData.JsonType.Result:
                var result = JsonUtility.FromJson <JsonData.GrayBlueJson <JsonData.MethodResult> >(e.Data).Content;
                requestAgent?.ExtractResultJson(result);
                break;

            case JsonData.JsonType.DeviceStateChange:
                var device = JsonUtility.FromJson <JsonData.GrayBlueJson <JsonData.Device> >(e.Data).Content;
                if (device.State == "Lost")
                {
                    context?.Post(_ => {
                        connectDelegate?.OnConnectLost(device.DeviceId);
                    }, null);
                }
                break;

            case JsonData.JsonType.NotifyIMU:
                var imuJson = JsonUtility.FromJson <JsonData.GrayBlueJson <JsonData.IMU> >(e.Data).Content;
                var imuData = JsonDataExtractor.ToIMUNotifyData(imuJson);
                context?.Post(_ => {
                    notifyDelegate?.OnIMUDataUpdate(imuData.deviceId, imuData.acc, imuData.gyro, imuData.mag, imuData.quat);
                }, null);
                break;

            case JsonData.JsonType.NotifyButton:
                var btnJson = JsonUtility.FromJson <JsonData.GrayBlueJson <JsonData.Button> >(e.Data).Content;
                var btnData = JsonDataExtractor.ToButtonNotifyData(btnJson);
                context?.Post(_ => {
                    if (btnData.isPress)
                    {
                        notifyDelegate?.OnButtonPush(btnData.deviceId, btnData.button);
                    }
                    else
                    {
                        notifyDelegate?.OnButtonRelease(btnData.deviceId, btnData.button, btnData.time);
                    }
                }, null);
                break;

            default:
                // Do Nothing
                break;
            }
        }
コード例 #2
0
ファイル: Plugin.cs プロジェクト: naninunenoy/GrayBlue_UWP
 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);
 }