-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
197 lines (163 loc) · 5.87 KB
/
demo.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
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
194
195
196
197
const http = require('http');
const https = require('node:https');
const fs = require('fs');
const protobuf = require('protobufjs');
const crypto = require("crypto");
const redis = require("redis");
// TODO: proto is copy-pasted
const PROTO_PATH = __dirname + '/protos/mama.proto';
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const mama_proto = grpc.loadPackageDefinition(packageDefinition).mama.server.api;
const MAMA_GRPC_URL = `${process.env['MAMA_HOST'] || 'localhost'}:${process.env['MAMA_PORT'] || '50051'}`;
const PORT = 5000;
const POLLING_INTERVAL = 1000;
const GTFS_URL = 'https://mkuran.pl/gtfs/warsaw/vehicles.pb';
const VEHICLE_STATE_EXPIRE_S = 180;
const subscribers = [];
let positions = [];
const mamaClient = new mama_proto.MamaService(MAMA_GRPC_URL, grpc.credentials.createInsecure());
const redisClient = redis.createClient({
url: `redis://${process.env['REDIS_HOST'] || 'localhost'}:${process.env['REDIS_PORT'] || '6379'}`
});
let FeedMessage = null;
function generateId() {
return crypto.randomBytes(16).toString("hex");
}
function log(msg) {
process.stderr.write(msg + '\n');
}
function notify(subscriber) {
log(`notify`);
const toNotify = subscriber ? [subscriber] : subscribers;
toNotify.forEach((subscriber) => {
subscriber.res.write('id: ' + subscribe.id + '\n');
subscriber.res.write('data: ' + JSON.stringify(positions) + '\n\n');
});
}
function subscribe(res) {
res.writeHead(200, {
'Content-Type' : 'text/event-stream',
'Cache-Control' : 'no-cache',
'Connection' : 'keep-alive'
});
subscribers.push({id: generateId(), res});
notify(subscribers[subscribers.length - 1]);
}
async function makeGrpcRequest(feedMessage, callback) {
const buildRedisKey = (id) => `vehicle:${id}:state`;
const request = {
entries: []
};
log('Getting state from Redis...');
// TODO: likely we can request all states in one request to Redis
for (let i = 0; i < feedMessage.entity.length; ++i) {
const entity = feedMessage.entity[i];
const location = {
timestamp: {
seconds: entity.vehicle.timestamp,
nanos: 0
},
latitude: entity.vehicle.position.latitude,
longitude: entity.vehicle.position.longitude,
speed: null,
bearing: null
};
const redisKey = buildRedisKey(entity.vehicle.vehicle.id);
const state = await redisClient.get(redis.commandOptions({ returnBuffers: true }), redisKey);
request.entries.push({location, state});
}
log('Calling service...');
mamaClient.match(request, async function(err, response) {
if (err || response.entries.length != feedMessage.entity.length) {
log(`Map matching error: ${err}`);
callback(err);
return;
}
const positions = [];
for (let i = 0; i < feedMessage.entity.length; ++i) {
const state = response.entries[i].state;
const redisKey = buildRedisKey(feedMessage.entity[i].vehicle.vehicle.id);
await redisClient.setEx(redisKey, VEHICLE_STATE_EXPIRE_S, state);
positions.push({
id: feedMessage.entity[i].vehicle.vehicle.id,
label: feedMessage.entity[i].vehicle.vehicle.label,
position: response.entries[i].location
});
}
callback(null, positions);
});
}
async function polling() {
https.get(GTFS_URL, (res) => {
log('polling');
const buffers = [];
res.on('data', (d) => {
buffers.push(d);
});
res.on('end', async () => {
try {
const buffer = Buffer.concat(buffers);
const message = FeedMessage.decode(buffer);
// message.entity = message.entity.filter(x => x.id == 'V/518/1');
// console.log(JSON.stringify(message.entity[0]));
log(`Got ${message.entity.length} vehicles. Matching...`);
// message.entity = [message.entity[0]];
await makeGrpcRequest(message, (err, matchedPositions) => {
if (err) {
return;
}
positions = matchedPositions;
log(`Got ${positions.length} positions`);
notify();
});
} catch(err) {
log(`Error: ${err}`);
} finally {
setTimeout(polling, POLLING_INTERVAL);
}
});
}).on('error', (e) => {
log(`Error on GTFS polling: ${e}. Retrying...`);
setTimeout(polling, POLLING_INTERVAL);
});
}
function startPolling() {
protobuf.load("gtfs.proto", function(err, root) {
if (err) {
throw error;
}
FeedMessage = root.lookupType("transit_realtime.FeedMessage");
polling();
});
}
async function main() {
await redisClient.connect();
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
if (request.headers.accept && request.headers.accept == 'text/event-stream') {
if (request.url == '/talk') {
subscribe(response);
}
} else {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}
}).listen(PORT);
log(`Server running at http://localhost:${PORT}/`)
startPolling();
});
}
main();