-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathambient.js
84 lines (68 loc) · 2.26 KB
/
ambient.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
var Q = require('q');
var _ = require('lodash');
var tessel = require('tessel');
var ambientlib = require('ambient-attx4');
var climatelib = require('climate-si7005');
var Reportr = require('reportr-api');
var config = require("./config");
console.log("Preparing with port: for climate="+config.ports.climate+", for ambient="+config.ports.ambient);
var climate = climatelib.use(tessel.port[config.ports.climate]);
var ambient = ambientlib.use(tessel.port[config.ports.ambient]);
var ambientReady = false, climateReady = false;
var client = new Reportr({
host: config.host,
auth: config.auth
});
var track = function() {
var properties = {};
return Q.nfcall(ambient.getSoundBuffer.bind(ambient))
.then(function(sounds) {
var sound = _.reduce(sounds, function(n, x) { return n + x; }, 0)/sounds.length;
console.log("sound=", sound);
properties.sound = sound;
return Q.nfcall(ambient.getLightBuffer.bind(ambient));
})
.then(function(lights) {
var light = _.reduce(lights, function(n, x) { return n + x; }, 0)/lights.length;
console.log("light=", light);
properties.light = light;
return Q.nfcall(climate.readTemperature.bind(climate));
})
.then(function(temperature) {
console.log("temperature=", temperature);
properties.temperature = temperature;
return Q.nfcall(climate.readHumidity.bind(climate));
})
.then(function(humidity) {
console.log("humidity=", humidity);
properties.humidity = humidity;
return client.postEvent(config.eventName, properties);
})
.then(function() {
console.log("Sent!");
}, function(err) {
console.log("Error", err);
});
};
var start = function() {
if (!ambientReady || !climateReady) return;
console.log("start tracking with interval", config.interval/1000, "seconds");
setInterval(track, config.interval);
track();
};
climate.on('ready', function () {
console.log("climate is ready");
climateReady = true;
start();
});
ambient.on('ready', function () {
console.log("ambient is ready");
ambientReady = true;
start();
});
ambient.on('error', function (err) {
console.log(err);
});
climate.on('error', function (err) {
console.log(err);
});