/// /// Copyright (c) 2015-2020 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using System.Text; using System.Xml.Serialization; namespace Workplace { public enum MassMethod { Scale, Spread, StdDev, Count } /// /// Class to be serialized to an XML file... /// [XmlRootAttribute("Workplace")] public class LocalSettings { static XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeof(LocalSettings) })[0]; /// User interface language (used as CultureInfo(..) constructor argument). public string Language; /// 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 ConnectionString; public string UsersDBConnString; public bool OracleBattery; public bool CheckPcbIsInOracle; public bool SearchOtherProcessFlowtube; public bool SearchForHeTestResultsInOracle; public bool ReadFromOracle; public RecordProcessing.RecordType RecordProcessing; public string WindowClassNameToActivate; public SharedDatabase.Mode Mode; public string WorkplaceId; /// Workplace name usid in Tracing DB for WP identification /// /// Workplace names shown in login dialog, not used in Tracing DB /// public int LocalWorkplacesCount; public string LocalWorkplace1; public string LocalWorkplace2; public string LocalWorkplace3; public string LastOrder; public string LastWorkflow; public string LastWorkstep; public Common.SerializableDictionary LastData; public string LastSiliconData; public DateTime LogoutTime1; public DateTime LogoutTime2; public DateTime LogoutTime3; public int BlackListIx1; public string BlackListFile1; public int BlackListIx2; public string BlackListFile2; public int BlackListIx3; public string BlackListFile3; public int ScaleSerialPort; public MassMethod MassMethod; public int MassRepeats; public double MassSpread; public double MinWeight; public double MaxWeight; public string WeightOptionsWay; public int OkFlashDuration; public int NokFlashDuration; public LocalSettings() { LastData = new Common.SerializableDictionary(); } public LocalSettings(bool createNew) : this() { if (createNew) { Language = "EN"; #if DEBUG ConnectionString = "SERVER=localhost; DATABASE=nanjingshared; UID=root; PASSWORD=kraken; CHARSET=utf8"; UsersDBConnString = "SERVER=localhost; DATABASE=nanjingshared; UID=root; PASSWORD=kraken; CHARSET=utf8;"; #else ConnectionString = "SERVER=10.116.192.200; DATABASE=nanjingshared; UID=nanjingshared; PASSWORD=C1ern4V0d4; CHARSET=utf8"; UsersDBConnString = "SERVER=10.116.192.200; DATABASE=nanjingshared; UID=nanjingshared; PASSWORD=C1ern4V0d4; CHARSET=utf8;"; #endif Mode = SharedDatabase.Mode.Production; #if SAVE_COMPONENTS_TO_ORACLE OracleBattery = true; CheckPcbIsInOracle = true; WorkplaceId = "Assembly 1"; #else OracleBattery = false; CheckPcbIsInOracle = false; WorkplaceId = "Assembly 1"; #endif LocalWorkplacesCount = 1; LocalWorkplace1 = WorkplaceId; LocalWorkplace2 = string.Empty; LocalWorkplace3 = string.Empty; BlackListIx1 = 0; BlackListIx2 = 0; BlackListIx3 = 0; BlackListFile1 = string.Empty; BlackListFile2 = string.Empty; BlackListFile3 = string.Empty; MassRepeats = 5; OkFlashDuration = 500; NokFlashDuration = 3000; } } /// /// Load public fields of this class from the XML file. /// /// LocalSettings object or null when Load() fails public static LocalSettings Load(string fileName) { try { bool isEncrypted = true; using (StreamReader reader = new StreamReader(fileName)) { isEncrypted = !reader.ReadLine().StartsWith(" /// Save public fields of this class to the XML file. /// 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; } } /// /// Updates history stored in a string array by a new latest string. /// /// Last entered string /// true = updated and saved public bool UpdateHistory(string lastStrValue, ref string[] history) { const int MaxHistoryLen = 10; if (string.IsNullOrEmpty(lastStrValue)) return false; int currentHistoryLength = (history != null) ? history.Length : 0; int match = -1; for (int i = 0; i < currentHistoryLength; i++) { if (history[i] == lastStrValue) { match = i; break; } } if (match >= 0 || currentHistoryLength >= MaxHistoryLen) { /// History does not have to be extended (because of a match) or should not be extended (because of the lenght) if (match < 0) match = currentHistoryLength - 1; for (int j = match; j > 0; j--) { history[j] = history[j - 1]; } history[0] = lastStrValue; } else { /// History wiil be extended, new item inserted at the beginning string[] newHistory = new string[currentHistoryLength + 1]; newHistory[0] = lastStrValue; for (int j = 1; j <= currentHistoryLength; j++) { newHistory[j] = history[j - 1]; } history = newHistory; } Save(); return true; } } }