-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Protocol client abstraction #9
Open
marzi333
wants to merge
2
commits into
tum-esi:main
Choose a base branch
from
marzi333:protocol-client-abstraction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,33 @@ | ||||||
// See https://aka.ms/new-console-template for more information | ||||||
// See https://aka.ms/new-console-template for more informatio5n | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
using WoT; | ||||||
using WoT.Definitions; | ||||||
using WoT.Implementation; | ||||||
|
||||||
SimpleHTTPConsumer consumer = new SimpleHTTPConsumer(); | ||||||
ThingDescription td = await consumer.RequestThingDescription("http://plugfest.thingweb.io:8083/smart-coffee-machine"); | ||||||
SimpleConsumedThing? consumedThing = await consumer.Consume(td) as SimpleConsumedThing; | ||||||
if(consumedThing != null) await consumedThing.WriteProperty<int>("availableResourceLevel", 100 ,new InteractionOptions { uriVariables = new Dictionary<string, object> { { "id", "water" } } }); | ||||||
Consumer consumer = new Consumer(); | ||||||
consumer.AddClient(new SimpleHTTPClient()); | ||||||
ThingDescription coffeeMachineTD = await consumer.RequestThingDescription("http://plugfest.thingweb.io:8083/smart-coffee-machine"); | ||||||
ThingDescription counterTD = await consumer.RequestThingDescription("http://plugfest.thingweb.io:8083/counter"); | ||||||
ConsumedThing coffeeMachineConsumedThing = consumer.Consume(coffeeMachineTD) as ConsumedThing; | ||||||
ConsumedThing counterConsumedThing = consumer.Consume(counterTD) as ConsumedThing; | ||||||
Action<IInteractionOutput<int>> countListener = async (IInteractionOutput<int> count) => { Console.WriteLine(await count.Value()); }; | ||||||
if (coffeeMachineConsumedThing != null) | ||||||
{ | ||||||
//ReadProperty test | ||||||
int count = await counterConsumedThing.ReadProperty<int>("count", new InteractionOptions { uriVariables = new Dictionary<string, object> { { "step", 5 } } }).Result.Value(); | ||||||
Console.WriteLine("Count = " + count); | ||||||
|
||||||
|
||||||
//ObserveProperty test | ||||||
ISubscription countObserver = await counterConsumedThing.ObserveProperty<int>("count", countListener); | ||||||
|
||||||
//InvokeAction test | ||||||
IInteractionOutput actionOutput = await counterConsumedThing.InvokeAction("increment", options: new InteractionOptions { uriVariables = new Dictionary<string, object> { { "step", 5 } } }) ; | ||||||
|
||||||
//WriteProperty | ||||||
await coffeeMachineConsumedThing.WriteProperty<int>("availableResourceLevel", 100, new InteractionOptions { uriVariables = new Dictionary<string, object> { { "id", "water" } } }); | ||||||
|
||||||
} | ||||||
|
||||||
|
||||||
Console.WriteLine("Done"); | ||||||
Console.ReadLine(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Schema; | ||
using System; | ||
using System.Text; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Tavis.UriTemplates; | ||
using WoT.Definitions; | ||
using WoT.Errors; | ||
using System.Threading; | ||
using WoT.ProtocolBindings; | ||
|
||
namespace WoT.Implementation | ||
{ | ||
/// <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 SimpleHTTPClient : IProtocolClient | ||
{ | ||
private readonly JsonSerializer _serializer; | ||
public readonly HttpClient httpClient; | ||
|
||
public string Scheme { get; } = "http"; | ||
|
||
/// <inheritdoc/> | ||
public SimpleHTTPClient() | ||
{ | ||
httpClient = new HttpClient(); | ||
_serializer = new JsonSerializer(); | ||
|
||
} | ||
|
||
|
||
public async Task<Stream> SendGetRequest(Form form) | ||
{ | ||
|
||
HttpResponseMessage interactionResponse = await httpClient.GetAsync(form.Href); | ||
interactionResponse.EnsureSuccessStatusCode(); | ||
Stream responseStream = await interactionResponse.Content.ReadAsStreamAsync(); | ||
return responseStream; | ||
|
||
} | ||
|
||
|
||
|
||
public async Task<Stream> SendGetRequest(Form form, CancellationToken cancellationToken) | ||
{ | ||
HttpResponseMessage interactionResponse = await httpClient.GetAsync(form.Href, cancellationToken); | ||
interactionResponse.EnsureSuccessStatusCode(); | ||
Stream responseStream = await interactionResponse.Content.ReadAsStreamAsync(); | ||
return responseStream; | ||
|
||
} | ||
public async Task<Stream> SendPostRequest(Form form) | ||
{ | ||
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, form.Href); | ||
var interactionResponse = await httpClient.SendAsync(message); | ||
interactionResponse.EnsureSuccessStatusCode(); | ||
Stream responseStream = await interactionResponse.Content.ReadAsStreamAsync(); | ||
return responseStream; | ||
} | ||
public async Task<Stream> SendPostRequest<U>(Form form, U parameters) | ||
{ | ||
|
||
string payloadString = JsonConvert.SerializeObject(parameters); | ||
StringContent payload = new StringContent(payloadString, Encoding.UTF8, "application/json"); | ||
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, form.Href); | ||
message.Content = payload; | ||
var interactionResponse = await httpClient.SendAsync(message); | ||
interactionResponse.EnsureSuccessStatusCode(); | ||
Stream responseStream = await interactionResponse.Content.ReadAsStreamAsync(); | ||
return responseStream; | ||
} | ||
|
||
public async Task SendPutRequest<T>(Form form, T value) | ||
{ | ||
string payloadString = JsonConvert.SerializeObject(value); | ||
var payload = new StringContent(payloadString, Encoding.UTF8, "application/json"); | ||
var message = new HttpRequestMessage(HttpMethod.Put, form.Href); | ||
message.Content = payload; | ||
HttpResponseMessage interactionResponse = await httpClient.SendAsync(message); | ||
interactionResponse.EnsureSuccessStatusCode(); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
public async Task<ThingDescription> RequestThingDescription(string url) | ||
{ | ||
Uri tdUrl = new Uri(url); | ||
HttpResponseMessage tdResponse = await httpClient.GetAsync(tdUrl); | ||
tdResponse.EnsureSuccessStatusCode(); | ||
Console.WriteLine($"Info: Fetched TD from {url} successfully"); | ||
Console.WriteLine($"Info: Parsing TD"); | ||
HttpContent body = tdResponse.Content; | ||
string tdData = await body.ReadAsStringAsync(); | ||
TextReader reader = new StringReader(tdData); | ||
ThingDescription td = _serializer.Deserialize(reader, typeof(ThingDescription)) as ThingDescription; | ||
Console.WriteLine($"Info: Parsed TD successfully"); | ||
return td; | ||
} | ||
|
||
public async Task<ThingDescription> RequestThingDescription(Uri tdUrl) | ||
{ | ||
if (tdUrl.Scheme != "http") throw new Exception($"The protocol for accessing the TD url {tdUrl.OriginalString} is not HTTP"); | ||
Console.WriteLine($"Info: Fetching TD from {tdUrl.OriginalString}"); | ||
HttpResponseMessage tdResponse = await httpClient.GetAsync(tdUrl); | ||
tdResponse.EnsureSuccessStatusCode(); | ||
Console.WriteLine($"Info: Fetched TD from {tdUrl.OriginalString} successfully"); | ||
Console.WriteLine($"Info: Parsing TD"); | ||
HttpContent body = tdResponse.Content; | ||
string tdData = await body.ReadAsStringAsync(); | ||
TextReader reader = new StringReader(tdData); | ||
ThingDescription td = _serializer.Deserialize(reader, typeof(ThingDescription)) as ThingDescription; | ||
Console.WriteLine($"Info: Parsed TD successfully"); | ||
return td; | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment should be removed and how new bindings can be implemented should be explained