diff --git a/services/api.ts b/services/api.ts index 1afc6ab..c631c36 100644 --- a/services/api.ts +++ b/services/api.ts @@ -30,20 +30,32 @@ const ELDERLY_AVATARS = [ // Room locations for sensor placement // Used in device settings to select where sensor is installed export const ROOM_LOCATIONS = [ - { id: 'bedroom', label: 'Bedroom', icon: '🛏️' }, - { id: 'living_room', label: 'Living Room', icon: '🛋️' }, - { id: 'kitchen', label: 'Kitchen', icon: '🍳' }, - { id: 'bathroom', label: 'Bathroom', icon: '🚿' }, - { id: 'hallway', label: 'Hallway', icon: '🚪' }, - { id: 'entrance', label: 'Entrance', icon: '🏠' }, - { id: 'garage', label: 'Garage', icon: '🚗' }, - { id: 'basement', label: 'Basement', icon: '🪜' }, - { id: 'office', label: 'Office', icon: '💼' }, - { id: 'other', label: 'Other', icon: '📍' }, + { id: 'bedroom', label: 'Bedroom', icon: '🛏️', legacyCode: 102 }, + { id: 'living_room', label: 'Living Room', icon: '🛋️', legacyCode: 103 }, + { id: 'kitchen', label: 'Kitchen', icon: '🍳', legacyCode: 104 }, + { id: 'bathroom', label: 'Bathroom', icon: '🚿', legacyCode: 105 }, + { id: 'hallway', label: 'Hallway', icon: '🚪', legacyCode: 106 }, + { id: 'entrance', label: 'Entrance', icon: '🏠', legacyCode: 111 }, + { id: 'garage', label: 'Garage', icon: '🚗', legacyCode: 108 }, + { id: 'basement', label: 'Basement', icon: '🪜', legacyCode: 110 }, + { id: 'office', label: 'Office', icon: '💼', legacyCode: 107 }, + { id: 'other', label: 'Other', icon: '📍', legacyCode: 200 }, ] as const; export type RoomLocationId = typeof ROOM_LOCATIONS[number]['id']; +// Helper to convert location ID to Legacy API code +function getLocationLegacyCode(locationId: string): number | undefined { + const location = ROOM_LOCATIONS.find(loc => loc.id === locationId); + return location?.legacyCode; +} + +// Helper to convert Legacy API code to location ID +export function getLocationIdFromCode(code: number): RoomLocationId | undefined { + const location = ROOM_LOCATIONS.find(loc => loc.legacyCode === code); + return location?.id; +} + // Get consistent avatar based on deployment_id function getAvatarForBeneficiary(deploymentId: number): string { const index = deploymentId % ELDERLY_AVATARS.length; @@ -1646,6 +1658,19 @@ class ApiService { status = 'offline'; // 🔴 Definitely problem } + // Convert numeric location code to string ID if needed + let locationId = ''; + if (location) { + const numericLocation = parseInt(location, 10); + if (!isNaN(numericLocation)) { + // It's a numeric code from Legacy API - convert to our ID + locationId = getLocationIdFromCode(numericLocation) || ''; + } else { + // It's already a string (legacy data or custom location) + locationId = location; + } + } + return { deviceId: deviceId.toString(), wellId: parseInt(wellId, 10), @@ -1653,7 +1678,7 @@ class ApiService { name: `WP_${wellId}_${mac.slice(-6).toLowerCase()}`, status: status, lastSeen: lastSeen, - location: location || '', + location: locationId, description: description || '', beneficiaryId: beneficiaryId, deploymentId: deploymentId, @@ -1779,7 +1804,7 @@ class ApiService { async updateDeviceMetadata( deviceId: string, updates: { - location?: string; + location?: string; // Location ID (e.g., 'bedroom', 'kitchen') - will be converted to Legacy API code description?: string; } ): Promise> { @@ -1797,8 +1822,15 @@ class ApiService { }); // Add optional fields if provided + // Location must be converted from ID to Legacy API numeric code if (updates.location !== undefined) { - formData.append('location', updates.location); + const legacyCode = getLocationLegacyCode(updates.location); + if (legacyCode !== undefined) { + formData.append('location', legacyCode.toString()); + } else { + // If location ID not found, log warning but don't fail + console.warn(`Unknown location ID: ${updates.location}, skipping location update`); + } } if (updates.description !== undefined) { formData.append('description', updates.description);