-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.ts
More file actions
750 lines (628 loc) · 28.2 KB
/
Copy pathtask.ts
File metadata and controls
750 lines (628 loc) · 28.2 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
import type { Static, TSchema } from '@sinclair/typebox';
import { Type } from '@sinclair/typebox';
import type { Event } from '@tak-ps/etl';
import { Feature } from '@tak-ps/node-cot'
import ETL, { SchemaType, handler as internal, local, DataFlowType, InvocationType } from '@tak-ps/etl';
import { fetch } from '@tak-ps/etl';
const Nullable = <T extends TSchema>(type: T) => Type.Union([Type.Null(), type]);
const DATA_TYPE_CFS = 'Calls for Service';
const DATA_TYPE_UNITS = 'Units';
/**
* The Input Schema contains the environment object that will be requested via the CloudTAK UI
* It should be a valid TypeBox object - https://github.com/sinclairzx81/typebox
*/
const InputSchema = Type.Object({
BaseURL: Type.String({
description: 'Base URL of the Pro Suite deployment - ie https://agency.centralsquarecloudgov.com'
}),
Username: Type.String({
description: 'Pro Suite API user provisioned by the agency administrator'
}),
Password: Type.String({
description: 'Password for the Pro Suite API user'
}),
Domain: Type.Optional(Type.String({
description: 'Active Directory domain - only required for agencies using LDAP authentication'
})),
DataType: Type.String({
default: DATA_TYPE_CFS,
enum: [DATA_TYPE_CFS, DATA_TYPE_UNITS],
description: 'Calls for Service posts CFS incident locations, Units posts AVL unit locations'
}),
BoundingBox: Type.Optional(Type.String({
description: 'Bounding Box of acceptable geocoded locations as minLon,minLat,maxLon,maxLat - ie -105.4,39.5,-104.6,40.0. Geocoded results outside of this box are discarded'
})),
FallbackCoordinates: Type.Optional(Type.String({
description: 'Latitude,Longitude used for records without coordinates (and that could not be geocoded) - ie 39.7392,-104.9903'
})),
DEBUG: Type.Boolean({
default: false,
description: 'Print results in logs'
})
});
const CADDropdown = Type.Object({
UniqueIdentifier: Type.Optional(Nullable(Type.Union([Type.String(), Type.Number()]))),
Description: Type.Optional(Nullable(Type.String())),
FullDescription: Type.Optional(Nullable(Type.String())),
Code: Type.Optional(Nullable(Type.String())),
Abbreviation: Type.Optional(Nullable(Type.String())),
Level: Type.Optional(Nullable(Type.Union([Type.String(), Type.Number()]))),
ORI: Type.Optional(Nullable(Type.String()))
});
const CADAddress = Type.Object({
FreeFormAddress: Type.Optional(Nullable(Type.String())),
Street: Type.Optional(Nullable(Type.String())),
City: Type.Optional(Nullable(Type.String())),
Community: Type.Optional(Nullable(Type.String())),
State: Type.Optional(Nullable(Type.Union([Type.String(), CADDropdown]))),
Zip: Type.Optional(Nullable(Type.String())),
Latitude: Type.Optional(Nullable(Type.Number())),
Longitude: Type.Optional(Nullable(Type.Number())),
Lat: Type.Optional(Nullable(Type.Number())),
Lon: Type.Optional(Nullable(Type.Number()))
});
const CADIncidentCode = Type.Object({
IncidentCode: Type.Optional(Nullable(CADDropdown)),
IsPrimary: Type.Optional(Nullable(Type.Boolean())),
UniqueIdentifier: Type.Optional(Nullable(Type.Union([Type.String(), Type.Number()]))),
Description: Type.Optional(Nullable(Type.String())),
Code: Type.Optional(Nullable(Type.String()))
});
/**
* Pro Suite is configured per agency and the CFS record exposes a superset of
* these fields - everything is optional so a single missing or agency specific
* value never discards an otherwise usable call
*/
const CFSCore = Type.Object({
CFSNumber: Type.Optional(Nullable(Type.String())),
ExternalCFSNumber: Type.Optional(Nullable(Type.String())),
CallDateTime: Type.Optional(Nullable(Type.String())),
IncidentDateTime: Type.Optional(Nullable(Type.String())),
ClosedDateTime: Type.Optional(Nullable(Type.String())),
CurrentlyActive: Type.Optional(Nullable(Type.Boolean())),
UseCaution: Type.Optional(Nullable(Type.Boolean())),
DispatchAgency: Type.Optional(Nullable(CADDropdown)),
CFSStatus: Type.Optional(Nullable(CADDropdown)),
Priority: Type.Optional(Nullable(CADDropdown)),
Zone: Type.Optional(Nullable(CADDropdown)),
Beat: Type.Optional(Nullable(CADDropdown)),
IncidentCode: Type.Optional(Nullable(Type.Union([Type.Array(CADIncidentCode), CADIncidentCode]))),
Disposition: Type.Optional(Nullable(Type.Union([Type.Array(CADDropdown), CADDropdown]))),
CallTaker: Type.Optional(Nullable(CADDropdown)),
PrimaryOfficer: Type.Optional(Nullable(CADDropdown)),
GeneralAddress: Type.Optional(Nullable(CADAddress)),
Address: Type.Optional(Nullable(CADAddress)),
Location: Type.Optional(Nullable(CADAddress)),
NearestCrossStreet: Type.Optional(Nullable(Type.String())),
NearestIntersection: Type.Optional(Nullable(Type.String())),
Reporter: Type.Optional(Nullable(Type.Object({
FreeformFullName: Type.Optional(Nullable(Type.String())),
FromPhoneNumber: Type.Optional(Nullable(Type.String())),
ContactPhoneNumber: Type.Optional(Nullable(Type.String())),
HowReported: Type.Optional(Nullable(CADDropdown))
}))),
Units: Type.Optional(Nullable(Type.Array(Type.Object({
UnitID: Type.Optional(Nullable(Type.Union([Type.String(), Type.Number()]))),
Unit: Type.Optional(Nullable(CADDropdown)),
UnitStatus: Type.Optional(Nullable(CADDropdown))
}))))
});
/**
* The /units/search response is agency configurable and is documented only as
* "unit status, configuration and GPS location data" - the same permissive
* approach used for CFS records applies
*/
const CADUnit = Type.Object({
UniqueIdentifier: Type.Optional(Nullable(Type.Union([Type.String(), Type.Number()]))),
UnitID: Type.Optional(Nullable(Type.Union([Type.String(), Type.Number()]))),
UnitName: Type.Optional(Nullable(Type.String())),
Unit: Type.Optional(Nullable(CADDropdown)),
UnitStatus: Type.Optional(Nullable(CADDropdown)),
Status: Type.Optional(Nullable(CADDropdown)),
DispatchAgency: Type.Optional(Nullable(CADDropdown)),
Agency: Type.Optional(Nullable(CADDropdown)),
Zone: Type.Optional(Nullable(CADDropdown)),
Beat: Type.Optional(Nullable(CADDropdown)),
CFSNumber: Type.Optional(Nullable(Type.String())),
Personnel: Type.Optional(Nullable(Type.Union([Type.Array(CADDropdown), CADDropdown]))),
Latitude: Type.Optional(Nullable(Type.Number())),
Longitude: Type.Optional(Nullable(Type.Number())),
Heading: Type.Optional(Nullable(Type.Union([Type.String(), Type.Number()]))),
Speed: Type.Optional(Nullable(Type.Union([Type.String(), Type.Number()]))),
LocationDateTime: Type.Optional(Nullable(Type.String())),
Location: Type.Optional(Nullable(CADAddress)),
Address: Type.Optional(Nullable(CADAddress))
});
const SuggestResponse = Type.Object({
items: Type.Array(Type.Object({
text: Type.String(),
magicKey: Type.String()
}))
});
const ForwardResponse = Type.Object({
items: Type.Array(Type.Object({
address: Type.String(),
location: Type.Object({
x: Type.Number(),
y: Type.Number()
}),
score: Type.Number()
}))
});
const TokenResponse = Type.Object({
access_token: Type.String(),
token_type: Type.Optional(Type.String()),
expires_in: Type.Optional(Type.Integer())
});
// Pro Suite tokens default to a 24 hour lifetime and the token endpoint is
// aggressively rate limited - refresh early rather than on every call
const TOKEN_LIFETIME_MS = 24 * 60 * 60 * 1000;
const TOKEN_REFRESH_MARGIN_MS = 60 * 60 * 1000;
const PAGE_LIMIT = 100;
const MAX_PAGES = 20;
const STALE_MINUTES = 20;
// Required on every request and recorded in the Pro Suite audit trail
const FROM_HEADER = 'CloudTAK-ETL';
type Unknowns = Record<string, unknown>;
function isObject(value: unknown): value is Unknowns {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
/**
* Reduce a Pro Suite dropdown object - { UniqueIdentifier, Description, Code } -
* or an array of them to a human readable string
*/
function describe(value: unknown, depth = 0): string | null {
if (value === null || value === undefined || depth > 3) return null;
if (typeof value === 'string') return value.trim() || null;
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
if (Array.isArray(value)) {
const parts = value.map((entry) => describe(entry, depth + 1)).filter((part) => part !== null);
return parts.length ? parts.join(', ') : null;
}
if (!isObject(value)) return null;
for (const key of ['Description', 'FullDescription', 'Name', 'Abbreviation', 'Code', 'Level']) {
const found = describe(value[key], depth + 1);
if (found) return found;
}
// Wrapper objects such as { IncidentCode: { Description }, IsPrimary: true }
for (const nested of Object.values(value)) {
if (isObject(nested)) {
const found = describe(nested, depth + 1);
if (found) return found;
}
}
return null;
}
/**
* IncidentCode is returned as an array of { IncidentCode, IsPrimary } entries on
* some deployments and as a single dropdown on others
*/
function primaryIncident(value: unknown): string | null {
if (Array.isArray(value)) {
const primary = value.find((entry) => isObject(entry) && entry.IsPrimary === true);
return describe(primary ?? value[0]);
}
return describe(value);
}
function toCoordinate(value: unknown): number | null {
if (value === null || value === undefined || value === '') return null;
const num = Number(value);
if (!Number.isFinite(num) || num === 0) return null;
return num;
}
const COORDINATE_CONTAINERS = ['GeneralAddress', 'Address', 'Location', 'IncidentAddress'];
const LATITUDE_KEYS = ['Latitude', 'Lat'];
const LONGITUDE_KEYS = ['Longitude', 'Lon', 'Long'];
function pick(source: Unknowns, keys: string[]): number | null {
for (const key of keys) {
const found = toCoordinate(source[key]);
if (found !== null) return found;
}
return null;
}
/**
* Coordinates are carried either on the record itself (units) or on a nested
* address object (calls for service) depending on the endpoint
*/
function coordinates(record: Unknowns): [number, number] | null {
const candidates: Unknowns[] = [record];
for (const key of COORDINATE_CONTAINERS) {
const container = record[key];
if (isObject(container)) candidates.push(container);
}
for (const candidate of candidates) {
const lat = pick(candidate, LATITUDE_KEYS);
const lon = pick(candidate, LONGITUDE_KEYS);
if (lat !== null && lon !== null) return [lon, lat];
}
return null;
}
/**
* Records without a usable location are placed at the optional configured
* fallback so the call or unit is still visible on the map
*/
function fallback(env: Static<typeof InputSchema>): [number, number] | null {
if (!env.FallbackCoordinates) return null;
const [lat, lon] = env.FallbackCoordinates.split(',').map((part) => toCoordinate(part.trim()));
if (lat === null || lat === undefined || lon === null || lon === undefined) {
console.error(`not ok - invalid FallbackCoordinates: ${env.FallbackCoordinates}`);
return null;
}
return [lon, lat];
}
type BBox = [number, number, number, number];
/**
* Parse the optional BoundingBox env - minLon,minLat,maxLon,maxLat - returning
* null when unset so geocoded results are accepted anywhere
*/
function bbox(env: Static<typeof InputSchema>): BBox | null {
if (!env.BoundingBox) return null;
const parts = env.BoundingBox.split(',').map((part) => Number(part.trim()));
if (parts.length !== 4 || parts.some((part) => !Number.isFinite(part)) || parts[0] > parts[2] || parts[1] > parts[3]) {
console.error(`not ok - invalid BoundingBox: ${env.BoundingBox}`);
return null;
}
return parts as BBox;
}
function within(box: BBox | null, [lon, lat]: [number, number]): boolean {
if (!box) return true;
return lon >= box[0] && lon <= box[2] && lat >= box[1] && lat <= box[3];
}
function addressLine(record: Unknowns): string | null {
for (const key of COORDINATE_CONTAINERS) {
const container = record[key];
if (!isObject(container)) continue;
const freeform = describe(container.FreeFormAddress);
if (freeform) return freeform;
const parts = [
describe(container.Street),
describe(container.City) ?? describe(container.Community),
describe(container.State),
describe(container.Zip)
].filter((part) => part !== null);
if (parts.length) return parts.join(', ');
}
return null;
}
function timestamp(value: unknown, fallback: Date): Date {
if (typeof value !== 'string' || !value.trim()) return fallback;
const parsed = new Date(value);
return Number.isNaN(parsed.getTime()) ? fallback : parsed;
}
function remarks(lines: Array<string | null>): string | undefined {
const body = lines.filter((line) => line !== null).join('\n');
return body || undefined;
}
/**
* Search responses are wrapped in a paginated envelope keyed by the resource -
* ie { previous, next, cfs_cores: [] }
*/
function envelope(body: Unknowns, preferred: string): Unknowns[] {
const candidates: unknown[] = [body[preferred]];
for (const [key, value] of Object.entries(body)) {
if (key === 'previous' || key === 'next') continue;
candidates.push(value);
}
for (const candidate of candidates) {
if (Array.isArray(candidate)) return candidate.filter(isObject);
}
return [];
}
export default class Task extends ETL {
static name = 'etl-central-square'
static flow = [ DataFlowType.Incoming ];
static invocation = [ InvocationType.Schedule ];
static invocationDefaults = {
schedule: { enabled: true, cron: 'rate(1 minute)' }
};
async schema(
type: SchemaType = SchemaType.Input,
flow: DataFlowType = DataFlowType.Incoming
): Promise<TSchema> {
if (flow === DataFlowType.Incoming) {
if (type === SchemaType.Input) {
return InputSchema;
} else {
const env = await this.env(InputSchema);
return env.DataType === DATA_TYPE_UNITS ? CADUnit : CFSCore;
}
} else {
return Type.Object({});
}
}
async control(): Promise<void> {
const env = await this.env(InputSchema);
const base = new URL(env.BaseURL);
const fc: Static<typeof Feature.InputFeatureCollection> = {
type: 'FeatureCollection',
features: []
};
if (env.DataType === DATA_TYPE_UNITS) {
for (const record of await this.controlSearch(env, base, '/units/search', 'units', {})) {
const feat = this.controlUnit(env, record);
if (feat) fc.features.push(feat);
}
} else {
const geocoded = new Map<string, [number, number] | null>();
for (const record of await this.controlSearch(env, base, '/cfs_core/search', 'cfs_cores', { CurrentlyActive: true })) {
const feat = await this.controlCFS(env, record, geocoded);
if (feat) fc.features.push(feat);
}
}
console.log(`ok - obtained ${fc.features.length} features`);
await this.submit(fc);
}
/**
* Obtain a Bearer Token, reusing the cached token until an hour before it expires
* The Pro Suite token endpoint is rate limited and authenticating on every
* invocation will lock the integration out of API access
*/
async controlToken(env: Static<typeof InputSchema>, base: URL, force = false): Promise<string> {
const layer = await this.fetchLayer();
const ephemeral = layer.incoming?.ephemeral ?? {};
if (
!force
&& ephemeral.access_token
&& ephemeral.access_token_expires
&& Number(ephemeral.access_token_expires) > +new Date()
) {
return String(ephemeral.access_token);
}
console.log('ok - requesting new token');
const body = new URLSearchParams({
grant_type: 'password',
username: env.Username,
password: env.Password
});
if (env.Domain) {
body.append('domain', env.Domain);
body.append('client_id', env.Domain);
}
const res = await fetch(new URL('/api/token', base), {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
From: FROM_HEADER
},
body,
safeUrlAllow: [base.origin]
});
if (!res.ok) {
throw new Error(`Pro Suite Authentication Failed: ${res.status} ${await res.text()}`);
}
const token = await res.typed(TokenResponse);
// Agencies can shorten the token lifetime - never let the margin consume
// more than half of it or every invocation would re-authenticate
const lifetime = token.expires_in ? token.expires_in * 1000 : TOKEN_LIFETIME_MS;
const margin = Math.min(TOKEN_REFRESH_MARGIN_MS, lifetime / 2);
await this.setEphemeral({
access_token: token.access_token,
access_token_expires: String(+new Date() + lifetime - margin)
});
return token.access_token;
}
async controlSearch(
env: Static<typeof InputSchema>,
base: URL,
path: string,
key: string,
filter: Unknowns
): Promise<Unknowns[]> {
const records: Unknowns[] = [];
let token = await this.controlToken(env, base);
// The cached token can be revoked or expired early by the agency - allow
// a single re-authentication rather than failing the entire invocation
let refreshed = false;
for (let page = 0; page < MAX_PAGES; page++) {
const url = new URL(`/api/cad/latest${path}`, base);
url.searchParams.set('skip', String(page * PAGE_LIMIT));
url.searchParams.set('limit', String(PAGE_LIMIT));
let res = await this.controlPage(env, base, url, token, filter);
if (res.status === 401 && !refreshed) {
console.log('ok - cached token rejected, re-authenticating');
refreshed = true;
token = await this.controlToken(env, base, true);
res = await this.controlPage(env, base, url, token, filter);
}
if (!res.ok) {
throw new Error(`Pro Suite ${path} Failed: ${res.status} ${await res.text()}`);
}
const body = await res.json() as Unknowns;
if (env.DEBUG) console.error(`DEBUG - ${path}: ${JSON.stringify(body)}`);
const items = envelope(body, key);
records.push(...items);
if (!body.next || !items.length) break;
if (page === MAX_PAGES - 1) {
console.log(`ok - MAX_PAGES (${MAX_PAGES}) reached, ${path} results were truncated`);
}
}
return records;
}
async controlPage(
env: Static<typeof InputSchema>,
base: URL,
url: URL,
token: string,
filter: Unknowns
) {
return await fetch(url, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
From: FROM_HEADER
},
body: JSON.stringify(filter),
safeUrlAllow: [base.origin]
});
}
/**
* Geocode a free form address via the CloudTAK Search API - suggest returns a
* magicKey which forward resolves to a location. Results outside of the
* configured BoundingBox are rejected. Requires the search:read permission
*
* @returns [lon, lat] or null if no acceptable location was found
*/
async controlGeocode(
env: Static<typeof InputSchema>,
address: string,
cache: Map<string, [number, number] | null>
): Promise<[number, number] | null> {
const cached = cache.get(address);
if (cached !== undefined) return cached;
let geometry: [number, number] | null = null;
try {
const box = bbox(env);
const suggest = new URL('/api/search/suggest', this.etl.api);
suggest.searchParams.set('query', address);
suggest.searchParams.set('limit', '1');
if (box) {
suggest.searchParams.set('longitude', String((box[0] + box[2]) / 2));
suggest.searchParams.set('latitude', String((box[1] + box[3]) / 2));
}
const suggestion = this.type(SuggestResponse, await this.fetch(suggest)).items[0];
if (suggestion) {
const forward = new URL('/api/search/forward', this.etl.api);
forward.searchParams.set('query', suggestion.text);
forward.searchParams.set('magicKey', suggestion.magicKey);
forward.searchParams.set('limit', '1');
const result = this.type(ForwardResponse, await this.fetch(forward)).items[0];
if (result) {
const candidate: [number, number] = [result.location.x, result.location.y];
if (within(box, candidate)) {
geometry = candidate;
} else if (env.DEBUG) {
console.error(`DEBUG - geocoded "${address}" to ${candidate.join(',')} which is outside of BoundingBox - rejecting`);
}
}
}
} catch (err) {
console.error(`not ok - geocoding "${address}" failed: ${err instanceof Error ? err.message : String(err)}`);
}
if (env.DEBUG) console.error(`DEBUG - geocoded "${address}" => ${geometry ? geometry.join(',') : 'no acceptable result'}`);
cache.set(address, geometry);
return geometry;
}
async controlCFS(
env: Static<typeof InputSchema>,
record: Unknowns,
geocoded: Map<string, [number, number] | null> = new Map()
): Promise<Static<typeof Feature.InputFeature> | null> {
const number = describe(record.CFSNumber) ?? describe(record.ExternalCFSNumber);
let geometry = coordinates(record);
if (!geometry) {
const address = addressLine(record);
if (address) {
if (env.DEBUG) console.error(`DEBUG - CFS ${number ?? 'Unknown'} has no coordinates - geocoding "${address}"`);
geometry = await this.controlGeocode(env, address, geocoded);
}
}
if (!geometry) {
geometry = fallback(env);
if (env.DEBUG) console.error(`DEBUG - CFS ${number ?? 'Unknown'} has no coordinates${geometry ? ' - using fallback' : ''}`);
if (!geometry) return null;
}
if (!number) return null;
let cfs: Static<typeof CFSCore>;
try {
cfs = this.type(CFSCore, record);
} catch (err) {
console.error(`not ok - skipping CFS ${number}: ${err instanceof Error ? err.message : String(err)}`);
return null;
}
const now = new Date();
const start = timestamp(cfs.CallDateTime ?? cfs.IncidentDateTime, now);
const incident = primaryIncident(cfs.IncidentCode);
return {
id: `central-square-cfs-${number}`,
type: 'Feature',
properties: {
callsign: incident ? `${incident} (${number})` : number,
type: 'a-f-G-U-i',
how: 'h-g-i-g-o',
time: start.toISOString(),
start: start.toISOString(),
stale: new Date(now.getTime() + STALE_MINUTES * 60 * 1000).toISOString(),
remarks: remarks([
`CFS: ${number}`,
incident ? `Incident: ${incident}` : null,
describe(cfs.CFSStatus) ? `Status: ${describe(cfs.CFSStatus)}` : null,
describe(cfs.Priority) ? `Priority: ${describe(cfs.Priority)}` : null,
addressLine(record) ? `Address: ${addressLine(record)}` : null,
cfs.NearestCrossStreet ? `Cross Street: ${cfs.NearestCrossStreet}` : null,
cfs.NearestIntersection ? `Intersection: ${cfs.NearestIntersection}` : null,
describe(cfs.DispatchAgency) ? `Agency: ${describe(cfs.DispatchAgency)}` : null,
describe(cfs.Zone) ? `Zone: ${describe(cfs.Zone)}` : null,
describe(cfs.Beat) ? `Beat: ${describe(cfs.Beat)}` : null,
describe(cfs.Units) ? `Units: ${describe(cfs.Units)}` : null,
describe(cfs.PrimaryOfficer) ? `Primary Officer: ${describe(cfs.PrimaryOfficer)}` : null,
describe(cfs.Reporter?.FreeformFullName) ? `Reporter: ${describe(cfs.Reporter?.FreeformFullName)}` : null,
describe(cfs.Reporter?.ContactPhoneNumber ?? cfs.Reporter?.FromPhoneNumber) ? `Phone: ${describe(cfs.Reporter?.ContactPhoneNumber ?? cfs.Reporter?.FromPhoneNumber)}` : null,
describe(cfs.Disposition) ? `Disposition: ${describe(cfs.Disposition)}` : null,
cfs.UseCaution ? 'Use Caution' : null
]),
metadata: cfs
},
geometry: {
type: 'Point',
coordinates: geometry
}
};
}
controlUnit(
env: Static<typeof InputSchema>,
record: Unknowns
): Static<typeof Feature.InputFeature> | null {
const geometry = coordinates(record) ?? fallback(env);
const identifier = describe(record.UnitID)
?? describe(record.UniqueIdentifier)
?? describe(record.UnitName)
?? describe(record.Unit);
if (!geometry || !identifier) return null;
let unit: Static<typeof CADUnit>;
try {
unit = this.type(CADUnit, record);
} catch (err) {
console.error(`not ok - skipping Unit ${identifier}: ${err instanceof Error ? err.message : String(err)}`);
return null;
}
const now = new Date();
const start = timestamp(unit.LocationDateTime, now);
const callsign = describe(unit.UnitName) ?? describe(unit.Unit) ?? identifier;
return {
id: `central-square-unit-${identifier}`,
type: 'Feature',
properties: {
callsign,
type: 'a-f-G-E-V',
how: 'm-g',
time: start.toISOString(),
start: start.toISOString(),
stale: new Date(now.getTime() + STALE_MINUTES * 60 * 1000).toISOString(),
track: (unit.Speed !== undefined && unit.Speed !== null) || (unit.Heading !== undefined && unit.Heading !== null) ? {
speed: unit.Speed !== undefined && unit.Speed !== null ? String(unit.Speed) : undefined,
course: unit.Heading !== undefined && unit.Heading !== null ? String(unit.Heading) : undefined
} : undefined,
remarks: remarks([
`Unit: ${callsign}`,
describe(unit.UnitStatus ?? unit.Status) ? `Status: ${describe(unit.UnitStatus ?? unit.Status)}` : null,
describe(unit.DispatchAgency ?? unit.Agency) ? `Agency: ${describe(unit.DispatchAgency ?? unit.Agency)}` : null,
describe(unit.Zone) ? `Zone: ${describe(unit.Zone)}` : null,
describe(unit.Beat) ? `Beat: ${describe(unit.Beat)}` : null,
unit.CFSNumber ? `CFS: ${unit.CFSNumber}` : null,
describe(unit.Personnel) ? `Personnel: ${describe(unit.Personnel)}` : null,
addressLine(record) ? `Location: ${addressLine(record)}` : null
]),
metadata: unit
},
geometry: {
type: 'Point',
coordinates: geometry
}
};
}
}
await local(await Task.init(import.meta.url), import.meta.url);
export async function handler(event: Event = {}) {
return await internal(await Task.init(import.meta.url), event);
}