# viettrung1404/dut.smarthomesystem architecture

> Architecture diagram of the GitHub repository viettrung1404/dut.smarthomesystem, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/viettrung1404/dut.smarthomesystem
- Repository: https://github.com/viettrung1404/dut.smarthomesystem

## Overview

This is a smart-home application: users manage homes, rooms and devices, issue commands, configure automations, and receive AI chat and suggestions through the mobile app and backend. Device commands reach hardware over MQTT; scheduled or state-triggered automations can also issue commands. Face verification, security, energy, and suggestion features are separate product branches. The excerpts confirm selected API and execution flows; wiring for unsampled modules is shown only where supported by their documented roles or direct imports.

Main entry points: [`mobile/services/api.ts`](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/mobile/services/api.ts) (API client), [`backend/src/database/core.py`](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/database/core.py) (PostgreSQL), [`backend/src/apis/devices/controller.py`](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/devices/controller.py) (Devices API), [`backend/src/mqtt_client.py`](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/mqtt_client.py) (MQTT client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Mobile client"]
  node_mobile["Mobile app<br/>[index.tsx]"]
  node_mobile_api["API client<br/>[api.ts]"]
end

subgraph group_api["Backend product APIs"]
  node_homes["Homes API<br/>[controller.py]"]
  node_rooms["Rooms API<br/>[controller.py]"]
  node_devices["Devices API<br/>[controller.py]"]
  node_auth["Authentication API<br/>[controller.py]"]
  node_automations["Automations API<br/>[controller.py]"]
  node_security["Security API<br/>[controller.py]"]
  node_energy["Energy API"]
  node_face["Face API<br/>[controller.py]"]
end

subgraph group_intelligence["AI and insights"]
  node_backend_chat["Smart-home chat<br/>[controller.py]"]
  node_suggestions["Suggestions API<br/>[controller.py]"]
  node_ai_service["Chat forwarding<br/>[service.py]"]
  node_ai_chat["AI chat endpoint<br/>[chat_routes.py]"]
  node_agent["Smart-home agent<br/>[agent.py]"]
  node_memory["Conversation memory<br/>[memory_store.py]"]
  node_llm["Ollama client<br/>[ollama_client.py]"]
end

subgraph group_runtime["Device runtime"]
  node_automation_engine["Automation engine<br/>[engine.py]"]
  node_mqtt_client["MQTT client<br/>[mqtt_client.py]"]
  node_face_service["Face recognition<br/>[service.py]"]
end

subgraph group_data["Persistence"]
  node_database[("PostgreSQL<br/>[core.py]")]
end

node_resident(("Resident"))
node_ollama{{"Ollama service"}}
node_broker{{"MQTT broker"}}
node_iot_device["IoT device<br/>[iot_client.py]"]

node_resident -->|"uses"| node_mobile
node_mobile -->|"requests data"| node_mobile_api
node_mobile_api -->|"calls"| node_auth
node_mobile_api -->|"calls"| node_homes
node_mobile_api -->|"calls"| node_rooms
node_mobile_api -->|"calls"| node_devices
node_mobile_api -->|"calls"| node_automations
node_mobile_api -->|"calls"| node_security
node_mobile_api -->|"calls"| node_energy
node_mobile_api -->|"calls"| node_face
node_mobile_api -->|"calls"| node_backend_chat
node_mobile_api -->|"calls"| node_suggestions
node_backend_chat -->|"forwards chat"| node_ai_service
node_ai_service -->|"sends request"| node_ai_chat
node_ai_chat -->|"dispatches"| node_agent
node_agent -->|"reads and writes"| node_memory
node_agent -->|"requests completion"| node_llm
node_llm -->|"calls"| node_ollama
node_backend_chat -->|"sends commands"| node_devices
node_devices -->|"publishes command"| node_mqtt_client
node_face -->|"verifies face"| node_face_service
node_face -->|"publishes unlock"| node_mqtt_client
node_automation_engine -->|"reads and updates"| node_database
node_automation_engine -->|"publishes action"| node_mqtt_client
node_mqtt_client -->|"publishes"| node_broker
node_iot_device -->|"exchanges messages"| node_broker
node_homes -->|"reads and writes"| node_database
node_rooms -->|"reads and writes"| node_database
node_devices -->|"reads and writes"| node_database
node_auth -->|"reads and writes"| node_database
node_automations -->|"reads and writes"| node_database
node_security -->|"reads and writes"| node_database
node_energy -->|"reads and writes"| node_database
node_suggestions -->|"reads and writes"| node_database

click node_mobile "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/mobile/app/(tabs)/index.tsx"
click node_mobile_api "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/mobile/services/api.ts"
click node_homes "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/homes/controller.py"
click node_rooms "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/rooms/controller.py"
click node_devices "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/devices/controller.py"
click node_auth "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/auth/controller.py"
click node_automations "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/automations/controller.py"
click node_security "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/security/controller.py"
click node_energy "https://github.com/viettrung1404/dut.smarthomesystem/tree/dev/backend/src/apis/energy"
click node_face "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/face/controller.py"
click node_backend_chat "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/ai_chat/controller.py"
click node_suggestions "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/suggestions/controller.py"
click node_ai_service "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/ai_chat/service.py"
click node_ai_chat "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/ai-server/app/api/chat_routes.py"
click node_agent "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/ai-server/app/agent/agent.py"
click node_memory "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/ai-server/app/memory/memory_store.py"
click node_llm "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/ai-server/app/llm/ollama_client.py"
click node_automation_engine "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/automation/engine.py"
click node_mqtt_client "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/mqtt_client.py"
click node_iot_device "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/Iot/iot_client.py"
click node_face_service "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/face/service.py"
click node_database "https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/database/core.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_mobile,node_mobile_api toneBlue
class node_homes,node_rooms,node_devices,node_auth,node_automations,node_security,node_energy,node_face toneAmber
class node_backend_chat,node_suggestions,node_ai_service,node_ai_chat,node_agent,node_memory,node_llm toneMint
class node_automation_engine,node_mqtt_client,node_face_service toneRose
class node_database,node_resident,node_ollama,node_broker toneIndigo
class node_iot_device toneTeal
```

## Components

### Mobile client

- [Mobile app](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/mobile/app/(tabs)/index.tsx): `mobile/app/(tabs)/index.tsx`
- [API client](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/mobile/services/api.ts): `mobile/services/api.ts`

### Backend product APIs

- [Homes API](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/homes/controller.py): `backend/src/apis/homes/controller.py`
- [Rooms API](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/rooms/controller.py): `backend/src/apis/rooms/controller.py`
- [Devices API](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/devices/controller.py): `backend/src/apis/devices/controller.py`
- [Authentication API](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/auth/controller.py): `backend/src/apis/auth/controller.py`
- [Automations API](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/automations/controller.py): `backend/src/apis/automations/controller.py`
- [Security API](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/security/controller.py): `backend/src/apis/security/controller.py`
- [Energy API](https://github.com/viettrung1404/dut.smarthomesystem/tree/dev/backend/src/apis/energy): `backend/src/apis/energy`
- [Face API](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/face/controller.py): `backend/src/apis/face/controller.py`

### AI and insights

- [Smart-home chat](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/ai_chat/controller.py): `backend/src/apis/ai_chat/controller.py`
- [Suggestions API](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/suggestions/controller.py): `backend/src/apis/suggestions/controller.py`
- [Chat forwarding](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/ai_chat/service.py): `backend/src/apis/ai_chat/service.py`
- [AI chat endpoint](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/ai-server/app/api/chat_routes.py): `ai-server/app/api/chat_routes.py`
- [Smart-home agent](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/ai-server/app/agent/agent.py): `ai-server/app/agent/agent.py`
- [Conversation memory](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/ai-server/app/memory/memory_store.py): `ai-server/app/memory/memory_store.py`
- [Ollama client](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/ai-server/app/llm/ollama_client.py): `ai-server/app/llm/ollama_client.py`

### Device runtime

- [Automation engine](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/automation/engine.py): `backend/src/automation/engine.py`
- [MQTT client](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/mqtt_client.py): `backend/src/mqtt_client.py`
- [Face recognition](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/apis/face/service.py): `backend/src/apis/face/service.py`

### Persistence

- [PostgreSQL](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/backend/src/database/core.py): `backend/src/database/core.py`

### Other components

- **Resident**
- **Ollama service**
- **MQTT broker**
- [IoT device](https://github.com/viettrung1404/dut.smarthomesystem/blob/dev/Iot/iot_client.py): `Iot/iot_client.py`

## Connections

- Resident → Mobile app: uses
- Mobile app → API client: requests data
- API client → Authentication API: calls
- API client → Homes API: calls
- API client → Rooms API: calls
- API client → Devices API: calls
- API client → Automations API: calls
- API client → Security API: calls
- API client → Energy API: calls
- API client → Face API: calls
- API client → Smart-home chat: calls
- API client → Suggestions API: calls
- Smart-home chat → Chat forwarding: forwards chat
- Chat forwarding → AI chat endpoint: sends request
- AI chat endpoint → Smart-home agent: dispatches
- Smart-home agent → Conversation memory: reads and writes
- Smart-home agent → Ollama client: requests completion
- Ollama client → Ollama service: calls
- Smart-home chat → Devices API: sends commands
- Devices API → MQTT client: publishes command
- Face API → Face recognition: verifies face
- Face API → MQTT client: publishes unlock
- Automation engine → PostgreSQL: reads and updates
- Automation engine → MQTT client: publishes action
- MQTT client → MQTT broker: publishes
- IoT device → MQTT broker: exchanges messages
- Homes API → PostgreSQL: reads and writes
- Rooms API → PostgreSQL: reads and writes
- Devices API → PostgreSQL: reads and writes
- Authentication API → PostgreSQL: reads and writes
- Automations API → PostgreSQL: reads and writes
- Security API → PostgreSQL: reads and writes
- Energy API → PostgreSQL: reads and writes
- Suggestions API → PostgreSQL: reads and writes
