private FileInfo BuildFileInfo(string fileName) { FileInfo fileInfo = new FileInfo(); string filename = settings.labelFolder + @"\" + fileName + ".lbl"; try { fileInfo.contents = File.ReadAllText(filename); } catch (System.IO.IOException exp) { MessageLogger.Add(string.Format("Label file {0} couldn't be opened. Msg {1}", filename, exp.ToString()), MessageLogger.MsgLevel.error); return(null); } StringReader sr = new StringReader(fileInfo.contents); string l; while ((l = sr.ReadLine()) != null) { int start = 0; while ((start = l.IndexOf('&', start)) != -1 && start < l.Length) { int end = l.IndexOf('&', start + 1); if (end == -1) { break; } fileInfo.symbols.Add(l.Substring(start, end - start + 1)); start = end + 1; } } return(fileInfo); }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { SerialPort port = null; try { port = new SerialPort(settings.readFrom, settings.baudRate, settings.parity, settings.dataBits, settings.stopBits); port.Open(); while (port.IsOpen) { BarcodeChar((char)port.ReadChar()); } } catch (Exception exp) { MessageLogger.Add(string.Format("Can't connect to barcode scanner {0}", settings), MessageLogger.MsgLevel.warning); MessageLogger.Add(string.Format("Error opening barcode port {0}", exp.ToString()), MessageLogger.MsgLevel.additional); } finally { if (port != null && port.IsOpen) { port.Close(); } } }
public static string Decrypt(string cipherText) { try { byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] cipherTextBytes = Convert.FromBase64String(cipherText); PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keySize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes); MemoryStream memoryStream = new MemoryStream(cipherTextBytes); CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); byte[] plainTextBytes = new byte[cipherTextBytes.Length]; int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); memoryStream.Close(); cryptoStream.Close(); string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); return(plainText); } catch (Exception e) { MessageLogger.Add("Error Decrypting password " + e.ToString(), MessageLogger.MsgLevel.critical); return(""); } }
static public bool SaveConfig(object settings) { // Just some boiler plate serialisation code... try { Type type = settings.GetType(); // Create an instance of the XmlSerializer class; specify the type of object to be deserialized. XmlSerializer serializer = new XmlSerializer(type); /* If the XML document has been altered with unknown nodes or attributes, handle them with the * UnknownNode and UnknownAttribute events.*/ serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode); serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute); // A FileStream is needed to read the XML document. System.IO.FileStream fs = new System.IO.FileStream(type.Name + ".xml", System.IO.FileMode.Create); /* Use the Deserialize method to restore the object's state withdata from the XML document. */ serializer.Serialize(fs, settings); } catch (Exception exp) { MessageLogger.Add(exp.ToString(), MessageLogger.MsgLevel.error); return(false); } return(true); }
public static string Encrypt(string plainText) { try { byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keySize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); byte[] cipherTextBytes = memoryStream.ToArray(); memoryStream.Close(); cryptoStream.Close(); string cipherText = Convert.ToBase64String(cipherTextBytes); return(cipherText); } catch (Exception e) { MessageLogger.Add("Error Encrypting password " + e.ToString(), MessageLogger.MsgLevel.critical); return(""); } }
public static bool OpenDBConnection() { if (myConn != null) { return(true); } if (myConn == null) { string cs = string.Format("server={0};userid={1};password={2};database=pck", settings.DBServer, settings.DBUser, CryptoSystem.Decrypt(settings.DBPassword)); try { myConn = new MySqlConnection(cs); myConn.Open(); //MySqlCommand cmd = myConn.CreateCommand(); //cmd.CommandText = "SET autocommit = 0"; //cmd.ExecuteNonQuery(); } catch (MySqlException ex) { MessageLogger.Add("Error opening DB connection " + ex.ToString(), MessageLogger.MsgLevel.error); return(false); } } return(true); }
public static bool Initialise() { settings = (DBSettings)XMLConfigHandler.ReadConfig(typeof(DBSettings)); if (settings == null) { MessageLogger.Add("Error ready DBSettings.XML", MessageLogger.MsgLevel.error); return(false); } return(true); }
public override void SetTare(decimal newTare) { try { decimal decigrams = decimal.Round(newTare * 100, 0); string cmd = String.Format("PT,{0}\r\n", decigrams); if (!mySerialPort.IsOpen) { mySerialPort.Open(); } mySerialPort.Write(cmd); } catch (Exception ex) { MessageLogger.Add("Error sending Tare to Serial Port " + ex.ToString(), MessageLogger.MsgLevel.error); } }
public override string PollScaleWeight() { if (!mySerialPort.IsOpen) { return("Serial port not open"); } try { mySerialPort.Write("RW,1\r\n"); return(mySerialPort.ReadLine()); } catch (Exception ex) { MessageLogger.Add("Error Reading Serial Port " + ex.ToString(), MessageLogger.MsgLevel.error); } return(null); }
public static List <LogRec> GetLogEntries(DateTime from, DateTime to) { var l = new List <LogRec>(); DBOperations.OpenDBConnection(); MySqlCommand cmd = DBOperations.myConn.CreateCommand(); cmd.CommandText = "SELECT * FROM pck.log WHERE created >= @from AND created <= @to ORDER BY id DESC"; MySqlDataReader rdr = null; try { cmd.Parameters.AddWithValue("@from", from); cmd.Parameters.AddWithValue("@to", to); rdr = cmd.ExecuteReader(); while (rdr.Read()) { var rec = new LogRec(); rec.created = rdr.GetDateTime(1); rec.msg = rdr.GetString(2); l.Add(rec); } rdr.Close(); } catch (MySqlException ex) { MessageLogger.Add("Error reading log messages from DB " + ex.ToString(), MessageLogger.MsgLevel.critical); return(null); } finally { if (rdr != null) { rdr.Close(); } } return(l); }
public bool Print(string fileName, Dictionary <string, string> fldValues) { FileInfo fileInfo; try { fileInfo = fileBuffer[fileName]; } catch (KeyNotFoundException) { fileInfo = BuildFileInfo(fileName); if (fileInfo == null) { return(false); } else { fileBuffer.Add(fileName, fileInfo); } } string s = string.Copy(fileInfo.contents); foreach (string sym in fileInfo.symbols) { try { var val = fldValues[sym].ToString(); s = s.Replace(sym, val); } catch (KeyNotFoundException) { MessageLogger.Add("Unknown symbol in label file: " + sym, MessageLogger.MsgLevel.warning); s = s.Replace(sym, "??"); } } return(RawPrinterHelper.SendStringToPrinter(settings.printerName, s)); }
public static bool Add(string msg) { DBOperations.OpenDBConnection(); DBOperations.BeginTransaction(); try { var cmd = DBOperations.myConn.CreateCommand(); cmd.CommandText = "INSERT INTO pck.log (msg) VALUES (@msg)"; cmd.Parameters.AddWithValue("@msg", msg); cmd.ExecuteNonQuery(); DBOperations.CommitTransaction(); } catch (MySqlException ex) { DBOperations.RollbackTransaction(); MessageLogger.Add("Error inserting record into PCK.LOG - see log for details", MessageLogger.MsgLevel.error); MessageLogger.Add(ex.ToString(), MessageLogger.MsgLevel.additional); return(false); } return(true); }
static public object ReadConfig(Type type) { // Just some boiler plate deserialisation code... var settings = Activator.CreateInstance(type); System.IO.FileStream fs = null; try { // Create an instance of the XmlSerializer class; specify the type of object to be deserialized. XmlSerializer serializer = new XmlSerializer(type); /* If the XML document has been altered with unknown nodes or attributes, handle them with the * UnknownNode and UnknownAttribute events.*/ serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode); serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute); // A FileStream is needed to read the XML document. fs = new System.IO.FileStream(type.Name + ".xml", System.IO.FileMode.Open); /* Use the Deserialize method to restore the object's state withdata from the XML document. */ settings = serializer.Deserialize(fs); } catch (Exception exp) { if (fs != null) { fs.Close(); } MessageLogger.Add(exp.ToString(), MessageLogger.MsgLevel.error); return(null); } fs.Close(); return(settings); }
static private void serializer_UnknownAttribute (object sender, System.Xml.Serialization.XmlAttributeEventArgs e) { System.Xml.XmlAttribute attr = e.Attr; MessageLogger.Add("Error in xml settings file - unknown attribute " + attr.Name + "='" + attr.Value + "'", MessageLogger.MsgLevel.error); }
static private void serializer_UnknownNode (object sender, System.Xml.Serialization.XmlNodeEventArgs e) { MessageLogger.Add(string.Format("Error in xml settings file - unknown Node: {0} \t {1}", e.Name, e.Text), MessageLogger.MsgLevel.error); }