初始化提交

This commit is contained in:
王立帮
2024-07-19 10:16:00 +08:00
parent 4c7b571f20
commit 4a2d56dcc4
7084 changed files with 741212 additions and 63 deletions

View File

@@ -0,0 +1,228 @@
goog.loadJs('web', () => {
goog.require('layui');
goog.require('Mixly.Env');
goog.require('Mixly.LayerExt');
goog.require('Mixly.Config');
goog.require('Mixly.Title');
goog.require('Mixly.Boards');
goog.require('Mixly.MFile');
goog.require('Mixly.MArray');
goog.require('Mixly.Msg');
goog.require('Mixly.WebSocket.Socket');
goog.require('Mixly.WebSocket.Serial');
goog.provide('Mixly.WebSocket.ArduShell');
const {
Env,
LayerExt,
Title,
Boards,
MFile,
MArray,
Msg,
Config
} = Mixly;
const { BOARD, SOFTWARE, USER } = Config;
const {
Socket,
ArduShell,
Serial
} = Mixly.WebSocket;
/**
* @function 编译
* @description 开始一个编译过程
* @return void
*/
ArduShell.initCompile = () => {
Socket.connect((WS) => {
layer.closeAll();
}, () => {
ArduShell.compile();
});
}
/**
* @function 编译
* @description 开始一个编译过程
* @return void
*/
ArduShell.compile = () => {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
mainStatusBarTabs.changeTo('output');
ArduShell.compiling = true;
ArduShell.uploading = false;
const boardType = Boards.getSelectedBoardCommandParam();
mainStatusBarTabs.show();
const layerNum = layer.open({
type: 1,
title: Msg.Lang['shell.compiling'] + "...",
content: $('#mixly-loader-div'),
shade: LayerExt.SHADE_NAV,
resize: false,
closeBtn: 0,
success: (layero, index) => {
$(".layui-layer-page").css("z-index", "198910151");
$("#mixly-loader-btn").off("click").click(() => {
$("#mixly-loader-btn").css('display', 'none');
layer.title(Msg.Lang['shell.aborting'] + '...', index);
ArduShell.cancel();
});
statusBarTerminal.setValue(Msg.Lang['shell.compiling'] + "...\n");
const code = MFile.getCode();
Socket.sendCommand({
obj: 'ArduShell',
func: 'compile',
args: [ index, boardType, code ]
});
},
end: () => {
$('#mixly-loader-div').css('display', 'none');
$("layui-layer-shade" + layerNum).remove();
$("#mixly-loader-btn").off("click");
$("#mixly-loader-btn").css('display', 'inline-block');
}
});
}
/**
* @function 初始化上传
* @description 关闭已打开的串口,获取当前所连接的设备数,然后开始上传程序
* @return void
*/
ArduShell.initUpload = () => {
Socket.connect((WS) => {
layer.closeAll();
}, () => {
ArduShell.compiling = false;
ArduShell.uploading = true;
const boardType = Boards.getSelectedBoardCommandParam();
const uploadType = Boards.getSelectedBoardConfigParam('upload_method');
let port = Serial.getSelectedPortName();
switch (uploadType) {
case 'STLinkMethod':
case 'jlinkMethod':
case 'usb':
port = 'None';
break;
}
if (port) {
ArduShell.upload(boardType, port);
} else {
layer.msg(Msg.Lang['statusbar.serial.noDevice'], {
time: 1000
});
}
});
}
/**
* @function 上传程序
* @description 通过所选择串口号开始一个上传过程
* @return void
*/
ArduShell.upload = (boardType, port) => {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
mainStatusBarTabs.changeTo("output");
const layerNum = layer.open({
type: 1,
title: Msg.Lang['shell.uploading'] + "...",
content: $('#mixly-loader-div'),
shade: LayerExt.SHADE_NAV,
resize: false,
closeBtn: 0,
success: function (layero, index) {
$(".layui-layer-page").css("z-index", "198910151");
$("#mixly-loader-btn").off("click").click(() => {
$("#mixly-loader-btn").css('display', 'none');
layer.title(Msg.Lang['shell.aborting'] + '...', index);
ArduShell.cancel();
});
mainStatusBarTabs.show();
statusBarTerminal.setValue(Msg.Lang['shell.uploading'] + "...\n");
const code = MFile.getCode();
Socket.sendCommand({
obj: 'ArduShell',
func: 'upload',
args: [ index, boardType, port, code ]
});
},
end: function () {
$('#mixly-loader-div').css('display', 'none');
$("layui-layer-shade" + layerNum).remove();
$("#mixly-loader-btn").off("click");
$("#mixly-loader-btn").css('display', 'inline-block');
}
});
}
ArduShell.operateSuccess = (type, layerNum, port, baud, timeCostStr) => {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
layer.close(layerNum);
const value = statusBarTerminal.getValue();
let prefix = '';
if (value.lastIndexOf('\n') !== value.length - 1) {
prefix = '\n';
}
statusBarTerminal.addValue(prefix);
const message = (type === 'compile' ? Msg.Lang['shell.compileSucc'] : Msg.Lang['shell.uploadSucc']);
statusBarTerminal.addValue("==" + message + "(" + Msg.Lang['shell.timeCost'] + " " + timeCostStr + ")==\n");
layer.msg(message, {
time: 1000
});
if (type === 'upload' && port) {
Serial.connect(port, baud - 0);
}
ArduShell.compiling = false;
ArduShell.uploading = false;
}
ArduShell.operateOnError = (type, layerNum, error) => {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
statusBarTerminal.addValue(error);
}
ArduShell.operateEndError = (type, layerNum, error) => {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
layer.close(layerNum);
const value = statusBarTerminal.getValue();
let prefix = '';
if (value.lastIndexOf('\n') !== value.length - 1) {
prefix = '\n';
}
statusBarTerminal.addValue(prefix);
error && statusBarTerminal.addValue(error + '\n');
const message = (type === 'compile' ? Msg.Lang['shell.compileFailed'] : Msg.Lang['shell.uploadFailed']);
statusBarTerminal.addValue("==" + message + "==\n");
ArduShell.compiling = false;
ArduShell.uploading = false;
}
/**
* @function 取消编译或上传
* @description 取消正在执行的编译或上传过程
* @return void
*/
ArduShell.cancel = function () {
Socket.sendCommand({
obj: 'ArduShell',
func: 'cancel',
args: []
});
}
ArduShell.addValue = function (data) {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
statusBarTerminal.addValue(data);
}
});

