trying to persist the db again

This commit is contained in:
Martin Donnelly 2015-11-09 13:52:03 +00:00
parent e2b1283daa
commit 84880116ce
10 changed files with 39141 additions and 76 deletions

View File

@ -2,6 +2,7 @@
<configuration default="false" name="bin/www" type="NodeJSConfigurationType" factoryName="Node.js" path-to-node="/usr/local/bin/node" path-to-js-file="bin/www" working-dir="$PROJECT_DIR$">
<envs>
<env name="DEBUG" value="silvrapi:*" />
<env name="DB_HOME" value="/Users/martin" />
</envs>
<browser url="http://localhost:3000/" />
<method />

View File

@ -1,8 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="8a985735-5aff-47ce-a55f-2957d4f9c5f9" name="Default" comment="trying to persist the db">
<list default="true" id="8a985735-5aff-47ce-a55f-2957d4f9c5f9" name="Default" comment="trying to persist the db again">
<change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/public/javascripts/temp.js" />
<change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/public/lib/angular.js" />
<change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/public/lib/jquery.js" />
<change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/routes/weight.js" />
<change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/views/temp.ejs" />
<change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/views/weight.ejs" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/runConfigurations/bin_www.xml" afterPath="$PROJECT_DIR$/.idea/runConfigurations/bin_www.xml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/app.js" afterPath="$PROJECT_DIR$/app.js" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/routes/temp.js" afterPath="$PROJECT_DIR$/routes/temp.js" />
</list>
<ignored path="silvrapi.iws" />
@ -24,75 +32,91 @@
</component>
<component name="FileEditorManager">
<leaf>
<file leaf-file-name="package.json" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/package.json">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-0.0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding>
<marker date="1447026540000" expanded="true" signature="0:378" placeholder="{&quot;name&quot;: &quot;silvrapi&quot;...}" />
<marker date="1447026540000" expanded="true" signature="78:113" placeholder="{&quot;start&quot;: &quot;node ./bin/www&quot;...}" />
<marker date="1447026540000" expanded="true" signature="133:323" placeholder="{&quot;body-parser&quot;: &quot;~1.13.2&quot;...}" />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="www" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/bin/www">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="46" column="3" selection-start-line="46" selection-start-column="3" selection-end-line="46" selection-end-column="3" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="index.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/routes/index.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="5" column="30" selection-start-line="5" selection-start-column="30" selection-end-line="5" selection-end-column="30" />
<caret line="5" column="46" selection-start-line="5" selection-start-column="2" selection-end-line="5" selection-end-column="46" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="app.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/app.js">
<file leaf-file-name="temp.ejs" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/views/temp.ejs">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-11.875">
<caret line="59" column="27" selection-start-line="0" selection-start-column="0" selection-end-line="92" selection-end-column="7" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="weight.ejs" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/views/weight.ejs">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-10.769231">
<caret line="56" column="0" selection-start-line="56" selection-start-column="0" selection-end-line="56" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="angular.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/public/lib/angular.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="36" column="27" selection-start-line="36" selection-start-column="27" selection-end-line="36" selection-end-column="27" />
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="time.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/routes/time.js">
<file leaf-file-name="jquery.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/public/lib/jquery.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="5" column="23" selection-start-line="5" selection-start-column="1" selection-end-line="5" selection-end-column="23" />
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="btc.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/routes/btc.js">
<file leaf-file-name="bower.json" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/bower.json">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-3.75">
<caret line="6" column="3" selection-start-line="6" selection-start-column="3" selection-end-line="6" selection-end-column="3" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="temp.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/public/javascripts/temp.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="3" column="0" selection-start-line="3" selection-start-column="0" selection-end-line="3" selection-end-column="0" />
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="temp.js" pinned="false" current-in-tab="true">
<file leaf-file-name="temp.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/routes/temp.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="1.2880921">
<caret line="79" column="11" selection-start-line="79" selection-start-column="11" selection-end-line="79" selection-end-column="11" />
<state vertical-scroll-proportion="0.0">
<caret line="73" column="2" selection-start-line="73" selection-start-column="2" selection-end-line="75" selection-end-column="3" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="weight.js" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/routes/weight.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.57771665">
<caret line="77" column="24" selection-start-line="77" selection-start-column="24" selection-end-line="77" selection-end-column="24" />
<folding />
</state>
</provider>
@ -108,22 +132,13 @@
</provider>
</entry>
</file>
<file leaf-file-name="users.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/routes/users.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="9" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="9" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="JavaScript File" />
<option value="EJS" />
</list>
</option>
</component>
@ -139,9 +154,18 @@
<option value="$PROJECT_DIR$/routes/btc.js" />
<option value="$PROJECT_DIR$/app.js" />
<option value="$PROJECT_DIR$/routes/temp.js" />
<option value="$PROJECT_DIR$/public/javascripts/temp.js" />
<option value="$PROJECT_DIR$/bower.json" />
<option value="$PROJECT_DIR$/views/temp.ejs" />
<option value="$PROJECT_DIR$/views/weight.ejs" />
<option value="$PROJECT_DIR$/routes/weight.js" />
</list>
</option>
</component>
<component name="JsBowerSettings">
<exe-path>/usr/local/bin/bower</exe-path>
<config-path>$PROJECT_DIR$/bower.json</config-path>
</component>
<component name="JsBuildToolGruntFileManager" detection-done="true" />
<component name="JsBuildToolPackageJson" detection-done="true">
<package-json value="$PROJECT_DIR$/package.json" />
@ -240,6 +264,42 @@
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="public" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="lib" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="public" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="javascripts" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
@ -254,6 +314,76 @@
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="app" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="bower_components" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="jquery" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="app" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="bower_components" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="jquery" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="dist" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="silvrapi" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="app" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="bower_components" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="angular" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
</subPane>
</pane>
<pane id="Scratches" />
@ -261,7 +391,7 @@
</panes>
</component>
<component name="PropertiesComponent">
<property name="settings.editor.selected.configurable" value="preferences.pluginManager" />
<property name="settings.editor.selected.configurable" value="Settings.JavaScript.Bower" />
<property name="settings.editor.splitter.proportion" value="0.2" />
<property name="WebServerToolWindowFactoryState" value="true" />
<property name="nodejs_interpreter_path" value="/usr/local/bin/node" />
@ -271,6 +401,12 @@
<property name="restartRequiresConfirmation" value="false" />
<property name="JavaScriptPreferStrict" value="true" />
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/bower.json" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/public/lib" />
</key>
</component>
<component name="RunManager" selected="Node.js.bin/www">
<configuration default="true" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application">
@ -361,7 +497,13 @@
<option name="project" value="LOCAL" />
<updated>1447067474637</updated>
</task>
<option name="localTasksCounter" value="6" />
<task id="LOCAL-00006" summary="trying to persist the db again">
<created>1447068685007</created>
<option name="number" value="00006" />
<option name="project" value="LOCAL" />
<updated>1447068685007</updated>
</task>
<option name="localTasksCounter" value="7" />
<servers />
</component>
<component name="ToolWindowManager">
@ -400,7 +542,8 @@
<MESSAGE value="debugging" />
<MESSAGE value="closing db to persist&#10;" />
<MESSAGE value="trying to persist the db" />
<option name="LAST_COMMIT_MESSAGE" value="trying to persist the db" />
<MESSAGE value="trying to persist the db again" />
<option name="LAST_COMMIT_MESSAGE" value="trying to persist the db again" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager />
@ -435,22 +578,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/views/index.ejs">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-4.375">
<caret line="7" column="8" selection-start-line="7" selection-start-column="8" selection-end-line="7" selection-end-column="8" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/routes/index.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="5" column="30" selection-start-line="5" selection-start-column="30" selection-end-line="5" selection-end-column="30" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/routes/btc.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
@ -470,15 +597,87 @@
<entry file="file://$PROJECT_DIR$/app.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="36" column="27" selection-start-line="36" selection-start-column="27" selection-end-line="36" selection-end-column="27" />
<caret line="38" column="16" selection-start-line="38" selection-start-column="16" selection-end-line="38" selection-end-column="16" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/routes/index.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="5" column="46" selection-start-line="5" selection-start-column="2" selection-end-line="5" selection-end-column="46" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/views/index.ejs">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-4.375">
<caret line="7" column="8" selection-start-line="7" selection-start-column="8" selection-end-line="7" selection-end-column="8" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/public/javascripts/temp.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/bower.json">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-3.75">
<caret line="6" column="3" selection-start-line="6" selection-start-column="3" selection-end-line="6" selection-end-column="3" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/public/lib/angular.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/public/lib/jquery.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/views/temp.ejs">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-11.875">
<caret line="59" column="27" selection-start-line="0" selection-start-column="0" selection-end-line="92" selection-end-column="7" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/views/weight.ejs">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="-10.769231">
<caret line="56" column="0" selection-start-line="56" selection-start-column="0" selection-end-line="56" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/routes/temp.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="1.2880921">
<caret line="79" column="11" selection-start-line="79" selection-start-column="11" selection-end-line="79" selection-end-column="11" />
<state vertical-scroll-proportion="0.0">
<caret line="73" column="2" selection-start-line="73" selection-start-column="2" selection-end-line="75" selection-end-column="3" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/routes/weight.js">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.57771665">
<caret line="77" column="24" selection-start-line="77" selection-start-column="24" selection-end-line="77" selection-end-column="24" />
<folding />
</state>
</provider>

