import * as clipboardy from "clipboardy" ; import * as fs from "fs-plus" ; import { Guid } from "guid-typescript" ; import * as os from "os" ; import * as path from "path" ; import * as vscode from "vscode" ; import { BoardProvider } from "../boardProvider" ; import { TypeNotSupportedError } from "../common/Error/SystemErrors/TypeNotSupportedError" ; import { OperationCanceledError } from "../common/Error/OperationCanceledError" ; import { OSPlatform , ScaffoldType } from "../constants" ; import { TelemetryContext } from "../telemetry" ; import { Board } from "./Interfaces/Board" ; import { ArduinoDeviceBase } from "./ArduinoDeviceBase" ; import { DeviceType } from "./Interfaces/Device" ; import { TemplateFileInfo } from "./Interfaces/ProjectTemplate" ; import { AzureConfigFileHandler } from "./AzureComponentConfig" ; import { ComponentType } from "./Interfaces/Component" ; import { SystemResourceNotFoundError } from "../common/Error/SystemErrors/SystemResourceNotFoundError" ; import { AzureConfigNotFoundError } from "../common/Error/SystemErrors/AzureConfigNotFoundErrors" ; enum ConfigDeviceSettings { Copy = "Copy" , ConfigCRC = "Config CRC" } export class Esp32Device extends ArduinoDeviceBase { private static _boardId = "esp32" ; private componentId : string ; get id ( ) : string { return this . componentId ; } static get boardId ( ) : string { return Esp32Device . _boardId ; } get board ( ) : Board { const boardProvider = new BoardProvider ( this . boardFolderPath ) ; const esp32 = boardProvider . find ( { id : Esp32Device . _boardId } ) ; if ( ! esp32 ) { throw new SystemResourceNotFoundError ( "Esp32 Device board" , `board id ${ Esp32Device . _boardId }` , "board list" ) ; } return esp32 ; } get version ( ) : string { const platform = os . platform ( ) ; let packageRootPath = "" ; let version = "0.0.1" ; if ( platform === OSPlatform . WIN32 ) { const homeDir = os . homedir ( ) ; const localAppData = path . join ( homeDir , "AppData" , "Local" ) ; packageRootPath = path . join ( localAppData , "Arduino15" , "packages" , "esp32" , "hardware" , "esp32" ) ; } else { packageRootPath = "~/Library/Arduino15/packages/esp32/hardware/esp32" ; } if ( fs . existsSync ( packageRootPath ) ) { const versions = fs . readdirSync ( packageRootPath ) ; if ( versions [ 0 ] ) { version = versions [ 0 ] ; } } return version ; } name = "Esp32Arduino" ; private azureConfigFileHandler : AzureConfigFileHandler ; constructor ( context , channel , telemetryContext , devicePath , templateFiles ? ) { super ( context , devicePath , channel , telemetryContext , DeviceType . Esp32 ) ; this . channel = channel ; this . componentId = Guid . create ( ) . toString ( ) ; if ( templateFiles ) { this . templateFiles = templateFiles ; } const projectFolder = devicePath + "/.." ; this . azureConfigFileHandler = new AzureConfigFileHandler ( projectFolder ) ; } async create ( ) { this . createCore ( ) ; } async configDeviceSettings ( ) { const configSelectionItems = [ { label : "Copy device connection string" , description : "Copy device connection string" , detail : ConfigDeviceSettings . Copy } , { label : "Generate CRC for OTA" , description : "Generate Cyclic Redundancy Check(CRC) code for OTA Update" , detail : ConfigDeviceSettings . ConfigCRC } ] ; const configSelection = await vscode . window . showQuickPick ( configSelectionItems , { ignoreFocusOut : true , matchOnDescription : true , matchOnDetail : true , placeHolder : "Select an option" } ) ; if ( ! configSelection ) { throw new OperationCanceledError ( "ESP32 device setting type selection cancelled." ) ; } if ( configSelection . detail === ConfigDeviceSettings . ConfigCRC ) { await this . generateCrc ( this . channel ) ; } else if ( configSelection . detail === "Copy" ) { let deviceConnectionString ; const componentConfig = await this . azureConfigFileHandler . getComponentByType ( ScaffoldType . Workspace , ComponentType . IoTHubDevice ) ; if ( componentConfig ) { deviceConnectionString = componentConfig . componentInfo ?. values . iotHubDeviceConnectionString ; } if ( ! deviceConnectionString ) { throw new AzureConfigNotFoundError ( "iotHubDeviceConnectionString" ) ; } clipboardy . writeSync ( deviceConnectionString ) ; return ; } else { throw new TypeNotSupportedError ( "configuration type" , `${ configSelection . detail }` ) ; } } async preCompileAction ( ) { return true ; } async preUploadAction ( ) { return true ; } }