Skip to content

Instantly share code, notes, and snippets.

@ahmedsa1983
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ahmedsa1983/ef94c5a37ec982a57ab6 to your computer and use it in GitHub Desktop.
Save ahmedsa1983/ef94c5a37ec982a57ab6 to your computer and use it in GitHub Desktop.
Get an event data from a Spark core using NodeJS
/*jslint node: true */
"use strict";
var spark = require('spark');
var fs = require('fs');
spark.on('login', function() {
//Get test event for specific core
spark.getEventStream('MyEvent', '00000000000000000', function(data) {
console.log("New Event: " + JSON.stringify(data) + ". Logging it to file");
fs.appendFile('Carpet1 Sensors Data.txt', data.data + "," +
data.published_at + "," + data.name + "\r\n", function (err) {
if(err)
console.log("File log failure. Error = " + JSON.stringify(err));
else
console.log("File log success");
});
});
});
// Login as usual
spark.login({ username: 'Myemail@gmail.com', password: 'MyPassword'});
@quarterpi
Copy link

I can't test this, but just a shot in the dark...

/*jslint node: true */
"use strict";
var spark = require('spark');
var fs = require('fs');
spark.on('login', function() {
//Get test event for specific core
spark.getEventStream('MyEvent', '00000000000000000', function(data) {
console.log("New Event: " + JSON.stringify(data) + ". Logging it to file");

    var published = new Date(data.published_at); // Create a new Date object with the published_at string

fs.appendFile('Carpet1 Sensors Data.txt', data.data + "," +
    published.toLocalString() + "," + data.name + "\r\n", function (err) {
        if(err)
      console.log("File log failure. Error = " + JSON.stringify(err));
        else
      console.log("File log success");
    });
});

});

// Login as usual
spark.login({ username: 'Myemail@gmail.com', password: 'MyPassword'});

I tried to find documentation on published_at but I couldn't find any. It seems like if you are getting a GMT or UTC string, you could just convert it to a date object and the do the conversion to local time. If it is already a Date object it should be even more simple, but I assume that the solution isn't as simple as it seems to me. What have you already tried? You said that you tried the toLocalString() method... what were the results? Did you get an error?
I'm sorry if this doesn't help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment