Fix a ton of bugs, Some new Features like Aufladen and Exit

This commit is contained in:
BlubbFish 2012-05-04 19:41:50 +00:00
parent f46f43dce2
commit cdec43f718
17 changed files with 395 additions and 132 deletions

View File

@ -7,36 +7,43 @@ namespace Matomat
{
class Automat
{
private User user;
private Prod prod;
private bool shutdown = false;
private LCDDisplay lcd;
private Input inp;
public delegate void stopEvent(bool stop);
public event stopEvent stopThread;
public Automat() {
lcd = Factory.getLCDDisplay();
inp = Factory.getInput();
}
internal void doJob(InputData input)
{
LCDDisplay lcd = Factory.getLCDDisplay();
Input inp = Factory.getInput();
if (input.type == InputData.types.Card)
{
user = this.findUser(input.id);
User user = new User(input.id);
if (!user.vaild())
return;
this.showUserInfo(user);
lcd.print("Bitte Produkt / Code über den Barcodeleser halten.");
lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
"² Bitte Produkt über den ²" +
"² Barcodeleser halten ²" +
"²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²");
InputData prodid = inp.getProdInput(20);
if (prodid.type == InputData.types.None)
return;
prod = this.findProd(prodid.id);
Prod prod = new Prod(prodid.id);
if(!prod.vaild())
return;
this.showProdInfo(prod);
if (prod.GetProdType() == Prod.ProdType.product)
{
this.sell(prod, user);
@ -46,31 +53,71 @@ namespace Matomat
switch (prod.GetFunctName())
{
case "exit();": this.stopThread(true); break;
case "aufladen(10);": this.InsAufladen(10); break;
case "aufladen(20);": this.InsAufladen(20); break;
case "aufladen(5);": this.InsAufladen(5); break;
case "addUser();": this.InsAddUser(); break;
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 "showStats();": this.InsShowStats(); break;
case "showStats();": this.InsShowStats(); break;*/
}
}
}
else if (input.type == InputData.types.Prod)
{
prod = this.findProd(input.id);
Prod prod = new Prod(input.id);
if (!prod.vaild())
return;
if (prod.GetFunctName().ToLower() == "exit();")
{
this.stopThread(true);
return;
}
this.showProdInfo(prod);
}
}
private void InsAufladen(int betrag, User 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);
System.Threading.Thread.Sleep(4500);
return;
}
lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
"² Bitte die Zielkarte auf den ²" +
"² RFID Leser auflegen ²" +
"²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²");
InputData target = inp.getCardInput(20);
if (target.type == InputData.types.None)
return;
User user = new User(target.id);
if (!user.vaild())
return;
lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
"² Betrag von User " + user.GetUserName().PadRight(8, ' ') + " ²" +
"² um "+betrag.ToString().PadLeft(2, ' ')+" € verringert ²" +
"²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²");
user.SetUserKonto(user.GetUserKonto()-betrag);
System.Threading.Thread.Sleep(2000);
}
private void sell(Prod prod, User user)
{
LCDDisplay lcd = Factory.getLCDDisplay();
if (user.GetUserKonto() > 19)
{
this.lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
"² Du hast zu viel Geld auf dem Konto ²" +
"² BITTE bezahlen! | Grenze 20€ ²" +
"²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²",
@ -80,12 +127,13 @@ namespace Matomat
private void showProdInfo(Prod prod)
{
LCDDisplay lcd = Factory.getLCDDisplay();
if (prod.GetProdType() == Prod.ProdType.product)
{
long id = prod.GetProdId();
double price = prod.GetProdPrice();
string name = prod.GetProdName();
this.lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
"² Produkt: " + name.PadRight(27, ' ') + " ²" +
"² Preis: " + price.ToString(2).PadLeft(6, ' ') + "€ EAN13: " + id.ToString().PadRight(14, ' ') + " ²" +
"²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", 2);
@ -93,41 +141,40 @@ namespace Matomat
if (prod.GetProdType() == Prod.ProdType.instruction)
{
string name = prod.GetFunctName();
this.lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
"² Instruktion: " + name.PadRight(23, ' ') + " ²" +
"² AdminCard + Instruction + TargetCard ²" +
"²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", 2);
}
}
private Prod findProd(long prodid)
{
return new Prod(prodid);
}
private void showUserInfo(User user)
{
LCDDisplay lcd = Factory.getLCDDisplay();
string name = user.GetUserName();
int konto = user.GetUserKonto();
long id = user.GetUserId();
int all = user.GetUserAll();
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);
}
private User findUser(long userId)
{
return new User(userId);
}
internal void GetInitStatus()
{
lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²abcdef 321" +
"² MATOMAT Wilkommen! ²ghijkl 123" +
"² Frohes genießen der Mate ²mnopqr 066" +
"²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²stuvwx 001");
LCDDisplay lcd = Factory.getLCDDisplay();
List<TBest> 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') : " ";
}
lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + p[0] +
"² MATOMAT Wilkommen! ²" + p[1] +
"² Frohes genießen der Mate ²" + p[2] +
"²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" + p[3]);
}
}
}

