This commit is contained in:
pelikhan 2023-02-17 09:31:44 -08:00
Родитель 91f70277d4
Коммит 6eab7dcb79
3 изменённых файлов: 35 добавлений и 33 удалений

Просмотреть файл

@ -1,5 +1,5 @@
import deviceRegistryData from "../../jacdac-spec/dist/devices.json"
import { CHANGE } from "./constants"
import { CHANGE, DOCS_ROOT } from "./constants"
import { JDEventSource } from "./eventsource"
import { Flags } from "./flags"
import { cryptoRandomUint32 } from "./random"
@ -192,6 +192,39 @@ export class DeviceCatalog extends JDEventSource {
}
}
/**
* Generate a URL image for jacdac-docs
*/
export function deviceCatalogImage(
specification: jdspec.DeviceSpec | undefined,
size?: "avatar" | "lazy" | "catalog" | "preview" | "full" | "list",
docsRoot?: string
) {
const sz = size || "full"
const root = docsRoot || DOCS_ROOT
return (
specification &&
`${root}images/devices/${identifierToUrlPath(
specification.id
)}.${sz}.jpg`
)
}
/**
* Given an identifier, generate a url path
*/
export function identifierToUrlPath(id: string) {
if (!id) return id
const escape = (s: string) => s.replace(/[.:]/g, "").toLowerCase()
const parts = id.split(/-/g)
if (parts.length === 1) return id.replace(/[.:]/g, "").toLowerCase()
return `${parts.slice(0, -1).map(escape).join("-")}/${escape(
parts[parts.length - 1]
)}`
}
/**
* The device catalog
*/

Просмотреть файл

@ -41,7 +41,6 @@ import {
CMD_EVENT_COUNTER_MASK,
SRV_BOOTLOADER,
ERROR_NO_ACK,
DOCS_ROOT,
} from "./constants"
import { read32, bufferEq, read16 } from "./utils"
import { getNumber, NumberFormat } from "./buffer"
@ -49,7 +48,7 @@ import { BusInteractionMode, JDBus } from "./bus"
import { JDService } from "./service"
import { serviceClass, shortDeviceId } from "./pretty"
import { JDNode } from "./node"
import { identifierToUrlPath, isInstanceOf, isSensor } from "./spec"
import { isInstanceOf, isSensor } from "./spec"
import { FirmwareInfo, FirmwareUpdater } from "./flashing"
import { LEDController } from "./ledcontroller"
import { JDEventSource } from "./eventsource"
@ -1080,18 +1079,3 @@ export class JDDevice extends JDNode {
await this.sendPktWithAck(pkt)
}
}
export function deviceCatalogImage(
specification: jdspec.DeviceSpec | undefined,
size?: "avatar" | "lazy" | "catalog" | "preview" | "full" | "list",
docsRoot?: string
) {
const sz = size || "full"
const root = docsRoot || DOCS_ROOT
return (
specification &&
`${root}images/devices/${identifierToUrlPath(
specification.id
)}.${sz}.jpg`
)
}

Просмотреть файл

@ -79,21 +79,6 @@ export function serviceSpecifications() {
return _serviceSpecifications.slice(0)
}
/**
* @internal
*/
export function identifierToUrlPath(id: string) {
if (!id) return id
const escape = (s: string) => s.replace(/[.:]/g, "").toLowerCase()
const parts = id.split(/-/g)
if (parts.length === 1) return id.replace(/[.:]/g, "").toLowerCase()
return `${parts.slice(0, -1).map(escape).join("-")}/${escape(
parts[parts.length - 1]
)}`
}
/**
* Checks if classIdentifier is compatible with requiredClassIdentifier
* @category Specification