Skip to content

High level API reading data

Daniel Frantik edited this page Mar 5, 2016 · 8 revisions

All data-read methods are implemented as extension methods of ITikConnection and ITikCommand objects. Every method returns some data entities. For details about entities see this wiki page.

  • connection.LoadAll: Loads all entity list without filtering.
  • connection.LoadSingle: Loads exactly one row (optionaly with filter).
  • connection.LoadSingleOrDefault: Loads one row or nothing.
  • connection.LoadById: Loads entity with specified id. Returns null if not found.
  • connection.LoadList: Loads entity list. Could be filtered with filterParameters.
  • command.LoadList: --dtto--
  • connection.LoadWithDuration: Calls command and reads all returned rows for given period.
  • command.LoadWithDuration: --dtto--
  • connection.LoadAsync: Calls command and starts backgroud reading thread. After that returns control to calling thread. All read rows are returned as callbacks
  • command.LoadAsync: -dtto-

###LoadAll Alias to LoadList without filter. You can filter list via LINQ on client side.

// load all QT entities
var queues = connection.LoadAll<QueueTree>()

###LoadSingle Alias to LoadList optionaly with filter, ensures that result contains exactly one row. Throws exeption if command returns zero or more than one entities.

// there should be exactly one row in response for /system/resource/print
var sysRes = connection.LoadSingle<SystemResource>();

###LoadSingleOrDefault Similar to LoadSingle but returns null if row is not found.

// try to find mangle by its comment
var startMangle = connection.LoadSingleOrDefault<FirewallMangle>(
    connection.CreateParameter("comment", "MY COMMENT"));

###LoadById Loads entity with specified id. Returns null if not found.

//from unittest
connection.Save(filter);

// verify that all values were realy written to mikrotik router
var loadedFilter = Connection.LoadById<BridgeFilter>(filter.Id);
Assert.IsNotNull(loadedFilter);
Assert.AreEqual(filter.Chain, loadedFilter.Chain);

###LoadList Loads entity list. Could be filtered with filterParameters.

// load all log entries
var logs = connection.LoadList<Log>();
foreach (Log log in logs)
{
    Console.WriteLine("{0}[{1}]: {2}", log.Time, log.Topics, log.Message);
}

###LoadWithDuration Calls command and reads all returned rows for given durationSec period. After this period calls cancell to mikrotik router and returns all loaded rows. Throws exception if any 'trap' row occurs. ###LoadAsync Calls command and starts backgroud reading thread. After that returns control to calling thread. All read rows are returned as callbacks (onLoadItemCallback, onExceptionCallback) from loading thread.

REMARKS: if you want to propagate loaded values to GUI, you should use some kind of synchronization or Invoke, because allbacks are called from non-ui thread.

The running load can be terminated by Cancel CancelAndJoin call on command object. Command is returned as result of the method.

var cmd = connection.LoadAsync<ToolTorch>(
    item => Console.WriteLine(item.ToString()), 
    error => Console.WriteLine("ERROR: " + error.ToString()),
    connection.CreateParameter("interface", interfaceName),
    connection.CreateParameter("port", "any"),
    connection.CreateParameter("src-address", "0.0.0.0/0"),
    connection.CreateParameter("dst-address", "0.0.0.0/0"));

    Console.ReadLine();

   cmd.Cancel();
Clone this wiki locally