Normalize inventory and alerts data structure in updateStateFromBridge function
This commit is contained in:
@@ -285,11 +285,31 @@ function updateStateFromBridge(data) {
|
|||||||
if (data.cache) {
|
if (data.cache) {
|
||||||
// Normalize itemList: Lua sends { name, total }, frontend expects { name, count }
|
// Normalize itemList: Lua sends { name, total }, frontend expects { name, count }
|
||||||
const rawItems = data.cache.itemList || [];
|
const rawItems = data.cache.itemList || [];
|
||||||
const normalizedItems = rawItems.map(item => ({
|
const normalizedItems = Array.isArray(rawItems) ? rawItems.map(item => ({
|
||||||
name: item.name,
|
name: item.name || '',
|
||||||
count: item.total !== undefined ? item.total : (item.count || 0),
|
count: item.total !== undefined ? item.total : (item.count || 0),
|
||||||
displayName: item.displayName || item.name,
|
displayName: item.displayName || item.name || '',
|
||||||
}));
|
})) : [];
|
||||||
|
|
||||||
|
// Normalize furnaceStatus: Lua sends array of { name, type, input, fuel, output, active }
|
||||||
|
// Frontend expects object { furnaceName: { active, input, fuel, output } }
|
||||||
|
const rawFurnaces = data.cache.furnaceStatus || [];
|
||||||
|
let normalizedFurnaces = {};
|
||||||
|
if (Array.isArray(rawFurnaces)) {
|
||||||
|
rawFurnaces.forEach(f => {
|
||||||
|
if (f && f.name) {
|
||||||
|
normalizedFurnaces[f.name] = {
|
||||||
|
active: f.active || false,
|
||||||
|
type: f.type || 'minecraft:furnace',
|
||||||
|
input: f.input || null,
|
||||||
|
fuel: f.fuel || null,
|
||||||
|
output: f.output || null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if (typeof rawFurnaces === 'object') {
|
||||||
|
normalizedFurnaces = rawFurnaces;
|
||||||
|
}
|
||||||
|
|
||||||
inventoryState = {
|
inventoryState = {
|
||||||
itemList: normalizedItems,
|
itemList: normalizedItems,
|
||||||
@@ -302,16 +322,49 @@ function updateStateFromBridge(data) {
|
|||||||
dropperOk: data.cache.dropperOk || false,
|
dropperOk: data.cache.dropperOk || false,
|
||||||
barrelOk: data.cache.barrelOk || false,
|
barrelOk: data.cache.barrelOk || false,
|
||||||
furnaceCount: data.cache.furnaceCount || 0,
|
furnaceCount: data.cache.furnaceCount || 0,
|
||||||
furnaceStatus: data.cache.furnaceStatus || {},
|
furnaceStatus: normalizedFurnaces,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.activity !== undefined) activityState = data.activity;
|
if (data.activity !== undefined) activityState = data.activity || {};
|
||||||
if (data.alerts !== undefined) alertsState = data.alerts;
|
|
||||||
|
// Normalize alerts: Lua sends [{ label, current, min }] (only triggered ones)
|
||||||
|
// Frontend expects [{ item, triggered, current, threshold }]
|
||||||
|
if (data.alerts !== undefined) {
|
||||||
|
const rawAlerts = data.alerts || [];
|
||||||
|
alertsState = Array.isArray(rawAlerts) ? rawAlerts.map(a => ({
|
||||||
|
item: a.name || a.item || a.label || '',
|
||||||
|
triggered: true,
|
||||||
|
current: a.current || 0,
|
||||||
|
threshold: a.min || a.threshold || 0,
|
||||||
|
})) : [];
|
||||||
|
}
|
||||||
|
|
||||||
if (data.smeltingPaused !== undefined) smeltingPaused = data.smeltingPaused;
|
if (data.smeltingPaused !== undefined) smeltingPaused = data.smeltingPaused;
|
||||||
if (data.disabledRecipes !== undefined) disabledRecipes = data.disabledRecipes;
|
if (data.disabledRecipes !== undefined) disabledRecipes = data.disabledRecipes || {};
|
||||||
if (data.smeltable !== undefined) smeltableRecipes = data.smeltable;
|
if (data.smeltable !== undefined) smeltableRecipes = data.smeltable || {};
|
||||||
if (data.craftable !== undefined) craftableRecipes = data.craftable;
|
|
||||||
|
// Normalize craftable: Lua sends { output, count, grid } (grid = flat array of 9)
|
||||||
|
// Frontend expects { output, count, slots } (slots = object { slotIdx: itemName })
|
||||||
|
if (data.craftable !== undefined) {
|
||||||
|
const rawCraftable = data.craftable || [];
|
||||||
|
craftableRecipes = Array.isArray(rawCraftable) ? rawCraftable.map(recipe => {
|
||||||
|
const slots = {};
|
||||||
|
if (recipe.grid && Array.isArray(recipe.grid)) {
|
||||||
|
recipe.grid.forEach((item, idx) => {
|
||||||
|
if (item) slots[idx + 1] = item;
|
||||||
|
});
|
||||||
|
} else if (recipe.slots) {
|
||||||
|
Object.assign(slots, recipe.slots);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
output: recipe.output || '',
|
||||||
|
count: recipe.count || 1,
|
||||||
|
slots,
|
||||||
|
};
|
||||||
|
}) : [];
|
||||||
|
}
|
||||||
|
|
||||||
if (data.craftTurtleOk !== undefined) craftTurtleOk = data.craftTurtleOk;
|
if (data.craftTurtleOk !== undefined) craftTurtleOk = data.craftTurtleOk;
|
||||||
|
|
||||||
lastUpdate = Date.now();
|
lastUpdate = Date.now();
|
||||||
|
|||||||
Reference in New Issue
Block a user