Usage code is not up to date and doesn't work with SDK
Closed this issue · 7 comments
Hi, the sample code at https://github.com/tibber/Tibber.SDK.NET doesn't work.
Installing the nuget package manager is not enough to get the code working (or the code needs to be updated to work with the current SDK).
One error I get at several lines is: "Error CS4033 - The 'await' operator can only be used within an async method"
Thanks!
/Johan
Hi @jvmatern. Thanks for reporting this. On which platform are you running it? .NET core or earlier framework? Which version?
Could you share an example of an Error CS4033 you get with details?
Also, note that you need to put the example codes within an async
function. Missing this will result in the errors you are getting. I'll close the ticket for now. Let us know if you need more assistance!
Hi, sorry for late reply but I cannot do this during office hours.
I've been using VS2019 and .NET framework 4.7.2 and tried to implement the code in an C# Windows Application. I hope this answers all questions about the environment.
You are correct in your assumtion that I haven't put the code in an async function. I'm a noob with basic programming skills and hade hoped that the sample code was provided at a completion level where even I could get going. I'm now realizing that the sample code needs higher skill level than basic.
I had also another error message when trying the code, but that one disappeared when I installed the GraphQlClientGenerator. I think that error was in the QueryBuilderExtensions Class, not sure exactly where (I've forgotten). This lead me to the assumption that more nuget installs (than Tibber.SDK) are needed to get the code working.
Update:
The first lines of code now works when I have it in an async function - Thanks!!
We can surely try to improve the example code bits! Thanks for input :)
Hi, your comment about async function was gold, it was so little missing to get the errors cleared.
I'm sure I'm not the only one that gets stuck at the next level by realizing that the data is trapped within the function. Haha, so frustrating. Please, if you or someone makes the code even easier to use then including some code to get the data out of it would be greatly appreciated. I've actually tried to get help from programmers at the office today, but realized that none of them knew how to do it. I will continue my struggle and hope Google can provide help.
Thanks!
static async Task GetDataFromTibber(string accessToken )
{
var userAgent = new ProductInfoHeaderValue("My-home-automation-system", "1.2");
var client = new TibberApiClient(accessToken, userAgent);
var basicData = await client.GetBasicData();
var homeId = basicData.Data.Viewer.Homes.First().Id.Value;
var consumption = await client.GetHomeConsumption(homeId, EnergyResolution.Monthly);
var customQueryBuilder =
new TibberQueryBuilder()
.WithAllScalarFields()
.WithViewer(
new ViewerQueryBuilder()
.WithAllScalarFields()
.WithAccountType()
.WithHome(
new HomeQueryBuilder()
.WithAllScalarFields()
.WithAddress(new AddressQueryBuilder().WithAllFields())
.WithCurrentSubscription(
new SubscriptionQueryBuilder()
.WithAllScalarFields()
.WithSubscriber(new LegalEntityQueryBuilder().WithAllFields())
.WithPriceInfo(new PriceInfoQueryBuilder()
.WithCurrent(new PriceQueryBuilder().WithAllFields())
.WithToday(new PriceQueryBuilder().WithAllFields())
.WithTomorrow(new PriceQueryBuilder().WithAllFields())
)
)
.WithOwner(new LegalEntityQueryBuilder().WithAllFields())
.WithFeatures(new HomeFeaturesQueryBuilder().WithAllFields())
.WithMeteringPointData(new MeteringPointDataQueryBuilder().WithAllFields()), homeId
)
);
var customQuery = customQueryBuilder.Build(); // produces plain GraphQL query text
var result = await client.Query(customQuery);
var Address = result.Data.Viewer.Home.Address;
var Current = result.Data.Viewer.Home.CurrentSubscription.PriceInfo.Current;
var Today = result.Data.Viewer.Home.CurrentSubscription.PriceInfo.Today;
var Tomorrow = result.Data.Viewer.Home.CurrentSubscription.PriceInfo.Tomorrow;
foreach (var d in result.Data.Viewer.Home.CurrentSubscription.PriceInfo.Tomorrow)
{
Console.WriteLine(d.StartsAt + "\t" + d.Energy);
}
}
Readme updated!