import * as path from "path" ; import * as vscode from "vscode" ; import { ColorizedChannel } from "../common/colorizedChannel" ; import { Constants } from "../common/constants" ; import { ProcessError } from "../common/processError" ; import { Utility } from "../common/utility" ; import { MessageType , UI } from "../view/ui" ; import { UIConstants } from "../view/uiConstants" ; /**
 * DigitalTwin model type
 */ export enum ModelType { Interface = "Interface" , CapabilityModel = "Capability Model" } /**
 * DigitalTwin device model manager
 */ export class DeviceModelManager { param name static convertToModelType ( name ) { return ModelType [ name as keyof typeof ModelType ] ; } param name static generateModelId ( name ) { return `urn:{companyName}:${ name }:1` ; } param name param type static generateModelFileName ( name , type ) { const fileType = type . replace ( /\s+/g , Constants . EMPTY_STRING ) . toLowerCase ( ) ; return `${ name }.${ fileType }.json` ; } param type static getTemplateFileName ( type ) { return DeviceModelManager . generateModelFileName ( Constants . SAMPLE_FILE_NAME , type ) ; } private readonly component : string ; constructor ( private readonly context , private readonly outputChannel ) { this . component = Constants . DEVICE_MODEL_COMPONENT ; } param type async createModel ( type ) { const folder = await UI . selectRootFolder ( UIConstants . SELECT_ROOT_FOLDER_LABEL ) ; const name = await UI . inputModelName ( UIConstants . INPUT_MODEL_NAME_LABEL , type , folder ) ; const operation = `Create ${ type } ${ name } in folder ${ folder }` ; this . outputChannel . start ( operation , this . component ) ; let filePath ; try { filePath = await this . doCreateModel ( type , folder , name ) ; } catch ( error ) { throw new ProcessError ( operation , error , this . component ) ; } await UI . openAndShowTextDocument ( filePath ) ; UI . showNotification ( MessageType . Info , ColorizedChannel . formatMessage ( operation ) ) ; this . outputChannel . end ( operation , this . component ) ; } param type param folder param name private async doCreateModel ( type , folder , name ) { const modelId = DeviceModelManager . generateModelId ( name ) ; const filePath = path . join ( folder , DeviceModelManager . generateModelFileName ( name , type ) ) ; const templatePath = this . context . asAbsolutePath ( path . join ( Constants . RESOURCE_FOLDER , Constants . TEMPLATE_FOLDER , DeviceModelManager . getTemplateFileName ( type ) ) ) ; const replacement = new Map < string , string > ( ) ; replacement . set ( Constants . DIGITAL_TWIN_ID_PLACEHOLDER , modelId ) ; await Utility . createFileFromTemplate ( templatePath , filePath , replacement ) ; return filePath ; } }