NamePlate fully in format in the yaml file
This commit is contained in:
parent
de3b6cd598
commit
a1dcf26b85
117
main.js
117
main.js
@ -1,4 +1,4 @@
|
||||
// main.js - CORRECTED
|
||||
// main.js - DEFINITIVE VERSION - Dynamic Line-by-Line Generation
|
||||
|
||||
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
|
||||
const path = require('path');
|
||||
@ -9,69 +9,112 @@ const createWindow = () => {
|
||||
width: 900,
|
||||
height: 750,
|
||||
webPreferences: {
|
||||
// CRITICAL: This links our main process to the renderer via the preload script
|
||||
preload: path.join(__dirname, 'preload.js'),
|
||||
// These security settings are essential for the preload script to work
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false
|
||||
}
|
||||
});
|
||||
|
||||
win.loadFile('index.html');
|
||||
|
||||
// Keep this active for debugging
|
||||
win.webContents.openDevTools();
|
||||
};
|
||||
|
||||
app.whenReady().then(() => {
|
||||
// CRITICAL: This handler listens for the 'form:submit' message from the renderer
|
||||
ipcMain.handle('form:submit', (event, data) => {
|
||||
// This log should appear in your VSCode/CMD/PowerShell terminal, NOT the app console
|
||||
console.log('--- Data received in main.js ---', data);
|
||||
|
||||
const browseName = data.BrowseName || 'default-machine';
|
||||
const yamlFileName = `${browseName.replace(/[^a-zA-Z0-9]/g, '_')}.yml`;
|
||||
// --- YAML Generation by Building an Array of Lines ---
|
||||
|
||||
// Define spacing constants for indentation
|
||||
const sp2 = ' ';
|
||||
const sp4 = ' ';
|
||||
const sp6 = ' ';
|
||||
|
||||
// Create an array to hold each line of the YAML file
|
||||
const yamlLines = [];
|
||||
|
||||
// 1. Build the 'machine' section line by line
|
||||
yamlLines.push('machine:');
|
||||
yamlLines.push(`${sp2}profile: ${data.machineType || 'Unspecified'}`);
|
||||
yamlLines.push(`${sp2}identification:`);
|
||||
|
||||
// Build the 'identification' sub-section, using the [VendorName] placeholder literally
|
||||
const machineType = data.machineType || 'Unspecified';
|
||||
const machineModel = data.model || '';
|
||||
const serialNo = data.serialNo || '';
|
||||
const VendorName = data.vendor || '';
|
||||
yamlLines.push(`${sp4}namespace: http://${VendorName}/${machineType}/${machineModel}/${serialNo}`);
|
||||
yamlLines.push(`${sp4}browseName: ${data.browseName || ''}`);
|
||||
yamlLines.push(`${sp4}manufacturer: "[${VendorName}]"`);
|
||||
|
||||
// Append the user-provided vendor as a comment, if it exists
|
||||
if (data.vendor) {
|
||||
yamlLines.push(`${sp4}# User-provided Vendor: ${data.vendor}`);
|
||||
}
|
||||
|
||||
yamlLines.push(`${sp4}model: "${machineModel}"`);
|
||||
yamlLines.push(`${sp4}serialNumber: "${serialNo}"`);
|
||||
yamlLines.push(`${sp4}yearofconstruction: "${data.buildYear || ''}"`);
|
||||
|
||||
// Add the final line for the 'machine' section
|
||||
yamlLines.push(`${sp2}version: "${data.version || 'Unset'}"`);
|
||||
|
||||
// 2. Build and Append the 'optionals' section if needed
|
||||
if (data.stopReason === 'Yes') {
|
||||
yamlLines.push(''); // Add a blank line for spacing
|
||||
yamlLines.push(`${sp2}optionals:`);
|
||||
yamlLines.push(`${sp4}- StopReason`);
|
||||
}
|
||||
|
||||
// 3. Build and Append the 'addins' section if needed
|
||||
const hasAddins = data.acpowerAddin === 'Yes' || data.utilities === 'Yes' || data.trackAddin === 'Yes';
|
||||
if (hasAddins) {
|
||||
yamlLines.push(''); // Add a blank line for spacing
|
||||
yamlLines.push(`${sp2}addins:`);
|
||||
if (data.acpowerAddin === 'Yes') {
|
||||
yamlLines.push(`${sp4}- name: AcPower`);
|
||||
}
|
||||
if (data.utilities === 'Yes') {
|
||||
yamlLines.push(`${sp4}- name: EnergyUtilitiesAddin`);
|
||||
yamlLines.push(`${sp6}# Requires data from a future 'Utilities ADDIN' UI.`);
|
||||
}
|
||||
if (data.trackAddin === 'Yes') {
|
||||
yamlLines.push(`${sp4}- name: TrackAdvance`);
|
||||
yamlLines.push(`${sp6}# Requires data from a future 'TrackAdvance ADDIN' UI.`);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Join all the generated lines into a single string
|
||||
const generatedContent = yamlLines.join('\n');
|
||||
|
||||
// --- End of YAML Generation Logic ---
|
||||
|
||||
console.log('--- Generated YAML Content ---\n', generatedContent);
|
||||
|
||||
// File saving logic (remains the same)
|
||||
const browseName = data.browseName || 'default-machine';
|
||||
const safeBrowseName = browseName.replace(/[^a-zA-Z0-9_-]/g, '_');
|
||||
const yamlFileName = `${safeBrowseName}.yml`;
|
||||
const documentsPath = app.getPath('documents');
|
||||
const yamlFilePath = path.join(documentsPath, yamlFileName);
|
||||
|
||||
let yamlString = '';
|
||||
for (const key in data) {
|
||||
yamlString += `${key}: ${data[key] || ''}\n`;
|
||||
}
|
||||
|
||||
try {
|
||||
fs.writeFileSync(yamlFilePath, yamlString, 'utf8');
|
||||
|
||||
fs.writeFileSync(yamlFilePath, generatedContent, 'utf8');
|
||||
dialog.showMessageBox({
|
||||
type: 'info',
|
||||
title: 'Success',
|
||||
message: 'Data Submitted Successfully!',
|
||||
detail: `Data was saved to:\n${yamlFilePath}`
|
||||
message: 'YAML File Generated Successfully!',
|
||||
detail: `The file was saved to:\n${yamlFilePath}`
|
||||
});
|
||||
|
||||
return { success: true, filePath: yamlFilePath };
|
||||
|
||||
} catch (error) {
|
||||
console.error("Failed to write file:", error);
|
||||
dialog.showErrorBox(
|
||||
'File Save Error',
|
||||
`Failed to save the data file.\nError: ${error.message}`
|
||||
);
|
||||
console.error("Failed to write YAML file:", error);
|
||||
dialog.showErrorBox('File Save Error', `Failed to save the YAML file.\nError: ${error.message}`);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
|
||||
createWindow();
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
createWindow();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow(); });
|
||||
});
|
||||
app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit(); });
|
||||
Loading…
Reference in New Issue
Block a user