diff --git a/Matomat/Automat.cs b/Matomat/Automat.cs index 989f499..6010d48 100644 --- a/Matomat/Automat.cs +++ b/Matomat/Automat.cs @@ -4,9 +4,9 @@ using System.Linq; using System.Text; using Matomat.Input; using Matomat.Output; -using Matomat.Data; using Matomat.Database.Tables; using Matomat.Helper; +using Matomat.Model; namespace Matomat { @@ -25,31 +25,47 @@ namespace Matomat { if (input.type == InputData.types.Card) { - RfidCode user = new RfidCode(input.id); - if (!user.vaild()) + User user; + try + { + user = new User(input.id); + } + catch (ArgumentException e) + { + Factory.getLCD().print(e.ToString(), 5, LCDDisplay.Status.OK); + System.Threading.Thread.Sleep(4500); return; + } + + Instruction.showUserInfo(user); - this.showUserInfo(user); - - Factory.getLCD().printb("Bitte Produkt über den\nBarcodeleser halten"); + Factory.getLCD().print("Bitte Produkt über den\nBarcodeleser halten"); InputData prodid = Factory.getInput().getProdInput(20); if (prodid.type == InputData.types.None) return; - EAN13 prod = new EAN13(prodid.id); - if(!prod.vaild()) + Product prod; + try + { + prod = new Product(prodid.id); + } + catch (ArgumentException e) + { + Factory.getLCD().print(e.ToString(), 5, LCDDisplay.Status.OK); + System.Threading.Thread.Sleep(4500); return; + } - this.showProdInfo(prod); + Instruction.ShowProdInfo(prod); - if (prod.GetProdType() == EAN13.ProdType.product) + if (prod.GetProdType() == Product.ProdType.Product) { Instruction.sell(prod, user); } - if (prod.GetProdType() == EAN13.ProdType.instruction) + if (prod.GetProdType() == Product.ProdType.Instruction) { - switch (prod.GetFunctName()) + switch (prod.GetName()) { case "exit();": this.stopThread(true); break; case "aufladen(10);": Instruction.InsAufladen(10, user); break; @@ -60,72 +76,39 @@ namespace Matomat case "showStats();": Instruction.InsShowStats(); break; case "addProd();": Instruction.InsAddProd(user); break; case "delProd();": Instruction.InsDelProd(user); break; - case "showStats(Month);": Instruction.InsShowStats(user,Instruction.Stats.LastMonth); break; + case "showStats(Month);": Instruction.InsShowStats(user, Instruction.Stats.LastMonth); break; case "showStats(All);": Instruction.InsShowStats(user, Instruction.Stats.All); break; } } } else if (input.type == InputData.types.Prod) { - EAN13 prod = new EAN13(input.id); - if (!prod.vaild()) - return; - - if (prod.GetProdType() == EAN13.ProdType.instruction) + Product prod; + try { - switch (prod.GetFunctName()) + prod = new Product(input.id); + } + catch (ArgumentException e) + { + Factory.getLCD().print(e.ToString(), 5, LCDDisplay.Status.OK); + System.Threading.Thread.Sleep(4500); + return; + } + + if (prod.GetProdType() == Product.ProdType.Instruction) + { + switch (prod.GetName()) { case "exit();": this.stopThread(true); break; case "showStats();": Instruction.InsShowStats(); break; - default: this.showProdInfo(prod); break; + default: Instruction.ShowProdInfo(prod); break; } } else { - this.showProdInfo(prod); + Instruction.ShowProdInfo(prod); } } } - - private void showProdInfo(EAN13 prod) - { - if (prod.GetProdType() == EAN13.ProdType.product) - { - long id = prod.GetProdId(); - double price = prod.GetProdPrice(); - string name = prod.GetProdName(); - Factory.getLCD().printb("Produkt: " + name + "\nPreis: " + price.ToString("N2") + "€ EAN13: " + id, 2); - } - if (prod.GetProdType() == EAN13.ProdType.instruction) - { - string name = prod.GetFunctName(); - Factory.getLCD().printb("Instruktion: " + name + "\nAdminCard + Instruction + TargetCard", 2); - } - } - - private void showUserInfo(RfidCode user) - { - string name = user.GetUserName(); - double konto = user.GetUserKonto(); - long id = user.GetUserId(); - double all = user.GetUserAll(); - Factory.getLCD().printb("User: " + name + " Betrag: " + konto.ToString("N2") + " €\nUserID: " + id + " Gesamt: " + all.ToString("N2") + " €", 5); - } - - internal void GetInitStatus() - { - List l = new Tables().getBestlist(); - string[] p = new string[3]; - for (int i = 0; i < 3; i++) - { - p[i] = (l.Count >= i + 1) ? l.ElementAt(i).name.PadRight(8, ' ').Substring(0, 8) + " " + (l.ElementAt(i).num % 1000).ToString().PadLeft(3, '0') : " "; - } - int k = 4232; - int avg = 234; - Factory.getLCD().print("Koffein im Umlauf: "+ k.ToString().PadLeft(4,' ') + " mg ("+ avg.ToString().PadLeft(4,' ')+" mg/Per)"+ - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + p[0] + - "² MATOMAT Wilkommen! ²" + p[1] + - "² Frohes genießen der Mate ²" + p[2]); - } } } diff --git a/Matomat/Data/EAN13.cs b/Matomat/Data/EAN13.cs deleted file mode 100644 index 204755c..0000000 --- a/Matomat/Data/EAN13.cs +++ /dev/null @@ -1,109 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Matomat.Output; -using Matomat.Database.Tables; -using Matomat.Helper; - -namespace Matomat.Data -{ - class EAN13 - { - private long prodId; - private bool found; - private ProdType type; - private int price; - private string name; - private string function; - private int id; - public enum ProdType - { - product, - instruction - } - - public EAN13(long prodId) - { - this.prodId = prodId; - this.load(); - } - - private void load() - { - TProduct t = new Tables().getProdTable(this.prodId); - if (t == null) - { - this.found = false; - return; - } - - this.found = true; - if (t.iscommand) - { - this.type = ProdType.instruction; - this.function = t.name; - } - else - { - this.type = ProdType.product; - this.name = t.name; - } - this.price = t.cost; - this.id = t.id; - } - - internal bool vaild() - { - if (!found) - { - Factory.getLCD().printb("Produkt wurde nich gefunden\nEAN13: " + this.GetProdId().ToString() + " unbekannt", LCDDisplay.Status.Error, 5); - System.Threading.Thread.Sleep(4500); - return false; - } - return true; - } - - internal ProdType GetProdType() - { - return this.type; - } - - internal long GetProdId() - { - return this.prodId; - } - - internal double GetProdPrice() - { - return ((double)this.price)/100; - } - - internal string GetProdName() - { - return this.name; - } - - internal string GetFunctName() - { - return this.function; - } - - public int GetDbId() - { - return this.id; - } - - public bool vaild(bool silent) - { - if (silent) - { - return found; - } - else - { - return vaild(); - } - } - } -} diff --git a/Matomat/Data/RfidCode.cs b/Matomat/Data/RfidCode.cs deleted file mode 100644 index 3a14bf2..0000000 --- a/Matomat/Data/RfidCode.cs +++ /dev/null @@ -1,110 +0,0 @@ -using System; -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.Data -{ - class RfidCode - { - private long userId; - private bool found; - private string username; - private string shortname; - private int credits; - private long all; - private bool admin; - private int id; - - public RfidCode(long userId) - { - this.userId = userId; - load(); - } - - private void load() - { - TUser u = new Tables().getUserTable(this.userId); - if (u == null) - { - this.found = false; - return; - } - TBest b = new Tables().getUserMax(u.id); - this.found = true; - this.shortname = u.shortname; - this.credits = u.credits; - this.all = b.num; - this.admin = u.admin; - this.id = u.id; - } - - public bool vaild(bool silent) - { - if (silent) - { - return found; - } - else - { - return vaild(); - } - } - - public bool vaild() - { - LCDDisplay lcd = Factory.getLCD(); - if (!found) - { - lcd.printb("User wurde nicht gefunden\nUserID:" + this.GetUserId().ToString() + " unbekannt", LCDDisplay.Status.Error, 5); - System.Threading.Thread.Sleep(4500); - return false; - } - return true; - } - - - internal string GetUserName() - { - return this.shortname; - } - - internal double GetUserKonto() - { - return ((double)this.credits) / 100; - } - - internal long GetUserId() - { - return this.userId; - } - - public int GetDbId() - { - return this.id; - } - - internal double GetUserAll() - { - return ((double)this.all) / 100; - } - - internal bool IsAdmin() - { - return this.admin; - } - - internal void SetUserKonto(double cost) - { - int p = (int)(cost * 100); - 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/DBDriverMysqli.cs b/Matomat/Database/DBDriverMysqli.cs index f653893..6a4490c 100644 --- a/Matomat/Database/DBDriverMysqli.cs +++ b/Matomat/Database/DBDriverMysqli.cs @@ -6,6 +6,7 @@ using MySql.Data; using MySql.Data.MySqlClient; using System.Threading; using Matomat.Helper; +using Matomat.Database.Query; namespace Matomat.Database { @@ -17,6 +18,7 @@ namespace Matomat.Database private MySqlDataReader data; private long id; private Thread pingThread; + protected string nameQuote = "`"; public DBDriverMysqli() { } @@ -218,6 +220,26 @@ namespace Matomat.Database this.data.Close(); return null; } + public override Dictionary loadAssoc() + { + if (!data_b || this.data == null) + { + return null; + } + if (this.data.HasRows) + { + this.data.Read(); + Dictionary row = new Dictionary(); + //object[] row = new object[this.data.FieldCount]; + for (int i = 0; i < this.data.FieldCount; i++) + { + row.Add(this.data.GetName(i),this.data[i]); + } + return row; + } + this.data.Close(); + return null; + } public override Exception getError() { return this.err; @@ -228,8 +250,26 @@ namespace Matomat.Database } public override string quote(string str) { - return str; + return "'" + MySqlHelper.EscapeString(str) + "'"; } + public override string quoteName(string name, string alias = null) + { + string quotedName = this.quoteNameStr(name.Split('.')); + if (alias != null) + { + quotedName += " AS " + this.quoteNameStr(new string[] { alias }); + } + return quotedName; + } + + private string quoteNameStr(string[] strArr) + { + for(int i=0;i _join; + private DatabaseQueryElement _select; + private DatabaseQueryElement _from; + private DatabaseQueryElement _where; + private DatabaseQueryElement _update; + private DatabaseQueryElement _set; + private DatabaseQueryElement _insert; + private DatabaseQueryElement _columns; + private DatabaseQueryElement _values; + private DatabaseQueryElement _group; + private DatabaseQueryElement _having; + private DatabaseQueryElement _order; + private DatabaseQueryElement _union; + private DatabaseQueryElement _delete; + private DatabaseQueryElement _limit; + public DBQuery(TDatabase db) + { + this.db = db; + } + + public DBQuery select(string columns) + { + this._type = QTypes.Select; + if (this._select == null) + { + this._select = new DatabaseQueryElement("SELECT", columns); + } + else + { + this._select.append(columns); + } + return this; + } + + public DBQuery from(string tables) + { + if (this._from == null) + { + this._from = new DatabaseQueryElement("FROM", tables); + } + else + { + this._from.append(tables); + } + return this; + } + + public DBQuery where(string conditions, string glue="AND") + { + if (this._where == null) + { + glue = glue.ToUpper(); + this._where = new DatabaseQueryElement("WHERE", conditions, " " + glue + " "); + } + else + { + this._where.append(conditions); + } + return this; + } + + public override string ToString() + { + string query = ""; + + switch (this._type) + { + case QTypes.Select: + query += this._select; + query += this._from; + if (this._join != null) + { + // special case for joins + foreach (DatabaseQueryElement join in this._join) + { + query += join; + } + } + + if (this._where != null) + { + query += this._where; + } + + if (this._group != null) + { + query += this._group; + } + + if (this._having != null) + { + query += this._having; + } + + if (this._order != null) + { + query += this._order; + } + if (this._limit != null) + { + query += this._limit; + } + + break; + case QTypes.Insert: + query += this._insert; + + if (this._set != null) + { + query += this._set; + } + else if (this._values != null) + { + if (this._columns != null) + { + query += this._columns; + } + + query += " VALUES "; + query += this._values; + } + break; + case QTypes.Update: + query += this._update; + + if (this._join != null) + { + // special case for joins + foreach (DatabaseQueryElement join in this._join) + { + query += join; + } + } + + query += this._set; + + if (this._where != null) + { + query += this._where; + } + break; + case QTypes.Union: + query += this._union; + break; + case QTypes.Delete: + query += this._delete; + query += this._from; + + if (this._join != null) + { + // special case for joins + foreach (DatabaseQueryElement join in this._join) + { + query += join; + } + } + if (this._where != null) + { + query += this._where; + } + break; + } + return query.Trim(); + } + + public DBQuery update(string table) + { + this._type = QTypes.Update; + this._update = new DatabaseQueryElement("UPDATE", table); + return this; + } + + public DBQuery set(string conditions, string glue=",") + { + if (this._set == null) + { + glue = glue.ToUpper(); + this._set = new DatabaseQueryElement("SET", conditions, "\n\t" + glue + " "); + } + else + { + this._set.append(conditions); + } + return this; + } + + public DBQuery insert(string table, bool incrementField = false) + { + this._type = QTypes.Insert; + this._insert = new DatabaseQueryElement("INSERT INTO", table); + this._autoIncrementField = incrementField; + return this; + } + + public DBQuery columns(string columns) + { + if (this._columns == null) + { + this._columns = new DatabaseQueryElement("()", columns); + } + else + { + this._columns.append(columns); + } + return this; + } + + public DBQuery values(string values) + { + if (this._values == null) + { + this._values = new DatabaseQueryElement("()", values, "),("); + } + else + { + this._values.append(values); + } + return this; + } + public DBQuery join(string type, string conditions) + { + if (this._join == null) + { + this._join = new List(); + } + this._join.Add(new DatabaseQueryElement(type.ToUpper() + " JOIN", conditions)); + return this; + } + public DBQuery outerJoin(string condition) + { + this.join("OUTER", condition); + return this; + } + public DBQuery rightJoin(string condition) + { + this.join("RIGHT", condition); + return this; + } + public DBQuery innerJoin(string condition) + { + this.join("INNER", condition); + return this; + } + public DBQuery leftJoin(string condition) + { + this.join("LEFT", condition); + return this; + } + public DBQuery group(string columns) + { + if (this._group == null) + { + this._group = new DatabaseQueryElement("GROUP BY", columns); + } + else + { + this._group.append(columns); + } + return this; + } + public DBQuery having(string conditions, string glue = "AND") + { + if (this._having == null) + { + glue = glue.ToUpper(); + this._having = new DatabaseQueryElement("HAVING", conditions, " " + glue + " "); + } + else + { + this._having.append(conditions); + } + return this; + } + public DBQuery order(string columns) + { + if (this._order == null) + { + this._order = new DatabaseQueryElement("ORDER BY", columns); + } + else + { + this._order.append(columns); + } + return this; + } + public DBQuery union(string query, bool distinct = false, string glue = "") + { + + if (this._order != null) + { + this.clear(QTypes.Order); + } + string name; + if (distinct) + { + name = "UNION DISTINCT ()"; + glue = ")\nUNION DISTINCT ("; + } + else + { + glue = ")\nUNION ("; + name = "UNION ()"; + } + if (this._union == null) + { + this._union = new DatabaseQueryElement(name, query, glue); + } + else + { + glue = ""; + this._union.append(query); + } + return this; + } + public DBQuery delete(string table) + { + this._type = QTypes.Delete; + this._delete = new DatabaseQueryElement("DELETE", null); + this.from(table); + return this; + } + public DBQuery clear(QTypes clause = QTypes.None) + { + switch (clause) + { + case QTypes.Select: + this._select = null; + this._type = QTypes.None; + break; + + case QTypes.Delete: + this._delete = null; + this._type = QTypes.None; + break; + + case QTypes.Update: + this._update = null; + this._type = QTypes.None; + break; + + case QTypes.Insert: + this._insert = null; + this._type = QTypes.None; + this._autoIncrementField = false; + break; + + case QTypes.From: + this._from = null; + break; + + case QTypes.Join: + this._join = null; + break; + + case QTypes.Set: + this._set = null; + break; + + case QTypes.Where: + this._where = null; + break; + + case QTypes.Group: + this._group = null; + break; + + case QTypes.Having: + this._having = null; + break; + + case QTypes.Order: + this._order = null; + break; + + case QTypes.Columns: + this._columns = null; + break; + + case QTypes.Values: + this._values = null; + break; + + case QTypes.Limit: + this._limit = null; + break; + + default: + this._type = QTypes.None; + this._select = null; + this._delete = null; + this._update = null; + this._insert = null; + this._from = null; + this._join = null; + this._set = null; + this._where = null; + this._group = null; + this._having = null; + this._order = null; + this._columns = null; + this._values = null; + this._autoIncrementField = false; + this._limit = null; + break; + } + + return this; + } + + public DBQuery limit(int start, int limit) + { + this._limit = new DatabaseQueryElement("LIMIT", start + "," + limit); + return this; + } + } +} diff --git a/Matomat/Database/Query/DBQueryMysqli.cs b/Matomat/Database/Query/DBQueryMysqli.cs new file mode 100644 index 0000000..ccd006f --- /dev/null +++ b/Matomat/Database/Query/DBQueryMysqli.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Matomat.Database.Query +{ + class DBQueryMysqli : DBQuery + { + public DBQueryMysqli(TDatabase db) : base(db) { + } + } +} diff --git a/Matomat/Database/Query/DatabaseQueryElement.cs b/Matomat/Database/Query/DatabaseQueryElement.cs new file mode 100644 index 0000000..bf7499b --- /dev/null +++ b/Matomat/Database/Query/DatabaseQueryElement.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Matomat.Database.Query +{ + class DatabaseQueryElement + { + private List elements; + private string name; + private string glue; + public DatabaseQueryElement(string name, string elements, string glue=",") + { + this.elements = new List(); + this.name = name; + this.glue = glue; + + this.append(elements); + } + + public void append(string elements) + { + if (!this.elements.Contains(elements)) + { + this.elements.Add(elements); + } + else + { + this.elements.Remove(elements); + this.elements.Add(elements); + } + } + + public void append(string[] elements) + { + foreach (string element in elements) + { + if (!this.elements.Contains(element)) + { + this.elements.Add(element); + } + else + { + this.elements.Remove(element); + this.elements.Add(element); + } + } + } + public override string ToString() + { + if (this.name.Substring(this.name.Length - 2) == "()") + { + return "\n" + this.name.Substring(this.name.Length - 2) + "(" + String.Join(this.glue, this.elements) + ")"; + } + else + { + return "\n" + this.name + " " + String.Join(this.glue, this.elements); + } + } + } +} diff --git a/Matomat/Database/TDatabase.cs b/Matomat/Database/TDatabase.cs index 9b58457..3d99f95 100644 --- a/Matomat/Database/TDatabase.cs +++ b/Matomat/Database/TDatabase.cs @@ -2,50 +2,135 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Matomat.Database.Query; +using Matomat.Database.Tables; 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; + 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]; - } + 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]); + 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); + db.connect(server, dbs, user, pw, port, driver); - if (db.getError() != null) - { - throw new Exception("db Fehler:" + db.getError()); - } + 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(); - } + 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 Dictionary loadAssoc(); + public abstract Exception getError(); + public abstract long getID(); + public abstract string quote(string str); + public abstract void RequestStop(); + public abstract DBQuery getQuery(bool newQuery=true); + public abstract string quoteName(string name, string alias = null); + + public void setQuery(DBQuery query) + { + this.query(query.ToString()); + } + public bool updateObject(string table, Table row, string key, bool nulls) + { + DBQuery query = this.getQuery(true); + + query.update(this.quoteName(table)); + System.Reflection.PropertyInfo[] p = row.GetType().GetProperties(); + foreach (System.Reflection.PropertyInfo item in p) + { + if (item.Name == key) + { + query.where(this.quoteName(key) + '=' + this.quote(item.GetValue(row, null).ToString())); + continue; + } + string val = ""; + if (item.GetValue(row, null) == null) + { + if (nulls) + { + val = "NULL"; + } + else + { + continue; + } + } + else + { + val = this.quote(item.GetValue(row, null).ToString()); + } + query.set(this.quoteName(item.Name) + "=" + val); + } + if (p.Length == 0) + { + return true; + } + this.setQuery(query); + return true; + } + + public bool updateObject(string table, Table row, string key) + { + return this.updateObject(table, row, key, false); + } + + public bool insertObject(string table, Table row, string key) + { + DBQuery query = this.getQuery(true); + + query.insert(this.quoteName(table)); + System.Reflection.PropertyInfo[] p = row.GetType().GetProperties(); + + List fields = new List(); + List values = new List(); + + foreach (System.Reflection.PropertyInfo item in p) + { + fields.Add(this.quoteName(item.Name)); + values.Add(item.GetValue(row, null).ToString()); + } + + if (p.Length == 0) + { + return true; + } + + query.columns(String.Join("", fields)); + query.values(String.Join("", values)); + + this.setQuery(query); + return true; + } + public bool insertObject(string table, Table row) + { + return this.insertObject(table, row, null); + } + } } diff --git a/Matomat/Database/Tables/Table.cs b/Matomat/Database/Tables/Table.cs new file mode 100644 index 0000000..4eaef25 --- /dev/null +++ b/Matomat/Database/Tables/Table.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Matomat.Helper; +using Matomat.Database.Query; + +namespace Matomat.Database.Tables +{ + abstract class Table + { + private string _tbl_key; + private string _tbl; + private TDatabase _db; + protected void Constructor(string table, string key, TDatabase db) + { + this._tbl = table; + this._tbl_key = key; + this._db = db; + } + public void reset() + { + System.Reflection.PropertyInfo[] fields = base.GetType().GetProperties(); + foreach (System.Reflection.PropertyInfo item in fields) + { + if (item.Name != this._tbl_key) + { + item.SetValue(this, null, null); + } + } + } + public bool load(Dictionary keys, bool reset = true) + { + if (reset) + { + this.reset(); + } + DBQuery query = Factory.getDBO().getQuery(true); + query.select("*"); + query.from(this._db.quoteName(this._tbl)); + System.Reflection.PropertyInfo[] fields = base.GetType().GetProperties(); + foreach (System.Reflection.PropertyInfo item in fields) + { + if (keys.Keys.Contains(item.Name)) + { + query.where(this._db.quoteName(item.Name) + " = " + this._db.quote(keys[item.Name])); + } + } + this._db.setQuery(query); + + Dictionary row; + try + { + row = this._db.loadAssoc(); + } + catch (Exception) + { + return false; + } + + if (this._db.getError() != null) + { + return false; + } + + if (row.Count == 0) + { + return false; + } + + return this.bind(row); + } + + public bool bind(Dictionary src) + { + return bind(src, new string[] { }); + } + + public bool bind(Dictionary src, string[] ignore) + { + if (src.Count == 0) + { + return false; + } + foreach (string itemignore in ignore) + { + if (src.Keys.Contains(itemignore)) + { + src.Remove(itemignore); + } + } + System.Reflection.PropertyInfo[] p = base.GetType().GetProperties(); + foreach (System.Reflection.PropertyInfo item in p) + { + if (src.Keys.Contains(item.Name)) + { + item.SetValue(this, src[item.Name], null); + } + } + return true; + } + public bool store() + { + return this.store(false); + } + public bool store(bool updateNulls) + { + string k = this._tbl_key; + bool stored; + if (base.GetType().GetProperty(k).GetValue(this, null) == null || (base.GetType().GetProperty(k).GetValue(this, null) != null && base.GetType().GetProperty(k).GetValue(this, null).ToString() == "0")) + { + stored = this._db.insertObject(this._tbl, this, this._tbl_key); + } else { + stored = this._db.updateObject(this._tbl, this, this._tbl_key, updateNulls); + } + return stored; + } + public bool delete() + { + string k = this._tbl_key; + if (base.GetType().GetProperty(k).GetValue(this, null) == null || (base.GetType().GetProperty(k).GetValue(this, null) != null && base.GetType().GetProperty(k).GetValue(this, null).ToString() == "0")) + { + return false; + } + DBQuery query = this._db.getQuery(true); + query.delete(this._tbl); + query.where(this._db.quoteName(this._tbl_key) + " = " + this._db.quote(base.GetType().GetProperty(k).GetValue(this, null).ToString())); + this._db.setQuery(query); + return true; + } + } +} diff --git a/Matomat/Database/Tables/TableHistory.cs b/Matomat/Database/Tables/TableHistory.cs new file mode 100644 index 0000000..c5a59c7 --- /dev/null +++ b/Matomat/Database/Tables/TableHistory.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Matomat.Database.Tables +{ + class TableHistory : Table + { + public TableHistory() + { + base.Constructor("history", "id", Matomat.Helper.Factory.getDBO()); + } + + public int id { get; private set; } + public int prod { get; private set; } + public int user { get; private set; } + public DateTime time { get; private set; } + } +} diff --git a/Matomat/Database/Tables/TableProduct.cs b/Matomat/Database/Tables/TableProduct.cs new file mode 100644 index 0000000..8ded24b --- /dev/null +++ b/Matomat/Database/Tables/TableProduct.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Matomat.Database.Tables +{ + class TableProduct : Table + { + public TableProduct() + { + base.Constructor("product", "id", Matomat.Helper.Factory.getDBO()); + } + + public int id { get; private set; } + public long barcode { get; private set; } + public string name { get; private set; } + public int cost { get; private set; } + public int caffeine { get; private set; } + public bool iscommand { get; private set; } + } +} diff --git a/Matomat/Database/Tables/TableUser.cs b/Matomat/Database/Tables/TableUser.cs new file mode 100644 index 0000000..ee60f80 --- /dev/null +++ b/Matomat/Database/Tables/TableUser.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Matomat.Database.Tables +{ + class TableUser : Table + { + public TableUser() { + base.Constructor("user", "id", Matomat.Helper.Factory.getDBO()); + } + + public int id { get; private set; } + public long userid { get; private set; } + public string username { get; private set; } + public int credits { get; private set; } + public bool admin { get; private set; } + public string shortname { get; private set; } + public string email { get; private set; } + } +} diff --git a/Matomat/Database/Tables/Tables.Best.cs b/Matomat/Database/Tables/Tables.Best.cs deleted file mode 100644 index d3fd42b..0000000 --- a/Matomat/Database/Tables/Tables.Best.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Data; - -namespace Matomat.Database.Tables -{ - class TBest : Tables - { - public TBest(long num, string name) - { - this._num = num; - this._name = name; - } - public TBest() - { - this._name = ""; - this._num = 0; - } - private long _num; - public long num - { - get - { - return _num; - } - set - { - if (_num != value) - { - _num = value; - } - } - } - - private string _name; - public string name - { - get - { - return _name; - } - set - { - if (_name != value) - { - _name = value; - } - } - } - } -} diff --git a/Matomat/Database/Tables/Tables.Product.cs b/Matomat/Database/Tables/Tables.Product.cs deleted file mode 100644 index d6674e9..0000000 --- a/Matomat/Database/Tables/Tables.Product.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Matomat.Database.Tables -{ - class TProduct : Tables - { - public TProduct() - { - } - public TProduct(int id, long barcode, string name, int cost, bool iscommand, int caffeine) - { - this._id = id; - this._barcode = barcode; - this._name = name; - this._cost = cost; - this._iscommand = iscommand; - this._caffeine = caffeine; - } - private int _id; - public int id - { - get - { - return _id; - } - set - { - if (_id != value) - { - _id = value; - } - } - } - - private long _barcode; - public long barcode - { - get - { - return _barcode; - } - set - { - if (_barcode != value) - { - _barcode = value; - } - } - } - - private string _name; - public string name - { - get - { - return _name; - } - set - { - if (_name != value) - { - _name = value; - } - } - } - private int _cost; - public int cost - { - get - { - return _cost; - } - set - { - if (_cost != value) - { - _cost = value; - } - } - } - private bool _iscommand; - public bool iscommand - { - get - { - return _iscommand; - } - set - { - if (_iscommand != value) - { - _iscommand = value; - } - } - } - - public int _caffeine { get; private set; } - } -} diff --git a/Matomat/Database/Tables/Tables.User.cs b/Matomat/Database/Tables/Tables.User.cs deleted file mode 100644 index d5712e4..0000000 --- a/Matomat/Database/Tables/Tables.User.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Data; - -namespace Matomat.Database.Tables -{ - class TUser : Tables - { - public TUser() - { - } - public TUser(int id, long userid, string username, int credits, bool admin, string shortname, string email) - { - this._id = id; - this._userid = userid; - this._username = username; - this._credits = credits; - this._admin = admin; - this._shortname = shortname; - this._email = email; - } - private int _id; - public int id - { - get - { - return _id; - } - set - { - if (_id != value) - { - _id = value; - } - } - } - - private long _userid; - public long userid - { - get - { - return _userid; - } - set - { - if (_userid != value) - { - _userid = value; - } - } - } - - private string _username; - public string username - { - get - { - return _username; - } - set - { - if (_username != value) - { - _username = value; - } - } - } - - private int _credits; - public int credits - { - get - { - return _credits; - } - set - { - if (_credits != value) - { - _credits = value; - } - } - } - - private bool _admin; - public bool admin - { - get - { - return _admin; - } - set - { - if (_admin != value) - { - _admin = value; - } - } - } - - private string _shortname; - public string shortname - { - get - { - return _shortname; - } - set - { - if (_shortname != value) - { - _shortname = value; - } - } - } - - private string _email; - public string email - { - get - { - return _email; - } - set - { - if (_email != value) - { - _email = value; - } - } - } - } -} diff --git a/Matomat/Database/Tables/Tables.cs b/Matomat/Database/Tables/Tables.cs deleted file mode 100644 index 988407e..0000000 --- a/Matomat/Database/Tables/Tables.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Matomat.Database; -using Matomat.Helper; - -namespace Matomat.Database.Tables -{ - class Tables - { - public TUser getUserTable(int id) - { - return new TUser(); - } - public TUser getUserTable(long userid) - { - 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) - { - return null; - } - return new TUser((int)row[0], (long)row[1], (string)row[2], (int)row[3], (bool)row[4], (string)row[5], (string)row[6]); - } - public TProduct getProdTable(int id) - { - return new TProduct(); - } - public TProduct getProdTable(long barcode) - { - TDatabase db = Factory.getDBO(); - db.query("SELECT `id`, `barcode`,`name`,`cost`,`iscommand`,`caffeine` FROM `product` WHERE `barcode` = " + db.quote(barcode.ToString())); - object[] row = db.getResult(); - if (row == null) - { - return null; - } - return new TProduct((int)row[0], (long)row[1], (string)row[2], (int)row[3], (bool)row[4], (int)row[5]); - } - - internal List getBestlist() - { - 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` "+ - "LEFT JOIN `user` AS `u` ON `u`.`id` = `h`.`user` "+ - "WHERE `p`.`iscommand` = 0 GROUP BY `h`.`user` ORDER BY `num` DESC LIMIT 0,4"); - List rows = db.getResultList(); - if (rows == null) - { - return null; - } - List ret = new List(); - foreach (object[] row in rows) - { - ret.Add(new TBest((long)row[0], (string)row[1])); - } - return ret; - } - - internal TBest getUserMax(int p) - { - 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` "+ - "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/Instruction.cs b/Matomat/Instruction.cs index 93ff622..22f1a14 100644 --- a/Matomat/Instruction.cs +++ b/Matomat/Instruction.cs @@ -2,10 +2,10 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using Matomat.Data; using Matomat.Helper; using Matomat.Output; using Matomat.Input; +using Matomat.Model; namespace Matomat { @@ -16,129 +16,149 @@ namespace Matomat LastMonth, All } - public static void InsAufladen(int betrag, RfidCode admin) + public static void InsAufladen(int betrag, User admin) { - if (!admin.IsAdmin()) + if (!admin.GetAdmin()) { - Factory.getLCD().printb("Du bist kein Admin,\ndu kannst nichts Aufladen!", LCDDisplay.Status.Error, 5); - System.Threading.Thread.Sleep(4500); - return; - } - if (!Factory.getConfig().sell) - { - Factory.getLCD().printb("Das Aufladen\nist abgeschaltet!", LCDDisplay.Status.Error, 5); + Factory.getLCD().print("Du bist kein Admin,\ndu kannst nichts Aufladen!", 5, LCDDisplay.Status.Error); System.Threading.Thread.Sleep(4500); return; } - Factory.getLCD().printb("Bitte die Zielkarte auf den\nRFID Leser auflegen"); + if (!Factory.getConfig().sell) + { + Factory.getLCD().print("Das Aufladen\nist abgeschaltet!", 5, LCDDisplay.Status.Error); + System.Threading.Thread.Sleep(4500); + return; + } + + Factory.getLCD().print("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()) + User user; + try + { + user = new User(target.id); + } + catch (ArgumentException e) + { + Factory.getLCD().print(e.ToString(), 5, LCDDisplay.Status.OK); + System.Threading.Thread.Sleep(4500); return; + } - Factory.getLCD().printb("Betrag von User " + user.GetUserName() + "\num " + betrag + " € verringert"); - user.SetUserKonto(user.GetUserKonto()-betrag); + Factory.getLCD().print("Betrag von User " + user.GetShortname() + "\num " + betrag + " € verringert"); + user.SetUserKonto(betrag*-1); System.Threading.Thread.Sleep(2000); } - public static void InsAddUser(RfidCode admin) + public static void InsAddUser(User admin) { - if (!admin.IsAdmin()) + if (!admin.GetAdmin()) { - Factory.getLCD().printb("Du bist kein Admin,\ndu kannst niemanden Hinzufügen!", LCDDisplay.Status.Error, 5); + Factory.getLCD().print("Du bist kein Admin,\ndu kannst niemanden Hinzufügen!", 5, LCDDisplay.Status.Error); System.Threading.Thread.Sleep(4500); return; } - Factory.getLCD().printb("Bitte die neue Userkarte auf\nden RFID Leser auflegen."); + Factory.getLCD().print("Bitte die neue Userkarte auf\nden RFID Leser auflegen."); InputData target = Factory.getInput().getCardInput(20); if (target.type == InputData.types.None) return; - RfidCode user = new RfidCode(target.id); - if (user.vaild(true)) + try { - Factory.getLCD().printb("Dieser User ist schon\nRegistriert", LCDDisplay.Status.Warn, 5); + User user = new User(target.id); + } + catch (ArgumentException) + { + Factory.getLCD().print("Bitte den Usernamen [255] eingeben:"); + string username = Console.ReadLine(); + Factory.getLCD().print("Username:\n" + username); + System.Threading.Thread.Sleep(4500); + + Factory.getLCD().print("Bitte Kürzel [8] eingeben:"); + string kurzel = Console.ReadLine(); + Factory.getLCD().print("Kürzel:\n" + kurzel); + System.Threading.Thread.Sleep(4500); + + Factory.getLCD().print("Bitte E-Mail [255] eingeben:"); + string email = Console.ReadLine(); + Factory.getLCD().print("E-Mail:\n" + email); + System.Threading.Thread.Sleep(4500); + + Factory.getLCD().print("Bitte Admin [1|0] eingeben:"); + bool isadmin; + try + { + isadmin = bool.Parse(Console.ReadLine()); + } + catch (Exception) + { + Factory.getLCD().print("Fehler bei der Eingabe\ndes Adminflags ABBRUCH!", 5, LCDDisplay.Status.Error); + System.Threading.Thread.Sleep(4500); + return; + } + Factory.getLCD().print("Admin:\n" + isadmin); + System.Threading.Thread.Sleep(4500); + + User.Add(target.id, username, isadmin, kurzel, email); + + Factory.getLCD().print("Neuen User eingefügt:\n" + kurzel); 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); + Factory.getLCD().print("Dieser User ist schon\nRegistriert", 5, LCDDisplay.Status.Warn); System.Threading.Thread.Sleep(4500); + return; } - public static void InsDelUser(RfidCode admin) + public static void InsDelUser(User admin) { - if (!admin.IsAdmin()) + if (!admin.GetAdmin()) { - Factory.getLCD().printb("Du bist kein Admin,\ndu kannst niemanden Löschen!", LCDDisplay.Status.Error, 5); + Factory.getLCD().print("Du bist kein Admin,\ndu kannst niemanden Löschen!", 5, LCDDisplay.Status.Error); System.Threading.Thread.Sleep(4500); return; } - Factory.getLCD().printb("Bitte die alte Userkarte auf\nden RFID Leser auflegen."); + Factory.getLCD().print("Bitte die alte Userkarte auf\nden RFID Leser auflegen."); InputData target = Factory.getInput().getCardInput(20); if (target.type == InputData.types.None) return; - RfidCode user = new RfidCode(target.id); - if (!user.vaild()) + User user; + try + { + user = new User(target.id); + } + catch (ArgumentException e) + { + Factory.getLCD().print(e.ToString(), 5, LCDDisplay.Status.OK); + System.Threading.Thread.Sleep(4500); return; + } - Factory.getDBO().query("DELETE * FROM `user` WHERE `id`='" + Factory.getDBO().quote(user.GetDbId().ToString()) + "'"); + User.Remove(user.GetId()); - Factory.getLCD().printb("User erfolgreich gelöscht:\n" + user.GetUserName(), 3); + Factory.getLCD().print("User erfolgreich gelöscht:\n" + user.GetShortname(), 3); System.Threading.Thread.Sleep(2500); } - public static void sell(EAN13 prod, RfidCode user) + public static void sell(Product prod, User user) { - if (Factory.getConfig().sell && user.GetUserKonto() > 1900) + if (Factory.getConfig().sell && user.GetCredits() >= 20) { - Factory.getLCD().printb("Du hast zu viel Geld auf dem Konto\nBITTE bezahlen! | Grenze 20€", LCDDisplay.Status.Error, 5); + Factory.getLCD().print("Du hast zu viel Geld auf dem Konto\nBITTE bezahlen! | Grenze 20€", 5, LCDDisplay.Status.Error); return; } - Factory.getDBO().query("INSERT INTO `history` (`prod`,`user`,`time`) VALUES (" + - "'" + Factory.getDBO().quote(prod.GetDbId().ToString()) + "'," + - "'" + Factory.getDBO().quote(user.GetDbId().ToString()) + "'," + - "NOW())"); + History.Add(prod.GetId(), user.GetId()); if (Factory.getConfig().sell) { - Factory.getDBO().query("UPDATE `user` SET `credits` = `credits`+" + Factory.getDBO().quote((prod.GetProdPrice() * 100).ToString("N0")) + " WHERE `user`.`id` = '" + - Factory.getDBO().quote(user.GetDbId().ToString()) + "'"); + user.SetUserKonto(prod.GetPrice()); } - Factory.getLCD().printb("Guten Durst mit der\n" + prod.GetProdName() + "!", 4); + Factory.getLCD().print("Guten Durst mit der\n" + prod.GetName() + "!", 4); System.Threading.Thread.Sleep(3500); } @@ -147,62 +167,113 @@ namespace Matomat throw new NotImplementedException(); } - internal static void InsAddProd(RfidCode admin) + internal static void InsAddProd(User admin) { - if (!admin.IsAdmin()) + if (!admin.GetAdmin()) { - Factory.getLCD().printb("Du bist kein Admin,\ndu kannst nichts Hinzufügen!", LCDDisplay.Status.Error, 5); + Factory.getLCD().print("Du bist kein Admin,\ndu kannst nichts Hinzufügen!", 5, LCDDisplay.Status.Error); System.Threading.Thread.Sleep(4500); return; } - Factory.getLCD().printb("Bitte das neue Produkt\nüber den Barcodeleser ziehen."); + Factory.getLCD().print("Bitte das neue Produkt\nüber den Barcodeleser ziehen."); InputData target = Factory.getInput().getProdInput(20); if (target.type == InputData.types.None) return; - EAN13 code = new EAN13(target.id); - if (code.vaild(true)) + try { - Factory.getLCD().printb("Dieser Produkt ist schon\nRegistriert", LCDDisplay.Status.Warn, 5); + Product code = new Product(target.id); + } + catch (ArgumentException) + { + Factory.getLCD().print("Bitte den Produktnamen [255] eingeben:"); + string name = Console.ReadLine(); + Factory.getLCD().print("Produktname:\n" + name); + System.Threading.Thread.Sleep(4500); + + Factory.getLCD().print("Bitte Preis [Cent] eingeben:"); + int preis; + try + { + preis = int.Parse(Console.ReadLine()); + } + catch (Exception) + { + Factory.getLCD().print("Fehler bei der Eingabe des Preises\nABBRUCH!", 5, LCDDisplay.Status.Error); + System.Threading.Thread.Sleep(4500); + return; + } + Factory.getLCD().print("Preis:\n" + preis + " €Cent"); + System.Threading.Thread.Sleep(4500); + + Factory.getLCD().print("Bitte Koffeingehalt [mg] eingeben:"); + int coffeine; + try + { + coffeine = int.Parse(Console.ReadLine()); + } + catch (Exception) + { + Factory.getLCD().print("Fehler bei der Eingabe\ndes Koffeingehaltes ABBRUCH!", 5, LCDDisplay.Status.Error); + System.Threading.Thread.Sleep(4500); + return; + } + Factory.getLCD().print("Koffeingehalt:\n" + coffeine + " mg"); + System.Threading.Thread.Sleep(4500); + + Product.Add(target.id, name, preis, coffeine); + + Factory.getLCD().print("Neues Produkt eingefügt:\n" + name); System.Threading.Thread.Sleep(4500); return; } - - Factory.getLCD().printb("Bitte den Produktnamen [255] eingeben:"); - string name = Console.ReadLine(); - Factory.getLCD().printb("Produktname:\n" + name); - System.Threading.Thread.Sleep(4500); - - Factory.getLCD().printb("Bitte Preis [Cent] eingeben:"); - string preis = Console.ReadLine(); - Factory.getLCD().printb("Preis:\n" + preis + " €Cent"); - System.Threading.Thread.Sleep(4500); - - Factory.getLCD().printb("Bitte Koffeingehalt [mg] eingeben:"); - string coffeine = Console.ReadLine(); - Factory.getLCD().printb("Koffeingehalt:\n" + coffeine + " mg"); - System.Threading.Thread.Sleep(4500); - - string sql = "INSERT INTO `product` (`barcode`,`name`,`cost`,`caffeine`) VALUES (" + - "'" + Factory.getDBO().quote(target.id.ToString()) + "'," + - "'" + Factory.getDBO().quote(name) + "'," + - "'" + Factory.getDBO().quote(preis) + "'," + - "'" + Factory.getDBO().quote(coffeine) + "')"; - Factory.getDBO().query(sql); - - Factory.getLCD().printb("Neues Produkt eingefügt:\n" + name); + Factory.getLCD().print("Dieser Produkt ist schon\nRegistriert", 5, LCDDisplay.Status.Warn); System.Threading.Thread.Sleep(4500); + return; } - internal static void InsDelProd(RfidCode user) + internal static void InsDelProd(User user) { throw new NotImplementedException(); } - internal static void InsShowStats(RfidCode user, Stats stats) + internal static void InsShowStats(User user, Stats stats) { throw new NotImplementedException(); } + + public static void ShowProdInfo(Product prod) + { + if (prod.GetProdType() == Product.ProdType.Product) + { + Factory.getLCD().print("Produkt: " + prod.GetName() + "\nPreis: " + prod.GetPrice().ToString("N2") + "€ EAN13: " + prod.GetBarcode(), 2); + } + if (prod.GetProdType() == Product.ProdType.Instruction) + { + Factory.getLCD().print("Instruktion: " + prod.GetName() + "\nAdminCard + Instruction + TargetCard", 2); + } + } + + public static void GetInitStatus() + { + List> l = History.GetBestlist(); + int k = History.GetCoffeine(); + int avg = History.GetAvgCoffeine(); + string[] p = new string[3]; + for (int i = 0; i < 3; i++) + { + p[i] = (l.Count >= i + 1) ? l.ElementAt(i).Item1.PadRight(8, ' ').Substring(0, 8) + " " + (l.ElementAt(i).Item2 % 1000).ToString().PadLeft(3, '0') : " "; + } + Factory.getLCD().print("Koffein im Umlauf: " + k.ToString().PadLeft(4, ' ') + " mg (" + avg.ToString().PadLeft(4, ' ') + " mg/Per)" + + "²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + p[0] + + "² MATOMAT Wilkommen! ²" + p[1] + + "² Frohes genießen der Mate ²" + p[2]); + } + + public static void showUserInfo(User user) + { + Factory.getLCD().print("User: " + user.GetShortname() + " Betrag: " + user.GetCredits().ToString("N2") + " €\nUserID: " + user.GetUserId() + " Gesamt: " + user.GetUserAll().ToString("N2") + " €", 5); + } } } diff --git a/Matomat/Matomat-UML.ncp b/Matomat/Matomat-UML.ncp new file mode 100644 index 0000000..e48a11e --- /dev/null +++ b/Matomat/Matomat-UML.ncp @@ -0,0 +1,975 @@ + + Matomat + + Matomat.exe + CSharp + + + Automat + Internal + + + False + public event stopEvent stopThread + public Automat() + internal Void doJob(InputData input) + private Void showProdInfo(EAN13 prod) + private Void showUserInfo(RfidCode user) + internal Void GetInitStatus() + None + + + DatabaseQueryElement + Internal + + + False + private List<String> elements + private String name + private String glue + public DatabaseQueryElement(String name, String elements) + public DatabaseQueryElement(String name, String elements, String glue) + public Void append(String elements) + public Void append(String[] elements) + public override String ToString() + None + + + DBQuery + Internal + + + False + private TDatabase db + private QTypes _type + private DatabaseQueryElement _select + private DatabaseQueryElement _from + private DatabaseQueryElement _where + public DBQuery(TDatabase db) + public DBQuery select(String columns) + public DBQuery from(String tables) + public DBQuery where(String conditions, String glue) + public DBQuery where(String conditions) + public override String ToString() + None + + + DBQueryMysqli + Internal + + + False + public DBQueryMysqli(TDatabase db) + None + + + Table + Internal + + + False + private String _tbl_key + private String _tbl + private TDatabase _db + protected Table() + protected Void Constructor(String table, String key, TDatabase db) + public Boolean load(Dictionary<String, String> keys, Boolean reset) + public Boolean bind(Dictionary<String, Object> src) + public Boolean bind(Dictionary<String, Object> src, String[] ignore) + Abstract + + + TableHistory + Internal + + + False + public Int32 id { get; private set; } + public Int32 prod { get; private set; } + public Int32 user { get; private set; } + public DateTime time { get; private set; } + public TableHistory() + None + + + TableProduct + Internal + + + False + public Int32 id { get; private set; } + public Int64 barcode { get; private set; } + public String name { get; private set; } + public Int32 cost { get; private set; } + public Int32 caffeine { get; private set; } + public Boolean iscommand { get; private set; } + public TableProduct() + None + + + TableUser + Internal + + + False + public Int32 id { get; private set; } + public Int64 userid { get; private set; } + public String username { get; private set; } + public Int32 credits { get; private set; } + public Boolean admin { get; private set; } + public String shortname { get; private set; } + public String email { get; private set; } + public TableUser() + None + + + BarcodeReader + Internal + + + False + private Boolean _shouldStop + public BarcodeReader() + internal Int64 getCodeID() + internal Void Abort() + None + + + Config + Internal + + + False + private AppSettingsReader config + private static Dictionary<String, Config> instances + 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 Int32 mysql_port { get; private set; } + public String mysql_pw { get; private set; } + public String mysql_driver { get; private set; } + public Boolean debug { get; private set; } + public Boolean sell { get; private set; } + private Config() + static Config() + private Void loadFile() + internal static Config getInstance(String file) + public Void RequestStop() + None + + + TDatabase + Internal + + + False + private static Dictionary<String, TDatabase> instances + protected TDatabase() + static TDatabase() + internal static TDatabase getInstance(String server, String dbs, String user, String pw, Int32 port, String driver) + public abstract Boolean connect(String server, String dbs, String user, String pw, Int32 port, String driver) + public abstract Void query(String sql) + public abstract Object[] getResult() + public abstract List<Object[]> getResultList() + public abstract Dictionary<String, Object> loadAssoc() + public abstract Exception getError() + public abstract Int64 getID() + public abstract String quote(String str) + public abstract Void RequestStop() + public abstract DBQuery getQuery(Boolean newQuery) + public abstract String quoteName(String name) + public abstract String quoteName(String name, String alias) + public Void setQuery(DBQuery query) + Abstract + + + DBDriverMysqli + Internal + + + False + private MySqlConnection conn + private Exception err + private Boolean data_b + private MySqlDataReader data + private Int64 id + private Thread pingThread + protected String nameQuote + public DBDriverMysqli() + public override Boolean connect(String server, String dbs, String user, String pw, Int32 port, String driver) + private Void startPing() + public Void ping() + public override Void query(String sql) + private Void set(String sql) + private Void create(String sql) + private Void truncate(String sql) + private Void delete(String sql) + private Void update(String sql) + private Void insert(String sql) + private Void select(String sql) + public override List<Object[]> getResultList() + public override Object[] getResult() + public override Dictionary<String, Object> loadAssoc() + public override Exception getError() + public override Int64 getID() + public override String quote(String str) + public override String quoteName(String name) + public override String quoteName(String name, String alias) + private String quoteNameStr(String[] strArr) + public override Void RequestStop() + public override DBQuery getQuery(Boolean newQuery) + None + + + Factory + Internal + + + False + private static LCDDisplay lcd_i + private static TInput inp_i + private static Config con_i + private static TDatabase dbo_i + public Factory() + static Factory() + public static LCDDisplay getLCD() + public static Void rmLCD() + private static LCDDisplay _createLCDDisplay() + public static TInput getInput() + private static TInput _createInput() + public static Void rmInput() + public static Config getConfig() + private static Config _createConfig(String file) + public static Void rmConfig() + public static TDatabase getDBO() + private static TDatabase _createDBO() + public static Void rmDBO() + None + + + String + Public + + + False + public static Byte[] ToCharArray(String str, String search, Byte[] replace) + Static + + + Array + Public + + + False + public static Byte[] Substring(Byte[] b, Int32 start, Int32 length) + public static Byte[] Substring(Byte[] b, Int32 start) + Static + + + Double + Public + + + False + public static String ToString(Double d, Int32 f) + Static + + + InputData + Internal + + + False + public types type { get; set; } + public Int64 id { get; set; } + public InputData() + None + + + TInput + Internal + + + False + private InputData data + private RFIDReader rfid + private BarcodeReader code + private Boolean cardrunning + private Boolean coderunning + private static Dictionary<String, TInput> instances + private TInput(String comport) + static TInput() + private Void doWorkCard() + private Void doWorkCode() + public InputData getAnyInput(Int32 timeout) + public InputData getProdInput(Int32 timeout) + public static TInput getInstance(String comport) + public InputData getCardInput(Int32 timeout) + public Void RequestStop() + None + + + Instruction + Internal + + + False + public Instruction() + public static Void InsAufladen(Int32 betrag, RfidCode admin) + public static Void InsAddUser(RfidCode admin) + public static Void InsDelUser(RfidCode admin) + public static Void sell(EAN13 prod, RfidCode user) + internal static Void InsShowStats() + internal static Void InsAddProd(RfidCode admin) + internal static Void InsDelProd(RfidCode user) + internal static Void InsShowStats(RfidCode user, Stats stats) + None + + + LCDDisplay + Internal + + + False + private Queue<Message> messages + private Queue<Beeb> beeb + private volatile Boolean _shouldStop + private SerialPort serialPort + private Boolean disable + private static Dictionary<String, LCDDisplay> instances + private LCDDisplay(String port) + static LCDDisplay() + private Void BeebThread() + private Void DisplayThread() + private Void beep(Int32 frequency, Int32 duration) + private Void display(String text, Status status) + private Void displayClear() + private Void displayCode(Byte[] p) + private Void createChars() + private Void displayEscCode(String code) + public Void printb(String text) + public Void printb(String text, Int32 time) + public Void printb(String text, Status status) + public Void printb(String text, Status status, Int32 time) + public Void print(String text) + public Void print(String text, Int32 time) + public Void print(String text, Status status) + public Void print(String text, Status status, Int32 time) + private Void anzeige(String text, Status status, Int32 time) + public static LCDDisplay getInstance(String port) + public Void RequestStop() + None + + + Message + Public + + + False + public String text + public Status status + public Int32 time + public Message(String text, Status status, Int32 time) + None + + + Beeb + Public + + + False + public Int32 frequency + public Int32 duration + public Beeb(Int32 frequency, Int32 duration) + None + + + EAN13 + Internal + + + False + private Int64 prodId + private Boolean found + private ProdType type + private Int32 price + private String name + private String function + private Int32 id + public EAN13(Int64 prodId) + private Void load() + internal Boolean vaild() + internal ProdType GetProdType() + internal Int64 GetProdId() + internal Double GetProdPrice() + internal String GetProdName() + internal String GetFunctName() + public Int32 GetDbId() + public Boolean vaild(Boolean silent) + None + + + Program + Internal + + + False + public Program() + public static Void Main(String[] args) + private static Void test() + None + + + RFIDReader + Internal + + + False + private RFIDReaderClass r + public RFIDReader(String comport) + internal Int64 getCardID() + internal Void Abort() + None + + + Tables + Internal + + + False + public Tables() + public TUser getUserTable(Int32 id) + public TUser getUserTable(Int64 userid) + public TProduct getProdTable(Int32 id) + public TProduct getProdTable(Int64 barcode) + internal List<TBest> getBestlist() + internal TBest getUserMax(Int32 p) + None + + + TBest + Internal + + + False + private Int64 _num + private String _name + public Int64 num { get; set; } + public String name { get; set; } + public TBest(Int64 num, String name) + public TBest() + None + + + TUser + Internal + + + False + private Int32 _id + private Int64 _userid + private String _username + private Int32 _credits + private Boolean _admin + private String _shortname + private String _email + public Int32 id { get; set; } + public Int64 userid { get; set; } + public String username { get; set; } + public Int32 credits { get; set; } + public Boolean admin { get; set; } + public String shortname { get; set; } + public String email { get; set; } + public TUser() + public TUser(Int32 id, Int64 userid, String username, Int32 credits, Boolean admin, String shortname, String email) + None + + + RfidCode + Internal + + + False + private Int64 userId + private Boolean found + private String username + private String shortname + private Int32 credits + private Int64 all + private Boolean admin + private Int32 id + public RfidCode(Int64 userId) + private Void load() + public Boolean vaild(Boolean silent) + public Boolean vaild() + internal String GetUserName() + internal Double GetUserKonto() + internal Int64 GetUserId() + public Int32 GetDbId() + internal Double GetUserAll() + internal Boolean IsAdmin() + internal Void SetUserKonto(Double cost) + None + + + Worker + Internal + + + False + private volatile Boolean _shouldStop + private Automat automat + private LCDDisplay lcd + private TInput inp + public Worker() + public Void DoWork() + private Void RequestStop(Boolean stop) + None + + + TProduct + Internal + + + False + private Int32 _id + private Int64 _barcode + private String _name + private Int32 _cost + private Boolean _iscommand + public Int32 id { get; set; } + public Int64 barcode { get; set; } + public String name { get; set; } + public Int32 cost { get; set; } + public Boolean iscommand { get; set; } + public Int32 _caffeine { get; private set; } + public TProduct() + public TProduct(Int32 id, Int64 barcode, String name, Int32 cost, Boolean iscommand, Int32 caffeine) + None + + + stopEvent + Public + + + False + Void + Boolean stop + + + QTypes + Public + + + False + Select = 0 + + + types + Public + + + False + Card = 0 + Prod = 1 + None = 2 + + + Stats + Public + + + False + LastMonth = 0 + All = 1 + + + Status + Public + + + False + OK = 0 + Warn = 1 + Error = 2 + + + ProdType + Public + + + False + product = 0 + instruction = 1 + + + + + Vertical + Vertical + + 2795 + 1997 + + + 2665 + 2022 + + + + Vertical + Vertical + + 2717 + 2001 + + + 2585 + 2001 + + + + Vertical + Vertical + + + Horizontal + Horizontal + + 4457 + 1654 + + + 4494 + 1654 + + + + Vertical + Vertical + + 801 + 1345 + + + + Vertical + Vertical + + 2832 + 1998 + + + + Vertical + Vertical + + + Vertical + Vertical + + 803 + 989 + + + + Horizontal + Horizontal + + 3277 + 2688 + + + + Vertical + Horizontal + + 3628 + 2513 + + + + Vertical + Horizontal + + 3961 + 2521 + + + + Vertical + Vertical + + 3811 + 2525 + + + 3811 + 2489 + + + + Horizontal + Horizontal + + + Vertical + Vertical + + 247 + 362 + + + 247 + 308 + + + + Vertical + Vertical + + 132 + 360 + + + 132 + 323 + + + + Vertical + Horizontal + + 325 + 210 + + + + + Vertical + Vertical + + 3353 + 1807 + + Unidirectional + Association + db + 1 + + + + Vertical + Vertical + Unidirectional + Association + _type + 1 + + + + Horizontal + Horizontal + Unidirectional + Association + _select + 1 + + + + Horizontal + Horizontal + + 3074 + 2643 + + Unidirectional + Association + _from + 1 + + + + Horizontal + Horizontal + + 3073 + 2616 + + Unidirectional + Association + _where + 1 + + + + Vertical + Vertical + + 3660 + 2176 + + + 3450 + 1807 + + Unidirectional + Association + _db + 1 + + + + Vertical + Vertical + Unidirectional + Association + lcd_i + 1 + + + + Horizontal + Vertical + + 4087 + 1318 + + Unidirectional + Association + inp_i + 1 + + + + Horizontal + Vertical + + 2829 + 1190 + + + 2172 + 1338 + + Unidirectional + Association + con_i + 1 + + + + Horizontal + Vertical + + 3096 + 1209 + + + 3398 + 1303 + + Unidirectional + Association + dbo_i + 1 + + + + Horizontal + Horizontal + Unidirectional + Association + data + 1 + + + + Vertical + Vertical + + 4061 + 1798 + + Unidirectional + Association + rfid + 1 + + + + Vertical + Vertical + Unidirectional + Association + code + 1 + + + + Horizontal + Horizontal + + 2822 + 2184 + + + 2842 + 2184 + + Unidirectional + Association + status + 1 + + + + Vertical + Vertical + Unidirectional + Association + type + 1 + + + + Vertical + Vertical + Unidirectional + Association + automat + 1 + + + + Vertical + Vertical + + + + Vertical + Vertical + + + + \ No newline at end of file diff --git a/Matomat/Matomat.csproj b/Matomat/Matomat.csproj index 7eb412a..b6871f6 100644 --- a/Matomat/Matomat.csproj +++ b/Matomat/Matomat.csproj @@ -56,6 +56,13 @@ + + + + + + + @@ -64,15 +71,13 @@ + + + - - - - - @@ -89,7 +94,6 @@ - diff --git a/Matomat/Model/History.cs b/Matomat/Model/History.cs new file mode 100644 index 0000000..7eb5dfa --- /dev/null +++ b/Matomat/Model/History.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Matomat.Helper; +using Matomat.Database.Query; +using Matomat.Database.Tables; + +namespace Matomat.Model +{ + class History + { + public static List> GetBestlist(int limit = 3) + { + DBQuery query = Factory.getDBO().getQuery(true); + query.select("COUNT(*) AS " + Factory.getDBO().quoteName("num")); + query.select(Factory.getDBO().quoteName("u.shortname", "name")); + query.from(Factory.getDBO().quoteName("history", "h")); + query.leftJoin(Factory.getDBO().quoteName("product", "p") + " ON " + Factory.getDBO().quoteName("p.id") + " = " + Factory.getDBO().quoteName("h.prod")); + query.leftJoin(Factory.getDBO().quoteName("user", "u") + " ON " + Factory.getDBO().quoteName("u.id") + " = " + Factory.getDBO().quoteName("h.user")); + query.where(Factory.getDBO().quoteName("p.iscommand") + " = " + Factory.getDBO().quote(0.ToString())); + query.where(Factory.getDBO().quoteName("h.time") + " >= DATE_SUB(NOW(),INTERVAL 7 DAY)"); + query.group(Factory.getDBO().quoteName("h.user")); + query.order(Factory.getDBO().quoteName("num")); + query.limit(0, limit); + + Factory.getDBO().setQuery(query); + + List rows = Factory.getDBO().getResultList(); + List> ret = new List>(); + if (rows == null) + { + return ret; + } + foreach (object[] row in rows) + { + ret.Add(new Tuple((string)row[1], (long)row[0])); + } + return ret; + } + + public static int GetCoffeine() + { + DBQuery query = Factory.getDBO().getQuery(true); + query.select("SUM(ROUND(POW(0.5,TIMESTAMPDIFF(SECOND," + Factory.getDBO().quoteName("h.time") + ",NOW())/4/60/60)*" + Factory.getDBO().quoteName("p.caffeine") + ")) AS " + Factory.getDBO().quoteName("coffeine_act")); + query.from(Factory.getDBO().quoteName("history", "h")); + query.leftJoin(Factory.getDBO().quoteName("product", "p") + " ON " + Factory.getDBO().quoteName("p.id") + " = " + Factory.getDBO().quoteName("h.prod")); + query.where(Factory.getDBO().quoteName("h.time") + " >= DATE_SUB(NOW(),INTERVAL 7 DAY)"); + Factory.getDBO().setQuery(query); + + object[] row = Factory.getDBO().getResult(); + if (row == null) + { + Console.WriteLine(Factory.getDBO().getError()); + return 0; + } + return (int)(double)row[0]; + } + + public static int GetAvgCoffeine() + { + DBQuery query = Factory.getDBO().getQuery(true); + query.select("SUM(ROUND(POW(0.5,TIMESTAMPDIFF(SECOND," + Factory.getDBO().quoteName("h.time") + ",NOW())/4/60/60)*" + Factory.getDBO().quoteName("p.caffeine") + ")) AS " + Factory.getDBO().quoteName("coffeine_act")); + query.select("SUM(if(ROUND(POW(0.5,TIMESTAMPDIFF(SECOND," + Factory.getDBO().quoteName("h.time") + ",NOW())/4/60/60)*" + Factory.getDBO().quoteName("p.caffeine") + ")=0,0,1)) AS " + Factory.getDBO().quoteName("num")); + query.from(Factory.getDBO().quoteName("history", "h")); + query.leftJoin(Factory.getDBO().quoteName("product", "p") + " ON " + Factory.getDBO().quoteName("p.id") + " = " + Factory.getDBO().quoteName("h.prod")); + query.where(Factory.getDBO().quoteName("h.time") + " >= DATE_SUB(NOW(),INTERVAL 7 DAY)"); + Factory.getDBO().setQuery(query); + + object[] row = Factory.getDBO().getResult(); + if (row == null) + { + Console.WriteLine(Factory.getDBO().getError()); + return 0; + } + return (int)((double)row[0]/ double.Parse(((decimal)row[1]).ToString())); + } + + public static void Add(int product_id, int user_id) + { + TableHistory h = new TableHistory(); + Dictionary k = new Dictionary(); + k.Add("prod",product_id); + k.Add("user",user_id); + k.Add("time",DateTime.Now); + h.bind(k); + h.store(); + } + } +} diff --git a/Matomat/Model/Product.cs b/Matomat/Model/Product.cs new file mode 100644 index 0000000..3c6d2e3 --- /dev/null +++ b/Matomat/Model/Product.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Matomat.Database.Tables; + +namespace Matomat.Model +{ + class Product + { + public enum ProdType + { + Product, + Instruction + } + private long barcode; + private TableProduct table; + + public Product(long barcode) + { + this.barcode = barcode; + if (this.init() == false) + { + throw new ArgumentException("Produkt wurde nich gefunden\nEAN13: " + barcode + " unbekannt"); + } + } + + private bool init() + { + this.table = new TableProduct(); + Dictionary load = new Dictionary(); + load.Add("barcode",barcode.ToString()); + this.table.load(load); + return (this.table.barcode == this.barcode); + } + + + public long GetBarcode() + { + return this.table.barcode; + } + + public double GetPrice() + { + return ((double)this.table.cost)/100; + } + + public string GetName() + { + return this.table.name; + } + + public ProdType GetProdType() + { + return (this.table.iscommand) ? ProdType.Instruction : ProdType.Product; + } + + internal int GetId() + { + return this.table.id; + } + + public static void Add(long barcode, string name, int preis, int coffeine) + { + TableProduct p = new TableProduct(); + Dictionary k = new Dictionary(); + k.Add("barcode", barcode); + k.Add("name", name); + k.Add("cost", preis); + k.Add("caffeine", coffeine); + p.bind(k); + p.store(); + } + } +} diff --git a/Matomat/Model/User.cs b/Matomat/Model/User.cs new file mode 100644 index 0000000..66c41f5 --- /dev/null +++ b/Matomat/Model/User.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Matomat.Database.Tables; +using Matomat.Database.Query; +using Matomat.Helper; + +namespace Matomat.Model +{ + class User + { + private long userid; + private TableUser table; + + public User(long userid) + { + this.userid = userid; + if (this.init() == false) + { + throw new ArgumentException("User wurde nich gefunden\nUserId: " + userid + " unbekannt"); + } + } + + private bool init() + { + this.table = new TableUser(); + Dictionary load = new Dictionary(); + load.Add("barcode", userid.ToString()); + this.table.load(load); + return (this.table.userid == this.userid); + } + + public string GetShortname() + { + return this.table.shortname; + } + + public double GetCredits() + { + return ((double)this.table.credits) / 100; + } + + public long GetUserId() + { + return this.table.userid; + } + + public double GetUserAll() + { + DBQuery query = Factory.getDBO().getQuery(); + query.select("SUM(" + Factory.getDBO().quoteName("p.cost") + ") AS " + Factory.getDBO().quoteName("num")); + query.from(Factory.getDBO().quoteName("history", "h")); + query.leftJoin(Factory.getDBO().quoteName("product", "p") + " ON " + Factory.getDBO().quoteName("p.id") + " = " + Factory.getDBO().quoteName("h.prod")); + query.where(Factory.getDBO().quoteName("p.iscommand") + " = " + Factory.getDBO().quote(0.ToString())); + query.where(Factory.getDBO().quoteName("h.user") + " = " + Factory.getDBO().quote(this.table.id.ToString())); + + Factory.getDBO().setQuery(query); + + object[] row = Factory.getDBO().getResult(); + if (row == null) + { + Console.WriteLine(Factory.getDBO().getError()); + return 0.0; + } + return ((double)row[0])/100; + } + + public bool GetAdmin() + { + return this.table.admin; + } + + public void SetUserKonto(double p) + { + p = (int)(p * 100); + Dictionary k = new Dictionary(); + k.Add("credits", this.table.credits + p); + this.table.bind(k); + this.table.store(); + } + + public static void Add(long userid, string username, bool isadmin, string kurzel, string email) + { + TableUser u = new TableUser(); + Dictionary k = new Dictionary(); + k.Add("userid", userid); + k.Add("username", username); + k.Add("admin", isadmin); + k.Add("shortname", kurzel); + k.Add("email", email); + u.bind(k); + u.store(); + } + + public int GetId() + { + return this.table.id; + } + + public static void Remove(int id) + { + TableUser u = new TableUser(); + Dictionary load = new Dictionary(); + load.Add("id", id.ToString()); + u.load(load); + u.delete(); + } + } +} diff --git a/Matomat/Output/LCDDisplay.cs b/Matomat/Output/LCDDisplay.cs index b1637bf..6b426c7 100644 --- a/Matomat/Output/LCDDisplay.cs +++ b/Matomat/Output/LCDDisplay.cs @@ -175,40 +175,7 @@ namespace Matomat.Output Warn, Error } - #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) + private void printb(string text, int time = 0, Status status = Status.OK) { string[] lines = text.Split('\n'); string line1 = ""; @@ -236,30 +203,28 @@ namespace Matomat.Output line2 = line2.PadLeft((int)Math.Floor(ll2) + line2.Length, ' '); line2 = line2.PadRight((int)Math.Ceiling(ll2) + line2.Length, ' '); - this.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + - "²" + line1 + "²" + - "²" + line2 + "²" + - "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", status, time); + this.anzeige("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + + "²" + 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) + + /// + /// Prints a text on the display with the minimum duration of the time and beebcodes when status is not Status.OK + /// if the text is >= than 160 Chars it will display a border. + /// + /// Text to display. Break with \n, max length 38 chars each line + /// Minimum duration of displaying the text + /// Status code for that Item + public void print(string text, int time = 0, Status status = Status.OK) { + if (text.Length <= 80) + { + this.printb(text, time, status); + return; + } this.anzeige(text, status, time); } - #endregion private void anzeige(string text, Status status, int time) { Message m = new Message(text, status, time); diff --git a/Matomat/Worker.cs b/Matomat/Worker.cs index a31ea8a..384af84 100644 --- a/Matomat/Worker.cs +++ b/Matomat/Worker.cs @@ -12,28 +12,26 @@ namespace Matomat { private volatile bool _shouldStop; private Automat automat; - private LCDDisplay lcd; - private TInput inp; public Worker() { automat = new Automat(); automat.stopThread += new Automat.stopEvent(RequestStop); - lcd = Factory.getLCD(); - inp = Factory.getInput(); + Factory.getLCD(); + Factory.getInput(); _shouldStop = false; } public void DoWork() { while (!_shouldStop) { - automat.GetInitStatus(); - InputData input = inp.getAnyInput(60); + Instruction.GetInitStatus(); + InputData input = Factory.getInput().getAnyInput(60); if (input.type != InputData.types.None) { automat.doJob(input); } } - Factory.getLCD().printb("Der Matomat wurde beendet.\nEinen schönen Tag!"); + Factory.getLCD().print("Der Matomat wurde beendet.\nEinen schönen Tag!"); Factory.rmLCD(); Factory.rmInput(); Factory.rmDBO();