-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyboom-cli.js
More file actions
193 lines (180 loc) · 6.44 KB
/
Copy pathtinyboom-cli.js
File metadata and controls
193 lines (180 loc) · 6.44 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
183
184
185
186
187
188
189
190
191
192
193
const program = require('commander');
const inquirer = require('inquirer');
const request = require('superagent');
const path = require('path');
const fs = require('fs');
const LinuxDevice = require('./models/LinuxDevice');
const RestApi = require('./library/rest-api');
const SocketService = require('./library/socket-service');
const imagesnap = require('./library/imagesnap');
const gstreamer = require('./library/gstreamer');
const packageVersion = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf-8')).version;
const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));
const WEBSOCKET_SEND_INTERVAL = 200;
program
.description('Tinyboom Linux client ' + packageVersion)
.version(packageVersion)
.option('-p, --project <key>', 'Project Code of the project you want to access')
.option('-a, --api-key <key>', 'API key to authenticate with Tinyboom')
// .option('--disable-camera', `Don't prompt for camera`)
// .option('--disable-microphone', `Don't prompt for microphone`)
.option('-w, --width <px>', 'Desired width of the camera stream', '640')
.option('-h, --height <px>', 'Desired height of the camera stream', '360')
// .option('--clean', 'Clear credentials')
// .option('--silent', `Run in silent mode, don't prompt for credentials`)
// .option('--dev', 'List development servers.')
.allowUnknownOption(true)
.parse(process.argv);
const options = program.opts();
const projectCodeArgv = options.project;
const apiKeyArgv = options.apiKey;
const widthArgv = options.width;
const heightArgv = options.height;
const dimensions = {
height: +heightArgv,
width: +widthArgv
}
if (!projectCodeArgv) {
console.error(`Error: --project must be specified`);
process.exit(1);
}
if (!apiKeyArgv) {
console.error(`Error: --api-key must be specified`);
process.exit(1);
}
const noCamera = false;
const isProphesee = false;
const verboseMode = false;
let camera;
let device;
console.debug(`[Tinyboom CLI] packageVersion`, packageVersion);
console.debug(`[Tinyboom CLI] platform`, process.platform);
console.debug(`[Tinyboom CLI] projectCodeArgv`, projectCodeArgv);
console.debug(`[Tinyboom CLI] apiKeyArgv`, apiKeyArgv);
console.debug(`[Tinyboom CLI] widthArgv`, widthArgv);
console.debug(`[Tinyboom CLI] heightArgv`, heightArgv);
(async () => {
if (!noCamera) {
// if (isProphesee) {
// camera = new prophesee_1.Prophesee(verboseArgv);
// }
// else
if (process.platform === 'darwin') {
camera = new imagesnap.Imagesnap();
} else if (process.platform === 'linux') {
camera = new gstreamer.GStreamer(verboseMode);
}
else {
throw new Error('Unsupported platform: "' + process.platform + '"');
}
await camera.init();
}
const linuxDevice = new LinuxDevice(camera, dimensions.height, dimensions.width);
let firstExit = true;
const onSignal = async () => {
if (!firstExit) {
process.exit(1);
} else {
console.log('Received stop signal, stopping application... ' +
'Press CTRL+C again to force quit.');
firstExit = false;
try {
if (camera) {
await camera.stop();
await camera.removeTemporaryFiles();
}
if (device) {
await RestApi.setDeviceInactive(projectCodeArgv, apiKeyArgv, device.id);
}
SocketService.disconnect();
process.exit(0);
} catch (ex2) {
let ex = ex2;
console.log('Failed to stop inferencing', ex.message);
}
if (device) {
await RestApi.setDeviceInactive(projectCodeArgv, apiKeyArgv, device.id);
}
SocketService.disconnect();
process.exit(1);
}
};
process.on('SIGHUP', onSignal);
process.on('SIGINT', onSignal);
const deviceId = await linuxDevice.getDeviceId();
console.debug(`[Tinyboom CLI] deviceId`, deviceId);
const deviceType = await linuxDevice.getDeviceType();
console.debug(`[Tinyboom CLI] deviceType`, deviceType);
linuxDevice.on('snapshot', async (buffer, filename) => {
if (linuxDevice.isSnapshotStreaming()) {
const frame = buffer.toString('base64');
const data = {
frame,
filename
};
const message = JSON.stringify(data);
await SocketService.sendMessage(project.id, deviceId, message);
}
});
SocketService.setup(apiKeyArgv);
SocketService.on(`capture-${deviceId}`, async (data) => {
const { action, filename, type, userId, teamId } = data;
if (device && action === 'capture-edgedevice-image') {
const snapshotFullPath = camera.getSnapshotPath(filename);
const { image } = await RestApi.uploadImage(projectCodeArgv, apiKeyArgv, device.id, userId, teamId, snapshotFullPath, type);
console.log(`SocketService.on('capture-${deviceId}') action=${action} uploaded image=${image}`);
} else {
console.log(`SocketService.on('capture-${deviceId}') action=${action} ignored`);
}
});
const info = await RestApi.getProjectInfo(projectCodeArgv, apiKeyArgv, deviceId, deviceType);
const project = info.project;
if (!project) {
console.error('Error: Invalid Project');
process.exit(1);
}
device = info.device;
if (!device) {
console.error('Error: Invalid Device');
process.exit(1);
}
console.debug(`[Tinyboom CLI] project`, project.name);
if (camera) {
let cameraDevice;
const cameraDevices = await camera.listDevices();
if (cameraDevices.length === 0) {
throw new Error('Cannot find any webcams, run this command with --disable-camera to skip selection');
} else if (cameraDevices.length === 1) {
cameraDevice = cameraDevices[0];
} else {
let inqRes = await inquirer.prompt([{
type: 'list',
choices: (cameraDevices || []).map(p => ({ name: p, value: p })),
name: 'camera',
message: 'Select a camera (or run this command with --disable-camera to skip selection)',
pageSize: 20
}]);
cameraDevice = inqRes.camera;
}
console.log('Using camera', cameraDevice, 'starting...');
if (isProphesee) {
await camera.start({
device: cameraDevice,
intervalMs: WEBSOCKET_SEND_INTERVAL,
dimensions: dimensions
});
} else {
await camera.start({
device: cameraDevice,
intervalMs: WEBSOCKET_SEND_INTERVAL,
dimensions: dimensions
});
}
camera.on('error', error => {
console.log('camera error', error);
});
console.log('Connected to camera');
await snooze(2000);
await linuxDevice.startSnapshotStreaming();
}
})();