You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generate a report and trigger some action based on the result code
#region Using directives
usingSystem;usingSystem.IO;usingSystem.Linq;usingSystem.Reflection.Emit;usingFTOptix.Core;usingFTOptix.HMIProject;usingFTOptix.NetLogic;usingFTOptix.Report;usingFTOptix.UI;usingUAManagedCore;
#endregion
publicpartialclassPdfReportLogic:BaseNetLogic{publicoverridevoidStart(){// Try to assign a value to the buttontry{generatePdfButton=Owner.Get<Button>("TrackedValues/Generate");viewPdfButton=Owner.Get<Button>("TrackedValues/View");viewPdfButton.Enabled=false;}catch{// Button does not existLog.Warning("PdfReportLogic","Can't find PDF buttons, maybe they were renamed?");}}publicoverridevoidStop(){myReport.OnGeneratePdfCompleted-=MyReport_OnGeneratePdfCompleted;}[ExportMethod]publicvoidGenerateReport(stringfileName,stringpdfLocale){// Hide buttongeneratePdfButton.Enabled=false;// Check if the path is emptyif(string.IsNullOrEmpty(fileName)){Log.Error("PdfReportLogic","Empty PDF name");SetOutputMessage("ReportEmptyPath");thrownewArgumentNullException();}// Check if the pdf has the extensionif(!fileName.EndsWith(".pdf",StringComparison.InvariantCultureIgnoreCase)){Log.Warning("PdfReportLogic","PDF extension not found, adding it");fileName+=".pdf";}// Make sure to use only the file namefileName=fileName.Split('\\','/').Last();// Check if the path is an Optix path variablevarpdfResourceUri=ResourceUri.FromProjectRelativePath(fileName);// Check if the locale is validif(string.IsNullOrEmpty(pdfLocale)){Log.Error("PdfReportLogic","Empty locale");SetOutputMessage("ReportEmptyLocale");thrownewArgumentNullException();}// Check if the locale is valid using regexif(!LocaleIdRegex().IsMatch(pdfLocale)){Log.Error("PdfReportLogic","Invalid locale");SetOutputMessage("ReportInvalidLocale");thrownewArgumentException();}// Generate the reportmyReport=Project.Current.Get<Report>("Reports/LoggerReport");Log.Debug("PdfReportLogic",$"Generating PDF report with locale: {pdfLocale}");Log.Debug("PdfReportLogic",$"Saving PDF report to: {pdfResourceUri.Uri}");myReport.GeneratePdf(pdfResourceUri,pdfLocale,outGuidoperationId);// Subscribe to the report generation eventmyReport.OnGeneratePdfCompleted+=MyReport_OnGeneratePdfCompleted;Log.Debug("PdfReportLogic",$"Report generation started with operation ID: {operationId}");}privatevoidMyReport_OnGeneratePdfCompleted(objectsender,GeneratePdfCompletedEvente){Log.Info("PdfReportLogic",$"Report generation completed with result: {e.Result}");if(e.Result==GeneratePdfCompletedResult.PdfSuccessfullyGenerated){SetOutputMessage("ReportSuccess");viewPdfButton.Enabled=true;}else{SetOutputMessage("ReportFailed");viewPdfButton.Enabled=false;Log.Error("PdfReportLogic",$"PDF generation failed, error: {e.Result}");}generatePdfButton.Enabled=true;}// Set output messageprivatevoidSetOutputMessage(stringtranslationKey){varoutputLabel=Owner.Get<FTOptix.UI.Label>("TrackedValues/OutputMessage");try{outputLabel.LocalizedText=newLocalizedText(outputLabel.NodeId.NamespaceIndex,translationKey);}catch{Log.Warning("PdfReportLogic",$"Translation key not found: {translationKey}");outputLabel.Text=translationKey;}}// Regex for locale ID[System.Text.RegularExpressions.GeneratedRegex("^[a-z]{2}-[A-Z]{2}$")]privatestaticpartialSystem.Text.RegularExpressions.RegexLocaleIdRegex();// PDF generator buttonprivateFTOptix.UI.ButtongeneratePdfButton;privateFTOptix.UI.ButtonviewPdfButton;privateFTOptix.Report.ReportmyReport;}