tbf/Statistics/LocalSettings.cs

258 lines
11 KiB
C#

///
/// Copyright (c) 2018 Sensus Slovensko a.s.
///
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace Statistics
{
/// <summary>
/// Class to be serialized to an XML file...
/// Public members are local settings.
/// These settings are modified by dialogs invoked by menu items
/// Edit->Preferences ... PreferencesDlg ... S1.1(language, etc. local settings), and
/// Edit->Advanced settings ... AdvancedSettingsDlg ... S1.2(spec. where bench settings are stored)
/// </summary>
[XmlRootAttribute("Statistics")]
public class LocalSettings
{
public const int BenchesCount = 6;
public const int FlowsCount = 5;
/// <summary>
/// User interface language (used as CultureInfo(..) constructor argument).
/// </summary>
public string Language;
///
/// Names of test benches
///
public string[] BenchNames;
///
public string GetBenchName(int i)
{
return ((BenchNames != null) && (i >= 0) && (i < BenchNames.Length)) ? BenchNames[i] : string.Empty;
}
///
/// Database settings of servers
///
public string[] ConnectionStrings;
///
public string GetConnectionString(int i)
{
return ((ConnectionStrings != null) && (i >= 0) && (i < ConnectionStrings.Length)) ? ConnectionStrings[i] : string.Empty;
}
///
/// Flow settings
///
public bool[] FlowEnabledWMs;
public bool[] FlowEnabledI1;
public bool[] FlowEnabledI2;
public bool[] FlowEnabledI3;
public bool[] FlowEnabledI4;
///
public bool GetFlowEnabled(UserControls.MidId midId, int i)
{
switch (midId)
{
case UserControls.MidId.WaterMeters:
return ((FlowEnabledWMs != null) && (i >= 0) && (i < FlowEnabledWMs.Length)) ? FlowEnabledWMs[i] : false;
case UserControls.MidId.I1: return ((FlowEnabledI1 != null) && (i >= 0) && (i < FlowEnabledI1.Length)) ? FlowEnabledI1[i] : false;
case UserControls.MidId.I2: return ((FlowEnabledI2 != null) && (i >= 0) && (i < FlowEnabledI2.Length)) ? FlowEnabledI2[i] : false;
case UserControls.MidId.I3: return ((FlowEnabledI3 != null) && (i >= 0) && (i < FlowEnabledI3.Length)) ? FlowEnabledI3[i] : false;
case UserControls.MidId.I4: return ((FlowEnabledI4 != null) && (i >= 0) && (i < FlowEnabledI4.Length)) ? FlowEnabledI4[i] : false;
default:
return false;
}
}
///
public void SetFlowEnabled(UserControls.MidId midId, int i, bool value)
{
switch (midId)
{
case UserControls.MidId.WaterMeters:
if ((FlowEnabledWMs != null) && (i >= 0) && (i < FlowEnabledWMs.Length)) { FlowEnabledWMs[i] = value; };
break;
case UserControls.MidId.I1: if ((FlowEnabledI1 != null) && (i >= 0) && (i < FlowEnabledI1.Length)) { FlowEnabledI1[i] = value; }; break;
case UserControls.MidId.I2: if ((FlowEnabledI2 != null) && (i >= 0) && (i < FlowEnabledI2.Length)) { FlowEnabledI2[i] = value; }; break;
case UserControls.MidId.I3: if ((FlowEnabledI3 != null) && (i >= 0) && (i < FlowEnabledI3.Length)) { FlowEnabledI3[i] = value; }; break;
case UserControls.MidId.I4: if ((FlowEnabledI4 != null) && (i >= 0) && (i < FlowEnabledI4.Length)) { FlowEnabledI4[i] = value; }; break;
default:
break;
}
}
public double[] FlowFromWMs;
public double[] FlowFromI1;
public double[] FlowFromI2;
public double[] FlowFromI3;
public double[] FlowFromI4;
///
public double GetFlowFrom(UserControls.MidId midId, int i)
{
switch (midId)
{
case UserControls.MidId.WaterMeters:
return ((FlowFromWMs != null) && (i >= 0) && (i < FlowFromWMs.Length)) ? FlowFromWMs[i] : 0;
case UserControls.MidId.I1: return ((FlowFromI1 != null) && (i >= 0) && (i < FlowFromI1.Length)) ? FlowFromI1[i] : 0;
case UserControls.MidId.I2: return ((FlowFromI2 != null) && (i >= 0) && (i < FlowFromI2.Length)) ? FlowFromI2[i] : 0;
case UserControls.MidId.I3: return ((FlowFromI3 != null) && (i >= 0) && (i < FlowFromI3.Length)) ? FlowFromI3[i] : 0;
case UserControls.MidId.I4: return ((FlowFromI4 != null) && (i >= 0) && (i < FlowFromI4.Length)) ? FlowFromI4[i] : 0;
default:
return 0;
}
}
///
public void SetFlowFrom(UserControls.MidId midId, int i, double value)
{
switch (midId)
{
case UserControls.MidId.WaterMeters:
if ((FlowFromWMs != null) && (i >= 0) && (i < FlowFromWMs.Length)) { FlowFromWMs[i] = value; };
break;
case UserControls.MidId.I1: if ((FlowFromI1 != null) && (i >= 0) && (i < FlowFromI1.Length)) { FlowFromI1[i] = value; }; break;
case UserControls.MidId.I2: if ((FlowFromI2 != null) && (i >= 0) && (i < FlowFromI2.Length)) { FlowFromI2[i] = value; }; break;
case UserControls.MidId.I3: if ((FlowFromI3 != null) && (i >= 0) && (i < FlowFromI3.Length)) { FlowFromI3[i] = value; }; break;
case UserControls.MidId.I4: if ((FlowFromI4 != null) && (i >= 0) && (i < FlowFromI4.Length)) { FlowFromI4[i] = value; }; break;
default:
break;
}
}
public double[] FlowToWMs;
public double[] FlowToI1;
public double[] FlowToI2;
public double[] FlowToI3;
public double[] FlowToI4;
///
public double GetFlowTo(UserControls.MidId midId, int i)
{
switch (midId)
{
case UserControls.MidId.WaterMeters:
return ((FlowToWMs != null) && (i >= 0) && (i < FlowToWMs.Length)) ? FlowToWMs[i] : 0;
case UserControls.MidId.I1: return ((FlowToI1 != null) && (i >= 0) && (i < FlowToI1.Length)) ? FlowToI1[i] : 0;
case UserControls.MidId.I2: return ((FlowToI2 != null) && (i >= 0) && (i < FlowToI2.Length)) ? FlowToI2[i] : 0;
case UserControls.MidId.I3: return ((FlowToI3 != null) && (i >= 0) && (i < FlowToI3.Length)) ? FlowToI3[i] : 0;
case UserControls.MidId.I4: return ((FlowToI4 != null) && (i >= 0) && (i < FlowToI4.Length)) ? FlowToI4[i] : 0;
default:
return 0;
}
}
///
public void SetFlowTo(UserControls.MidId midId, int i, double value)
{
switch (midId)
{
case UserControls.MidId.WaterMeters:
if ((FlowToWMs != null) && (i >= 0) && (i < FlowToWMs.Length)) { FlowToWMs[i] = value; };
break;
case UserControls.MidId.I1: if ((FlowToI1 != null) && (i >= 0) && (i < FlowToI1.Length)) { FlowToI1[i] = value; }; break;
case UserControls.MidId.I2: if ((FlowToI2 != null) && (i >= 0) && (i < FlowToI2.Length)) { FlowToI2[i] = value; }; break;
case UserControls.MidId.I3: if ((FlowToI3 != null) && (i >= 0) && (i < FlowToI3.Length)) { FlowToI3[i] = value; }; break;
case UserControls.MidId.I4: if ((FlowToI4 != null) && (i >= 0) && (i < FlowToI4.Length)) { FlowToI4[i] = value; }; break;
default:
break;
}
}
/// StatisticsDlg UI
public int SelectedRadioButtonNumber;
/// MainWnd
public int MainWndLeft;
public int MainWndTop;
public int MainWndWidth;
public int MainWndHeight;
public bool ManiWndMaximized;
public int SplitterDistance;
/// <summary>
/// Default constructor which is used to create default settings
/// </summary>
public LocalSettings()
{
Language = "SK";
BenchNames = new string[] { "WR10", "WR11", "WR13", "WR14", "WR15", "PT7" };
ConnectionStrings = new string[]
{
"SERVER=10.42.128.93; DATABASE=st-wr10-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;",
"SERVER=10.42.128.102; DATABASE=st-wr11-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;",
"SERVER=10.42.128.89; DATABASE=st-wr13-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;",
"SERVER=10.42.128.213; DATABASE=st-wr14-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;",
"SERVER=10.42.128.214; DATABASE=st-wr15-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;",
"SERVER=10.42.129.26; DATABASE=iperlspecial-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;",
};
FlowEnabledWMs = new bool[FlowsCount];
FlowEnabledI1 = new bool[FlowsCount];
FlowEnabledI2 = new bool[FlowsCount];
FlowEnabledI3 = new bool[FlowsCount];
FlowEnabledI4 = new bool[FlowsCount];
FlowFromWMs = new double[FlowsCount];
FlowFromI1 = new double[FlowsCount];
FlowFromI2 = new double[FlowsCount];
FlowFromI3 = new double[FlowsCount];
FlowFromI4 = new double[FlowsCount];
FlowToWMs = new double[FlowsCount];
FlowToI1 = new double[FlowsCount];
FlowToI2 = new double[FlowsCount];
FlowToI3 = new double[FlowsCount];
FlowToI4 = new double[FlowsCount];
SelectedRadioButtonNumber = 1;
}
/// <summary>
/// Load local settings (public fields of this class) from the XML file.
/// </summary>
public static LocalSettings Load(string fileName)
{
try
{
using (TextReader reader = new StreamReader(fileName))
{
LocalSettings ls = (new XmlSerializer(typeof(LocalSettings))).Deserialize(reader) as LocalSettings;
// Copy the settings just in case De-serialize() completes OK
return ls;
}
}
catch (Exception e)
{
string msg = e.Message;
return null;
}
}
/// <summary>
/// Save local settings (public fields of this class) to the XML file.
/// </summary>
public void Save()
{
try
{
using (TextWriter writer = new StreamWriter(Program.LocalSettingsFileName))
{
(new XmlSerializer(typeof(LocalSettings))).Serialize(writer, this);
}
}
catch (Exception e)
{
string msg = e.Message;
}
}
}
}