import { DeviceClient } from '@lua-ai-global/device-client';
// Simulated hardware interfaces
const scanner = { scan: async () => ({ value: 'SKU-2024-0847', format: 'CODE128' }) };
const scale = { read: async () => ({ weight: 12.4, unit: 'kg' }) };
const inventory: Record<string, { quantity: number; threshold: number; name: string }> = {
'SKU-2024-0847': { quantity: 8, threshold: 10, name: 'Widget A' },
'SKU-2024-0312': { quantity: 142, threshold: 20, name: 'Bracket B' },
'SKU-2024-1100': { quantity: 3, threshold: 15, name: 'Sensor C' },
};
const gate = { open: async () => ({ opened: true }) };
const device = new DeviceClient({
agentId: process.env.LUA_AGENT_ID!,
apiKey: process.env.LUA_API_KEY!,
deviceName: 'warehouse-station-01',
group: 'warehouse-floor',
commands: [
{
name: 'scan_barcode',
description: 'Activate the barcode scanner and return the scanned code. Takes 1-2 seconds. Returns barcode value and format.',
timeoutMs: 10000,
},
{
name: 'read_weight',
description: 'Read the current weight on the shipping scale in kilograms',
},
{
name: 'check_stock_level',
description: 'Check current stock level for a given SKU',
inputSchema: {
type: 'object',
properties: {
sku: { type: 'string', description: 'Product SKU to check' },
},
required: ['sku'],
},
},
{
name: 'open_gate',
description: 'Open the dock gate for incoming/outgoing shipments. Confirm with the user before opening.',
inputSchema: {
type: 'object',
properties: {
gate: { type: 'string', enum: ['dock-a', 'dock-b', 'dock-c'] },
},
required: ['gate'],
},
timeoutMs: 15000,
},
],
});
device.onCommand('scan_barcode', async () => {
const result = await scanner.scan();
return { barcode: result.value, format: result.format, timestamp: new Date().toISOString() };
});
device.onCommand('read_weight', async () => {
const result = await scale.read();
return { weight: result.weight, unit: result.unit };
});
device.onCommand('check_stock_level', async (payload) => {
const item = inventory[payload.sku];
if (!item) {
return { error: `Unknown SKU: ${payload.sku}`, found: false };
}
return {
sku: payload.sku,
name: item.name,
quantity: item.quantity,
threshold: item.threshold,
status: item.quantity <= item.threshold ? 'low' : 'ok',
found: true,
};
});
device.onCommand('open_gate', async (payload) => {
const result = await gate.open();
return { gate: payload.gate, opened: result.opened, timestamp: new Date().toISOString() };
});
async function main() {
await device.connect();
console.log('Warehouse station online');
// Monitor stock levels every 60 seconds
setInterval(async () => {
for (const [sku, item] of Object.entries(inventory)) {
if (item.quantity <= item.threshold) {
await device.trigger('low_stock_alert', {
sku,
name: item.name,
currentQuantity: item.quantity,
threshold: item.threshold,
});
}
}
}, 60000);
}
main().catch(console.error);