/
index.js
152 lines (130 loc) · 3.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var exec = require('child_process').exec;
var child;
function Sparky(config) {
// If no config obj exists, make one
if (!config) {
config = {};
}
// Check the process.env if values aren't passed in the config
if (!config.deviceId && process.env.SPARK_DEVICE_ID) {
config.deviceId = process.env.SPARK_DEVICE_ID;
}
if (!config.token && process.env.SPARK_TOKEN) {
config.token = process.env.SPARK_TOKEN;
}
// Bail if we don't have config values
if (!config.deviceId) {
console.log('Device id not found.');
process.exit(1);
}
if (!config.token) {
console.log('Spark token not found.');
process.exit(1);
}
this.config = config;
this.pinThrottle = {};
}
Sparky.prototype = {
debug: function() {
if (this.config.debug) {
console.log.apply(console, arguments)
}
},
_command: function(command, params, callback) {
params = params || '';
// Set a throttle if no throttle is set for the current args
if (this.nextThrottle && !this.pinThrottle[params]) {
this.pinThrottle[params] = Date.now() + this.nextThrottle;
delete this.nextThrottle;
}
// Return early if we have a throttle for this pin combination
if (this.pinThrottle[params] && Date.now() < this.pinThrottle[params]) {
return false;
} else if(this.pinThrottle[params]) {
delete this.pinThrottle[params];
}
command = 'curl https://api.spark.io/v1/devices/' + this.config.deviceId + '/' + command + ' -d access_token=' + this.config.token + ' -d "args=' + params+'"';
this.debug('Running command: ', command);
child = exec(command,
function (error, stdout, stderr) {
this.debug('stdout: ' + stdout);
this.debug('stderr: ' + stderr);
if (error !== null) {
this.debug('exec error: ' + error);
}
if (callback) {
try {
callback(JSON.parse(stdout).return_value);
}
catch (err) {
callback(undefined);
}
}
}.bind(this));
},
/**
* Formats values to expected values,
* 1, true -> 'HIGH'
* 0, false -> 'LOW'
*/
formatDigitalValue: function(value) {
if (value == 'HIGH' || value == 'LOW') {
return value;
} else {
return value ? 'HIGH' : 'LOW';
}
},
digitalWrite: function(pin, value) {
value = this.formatDigitalValue(value);
this._command('digitalwrite', pin + ',' + value);
},
analogWrite: function(pin, value) {
this._command('analogwrite', pin + ',' + value);
},
digitalRead: function(pin, callback) {
this._command('digitalread', pin, callback);
},
analogRead: function(pin, callback) {
this._command('analogread', pin, callback);
},
/**
* Runs a custom spark core command
*/
run: function(command, params, callback) {
this._command(command, params, callback);
},
/**
* Gets a variable from the spark core
*/
get: function(command, callback) {
command = 'curl https://api.spark.io/v1/devices/' + this.config.deviceId + '/' + command + '?access_token=' + this.config.token;
this.debug('Running command: ', command);
child = exec(command,
function (error, stdout, stderr) {
this.debug('stdout: ' + stdout);
this.debug('stderr: ' + stderr);
if (error !== null) {
this.debug('exec error: ' + error);
}
if (callback) {
try {
callback(JSON.parse(stdout));
}
catch (err) {
callback(undefined);
}
}
}.bind(this));
},
/**
* Do not send the request with the same args to the server within x ms
* @param {Integer} Number of seconds to throttle
*/
throttle: function(ms) {
// Set the next throttle value
// The logic to set this actually happens whenever we send the next command
this.nextThrottle = ms;
return this;
}
};
module.exports = Sparky;