diff --git a/Matomat/App.config b/Matomat/App.config index b6c6250..60aa43a 100644 --- a/Matomat/App.config +++ b/Matomat/App.config @@ -1,13 +1,14 @@  - - + + - + + \ No newline at end of file diff --git a/Matomat/Automat.cs b/Matomat/Automat.cs index 3923a5e..bfae845 100644 --- a/Matomat/Automat.cs +++ b/Matomat/Automat.cs @@ -2,6 +2,11 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Matomat.Input; +using Matomat.Output; +using Matomat.Data; +using Matomat.Database.Tables; +using Matomat.Helper; namespace Matomat { @@ -18,37 +23,31 @@ namespace Matomat } internal void doJob(InputData input) { - LCDDisplay lcd = Factory.getLCDDisplay(); - Input inp = Factory.getInput(); - if (input.type == InputData.types.Card) { - User user = new User(input.id); + RfidCode user = new RfidCode(input.id); if (!user.vaild()) return; this.showUserInfo(user); - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "² Bitte Produkt über den ²" + - "² Barcodeleser halten ²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²"); + Factory.getLCD().printb("Bitte Produkt über den\nBarcodeleser halten"); - InputData prodid = inp.getProdInput(20); + InputData prodid = Factory.getInput().getProdInput(20); if (prodid.type == InputData.types.None) return; - Prod prod = new Prod(prodid.id); + EAN13 prod = new EAN13(prodid.id); if(!prod.vaild()) return; this.showProdInfo(prod); - if (prod.GetProdType() == Prod.ProdType.product) + if (prod.GetProdType() == EAN13.ProdType.product) { this.sell(prod, user); } - if (prod.GetProdType() == Prod.ProdType.instruction) + if (prod.GetProdType() == EAN13.ProdType.instruction) { switch (prod.GetFunctName()) { @@ -56,15 +55,15 @@ namespace Matomat case "aufladen(10);": this.InsAufladen(10, user); break; case "aufladen(20);": this.InsAufladen(20, user); break; case "aufladen(5);": this.InsAufladen(5, user); break; - /*case "addUser();": this.InsAddUser(); break; - case "delUser();": this.InsDelUser(); break; + case "addUser();": this.InsAddUser(user); break; + /*case "delUser();": this.InsDelUser(); break; case "showStats();": this.InsShowStats(); break;*/ } } } else if (input.type == InputData.types.Prod) { - Prod prod = new Prod(input.id); + EAN13 prod = new EAN13(input.id); if (!prod.vaild()) return; @@ -78,103 +77,137 @@ namespace Matomat } } - private void InsAufladen(int betrag, User admin) + private void InsAddUser(RfidCode admin) { - LCDDisplay lcd = Factory.getLCDDisplay(); - Input inp = Factory.getInput(); if (!admin.IsAdmin()) { - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "² Du bist kein Admin, ²" + - "² du kannst nichts Aufladen! ²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", - LCDDisplay.Status.Error, 5); + Factory.getLCD().printb("Du bist kein Admin,\ndu kannst nichts Aufladen!", LCDDisplay.Status.Error, 5); System.Threading.Thread.Sleep(4500); return; } - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "² Bitte die Zielkarte auf den ²" + - "² RFID Leser auflegen ²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²"); - InputData target = inp.getCardInput(20); + + Factory.getLCD().printb("Bitte die neue Userkarte auf\nden RFID Leser auflegen."); + InputData target = Factory.getInput().getCardInput(20); if (target.type == InputData.types.None) return; - User user = new User(target.id); + RfidCode user = new RfidCode(target.id); + if (user.vaild(true)) + { + Factory.getLCD().printb("Dieser User ist schon\nRegistriert", LCDDisplay.Status.Warn, 5); + System.Threading.Thread.Sleep(4500); + return; + } + + Factory.getLCD().printb("Bitte den Usernamen [255] eingeben:"); + string username = Console.ReadLine(); + Factory.getLCD().printb("Username:\n" + username); + System.Threading.Thread.Sleep(4500); + + Factory.getLCD().printb("Bitte Kürzel [8] eingeben:"); + string kurzel = Console.ReadLine(); + Factory.getLCD().printb("Kürzel:\n" + kurzel); + System.Threading.Thread.Sleep(4500); + + Factory.getLCD().printb("Bitte E-Mail [255] eingeben:"); + string email = Console.ReadLine(); + Factory.getLCD().printb("E-Mail:\n" + email); + System.Threading.Thread.Sleep(4500); + + Factory.getLCD().printb("Bitte Admin [1|0] eingeben:"); + string isadmin = Console.ReadLine(); + Factory.getLCD().printb("Admin:\n" + isadmin); + System.Threading.Thread.Sleep(4500); + + string sql = "INSERT INTO `user` (`userid`,`username`,`admin`,`shortname`,`email`) VALUES (" + + "'" + Factory.getDBO().quote(target.id.ToString()) + "'," + + "'" + Factory.getDBO().quote(username) + "'," + + "'" + Factory.getDBO().quote(isadmin == "1"?"1":"0") + "'," + + "'" + Factory.getDBO().quote(kurzel) + "'," + + "'" + Factory.getDBO().quote(email) + "')"; + Factory.getDBO().query(sql); + + Factory.getLCD().printb("Neuen User eingefügt:\n" + kurzel); + System.Threading.Thread.Sleep(4500); + } + + private void InsAufladen(int betrag, RfidCode admin) + { + if (!admin.IsAdmin()) + { + Factory.getLCD().printb("Du bist kein Admin,\ndu kannst nichts Aufladen!", LCDDisplay.Status.Error, 5); + System.Threading.Thread.Sleep(4500); + return; + } + + Factory.getLCD().printb("Bitte die Zielkarte auf den\nRFID Leser auflegen"); + InputData target = Factory.getInput().getCardInput(20); + if (target.type == InputData.types.None) + return; + + RfidCode user = new RfidCode(target.id); if (!user.vaild()) return; - - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "² Betrag von User " + user.GetUserName().PadRight(8, ' ') + " ²" + - "² um "+betrag.ToString().PadLeft(2, ' ')+" € verringert ²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²"); + + Factory.getLCD().printb("Betrag von User " + user.GetUserName() + "\num " + betrag + " € verringert"); user.SetUserKonto(user.GetUserKonto()-betrag); System.Threading.Thread.Sleep(2000); } - private void sell(Prod prod, User user) + private void sell(EAN13 prod, RfidCode user) { - LCDDisplay lcd = Factory.getLCDDisplay(); if (user.GetUserKonto() > 19) { - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "² Du hast zu viel Geld auf dem Konto ²" + - "² BITTE bezahlen! | Grenze 20€ ²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", - LCDDisplay.Status.Error, 5); + Factory.getLCD().printb("Du hast zu viel Geld auf dem Konto\nBITTE bezahlen! | Grenze 20€", LCDDisplay.Status.Error, 5); + return; } + Factory.getDBO().query("INSERT INTO `history` (`prod`,`user`,`time`) VALUES (" + + "'" + Factory.getDBO().quote(prod.GetDbId().ToString()) + "'," + + "'" + Factory.getDBO().quote(user.GetDbId().ToString()) + "'," + + "NOW())"); + Factory.getDBO().query("UPDATE `user` SET `credits` = `credits`+1 WHERE `user`.`id` = '" + + Factory.getDBO().quote(user.GetDbId().ToString()) + "'"); + Factory.getLCD().printb("Guten Durst mit der\n" + prod.GetProdName() + "!", 4); + System.Threading.Thread.Sleep(3500); } - private void showProdInfo(Prod prod) + private void showProdInfo(EAN13 prod) { - LCDDisplay lcd = Factory.getLCDDisplay(); - if (prod.GetProdType() == Prod.ProdType.product) + if (prod.GetProdType() == EAN13.ProdType.product) { long id = prod.GetProdId(); double price = prod.GetProdPrice(); string name = prod.GetProdName(); - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "² Produkt: " + name.PadRight(27, ' ') + " ²" + - "² Preis: " + price.ToString(2).PadLeft(6, ' ') + "€ EAN13: " + id.ToString().PadRight(14, ' ') + " ²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", 2); + Factory.getLCD().printb("Produkt: " + name + "\nPreis: " + price + "€ EAN13: " + id, 2); } - if (prod.GetProdType() == Prod.ProdType.instruction) + if (prod.GetProdType() == EAN13.ProdType.instruction) { string name = prod.GetFunctName(); - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "² Instruktion: " + name.PadRight(23, ' ') + " ²" + - "² AdminCard + Instruction + TargetCard ²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", 2); + Factory.getLCD().printb("Instruktion: " + name + "\nAdminCard + Instruction + TargetCard", 2); } } - private void showUserInfo(User user) + private void showUserInfo(RfidCode user) { - LCDDisplay lcd = Factory.getLCDDisplay(); - string name = user.GetUserName(); int konto = user.GetUserKonto(); long id = user.GetUserId(); long all = user.GetUserAll(); - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "² User: " + name.PadRight(8, ' ') + " Betrag: " + (konto.ToString() + " €").PadRight(7, ' ') + " ²" + - "² UserID: " + id.ToString().PadRight(12, ' ') + " Gesamt: " + (all.ToString() + " €").PadRight(7, ' ') + " ²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", 5); + Factory.getLCD().printb("User: " + name + " Betrag: " + konto + " €\nUserID: " + id + " Gesamt: " + all + " €", 5); } internal void GetInitStatus() { - LCDDisplay lcd = Factory.getLCDDisplay(); List l = new Tables().getBestlist(); string[] p = new string[4]; for (int i = 0; i < 4; i++) { - p[i] = (l.Count >= i + 1) ? l.ElementAt(i).name.PadRight(6, ' ').Substring(0, 6) + " " + (l.ElementAt(i).num % 1000).ToString().PadLeft(3, '0') : " "; + p[i] = (l.Count >= i + 1) ? l.ElementAt(i).name.PadRight(8, ' ').Substring(0, 8) + " " + (l.ElementAt(i).num % 1000).ToString().PadLeft(3, '0') : " "; } - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + p[0] + - "² MATOMAT Wilkommen! ²" + p[1] + - "² Frohes genießen der Mate ²" + p[2] + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + p[3]); + Factory.getLCD().print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + p[0] + + "² MATOMAT Wilkommen! ²" + p[1] + + "² Frohes genießen der Mate ²" + p[2] + + "²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + p[3]); } } } diff --git a/Matomat/Prod.cs b/Matomat/Data/EAN13.cs similarity index 74% rename from Matomat/Prod.cs rename to Matomat/Data/EAN13.cs index 27f20fd..14fa7f5 100644 --- a/Matomat/Prod.cs +++ b/Matomat/Data/EAN13.cs @@ -2,10 +2,13 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Matomat.Output; +using Matomat.Database.Tables; +using Matomat.Helper; -namespace Matomat +namespace Matomat.Data { - class Prod + class EAN13 { private long prodId; private bool found; @@ -13,13 +16,14 @@ namespace Matomat private int price; private string name; private string function; + private int id; public enum ProdType { product, instruction } - public Prod(long prodId) + public EAN13(long prodId) { this.prodId = prodId; this.load(); @@ -46,18 +50,19 @@ namespace Matomat this.name = t.name; } this.price = t.cost; - + this.id = t.id; } internal bool vaild() { - LCDDisplay lcd = Factory.getLCDDisplay(); + LCDDisplay lcd = Factory.getLCD(); if (!found) { - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²"+ + lcd.printb("Produkt wurde nich gefunden\nEAN13: " + this.GetProdId().ToString() + " unbekannt", LCDDisplay.Status.Error, 5); + /*lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²"+ "² Produkt wurde nich gefunden ²"+ "² EAN13: "+(this.GetProdId().ToString()+" unbekannt").PadRight(29,' ')+" ²"+ - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", LCDDisplay.Status.Error, 5); + "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", LCDDisplay.Status.Error, 5);*/ System.Threading.Thread.Sleep(4500); return false; } @@ -88,5 +93,10 @@ namespace Matomat { return this.function; } + + public int GetDbId() + { + return this.id; + } } } diff --git a/Matomat/User.cs b/Matomat/Data/RfidCode.cs similarity index 68% rename from Matomat/User.cs rename to Matomat/Data/RfidCode.cs index b5ff018..3d3b8ac 100644 --- a/Matomat/User.cs +++ b/Matomat/Data/RfidCode.cs @@ -2,10 +2,14 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Matomat.Output; +using Matomat.Database; +using Matomat.Database.Tables; +using Matomat.Helper; -namespace Matomat +namespace Matomat.Data { - class User + class RfidCode { private long userId; private bool found; @@ -16,7 +20,7 @@ namespace Matomat private bool admin; private int id; - public User(long userId) + public RfidCode(long userId) { this.userId = userId; load(); @@ -39,15 +43,24 @@ namespace Matomat this.id = u.id; } - internal bool vaild() + public bool vaild(bool silent) { - LCDDisplay lcd = Factory.getLCDDisplay(); + if (silent) + { + return found; + } + else + { + return vaild(); + } + } + + public bool vaild() + { + LCDDisplay lcd = Factory.getLCD(); if (!found) { - lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "² User wurde nicht gefunden ²" + - "² UserID: " + (this.GetUserId().ToString() + " unbekannt").PadRight(28, ' ') + " ²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", LCDDisplay.Status.Error, 5); + lcd.printb("User wurde nicht gefunden\nUserID:" + this.GetUserId().ToString() + " unbekannt", LCDDisplay.Status.Error, 5); System.Threading.Thread.Sleep(4500); return false; } @@ -70,6 +83,11 @@ namespace Matomat return this.userId; } + public int GetDbId() + { + return this.id; + } + internal long GetUserAll() { return this.all; @@ -82,7 +100,7 @@ namespace Matomat internal void SetUserKonto(int p) { - Database db = Factory.getDBO(); + TDatabase db = Factory.getDBO(); this.credits = p; db.query("UPDATE `user` SET `credits` = '" + db.quote(p.ToString())+"' WHERE "+ "`id` = '"+db.quote(this.id.ToString())+"'"); diff --git a/Matomat/Database.cs b/Matomat/Database.cs deleted file mode 100644 index 009176b..0000000 --- a/Matomat/Database.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Matomat -{ - abstract class Database - { - private class instance - { - public string signature; - public Database iDatabase; - - public instance(Database db, string p) - { - this.iDatabase = db; - this.signature = p; - } - } - private static List instances; - internal static Database getInstance(string server, string dbs, string user, string pw, int port, string driver) - { - if (instances == null) - instances = new List(); - foreach (instance i in instances) - { - if (i.signature == server + dbs + user + pw + port + driver) - return i.iDatabase; - } - string object_driver = "Matomat.DBDriver" + char.ToUpper(driver[0]) + driver.Substring(1).ToLower(); - Type t = Type.GetType(object_driver, true); - Database db = (Database)t.GetConstructor(Type.EmptyTypes).Invoke(new Object[0]); - db.connect(server, dbs, user, pw, port, driver); - if (db.getError() != null) - { - throw new Exception("db Fehler:" + db.getError()); - } - instance instance = new instance(db, server + dbs + user + pw + port + driver); - instances.Add(instance); - return instance.iDatabase; - } - public abstract bool connect(string server, string dbs, string user, string pw, int port, string driver); - public abstract void query(string sql); - public abstract object[] getResult(); - public abstract List getResultList(); - public abstract Exception getError(); - public abstract long getID(); - public abstract string quote(string str); - public abstract void RequestStop(); - } -} diff --git a/Matomat/DBDriverMysqli.cs b/Matomat/Database/DBDriverMysqli.cs similarity index 92% rename from Matomat/DBDriverMysqli.cs rename to Matomat/Database/DBDriverMysqli.cs index ce35dde..f653893 100644 --- a/Matomat/DBDriverMysqli.cs +++ b/Matomat/Database/DBDriverMysqli.cs @@ -5,10 +5,11 @@ using System.Text; using MySql.Data; using MySql.Data.MySqlClient; using System.Threading; +using Matomat.Helper; -namespace Matomat +namespace Matomat.Database { - class DBDriverMysqli : Database + class DBDriverMysqli : TDatabase { MySqlConnection conn; private Exception err; @@ -73,6 +74,10 @@ namespace Matomat this.set(sql); else this.err = new Exception("Keine unterstütze MySQL Abfrage '" + sql + "'..."); + if (Factory.getConfig().debug) + { + Console.WriteLine("SQL: " + sql + ((err != null) ? " Err" + err.Message : "")); + } } private void set(string sql) diff --git a/Matomat/Database/TDatabase.cs b/Matomat/Database/TDatabase.cs new file mode 100644 index 0000000..9b58457 --- /dev/null +++ b/Matomat/Database/TDatabase.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Matomat.Database +{ + abstract class TDatabase + { + private static Dictionary instances = new Dictionary(); + internal static TDatabase getInstance(string server, string dbs, string user, string pw, int port, string driver) + { + string signature = server + dbs + user + pw + port + driver; + + if (instances.Keys.Contains(signature)) + { + return instances[signature]; + } + + string object_driver = "Matomat.Database.DBDriver" + char.ToUpper(driver[0]) + driver.Substring(1).ToLower(); + Type t = null; + try + { + t = Type.GetType(object_driver, true); + } + catch (TypeLoadException) + { + throw new Exception("DB Treiber nicht gefunden: " + object_driver); + } + TDatabase db = (TDatabase)t.GetConstructor(Type.EmptyTypes).Invoke(new Object[0]); + + db.connect(server, dbs, user, pw, port, driver); + + if (db.getError() != null) + { + throw new Exception("db Fehler:" + db.getError()); + } + + instances.Add(signature,db); + return db; + } + public abstract bool connect(string server, string dbs, string user, string pw, int port, string driver); + public abstract void query(string sql); + public abstract object[] getResult(); + public abstract List getResultList(); + public abstract Exception getError(); + public abstract long getID(); + public abstract string quote(string str); + public abstract void RequestStop(); + } +} diff --git a/Matomat/Tables/Tables.Best.cs b/Matomat/Database/Tables/Tables.Best.cs similarity index 91% rename from Matomat/Tables/Tables.Best.cs rename to Matomat/Database/Tables/Tables.Best.cs index 67a99da..d3fd42b 100644 --- a/Matomat/Tables/Tables.Best.cs +++ b/Matomat/Database/Tables/Tables.Best.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Data; -namespace Matomat +namespace Matomat.Database.Tables { class TBest : Tables { diff --git a/Matomat/Tables/Tables.Product.cs b/Matomat/Database/Tables/Tables.Product.cs similarity index 93% rename from Matomat/Tables/Tables.Product.cs rename to Matomat/Database/Tables/Tables.Product.cs index 608dcf9..07bd544 100644 --- a/Matomat/Tables/Tables.Product.cs +++ b/Matomat/Database/Tables/Tables.Product.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -namespace Matomat +namespace Matomat.Database.Tables { class TProduct : Tables { diff --git a/Matomat/Tables/Tables.User.cs b/Matomat/Database/Tables/Tables.User.cs similarity index 94% rename from Matomat/Tables/Tables.User.cs rename to Matomat/Database/Tables/Tables.User.cs index 8042c73..d5712e4 100644 --- a/Matomat/Tables/Tables.User.cs +++ b/Matomat/Database/Tables/Tables.User.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Data; -namespace Matomat +namespace Matomat.Database.Tables { class TUser : Tables { diff --git a/Matomat/Tables/Tables.cs b/Matomat/Database/Tables/Tables.cs similarity index 77% rename from Matomat/Tables/Tables.cs rename to Matomat/Database/Tables/Tables.cs index 0d5e3e9..fbc6a4f 100644 --- a/Matomat/Tables/Tables.cs +++ b/Matomat/Database/Tables/Tables.cs @@ -2,8 +2,10 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Matomat.Database; +using Matomat.Helper; -namespace Matomat +namespace Matomat.Database.Tables { class Tables { @@ -13,7 +15,7 @@ namespace Matomat } public TUser getUserTable(long userid) { - Database db = Factory.getDBO(); + TDatabase db = Factory.getDBO(); db.query("SELECT `id`, `userid`, `username`, `credits`, `admin`, `shortname`, `email` FROM `user` WHERE `userid` =" + db.quote(userid.ToString())); object[] row = db.getResult(); if (row == null) @@ -28,7 +30,7 @@ namespace Matomat } public TProduct getProdTable(long barcode) { - Database db = Factory.getDBO(); + TDatabase db = Factory.getDBO(); db.query("SELECT * FROM `product` WHERE `barcode` = " + db.quote(barcode.ToString())); object[] row = db.getResult(); if (row == null) @@ -40,7 +42,7 @@ namespace Matomat internal List getBestlist() { - Database db = Factory.getDBO(); + TDatabase db = Factory.getDBO(); db.query("SELECT COUNT(*) AS `num`,`u`.`shortname` AS `name` "+ "FROM `history` AS `h` "+ "LEFT JOIN `product` AS `p` ON `p`.`id` = `h`.`prod` "+ @@ -61,17 +63,23 @@ namespace Matomat internal TBest getUserMax(int p) { - Database db = Factory.getDBO(); - db.query("SELECT SUM(`p`.`cost`) AS `num`,`u`.`shortname` AS `name` "+ + TDatabase db = Factory.getDBO(); + string sql = "SELECT SUM(`p`.`cost`) AS `num`,`u`.`shortname` AS `name` "+ "FROM `history` AS `h` "+ "LEFT JOIN `product` AS `p` ON `p`.`id` = `h`.`prod` "+ - "LEFT JOIN `user` AS `u` ON `u`.`id` = `h`.`user` "+ - "WHERE `p`.`iscommand` = 0 AND `h`.`user` = " + db.quote(p.ToString())); + "RIGHT JOIN `user` AS `u` ON `u`.`id` = `h`.`user` "+ + "WHERE `p`.`iscommand` = 0 AND `h`.`user` = " + db.quote(p.ToString()); + db.query(sql); + object[] row = db.getResult(); if (row == null) { return null; } + if (row[0] == System.DBNull.Value) + { + return new TBest(Decimal.ToInt64(0), (string)row[1]); + } return new TBest(Decimal.ToInt64((decimal)row[0]), (string)row[1]); } } diff --git a/Matomat/Factory.cs b/Matomat/Helper/Factory.cs similarity index 54% rename from Matomat/Factory.cs rename to Matomat/Helper/Factory.cs index 61440ac..b259633 100644 --- a/Matomat/Factory.cs +++ b/Matomat/Helper/Factory.cs @@ -2,72 +2,26 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Matomat.Output; +using Matomat.Input; +using Matomat.Database; -namespace Matomat +namespace Matomat.Helper { class Factory { private static LCDDisplay lcd_i = null; - private static Input inp_i = null; + private static TInput inp_i = null; private static Config con_i = null; - private static Database dbo_i; - internal static LCDDisplay getLCDDisplay() + private static TDatabase dbo_i; + + public static LCDDisplay getLCD() { if (lcd_i == null) lcd_i = Factory._createLCDDisplay(); return lcd_i; } - - private static LCDDisplay _createLCDDisplay() - { - return LCDDisplay.getInstance(Factory.getConfig().com_display); - } - - internal static Input getInput() - { - if (inp_i == null) - inp_i = Factory._createInput(); - return inp_i; - } - - private static Input _createInput() - { - return Input.getInstance(); - } - internal static Config getConfig() - { - if (con_i == null) - con_i = Factory._createConfig("App.config"); - return con_i; - } - - private static Config _createConfig(string file) - { - return Config.getInstance(file); - } - internal static Database getDBO() - { - if (dbo_i == null) - dbo_i = Factory._createDBO(); - return dbo_i; - } - - private static Database _createDBO() - { - Config c = Factory.getConfig(); - return Database.getInstance(c.mysql_server, c.mysql_db, c.mysql_user, c.mysql_pw, c.mysql_port, c.mysql_driver); - } - - internal static void rmDBO() - { - if (dbo_i != null) - { - dbo_i.RequestStop(); - dbo_i = null; - } - } - - internal static void rmLCDDisplay() + public static void rmLCD() { if (lcd_i != null) { @@ -75,5 +29,67 @@ namespace Matomat lcd_i = null; } } + private static LCDDisplay _createLCDDisplay() + { + return LCDDisplay.getInstance(Factory.getConfig().com_display); + } + + public static TInput getInput() + { + if (inp_i == null) + inp_i = Factory._createInput(); + return inp_i; + } + private static TInput _createInput() + { + return TInput.getInstance(Factory.getConfig().com_rfid); + } + public static void rmInput() + { + if (inp_i != null) + { + inp_i.RequestStop(); + inp_i = null; + } + } + + public static Config getConfig() + { + if (con_i == null) + con_i = Factory._createConfig("App.config"); + return con_i; + } + private static Config _createConfig(string file) + { + return Config.getInstance(file); + } + public static void rmConfig() + { + if (con_i != null) + { + con_i.RequestStop(); + con_i = null; + } + } + + public static TDatabase getDBO() + { + if (dbo_i == null) + dbo_i = Factory._createDBO(); + return dbo_i; + } + private static TDatabase _createDBO() + { + Config c = Factory.getConfig(); + return TDatabase.getInstance(c.mysql_server, c.mysql_db, c.mysql_user, c.mysql_pw, c.mysql_port, c.mysql_driver); + } + public static void rmDBO() + { + if (dbo_i != null) + { + dbo_i.RequestStop(); + dbo_i = null; + } + } } } diff --git a/Matomat/Helper.cs b/Matomat/Helper/Types.cs similarity index 92% rename from Matomat/Helper.cs rename to Matomat/Helper/Types.cs index 1e22f4d..1beddc0 100644 --- a/Matomat/Helper.cs +++ b/Matomat/Helper/Types.cs @@ -7,12 +7,8 @@ using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; -namespace Matomat +namespace Matomat.Helper { - class Helper - { - - } public static class String { public static byte[] ToCharArray(this string str, string search, byte[] replace) diff --git a/Matomat/BarcodeReader.cs b/Matomat/Input/BarcodeReader.cs similarity index 94% rename from Matomat/BarcodeReader.cs rename to Matomat/Input/BarcodeReader.cs index e111a38..d629151 100644 --- a/Matomat/BarcodeReader.cs +++ b/Matomat/Input/BarcodeReader.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.IO; -namespace Matomat +namespace Matomat.Input { class BarcodeReader { diff --git a/Matomat/Config.cs b/Matomat/Input/Config.cs similarity index 52% rename from Matomat/Config.cs rename to Matomat/Input/Config.cs index 65c35fc..7f26cc5 100644 --- a/Matomat/Config.cs +++ b/Matomat/Input/Config.cs @@ -5,26 +5,14 @@ using System.Text; using System.IO; using System.Configuration; -namespace Matomat +namespace Matomat.Input { class Config { - private class instance - { - public Config config; - public string signature; - - public instance(Config config, string signature) - { - this.config = config; - this.signature = signature; - } - } - static List instances; + private static Dictionary instances = new Dictionary(); private AppSettingsReader config; - - public Config() + private Config() { this.config = new AppSettingsReader(); loadFile(); @@ -41,37 +29,34 @@ namespace Matomat this.mysql_port = (int)config.GetValue("mysql_port", typeof(int)); this.mysql_pw = (string)config.GetValue("mysql_pw", typeof(string)); this.mysql_driver = (string)config.GetValue("mysql_driver", typeof(string)); + this.debug = (bool)config.GetValue("debug", typeof(bool)); } catch (Exception) { } } internal static Config getInstance(string file) { - if (instances == null) - instances = new List(); - foreach (instance i in instances) + if (instances.Keys.Contains(file)) { - if (i.signature == file) - return i.config; + return instances[file]; } - instance instance = new instance(new Config(), file); - instances.Add(instance); - return instance.config; + instances.Add(file,new Config()); + return instances[file]; } - public string com_display { get; set; } - public string com_rfid { get; set; } + public string com_display { get; private set; } + public string com_rfid { get; private set; } + public string mysql_server { get; private set; } + public string mysql_user { get; private set; } + public string mysql_db { get; private set; } + public int mysql_port { get; private set; } + public string mysql_pw { get; private set; } + public string mysql_driver { get; private set; } + public bool debug { get; private set; } - public string mysql_server { get; set; } - - public string mysql_user { get; set; } - - public string mysql_db { get; set; } - - public int mysql_port { get; set; } - - public string mysql_pw { get; set; } - - public string mysql_driver { get; set; } + public void RequestStop() + { + + } } } diff --git a/Matomat/RFIDReader.cs b/Matomat/Input/RFIDReader.cs similarity index 79% rename from Matomat/RFIDReader.cs rename to Matomat/Input/RFIDReader.cs index 346a8df..0fd9cd0 100644 --- a/Matomat/RFIDReader.cs +++ b/Matomat/Input/RFIDReader.cs @@ -4,14 +4,14 @@ using System.Linq; using System.Text; using RfidClass; -namespace Matomat +namespace Matomat.Input { class RFIDReader { private RFIDReaderClass r; - public RFIDReader() + public RFIDReader(string comport) { - r = new RFIDReaderClass(Factory.getConfig().com_rfid); + r = new RFIDReaderClass(comport); } internal long getCardID() { diff --git a/Matomat/Input.cs b/Matomat/Input/TInput.cs similarity index 72% rename from Matomat/Input.cs rename to Matomat/Input/TInput.cs index a4b061e..138cfae 100644 --- a/Matomat/Input.cs +++ b/Matomat/Input/TInput.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading; -namespace Matomat +namespace Matomat.Input { class InputData { @@ -22,33 +22,22 @@ namespace Matomat public types type { get; set; } public long id { get; set; } } - class Input + class TInput { - - - private class instance - { - public Input input; - public double signature; - - public instance(Input input, double signature) - { - this.input = input; - this.signature = signature; - } - } - static List instances; + private static Dictionary instances = new Dictionary(); private InputData data; private RFIDReader rfid; private BarcodeReader code; - public Input() + private bool cardrunning = true; + private bool coderunning = true; + private TInput(string comport) { - rfid = new RFIDReader(); + rfid = new RFIDReader(comport); code = new BarcodeReader(); } private void doWorkCard() { - while (true) + while (this.cardrunning) { Thread.Sleep(1); long id = rfid.getCardID(); @@ -62,7 +51,7 @@ namespace Matomat } private void doWorkCode() { - while (true) + while (this.coderunning) { Thread.Sleep(1); long id = code.getCodeID(); @@ -73,11 +62,13 @@ namespace Matomat } } } - internal InputData getAnyInput(int timeout) + public InputData getAnyInput(int timeout) { if (timeout == 0) timeout = Int32.MaxValue; data = new InputData(); + this.cardrunning = true; + this.coderunning = true; Thread wtCard = new Thread(this.doWorkCard); Thread wtCode = new Thread(this.doWorkCode); wtCard.Start(); @@ -96,11 +87,12 @@ namespace Matomat return data; } - internal InputData getProdInput(int timeout) + public InputData getProdInput(int timeout) { if (timeout == 0) timeout = Int32.MaxValue; data = new InputData(); + this.coderunning = true; Thread wtCode = new Thread(this.doWorkCode); wtCode.Start(); while (!wtCode.IsAlive) ; @@ -114,26 +106,22 @@ namespace Matomat return data; } - internal static Input getInstance() + public static TInput getInstance(string comport) { - if (instances == null) - instances = new List(); - double signature = 1.0; - foreach (instance i in instances) + if (instances.Keys.Contains(comport)) { - if (i.signature == signature) - return i.input; + return instances[comport]; } - instance instance = new instance(new Input(), signature); - instances.Add(instance); - return instance.input; + instances.Add(comport,new TInput(comport)); + return instances[comport]; } - internal InputData getCardInput(int timeout) + public InputData getCardInput(int timeout) { if (timeout == 0) timeout = Int32.MaxValue; data = new InputData(); + this.cardrunning = true; Thread wtCard = new Thread(this.doWorkCard); wtCard.Start(); while (!wtCard.IsAlive) ; @@ -146,5 +134,11 @@ namespace Matomat rfid.Abort(); return data; } + + public void RequestStop() + { + this.cardrunning = false; + this.coderunning = false; + } } } diff --git a/Matomat/Matomat.csproj b/Matomat/Matomat.csproj index 5080461..2f272be 100644 --- a/Matomat/Matomat.csproj +++ b/Matomat/Matomat.csproj @@ -34,8 +34,14 @@ 4 - - + + False + dll\mysql.data.dll + + + False + dll\mysql.data.entity.dll + False .\RfidClass.dll @@ -50,22 +56,22 @@ - - - - - - - - - + + + + + + + + + - - - - - + + + + + @@ -82,7 +88,7 @@ - + diff --git a/Matomat/LCDDisplay.cs b/Matomat/Output/LCDDisplay.cs similarity index 57% rename from Matomat/LCDDisplay.cs rename to Matomat/Output/LCDDisplay.cs index 0aa4825..b1637bf 100644 --- a/Matomat/LCDDisplay.cs +++ b/Matomat/Output/LCDDisplay.cs @@ -4,23 +4,13 @@ using System.Linq; using System.Text; using System.Threading; using System.IO.Ports; +using Matomat.Helper; -namespace Matomat +namespace Matomat.Output { class LCDDisplay { - private class instance - { - public LCDDisplay lCDDisplay; - public string signature; - - public instance(LCDDisplay lCDDisplay, string signature) - { - this.lCDDisplay = lCDDisplay; - this.signature = signature; - } - } private class Message { public string text; @@ -45,14 +35,14 @@ namespace Matomat this.duration = duration; } } - static List instances; + private static Dictionary instances = new Dictionary(); private Queue messages; private Queue beeb; private volatile bool _shouldStop; private SerialPort serialPort; private bool disable = false; - public LCDDisplay(string port) + private LCDDisplay(string port) { this.serialPort = new SerialPort(port, 19200, Parity.None, 8, StopBits.One); try @@ -120,7 +110,6 @@ namespace Matomat this.beep(37, 100); this.beep(750, 500); } - Console.WriteLine(text); this.displayClear(); byte[] btext = text.ToCharArray("ÄÖÜäöüß²€", new byte[] { 0xE1, 0xEF, 0xF5, 0xE1, 0xEF, 0xF5, 0xE2, 0xFF, 0x80}); byte[] upper; @@ -139,6 +128,10 @@ namespace Matomat this.displayCode(upper); this.displayEscCode("GTO2@"); this.displayCode(lower); + + text = text.PadRight(160, ' '); + text = text.Substring(0, 40) + "\n" + text.Substring(40, 40) + "\n" + text.Substring(80, 40) + "\n" + text.Substring(120, 40); + Console.WriteLine(text); } private void displayClear() @@ -182,44 +175,105 @@ namespace Matomat Warn, Error } - internal void print(string text) + #region printb Display method for two lines (Border) + /// + /// Displays a two line string on the display + /// + /// Text to display. Break with \n, max length 38 chars each line + public void printb(string text) + { + this.printb(text, Status.OK, 0); + } + /// + /// Displays a two line string on the display with the minimum duration of the time + /// + /// Text to display. Break with \n, max length 38 chars each line + /// Minimum duration of displaying the text + public void printb(string text, int time) + { + this.printb(text, Status.OK, time); + } + /// + /// Displays a two line string on the display with beebcodes when Status is not OK + /// + /// Text to display. Break with \n, max length 38 chars each line + /// Status code for that Item + public void printb(string text, Status status) + { + this.printb(text, status, 0); + } + /// + /// Displays a two line string on the display with the minimum duration of the time and beebcodes when Status is not OK + /// + /// Text to display. Break with \n, max length 38 chars each line + /// Status code for that Item + /// Minimum duration of displaying the text + public void printb(string text, Status status, int time) + { + string[] lines = text.Split('\n'); + string line1 = ""; + string line2 = ""; + if (lines.Length == 1) + { + line1 = lines[0]; + } + else + { + line1 = lines[0]; + line2 = lines[1]; + } + if(line1.Length > 38) + line1 = line1.Substring(0, 38); + double ll1 = ((38D - line1.Length) / 2); + + line1 = line1.PadLeft((int)Math.Floor(ll1) + line1.Length, ' '); + line1 = line1.PadRight((int)Math.Ceiling(ll1) + line1.Length, ' '); + + if (line2.Length > 38) + line2 = line2.Substring(0, 38); + double ll2 = ((38D - line2.Length) / 2); + + line2 = line2.PadLeft((int)Math.Floor(ll2) + line2.Length, ' '); + line2 = line2.PadRight((int)Math.Ceiling(ll2) + line2.Length, ' '); + + this.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + + "²" + line1 + "²" + + "²" + line2 + "²" + + "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", status, time); + } + #endregion + #region print Display method for the whole display + public void print(string text) { this.anzeige(text, Status.OK, 0); } - + public void print(string text, int time) + { + this.anzeige(text, Status.OK, time); + } + public void print(string text, Status status) + { + this.anzeige(text, status, 0); + } + public void print(string text, Status status, int time) + { + this.anzeige(text, status, time); + } + #endregion private void anzeige(string text, Status status, int time) { Message m = new Message(text, status, time); messages.Enqueue(m); } - internal void print(string text, int time) + public static LCDDisplay getInstance(string port) { - this.anzeige(text, Status.OK, time); - } - - internal void print(string text, Status status) - { - this.anzeige(text, status, 0); - } - - internal void print(string text, Status status, int time) - { - this.anzeige(text, status, time); - } - - internal static LCDDisplay getInstance(string port) - { - if (instances == null) - instances = new List(); - foreach (instance i in instances) + if (instances.Keys.Contains(port)) { - if (i.signature == port) - return i.lCDDisplay; + return instances[port]; } - instance instance = new instance(new LCDDisplay(port), port); - instances.Add(instance); - return instance.lCDDisplay; + instances.Add(port,new LCDDisplay(port)); + return instances[port]; } public void RequestStop() { diff --git a/Matomat/Program.cs b/Matomat/Program.cs index 3dc0508..5214d86 100644 --- a/Matomat/Program.cs +++ b/Matomat/Program.cs @@ -8,7 +8,7 @@ namespace Matomat { class Program { - static void Main(string[] args) + public static void Main(string[] args) { Worker workerObject = new Worker(); Thread workerThread = new Thread(workerObject.DoWork); diff --git a/Matomat/Worker.cs b/Matomat/Worker.cs index 2411f2b..02ae142 100644 --- a/Matomat/Worker.cs +++ b/Matomat/Worker.cs @@ -2,24 +2,23 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Matomat.Input; +using Matomat.Output; +using Matomat.Helper; namespace Matomat { class Worker { private volatile bool _shouldStop; - private RFIDReader rfid; - private BarcodeReader barcode; private Automat automat; private LCDDisplay lcd; - private Input inp; + private TInput inp; public Worker() { - rfid = new RFIDReader(); - barcode = new BarcodeReader(); automat = new Automat(); automat.stopThread += new Automat.stopEvent(RequestStop); - lcd = Factory.getLCDDisplay(); + lcd = Factory.getLCD(); inp = Factory.getInput(); _shouldStop = false; } @@ -31,12 +30,11 @@ namespace Matomat InputData input = inp.getAnyInput(0); automat.doJob(input); } - this.lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "² Der Matomat wurde beendet. ²" + - "² Einen schönen Tag! ²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²"); - Factory.rmLCDDisplay(); + Factory.getLCD().printb("Der Matomat wurde beendet.\nEinen schönen Tag!"); + Factory.rmLCD(); + Factory.rmInput(); Factory.rmDBO(); + Factory.rmConfig(); } private void RequestStop(bool stop) {