Skip to content

Commit

Permalink
add documentation of implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
FadySalama committed Feb 8, 2024
1 parent c8e100b commit a1118a8
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 26 deletions.
22 changes: 11 additions & 11 deletions WoT/Errors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WoT.Errors
{
class EvalError : Exception
public class EvalError : Exception
{
public EvalError() : base() { }
public EvalError(string messsage) : base(messsage) { }
Expand All @@ -15,7 +15,7 @@ public EvalError(string messsage, Exception inner) : base(messsage, inner) { }
}

}
class RangeError : Exception
public class RangeError : Exception
{
public RangeError() : base() { }
public RangeError(string messsage) : base(messsage) { }
Expand All @@ -26,7 +26,7 @@ public RangeError(string messsage, Exception inner) : base(messsage, inner) { }
}

}
class ReferenceError : Exception
public class ReferenceError : Exception
{
public ReferenceError() : base() { }
public ReferenceError(string messsage) : base(messsage) { }
Expand All @@ -37,7 +37,7 @@ public ReferenceError(string messsage, Exception inner) : base(messsage, inner)
}

}
class TypeError : Exception
public class TypeError : Exception
{
public TypeError() : base() { }
public TypeError(string messsage) : base(messsage) { }
Expand All @@ -47,7 +47,7 @@ public TypeError(string messsage, Exception inner) : base(messsage, inner) { }
return $"TypeError: {this.Message}";
}
}
class URIError : Exception
public class URIError : Exception
{
public URIError() : base() { }
public URIError(string messsage) : base(messsage) { }
Expand All @@ -58,7 +58,7 @@ public URIError(string messsage, Exception inner) : base(messsage, inner) { }
}

}
class NotFoundError : Exception
public class NotFoundError : Exception
{
public NotFoundError() : base() { }
public NotFoundError(string messsage) : base(messsage) { }
Expand All @@ -69,7 +69,7 @@ public NotFoundError(string messsage, Exception inner) : base(messsage, inner) {
}
}

class NotSupportedError : Exception
public class NotSupportedError : Exception
{
public NotSupportedError() : base() { }
public NotSupportedError(string messsage) : base(messsage) { }
Expand All @@ -80,7 +80,7 @@ public NotSupportedError(string messsage, Exception inner) : base(messsage, inne
}
}

class SyntaxErrpr : Exception
public class SyntaxErrpr : Exception
{
public SyntaxErrpr() : base() { }
public SyntaxErrpr(string messsage) : base(messsage) { }
Expand All @@ -91,7 +91,7 @@ public SyntaxErrpr(string messsage, Exception inner) : base(messsage, inner) { }
}
}

class NotReadableError: Exception
public class NotReadableError: Exception
{
public NotReadableError() : base() { }
public NotReadableError(string messsage) : base(messsage) { }
Expand All @@ -102,7 +102,7 @@ public NotReadableError(string messsage, Exception inner) : base(messsage, inner
}
}

class OperationError : Exception
public class OperationError : Exception
{
public OperationError() : base() { }
public OperationError(string messsage) : base(messsage) { }
Expand All @@ -113,7 +113,7 @@ public OperationError(string messsage, Exception inner) : base(messsage, inner)
}
}

