NWEventReader.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net.Sockets;
  9. using System.Threading;
  10. using UnityEngine;
  11. using Logger;
  12. namespace Networkreader
  13. {
  14. public class NWEventReader : AbstractNWReader, INWReader
  15. {//int SID, double timeS, (double timeE,) 3 x float Pos, (3 x float IPos,) int type
  16. #region Parameter
  17. private readonly int ROWLENGTHINBYTESTYPE1 = sizeof(Int32) + sizeof(Double) + sizeof(Single) + sizeof(Single) + sizeof(Single) +
  18. sizeof(Int32);
  19. private readonly int ROWLENGTHINBYTESTYPE2 = sizeof(Int32) + sizeof(Double) + sizeof(Double) + sizeof(Single) + sizeof(Single) +
  20. sizeof(Single) + sizeof(Int32);
  21. private readonly int ROWLENGTHINBYTESTYPE3 = sizeof(Int32) + sizeof(Double) + sizeof(Single) + sizeof(Single) + sizeof(Single) +
  22. sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Int32);
  23. private readonly int ROWLENGTHINBYTESTYPE4 = sizeof(Int32) + sizeof(Double) + sizeof(Double) + sizeof(Single) + sizeof(Single) +
  24. sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Single) + sizeof(Int32);
  25. private StreamWriter writer;
  26. #endregion
  27. #region Constructor
  28. public NWEventReader(string ipAdress, int port) : base(ipAdress, port)
  29. {
  30. }
  31. #endregion
  32. #region Exposed
  33. public double GetNewestTimestamp()
  34. {
  35. throw new NotImplementedException();
  36. }
  37. public double GetOldestTimestamp()
  38. {
  39. throw new NotImplementedException();
  40. }
  41. #endregion
  42. #region Internal
  43. protected override void WriteToFileThread(object filepath)
  44. {
  45. Streaming = true;
  46. NWStream = Client.GetStream();
  47. Stopwatch watch = new Stopwatch();
  48. byte[] buffer;
  49. writer = new StreamWriter(new FileStream(filepath.ToString(), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite));
  50. watch.Start();
  51. while (Streaming)
  52. {
  53. try
  54. {
  55. if (NWStream.DataAvailable)
  56. {
  57. buffer = new byte[1];
  58. ReadBuffer(buffer);
  59. byte type = buffer[0];
  60. int thisrowlength = 0;
  61. switch (type)
  62. {
  63. case 0:
  64. thisrowlength = ROWLENGTHINBYTESTYPE1;
  65. break;
  66. case 1:
  67. thisrowlength = ROWLENGTHINBYTESTYPE2;
  68. break;
  69. case 2:
  70. thisrowlength = ROWLENGTHINBYTESTYPE3;
  71. break;
  72. case 3:
  73. thisrowlength = ROWLENGTHINBYTESTYPE4;
  74. break;
  75. default:
  76. thisrowlength = 0;
  77. CityLogger.LogError("Invalid Event-Length-Type received in NWEventReader.");
  78. break;
  79. }
  80. buffer = new byte[thisrowlength];
  81. ReadBuffer(buffer);
  82. Write(buffer, type);
  83. }
  84. else
  85. Thread.Sleep(10);
  86. }
  87. catch (Exception e)
  88. {
  89. CityLogger.LogError(e.Message);
  90. return;
  91. }
  92. }
  93. Client.Close();
  94. }
  95. public void Write(byte[] data, byte lengthtype)
  96. {
  97. String line = "";
  98. line += BitConverter.ToInt32(data, 0);
  99. line += ";";
  100. line += BitConverter.ToDouble(data, 4);
  101. line += ";";
  102. switch (lengthtype)
  103. {
  104. case 0://Length of 6
  105. line += BitConverter.ToSingle(data, 12);
  106. line += ";";
  107. line += BitConverter.ToSingle(data, 16);
  108. line += ";";
  109. line += BitConverter.ToSingle(data, 20);
  110. line += ";";
  111. line += BitConverter.ToInt32(data, 24);
  112. break;
  113. case 1://Length of 7
  114. line += BitConverter.ToDouble(data, 12);
  115. line += ";";
  116. line += BitConverter.ToSingle(data, 20);
  117. line += ";";
  118. line += BitConverter.ToSingle(data, 24);
  119. line += ";";
  120. line += BitConverter.ToSingle(data, 28);
  121. line += ";";
  122. line += BitConverter.ToInt32(data, 32);
  123. break;
  124. case 2://Length of 9
  125. line += BitConverter.ToSingle(data, 12);
  126. line += ";";
  127. line += BitConverter.ToSingle(data, 16);
  128. line += ";";
  129. line += BitConverter.ToSingle(data, 20);
  130. line += ";";
  131. line += BitConverter.ToSingle(data, 24);
  132. line += ";";
  133. line += BitConverter.ToSingle(data, 28);
  134. line += ";";
  135. line += BitConverter.ToSingle(data, 32);
  136. line += ";";
  137. line += BitConverter.ToInt32(data, 36);
  138. break;
  139. case 3://Length of 10
  140. line += BitConverter.ToDouble(data, 12);
  141. line += ";";
  142. line += BitConverter.ToSingle(data, 20);
  143. line += ";";
  144. line += BitConverter.ToSingle(data, 24);
  145. line += ";";
  146. line += BitConverter.ToSingle(data, 28);
  147. line += ";";
  148. line += BitConverter.ToSingle(data, 32);
  149. line += ";";
  150. line += BitConverter.ToSingle(data, 36);
  151. line += ";";
  152. line += BitConverter.ToSingle(data, 40);
  153. line += ";";
  154. line += BitConverter.ToInt32(data, 44);
  155. break;
  156. }
  157. line += "\n";
  158. writer.Write(line);
  159. writer.Flush();
  160. }
  161. protected override string GetFilename()
  162. {
  163. return "EventStream_" + DateTime.Now.ToString(new CultureInfo("de-DE")).Replace(" ", "_").Replace(":", ".") + ".csv";
  164. }
  165. #endregion
  166. }
  167. }