Example #1
0
        private void PreloadStreams()
        {
            IDictionary <ChannelIdentifier, short[]> output = new Dictionary <ChannelIdentifier, short[]>();

            foreach (var s in ActiveOutputStreams.Cast <HekaDAQOutputStream>())
            {
                s.Reset();
                var outputSamples = new List <short>();
                while (TimeSpanExtensions.FromSamples((uint)outputSamples.Count(), s.SampleRate).TotalSeconds < PRELOAD_DURATION_SECONDS) // && s.HasMoreData
                {
                    var nextOutputDataForStream = NextOutputDataForStream(s);
                    var nextSamples             = nextOutputDataForStream.DataWithUnits(HekaDAQOutputStream.DAQCountUnits).Data.
                                                  Select(
                        (m) => (short)m.QuantityInBaseUnit);

                    outputSamples = outputSamples.Concat(nextSamples).ToList();
                }

                if (!outputSamples.Any())
                {
                    throw new HekaDAQException("Unable to pull data to preload stream " + s.Name);
                }


                output[new ChannelIdentifier {
                           ChannelNumber = s.ChannelNumber, ChannelType = (ushort)s.ChannelType
                       }] =
                    outputSamples.ToArray();
            }

            Device.Preload(output);
        }
Example #2
0
        private IEnumerable <KeyValuePair <IDAQOutputStream, Task <IOutputData> > > NextOutgoingData()
        {
            var outgoingData = ActiveOutputStreams.ToDictionary(
                s => s,
                s => Task.Factory.StartNew(
                    () => NextOutputDataForStream(s),
                    TaskCreationOptions.PreferFairness
                    )
                );

            return(outgoingData);
        }
Example #3
0
        protected override IDictionary <IDAQInputStream, IInputData> ProcessLoopIteration(IDictionary <IDAQOutputStream, IOutputData> outData, TimeSpan deficit)
        {
            IDictionary <ChannelIdentifier, short[]> output        = new Dictionary <ChannelIdentifier, short[]>();
            IDictionary <ChannelIdentifier, short[]> deficitOutput = new Dictionary <ChannelIdentifier, short[]>();



            foreach (var s in ActiveOutputStreams.Cast <HekaDAQOutputStream>())
            {
                var outputData = outData[s];
                var cons       = outputData.DataWithUnits(HekaDAQOutputStream.DAQCountUnits).SplitData(deficit);

                short[] outputSamples        = cons.Rest.Data.Select((m) => (short)m.QuantityInBaseUnit).ToArray();
                short[] deficitOutputSamples = cons.Head.Data.Select((m) => (short)m.QuantityInBaseUnit).ToArray();
                output[new ChannelIdentifier {
                           ChannelNumber = s.ChannelNumber, ChannelType = (ushort)s.ChannelType
                       }] =
                    outputSamples;
                deficitOutput[new ChannelIdentifier {
                                  ChannelNumber = s.ChannelNumber, ChannelType = (ushort)s.ChannelType
                              }] =
                    deficitOutputSamples;
            }

            if (deficitOutput.Any())
            {
                Device.Write(deficitOutput);
            }

            var inputChannels =
                ActiveInputStreams.
                Cast <HekaDAQInputStream>().
                Select((s) => new ChannelIdentifier {
                ChannelNumber = s.ChannelNumber, ChannelType = (ushort)s.ChannelType
            }).
                ToList();

            int nsamples;

            if (output.Values.Any())
            {
                if (output.Values.Select(a => a.Length).Distinct().Count() > 1)
                {
                    throw new HekaDAQException("Output buffers are not equal length.");
                }

                nsamples = output.Values.Select((a) => a.Length).Distinct().First();
            }
            else
            {
                nsamples = (int)TimeSpan.FromSeconds(DEFAULT_TRANSFER_BLOCK_SECONDS).Samples(SampleRate);
            }

            IEnumerable <KeyValuePair <ChannelIdentifier, short[]> > input = Device.ReadWrite(output, inputChannels, nsamples);

            var result = new ConcurrentDictionary <IDAQInputStream, IInputData>();

            Parallel.ForEach(input, (kvp) =>
            {
                var s = StreamWithIdentifier(kvp.Key) as IDAQInputStream;
                if (s == null)
                {
                    throw new DAQException(
                        "ChannelIdentifier does not specify an input stream.");
                }

                //Create the raw input data

                IInputData rawData = new InputData(
                    kvp.Value.Select(
                        v => new Measurement(v, HekaDAQInputStream.DAQCountUnits)).
                    ToList(),
                    StreamWithIdentifier(kvp.Key).SampleRate,
                    Clock.Now
                    ).DataWithNodeConfiguration("Heka.HekaDAQController", Configuration);


                //Convert to input units and store
                result[s] = rawData;
            });


            return(result);
        }