class NotAllowedError : Exception
public class NotAllowedError : Exception
{
public NotAllowedError() : base() { }
public NotAllowedError(string messsage) : base(messsage) { }
Expand Down
64 changes: 51 additions & 13 deletions WoT/SimpleHTTPConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@

namespace WoT.Implementation
{
public class SimpleHTTPConsumer : IConsumer, IDiscovery
/// <summary>
/// A simple WoT Consumer that is capable of requesting TDs only from HTTP resources und consumes them to generate <see cref="SimpleConsumedThing"/>
/// </summary>
public class SimpleHTTPConsumer : IConsumer, IRequester
{
private readonly JsonSerializer _serializer;
public readonly HttpClient httpClient;

/// <inheritdoc/>
public SimpleHTTPConsumer()
{
httpClient = new HttpClient();
_serializer = new JsonSerializer();

}

public async Task<IConsumedThing> Consume(ThingDescription td)
{
Task<SimpleConsumedThing> task = Task.Run(() =>
Expand Down Expand Up @@ -68,6 +73,12 @@ public async Task<ThingDescription> RequestThingDescription(Uri tdUrl)
}
}

/// <summary>
/// A representation of a consumed Thing that is capable of interacting only with HTTP resources.
/// </summary>
/// <remarks>
/// This simple Consumed Thing class can only handle HTTP request and responses, <c>"application/json"</c> content type, and <c>"observeproperty"</c> and <c>"subscribeevent"</c> using long polling
/// </remarks>
public class SimpleConsumedThing : IConsumedThing
{

Expand All @@ -81,7 +92,6 @@ public SimpleConsumedThing(ThingDescription td, SimpleHTTPConsumer consumer)
_consumer = consumer;
_activeSubscriptions = new Dictionary<string, ISubscription>();
_activeObservations = new Dictionary<string, ISubscription>();

}

/****************************************************** Action Operations ******************************************************/
Expand Down Expand Up @@ -192,7 +202,7 @@ public async Task<IInteractionOutput<T>> InvokeAction<T,U>(string actionName, U
}

/****************************************************** Property Operations ******************************************************/

public async Task<IInteractionOutput<T>> ReadProperty<T>(string propertyName, InteractionOptions? options = null)
{
var properties = this._td.Properties;
Expand All @@ -201,17 +211,17 @@ public async Task<IInteractionOutput<T>> ReadProperty<T>(string propertyName, In
if (!properties.TryGetValue(propertyName, out var propertyAffordance))
{
// 4. If interaction is undefined, reject promise with a NotFoundError and stop.
throw new Exception($"Property {propertyName} not found in TD with ID {_td.Id}");
throw new NotFoundError($"Property {propertyName} not found in TD with ID {_td.Id}");
}
else
{
if (propertyAffordance.WriteOnly == true) throw new Exception($"Cannot read writeOnly property {propertyName}");
if (propertyAffordance.WriteOnly == true) throw new NotAllowedError($"Cannot read writeOnly property {propertyName}");
// find suitable form
form = FindSuitableForm(propertyAffordance.Forms, "readproperty", "http", options);
// Handle UriVariables
if (options.HasValue && options.Value.uriVariables != null) form = HandleUriVariables(form, options.Value.uriVariables);

if (form == null) throw new Exception($"Could not find a form that allows reading property {propertyName}");
if (form == null) throw new NotFoundError($"Could not find a form that allows reading property {propertyName}");
HttpResponseMessage interactionResponse = await _consumer.httpClient.GetAsync(form.Href);
interactionResponse.EnsureSuccessStatusCode();
Stream responseStream = await interactionResponse.Content.ReadAsStreamAsync();
Expand Down Expand Up @@ -593,19 +603,34 @@ public async Task<ISubscription> SubscribeEvent<T>(string eventName, Action<IInt
/****************************************************** Utility Methods ******************************************************/
public ThingDescription GetThingDescription() { return _td; }

public bool HasActiveListeners()
{
return _activeObservations.Count > 0 || _activeSubscriptions.Count > 0;
}
/// <summary>
/// Indicates if there are active subscriptions for observations or events
/// </summary>
/// <value>
/// wether there are active subscriptions
/// </value>
public bool HasActiveListeners { get => _activeObservations.Count > 0 || _activeSubscriptions.Count > 0; }


/// <summary>
/// Add credentials for a Thing with given ID
/// </summary>
/// <param name="id">Thing ID</param>
/// <param name="password"></param>
/// <exception cref="NotImplementedException"></exception>
public void AddCredentials(string id, string password)
{

throw new NotImplementedException();
}

/// <summary>
/// Removes credentials for a Thing with given ID
/// </summary>
/// <param name="id">Thing ID</param>
/// <exception cref="NotImplementedException"></exception>
public void RemoveCredentials(string id)
{

throw new NotImplementedException ();
}

protected Form HandleUriVariables(Form form, Dictionary<string, object> uriVariavles)
Expand Down Expand Up @@ -667,7 +692,7 @@ public InteractionOutput(Form form)
}
public Stream Data => null;

public bool DataUsed => true;
public bool DataUsed => false;

public Form Form => _form;

Expand All @@ -682,6 +707,11 @@ public Task Value()
return null;
}
}

/// <summary>
/// An implementation of <see cref="IInteractionOutput{T}"/>
/// </summary>
/// <typeparam name="T">output data type</typeparam>
public class InteractionOutput<T> : IInteractionOutput<T>
{
private readonly Form _form;
Expand Down Expand Up @@ -798,6 +828,10 @@ public async Task<T> Value()
return await task;
}
}

/// <summary>
/// An implementation of <see cref="ISubscription"/>
/// </summary>
public class Subscription : ISubscription
{
private readonly SubscriptionType _type;
Expand All @@ -811,6 +845,10 @@ public class Subscription : ISubscription

public event EventHandler StopEvent;
public event EventHandler StopObservation;

/// <summary>
/// Subscription Types
/// </summary>
public enum SubscriptionType
{
Event,
Expand Down
Loading

0 comments on commit a1118a8

Please sign in to comment.