2
app.js
View File

@ -14,6 +14,7 @@ var users = require('./routes/users');
var timeroute = require('./routes/time');
var btcroute = require('./routes/btc');
var temproute = require('./routes/temp');
var weightroute = require('./routes/weight');
var app = express();
@ -35,6 +36,7 @@ app.use('/', routes);
app.use('/time', timeroute);
app.use('/btc', btcroute);
app.use('/temp',temproute);
app.use('/weight',weightroute);
// catch 404 and forward to error handler
app.use(function(req, res, next) {

View File

29363
public/lib/angular.js vendored Normal file

File diff suppressed because it is too large Load Diff

9190
public/lib/jquery.js vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@ var express = require('express'), fs = require('fs'), sqlite3 = require('sqlite3
var router = express.Router();
var temp_db;
var file = "/user/martind2000/temp.db";
var file = process.env.DB_HOME + '/' + "temp.db";
var exists;
/* GET users listing. */
@ -12,10 +12,14 @@ function createDB() {
if (!fs.existsSync(file)) {
console.log('creating db file');
fs.openSync(file,'w');
temp_db = new sqlite3.Database(file, createTable);
temp_db.close();
}
temp_db = new sqlite3.Database(file);
}
function connectDB() {
@ -63,13 +67,17 @@ router.get('/', function(req, res, next) {
insertTempReading(now.toJSON(),parseFloat(req.query.temp) );
closeDB();
res.writeHead(200, {"ContentType": "application/json"});
//res.send(JSON.stringify(t));
res.end(JSON.stringify(t));
} else
{
res.render('temp', { });
}
res.writeHead(200, {"ContentType": "application/json"});
//res.send(JSON.stringify(t));
res.end(JSON.stringify(t));
});
@ -104,4 +112,4 @@ router.get('/all', function(req, res, next) {
module.exports = router;
//createDB();
createDB();

116
routes/weight.js Normal file
View File

@ -0,0 +1,116 @@
var express = require('express'), fs = require('fs'), sqlite3 = require('sqlite3').verbose();
var router = express.Router();
var weight_db;
var file = process.env.DB_HOME + '/' + "weight.db";
/* GET users listing. */
function createWeightDB() {
console.log('Creating Weight db...');
console.log(file);
if (!fs.existsSync(file)) {
console.log('creating db file');
fs.openSync(file,'w');
weight_db = new sqlite3.Database(file, createWeightTable);
weight_db.close();
}
}
function connectWeightDB() {
"use strict";
console.log('Connect db.');
weight_db = new sqlite3.Database(file);
}
function createWeightTable() {
console.log('Creating weight table...');
weight_db.run('CREATE TABLE IF NOT EXISTS weight (date text, reading real);');
}
function closeWeightDB() {
"use strict";
console.log('Closing db.');
weight_db.close();
}
function insertWeightReading(time, reading) {
console.log('Inserting reading:' + reading);
// var sql = db.prepare('INSERT into temperature(date, reading) VALUES
// (?,?)',[time],[reading]);
weight_db.run('BEGIN TRANSACTION');
weight_db.run('INSERT into weight(date, reading) VALUES (?,?)',[time, reading]);
weight_db.run('END');
}
router.get('/', function(req, res, next) {
var now = new Date();
var t = req.query;
if (req.query != {} && req.query.weight != null)
{
// createDB();
console.log(req.query);
connectWeightDB();
insertWeightReading(now.toJSON(),parseFloat(req.query.weight) );
closeWeightDB();
res.writeHead(200, {"ContentType": "application/json"});
//res.send(JSON.stringify(t));
res.end(JSON.stringify(t));
} else
{
res.render('weight', { });
}
});
router.get('/all', function(req, res, next) {
"use strict";
console.log('Selecint all..');
connectWeightDB();
weight_db.all('SELECT * from weight', function(err, rows) {
var out = [];
console.log(err);
console.log(rows);
rows.forEach(function (row) {
console.log(row.date + ', ' + row.reading);
// var t = ;
// out.push({"date": Date(row.date), "reading":row.reading});
out.push({"date": row.date, "reading":row.reading});
});
res.writeHead(200, {"ContentType": "application/json"});
//res.send(JSON.stringify(t));
res.end(JSON.stringify(out));
closeWeightDB();
});
});
module.exports = router;
createWeightDB();

93
views/temp.ejs Normal file
View File

@ -0,0 +1,93 @@
<!DOCTYPE html>
<html ng-app="Temp">
<head>
<title>Temp</title>
<style>
a[ ng-click ] {
color: #ff00cc;
cursor: pointer;
text-decoration: underline;
}
</style>
<script type="text/javascript" src="lib/jquery.js"></script>
<script type="text/javascript" src="lib/angular.js"></script>
</head>
<body ng-controller="TempController">
<ul>
<li ng-repeat="entry in tempData">
{{ entry }}
</li>
</ul>
<script type="text/javascript">
var app = angular.module( "Temp", []);
app.controller(
"TempController",
function( $scope, tempService) {
$scope.tempData = [];
loadRemoteData();
function applyRemoteData(newData)
{
$scope.tempData = newData;
}
function loadRemoteData() {
tempService.getTemp()
.then(
function(tempData) {
applyRemoteData(tempData);
}
)
}
}
);
app.service(
"tempService",
function($http, $q) {
return({getTemp:getTemp});
function getTemp() {
var request = $http({
method: "get",
url:"/temp/all",
params: {
/* action:"get"*/
}
});
return (request.then(handleSuccess, handleError));
}
function handleError(response) {
if ( !angular.isObject(response.data) || !response.data.message) {
return( $q.reject("An unknown error occured"));
}
return($q.reject(response.data.message));
}
function handleSuccess(response)
{
return(response.data);
}
}
);
</script>
</body>
</html>

93
views/weight.ejs Normal file
View File

@ -0,0 +1,93 @@
<!DOCTYPE html>
<html ng-app="Weight">
<head>
<title>Weight</title>
<style>
a[ ng-click ] {
color: #ff00cc;
cursor: pointer;
text-decoration: underline;
}
</style>
<script type="text/javascript" src="lib/jquery.js"></script>
<script type="text/javascript" src="lib/angular.js"></script>
</head>
<body ng-controller="WeightController">
<ul>
<li ng-repeat="entry in weightData">
{{ entry }}
</li>
</ul>
<script type="text/javascript">
var app = angular.module( "Weight", []);
app.controller(
"WeightController",
function( $scope, weightService) {
$scope.weightData = [];
loadRemoteData();
function applyRemoteData(newData)
{
$scope.weightData = newData;
}
function loadRemoteData() {
weightService.getWeight()
.then(
function(weightData) {
applyRemoteData(weightData);
}
)
}
}
);
app.service(
"weightService",
function($http, $q) {
return({getWeight:getWeight});
function getWeight() {
var request = $http({
method: "get",
url:"/weight/all",
params: {
/* action:"get"*/
}
});
return (request.then(handleSuccess, handleError));
}
function handleError(response) {
if ( !angular.isObject(response.data) || !response.data.message) {
return( $q.reject("An unknown error occured"));
}
return($q.reject(response.data.message));
}
function handleSuccess(response)
{
return(response.data);
}
}
);
</script>
</body>
</html>