PEPTool/AV-ToolV3/Tracker.cs
2019-07-26 07:36:50 +02:00

82 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace PEP_Tool
{
class Tracker
{
static DispatcherTimer timer = new DispatcherTimer();
//public double time = 0;
static string client = "";
public static double time { get; set; }
private static int count = 0;
public static async void Init_Track()
{
client = await Crypto.EncryptString(Environment.MachineName);
time = await Reader.ReadTime(await Reader.ReadTimeJSON());
SaveTrack(true);
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
timer.Start();
}
private static void Timer_Tick(object sender, EventArgs e)
{
//var t = time.Item1;
time += 1;
count++;
if(count >= 30)
{
SaveTrack(true);
count = 0;
}
}
public static async Task SaveTrack(bool Online)
{
var save = await Reader.ReadTimeJSON();
//var save = new Dictionary<string, double>();
if (save == null)
save = new Dictionary<string, Tuple<double, bool, string>>();
if (save.ContainsKey(client))
save[client] = new Tuple<double, bool, string>(time, Online, Properties.Settings.Default.Version);
else save.Add(client, new Tuple<double, bool, string>(time, Online, Properties.Settings.Default.Version));
if (client == "") return;
try
{
var json = Newtonsoft.Json.JsonConvert.SerializeObject(save);
//json = await Crypto.EncryptString(json);
byte[] jsonB = new UTF8Encoding(true).GetBytes(json);
using (var fs = Writer.WaitForFile(@"\\bku.db.de\db\DB_006\GLW_11\ICE-Fertigung\M2\Personaleinsatzplanung\Log\Track.json", System.IO.FileMode.Open, System.IO.FileAccess.Write, System.IO.FileShare.Read))//Write | System.IO.FileShare.Delete
{
if (fs != null)
{
fs.SetLength(0);
await fs.WriteAsync(jsonB, 0, jsonB.Length);
}
else fs.Dispose();
}
}
catch (Exception ex)
{
LogFile.WriteLine(ex);
}
}
}
}