-
Notifications
You must be signed in to change notification settings - Fork 94
ADO.NET like API
Second option is to use ADO.NET like API. This API tries to be very simmilar to database API used in .NET world. It allows you to create ITikCommand commands, use parameters and other features like ADO.NET does.
The main entry point is mikrotik router connection object (ITikConnection). Instance of connection object should be instancied via factory (ConnectionFactory). ITikCommandinstance should be created via ITikConnection methods.
using tik4net;
// ...
connection = ConnectionFactory.OpenConnection(TikConnectionType.Api, HOST, USER, PASS);
ITikCommand command = connection.CreateCommand("/system/identity/print");
string identity = cmd.ExecuteScalar();
Console.WriteLine("Router identity: {0}", identity);
##How to create command
- connection.CreateCommand
- connection.CreateCommandAndParameters
ITikCommand command = connection.CreateCommand("/system/identity/print");
##Command methods
- ExecuteNonQuery - excecutes command on router and ensures that operation was sucessfull.
- ExecuteScalar - executes command on router and ensures that operation returns one value (=ret parameter), which is returned as result.
- ExecuteSingleRow - executes command on router and ensures that operation returns exactly one row (1x !re and 1x !done) as result.
- ExecuteList - executes command on router and returns all result sentences (all !re sentences) as result.
- ExecuteListWithDuration - Executes command on router and returns all result sentences (all !re sentences) which are returned during given duration. After this period, command is automatically stopped via Cancel command.
- ExecuteAsync - executes command on router asynchronously. Response is returned via callback when it is read from mikrotik (for tag, which has been dynamically assigned). Running command should be later canceled via Cancel method.
##How to use parameters Parameters are similar to DB ones. Instance of ITikCommandParameter should be created via connection.CreateParameter method.
var cmd = connection.CreateCommand("/log/" + logLevelCommandSufix,
connection.CreateParameter("message", message));
cmd.ExecuteNonQuery();
##Suport of asynchronous commands Sometimes you need to perform command for longer time and read response messages. Typical example is using of torch. For this reason command.ExecuteAsync method is provided. It handles all .tag magic (see mikrotik wiki) on background.
Async command support methods:
- command.ExecuteAsync
- command.Cancel
- command.CancelAndJoin
ITikCommand torchCmd = connection.CreateCommand("/tool/torch",
connection.CreateParameter("interface", "ether1"),
connection.CreateParameter("port", "any"),
connection.CreateParameter("src-address", "0.0.0.0/0"),
connection.CreateParameter("dst-address", "0.0.0.0/0")
);
torchCmd.ExecuteAsync(response => Console.WriteLine("Row: " + response.GetResponseField("tx")));
Console.WriteLine("Press ENTER");
Console.ReadLine();
torchCmd.CancelAndJoin();