generated from dfpc-coe/etl-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.ts
132 lines (113 loc) · 3.99 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
import { Static, Type, TSchema } from '@sinclair/typebox';
import type { Event } from '@tak-ps/etl';
import ETL, { SchemaType, handler as internal, local, InputFeatureCollection, DataFlowType, InvocationType } from '@tak-ps/etl';
import { fetch } from '@tak-ps/etl';
const InputSchema = Type.Object({
OptimusClientID: Type.String(),
OptimusAPIToken: Type.String(),
DEBUG: Type.Boolean({
default: false,
description: 'Print results in logs'
})
});
const Position = Type.Object({
id: Type.Number(),
latitude: Type.Number(),
longitude: Type.Number(),
deviceId: Type.Integer(),
utcDate: Type.String({ format: 'date-time' }),
speed: Type.Number(),
azimuth: Type.Number(),
altitude: Type.Number(),
signal: Type.Number(),
battery: Type.Number(),
isOn: Type.Boolean(),
isIdle: Type.Boolean(),
tanks: Type.Unknown(),
thermometers: Type.Unknown(),
})
const Device = Type.Object({
id: Type.Integer(),
clientId: Type.Integer(),
pin: Type.String(),
description: Type.String(),
utcOffsetMinutes: Type.Integer(),
extra: Type.Record(Type.String(), Type.String())
});
const OutputSchema = Position;
export default class Task extends ETL {
static name = 'etl-optimus'
static flow = [ DataFlowType.Incoming ];
static invocation = [ InvocationType.Schedule ];
async schema(
type: SchemaType = SchemaType.Input,
flow: DataFlowType = DataFlowType.Incoming
): Promise<TSchema> {
if (flow === DataFlowType.Incoming) {
if (type === SchemaType.Input) {
return InputSchema;
} else {
return OutputSchema;
}
} else {
return Type.Object({});
}
}
async control(): Promise<void> {
const env = await this.env(InputSchema);
const fc: Static<typeof InputFeatureCollection> = {
type: 'FeatureCollection',
features: []
}
const res = await fetch(`https://api3p.optimustracking.com/v1/clients/${env.OptimusClientID}/position/latest`, {
headers: {
Accept: 'application/json',
'api-key': env.OptimusAPIToken
}
});
const positions = await res.typed(Type.Array(Position))
const devRes = await fetch(`https://api3p.optimustracking.com/v1/clients/${env.OptimusClientID}/devices`, {
headers: {
Accept: 'application/json',
'api-key': env.OptimusAPIToken
}
});
const devices = await devRes.typed(Type.Array(Device))
const deviceMap = new Map<number, Static<typeof Device>>();
devices.forEach((device) => {
deviceMap.set(device.id, device);
});
const future = new Date();
future.setTime(future.getTime() + 172800000);
for (const position of positions) {
if (new Date(position.utcDate) < future) {
const device = deviceMap.get(position.deviceId);
fc.features.push({
id: String(position.deviceId),
type: 'Feature',
properties: {
type: 'a-h-G',
how: 'm-g',
callsign: device.description || 'Optimus',
speed: position.speed,
course: position.azimuth,
metadata: position
},
geometry: {
type: 'Point',
coordinates: [
position.longitude,
position.latitude,
position.altitude
]
}
});
}
}
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);
}