0

Axiom Scripting Methods

Is there an example on how to properly use IDataprovider methods like GetRawData? The scripting documentation has some examples but not any showcasing this.

2 replies

null
    • laruer
    • 1 yr ago
    • Reported - view

    nicolas

    Here's an example of using DataProvider.GetRawData :

    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;
    
    namespace AxiomScript
    {
        public partial class ScreenScript : IDisposable
        {
            // 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;
    
            // EventTimer setup from documentation example https://axiomscripting.canarylabs.com/22.3/html/8675300e-3c1c-42c5-8590-9e4d4e9d7627.htm
            private EventTimer _timer = null;
            private int _initialTime = Convert.ToInt32(TimeSpan.FromSeconds(1).TotalMilliseconds);
            private int _interval = Convert.ToInt32(TimeSpan.FromSeconds(5).TotalMilliseconds);
    
            public void OnScreenVisible()
            {
                // check if Label1 exists, and create it if it doesn't already
                if(!Screen.ScreenControls.ContainsKey("Label1")){
                    var label1 = ControlFactory.CreateControl(ControlKind.Label, "Label1");
                    Screen.Children.Add(label1);
                }
                if (_timer == null)
                {
                    // initialize timer
                    _timer = new EventTimer(
                        callback: TimerCallback,
                        dueTime: _initialTime,
                        period: _interval);
                }
                else
                {
                    // reset timer
                    _timer.Change(
                        dueTime: _initialTime,
                        period: _interval);
                }
            }
    
            public void OnScreenInvisible()
            {
                if (_timer != null)
                {
                    // pause timer
                    _timer.Change(
                        dueTime: EventTimer.Infinite,
                        period: EventTimer.Infinite);
                }
            }
    
            public void Dispose()
            {
            }
    
            private void TimerCallback(EventTimer timer, object userData)
            {
                // update Label1 with CPU Usage tag data
                if(Screen.ScreenControls.ContainsKey("Label1")){
    
                    var label1 = (ControlLabel)Screen.ScreenControls["Label1"];
    
                    // use DataProvider to get raw data from a specific time period
                    var result = DataProvider.GetRawData("localhost.{Diagnostics}.Sys.CPU Usage Total",DateTime.Now.AddSeconds(-1),DateTime.Now,1,false);
    
                    Log.Info($"Got {result.Values.Count().ToString()} tag value(s)");
    
                    // loop through each TVQ in result
                    foreach(var tvq in result.Values){
                        Log.Info(tvq.ToString());
                    }
    
                    // in this example we are only using the first TVQ in the result
                    var firstTvq = result.Values.FirstOrDefault();
                    if(firstTvq != null){
                        var stringValue = firstTvq.Value.ToString();
                        label1.Text = stringValue;
                    }
                }
            }
        }
    }
    
    • nicolas
    • 1 yr ago
    • Reported - view

    Thank you so much! This is very helpful laruer

Content aside

print this pagePrint this page
  • 1 yr agoLast active
  • 2Replies
  • 172Views
  • 2 Following