View File

@ -8,22 +8,36 @@ namespace Matomat
{
class BarcodeReader
{
private TextReader r;
private bool _shouldStop;
internal long getCodeID()
{
r = Console.In;
string str = r.ReadLine();
if (Console.KeyAvailable)
{
string str = "";
this._shouldStop = false;
while (!this._shouldStop)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo k = Console.ReadKey(true);
if (k.Key == ConsoleKey.Enter)
break;
str = str + k.KeyChar.ToString();
}
System.Threading.Thread.Sleep(10);
}
try
{
return Convert.ToInt64(str);
}
catch (Exception) { }
}
return 0;
}
internal void Abort()
{
r.Close();
this._shouldStop = true;
}
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Threading;
namespace Matomat
{
@ -14,6 +15,7 @@ namespace Matomat
private bool data_b;
private MySqlDataReader data;
private long id;
private Thread pingThread;
public DBDriverMysqli()
{
}
@ -31,8 +33,23 @@ namespace Matomat
}
this.query("SET NAMES 'UTF8'");
this.query("SET CHARACTER SET 'UTF8'");
this.startPing();
return true;
}
private void startPing()
{
this.pingThread = new Thread(this.ping);
this.pingThread.Start();
}
public void ping()
{
while (true)
{
Thread.Sleep(1 * 60 * 1000);
this.query("SELECT 1");
}
}
public override void query(string sql)
{
this.err = null;
@ -208,8 +225,9 @@ namespace Matomat
{
return str;
}
~DBDriverMysqli()
public override void RequestStop()
{
this.pingThread.Abort();
if (!this.data.IsClosed)
this.data.Close();
this.conn.Close();

View File

@ -5,7 +5,7 @@ using System.Text;
namespace Matomat
{
class Database
abstract class Database
{
private class instance
{
@ -40,36 +40,13 @@ namespace Matomat
instances.Add(instance);
return instance.iDatabase;
}
public virtual bool connect(string server, string dbs, string user, string pw, int port, string driver)
{
return false;
}
public virtual void query(string sql)
{
return;
}
public virtual object[] getResult()
{
return null;
}
public virtual List<object[]> getResultList()
{
return null;
}
public virtual Exception getError()
{
return null;
}
public virtual long getID()
{
return 0;
}
public virtual string quote(string str)
{
return str;
}
~Database()
{
}
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<object[]> getResultList();
public abstract Exception getError();
public abstract long getID();
public abstract string quote(string str);
public abstract void RequestStop();
}
}

View File

@ -57,5 +57,23 @@ namespace Matomat
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()
{
if (lcd_i != null)
{
lcd_i.RequestStop();
lcd_i = null;
}
}
}
}

View File

@ -128,5 +128,23 @@ namespace Matomat
instances.Add(instance);
return instance.input;
}
internal InputData getCardInput(int timeout)
{
if (timeout == 0)
timeout = Int32.MaxValue;
data = new InputData();
Thread wtCard = new Thread(this.doWorkCard);
wtCard.Start();
while (!wtCard.IsAlive) ;
DateTime time = DateTime.Now.AddSeconds(timeout);
while (time.Ticks > DateTime.Now.Ticks && data.type == InputData.types.None)
{
Thread.Sleep(1);
}
wtCard.Abort();
rfid.Abort();
return data;
}
}
}

View File

