Project: Pet Feeder | page_id: 1705
XSTRIVE Smart Pet Feeder - Data Storage and Device Status Description
Version: v1.1.0
Date: 2024-04-14
🏗️ System Architecture Overview
┌─────────────────────────────────────────────────────────────────┐
│ User's Mobile Phone │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Mobile App │ │
│ │ • User interaction interface │ │
│ │ • Local data cache (SQLite / SharedPreferences) │ │
│ │ • Calls server APIs │ │
│ └─────────────────────────────────────────────────────────┘ │
└──────────────────────────┬────────────────────────────────────┘
│ HTTP/MQTT
▼
┌─────────────────────────────────────────────────────────────────┐
│ Cloud Server │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Server │ │
│ │ • User data management (account, pets, family) │ │
│ │ • Device management (binding, status monitoring) │ │
│ │ • Feeding plan storage and delivery │ │
│ │ • Message push │ │
│ │ • Database (MySQL/PostgreSQL + Redis) │ │
│ └─────────────────────────────────────────────────────────┘ │
└──────────────────────────┬────────────────────────────────────┘
│ MQTT / HTTP
▼
┌─────────────────────────────────────────────────────────────────┐
│ Feeder Device │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Device Firmware │ │
│ │ • Local storage (feeding plan, WiFi configuration) │ │
│ │ • Scheduled feeding (executed even offline) │ │
│ │ • Status reporting │ │
│ │ • Flash storage (~1MB) │ │
│ └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
📊 Three-Layer Data Storage Comparison Table
| Data Type |
Mobile App |
Server |
Device Firmware |
Description |
| Feeding plan |
🟡Cache |
🔴Storage |
🔴Storage |
Core data, synchronized across the three ends |
| WiFi configuration |
- |
- |
🔴Storage |
Device network credentials |
| Device basic configuration |
🟡Cache |
🔴Storage |
🔴Storage |
Name, switches, etc. |
| Pet information |
🟡Cache |
🔴Storage |
🟡Cache |
For App display |
| User account |
🟡Cache |
🔴Storage |
- |
Not related to the device |
| Family members |
🟡Cache |
🔴Storage |
- |
Pure cloud data |
| Message records |
🟡Cache |
🔴Storage |
- |
For App display |
| Album photos/videos |
🟡Cache |
🔴Storage |
- |
CDN storage |
| Feeding records |
🟡Cache |
🔴Storage |
🔴Storage |
Latest N records stored locally |
| Device real-time status |
🟡Cache |
🔴Storage |
🔴Storage |
Online/battery/food level |
| User settings |
🟡Cache |
🔴Storage |
🟡Cache |
App configuration |
🔴 = Must be stored | 🟡 = Cache/optional | - = Not needed
🔴 Device Firmware Layer (Feeder Hardware)
Storage Content
| Data |
Size |
Description |
| Feeding plan |
~2KB |
Up to 20 plans |
| WiFi configuration |
~100B |
SSID + encrypted password |
| Device configuration |
~500B |
Name, default food amount, etc. |
| Feeding records |
~80KB |
Latest 1000 records |
| Device status |
~100B |
Online/offline/battery/food level |
Core Responsibilities
- ✅ Scheduled feeding (executed even when the device is offline!)
- ✅ Local storage of feeding plans
- ✅ Reporting device status to the server
- ✅ Responding to server commands (when the device is online)
- ✅ Automatic reconnection after network loss
Offline Behavior
When the device is offline:
┌─────────────────────────────────────────┐
│ Feeding plan due → Local execution → Food dispensed ✅ │
│ User manually feeds from App → Command cannot be sent → Failed ❌ │
│ Device reconnects to network → Auto-sync plan → Normal ✅ │
└─────────────────────────────────────────┘
🟡 App Layer (Mobile Phone)
Storage Content (Local Cache)
| Data |
Storage Method |
Description |
| User token |
SharedPreferences |
Login credentials |
| Feeding plan list |
SQLite |
Offline display |
| Pet list |
SQLite |
Offline display |
| Device status |
SQLite |
Last synchronized status |
| Message records |
SQLite |
Latest 100 records |
| User settings |
SharedPreferences |
App preferences |
Core Responsibilities
- ✅ User interaction interface
- ✅ Caching server data (faster loading, viewable offline)
- ✅ Calling server APIs
- ✅ Displaying device status (from cache, not real-time)
Offline Behavior
When the App is offline:
┌─────────────────────────────────────────┐
│ Open App → Display local cached data ✅ │
│ Add feeding plan → Save locally + pending sync queue │
│ Device offline flag → Display gray/offline status ✅ │
│ Manual feeding → Prompt "Device offline" ❌ │
└─────────────────────────────────────────┘
🔴 Server Layer (Cloud)
Storage Content
| Data |
Database |
Description |
| User account |
users |
Phone number, password hash, token |
| Device information |
devices |
SN, name, binding status |
| Feeding plans |
feeding_plans |
All plans |
| Pet information |
pets |
Pet profiles |
| Family members |
family_members |
Family relationships |
| Message records |
messages |
Notification messages |
| Album |
media |
Photo/video metadata |
| Feeding records |
feeding_logs |
Historical records |
| User settings |
user_settings |
Notification switches, etc. |
Core Responsibilities
- ✅ Data storage (the only source of truth)
- ✅ Business logic processing
- ✅ Device status management
- ✅ Delivering feeding plans to the device
- ✅ Message push
⚡ Operation Classification (Three-End Perspective)
✅ Operations That Can Be Executed When the Device Is Offline
| Operation |
App (Phone) |
Server |
Device Firmware |
Description |
| View feeding plans |
🟡Local cache |
✅Process |
- |
Displays cached data |
| View pet list |
🟡Local cache |
✅Process |
- |
Displays cached data |
| View device status |
🟡Local cache |
✅Process |
- |
Displays the last status, not real-time |
| View historical messages |
🟡Local cache |
✅Process |
- |
Displays cached messages |
| Add feeding plan |
🟡Local + pending sync |
✅Store |
❌Not received |
Executed only after the device goes online |
| Edit feeding plan |
🟡Local + pending sync |
✅Store |
❌Not updated |
Same as above |
| Delete feeding plan |
🟡Deleted locally |
✅Delete |
❌Not deleted |
Same as above |
| Add pet |
🟡Local cache |
✅Store |
🟡Cached after sync |
Synced after the device goes online |
| Family member management |
✅Cloud only |
✅Process |
- |
Does not involve the device |
| View user settings |
🟡Local cache |
✅Process |
- |
Displays cache |
❌ Operations That Require the Device to Be Online
| Operation |
App (Phone) |
Server |
Device Firmware |
Offline Result |
| Manual feeding |
✅Send command |
✅Forward |
✅Execute |
❌Failed |
| Device binding |
✅Network config page |
✅Process |
✅Respond |
❌Failed |
| Device unbinding |
✅Send request |
✅Process |
✅Confirm |
❌Failed |
| Real-time monitoring video |
✅Display image |
✅Forward |
✅Push stream |
❌Failed |
| Voice intercom |
✅Call interface |
✅Forward |
✅Receive |
❌Failed |
| Firmware upgrade |
✅Download firmware |
✅Provide |
✅Receive and install |
❌Failed |
| View real-time status |
🟡Request → wait |
✅Query |
✅Report |
❌Timeout |
⏳ Data Synchronization Sequence When the Device Is Offline
User adds a feeding plan (device offline):
──────────────────────────────────────────────────────────
[App] [Server] [Device]
│ │ │
│ 1. Click "Add plan" │ │
│─────────────────────────→│ │
│ │ │
│ 2. Save to cloud database │ │
│ ─────────────────────────→ │
│ │ │
│ 3. Return "Added successfully" │ │
│←─────────────────────────│ │
│ │ │
│ 4. Store in local pending sync queue │ │
│ (waiting for the device to go online) │ │
│ │ │
│ │ 5. Device online │
│ │←───────────────────────│
│ │ │
│ │ 6. Deliver feeding plan │
│ │───────────────────────│
│ │ │
│ │ 7. Device local storage │
│ │←───────────────────────│
│ │ │
│ 8. App refreshes → shows the latest plan │ │
│←─────────────────────────│ │
│ │ │
🔄 Data Synchronization Strategy
Device → Server (Reporting)
| Data |
Trigger |
Offline Handling |
| Device status |
Every 30 seconds / on change |
Recorded locally, re-uploaded when online |
| Feeding records |
After feeding is completed |
Cached locally, synchronized when online |
| Device alarms |
When triggered |
Recorded locally, re-uploaded when online |
| Heartbeat |
Every 60 seconds |
- |
Server → Device (Delivery)
| Data |
Trigger |
Offline Handling |
| Feeding plan |
When added/modified |
Stored in queue, delivered when online |
| Device configuration |
When modified |
Stored in queue, delivered when online |
| Firmware push |
Triggered by user |
Downloaded when the device is online |
App ↔ Server
| Data |
App Cache Strategy |
Description |
| Feeding plans |
SQLite storage |
Viewable offline |
| Pet list |
SQLite storage |
Viewable offline |
| Device status |
SQLite storage |
Not real-time |
| Message records |
SQLite storage |
Latest 100 records |
📋 Interface Responsibilities Across the Three Ends
| Interface |
App Responsibility |
Server Responsibility |
Device Responsibility |
| /api/feeding/list |
Display cache |
Store + return |
- |
| /api/feeding/add |
Initiate request |
Store + deliver |
Receive + store |
| /api/feeding/update |
Initiate request |
Update + deliver |
Update locally |
| /api/feeding/delete |
Initiate request |
Delete + notify |
Delete locally |
| /api/feeding/manual |
Initiate request |
Forward command |
Execute |
| /api/device/detail |
Display cache |
Query + return |
- |
| /api/device/bind |
Network configuration guidance |
Verify + bind |
Respond to network configuration |
| /api/album/capture |
Initiate request |
Save file |
Execute capture |
| /api/message/list |
Display cache |
Return messages |
- |
📝 Development Notes
App Development
- Offline status awareness - When the device is offline, the App should display an "Offline" label
- Local cache update - After a successful operation, optimistically update the local cache
- Sync queue - Offline operations are stored in a queue and automatically synced when online
- Status refresh - After the device goes online, the App should actively refresh the data
Server Development
- Message push - When the device status changes, push through MQTT
- Plan delivery - When the feeding plan changes, immediately deliver it to the device
- Offline compensation - When the device reconnects, send the changes made during the offline period
- Conflict handling - If the device local time conflicts with the server time, the device time prevails
Device Firmware Development
- Local storage - Use Flash to store feeding plans, supporting power-off retention
- Scheduled execution - Controlled by the RTC clock, feeding on time even when the network is disconnected
- Status reporting - Periodically report the status to the server when online
- Automatic reconnection - Automatically reconnect after network loss and receive the data delivered during the offline period
Version History:
- v1.1.0 - 2024-04-14: Added the three-layer architecture comparison table, clearly distinguishing App/Server/Device firmware
- v1.0.0 - 2024-04-13: Initial version