-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
174 lines (136 loc) · 4.25 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express()
const Spreadsheet = require('edit-google-spreadsheet')
const BBCMicrobit = require('bbc-microbit')
const pin = 0;
const EVENT_FAMILY = 8888;
const EVENT_VALUE_ANY = 0;
let currentDate = '';
let pointer = 2;
var microbit_;
function getDate() {
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
let day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return day +'-' + month + "-" + year;
}
function getTime() {
let date = new Date();
let hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
let min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
let sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return hour +':' + min;
}
function UpdateGoogleSheet(pointer){
Spreadsheet.load({
debug: false,
spreadsheetId: '1wbHoGoEGDrOX6yZ6pZlM15g07kJkoz0wdEJuCFLtBqs',
worksheetName: 'PillReminder',
oauth2: {
client_id: "228109464162-evmtm8naeia87eqibjp0gt0sni5ngik2.apps.googleusercontent.com",
client_secret: "tdWpkSpxkYnXNRN30g-rEM68",
refresh_token: "1/EcUynf9dQCc5W-9HycjmgJ-Jd_Vq8qsd5nw_mK9rkco"
},
},
function sheetReady(err, spreadsheet) {
if (err) {
throw err;
}
spreadsheet.receive(function(err, rows, info) {
if (err) {
throw err;
}
//console.log(info);
for (let i=2; i<= info.totalRows; i++) {
console.log('data:' + rows[i][1]);
}
console.log(pointer);
if(pointer <= info.totalRows) {
let addData = '{' + '"' + pointer + '"' + ': {' + '"2" : "' + getTime() + '" } }';
spreadsheet.add(JSON.parse(addData));
spreadsheet.send(function(err) {
if(err) throw err;
console.log("Updated Cell");
});
}
});
})
return;
}
console.log('Scanning for microbit');
BBCMicrobit.discover(function(microbit) {
console.log('\tdiscovered microbit: id = %s, address = %s', microbit.id, microbit.address);
microbit.on('disconnect', function() {
console.log('\tmicrobit disconnected!');
process.exit(0);
});
microbit.on('pinDataChange', function(pin, value) {
console.log('\ton -> pin data change: pin = %d, value = %d', pin, value);
if(value == 2) {
if(currentDate != getDate()) {
currentDate = getDate();
console.log(currentDate);
pointer = 2;
}
UpdateGoogleSheet(pointer);
pointer = pointer + 1;
microbit_.writeEvent(19, 8888, function() {
console.log('Alarm is off');
});
}
});
console.log('connecting to microbit');
microbit.connectAndSetUp(function() {
console.log('\tconnected to microbit');
console.log('setting pin %d as input', pin);
microbit.pinInput(pin, function() {
console.log('\tpin set as input');
console.log('setting pin %d as analog', pin);
microbit.pinAnalog(pin, function() {
console.log('\tpin set as analog');
console.log('subscribing to pin data');
microbit.subscribePinData(function() {
console.log('\tsubscribed to pin data');
});
});
});
console.log('subscribing to event family, any event value');
microbit.subscribeEvents(EVENT_VALUE_ANY, EVENT_FAMILY, function() {
console.log('\tsubscribed to micro:bit events of required type');
});
microbit_ = microbit;
});
});
app.set('port', (process.env.PORT || 5000))
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))
// Process application/json
app.use(bodyParser.json())
// Index route
app.get('/', function (req, res) {
res.send('Hello world, I am micro:bit Singapore')
})
app.get('/webhook/', function (req, res) {
res.sendStatus(200);
})
app.post('/webhook/', function (req, res) {
let msg = JSON.parse(JSON.stringify(req.body));
console.log(msg);
microbit_.writeEvent(18, 8888, function() {
console.log('Alarm is on');
});
res.sendStatus(200)
})
// Spin up the server
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'))
})