1
+ /*---------------------------------------------------------
2
+ * Copyright (C) Microsoft Corporation. All rights reserved.
3
+ *--------------------------------------------------------*/
4
+
5
+ 'use strict' ;
6
+
7
+ import {
8
+ DebugSession ,
9
+ InitializedEvent , TerminatedEvent , StoppedEvent , OutputEvent , Event ,
10
+ Thread
11
+ } from 'vscode-debugadapter' ;
12
+
13
+ import { DebugProtocol } from 'vscode-debugprotocol' ;
14
+ import { readFileSync } from 'fs' ;
15
+ import { basename } from 'path' ;
16
+
17
+ import * as nodemcu from "./nodeMcuCommunication" ;
18
+
19
+ export interface LaunchRequestArguments extends DebugProtocol . LaunchRequestArguments {
20
+ /** An absolute path to the program to debug. */
21
+ program : string ;
22
+ }
23
+
24
+ class NodeMcuDebugSession extends DebugSession {
25
+
26
+ // we don't support multiple threads, so we can use a hardcoded ID for the default thread
27
+ private static THREAD_ID = 1 ;
28
+
29
+ // the contents (= lines) of the one and only file
30
+ private _sourceLines = new Array < string > ( ) ;
31
+
32
+ private _terminal : nodemcu . NodeMcuCommunicator ;
33
+
34
+ private _portclosing : boolean = false ;
35
+
36
+ /**
37
+ * Creates a new debug adapter that is used for one debug session.
38
+ * We configure the default implementation of a debug adapter here.
39
+ */
40
+ public constructor ( ) {
41
+ super ( ) ;
42
+
43
+ // this debugger uses zero-based lines and columns
44
+ this . setDebuggerLinesStartAt1 ( false ) ;
45
+ this . setDebuggerColumnsStartAt1 ( false ) ;
46
+ }
47
+
48
+
49
+ /**
50
+ * The 'initialize' request is the first request called by the frontend
51
+ * to interrogate the features the debug adapter provides.
52
+ */
53
+ protected initializeRequest ( response : DebugProtocol . InitializeResponse , args : DebugProtocol . InitializeRequestArguments ) : void {
54
+
55
+ // since this debug adapter can accept configuration requests like 'setBreakpoint' at any time,
56
+ // we request them early by sending an 'initializeRequest' to the frontend.
57
+ // The frontend will end the configuration sequence by calling 'configurationDone' request.
58
+ this . sendEvent ( new InitializedEvent ( ) ) ;
59
+
60
+ // This debug adapter implements the configurationDoneRequest.
61
+ response . body . supportsConfigurationDoneRequest = false ;
62
+
63
+ // make VS Code to use 'evaluate' when hovering over source
64
+ response . body . supportsEvaluateForHovers = false ;
65
+
66
+ // make VS Code to show a 'step back' button
67
+ response . body . supportsStepBack = false ;
68
+
69
+ this . sendResponse ( response ) ;
70
+ }
71
+
72
+ protected launchRequest ( response : DebugProtocol . LaunchResponse , args : LaunchRequestArguments ) : void {
73
+
74
+ let sourceCode = readFileSync ( args . program ) . toString ( ) ;
75
+ this . _sourceLines = sourceCode . split ( '\n' ) ;
76
+
77
+ nodemcu . NodeMcuCommunicator . detectPort ( ( error : string , ports : nodemcu . PortInformation [ ] ) => {
78
+
79
+ if ( ports . length > 0 ) {
80
+ this . sendEvent ( new OutputEvent ( `NodeMCU device found on: ` + ports [ 0 ] . comName + "\n" ) ) ;
81
+ this . _terminal = new nodemcu . NodeMcuCommunicator ( ports [ 0 ] . comName ) ;
82
+
83
+ this . _terminal . registerOnPortDisconnect ( ( error : string ) => {
84
+ if ( ! this . _portclosing ) {
85
+ this . sendErrorResponse ( response , 0 , "NodeMCU device disconnected" ) ;
86
+ this . shutdown ( ) ;
87
+ }
88
+ } ) ;
89
+
90
+ this . _terminal . registerOnError ( ( error : string ) => {
91
+ this . sendErrorResponse ( response , 0 , "An error occured on NodeMCU device communication: " + error ) ;
92
+ } ) ;
93
+
94
+ this . _terminal . registerOnDataReceived ( ( data : string ) => {
95
+ this . sendEvent ( new OutputEvent ( data + "\n" ) ) ;
96
+ } ) ;
97
+
98
+ this . _terminal . registerOnPortOpen ( ( ) => {
99
+ this . sendEvent ( new OutputEvent ( "Port opened\n" ) ) ;
100
+ this . sendEvent ( new OutputEvent ( "The file is uploading to NodeMCU device\n" ) ) ;
101
+ this . _terminal . uploadFile ( this . _sourceLines , basename ( args . program ) ) ;
102
+ } ) ;
103
+
104
+ this . _terminal . open ( ) ;
105
+ } else {
106
+
107
+ this . sendErrorResponse ( response , 0 , "NodeMCU device not found" ) ;
108
+ this . shutdown ( ) ;
109
+ }
110
+ } ) ;
111
+ }
112
+
113
+ protected disconnectRequest ( response : DebugProtocol . DisconnectResponse , args : DebugProtocol . DisconnectArguments ) : void {
114
+ if ( this . _terminal != null ) {
115
+ this . _portclosing = true ;
116
+ this . _terminal . close ( ) ;
117
+ }
118
+
119
+ super . disconnectRequest ( response , args ) ;
120
+ }
121
+
122
+ protected evaluateRequest ( response : DebugProtocol . EvaluateResponse , args : DebugProtocol . EvaluateArguments ) : void {
123
+ response . body = {
124
+ result : "" ,
125
+ variablesReference : 0
126
+ } ;
127
+
128
+ this . _terminal . write ( args . expression ) ;
129
+
130
+ this . sendResponse ( response ) ;
131
+ }
132
+
133
+ protected threadsRequest ( response : DebugProtocol . ThreadsResponse ) : void {
134
+
135
+ // return the default thread
136
+ response . body = {
137
+ threads : [
138
+ new Thread ( NodeMcuDebugSession . THREAD_ID , "thread 1" )
139
+ ]
140
+ } ;
141
+ this . sendResponse ( response ) ;
142
+ }
143
+
144
+ protected setBreakPointsRequest ( response : DebugProtocol . SetBreakpointsResponse , args : DebugProtocol . SetBreakpointsArguments ) : void {
145
+ }
146
+
147
+ protected stackTraceRequest ( response : DebugProtocol . StackTraceResponse , args : DebugProtocol . StackTraceArguments ) : void {
148
+ }
149
+
150
+ protected scopesRequest ( response : DebugProtocol . ScopesResponse , args : DebugProtocol . ScopesArguments ) : void {
151
+ }
152
+
153
+ protected variablesRequest ( response : DebugProtocol . VariablesResponse , args : DebugProtocol . VariablesArguments ) : void {
154
+ }
155
+
156
+ protected continueRequest ( response : DebugProtocol . ContinueResponse , args : DebugProtocol . ContinueArguments ) : void {
157
+ }
158
+
159
+ protected nextRequest ( response : DebugProtocol . NextResponse , args : DebugProtocol . NextArguments ) : void {
160
+ }
161
+
162
+ protected stepBackRequest ( response : DebugProtocol . StepBackResponse , args : DebugProtocol . StepBackArguments ) : void {
163
+ }
164
+ }
165
+
166
+ DebugSession . run ( NodeMcuDebugSession ) ;
0 commit comments