View File

@@ -0,0 +1,506 @@
goog.loadJs('web', () => {
goog.require('layui');
goog.require('Mixly.Config');
goog.require('Mixly.LayerExt');
goog.require('Mixly.Env');
goog.require('Mixly.Boards');
goog.require('Mixly.MFile');
goog.require('Mixly.MString');
goog.require('Mixly.Msg');
goog.require('Mixly.WebSocket.Serial');
goog.require('Mixly.WebSocket.Socket');
goog.provide('Mixly.WebSocket.BU');
const {
Config,
LayerExt,
Env,
Boards,
MFile,
MString,
Msg
} = Mixly;
const { BU, Serial, Socket } = Mixly.WebSocket;
const { BOARD, SELECTED_BOARD } = Config;
const { form } = layui;
BU.uploading = false;
BU.burning = false;
BU.shell = null;
/**
* @function 根据传入的stdout判断磁盘数量并选择对应操作
* @param type {string} 值为'burn' | 'upload'
* @param stdout {string} 磁盘名称字符串,形如'G:K:F:'
* @param startPath {string} 需要拷贝的文件路径
* @return {void}
**/
BU.checkNumOfDisks = function (type, stdout, startPath) {
let wmicResult = stdout;
wmicResult = wmicResult.replace(/\s+/g, "");
wmicResult = wmicResult.replace("DeviceID", "");
// wmicResult = 'G:K:F:';
let result = wmicResult.split(':');
let pathAdd = (Env.currentPlatform === "win32") ? ':' : '';
if (stdout.indexOf(":") != stdout.lastIndexOf(":")) {
let form = layui.form;
let devicesName = $('#mixly-selector-type');
let oldDevice = $('#mixly-selector-type option:selected').val();
devicesName.empty();
for (let i = 0; i < result.length; i++) {
if (result[i]) {
if (oldDevice == result[i] + pathAdd) {
devicesName.append('<option value="' + result[i] + pathAdd + '" selected>' + result[i] + pathAdd + '</option>');
} else {
devicesName.append('<option value="' + result[i] + pathAdd + '">' + result[i] + pathAdd + '</option>');
}
}
}
form.render();
let initBtnClicked = false;
const layerNum = layer.open({
type: 1,
id: "serial-select",
title: Msg.Lang['检测到多个同类型设备,请选择:'],
area: ['350px', '150px'],
content: $('#mixly-selector-div'),
shade: LayerExt.SHADE_ALL,
resize: false,
closeBtn: 0,
success: function (layero) {
$('#serial-select').css('height', '195px');
$(".layui-layer-page").css("z-index","198910151");
$("#mixly-selector-btn1").off("click").click(() => {
layer.close(layerNum);
BU.cancel();
});
$("#mixly-selector-btn2").off("click").click(() => {
layer.close(layerNum);
initBtnClicked = true;
});
},
end: function () {
$('#mixly-selector-div').css('display', 'none');
$("#layui-layer-shade" + layerNum).remove();
if (initBtnClicked) {
BU.initWithDropdownBox(type, startPath);
}
$("#mixly-selector-btn1").off("click");
$("#mixly-selector-btn2").off("click");
}
});
} else {
const layerNum = layer.open({
type: 1,
title: (type === 'burn'? Msg.Lang['shell.burning'] : Msg.Lang['shell.uploading']) + '...',
content: $('#mixly-loader-div'),
shade: LayerExt.SHADE_NAV,
resize: false,
closeBtn: 0,
success: function (layero, index) {
BU.copyFiles(type, index, startPath, result[0] + pathAdd + '/');
$("#mixly-loader-btn").off("click").click(() => {
layer.close(index);
BU.cancel();
});
},
end: function () {
$('#mixly-selector-div').css('display', 'none');
$("#layui-layer-shade" + layerNum).remove();
$("#mixly-loader-btn").off("click");
}
});
}
}
BU.copyFiles = (type, layerNum, startPath, desPath) => {
const code = MFile.getCode();
const {
copyLib = false,
libPath = []
} = SELECTED_BOARD.upload;
Socket.sendCommand({
obj: 'BU',
func: 'copyFiles',
args: [ type, layerNum, startPath, desPath, code, copyLib, libPath ]
});
}
BU.operateSuccess = (type, layerNum, port) => {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
layer.close(layerNum);
const message = (type === 'burn'? Msg.Lang['shell.burnSucc'] : Msg.Lang['shell.uploadSucc']);
layer.msg(message, {
time: 1000
});
const value = statusBarTerminal.getValue();
let prefix = '';
if (value.lastIndexOf('\n') !== value.length - 1) {
prefix = '\n';
}
statusBarTerminal.addValue(prefix + `==${message}==\n`);
if (type === 'upload' && (Serial.uploadPorts.length === 1 || port)) {
Serial.connect(port ?? Serial.uploadPorts[0].name, null);
}
BU.burning = false;
BU.uploading = false;
}
BU.operateError = (type, layerNum, error) => {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
layer.close(layerNum);
const value = statusBarTerminal.getValue();
let prefix = '';
if (value.lastIndexOf('\n') !== value.length - 1) {
prefix = '\n';
}
statusBarTerminal.addValue(prefix + error + '\n');
console.log(error);
BU.burning = false;
BU.uploading = false;
}
BU.noDevice = () => {
layer.msg(Msg.Lang['statusbar.serial.noDevice'], {
time: 1000
});
BU.burning = false;
BU.uploading = false;
}
/**
* @function 判断当前环境,以开始一个上传过程
* @param type {string} 值为'burn' | 'upload'
* @param startPath {string} 需要拷贝的文件或文件夹的路径
* @return {void}
*/
BU.initWithDropdownBox = function (type, startPath) {
const layerNum = layer.open({
type: 1,
title: (type === 'burn'? Msg.Lang['shell.burning'] : Msg.Lang['shell.uploading']) + '...',
content: $('#mixly-loader-div'),
shade: LayerExt.SHADE_NAV,
resize: false,
closeBtn: 0,
success: function (layero, index) {
$(".layui-layer-page").css("z-index","198910151");
$("#mixly-loader-btn").off("click").click(() => {
layer.close(index);
BU.cancel();
});
const desPath = $('#mixly-selector-type option:selected').val();
BU.copyFiles(type, index, startPath, desPath);
},
end: function () {
$('#mixly-loader-div').css('display', 'none');
$("#layui-layer-shade" + layerNum).remove();
}
});
}
/**
* @function 取消烧录或上传
* @return {void}
*/
BU.cancel = function () {
Socket.sendCommand({
obj: 'BU',
func: 'cancel',
args: []
});
if (BU.uploading) {
BU.uploading = false;
layer.msg(Msg.Lang['shell.uploadCanceled'], {
time: 1000
});
} else if (BU.burning) {
BU.burning = false;
layer.msg(Msg.Lang['shell.burnCanceled'], {
time: 1000
});
}
}
/**
* @function 开始一个烧录过程
* @return {void}
*/
BU.initBurn = function () {
Socket.connect((WS) => {
layer.closeAll();
}, () => {
if (BU.burning) return;
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
const { burn } = SELECTED_BOARD;
statusBarTerminal.setValue('');
mainStatusBarTabs.changeTo("output");
mainStatusBarTabs.show();
BU.burning = true;
BU.uploading = false;
if (burn.type === 'volume') {
Socket.sendCommand({
obj: 'BU',
func: 'getDisksWithVolumesName',
args: [ 'burn', burn.volume, burn.filePath ]
});
} else {
const port = Serial.getSelectedPortName();
BU.burnWithPort(port, burn.command);
}
});
}
/**
* @function 开始一个上传过程
* @return {void}
*/
BU.initUpload = function () {
Socket.connect((WS) => {
layer.closeAll();
}, () => {
if (BU.uploading) return;
const { upload } = SELECTED_BOARD;
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
statusBarTerminal.setValue('');
mainStatusBarTabs.changeTo("output");
mainStatusBarTabs.show();
BU.burning = false;
BU.uploading = true;
if (upload.type === "volume") {
Socket.sendCommand({
obj: 'BU',
func: 'getDisksWithVolumesName',
args: [ 'upload', upload.volume, upload.filePath ]
});
} else {
const port = Serial.getSelectedPortName();
BU.uploadWithPort(port, upload.command);
}
});
}
/**
* @function 通过cmd烧录
* @param layerNum {number} 烧录或上传加载弹窗的编号,用于关闭此弹窗
* @param port {string} 所选择的串口
* @param command {string} 需要执行的指令
* @return {void}
*/
BU.burnByCmd = function (layerNum, port, command) {
const newCommand = MString.tpl(command, { com: port });
Socket.sendCommand({
obj: 'BU',
func: 'burnByCmd',
args: [ layerNum, port, newCommand ]
});
}
/**
* @function 通过cmd上传
* @param layerNum {number} 烧录或上传加载弹窗的编号,用于关闭此弹窗
* @param port {string} 所选择的串口
* @param command {string} 需要执行的指令
* @return {void}
*/
BU.uploadByCmd = function (layerNum, port, command) {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
const newCommand = MString.tpl(command, { com: port });
const { upload } = SELECTED_BOARD;
const {
filePath,
copyLib = false,
libPath = []
} = upload;
const code = MFile.getCode();
statusBarTerminal.addValue(Msg.Lang['shell.uploading'] + '...\n');
Socket.sendCommand({
obj: 'BU',
func: 'uploadByCmd',
args: [ layerNum, port, newCommand, code, filePath, copyLib, libPath ]
});
}
/**
* @function 特殊固件的烧录
* @return {void}
**/
BU.burnWithSpecialBin = () => {
const devNames = $('#mixly-selector-type');
let oldDevice = $('#mixly-selector-type option:selected').val();
devNames.empty();
let firmwareList = BOARD.burn.special;
let firmwareObj = {};
for (let i = 0; i < firmwareList.length; i++)
firmwareObj[firmwareList[i].name] = firmwareList[i].command;
firmwareList.map(firmware => {
if (!firmware?.name && !firmware?.command) return;
if (`${firmware.name}` == oldDevice) {
devNames.append($(`<option value="${firmware.name}" selected>${firmware.name}</option>`));
} else {
devNames.append($(`<option value="${firmware.name}">${firmware.name}</option>`));
}
});
form.render();
let initBtnClicked = false;
const layerNum = layer.open({
type: 1,
id: "serial-select",
title: "请选择固件:",
area: ['350px', '150px'],
content: $('#mixly-selector-div'),
shade: Mixly.LayerExt.SHADE_ALL,
resize: false,
closeBtn: 0,
success: function (layero) {
$('#serial-select').css('height', '180px');
$('#serial-select').css('overflow', 'inherit');
$(".layui-layer-page").css("z-index", "198910151");
$("#mixly-selector-btn1").off("click").click(() => {
layer.close(layerNum);
});
$("#mixly-selector-btn2").click(() => {
layer.close(layerNum);
initBtnClicked = true;
});
},
end: function () {
$("#mixly-selector-btn1").off("click");
$("#mixly-selector-btn2").off("click");
$('#mixly-selector-div').css('display', 'none');
$(".layui-layer-shade").remove();
if (initBtnClicked) {
let selectedFirmwareName = $('#mixly-selector-type option:selected').val();
try {
firmwareObj[selectedFirmwareName] = firmwareObj[selectedFirmwareName].replace(/\\/g, "/");
} catch (e) {
console.log(e);
}
let pyToolName = ["esptool", "kflash", "stm32loader", "stm32bl"];
let pyToolPath = "{path}/mixpyBuild/win_python3/Lib/site-packages/"
if (Env.currentPlatform == "darwin" || Env.currentPlatform == "linux") {
pyToolPath = "{path}/tools/python/";
}
for (let i = 0; i < pyToolName.length; i++) {
if (firmwareObj[selectedFirmwareName].indexOf("\"") != -1) {
firmwareObj[selectedFirmwareName] = replaceWithReg(firmwareObj[selectedFirmwareName], Env.python3Path + "\" \"" + pyToolPath + pyToolName[i] + ".py", pyToolName[i]);
} else {
firmwareObj[selectedFirmwareName] = replaceWithReg(firmwareObj[selectedFirmwareName], Env.python3Path + " " + pyToolPath + pyToolName[i] + ".py", pyToolName[i]);
}
}
firmwareObj[selectedFirmwareName] = replaceWithReg(firmwareObj[selectedFirmwareName], Env.clientPath, "path");
firmwareObj[selectedFirmwareName] = replaceWithReg(firmwareObj[selectedFirmwareName], Env.boardDirPath, "indexPath");
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
statusBarTerminal.setValue('');
mainStatusBarTabs.changeTo("output");
mainStatusBarTabs.show();
BU.burning = true;
BU.uploading = false;
const port = Serial.getSelectedPortName();
BU.burnWithPort(port, firmwareObj[selectedFirmwareName]);
} else {
layer.msg(Msg.Lang['shell.burnCanceled'], { time: 1000 });
}
}
});
}
/**
* @function 通过串口执行命令行烧录或上传操作
* @param type {string} 值为 'burn' | 'upload'
* @param port {string} 所选择的串口
* @param command {string} 需要执行的指令
* @return {void}
**/
BU.operateWithPort = (type, port, command) => {
if (!port) {
layer.msg(Msg.Lang['statusbar.serial.noDevice'], {
time: 1000
});
BU.burning = false;
BU.uploading = false;
return;
}
const title = (type === 'burn' ? Msg.Lang['shell.burning'] : Msg.Lang['shell.uploading']) + '...';
const operate = () => {
const layerNum = layer.open({
type: 1,
title,
content: $('#mixly-loader-div'),
shade: LayerExt.SHADE_NAV,
resize: false,
closeBtn: 0,
success: function (layero, index) {
$(".layui-layer-page").css("z-index","198910151");
switch (type) {
case 'burn':
BU.burnByCmd(index, port, command);
break;
case 'upload':
default:
BU.uploadByCmd(index, port, command);
}
$("#mixly-loader-btn").off("click").click(() => {
$("#mixly-loader-btn").css('display', 'none');
switch (type) {
case 'burn':
layer.title(Msg.Lang['shell.aborting'] + '...', index);
break;
case 'upload':
default:
layer.title(Msg.Lang['shell.aborting'] + '...', index);
}
BU.cancel(type);
});
},
end: function () {
$('#mixly-loader-div').css('display', 'none');
$("layui-layer-shade" + layerNum).remove();
$("#mixly-loader-btn").off("click");
$("#mixly-loader-btn").css('display', 'inline-block');
}
});
}
operate();
}
/**
* @function 通过串口执行命令行烧录操作
* @param port {string} 所选择的串口
* @param command {string} 需要执行的指令
* @return {void}
**/
BU.burnWithPort = (port, command) => {
BU.operateWithPort('burn', port, command);
}
/**
* @function 通过串口执行命令行上传操作
* @param port {string} 所选择的串口
* @param command {string} 需要执行的指令
* @return {void}
**/
BU.uploadWithPort = (port, command) => {
BU.operateWithPort('upload', port, command);
}
BU.addValue = function (data) {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
statusBarTerminal.addValue(data);
}
});

