예제 #1
0
        public virtual async Task <int> loadSummary(byte[] data)
        {
            List <List <double> > dataRows = new List <List <double> >();
            double        tim;
            double        mitv;
            double        itv;
            double        fitv;
            double        end_;
            int           nCols;
            int           minCol;
            int           avgCol;
            int           maxCol;
            int           res;
            int           m_pos;
            double        previewTotalTime;
            double        previewTotalAvg;
            double        previewMinVal;
            double        previewMaxVal;
            double        previewAvgVal;
            double        previewStartMs;
            double        previewStopMs;
            double        previewDuration;
            double        streamStartTimeMs;
            double        streamDuration;
            double        streamEndTimeMs;
            double        minVal;
            double        avgVal;
            double        maxVal;
            double        summaryStartMs;
            double        summaryStopMs;
            double        summaryTotalTime;
            double        summaryTotalAvg;
            double        summaryMinVal;
            double        summaryMaxVal;
            string        url;
            string        strdata;
            List <double> measure_data = new List <double>();

            if (_progress < 0)
            {
                strdata = YAPI.DefaultEncoding.GetString(data);
                if (strdata == "{}")
                {
                    _parent._throw(YAPI.VERSION_MISMATCH, "device firmware is too old");
                    return(YAPI.VERSION_MISMATCH);
                }
                res = await this._parse(strdata);

                if (res < 0)
                {
                    return(res);
                }
            }
            summaryTotalTime = 0;
            summaryTotalAvg  = 0;
            summaryMinVal    = YAPI.MAX_DOUBLE;
            summaryMaxVal    = YAPI.MIN_DOUBLE;
            summaryStartMs   = YAPI.MAX_DOUBLE;
            summaryStopMs    = YAPI.MIN_DOUBLE;

            // Parse complete streams
            for (int ii = 0; ii < _streams.Count; ii++)
            {
                streamStartTimeMs = Math.Round(await _streams[ii].get_realStartTimeUTC() * 1000);
                streamDuration    = await _streams[ii].get_realDuration();
                streamEndTimeMs   = streamStartTimeMs + Math.Round(streamDuration * 1000);
                if ((streamStartTimeMs >= _startTimeMs) && ((_endTimeMs == 0) || (streamEndTimeMs <= _endTimeMs)))
                {
                    // stream that are completely inside the dataset
                    previewMinVal   = await _streams[ii].get_minValue();
                    previewAvgVal   = await _streams[ii].get_averageValue();
                    previewMaxVal   = await _streams[ii].get_maxValue();
                    previewStartMs  = streamStartTimeMs;
                    previewStopMs   = streamEndTimeMs;
                    previewDuration = streamDuration;
                }
                else
                {
                    // stream that are partially in the dataset
                    // we need to parse data to filter value outside the dataset
                    url  = _streams[ii].imm_get_url();
                    data = await _parent._download(url);

                    _streams[ii].imm_parseStream(data);
                    dataRows = await _streams[ii].get_dataRows();
                    if (dataRows.Count == 0)
                    {
                        return(await this.get_progress());
                    }
                    tim    = streamStartTimeMs;
                    fitv   = Math.Round(await _streams[ii].get_firstDataSamplesInterval() * 1000);
                    itv    = Math.Round(await _streams[ii].get_dataSamplesInterval() * 1000);
                    nCols  = dataRows[0].Count;
                    minCol = 0;
                    if (nCols > 2)
                    {
                        avgCol = 1;
                    }
                    else
                    {
                        avgCol = 0;
                    }
                    if (nCols > 2)
                    {
                        maxCol = 2;
                    }
                    else
                    {
                        maxCol = 0;
                    }
                    previewTotalTime = 0;
                    previewTotalAvg  = 0;
                    previewStartMs   = streamEndTimeMs;
                    previewStopMs    = streamStartTimeMs;
                    previewMinVal    = YAPI.MAX_DOUBLE;
                    previewMaxVal    = YAPI.MIN_DOUBLE;
                    m_pos            = 0;
                    while (m_pos < dataRows.Count)
                    {
                        measure_data = dataRows[m_pos];
                        if (m_pos == 0)
                        {
                            mitv = fitv;
                        }
                        else
                        {
                            mitv = itv;
                        }
                        end_ = tim + mitv;
                        if ((end_ > _startTimeMs) && ((_endTimeMs == 0) || (tim < _endTimeMs)))
                        {
                            minVal = measure_data[minCol];
                            avgVal = measure_data[avgCol];
                            maxVal = measure_data[maxCol];
                            if (previewStartMs > tim)
                            {
                                previewStartMs = tim;
                            }
                            if (previewStopMs < end_)
                            {
                                previewStopMs = end_;
                            }
                            if (previewMinVal > minVal)
                            {
                                previewMinVal = minVal;
                            }
                            if (previewMaxVal < maxVal)
                            {
                                previewMaxVal = maxVal;
                            }
                            previewTotalAvg  = previewTotalAvg + (avgVal * mitv);
                            previewTotalTime = previewTotalTime + mitv;
                        }
                        tim   = end_;
                        m_pos = m_pos + 1;
                    }
                    if (previewTotalTime > 0)
                    {
                        previewAvgVal   = previewTotalAvg / previewTotalTime;
                        previewDuration = (previewStopMs - previewStartMs) / 1000.0;
                    }
                    else
                    {
                        previewAvgVal   = 0.0;
                        previewDuration = 0.0;
                    }
                }
                _preview.Add(new YMeasure(previewStartMs / 1000.0, previewStopMs / 1000.0, previewMinVal, previewAvgVal, previewMaxVal));
                if (summaryMinVal > previewMinVal)
                {
                    summaryMinVal = previewMinVal;
                }
                if (summaryMaxVal < previewMaxVal)
                {
                    summaryMaxVal = previewMaxVal;
                }
                if (summaryStartMs > previewStartMs)
                {
                    summaryStartMs = previewStartMs;
                }
                if (summaryStopMs < previewStopMs)
                {
                    summaryStopMs = previewStopMs;
                }
                summaryTotalAvg  = summaryTotalAvg + (previewAvgVal * previewDuration);
                summaryTotalTime = summaryTotalTime + previewDuration;
            }
            if ((_startTimeMs == 0) || (_startTimeMs > summaryStartMs))
            {
                _startTimeMs = summaryStartMs;
            }
            if ((_endTimeMs == 0) || (_endTimeMs < summaryStopMs))
            {
                _endTimeMs = summaryStopMs;
            }
            if (summaryTotalTime > 0)
            {
                _summary = new YMeasure(summaryStartMs / 1000.0, summaryStopMs / 1000.0, summaryMinVal, summaryTotalAvg / summaryTotalTime, summaryMaxVal);
            }
            else
            {
                _summary = new YMeasure(0.0, 0.0, YAPI.INVALID_DOUBLE, YAPI.INVALID_DOUBLE, YAPI.INVALID_DOUBLE);
            }
            return(await this.get_progress());
        }
