fix: init local device handlers even when connected to Socket Server

This commit is contained in:
yczpf2019
2026-01-24 18:37:54 +08:00
parent 8b59808c17
commit c7d57d39a1

View File

@@ -1,186 +1,174 @@
goog.loadJs('web', () => { goog.loadJs('web', () => {
goog.require('path'); goog.require('path');
goog.require('Mixly.Config'); goog.require('Mixly.Config');
goog.require('Mixly.Env'); goog.require('Mixly.Env');
goog.require('Mixly.Msg'); goog.require('Mixly.Msg');
goog.require('Mixly.Registry'); goog.require('Mixly.Registry');
goog.require('Mixly.Serial'); goog.require('Mixly.Serial');
goog.require('Mixly.LayerExt'); goog.require('Mixly.LayerExt');
goog.require('Mixly.HTMLTemplate'); goog.require('Mixly.HTMLTemplate');
goog.require('Mixly.Web.SerialPort'); goog.require('Mixly.Web.SerialPort');
goog.require('Mixly.Web.USB'); goog.require('Mixly.Web.USB');
goog.require('Mixly.Web.USBMini'); goog.require('Mixly.Web.USBMini');
goog.require('Mixly.Web.HID'); goog.require('Mixly.Web.HID');
goog.provide('Mixly.Web.Serial'); goog.provide('Mixly.Web.Serial');
const { const {
Config, Config,
Env, Env,
Msg, Msg,
Registry, Registry,
Serial, Serial,
LayerExt, LayerExt,
HTMLTemplate, HTMLTemplate,
Web Web
} = Mixly; } = Mixly;
const { const {
SerialPort, SerialPort,
USB, USB,
USBMini, USBMini,
HID HID
} = Web; } = Web;
const { BOARD } = Config; const { BOARD } = Config;
const platform = goog.platform(); const platform = goog.platform();
const fullPlatform = goog.fullPlatform(); const fullPlatform = goog.fullPlatform();
class WebSerial extends Serial { class WebSerial extends Serial {
static { static {
this.devicesRegistry = new Registry(); this.devicesRegistry = new Registry();
this.type = Serial.type; this.type = Serial.type;
this.DEVICES_SELECT_LAYER = new HTMLTemplate( this.DEVICES_SELECT_LAYER = new HTMLTemplate(
goog.readFileSync(path.join(Env.templatePath, 'html/devices-select-layer.html')) goog.readFileSync(path.join(Env.templatePath, 'html/devices-select-layer.html'))
); );
this.getConfig = function () { this.getConfig = function () {
return Serial.getConfig(); return Serial.getConfig();
}
this.getSelectedPortName = function () {
return Serial.getSelectedPortName();
}
this.getCurrentPortsName = function () {
return Serial.getCurrentPortsName();
}
this.refreshPorts = function () {
Serial.refreshPorts();
}
this.requestPort = function () {
if (this.devicesRegistry.length() < 1) {
throw Error('can not find any device handler');
} else if (this.devicesRegistry.length() === 1) {
const keys = this.devicesRegistry.keys();
return this.devicesRegistry.getItem(keys[0]).requestPort();
} }
const msg = {
serialMsg: Msg.Lang['layer.devices.serial'], this.getSelectedPortName = function () {
serialStatus: this.devicesRegistry.hasKey('serial') ? '' : 'disabled', return Serial.getSelectedPortName();
hidMsg: Msg.Lang['layer.devices.hid'], }
hidStatus: this.devicesRegistry.hasKey('hid') ? '' : 'disabled',
usbMsg: Msg.Lang['layer.devices.usb'], this.getCurrentPortsName = function () {
usbStatus: ( return Serial.getCurrentPortsName();
this.devicesRegistry.hasKey('usb') || this.devicesRegistry.hasKey('usbmini') }
) ? '' : 'disabled'
}; this.refreshPorts = function () {
return new Promise((resolve, reject) => { Serial.refreshPorts();
let selected = false; }
const layerNum = LayerExt.open({
title: [Msg.Lang['layer.devices.select'], '36px'], this.requestPort = function () {
area: ['400px', '150px'], if (this.devicesRegistry.length() < 1) {
max: false, throw Error('can not find any device handler');
min: false, } else if (this.devicesRegistry.length() === 1) {
content: this.DEVICES_SELECT_LAYER.render(msg), const keys = this.devicesRegistry.keys();
shade: LayerExt.SHADE_ALL, return this.devicesRegistry.getItem(keys[0]).requestPort();
resize: false, }
success: function (layero, index) { const msg = {
$(layero).on('click', 'button', (event) => { serialMsg: Msg.Lang['layer.devices.serial'],
selected = true; serialStatus: this.devicesRegistry.hasKey('serial') ? '' : 'disabled',
layer.close(layerNum); hidMsg: Msg.Lang['layer.devices.hid'],
const $btn = $(event.currentTarget); hidStatus: this.devicesRegistry.hasKey('hid') ? '' : 'disabled',
let mId = $btn.attr('m-id'); usbMsg: Msg.Lang['layer.devices.usb'],
if (mId === 'usb' && WebSerial.devicesRegistry.hasKey('usbmini')) { usbStatus: (
mId = 'usbmini'; this.devicesRegistry.hasKey('usb') || this.devicesRegistry.hasKey('usbmini')
) ? '' : 'disabled'
};
return new Promise((resolve, reject) => {
let selected = false;
const layerNum = LayerExt.open({
title: [Msg.Lang['layer.devices.select'], '36px'],
area: ['400px', '150px'],
max: false,
min: false,
content: this.DEVICES_SELECT_LAYER.render(msg),
shade: LayerExt.SHADE_ALL,
resize: false,
success: function (layero, index) {
$(layero).on('click', 'button', (event) => {
selected = true;
layer.close(layerNum);
const $btn = $(event.currentTarget);
let mId = $btn.attr('m-id');
if (mId === 'usb' && WebSerial.devicesRegistry.hasKey('usbmini')) {
mId = 'usbmini';
}
const Device = WebSerial.devicesRegistry.getItem(mId);
Device.requestPort().then(resolve).catch(reject);
});
},
end: function () {
if (!selected) {
reject('user not select any device');
} }
const Device = WebSerial.devicesRegistry.getItem(mId); $(`#layui-layer-shade${layerNum}`).remove();
Device.requestPort().then(resolve).catch(reject);
});
},
end: function () {
if (!selected) {
reject('user not select any device');
} }
$(`#layui-layer-shade${layerNum}`).remove(); });
}
}); });
});
}
this.getHandler = function (device) {
if (device.constructor.name === 'SerialPort') {
return SerialPort;
} else if (device.constructor.name === 'HIDDevice') {
return HID;
} else if (device.constructor.name === 'USBDevice') {
if (this.devicesRegistry.hasKey('usbmini')) {
return USBMini;
} else {
return USB;
}
} }
return null;
}
this.getPort = function (name) { this.getHandler = function (device) {
return Serial.nameToPortRegistry.getItem(name); if (device.constructor.name === 'SerialPort') {
} return SerialPort;
} else if (device.constructor.name === 'HIDDevice') {
this.addPort = function (device) { return HID;
const handler = this.getHandler(device); } else if (device.constructor.name === 'USBDevice') {
if (!handler) { if (this.devicesRegistry.hasKey('usbmini')) {
return; return USBMini;
} } else {
handler.addPort(device); return USB;
}
this.removePort = function (device) {
const handler = this.getHandler(device);
if (!handler) {
return;
}
handler.removePort(device);
}
this.addEventsListener = function () {}
this.init = function () {
if (Env.hasSocketServer) {
return;
}
if (platform === 'win32' && fullPlatform !== 'win10') {
if (BOARD?.web?.devices?.hid) {
this.devicesRegistry.register('hid', HID);
HID.init();
}
if (BOARD?.web?.devices?.serial) {
this.devicesRegistry.register('serial', SerialPort);
SerialPort.init();
}
if (BOARD?.web?.devices?.usb) {
if (['BBC micro:bit', 'Mithon CC'].includes(BOARD.boardType)) {
this.devicesRegistry.register('usb', USB);
USB.init();
} }
} }
} else if (platform === 'mobile') { return null;
if (['BBC micro:bit', 'Mithon CC'].includes(BOARD.boardType)) { }
this.devicesRegistry.register('usb', USB);
USB.init(); this.getPort = function (name) {
} else { return Serial.nameToPortRegistry.getItem(name);
this.devicesRegistry.register('usbmini', USBMini); }
USBMini.init();
this.addPort = function (device) {
const handler = this.getHandler(device);
if (!handler) {
return;
} }
} else { handler.addPort(device);
if (BOARD?.web?.devices?.serial) { }
this.devicesRegistry.register('serial', SerialPort);
SerialPort.init(); this.removePort = function (device) {
} else if (BOARD?.web?.devices?.usb) { const handler = this.getHandler(device);
if (!handler) {
return;
}
handler.removePort(device);
}
this.addEventsListener = function () { }
this.init = function () {
// if (Env.hasSocketServer) {
// return;
// }
if (platform === 'win32' && fullPlatform !== 'win10') {
if (BOARD?.web?.devices?.hid) {
this.devicesRegistry.register('hid', HID);
HID.init();
}
if (BOARD?.web?.devices?.serial) {
this.devicesRegistry.register('serial', SerialPort);
SerialPort.init();
}
if (BOARD?.web?.devices?.usb) {
if (['BBC micro:bit', 'Mithon CC'].includes(BOARD.boardType)) {
this.devicesRegistry.register('usb', USB);
USB.init();
}
}
} else if (platform === 'mobile') {
if (['BBC micro:bit', 'Mithon CC'].includes(BOARD.boardType)) { if (['BBC micro:bit', 'Mithon CC'].includes(BOARD.boardType)) {
this.devicesRegistry.register('usb', USB); this.devicesRegistry.register('usb', USB);
USB.init(); USB.init();
@@ -188,28 +176,40 @@ class WebSerial extends Serial {
this.devicesRegistry.register('usbmini', USBMini); this.devicesRegistry.register('usbmini', USBMini);
USBMini.init(); USBMini.init();
} }
} else if (BOARD?.web?.devices?.hid) { } else {
this.devicesRegistry.register('hid', HID); if (BOARD?.web?.devices?.serial) {
HID.init(); this.devicesRegistry.register('serial', SerialPort);
SerialPort.init();
} else if (BOARD?.web?.devices?.usb) {
if (['BBC micro:bit', 'Mithon CC'].includes(BOARD.boardType)) {
this.devicesRegistry.register('usb', USB);
USB.init();
} else {
this.devicesRegistry.register('usbmini', USBMini);
USBMini.init();
}
} else if (BOARD?.web?.devices?.hid) {
this.devicesRegistry.register('hid', HID);
HID.init();
}
} }
} }
this.init();
} }
this.init(); constructor(port) {
} super(port);
const device = WebSerial.getPort(port);
constructor(port) { const handler = WebSerial.getHandler(device);
super(port); if (!handler) {
const device = WebSerial.getPort(port); return;
const handler = WebSerial.getHandler(device); }
if (!handler) { return new handler(port);
return;
} }
return new handler(port);
} }
}
Web.Serial = WebSerial; Web.Serial = WebSerial;
}); });