Skip to content

Commit a9e8ddc

Browse files
committed
Added 'server' mode
1 parent 03c0465 commit a9e8ddc

7 files changed

Lines changed: 161 additions & 575 deletions

File tree

inst/app/ui/main.js

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -38,95 +38,92 @@ var libs = [
3838
]
3939

4040
define(libs, function($, io, i18next, init, utils) {
41-
$(function() {
4241

43-
var
44-
infoText = {
45-
version: 'v3.0.0-beta.8',
46-
copyright: 'Copyright \u00A9 2015 - 2017 CBS, Statistics Netherlands'
47-
};
42+
var infoText = {
43+
version: 'v3.0.0-rc.2',
44+
copyright: 'Copyright \u00A9 2015 - 2018 CBS, Statistics Netherlands'
45+
};
4846

49-
/**
50-
* Initializes the application
51-
* @param {Object} socket The websocket used to show the progress of the operation to the user
52-
* @return {void}
53-
*/
54-
function initApp(socket) {
55-
var files = ["i18n/grid.locale-" + i18next.lng()],
56-
strProcess = i18next.t("Main.Process"),
57-
strGet = i18next.t("Main.Get");
47+
/**
48+
* Initializes the application
49+
* @param {Object} socket The websocket used to show the progress of the operation to the user
50+
* @return {void}
51+
*/
52+
function initApp(socket) {
53+
var files = ["i18n/grid.locale-" + i18next.lng()],
54+
strProcess = i18next.t("Main.Process"),
55+
strGet = i18next.t("Main.Get");
5856

59-
if (utils.getConfig().Locale != "en") {
60-
files.push("globalize.culture." + utils.getConfig().Language);
61-
}
62-
63-
socket.on('source', function(data) {
64-
if (!utils.dialogProgress.dialog("isOpen")) {
65-
utils.dialogProgress.dialog("open");
66-
}
67-
utils.dialogProgress.children('#processSource').text(strGet + data.source + ' - ' + data.message);
68-
utils.dialogProgress.children('#processBar').progressbar({value: data.index});
69-
});
57+
if (utils.getConfig().Locale != "en") {
58+
files.push("globalize.culture." + utils.getConfig().Language);
59+
}
7060

71-
socket.on('processPG', function(data) {
72-
utils.dialogProgress.dialog("option","title", strProcess);
61+
socket.on('source', function(data) {
62+
if (!utils.dialogProgress.dialog("isOpen")) {
7363
utils.dialogProgress.dialog("open");
74-
utils.dialogProgress.children('#processBar').progressbar({max: data.totalSources});
75-
});
64+
}
65+
utils.dialogProgress.children('#processSource').text(strGet + data.source + ' - ' + data.message);
66+
utils.dialogProgress.children('#processBar').progressbar({value: data.index});
67+
});
7668

77-
socket.on('processPath', function(data) {
78-
utils.dialogProgress.children('#stepBar').progressbar({max: data.totalSteps, value: data.index});
79-
});
69+
socket.on('processPG', function(data) {
70+
utils.dialogProgress.dialog({title: strProcess});
71+
utils.dialogProgress.dialog("open");
72+
utils.dialogProgress.children('#processBar').progressbar({max: data.totalSources});
73+
});
8074

81-
socket.on('step', function(data) {
82-
utils.dialogProgress.children('#stepBar').progressbar({value: data.index});
83-
});
75+
socket.on('processPath', function(data) {
76+
utils.dialogProgress.children('#stepBar').progressbar({max: data.totalSteps, value: data.index});
77+
});
8478

85-
require(files, function() {
86-
Globalize.culture(utils.getConfig().Locale);
87-
$.getJSON('/data.html', {
88-
action: 'getProductgroup',
89-
element: "sidebar"
90-
}, function(data) {
91-
init.initWindow(data, infoText);
92-
});
79+
socket.on('step', function(data) {
80+
utils.dialogProgress.children('#stepBar').progressbar({value: data.index});
81+
});
82+
83+
require(files, function() {
84+
Globalize.culture(utils.getConfig().Locale);
85+
$.getJSON('/data.html', {
86+
action: 'getProductgroup',
87+
element: "sidebar"
88+
}, function(data) {
89+
init.initWindow(data, infoText);
9390
});
94-
}
91+
});
92+
}
9593

96-
$(document).ready(function() {
97-
var socket = io();
94+
$(document).ready(function() {
95+
var socket = io();
9896

99-
utils.dialogProgress = $('div#dialogProgress')
100-
.dialog({
101-
autoOpen: false,
102-
modal: true,
103-
dialogClass: 'no-close',
104-
closeOnEscape: false,
105-
width: 400
106-
});
97+
utils.dialogProgress = $('div#dialogProgress')
98+
.dialog({
99+
autoOpen: false,
100+
modal: true,
101+
dialogClass: 'no-close',
102+
closeOnEscape: false,
103+
width: 400
104+
});
107105

108-
$(document).ajaxError(function(event, xhr) {
109-
var data;
110-
try {
111-
data = JSON.parse(xhr.responseText);
112-
if ($.inArray(data.action, ["updateSource", "updateSourcePath", "updateProductgroup"]) == -1) {
113-
alert(data.message);
114-
}
115-
} catch (e) {
106+
$(document).ajaxError(function(event, xhr) {
107+
var data;
108+
try {
109+
data = JSON.parse(xhr.responseText);
110+
if ($.inArray(data.action, ["updateSource", "updateSourcePath", "updateProductgroup"]) == -1) {
111+
alert(data.message);
116112
}
117-
$("#spinner").hide();
118-
});
113+
} catch (e) {}
114+
$("#spinner").hide();
115+
});
119116

120-
$.getJSON('/config/config.json', function(cfg) {
121-
utils.setConfig(cfg);
122-
i18next.init({
123-
lng: cfg.Language,
124-
fallbackLng: "en",
125-
resGetPath: '/locales/__ns__-__lng__.json'
126-
}, function(err, t) {
127-
initApp(socket);
128-
});
117+
$.get('/data.html?action=getConfig', function(cfg) {
118+
utils.setConfig(JSON.parse(cfg));
119+
i18next.init({
120+
lng: cfg.Language,
121+
fallbackLng: "en",
122+
resGetPath: '/locales/__ns__-__lng__.json'
123+
}, function(err, t) {
124+
initApp(socket);
129125
});
130126
});
131127
});
128+
132129
});

inst/server/robotDB.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,22 @@
2424

2525
var
2626
sqlite3 = require('sqlite3').verbose(),
27-
db = new sqlite3.Database('./inst/server/db/observationDB.sqlite'),
2827
csv = require('fast-csv'),
2928
moment = require('moment'),
3029
_ = require('lodash'),
3130
url = require('url'),
3231
fs = require('fs'),
3332
async = require('async'),
34-
userConfig = require('./../app/config/config.json');
33+
db,
34+
serverMessages;
3535

36-
var serverMessages = require('./../app/locales/translation-' + userConfig.Language + '.json').Server;
37-
38-
var init = function () {
36+
var init = function (paths, config) {
37+
serverMessages = require('./../app/locales/translation-' + config.Language + '.json').Server;
38+
if (config.mode === 'server') {
39+
db = new sqlite3.Database(paths.data + '/observationDB.sqlite');
40+
} else {
41+
db = new sqlite3.Database('./inst/server/db/observationDB.sqlite');
42+
}
3943
var sql = "PRAGMA foreign_keys=1";
4044
db.run(sql);
4145
};
@@ -463,7 +467,6 @@ var updateSourcePath = function (qry, callback) {
463467
break;
464468

465469
case 'del':
466-
console.log("nu hier");
467470
sql =
468471
'DELETE FROM SourcePath ' +
469472
'WHERE sourcepath_id="' + qry.id + '"';

inst/server/server.js

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,31 @@ var connect = require('connect'),
2727
http = require('http'),
2828
robotDB = require('./robotDB.js'),
2929
initWebData = require('./webData.js').init,
30-
open = require('opn');
30+
open = require('opn'),
31+
fs = require('fs'),
32+
mkdirp = require('mkdirp'),
33+
envPaths = require('env-paths'),
34+
path = require('path'),
35+
paths = envPaths('Robottool'),
36+
config,
37+
userconfig = require('./../app/config/config.json');
38+
39+
if (userconfig.mode === 'server') {
40+
// Robottool is installed globally
41+
if (!fs.existsSync(paths.config + '/config.json')) {
42+
// user starts Robottool for the first time
43+
mkdirp.sync(paths.config);
44+
mkdirp.sync(paths.data);
45+
mkdirp.sync(paths.data + '/downloadedFiles');
46+
mkdirp.sync(paths.data + '/screenshot');
47+
fs.copyFileSync('./inst/app/config/config.json', paths.config + '/config.json');
48+
fs.copyFileSync('./inst/server/db/observationDB.sqlite', paths.data + '/observationDB.sqlite');
49+
}
50+
config = require(paths.config + '/config.json');
51+
} else {
52+
// Robottool is installed locally
53+
config = require('./../app/config/config.json');
54+
}
3155

3256
var app = connect()
3357
.use(serveStatic(__dirname + '/../app'))
@@ -150,6 +174,9 @@ var app = connect()
150174
res.end(JSON.stringify(result));
151175
});
152176
break;
177+
case 'getConfig':
178+
res.end(JSON.stringify(config));
179+
break;
153180
default:
154181
res.end('');
155182
}
@@ -162,15 +189,22 @@ var app = connect()
162189

163190
var server = http.createServer(app);
164191
server.timeout = 0;
165-
var config = require('./../app/config/config.json');
166-
167-
server.listen(
168-
config.port,
169-
function () {
170-
open('http://localhost:' + config.port, {app: config.Browser});
171-
}
172-
);
173192

193+
if (config.port) {
194+
server.listen(
195+
config.port,
196+
function () {
197+
open('http://localhost:' + config.port, {app: config.Browser});
198+
}
199+
);
200+
} else {
201+
server.listen(
202+
0,
203+
function () {
204+
open('http://localhost:' + config.port, {app: config.Browser});
205+
}
206+
);
207+
}
174208

175-
robotDB.init();
176-
initWebData(server);
209+
robotDB.init(paths, config);
210+
initWebData(server, config);

0 commit comments

Comments
 (0)