-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.js
More file actions
172 lines (160 loc) · 5.15 KB
/
camera.js
File metadata and controls
172 lines (160 loc) · 5.15 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
const debug = require("debug")("view360:camera");
const events = require("events");
const os = require('os');
const spawn = require('child_process').spawn;
const HOME_DIRECTORY = os.homedir();
const GPHOTO2 = "gphoto2"
class Camera extends events.EventEmitter {
constructor() {
super();
this.setCaptureDirectory(HOME_DIRECTORY+"/Pictures/");
this.setModel("Not detected yet.");
this.detectModel();
this.setBatteryLevel("___%");
};
monitorBatteryLevel() {
this.setBatteryLevel("unknown");
this.checkBattery();
// update battery level once a minute
setInterval(this.checkBattery,60*1000);
};
setCaptureDirectory(newDirectory) {
this.captureDirectory = newDirectory;
debug(`captureDirectory: ${this.captureDirectory}`);
this.emit("directory",newDirectory);
};
setBatteryLevel(level) {
debug(`emit battery ${level}`);
this.emit("battery", level);
};
getModel() {
debug("Asked to get model");
return this.model;
};
setModel(model) {
this.model = model;
debug(`New camera model "${model}"`);
this.emit("model", model);
};
detectModel() {
//debug(`spawn path: ${process.env.PATH}`);
let instance = this;
let detectedModel = "No camera detected.";
const command = GPHOTO2;
const workingDirectory = this.captureDirectory;
const args = [];
args.push("--auto-detect");
debug(`${command} ${JSON.stringify(args)}`);
const spawned = spawn(command, args, { cwd: workingDirectory }).on("error", (err) => { throw err });
spawned.stdout.setEncoding('utf8');
spawned.stdout.on('data', function (data) {
const str = data.toString()
const lines = str.split(/(\r?\n)/g);
for (let line of lines) {
if (line.length > 3 && !line.startsWith("Model") && !line.startsWith("-----")) {
const spaces = line.indexOf(" ");
if (spaces < 0) {
detectedModel = line;
} else {
detectedModel = line.substring(0,spaces);
}
}
}
});
spawned.stderr.on('data', function (data) {
const str = data.toString()
const lines = str.split(/(\r?\n)/g);
debug(lines.join("###"));
});
spawned.on('close', function (code) {
debug('process exit code ' + code);
instance.setModel(detectedModel);
});
return this.model;
};
takePreview() {
const command = GPHOTO2;
const workingDirectory = this.captureDirectory;
const args = [];
args.push("--force-overwrite");
args.push("--capture-preview");
debug(`${command} ${JSON.stringify(args)}`);
const spawned = spawn(command, args, { cwd: workingDirectory }).on("error", (err) => { throw err });
spawned.stdout.setEncoding('utf8');
spawned.stdout.on('data', function (data) {
const str = data.toString()
const lines = str.split(/(\r?\n)/g);
for (let line of lines) {
debug(line);
}
});
spawned.stderr.on('data', function (data) {
const str = data.toString()
const lines = str.split(/(\r?\n)/g);
debug(lines.join("###"));
});
spawned.on('close', function (code) {
debug('process exit code ' + code);
debug('Done taking preview');
});
};
takePicture() {
const command = GPHOTO2;
const workingDirectory = this.captureDirectory;
const args = [];
args.push("--force-overwrite");
args.push("--capture-image-and-download");
debug(`${command} ${JSON.stringify(args)}`);
const spawned = spawn(command, args, { cwd: workingDirectory }).on("error", (err) => { throw err });
spawned.stdout.setEncoding('utf8');
spawned.stdout.on('data', function (data) {
const str = data.toString()
const lines = str.split(/(\r?\n)/g);
for (let line of lines) {
debug(line);
}
});
spawned.stderr.on('data', function (data) {
const str = data.toString()
const lines = str.split(/(\r?\n)/g);
debug(lines.join("###"));
});
spawned.on('close', function (code) {
debug('process exit code ' + code);
debug('Done taking picture');
});
};
checkBattery() {
let instance = this;
const command = GPHOTO2;
const workingDirectory = this.captureDirectory;
const args = [];
let batteryLevel = "___%";
args.push("--get-config");
args.push("/main/status/batterylevel");
debug(`${command} ${JSON.stringify(args)}`);
const spawned = spawn(command, args, { cwd: workingDirectory }).on("error", (err) => { throw err });
spawned.stdout.setEncoding('utf8');
spawned.stdout.on('data', function (data) {
const str = data.toString()
const lines = str.split(/(\r?\n)/g);
for (let line of lines) {
if (line.startsWith("Current: ")) {
batteryLevel = line.substring(9);
}
}
});
spawned.stderr.on('data', function (data) {
const str = data.toString()
const lines = str.split(/(\r?\n)/g);
debug(lines.join("###"));
});
spawned.on('close', function (code) {
//debug('process exit code ' + code);
//debug('Done checking battery');
camera.setBatteryLevel(batteryLevel);
});
}
}
const camera = new Camera();
module.exports = camera;