Beispiel #1
0
    private void MainSignalChange(Signal signal)
    {
        switch ((SignalType)signal.type)
        {
        case SignalType.UNKNOWN:
            UpdateUnknownSignalEvent?.Invoke(signal.value);
            break;

        case SignalType.STATE_CHANGE:
            UpdateStateChangeSignalEvent?.Invoke(signal.value);
            break;

        case SignalType.POOR_SIGNAL:
            UpdatePoorSignalEvent?.Invoke(signal.value);
            break;

        case SignalType.ATTENTION:
            UpdateAttentionEvent?.Invoke(signal.value);
            break;

        case SignalType.MEDITATION:
            UpdateMeditationEvent?.Invoke(signal.value);
            break;

        case SignalType.BLINK:
            UpdateBlinkEvent?.Invoke(signal.value);
            break;

        case SignalType.SLEEP_STAGE:
            UpdateSleepStageEvent?.Invoke(signal.value);
            break;

        case SignalType.LOW_BATTERY:
            UpdateLowBatteryEvent?.Invoke(signal.value);
            break;

        case SignalType.RAW_COUNT:
            UpdateRawCountEvent?.Invoke(signal.value);
            break;

        case SignalType.RAW_DATA:
            UpdateRawDataEvent?.Invoke(signal.value);
            break;

        case SignalType.HEART_RATE:
            UpdateHeartRateEvent?.Invoke(signal.value);
            break;

        case SignalType.RAW_MULTI:
            UpdateRawMultiEventl?.Invoke(signal.value);
            break;

        case SignalType.EEG_POWER:
            UpdateEEGPowerEvent?.Invoke(signal.value);
            break;
        }
    }
        private void ParseData()
        {
            if (stream.CanRead)
            {
                try
                {
                    int bytesRead = stream.Read(buffer, 0, buffer.Length);

                    List <PackatData> packets = new List <PackatData>();

                    String packet = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                    if (!string.IsNullOrEmpty(packet))
                    {
                        Debug.Log(packet);
                        if (packet.Contains("}"))
                        {
                            int count = GetObjectCount(packet);
                            if (count == 1)
                            {
                                PackatData data = JsonMapper.ToObject <PackatData>(packet);
                                packets.Add(data);
                            }
                            else if (count > 1)
                            {
                                PackatData[] data = JsonMapper.ToObject <PackatData[]>(packet);
                                for (int index = 0; index < data.Length; ++index)
                                {
                                    packets.Add(data[index]);
                                }
                            }
                        }
                    }

                    foreach (PackatData data in packets)
                    {
                        if (null == data)
                        {
                            continue;
                        }
                        if (data.poorSignalLevel != 0)
                        {
                            Debug.Log("data.poorSignalLevel: " + data.poorSignalLevel);
                            if (null != UpdatePoorSignalEvent)
                            {
                                UpdatePoorSignalEvent.Invoke(data.poorSignalLevel);
                            }

                            if (null != data.eSense)
                            {
                                if (UpdateAttentionEvent != null)
                                {
                                    UpdateAttentionEvent(data.eSense.attention);
                                }
                                if (UpdateMeditationEvent != null)
                                {
                                    UpdateMeditationEvent(data.eSense.meditation);
                                }

                                if (null != data.eSense.eegPower)
                                {
                                    if (UpdateDeltaEvent != null)
                                    {
                                        UpdateDeltaEvent(data.eSense.eegPower.delta);
                                    }
                                    if (UpdateThetaEvent != null)
                                    {
                                        UpdateThetaEvent(data.eSense.eegPower.theta);
                                    }
                                    if (UpdateLowAlphaEvent != null)
                                    {
                                        UpdateLowAlphaEvent(data.eSense.eegPower.lowAlpha);
                                    }
                                    if (UpdateHighAlphaEvent != null)
                                    {
                                        UpdateHighAlphaEvent(data.eSense.eegPower.highAlpha);
                                    }
                                    if (UpdateLowBetaEvent != null)
                                    {
                                        UpdateLowBetaEvent(data.eSense.eegPower.lowBeta);
                                    }
                                    if (UpdateHighBetaEvent != null)
                                    {
                                        UpdateHighBetaEvent(data.eSense.eegPower.highBeta);
                                    }
                                    if (UpdateLowGammaEvent != null)
                                    {
                                        UpdateLowGammaEvent(data.eSense.eegPower.lowGamma);
                                    }
                                    if (UpdateHighGammaEvent != null)
                                    {
                                        UpdateHighGammaEvent(data.eSense.eegPower.highGamma);
                                    }
                                }
                            }
                        }
                        else if (data.rawEeg != 0)
                        {
                            if (null != UpdateRawdataEvent)
                            {
                                UpdateRawdataEvent(data.rawEeg);
                            }
                        }
                        else if (data.blinkStrength != 0)
                        {
                            if (null != UpdateRawdataEvent)
                            {
                                UpdateBlinkEvent(data.blinkStrength);
                            }
                        }
                    }
                }
                catch (IOException e)
                {
                    Debug.Log("IOException " + e);
                }
                catch (System.Exception e)
                {
                    Debug.Log("Exception " + e);
                }
            }
        } // end ParseData