using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Net.Sockets; using System.Threading; using UnityEngine; using Logger; namespace Networkreader { public class NWEventReader : AbstractNWReader, INWReader {//int SID, double timeS, (double timeE,) 3 x float Pos, (3 x float IPos,) int type #region Parameter private readonly int ROWLENGTHINBYTESTYPE1 = sizeof(Int32) + sizeof(Double) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Int32); private readonly int ROWLENGTHINBYTESTYPE2 = sizeof(Int32) + sizeof(Double) + sizeof(Double) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Int32); private readonly int ROWLENGTHINBYTESTYPE3 = sizeof(Int32) + sizeof(Double) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Int32); private readonly int ROWLENGTHINBYTESTYPE4 = sizeof(Int32) + sizeof(Double) + sizeof(Double) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Int32); private StreamWriter writer; #endregion #region Constructor public NWEventReader(string ipAdress, int port) : base(ipAdress, port) { } #endregion #region Exposed public double GetNewestTimestamp() { throw new NotImplementedException(); } public double GetOldestTimestamp() { throw new NotImplementedException(); } #endregion #region Internal protected override void WriteToFileThread(object filepath) { Streaming = true; NWStream = Client.GetStream(); Stopwatch watch = new Stopwatch(); byte[] buffer; writer = new StreamWriter(new FileStream(filepath.ToString(), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)); watch.Start(); while (Streaming) { try { if (NWStream.DataAvailable) { buffer = new byte[1]; ReadBuffer(buffer); byte type = buffer[0]; int thisrowlength = 0; switch (type) { case 0: thisrowlength = ROWLENGTHINBYTESTYPE1; break; case 1: thisrowlength = ROWLENGTHINBYTESTYPE2; break; case 2: thisrowlength = ROWLENGTHINBYTESTYPE3; break; case 3: thisrowlength = ROWLENGTHINBYTESTYPE4; break; default: thisrowlength = 0; CityLogger.LogError("Invalid Event-Length-Type received in NWEventReader."); break; } buffer = new byte[thisrowlength]; ReadBuffer(buffer); Write(buffer, type); } else Thread.Sleep(10); } catch (Exception e) { CityLogger.LogError(e.Message); return; } } Client.Close(); } public void Write(byte[] data, byte lengthtype) { String line = ""; line += BitConverter.ToInt32(data, 0); line += ";"; line += BitConverter.ToDouble(data, 4); line += ";"; switch (lengthtype) { case 0://Length of 6 line += BitConverter.ToSingle(data, 12); line += ";"; line += BitConverter.ToSingle(data, 16); line += ";"; line += BitConverter.ToSingle(data, 20); line += ";"; line += BitConverter.ToInt32(data, 24); break; case 1://Length of 7 line += BitConverter.ToDouble(data, 12); line += ";"; line += BitConverter.ToSingle(data, 20); line += ";"; line += BitConverter.ToSingle(data, 24); line += ";"; line += BitConverter.ToSingle(data, 28); line += ";"; line += BitConverter.ToInt32(data, 32); break; case 2://Length of 9 line += BitConverter.ToSingle(data, 12); line += ";"; line += BitConverter.ToSingle(data, 16); line += ";"; line += BitConverter.ToSingle(data, 20); line += ";"; line += BitConverter.ToSingle(data, 24); line += ";"; line += BitConverter.ToSingle(data, 28); line += ";"; line += BitConverter.ToSingle(data, 32); line += ";"; line += BitConverter.ToInt32(data, 36); break; case 3://Length of 10 line += BitConverter.ToDouble(data, 12); line += ";"; line += BitConverter.ToSingle(data, 20); line += ";"; line += BitConverter.ToSingle(data, 24); line += ";"; line += BitConverter.ToSingle(data, 28); line += ";"; line += BitConverter.ToSingle(data, 32); line += ";"; line += BitConverter.ToSingle(data, 36); line += ";"; line += BitConverter.ToSingle(data, 40); line += ";"; line += BitConverter.ToInt32(data, 44); break; } line += "\n"; writer.Write(line); writer.Flush(); } protected override string GetFilename() { return "EventStream_" + DateTime.Now.ToString(new CultureInfo("de-DE")).Replace(" ", "_").Replace(":", ".") + ".csv"; } #endregion } }