Compare commits
13 Commits
c232332d69
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
708bdc6bf0 | ||
|
|
44b02f4781 | ||
|
|
48ce7f35e1 | ||
|
|
9b80cf73d3 | ||
|
|
67a3a3bd78 | ||
|
|
e8de8f19c2 | ||
|
|
7d2076f165 | ||
|
|
10d5254a7e | ||
|
|
a67284afe5 | ||
|
|
4ac522ffc6 | ||
|
|
5456419bb3 | ||
|
|
9bcc49059e | ||
|
|
0c6199d8e4 |
@@ -1,32 +1,37 @@
|
|||||||
goog.loadJs('web', () => {
|
goog.loadJs('web', () => {
|
||||||
|
|
||||||
goog.require('layui');
|
goog.require('layui');
|
||||||
goog.require('dayjs.duration');
|
goog.require('dayjs.duration');
|
||||||
goog.require('Mixly.Boards');
|
goog.require('Mixly.Boards');
|
||||||
goog.require('Mixly.Debug');
|
goog.require('Mixly.Debug');
|
||||||
goog.require('Mixly.LayerExt');
|
goog.require('Mixly.LayerExt');
|
||||||
goog.require('Mixly.Msg');
|
goog.require('Mixly.Msg');
|
||||||
goog.require('Mixly.Workspace');
|
goog.require('Mixly.Workspace');
|
||||||
goog.require('Mixly.LayerProgress');
|
goog.require('Mixly.LayerProgress');
|
||||||
goog.require('Mixly.WebSocket.Serial');
|
goog.require('Mixly.WebSocket.Serial');
|
||||||
goog.provide('Mixly.WebSocket.ArduShell');
|
goog.require('Mixly.WebCompiler.ArduShell');
|
||||||
|
goog.provide('Mixly.WebSocket.ArduShell');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
Boards,
|
Boards,
|
||||||
Debug,
|
Debug,
|
||||||
LayerExt,
|
LayerExt,
|
||||||
Msg,
|
Msg,
|
||||||
Workspace,
|
Workspace,
|
||||||
LayerProgress,
|
LayerProgress,
|
||||||
WebSocket
|
WebSocket,
|
||||||
} = Mixly;
|
WebCompiler = {}
|
||||||
|
} = Mixly;
|
||||||
|
|
||||||
const { Serial } = WebSocket;
|
// 动态获取 WebCompiler.ArduShell,用于本地上传
|
||||||
|
const getWebCompilerArduShell = () => Mixly.WebCompiler?.ArduShell;
|
||||||
|
|
||||||
const { layer } = layui;
|
const { Serial } = WebSocket;
|
||||||
|
|
||||||
|
const { layer } = layui;
|
||||||
|
|
||||||
|
|
||||||
class WebSocketArduShell {
|
class WebSocketArduShell {
|
||||||
static {
|
static {
|
||||||
this.mixlySocket = null;
|
this.mixlySocket = null;
|
||||||
this.socket = null;
|
this.socket = null;
|
||||||
@@ -46,6 +51,12 @@ class WebSocketArduShell {
|
|||||||
this.shell = new WebSocketArduShell();
|
this.shell = new WebSocketArduShell();
|
||||||
const socket = this.socket;
|
const socket = this.socket;
|
||||||
|
|
||||||
|
// 同时初始化 WebCompiler.ArduShell,用于本地上传
|
||||||
|
const WebCompilerArduShell = getWebCompilerArduShell();
|
||||||
|
if (WebCompilerArduShell && !WebCompilerArduShell.getMixlySocket()) {
|
||||||
|
WebCompilerArduShell.init(mixlySocket);
|
||||||
|
}
|
||||||
|
|
||||||
socket.on('arduino.dataEvent', (data) => {
|
socket.on('arduino.dataEvent', (data) => {
|
||||||
if (data.length > 1000) {
|
if (data.length > 1000) {
|
||||||
return;
|
return;
|
||||||
@@ -64,6 +75,16 @@ class WebSocketArduShell {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
Debug.error(error);
|
Debug.error(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 过滤掉无关紧要的端口错误(因为已经在本地完成上传了)
|
||||||
|
if (typeof data === 'string' && (
|
||||||
|
data.includes('cannot open serial1') ||
|
||||||
|
data.includes('cannot open device "undefined"') ||
|
||||||
|
data.includes('No such file or directory, cannot open')
|
||||||
|
)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
statusBarTerminal.addValue(data);
|
statusBarTerminal.addValue(data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -92,6 +113,13 @@ class WebSocketArduShell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.initUpload = function () {
|
this.initUpload = function () {
|
||||||
|
// 委托给 WebCompiler.ArduShell 处理本地上传(使用 AVRUploader 或 esptool-js)
|
||||||
|
// 服务器无法访问用户本地的串口设备,必须在浏览器端完成上传
|
||||||
|
const WebCompilerArduShell = getWebCompilerArduShell();
|
||||||
|
if (WebCompilerArduShell) {
|
||||||
|
return WebCompilerArduShell.initUpload();
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.mixlySocket.isConnected()) {
|
if (!this.mixlySocket.isConnected()) {
|
||||||
layer.msg(Msg.Lang['websocket.offline'], { time: 1000 });
|
layer.msg(Msg.Lang['websocket.offline'], { time: 1000 });
|
||||||
return;
|
return;
|
||||||
@@ -152,8 +180,7 @@ class WebSocketArduShell {
|
|||||||
statusBarTerminal.addValue(`\n==${message}==\n`);
|
statusBarTerminal.addValue(`\n==${message}==\n`);
|
||||||
} else {
|
} else {
|
||||||
message = (this.shell.isCompiling() ? Msg.Lang['shell.compileSucc'] : Msg.Lang['shell.uploadSucc']);
|
message = (this.shell.isCompiling() ? Msg.Lang['shell.compileSucc'] : Msg.Lang['shell.uploadSucc']);
|
||||||
statusBarTerminal.addValue(`\n==${message}(${Msg.Lang['shell.timeCost']} ${
|
statusBarTerminal.addValue(`\n==${message}(${Msg.Lang['shell.timeCost']} ${dayjs.duration(time).format('HH:mm:ss.SSS')
|
||||||
dayjs.duration(time).format('HH:mm:ss.SSS')
|
|
||||||
})==\n`);
|
})==\n`);
|
||||||
}
|
}
|
||||||
layer.msg(message, { time: 1000 });
|
layer.msg(message, { time: 1000 });
|
||||||
@@ -268,8 +295,8 @@ class WebSocketArduShell {
|
|||||||
isCompiling() {
|
isCompiling() {
|
||||||
return this.#running_ && !this.#upload_;
|
return this.#running_ && !this.#upload_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocket.ArduShell = WebSocketArduShell;
|
WebSocket.ArduShell = WebSocketArduShell;
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -31,8 +31,8 @@ goog.loadJs('web', () => {
|
|||||||
|
|
||||||
const { SELECTED_BOARD, BOARD } = Config;
|
const { SELECTED_BOARD, BOARD } = Config;
|
||||||
|
|
||||||
// 引入本地 Web.BU 模块,用于 MicroPython 本地上传
|
// WebBU 需要在使用时动态获取,避免模块加载顺序问题
|
||||||
const WebBU = Mixly.Web?.BU;
|
const getWebBU = () => Mixly.Web?.BU;
|
||||||
|
|
||||||
const { Serial } = WebSocket;
|
const { Serial } = WebSocket;
|
||||||
|
|
||||||
@@ -74,6 +74,7 @@ goog.loadJs('web', () => {
|
|||||||
|
|
||||||
this.initBurn = function () {
|
this.initBurn = function () {
|
||||||
// MicroPython 板卡使用本地 Web Serial API 烧录,而非通过服务器
|
// MicroPython 板卡使用本地 Web Serial API 烧录,而非通过服务器
|
||||||
|
const WebBU = getWebBU();
|
||||||
if (SELECTED_BOARD?.language === 'MicroPython' && WebBU) {
|
if (SELECTED_BOARD?.language === 'MicroPython' && WebBU) {
|
||||||
return WebBU.initBurn();
|
return WebBU.initBurn();
|
||||||
}
|
}
|
||||||
@@ -111,6 +112,7 @@ goog.loadJs('web', () => {
|
|||||||
|
|
||||||
this.initUpload = function () {
|
this.initUpload = function () {
|
||||||
// MicroPython 板卡使用本地 Web Serial API 上传,而非通过服务器
|
// MicroPython 板卡使用本地 Web Serial API 上传,而非通过服务器
|
||||||
|
const WebBU = getWebBU();
|
||||||
if (SELECTED_BOARD?.language === 'MicroPython' && WebBU) {
|
if (SELECTED_BOARD?.language === 'MicroPython' && WebBU) {
|
||||||
return WebBU.initUpload();
|
return WebBU.initUpload();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,24 @@
|
|||||||
goog.loadJs('web', () => {
|
goog.loadJs('web', () => {
|
||||||
|
|
||||||
goog.require('path');
|
goog.require('path');
|
||||||
goog.require('Mustache');
|
goog.require('Mustache');
|
||||||
goog.require('Mixly.Env');
|
goog.require('Mixly.Env');
|
||||||
goog.require('Mixly.Events');
|
goog.require('Mixly.Events');
|
||||||
goog.require('Mixly.Msg');
|
goog.require('Mixly.Msg');
|
||||||
goog.require('Mixly.Ampy');
|
goog.require('Mixly.Ampy');
|
||||||
goog.require('Mixly.Web');
|
goog.require('Mixly.Web');
|
||||||
goog.provide('Mixly.Web.Ampy');
|
goog.provide('Mixly.Web.Ampy');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
Env,
|
Env,
|
||||||
Events,
|
Events,
|
||||||
Msg,
|
Msg,
|
||||||
Ampy,
|
Ampy,
|
||||||
Web
|
Web
|
||||||
} = Mixly;
|
} = Mixly;
|
||||||
|
|
||||||
|
|
||||||
class AmpyExt extends Ampy {
|
class AmpyExt extends Ampy {
|
||||||
static {
|
static {
|
||||||
this.LS = goog.readFileSync(path.join(Env.templatePath, 'python/ls.py'));
|
this.LS = goog.readFileSync(path.join(Env.templatePath, 'python/ls.py'));
|
||||||
this.LS_RECURSIVE = goog.readFileSync(path.join(Env.templatePath, 'python/ls-recursive.py'));
|
this.LS_RECURSIVE = goog.readFileSync(path.join(Env.templatePath, 'python/ls-recursive.py'));
|
||||||
@@ -228,9 +228,10 @@ class AmpyExt extends Ampy {
|
|||||||
if (!await this.exitRawREPL()) {
|
if (!await this.exitRawREPL()) {
|
||||||
throw new Error(Msg.Lang['ampy.exitRawREPLFailed']);
|
throw new Error(Msg.Lang['ampy.exitRawREPLFailed']);
|
||||||
}
|
}
|
||||||
/*if (!await this.exitREPL()) {
|
// 发送 Ctrl+D 触发软复位,让设备执行上传的 main.py
|
||||||
|
if (!await this.exitREPL()) {
|
||||||
throw new Error(Msg.Lang['ampy.exitREPLFailed']);
|
throw new Error(Msg.Lang['ampy.exitREPLFailed']);
|
||||||
}*/
|
}
|
||||||
await this.#device_.close();
|
await this.#device_.close();
|
||||||
this.#active_ = false;
|
this.#active_ = false;
|
||||||
}
|
}
|
||||||
@@ -417,13 +418,17 @@ class AmpyExt extends Ampy {
|
|||||||
|
|
||||||
async dispose() {
|
async dispose() {
|
||||||
this.#active_ = false;
|
this.#active_ = false;
|
||||||
|
if (this.#device_) {
|
||||||
await this.#device_.dispose();
|
await this.#device_.dispose();
|
||||||
this.#device_ = null;
|
this.#device_ = null;
|
||||||
|
}
|
||||||
|
if (this.#events_) {
|
||||||
this.#events_.reset();
|
this.#events_.reset();
|
||||||
this.#events_ = null;
|
this.#events_ = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Web.Ampy = AmpyExt;
|
Web.Ampy = AmpyExt;
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -11,38 +11,60 @@ export default class ShellArduino extends Shell {
|
|||||||
async compile(config) {
|
async compile(config) {
|
||||||
let arduino = _.merge({}, ARDUINO);
|
let arduino = _.merge({}, ARDUINO);
|
||||||
arduino = _.merge(arduino, config);
|
arduino = _.merge(arduino, config);
|
||||||
const command = [
|
|
||||||
`"${arduino.path.cli}"`,
|
// 构建参数数组,避免 shell 解析引号问题
|
||||||
|
const args = [
|
||||||
'compile',
|
'compile',
|
||||||
'-b', arduino.key,
|
'-b', arduino.key,
|
||||||
'--config-file', `"${arduino.path.config}"`,
|
'--config-file', arduino.path.config,
|
||||||
'--verbose',
|
'--verbose'
|
||||||
'--libraries', `"${arduino.path.libraries.join('","')}"`,
|
];
|
||||||
'--build-path', `"${arduino.path.build}"`,
|
|
||||||
'--output-dir', `"${arduino.path.output}"`,
|
// 为每个 library 路径添加参数
|
||||||
`"${arduino.path.code}"`,
|
// 注意:config.json 中的路径是指向包含多个库的集合目录,所以必须用 --libraries
|
||||||
|
// 如果用 --library,arduino-cli 会尝试把该目录当作单个库处理,导致找不到头文件报错
|
||||||
|
for (const lib of arduino.path.libraries) {
|
||||||
|
args.push('--libraries', lib);
|
||||||
|
}
|
||||||
|
|
||||||
|
args.push(
|
||||||
|
'--build-path', arduino.path.build,
|
||||||
|
'--output-dir', arduino.path.output,
|
||||||
|
arduino.path.code,
|
||||||
'--no-color'
|
'--no-color'
|
||||||
].join(' ');
|
);
|
||||||
return this.execUntilClosed(command, { maxBuffer: 4096 * 1000000 });
|
|
||||||
|
return this.execFileUntilClosed(arduino.path.cli, args, { maxBuffer: 4096 * 1000000 });
|
||||||
}
|
}
|
||||||
|
|
||||||
async upload(config) {
|
async upload(config) {
|
||||||
let arduino = _.merge({}, ARDUINO);
|
let arduino = _.merge({}, ARDUINO);
|
||||||
arduino = _.merge(arduino, config);
|
arduino = _.merge(arduino, config);
|
||||||
const command = [
|
|
||||||
`"${arduino.path.cli}"`,
|
// 构建参数数组,避免 shell 解析引号问题
|
||||||
|
const args = [
|
||||||
'compile',
|
'compile',
|
||||||
'--upload',
|
'--upload',
|
||||||
'-p', arduino.port,
|
'-p', arduino.port,
|
||||||
'-b', arduino.key,
|
'-b', arduino.key,
|
||||||
'--config-file', `"${arduino.path.config}"`,
|
'--config-file', arduino.path.config,
|
||||||
'--verbose',
|
'--verbose'
|
||||||
'--libraries', `"${arduino.path.libraries.join('","')}"`,
|
];
|
||||||
'--build-path', `"${arduino.path.build}"`,
|
|
||||||
'--output-dir', `"${arduino.path.output}"`,
|
// 为每个 library 路径添加参数
|
||||||
`"${arduino.path.code}"`,
|
// 注意:config.json 中的路径是指向包含多个库的集合目录,所以必须用 --libraries
|
||||||
|
// 如果用 --library,arduino-cli 会尝试把该目录当作单个库处理,导致找不到头文件报错
|
||||||
|
for (const lib of arduino.path.libraries) {
|
||||||
|
args.push('--libraries', lib);
|
||||||
|
}
|
||||||
|
|
||||||
|
args.push(
|
||||||
|
'--build-path', arduino.path.build,
|
||||||
|
'--output-dir', arduino.path.output,
|
||||||
|
arduino.path.code,
|
||||||
'--no-color'
|
'--no-color'
|
||||||
].join(' ');
|
);
|
||||||
return this.execUntilClosed(command, { maxBuffer: 4096 * 1000000 });
|
|
||||||
|
return this.execFileUntilClosed(arduino.path.cli, args, { maxBuffer: 4096 * 1000000 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -173,6 +173,12 @@ export default class Socket {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('micropython.burn', async (config, callback) => {
|
socket.on('micropython.burn', async (config, callback) => {
|
||||||
|
// 检查端口是否是有效的系统设备路径(而非前端内部标识符如 serial1)
|
||||||
|
if (config.port && !config.port.startsWith('/dev/') && !config.port.includes(':')) {
|
||||||
|
console.log(`[MicroPython] 忽略无效端口请求: ${config.port},应使用本地 Web Serial API`);
|
||||||
|
callback([null, { code: 0, time: 0 }]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const shell = this.#shellMicroPython_.getItem(socket.id);
|
const shell = this.#shellMicroPython_.getItem(socket.id);
|
||||||
const [error, result] = await to(shell.burn(config));
|
const [error, result] = await to(shell.burn(config));
|
||||||
error && Debug.error(error);
|
error && Debug.error(error);
|
||||||
@@ -180,6 +186,12 @@ export default class Socket {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('micropython.upload', async (config, callback) => {
|
socket.on('micropython.upload', async (config, callback) => {
|
||||||
|
// 检查端口是否是有效的系统设备路径(而非前端内部标识符如 serial1)
|
||||||
|
if (config.port && !config.port.startsWith('/dev/') && !config.port.includes(':')) {
|
||||||
|
console.log(`[MicroPython] 忽略无效端口请求: ${config.port},应使用本地 Web Serial API`);
|
||||||
|
callback([null, { code: 0, time: 0 }]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const shell = this.#shellMicroPython_.getItem(socket.id);
|
const shell = this.#shellMicroPython_.getItem(socket.id);
|
||||||
let { filePath = '', libraries = {} } = config;
|
let { filePath = '', libraries = {} } = config;
|
||||||
filePath = MString.tpl(filePath, {
|
filePath = MString.tpl(filePath, {
|
||||||
@@ -250,8 +262,16 @@ export default class Socket {
|
|||||||
error2 && Debug.error(error2);
|
error2 && Debug.error(error2);
|
||||||
let [error3,] = await to(fsExtra.outputFile(config.path.code, config.code));
|
let [error3,] = await to(fsExtra.outputFile(config.path.code, config.code));
|
||||||
error3 && Debug.error(error3);
|
error3 && Debug.error(error3);
|
||||||
const [error, result] = await to(shell.upload(config));
|
const [error, result] = await to(shell.compile(config));
|
||||||
error && Debug.error(error);
|
if (error) {
|
||||||
|
Debug.error(error);
|
||||||
|
callback([error, result]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (result.code === 0) {
|
||||||
|
const buildPath = config.path.build;
|
||||||
|
result.files = await Boards.getFiles(config.key, buildPath);
|
||||||
|
}
|
||||||
callback([error, result]);
|
callback([error, result]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user