February 5, 2015 Leave a comment
I wrote a small C# portable class library (.NET4.5/WP8.1/Win8) that allow users to talk with the Smappee API.
I recently bought a Smappee and I really love the device. It enables me to track the power usage of all my devices throughout the house and I even found some nasty buggers who consume way more than they should. Currently only Android and IPhone users can really benefit from this device (the my.smappee.com webapp is too lightweight to really utilize the full power of the device).
I’m planning on writing a simple Windows Phone 8 app (and perhaps Windows 8) to have some basic information Smappee data on my phone. Unfortunately the API is pretty meagre (you can’t access everything needed), so I won’t be able to recreate the existing Android app. The full API documentation can be found here.
Before you can use the public API, you will first need to request the credentials (client_id and cleint_secret) from Smappee, explained here. Once that is done, you’ll also need to have an active my.smappee.com account, which you can create using the Iphone or Android app (you can’t do this through the website unfortunately). These 4 credentials will be needed to access the API.
Where is the goddamn code?
The actual code for the PCL is hosted here: https://github.com/timdams/Smappee_API_CS
Two projects are added:
- SmappeePrototype: A simple console-app that shows how to use the lib
- SmappeeAPITD: the actual PCL. I use JSON.NET for the parsing, which is the only external dependency.
To use the code, make sure you have all the credentials at hand, you can then start querying the API once you build the PCL and added it to your project:
SmappeeAPI dll = new SmappeeAPI("yourClient_id", "yourClient_secret", "yourmysmappee_username","yourmysmappee_password"); await dll.RetrieveAccessToken(); var serv = await dll.GetServiceLocations(); //Ask consumption of last week of first smappeedevice retrieved from ServiceLocations var resk = await dll.GetConsumption(serv.serviceLocations.serviceLocationId, DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0)), DateTime.Now, SmappeeAPITD.Aggregation.Hourly);
This code was build a few minutes ago and a lot is missing. Specifically:
- No exception handling whatsoever
- The Actuator duration can’t be set, because the API has a strange way of doing this (you can give only the value 300,900,1800 or 3600 , specifying the time in seconds the actuator
should be turned on. Any other value results in turning on for an undetermined period of time. )
- No logic is added to refresh the token once it’s expired.