Weiter weiter...

This commit is contained in:
BlubbFish 2016-01-25 15:06:54 +00:00
parent 39b5e9c9d6
commit 1c8d49f9d9
5 changed files with 140 additions and 5 deletions

View File

@ -3,16 +3,25 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO.Ports; using System.IO.Ports;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using TimeKeeper.Models.Types;
namespace TimeKeeper.Models { namespace TimeKeeper.Models {
class Tray : OwnModel<Tray> { class Tray : OwnModel<Tray> {
private Boolean isConnectedValue; private Boolean isConnectedValue = false;
private TimeSpan OffsetTimeValue = new TimeSpan();
private SerialPort serial; private SerialPort serial;
private Thread serialConnectThread;
private Thread setTimeThread;
private InIReader settingsfile; private InIReader settingsfile;
private FileLogger sLogger;
private Stack<WorkMessages> MessagesValue = new Stack<WorkMessages>();
private Tray() { private Tray() {
this.sLogger = FileLogger.getInstance("serial.log", true);
this.init(); this.init();
} }
@ -21,14 +30,117 @@ namespace TimeKeeper.Models {
set { this.isConnectedValue = value; this.update(); } set { this.isConnectedValue = value; this.update(); }
} }
public TimeSpan OffsetTime {
get { return this.OffsetTimeValue; }
set { this.OffsetTimeValue = value; this.update(); }
}
public Stack<WorkMessages> Messages {
get { return this.MessagesValue; }
}
public void MessagesPush(WorkMessages m) {
this.MessagesValue.Push(m);
this.update();
}
override protected void init() { override protected void init() {
this.settingsfile = InIReader.getInstance("settings.ini"); this.settingsfile = InIReader.getInstance("settings.ini");
this.serial = new SerialPort(this.settingsfile.getValue("general", "comport")); this.serial = new SerialPort(this.settingsfile.getValue("general", "comport"));
try { this.serial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
this.serial.Open(); this.serialConnectThread = new Thread(connectRunner);
} catch(Exception) { this.serialConnectThread.Start();
this.isConnected = false; 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;
}
} }
} }

View File

@ -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;
}
}
}

View File

@ -53,6 +53,7 @@
<Compile Include="Controller\CWindow.cs" /> <Compile Include="Controller\CWindow.cs" />
<Compile Include="Models\MTray.cs" /> <Compile Include="Models\MTray.cs" />
<Compile Include="Models\MWindow.cs" /> <Compile Include="Models\MWindow.cs" />
<Compile Include="Models\Types\WorkMessages.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs"> <Compile Include="Properties\Resources.Designer.cs">

View File

@ -35,6 +35,7 @@ namespace TimeKeeper.View {
override public void Dispose() { override public void Dispose() {
this.trayi.Visible = false; this.trayi.Visible = false;
this.Model.Dispose();
Application.ExitThread(); Application.ExitThread();
} }