View File

@@ -0,0 +1,182 @@
goog.loadJs('web', () => {
goog.require('layui');
goog.require('Blockly');
goog.require('Mixly.XML');
goog.require('Mixly.MFile');
goog.require('Mixly.Boards');
goog.require('Mixly.LayerExt');
goog.require('Mixly.Msg');
goog.require('Mixly.WebSocket.Socket');
goog.provide('Mixly.WebSocket.File');
const {
XML,
MFile,
Boards,
LayerExt,
Msg
} = Mixly;
const { Socket, File } = Mixly.WebSocket;
const boardType = Boards.getType();
const { form } = layui;
File.saveToCloud = () => {
Socket.connect((WS) => {
layer.closeAll();
}, () => {
layer.prompt({
title: Blockly.Msg.MSG['save_ser'],
shade: LayerExt.SHADE_ALL,
value: 'main.mix',
success: function(layero, index) {
$(layero).find('input').attr('spellcheck', false);
}
}, function(value, index, elem) {
layer.close(index);
const extname = value.substring(value.lastIndexOf('.')).toLowerCase();
let saveType = [];
MFile.saveFilters.map((filter) => {
saveType = [ ...saveType, ...filter.extensions ];
});
if (!saveType.includes(extname.substring(1))) {
layer.msg(Msg.Lang['文件后缀错误'], {
time: 1000
});
return;
}
let data;
switch (extname) {
case '.mix':
data = MFile.getMix();
break;
case '.ino':
case '.py':
data = MFile.getCode();
default:
layer.msg(Msg.Lang['文件后缀错误'], {
time: 1000
});
}
if (!data) {
return;
}
Socket.sendCommand({
obj: 'File',
func: 'saveAs',
args: [ boardType, value, data ]
});
});
});
}
File.saveSuccess = (filename) => {
layer.msg(filename + ' ' + Msg.Lang['保存成功'], {
time: 1000
});
}
File.saveError = (filename, error) => {
layer.msg(filename + ' ' + '保存失败', {
time: 1000
});
}
File.openFromCloud = () => {
Socket.sendCommand({
obj: 'File',
func: 'getUserFilesInfo',
args: [ boardType ]
});
}
File.showOpenDialog = (filesObj) => {
const $options = $('#mixly-selector-type');
$options.empty();
filesObj.map(file => {
$options.append($(`<option value="${file.path}">${file.name}</option>`));
});
form.render();
let initBtnClicked = false;
const layerNum = layer.open({
type: 1,
id: "serial-select",
title: "请选择需要打开的文件:",
area: ['350px', '150px'],
content: $('#mixly-selector-div'),
shade: Mixly.LayerExt.SHADE_ALL,
resize: false,
closeBtn: 0,
success: function (layero) {
$('#serial-select').css('height', '180px');
$('#serial-select').css('overflow', 'inherit');
$(".layui-layer-page").css("z-index", "198910151");
$("#mixly-selector-btn1").off("click").click(() => {
layer.close(layerNum);
});
$("#mixly-selector-btn2").click(() => {
layer.close(layerNum);
initBtnClicked = true;
});
},
end: function () {
$("#mixly-selector-btn1").off("click");
$("#mixly-selector-btn2").off("click");
$('#mixly-selector-div').css('display', 'none');
$(".layui-layer-shade").remove();
if (!initBtnClicked) {
return;
}
const selectedFilePath = $('#mixly-selector-type option:selected').val();
Socket.sendCommand({
obj: 'File',
func: 'open',
args: [ boardType, selectedFilePath ]
});
}
});
}
File.open = (extname, data) => {
switch (extname) {
case '.mix':
case '.xml':
Editor.mainEditor.drag.full('POSITIVE');
try {
data = XML.convert(data, true);
data = data.replace(/\\(u[0-9a-fA-F]{4})/g, function (s) {
return unescape(s.replace(/\\(u[0-9a-fA-F]{4})/g, '%$1'));
});
} catch (error) {
console.log(error);
}
MFile.parseMix($(data), false, false, (message) => {
Editor.blockEditor.scrollCenter();
Blockly.hideChaff();
});
break;
case '.ino':
case '.py':
Editor.mainEditor.drag.full('NEGATIVE');
Editor.codeEditor.setValue(data, -1);
break;
}
}
File.openSuccess = (filename) => {
layer.msg(filename + ' ' + '打开成功', {
time: 1000
});
}
File.openError = (filename, error) => {
layer.msg(filename + ' ' + '打开失败', {
time: 1000
});
}
});

