From 83c76340fc0cf184d3725f3f41ef0e9e5b23a1e2 Mon Sep 17 00:00:00 2001 From: jtalborough Date: Tue, 9 May 2023 09:08:28 -0400 Subject: [PATCH] fix: reapply fixes for fileIO 4 series --- .../PepperDashEssentialsBase/File/FileIO.cs | 473 +++++++++--------- 1 file changed, 238 insertions(+), 235 deletions(-) diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/File/FileIO.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/File/FileIO.cs index 51d64230..e91b2bfd 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/File/FileIO.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/File/FileIO.cs @@ -9,270 +9,273 @@ using Crestron.SimplSharpPro.CrestronThread; namespace PepperDash.Essentials.Core { - public static class FileIO - { + public static class FileIO + { - static CCriticalSection fileLock = new CCriticalSection(); - public delegate void GotFileEventHandler(object sender, FileEventArgs e); - public static event GotFileEventHandler GotFileEvent; + static CCriticalSection fileLock = new CCriticalSection(); + public delegate void GotFileEventHandler(object sender, FileEventArgs e); + public static event GotFileEventHandler GotFileEvent; - /// - /// Get the full file info from a path/filename, can include wildcards. - /// - /// - /// - public static FileInfo[] GetFiles(string fileName) - { - DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(fileName)); - var files = dirInfo.GetFiles(Path.GetFileName(fileName)); - Debug.Console(0, "FileIO found: {0}, {1}", files.Count(), fileName); - if (files.Count() > 0) - { - return files; - } - else - { - return null; - } - } + /// + /// Get the full file info from a path/filename, can include wildcards. + /// + /// + /// + public static FileInfo[] GetFiles(string fileName) + { + string fullFilePath = Global.FilePathPrefix + fileName; + DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(fullFilePath)); + var files = dirInfo.GetFiles(Path.GetFileName(fullFilePath)); + Debug.Console(0, "FileIO found: {0}, {1}", files.Count(), fullFilePath); + if (files.Count() > 0) + { + return files; + } + else + { + return null; + } + } - public static FileInfo GetFile(string fileName) - { - DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(fileName)); - var files = dirInfo.GetFiles(Path.GetFileName(fileName)); - Debug.Console(0, "FileIO found: {0}, {1}", files.Count(), fileName); - if (files.Count() > 0) - { - return files.FirstOrDefault(); - } - else - { - return null; - } - } + public static FileInfo GetFile(string fileName) + { + string fullFilePath = Global.FilePathPrefix + fileName; + DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(fullFilePath)); + var files = dirInfo.GetFiles(Path.GetFileName(fullFilePath)); + Debug.Console(0, "FileIO found: {0}, {1}", files.Count(), fullFilePath); + if (files.Count() > 0) + { + return files.FirstOrDefault(); + } + else + { + return null; + } + } - /// - /// Get the data from string path/filename - /// - /// - /// - public static string ReadDataFromFile(string fileName) - { - try - { - return ReadDataFromFile(GetFile(fileName)); - } - catch (Exception e) - { - Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); - return ""; - } - } + /// + /// Get the data from string path/filename + /// + /// + /// + public static string ReadDataFromFile(string fileName) + { + try + { + return ReadDataFromFile(GetFile(fileName)); + } + catch (Exception e) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); + return ""; + } + } - /// - /// Get the data with fileInfo object - /// - /// - /// - public static string ReadDataFromFile(FileInfo file) - { - try - { - if (fileLock.TryEnter()) - { - DirectoryInfo dirInfo = new DirectoryInfo(file.Name); - Debug.Console(2, "FileIO Getting Data {0}", file.FullName); + /// + /// Get the data with fileInfo object + /// + /// + /// + public static string ReadDataFromFile(FileInfo file) + { + try + { + if (fileLock.TryEnter()) + { + DirectoryInfo dirInfo = new DirectoryInfo(file.DirectoryName); + Debug.Console(2, "FileIO Getting Data {0}", file.FullName); - if (File.Exists(file.FullName)) - { - using (StreamReader r = new StreamReader(file.FullName)) - { - return r.ReadToEnd(); - } - } - else - { - Debug.Console(2, "File {0} does not exsist", file.FullName); - return ""; - } - } - else - { - Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock"); - return ""; - } - - } - catch (Exception e) - { - Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); - return ""; - } - finally - { - if (fileLock != null && !fileLock.Disposed) - fileLock.Leave(); + if (File.Exists(file.FullName)) + { + using (StreamReader r = new StreamReader(file.FullName)) + { + return r.ReadToEnd(); + } + } + else + { + Debug.Console(2, "File {0} does not exsist", file.FullName); + return ""; + } + } + else + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock"); + return ""; + } - } - } + } + catch (Exception e) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); + return ""; + } + finally + { + if (fileLock != null && !fileLock.Disposed) + fileLock.Leave(); + + } + } - public static void ReadDataFromFileASync(string fileName) - { - try - { - ReadDataFromFileASync(GetFile(fileName)); - } - catch (Exception e) - { - Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); - } - } + public static void ReadDataFromFileASync(string fileName) + { + try + { + ReadDataFromFileASync(GetFile(fileName)); + } + catch (Exception e) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); + } + } - public static void ReadDataFromFileASync(FileInfo file) - { - try - { - CrestronInvoke.BeginInvoke(o => _ReadDataFromFileASync(file)); - } - catch (Exception e) - { - Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); - } - } + public static void ReadDataFromFileASync(FileInfo file) + { + try + { + CrestronInvoke.BeginInvoke(o => _ReadDataFromFileASync(file)); + } + catch (Exception e) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); + } + } - private static void _ReadDataFromFileASync(FileInfo file) - { - string data; - try - { - if (fileLock.TryEnter()) - { - DirectoryInfo dirInfo = new DirectoryInfo(file.Name); - Debug.Console(2, "FileIO Getting Data {0}", file.FullName); + private static void _ReadDataFromFileASync(FileInfo file) + { + string data; + try + { + if (fileLock.TryEnter()) + { + DirectoryInfo dirInfo = new DirectoryInfo(file.Name); + Debug.Console(2, "FileIO Getting Data {0}", file.FullName); - if (File.Exists(file.FullName)) - { - using (StreamReader r = new StreamReader(file.FullName)) - { - data = r.ReadToEnd(); - } - } - else - { - Debug.Console(2, "File {0} Does not exsist", file.FullName); - data = ""; - } - GotFileEvent.Invoke(null, new FileEventArgs(data)); - } - else - { - Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock"); - } + if (File.Exists(file.FullName)) + { + using (StreamReader r = new StreamReader(file.FullName)) + { + data = r.ReadToEnd(); + } + } + else + { + Debug.Console(2, "File {0} Does not exsist", file.FullName); + data = ""; + } + GotFileEvent.Invoke(null, new FileEventArgs(data)); + } + else + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock"); + } - } - catch (Exception e) - { - Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); - data = ""; - } - finally - { - if (fileLock != null && !fileLock.Disposed) - fileLock.Leave(); + } + catch (Exception e) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); + data = ""; + } + finally + { + if (fileLock != null && !fileLock.Disposed) + fileLock.Leave(); - } + } - } + } - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public static void WriteDataToFile(string data, string filePath) - { - Thread _WriteFileThread; - _WriteFileThread = new Thread((O) => _WriteFileMethod(data, filePath), null, Thread.eThreadStartOptions.CreateSuspended); - _WriteFileThread.Priority = Thread.eThreadPriority.LowestPriority; - _WriteFileThread.Start(); - Debug.Console(0, Debug.ErrorLogLevel.Notice, "New WriteFile Thread"); + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static void WriteDataToFile(string data, string filePath) + { + Thread _WriteFileThread; + _WriteFileThread = new Thread((O) => _WriteFileMethod(data, Global.FilePathPrefix + "/" + filePath), null, Thread.eThreadStartOptions.CreateSuspended); + _WriteFileThread.Priority = Thread.eThreadPriority.LowestPriority; + _WriteFileThread.Start(); + Debug.Console(0, Debug.ErrorLogLevel.Notice, "New WriteFile Thread"); - } + } - static object _WriteFileMethod(string data, string filePath) - { - Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to write file: '{0}'", filePath); + static object _WriteFileMethod(string data, string filePath) + { + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to write file: '{0}'", filePath); - try - { - if (fileLock.TryEnter()) - { - using (StreamWriter sw = new StreamWriter(filePath)) - { - sw.Write(data); - sw.Flush(); - } + try + { + if (fileLock.TryEnter()) + { - } - else - { - Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock"); - } + using (StreamWriter sw = new StreamWriter(filePath)) + { + sw.Write(data); + sw.Flush(); + } - } - catch (Exception e) - { - Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO write failed: \r{0}", e); - } - finally - { - if (fileLock != null && !fileLock.Disposed) - fileLock.Leave(); + } + else + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock"); + } - } - return null; + } + catch (Exception e) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO write failed: \r{0}", e); + } + finally + { + if (fileLock != null && !fileLock.Disposed) + fileLock.Leave(); - } + } + return null; - /// - /// - /// - /// - public static bool FileIoUnitTest() - { - var testData = "Testing FileIO"; - FileIO.WriteDataToFile(testData, "\\user\\FileIOTest.pdt"); + } - var file = FileIO.GetFile("\\user\\*FileIOTest*"); - - var readData = FileIO.ReadDataFromFile(file); - Debug.Console(0, "Returned {0}", readData); - File.Delete(file.FullName); - if (testData == readData) - { - return true; - } - else - { - return false; - } - } + /// + /// + /// + /// + public static bool FileIoUnitTest() + { + var testData = "Testing FileIO"; + FileIO.WriteDataToFile(testData, "\\user\\FileIOTest.pdt"); - } - public class FileEventArgs - { - public FileEventArgs(string data) { Data = data; } - public string Data { get; private set; } // readonly + var file = FileIO.GetFile("\\user\\*FileIOTest*"); - } + var readData = FileIO.ReadDataFromFile(file); + Debug.Console(0, "Returned {0}", readData); + File.Delete(file.FullName); + if (testData == readData) + { + return true; + } + else + { + return false; + } + } + + } + public class FileEventArgs + { + public FileEventArgs(string data) { Data = data; } + public string Data { get; private set; } // readonly + + } } \ No newline at end of file