예제 #1
0
        private async void StartTrace()
        {
            traceModel.StopTrace().Wait();
            traceModel.StartTrace().Wait();
            VSenseCollection.Clear();
            ISenseCollection.Clear();
            IRefCollection.Clear();
            TemperatureFourCollection.Clear();
            TemperatureThreeCollection.Clear();
            TemperatureTwoCollection.Clear();
            TemperatureOneCollection.Clear();

            cts   = new CancellationTokenSource();
            token = cts.Token;

            var task = Task.Factory.StartNew(() =>
            {
                TraceSamples();
            }, token);
        }
예제 #2
0
        private async void TraceSamples()
        {
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            var    modValue       = 1000;
            var    samplesToClear = 0;
            double sampledTime    = 0;
            double sampleNumber   = 0;
            var    updateRanges   = 0;
            var    m = 10;

            while (true)
            {
                //var watch = System.Diagnostics.Stopwatch.StartNew();

                if (token.IsCancellationRequested == true)
                {
                    await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        SampleMinimumX = 0;
                        SampleMaximumX = 1;
                        VSenseCollection.Clear();
                        ISenseCollection.Clear();
                        IRefCollection.Clear();
                        TemperatureFourCollection.Clear();
                        TemperatureThreeCollection.Clear();
                        TemperatureTwoCollection.Clear();
                        TemperatureOneCollection.Clear();
                    }));

                    break;
                }

                var numberOfAvailableSamples = traceModel.GetNumberOfAvailableSamples().Result;
                if (numberOfAvailableSamples.succesfulResponse)
                {
                    var availableSamples     = Helper.GetIntFromBigEndian(numberOfAvailableSamples.response);
                    var firstAvailableSample = traceModel.GetFirstAvailableSampleIndex().Result;
                    if (firstAvailableSample.succesfulResponse)
                    {
                        var i = Helper.GetIntFromBigEndian(firstAvailableSample.response) % modValue;

                        //for (var t = 0; t < availableSamples; t+=m)
                        //{
                        //    if (token.IsCancellationRequested == true)
                        //    {
                        //        break;
                        //    }
                        //Console.WriteLine($"available samples: {availableSamples}");

                        var updateNumber = Math.Max(refresh, availableSamples);
                        for (var j = 0; j < availableSamples; j++)
                        {
                            sampledTime = sampleNumber / (double)selectedResolution;

                            if (updateRanges >= refresh)
                            {
                                await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    SampleMaximumX = sampledTime + refreshTime;
                                    SampleMinimumX = sampledTime;
                                }));

                                updateRanges = 0;
                            }
                            // Update chart
                            var vSense = traceModel.ReadVsenseSamples(i).Result;
                            if (vSense.succesfulResponse)
                            {
                                var vSenseDataValue = Helper.GetFloatFromBigEndian(vSense.response);

                                await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    VSenseCollection.SurpressedAdd(new DataPoint(sampledTime, vSenseDataValue), refresh);

                                    if (sampleNumber > refresh)
                                    {
                                        VSenseCollection.SurpressedRemoveAt(0);
                                    }
                                }));
                            }

                            var iSense = traceModel.ReadIsenseSamples(i).Result;
                            if (iSense.succesfulResponse)
                            {
                                var iSenseDataValue = Helper.GetFloatFromBigEndian(iSense.response);

                                await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    ISenseCollection.SurpressedAdd(new DataPoint(sampledTime, iSenseDataValue), refresh);

                                    if (sampleNumber > refresh)
                                    {
                                        ISenseCollection.SurpressedRemoveAt(0);
                                    }
                                }));
                            }

                            var iRef = traceModel.ReadIrefSamples(i).Result;
                            if (iRef.succesfulResponse)
                            {
                                var iRefDataValue = Helper.GetFloatFromBigEndian(iRef.response);

                                await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    IRefCollection.SurpressedAdd(new DataPoint(sampledTime, iRefDataValue), refresh);

                                    if (sampleNumber > refresh)
                                    {
                                        IRefCollection.SurpressedRemoveAt(0);
                                    }
                                }));
                            }

                            var temperatureOne = traceModel.ReadTempOneSamples(i).Result;
                            if (temperatureOne.succesfulResponse)
                            {
                                var temperatureOneDataValue = Helper.GetFloatFromBigEndian(temperatureOne.response);

                                await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    TemperatureOneCollection.SurpressedAdd(new DataPoint(sampledTime, temperatureOneDataValue), refresh);

                                    if (sampleNumber > refresh)
                                    {
                                        TemperatureOneCollection.SurpressedRemoveAt(0);
                                    }
                                }));
                            }

                            var temperatureTwo = traceModel.ReadTempTwoSamples(i).Result;
                            if (temperatureTwo.succesfulResponse)
                            {
                                var temperatureTwoDataValue = Helper.GetFloatFromBigEndian(temperatureTwo.response);

                                await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    TemperatureTwoCollection.SurpressedAdd(new DataPoint(sampledTime, temperatureTwoDataValue), refresh);

                                    if (sampleNumber > refresh)
                                    {
                                        TemperatureTwoCollection.SurpressedRemoveAt(0);
                                    }
                                }));
                            }

                            var temperatureThree = traceModel.ReadTempThreeSamples(i).Result;
                            if (temperatureThree.succesfulResponse)
                            {
                                var temperatureThreeDataValue = Helper.GetFloatFromBigEndian(temperatureThree.response);

                                await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    TemperatureThreeCollection.SurpressedAdd(new DataPoint(sampledTime, temperatureThreeDataValue), refresh);

                                    if (sampleNumber > refresh)
                                    {
                                        TemperatureThreeCollection.SurpressedRemoveAt(0);
                                    }
                                }));
                            }

                            var temperatureFour = traceModel.ReadTempFourSamples(i).Result;
                            if (temperatureFour.succesfulResponse)
                            {
                                var temperatureFourDataValue = Helper.GetFloatFromBigEndian(temperatureFour.response);

                                await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    TemperatureFourCollection.SurpressedAdd(new DataPoint(sampledTime, temperatureFourDataValue), refresh);
                                    if (sampleNumber > refresh)
                                    {
                                        TemperatureFourCollection.SurpressedRemoveAt(0);
                                    }
                                }));
                            }
                            // update what to read next
                            i = (i + 1) % modValue;
                            samplesToClear++;
                            sampleNumber++;
                            updateRanges++;



                            //}
                        }

                        var removeSamples = traceModel.SetReadSamples(samplesToClear).Result;
                        if (removeSamples.succesfulResponse)
                        {
                            var foo = traceModel.GetNumberOfAvailableSamples().Result;

                            var foo2 = Helper.GetIntFromBigEndian(foo.response);
                            if (foo2 > 20)
                            {
                                traceModel.SetReadSamples(foo2).Wait();
                                var foo3 = traceModel.GetNumberOfAvailableSamples().Result;
                                //var foo4 = Helper.GetIntFromBigEndian(foo3.response);
                                //Console.WriteLine($"!!!available samples AFTER CLEAR: {foo4}");
                            }

                            //Console.WriteLine($"Cleared: {samplesToClear} samples");
                            samplesToClear = 0;
                        }

                        //watch.Stop();
                        //var elapsedMs = watch.ElapsedMilliseconds;
                        //Console.WriteLine($"Time: {elapsedMs} ****");
                    }
                }
            }
        }
