hot reload

This commit is contained in:
EugeneTes
2026-02-18 11:01:35 +01:00
parent 2bac7140a4
commit 14fe822962
9 changed files with 345 additions and 47 deletions

View File

@@ -17,11 +17,16 @@ namespace VisionBuilder.UI.Recipes.HawkeyeRecipe
{
private readonly IImageSource _imageSource;
private readonly HawkeyeRecognitionSettings _recognitionConfiguration;
private CancellationTokenSource _cts;
private Task _loopTask;
private FileSystemWatcher? _fileWatcher;
private System.Timers.Timer? _debounceTimer;
private string? _pendingHotReloadFile;
private string? _currentRecipeFilePath;
public HawkeyeRecognitionControl(IImageSource imageSource, HawkeyeRecognitionSettings recognitionConfiguration, ILoadingService loadingService): base(recognitionConfiguration,loadingService)
{
_imageSource = imageSource;
@@ -62,7 +67,10 @@ namespace VisionBuilder.UI.Recipes.HawkeyeRecipe
protected override void Initialize(RecipeData currentRecipe)
{
_workflow = WorkflowList.LoadJSONFromFile("..\\Data\\Recipes\\" + currentRecipe.RecipeName + ".jhrcp");
var path = "..\\Data\\Recipes\\" + currentRecipe.RecipeName + ".jhrcp";
_currentRecipeFilePath = Path.GetFullPath(path);
_workflow = WorkflowList.LoadJSONFromFile(path);
StartFileWatcher(_currentRecipeFilePath);
}
protected override void WarmUp()
@@ -75,7 +83,10 @@ namespace VisionBuilder.UI.Recipes.HawkeyeRecipe
protected override (Mat originalImage, Mat analysisImage,TimeSpan processingTime, TimeSpan acquisitionTime, string[] errorNames)? ProcessImage(CancellationToken token)
{
var pendingFile = Interlocked.Exchange(ref _pendingHotReloadFile, null);
if (pendingFile != null)
_workflow.HotReloadParameters(pendingFile);
var sw = Stopwatch.StartNew();
_workflow.Context.CancellationToken = token;
_workflow.ImageSource = _imageSource;
@@ -113,5 +124,52 @@ namespace VisionBuilder.UI.Recipes.HawkeyeRecipe
return (originalImage, analysisImage, sw.Elapsed, cameraTime, errorNames);
}
private void StartFileWatcher(string filePath)
{
StopFileWatcher();
var directory = Path.GetDirectoryName(filePath)!;
var fileName = Path.GetFileName(filePath);
_debounceTimer = new System.Timers.Timer(500) { AutoReset = false };
_debounceTimer.Elapsed += (_, _) => RaiseRecipeFileChanged();
_fileWatcher = new FileSystemWatcher(directory, fileName)
{
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size,
EnableRaisingEvents = true
};
_fileWatcher.Changed += (_, _) =>
{
_debounceTimer.Stop();
_debounceTimer.Start();
};
}
private void StopFileWatcher()
{
if (_fileWatcher != null)
{
_fileWatcher.EnableRaisingEvents = false;
_fileWatcher.Dispose();
_fileWatcher = null;
}
if (_debounceTimer != null)
{
_debounceTimer.Stop();
_debounceTimer.Dispose();
_debounceTimer = null;
}
}
public override void HotReload()
{
Interlocked.Exchange(ref _pendingHotReloadFile, _currentRecipeFilePath);
}
protected override void Cleanup()
{
StopFileWatcher();
}
}
}