-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
130 lines (110 loc) · 3.57 KB
/
app.js
File metadata and controls
130 lines (110 loc) · 3.57 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
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
var util = require('util');
var express = require('express');
var app = express();
// Used to make requests to the BOS API
var request = require('request');
// Expose our public static folder
app.use(express.static('public'));
// We use Jade for our views
app.set('view engine', 'jade');
// Setup sessions so we can store the auth token
var session = require('express-session')
app.use(session({
secret: '2HD0mWix5CBNoa8szqqTbZu13IBdk2',
resave: false,
saveUninitialized: true
}))
// Our custom app configs
// Contains Oauth2 key/secret
var config = require('./config_example.json');
var oauth2 = require('simple-oauth2')({
clientID: config.client_id,
clientSecret: config.client_secret,
site: 'https://api.buildingos.com',
authorizationPath: '/o/authorize/',
tokenPath: '/o/token/',
revocationPath: '/oauth2/revoke/'
});
// Authorization uri definition
var authorization_uri = oauth2.authCode.authorizeURL({
redirect_uri: 'http://localhost:3000/callback',
scope: 'read',
state: 'simple_state'
});
// Initial page redirecting to BuildingOS
app.get('/auth', function (req, res) {
res.redirect(authorization_uri);
});
// Callback service parsing the authorization token and asking for the access token
app.get('/callback', function (req, res) {
var code = req.query.code;
console.log('/callback');
console.log(req.query);
oauth2.authCode.getToken({
code: code,
redirect_uri: 'http://localhost:3000/callback'
}, saveToken);
function saveToken(error, result) {
console.log('result', util.inspect(result));
console.log('error', util.inspect(error));
if (error) {
console.log('Access Token Error', error.message);
}
else {
// stash the token in the user session
var auth = oauth2.accessToken.create(result);
console.log('Returned Auth Token', util.inspect(auth.token));
req.session.access_token = auth.token.access_token;
res.redirect('/buildings');
}
}
});
// root of the app. Just has a link to kick off the auth process
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!'});
});
// Lists the buildings for the current user
app.get('/buildings', function (req, res){
console.log('Session Auth Token', req.session.access_token);
var buildings = [];
var options = {
url: 'https://api.buildingos.com/buildings',
headers: {
'Authorization': 'Bearer ' + req.session.access_token
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
buildings = JSON.parse(body).data;
res.render('buildings', {title: 'Bulidings', buildings: buildings});
}
});
});
// Experiment with Chart.js
app.get('/chart_demo', function (req, res){
res.render('chart_demo', {title: 'Demo'});
});
// Experiment with generating data readings for charting demo
app.get('/data', function (req, res){
var moment = require('moment');
require('moment-range');
var start = new Date(2014, 1, 1);
var end = new Date(2015, 1, 1);
var timespan = moment.range(start, end);
var readings = [];
timespan.by('months', function(moment) {
var ts = moment.format();
var vals = {'a':1, 'b': 2, 'c': 3};
var ts_reading = {};
ts_reading[ts] = vals;
readings.push(ts_reading);
});
res.json({"data": readings});
});
// starting up the app and logging the address/port
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
process.env.DEBUG=true;
console.log('Example app listening at http://%s:%s', host, port);
});