コード例 #1
0
        private void ProcessTSSCMeasurements(IFrame frame)
        {
            lock (m_tsscSyncLock)
            {
                try
                {
                    if (!Enabled)
                    {
                        return;
                    }

                    if ((object)m_tsscEncoder == null || m_resetTsscEncoder)
                    {
                        m_resetTsscEncoder  = false;
                        m_tsscEncoder       = new TsscEncoder();
                        m_tsscWorkingBuffer = new byte[32 * 1024];
                        OnStatusMessage(MessageLevel.Info, $"TSSC algorithm reset before sequence number: {m_tsscSequenceNumber}", "TSSC");
                        m_tsscSequenceNumber = 0;
                        m_tsscEncoder.SetBuffer(m_tsscWorkingBuffer, 0, m_tsscWorkingBuffer.Length);
                    }
                    else
                    {
                        m_tsscEncoder.SetBuffer(m_tsscWorkingBuffer, 0, m_tsscWorkingBuffer.Length);
                    }

                    int count = 0;

                    foreach (IMeasurement measurement in frame.Measurements.Values)
                    {
                        ushort index = m_signalIndexCache.GetSignalIndex(measurement.Key);

                        if (!m_tsscEncoder.TryAddMeasurement(index, measurement.Timestamp.Value, (uint)measurement.StateFlags, (float)measurement.AdjustedValue))
                        {
                            SendTSSCPayload(frame.Timestamp, count);
                            count = 0;
                            m_tsscEncoder.SetBuffer(m_tsscWorkingBuffer, 0, m_tsscWorkingBuffer.Length);

                            // This will always succeed
                            m_tsscEncoder.TryAddMeasurement(index, measurement.Timestamp.Value, (uint)measurement.StateFlags, (float)measurement.AdjustedValue);
                        }

                        count++;
                    }

                    if (count > 0)
                    {
                        SendTSSCPayload(frame.Timestamp, count);
                    }

                    // Update latency statistics
                    long publishTime = DateTime.UtcNow.Ticks;
                    m_parent.UpdateLatencyStatistics(frame.Measurements.Values.Select(m => (long)(publishTime - m.Timestamp)));
                }
                catch (Exception ex)
                {
                    string message = $"Error processing measurements: {ex.Message}";
                    OnProcessException(MessageLevel.Info, new InvalidOperationException(message, ex));
                }
            }
        }
