generated from dfpc-coe/etl-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.ts
178 lines (158 loc) · 6.46 KB
/
task.ts
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
import { Type, TSchema, Static } from '@sinclair/typebox';
import type { Event } from '@tak-ps/etl';
import ETL, { InputFeature, InputFeatureCollection, SchemaType, handler as internal, local } from '@tak-ps/etl';
import { fetch } from '@tak-ps/etl';
const InputSchema = Type.Object({
'DEBUG': Type.Boolean({
default: false,
description: 'Print results in logs'
})
});
const OutputSchema = Type.Object({})
export default class Task extends ETL {
static name = 'etl-cotrip-cameras';
async schema(type: SchemaType = SchemaType.Input): Promise<TSchema> {
if (type === SchemaType.Input) {
return InputSchema;
} else {
return OutputSchema;
}
}
async control(): Promise<void> {
await this.env(InputSchema);
const features: Static<typeof InputFeature>[] = [];
const res = await fetch('https://www.cotrip.org/api/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: `
query MapFeatures($input: MapFeaturesArgs!) {
mapFeaturesQuery(input: $input) {
mapFeatures {
tooltip
features {
id
geometry
properties
}
... on Camera {
views(limit: 5) {
... on CameraView {
sources {
type
src
}
}
category
}
}
}
error {
message
type
}
}
}
`,
variables: {
input: {
west: -180,
south: -85,
east: 180,
north: 85,
zoom: 11,
nonClusterableUris: ['dashboard'],
layerSlugs: ['normalCameras']
}
}
})
})
const body = await res.typed(Type.Object({
data: Type.Object({
mapFeaturesQuery: Type.Object({
mapFeatures: Type.Array(Type.Object({
tooltip: Type.String(),
features: Type.Array(Type.Object({
id: Type.String(),
properties: Type.Unknown(),
geometry: Type.Object({
type: Type.Literal('Point'),
coordinates: Type.Array(Type.Number())
})
})),
views: Type.Array(Type.Object({
category: Type.String(),
sources: Type.Union([Type.Array(Type.Object({
type: Type.String(),
src: Type.String()
})), Type.Null()])
}))
}))
})
})
}));
for (const camera of body.data.mapFeaturesQuery.mapFeatures) {
if (!camera.features || !camera.features.length) {
console.warn(`ok - skipping ${camera.tooltip} - missing feature`)
continue;
} else if (!camera.views || !camera.views.length) {
console.warn(`ok - skipping ${camera.tooltip} - mission sources`)
continue;
}
const feat = camera.features[0];
if (feat.geometry.coordinates.length < 2) {
console.warn(`ok - skipping ${camera.tooltip} - invalid coordinates`)
} else if (feat.geometry.coordinates[0] === 0 && feat.geometry.coordinates[1] === 0) {
console.warn(`ok - skipping ${camera.tooltip} - null island coordinates`)
}
const view = camera.views[0];
if (!view.sources || !view.sources.length || view.category !== "VIDEO") {
console.warn(`ok - skipping ${camera.tooltip} - missing streaming video`)
continue;
}
const source = view.sources[0];
if (feat.geometry.coordinates[0] === 0 && feat.geometry.coordinates[1] === 0) {
console.warn(`ok - skipping ${camera.tooltip} - Null Island Coordinates`)
continue;
}
features.push({
id: feat.id.replace('camera/', ''),
type: 'Feature',
properties: {
type: 'b-m-p-s-p-loc',
callsign: camera.tooltip,
video: {
uid: feat.id.replace('camera/', '') + '-video',
sensor: camera.tooltip,
url: source.src,
connection: {
networkTimeout: 12000,
uid: feat.id.replace('camera/', '') + '-video',
path: "",
protocol: "raw",
bufferTime: -1,
address: source.src,
port: -1,
roverPort: -1,
rtspReliable: 0,
ignoreEmbeddedKLV: false,
alias: camera.tooltip
}
}
},
geometry: feat.geometry
})
}
const fc: Static<typeof InputFeatureCollection> = {
type: 'FeatureCollection',
features: features
}
await this.submit(fc);
}
}
await local(new Task(import.meta.url), import.meta.url);
export async function handler(event: Event = {}) {
return await internal(new Task(import.meta.url), event);
}