I’m trying to convert my existing windows application to use openHAB and in if possible to use openhab only. But for doing this gradually I wanted to use a subscription on the events.
I have a starting net application to log these events, but it is in C#.
I wanted to try to convert this to vbnet because mine application is in vbnet.
But it want start in vb. It stops with the line:
Using StreamReader As New StreamReader(Await client.GetStreamAsync(url))
Anyone an Idea. Or maybe a starting point for vbnet ? Or an example in vbnet?
To send command in vb is covered.
C:
namespace ConsoleApp2
{
class Program
{
static async Task Main(string[] args)
{
HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(5);
string url = $“http://localhost:8080/rest/events”;
while (true)
{
try
{
Console.WriteLine(“Establishing connection”);
using (var streamReader = new StreamReader(await client.GetStreamAsync(url)))
{
while (!streamReader.EndOfStream)
{
var message = await streamReader.ReadLineAsync();
Console.WriteLine($“Received message: {message}”);
}
}
}
catch (Exception ex)
{
//Here you can check for
//specific types of errors before continuing
//Since this is a simple example, i’m always going to retry
Console.WriteLine($“Error: {ex.Message}”);
Console.WriteLine(“Retrying in 5 seconds”);
await Task.Delay(TimeSpan.FromSeconds(5));
}
}
}
}
}
vb:
Module Program
Public Sub Main(args As String())
ReadAndDisplayAsync()
End Sub
Async Sub ReadAndDisplayAsync()
Dim client As HttpClient = New HttpClient()
client.Timeout = TimeSpan.FromSeconds(5)
Dim url As String = $"http://localhost:8080/rest/events"
While (True)
Try
Console.WriteLine("Establishing connection")
Using StreamReader As New StreamReader(Await client.GetStreamAsync(url))
'Using streamReader As New StreamReader(await client.GetStreamAsync(url))
While (Not StreamReader.EndOfStream)
Dim message = Await StreamReader.ReadLineAsync()
Console.WriteLine($"Received price update: {message}")
End While
End Using
Catch ex As Exception
Console.WriteLine($"Error: {ex.Message}")
Console.WriteLine("Retrying in 5 seconds")
'await Task.Delay(TimeSpan.FromSeconds(5))
'Task.Delay(TimeSpan.FromSeconds(5))
'Dim result As String = Await WaitAsynchronouslyAsync()
WaitAsynchronouslyAsync()
End Try
End While
End Sub
Public Async Function WaitAsynchronouslyAsync() As Task(Of String)
Await Task.Delay(10000)
Return "Finished"
End Function
End Module