'use strict' ; import { DebugSession , InitializedEvent , TerminatedEvent , StoppedEvent , OutputEvent , Event , Thread } from 'vscode-debugadapter' ; import { DebugProtocol } from 'vscode-debugprotocol' ; import { readFileSync } from 'fs' ; import * as nodemcu from "./nodeMcuCommunication" ; import { TaskServer } from './debugger/TaskServer' ; import { DebuggerTask } from "./debugger/DebuggerTask" ; /**
 *
 */ export interface LaunchRequestArguments extends DebugProtocol . LaunchRequestArguments { port : string ; baud : number ; } /**
 *
 */ class SendToMcuTask implements DebuggerTask { private _terminal : nodemcu . NodeMcuCommunicator ; private _message : string ; constructor ( terminal , message ) { this . _terminal = terminal ; this . _message = message ; } /**
     *
     */ public run ( callback ) { this . _terminal . write ( this . _message + "\r\n" ) ; process . nextTick ( callback ) ; } /**
     *
     */ public dispose ( ) { } } /**
 *
 */ class NodeMcuDebugSession extends DebugSession { private static THREAD_ID = 1 ; private _terminal : nodemcu . NodeMcuCommunicator ; private _portclosing : boolean = false ; private _taskServer : TaskServer ; private _inputBuffer : string ; /**
     * Creates a new debug adapter that is used for one debug session.
     * We configure the default implementation of a debug adapter here.
     */ public constructor ( ) { super ( ) ; this . setDebuggerLinesStartAt1 ( false ) ; this . setDebuggerColumnsStartAt1 ( false ) ; this . _inputBuffer = "" ; } /**
     * The 'initialize' request is the first request called by the frontend
     * to interrogate the features the debug adapter provides.
     */ protected initializeRequest ( response , args ) { this . sendEvent ( new InitializedEvent ( ) ) ; response . body . supportsConfigurationDoneRequest = false ; response . body . supportsEvaluateForHovers = false ; response . body . supportsStepBack = false ; this . sendResponse ( response ) ; } /**
     *
     */ protected launchRequest ( response , args ) { nodemcu . NodeMcuCommunicator . detectPort ( args . port , ( error , ports ) => { if ( ports . length > 0 ) { this . sendEvent ( new OutputEvent ( `NodeMCU device found on: ` + ports [ 0 ] . comName + "\n" ) ) ; this . _terminal = this . createTerminal ( response , ports [ 0 ] . comName , args . baud ) ; this . _taskServer = new TaskServer ( this , this . _terminal ) ; } else { this . sendErrorResponse ( response , 0 , "NodeMCU device not found." ) ; this . shutdown ( ) ; } } ) ; } /**
     *
     */ private createTerminal ( response , comName , baud ) { let terminal = new nodemcu . NodeMcuCommunicator ( comName , baud ) ; terminal . registerOnPortDisconnect ( ( error ) => { if ( ! this . _portclosing ) { this . sendErrorResponse ( response , 0 , "NodeMCU device disconnected!" ) ; this . shutdown ( ) ; } else { this . sendEvent ( new OutputEvent ( "NodeMCU device disconnected.\n" ) ) ; } } ) ; terminal . registerOnError ( ( error ) => { this . sendErrorResponse ( response , 0 , "An error occured on NodeMCU device communication: " + error ) ; this . shutdown ( ) ; } ) ; terminal . registerOnDataReceived ( ( data ) => { this . _inputBuffer += data ; if ( this . _inputBuffer . indexOf ( "\n" ) >= 0 ) { let out = this . _inputBuffer . split ( /\r?\n/ ) ; for ( let i = 0 ; i < out . length - 1 ; i ++ ) { this . sendEvent ( new OutputEvent ( out [ i ] ) ) ; } this . _inputBuffer = out [ out . length - 1 ] ; } else if ( this . _inputBuffer . length > 120 ) { this . sendEvent ( new OutputEvent ( this . _inputBuffer ) ) ; this . _inputBuffer = "" ; } } ) ; terminal . registerOnPortOpen ( ( ) => { this . sendEvent ( new OutputEvent ( "NodeMCU device connected ...\n" ) ) ; this . sendResponse ( response ) ; } ) ; terminal . open ( ) ; return terminal ; } /**
     *
     */ protected disconnectRequest ( response , args ) { if ( this . _terminal != null ) { this . _portclosing = true ; this . _terminal . close ( ) ; this . _terminal = null ; } if ( this . _taskServer != null ) { this . _taskServer . dispose ( ) ; this . _taskServer = null ; } super . disconnectRequest ( response , args ) ; } protected evaluateRequest ( response , args ) { response . body = { result : "" , variablesReference : 0 } ; this . _taskServer . queueTask ( new SendToMcuTask ( this . _terminal , args . expression ) ) ; this . sendResponse ( response ) ; } protected threadsRequest ( response ) { response . body = { threads : [ new Thread ( NodeMcuDebugSession . THREAD_ID , "thread 1" ) ] } ; this . sendResponse ( response ) ; } protected setBreakPointsRequest ( response , args ) { } protected stackTraceRequest ( response , args ) { } protected scopesRequest ( response , args ) { } protected variablesRequest ( response , args ) { } protected continueRequest ( response , args ) { } protected nextRequest ( response , args ) { } protected stepBackRequest ( response , args ) { } } DebugSession . run ( NodeMcuDebugSession ) ;