-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·182 lines (162 loc) · 4.69 KB
/
Copy pathserver.js
File metadata and controls
executable file
·182 lines (162 loc) · 4.69 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
global.RecObj = {};
global.RecObj.RecStatus = false;
app.use(express.static(__dirname + '/app/public'));
app.set('views', __dirname + '/app/views');
app.set('view engine', 'ejs');
var fs = require('fs');
//setup config variables
var config = require("./lib/config.json");
global.config=config;
var data = {};
data = config;
data.foo="bar";
var path = "/home/pi/data/";
//test and configure for where it's running
var www_port=config.dev_port;
var isPi = require('detect-rpi');
if (isPi()) {
//setup specific to rPi
www_port = config.prod_port;
//new objects and events to handle tapping
const TapWatcher = require('./lib/TapWatcher.js');
// create a new instance of our PubSub class
const watcher = new TapWatcher(config.tap_source,config.accel_threshold);
// listen for any events
watcher.on('tap', function (data) {
//console.log('1_tap!'+data)
tap(data)
})
} else {
//setup test variables for running on laptop
data.test=true
}
//start a server and log its start to our console
http.listen(www_port, function () {
var port = http.address().port;
console.log('Tapper is listening on port ', port);
});
//basic socket.io listener
io.on('connection', function(socket){
console.log("socket.io connected");
socket.on('disconnect', function(){
console.log("socket.io disconnected")
});
socket.on('start', function(data){
global.RecObj=data;
global.RecObj.date=getDateTime(":");
global.RecObj.RecStatus = true;
global.RecObj.data = [];
global.RecObj.time = [];
global.RecObj.lastclick=new Date().getTime();
console.log("starting");
console.log(global.RecObj);
//store the object persistently
});
socket.on('stop', function(data){
console.log("stopping recording");
global.RecObj.RecStatus = false;
global.led.value(true);
//store the object persistently
var fpath=path+getDateTime("")+".json";
fs.writeFile(fpath, JSON.stringify(global.RecObj), (err) => {
if (err) {
console.error(err);
return;
};
console.log("File has been created");
});
});
socket.on('tap', function(data){
tap();
});
});
// reply to request
app.get('/', function (req, res) {
var action = req.query.action;
var fileid = req.query.id;
if (action=="delete") {
var delitems = fs.readdirSync(path)
try {
fs.unlinkSync(path+delitems[data.fileid]);
console.log("item deleted");
} catch (er) {
console.log(er);
}
}
var file_titles=[];
var file;
fs.readdir(path, function(err, items) {
if (items) {
for (var i=0; i<items.length; i++) {
try {
file=JSON.parse(fs.readFileSync(path+items[i], 'utf8'));
file_titles[i]=file.name;
} catch (er) {
console.log(er);
//there's an empty file. This happens. delete it
fs.unlinkSync(path+items[i]);
}
}
data.hasfiles=true;
} else {
data.hasfiles=false;
}
data.file_titles=file_titles;
res.render('index',data);
});
});
app.get('/view', function (req, res) {
data.fileid = req.query.id;
var file;
fs.readdir(path, function(err, items) {
file=JSON.parse(fs.readFileSync(path+items[data.fileid], 'utf8'));
data.file=file;
res.render('view',data);
});
});
//Some utility functions
function tap() {
if (global.RecObj.RecStatus) {
var now = new Date().getTime();
var diff = now - global.RecObj.lastclick;
if (diff>config.minGap) {
global.RecObj.lastclick = now;
global.RecObj.data.push(diff);
//global.RecObj.data.push(data);
global.RecObj.time.push(getTime(":"));
io.sockets.emit("tap",diff);
}
} else {
//console.log("not recording");
}
}
function getTime(delim) {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return hour + delim + min + delim + sec;
}
function getDateTime(delim) {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + delim + month + delim + day + delim + hour + delim + min + delim + sec;
}