-
Notifications
You must be signed in to change notification settings - Fork 0
/
cameraStreamer.js
143 lines (111 loc) · 3.13 KB
/
cameraStreamer.js
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
var fs = require("fs");
var raspcam = require("./raspcam.js");
var logger = require("./logger.js");
var recordingsPath = __dirname + "/recordings/";
var intervalTimerObj;
var running = false;
var recording = false;
var recordingsDir;
var recordingIndex;
function startStream(sockets) {
if(!intervalTimerObj) {
raspcam.takePicture();
running = true;
}
clearTimeout(intervalTimerObj);
intervalTimerObj = setTimeout(updateStream, 1000, sockets);
}
exports.startStream = startStream;
function stopStream() {
clearTimeout(intervalTimerObj);
intervalTimerObj = undefined;
raspcam.stopAll();
running = false;
stopRecording();
}
exports.stopStream = stopStream;
function updateStream(sockets){
fs.readFile( __dirname + "/pic.jpg", function(err, image) {
if(err) {
logger.logSevere("Error loading image.");
}
sockets.forEach(function(socket) {
socket.emit("image", image);
});
if(recording === true) {
fs.writeFile(recordingsDir + "/pic" + padZeros(recordingIndex, 5) + ".jpg", image, function(error) {
if(error) {
logger.logSevere("updateStream: " + error);
}
recordingIndex++;
image = null;
});
}
startStream(sockets);
});
}
function setOptionsString(options) {
raspcam.setOptionsString(options);
if(options["running"] === true) {
raspcam.restart();
}
}
exports.setOptionsString = setOptionsString;
function startRecording() {
var date = new Date();
var dirName = recordingsPath +
+ date.getUTCFullYear() + "-"
+ padZeros(date.getUTCMonth(), 2) + "-"
+ padZeros(date.getUTCDay(),2 ) + "_"
+ padZeros(date.getUTCHours(), 2) + ":"
+ padZeros(date.getUTCMinutes(), 2) + ":"
+ padZeros(date.getUTCSeconds(), 2);
fs.exists(__dirname + "/recordings", function(exists) {
if(!exists) {
fs.mkdir(__dirname + "/recordings");
}
fs.mkdir(dirName);
recording = true;
recordingsDir = dirName;
recordingIndex = 0;
});
}
exports.startRecording = startRecording;
function stopRecording() {
recording = false;
recordingsDir = undefined;
}
exports.stopRecording = stopRecording;
function sendRecordingsList(socket) {
fs.readdir(recordingsPath, function(error, files) {
if(error) {
logger.logSevere("getAllRecordings:" + error);
}
socket.emit("recordingsListResponse", files);
});
}
exports.sendRecordingsList = sendRecordingsList;
function sendRecording(socket, imagePath) {
fs.readdir(recordingsPath + imagePath, function(error, files) {
sendRecord(socket, imagePath, files, 0);
});
}
exports.sendRecording = sendRecording;
function sendRecord(socket, imagePath, imagesNames, iteration) {
if(imagesNames.length > iteration && socket.connected != false) {
console.log(recordingsPath + imagePath + "/" + imagesNames[iteration]);
fs.readFile(recordingsPath + imagePath + "/" + imagesNames[iteration], function(error, image) {
if(error) {
console.log(error);
}
socket.emit("image", image);
setTimeout(sendRecord, 1000, socket,imagePath, imagesNames, iteration + 1);
});
} else {
socket.emit("recordingFinished", []);
}
}
function padZeros(num, size) {
var s = "000000000" + num;
return s.substr(s.length-size);
}