feat: migrate recipe editor

- using eventbus in recipe editor
- migrate to logging instead of console log
- fix case swap not saved, value not update after change, topping slot bug

Signed-off-by: pakintada@gmail.com <Pakin>
This commit is contained in:
pakintada@gmail.com 2026-06-19 17:27:28 +07:00
parent 270faf6b34
commit f0619c5a10
65 changed files with 1600 additions and 557 deletions

View file

@ -1,3 +1,4 @@
import { logger } from '$lib/core/utils/logger';
import { Adb, AdbDaemonTransport, encodeUtf8 } from '@yume-chan/adb';
import AdbWebCredentialStore from '@yume-chan/adb-credential-web';
import {
@ -138,9 +139,9 @@ async function connectWithRetry<T>(
export async function connnectViaWebUSB(connectAndroidServer = true) {
const device = await AdbDaemonWebUsbDeviceManager.BROWSER?.requestDevice();
console.log('usb ok', globalThis.navigator.usb);
logger.info('usb ok', globalThis.navigator.usb);
if (device) {
console.log('connect ', device.name);
logger.info('connect ', device.name);
try {
const credentialStore = new AdbWebCredentialStore();
@ -161,7 +162,7 @@ export async function connnectViaWebUSB(connectAndroidServer = true) {
// save device info
await deviceCredentialManager.saveDeviceInfo(device);
} catch (e: any) {
console.error('error on connect', e);
logger.error('error on connect', e);
if (e instanceof AdbDaemonWebUsbDevice.DeviceBusyError) {
addNotification(
@ -218,9 +219,9 @@ export async function connectRecipeMenuViaWebUSB() {
if (recipeMenuAdbConnectPromise) return await recipeMenuAdbConnectPromise;
const device = await AdbDaemonWebUsbDeviceManager.BROWSER?.requestDevice();
console.log('recipe menu usb ok', 'usb' in globalThis.navigator);
logger.info('recipe menu usb ok', 'usb' in globalThis.navigator);
if (device) {
console.log('recipe menu connect ', device.name);
logger.info('recipe menu connect ', device.name);
try {
const credentialStore = new AdbWebCredentialStore();
@ -230,7 +231,7 @@ export async function connectRecipeMenuViaWebUSB() {
await deviceCredentialManager.saveDeviceInfo(device);
return adb;
} catch (e: any) {
console.error('recipe menu connect error', e);
logger.error('recipe menu connect error', e);
if (e instanceof AdbDaemonWebUsbDevice.DeviceBusyError) {
addNotification(
@ -306,10 +307,10 @@ export async function sendRecipeMenuMessageToAndroid(message: any) {
try {
const encoder = new TextEncoder();
await writer.write(encoder.encode(JSON.stringify(message) + '\n'));
console.log('recipe menu sent! ', JSON.stringify(message).length);
logger.info('recipe menu sent! ', JSON.stringify(message).length);
return true;
} catch (error) {
console.error('recipe menu write failed', error);
logger.error('recipe menu write failed', error);
addNotification(`ERR:Failed to send recipe menu\n${error}`);
return false;
}
@ -319,7 +320,7 @@ export async function executeCmd(command: string) {
let instance = getAdbInstance();
if (!instance) {
console.error('instance not found');
logger.error('instance not found');
return {};
}
@ -348,7 +349,7 @@ export async function executeCmd(command: string) {
};
}
} catch (e: any) {
// console.log(e.message);
// logger.info(e.message);
//ExactReadable ended
if (e.message.includes('ExactReadable ended')) {
return {
@ -358,7 +359,7 @@ export async function executeCmd(command: string) {
};
}
console.error('error while execute command', e);
logger.error('error while execute command', e);
return {};
}
}
@ -445,7 +446,7 @@ export async function disconnect() {
if (instance) {
try {
await instance.close();
console.log('close instance');
logger.info('close instance');
} finally {
await saveAdbInstance(undefined);
}
@ -457,7 +458,7 @@ export async function cleanupSync() {
try {
await syncConnection.dispose();
} catch (e) {
console.error('error on dispose sync', e);
logger.error('error on dispose sync', e);
}
}
@ -489,7 +490,7 @@ export async function pull(filename: string, timeoutMs: number = 5000) {
return result_string;
}
} catch (pull_error: any) {
console.log('pulling error', pull_error);
logger.info('pulling error', pull_error);
} finally {
await cleanupSync();
}
@ -511,14 +512,14 @@ export async function push(path: string, obj: string) {
});
try {
console.log('support push v2', sync.supportsSendReceiveV2);
logger.info('support push v2', sync.supportsSendReceiveV2);
await sync.write({
filename: path,
file
});
} catch (error) {
console.log('error while trying to write to machine', error);
logger.info('error while trying to write to machine', error);
} finally {
await sync.dispose();
}
@ -563,7 +564,7 @@ export async function pushBinary(
onProgress?.(total, total);
return true;
} catch (error) {
console.log('error while pushing binary to machine', error);
logger.info('error while pushing binary to machine', error);
return false;
} finally {
await sync.dispose();
@ -582,7 +583,7 @@ async function connectToAndroidServer(maxRetries = 5) {
try {
let inst = getAdbInstance();
if (!inst) {
console.warn('adb instance not found');
logger.warn('adb instance not found');
return;
}
@ -599,7 +600,7 @@ async function connectToAndroidServer(maxRetries = 5) {
const writer = stream.writable.getWriter();
const reader = stream.readable.getReader();
console.log('checking on writer ', writer);
// logger.info('checking on writer ', writer);
adbWriter.set(writer);
if (writer) {
addNotification('INFO:Enable Brewing Mode T on machine');
@ -632,7 +633,7 @@ async function connectToAndroidServer(maxRetries = 5) {
// handleAdbPayload(new TextDecoder().decode(value));
}
} catch (e) {
console.error('read error', e);
logger.error('read error', e);
if (isRecoverableError(e)) {
void connectToAndroidServer();
}
@ -671,7 +672,7 @@ async function connectToAndroidServer(maxRetries = 5) {
}
if (lastError) {
console.error('Connection failed. Suspect java running or not', lastError);
logger.error('Connection failed. Suspect java running or not', lastError);
addNotification(`ERR:Fail to enable brewing mode T\n${lastError.message ?? ''}`);
}
}
@ -693,7 +694,7 @@ async function connectToAndroidRecipeMenuServerOnce(notifyFailure = true, retryO
try {
let inst = getAdbInstance();
if (!inst) {
console.warn('recipe menu adb instance not found');
logger.warn('recipe menu adb instance not found');
return;
}
@ -704,7 +705,7 @@ async function connectToAndroidRecipeMenuServerOnce(notifyFailure = true, retryO
const writer = stream.writable.getWriter();
const reader = stream.readable.getReader();
console.log('checking recipe menu writer ', writer);
logger.info('checking recipe menu writer ', writer);
adbWriter.set(writer);
if (writer) {
addNotification('INFO:Enable Android recipe menu channel');
@ -712,7 +713,7 @@ async function connectToAndroidRecipeMenuServerOnce(notifyFailure = true, retryO
addNotification('WARN:Android recipe menu channel unavailable');
setTimeout(async () => {
console.log('reconnecting android recipe menu server');
logger.info('reconnecting android recipe menu server');
await connectToAndroidRecipeMenuServer();
}, 5000);
}
@ -726,7 +727,7 @@ async function connectToAndroidRecipeMenuServerOnce(notifyFailure = true, retryO
if (done) break;
const decoded = decoder.decode(value, { stream: true });
console.log('[ADB Reader] Received raw:', decoded.slice(0, 200));
logger.info('[ADB Reader] Received raw:', decoded.slice(0, 200));
messageBuffer += decoded;
const messages = messageBuffer.split('\n');
messageBuffer = messages.pop() ?? '';
@ -734,7 +735,7 @@ async function connectToAndroidRecipeMenuServerOnce(notifyFailure = true, retryO
for (const message of messages) {
const trimmedMessage = message.trim();
if (trimmedMessage) {
console.log('[ADB Reader] Processing message:', trimmedMessage.slice(0, 200));
logger.info('[ADB Reader] Processing message:', trimmedMessage.slice(0, 200));
GlobalEventBus.emit('adb:raw-payload', trimmedMessage);
handleAdbPayload(trimmedMessage);
}
@ -747,7 +748,7 @@ async function connectToAndroidRecipeMenuServerOnce(notifyFailure = true, retryO
handleAdbPayload(remainingMessage);
}
} catch (e) {
console.error('recipe menu read error', e);
logger.error('recipe menu read error', e);
} finally {
adbWriter.set(null);
addNotification('WARN:Android recipe menu channel offline ...');
@ -757,7 +758,7 @@ async function connectToAndroidRecipeMenuServerOnce(notifyFailure = true, retryO
}
})();
} catch (err) {
console.error('Recipe menu connection failed. Suspect java running or not', err);
logger.error('Recipe menu connection failed. Suspect java running or not', err);
adbWriter.set(null);
if (notifyFailure) addNotification('ERR:Fail to enable Android recipe menu channel');
if (retryOnFailure) {