예제 #2
0
        public virtual async Task <int> processMore(int progress, byte[] data)
        {
            YDataStream           stream;
            List <List <double> > dataRows = new List <List <double> >();
            string strdata;
            double tim;
            double itv;
            int    nCols;
            int    minCol;
            int    avgCol;
            int    maxCol;

            if (progress != _progress)
            {
                return(_progress);
            }
            if (_progress < 0)
            {
                strdata = YAPI.DefaultEncoding.GetString(data);
                if (strdata == "{}")
                {
                    _parent._throw(YAPI.VERSION_MISMATCH, "device firmware is too old");
                    return(YAPI.VERSION_MISMATCH);
                }
                return(await this._parse(strdata));
            }
            stream = _streams[_progress];
            stream.imm_parseStream(data);
            dataRows = await stream.get_dataRows();

            _progress = _progress + 1;
            if (dataRows.Count == 0)
            {
                return(await this.get_progress());
            }
            tim = (double)await stream.get_startTimeUTC();

            itv = await stream.get_dataSamplesInterval();

            if (tim < itv)
            {
                tim = itv;
            }
            nCols  = dataRows[0].Count;
            minCol = 0;
            if (nCols > 2)
            {
                avgCol = 1;
            }
            else
            {
                avgCol = 0;
            }
            if (nCols > 2)
            {
                maxCol = 2;
            }
            else
            {
                maxCol = 0;
            }

            for (int ii = 0; ii < dataRows.Count; ii++)
            {
                if ((tim >= _startTime) && ((_endTime == 0) || (tim <= _endTime)))
                {
                    _measures.Add(new YMeasure(tim - itv, tim, dataRows[ii][minCol], dataRows[ii][avgCol], dataRows[ii][maxCol]));
                }
                tim = tim + itv;
                tim = Math.Round(tim * 1000) / 1000.0;
            }
            return(await this.get_progress());
        }