diff --git a/TimeKeeper/Models/MTray.cs b/TimeKeeper/Models/MTray.cs index d1fef55..0f6dedb 100644 --- a/TimeKeeper/Models/MTray.cs +++ b/TimeKeeper/Models/MTray.cs @@ -3,16 +3,25 @@ using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; +using System.Threading; using System.Threading.Tasks; +using TimeKeeper.Models.Types; namespace TimeKeeper.Models { class Tray : OwnModel { - private Boolean isConnectedValue; + private Boolean isConnectedValue = false; + private TimeSpan OffsetTimeValue = new TimeSpan(); private SerialPort serial; + private Thread serialConnectThread; + private Thread setTimeThread; private InIReader settingsfile; + private FileLogger sLogger; + private Stack MessagesValue = new Stack(); private Tray() { + this.sLogger = FileLogger.getInstance("serial.log", true); this.init(); } @@ -21,14 +30,117 @@ namespace TimeKeeper.Models { set { this.isConnectedValue = value; this.update(); } } + public TimeSpan OffsetTime { + get { return this.OffsetTimeValue; } + set { this.OffsetTimeValue = value; this.update(); } + } + + public Stack Messages { + get { return this.MessagesValue; } + } + + public void MessagesPush(WorkMessages m) { + this.MessagesValue.Push(m); + this.update(); + } + override protected void init() { this.settingsfile = InIReader.getInstance("settings.ini"); this.serial = new SerialPort(this.settingsfile.getValue("general", "comport")); - try { - this.serial.Open(); - } catch(Exception) { - this.isConnected = false; + this.serial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); + this.serialConnectThread = new Thread(connectRunner); + this.serialConnectThread.Start(); + this.setTimeThread = new Thread(timeRunner); + this.setTimeThread.Start(); + } + + internal void Dispose() { + this.serialConnectThread.Abort(); + this.setTimeThread.Abort(); + if(this.serial.IsOpen) { + this.serial.Close(); } } + + private void timeRunner() { + Thread.Sleep(1000 * 10); + while(true) { + DateTime n = DateTime.UtcNow; + this.DataSendHandler("time=" + ((int)((n - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds)).ToString()); + Thread.Sleep(1000 * 60 * 5); + } + } + + private void connectRunner() { + while(true) { + if(!this.serial.IsOpen) { + try { + this.serial.Open(); + this.serial.DtrEnable = true; + this.isConnected = true; + this.serial.WriteLine(""); + } catch(Exception) { + this.isConnected = false; + } + } + Thread.Sleep(1000 * 60); //Check Every Minute + } + } + + private void DataReceivedHandler(Object sender, SerialDataReceivedEventArgs e) { + SerialPort sp = (SerialPort)sender; + string s = sp.ReadLine().Trim(); + sLogger.setLine("<-: " + s); + this.parseSerial(s); + } + + [MethodImpl(MethodImplOptions.Synchronized)] + private void DataSendHandler(String v) { + sLogger.setLine("->: " + v); + this.serial.WriteLine(v); + } + + private void parseSerial(String s) { + if(s == "requestKeep=1") { + this.DataSendHandler("keep=1"); + } else if(s == "Init...." || s == "Init loading!" || s == "Init finished!") { + //Ignore that Stuff + } else if((s.Length > 4 && s.Substring(0, 4) == "d->:") || (s.Length > 4 && s.Substring(0, 4) == "i<-:")) { + //Ignore that Stuff also.... + } else if(s.Length > 2 && s.Substring(0, 2) == "t=") { + this.setOffset(s.Split('=')[1]); + } else if(s.Length > 4 && s.Substring(0, 4) == "tag=") { + this.parseTag(s); + } else { + throw new NotImplementedException(); + } + } + + private void parseTag(String s) { + string[] parts = s.Split(';'); + long userID = 0; + DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + int jobID = 0; + int working = 0; + foreach(String part in parts) { + string[] t = part.Split('='); + if(t[0] == "tag") { + userID = long.Parse(t[1]); + } else if(t[0] == "time") { + time = time.AddSeconds(int.Parse(t[1])).ToLocalTime(); + } else if(t[0] == "job") { + jobID = int.Parse(t[1]); + } else if(t[0] == "online") { + working = int.Parse(t[1]); + } + } + this.MessagesPush(new WorkMessages(userID, time, jobID, working)); + } + + private void setOffset(String v) { + DateTime ctime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + ctime = ctime.AddSeconds(int.Parse(v)).ToLocalTime(); + this.OffsetTime = ctime - DateTime.Now; + } } } diff --git a/TimeKeeper/Models/Types/WorkMessages.cs b/TimeKeeper/Models/Types/WorkMessages.cs new file mode 100644 index 0000000..c529668 --- /dev/null +++ b/TimeKeeper/Models/Types/WorkMessages.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TimeKeeper.Models.Types { + class WorkMessages { + public Int32 jobID; + public DateTime time; + public Int64 userID; + public Int32 working; + + public WorkMessages(Int64 userID, DateTime time, Int32 jobID, Int32 working) { + this.userID = userID; + this.time = time; + this.jobID = jobID; + this.working = working; + } + } +} diff --git a/TimeKeeper/TimeKeeper.csproj b/TimeKeeper/TimeKeeper.csproj index 34c2649..97d3a5e 100644 --- a/TimeKeeper/TimeKeeper.csproj +++ b/TimeKeeper/TimeKeeper.csproj @@ -53,6 +53,7 @@ + diff --git a/TimeKeeper/View/VTray.cs b/TimeKeeper/View/VTray.cs index 5e236d4..233fc55 100644 --- a/TimeKeeper/View/VTray.cs +++ b/TimeKeeper/View/VTray.cs @@ -35,6 +35,7 @@ namespace TimeKeeper.View { override public void Dispose() { this.trayi.Visible = false; + this.Model.Dispose(); Application.ExitThread(); } diff --git a/TimeKeeper/bin/Release/TimeKeeper.exe b/TimeKeeper/bin/Release/TimeKeeper.exe index 8cf13ce..00c5717 100644 Binary files a/TimeKeeper/bin/Release/TimeKeeper.exe and b/TimeKeeper/bin/Release/TimeKeeper.exe differ