using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace DamLiveSensorSliders { public struct SensorInfo { public string Name; public int MinValue; public int MaxValue; public string SensorFilename; } public partial class MainFormSensorSliders : Form { private int minWaterLevel = 0; private int maxWaterLevel = 300; private int minPolderLevel = -140; private int maxPolderLevel = 30; private const int SensorCount = 2; private const int waterLevelSensorIndex = 0; private const int polderLevelSensorIndex = 1; private const string sensorBaseFilename = "Sensor"; private string workingDirectory; private string sensorMeasurementsDirectory; public double waterLevel; private double polderLevel; private SensorInfo[] sensorInfos; private readonly double[] sensorValues = new double[SensorCount]; public MainFormSensorSliders() { InitializeComponent(); InitializeMeasurementDirectory(); InitializeSensorInfos(); InitializeSensorUi(); } private void UpdateForm() { if (waterLevelSensorIndex < SensorCount) { Sensor01Label.Text = String.Format("{0} = {1:0.00} [m]", sensorInfos[waterLevelSensorIndex].Name, sensorValues[waterLevelSensorIndex]); } if (polderLevelSensorIndex < SensorCount) { Sensor02Label.Text = String.Format("{0} = {1:0.00} [m]", sensorInfos[polderLevelSensorIndex].Name, sensorValues[polderLevelSensorIndex]); } } private void WaterLevelTrackBar_ValueChanged(object sender, EventArgs e) { sensorValues[waterLevelSensorIndex] = 0.01 * Sensor01TrackBar.Value; UpdateSensorMeasurement(waterLevelSensorIndex); UpdateForm(); } private void PolderlevelTrackBar_ValueChanged(object sender, EventArgs e) { sensorValues[polderLevelSensorIndex] = 0.01 * Sensor02TrackBar.Value; UpdateSensorMeasurement(polderLevelSensorIndex); UpdateForm(); } private void InitializeMeasurementDirectory() { workingDirectory = Path.GetFullPath(Properties.Settings.Default.WorkingDirectory); sensorMeasurementsDirectory = Path.GetFullPath(Path.Combine(workingDirectory, Properties.Settings.Default.SensorMeasurementsDirectory)); if (!Directory.Exists(sensorMeasurementsDirectory)) { Directory.CreateDirectory(sensorMeasurementsDirectory); } } private void UpdateSensorMeasurement(int sensorIndex) { using (TextWriter textWriter = new StreamWriter(sensorInfos[sensorIndex].SensorFilename)) { textWriter.WriteLine(String.Format("{0:0.000}", sensorValues[sensorIndex])); textWriter.Close(); }; } private void InitializeSensorInfos() { sensorInfos = new SensorInfo[SensorCount]; if (waterLevelSensorIndex < SensorCount) { sensorInfos[waterLevelSensorIndex] = new SensorInfo() { Name = "Waterlevel", MinValue = 0, MaxValue = 750, SensorFilename = Path.Combine(sensorMeasurementsDirectory, String.Format("{0}{1:00}", sensorBaseFilename, waterLevelSensorIndex)) }; } if (polderLevelSensorIndex < SensorCount) { sensorInfos[polderLevelSensorIndex] = new SensorInfo() { Name = "Polderlevel", MinValue = -140, MaxValue = 30, SensorFilename = Path.Combine(sensorMeasurementsDirectory, String.Format("{0}{1:00}", sensorBaseFilename, polderLevelSensorIndex)) }; } } private void InitializeSensorUi() { Sensor01Label.Visible = false; Sensor01TrackBar.Visible = false; Sensor02Label.Visible = false; Sensor02TrackBar.Visible = false; if (waterLevelSensorIndex < SensorCount) { Sensor01TrackBar.Minimum = sensorInfos[waterLevelSensorIndex].MinValue; Sensor01TrackBar.Maximum = sensorInfos[waterLevelSensorIndex].MaxValue; Sensor01TrackBar.Value = (Sensor01TrackBar.Maximum + Sensor01TrackBar.Minimum)/2; Sensor01Label.Text = sensorInfos[waterLevelSensorIndex].Name; Sensor01Label.Visible = true; Sensor01TrackBar.Visible = true; } if (polderLevelSensorIndex < SensorCount) { Sensor02TrackBar.Minimum = sensorInfos[polderLevelSensorIndex].MinValue; Sensor02TrackBar.Maximum = sensorInfos[polderLevelSensorIndex].MaxValue; Sensor02TrackBar.Value = (Sensor02TrackBar.Maximum + Sensor02TrackBar.Minimum)/2; Sensor02Label.Text = sensorInfos[polderLevelSensorIndex].Name; Sensor02Label.Visible = true; Sensor02TrackBar.Visible = true; } } } }