diff --git a/Matomat/Controller/Instruction.cs b/Matomat/Controller/Instruction.cs index dfed39a..0c65ffa 100644 --- a/Matomat/Controller/Instruction.cs +++ b/Matomat/Controller/Instruction.cs @@ -249,7 +249,6 @@ namespace Matomat "²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + p[0] + "² MATOMAT Wilkommen! ²" + p[1] + "² Frohes genießen der Mate ²" + p[2]); - Factory.getLCD().print("µ_¶·-¸¹ºµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµ"); } public static void showUserInfo(User user) @@ -303,6 +302,41 @@ namespace Matomat public static void InsShowStats(User user, bool lastMonth = true) { + List products = History.GetDrunkProducts(user, lastMonth); + foreach (Product item in products) + { + string output=""; + if (lastMonth) + { + output += " Dein Verbrauch im letzten Monat an "; + } + else + { + output += " Dein Verbrauch seit Anbeginn der Zeit "; + } + output += Factory.getLCD().CenterString("\""+item.GetName()+"\""); + Tuple sum = History.GetDrunkProductsSum(user, item, lastMonth); + output += Factory.getLCD().CenterString(sum.Item1 + " Flaschen mit " + sum.Item2.ToString("N2") + " g Koffein"); + output += " Verlauf: "; + List chart = History.GetDrunkProductsChart(user, item, lastMonth); + foreach (int value in chart) + { + switch (value) + { + case 0: output += "µ"; break; + case 1: output += "_"; break; + case 2: output += "¶"; break; + case 3: output += "·"; break; + case 4: output += "-"; break; + case 5: output += "¸"; break; + case 6: output += "¹"; break; + case 7: output += "º"; break; + default: output += " "; break; + } + } + Factory.getLCD().print(output, 5); + System.Threading.Thread.Sleep(4500); + } /** Alle Daten aus der DB Holen die aus dem zeitraum für den user da sind zu Produkt X * Lege liste mit 40 plätzen an * Teile Zeit auf die 40 Plätze auf, älteste bewegung = t0, neueste bewegung = t40 @@ -311,6 +345,16 @@ namespace Matomat * Rechne die prozentuale getränke pro platz * erstelle en Graphen */ + + /*1234567890123456789012345678901234567890 + * Verlauf: -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ + * Dein Verbrauch seit Anbeginn der Zeit + * Dein Verbrauch im letzten Monat an + * "Club-Mate" + * 1221 Flaschen mit 101,22 g Koffein + *_-----___--__-_--_----_----_-------___-- + * + */ } } } diff --git a/Matomat/Model/History.cs b/Matomat/Model/History.cs index 51d39b3..0afe532 100644 --- a/Matomat/Model/History.cs +++ b/Matomat/Model/History.cs @@ -90,5 +90,117 @@ namespace Matomat.Model h.bind(k); h.store(); } + + public static List GetDrunkProducts(User u, bool lastMonth) + { + DBQuery query = Factory.getDBO().getQuery(true); + query.select(Factory.getDBO().quoteName("p.barcode", "id")); + 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.user") + " = " + Factory.getDBO().quote(u.GetId().ToString())); + if (lastMonth) + { + query.where(Factory.getDBO().quoteName("h.time") + " >= DATE_SUB(NOW(),INTERVAL 30 DAY)"); + } + query.group(Factory.getDBO().quoteName("h.prod")); + + 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 Product((long)row[0])); + } + return ret; + } + + public static Tuple GetDrunkProductsSum(User u, Product item, bool lastMonth) + { + DBQuery query = Factory.getDBO().getQuery(true); + query.select("COUNT(*) AS " + Factory.getDBO().quoteName("boddles")); + query.select("SUM(" + Factory.getDBO().quoteName("p.caffeine") + ") AS " + Factory.getDBO().quoteName("caffeine")); + 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.user") + " = " + Factory.getDBO().quote(u.GetId().ToString())); + if (lastMonth) + { + query.where(Factory.getDBO().quoteName("h.time") + " >= DATE_SUB(NOW(),INTERVAL 30 DAY)"); + } + + Factory.getDBO().setQuery(query); + + object[] row = Factory.getDBO().getResult(); + if (row == null) + { + return new Tuple(0,0); + } + return new Tuple(int.Parse(row[0].ToString()), double.Parse(row[1].ToString()) / 1000); + } + + public static List GetDrunkProductsChart(User u, Product item, bool lastMonth, int days = 30) + { + DBQuery query = Factory.getDBO().getQuery(true); + query.select(Factory.getDBO().quoteName("h.prod", "product")); + query.select(Factory.getDBO().quoteName("h.time", "time")); + query.from(Factory.getDBO().quoteName("history", "h")); + query.order(Factory.getDBO().quoteName("h.time")+" ASC"); + query.where(Factory.getDBO().quoteName("h.user") + " = " + Factory.getDBO().quote(u.GetId().ToString())); + if (lastMonth) + { + query.where(Factory.getDBO().quoteName("h.time") + " >= DATE_SUB(NOW(),INTERVAL 30 DAY)"); + } + + Factory.getDBO().setQuery(query); + + List rows = Factory.getDBO().getResultList(); + List bottles = new List(); + List ret = new List(); + if (rows == null) + { + return ret; + } + foreach (object[] row in rows) + { + bottles.Add((DateTime)row[1]); + } + DateTime first = bottles.ElementAt(0); + DateTime last = DateTime.Now; + if (lastMonth) + { + first = last.Subtract(TimeSpan.FromDays(30)); + } + double step = (last - first).TotalSeconds / days; + int max = 0; + List tmplist = new List(); + for (int i = 0; i < days; i++) + { + inRange_f = first.AddSeconds(step * i); + inRange_t = first.AddSeconds((step * (i + 1)) - 1); + int v = bottles.FindAll(inRange).Count(); + tmplist.Add(v); + if (v > max) + max = v; + } + foreach (int point in tmplist) + { + ret.Add((int)Math.Round(7 * ((double)point / (double)max))); + } + return ret; + } + private static DateTime inRange_f = DateTime.Now; + private static DateTime inRange_t = DateTime.Now; + private static bool inRange(DateTime el) + { + if (el >= inRange_f && el < inRange_t) + { + return true; + } + return false; + } } } diff --git a/Matomat/Output/LCDDisplay.cs b/Matomat/Output/LCDDisplay.cs index b1ae011..c527221 100644 --- a/Matomat/Output/LCDDisplay.cs +++ b/Matomat/Output/LCDDisplay.cs @@ -165,9 +165,9 @@ namespace Matomat.Output this.displayEscCode("UDC1@@@@@@@_"); //[1, 0x81] Strich 1te von unten this.displayEscCode("UDC2@@@@@_@@"); //[2, 0x82] Strich 3te von unten this.displayEscCode("UDC3@@@@_@@@"); //[3, 0x83] Strich 4te von unten - this.displayEscCode("UDC4@@_@@@@@"); //[4, 0x84] Strich 3te von unten - this.displayEscCode("UDC5@_@@@@@@"); //[5, 0x05] Strich 3te von unten - this.displayEscCode("UDC6_@@@@@@@"); //[6, 0x06] Strich 3te von unten + this.displayEscCode("UDC4@@_@@@@@"); //[4, 0x84] Strich 6te von unten + this.displayEscCode("UDC5@_@@@@@@"); //[5, 0x05] Strich 7te von unten + this.displayEscCode("UDC6_@@@@@@@"); //[6, 0x06] Strich 8te von unten } private void displayEscCode(string code) @@ -218,7 +218,19 @@ namespace Matomat.Output "²" + line2 + "²" + "²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", status, time); } - + + public string CenterString(string text, int length = 40) + { + if (text.Length > length) + text = text.Substring(0, length); + double textlength = ((((double)length) - text.Length) / 2); + + text = text.PadLeft((int)Math.Floor(textlength) + text.Length, ' '); + text = text.PadRight((int)Math.Ceiling(textlength) + text.Length, ' '); + + return text; + } + /// /// 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. diff --git a/Matomat/Program.cs b/Matomat/Program.cs index 1fe6fe1..744eea0 100644 --- a/Matomat/Program.cs +++ b/Matomat/Program.cs @@ -17,6 +17,7 @@ namespace Matomat public static void Main(string[] args) { + test(); InitMainThread(); Thread t = new Thread(MainThread); t.Start(); @@ -27,6 +28,12 @@ namespace Matomat } } + private static void test() + { + User u = new User(2855543482); + Instruction.InsShowStats(u, true); + } + #region MainThread private static void InitMainThread() {