-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagDataModule.js
More file actions
62 lines (54 loc) · 1.72 KB
/
MagDataModule.js
File metadata and controls
62 lines (54 loc) · 1.72 KB
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
var async = require('async');
var SensorTag = require('sensortag');
var tempoiq = require('tempoiq');
var sTag;
var tClient;
var config = true;
module.exports = {
getMagData: function getMagData(sensorTag, client, pollPeriod, callback){
sTag = sensorTag;
tClient = client;
setInterval(readData, pollPeriod);
}
}
function reportData(uuid, data, type){
var device = uuid;
var t1 = new Date();
var tempdata = new tempoiq.BulkWrite();
tempdata.push(device, type,
new tempoiq.DataPoint(t1, Number(data)));
console.log(JSON.stringify(tempdata.toJSON()));
tClient.writeBulk(tempdata, function(err) {
if (err) throw err;
});
}
function readData(){
async.series([
function(callback) {
console.log('enableMagnetometer');
sTag.enableMagnetometer(callback);
},
function(callback) {
setTimeout(callback, 1000);
},
function(callback) {
console.log('readMagnetometer');
sTag.readMagnetometer(function(error, x, y, z) {
//console.log('\tx = %d μT', x.toFixed(1));
console.log('\ty = %d μT', y.toFixed(1));
var curMag = y.toFixed(1);
reportData(sTag.uuid, curMag, "magnetometer");
if (config){
reportData("config-" + sTag.uuid, curMag, "magnetometer");
config = false;
}
});
callback();
},
function(callback) {
console.log('disableMagnetometer');
sTag.disableMagnetometer(callback);
}
]
);
}