Hi,
Is it possible to run the Spark CLI from nodejs?
Because I have a javascript variable ( the coreId ) that I want to pass to the bash command line. But that’s not working fine. Or is there perhaps a nodejs module that can fix this problem.
Regards.
Albert.
@elnavdo ,
Spark-Cli is written on Node.js and you should be able to adapt and call the functions written on node.js (i believe)?
Hi Kenneth,
Is there a specific Spark CLI module that I can use in nodejs?
For instance var http = require(“http”); but then for the Spark CLI ?
Regards.
Albert.
I have NO idea (Total noob in web development )
Are you familiar with node.js? You can look at the source code here: https://github.com/spark/spark-cli
I believe you can use the code and call them from your own.
We basically install them via npm
… Sorry if it’s confusing…
A web savvy guy can nail this question in 1 second
Hi Kenneth,
Thanks for your fast response. I have looked at the link you mentioned. I don’t have the answer, but I’m a little step further now.
Regards.
Albert.
zach
June 3, 2014, 2:01pm
6
There are also other Node.js wrappers for Spark. For instance:
https://www.npmjs.org/package/sparky
1 Like
Hi Zach,
Thanks a lot for your answer. In the meantime we have created a server based on nodejs (Ubuntu) and we are using the spark.function for our application.
Spark.function(“flashuInt32”, uInt32);
By my collegue it’s working fine, but not for me. I get the following response back on my server:
Successfull flashuInt32
{ code: 400,
error: ‘invalid_request’,
error_description: ‘Error: invalid json’ }
Our servers are exactly the same. When we do the test with the same spark device at the same time and both servers are connected, he gets the successful response and I don’t.
Do you perhaps know the answer to this problem?
Regards.
Albert.
zach
June 16, 2014, 7:11pm
8
Hmm. Can you share the code that you’re using on the server side? @dave might have some ideas
1 Like
Dave
June 16, 2014, 7:18pm
9
Heya!
If you’re looking for the Node.js code that makes API calls in the spark-cli, look no further than:
https://github.com/spark/spark-cli/blob/master/js/lib/ApiClient.js#L480
Thanks!
David
1 Like
Hi Zach, Dave,
This is in the server side
setTimeout(doDuikelFunctions(), 1000);
function doDuikelFunctions() {
console.log("DuikelFunctions");
duikelFunction(coreId, "flashuInt32", "0:3"); // ASCII representation of the number
duikelFunction(coreId, "flashuInt32", "1:110099"); // ASCII representation of the first code
duikelFunction(coreId, "flashuInt32", "2:266771"); // ASCII representation of the second code
duikelFunction(coreId, "flashuInt32", "3:376873"); // ASCII representation of the checksum
}
function duikelFunction(coreId, functionName, funcParam) {
console.log('callFunction for user: ' + " " + coreId + ", " + functionName + ", " + funcParam + ", " + accessToken);
request({
uri: "https://api.spark.io/v1/devices/" + coreId + "/" + functionName,
method: "POST",
form: {
arg: funcParam,
access_token: accessToken
},
json: true
},
function (error, response, body) {
if (error) {
console.log("Error flashuInt32");
console.log(error);
}
else {
console.log("Successfull flashuInt32");
console.log(body);
}
});
}
And this is in the SparkCore:
//Register the Duikel functions
Spark.function("flashuInt32", uInt32);
uint8_t getPosition(String args) {
String position = args.substring(0,args.indexOf(":"));
return (uint8_t)position.toInt();
}
int32_t getValue(String args) {
String value = args.substring(args.indexOf(":")+1);
return (int32_t)value.toInt();
}
int uInt32(String args) {
return getPosition(args);
//return getValue(args);
}
Regards.
Albert.
Dave
June 16, 2014, 8:36pm
11
Not sure if this is related, but
setTimeout(doDuikelFunctions(), 1000);
should be:
setTimeout(doDuikelFunctions, 1000);
Hi Dave,
That makes no difference.
But what I see in Wireshark is this:
1840 79.472105000 54.86.91.207 192.168.2.3 TCP 54 [TCP
ACKed unseen segment] https > 58791 [ACK] Seq=4475 Ack=958 Win=23360 Len=0
Detailed information:
[This frame ACKs a segment we have not seen]
Does this say anything to you?
Regards.
Albert.
Hi Dave,
I have found the problem.
I didn’t have node module “request” installed.
After installing with npm install request it’s working fine now.
The problem is, that javascript itself doesn’t come with this fault.
Anyway, thanks a lot for your help.
Regards.
Albert.
2 Likes
For future reference use npm install --save
That will automatically add the module to the package.json file so you can ignore the dependencies in node_modules
(its good practice to do a touch .gitignore && echo 'node_modules >> .gitignore when you make a new app with node, it keeps your git repos smaller)
Then when you’re getting started do a simple npm install which will read the .json file and automatically grab all of your dependencies.
2 Likes