Exemplo n.º 1
0
        private void TGMockDataGenerator()
        {
            var TGDataRow = new TGData();
            var Rand      = new Random((int)DateTime.Now.Ticks);

            // Generate Randome Data 30%
            if (Rand.Next(1, 101) < 30)
            {
                TGDataRow.PoorSignal = Rand.Next(0, 30);
            }
            if (Rand.Next(1, 101) < 30)
            {
                TGDataRow.Attention = Rand.Next(0, 101);
            }
            if (Rand.Next(1, 101) < 30)
            {
                TGDataRow.Meditation = Rand.Next(0, 101);
            }

            // Generate Randome Data 10%
            if (Rand.Next(1, 101) < 10)
            {
                TGDataRow.BlinkStrength = Rand.Next(0, 256);
            }
            if (Rand.Next(1, 101) < 10)
            {
                TGDataRow.TaskFamiliarity = Rand.Next(-1000, 0);
            }
            if (Rand.Next(1, 101) < 10)
            {
                TGDataRow.TaskDifficulty = Rand.Next(0, 1000);
            }
            if (Rand.Next(1, 101) < 10)
            {
                TGDataRow.MentalEffort = Rand.Next(0, 1000);
            }

            // Generate Randome in every row
            TGDataRow.EegPowerDelta  = Rand.Next(0, 2000000);
            TGDataRow.EegPowerTheta  = Rand.Next(0, 1000000);
            TGDataRow.EegPowerAlpha1 = Rand.Next(0, 200000);
            TGDataRow.EegPowerAlpha2 = Rand.Next(0, 200000);
            TGDataRow.EegPowerBeta1  = Rand.Next(0, 100000);
            TGDataRow.EegPowerBeta2  = Rand.Next(0, 50000);
            TGDataRow.EegPowerGamma1 = Rand.Next(0, 50000);
            TGDataRow.EegPowerGamma2 = Rand.Next(0, 100000);

            // Add TGDataRow to Queue
            TGQueue.Enqueue(TGDataRow);
            this.RefreshUI(TGDataRow);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Refreshes the UI.
        /// </summary>
        /// <param name="currentTGData">The current tg data.</param>
        private void RefreshUI(TGData currentTGData)
        {
            this.Dispatcher.Invoke(() =>
            {
                try
                {
                    // Gauge Signal Quality
                    // هر هنگام که اطلاعات جدیدی بیاید مقدار را به روزآوری می کنیم
                    if (currentTGData.PoorSignal != -1)
                    {
                        var SignalQuality = Math.Round((200D - currentTGData.PoorSignal) / 2D, 1);
                        if (SignalQuality < 0)
                        {
                            SignalQuality = 0;
                        }
                        GaugeSignal.Value       = SignalQuality;
                        TGLatestData.PoorSignal = currentTGData.PoorSignal;
                    }

                    // Gauge ATTENTION/MEDITATION
                    if (currentTGData.Attention != -1)
                    {
                        GaugeAttention.Value = TGLatestData.Attention = currentTGData.Attention;
                    }
                    if (currentTGData.Meditation != -1)
                    {
                        GaugeMeditation.Value = TGLatestData.Meditation = currentTGData.Meditation;
                    }

                    // AngularGauges: BlinkStrength
                    if (currentTGData.BlinkStrength != -1)
                    {
                        GaugeBlinkStrength.Value   = (currentTGData.BlinkStrength / 255D * 100D);
                        GaugeBlinkStrength.Tag     = _RESET_WAITING_STEPS;
                        TGLatestData.BlinkStrength = currentTGData.BlinkStrength;
                    }
                    if (GaugeBlinkStrength.Tag != null)
                    {
                        var CleanTime = (int)GaugeBlinkStrength.Tag - 1;
                        if (CleanTime == 0)
                        {
                            GaugeBlinkStrength.Tag        = null;
                            GaugeBlinkStrength.Value      = 0;
                            GaugeBlinkStrength.NeedleFill = Brushes.Black;
                        }
                        else
                        {
                            GaugeBlinkStrength.Tag        = CleanTime;
                            GaugeBlinkStrength.NeedleFill = Brushes.Gold;
                        }
                    }

                    // AngularGauges: TaskFamiliarity
                    if (currentTGData.TaskFamiliarity != 0)
                    {
                        GaugeTaskFamiliarity.Value   = currentTGData.TaskFamiliarity;
                        GaugeTaskFamiliarity.Tag     = _RESET_WAITING_STEPS;
                        TGLatestData.TaskFamiliarity = currentTGData.TaskFamiliarity;
                    }
                    if (GaugeTaskFamiliarity.Tag != null)
                    {
                        var CleanTime = (int)GaugeTaskFamiliarity.Tag - 1;
                        if (CleanTime == 0)
                        {
                            GaugeTaskFamiliarity.Tag        = null;
                            GaugeTaskFamiliarity.Value      = 0;
                            GaugeTaskFamiliarity.NeedleFill = Brushes.Black;
                        }
                        else
                        {
                            GaugeTaskFamiliarity.Tag        = CleanTime;
                            GaugeTaskFamiliarity.NeedleFill = Brushes.Gold;
                        }
                    }

                    // AngularGauges: MentalEffort
                    if (currentTGData.MentalEffort != 0)
                    {
                        GaugeMentalEffort.Value   = currentTGData.MentalEffort;
                        GaugeMentalEffort.Tag     = _RESET_WAITING_STEPS;
                        TGLatestData.MentalEffort = currentTGData.MentalEffort;
                    }
                    if (GaugeMentalEffort.Tag != null)
                    {
                        var CleanTime = (int)GaugeMentalEffort.Tag - 1;
                        if (CleanTime == 0)
                        {
                            GaugeMentalEffort.Tag        = null;
                            GaugeMentalEffort.Value      = 0;
                            GaugeMentalEffort.NeedleFill = Brushes.Black;
                        }
                        else
                        {
                            GaugeMentalEffort.Tag        = CleanTime;
                            GaugeMentalEffort.NeedleFill = Brushes.Gold;
                        }
                    }

                    // Chart Data / EEG Data
                    // اطلاعات را ابتدا تبدیل به ساختارهای مناسب برای چارت کرده و سپس آن را یکجا به روزآوری می کنیم
                    if (currentTGData.EegPowerDelta != 0)
                    {
                        var ListLineSeries = new List <LineSeries>();
                        ListLineSeries.Add(new LineSeries()
                        {
                            Title = "Delta Power", Values = new ChartValues <double>(TGQueue.Select(q => q.EegPowerDelta).ToArray()), PointGeometry = null,
                        });
                        ListLineSeries.Add(new LineSeries()
                        {
                            Title = "Theta Power", Values = new ChartValues <double>(TGQueue.Select(q => q.EegPowerTheta).ToArray()), PointGeometry = null,
                        });
                        ListLineSeries.Add(new LineSeries()
                        {
                            Title = "Low Alpha Power", Values = new ChartValues <double>(TGQueue.Select(q => q.EegPowerAlpha1).ToArray()), PointGeometry = null,
                        });
                        ListLineSeries.Add(new LineSeries()
                        {
                            Title = "High Alpha Power", Values = new ChartValues <double>(TGQueue.Select(q => q.EegPowerAlpha2).ToArray()), PointGeometry = null,
                        });
                        ListLineSeries.Add(new LineSeries()
                        {
                            Title = "Low Beta Power", Values = new ChartValues <double>(TGQueue.Select(q => q.EegPowerBeta1).ToArray()), PointGeometry = null,
                        });
                        ListLineSeries.Add(new LineSeries()
                        {
                            Title = "High Beta Power", Values = new ChartValues <double>(TGQueue.Select(q => q.EegPowerBeta2).ToArray()), PointGeometry = null,
                        });
                        ListLineSeries.Add(new LineSeries()
                        {
                            Title = "Low Gamma Power", Values = new ChartValues <double>(TGQueue.Select(q => q.EegPowerGamma1).ToArray()), PointGeometry = null,
                        });
                        ListLineSeries.Add(new LineSeries()
                        {
                            Title = "High Gamma Power", Values = new ChartValues <double>(TGQueue.Select(q => q.EegPowerGamma2).ToArray()), PointGeometry = null,
                        });
                        var TGChartSeriesCollection = new SeriesCollection();
                        TGChartSeriesCollection.AddRange(ListLineSeries);
                        this.CartesianChartWaves.AxisX[0].Labels         = TGQueue.Select(q => q.TimeStamp.ToString()).ToArray();
                        this.CartesianChartWaves.AxisY[0].LabelFormatter = value => value.ToString("N0");
                        this.CartesianChartWaves.Series = TGChartSeriesCollection;

                        // به روزآوری اطلاعات
                        TGLatestData.EegPowerDelta  = currentTGData.EegPowerDelta;
                        TGLatestData.EegPowerTheta  = currentTGData.EegPowerTheta;
                        TGLatestData.EegPowerAlpha1 = currentTGData.EegPowerAlpha1;
                        TGLatestData.EegPowerAlpha2 = currentTGData.EegPowerAlpha2;
                        TGLatestData.EegPowerBeta1  = currentTGData.EegPowerBeta1;
                        TGLatestData.EegPowerBeta2  = currentTGData.EegPowerBeta2;
                        TGLatestData.EegPowerGamma1 = currentTGData.EegPowerGamma1;
                        TGLatestData.EegPowerGamma2 = currentTGData.EegPowerGamma2;
                    }

                    // اطلاعاتی که در مورد اقدامی انجام نشده است
                    if (currentTGData.TaskDifficulty != 0)
                    {
                        TGLatestData.TaskDifficulty = currentTGData.TaskDifficulty;
                    }
                    if (currentTGData.RawCh1 != 0)
                    {
                        TGLatestData.RawCh1 = currentTGData.RawCh1;
                    }
                    if (currentTGData.RawCh2 != 0)
                    {
                        TGLatestData.RawCh2 = currentTGData.RawCh2;
                    }
                    if (currentTGData.RawCh3 != 0)
                    {
                        TGLatestData.RawCh3 = currentTGData.RawCh3;
                    }
                    if (currentTGData.RawCh4 != 0)
                    {
                        TGLatestData.RawCh4 = currentTGData.RawCh4;
                    }
                    if (currentTGData.RawCh5 != 0)
                    {
                        TGLatestData.RawCh5 = currentTGData.RawCh5;
                    }
                    if (currentTGData.RawCh6 != 0)
                    {
                        TGLatestData.RawCh6 = currentTGData.RawCh6;
                    }
                    if (currentTGData.RawCh7 != 0)
                    {
                        TGLatestData.RawCh7 = currentTGData.RawCh7;
                    }
                    if (currentTGData.RawCh8 != 0)
                    {
                        TGLatestData.RawCh8 = currentTGData.RawCh8;
                    }
                    if (currentTGData.MSG_ERR_CFG_OVERRIDE != 0)
                    {
                        TGLatestData.MSG_ERR_CFG_OVERRIDE = currentTGData.MSG_ERR_CFG_OVERRIDE;
                    }
                    if (currentTGData.MSG_ERR_NOT_PROVISIONED != 0)
                    {
                        TGLatestData.MSG_ERR_NOT_PROVISIONED = currentTGData.MSG_ERR_NOT_PROVISIONED;
                    }
                    if (currentTGData.MSG_MODEL_IDENTIFIED != 0)
                    {
                        TGLatestData.MSG_MODEL_IDENTIFIED = currentTGData.MSG_MODEL_IDENTIFIED;
                    }
                    if (currentTGData.RespiratoryRate != 0)
                    {
                        TGLatestData.RespiratoryRate = currentTGData.TaskDifficulty;
                    }
                    if (currentTGData.Positivity)
                    {
                        TGLatestData.Positivity = currentTGData.Positivity;
                    }

                    // Update TextLog | نمایش متنی
                    var TextLog = new StringBuilder();
                    TextLog.AppendLine("آخرین اطلاعات دریافتی");
                    typeof(TGData).GetProperties().ToList().ForEach(p => TextLog.AppendLine($"{p.Name}: {(p.GetValue(this.TGLatestData)?.ToString() ?? "null")}"));
                    this.TextBlockLog.Text = TextLog.ToString();
                }
                catch (Exception ex)
                {
                    this.LogAdd("Exception: RefreshUI");
                    this.LogAdd(ex.Message);
                }
            }, DispatcherPriority.ContextIdle);
        }
Exemplo n.º 3
0
        private void TGConnector_DataReceived(object sender, EventArgs e)
        {
            Device device = (Device)sender;

            // Parse Data #1
            var TGParser = new TGParser();

            TGParser.Read((e as Device.DataEventArgs).DataRowArray);

            // Parse Data #2
            var TGDataRow        = new TGData();
            var TGDataProperties = typeof(TGData).GetProperties();

            for (int i = 0; i < TGParser.ParsedData.Length; i++)
            {
                foreach (var Key in TGParser.ParsedData[i].Keys)
                {
                    if (Key == "Time")
                    {
                        continue;
                    }
                    if (Key == "Raw")
                    {
                        continue;
                    }
                    var PropertyInfo = TGDataProperties.Where(prop => prop.Name == Key).FirstOrDefault();
                    if (PropertyInfo == null)
                    {
                        Debug.Assert(false, "اطلاعات به صورت کامل ترجمه نشده است");
                        continue;
                    }
                    PropertyInfo.SetValue(TGDataRow, TGParser.ParsedData[i][PropertyInfo.Name]);

                    // IMP: This algorithm is resource and computation intensive.
                    // If you need to run with the Debugger, be aware that this calculation may take many minutes to complete
                    // when the debugger is engaged. It will output its results only after its calculations are complete.
                    //
                    // If these methods are called before the MSG_MODEL_IDENTIFIED has been received,
                    // it is considered a request to be processed when the connected equipment is identified.
                    // It is possible to Enable this feature and later find that it is no longer enabled.
                    // Once the connected equipment has been identified, if the request is incompatible with the hardware
                    // or software it will be overridden and the MSG_ERR_CFG_OVERRIDE message sent to provide notification.
                    if (Key == "MSG_MODEL_IDENTIFIED")
                    {
                        LogAdd("MSG_MODEL_IDENTIFIED Recevied...");
                        TGConnector.setMentalEffortRunContinuous(true);
                        TGConnector.setMentalEffortEnable(true);
                        TGConnector.setTaskFamiliarityRunContinuous(true);
                        TGConnector.setTaskFamiliarityEnable(true);
                        TGConnector.setPositivityEnable(false);

                        // the following are included to demonstrate the overide messages
                        TGConnector.setRespirationRateEnable(true); // not allowed with EEG
                        TGConnector.setPositivityEnable(true);      // not allowed when famil/diff are enabled
                    }

                    // If these methods are called before the MSG_MODEL_IDENTIFIED has been received,
                    // it is considered a request to be processed when the connected equipment is identified.
                    // It is possible to Enable this feature and later find that it is no longer enabled.
                    // Once the connected equipment has been identified, if the request is incompatible with the hardware
                    // or software it will be overridden and the MSG_ERR_CFG_OVERRIDE message sent to provide notification.
                    if (TGParser.ParsedData[i].ContainsKey("MSG_ERR_CFG_OVERRIDE"))
                    {
                        LogAdd($"ErrorConfigurationOverride: {TGParser.ParsedData[i]["MSG_ERR_CFG_OVERRIDE"]} Recevied...");
                    }
                    if (TGParser.ParsedData[i].ContainsKey("MSG_ERR_NOT_PROVISIONED"))
                    {
                        LogAdd($"ErrorModuleNotProvisioned: {TGParser.ParsedData[i]["MSG_ERR_NOT_PROVISIONED"]} Recevied...");
                    }
                }
            }

            // Add TGDataRow to Queue
            if (TGDataRow.EegPowerDelta != 0)
            {
                TGQueue.Enqueue(TGDataRow);
            }
            this.RefreshUI(TGDataRow);
        }