コード例 #2
0
        public void TestReading1()
        {
            //Just adds metadata
            var r = new Randomizer(3);

            byte[] buffer = new byte[65536];
            int    endPosition;

            {
                var comp = new TsscEncoder();
                comp.SetBuffer(buffer, 0, 65530);
                for (int x = 0; x < 1000; x++)
                {
                    if (!comp.TryAddMeasurement(r.GetUInt16(), r.GetDate(), r.GetUInt16(), r.GetUInt16()))
                    {
                        throw new Exception();
                    }
                }
                endPosition = comp.FinishBlock();
            }

            r.Reset();

            {
                ushort id;
                long   timestamp;
                uint   quality;
                float  value;

                var reading = new TsscDecoder();
                reading.SetBuffer(buffer, 0, endPosition);

                for (int x = 0; x < 1000; x++)
                {
                    if (!reading.TryGetMeasurement(out id, out timestamp, out quality, out value))
                    {
                        throw new ArgumentOutOfRangeException();
                    }
                    if (id != r.GetUInt16())
                    {
                        throw new Exception();
                    }
                    if (timestamp != r.GetDate())
                    {
                        throw new Exception();
                    }
                    if (quality != r.GetUInt16())
                    {
                        throw new Exception();
                    }
                    if (value != r.GetUInt16())
                    {
                        throw new Exception();
                    }
                }

                if (reading.TryGetMeasurement(out id, out timestamp, out quality, out value))
                {
                    throw new ArgumentOutOfRangeException();
                }
                if (reading.TryGetMeasurement(out id, out timestamp, out quality, out value))
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
コード例 #3
0
        private static void TestEncoding(TsscEncoder encoder, TsscDecoder decoder, TsscEncoder encoder2, byte[] writeBuffer, byte randomValue, OrigReader reader2, ushort idOld, long timestampOld, uint qualityOld, float valueOld)
        {
            ushort id2;
            long   timestamp2;
            uint   quality2;
            float  value2;

            ushort id3        = 0;
            long   timestamp3 = 0;
            uint   quality3   = 0;
            float  value3     = 0;
            int    flushPosition;

            flushPosition = encoder.FinishBlock();
            Array.Clear(writeBuffer, flushPosition, writeBuffer.Length - flushPosition);

            byte[] writeBuffer2 = new byte[writeBuffer.Length];
            encoder2.SetBuffer(writeBuffer2, randomValue, writeBuffer2.Length - randomValue);

            decoder.SetBuffer(writeBuffer, randomValue, flushPosition - randomValue);

            while (decoder.TryGetMeasurement(out id2, out timestamp2, out quality2, out value2))
            {
                if (!encoder2.TryAddMeasurement(id2, timestamp2, quality2, value2))
                {
                    throw new Exception();
                }
                if (!reader2.ReadNextMeasurement(out id3, out timestamp3, out quality3, out value3))
                {
                    throw new Exception();
                }
                if (id2 != id3)
                {
                    throw new Exception();
                }
                if (timestamp2 != timestamp3)
                {
                    throw new Exception();
                }
                if (quality2 != quality3)
                {
                    throw new Exception();
                }
                if (value2 != value3)
                {
                    throw new Exception();
                }
            }

            if (idOld != id3)
            {
                throw new Exception();
            }
            if (timestampOld != timestamp3)
            {
                throw new Exception();
            }
            if (qualityOld != quality3)
            {
                throw new Exception();
            }
            if (valueOld != value3)
            {
                throw new Exception();
            }

            if (flushPosition != encoder2.FinishBlock())
            {
                throw new Exception();
            }

            for (int x = 0; x < writeBuffer.Length; x++)
            {
                if (writeBuffer[x] != writeBuffer2[x])
                {
                    throw new Exception();
                }
            }
        }
コード例 #4
0
        //[TestMethod]
        public unsafe void TestSmallerSegments()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            long totalSize                     = 0;
            long measurementsProcessed         = 0;
            int  quitAfterCount                = 0;
            Dictionary <Guid, ushort> idLookup = new Dictionary <Guid, ushort>();

            var encoder  = new TsscEncoder();
            var encoder2 = new TsscEncoder();
            var decoder  = new TsscDecoder();

            foreach (var file in Directory.GetFiles(@"D:\TsscTest", "*.PhasorStream"))
            {
                if (quitAfterCount == 1)
                {
                    return;
                }
                quitAfterCount++;

                byte[] data    = File.ReadAllBytes(file);
                var    reader1 = new OrigReader(data, idLookup);
                var    reader2 = new OrigReader(data, idLookup);

                byte   randomValue = Security.Cryptography.Random.ByteBetween(1, 200);
                byte[] writeBuffer = new byte[randomValue * 1024];
                encoder.SetBuffer(writeBuffer, randomValue, writeBuffer.Length - randomValue);

                ushort id;
                long   timestamp;
                uint   quality;
                float  value;

                ushort idOld        = 0;
                long   timestampOld = 0;
                uint   qualityOld   = 0;
                float  valueOld     = 0;

                int quitAfter = Security.Cryptography.Random.ByteBetween(0, 40);
                while (reader1.ReadNextMeasurement(out id, out timestamp, out quality, out value))
                {
                    measurementsProcessed++;
                    if (quitAfter <= 0 || !encoder.TryAddMeasurement(id, timestamp, quality, value))
                    {
                        totalSize += encoder.FinishBlock();
                        TestEncoding(encoder, decoder, encoder2, writeBuffer, randomValue, reader2, idOld, timestampOld, qualityOld, valueOld);

                        randomValue = Security.Cryptography.Random.ByteBetween(1, 200);
                        writeBuffer = new byte[randomValue * 1024];
                        encoder.SetBuffer(writeBuffer, randomValue, writeBuffer.Length - randomValue);
                        if (!encoder.TryAddMeasurement(id, timestamp, quality, value))
                        {
                            throw new Exception();
                        }

                        quitAfter = Security.Cryptography.Random.ByteBetween(0, 40);
                    }

                    quitAfter--;

                    idOld        = id;
                    timestampOld = timestamp;
                    qualityOld   = quality;
                    valueOld     = value;
                }

                totalSize += encoder.FinishBlock();
                TestEncoding(encoder, decoder, encoder2, writeBuffer, randomValue, reader2, idOld, timestampOld, qualityOld, valueOld);

                System.Console.WriteLine(measurementsProcessed.ToString("N0") + " " + file + " " + sw.Elapsed.TotalSeconds.ToString("N1") + " " + (totalSize * 8 / (double)measurementsProcessed).ToString("N2"));
            }
        }