mirror of
https://github.com/sstent/node_app.git
synced 2026-01-26 07:02:53 +00:00
adding test code for reading garmin connect
This commit is contained in:
88
app.js
88
app.js
@@ -6,7 +6,7 @@ var fs = require('fs');
|
||||
var path = require('path');
|
||||
var mongo = require('mongodb');
|
||||
var BSON = mongo.BSONPure;
|
||||
var db = require('mongoskin').db('localhost:27017/test');
|
||||
var db = require('mongoskin').db('localhost:27017/test');
|
||||
var testcollection = db.collection('testcollection');
|
||||
var exercisecollection = db.collection('exercisecollection');
|
||||
var expressocollection = db.collection('expressocollection');
|
||||
@@ -16,11 +16,13 @@ var formidable = require('formidable');
|
||||
var xml2js = require('xml2js');
|
||||
var parser = new xml2js.Parser();
|
||||
var dateFormat = require('dateformat');
|
||||
var nodeGarminConnect = require('./node-garmin-connect');
|
||||
var garminClient = new nodeGarminConnect();
|
||||
|
||||
var app = require('http').createServer(function handler(request, response) {
|
||||
|
||||
|
||||
console.log('request starting...;' + request.url);
|
||||
|
||||
|
||||
switch(request.url) {
|
||||
case '/upload':
|
||||
var form = new formidable.IncomingForm(),
|
||||
@@ -52,13 +54,13 @@ var app = require('http').createServer(function handler(request, response) {
|
||||
files.push([field, file]);
|
||||
});
|
||||
form.on('end', function() {
|
||||
console.log('-> upload done');
|
||||
console.log('-> upload done');
|
||||
response.writeHead(200, {'content-type': 'text/plain'});
|
||||
response.write('received fields:\n\n '+util.inspect(fields));
|
||||
response.write('\n\n');
|
||||
response.write('received files:\n\n '+util.inspect(files));
|
||||
});
|
||||
|
||||
|
||||
form.parse(request, function(err, fields, files) {
|
||||
console.log('-> uploaded -' + files.upload.path);
|
||||
fs.readFile(files.upload.path, function(err, data) {
|
||||
@@ -68,7 +70,7 @@ var app = require('http').createServer(function handler(request, response) {
|
||||
console.log('Done');
|
||||
|
||||
//hrdatacollectionJSON.stringify(result)
|
||||
var data = JSON.stringify(result)
|
||||
var data = JSON.stringify(result);
|
||||
var buf1 = new Buffer(12);
|
||||
var dataid = JSON.parse(data).Activities.Activity.Id;
|
||||
var datadate = Date.parse(dataid);
|
||||
@@ -88,7 +90,7 @@ var app = require('http').createServer(function handler(request, response) {
|
||||
var filePath = '.' + request.url;
|
||||
if (filePath == './')
|
||||
filePath = './index.html';
|
||||
|
||||
|
||||
var extname = path.extname(filePath);
|
||||
var contentType = 'text/html';
|
||||
switch (extname) {
|
||||
@@ -100,7 +102,7 @@ var app = require('http').createServer(function handler(request, response) {
|
||||
break;
|
||||
}
|
||||
path.exists(filePath, function(exists) {
|
||||
|
||||
|
||||
if (exists) {
|
||||
fs.readFile(filePath, function(error, content) {
|
||||
if (error) {
|
||||
@@ -118,10 +120,10 @@ var app = require('http').createServer(function handler(request, response) {
|
||||
response.end();
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
}).listen(3000);
|
||||
|
||||
|
||||
|
||||
var io = require('socket.io');
|
||||
io = io.listen(app);
|
||||
io.configure('development', function(){
|
||||
@@ -129,10 +131,10 @@ io.set("transports", ["websocket"]);
|
||||
});
|
||||
|
||||
io.sockets.on('connection', function(socket) {
|
||||
console.log('Client connected');
|
||||
|
||||
console.log('Client connected');
|
||||
|
||||
socket.on('getactivites', function(data) {
|
||||
console.log('getactivites')
|
||||
console.log('getactivites');
|
||||
testcollection.find().toArray(function(err, result) {
|
||||
if (err) throw err;
|
||||
socket.emit('populateactivities', result);
|
||||
@@ -140,23 +142,23 @@ io.sockets.on('connection', function(socket) {
|
||||
});
|
||||
///////////////////////////////////////
|
||||
socket.on('getactivitybyid', function(id) {
|
||||
console.log('getactivitybyid')
|
||||
console.log('getactivitybyid');
|
||||
testcollection.findById(id, function(err, result) {
|
||||
if (err) throw err;
|
||||
socket.emit('populateactivitybyid', result);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
////////////////////////
|
||||
socket.on('addactivity', function(data, docid) {
|
||||
console.log('addactivity' + docid)
|
||||
console.log('addactivity' + docid);
|
||||
if (docid === null) {
|
||||
var document_id = new BSON.ObjectID();
|
||||
}
|
||||
else {
|
||||
var document_id = new BSON.ObjectID(docid);
|
||||
};
|
||||
}
|
||||
//var document_id = new BSON.ObjectID(docid);
|
||||
console.log('inserted BSONID' + document_id);
|
||||
testcollection.update({_id:document_id}, data,{upsert:true} , function(err, result) {
|
||||
@@ -164,40 +166,40 @@ io.sockets.on('connection', function(socket) {
|
||||
exercisecollection.find().toArray(function(err, result) {
|
||||
if (err) throw err;
|
||||
socket.emit('populateexercises', result);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
/////////////////////
|
||||
/////////////////////
|
||||
socket.on('delactivity', function(id) {
|
||||
testcollection.removeById(id,function(err, reply){
|
||||
if (err) throw err;
|
||||
testcollection.find().toArray(function(err, result) {
|
||||
if (err) throw err;
|
||||
socket.emit('populateactivities', result);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
///////////////////
|
||||
socket.on('getexercises', function(data) {
|
||||
console.log('emit exercises')
|
||||
console.log('emit exercises');
|
||||
exercisecollection.find().toArray(function(err, result) {
|
||||
if (err) throw err;
|
||||
socket.emit('populateexercises', result);
|
||||
});
|
||||
});
|
||||
socket.on('updateexercises', function(data, docid) {
|
||||
console.log('updateexecises' + JSON.stringify(data))
|
||||
console.log('updateexecises' + JSON.stringify(data));
|
||||
if (docid == 'undefined') {
|
||||
console.log('edited updateexecises' + JSON.stringify(data))
|
||||
console.log('edited updateexecises' + JSON.stringify(data));
|
||||
exercisecollection.insert(data, function(err, result) {
|
||||
if (err) throw err;
|
||||
exercisecollection.find().toArray(function(err, result) {
|
||||
if (err) throw err;
|
||||
socket.emit('populateexercises', result);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
@@ -208,34 +210,34 @@ io.sockets.on('connection', function(socket) {
|
||||
if (err) throw err;
|
||||
console.log('populateexercises');
|
||||
socket.emit('populateexercises', result);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
});
|
||||
////////////////
|
||||
socket.on('getexpresso', function(data) {
|
||||
console.log('emit expresso')
|
||||
console.log('emit expresso');
|
||||
expressocollection.find().toArray(function(err, result) {
|
||||
if (err) throw err;
|
||||
socket.emit('populateexpresso', result);
|
||||
});
|
||||
});
|
||||
socket.on('updateexpresso', function(data, docid) {
|
||||
|
||||
|
||||
if (docid == 'undefined') {
|
||||
console.log('addedexpresso ' + JSON.stringify(data))
|
||||
console.log('edited updateexpresso' + JSON.stringify(data))
|
||||
console.log('addedexpresso ' + JSON.stringify(data));
|
||||
console.log('edited updateexpresso' + JSON.stringify(data));
|
||||
expressocollection.insert(data, function(err, result) {
|
||||
if (err) throw err;
|
||||
expressocollection.find().toArray(function(err, result) {
|
||||
if (err) throw err;
|
||||
socket.emit('populateexpresso', result);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
console.log('updateexpresso ' + JSON.stringify(data))
|
||||
console.log('updateexpresso ' + JSON.stringify(data));
|
||||
var document_id = new BSON.ObjectID(docid);
|
||||
expressocollection.update({_id:document_id}, data,{upsert:true} , function(err, result) {
|
||||
if (err) throw err;
|
||||
@@ -243,23 +245,23 @@ io.sockets.on('connection', function(socket) {
|
||||
if (err) throw err;
|
||||
console.log('populateexpresoo');
|
||||
socket.emit('populateexpresso', result);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
});
|
||||
////////////////
|
||||
socket.on('gethrdata', function(data) {
|
||||
console.log('emit hrdata')
|
||||
console.log('emit hrdata');
|
||||
hrdatacollection.find().toArray(function(err, result) {
|
||||
if (err) throw err;
|
||||
socket.emit('populatehrdata', result);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
////////////////
|
||||
});
|
||||
|
||||
|
||||
|
||||
////////////////
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user