-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathcore-service-impl.ts
157 lines (140 loc) · 6.83 KB
/
core-service-impl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { FileUri } from '@theia/core/lib/node/file-uri';
import { inject, injectable } from 'inversify';
import { dirname } from 'path';
import { CoreService } from '../common/protocol/core-service';
import { CompileReq, CompileResp } from './cli-protocol/commands/compile_pb';
import { CoreClientProvider } from './core-client-provider';
import { UploadReq, UploadResp, BurnBootloaderReq, BurnBootloaderResp, UploadUsingProgrammerReq, UploadUsingProgrammerResp } from './cli-protocol/commands/upload_pb';
import { OutputService } from '../common/protocol/output-service';
import { NotificationServiceServer } from '../common/protocol';
import { ClientReadableStream } from '@grpc/grpc-js';
import { ArduinoCoreClient } from './cli-protocol/commands/commands_grpc_pb';
import { firstToUpperCase, firstToLowerCase } from '../common/utils';
@injectable()
export class CoreServiceImpl implements CoreService {
@inject(CoreClientProvider)
protected readonly coreClientProvider: CoreClientProvider;
@inject(OutputService)
protected readonly outputService: OutputService;
@inject(NotificationServiceServer)
protected readonly notificationService: NotificationServiceServer;
async compile(options: CoreService.Compile.Options): Promise<void> {
this.outputService.append({ name: 'compile', chunk: 'Compile...\n' + JSON.stringify(options, null, 2) + '\n--------------------------\n' });
const { sketchUri, fqbn } = options;
const sketchFilePath = FileUri.fsPath(sketchUri);
const sketchpath = dirname(sketchFilePath);
const coreClient = await this.coreClientProvider.client();
if (!coreClient) {
return;
}
const { client, instance } = coreClient;
const compilerReq = new CompileReq();
compilerReq.setInstance(instance);
compilerReq.setSketchpath(sketchpath);
if (fqbn) {
compilerReq.setFqbn(fqbn);
}
compilerReq.setOptimizefordebug(options.optimizeForDebug);
compilerReq.setPreprocess(false);
compilerReq.setVerbose(true);
compilerReq.setQuiet(false);
const result = client.compile(compilerReq);
try {
await new Promise<void>((resolve, reject) => {
result.on('data', (cr: CompileResp) => {
this.outputService.append({ name: 'compile', chunk: Buffer.from(cr.getOutStream_asU8()).toString() });
this.outputService.append({ name: 'compile', chunk: Buffer.from(cr.getErrStream_asU8()).toString() });
});
result.on('error', error => reject(error));
result.on('end', () => resolve());
});
this.outputService.append({ name: 'compile', chunk: '\n--------------------------\nCompilation complete.\n' });
} catch (e) {
this.outputService.append({ name: 'compile', chunk: `Compilation error: ${e}\n`, severity: 'error' });
throw e;
}
}
async upload(options: CoreService.Upload.Options): Promise<void> {
await this.doUpload(options, () => new UploadReq(), (client, req) => client.upload(req));
}
async uploadUsingProgrammer(options: CoreService.Upload.Options): Promise<void> {
await this.doUpload(options, () => new UploadUsingProgrammerReq(), (client, req) => client.uploadUsingProgrammer(req), 'upload using programmer');
}
protected async doUpload(
options: CoreService.Upload.Options,
requestProvider: () => UploadReq | UploadUsingProgrammerReq,
responseHandler: (client: ArduinoCoreClient, req: UploadReq | UploadUsingProgrammerReq) => ClientReadableStream<UploadResp | UploadUsingProgrammerResp>,
task: string = 'upload'): Promise<void> {
await this.compile(options);
const chunk = firstToUpperCase(task) + '...\n';
this.outputService.append({ name: 'upload', chunk: chunk + JSON.stringify(options, null, 2) + '\n--------------------------\n' });
const { sketchUri, fqbn, port, programmer } = options;
const sketchFilePath = FileUri.fsPath(sketchUri);
const sketchpath = dirname(sketchFilePath);
const coreClient = await this.coreClientProvider.client();
if (!coreClient) {
return;
}
const { client, instance } = coreClient;
const req = requestProvider();
req.setInstance(instance);
req.setSketchPath(sketchpath);
if (fqbn) {
req.setFqbn(fqbn);
}
if (port) {
req.setPort(port);
}
if (programmer) {
req.setProgrammer(programmer.id);
}
const result = responseHandler(client, req);
try {
await new Promise<void>((resolve, reject) => {
result.on('data', (resp: UploadResp) => {
this.outputService.append({ name: task, chunk: Buffer.from(resp.getOutStream_asU8()).toString() });
this.outputService.append({ name: task, chunk: Buffer.from(resp.getErrStream_asU8()).toString() });
});
result.on('error', error => reject(error));
result.on('end', () => resolve());
});
this.outputService.append({ name: 'upload', chunk: '\n--------------------------\n' + firstToLowerCase(task) + ' complete.\n' });
} catch (e) {
this.outputService.append({ name: 'upload', chunk: `${firstToUpperCase(task)} error: ${e}\n`, severity: 'error' });
throw e;
}
}
async burnBootloader(options: CoreService.Bootloader.Options): Promise<void> {
const coreClient = await this.coreClientProvider.client();
if (!coreClient) {
return;
}
const { fqbn, port, programmer } = options;
const { client, instance } = coreClient;
const burnReq = new BurnBootloaderReq();
burnReq.setInstance(instance);
if (fqbn) {
burnReq.setFqbn(fqbn);
}
if (port) {
burnReq.setPort(port);
}
if (programmer) {
burnReq.setProgrammer(programmer.id);
}
const result = client.burnBootloader(burnReq);
try {
await new Promise<void>((resolve, reject) => {
result.on('data', (resp: BurnBootloaderResp) => {
this.outputService.append({ name: 'bootloader', chunk: Buffer.from(resp.getOutStream_asU8()).toString() });
this.outputService.append({ name: 'bootloader', chunk: Buffer.from(resp.getErrStream_asU8()).toString() });
});
result.on('error', error => reject(error));
result.on('end', () => resolve());
});
} catch (e) {
this.outputService.append({ name: 'bootloader', chunk: `Error while burning the bootloader: ${e}\n`, severity: 'error' });
throw e;
}
}
}