Created
July 20, 2020 17:22
-
-
Save hasokeric/d2d233da36534d9234ac30ecf3fbb224 to your computer and use it in GitHub Desktop.
file watcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Initialize the File Trigger Watcher which will monitor the fullFilePath | |
* directory and when the BPM writes a new file it will read the file and | |
* based on the contents of the file we can do certain actions think of it as a | |
* bridge to have the BPM Communicate with our C# Code | |
* | |
* @type Custom Function | |
* @return void | |
*/ | |
private void InitializeFileTriggerWatcher() | |
{ | |
// Get Trigger File Path | |
string wtPath = this.GetWriteTriggerFilePath("IT", "WTPath"); | |
// Generate the Path Variable which will consist of the file path | |
// and the USER currently logged in this way we do not collide with others | |
// in case you had multiple Handheld devices. | |
string fullFilePath = System.IO.Path.Combine(wtPath, "trigger-" + ((Ice.Core.Session)(this.oTrans.Session)).UserID.ToString() ); | |
// Delete the File if it exists for Cleanup | |
// Otherwise the Changed Event might occur twice | |
File.Delete(fullFilePath); | |
// Create Watcher | |
watcher = new FileSystemWatcher(); | |
watcher.Path = Path.GetDirectoryName(fullFilePath); | |
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime; | |
watcher.Filter = Path.GetFileName(fullFilePath); | |
watcher.Changed += new FileSystemEventHandler(NewFileTriggerEvent); | |
watcher.EnableRaisingEvents = true; | |
} | |
/** | |
* Destroy the File Watcher and Dispose | |
* | |
* @type Custom Function | |
* @return void | |
*/ | |
private void DestroyFileTriggerWatcher() | |
{ | |
watcher.Changed -= new FileSystemEventHandler(NewFileTriggerEvent); | |
watcher.Dispose(); | |
} | |
/** | |
* When the FileWatcher event detects a new file then this function | |
* will be triggered to Handle the Event | |
* | |
* @var source object | |
* @var e Event | |
* @type Custom Function | |
* @return void | |
*/ | |
private void NewFileTriggerEvent(object source, FileSystemEventArgs e) | |
{ | |
if (File.Exists(e.FullPath.ToString()) == false) | |
{ | |
return; | |
} | |
// Initialize variables | |
string date = string.Empty; | |
string eventKey = string.Empty; | |
string company = string.Empty; | |
string user = string.Empty; | |
// There should never be a problem because if we are in this | |
// Event that means the file is there But just an extra precaution step | |
try { | |
if (File.Exists(e.FullPath.ToString()) == false) | |
{ | |
return; | |
} | |
// Read the file and get the values | |
string[] values = File.ReadAllText(e.FullPath.ToString()).Split(','); | |
date = values[0]; | |
eventKey = values[1]; | |
company = values[2]; | |
user = values[3]; | |
} | |
catch { | |
//playSound("ERROR"); // must let the user know that his process has been stopped | |
//MessageBox.Show("There was an issue with Reading Trigger File. This could mean there is another instance of HH01 running Packout."); | |
// | |
return; | |
} | |
// Do Something Special with the Event | |
switch (eventKey) | |
{ | |
case "PACKOUT_EXCEPTION": | |
playSound("ERROR"); | |
break; | |
case "LABEL_VALIDATION_IF_REQUIRED": | |
this.isLabelValidationApproved = true; | |
break; | |
} | |
File.Delete(e.FullPath.ToString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment