Skip to content

Commit

Permalink
API method for hardware sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
kkarczmarczyk committed Jun 17, 2016
1 parent 9d183ee commit 7ce3040
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ jspm_packages
# Optional REPL history
.node_repl_history
/firebase.json
/config.json
31 changes: 25 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ var express = require('express');
var firebase = require('firebase');

var app = express();
var firebase_key = process.env.FIREBASE_KEY || require('./firebase.json').private_key;
var config = {};
var firebase_key = process.env.FIREBASE_KEY || require('./config.json').private_key;
var token = process.env.TOKEN || require('./config.json').token;
var live = {};

console.log(firebase_key);
firebase.initializeApp({
serviceAccount: {
projectId: "979611318308473915",
Expand All @@ -15,11 +15,18 @@ firebase.initializeApp({
databaseURL: "https://project-979611318308473915.firebaseio.com/"
});

firebase.database().ref('/config').on('value', function(data) {
config = data.val();
firebase.database().ref('/live').on('value', function(data) {
live = data.val();
});

app.set('port', (process.env.PORT || 5000));
app.use('/api', function(req, res, next) {
if(req.query.token !== token) {
res.status(403).end('You must specify token');
} else {
next();
}
});

//app.use(express.static(__dirname + '/public'));

Expand All @@ -28,7 +35,19 @@ app.get('/', function(req, res) {
});

app.get('/api', function(req, res) {
res.json(config);
res.json(live);
});

app.get('/api/lap', function(req, res) {
res.end();
if(req.query.ms && live.race && live.user && live.status) {
if(live.status == "stop") {
firebase.database().ref('/live/status/').set("run");
} else {
firebase.database().ref('/race/' + live.race + '/' + live.user + '/').push(req.query.ms);
}

}
});

app.listen(app.get('port'), function() {
Expand Down

0 comments on commit 7ce3040

Please sign in to comment.