Repository navigation
Expand file tree
/
Copy pathreverse-iteration.cs
More file actions
67 lines (55 loc) · 2.19 KB
/
Copy pathreverse-iteration.cs
File metadata and controls
67 lines (55 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// SQL Race Example: SQL Race Example: Reverse Iteration
//
// Demonstrates: Using GoTo and GetNextSamples with StepDirection.Reverse to
// read data backwards from the end of a session
// Prerequisites: MESL.SQLRace.API, a session with data
// Expected output: Samples printed in reverse chronological order
// Related: read-samples-between.cs, bulk-parameter-read.cs
//
using MAT.OCS.Core;
using MESL.SqlRace.Domain;
using MESL.SqlRace.Domain.Infrastructure.Enumerators;
Core.LicenceProgramName = "SQLRace";
Core.Initialize();
var connectionString = $"DbEngine=SQLite;Data Source={Path.Combine(Path.GetTempPath(), "sqlrace-examples.ssn2")};";
if (args.Length == 0)
{
Console.WriteLine("Usage: dotnet run -- <session-guid> [parameter-identifier]");
return;
}
SessionKey sessionKey;
try { sessionKey = SessionKey.Parse(args[0]); }
catch (Exception ex) { Console.WriteLine($"ERROR: '{args[0]}' is not a valid GUID. {ex.Message}"); return; }
var paramId = args.Length > 1 ? args[1] : "Temperature:Sensors";
var sessionManager = SessionManager.CreateSessionManager();
IClientSession clientSession;
try { clientSession = sessionManager.Load(sessionKey, connectionString); }
catch (Exception ex)
{
Console.WriteLine($"ERROR: Could not load session '{sessionKey}': {ex.Message}");
return;
}
using (clientSession)
{
var session = clientSession.Session;
if (!session.Parameters.Any(p => p.Identifier == paramId))
{
Console.WriteLine($"ERROR: Parameter '{paramId}' not found in session.");
Console.WriteLine($"Available: {string.Join(", ", session.Parameters.Take(10).Select(p => p.Identifier))}");
return;
}
using var pda = session.CreateParameterDataAccess(paramId);
// --- Position the PDA at the end of the session ---
pda.GoTo(session.EndTime);
// --- Read 10 samples backwards ---
var samples = pda.GetNextSamples(10, StepDirection.Reverse);
Console.WriteLine($"Parameter: {paramId}");
Console.WriteLine($"Last {samples.SampleCount} samples (reverse order):");
Console.WriteLine($"{"Timestamp (ns)",20} {"Value",12}");
Console.WriteLine(new string('-', 34));
for (var i = 0; i < samples.SampleCount; i++)
{
Console.WriteLine($"{samples.Timestamp[i],20} {samples.Data[i],12:F4}");
}
}