Spark Sharp for C# developers

Hi everybody,
I just started a little project on my own to make Spark Core working for C# developers after some comments/posts about that.

Take a look to Spark Sharp and feel free to add requests or issues.

I’m building a little iOS app too but it’s too buggy to make it work and share on Git, but we could talk about it (I’m not an Obj-C dev, I’m using Xamarin).

6 Likes

Very cool, thanks for sharing! Parsing server sent events in C# can be daunting, so I’m sure this will help lots of people :slight_smile:

Thanks,
David

It’s just a draft, but async SSE could be a “killer” feature. I’ll keep you in touch!

1 Like

Hi everybody,

a little update about the new features:

  1. new HTTP verbs to cover mostly all the Spark API:

  2. CallFunctionAsync for simple functions with no args

  3. GetVariableAsync for variable values

  4. ListTokensAsync, DeleteTokenAsync, PostAuthAsync for Authorization management.

  5. All the related Response type to wrap JSON data.

Configuration:

  1. new idea about configuration, mostly saved on an embedded xml file.

TO DOs:

  1. Fix the DELETE
  2. Add arguments to the CallFunctionAsync
  3. Find a way to list all the functions/variables on the core (Does it exist?).
  4. Build some local storage (Access / ADO .NET / Sqlite).
  5. Use SSE events properly (it’s just a good start)

Other Ideas?

1 Like

Hi @Barabba I’ve been playing with Spark and C# and this is how I get all variables and functions. First I get the devices and if they are connected I update my device object with data on variables and functions. I don’t get why a device listed by …/devices? call are different from the device listed by …/devices/[deviceID]? but I just parse out the significant device data from the second call (variables, functions etc).

using (HttpClient hc = new HttpClient())
{
string json = hc.GetStringAsync(“https://api.spark.io/v1/devices?access_token=” + _accessToken.token).Result;
devices = JsonConvert.DeserializeObject<List>(json);
foreach (Device device in devices)
{
if (device.connected)
{
device.accessToken = _accessToken;
json = hc.GetStringAsync(“https://api.spark.io/v1/devices/” + device.id + “?access_token=” + device.accessToken.token).Result;
Device tmpDevice = JsonConvert.DeserializeObject(json);
device.variables = tmpDevice.variables == null ? new Dictionary<string,string>() : tmpDevice.variables;
device.functions = tmpDevice.functions == null ? new List() : tmpDevice.functions;
device.cc3000_patch_version = tmpDevice.cc3000_patch_version;
}
}
}

Nice,
I’ll try it and let you know. Does the REST call return function arguments too?

@Barabba No, as far as I understand I just supports one argument and that’s a “args=string”? I don’t know the supported max length of the parameter value (if you can use JSON to turbo charge it). The way I see it you don’t really have any way of saying how your function is exposed, it’s just magic mapping the “args” parameter to your function (not like Web API). I need to do more testing (in lack of documentation :wink: ).

I have written code (in a C# Spark API of my own, before I saw yours) that:

  1. Log in and gets all access tokens, creates a new one if needed (can delete old ones)
  2. Gets all devices and their functions and variables
  3. I made a naming convention for humidity, temperature, switches (relays) functions and variables on Spark so that the C# lib finds them and lists them as separate generic virtual sensors etc.
  4. Added naming support so that one can give a temp sensor a name like “bathroom” and then check for the temp in the “bathroom” with a generics call regardless of where and how that sensor is connected

So I kind of took a different approach then you I think? It’s not on Github but I’d be happy to share code if it is anything you may need that I might have solved. In my API the Spark implementation is just a provider so that I can mix different providers that expose ITemperature devices for instance. Or something compleatly different like a ISun device that expose methods for sunrise and sunset from a weather site or a outdoor light sensor (my current uses yr.no).

Hi @Barabba
This looks great. I have been looking into developing something similar and thanks for doing it. Were you able to write the iPhoe app using Xamarin? I am not an Obj-C dev also and looking to develop iPhone and Android apps to control my spark cores using Xamarin.

Thank you

Maybe we can merge something into a common library! I’ve to build a lot of features (when possible :smile:)
but, I’ve 3 ideas about the function list:

  1. To add a standard API to return the list of all the functions present in the Spark Firmware, maybe a JSon, with an hardcoded naming convention.
  2. Use the spark-cli client and call the spark list command on the server to return the list
  3. Use the Javascript API…

I don’t like 2 and 3 too much…

I’m using Xamarin! But for personal interest maybe I will go on Swift, not ready sure about it.

Can you post a link to your xamarin code? I’d really like to know how you used xamarin to interact with the core.

Thanks

My Xamarin Code is actually a piece of Class Library + a textbox area, nothing trascendental! I will keep you updated if something changes if you’re interested :wink:

how do i use this i dont understand

@Jeffery, this applies here too

Hi there, done some updates and bug fixing(the C# library on the site seems overcomplicated to me)
I need testers :smile:

Thanks

coming a little late to this apparently. Are you building a lib to work with the spark core devices only or spark and photon, also is this meant to be only local or local and web?