From 4cb98f86721ec12191d16558b6ff3421ea31ef59 Mon Sep 17 00:00:00 2001 From: "pakintada@gmail.com" Date: Wed, 29 Apr 2026 16:05:10 +0700 Subject: [PATCH] feat: edit value & send to brew - edited value is now sent to brew Signed-off-by: pakintada@gmail.com --- .../recipelist-value-editor.svelte | 3 +- .../recipe-details/recipelist-value.svelte | 29 ++++++ .../components/recipe-editor-dialog.svelte | 93 ++++++++++++++++++- src/lib/core/adb/adb.ts | 11 +++ src/lib/core/handlers/adbPayloadHandler.ts | 2 + src/lib/core/handlers/ws_messageSender.ts | 5 + src/lib/core/stores/adbWriter.ts | 2 +- 7 files changed, 141 insertions(+), 4 deletions(-) diff --git a/src/lib/components/recipe-details/recipelist-value-editor.svelte b/src/lib/components/recipe-details/recipelist-value-editor.svelte index 14f728d..5a95d61 100644 --- a/src/lib/components/recipe-details/recipelist-value-editor.svelte +++ b/src/lib/components/recipe-details/recipelist-value-editor.svelte @@ -326,7 +326,7 @@ Value Editor - Make changes to current order of material. Click save when you're done. + Make changes to current order of material. Click Apply when you're done editing. @@ -606,6 +606,7 @@ + diff --git a/src/lib/components/recipe-details/recipelist-value.svelte b/src/lib/components/recipe-details/recipelist-value.svelte index 0c7a0c6..c03d0b1 100644 --- a/src/lib/components/recipe-details/recipelist-value.svelte +++ b/src/lib/components/recipe-details/recipelist-value.svelte @@ -267,6 +267,35 @@ console.log('topping applying', topping_change); currentToppings[getToppingSlot()] = topping_change; } else { + console.log('apply change on recipelist value', key); + + if (key.startsWith('powder')) { + if (key.endsWith('gram')) { + powder.gram = parseInt(value[key]); + } else if (key.endsWith('time')) { + powder.time = parseInt(value[key]); + } + } else if (key.startsWith('syrup')) { + if (key.endsWith('gram')) { + syrup.gram = parseInt(value[key]); + } else if (key.endsWith('time')) { + syrup.time = parseInt(value[key]); + } + } else if (key.startsWith('water')) { + if (key.endsWith('yield')) { + water.yield = parseInt(value[key]); + } else if (key.endsWith('cold')) { + water.cold = parseInt(value[key]); + } + } else if (key.startsWith('feed')) { + if (key.endsWith('pattern')) { + feed.pattern = parseInt(value[key]); + } else if (key.endsWith('parameter')) { + feed.parameter = parseInt(value[key]); + } + } else if (key == 'stir_time') { + stir_time = parseInt(value[key]); + } } } } diff --git a/src/lib/components/recipe-editor-dialog.svelte b/src/lib/components/recipe-editor-dialog.svelte index f5b3986..9e73037 100644 --- a/src/lib/components/recipe-editor-dialog.svelte +++ b/src/lib/components/recipe-editor-dialog.svelte @@ -88,6 +88,34 @@ // get(currentEditingRecipeProductCode) // ); + console.log('accepting changes'); + let current_data_to_brew = + ready_to_send_brew.length == 0 ? $state.snapshot(currentData) : ready_to_send_brew.shift(); + + if ( + new_topping_value_for_save != undefined && + new_topping_value_for_save != null && + new_topping_value_for_save.length > 0 + ) { + current_data_to_brew['ToppingSet'] = new_topping_value_for_save; + } else if ( + topping_value_for_revert != undefined && + topping_value_for_revert != null && + topping_value_for_revert.length > 0 + ) { + current_data_to_brew['ToppingSet'] = topping_value_for_revert; + } + + // if (current_data_to_brew['ToppingSet'].length == 0) { + // // rollback if empty + // current_data_to_brew['ToppingSet'] = topping_value_for_revert; + // } + + // apply changes now + let new_change = applyChangeToRecipeForBrewing(latest_event, current_data_to_brew); + + ready_to_send_brew.push(new_change); + callback_revert_value_if_not_save = (save: any) => { if (!save) { latestRecipeToppingData.set(topping_value_for_revert); @@ -136,16 +164,77 @@ // } + function getRecipeListKeyName(name: string) { + switch (name) { + case 'powder_gram': + return 'powderGram'; + case 'powder_time': + return 'powderTime'; + case 'syrup_gram': + return 'syrupGram'; + case 'syrup_time': + return 'syrupTime'; + case 'water_cold': + return 'waterCold'; + case 'water_yield': + return 'waterYield'; + case 'feed_parameter': + return 'FeedParameter'; + case 'feed_pattern': + return 'FeedPattern'; + case 'stir_time': + return 'stirTime'; + default: + return name; + } + } + + function applyChangeToRecipeForBrewing(latest_event: any, current_data_to_brew: any) { + console.log('applyChangeToRecipeForBrewing', Object.keys(latest_event?.payload)); + + let changes = latest_event?.payload.change; + + let apply_to_index = latest_event?.index; + let apply_to_keys = Object.keys(changes); + + console.log('applying', apply_to_index, apply_to_keys, current_data_to_brew['recipes'].length); + + console.log('topping before apply', current_data_to_brew['ToppingSet']); + for (const key of apply_to_keys) { + if (key == 'toppings') { + current_data_to_brew['ToppingSet'][apply_to_index - 1] = changes[key][apply_to_index - 1]; + console.log('applying topping', apply_to_index - 1); + } else { + // get actual key + + current_data_to_brew['recipes'][apply_to_index][getRecipeListKeyName(key)] = parseInt( + changes[key] + ); + console.log('applying ', key, current_data_to_brew['recipes'][apply_to_index]); + } + } + + console.log('topping after apply', current_data_to_brew['ToppingSet']); + + return current_data_to_brew; + } + async function sendBrewNow() { try { + while (ready_to_send_brew.length > 1) { + ready_to_send_brew.shift(); + } + + console.log('sending brewing payload', ready_to_send_brew); + await sendToAndroid({ type: 'brew', payload: { start: new Date().toLocaleTimeString(), // use this field for unchanged data - target: ready_to_send_brew.length == 1 ? '-' : currentData.productCode, + target: ready_to_send_brew.length >= 1 ? '-' : currentData.productCode, // use this field for new or modified data - data: ready_to_send_brew.length == 1 ? ready_to_send_brew[0][1] : null + data: ready_to_send_brew.length >= 1 ? ready_to_send_brew[0] : {} } }); } catch (e) { diff --git a/src/lib/core/adb/adb.ts b/src/lib/core/adb/adb.ts index 41c5cb3..5fd830d 100644 --- a/src/lib/core/adb/adb.ts +++ b/src/lib/core/adb/adb.ts @@ -223,11 +223,20 @@ async function connectToAndroidServer() { console.warn('adb instance not found'); return; } + + await push('/sdcard/coffeevending/enable_adb_block_watch', '1'); + const stream = await inst.transport.connect(env.PUBLIC_BREW_CONN_PORT); const writer = stream.writable.getWriter(); const reader = stream.readable.getReader(); + console.log('checking on writer ', writer); adbWriter.set(writer); + if (writer) { + addNotification('INFO:Enable Brewing Mode T on machine'); + } else { + addNotification('WARN:Brewing Mode T unavailable'); + } (async () => { try { @@ -240,10 +249,12 @@ async function connectToAndroidServer() { console.error('read error', e); } finally { adbWriter.set(null); + addNotification('WARN:Brewing Mode T Offline ...'); } })(); } catch (err) { console.error('Connection failed. Suspect java running or not', err); + addNotification('ERR:Fail to enable brewing mode T'); } } diff --git a/src/lib/core/handlers/adbPayloadHandler.ts b/src/lib/core/handlers/adbPayloadHandler.ts index ea4301f..55e0a7b 100644 --- a/src/lib/core/handlers/adbPayloadHandler.ts +++ b/src/lib/core/handlers/adbPayloadHandler.ts @@ -34,6 +34,8 @@ async function handleAdbPayload(raw_payload: string) { let next = states[1].replace('MACHINE_STATE_', ''); console.log('current state', curr, 'next state', next); + + addNotification('INFO:Machine Status Updated, ' + next); } break; case 'error': diff --git a/src/lib/core/handlers/ws_messageSender.ts b/src/lib/core/handlers/ws_messageSender.ts index 72e0dc6..2f60dca 100644 --- a/src/lib/core/handlers/ws_messageSender.ts +++ b/src/lib/core/handlers/ws_messageSender.ts @@ -51,6 +51,11 @@ export function sendMessage(msg: OutMessage): boolean { console.warn('WebSocket not connected, put to queue'); let currentQueue = get(queue); + if (currentQueue.length >= 10) { + while (currentQueue.length >= 10) { + currentQueue.shift(); + } + } currentQueue.push(data); queue.set(currentQueue); diff --git a/src/lib/core/stores/adbWriter.ts b/src/lib/core/stores/adbWriter.ts index dd19153..4dffe45 100644 --- a/src/lib/core/stores/adbWriter.ts +++ b/src/lib/core/stores/adbWriter.ts @@ -14,7 +14,7 @@ async function sendToAndroid(message: any) { const encoder = new TextEncoder(); console.log(writer); await writer.write(encoder.encode(JSON.stringify(message) + '\n')); - console.log('sent!'); + console.log('sent! ', JSON.stringify(message)); } catch (error) { console.error('write failed', error); }