using FluentFTP; using GSG.NET.Logging; using GSG.NET.ObjectBase; using GSG.NET.TCP; using GSG.NET.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace VehicleControlSystem.Managers { /// /// 아날로그 입력 정보를 종합하여 로그로 남기는 역할 /// FTP 를 이용하여 파일 전송 /// class PhysicalCheckupLogger : SingletonBase, IDisposable { Logger frontlogger = Logger.GetLogger( "PhysicalCheckup_Front" ); Logger rearlogger = Logger.GetLogger( "PhysicalCheckup_Rear" ); Logger logger = Logger.GetLogger(); TcpConnector h = new TcpConnector(); const byte CR = 0x0d; public string PLCAddress { get; set; } = "192.168.0.20"; public int PLCPort { get; set; } = 8501; public string PLCFolderName { get; set; } public string FTPFolderName { get; set; } private PhysicalCheckupLogger() { } public void Dispose() { this.h.CloseSocket(); } 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}" ); 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}" ); public async Task UploadPhysicalCheckupLogAsync() { var token = new CancellationToken(); using ( var ftp = new FtpClient( "192.168.127.188" ) ) { await ftp.ConnectAsync( token ); // upload a folder and all its files await ftp.UploadDirectoryAsync( @"C:\LOG\FTP\", @"/DriveLog", FtpFolderSyncMode.Update ); // upload a folder and all its files, and delete extra files on the server //ftp.UploadDirectory( @"C:\website\assets\", @"/public_html/assets", FtpFolderSyncMode.Mirror ); //ToDo: Delete Files } } public void DownloadPLCLog() { using ( var ftp = new FtpClient( "192.168.0.20", "KVID", "1234" ) ) { ftp.Connect(); // download a folder and all its files ftp.DownloadDirectory( @"C:\LOG\FTP\", @"/0_CARD/log0/", FtpFolderSyncMode.Update ); // download a folder and all its files, and delete extra files on disk //await ftp.DownloadDirectoryAsync( @"C:\website\dailybackup\", @"/public_html/", FtpFolderSyncMode.Mirror ); //ftp.DeleteFile( "/full/or/relative/path/to/file" ); } } public void FTPServerDeleteFile() { using ( var ftp = new FtpClient( "192.168.0.20", "KV", "1234" ) ) { ftp.Connect(); ftp.DeleteDirectory( "/0_CARD/log0/" ); } } public void Connecte() { h.Connect( new TcpComm { Active = true, Ip = "192.168.0.20", PortNo = 8501, } ); } /// /// 주행 시작 시 진단 PLC Bit On /// /// public bool SetPLCStartDrive() { try { if ( !h.Connected ) { h.Connect( new TcpComm { Active = true, Ip = "192.168.0.20", PortNo = 8501, } ); } if ( !h.Connected ) return false; var mb = new MemoryBuffer(); mb.AppendAscii( "WR MR100 1" ); mb.Append( CR ); this.h.WriteFlush( mb.ToBytes ); //h.CloseSocket(); } catch ( Exception e ) { h.CloseSocket(); logger.E( $"[PLC] - Set Value Connection Error {e}" ); return false; } return true; } /// /// 주행 정지 시 진단 PLC Bit Off /// /// public bool ResetPLCStartDrive() { try { if ( !h.Connected ) { h.Connect( new TcpComm { Active = true, Ip = "192.168.0.20", PortNo = 8501, } ); } if ( !h.Connected ) return false; var mb = new MemoryBuffer(); mb.AppendAscii( "WR MR100 0" ); mb.Append( CR ); this.h.WriteFlush( mb.ToBytes ); //h.CloseSocket(); } catch ( Exception e ) { h.CloseSocket(); logger.E( $"[PLC] - Reset Value Connection Error {e}" ); return false; } return true; } } }