diff --git a/Matomat/Automat.cs b/Matomat/Automat.cs index efd9c08..98bfbad 100644 --- a/Matomat/Automat.cs +++ b/Matomat/Automat.cs @@ -8,6 +8,7 @@ namespace Matomat class Automat { private User user; + private bool shutdown = false; internal void doJob(int userId, long codeId) { user = findUser(userId); @@ -20,5 +21,10 @@ namespace Matomat { return new User(userId); } + + internal bool die() + { + return shutdown; + } } } diff --git a/Matomat/BarcodeReader.cs b/Matomat/BarcodeReader.cs index ba8d4ae..f3a75cd 100644 --- a/Matomat/BarcodeReader.cs +++ b/Matomat/BarcodeReader.cs @@ -9,6 +9,7 @@ namespace Matomat { internal long getCodeID() { + Console.ReadKey(); return 1234567890123; } } diff --git a/Matomat/Factory.cs b/Matomat/Factory.cs new file mode 100644 index 0000000..fff9b37 --- /dev/null +++ b/Matomat/Factory.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Matomat +{ + class Factory + { + static LCDDisplay lcd_i = null; + internal static LCDDisplay getLCDDisplay() + { + if (lcd_i == null) + lcd_i = Factory._createLCDDisplay(); + return lcd_i; + } + + private static LCDDisplay _createLCDDisplay() + { + LCDDisplay lcd = LCDDisplay.getInstance("lpd1"); + return lcd; + } + } +} diff --git a/Matomat/Helper.cs b/Matomat/Helper.cs new file mode 100644 index 0000000..563f893 --- /dev/null +++ b/Matomat/Helper.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; +using System.IO; + +namespace Matomat +{ + class Helper + { + internal static double Serialize(string text) + { + Stream stream = new MemoryStream(255); + IFormatter formatter = new BinaryFormatter(); + formatter.Serialize(stream, text); + stream.Position = 0; + byte[] bytes = new byte[(int)stream.Length]; + stream.Read(bytes, 0, (int)stream.Length); + return BitConverter.ToDouble(bytes, 0); + } + } +} diff --git a/Matomat/LCDDisplay.cs b/Matomat/LCDDisplay.cs index 4e9a566..bae7fc4 100644 --- a/Matomat/LCDDisplay.cs +++ b/Matomat/LCDDisplay.cs @@ -2,19 +2,123 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; + namespace Matomat { class LCDDisplay { - internal static void print(string text) + private class instance { - throw new NotImplementedException(); + public LCDDisplay lCDDisplay; + public double signature; + + public instance(LCDDisplay lCDDisplay, double signature) + { + this.lCDDisplay = lCDDisplay; + this.signature = signature; + } + } + private class Message + { + public string text; + public Status status; + public int time; + + public Message(string text, Status status, int time) + { + this.text = text; + this.status = status; + this.time = time; + } + } + static List instances; + private string port; + private Queue messages; + private volatile bool _shouldStop; + + public LCDDisplay(string port) + { + this.port = port; + messages = new Queue(); + Thread workerThread = new Thread(this.DoWork); + workerThread.Start(); + while (!workerThread.IsAlive); + } + public void DoWork() + { + Message m; + DateTime timeout = new DateTime(); + while (!_shouldStop) + { + Thread.Sleep(1); + if (messages.Count != 0) + { + if (timeout.Ticks < DateTime.Now.Ticks) + { + m = messages.Dequeue(); + timeout = DateTime.Now.AddSeconds(m.time); + display(m.text, m.status); + } + } + + } } - internal static void print(string text, int time) + private void display(string text, Status status) { - throw new NotImplementedException(); + Console.WriteLine(text); + } + public enum Status + { + OK, + Warn, + Error + } + internal void print(string text) + { + this.anzeige(text, Status.OK, 0); + } + + private void anzeige(string text, Status status, int time) + { + Message m = new Message(text, status, time); + messages.Enqueue(m); + } + + internal void print(string text, int time) + { + this.anzeige(text, Status.OK, time); + } + + internal void print(string text, Status status) + { + this.anzeige(text, status, 0); + } + + internal void print(string text, Status status, int time) + { + this.anzeige(text, status, time); + } + + internal static LCDDisplay getInstance(string port) + { + if (instances == null) + instances = new List(); + double signature = Helper.Serialize(port); + foreach (instance i in instances) + { + if (i.signature == signature) + return i.lCDDisplay; + } + instance instance = new instance(new LCDDisplay(port),signature); + instances.Add(instance); + return instance.lCDDisplay; + } + public void RequestStop() + { + _shouldStop = true; } } } diff --git a/Matomat/Matomat.csproj b/Matomat/Matomat.csproj index ccd3589..3b7b34e 100644 --- a/Matomat/Matomat.csproj +++ b/Matomat/Matomat.csproj @@ -45,6 +45,8 @@ + + diff --git a/Matomat/RFIDReader.cs b/Matomat/RFIDReader.cs index b41de0a..a6deb60 100644 --- a/Matomat/RFIDReader.cs +++ b/Matomat/RFIDReader.cs @@ -9,6 +9,7 @@ namespace Matomat { internal int getCardID() { + Console.ReadKey(); return 0x1234; } } diff --git a/Matomat/User.cs b/Matomat/User.cs index 3a1ed7c..29b6755 100644 --- a/Matomat/User.cs +++ b/Matomat/User.cs @@ -25,11 +25,13 @@ namespace Matomat internal bool vaild() { + LCDDisplay lcd = Factory.getLCDDisplay(); if (!found) { - LCDDisplay.print("User wurde nich gefunden",5); + lcd.print("User wurde nich gefunden",LCDDisplay.Status.Error,5); return false; } + return true; } } diff --git a/Matomat/Worker.cs b/Matomat/Worker.cs index d0c6297..afd57c1 100644 --- a/Matomat/Worker.cs +++ b/Matomat/Worker.cs @@ -11,22 +11,25 @@ namespace Matomat private RFIDReader rfid; private BarcodeReader barcode; private Automat automat; + private LCDDisplay lcd; public Worker() { rfid = new RFIDReader(); barcode = new BarcodeReader(); automat = new Automat(); + lcd = Factory.getLCDDisplay(); } public void DoWork() { while (!_shouldStop) { - LCDDisplay.print("Bitte Sudierendenausweis über den RFID Leser halten."); + lcd.print("Bitte Sudierendenausweis über den RFID Leser halten."); int userId = rfid.getCardID(); - LCDDisplay.print("Bitte Produkt / Code über den Barcodeleser halten."); + lcd.print("Bitte Produkt / Code über den Barcodeleser halten."); long codeId = barcode.getCodeID(); automat.doJob(userId, codeId); - + if (automat.die()) + RequestStop(); } Console.WriteLine("worker thread: terminating gracefully."); }