using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Matomat { class InputData { public InputData() { this.id = 0; this.type = types.None; } public enum types { Card, Prod, None } public types type { get; set; } public long id { get; set; } } class Input { private class instance { public Input input; public double signature; public instance(Input input, double signature) { this.input = input; this.signature = signature; } } static List instances; private InputData data; private RFIDReader rfid; private BarcodeReader code; public Input() { rfid = new RFIDReader(); code = new BarcodeReader(); } private void doWorkCard() { while (true) { Thread.Sleep(1); long id = rfid.getCardID(); if (id != 0) { data.id = id; data.type = InputData.types.Card; } } } private void doWorkCode() { while (true) { Thread.Sleep(1); long id = code.getCodeID(); if (id != 0) { data.id = id; data.type = InputData.types.Prod; } } } internal InputData getAnyInput(int timeout) { if (timeout == 0) timeout = Int32.MaxValue; data = new InputData(); Thread wtCard = new Thread(this.doWorkCard); Thread wtCode = new Thread(this.doWorkCode); wtCard.Start(); wtCode.Start(); while (!wtCard.IsAlive) ; while (!wtCode.IsAlive) ; DateTime time = DateTime.Now.AddSeconds(timeout); while (time.Ticks > DateTime.Now.Ticks && data.type == InputData.types.None) { Thread.Sleep(1); } wtCard.Abort(); wtCode.Abort(); rfid.Abort(); code.Abort(); return data; } internal InputData getProdInput(int timeout) { if (timeout == 0) timeout = Int32.MaxValue; data = new InputData(); Thread wtCode = new Thread(this.doWorkCode); wtCode.Start(); while (!wtCode.IsAlive) ; DateTime time = DateTime.Now.AddSeconds(timeout); while (time.Ticks > DateTime.Now.Ticks && data.type == InputData.types.None) { Thread.Sleep(1); } wtCode.Abort(); code.Abort(); return data; } internal static Input getInstance() { if (instances == null) instances = new List(); double signature = 1.0; foreach (instance i in instances) { if (i.signature == signature) return i.input; } instance instance = new instance(new Input(), signature); 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; } } }