예제 #3
0
        private async void SaveSamplesThread()
        {
            var status = snapshotModel.ReadStatus().Result;

            if (status.succesfulResponse)
            {
                if (ReadyToSave(status.response))
                {
                    IsNotSaving  = false;
                    ProgressText = string.Empty;

                    // Process VSense
                    List <string> vSenseData           = new List <string>(numberOfSamples);
                    List <string> iSenseData           = new List <string>(numberOfSamples);
                    List <string> iRefData             = new List <string>(numberOfSamples);
                    List <string> temperatureOneData   = new List <string>(numberOfSamples);
                    List <string> temperatureTwoData   = new List <string>(numberOfSamples);
                    List <string> temperatureThreeData = new List <string>(numberOfSamples);
                    List <string> temperatureFourData  = new List <string>(numberOfSamples);
                    await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        VSenseCollection.Clear();
                        ISenseCollection.Clear();
                        IRefCollection.Clear();
                        TemperatureFourCollection.Clear();
                        TemperatureThreeCollection.Clear();
                        TemperatureTwoCollection.Clear();
                        TemperatureOneCollection.Clear();
                        SetEnableStatuses(ButtonAction.SavingState);
                    }));


                    for (var sampleNumber = 0; sampleNumber < numberOfSamples; sampleNumber++)
                    {
                        if (token.IsCancellationRequested == true)
                        {
                            await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                ProgressText = "Cancelled";
                                SaveValue = 0;
                                VSenseCollection.Clear();
                                ISenseCollection.Clear();
                                IRefCollection.Clear();
                                TemperatureFourCollection.Clear();
                                TemperatureThreeCollection.Clear();
                                TemperatureTwoCollection.Clear();
                                TemperatureOneCollection.Clear();
                                IsNotSaving = true;
                                SetEnableStatuses(ButtonAction.AfterSaveCancelOrFinished);
                            }));

                            return;
                        }

                        var sampledTime = (double)sampleNumber / (double)SelectedResolution;
                        await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            SampleMaximumX = (double)numberOfSamples / (double)selectedResolution;
                        }));


                        var errorCounter = 0;

                        CommunicationData vSense;
                        do
                        {
                            vSense = snapshotModel.ReadVsenseSamples(sampleNumber).Result;
                            errorCounter++;
                            if (!vSense.succesfulResponse)
                            {
                                Thread.Sleep(100);
                            }

                            if (errorCounter > numberOfErrorsBeforeRetry)
                            {
                                ProgressText = "Too many unsuccesful communication attempts, please try again.";
                                IsNotSaving  = true;
                                return;
                            }
                        } while (!vSense.succesfulResponse);

                        vSenseData.Add(ReadDataInSelectedFormat(vSense.response, out var vSenseDataValue));
                        await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            VSenseCollection.SurpressedAdd(new DataPoint(sampledTime, vSenseDataValue), numberOfSamples);
                        }));

                        errorCounter = 0;

                        CommunicationData iRef;
                        do
                        {
                            iRef = snapshotModel.ReadIrefSamples(sampleNumber).Result;
                            errorCounter++;
                            if (!iRef.succesfulResponse)
                            {
                                Thread.Sleep(100);
                            }

                            if (errorCounter > numberOfErrorsBeforeRetry)
                            {
                                ProgressText = "Too many unsuccesful communication attempts, please try again.";
                                IsNotSaving  = true;
                                return;
                            }
                        } while (!iRef.succesfulResponse);

                        var iRefDataValue = Helper.GetFloatFromBigEndian(iRef.response);
                        iRefData.Add(iRefDataValue.ToString());
                        errorCounter = 0;
                        await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            IRefCollection.SurpressedAdd(new DataPoint(sampledTime, iRefDataValue), numberOfSamples);
                        }));

                        CommunicationData iSense;
                        do
                        {
                            iSense = snapshotModel.ReadIsenseSamples(sampleNumber).Result;
                            errorCounter++;

                            if (!iSense.succesfulResponse)
                            {
                                Thread.Sleep(100);
                            }

                            if (errorCounter > numberOfErrorsBeforeRetry)
                            {
                                ProgressText = "Too many unsuccesful communication attempts, please try again.";
                                IsNotSaving  = true;
                                return;
                            }
                        } while (!iSense.succesfulResponse);

                        errorCounter = 0;
                        iSenseData.Add(ReadDataInSelectedFormat(iSense.response, out var iSenseDataValue));
                        await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            ISenseCollection.SurpressedAdd(new DataPoint(sampledTime, iSenseDataValue), numberOfSamples);
                        }));

                        CommunicationData temperatureOne;
                        do
                        {
                            temperatureOne = snapshotModel.ReadTempOneSamples(sampleNumber).Result;
                            errorCounter++;
                            if (!temperatureOne.succesfulResponse)
                            {
                                Thread.Sleep(100);
                            }

                            if (errorCounter > numberOfErrorsBeforeRetry)
                            {
                                ProgressText = "Too many unsuccesful communication attempts, please try again.";
                                IsNotSaving  = true;
                                return;
                            }
                        } while (!temperatureOne.succesfulResponse);

                        temperatureOneData.Add(ReadDataInSelectedFormat(temperatureOne.response, out var temperatureOneDataValue));
                        await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            TemperatureOneCollection.SurpressedAdd(new DataPoint(sampledTime, temperatureOneDataValue), numberOfSamples);
                        }));

                        errorCounter = 0;

                        CommunicationData temperatureTwo;
                        do
                        {
                            temperatureTwo = snapshotModel.ReadTempTwoSamples(sampleNumber).Result;
                            errorCounter++;
                            if (!temperatureTwo.succesfulResponse)
                            {
                                Thread.Sleep(100);
                            }

                            if (errorCounter > numberOfErrorsBeforeRetry)
                            {
                                ProgressText = "Too many unsuccesful communication attempts, please try again.";
                                IsNotSaving  = true;
                                return;
                            }
                        } while (!temperatureTwo.succesfulResponse);

                        temperatureTwoData.Add(ReadDataInSelectedFormat(temperatureTwo.response, out var temperatureTwoDataValue));
                        await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            TemperatureTwoCollection.SurpressedAdd(new DataPoint(sampledTime, temperatureTwoDataValue), numberOfSamples);
                        }));

                        errorCounter = 0;

                        CommunicationData temperatureThree;
                        do
                        {
                            temperatureThree = snapshotModel.ReadTempThreeSamples(sampleNumber).Result;
                            errorCounter++;
                            if (!temperatureThree.succesfulResponse)
                            {
                                Thread.Sleep(100);
                            }

                            if (errorCounter > numberOfErrorsBeforeRetry)
                            {
                                ProgressText = "Too many unsuccesful communication attempts, please try again.";
                                IsNotSaving  = true;
                                return;
                            }
                        } while (!temperatureThree.succesfulResponse);

                        temperatureThreeData.Add(ReadDataInSelectedFormat(temperatureThree.response, out var temperatureThreeDataValue));
                        await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            TemperatureThreeCollection.SurpressedAdd(new DataPoint(sampledTime, temperatureThreeDataValue), numberOfSamples);
                        }));

                        errorCounter = 0;

                        CommunicationData temperatureFour;
                        do
                        {
                            temperatureFour = snapshotModel.ReadTempFourSamples(sampleNumber).Result;
                            errorCounter++;
                            if (!temperatureFour.succesfulResponse)
                            {
                                Thread.Sleep(100);
                            }

                            if (errorCounter > numberOfErrorsBeforeRetry)
                            {
                                ProgressText = "Too many unsuccesful communication attempts, please try again.";
                                IsNotSaving  = true;
                                return;
                            }
                        } while (!temperatureFour.succesfulResponse);

                        temperatureFourData.Add(ReadDataInSelectedFormat(temperatureFour.response, out var temperatureFourDataValue));
                        await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            TemperatureFourCollection.SurpressedAdd(new DataPoint(sampledTime, temperatureFourDataValue), numberOfSamples);
                        }));

                        errorCounter = 0;

                        await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            ProgressText = $"{sampleNumber} / {NumberOfSamples}";
                            SaveValue = sampleNumber;
                        }));
                    }

                    // Save to csv

                    var fileFullName = $"\\{FileName} {DateTime.Now.ToString("MM dd yyyy HH mm ss")}.csv";
                    var filePath     = Directory.GetCurrentDirectory() + fileFullName;

                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    try
                    {
                        using (FileStream file = new FileStream(filePath, FileMode.Create))
                        {
                            using (StreamWriter sw = new StreamWriter(file))
                            {
                                sw.WriteLine(string.Format("vSense, iSense, iRef, Temperature1, Temperature2, Temperature3, Temperature4"));

                                for (var sampleNumber = 0; sampleNumber < numberOfSamples; sampleNumber++)
                                {
                                    sw.WriteLine(string.Format($"{vSenseData[sampleNumber]}, {iSenseData[sampleNumber]}, {iRefData[sampleNumber]}, {temperatureOneData[sampleNumber]}, {temperatureTwoData[sampleNumber]}, {temperatureThreeData[sampleNumber]}, {temperatureFourData[sampleNumber]}"));
                                }
                            }
                        }
                    }
                    catch
                    {
                        await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            ProgressText = "Exception Thrown, Save incomplete.";
                            SaveValue = 0;
                            VSenseCollection.Clear();
                            ISenseCollection.Clear();
                            IRefCollection.Clear();
                            TemperatureFourCollection.Clear();
                            TemperatureThreeCollection.Clear();
                            TemperatureTwoCollection.Clear();
                            TemperatureOneCollection.Clear();
                            IsNotSaving = true;
                            SetEnableStatuses(ButtonAction.AfterSaveCancelOrFinished);
                        }));

                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }
                        return;
                    }

                    await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        SetEnableStatuses(ButtonAction.AfterSaveCancelOrFinished);
                        ProgressText = "Complete";
                        SaveValue = 0;
                        IsNotSaving = true;
                    }));
                }
            }
            else
            {
                ProgressText = "Unsuccesful communication";
            }
        }