Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TAKMessage for from_proto [Major Version] #33

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// 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 @@
*/
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 @@
* 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 @@
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.

Loading