205 lines
7.9 KiB
C#
205 lines
7.9 KiB
C#
///
|
|
/// Copyright (c) 2019-2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace GenericTest
|
|
{
|
|
/// <summary>
|
|
/// Class to be serialized to an XML file...
|
|
/// </summary>
|
|
[XmlRootAttribute("GenericTesterTracer")]
|
|
public class LocalSettings
|
|
{
|
|
static XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeof(LocalSettings) })[0];
|
|
|
|
/// Configuration file ecryption/decryption key and initialization vector
|
|
static byte[] key = new byte[] { 83, 254, 105, 64, 184, 201, 195, 127, 52, 99, 77, 45, 252, 132, 163, 156 };
|
|
static byte[] IV = new byte[] { 189, 23, 137, 56, 204, 241, 242, 118, 28, 89, 9, 198, 224, 111, 186, 125 };
|
|
|
|
|
|
public string Workplace;
|
|
public bool IsUserLoginRequired;
|
|
|
|
public string ConnectionString;
|
|
public string UsersDBConnString;
|
|
public string TestResultsFolder;
|
|
public string ArchiveFolder;
|
|
public bool DoCheckSNsValidity;
|
|
public string ValidSNPrefixes;
|
|
public bool DoCheckPreviousTracingRecords;
|
|
public bool DoSaveTestResultToOracle;
|
|
public TracingDB.Mode OracleMode;
|
|
public bool DoPrintLabels;
|
|
public bool DoPrintGoodLabelsOnly;
|
|
public bool MaximizeWindowOnError;
|
|
|
|
public bool DoReadSensor;
|
|
public string SensorName;
|
|
public int SensorComPortNr;
|
|
public int SensorAddress;
|
|
public int SensorFilterTimeMinutes;
|
|
|
|
/// MainWnd
|
|
public bool MainWndMaximized;
|
|
public int MainWndLeft;
|
|
public int MainWndTop;
|
|
public int MainWndWidth;
|
|
public int MainWndHeight;
|
|
|
|
|
|
public LocalSettings()
|
|
{
|
|
}
|
|
|
|
public LocalSettings(bool createNew)
|
|
: this()
|
|
{
|
|
if (createNew)
|
|
{
|
|
/// Default settings
|
|
Workplace = GenericTestDlg.DfltWorkplaceName;
|
|
|
|
ConnectionString = "SERVER=10.42.128.24; DATABASE=sledovanie2019; UID=vyroba2019; PASSWORD=qwerty; CHARSET=utf8";
|
|
UsersDBConnString = "SERVER=10.42.128.24; DATABASE=st_wr_shared_config; UID=u9305784; PASSWORD=p89344390; CHARSET=utf8;";
|
|
TestResultsFolder = "C:\\FTS\\HookSpool\\Filemover";
|
|
ArchiveFolder = "C:\\FTS\\HookSpool\\Filemover";
|
|
DoCheckSNsValidity = false;
|
|
ValidSNPrefixes = "297,298,299,300,301,302";
|
|
DoCheckPreviousTracingRecords = true;
|
|
DoSaveTestResultToOracle = true;
|
|
OracleMode = TracingDB.Mode.Production;
|
|
DoPrintLabels = true;
|
|
DoPrintGoodLabelsOnly = true;
|
|
MaximizeWindowOnError = false;
|
|
|
|
MainWndMaximized = false;
|
|
MainWndWidth = 1000;
|
|
MainWndHeight = 260;
|
|
MainWndLeft = 50;
|
|
MainWndTop = 50;
|
|
|
|
DoReadSensor = false;
|
|
SensorName = "Temp.";
|
|
SensorComPortNr = 4;
|
|
SensorAddress = 1;
|
|
SensorFilterTimeMinutes = 15;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load public fields of this class from the XML file.
|
|
/// </summary>
|
|
/// <returns>LocalSettings object or null when Load() fails</returns>
|
|
public static LocalSettings Load(string fileName)
|
|
{
|
|
try
|
|
{
|
|
bool isEncrypted = true;
|
|
using (StreamReader reader = new StreamReader(fileName))
|
|
{
|
|
isEncrypted = !reader.ReadLine().StartsWith("<?xml version=");
|
|
reader.Close();
|
|
}
|
|
|
|
if (isEncrypted)
|
|
{
|
|
/// Write settings to a string
|
|
StringBuilder plain = new StringBuilder();
|
|
|
|
using (RijndaelManaged aes = new RijndaelManaged { Padding = PaddingMode.Zeros })
|
|
{
|
|
using (FileStream fsCrypt = new FileStream(fileName, FileMode.Open))
|
|
{
|
|
using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
|
|
{
|
|
using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
|
|
{
|
|
using (MemoryStream mStream = new MemoryStream())
|
|
{
|
|
using (var writer = new BinaryWriter(mStream))
|
|
{
|
|
int data;
|
|
while ((data = cs.ReadByte()) != -1)
|
|
{
|
|
if (data != 0) writer.Write((byte)data);
|
|
}
|
|
writer.Flush();
|
|
mStream.Position = 0;
|
|
|
|
using (var reader = new StreamReader(mStream))
|
|
{
|
|
return serializer.Deserialize(reader) as LocalSettings;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
using (StreamReader reader = new StreamReader(fileName))
|
|
{
|
|
return serializer.Deserialize(reader) as LocalSettings;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
string msg = e.Message;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Save public fields of this class to the XML file.
|
|
/// </summary>
|
|
public void Save()
|
|
{
|
|
try
|
|
{
|
|
/// Encrypt and write to file 'Program.LocalSettingsFileName'
|
|
using (RijndaelManaged aes = new RijndaelManaged { Padding = PaddingMode.Zeros })
|
|
{
|
|
using (FileStream fsCrypt = new FileStream(Program.LocalSettingsFileName, FileMode.Create))
|
|
{
|
|
using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
|
|
{
|
|
using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
|
|
{
|
|
using (MemoryStream mStream = new MemoryStream())
|
|
{
|
|
using (var writer = new StreamWriter(mStream))
|
|
{
|
|
/// Serialize and write settings to a memory stream
|
|
serializer.Serialize(writer, this);
|
|
writer.Flush();
|
|
mStream.Position = 0;
|
|
|
|
using (var reader = new StreamReader(mStream))
|
|
{
|
|
/// Encrypt and write to file 'Program.LocalSettingsFileName'
|
|
int data;
|
|
while ((data = mStream.ReadByte()) != -1) cs.WriteByte((byte)data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
string msg = e.Message;
|
|
}
|
|
}
|
|
}
|
|
}
|