-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspark.js
96 lines (77 loc) · 2.15 KB
/
spark.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
var request = require('request');
var url = require('url');
var SparkStatus = module.exports = function (opt) {
this.deviceId = opt.deviceId;
this.accessToken = opt.accessToken;
this.queue = [];
};
SparkStatus.prototype.buildStatus = function(build) {
if (build.success) {
this.backlight("00FF00");
this.write(build.repo + "/" + build.branch + "\n" + build.commit_msg);
return;
}
this.backlight("FF0000");
this.write(build.repo + "/" + build.branch + "\n" + build.commit_msg);
};
SparkStatus.prototype.sendQueued = function(cmd, data, cb) {
this.queue.push([cmd, data, cb]);
this.processQueue();
};
SparkStatus.prototype.processQueue = function() {
if (this.sending) { return; }
var item = this.queue.shift();
if (!item) {
return;
}
this.sending = true;
var cmd = item[0];
var data = item[1];
var cb = item[2];
this.send(cmd, data, function(){
if (cb) {
cb.apply(null, arguments);
}
this.sending = false;
this.processQueue();
}.bind(this));
};
SparkStatus.prototype.send = function(cmd, data, cb) {
cmd = cmd || "";
data = data || {};
data.access_token = this.accessToken;
var opts = {
form: data,
json: true
};
var url = "https://api.spark.io/v1/devices/" + this.deviceId + "/" + cmd;
return request.post(url, opts, function(error, response, body){
if (error || response.statusCode != 200 || body.ok === false) {
var err = error || new Error(body.error);
return cb && cb(err, body);
}
if (cb) {
cb(null, body.return_value);
}
});
};
/*
spark.write(message, cb);
message: string // one or two lines, 16 chars wide
cb: undefined or function(err, value){}
see http://www.adafruit.com/datasheets/HD44780.pdf for special char codes
*/
SparkStatus.prototype.write = function(message, cb) {
this.send('printLCD', {params: message}, cb);
};
/*
spark.backlight(hex, cb);
hex: 'FF00FF' hex color code
cb: undefined or function(err, value){}
*/
SparkStatus.prototype.backlight = function(hex, cb) {
this.send('backlight', {params: hex}, cb);
};
SparkStatus.prototype.testBacklight = function(cb) {
this.send('testLight', {}, cb);
};