Sparksharp

View the Project on GitHub Brbb/sparksharp

Welcome to Sparksharp.

This should be a good starting point for all .NET developers involved in Spark Core projects. Spark Sharp provides REST wrappers for direct Spark API calls and event parsers to make your app running clean and smooth.

The Spark Sharp library is built using Mono and needs RestSharp and Json.NET to run, so add to your projects libraries starting from your beloved development environment.

Login and token retrive:


string username = "myUser";
string password = "myPassword";

var baseUrl = "https://api.particle.io";
var accessTokenController = new AccessTokenController (baseUrl);

// Login, if a token exists and it's not expired use it.
var accessTokens = accessTokenController.ListTokensAsync (username, password).Result;
var userToken = accessTokens.FirstOrDefault (t => t.Client == "user");

if (userToken == null) {
	var authResponse = accessTokenController.GenerateAccessTokenAsync (username, password).Result;
	userToken = new AccessToken {
		Token = authResponse.Access_Token,
		Expires_At = DateTime.UtcNow.AddSeconds (authResponse.Expires_In).ToString ("o"),
		Client = "user"
	};
}

Retrieve devices and perform operations:


var deviceController = new DeviceController (baseUrl);
var devices = deviceController.ListDevicesAsync (userToken.Token).Result;

var device = devices.FirstOrDefault ();

if (device != null && device.Connected) {
	var cloudController = new CloudController (baseUrl, userToken, device);

	Task.Run (async () => {

		var deviceInfo = await deviceController.GetDeviceInfoByIdAsync (userToken.Token, device.Id);
		var testFunction = deviceInfo.Functions.FirstOrDefault ();
		var fResp = await cloudController.CallFunctionAsync (testFunction, "test");

		if(fResp != null)
			Console.WriteLine (fResp.Return_Value);	
		
	}).Wait();
}
  

Server Sent Events:


var sseManager = new SSECore (baseUrl,userToken.Token);
sseManager.SSEPublished += (eve) => {
	Console.WriteLine (eve.Info.Data);
	};
Task.Run (() => {
	sseManager.GetStreamOfEvents ("Dummy");
	});

Support or Contact

Having trouble? Feel free to open an issue on the Github Project Page.