refactor: Decentralize data fetching and add axis initialization

Refactor data fetching architecture from centralized App state to
component-local data management for improved maintainability and
data freshness guarantees.

Changes:
- SettingsModal: Fetch config data on modal open
- RecipePanel: Fetch recipe list on panel open
- IOMonitorPage: Fetch IO list on page mount with real-time updates
- Remove unnecessary props drilling through component hierarchy
- Simplify App.tsx by removing centralized config/recipes state

New feature:
- Add InitializeModal for sequential axis initialization (X, Y, Z)
- Each axis initializes with 3-second staggered start
- Progress bar animation for each axis
- Auto-close on completion

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-25 01:35:32 +09:00
parent 27cc2507cf
commit 362263ab05
10 changed files with 631 additions and 201 deletions

View File

@@ -2,6 +2,9 @@
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace HMIWeb
{
@@ -13,9 +16,83 @@ namespace HMIWeb
// Reference to the main form to update logic
private MainForm _host;
// Data folder paths
private readonly string _dataFolder;
private readonly string _settingsPath;
private readonly string _recipeFolder;
public MachineBridge(MainForm host)
{
_host = host;
// Initialize data folder paths
_dataFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
_settingsPath = Path.Combine(_dataFolder, "settings.json");
_recipeFolder = Path.Combine(_dataFolder, "recipe");
// Ensure folders exist
EnsureDataFoldersExist();
}
private void EnsureDataFoldersExist()
{
try
{
if (!Directory.Exists(_dataFolder))
Directory.CreateDirectory(_dataFolder);
if (!Directory.Exists(_recipeFolder))
Directory.CreateDirectory(_recipeFolder);
// Initialize default settings if not exists
if (!File.Exists(_settingsPath))
InitializeDefaultSettings();
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to create data folders: {ex.Message}");
}
}
private void InitializeDefaultSettings()
{
var defaultSettings = new List<object>
{
new { Key = "Site Name", Value = "Smart Factory A-1", Group = "System Information", Type = "String", Description = "The display name of the factory site." },
new { Key = "Line ID", Value = "L-2024-001", Group = "System Information", Type = "String", Description = "Unique identifier for this production line." },
new { Key = "API Endpoint", Value = "https://api.factory.local/v1", Group = "Network Configuration", Type = "String", Description = "Base URL for the backend API." },
new { Key = "Support Contact", Value = "010-1234-5678", Group = "System Information", Type = "String", Description = "Emergency contact number for maintenance." },
new { Key = "Debug Mode", Value = "false", Group = "System Information", Type = "Boolean", Description = "Enable detailed logging for debugging." },
new { Key = "Max Speed", Value = "1500", Group = "Motion Control", Type = "Number", Description = "Maximum velocity in mm/s." },
new { Key = "Acceleration", Value = "500", Group = "Motion Control", Type = "Number", Description = "Acceleration ramp in mm/s²." }
};
for (int i = 1; i <= 5; i++)
{
defaultSettings.Add(new
{
Key = $"Sensor_{i}_Threshold",
Value = (i * 10).ToString(),
Group = "Sensor Calibration",
Type = "Number",
Description = $"Trigger threshold for Sensor {i}."
});
}
for (int i = 1; i <= 3; i++)
{
defaultSettings.Add(new
{
Key = $"Safety_Zone_{i}",
Value = "true",
Group = "Safety Settings",
Type = "Boolean",
Description = $"Enable monitoring for Safety Zone {i}."
});
}
File.WriteAllText(_settingsPath, JsonConvert.SerializeObject(defaultSettings, Formatting.Indented));
Console.WriteLine("[INFO] Default settings.json created");
}
// --- Methods called from React (App.tsx) ---
@@ -45,18 +122,31 @@ namespace HMIWeb
{
Console.WriteLine($"[C#] Selecting Recipe: {recipeId}");
// In real app, load recipe settings here
// For now, just return success
try
{
// Simulate recipe loading logic
string recipePath = Path.Combine(_recipeFolder, $"{recipeId}.json");
if (!File.Exists(recipePath))
{
var response = new { success = false, message = "Recipe not found" };
return JsonConvert.SerializeObject(response);
}
// Load recipe data from file
string json = File.ReadAllText(recipePath);
var recipeData = JsonConvert.DeserializeObject<dynamic>(json);
// Set as current recipe
_host.SetCurrentRecipe(recipeId);
var response = new { success = true, message = "Recipe selected successfully", recipeId = recipeId };
return JsonConvert.SerializeObject(response);
Console.WriteLine($"[INFO] Recipe {recipeId} selected successfully");
var response2 = new { success = true, message = "Recipe selected successfully", recipeId = recipeId, recipe = recipeData };
return JsonConvert.SerializeObject(response2);
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to select recipe: {ex.Message}");
var response = new { success = false, message = ex.Message };
return JsonConvert.SerializeObject(response);
}
@@ -64,55 +154,48 @@ namespace HMIWeb
public string GetConfig()
{
// Generate 20 Mock Settings
var settings = new System.Collections.Generic.List<object>();
// Core Settings
settings.Add(new { Key = "Site Name", Value = "Smart Factory A-1", Group = "System Information", Type = "String", Description = "The display name of the factory site." });
settings.Add(new { Key = "Line ID", Value = "L-2024-001", Group = "System Information", Type = "String", Description = "Unique identifier for this production line." });
settings.Add(new { Key = "API Endpoint", Value = "https://api.factory.local/v1", Group = "Network Configuration", Type = "String", Description = "Base URL for the backend API." });
settings.Add(new { Key = "Support Contact", Value = "010-1234-5678", Group = "System Information", Type = "String", Description = "Emergency contact number for maintenance." });
settings.Add(new { Key = "Debug Mode", Value = "false", Group = "System Information", Type = "Boolean", Description = "Enable detailed logging for debugging." });
settings.Add(new { Key = "Max Speed", Value = "1500", Group = "Motion Control", Type = "Number", Description = "Maximum velocity in mm/s." });
settings.Add(new { Key = "Acceleration", Value = "500", Group = "Motion Control", Type = "Number", Description = "Acceleration ramp in mm/s²." });
// Generated Settings
for (int i = 1; i <= 5; i++)
try
{
settings.Add(new {
Key = $"Sensor_{i}_Threshold",
Value = (i * 10).ToString(),
Group = "Sensor Calibration",
Type = "Number",
Description = $"Trigger threshold for Sensor {i}."
});
}
for (int i = 1; i <= 3; i++)
{
settings.Add(new {
Key = $"Safety_Zone_{i}",
Value = "true",
Group = "Safety Settings",
Type = "Boolean",
Description = $"Enable monitoring for Safety Zone {i}."
});
}
if (!File.Exists(_settingsPath))
{
InitializeDefaultSettings();
}
Console.WriteLine("get config (20 items)");
return Newtonsoft.Json.JsonConvert.SerializeObject(settings);
string json = File.ReadAllText(_settingsPath);
Console.WriteLine($"[INFO] Loaded settings from {_settingsPath}");
return json;
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to load settings: {ex.Message}");
return "[]";
}
}
public void SaveConfig(string configJson)
{
Console.WriteLine($"[Backend] SAVE CONFIG REQUEST RECEIVED");
Console.WriteLine($"[Backend] Data: {configJson}");
// In a real app, we would save this to a file or database
try
{
Console.WriteLine($"[Backend] SAVE CONFIG REQUEST RECEIVED");
// Validate JSON format
var settings = JsonConvert.DeserializeObject(configJson);
// Save to file with formatting
File.WriteAllText(_settingsPath, JsonConvert.SerializeObject(settings, Formatting.Indented));
Console.WriteLine($"[INFO] Settings saved successfully to {_settingsPath}");
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to save settings: {ex.Message}");
}
}
public string GetIOList()
{
var ioList = new System.Collections.Generic.List<object>();
Console.WriteLine("GetIOList");
// Outputs (0-31)
for (int i = 0; i < 32; i++)
{
@@ -144,14 +227,98 @@ namespace HMIWeb
}
public string GetRecipeList()
{
var recipes = new System.Collections.Generic.List<object>();
recipes.Add(new { id = "1", name = "Wafer_Proc_300_Au", lastModified = "2023-10-25" });
recipes.Add(new { id = "2", name = "Wafer_Insp_200_Adv", lastModified = "2023-10-26" });
recipes.Add(new { id = "3", name = "Glass_Gen5_Bonding", lastModified = "2023-10-27" });
recipes.Add(new { id = "4", name = "Solar_Cell_Cut_A", lastModified = "2023-11-01" });
recipes.Add(new { id = "5", name = "LED_Mount_HighSpeed", lastModified = "2023-11-15" });
try
{
var recipes = new List<object>();
return Newtonsoft.Json.JsonConvert.SerializeObject(recipes);
if (!Directory.Exists(_recipeFolder))
{
Directory.CreateDirectory(_recipeFolder);
return JsonConvert.SerializeObject(recipes);
}
var jsonFiles = Directory.GetFiles(_recipeFolder, "*.json");
foreach (var filePath in jsonFiles)
{
try
{
string fileName = Path.GetFileNameWithoutExtension(filePath);
string json = File.ReadAllText(filePath);
var recipeData = JsonConvert.DeserializeObject<dynamic>(json);
var lastModified = File.GetLastWriteTime(filePath).ToString("yyyy-MM-dd");
recipes.Add(new
{
id = fileName,
name = recipeData?.name ?? fileName,
lastModified = lastModified
});
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to read recipe {filePath}: {ex.Message}");
}
}
Console.WriteLine($"[INFO] Loaded {recipes.Count} recipes from {_recipeFolder}");
return JsonConvert.SerializeObject(recipes);
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to get recipe list: {ex.Message}");
return "[]";
}
}
public string GetRecipe(string recipeId)
{
try
{
string recipePath = Path.Combine(_recipeFolder, $"{recipeId}.json");
if (!File.Exists(recipePath))
{
var response = new { success = false, message = "Recipe not found" };
return JsonConvert.SerializeObject(response);
}
string json = File.ReadAllText(recipePath);
Console.WriteLine($"[INFO] Loaded recipe {recipeId}");
return json;
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to get recipe {recipeId}: {ex.Message}");
var response = new { success = false, message = ex.Message };
return JsonConvert.SerializeObject(response);
}
}
public string SaveRecipe(string recipeId, string recipeData)
{
try
{
string recipePath = Path.Combine(_recipeFolder, $"{recipeId}.json");
// Validate JSON format
var recipe = JsonConvert.DeserializeObject(recipeData);
// Save to file with formatting
File.WriteAllText(recipePath, JsonConvert.SerializeObject(recipe, Formatting.Indented));
Console.WriteLine($"[INFO] Recipe {recipeId} saved successfully to {recipePath}");
var response = new { success = true, message = "Recipe saved successfully", recipeId = recipeId };
return JsonConvert.SerializeObject(response);
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to save recipe {recipeId}: {ex.Message}");
var response = new { success = false, message = ex.Message };
return JsonConvert.SerializeObject(response);
}
}
public string CopyRecipe(string recipeId, string newName)
@@ -160,12 +327,33 @@ namespace HMIWeb
try
{
// In real app, copy recipe data from database/file
// Generate new ID
string newId = System.Guid.NewGuid().ToString().Substring(0, 8);
string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd");
string sourcePath = Path.Combine(_recipeFolder, $"{recipeId}.json");
var response = new {
if (!File.Exists(sourcePath))
{
var response = new { success = false, message = "Source recipe not found" };
return JsonConvert.SerializeObject(response);
}
// Generate new ID
string newId = Guid.NewGuid().ToString().Substring(0, 8);
string destPath = Path.Combine(_recipeFolder, $"{newId}.json");
// Read source recipe
string json = File.ReadAllText(sourcePath);
var recipeData = JsonConvert.DeserializeObject<dynamic>(json);
// Update name in recipe data
recipeData.name = newName;
// Save to new file
File.WriteAllText(destPath, JsonConvert.SerializeObject(recipeData, Formatting.Indented));
string timestamp = DateTime.Now.ToString("yyyy-MM-dd");
Console.WriteLine($"[INFO] Recipe copied from {recipeId} to {newId}");
var response2 = new {
success = true,
message = "Recipe copied successfully",
newRecipe = new {
@@ -174,10 +362,11 @@ namespace HMIWeb
lastModified = timestamp
}
};
return JsonConvert.SerializeObject(response);
return JsonConvert.SerializeObject(response2);
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to copy recipe: {ex.Message}");
var response = new { success = false, message = ex.Message };
return JsonConvert.SerializeObject(response);
}
@@ -189,19 +378,32 @@ namespace HMIWeb
try
{
// In real app, delete recipe from database/file
// Check if recipe is in use
if (recipeId == _host.GetCurrentRecipe())
{
var response = new { success = false, message = "Cannot delete currently selected recipe" };
var response1 = new { success = false, message = "Cannot delete currently selected recipe" };
return JsonConvert.SerializeObject(response1);
}
string recipePath = Path.Combine(_recipeFolder, $"{recipeId}.json");
if (!File.Exists(recipePath))
{
var response = new { success = false, message = "Recipe not found" };
return JsonConvert.SerializeObject(response);
}
var response = new { success = true, message = "Recipe deleted successfully", recipeId = recipeId };
return JsonConvert.SerializeObject(response);
// Delete the file
File.Delete(recipePath);
Console.WriteLine($"[INFO] Recipe {recipeId} deleted successfully");
var response2 = new { success = true, message = "Recipe deleted successfully", recipeId = recipeId };
return JsonConvert.SerializeObject(response2);
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to delete recipe: {ex.Message}");
var response = new { success = false, message = ex.Message };
return JsonConvert.SerializeObject(response);
}