Skip to content

Instantly share code, notes, and snippets.

@ridomin
Last active January 24, 2023 04:06
Show Gist options
  • Save ridomin/bff78a3974f72b5314513998afcd0b47 to your computer and use it in GitHub Desktop.
Save ridomin/bff78a3974f72b5314513998afcd0b47 to your computer and use it in GitHub Desktop.
DtdlInterfaceInfo
// @ts-check
const filterMap = (om, dtmi, kind) => Object.entries(om).filter(e => e[1].EntityKind === kind && e[1].ChildOf === dtmi).map(e => e[1])
export class InterfaceInfo {
constructor (om, dtmi) {
/** @type import("./DtdlOM").DtdlObjectModel */
this.om = om
this.telemetries = filterMap(om, dtmi, "Telemetry")
this.properties = filterMap(om, dtmi, "Property")
this.commands = filterMap(om, dtmi, "Command")
this.components = filterMap(om, dtmi, "Component")
}
compoTels = compoId => filterMap(this.om, compoId, "Telemetry")
compoProps = compoId => filterMap(this.om, compoId, "Property")
compoCmds =compoId => filterMap(this.om, compoId, "Command")
}
import { InterfaceInfo } from "./interfaceInfo.js"
const app = document.getElementById('app')
const myDtdlOM = await (await fetch('sample-om.json')).json()
const applog = (a, b) => {
app.innerHTML += a + b + '\n'
}
const info = new InterfaceInfo(myDtdlOM, 'dtmi:samplesv3:Compos;1')
info.telemetries.forEach(t => applog('t:', t.name))
info.properties.forEach(p => applog('p:', p.name))
info.commands.forEach(c => applog('c:', c.name))
info.components.forEach(co => {
applog('co:', co.name, co.schema)
info.compoProps(co.schema).forEach(p => applog(' p:', p.name))
info.compoTels(co.schema).forEach(t => applog(' t:', t.name))
info.compoCmds(co.schema).forEach(c => applog(' c:', c.name))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment