Skip to content

Commit

Permalink
Merge pull request #33 from dfpc-coe/tak-message
Browse files Browse the repository at this point in the history
Use TAKMessage for from_proto [Major Version]
  • Loading branch information
ingalls committed Aug 14, 2024
2 parents 88a00c9 + f55534a commit 397d7e1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
54 changes: 29 additions & 25 deletions lib/cot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import fs from 'fs';
// GeoJSON Geospatial ops will truncate to the below
const COORDINATE_PRECISION = 6;

const RootMessage = await protobuf.load(new URL('./proto/cotevent.proto', import.meta.url).pathname);
const RootMessage = await protobuf.load(new URL('./proto/takmessage.proto', import.meta.url).pathname);

const pkg = JSON.parse(String(fs.readFileSync(new URL('../package.json', import.meta.url))));

Expand Down Expand Up @@ -436,29 +436,31 @@ export default class CoT {
*/
to_proto(version = 1): Uint8Array {
if (version < 1 || version > 1) throw new Err(400, null, `Unsupported Proto Version: ${version}`);
const ProtoMessage = RootMessage.lookupType(`atakmap.commoncommo.protobuf.v${version}.CotEvent`)
const ProtoMessage = RootMessage.lookupType(`atakmap.commoncommo.protobuf.v${version}.TakMessage`)

const detail = this.raw.event.detail;

const msg: any = {

Check warning on line 443 in lib/cot.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 443 in lib/cot.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
...this.raw.event._attributes,
sendTime: new Date(this.raw.event._attributes.time).getTime(),
startTime: new Date(this.raw.event._attributes.start).getTime(),
staleTime: new Date(this.raw.event._attributes.stale).getTime(),
...this.raw.event.point._attributes,
detail: {
xmlDetail: ''
cotEvent: {
...this.raw.event._attributes,
sendTime: new Date(this.raw.event._attributes.time).getTime(),
startTime: new Date(this.raw.event._attributes.start).getTime(),
staleTime: new Date(this.raw.event._attributes.stale).getTime(),
...this.raw.event.point._attributes,
detail: {
xmlDetail: ''
}
}
};

for (const key in detail) {
if(['contact', 'group', 'precisionlocation', 'status', 'takv', 'track'].includes(key)) {
msg.detail[key] = detail[key]._attributes;
msg.cotEvent.detail[key] = detail[key]._attributes;
delete detail[key]
}
}

msg.detail.xmlDetail = xmljs.js2xml({
msg.cotEvent.detail.xmlDetail = xmljs.js2xml({
...detail,
metadata: this.metadata
}, { compact: true });
Expand All @@ -470,16 +472,18 @@ export default class CoT {
* Parse an ATAK compliant Protobuf to a JS Object
*/
static from_proto(raw: Uint8Array, version = 1): CoT {
const ProtoMessage = RootMessage.lookupType(`atakmap.commoncommo.protobuf.v${version}.CotEvent`)
const ProtoMessage = RootMessage.lookupType(`atakmap.commoncommo.protobuf.v${version}.TakMessage`)

// TODO Type this
const msg: any = ProtoMessage.decode(raw);

Check warning on line 478 in lib/cot.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 478 in lib/cot.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

if (!msg.cotEvent) throw new Err(400, null, 'No cotEvent Data');

const detail: Record<string, any> = {};

Check warning on line 482 in lib/cot.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 482 in lib/cot.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
const metadata: Record<string, unknown> = {};
for (const key in msg.detail) {
for (const key in msg.cotEvent.detail) {
if (key === 'xmlDetail') {
const parsed: any = xmljs.xml2js(`<detail>${msg.detail.xmlDetail}</detail>`, { compact: true });
const parsed: any = xmljs.xml2js(`<detail>${msg.cotEvent.detail.xmlDetail}</detail>`, { compact: true });

Check warning on line 486 in lib/cot.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 486 in lib/cot.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
Object.assign(detail, parsed.detail);

if (detail.metadata) {
Expand All @@ -489,28 +493,28 @@ export default class CoT {
delete detail.metadata;
}
} else if (['contact', 'group', 'precisionlocation', 'status', 'takv', 'track'].includes(key)) {
detail[key] = { _attributes: msg.detail[key] };
detail[key] = { _attributes: msg.cotEvent.detail[key] };
}
}

const cot = new CoT({
event: {
_attributes: {
version: '2.0',
uid: msg.uid, type: msg.type, how: msg.how,
qos: msg.qos, opex: msg.opex, access: msg.access,
time: new Date(msg.sendTime.toNumber()).toISOString(),
start: new Date(msg.startTime.toNumber()).toISOString(),
stale: new Date(msg.staleTime.toNumber()).toISOString(),
uid: msg.cotEvent.uid, type: msg.cotEvent.type, how: msg.cotEvent.how,
qos: msg.cotEvent.qos, opex: msg.cotEvent.opex, access: msg.cotEvent.access,
time: new Date(msg.cotEvent.sendTime.toNumber()).toISOString(),
start: new Date(msg.cotEvent.startTime.toNumber()).toISOString(),
stale: new Date(msg.cotEvent.staleTime.toNumber()).toISOString(),
},
detail,
point: {
_attributes: {
lat: msg.lat,
lon: msg.lon,
hae: msg.hae,
le: msg.le,
ce: msg.ce,
lat: msg.cotEvent.lat,
lon: msg.cotEvent.lon,
hae: msg.cotEvent.hae,
le: msg.cotEvent.le,
ce: msg.cotEvent.ce,
},
}
}
Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 397d7e1

Please sign in to comment.