-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
84 lines (69 loc) · 2.16 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
var tessel = require('tessel'),
accel = require('accel-mma84').use(tessel.port['A']),
wifi = require('wifi-cc3000'),
date_parts = require('./lib/date_parts.js'),
Door = require('./lib/door.js'),
Logger = require('./lib/logger.js'),
ElasticsearchIndexer = require('./lib/es_indexer.js'),
ClockSync = require('./lib/clock_sync.js');
var wiFiSsid = process.argv[2];
var wiFiPassword = process.argv[3];
var esBaseUrl = process.argv[4];
// Create a logger for logging log messages generated by this application
var logger = new Logger({
esBaseUrl: esBaseUrl
});
// Create an Elasticsearch indexer for indexing visits
var INDEX_NAME = 'visits';
var VISIT_TYPE = 'visit';
var esVisitIndexer = new ElasticsearchIndexer(esBaseUrl, INDEX_NAME, VISIT_TYPE);
// Create a ClockSync object to keep onboard clock in sync with NTP time
new ClockSync(logger);
wifi.on('connect', function(res) {
logger.info("WiFi is connected. IP address = " + res.ip);
});
wifi.on('timeout', function() {
logger.info("WiFi conection timed out. Retrying connection...");
wifi.connect({
ssid: wiFiSsid,
password: wiFiPassword
});
});
wifi.on('disconnect', function() {
logger.info("WiFi is disconnected :( Retrying connection...");
wifi.connect({
ssid: wiFiSsid,
password: wiFiPassword
});
});
var door = new Door();
accel.on('ready', function () {
logger.info('Accelerometer ready');
// // Attempt wifi connection after 10 seconds; for some bizzare
// // reason this delay is required when the board is powered
// // standalone (as opposed to being connected to a computer)
// setTimeout(function() {
// wifi.connect({
// ssid: wiFiSsid,
// password: wiFiPassword
// });
// }, 7 * 1000);
accel.on('data', function (xyz) {
var x = xyz[0];
if (x >= -0.99) {
door.open();
} else {
door.close();
}
});
accel.on('error', function(err) {
logger.info('Error:', err);
});
door.on('visit-start', function() {
logger.info("Visit just started");
});
door.on('visit-end', function(visit) {
logger.info("Visit just ended: " + JSON.stringify(visit));
esVisitIndexer.index(visit);
});
});