123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 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
- }
- }
|