Reiniciar a visualización en tiempo real
Tengo algunos controles para configurar todos los ValueBox a la fecha seleccionable, ahora, quiero que al presionar el boton "ACTUAL" me regrese a la visualización en tiempo real. ¿Como hago esto a partir de un script?
1 reply
-
Hi Luis,
If you want to reset each of the ValueBox controls back to the Live values, you can create a script that runs when you click the Actual button to set the AggregateKind, AggregateStartTime, and AggregateInterval values back to the initial values. I'm assuming you're using the OnDateTimeChanged event of the DateTimePicker to set these values in the first place.
Below is an example application that has a grid with some ValueBoxes and a DateTimePicker. When you select a date, it changes the ValueBoxes to use that date as the end of the aggregate interval. And then when you click the Actual button, it sets it back to Live values.
using AxiomCore2.Client; using AxiomCore2.ControlProperties; using AxiomCore2.Controls; using AxiomCore2.Data; using AxiomCore2.Events; using AxiomCore2.Legacy; using AxiomCore2.Log; using AxiomCore2.Managers; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Collections; using System.Drawing; //using CanaryWebServiceHelper; //using CanaryWebServiceHelper.HistorianWebService; namespace AxiomScript { public partial class ScreenScript : IDisposable { // NOTE: click help icon in dialog header for API documentation and examples. // NOTE: use EventTimer class when timer is necessary. See API documentation. // axiom references public ControlApplication Application { get; } = ControlApplication.Instance; public ControlFactoryManager ControlFactory { get; } = ControlFactoryManager.Instance; public IDataProvider DataProvider { get; } = DataProviderManager.CreateInstance(); public ILog Log { get; } = ClientLog.UserScript; public NavigationManager Navigation { get; } = NavigationManager.Instance; public ControlScreen Screen => _screen; //This is important, set this to the amount of time you want to use for your report. //It can be any amount of time, but it must be a relative timespan string //See docs here https://axiomscripting.canarylabs.com/22.3/html/f3220ae7-46ca-4faa-b662-6eafd473fdad.htm string reportTimespan = "12H"; public void OnScreenVisible() { //Actual Button if(!Screen.ScreenControls.ContainsKey("ActualButton")){ var actualButton = (ControlButton)ControlFactory.CreateControl(ControlKind.Button, "ActualButton"); Screen.Children.Add(actualButton); actualButton.Text = "Actual"; actualButton.Width = 175; actualButton.X = 200; actualButton.Y = 0; actualButton.OnClick = "ActualButton_OnClick"; } //Report DateTimePicker ControlDateTimePicker reportDateTimePicker; if(!Screen.ScreenControls.ContainsKey("ReportDateTimePicker")){ reportDateTimePicker = (ControlDateTimePicker)ControlFactory.CreateControl(ControlKind.DateTimePicker, "ReportDateTimePicker"); Screen.Children.Add(reportDateTimePicker); reportDateTimePicker.X = 0; reportDateTimePicker.Y = 0; reportDateTimePicker.DateTime = DateTime.Now.Date; reportDateTimePicker.OnDateTimeChange = "ReportDateTimePicker_OnDateTimeChange"; } else { reportDateTimePicker = Screen.ScreenControls["ReportDateTimePicker"] as ControlDateTimePicker; reportDateTimePicker.DateTime = DateTime.Now.Date; } //Report Grid ControlGrid reportGrid = null; if(!Screen.ScreenControls.ContainsKey("ReportGrid")){ reportGrid = (ControlGrid)ControlFactory.CreateControl(ControlKind.Grid, "ReportGrid"); Screen.Children.Add(reportGrid); reportGrid.X = 0; reportGrid.Y = 50; } else { reportGrid = Screen.ScreenControls["ReportGrid"] as ControlGrid; } reportGrid.Children.Clear(); //Create array of headers var headers = new string[]{ "Caldera", "Actual", "Promedio" }; var labels = new string[]{ "Historian Threads", "CPU %" }; var tags = new string[]{ "localhost.{Diagnostics}.Sys.Historian Thread Count", "localhost.{Diagnostics}.Sys.CPU Usage Total"}; reportGrid.ColumnHeaders = headers; //Loop through each row in the grid for(int i = 0; i < labels.Length; i++){ //Create a GridRow var gridRow = (ControlGridRow)ControlFactory.CreateControl(ControlKind.GridRow); reportGrid.Children.Add(gridRow); //Column 1: Label var label = (ControlLabel)ControlFactory.CreateControl(ControlKind.Label); label.Text = labels[i]; gridRow.Children.Add(label); //Column 2: Live value var actualValueBox = (ControlValueBox)ControlFactory.CreateControl(ControlKind.ValueBox); gridRow.Children.Add(actualValueBox); actualValueBox.SourceTag = tags[i]; //Column 3: Average value var promedioValueBox = (ControlValueBox)ControlFactory.CreateControl(ControlKind.ValueBox); gridRow.Children.Add(promedioValueBox); promedioValueBox.SourceTag = tags[i]; promedioValueBox.AggregateStartTime = $"Now - {reportTimespan}"; promedioValueBox.AggregateInterval = reportTimespan; promedioValueBox.AggregateKind = "TimeAverage2"; } } public void OnScreenInvisible() { } public void Dispose() { } public void ReportDateTimePicker_OnDateTimeChange(object sender, DateTime dateTime) { //Get the list of all ValueBox controls on screen var valueBoxes = Screen.Descendants<ControlValueBox>(true); var reportStartTime = CanaryShared.DateTimeExtensions.SubtractRelativeInterval(new DateTimeOffset(dateTime), new CanaryShared.RelativeInterval(reportTimespan)); //Get the DateTime from the DateTimePicker as a string var startTimeString = reportStartTime.ToString("o"); //Loop through each ValueBox foreach(var valueBox in valueBoxes){ //Set the AggregateStartTime to the DateTime from the DateTimePicker minus your reporting timespan //This could be 1H or any timespan you want valueBox.AggregateStartTime = $"{startTimeString}"; //Check if this ValueBox is in the second column of the grid (index = 1) //The second column is for the "Live" values if(valueBox.Parent.Children.IndexOf(valueBox) == 1){ //Use EndBound to show the last value in the reporting timespan valueBox.AggregateKind = "EndBound"; //Check if this ValueBox is in the third column of the grid (index = 2) //The third column is for the average values over some reporting time period } else if(valueBox.Parent.Children.IndexOf(valueBox) == 2) { //Use TimeAverage2 or any other aggregate you want valueBox.AggregateKind = "TimeAverage2"; } //Set the AggregateInterval to be the reporting timespan used earlier valueBox.AggregateInterval = "1H"; } } public void ActualButton_OnClick(object sender, EventArgs eventArgs) { //Get the list of all ValueBox controls on screen var valueBoxes = Screen.Descendants<ControlValueBox>(true); //Loop through each ValueBox foreach(var valueBox in valueBoxes){ //Check if this ValueBox is in the second column of the grid (index = 1) //The second column is for the "Live" values if(valueBox.Parent.Children.IndexOf(valueBox) == 1){ //Set the AggregateStartTime, AggregateKind, and AggregateInterval to null to reset to "Live" values valueBox.AggregateStartTime = null; valueBox.AggregateKind = null; valueBox.AggregateInterval = null; //Check if this ValueBox is in the third column of the grid (index = 2) //The third column is for the average values over some reporting time period } else if(valueBox.Parent.Children.IndexOf(valueBox) == 2) { //Set the AggregateStartTime to be Now minus the reporting timespan valueBox.AggregateStartTime = $"Now - {reportTimespan}"; //Use TimeAverage2 or any other aggregate valueBox.AggregateKind = "TimeAverage2"; //Set the AggregateInterval to be the same as the reportin timespan valueBox.AggregateInterval = reportTimespan; } } } } }