using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Deltares.Dam.DamLiveShowcase
{
public partial class MainFormDamLiveShowcase : Form
{
private const int timeInterval = 15000; // ms
private readonly IDamLiveProcessor damLiveProcessor = new DemoDamLiveProcessor();
private string workingDirectory;
private string sensorMeasurementsDirectory;
///
/// Initializes a new instance of the class.
///
public MainFormDamLiveShowcase()
{
InitializeComponent();
InitializeWorkingDirectories();
String executableDirectory = Path.GetDirectoryName(Application.ExecutablePath);
damLiveProcessor = new DamLiveProcessor()
{
DamLiveExecutableFilename = Path.Combine(executableDirectory, "DamLive.exe"),
WorkingDirectory = workingDirectory,
DamProjectFilename = "input.damx",
InputTimeSerieFilename = "live.InputTimeSeries.xml",
OutputTimeSerieFilename = "live.OutputTimeSeries.xml",
CalculationParametersFilename = "live.ParametersFile.xml",
SensorMeasurementsDirectory = sensorMeasurementsDirectory,
SensorChangeTolerance = Properties.Settings.Default.SensorChangeTolerance
};
damLiveProcessor.Initialize();
}
///
/// Initializes the working directories.
///
private void InitializeWorkingDirectories()
{
// Set and check working directory
workingDirectory = Path.GetFullPath(Properties.Settings.Default.WorkingDirectory);
if (!Directory.Exists(workingDirectory))
{
// initialize for development environment
String executableDirectory = Path.GetDirectoryName(Application.ExecutablePath);
workingDirectory = Path.GetFullPath(executableDirectory + @"\..\..\..\data\Dam\DamLive\Showcase\working_dir");
}
if (!Directory.Exists(workingDirectory))
{
throw new DirectoryNotFoundException(String.Format("DamLive data not found: {0}", Properties.Settings.Default.WorkingDirectory));
}
// Set and check measurements data directory
sensorMeasurementsDirectory = Path.GetFullPath(Path.Combine(workingDirectory, Properties.Settings.Default.SensorMeasurementsDirectory));
if (!Directory.Exists(sensorMeasurementsDirectory))
{
throw new DirectoryNotFoundException(String.Format("Sensor measurements data directory not found: {0}", Properties.Settings.Default.WorkingDirectory));
}
}
///
/// Handles the Shown event of the MainWindowForm control.
///
/// The source of the event.
/// The instance containing the event data.
private void MainWindowForm_Shown(object sender, EventArgs e)
{
CalculationTimer.Interval = timeInterval;
CalculationTimer.Start();
// this.WindowState = FormWindowState.Maximized;
TimeSerieChart.Visible = false; // make visible when it is functional
PerformCalculation();
}
///
/// Handles the Tick event of the CalculationTimer control.
///
/// The source of the event.
/// The instance containing the event data.
private void CalculationTimer_Tick(object sender, EventArgs e)
{
PerformCalculation();
}
///
/// Updates the time step data.
///
/// The time step data.
private void UpdateTimeStep(TimeStepData timeStepData)
{
bool fileExists = !String.IsNullOrEmpty(timeStepData.SlidingCurveFilename) && File.Exists(timeStepData.SlidingCurveFilename);
if (fileExists)
{
Metafile metafile = new Metafile(timeStepData.SlidingCurveFilename);
System.Drawing.Graphics graphics = SlidingCurvePanel.CreateGraphics();
RectangleF destinationRect = new RectangleF(0, 0, SlidingCurvePanel.Width, SlidingCurvePanel.Height);
RectangleF sourceRect = new RectangleF(0, 0, metafile.Width, metafile.Height);
graphics.Clear(SlidingCurvePanel.BackColor);
graphics.DrawImage(metafile, destinationRect, sourceRect, GraphicsUnit.Pixel);
}
TimeStepLabel.Text = String.Format("Time = {0:T}", timeStepData.Time);
WaterLevelLabel.Text = String.Format("Water level = {0:0.000}", timeStepData.WaterLevel);
PolderLevelLabel.Text = String.Format("Polder level = {0:0.000}", timeStepData.PolderLevel);
SensorValueLabel.Text = String.Format("Sensor value = {0:0.000}", timeStepData.SensorValue);
SafetyFactorLabel.Text = String.Format("Safetyfactor = {0:0.000}", timeStepData.SafetyFactor);
}
///
/// Performs the calculation.
///
private void PerformCalculation()
{
var timeStep = damLiveProcessor.CalculateTimestep();
if (timeStep != null)
{
UpdateTimeStep(timeStep);
}
}
}
}