UNPKG

@tak-ps/node-cot

Version:

Lightweight JavaScript library for parsing and manipulating TAK messages

119 lines 3.83 kB
import fsp from 'node:fs/promises'; import xmljs from 'xml-js'; import TypeValidator from '../utils/type.js'; import MilSymType, { StandardIdentity, Domain } from '../utils/2525.js'; import { Type } from '@sinclair/typebox'; export const AugmentedType = Type.Object({ cot: Type.String(), desc: Type.String(), full: Type.Optional(Type.String()), '2525b': Type.Optional(Type.String()) }); export const TypeFormat_COT = Type.Object({ cot: Type.String(), desc: Type.String(), full: Type.Optional(Type.String()), }); export const TypeFormat_Weapon = Type.Object({ cot: Type.String(), desc: Type.String() }); export const TypeFormat_Relation = Type.Object({ cot: Type.String(), desc: Type.String() }); export const TypeFormat_Is = Type.Object({ what: Type.String(), match: Type.String() }); export const TypeFormat_How = Type.Object({ what: Type.String(), value: Type.String() }); export const TypeFormat = Type.Object({ types: Type.Object({ cot: Type.Array(Type.Object({ _attributes: TypeFormat_COT })), weapon: Type.Array(Type.Object({ _attributes: TypeFormat_Weapon })), relation: Type.Array(Type.Object({ _attributes: TypeFormat_Relation })), is: Type.Array(Type.Object({ _attributes: TypeFormat_Is })), how: Type.Array(Type.Object({ _attributes: TypeFormat_How })) }) }); export default class CoTTypes { cots; weapons; relations; is; how; constructor(cots, weapons, relations, is, how) { this.cots = cots; this.weapons = weapons; this.relations = relations; this.is = is; this.how = how; } static async load() { const xml = xmljs.xml2js(String(await fsp.readFile(new URL('cot-types.xml', import.meta.url))), { compact: true }); const types = TypeValidator.type(TypeFormat, xml); const cots = new Map(); for (const cot of types.types.cot) { cots.set(cot._attributes.cot, cot._attributes); } const weapons = new Map(); for (const weapon of types.types.weapon) { weapons.set(weapon._attributes.cot, weapon._attributes); } const relations = new Map(); for (const relation of types.types.relation) { relations.set(relation._attributes.cot, relation._attributes); } const is = new Map(); for (const i of types.types.is) { is.set(i._attributes.what, i._attributes); } const how = new Map(); for (const h of types.types.how) { how.set(h._attributes.value, h._attributes); } return new CoTTypes(cots, weapons, relations, is, how); } types(si, opts = {}) { if (!si) throw new TypeError('No StandardIdentity Parameter provided'); const filtered = new Set(); for (const cot of this.cots.values()) { if (opts.domain) { if (!cot.cot.startsWith(`${opts.domain}-`)) { continue; } ; } if (cot.cot.match(/^a-\.-/)) { const type = cot.cot.replace(/^a-\.-/, `a-${si}-`); filtered.add({ ...cot, cot: type, '2525b': MilSymType.is2525BConvertable(type) ? MilSymType.to2525B(type) : undefined }); } else { filtered.add({ ...cot, '2525b': MilSymType.is2525BConvertable(cot.cot) ? MilSymType.to2525B(cot.cot) : undefined }); } } return filtered; } } //# sourceMappingURL=cot-types.js.map