internal AsyncLoadingContext(ITikCommand loadingCommand, TikAsyncLoadingThread loadingThread) { Guard.ArgumentNotNull(loadingCommand, "loadingCommand"); Guard.ArgumentNotNull(loadingThread, "loadingThread"); _loadingCommand = loadingCommand; _loadingThread = loadingThread; }
/// <summary> /// Initializes a new instance of the <see cref="TikConnectionException"/> class. /// </summary> /// <param name="message">The exception message.</param> /// <param name="command">The command sent to target.</param> /// <param name="responseList">The response from target.</param> public TikConnectionException(string message, ITikCommand command, IEnumerable<ITikSentence> responseList) : this(FormatMessage(message, command, responseList)) { }
/// <summary> /// Initializes a new instance of the <see cref="TikConnectionException"/> class. /// </summary> /// <param name="message">The exception message.</param> /// <param name="command">The command sent to target.</param> public TikConnectionException(string message, ITikCommand command) : this(FormatMessage(message, command, null)) { }
/// <summary> /// Initializes a new instance of the <see cref="TikConnectionException"/> class. /// </summary> /// <param name="message">The exception message.</param> /// <param name="command">The command sent to target.</param> /// <param name="responseList">The response from target.</param> public TikConnectionException(string message, ITikCommand command, IEnumerable <ITikSentence> responseList) : this(FormatMessage(message, command, responseList)) { }
/// <summary> /// ctor. /// </summary> /// <param name="command">Commant that throws exception.</param> /// <param name="message">Message exception.</param> /// <param name="code">Code of the error.</param> /// <param name="codeDescription">Code description of the error.</param> protected TikCommandException(ITikCommand command, string message) : base(message) { Command = command; }
protected TikCommandTrapException(ITikCommand command, string message) : base(command, message) { Code = null; CodeDescription = null; }
/// <summary> /// ctor. /// </summary> /// <param name="command">Commant that throws exception.</param> /// <param name="message">Message exception.</param> public TikCommandException(ITikCommand command, string message) : base(message) { Command = command; }
/// <summary> /// .ctor /// </summary> public TikCommandAmbiguousResultException(ITikCommand command) : base(command, $"only one response item expected\n{command}") { }
/// <summary> /// .ctor /// </summary> public TikNoSuchItemException(ITikCommand command) : base(command, "no such item") { }
/// <summary> /// Alias to <see cref="LoadList{TEntity}(ITikCommand)"/> without filter, ensures that result contains exactly one row. /// </summary> /// <typeparam name="TEntity">Loaded entities type.</typeparam> /// <param name="command">Command</param> /// <returns>Loaded single entity or null.</returns> public static TEntity LoadSingleOrDefault <TEntity>(this ITikCommand command) where TEntity : new() { return(LoadList <TEntity>(command).SingleOrDefault()); }
/// <summary> /// Saves entity to mikrotik router. Does insert (/add) whan entity has empty id and update(/set + /unset) when id is present). /// Behavior of save is modified via <see cref="TikPropertyAttribute"/> on properties. /// See <see cref="TikPropertyAttribute.DefaultValue"/>, <see cref="TikPropertyAttribute.UnsetOnDefault"/>. /// </summary> /// <typeparam name="TEntity">Saved entitie type.</typeparam> /// <param name="connection">Tik connection used to save.</param> /// <param name="entity">Saved entity.</param> /// <param name="usedFieldsFilter">List of field names (on mikrotik) which should be modified. If is not null, only listed fields will be modified.</param> public static void Save <TEntity>(this ITikConnection connection, TEntity entity, IEnumerable <string> usedFieldsFilter = null) where TEntity : new() { var metadata = TikEntityMetadataCache.GetMetadata <TEntity>(); EnsureNotReadonlyEntity(metadata); string id; if (metadata.IsSingleton) { id = null; } else { EnsureHasIdProperty(metadata); id = metadata.IdProperty.GetEntityValue(entity); } if (!metadata.IsSingleton && string.IsNullOrEmpty(id)) { //create ITikCommand createCmd = connection.CreateCommand(metadata.EntityPath + "/add", TikCommandParameterFormat.NameValue); foreach (var property in metadata.Properties .Where(pm => !pm.IsReadOnly) .Where(pm => usedFieldsFilter == null || usedFieldsFilter.Contains(pm.FieldName, StringComparer.OrdinalIgnoreCase))) { if (!property.HasDefaultValue(entity)) { createCmd.AddParameter(property.FieldName, property.GetEntityValue(entity)); } } id = createCmd.ExecuteScalar(); if (metadata.HasIdProperty) { metadata.IdProperty.SetEntityValue(entity, id); // update saved id into entity } } else { //update (set+unset) ITikCommand setCmd = connection.CreateCommand(metadata.EntityPath + "/set", TikCommandParameterFormat.NameValue); if (!metadata.IsSingleton && usedFieldsFilter == null) { //compare state on mikrotik and update different fields only var unmodifiedEntity = connection.LoadById <TEntity>(id); //TODO some kind of "loaded entities" session cache could be used to avoid another load before save. usedFieldsFilter = entity.GetDifferentFields(unmodifiedEntity); } List <string> fieldsToUnset = new List <string>(); foreach (var property in metadata.Properties .Where(pm => !pm.IsReadOnly) .Where(pm => usedFieldsFilter == null || usedFieldsFilter.Contains(pm.FieldName, StringComparer.OrdinalIgnoreCase))) { if (property.HasDefaultValue(entity) && property.UnsetOnDefault) { fieldsToUnset.Add(property.FieldName); } else { setCmd.AddParameter(property.FieldName, property.GetEntityValue(entity)); //full update (all values) } } if (fieldsToUnset.Count > 0) { // this should also work (see http://forum.mikrotik.com/viewtopic.php?t=28821 ) //ip/route/unset //=.id = *1 //= value-name=routing-mark foreach (string fld in fieldsToUnset) { ITikCommand unsetCmd = connection.CreateCommand(metadata.EntityPath + "/unset", TikCommandParameterFormat.NameValue); unsetCmd.AddParameter(TikSpecialProperties.Id, id, TikCommandParameterFormat.NameValue); unsetCmd.AddParameter(TikSpecialProperties.UnsetValueName, fld); unsetCmd.ExecuteNonQuery(); } } if (setCmd.Parameters.Any()) { if (!metadata.IsSingleton) { setCmd.AddParameter(TikSpecialProperties.Id, id, TikCommandParameterFormat.NameValue); } setCmd.ExecuteNonQuery(); } } }
/// <summary> /// ctor. /// </summary> /// <param name="command">Commant that throws exception.</param> /// <param name="trapSentence">Error=trap sentence returned from mikrotik router as response to <paramref name="command"/> call.</param> public TikCommandException(ITikCommand command, ITikTrapSentence trapSentence) : this(command, trapSentence.CategoryCode, trapSentence.CategoryDescription, trapSentence.Message) { }
/// <summary> /// ctor. /// </summary> /// <param name="command">Commant that throws exception.</param> /// <param name="message">Message exception.</param> /// <param name="code">Code of the error.</param> /// <param name="codeDescription">Code description of the error.</param> public TikCommandException(ITikCommand command, string code, string codeDescription, string message) : this(command, message) { Code = code; CodeDescription = codeDescription; }
/// <summary> /// Initializes a new instance of the <see cref="TikConnectionException"/> class. /// </summary> /// <param name="message">The exception message.</param> /// <param name="command">The command sent to target.</param> /// <param name="responseList">The response from target.</param> public TikCommandUnexpectedResponseException(string message, ITikCommand command, IEnumerable <ITikSentence> responseList) : base(command, FormatMessage(message, command, responseList)) { }
/// <summary> /// .ctor /// </summary> public TikCommandAmbiguousResultException(ITikCommand command, int ambiguousItemsCnt) : base(command, $"only one response item expected, returned {ambiguousItemsCnt} items\n{command}") { }
/// <summary> /// Adds new instance of parameter with .proplist to <see cref="Parameters"/> list. /// </summary> /// <param name="proplist">Names of the wanted properties</param> /// <returns>Instance of added parameter.</returns> public static ITikCommandParameter AddProplistParameter(this ITikCommand command, params string[] proplist) { ITikCommandParameter result = command.AddParameter(TikSpecialProperties.Proplist, string.Join(",", proplist), TikCommandParameterFormat.NameValue); return(result); }
/// <summary> /// ctor. /// </summary> /// <param name="command">Commant that throws exception.</param> /// <param name="trapSentence">Error=trap sentence returned from mikrotik router as response to <paramref name="command"/> call.</param> public TikCommandTrapException(ITikCommand command, ITikTrapSentence trapSentence) : base(command, trapSentence.Message) { Code = trapSentence.CategoryCode; CodeDescription = trapSentence.CategoryDescription; }
/// <summary> /// .ctor /// </summary> public TikNoSuchItemException(ITikCommand command) : base(command, $"no such item\n{command}") { }
/// <summary> /// ctor. /// </summary> /// <param name="command">Commant that throws exception.</param> /// <param name="trapSentence">Error=trap sentence returned from mikrotik router as response to <paramref name="command"/> call.</param> public TikNoSuchCommandException(ITikCommand command, ITikTrapSentence trapSentence) : base(command, trapSentence) { }
/// <summary> /// ctor. /// </summary> /// <param name="command">Commant that throws exception.</param> /// <param name="trapSentence">Error=trap sentence returned from mikrotik router as response to <paramref name="command"/> call.</param> public TikAlreadyHaveSuchItemException(ITikCommand command, ITikTrapSentence trapSentence) : base(command, trapSentence) { }
/// <summary> /// Initializes a new instance of the <see cref="TikConnectionException"/> class. /// </summary> /// <param name="message">The exception message.</param> /// <param name="command">The command sent to target.</param> /// <param name="response">The response from target.</param> public TikConnectionException(string message, ITikCommand command, ITikSentence response) : this(FormatMessage(message, command, new ITikSentence[] { response })) { }
/// <summary> /// ctor. /// </summary> /// <param name="command">Commant that throws exception.</param> /// <param name="message">Message exception.</param> public TikCommandFatalException(ITikCommand command, string message) : base(command, message) { }
/// <summary> /// ctor. /// </summary> /// <param name="command">Commant that throws exception.</param> /// <param name="message">Message exception.</param> public TikCommandAbortException(ITikCommand command, string message) : base(command, message) { }
/// <summary> /// Initializes a new instance of the <see cref="TikConnectionException"/> class. /// </summary> /// <param name="message">The exception message.</param> /// <param name="command">The command sent to target.</param> /// <param name="response">The response from target.</param> public TikCommandUnexpectedResponseException(string message, ITikCommand command, ITikSentence response) : base(command, FormatMessage(message, command, new ITikSentence[] { response })) { }
private static string FormatMessage(string message, ITikCommand command, IEnumerable<ITikSentence> responseList) { Guard.ArgumentNotNull(message, "message"); StringBuilder result = new StringBuilder(); result.AppendLine(message); if (command != null) { result.AppendLine(" COMMAND: " + command.CommandText); foreach (ITikCommandParameter param in command.Parameters) { result.AppendLine(" " + param.ToString()); } } if (responseList != null) { result.AppendLine(" RESPONSE:"); foreach (ITikSentence sentence in responseList) { result.AppendLine(" " + sentence.ToString()); } } return result.ToString(); }
internal static IEnumerable <ITikReSentence> GetPackageListFromMikrotik(ITikConnection connection) { ITikCommand PackageCmd = connection.CreateCommand("/ppp/profile/print"); return(PackageCmd.ExecuteList()); }