mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-01-28 20:04:56 +00:00
Compare commits
1 Commits
plugin-min
...
hotfix/fil
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83c76340fc |
@@ -9,270 +9,273 @@ using Crestron.SimplSharpPro.CrestronThread;
|
|||||||
|
|
||||||
namespace PepperDash.Essentials.Core
|
namespace PepperDash.Essentials.Core
|
||||||
{
|
{
|
||||||
public static class FileIO
|
public static class FileIO
|
||||||
{
|
{
|
||||||
|
|
||||||
static CCriticalSection fileLock = new CCriticalSection();
|
static CCriticalSection fileLock = new CCriticalSection();
|
||||||
public delegate void GotFileEventHandler(object sender, FileEventArgs e);
|
public delegate void GotFileEventHandler(object sender, FileEventArgs e);
|
||||||
public static event GotFileEventHandler GotFileEvent;
|
public static event GotFileEventHandler GotFileEvent;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the full file info from a path/filename, can include wildcards.
|
/// Get the full file info from a path/filename, can include wildcards.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fileName"></param>
|
/// <param name="fileName"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static FileInfo[] GetFiles(string fileName)
|
public static FileInfo[] GetFiles(string fileName)
|
||||||
{
|
{
|
||||||
DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(fileName));
|
string fullFilePath = Global.FilePathPrefix + fileName;
|
||||||
var files = dirInfo.GetFiles(Path.GetFileName(fileName));
|
DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(fullFilePath));
|
||||||
Debug.Console(0, "FileIO found: {0}, {1}", files.Count(), fileName);
|
var files = dirInfo.GetFiles(Path.GetFileName(fullFilePath));
|
||||||
if (files.Count() > 0)
|
Debug.Console(0, "FileIO found: {0}, {1}", files.Count(), fullFilePath);
|
||||||
{
|
if (files.Count() > 0)
|
||||||
return files;
|
{
|
||||||
}
|
return files;
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
return null;
|
{
|
||||||
}
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static FileInfo GetFile(string fileName)
|
public static FileInfo GetFile(string fileName)
|
||||||
{
|
{
|
||||||
DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(fileName));
|
string fullFilePath = Global.FilePathPrefix + fileName;
|
||||||
var files = dirInfo.GetFiles(Path.GetFileName(fileName));
|
DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(fullFilePath));
|
||||||
Debug.Console(0, "FileIO found: {0}, {1}", files.Count(), fileName);
|
var files = dirInfo.GetFiles(Path.GetFileName(fullFilePath));
|
||||||
if (files.Count() > 0)
|
Debug.Console(0, "FileIO found: {0}, {1}", files.Count(), fullFilePath);
|
||||||
{
|
if (files.Count() > 0)
|
||||||
return files.FirstOrDefault();
|
{
|
||||||
}
|
return files.FirstOrDefault();
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
return null;
|
{
|
||||||
}
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the data from string path/filename
|
/// Get the data from string path/filename
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fileName"></param>
|
/// <param name="fileName"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string ReadDataFromFile(string fileName)
|
public static string ReadDataFromFile(string fileName)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return ReadDataFromFile(GetFile(fileName));
|
return ReadDataFromFile(GetFile(fileName));
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the data with fileInfo object
|
/// Get the data with fileInfo object
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fileName"></param>
|
/// <param name="fileName"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string ReadDataFromFile(FileInfo file)
|
public static string ReadDataFromFile(FileInfo file)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (fileLock.TryEnter())
|
if (fileLock.TryEnter())
|
||||||
{
|
{
|
||||||
DirectoryInfo dirInfo = new DirectoryInfo(file.Name);
|
DirectoryInfo dirInfo = new DirectoryInfo(file.DirectoryName);
|
||||||
Debug.Console(2, "FileIO Getting Data {0}", file.FullName);
|
Debug.Console(2, "FileIO Getting Data {0}", file.FullName);
|
||||||
|
|
||||||
if (File.Exists(file.FullName))
|
if (File.Exists(file.FullName))
|
||||||
{
|
{
|
||||||
using (StreamReader r = new StreamReader(file.FullName))
|
using (StreamReader r = new StreamReader(file.FullName))
|
||||||
{
|
{
|
||||||
return r.ReadToEnd();
|
return r.ReadToEnd();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Console(2, "File {0} does not exsist", file.FullName);
|
Debug.Console(2, "File {0} does not exsist", file.FullName);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock");
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock");
|
||||||
return "";
|
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();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
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)
|
public static void ReadDataFromFileASync(string fileName)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ReadDataFromFileASync(GetFile(fileName));
|
ReadDataFromFileASync(GetFile(fileName));
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ReadDataFromFileASync(FileInfo file)
|
public static void ReadDataFromFileASync(FileInfo file)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
CrestronInvoke.BeginInvoke(o => _ReadDataFromFileASync(file));
|
CrestronInvoke.BeginInvoke(o => _ReadDataFromFileASync(file));
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void _ReadDataFromFileASync(FileInfo file)
|
private static void _ReadDataFromFileASync(FileInfo file)
|
||||||
{
|
{
|
||||||
string data;
|
string data;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (fileLock.TryEnter())
|
if (fileLock.TryEnter())
|
||||||
{
|
{
|
||||||
DirectoryInfo dirInfo = new DirectoryInfo(file.Name);
|
DirectoryInfo dirInfo = new DirectoryInfo(file.Name);
|
||||||
Debug.Console(2, "FileIO Getting Data {0}", file.FullName);
|
Debug.Console(2, "FileIO Getting Data {0}", file.FullName);
|
||||||
|
|
||||||
|
|
||||||
if (File.Exists(file.FullName))
|
if (File.Exists(file.FullName))
|
||||||
{
|
{
|
||||||
using (StreamReader r = new StreamReader(file.FullName))
|
using (StreamReader r = new StreamReader(file.FullName))
|
||||||
{
|
{
|
||||||
data = r.ReadToEnd();
|
data = r.ReadToEnd();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Console(2, "File {0} Does not exsist", file.FullName);
|
Debug.Console(2, "File {0} Does not exsist", file.FullName);
|
||||||
data = "";
|
data = "";
|
||||||
}
|
}
|
||||||
GotFileEvent.Invoke(null, new FileEventArgs(data));
|
GotFileEvent.Invoke(null, new FileEventArgs(data));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock");
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
||||||
data = "";
|
data = "";
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (fileLock != null && !fileLock.Disposed)
|
if (fileLock != null && !fileLock.Disposed)
|
||||||
fileLock.Leave();
|
fileLock.Leave();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data"></param>
|
/// <param name="data"></param>
|
||||||
/// <param name="filePath"></param>
|
/// <param name="filePath"></param>
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data"></param>
|
/// <param name="data"></param>
|
||||||
/// <param name="filePath"></param>
|
/// <param name="filePath"></param>
|
||||||
public static void WriteDataToFile(string data, string filePath)
|
public static void WriteDataToFile(string data, string filePath)
|
||||||
{
|
{
|
||||||
Thread _WriteFileThread;
|
Thread _WriteFileThread;
|
||||||
_WriteFileThread = new Thread((O) => _WriteFileMethod(data, filePath), null, Thread.eThreadStartOptions.CreateSuspended);
|
_WriteFileThread = new Thread((O) => _WriteFileMethod(data, Global.FilePathPrefix + "/" + filePath), null, Thread.eThreadStartOptions.CreateSuspended);
|
||||||
_WriteFileThread.Priority = Thread.eThreadPriority.LowestPriority;
|
_WriteFileThread.Priority = Thread.eThreadPriority.LowestPriority;
|
||||||
_WriteFileThread.Start();
|
_WriteFileThread.Start();
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Notice, "New WriteFile Thread");
|
Debug.Console(0, Debug.ErrorLogLevel.Notice, "New WriteFile Thread");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static object _WriteFileMethod(string data, string filePath)
|
static object _WriteFileMethod(string data, string filePath)
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to write file: '{0}'", filePath);
|
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to write file: '{0}'", filePath);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (fileLock.TryEnter())
|
if (fileLock.TryEnter())
|
||||||
{
|
{
|
||||||
using (StreamWriter sw = new StreamWriter(filePath))
|
|
||||||
{
|
|
||||||
sw.Write(data);
|
|
||||||
sw.Flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
using (StreamWriter sw = new StreamWriter(filePath))
|
||||||
else
|
{
|
||||||
{
|
sw.Write(data);
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock");
|
sw.Flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
else
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO write failed: \r{0}", e);
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock");
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
if (fileLock != null && !fileLock.Disposed)
|
|
||||||
fileLock.Leave();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
|
||||||
/// <summary>
|
}
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static bool FileIoUnitTest()
|
|
||||||
{
|
|
||||||
var testData = "Testing FileIO";
|
|
||||||
FileIO.WriteDataToFile(testData, "\\user\\FileIOTest.pdt");
|
|
||||||
|
|
||||||
var file = FileIO.GetFile("\\user\\*FileIOTest*");
|
/// <summary>
|
||||||
|
///
|
||||||
var readData = FileIO.ReadDataFromFile(file);
|
/// </summary>
|
||||||
Debug.Console(0, "Returned {0}", readData);
|
/// <returns></returns>
|
||||||
File.Delete(file.FullName);
|
public static bool FileIoUnitTest()
|
||||||
if (testData == readData)
|
{
|
||||||
{
|
var testData = "Testing FileIO";
|
||||||
return true;
|
FileIO.WriteDataToFile(testData, "\\user\\FileIOTest.pdt");
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
var file = FileIO.GetFile("\\user\\*FileIOTest*");
|
||||||
public class FileEventArgs
|
|
||||||
{
|
|
||||||
public FileEventArgs(string data) { Data = data; }
|
|
||||||
public string Data { get; private set; } // readonly
|
|
||||||
|
|
||||||
}
|
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
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user