View File

@@ -0,0 +1,294 @@
goog.loadJs('web', () => {
goog.require('path');
goog.require('Mixly.Env');
goog.require('Mixly.Config');
goog.require('Mixly.Boards');
goog.require('Mixly.Command');
goog.require('Mixly.MJSON');
goog.require('Mixly.MArray');
goog.require('Mixly.LayerExt');
goog.require('Mixly.WebSocket');
goog.provide('Mixly.WebSocket.Socket');
const {
Env,
Config,
Boards,
Command,
MJSON,
MArray,
LayerExt
} = Mixly;
const { BOARD, SELECTED_BOARD, SOFTWARE } = Config;
const { Socket } = Mixly.WebSocket;
Socket.obj = null;
Socket.url = 'ws://127.0.0.1/socket';
Socket.jsonArr = [];
Socket.connected = false;
Socket.initFunc = null;
Socket.debug = SOFTWARE.debug;
BOARD.server = { ...SOFTWARE.webSocket };
let { hostname, protocol, port } = window.location;
if (protocol === 'http:') {
Socket.protocol = 'ws:';
} else {
Socket.protocol = 'wss:';
}
if (port) {
port = ':' + port;
}
Socket.url = Socket.protocol + '//' + hostname + port + '/socket';
Socket.IPAddress = hostname;
let lockReconnect = false; //避免重复连接
let timeoutFlag = true;
let timeoutSet = null;
let reconectNum = 0;
const timeout = 5000; //超时重连间隔
function reconnect () {
if (lockReconnect) return;
lockReconnect = true;
//没连接上会一直重连,设置延迟避免请求过多
setTimeout(function () {
timeoutFlag = true;
Socket.init();
console.info(`正在重连第${reconectNum + 1}`);
reconectNum++;
lockReconnect = false;
}, timeout); //这里设置重连间隔(ms)
}
//心跳检测
const heartCheck = {
timeout, //毫秒
timeoutObj: null,
serverTimeoutObj: null,
reset: function () {
clearInterval(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
return this;
},
start: function () {
const self = this;
let count = 0;
let WS = Socket;
this.timeoutObj = setInterval(() => {
if (count < 3) {
if (WS.obj.readyState === 1) {
WS.obj.send('HeartBeat');
console.info(`HeartBeat第${count + 1}`);
}
count++;
} else {
clearInterval(this.timeoutObj);
count = 0;
if (WS.obj.readyState === 0 && WS.obj.readyState === 1) {
WS.obj.close();
}
}
}, self.timeout);
}
}
Socket.init = (onopenFunc = (data) => {}, doFunc = () => {}) => {
if (Socket.connected) {
if (Socket.initFunc) {
Socket.initFunc();
Socket.initFunc = null;
}
doFunc();
return;
}
timeoutSet = setTimeout(() => {
if (timeoutFlag && reconectNum < 3) {
console.info(`重连`);
reconectNum++;
Socket.init();
}
}, timeout);
let WS = Socket;
WS.obj = new WebSocket(WS.url);
WS.obj.onopen = () => {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
console.log('已连接' + WS.url);
mainStatusBarTabs.show();
mainStatusBarTabs.changeTo('output');
statusBarTerminal.setValue(WS.url + '连接成功\n');
WS.connected = true;
Socket.toggleUIToolbar(true);
Socket.initFunc = doFunc;
reconectNum = 0;
timeoutFlag = false;
clearTimeout(timeoutSet);
heartCheck.reset().start();
onopenFunc(WS);
};
WS.obj.onmessage = (event) => {
heartCheck.reset().start();
let command = Command.parse(event.data);
command = MJSON.decode(command);
if (Socket.debug)
console.log('receive -> ', event.data);
/*if (command && command.obj && command.function) {
if (command.type === 1) {
let args = command.args ?? [];
try {
if (window[command.obj][command.function])
window[command.obj][command.function](...args);
} catch (e) {
console.log(e);
}
}
}*/
Command.run(command);
};
WS.obj.onerror = (event) => {
console.log('WebSocket error: ', event);
reconnect(); //重连
};
WS.obj.onclose = (event) => {
const { mainStatusBarTabs } = Mixly;
const statusBarTerminal = mainStatusBarTabs.getStatusBarById('output');
WS.connected = false;
console.log('已断开' + WS.url);
mainStatusBarTabs.show();
mainStatusBarTabs.changeTo('output');
statusBarTerminal.setValue(WS.url + '连接断开,请在设置中重新连接\n');
let ports = [ ...mainStatusBarTabs.statusBarIndexToIds ];
MArray.remove(ports, 'output');
for (let i = 0; i < ports.length; i++) {
const statusBarSerial = mainStatusBarTabs.getStatusBarById(ports[i]);
statusBarSerial.close(ports[i]);
}
Socket.toggleUIToolbar(false);
layer.closeAll();
Mixly.WebSocket.BU.burning = false;
Mixly.WebSocket.BU.uploading = false;
Mixly.WebSocket.ArduShell.compiling = false;
Mixly.WebSocket.ArduShell.uploading = false;
console.info(`关闭`, event.code);
if (event.code !== 1000) {
timeoutFlag = false;
clearTimeout(timeoutSet);
reconnect();
} else {
clearInterval(heartCheck.timeoutObj);
clearTimeout(heartCheck.serverTimeoutObj);
}
}
}
Socket.sendCommand = (command) => {
let WS = Mixly.WebSocket.Socket;
if (!WS.connected) {
layer.msg('未连接' + WS.url, {time: 1000});
return;
}
let commandStr = '';
try {
commandStr = JSON.stringify(MJSON.encode(command));
if (Socket.debug)
console.log('send -> ', commandStr);
} catch (e) {
console.log(e);
return;
}
WS.obj.send(commandStr);
}
Socket.clickConnect = () => {
if (Socket.connected) {
Socket.disconnect();
} else {
Socket.connect((WS) => {
layer.closeAll();
layer.msg(WS.url + '连接成功', { time: 1000 });
});
}
}
Socket.openLoadingBox = (title, successFunc = () => {}, endFunc = () => {}) => {
layer.open({
type: 1,
title: title,
content: $('#mixly-loader-div'),
shade: LayerExt.SHADE_ALL,
closeBtn: 0,
success: function () {
$("#webusb-cancel").css("display","none");
$(".layui-layer-page").css("z-index", "198910151");
successFunc();
},
end: function () {
$("#mixly-loader-div").css("display", "none");
$(".layui-layer-shade").remove();
$("#webusb-cancel").css("display", "unset");
if (Socket.connected)
endFunc();
}
});
}
Socket.connect = (onopenFunc = (data) => {}, doFunc = () => {}) => {
if (Socket.connected) {
doFunc();
return;
}
let title = '连接中...';
Socket.openLoadingBox(title, () => {
setTimeout(() => {
Socket.init(onopenFunc);
}, 1000);
}, doFunc);
}
Socket.disconnect = () => {
if (!Socket.connected)
return;
let title = '断开中...';
Socket.openLoadingBox(title, () => {
Socket.obj.close();
});
}
Socket.toggleUIToolbar = (connected) => {
try {
if (connected) {
$('#socket-connect-btn').html(Blockly.Msg.MSG['disconnect']);
$('#socket-connect-btn').removeClass('icon-link').addClass('icon-unlink');
} else {
$('#socket-connect-btn').html(Blockly.Msg.MSG['connect']);
$('#socket-connect-btn').removeClass('icon-unlink').addClass('icon-link');
}
} catch (e) {
console.log(e);
}
}
Socket.updateSelectedBoardConfig = (info) => {
Env.currentPlatform = info.currentPlatform;
info.clientPath = info.clientPath.replaceAll('\\', '/');
Env.clientPath = info.clientPath;
info.appPath = info.appPath.replaceAll('\\', '/');
Env.srcDirPath = info.appPath;
Env.indexDirPath = path.join(Env.srcDirPath, 'boards');
Env.boardDirPath = path.join(Env.srcDirPath, BOARD.boardIndex, '../');
Env.python3Path = info.python3Path;
const boardType = Boards.getSelectedBoardName();
Boards.changeTo(boardType);
}
});

View File

@@ -0,0 +1,6 @@
goog.loadJs('web', () => {
goog.require('Mixly');
goog.provide('Mixly.WebSocket');
});