tbf/TBF/BenchControl/Sequences/Plotter.cs

81 lines
2.4 KiB
C#

///
/// Copyright (c) 2018 Sensus Slovensko a.s.
///
using System;
using System.IO;
using System.Collections.Generic;
using log4net;
namespace TBF.BenchControl.Sequences
{
public class Plotter : BenchControl.GenericDevices.IPlotter
{
static readonly ILog log = LogManager.GetLogger(typeof(Plotter));
static readonly Dictionary<int, BinaryWriter> writers = new Dictionary<int, BinaryWriter>();
static int nextGraphId = 1;
readonly string caption;
///
public Plotter(string caption)
{
this.caption = caption;
}
public int StartGraph(int batchNr, string testName, int repetition)
{
try
{
int graphId = nextGraphId++;
string directory = Path.Combine(TBF.Program.GraphsDir, batchNr.ToString(), testName, repetition.ToString());
Directory.CreateDirectory(directory);
BinaryWriter writer = new BinaryWriter(File.Open(Path.Combine(directory, caption), FileMode.Create));
writers.Add(graphId, writer);
log.InfoFormat("Graph file #{0} successfully created (batch={1} test={2} repet={3} caption={4})",
graphId, batchNr, testName, repetition, caption);
return graphId;
}
catch (Exception exc)
{
log.ErrorFormat("Failed to create a graph file (batch={0} test={1} repet={2} caption={3}): {4}",
batchNr, testName, repetition, caption, exc.Message);
return 0;
}
}
public void UpdateGraph(int graphId, float x, float y)
{
try
{
BinaryWriter writer = writers[graphId];
writer.Write(x);
writer.Write(y);
}
catch (Exception exc)
{
log.ErrorFormat("Failed to update graph file #{0}: {1}", graphId, exc.Message);
}
}
public void StopGraph(int graphId, float ymin, float ymax)
{
try
{
BinaryWriter writer = writers[graphId];
writer.Write(ymin);
writer.Write(ymax);
writer.Close();
writers.Remove(graphId);
}
catch (Exception exc)
{
log.ErrorFormat("Failed to close graph file #{0}: {1}", graphId, exc.Message);
}
}
}
}