PhysicalCheckupLogger.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using FluentFTP;
  2. using GSG.NET.Logging;
  3. using GSG.NET.ObjectBase;
  4. using GSG.NET.TCP;
  5. using GSG.NET.Utils;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace VehicleControlSystem.Managers
  13. {
  14. /// <summary>
  15. /// 아날로그 입력 정보를 종합하여 로그로 남기는 역할
  16. /// FTP 를 이용하여 파일 전송
  17. /// </summary>
  18. class PhysicalCheckupLogger : SingletonBase<PhysicalCheckupLogger>, IDisposable
  19. {
  20. Logger frontlogger = Logger.GetLogger( "PhysicalCheckup_Front" );
  21. Logger rearlogger = Logger.GetLogger( "PhysicalCheckup_Rear" );
  22. Logger logger = Logger.GetLogger();
  23. TcpConnector h = new TcpConnector();
  24. const byte CR = 0x0d;
  25. public string PLCAddress { get; set; } = "192.168.0.20";
  26. public int PLCPort { get; set; } = 8501;
  27. public string PLCFolderName { get; set; }
  28. public string FTPFolderName { get; set; }
  29. private PhysicalCheckupLogger()
  30. {
  31. }
  32. public void Dispose()
  33. {
  34. this.h.CloseSocket();
  35. }
  36. public void FrontWheelLogging( string speed, string torque, string RPM, string LoadFactor, string MCR ) => frontlogger.I( $"Speed : {speed} / Torque : {torque} / RPM : {RPM} / LoadFactor : {LoadFactor} / MCR : {MCR}" );
  37. public void RearWheelLoggging( string speed, string torque, string RPM, string LoadFactor, string MCR ) => rearlogger.I( $"Speed : {speed} / Torque : {torque} / RPM : {RPM} / LoadFactor : {LoadFactor} / MCR : {MCR}" );
  38. public async Task UploadPhysicalCheckupLogAsync()
  39. {
  40. var token = new CancellationToken();
  41. using ( var ftp = new FtpClient( "192.168.127.188" ) )
  42. {
  43. await ftp.ConnectAsync( token );
  44. // upload a folder and all its files
  45. await ftp.UploadDirectoryAsync( @"C:\LOG\FTP\", @"/DriveLog", FtpFolderSyncMode.Update );
  46. // upload a folder and all its files, and delete extra files on the server
  47. //ftp.UploadDirectory( @"C:\website\assets\", @"/public_html/assets", FtpFolderSyncMode.Mirror );
  48. //ToDo: Delete Files
  49. }
  50. }
  51. public void DownloadPLCLog()
  52. {
  53. using ( var ftp = new FtpClient( "192.168.0.20", "KVID", "1234" ) )
  54. {
  55. ftp.Connect();
  56. // download a folder and all its files
  57. ftp.DownloadDirectory( @"C:\LOG\FTP\", @"/0_CARD/log0/", FtpFolderSyncMode.Update );
  58. // download a folder and all its files, and delete extra files on disk
  59. //await ftp.DownloadDirectoryAsync( @"C:\website\dailybackup\", @"/public_html/", FtpFolderSyncMode.Mirror );
  60. //ftp.DeleteFile( "/full/or/relative/path/to/file" );
  61. }
  62. }
  63. public void FTPServerDeleteFile()
  64. {
  65. using ( var ftp = new FtpClient( "192.168.0.20", "KV", "1234" ) )
  66. {
  67. ftp.Connect();
  68. ftp.DeleteDirectory( "/0_CARD/log0/" );
  69. }
  70. }
  71. public void Connecte()
  72. {
  73. h.Connect( new TcpComm
  74. {
  75. Active = true,
  76. Ip = "192.168.0.20",
  77. PortNo = 8501,
  78. } );
  79. }
  80. /// <summary>
  81. /// 주행 시작 시 진단 PLC Bit On
  82. /// </summary>
  83. /// <returns></returns>
  84. public bool SetPLCStartDrive()
  85. {
  86. try
  87. {
  88. if ( !h.Connected )
  89. {
  90. h.Connect( new TcpComm
  91. {
  92. Active = true,
  93. Ip = "192.168.0.20",
  94. PortNo = 8501,
  95. } );
  96. }
  97. if ( !h.Connected )
  98. return false;
  99. var mb = new MemoryBuffer();
  100. mb.AppendAscii( "WR MR100 1" );
  101. mb.Append( CR );
  102. this.h.WriteFlush( mb.ToBytes );
  103. //h.CloseSocket();
  104. }
  105. catch ( Exception e )
  106. {
  107. h.CloseSocket();
  108. logger.E( $"[PLC] - Set Value Connection Error {e}" );
  109. return false;
  110. }
  111. return true;
  112. }
  113. /// <summary>
  114. /// 주행 정지 시 진단 PLC Bit Off
  115. /// </summary>
  116. /// <returns></returns>
  117. public bool ResetPLCStartDrive()
  118. {
  119. try
  120. {
  121. if ( !h.Connected )
  122. {
  123. h.Connect( new TcpComm
  124. {
  125. Active = true,
  126. Ip = "192.168.0.20",
  127. PortNo = 8501,
  128. } );
  129. }
  130. if ( !h.Connected )
  131. return false;
  132. var mb = new MemoryBuffer();
  133. mb.AppendAscii( "WR MR100 0" );
  134. mb.Append( CR );
  135. this.h.WriteFlush( mb.ToBytes );
  136. //h.CloseSocket();
  137. }
  138. catch ( Exception e )
  139. {
  140. h.CloseSocket();
  141. logger.E( $"[PLC] - Reset Value Connection Error {e}" );
  142. return false;
  143. }
  144. return true;
  145. }
  146. }
  147. }