-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim-source.js
146 lines (131 loc) · 3.89 KB
/
sim-source.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
const request = require('request')
const fs = require('fs')
const path = require('path')
// let config = {
// ROUTE_SRC: process.env['simulator_route_source'] || 'data/route.json',
// STOPS_SRC: process.env['simulator_stops_source'] || 'data/stops.json'
// }
let routeCoordinates = null
// let stopsCoordinates = null
let stops = null
const init = options => {
let config = options || {}
const getRoute = config.ROUTE_SRC.startsWith('http') ? getFromURL : getFromFile
const getStops = config.STOPS_SRC.startsWith('http') ? getFromURL : getFromFile
return Promise.all([getRoute(config.ROUTE_SRC), getStops(config.STOPS_SRC)])
.then(sources => {
if (!sources[0] && !sources[1]) {
console.warn('route and stops sources not found')
return Promise.reject(new Error('route and stops sources not found'))
} else {
initRoute(sources[0], sources[1])
initStops(sources[0], sources[1])
}
console.log(`loaded route with ${routeCoordinates.length} coordinates`)
console.log(`loaded ${stops.length} stops`)
return Promise.resolve({
route: routeCoordinates,
stops: stops
})
})
}
const initRoute = (routeJson, stopsJson) => {
let coordinates = []
if (routeJson && routeJson.type === 'FeatureCollection') {
let features = routeJson.features
features.forEach(f => {
if (f.geometry) {
if (f.geometry.type === 'Point') {
if (f.properties) {
coordinates.push({
coordinates: f.geometry.coordinates,
properties: f.properties
})
} else {
coordinates.push(f.geometry.coordinates)
}
} else if (f.geometry.type === 'LineString') {
coordinates = coordinates.concat(f.geometry.coordinates)
}
}
})
} else if (stopsJson && stopsJson.routeCoordinates) {
coordinates = stopsJson.routeCoordinates
}
routeCoordinates = coordinates
}
const initStops = (routeJson, stopsJson) => {
let stopsData = []
if (stopsJson) {
if (stopsJson.type === 'FeatureCollection') {
let features = stopsJson.features
features.forEach(f => {
if (f.geometry) {
let s = f.properties
s['coordinates'] = f.geometry.coordinates
stopsData.push(s)
}
})
} else if (stopsJson.stops) {
const stopsKeys = Object.keys(stopsJson.stops)
for (let i = 0; i < stopsKeys.length; i++) {
let s = stopsJson.stops[stopsKeys[i]]
stopsData.push({
name: stopsKeys[i],
description: s.description,
poi: s.poi,
coordinates: s.coordinates
})
}
}
}
stops = stopsData
// stopsCoordinates = stops.map(stop => {
// return stop.coordinates
// })
}
const getFromURL = (srcUrl) => {
return Promise.resolve()
.then(() => {
return new Promise((resolve, reject) => {
request.get({url: srcUrl}, (err, response, body) => {
if (err) {
console.error(err)
resolve(false)
} else {
console.log(`${srcUrl} has been retrieved`)
resolve(JSON.parse(body))
}
})
})
})
.catch(err => {
console.error(err)
return Promise.resolve(false)
})
}
const getFromFile = (filePath) => {
return Promise.resolve()
.then(() => {
return new Promise((resolve, reject) => {
let filepath = filePath
if (filePath.startsWith('file://')) {
filepath = filePath.substring(7)
} else {
filepath = path.join(__dirname, filePath)
}
let body = fs.readFileSync(filepath, 'utf-8')
console.log(`file ${filepath} has been read`)
resolve(JSON.parse(body))
})
})
.catch(err => {
console.error(err)
return Promise.resolve(false)
})
}
module.exports = {
init: init,
route: routeCoordinates,
stops: stops
}