create branch dev and commit code
This commit is contained in:
parent
3b70cc9fe8
commit
ea68fa5cc4
44 changed files with 12421 additions and 214 deletions
|
|
@ -14,9 +14,53 @@ import { handleAdbPayload } from '../handlers/adbPayloadHandler';
|
|||
import { adbWriter } from '../stores/adbWriter';
|
||||
import { WritableStream } from '@yume-chan/stream-extra';
|
||||
import { env } from '$env/dynamic/public';
|
||||
import type Dice_2 from '@lucide/svelte/icons/dice-2';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
let syncConnection: any = null;
|
||||
let syncOperation: Promise<unknown> = Promise.resolve();
|
||||
let recipeMenuAdbConnectPromise: Promise<Adb | undefined> | null = null;
|
||||
let recipeMenuAndroidServerConnectPromise: Promise<void> | null = null;
|
||||
let recipeMenuAndroidServerRetryTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
async function runSyncOperation<T>(operation: () => Promise<T>) {
|
||||
const run = syncOperation.then(operation, operation);
|
||||
syncOperation = run.catch(() => {});
|
||||
return await run;
|
||||
}
|
||||
|
||||
function clearRecipeMenuAndroidServerRetry() {
|
||||
if (recipeMenuAndroidServerRetryTimer) {
|
||||
clearTimeout(recipeMenuAndroidServerRetryTimer);
|
||||
recipeMenuAndroidServerRetryTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleRecipeMenuAndroidServerReconnect(delayMs = 2000) {
|
||||
if (recipeMenuAndroidServerRetryTimer || !getAdbInstance()) return;
|
||||
|
||||
recipeMenuAndroidServerRetryTimer = setTimeout(() => {
|
||||
recipeMenuAndroidServerRetryTimer = null;
|
||||
void connectToAndroidRecipeMenuServer(false);
|
||||
}, delayMs);
|
||||
}
|
||||
|
||||
async function connectRecipeMenuWebUsbDevice(
|
||||
device: AdbDaemonWebUsbDevice,
|
||||
credentialStore: AdbWebCredentialStore
|
||||
) {
|
||||
const connection = await device.connect();
|
||||
const transport = await AdbDaemonTransport.authenticate({
|
||||
connection: connection,
|
||||
serial: device.serial,
|
||||
credentialStore: credentialStore
|
||||
});
|
||||
|
||||
const adb = new Adb(transport);
|
||||
await saveAdbInstance(adb);
|
||||
await connectToAndroidRecipeMenuServer();
|
||||
|
||||
return adb;
|
||||
}
|
||||
|
||||
function isRecoverableError(error: any): boolean {
|
||||
if (!error) return false;
|
||||
|
|
@ -91,7 +135,7 @@ async function connectWithRetry<T>(
|
|||
);
|
||||
}
|
||||
|
||||
export async function connnectViaWebUSB() {
|
||||
export async function connnectViaWebUSB(connectAndroidServer = true) {
|
||||
const device = await AdbDaemonWebUsbDeviceManager.BROWSER?.requestDevice();
|
||||
console.log('usb ok', globalThis.navigator.usb);
|
||||
if (device) {
|
||||
|
|
@ -109,7 +153,9 @@ export async function connnectViaWebUSB() {
|
|||
|
||||
const adb = new Adb(transport);
|
||||
await saveAdbInstance(adb);
|
||||
await connectToAndroidServer();
|
||||
if (connectAndroidServer) {
|
||||
await connectToAndroidServer();
|
||||
}
|
||||
|
||||
// save device info
|
||||
await deviceCredentialManager.saveDeviceInfo(device);
|
||||
|
|
@ -129,7 +175,8 @@ export async function connnectViaWebUSB() {
|
|||
|
||||
export async function connectDeviceByCred(
|
||||
device: AdbDaemonWebUsbDevice,
|
||||
credStore: AdbWebCredentialStore
|
||||
credStore: AdbWebCredentialStore,
|
||||
connectAndroidServer = true
|
||||
) {
|
||||
try {
|
||||
const connection = await device.connect();
|
||||
|
|
@ -142,7 +189,9 @@ export async function connectDeviceByCred(
|
|||
const adb = new Adb(transport);
|
||||
|
||||
await saveAdbInstance(adb);
|
||||
await connectToAndroidServer();
|
||||
if (connectAndroidServer) {
|
||||
await connectToAndroidServer();
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
|
|
@ -159,6 +208,112 @@ export function getAdbInstance() {
|
|||
return AdbInstance.instance;
|
||||
}
|
||||
|
||||
export async function connectRecipeMenuViaWebUSB() {
|
||||
const currentInstance = getAdbInstance();
|
||||
if (currentInstance) {
|
||||
await connectToAndroidRecipeMenuServer();
|
||||
return currentInstance;
|
||||
}
|
||||
if (recipeMenuAdbConnectPromise) return await recipeMenuAdbConnectPromise;
|
||||
|
||||
const device = await AdbDaemonWebUsbDeviceManager.BROWSER?.requestDevice();
|
||||
console.log('recipe menu usb ok', 'usb' in globalThis.navigator);
|
||||
if (device) {
|
||||
console.log('recipe menu connect ', device.name);
|
||||
|
||||
try {
|
||||
const credentialStore = new AdbWebCredentialStore();
|
||||
recipeMenuAdbConnectPromise = connectRecipeMenuWebUsbDevice(device, credentialStore);
|
||||
const adb = await recipeMenuAdbConnectPromise;
|
||||
|
||||
await deviceCredentialManager.saveDeviceInfo(device);
|
||||
return adb;
|
||||
} catch (e: any) {
|
||||
console.error('recipe menu connect error', e);
|
||||
|
||||
if (e instanceof AdbDaemonWebUsbDevice.DeviceBusyError) {
|
||||
addNotification(
|
||||
'ERR:Device is already in use by another program, please close the program and try again'
|
||||
);
|
||||
}
|
||||
|
||||
throw e;
|
||||
} finally {
|
||||
recipeMenuAdbConnectPromise = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function connectRecipeMenuDeviceByCred(
|
||||
device: AdbDaemonWebUsbDevice,
|
||||
credStore: AdbWebCredentialStore
|
||||
) {
|
||||
const currentInstance = getAdbInstance();
|
||||
if (currentInstance) {
|
||||
await connectToAndroidRecipeMenuServer();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (recipeMenuAdbConnectPromise) {
|
||||
await recipeMenuAdbConnectPromise;
|
||||
return Boolean(getAdbInstance());
|
||||
}
|
||||
|
||||
try {
|
||||
recipeMenuAdbConnectPromise = connectRecipeMenuWebUsbDevice(device, credStore);
|
||||
await recipeMenuAdbConnectPromise;
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
} finally {
|
||||
recipeMenuAdbConnectPromise = null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function reconnectAndroidRecipeMenuServer() {
|
||||
await connectToAndroidRecipeMenuServer(true);
|
||||
}
|
||||
|
||||
export async function sendRecipeMenuFileToAndroid(recipe: any) {
|
||||
return await sendRecipeMenuMessageToAndroid({
|
||||
type: 'save_recipe_menu_file',
|
||||
payload: {
|
||||
time: new Date().toLocaleTimeString(),
|
||||
data: recipe
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function sendRecipeMenuMessageToAndroid(message: any) {
|
||||
let writer: any = get(adbWriter);
|
||||
|
||||
if (!writer) {
|
||||
if (getAdbInstance()) {
|
||||
await connectToAndroidRecipeMenuServer(false);
|
||||
} else {
|
||||
await connectRecipeMenuViaWebUSB();
|
||||
}
|
||||
writer = get(adbWriter);
|
||||
}
|
||||
|
||||
if (!writer) {
|
||||
addNotification('ERR:No active Android recipe connection');
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const encoder = new TextEncoder();
|
||||
await writer.write(encoder.encode(JSON.stringify(message) + '\n'));
|
||||
console.log('recipe menu sent! ', JSON.stringify(message).length);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('recipe menu write failed', error);
|
||||
addNotification(`ERR:Failed to send recipe menu\n${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function executeCmd(command: string) {
|
||||
let instance = getAdbInstance();
|
||||
|
||||
|
|
@ -232,64 +387,117 @@ export async function cleanupSync() {
|
|||
}
|
||||
|
||||
export async function pull(filename: string, timeoutMs: number = 5000) {
|
||||
let instance = getAdbInstance();
|
||||
return await runSyncOperation(async () => {
|
||||
let instance = getAdbInstance();
|
||||
|
||||
await cleanupSync();
|
||||
|
||||
try {
|
||||
if (instance) {
|
||||
let chunkList: Uint8Array<ArrayBufferLike>[] = [];
|
||||
const syncProm = instance.sync();
|
||||
const timeoutProm = new Promise<never>((_, reject) => {
|
||||
setTimeout(() => reject(new Error('sync timeout')), timeoutMs);
|
||||
});
|
||||
|
||||
syncConnection = await Promise.race([syncProm, timeoutProm]);
|
||||
const content = syncConnection.read(filename);
|
||||
let result_string = '';
|
||||
|
||||
for await (const chunk of content) {
|
||||
result_string += new TextDecoder().decode(chunk);
|
||||
}
|
||||
|
||||
return result_string;
|
||||
}
|
||||
} catch (pull_error: any) {
|
||||
console.log('pulling error', pull_error);
|
||||
} finally {
|
||||
await cleanupSync();
|
||||
}
|
||||
|
||||
try {
|
||||
if (instance) {
|
||||
let chunkList: Uint8Array<ArrayBufferLike>[] = [];
|
||||
const syncProm = instance.sync();
|
||||
const timeoutProm = new Promise<never>((_, reject) => {
|
||||
setTimeout(() => reject(new Error('sync timeout')), timeoutMs);
|
||||
});
|
||||
|
||||
syncConnection = await Promise.race([syncProm, timeoutProm]);
|
||||
const content = syncConnection.read(filename);
|
||||
let result_string = '';
|
||||
|
||||
for await (const chunk of content) {
|
||||
result_string += new TextDecoder().decode(chunk);
|
||||
}
|
||||
|
||||
return result_string;
|
||||
}
|
||||
} catch (pull_error: any) {
|
||||
console.log('pulling error', pull_error);
|
||||
} finally {
|
||||
await cleanupSync();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function push(path: string, obj: string) {
|
||||
let instance = getAdbInstance();
|
||||
if (instance) {
|
||||
let sync = await instance.sync();
|
||||
const encoder = new TextEncoder();
|
||||
return await runSyncOperation(async () => {
|
||||
let instance = getAdbInstance();
|
||||
if (instance) {
|
||||
let sync = await instance.sync();
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
const file: ReadableStream<MaybeConsumable<Uint8Array>> = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(new Uint8Array(encoder.encode(obj)));
|
||||
controller.close();
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
console.log('support push v2', sync.supportsSendReceiveV2);
|
||||
|
||||
await sync.write({
|
||||
filename: path,
|
||||
file
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('error while trying to write to machine', error);
|
||||
} finally {
|
||||
await sync.dispose();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Push a binary file (e.g. an .mp4 video) to the machine. Unlike push() which
|
||||
// text-encodes a string, this streams raw bytes in chunks so binary data is not
|
||||
// corrupted, and reports progress.
|
||||
export async function pushBinary(
|
||||
path: string,
|
||||
data: Uint8Array,
|
||||
onProgress?: (sent: number, total: number) => void
|
||||
): Promise<boolean> {
|
||||
return await runSyncOperation(async () => {
|
||||
let instance = getAdbInstance();
|
||||
if (!instance) return false;
|
||||
|
||||
const total = data.byteLength;
|
||||
onProgress?.(0, total);
|
||||
|
||||
let sync = await instance.sync();
|
||||
|
||||
// Mirror the working text push(): a single enqueue then close. @yume-chan
|
||||
// packetizes internally for the ADB protocol; a multi-chunk or pull-based
|
||||
// stream can stall the transfer.
|
||||
const file: ReadableStream<MaybeConsumable<Uint8Array>> = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(new Uint8Array(encoder.encode(obj)));
|
||||
controller.enqueue(data);
|
||||
controller.close();
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
console.log('support push v2', sync.supportsSendReceiveV2);
|
||||
|
||||
await sync.write({
|
||||
filename: path,
|
||||
file
|
||||
});
|
||||
const writeProm = sync.write({ filename: path, file });
|
||||
// Safety net so a stalled transfer can't hang the UI forever.
|
||||
const timeoutProm = new Promise<never>((_, reject) =>
|
||||
setTimeout(() => reject(new Error('push write timeout (120s)')), 120000)
|
||||
);
|
||||
await Promise.race([writeProm, timeoutProm]);
|
||||
onProgress?.(total, total);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.log('error while trying to write to machine', error);
|
||||
console.log('error while pushing binary to machine', error);
|
||||
return false;
|
||||
} finally {
|
||||
await sync.dispose();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// NOTE: adb reverse is not work by unavailable features support
|
||||
export async function reconnectAndroidServer() {
|
||||
await connectToAndroidServer();
|
||||
}
|
||||
|
||||
async function connectToAndroidServer(maxRetries = 5) {
|
||||
let lastError: any;
|
||||
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
||||
|
|
@ -300,10 +508,12 @@ async function connectToAndroidServer(maxRetries = 5) {
|
|||
return;
|
||||
}
|
||||
|
||||
const brewConnectionPort = env.PUBLIC_BREW_CONN_PORT || 'tcp:36588';
|
||||
|
||||
// add retry mechanism
|
||||
const stream = await connectWithRetry(
|
||||
async () => inst.transport.connect(env.PUBLIC_BREW_CONN_PORT),
|
||||
`connect to Android server port ${env.PUBLIC_BREW_CONN_PORT}`,
|
||||
async () => inst.transport.connect(brewConnectionPort),
|
||||
`connect to Android server port ${brewConnectionPort}`,
|
||||
3,
|
||||
500
|
||||
);
|
||||
|
|
@ -316,22 +526,24 @@ async function connectToAndroidServer(maxRetries = 5) {
|
|||
if (writer) {
|
||||
addNotification('INFO:Enable Brewing Mode T on machine');
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
handleAdbPayload(new TextDecoder().decode(value));
|
||||
(async () => {
|
||||
try {
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
handleAdbPayload(new TextDecoder().decode(value));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('read error', e);
|
||||
if (isRecoverableError(e)) {
|
||||
void connectToAndroidServer();
|
||||
}
|
||||
} finally {
|
||||
adbWriter.set(null);
|
||||
addNotification('WARN:Brewing Mode T Offline ...');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('read error', e);
|
||||
if (isRecoverableError(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
adbWriter.set(null);
|
||||
addNotification('WARN:Brewing Mode T Offline ...');
|
||||
}
|
||||
})();
|
||||
return;
|
||||
} else {
|
||||
addNotification('WARN:Brewing Mode T unavailable');
|
||||
|
||||
|
|
@ -366,6 +578,94 @@ async function connectToAndroidServer(maxRetries = 5) {
|
|||
}
|
||||
}
|
||||
|
||||
async function connectToAndroidRecipeMenuServer(notifyFailure = true, retryOnFailure = false) {
|
||||
if (recipeMenuAndroidServerConnectPromise) return recipeMenuAndroidServerConnectPromise;
|
||||
|
||||
recipeMenuAndroidServerConnectPromise = connectToAndroidRecipeMenuServerOnce(
|
||||
notifyFailure,
|
||||
retryOnFailure
|
||||
).finally(() => {
|
||||
recipeMenuAndroidServerConnectPromise = null;
|
||||
});
|
||||
|
||||
return recipeMenuAndroidServerConnectPromise;
|
||||
}
|
||||
|
||||
async function connectToAndroidRecipeMenuServerOnce(notifyFailure = true, retryOnFailure = false) {
|
||||
try {
|
||||
let inst = getAdbInstance();
|
||||
if (!inst) {
|
||||
console.warn('recipe menu adb instance not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const brewConnectionPort = env.PUBLIC_BREW_CONN_PORT || 'tcp:36588';
|
||||
|
||||
clearRecipeMenuAndroidServerRetry();
|
||||
const stream = await inst.transport.connect(brewConnectionPort);
|
||||
const writer = stream.writable.getWriter();
|
||||
const reader = stream.readable.getReader();
|
||||
|
||||
console.log('checking recipe menu writer ', writer);
|
||||
adbWriter.set(writer);
|
||||
if (writer) {
|
||||
addNotification('INFO:Enable Android recipe menu channel');
|
||||
} else {
|
||||
addNotification('WARN:Android recipe menu channel unavailable');
|
||||
|
||||
setTimeout(async () => {
|
||||
console.log('reconnecting android recipe menu server');
|
||||
await connectToAndroidRecipeMenuServer();
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
let messageBuffer = '';
|
||||
const decoder = new TextDecoder();
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
const decoded = decoder.decode(value, { stream: true });
|
||||
console.log('[ADB Reader] Received raw:', decoded.slice(0, 200));
|
||||
messageBuffer += decoded;
|
||||
const messages = messageBuffer.split('\n');
|
||||
messageBuffer = messages.pop() ?? '';
|
||||
|
||||
for (const message of messages) {
|
||||
const trimmedMessage = message.trim();
|
||||
if (trimmedMessage) {
|
||||
console.log('[ADB Reader] Processing message:', trimmedMessage.slice(0, 200));
|
||||
handleAdbPayload(trimmedMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const remainingMessage = messageBuffer.trim();
|
||||
if (remainingMessage) {
|
||||
handleAdbPayload(remainingMessage);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('recipe menu read error', e);
|
||||
} finally {
|
||||
adbWriter.set(null);
|
||||
addNotification('WARN:Android recipe menu channel offline ...');
|
||||
if (retryOnFailure) {
|
||||
scheduleRecipeMenuAndroidServerReconnect();
|
||||
}
|
||||
}
|
||||
})();
|
||||
} catch (err) {
|
||||
console.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) {
|
||||
scheduleRecipeMenuAndroidServerReconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// logcat stream
|
||||
|
||||
// TODO: screen mirror
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ export async function checkAllowAccess(userDomain: string): Promise<boolean> {
|
|||
|
||||
if (snapshot.exists()) {
|
||||
let domains = snapshot.data();
|
||||
// console.log(`domains: ${JSON.stringify(domains)}`);
|
||||
return domains['account_email'].includes(userDomain);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,8 +18,11 @@ async function sendCommand(type: string, params?: string[]) {
|
|||
let inst = adb.getAdbInstance();
|
||||
if (inst) {
|
||||
try {
|
||||
const commandPath = env.PUBLIC_BREW_CMD_WEB;
|
||||
if (!commandPath) throw new BrewCommandError('PUBLIC_BREW_CMD_WEB is not configured');
|
||||
|
||||
let cmd = type + ' ' + (params?.join(' ') ?? '');
|
||||
await adb.push(env.PUBLIC_BREW_CMD_WEB, cmd);
|
||||
await adb.push(commandPath, cmd);
|
||||
} catch (e) {
|
||||
throw new BrewCommandError('Command failed', `${e}`);
|
||||
}
|
||||
|
|
@ -32,9 +35,18 @@ async function sendReset() {
|
|||
let inst = adb.getAdbInstance();
|
||||
if (inst) {
|
||||
try {
|
||||
await adb.push(env.PUBLIC_BREW_CMD_WEB, '');
|
||||
await adb.push(env.PUBLIC_BREW_CURRENT_RECIPE, '');
|
||||
await adb.push(env.PUBLIC_BREW_WEB_STATUS, '');
|
||||
const commandPath = env.PUBLIC_BREW_CMD_WEB;
|
||||
const currentRecipePath = env.PUBLIC_BREW_CURRENT_RECIPE;
|
||||
const statusPath = env.PUBLIC_BREW_WEB_STATUS;
|
||||
|
||||
if (!commandPath) throw new BrewCommandError('PUBLIC_BREW_CMD_WEB is not configured');
|
||||
if (!currentRecipePath)
|
||||
throw new BrewCommandError('PUBLIC_BREW_CURRENT_RECIPE is not configured');
|
||||
if (!statusPath) throw new BrewCommandError('PUBLIC_BREW_WEB_STATUS is not configured');
|
||||
|
||||
await adb.push(commandPath, '');
|
||||
await adb.push(currentRecipePath, '');
|
||||
await adb.push(statusPath, '');
|
||||
} catch (e) {
|
||||
throw new BrewCommandError('Reset failed', `${e}`);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,42 +1,73 @@
|
|||
import { updateMachineStatus } from '../stores/machineInfoStore';
|
||||
import { addNotification } from '../stores/noti';
|
||||
import {
|
||||
loadAndroidRecipeExportFromDevice,
|
||||
saveAndroidRecipeExportPayload
|
||||
} from '../services/androidRecipeExportService';
|
||||
import { handleIncomingMessages } from './messageHandler';
|
||||
import { setMenuSaved, setMenuSaveError } from '../stores/menuSaveStore';
|
||||
|
||||
type AdbPayload = { type: string; payload: any };
|
||||
|
||||
async function handleAdbPayload(raw_payload: string) {
|
||||
console.log('get payload', raw_payload);
|
||||
console.log('[ADB] Received payload:', raw_payload.slice(0, 300));
|
||||
try {
|
||||
const payload: AdbPayload = JSON.parse(raw_payload);
|
||||
console.log('[ADB] Parsed type:', payload.type, 'payload:', payload.payload);
|
||||
switch (payload.type) {
|
||||
case 'log':
|
||||
let log_level = payload.payload['level'] ?? 'INFO';
|
||||
let log_message = payload.payload['msg'] ?? '';
|
||||
|
||||
if (log_message !== '') addNotification(`${log_level}`);
|
||||
if (log_message !== '') {
|
||||
console.log('[ADB LOG]', log_level, log_message);
|
||||
addNotification(`${log_level}:${log_message}`);
|
||||
}
|
||||
break;
|
||||
case 'response':
|
||||
if (payload.payload instanceof String) {
|
||||
if (typeof payload.payload === 'string' || payload.payload instanceof String) {
|
||||
// single message response
|
||||
let raw_payload = payload.payload.toString();
|
||||
|
||||
if (raw_payload.startsWith('save_recipe_machine')) {
|
||||
if (
|
||||
raw_payload.startsWith('save_recipe_machine') ||
|
||||
raw_payload.startsWith('save_recipe_menu_file')
|
||||
) {
|
||||
let res = raw_payload.split('/');
|
||||
|
||||
let pd = res[1] ?? '';
|
||||
let action = res[2] ?? '';
|
||||
let uiAction = res[3] ?? '';
|
||||
|
||||
handleIncomingMessages(
|
||||
JSON.stringify({
|
||||
type: 'ui_action',
|
||||
payload: {
|
||||
action: uiAction,
|
||||
from: 'brew',
|
||||
ref: `${pd}.${action}`
|
||||
}
|
||||
})
|
||||
);
|
||||
console.log('[ADB] Save response parsed:', { pd, action, uiAction, raw_payload });
|
||||
|
||||
// Track menu save status
|
||||
if (raw_payload.startsWith('save_recipe_menu_file') && pd) {
|
||||
if (action === 'success' || action === 'ok' || uiAction === 'refreshNow') {
|
||||
setMenuSaved(pd);
|
||||
addNotification(`INFO:Menu saved: ${pd}`);
|
||||
} else if (action === 'error' || action === 'fail') {
|
||||
setMenuSaveError(pd, 'Save failed');
|
||||
addNotification(`ERR:Failed to save menu: ${pd}`);
|
||||
} else {
|
||||
// Assume success if we get a response
|
||||
setMenuSaved(pd);
|
||||
addNotification(`INFO:Menu saved: ${pd}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (raw_payload.startsWith('save_recipe_machine')) {
|
||||
handleIncomingMessages(
|
||||
JSON.stringify({
|
||||
type: 'ui_action',
|
||||
payload: {
|
||||
action: uiAction,
|
||||
from: 'brew',
|
||||
ref: `${pd}.${action}`
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
} else if (raw_payload.startsWith('state')) {
|
||||
let res = raw_payload.split('/');
|
||||
let new_machine_state = res[1] ?? '';
|
||||
|
|
@ -84,6 +115,39 @@ async function handleAdbPayload(raw_payload: string) {
|
|||
addNotification(`ERR:${payload.payload}`);
|
||||
// send message to server if needed
|
||||
break;
|
||||
case 'recipe-export':
|
||||
if (payload.payload?.content) {
|
||||
saveAndroidRecipeExportPayload({
|
||||
content: payload.payload.content,
|
||||
exportedAt: payload.payload.exportedAt,
|
||||
source: payload.payload.source,
|
||||
fileSizeBytes: payload.payload.fileSizeBytes,
|
||||
lineCount: payload.payload.lineCount,
|
||||
message: payload.payload.message
|
||||
});
|
||||
addNotification('INFO:Recipe export received from Android');
|
||||
} else if (payload.payload?.message) {
|
||||
addNotification(`ERR:${payload.payload.message}`);
|
||||
}
|
||||
break;
|
||||
case 'recipe-export-ready':
|
||||
if (payload.payload?.message && payload.payload?.fileSizeBytes === 0) {
|
||||
addNotification(`ERR:${payload.payload.message}`);
|
||||
break;
|
||||
}
|
||||
|
||||
void loadAndroidRecipeExportFromDevice({
|
||||
exportedAt: payload.payload?.exportedAt,
|
||||
source: payload.payload?.source,
|
||||
fileSizeBytes: payload.payload?.fileSizeBytes,
|
||||
lineCount: payload.payload?.lineCount,
|
||||
message: payload.payload?.message
|
||||
})
|
||||
.then(() => addNotification('INFO:Recipe export loaded from Android'))
|
||||
.catch((error: any) =>
|
||||
addNotification(`ERR:${error?.message ?? 'Unable to load recipe export from Android'}`)
|
||||
);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
} catch (error: any) {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,24 @@ import {
|
|||
toppingGroupFromServerQuery,
|
||||
toppingListFromServerQuery
|
||||
} from '../stores/recipeStore';
|
||||
import {
|
||||
handleSheetStreamStart,
|
||||
handleSheetStreamChunk,
|
||||
handleSheetStreamEnd,
|
||||
handleSheetStreamError,
|
||||
handleCatalogsResponse,
|
||||
handleListMenuResponse,
|
||||
sheetCatalogsLoading,
|
||||
handleRawStreamHeader,
|
||||
handleRawStreamChunk,
|
||||
handleRawStreamEnd
|
||||
} from '../stores/sheetStore';
|
||||
import {
|
||||
handleGenLayoutBatchStart,
|
||||
handleGenLayoutFile,
|
||||
handleGenLayoutBatchEnd,
|
||||
handleGenLayoutError
|
||||
} from '../stores/genLayoutStore';
|
||||
import { buildOverviewFromServer } from '$lib/data/recipeService';
|
||||
import { auth } from '../client/firebase';
|
||||
import { type RecipeVersion } from '$lib/models/recipe_version.model';
|
||||
|
|
@ -202,19 +220,105 @@ const handlers: Record<string, (payload: any) => void> = {
|
|||
},
|
||||
stream_patch_update: (p) => {},
|
||||
notify: (p) => {
|
||||
let noti_level = p.level ?? 'INFO';
|
||||
let msg = p.msg ?? `Notify from ${p.from}`;
|
||||
let target = p.to;
|
||||
const from = p.from;
|
||||
const level = p.level ?? 'INFO';
|
||||
const msg = p.msg;
|
||||
const target = p.to;
|
||||
|
||||
// Handle list-menu response
|
||||
if (from === 'list-menu') {
|
||||
const currentUid = auth.currentUser?.uid;
|
||||
if (target && currentUid && target === currentUid && p.value) {
|
||||
handleListMenuResponse({ codes: p.value });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle gen-service responses
|
||||
if (from === 'gen-service') {
|
||||
switch (level) {
|
||||
case 'batch_start':
|
||||
handleGenLayoutBatchStart({
|
||||
batch_id: p.batch_id,
|
||||
total_files: p.total_files,
|
||||
total_size_bytes: p.total_size_bytes
|
||||
});
|
||||
addNotification(`INFO:Gen Layout started (${p.total_files} files)`);
|
||||
break;
|
||||
case 'file':
|
||||
handleGenLayoutFile({
|
||||
batch_id: p.batch_id,
|
||||
file_index: p.file_index,
|
||||
total_files: p.total_files,
|
||||
file: p.file,
|
||||
content: p.content,
|
||||
is_chunked: p.is_chunked,
|
||||
part_index: p.part_index,
|
||||
total_parts: p.total_parts,
|
||||
is_last_part: p.is_last_part
|
||||
});
|
||||
break;
|
||||
case 'batch_end':
|
||||
handleGenLayoutBatchEnd({
|
||||
batch_id: p.batch_id,
|
||||
total_files: p.total_files
|
||||
});
|
||||
addNotification('INFO:Gen Layout complete');
|
||||
break;
|
||||
case 'ERROR':
|
||||
handleGenLayoutError(msg);
|
||||
addNotification(`ERR:Gen Layout error: ${msg}`);
|
||||
break;
|
||||
default:
|
||||
console.log('[GenService] Received:', level, msg);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (from === 'sheet-service' && level === 'content') {
|
||||
const currentUid = auth.currentUser?.uid;
|
||||
|
||||
if (target && currentUid && target === currentUid) {
|
||||
if (!msg && p.content?.catalogs) {
|
||||
handleCatalogsResponse(p.content);
|
||||
addNotification(`INFO:Loaded ${p.content.catalogs?.length || 0} catalogs`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle streaming messages (with msg field)
|
||||
switch (msg) {
|
||||
case 'start':
|
||||
handleSheetStreamStart(p);
|
||||
addNotification('INFO:Sheet data streaming started');
|
||||
break;
|
||||
case 'chunk':
|
||||
handleSheetStreamChunk(p);
|
||||
break;
|
||||
case 'end':
|
||||
handleSheetStreamEnd(p);
|
||||
addNotification('INFO:Sheet data streaming complete');
|
||||
break;
|
||||
case 'error':
|
||||
handleSheetStreamError(p);
|
||||
addNotification(`ERR:Sheet streaming error: ${p.content?.error_detail}`);
|
||||
break;
|
||||
default:
|
||||
// Handle other content notifications from sheet-service
|
||||
console.log('[Sheet] Received content:', p.content);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Default notification handling
|
||||
if (target) {
|
||||
//
|
||||
let currentUsername = auth.currentUser?.displayName;
|
||||
if (currentUsername && currentUsername === target) {
|
||||
addNotification(`${noti_level}:${msg}`);
|
||||
addNotification(`${level}:${msg}`);
|
||||
}
|
||||
} else {
|
||||
// broadcast to all
|
||||
addNotification(`${noti_level}:${msg}`);
|
||||
addNotification(`${level}:${msg}`);
|
||||
}
|
||||
},
|
||||
ui_action: (p) => {
|
||||
|
|
@ -259,12 +363,33 @@ const handlers: Record<string, (payload: any) => void> = {
|
|||
socketConnectionOfflineCount.set(0);
|
||||
socketAlreadySendHeartbeat.set(0);
|
||||
console.log('heartbeat reset offline count');
|
||||
},
|
||||
// Raw stream handlers for sheet data (e.g., price)
|
||||
raw_stream: (p) => {
|
||||
// Format: raw_stream with subtype in payload
|
||||
// Header: { subtype: 'price', request_id, header?, country? }
|
||||
const subtype = p.subtype;
|
||||
if (subtype) {
|
||||
handleRawStreamHeader(subtype, p);
|
||||
}
|
||||
},
|
||||
raw_stream_price: (p) => {
|
||||
// Header for price stream
|
||||
handleRawStreamHeader('price', p);
|
||||
},
|
||||
raw_stream_chunk_price: (p) => {
|
||||
// Chunk for price stream
|
||||
handleRawStreamChunk('price', p);
|
||||
},
|
||||
raw_stream_end_price: (p) => {
|
||||
// End for price stream
|
||||
handleRawStreamEnd('price', p);
|
||||
}
|
||||
};
|
||||
|
||||
export function handleIncomingMessages(raw: string) {
|
||||
const msg: WSMessage = JSON.parse(raw);
|
||||
// console.log(`${new Date().toLocaleTimeString()}:ws msg`, msg);
|
||||
// console.log(`[WS MSG] type=${msg.type}`, msg.payload);
|
||||
if (msg == null) {
|
||||
// error response
|
||||
addNotification('ERR:No response from server');
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ function getServiceName(cmdReq: CommandRequest) {
|
|||
}
|
||||
|
||||
// Websocket message wrapper for commands like `sheet`, `command`
|
||||
export function sendCommandRequest(target: CommandRequest, values: any) {
|
||||
export function sendCommandRequest(target: CommandRequest, values: any): boolean {
|
||||
let srv_name = getServiceName(target);
|
||||
let curr_user = get(auth);
|
||||
|
||||
|
|
@ -31,7 +31,7 @@ export function sendCommandRequest(target: CommandRequest, values: any) {
|
|||
};
|
||||
}
|
||||
|
||||
sendMessage({
|
||||
return sendMessage({
|
||||
type: target,
|
||||
payload: {
|
||||
user_info: user_info ?? {},
|
||||
|
|
|
|||
284
src/lib/core/services/androidRecipeExportService.ts
Normal file
284
src/lib/core/services/androidRecipeExportService.ts
Normal file
|
|
@ -0,0 +1,284 @@
|
|||
import { browser } from '$app/environment';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
export const ANDROID_RECIPE_EXPORT_PATH = '/mnt/sdcard/recipe_export_all.tsv';
|
||||
const ANDROID_RECIPE_EXPORT_CACHE_KEY = 'android_recipe_export_payload_v1';
|
||||
const ANDROID_RECIPE_EXPORT_DB_NAME = 'android_recipe_export_cache';
|
||||
const ANDROID_RECIPE_EXPORT_STORE_NAME = 'payloads';
|
||||
|
||||
export type AndroidRecipeExportPayload = {
|
||||
content: string;
|
||||
exportedAt?: number;
|
||||
source?: string;
|
||||
fileSizeBytes?: number;
|
||||
lineCount?: number;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
export type AndroidRecipeExportRow = {
|
||||
lineNumber: number;
|
||||
cells: string[];
|
||||
values: Record<string, string>;
|
||||
};
|
||||
|
||||
export type AndroidRecipeExportData = {
|
||||
headers: string[];
|
||||
rows: AndroidRecipeExportRow[];
|
||||
lineCount: number;
|
||||
};
|
||||
|
||||
export const androidRecipeExportPayload = writable<AndroidRecipeExportPayload | null>(null);
|
||||
let deviceExportLoadPromise: Promise<void> | null = null;
|
||||
|
||||
function openAndroidRecipeExportDb(): Promise<IDBDatabase> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = indexedDB.open(ANDROID_RECIPE_EXPORT_DB_NAME, 1);
|
||||
|
||||
request.onupgradeneeded = () => {
|
||||
request.result.createObjectStore(ANDROID_RECIPE_EXPORT_STORE_NAME);
|
||||
};
|
||||
request.onsuccess = () => resolve(request.result);
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
async function readCachedPayloadFromIndexedDb(): Promise<AndroidRecipeExportPayload | null> {
|
||||
const db = await openAndroidRecipeExportDb();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction(ANDROID_RECIPE_EXPORT_STORE_NAME, 'readonly');
|
||||
const store = transaction.objectStore(ANDROID_RECIPE_EXPORT_STORE_NAME);
|
||||
const request = store.get(ANDROID_RECIPE_EXPORT_CACHE_KEY);
|
||||
|
||||
request.onsuccess = () => resolve((request.result as AndroidRecipeExportPayload) ?? null);
|
||||
request.onerror = () => reject(request.error);
|
||||
transaction.oncomplete = () => db.close();
|
||||
transaction.onerror = () => {
|
||||
db.close();
|
||||
reject(transaction.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async function writeCachedPayloadToIndexedDb(payload: AndroidRecipeExportPayload): Promise<void> {
|
||||
const db = await openAndroidRecipeExportDb();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction(ANDROID_RECIPE_EXPORT_STORE_NAME, 'readwrite');
|
||||
const store = transaction.objectStore(ANDROID_RECIPE_EXPORT_STORE_NAME);
|
||||
|
||||
store.put(payload, ANDROID_RECIPE_EXPORT_CACHE_KEY);
|
||||
transaction.oncomplete = () => {
|
||||
db.close();
|
||||
resolve();
|
||||
};
|
||||
transaction.onerror = () => {
|
||||
db.close();
|
||||
reject(transaction.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async function deleteCachedPayloadFromIndexedDb(): Promise<void> {
|
||||
const db = await openAndroidRecipeExportDb();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction(ANDROID_RECIPE_EXPORT_STORE_NAME, 'readwrite');
|
||||
const store = transaction.objectStore(ANDROID_RECIPE_EXPORT_STORE_NAME);
|
||||
|
||||
store.delete(ANDROID_RECIPE_EXPORT_CACHE_KEY);
|
||||
transaction.oncomplete = () => {
|
||||
db.close();
|
||||
resolve();
|
||||
};
|
||||
transaction.onerror = () => {
|
||||
db.close();
|
||||
reject(transaction.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export async function loadCachedAndroidRecipeExport(): Promise<AndroidRecipeExportPayload | null> {
|
||||
if (!browser) return null;
|
||||
|
||||
try {
|
||||
const payload = await readCachedPayloadFromIndexedDb();
|
||||
if (!payload?.content) return null;
|
||||
|
||||
androidRecipeExportPayload.set(payload);
|
||||
return payload;
|
||||
} catch (error) {
|
||||
console.error('failed to load cached android recipe export from IndexedDB', error);
|
||||
}
|
||||
|
||||
try {
|
||||
const cached = localStorage.getItem(ANDROID_RECIPE_EXPORT_CACHE_KEY);
|
||||
if (!cached) return null;
|
||||
|
||||
const payload = JSON.parse(cached) as AndroidRecipeExportPayload;
|
||||
if (!payload?.content) return null;
|
||||
|
||||
androidRecipeExportPayload.set(payload);
|
||||
void writeCachedPayloadToIndexedDb(payload);
|
||||
localStorage.removeItem(ANDROID_RECIPE_EXPORT_CACHE_KEY);
|
||||
return payload;
|
||||
} catch (error) {
|
||||
console.error('failed to load legacy android recipe export cache', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function saveAndroidRecipeExportPayload(payload: AndroidRecipeExportPayload) {
|
||||
androidRecipeExportPayload.set(payload);
|
||||
|
||||
if (!browser) return;
|
||||
|
||||
void writeCachedPayloadToIndexedDb(payload).catch((error) => {
|
||||
console.error('failed to cache android recipe export', error);
|
||||
});
|
||||
}
|
||||
|
||||
export function clearCachedAndroidRecipeExport() {
|
||||
androidRecipeExportPayload.set(null);
|
||||
|
||||
if (!browser) return;
|
||||
|
||||
localStorage.removeItem(ANDROID_RECIPE_EXPORT_CACHE_KEY);
|
||||
void deleteCachedPayloadFromIndexedDb().catch((error) => {
|
||||
console.error('failed to clear android recipe export cache', error);
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeHeader(value: string, index: number): string {
|
||||
const header = value.trim().replace(/^\uFEFF/, '');
|
||||
return header || `Column ${index + 1}`;
|
||||
}
|
||||
|
||||
function splitTsvLine(line: string): string[] {
|
||||
return line.split('\t').map((cell) => cell.trim());
|
||||
}
|
||||
|
||||
function collectNonEmptyLines(raw: string, maxLines: number): string[] {
|
||||
const lines: string[] = [];
|
||||
let lineStart = 0;
|
||||
|
||||
for (let index = 0; index <= raw.length; index += 1) {
|
||||
const isEnd = index === raw.length;
|
||||
const char = raw[index];
|
||||
|
||||
if (!isEnd && char !== '\n') continue;
|
||||
|
||||
const lineEnd = index > lineStart && raw[index - 1] === '\r' ? index - 1 : index;
|
||||
const line = raw.slice(lineStart, lineEnd);
|
||||
|
||||
if (line.trim().length > 0) {
|
||||
lines.push(line);
|
||||
if (lines.length >= maxLines) break;
|
||||
}
|
||||
|
||||
lineStart = index + 1;
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
function buildUniqueHeaders(rawHeaders: string[], maxColumns: number): string[] {
|
||||
const headers = [...rawHeaders];
|
||||
|
||||
for (let i = headers.length; i < maxColumns; i += 1) {
|
||||
headers.push(`Column ${i + 1}`);
|
||||
}
|
||||
|
||||
const seen = new Map<string, number>();
|
||||
return headers.map((header, index) => {
|
||||
const normalized = normalizeHeader(header, index);
|
||||
const count = seen.get(normalized) ?? 0;
|
||||
seen.set(normalized, count + 1);
|
||||
|
||||
return count === 0 ? normalized : `${normalized} ${count + 1}`;
|
||||
});
|
||||
}
|
||||
|
||||
export function parseAndroidRecipeExport(
|
||||
raw: string,
|
||||
maxRows = Number.POSITIVE_INFINITY
|
||||
): AndroidRecipeExportData {
|
||||
const maxLines = Number.isFinite(maxRows) ? Math.max(1, maxRows + 1) : Number.MAX_SAFE_INTEGER;
|
||||
const lines = collectNonEmptyLines(raw, maxLines);
|
||||
|
||||
if (lines.length === 0) {
|
||||
return {
|
||||
headers: [],
|
||||
rows: [],
|
||||
lineCount: 0
|
||||
};
|
||||
}
|
||||
|
||||
const parsedLines = lines.map(splitTsvLine);
|
||||
const maxColumns = Math.max(...parsedLines.map((line) => line.length));
|
||||
const headers = buildUniqueHeaders(parsedLines[0], maxColumns);
|
||||
|
||||
const rows = parsedLines.slice(1).map((cells, index) => {
|
||||
const paddedCells = [...cells];
|
||||
|
||||
for (let cellIndex = paddedCells.length; cellIndex < headers.length; cellIndex += 1) {
|
||||
paddedCells.push('');
|
||||
}
|
||||
|
||||
const values = Object.fromEntries(
|
||||
headers.map((header, cellIndex) => [header, paddedCells[cellIndex] ?? ''])
|
||||
);
|
||||
|
||||
return {
|
||||
lineNumber: index + 2,
|
||||
cells: paddedCells,
|
||||
values
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
headers,
|
||||
rows,
|
||||
lineCount: lines.length
|
||||
};
|
||||
}
|
||||
|
||||
export async function pullAndroidRecipeExport(timeoutMs = 15000): Promise<string> {
|
||||
const adb = await import('$lib/core/adb/adb');
|
||||
const instance = adb.getAdbInstance();
|
||||
|
||||
if (!instance) {
|
||||
throw new Error('ADB device is not connected');
|
||||
}
|
||||
|
||||
const content = await adb.pull(ANDROID_RECIPE_EXPORT_PATH, timeoutMs);
|
||||
|
||||
if (content === undefined) {
|
||||
throw new Error(`Unable to pull ${ANDROID_RECIPE_EXPORT_PATH}`);
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
export async function loadAndroidRecipeExportFromDevice(
|
||||
meta: Partial<AndroidRecipeExportPayload> = {}
|
||||
): Promise<void> {
|
||||
if (deviceExportLoadPromise) return deviceExportLoadPromise;
|
||||
|
||||
deviceExportLoadPromise = (async () => {
|
||||
const content = await pullAndroidRecipeExport(30000);
|
||||
|
||||
saveAndroidRecipeExportPayload({
|
||||
content,
|
||||
exportedAt: meta.exportedAt ?? Date.now(),
|
||||
source: meta.source ?? ANDROID_RECIPE_EXPORT_PATH,
|
||||
fileSizeBytes: meta.fileSizeBytes,
|
||||
lineCount: meta.lineCount,
|
||||
message: meta.message
|
||||
});
|
||||
})().finally(() => {
|
||||
deviceExportLoadPromise = null;
|
||||
});
|
||||
|
||||
return deviceExportLoadPromise;
|
||||
}
|
||||
251
src/lib/core/services/sheetService.ts
Normal file
251
src/lib/core/services/sheetService.ts
Normal file
|
|
@ -0,0 +1,251 @@
|
|||
import { sendCommandRequest, sendMessage } from '../handlers/ws_messageSender';
|
||||
import { get } from 'svelte/store';
|
||||
import { auth } from '../stores/auth';
|
||||
import {
|
||||
productCodesLoading,
|
||||
hasSheetPriceBeenSent,
|
||||
markSheetPriceAsSent,
|
||||
sheetPriceLoading,
|
||||
streamingRawData,
|
||||
setPendingProductCodesCountry
|
||||
} from '../stores/sheetStore';
|
||||
import { setGenLayoutGenerating } from '../stores/genLayoutStore';
|
||||
|
||||
export function requestCatalogs(country: string): boolean {
|
||||
return sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
param: 'catalogs'
|
||||
});
|
||||
}
|
||||
|
||||
export function enterRoom(country: string, catalog: string): boolean {
|
||||
return sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
catalog: catalog,
|
||||
param: 'enter'
|
||||
});
|
||||
}
|
||||
|
||||
export function sendHeartbeat(country: string, catalog: string): boolean {
|
||||
return sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
catalog: catalog,
|
||||
param: 'heartbeat'
|
||||
});
|
||||
}
|
||||
|
||||
export function exitRoom(country: string, catalog: string): boolean {
|
||||
return sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
catalog: catalog,
|
||||
param: 'exit'
|
||||
});
|
||||
}
|
||||
|
||||
export function requestCatalogMenu(country: string, catalog: string): boolean {
|
||||
return sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
catalog: catalog,
|
||||
param: 'catalog/menu'
|
||||
});
|
||||
}
|
||||
|
||||
export function updateMenu(country: string, catalog: string, content: any[]): boolean {
|
||||
return sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
catalog: catalog,
|
||||
content: content,
|
||||
param: 'update/menu'
|
||||
});
|
||||
}
|
||||
|
||||
export function addMenu(country: string, catalog: string, content: any[]): boolean {
|
||||
console.log('[sheetService] Adding menu:', { country, catalog, content });
|
||||
const sent = sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
catalog: catalog,
|
||||
content: content,
|
||||
param: 'add/menu'
|
||||
});
|
||||
console.log('[sheetService] Add menu sent:', sent);
|
||||
return sent;
|
||||
}
|
||||
|
||||
export function deleteMenu(country: string, catalog: string, targetIds: number[]): boolean {
|
||||
const content = targetIds.map((id) => ({ target_id: id }));
|
||||
return sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
catalog: catalog,
|
||||
content: content,
|
||||
param: 'delete/menu'
|
||||
});
|
||||
}
|
||||
|
||||
export function swapMenu(
|
||||
country: string,
|
||||
catalog: string,
|
||||
swaps: { source_id: number; target_id: number }[]
|
||||
): boolean {
|
||||
return sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
catalog: catalog,
|
||||
content: swaps,
|
||||
param: 'swap/menu'
|
||||
});
|
||||
}
|
||||
|
||||
export function requestListMenu(country: string, boxid?: string): boolean {
|
||||
const curr_user = get(auth);
|
||||
|
||||
let user_info: any = {};
|
||||
if (curr_user) {
|
||||
user_info = {
|
||||
displayName: curr_user.displayName,
|
||||
email: curr_user.email,
|
||||
uid: curr_user.uid
|
||||
};
|
||||
}
|
||||
|
||||
productCodesLoading.set(true);
|
||||
setPendingProductCodesCountry(country);
|
||||
|
||||
console.log('[sheetService] Sending list_menu request for country:', country, 'boxid:', boxid);
|
||||
|
||||
return sendMessage({
|
||||
type: 'list_menu',
|
||||
payload: {
|
||||
user_info,
|
||||
country,
|
||||
boxid: boxid || undefined
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function requestGenLayout(country: string): boolean {
|
||||
const curr_user = get(auth);
|
||||
|
||||
let user_info: any = {};
|
||||
if (curr_user) {
|
||||
user_info = {
|
||||
displayName: curr_user.displayName,
|
||||
email: curr_user.email,
|
||||
uid: curr_user.uid
|
||||
};
|
||||
}
|
||||
|
||||
setGenLayoutGenerating();
|
||||
|
||||
console.log('[sheetService] Sending gen-layout request for country:', country);
|
||||
|
||||
return sendMessage({
|
||||
type: 'command',
|
||||
payload: {
|
||||
user_info,
|
||||
srv_name: 'gen-service',
|
||||
values: {
|
||||
file_layout: 'sheet',
|
||||
file_desc: 'sheet',
|
||||
country: country,
|
||||
param: 'new-inter-v3-multi-promotion-other_catalog-supra_app'
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Request price data from sheet for specific product codes
|
||||
* NOTE: Can only send once per type (price). Use hasSheetPriceBeenSent to check.
|
||||
*/
|
||||
export function requestSheetPrice(country: string, productCodes: string[]): boolean {
|
||||
// Check if already sent
|
||||
if (hasSheetPriceBeenSent('price')) {
|
||||
console.warn('[sheetService] Price request already sent, skipping');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!productCodes || productCodes.length === 0) {
|
||||
console.warn('[sheetService] No product codes to request price for');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Generate request_id (UUID v4)
|
||||
const request_id = crypto.randomUUID();
|
||||
|
||||
// Store request_id and country in streamingRawData for tracking
|
||||
streamingRawData.update((data) => ({
|
||||
...data,
|
||||
price: {
|
||||
request_id,
|
||||
country,
|
||||
chunks: [],
|
||||
rawParts: []
|
||||
}
|
||||
}));
|
||||
|
||||
sheetPriceLoading.set(true);
|
||||
|
||||
// Convert to array of objects (backend expects objects, not strings)
|
||||
const content = productCodes.map((code) => ({ product_code: code }));
|
||||
|
||||
console.log('[sheetService] Sending sheet price request for country:', country, 'codes:', productCodes.length, 'request_id:', request_id);
|
||||
|
||||
const sent = sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
content: content,
|
||||
param: 'price',
|
||||
stream: true,
|
||||
request_id
|
||||
});
|
||||
|
||||
if (sent) {
|
||||
markSheetPriceAsSent('price');
|
||||
} else {
|
||||
sheetPriceLoading.set(false);
|
||||
}
|
||||
|
||||
return sent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update price data in sheet
|
||||
* content: [{ row_index: number, cells: [{ value: string, coord: { row: number, col: number } }] }]
|
||||
*/
|
||||
export function updateSheetPrice(
|
||||
country: string,
|
||||
content: { row_index: number; cells: { value: string; coord: { row: number; col: number } }[] }[]
|
||||
): boolean {
|
||||
if (!content || content.length === 0) {
|
||||
console.warn('[sheetService] No content to update');
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log('[sheetService] Updating sheet price for country:', country, 'items:', content.length);
|
||||
|
||||
return sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
content: content,
|
||||
param: 'update/price'
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new price rows to sheet (for product codes that don't exist in price sheet)
|
||||
* content: [{ cells: [product_code, name_en, name_th, ..., price, ...] }]
|
||||
*/
|
||||
export function addSheetPrice(
|
||||
country: string,
|
||||
content: { cells: string[] }[]
|
||||
): boolean {
|
||||
if (!content || content.length === 0) {
|
||||
console.warn('[sheetService] No content to add');
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log('[sheetService] Adding price rows for country:', country, 'items:', content.length, content);
|
||||
|
||||
return sendCommandRequest('sheet', {
|
||||
country: country,
|
||||
content: content,
|
||||
param: 'add/price'
|
||||
});
|
||||
}
|
||||
|
|
@ -7,16 +7,27 @@ async function sendToAndroid(message: any) {
|
|||
let writer: any = get(adbWriter);
|
||||
console.log('writer', writer);
|
||||
if (!writer) {
|
||||
// addNotification('ERR:No active connection');
|
||||
return;
|
||||
addNotification('ERR:No active Android connection');
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
const encoder = new TextEncoder();
|
||||
// console.log(writer);
|
||||
await writer.write(encoder.encode(JSON.stringify(message) + '\n'));
|
||||
console.log('sent! ', JSON.stringify(message).length);
|
||||
const serializedMessage = JSON.stringify(message);
|
||||
await writer.write(encoder.encode(serializedMessage + '\n'));
|
||||
console.log('[ADB] sent', {
|
||||
type: message?.type,
|
||||
bytes: serializedMessage.length,
|
||||
productCode: message?.payload?.data?.productCode,
|
||||
batchCount: Array.isArray(message?.payload?.data) ? message.payload.data.length : undefined,
|
||||
batchProductCodes: Array.isArray(message?.payload?.data)
|
||||
? message.payload.data.map((menu: any) => menu?.productCode)
|
||||
: undefined
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('write failed', error);
|
||||
addNotification('ERR:Failed to send message to Android');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
249
src/lib/core/stores/genLayoutStore.ts
Normal file
249
src/lib/core/stores/genLayoutStore.ts
Normal file
|
|
@ -0,0 +1,249 @@
|
|||
import { writable, get } from 'svelte/store';
|
||||
|
||||
export interface GenLayoutFile {
|
||||
file: string;
|
||||
content: string;
|
||||
file_index: number;
|
||||
}
|
||||
|
||||
export interface GenLayoutBatch {
|
||||
batch_id: string;
|
||||
total_files: number;
|
||||
total_size_bytes: number;
|
||||
status: 'idle' | 'generating' | 'receiving' | 'complete' | 'error';
|
||||
files: GenLayoutFile[];
|
||||
error?: string;
|
||||
}
|
||||
|
||||
// Track chunked file parts: Map<file_index, Map<part_index, content>>
|
||||
interface ChunkedFileTracker {
|
||||
file: string;
|
||||
file_index: number;
|
||||
total_parts: number;
|
||||
parts: Map<number, string>;
|
||||
}
|
||||
|
||||
const initialState: GenLayoutBatch = {
|
||||
batch_id: '',
|
||||
total_files: 0,
|
||||
total_size_bytes: 0,
|
||||
status: 'idle',
|
||||
files: []
|
||||
};
|
||||
|
||||
export const genLayoutBatch = writable<GenLayoutBatch>(initialState);
|
||||
|
||||
// Track chunked files being assembled
|
||||
const chunkedFiles = new Map<number, ChunkedFileTracker>();
|
||||
|
||||
// Callbacks for when batch completes
|
||||
let onBatchCompleteCallback: ((files: GenLayoutFile[]) => void) | null = null;
|
||||
|
||||
export function setOnBatchCompleteCallback(cb: (files: GenLayoutFile[]) => void) {
|
||||
onBatchCompleteCallback = cb;
|
||||
}
|
||||
|
||||
export function clearOnBatchCompleteCallback() {
|
||||
onBatchCompleteCallback = null;
|
||||
}
|
||||
|
||||
export function handleGenLayoutBatchStart(payload: {
|
||||
batch_id: string;
|
||||
total_files: number;
|
||||
total_size_bytes: number;
|
||||
}) {
|
||||
genLayoutBatch.set({
|
||||
batch_id: payload.batch_id,
|
||||
total_files: payload.total_files,
|
||||
total_size_bytes: payload.total_size_bytes,
|
||||
status: 'receiving',
|
||||
files: []
|
||||
});
|
||||
console.log('[GenLayout] Batch started:', payload.batch_id, 'total files:', payload.total_files);
|
||||
}
|
||||
|
||||
export function handleGenLayoutFile(payload: {
|
||||
batch_id: string;
|
||||
file_index: number;
|
||||
total_files: number;
|
||||
file: string;
|
||||
content: string;
|
||||
is_chunked?: boolean;
|
||||
part_index?: number;
|
||||
total_parts?: number;
|
||||
is_last_part?: boolean;
|
||||
}) {
|
||||
const batch = get(genLayoutBatch);
|
||||
if (batch.batch_id !== payload.batch_id) return;
|
||||
|
||||
if (payload.is_chunked) {
|
||||
const fileIndex = payload.file_index;
|
||||
const partIndex = payload.part_index ?? 0;
|
||||
const totalParts = payload.total_parts ?? 1;
|
||||
|
||||
// Get or create tracker for this file
|
||||
let tracker = chunkedFiles.get(fileIndex);
|
||||
if (!tracker) {
|
||||
tracker = {
|
||||
file: payload.file,
|
||||
file_index: fileIndex,
|
||||
total_parts: totalParts,
|
||||
parts: new Map()
|
||||
};
|
||||
chunkedFiles.set(fileIndex, tracker);
|
||||
}
|
||||
|
||||
// Store this chunk
|
||||
tracker.parts.set(partIndex, payload.content);
|
||||
|
||||
console.log(
|
||||
'[GenLayout] Received chunk:',
|
||||
partIndex + 1,
|
||||
'/',
|
||||
totalParts,
|
||||
'for file',
|
||||
fileIndex + 1,
|
||||
'/',
|
||||
payload.total_files,
|
||||
payload.file
|
||||
);
|
||||
|
||||
// Check if all parts received
|
||||
if (tracker.parts.size === totalParts) {
|
||||
// Assemble the complete file content
|
||||
const sortedParts: string[] = [];
|
||||
for (let i = 0; i < totalParts; i++) {
|
||||
sortedParts.push(tracker.parts.get(i) ?? '');
|
||||
}
|
||||
const completeContent = sortedParts.join('');
|
||||
|
||||
// Add to files
|
||||
addFileToStore(payload.batch_id, {
|
||||
file: payload.file,
|
||||
content: completeContent,
|
||||
file_index: fileIndex
|
||||
}, payload.total_files);
|
||||
|
||||
// Clean up tracker
|
||||
chunkedFiles.delete(fileIndex);
|
||||
|
||||
console.log(
|
||||
'[GenLayout] Assembled chunked file:',
|
||||
fileIndex + 1,
|
||||
'/',
|
||||
payload.total_files,
|
||||
payload.file
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Handle non-chunked file
|
||||
addFileToStore(payload.batch_id, {
|
||||
file: payload.file,
|
||||
content: payload.content,
|
||||
file_index: payload.file_index
|
||||
}, payload.total_files);
|
||||
|
||||
console.log(
|
||||
'[GenLayout] Received file:',
|
||||
payload.file_index + 1,
|
||||
'/',
|
||||
payload.total_files,
|
||||
payload.file
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function addFileToStore(batchId: string, file: GenLayoutFile, totalFiles: number) {
|
||||
genLayoutBatch.update((batch) => {
|
||||
if (batch.batch_id !== batchId) return batch;
|
||||
|
||||
const existingIndex = batch.files.findIndex((f) => f.file_index === file.file_index);
|
||||
const newFiles =
|
||||
existingIndex >= 0
|
||||
? batch.files.map((f, index) => (index === existingIndex ? file : f))
|
||||
: [...batch.files, file];
|
||||
const sortedFiles = newFiles.sort((a, b) => a.file_index - b.file_index);
|
||||
|
||||
return {
|
||||
...batch,
|
||||
total_files: totalFiles || batch.total_files,
|
||||
files: sortedFiles
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function handleGenLayoutBatchEnd(payload: { batch_id: string; total_files: number }) {
|
||||
const batch = get(genLayoutBatch);
|
||||
|
||||
if (batch.batch_id !== payload.batch_id) return;
|
||||
|
||||
const expectedTotal = payload.total_files || batch.total_files;
|
||||
const sortedFiles = [...batch.files].sort((a, b) => a.file_index - b.file_index);
|
||||
const missingIndexes = getMissingFileIndexes(sortedFiles, expectedTotal);
|
||||
|
||||
if (missingIndexes.length > 0) {
|
||||
// const error = `Gen Layout incomplete: received ${sortedFiles.length}/${expectedTotal} files. Missing file index: ${missingIndexes.join(', ')}`;
|
||||
const error = `ไฟล์ไม่ครับ ทั้งหมด: ${sortedFiles.length}/${expectedTotal}`
|
||||
genLayoutBatch.update((b) => ({
|
||||
...b,
|
||||
total_files: expectedTotal,
|
||||
status: 'error',
|
||||
files: sortedFiles,
|
||||
error
|
||||
}));
|
||||
console.warn('[GenLayout] Batch incomplete:', error, sortedFiles);
|
||||
return;
|
||||
}
|
||||
|
||||
genLayoutBatch.update((b) => ({
|
||||
...b,
|
||||
total_files: expectedTotal,
|
||||
status: 'complete',
|
||||
files: sortedFiles
|
||||
}));
|
||||
|
||||
console.log('[GenLayout] Batch complete, received', sortedFiles.length, 'files');
|
||||
|
||||
if (onBatchCompleteCallback) {
|
||||
onBatchCompleteCallback(sortedFiles);
|
||||
}
|
||||
}
|
||||
|
||||
function getMissingFileIndexes(files: GenLayoutFile[], totalFiles: number) {
|
||||
if (totalFiles <= 0) return [];
|
||||
|
||||
const receivedIndexes = new Set(files.map((file) => file.file_index));
|
||||
const indexes = [...receivedIndexes];
|
||||
const startsAtOne = indexes.length > 0 && Math.min(...indexes) >= 1;
|
||||
const firstIndex = startsAtOne ? 1 : 0;
|
||||
const lastIndex = startsAtOne ? totalFiles : totalFiles - 1;
|
||||
const missingIndexes: number[] = [];
|
||||
|
||||
for (let index = firstIndex; index <= lastIndex; index += 1) {
|
||||
if (!receivedIndexes.has(index)) {
|
||||
missingIndexes.push(index);
|
||||
}
|
||||
}
|
||||
|
||||
return missingIndexes;
|
||||
}
|
||||
|
||||
export function handleGenLayoutError(error: string) {
|
||||
genLayoutBatch.update((batch) => ({
|
||||
...batch,
|
||||
status: 'error',
|
||||
error
|
||||
}));
|
||||
}
|
||||
|
||||
export function resetGenLayoutBatch() {
|
||||
genLayoutBatch.set(initialState);
|
||||
chunkedFiles.clear();
|
||||
}
|
||||
|
||||
export function setGenLayoutGenerating() {
|
||||
genLayoutBatch.update((batch) => ({
|
||||
...batch,
|
||||
status: 'generating'
|
||||
}));
|
||||
}
|
||||
85
src/lib/core/stores/menuSaveStore.ts
Normal file
85
src/lib/core/stores/menuSaveStore.ts
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
import { writable, get } from 'svelte/store';
|
||||
|
||||
export type MenuSaveStatus = 'idle' | 'saving' | 'saved' | 'error';
|
||||
|
||||
export interface MenuSaveState {
|
||||
productCode: string;
|
||||
status: MenuSaveStatus;
|
||||
error?: string;
|
||||
savedAt?: number;
|
||||
}
|
||||
|
||||
// Store for tracking menu save status
|
||||
export const menuSaveStates = writable<Map<string, MenuSaveState>>(new Map());
|
||||
|
||||
// Callback to be called when a menu is saved successfully
|
||||
let onMenuSavedCallback: ((productCode: string) => void) | null = null;
|
||||
|
||||
export function setOnMenuSavedCallback(callback: (productCode: string) => void) {
|
||||
onMenuSavedCallback = callback;
|
||||
}
|
||||
|
||||
export function clearOnMenuSavedCallback() {
|
||||
onMenuSavedCallback = null;
|
||||
}
|
||||
|
||||
export function setMenuSaving(productCode: string) {
|
||||
menuSaveStates.update((states) => {
|
||||
const newStates = new Map(states);
|
||||
newStates.set(productCode, {
|
||||
productCode,
|
||||
status: 'saving'
|
||||
});
|
||||
return newStates;
|
||||
});
|
||||
}
|
||||
|
||||
export function setMenuSaved(productCode: string) {
|
||||
menuSaveStates.update((states) => {
|
||||
const newStates = new Map(states);
|
||||
newStates.set(productCode, {
|
||||
productCode,
|
||||
status: 'saved',
|
||||
savedAt: Date.now()
|
||||
});
|
||||
return newStates;
|
||||
});
|
||||
|
||||
// Call the callback if registered
|
||||
if (onMenuSavedCallback) {
|
||||
onMenuSavedCallback(productCode);
|
||||
}
|
||||
}
|
||||
|
||||
export function setMenuSaveError(productCode: string, error: string) {
|
||||
menuSaveStates.update((states) => {
|
||||
const newStates = new Map(states);
|
||||
newStates.set(productCode, {
|
||||
productCode,
|
||||
status: 'error',
|
||||
error
|
||||
});
|
||||
return newStates;
|
||||
});
|
||||
}
|
||||
|
||||
export function clearMenuSaveState(productCode: string) {
|
||||
menuSaveStates.update((states) => {
|
||||
const newStates = new Map(states);
|
||||
newStates.delete(productCode);
|
||||
return newStates;
|
||||
});
|
||||
}
|
||||
|
||||
export function getMenuSaveStatus(productCode: string): MenuSaveStatus {
|
||||
const states = get(menuSaveStates);
|
||||
return states.get(productCode)?.status ?? 'idle';
|
||||
}
|
||||
|
||||
export function isMenuSaving(productCode: string): boolean {
|
||||
return getMenuSaveStatus(productCode) === 'saving';
|
||||
}
|
||||
|
||||
export function isMenuSaved(productCode: string): boolean {
|
||||
return getMenuSaveStatus(productCode) === 'saved';
|
||||
}
|
||||
805
src/lib/core/stores/sheetStore.ts
Normal file
805
src/lib/core/stores/sheetStore.ts
Normal file
|
|
@ -0,0 +1,805 @@
|
|||
import { writable, get } from 'svelte/store';
|
||||
|
||||
// Catalog types
|
||||
export interface Catalog {
|
||||
catalog: string;
|
||||
row_index: number;
|
||||
status: 'free' | 'locked';
|
||||
locked_by: string | null;
|
||||
}
|
||||
|
||||
export interface CatalogsResponse {
|
||||
status: string;
|
||||
country: string;
|
||||
catalogs: Catalog[];
|
||||
}
|
||||
|
||||
export const sheetCatalogs = writable<Catalog[]>([]);
|
||||
export const sheetCatalogsLoading = writable<boolean>(false);
|
||||
|
||||
export const countryPrimaryLanguageMap: Record<string, string> = {
|
||||
THAI: 'Thai',
|
||||
tha: 'Thai',
|
||||
cocktail_tha: 'Thai',
|
||||
counter01: 'Thai',
|
||||
MYS: 'Malaysia',
|
||||
mys: 'Malaysia',
|
||||
IDR: 'Indonesian',
|
||||
idr: 'Indonesian',
|
||||
AUS: 'English',
|
||||
aus: 'English',
|
||||
USA_PEPSI: 'English',
|
||||
usa_pepsi: 'English',
|
||||
SG: 'English',
|
||||
SGP: 'English',
|
||||
sgp: 'English',
|
||||
UAE_DUBAI: 'Arabic',
|
||||
uae_dubai: 'Arabic',
|
||||
dubai: 'Arabic',
|
||||
HKG: 'Cantonese',
|
||||
hkg: 'Cantonese',
|
||||
GBR: 'English',
|
||||
gbr: 'English',
|
||||
LTU: 'Lithuanian',
|
||||
ltu: 'Lithuanian',
|
||||
ROU: 'Romanian',
|
||||
rou: 'Romanian',
|
||||
LVA: 'Latvian',
|
||||
lva: 'Latvian',
|
||||
EST: 'Estonian',
|
||||
est: 'Estonian'
|
||||
};
|
||||
|
||||
export function getCountryPrimaryLanguage(countryCode: string): string {
|
||||
return (
|
||||
countryPrimaryLanguageMap[countryCode] ??
|
||||
countryPrimaryLanguageMap[countryCode.toUpperCase()] ??
|
||||
'Unknown'
|
||||
);
|
||||
}
|
||||
|
||||
// Sheet column configuration by country for new_layout_v2
|
||||
// Maps language keys to column indices and product code columns
|
||||
export const SHEET_COLUMN_CONFIG_BY_COUNTRY: Record<string, {
|
||||
language: Record<string, number>;
|
||||
productCode: { hot: number; cold: number; blend: number };
|
||||
primaryLanguage: string;
|
||||
}> = {
|
||||
tha: {
|
||||
language: { en: 3, th: 4, zh: 5, my: 8 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'th'
|
||||
},
|
||||
aus: {
|
||||
language: { en: 3, th: 4 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'en'
|
||||
},
|
||||
gbr: {
|
||||
language: { en: 3 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'en'
|
||||
},
|
||||
hkg: {
|
||||
language: { en: 3, zh_hans: 4, zh_hant: 5, th: 6 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'zh_hant'
|
||||
},
|
||||
ltu: {
|
||||
language: { en: 3, th: 4, lt: 5, ro: 6 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'lt'
|
||||
},
|
||||
rou: {
|
||||
language: { en: 3, th: 4, lt: 5, ro: 6 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'ro'
|
||||
},
|
||||
lva: {
|
||||
language: { en: 3, th: 4, lt: 5, ro: 6 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'en'
|
||||
},
|
||||
est: {
|
||||
language: { en: 3, th: 4, lt: 5, ro: 6 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'en'
|
||||
},
|
||||
mys: {
|
||||
language: { en: 3, th: 4, ms: 7 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'ms'
|
||||
},
|
||||
sgp: {
|
||||
language: { en: 3, th: 4 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'en'
|
||||
},
|
||||
uae_dubai: {
|
||||
language: { en: 3, ar: 4 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'ar'
|
||||
},
|
||||
dubai: {
|
||||
language: { en: 3, ar: 4 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'ar'
|
||||
},
|
||||
default: {
|
||||
language: { en: 3, th: 4 },
|
||||
productCode: { hot: 9, cold: 10, blend: 11 },
|
||||
primaryLanguage: 'en'
|
||||
}
|
||||
};
|
||||
|
||||
export function getSheetColumnConfig(countryCode: string) {
|
||||
return SHEET_COLUMN_CONFIG_BY_COUNTRY[countryCode.toLowerCase()]
|
||||
|| SHEET_COLUMN_CONFIG_BY_COUNTRY.default;
|
||||
}
|
||||
|
||||
export function handleCatalogsResponse(content: CatalogsResponse) {
|
||||
if (content && content.catalogs) {
|
||||
sheetCatalogs.set(content.catalogs);
|
||||
}
|
||||
sheetCatalogsLoading.set(false);
|
||||
}
|
||||
|
||||
export interface SheetStreamMeta {
|
||||
batch_id: string;
|
||||
total_chunks: number;
|
||||
total_items: number;
|
||||
current_chunk: number;
|
||||
status: 'idle' | 'streaming' | 'complete' | 'error';
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface SheetMenuItem {
|
||||
new_layout_v2: {
|
||||
row_index: number;
|
||||
cells: { value: string; coord: { row: number; col: number } }[];
|
||||
}[];
|
||||
name_desc_v2: {
|
||||
key: string;
|
||||
row_index: number;
|
||||
cells: { value: string; coord: { row: number; col: number } }[];
|
||||
}[];
|
||||
}
|
||||
|
||||
// Store for streaming metadata
|
||||
export const sheetStreamMeta = writable<SheetStreamMeta | null>(null);
|
||||
|
||||
// Store for accumulated sheet data
|
||||
export const sheetData = writable<SheetMenuItem[]>([]);
|
||||
|
||||
// Store for loading state
|
||||
export const sheetLoading = writable<boolean>(false);
|
||||
|
||||
// Store for error state
|
||||
export const sheetError = writable<string | null>(null);
|
||||
|
||||
// Handler functions for sheet-service streaming
|
||||
export function handleSheetStreamStart(payload: {
|
||||
batch_id: string;
|
||||
total_chunks: number;
|
||||
total_items: number;
|
||||
content: { message: string };
|
||||
}) {
|
||||
sheetStreamMeta.set({
|
||||
batch_id: payload.batch_id,
|
||||
total_chunks: payload.total_chunks,
|
||||
total_items: payload.total_items,
|
||||
current_chunk: 0,
|
||||
status: 'streaming'
|
||||
});
|
||||
sheetData.set([]);
|
||||
sheetLoading.set(true);
|
||||
sheetError.set(null);
|
||||
}
|
||||
|
||||
export function handleSheetStreamChunk(payload: {
|
||||
batch_id: string;
|
||||
current_chunk: number;
|
||||
total_chunks: number;
|
||||
total_items: number;
|
||||
content: SheetMenuItem[];
|
||||
}) {
|
||||
const meta = get(sheetStreamMeta);
|
||||
|
||||
// Verify batch_id matches
|
||||
if (meta && meta.batch_id === payload.batch_id) {
|
||||
// Append new data
|
||||
sheetData.update((current) => [...current, ...payload.content]);
|
||||
|
||||
// Update progress
|
||||
sheetStreamMeta.set({
|
||||
...meta,
|
||||
current_chunk: payload.current_chunk,
|
||||
total_chunks: payload.total_chunks,
|
||||
total_items: payload.total_items
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function handleSheetStreamEnd(payload: {
|
||||
batch_id: string;
|
||||
total_chunks: number;
|
||||
total_items: number;
|
||||
content: { message: string };
|
||||
}) {
|
||||
const meta = get(sheetStreamMeta);
|
||||
|
||||
if (meta && meta.batch_id === payload.batch_id) {
|
||||
sheetStreamMeta.set({
|
||||
...meta,
|
||||
status: 'complete',
|
||||
current_chunk: payload.total_chunks
|
||||
});
|
||||
sheetLoading.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
export function handleSheetStreamError(payload: {
|
||||
batch_id: string;
|
||||
content: { error_detail: string };
|
||||
}) {
|
||||
const meta = get(sheetStreamMeta);
|
||||
|
||||
if (meta && meta.batch_id === payload.batch_id) {
|
||||
sheetStreamMeta.set({
|
||||
...meta,
|
||||
status: 'error',
|
||||
error: payload.content.error_detail
|
||||
});
|
||||
sheetError.set(payload.content.error_detail);
|
||||
sheetLoading.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset all sheet stores
|
||||
export function resetSheetStore() {
|
||||
sheetStreamMeta.set(null);
|
||||
sheetData.set([]);
|
||||
sheetLoading.set(false);
|
||||
sheetError.set(null);
|
||||
}
|
||||
|
||||
// Store for existing product codes (for duplicate checking)
|
||||
export const existingProductCodes = writable<Set<string>>(new Set());
|
||||
export const productCodesLoading = writable<boolean>(false);
|
||||
|
||||
// ─────────────────────────────────────────
|
||||
// Sheet Price Streaming
|
||||
// ─────────────────────────────────────────
|
||||
|
||||
export interface GristCell {
|
||||
coord: {
|
||||
col: number;
|
||||
row: number;
|
||||
};
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface SheetPriceItem {
|
||||
product_code: string;
|
||||
cells: GristCell[];
|
||||
}
|
||||
|
||||
// Price sheet header name mappings by country
|
||||
// Maps our field names to the actual header names in the sheet
|
||||
export const PRICE_HEADER_NAMES_BY_COUNTRY: Record<string, {
|
||||
cash_price: string[]; // Possible header names for cash price
|
||||
non_cash_price: string[]; // Possible header names for non-cash price
|
||||
}> = {
|
||||
tha: {
|
||||
cash_price: ['Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
mys: {
|
||||
cash_price: ['F', 'Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
aus: {
|
||||
cash_price: ['AUD', 'Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
sgp: {
|
||||
cash_price: ['SGD', 'Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
hkg: {
|
||||
cash_price: ['HK Final Px', 'Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
gbr: {
|
||||
cash_price: ['GBR', 'Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
uae_dubai: {
|
||||
cash_price: ['AED', 'Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
dubai: {
|
||||
cash_price: ['AED', 'Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
ltu: {
|
||||
cash_price: ['Price in Euro', 'Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
rou: {
|
||||
cash_price: ['Price From LTU (EUR)', 'Price in RON'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
lva: {
|
||||
cash_price: ['Price in Euro', 'Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
est: {
|
||||
cash_price: ['Price in Euro', 'Price'],
|
||||
non_cash_price: ['MainPrice']
|
||||
},
|
||||
// Default fallback for other countries
|
||||
default: {
|
||||
cash_price: ['Price', 'Price in Euro', 'CashPrice', 'AUD', 'SGD', 'GBR', 'AED', 'F'],
|
||||
non_cash_price: ['MainPrice', 'NonCashPrice']
|
||||
}
|
||||
};
|
||||
|
||||
// Find column index from header array by matching header names
|
||||
export function findHeaderIndex(headerArray: string[], possibleNames: string[]): number {
|
||||
for (const name of possibleNames) {
|
||||
const idx = headerArray.findIndex(h => h.toLowerCase() === name.toLowerCase());
|
||||
if (idx !== -1) {
|
||||
// Return col index (header index + 1 because cells start from col 1)
|
||||
return idx + 1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Store: lastRequestSheetPrice[country][product_code] = cells (first row only, for display)
|
||||
export const lastRequestSheetPrice = writable<Record<string, Record<string, GristCell[]>>>({});
|
||||
|
||||
// Store: sheetPriceHeader[country] = header array
|
||||
export const sheetPriceHeader = writable<Record<string, string[]>>({});
|
||||
|
||||
// Store: sheetPriceAllRows[country][product_code] = array of {row, cells} (ALL rows for duplicates)
|
||||
export const sheetPriceAllRows = writable<Record<string, Record<string, { row: number; cells: GristCell[] }[]>>>({});
|
||||
|
||||
// Helper function to get price value from cells using dynamic header lookup
|
||||
export function getPriceFromCells(
|
||||
country: string,
|
||||
cells: GristCell[],
|
||||
priceType: 'cash_price' | 'non_cash_price' = 'cash_price'
|
||||
): string | null {
|
||||
const headers = get(sheetPriceHeader)[country];
|
||||
if (!headers || headers.length === 0) {
|
||||
console.warn(`[getPriceFromCells] No header found for country: ${country}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get possible header names for this country
|
||||
const headerNames = PRICE_HEADER_NAMES_BY_COUNTRY[country] || PRICE_HEADER_NAMES_BY_COUNTRY.default;
|
||||
const possibleNames = priceType === 'cash_price' ? headerNames.cash_price : headerNames.non_cash_price;
|
||||
|
||||
// Find the column index for this price type
|
||||
const colIdx = findHeaderIndex(headers, possibleNames);
|
||||
//console.log(`[getPriceFromCells] ${country} ${priceType}: colIdx=${colIdx}, headers=`, headers, 'possibleNames=', possibleNames);
|
||||
|
||||
if (colIdx < 0) {
|
||||
console.warn(`[getPriceFromCells] No ${priceType} column found for ${country}, tried:`, possibleNames);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Find the cell with matching column index
|
||||
const priceCell = cells.find((c) => c.coord?.col === colIdx);
|
||||
//console.log(`[getPriceFromCells] Found cell for col ${colIdx}:`, priceCell);
|
||||
return priceCell?.value ?? null;
|
||||
}
|
||||
|
||||
// Store for tracking streaming state
|
||||
export const sheetPriceStreamMeta = writable<{
|
||||
request_id: string;
|
||||
country: string;
|
||||
status: 'idle' | 'streaming' | 'complete' | 'error';
|
||||
error?: string;
|
||||
} | null>(null);
|
||||
|
||||
export const sheetPriceLoading = writable<boolean>(false);
|
||||
|
||||
// Track sent request types (can only send once per type)
|
||||
export const sheetPriceSentTypes = writable<Set<string>>(new Set());
|
||||
|
||||
// Raw streaming data accumulator
|
||||
export const streamingRawData = writable<
|
||||
Record<
|
||||
string,
|
||||
{
|
||||
request_id: string;
|
||||
header?: string[];
|
||||
country?: string;
|
||||
chunks: any[];
|
||||
rawParts: string[]; // For accumulating raw JSON string parts
|
||||
}
|
||||
>
|
||||
>({});
|
||||
|
||||
// Handler: raw_stream header (e.g., raw_stream_price)
|
||||
export function handleRawStreamHeader(subtype: string, payload: any) {
|
||||
console.log(`[RawStream] Header for ${subtype}:`, payload);
|
||||
|
||||
// Get existing stream data to preserve country from request
|
||||
const currentData = get(streamingRawData);
|
||||
const existingData = currentData[subtype];
|
||||
|
||||
streamingRawData.update((data) => ({
|
||||
...data,
|
||||
[subtype]: {
|
||||
request_id: payload.request_id,
|
||||
header: payload.header || payload.headers,
|
||||
country: payload.country || existingData?.country || '',
|
||||
chunks: [],
|
||||
rawParts: [] // Initialize for accumulating raw JSON string parts
|
||||
}
|
||||
}));
|
||||
|
||||
if (subtype === 'price') {
|
||||
sheetPriceStreamMeta.set({
|
||||
request_id: payload.request_id,
|
||||
country: payload.country || existingData?.country || '',
|
||||
status: 'streaming'
|
||||
});
|
||||
sheetPriceLoading.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
// Handler: raw_stream chunk (e.g., raw_stream_chunk_price)
|
||||
export function handleRawStreamChunk(subtype: string, payload: any) {
|
||||
console.log(`[RawStream] Chunk ${payload.idx} for ${subtype}, raw length:`, payload.raw?.length);
|
||||
|
||||
const currentData = get(streamingRawData);
|
||||
const streamData = currentData[subtype];
|
||||
|
||||
if (!streamData || streamData.request_id !== payload.request_id) {
|
||||
console.warn(`[RawStream] Chunk received for unknown stream: ${subtype}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if data is in 'raw' field as JSON string (chunked)
|
||||
if (payload.raw && typeof payload.raw === 'string') {
|
||||
// Accumulate raw parts - will be joined and parsed in handleRawStreamEnd
|
||||
streamingRawData.update((data) => ({
|
||||
...data,
|
||||
[subtype]: {
|
||||
...streamData,
|
||||
country: payload.country || streamData.country,
|
||||
rawParts: [...(streamData.rawParts || []), payload.raw]
|
||||
}
|
||||
}));
|
||||
console.log(`[RawStream] Accumulated chunk ${payload.idx} for ${subtype}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle non-chunked payload (already parsed content)
|
||||
const content = payload.content || payload.data || payload.rows || [];
|
||||
const contentArray = Array.isArray(content) ? content : [content];
|
||||
|
||||
streamingRawData.update((data) => ({
|
||||
...data,
|
||||
[subtype]: {
|
||||
...streamData,
|
||||
country: payload.country || streamData.country,
|
||||
chunks: [...streamData.chunks, ...contentArray]
|
||||
}
|
||||
}));
|
||||
|
||||
console.log(`[RawStream] Chunk for ${subtype}: +${contentArray.length} items`);
|
||||
}
|
||||
|
||||
// Handler: raw_stream end (e.g., raw_stream_end_price)
|
||||
export function handleRawStreamEnd(subtype: string, payload: any) {
|
||||
console.log(`[RawStream] End payload for ${subtype}:`, payload);
|
||||
|
||||
const currentData = get(streamingRawData);
|
||||
const streamData = currentData[subtype];
|
||||
|
||||
if (!streamData || streamData.request_id !== payload.request_id) {
|
||||
console.warn(`[RawStream] End received for unknown stream: ${subtype}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get country from stored stream data or payload
|
||||
const country = streamData.country || payload.country || '';
|
||||
|
||||
// If we have accumulated raw parts, join and parse them now
|
||||
let chunks = streamData.chunks || [];
|
||||
if (streamData.rawParts && streamData.rawParts.length > 0) {
|
||||
const fullRawJson = streamData.rawParts.join('');
|
||||
console.log(
|
||||
`[RawStream] Joining ${streamData.rawParts.length} raw parts, total length: ${fullRawJson.length}`
|
||||
);
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(fullRawJson);
|
||||
console.log(`[RawStream] Parsed combined raw data, keys:`, Object.keys(parsed));
|
||||
|
||||
// Extract content from nested structure: { payload: { content: [...] } }
|
||||
const content = parsed?.payload?.content || parsed?.content || parsed || [];
|
||||
chunks = Array.isArray(content) ? content : [content];
|
||||
|
||||
// Log first item to see actual structure
|
||||
if (chunks.length > 0) {
|
||||
console.log(`[RawStream] First content item:`, JSON.stringify(chunks[0]).substring(0, 200));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`[RawStream] Failed to parse combined raw JSON:`, e);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[RawStream] End for ${subtype}: total ${chunks.length} items, country: ${country}`);
|
||||
|
||||
if (subtype === 'price') {
|
||||
processSheetPriceData(country, streamData.header || [], chunks);
|
||||
sheetPriceStreamMeta.update((meta) => (meta ? { ...meta, status: 'complete' } : null));
|
||||
sheetPriceLoading.set(false);
|
||||
}
|
||||
|
||||
// Clear the streaming data
|
||||
streamingRawData.update((data) => {
|
||||
const newData = { ...data };
|
||||
delete newData[subtype];
|
||||
return newData;
|
||||
});
|
||||
}
|
||||
|
||||
// Process and store sheet price data
|
||||
function processSheetPriceData(country: string, header: string[], chunks: any[]) {
|
||||
console.log(`[SheetPrice] Processing data for ${country}:`, {
|
||||
header,
|
||||
chunksCount: chunks.length
|
||||
});
|
||||
console.log(`[SheetPrice] Sample chunk:`, chunks[0]);
|
||||
|
||||
// Try to extract header from first chunk item if not provided separately
|
||||
// Backend sends header embedded in each item: { header: [...], key: "...", payload: [...] }
|
||||
let effectiveHeader = header;
|
||||
if ((!effectiveHeader || effectiveHeader.length === 0) && chunks.length > 0) {
|
||||
const firstChunk = chunks[0];
|
||||
if (firstChunk?.header && Array.isArray(firstChunk.header)) {
|
||||
effectiveHeader = firstChunk.header;
|
||||
console.log(`[SheetPrice] Extracted header from first chunk:`, effectiveHeader);
|
||||
}
|
||||
}
|
||||
|
||||
// Save header for dynamic column lookup later
|
||||
if (effectiveHeader && effectiveHeader.length > 0) {
|
||||
sheetPriceHeader.update((data) => ({
|
||||
...data,
|
||||
[country]: effectiveHeader
|
||||
}));
|
||||
console.log(`[SheetPrice] Saved header for ${country}:`, effectiveHeader);
|
||||
}
|
||||
|
||||
// Find column indices dynamically from header
|
||||
// product_code header is typically "ProductCode" or similar
|
||||
const productCodeIdx = findHeaderIndex(effectiveHeader, ['ProductCode', 'Product_Code', 'product_code', 'Code']);
|
||||
console.log(`[SheetPrice] productCodeIdx from header:`, productCodeIdx, 'header:', effectiveHeader);
|
||||
|
||||
const priceByProductCode: Record<string, GristCell[]> = {};
|
||||
// Track ALL rows per product code (for duplicates)
|
||||
const allRowsByProductCode: Record<string, { row: number; cells: GristCell[] }[]> = {};
|
||||
|
||||
for (const row of chunks) {
|
||||
if (!row) {
|
||||
console.log(`[SheetPrice] Row is null/undefined`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Support new structure: {key, payload} where key=product_code
|
||||
// payload can be: [{cells: [...], row_index: ...}, ...] - multiple entries for duplicates
|
||||
if (row.key !== undefined) {
|
||||
const productCode = row.key;
|
||||
|
||||
// Handle payload structure - iterate ALL entries in payload for duplicates
|
||||
if (Array.isArray(row.payload) && row.payload.length > 0) {
|
||||
// Check if payload[0] has cells property (nested structure with row_index)
|
||||
if (row.payload[0]?.cells) {
|
||||
// payload: [{cells: [...], row_index: ...}, {cells: [...], row_index: ...}, ...]
|
||||
// Store first one for backward compatibility
|
||||
priceByProductCode[productCode] = row.payload[0].cells;
|
||||
|
||||
// Store ALL rows for duplicate handling
|
||||
if (!allRowsByProductCode[productCode]) {
|
||||
allRowsByProductCode[productCode] = [];
|
||||
}
|
||||
for (const entry of row.payload) {
|
||||
if (entry.cells && entry.row_index !== undefined) {
|
||||
allRowsByProductCode[productCode].push({
|
||||
row: entry.row_index,
|
||||
cells: entry.cells
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (row.payload[0]?.coord) {
|
||||
// payload: [{coord: {...}, value: ...}, ...] - flat cells array
|
||||
priceByProductCode[productCode] = row.payload;
|
||||
// Extract row from first cell's coord
|
||||
const rowNum = row.payload[0]?.coord?.row;
|
||||
if (rowNum !== undefined) {
|
||||
allRowsByProductCode[productCode] = [{ row: rowNum, cells: row.payload }];
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Fallback: Check if row has cells or if row itself is the cells array
|
||||
let cells: GristCell[] = row.cells || row;
|
||||
|
||||
if (!Array.isArray(cells)) {
|
||||
console.log(`[SheetPrice] Unknown row structure:`, row);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find product_code from cells by column index
|
||||
if (productCodeIdx >= 0) {
|
||||
const productCodeCell = cells.find((c: GristCell) => c.coord?.col === productCodeIdx);
|
||||
const productCode = productCodeCell?.value;
|
||||
|
||||
if (productCode) {
|
||||
priceByProductCode[productCode] = cells;
|
||||
// Extract row from first cell's coord
|
||||
const rowNum = cells[0]?.coord?.row;
|
||||
if (rowNum !== undefined) {
|
||||
if (!allRowsByProductCode[productCode]) {
|
||||
allRowsByProductCode[productCode] = [];
|
||||
}
|
||||
allRowsByProductCode[productCode].push({ row: rowNum, cells });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lastRequestSheetPrice.update((data) => ({
|
||||
...data,
|
||||
[country]: {
|
||||
...(data[country] || {}),
|
||||
...priceByProductCode
|
||||
}
|
||||
}));
|
||||
|
||||
// Update sheetPriceAllRows for duplicate handling
|
||||
sheetPriceAllRows.update((data) => ({
|
||||
...data,
|
||||
[country]: {
|
||||
...(data[country] || {}),
|
||||
...allRowsByProductCode
|
||||
}
|
||||
}));
|
||||
|
||||
console.log(
|
||||
`[SheetPrice] Processed ${Object.keys(priceByProductCode).length} prices for ${country}`
|
||||
);
|
||||
console.log(`[SheetPrice] Sample product codes:`, Object.keys(priceByProductCode).slice(0, 5));
|
||||
// Log duplicates info
|
||||
const duplicates = Object.entries(allRowsByProductCode).filter(([_, rows]) => rows.length > 1);
|
||||
if (duplicates.length > 0) {
|
||||
console.log(`[SheetPrice] Found ${duplicates.length} product codes with duplicate rows:`, duplicates.slice(0, 3));
|
||||
}
|
||||
if (chunks.length > 0 && Object.keys(priceByProductCode).length > 0) {
|
||||
const sampleKey = Object.keys(priceByProductCode)[0];
|
||||
console.log(`[SheetPrice] Sample cells for ${sampleKey}:`, priceByProductCode[sampleKey]);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset sheet price stores
|
||||
export function resetSheetPriceStore() {
|
||||
sheetPriceStreamMeta.set(null);
|
||||
sheetPriceLoading.set(false);
|
||||
streamingRawData.set({});
|
||||
}
|
||||
|
||||
// Check if a request type has already been sent
|
||||
export function hasSheetPriceBeenSent(type: string): boolean {
|
||||
return get(sheetPriceSentTypes).has(type);
|
||||
}
|
||||
|
||||
// Mark a request type as sent
|
||||
export function markSheetPriceAsSent(type: string) {
|
||||
sheetPriceSentTypes.update((types) => {
|
||||
const newTypes = new Set(types);
|
||||
newTypes.add(type);
|
||||
return newTypes;
|
||||
});
|
||||
}
|
||||
|
||||
// Clear sent types (for reset)
|
||||
export function clearSheetPriceSentTypes() {
|
||||
sheetPriceSentTypes.set(new Set());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a newly generated product code to the store (local tracking before server sync)
|
||||
*/
|
||||
export function addGeneratedProductCode(code: string) {
|
||||
existingProductCodes.update((codes) => {
|
||||
const newSet = new Set(codes);
|
||||
newSet.add(code);
|
||||
return newSet;
|
||||
});
|
||||
console.log('[sheetStore] Added generated code:', code);
|
||||
}
|
||||
|
||||
const PRODUCT_CODES_STORAGE_KEY = 'supra_product_codes';
|
||||
|
||||
// Track current/pending country for product codes
|
||||
let currentProductCodesCountry = '';
|
||||
let pendingProductCodesCountry = '';
|
||||
|
||||
// Set pending country when making a request
|
||||
export function setPendingProductCodesCountry(country: string) {
|
||||
pendingProductCodesCountry = country;
|
||||
console.log('[sheetStore] Pending product codes country:', country);
|
||||
}
|
||||
|
||||
// Load product codes from localStorage for specific country
|
||||
export function loadProductCodesFromCache(country?: string): boolean {
|
||||
try {
|
||||
const cached = localStorage.getItem(PRODUCT_CODES_STORAGE_KEY);
|
||||
if (cached) {
|
||||
const data = JSON.parse(cached);
|
||||
// Only load if country matches (or no country filter specified)
|
||||
if (data.codes && Array.isArray(data.codes)) {
|
||||
if (country && data.country && data.country !== country) {
|
||||
console.log('[sheetStore] Cache is for different country:', data.country, '!= requested:', country);
|
||||
// Clear the store for different country
|
||||
existingProductCodes.set(new Set());
|
||||
return false;
|
||||
}
|
||||
existingProductCodes.set(new Set(data.codes));
|
||||
currentProductCodesCountry = data.country || '';
|
||||
console.log('[sheetStore] Loaded', data.codes.length, 'product codes from cache for', data.country || 'unknown');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[sheetStore] Failed to load from cache:', e);
|
||||
}
|
||||
// Clear store if no valid cache
|
||||
existingProductCodes.set(new Set());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clear product codes (call when switching countries)
|
||||
export function clearProductCodes() {
|
||||
existingProductCodes.set(new Set());
|
||||
currentProductCodesCountry = '';
|
||||
console.log('[sheetStore] Cleared product codes');
|
||||
}
|
||||
|
||||
export function handleListMenuResponse(payload: { codes: string[]; country?: string }) {
|
||||
// Use pending country if not in payload
|
||||
const country = payload.country || pendingProductCodesCountry;
|
||||
console.log('[sheetStore] Received list_menu_response for', country, ':', payload.codes?.length, 'codes');
|
||||
|
||||
if (payload && payload.codes) {
|
||||
existingProductCodes.set(new Set(payload.codes));
|
||||
currentProductCodesCountry = country;
|
||||
|
||||
// Save to localStorage with country
|
||||
try {
|
||||
localStorage.setItem(
|
||||
PRODUCT_CODES_STORAGE_KEY,
|
||||
JSON.stringify({
|
||||
codes: payload.codes,
|
||||
country: country,
|
||||
timestamp: Date.now()
|
||||
})
|
||||
);
|
||||
console.log('[sheetStore] Saved', payload.codes.length, 'product codes to cache for', country);
|
||||
} catch (e) {
|
||||
console.warn('[sheetStore] Failed to save to cache:', e);
|
||||
}
|
||||
}
|
||||
productCodesLoading.set(false);
|
||||
}
|
||||
|
|
@ -16,6 +16,38 @@ export const socketConnectionOfflineCount = writable<number>(0);
|
|||
export const socketAlreadySendHeartbeat = writable<number>(0);
|
||||
export const socketStore = writable<WebSocket | null>(null);
|
||||
|
||||
export function waitForOpenSocket(timeoutMs = 8000): Promise<WebSocket | null> {
|
||||
const currentSocket = get(socketStore);
|
||||
if (currentSocket?.readyState === WebSocket.OPEN) {
|
||||
return Promise.resolve(currentSocket);
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
let settled = false;
|
||||
let unsubscribe = () => {};
|
||||
const timeout = setTimeout(() => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
unsubscribe();
|
||||
resolve(null);
|
||||
}, timeoutMs);
|
||||
|
||||
function settle(nextSocket: WebSocket | null) {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timeout);
|
||||
unsubscribe();
|
||||
resolve(nextSocket);
|
||||
}
|
||||
|
||||
unsubscribe = socketStore.subscribe((nextSocket) => {
|
||||
if (nextSocket?.readyState === WebSocket.OPEN) {
|
||||
settle(nextSocket);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function connectToWebsocket(id_token?: string) {
|
||||
if (browser) {
|
||||
// console.log('connecting to ', env.PUBLIC_WSS);
|
||||
|
|
@ -41,6 +73,13 @@ export function connectToWebsocket(id_token?: string) {
|
|||
let auth_data = get(authStore);
|
||||
let perms = get(permission);
|
||||
|
||||
// Debug: check if auth_data has uid
|
||||
console.log('[WS Auth] Sending auth with:', {
|
||||
uid: auth_data?.uid,
|
||||
name: auth_data?.displayName,
|
||||
email: auth_data?.email
|
||||
});
|
||||
|
||||
sendMessage({
|
||||
type: 'auth',
|
||||
payload: {
|
||||
|
|
@ -53,6 +92,7 @@ export function connectToWebsocket(id_token?: string) {
|
|||
}
|
||||
});
|
||||
}
|
||||
console.log(socket);
|
||||
|
||||
// heartbeat 10s
|
||||
setInterval(() => {
|
||||
|
|
|
|||
12
src/lib/core/types/catalogData.ts
Normal file
12
src/lib/core/types/catalogData.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
export interface MenuItem {
|
||||
row_index: number;
|
||||
name_th: string;
|
||||
name_en: string;
|
||||
product_codes: string[];
|
||||
}
|
||||
|
||||
export interface CatalogDetail {
|
||||
country: string;
|
||||
catalog: string;
|
||||
items: MenuItem[];
|
||||
}
|
||||
|
|
@ -53,6 +53,15 @@ export type OutMessage =
|
|||
values: any;
|
||||
};
|
||||
}
|
||||
| {
|
||||
type: 'list_menu';
|
||||
payload: {
|
||||
user_info: any;
|
||||
country: string;
|
||||
boxid?: string;
|
||||
};
|
||||
}
|
||||
|
||||
| {
|
||||
type: 'price';
|
||||
payload: {
|
||||
|
|
|
|||
162
src/lib/core/utils/productCode.ts
Normal file
162
src/lib/core/utils/productCode.ts
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
import { get } from 'svelte/store';
|
||||
import { existingProductCodes } from '../stores/sheetStore';
|
||||
|
||||
// Country code mapping (country short from Android -> 2-digit product code prefix)
|
||||
// Country short is read from /sdcard/coffeevending/country/short
|
||||
export const countryCodeMap: Record<string, string> = {
|
||||
// Thailand
|
||||
THAI: '12',
|
||||
tha: '12',
|
||||
// Malaysia
|
||||
MYS: '12',
|
||||
mys: '12',
|
||||
// Indonesia
|
||||
IDR: '13',
|
||||
idr: '13',
|
||||
// Australia
|
||||
AUS: '51',
|
||||
aus: '51',
|
||||
// Singapore
|
||||
SGP: '52',
|
||||
sgp: '52',
|
||||
SG: '52', // obsolete
|
||||
// UAE Dubai
|
||||
UAE_DUBAI: '53',
|
||||
uae_dubai: '53',
|
||||
dubai: '53',
|
||||
// Hong Kong
|
||||
HKG: '54',
|
||||
hkg: '54',
|
||||
// UK
|
||||
GBR: '55',
|
||||
gbr: '55',
|
||||
// Romania
|
||||
ROU: '56',
|
||||
rou: '56',
|
||||
// Latvia
|
||||
LVA: '57',
|
||||
lva: '57',
|
||||
// Estonia
|
||||
EST: '58',
|
||||
est: '58',
|
||||
// Lithuania
|
||||
LTU: '59',
|
||||
ltu: '59',
|
||||
// USA Pepsi (uses Thai prefix)
|
||||
USA_PEPSI: '12',
|
||||
usa_pepsi: '12',
|
||||
// Counter machines
|
||||
counter01: '19'
|
||||
};
|
||||
|
||||
// Temperature codes
|
||||
export const tempCodes: Record<TempType, string> = {
|
||||
hot: '01',
|
||||
cold: '02',
|
||||
blend: '03'
|
||||
};
|
||||
|
||||
// Category/Drink type codes
|
||||
export const categoryOptions = [
|
||||
{ value: '01', label: 'Coffee V1' },
|
||||
{ value: '21', label: 'Coffee V2' },
|
||||
{ value: '02', label: 'Tea' },
|
||||
{ value: '03', label: 'Milk' },
|
||||
{ value: '04', label: 'Whey' },
|
||||
{ value: '05', label: 'Soda & Other' }
|
||||
] as const;
|
||||
|
||||
export type TempType = 'hot' | 'cold' | 'blend';
|
||||
export type CategoryCode = (typeof categoryOptions)[number]['value'];
|
||||
|
||||
/**
|
||||
* Get all existing product code suffixes (last 4 digits)
|
||||
* @param category - Optional category code to filter by (e.g., '01' for Coffee V1)
|
||||
*/
|
||||
export function getExistingCodeSuffixes(category?: string): Set<string> {
|
||||
const codes = get(existingProductCodes);
|
||||
|
||||
if (category) {
|
||||
// Filter codes by category (2nd segment: XX-[category]-XX-XXXX)
|
||||
return new Set(
|
||||
[...codes]
|
||||
.filter((code) => {
|
||||
const parts = code.split('-');
|
||||
return parts[1] === category;
|
||||
})
|
||||
.map((code) => code.split('-')[3])
|
||||
);
|
||||
}
|
||||
|
||||
return new Set([...codes].map((code) => code.split('-')[3]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate next suffix by finding max existing + 1 for a specific category
|
||||
* @param category - Optional category code to find max within (e.g., '01' for Coffee V1)
|
||||
*/
|
||||
export function generateNextSuffix(category?: string): string {
|
||||
const suffixes = getExistingCodeSuffixes(category);
|
||||
let maxSuffix = 0;
|
||||
|
||||
for (const suffix of suffixes) {
|
||||
const num = parseInt(suffix, 10);
|
||||
if (!isNaN(num) && num > maxSuffix) {
|
||||
maxSuffix = num;
|
||||
}
|
||||
}
|
||||
|
||||
const nextSuffix = maxSuffix + 1;
|
||||
|
||||
if (nextSuffix > 9999) {
|
||||
throw new Error('Product code suffix exceeded 9999');
|
||||
}
|
||||
|
||||
return String(nextSuffix).padStart(4, '0');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a complete product code
|
||||
* @param country - Country code (e.g., 'tha')
|
||||
* @param category - Category code (e.g., '01' for Coffee V1)
|
||||
* @param temp - Temperature type ('hot', 'cold', 'blend')
|
||||
* @param suffix - Optional specific suffix, otherwise auto-generate
|
||||
* @returns Product code like '12-01-01-0006'
|
||||
*/
|
||||
export function generateProductCode(
|
||||
country: string,
|
||||
category: string,
|
||||
temp: TempType,
|
||||
suffix?: string
|
||||
): string {
|
||||
const countryCode = countryCodeMap[country] || '99';
|
||||
const tempCode = tempCodes[temp];
|
||||
const codeSuffix = suffix ?? generateNextSuffix();
|
||||
|
||||
return `${countryCode}-${category}-${tempCode}-${codeSuffix}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a product code already exists
|
||||
*/
|
||||
export function isProductCodeExists(code: string): boolean {
|
||||
const codes = get(existingProductCodes);
|
||||
return codes.has(code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique product code (auto-retry if exists)
|
||||
*/
|
||||
export function generateUniqueProductCode(
|
||||
country: string,
|
||||
category: string,
|
||||
temp: TempType
|
||||
): string {
|
||||
const code = generateProductCode(country, category, temp);
|
||||
|
||||
if (isProductCodeExists(code)) {
|
||||
throw new Error(`Product code already exists: ${code}`);
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue