-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlocalmap.js
More file actions
78 lines (67 loc) · 1.79 KB
/
Copy pathlocalmap.js
File metadata and controls
78 lines (67 loc) · 1.79 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
import {
connection,
decoding,
status,
constants
} from '../lib/index';
import * as fs from 'fs';
const {
discover,
createSocket,
sendPeriodicHeartbeat,
createConnectionSubject
} = connection
const {
createObservable,
parseBinaryDatabase,
aggregateBundles,
generateTreeFromDatabase,
parseBinaryMap
} = decoding
const {
connected
} = status
const {
channels,
commands
} = constants
discover()
.then(server => createSocket(server.info.address))
.then(socket => {
sendPeriodicHeartbeat(socket)
return createConnectionSubject(socket)
})
.then(subject => {
connected(subject)
.then(handshake => {
console.log('Connected!', handshake)
const localMap = subject
.filter(x => x.type === channels.LocalMapUpdate)
.map(x => parseBinaryMap(x.payload))
localMap
.distinctUntilChanged()
.subscribe(map => {
// We could write the file out as a PNG or JPEG if we wanted.
// We'll write a file out as PGM directly so we don't
// need a dependency for this example.
//
// If you don't have a viewer for PGM but have imagemagick, run
// convert localmap.pgm localmap.png
var imageFile = fs.createWriteStream('localmap.pgm', {
flags: 'w',
defaultEncoding: 'binary',
mode: 0o666
})
imageFile.write(`P5 ${map.width} ${map.height} 255\n ${map.pixels.toString('binary')}`)
console.log('Wrote file');
})
subject.observer.onNext(['RequestLocalMapSnapshot'])
})
.catch(err => {
console.error('Couldn\'t establish connection!', err);
console.error(err.stack);
})
})
.catch(err => {
throw err
})