@ -34,8 +34,20 @@ namespace Matomat
this.time = time;
}
}
private class Beeb
{
public int frequency;
public int duration;
public Beeb(int frequency, int duration)
{
this.frequency = frequency;
this.duration = duration;
}
}
static List<instance> instances;
private Queue<Message> messages;
private Queue<Beeb> beeb;
private volatile bool _shouldStop;
private SerialPort serialPort;
private bool disable = false;
@ -52,12 +64,25 @@ namespace Matomat
this.disable = true;
}
this.createChars();
messages = new Queue<Message>();
Thread workerThread = new Thread(this.DoWork);
workerThread.Start();
while (!workerThread.IsAlive);
this.messages = new Queue<Message>();
new Thread(this.DisplayThread).Start();
this.beeb = new Queue<Beeb>();
new Thread(this.BeebThread).Start();
}
public void DoWork()
private void BeebThread()
{
Beeb b;
while (!_shouldStop)
{
Thread.Sleep(1);
if (this.beeb.Count != 0)
{
b = this.beeb.Dequeue();
Console.Beep(b.frequency, b.duration);
}
}
}
private void DisplayThread()
{
Message m;
DateTime timeout = new DateTime();
@ -76,20 +101,24 @@ namespace Matomat
}
}
private void beep(int frequency, int duration)
{
Beeb b = new Beeb(frequency, duration);
this.beeb.Enqueue(b);
}
private void display(string text, Status status)
{
if (status == Status.Warn)
{
Console.Beep(750, 750);
this.beep(750, 750);
}
if (status == Status.Error)
{
Console.Beep(750, 500);
Console.Beep(37, 100);
Console.Beep(750, 500);
Console.Beep(37, 100);
Console.Beep(750, 500);
this.beep(750, 500);
this.beep(37, 100);
this.beep(750, 500);
this.beep(37, 100);
this.beep(750, 500);
}
Console.WriteLine(text);
this.displayClear();
@ -194,6 +223,9 @@ namespace Matomat
}
public void RequestStop()
{
while (messages.Count != 0) { Thread.Sleep(10); }
while (beeb.Count != 0) { Thread.Sleep(10); }
Thread.Sleep(100);
_shouldStop = true;
if (this.serialPort.IsOpen)
this.serialPort.Close();

View File

@ -34,21 +34,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="mysql.data, Version=6.2.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>.\mysql.data.dll</HintPath>
</Reference>
<Reference Include="mysql.data.entity, Version=6.2.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>.\mysql.data.entity.dll</HintPath>
</Reference>
<Reference Include="mysql.visualstudio">
<HintPath>.\mysql.visualstudio.dll</HintPath>
</Reference>
<Reference Include="mysql.web, Version=6.2.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>.\mysql.web.dll</HintPath>
</Reference>
<Reference Include="MySql.Data, Version=6.2.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL" />
<Reference Include="MySql.Data.Entity, Version=6.2.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL" />
<Reference Include="RfidClass, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>.\RfidClass.dll</HintPath>
@ -75,6 +62,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RFIDReader.cs" />
<Compile Include="Tables\Tables.Best.cs" />
<Compile Include="Tables\Tables.cs" />
<Compile Include="Tables\Tables.User.cs" />
<Compile Include="User.cs" />

View File

@ -10,6 +10,9 @@ namespace Matomat
private long prodId;
private bool found;
private ProdType type;
private int price;
private string name;
private string function;
public enum ProdType
{
product,
@ -35,11 +38,14 @@ namespace Matomat
if (t.iscommand)
{
this.type = ProdType.instruction;
this.function = t.name;
}
else
{
this.type = ProdType.product;
this.name = t.name;
}
this.price = t.cost;
}
@ -70,17 +76,17 @@ namespace Matomat
internal double GetProdPrice()
{
return 1.0;
return this.price;
}
internal string GetProdName()
{
return "Mate-Cola";
return this.name;
}
internal string GetFunctName()
{
return "aufladen(10);";
return this.function;
}
}
}

View File

@ -19,9 +19,9 @@ namespace Matomat
while (workerThread.IsAlive)
{
Thread.Sleep(1);
Thread.Sleep(100);
}
Console.WriteLine("main thread: Worker thread has terminated.");
//Console.ReadLine();
}
}
}

View File

@ -18,6 +18,7 @@ namespace Matomat
try
{
return r.readKey();
//return 388914410;
}
catch (Exception)// e)
{

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Matomat
{
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;
}
}
}
}
}

