Exemplo n.º 1
0
        /// <summary>
        /// Parses all pending TCPPackets received
        /// </summary>
        protected override void ParsePendingData()
        {
            TcpPacket packet;
            string    message;
            int       i;

            do
            {
                packet = dataReceived.Consume(20);
                if (packet == null)
                {
                    return;
                }
                if (!packet.IsAnsi)
                {
                    continue;
                }
                for (i = 0; i < packet.DataStrings.Length; ++i)
                {
                    message = packet.DataStrings[i].Trim();

                    ParseMessage(message);
                }
            } while (!stopMainThread && dataReceived.Count > 0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executes parsing of data received
        /// </summary>
        private void ParserThreadTask()
        {
            T data;

            running = true;
            while (running)
            {
                try
                {
                    data = dataReceived.Consume(100);
                    if (data != null)
                    {
                        Parse(data);
                    }
                }
                catch (ThreadInterruptedException)
                {
                    dataReceived.Clear();
                    return;
                }
                catch (ThreadAbortException)
                {
                    dataReceived.Clear();
                    return;
                }
                catch
                {
                    continue;
                }
            }
        }
Exemplo n.º 3
0
 void StartRecording(int channelIndex, Dictionary <string, string> exp_info = null)
 {
     //Attach ourselves to the sample read event queue
     if (!IsRecordingCh1)
     {
         HardwareManager.DaqBoard.ReadDone += RecordSamples;
     }
     if (channelIndex == 1)
     {
         _recordTask = new Task(() =>
         {
             string data_file_name = CreateFilename(1) + ".data";
             //Write info file
             if (exp_info == null)
             {
                 exp_info = new Dictionary <string, string>();
                 exp_info["Experiment type"] = "Free run";
             }
             exp_info["datafile"]  = Path.GetFileName(data_file_name);
             exp_info["daq_rate"]  = HardwareSettings.DAQ.Rate.ToString();
             exp_info["channel"]   = channelIndex.ToString();
             exp_info["mV_per_V"]  = mV_per_V.ToString();
             exp_info["pA_per_V"]  = pA_per_V.ToString();
             TextWriter infoWriter = new StreamWriter(CreateFilename(1) + ".info");
             WriteExperimentInfo(BaseFNameCh1, exp_info, infoWriter);
             infoWriter.Dispose();
             BinaryWriter ch1File = new BinaryWriter(File.OpenWrite(data_file_name));
             while (true)
             {
                 ChannelReadDataChunk chnk = _record_dataQueue.Consume();
                 if (chnk.StartIndex == 0 && chnk.Data == null)
                 {
                     break;
                 }
                 for (int i = 0; i < chnk.Data.GetLength(1); i++)
                 {
                     WriteChannel1Sample(ch1File, chnk.StartIndex + i, chnk.Data[2, i] > 2, (float)chnk.Data[4, i], (float)chnk.Data[0, i],
                                         (float)chnk.Data[6, i]);
                 }
             }
             ch1File.Dispose();
         });
         _recordTask.Start();
         IsRecordingCh1 = true;
     }
     else
     {
         throw new NotImplementedException();
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Gets called by the UI timer to update the display of
        /// the live plot
        /// </summary>
        private void UpdateLivePlot()
        {
            int bin_factor = HardwareSettings.DAQ.Rate / 2000;
            int c          = _liveDump.Count;

            if (c < bin_factor)
            {
                return;
            }
            if (_chartRunningSamples == null || _chartRunningSamples.Length != bin_factor)
            {
                _chartRunningSamples = new double[bin_factor];
            }
            //we take only a number of samples that evenly fits
            //into our binning
            int to_take = c - (c % bin_factor);

            //create the appropriate chart values as the absolute maximum within the bin
            double[] values = new double[to_take / bin_factor];
            for (int i = 0; i < to_take; i++)
            {
                _chartRunningSamples[_chartRunningCount % bin_factor] = _liveDump.Consume();
                _chartRunningCount++;
                if (_chartRunningCount % bin_factor == 0)
                {
                    int    maxIndex = -1;
                    double absMax   = -1;
                    for (int j = 0; j < bin_factor; j++)
                    {
                        if (Math.Abs(_chartRunningSamples[j]) > absMax)
                        {
                            absMax   = Math.Abs(_chartRunningSamples[j]);
                            maxIndex = j;
                        }
                    }
                    //add our binned value after conversion into mV (current clamp) or pA (voltage clamp)
                    var daqvolts = _chartRunningSamples[maxIndex];
                    if (!VC)
                    {
                        daqvolts = HardwareSettings.DAQ.DAQV_to_mV(daqvolts);
                    }
                    else
                    {
                        daqvolts = HardwareSettings.DAQ.DAQV_to_pA(daqvolts);
                    }
                    values[i / bin_factor] = daqvolts;
                }
            }
            PlotData_Live.Append(values);
        }
    static void ConsumerJob()
    {
        // Make sure we get a different random seed from the
        // first thread
        Random rng = new Random(1);

        // We happen to know we've only got 10
        // items to receive
        for (int i = 0; i < 10; i++)
        {
            object o = queue.Consume();
            Console.WriteLine("\t\t\t\tConsuming {0}", o);
            Thread.Sleep(rng.Next(1000));
        }
    }
Exemplo n.º 6
0
        /// <summary>
        /// Parses all received messages
        /// </summary>
        protected override void ParsePendingData()
        {
            string message;

            do
            {
                message = dataReceived.Consume(20);
                if (message == null)
                {
                    return;
                }

                ParseMessage(message);
            } while (!stopMainThread && dataReceived.Count > 0);
        }
Exemplo n.º 7
0
        public override IShortestPathResult Process(uint @from, uint to)
        {
            _result = new BfsConcurrentResult(from, to);

            INode <T, TEdgeCustom> nodeFrom = Graph[from];

            nodeFrom.Distance = 0;

            _table.Initialise = () => _table.Produce(nodeFrom);

            _table.Producing = node =>
            {
                if (node.Key != _result.ToNode)
                {
                    node.EachChild((in Edge <T, TEdgeCustom> e) =>
                    {
                        _table.Consume(new MapReduceJob(node.Key, e.Node.Key, node.Distance, e.Cost));
                    });
                }
Exemplo n.º 8
0
        protected virtual void SpeechThreadTask()
        {
            running = true;
            #region Voices Initialization
            // Voices Initialization
            //while (running && (voiceNames.Count < 1))
            //{
            //    Thread.Sleep(100);
            //    LoadVoices();
            //}
            #endregion

            while (running)
            {
                try
                {
                    SpeechTextTask stt = speechQueue.Consume(100);
                    if (stt == null)
                    {
                        continue;
                    }
                    Speak(stt);
                }
                catch (ThreadInterruptedException tiex)
                {
                    Console.WriteLine("SpeechThread aborted: " + tiex.ToString());
                    return;
                }
                catch (ThreadAbortException taex)
                {
                    Console.WriteLine("SpeechThread aborted: " + taex.ToString());
                    ShutUp();
                    return;
                }
                catch { continue; }
            }
            ShutUp();
            speechQueue.Clear();
        }
Exemplo n.º 9
0
        private void PlayAudioThreadTask()
        {
            PlayAudioTask audioTask;

            running = true;

            while (running)
            {
                try
                {
                    audioTask = playQueue.Consume();
                    if ((audioTask == null) || (audioTask.Audio == null))
                    {
                        continue;
                    }
                    if (!Play(audioTask))
                    {
                        playQueue.Produce(audioTask);
                    }
                }
                catch (ThreadAbortException taex)
                {
                    Console.WriteLine(taex.Message);
                    playQueue.Clear();
                    return;
                }
                catch (ThreadInterruptedException tiex)
                {
                    Console.WriteLine(tiex.Message);
                    continue;
                }
                catch
                {
                    Thread.Sleep(100);
                    continue;
                }
            }
        }
Exemplo n.º 10
0
        public override IShortestPathResult Process(uint @from, uint to)
        {
            _result = new BfsConcurrentResult(from, to);

            INode <T, TEdgeCustom> nodeFrom = Graph[from];

            nodeFrom.Distance = 0;

            _table.Initialise = () => _table.Produce(nodeFrom);

            _table.Producing = node =>
            {
                if (node.Key != _result.ToNode)
                {
                    for (int i = 0; i < node.Children.Count; i++)
                    {
                        Edge <T, TEdgeCustom> e = node.Children[i];
                        _table.Consume(new MapReduceJob(node.Key, e.Node.Key, node.Distance, e.Cost));
                    }
                }
            };

            _table.Consuming = job =>
            {
                if (Reduce(job.From, Graph[job.To], job.Distance))
                {
                    _table.Produce(Graph[job.To]);
                }
            };

            _table.Work();

            _result.Distance = Graph[to].Distance;

            return(_result);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Executes the main tasks of the service
        /// </summary>
        private void MainThreadTask()
        {
            TcpPacket packet;
            int       i;
            IAction   action;

            //Thread thread;

            if (!cnnMan.IsRunning)
            {
                cnnMan.Start();
            }

            running = true;
            while (running)
            {
                try
                {
                    if (Paused && Monitor.TryEnter(oPause, 1))
                    {
                        while (Paused)
                        {
                            Monitor.Wait(oPause);
                        }
                        Monitor.Exit(oPause);
                    }
                    packet = dataReceived.Consume();
                    if ((packet != null) && packet.IsAnsi)
                    {
                        for (i = 0; i < packet.DataStrings.Length; ++i)
                        {
                            if (Parse(packet.DataStrings[i], out action))
                            {
                                //thread = new Thread(dlgExecute);
                                //thread.IsBackground = true;
                                //thread.Start(action);
                                icon.Icon = Properties.Resources.icoGreen;
                                action.Execute();
                                icon.Icon = Properties.Resources.icoBlue;
                            }
                        }
                    }
                }
                catch (ThreadInterruptedException tiex)
                {
                    tiex.GetType();
                    continue;
                }
                catch (ThreadAbortException taex)
                {
                    running = false;
                    if (cnnMan.IsRunning)
                    {
                        cnnMan.Stop();
                    }
                    taex.GetType();
                    return;
                }
                catch
                {
                    continue;
                }
            }

            if (cnnMan.IsRunning)
            {
                cnnMan.Stop();
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Parses all pending TCPPackets received
        /// </summary>
        private void ParsePendingCommands()
        {
            Command  c;
            Response r;

            do
            {
                c = commandsReceived.Consume(100) as Command;
                if (c == null)
                {
                    continue;
                }

                switch (c.Command)
                {
                case "alive":
                    AliveCommand(c);
                    break;

                case "bbstatus":
                    BbStatus(c);
                    break;

                case "busy":
                    BusyCommand(c);
                    break;

                case "connected":
                    ConnectedCommand(c);
                    break;

                case "create_var":
                    CreateVarCommand(c);
                    break;

                case "idletime":
                    IdleTimeCommand(c);
                    break;

                case "list_vars":
                    ListVarsCommand(c);
                    break;

                case "modules":
                    ModulesCommand(c);
                    break;

                case "mstatus":
                    MstatusCommand(c);
                    break;

                case "mystat":
                    MystatCommand(c);
                    break;

                case "prototypes":
                    PrototypesCommand(c);
                    break;

                case "querymodule":
                    QueryModuleCommand(c);
                    break;

                case "read_var":
                    ReadVarCommand(c);
                    break;

                case "reset":
                    ResetCommand(c);
                    break;

                case "ready":
                    ReadyCommand(c);
                    break;

                case "setupmodule":
                    SetupModuleCommand(c);
                    break;

                case "start_module_app":
                    StartModuleAppCommand(c);
                    break;

                case "stat_var":
                    StatVarCommand(c);
                    break;

                case "stop_module_app":
                    StopModuleAppCommand(c);
                    break;

                case "suscribe_var":
                case "subscribe_var":
                    SubscribeVarCommand(c);
                    break;

                case "unsubscribe_var":
                    UnsubscribeVarCommand(c);
                    break;

                case "write_var":
                    WriteVarCommand(c);
                    break;

                default:
                    r = null;
                    try { r = Response.CreateFromCommand(c, ResponseFailReason.Unknown); }
                    catch { }
                    if (r != null)
                    {
                        OnResponseReceived(r);
                    }
                    break;
                }
            } while (!stopMainThread && (commandsReceived.Count > 0));
        }
Exemplo n.º 13
0
        /// <summary>
        /// Gets called to update our triggered plot with seal-test samples
        /// </summary>
        private void UpdateSealTest()
        {
            //Some constants that should be settable later
            int sealTestFreq      = 10;
            int sealTestSamples   = HardwareSettings.DAQ.Rate / sealTestFreq;
            int sealTestOnSamples = sealTestSamples / 2;
            int nAccum            = sealTestFreq / 2;
            // We want to align plotting of the seal test, such that the
            // current spikes at start and end of step are in the middle of the plot
            // i.e. such that the middle of the OnSamples is in the middle of the plot
            int zeroPoint = sealTestSamples - sealTestOnSamples / 2;

            if (_sealTestAccum == null || _sealTestAccum.Length != sealTestSamples)
            {
                _sealTestAccum = new double[sealTestSamples];
                _sealTestTime  = new double[sealTestSamples];
                for (int i = 0; i < sealTestSamples; i++)
                {
                    _sealTestTime[i] = (1000.0 / HardwareSettings.DAQ.Rate) * i;
                }
                PlotData_Seal.Capacity = sealTestSamples;
                SealXRange             = new Range <double>(0, 1000.0 / sealTestFreq);
            }
            // consume samples
            int c = _sealTestDump.Count;

            if (c < sealTestSamples)
            {
                return;
            }
            for (int i = 0; i < c; i++)
            {
                var sample = _sealTestDump.Consume();
                //check if this sample belongs to a new accumulation window - if yes, plot and reset accumulator
                long window_index = (sample.Index + zeroPoint) % (sealTestSamples * nAccum);
                if (window_index == 0)
                {
                    //update calculated resistances
                    double currMax = (_sealTestAccum.Max() - _sealTestAccum.Min()) / 2;
                    //Seal resistance = voltage_step / max_current
                    RSeal = (10e-3 / (currMax * 1e-12)) / 1e6; // Resistance in MOhm
                    //Membrane resistance = voltage_step / steady_state_current
                    double currSS   = 0;                       //The steady state current (seal test peak)
                    double currMean = 0;                       //The mean current (start of seal test window)
                    for (int j = sealTestOnSamples - 10; j <= sealTestOnSamples + 10; j++)
                    {
                        currSS += _sealTestAccum[j] / 21;
                    }
                    for (int j = 0; j <= 20; j++)
                    {
                        currMean += _sealTestAccum[j] / 21;
                    }
                    RMembrane = (10e-3 / ((currSS - currMean) * 1e-12)) / 1e6; // Resistance in MOhm
                    //plot
                    PlotData_Seal.Clear();
                    for (int j = 0; j < _sealTestAccum.Length; j++)
                    {
                        _sealTestAccum[j] -= currMean;
                    }
                    PlotData_Seal.Append(_sealTestTime, _sealTestAccum);
                    //reset accumulator
                    for (int j = 0; j < sealTestSamples; j++)
                    {
                        _sealTestAccum[j] = 0;
                    }
                }
                //add new samples to array
                int array_index = (int)((sample.Index % sealTestSamples - zeroPoint + sealTestSamples) % sealTestSamples);
                _sealTestAccum[array_index] += HardwareSettings.DAQ.DAQV_to_pA(sample.Sample / nAccum);
            }
        }
Exemplo n.º 14
0
        private void MainThreadTask()
        {
            running = true;
            int            i;
            ReceivedPacket packet;
            Command        command;
            Response       response;
            string         s;
            bool           handled;

            while (running)
            {
                packet = dataReceived.Consume();
                if (running && (packet != null) && (packet.TcpPacket.IsAnsi))
                {
                    for (i = 0; i < packet.TcpPacket.DataStrings.Length; ++i)
                    {
                        s       = packet.TcpPacket.DataStrings[i];
                        handled = false;
                        if (Response.TryParse(packet.ConnectionManager, s, out response))
                        {
                            //response.SourceModule = packet.ConnectionManager.ModuleName;
                            if (!performanceTestInProgress)
                            {
                                Console("<=" + response.ToString());
                            }
                            lock (receivedResponses)
                            {
                                receivedResponses.Add(response);
                            }
                            continue;
                        }

                        if (!Command.TryParse(packet.ConnectionManager, s, out command))
                        {
                            continue;
                        }
                        //command.SourceModule = packet.ConnectionManager.ModuleName;
                        foreach (AutoResponder responder in autoResponders)
                        {
                            if (responder.Match(command))
                            {
                                response = responder.GetResponse(command);
                                packet.ConnectionManager.Send(response);
                                handled = true;
                                Console("AutoResponder (" + command.SourceModule + "): [" + command.StringToSend + "] => [" + response.StringToSend + "]");
                                break;
                            }
                        }

                        foreach (Prompter prompter in prompters)
                        {
                            if (prompter.Match(command))
                            {
                                prompter.Prompt(packet.ConnectionManager, command);
                                //Console("Prompter:" + command.StringToSend + " => " + response.StringToSend);
                                handled = true;
                                break;
                            }
                        }
                        if (handled)
                        {
                            continue;
                        }
                    }
                }
            }

            foreach (ConnectionManager cnnMan in modules.Values)
            {
                cnnMan.Stop();
            }
        }
Exemplo n.º 15
0
        public void Check()
        {
            StatusReportList = new List <Report>();
            var clusters              = _setting.General.Kafka.Clusters;
            var connectionTimeoutSec  = _setting.General.Kafka.ConnectionTimeoutSec;
            var topicNameFromSettings = _setting.General.Kafka.TopicName;
            var certificateLocation   = _setting.General.Kafka.SslCertificateLocation;
            var certificateSubject    = _setting.General.Kafka.SslCertificateSubject;
            var date = DateTime.Now.ToString("dd.MM.yyyy.HH.m");


            if (File.Exists(certificateLocation))
            {
                // delete an old certificate
                File.Delete(certificateLocation);
            }

            var counter = 0;

            foreach (var cluster in clusters)
            {
                counter++;
                string topicName;
                var    kafkaStatus   = ReportStatus.Undefined;
                var    mongoDbStatus = ReportStatus.Undefined;
                var    statusReport  = new Report();
                statusReport.Number  = counter;
                statusReport.EnvName = cluster.Name;
                _logger.Info($" [{counter}] from [{clusters.Count}]. " +
                             $"Work with the '{cluster.Name}' cluster");

                // Check Mongo
                _logger.Info($"Checking Mongo DB:");
                var mongoDbHelper        = new MongoDbHelper();
                var mongoDbConnectionStr = cluster.MongoDb;
                mongoDbStatus = mongoDbHelper.Ping(mongoDbConnectionStr, connectionTimeoutSec);

                // Check Kafka
                _logger.Info("Checking Kafka:");
                var bootStrapServers = string.Join(",", cluster.BootstrapServers);
                _logger.Info($" bootstrap servers: {bootStrapServers}");
                var clientConfig = new ClientConfig
                {
                    BootstrapServers = bootStrapServers,
                    SocketTimeoutMs  = connectionTimeoutSec * 1000,
                };
                topicName = $"{topicNameFromSettings}.{date}";
                if (cluster.SslEnabled)
                {
                    _logger.Info("SSL connection is enabled for this cluster");
                    if (!File.Exists(certificateLocation))
                    {
                        try
                        {
                            var certificate = CertificateHelper.GetCertificate(certificateSubject);
                            CertificateHelper.ExportToPEMFile(certificate, certificateLocation);
                        }
                        catch (CertificateException ce)
                        {
                            _logger.Error(ce.Message);
                            kafkaStatus = ReportStatus.CertificateError;
                            _logger.Warn($" Kafka status - [{kafkaStatus}]");
                            WriteClusterStatus(cluster, statusReport, mongoDbStatus, kafkaStatus);
                            StatusReportList.Add(statusReport);
                            continue;
                        }
                    }
                    clientConfig.SslCaLocation    = certificateLocation;
                    clientConfig.SecurityProtocol = SecurityProtocol.Ssl;
                    clientConfig.Debug            = "security";
                    topicName = $"{topicNameFromSettings}.ssl.{date}";
                }
                var bootstrapServersCount = cluster.BootstrapServers.Count;
                var topicHelper           = new TopicHelper();
                var producerConsumer      = new ProducerConsumer();

                var connectionIsOk = topicHelper
                                     .CheckConnectivity(clientConfig,
                                                        bootstrapServersCount);

                if (connectionIsOk)
                {
                    var topicWasCreated = topicHelper
                                          .CreateTopic(clientConfig, bootstrapServersCount, topicName);
                    if (topicWasCreated)
                    {
                        _logger.Info(string.Empty);
                        var producedMessageCount = producerConsumer.Produce(clientConfig, topicName);
                        var consumedMessageCount = producerConsumer.Consume(clientConfig, topicName);

                        if (producedMessageCount == consumedMessageCount)
                        {
                            _logger.Info($" * Produced messages == consumed messages: '{consumedMessageCount}' - [ok]");
                            kafkaStatus = ReportStatus.Ok;
                        }
                        else
                        {
                            _logger.Error($" * Produced messages != consumed messages: '{consumedMessageCount}' - [error]");
                            kafkaStatus = ReportStatus.Error;
                        }
                    }
                }
                else
                {
                    kafkaStatus = ReportStatus.Error;
                }
                _logger.Info($" Kafka status - [{kafkaStatus}]");
                WriteClusterStatus(cluster, statusReport, mongoDbStatus, kafkaStatus);
                StatusReportList.Add(statusReport);
                _logger.Info(string.Empty);
            }
            new HtmlReportHelper().PopulateTemplate(StatusReportList);
        }