View File

@ -7,6 +7,17 @@ namespace Matomat
{
class TProduct : Tables
{
public TProduct()
{
}
public TProduct(int id, long barcode, string name, int cost, bool iscommand)
{
this._id = id;
this._barcode = barcode;
this._name = name;
this._cost = cost;
this._iscommand = iscommand;
}
private int _id;
public int id
{

View File

@ -8,6 +8,19 @@ namespace Matomat
{
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
{

View File

@ -14,9 +14,13 @@ namespace Matomat
public TUser getUserTable(long userid)
{
Database db = Factory.getDBO();
db.query("SELECT * FROM `user` WHERE `userid` =" + db.quote(userid.ToString()));
db.query("SELECT `id`, `userid`, `username`, `credits`, `admin`, `shortname`, `email` FROM `user` WHERE `userid` =" + db.quote(userid.ToString()));
object[] row = db.getResult();
return new TUser();
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)
{
@ -31,13 +35,44 @@ namespace Matomat
{
return null;
}
TProduct ret = new TProduct();
ret.id = (int)row[0];
ret.barcode = (long)row[1];
ret.name = (string)row[2];
ret.cost = (int)row[3];
ret.iscommand = (bool)row[4];
return new TProduct((int)row[0], (long)row[1], (string)row[2], (int)row[3], (bool)row[4]);
}
internal List<TBest> getBestlist()
{
Database 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<object[]> rows = db.getResultList();
if (rows == null)
{
return null;
}
List<TBest> ret = new List<TBest>();
foreach (object[] row in rows)
{
ret.Add(new TBest((long)row[0], (string)row[1]));
}
return ret;
}
internal TBest getUserMax(int p)
{
Database db = Factory.getDBO();
db.query("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()));
object[] row = db.getResult();
if (row == null)
{
return null;
}
return new TBest(Decimal.ToInt64((decimal)row[0]), (string)row[1]);
}
}
}

View File

@ -9,6 +9,12 @@ namespace Matomat
{
private long userId;
private bool found;
private string username;
private string shrotname;
private int credits;
private long all;
private bool admin;
private int id;
public User(long userId)
{
@ -24,7 +30,13 @@ namespace Matomat
this.found = false;
return;
}
TBest b = new Tables().getUserMax(u.id);
this.found = true;
this.shrotname = u.shortname;
this.credits = u.credits;
this.all = b.num;
this.admin = u.admin;
this.id = u.id;
}
internal bool vaild()
@ -32,7 +44,10 @@ namespace Matomat
LCDDisplay lcd = Factory.getLCDDisplay();
if (!found)
{
lcd.print("User wurde nich gefunden",LCDDisplay.Status.Error,5);
lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
"² User wurde nicht gefunden ²" +
"² UserID: " + (this.GetUserId().ToString() + " unbekannt").PadRight(28, ' ') + " ²" +
"²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²", LCDDisplay.Status.Error, 5);
System.Threading.Thread.Sleep(4500);
return false;
}
@ -42,12 +57,12 @@ namespace Matomat
internal string GetUserName()
{
return "pschel2s";
return this.shrotname;
}
internal int GetUserKonto()
{
return 20;
return this.credits;
}
internal long GetUserId()
@ -55,9 +70,22 @@ namespace Matomat
return this.userId;
}
internal int GetUserAll()
internal long GetUserAll()
{
return 35;
return this.all;
}
internal bool IsAdmin()
{
return this.admin;
}
internal void SetUserKonto(int p)
{
Database db = Factory.getDBO();
this.credits = p;
db.query("UPDATE `user` SET `credits` = '" + db.quote(p.ToString())+"' WHERE "+
"`id` = '"+db.quote(this.id.ToString())+"'");
}
}
}

View File

@ -31,8 +31,12 @@ namespace Matomat
InputData input = inp.getAnyInput(0);
automat.doJob(input);
}
lcd.print("Matomat Beendet!");
lcd.RequestStop();
this.lcd.print("²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²" +
"² Der Matomat wurde beendet. ²" +
"² Einen schönen Tag! ²" +
"²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²");
Factory.rmLCDDisplay();
Factory.rmDBO();
}
private void RequestStop(bool stop)
{