# Backend Development

This page covers the local development environment setup, module structure, and extension development of the FastBee Community Edition backend.

## Page Guide

- **Use Cases**: Developers who need to read or modify backend source code, add endpoints or protocols, or do custom development.
- **Prerequisites**: JDK 8, Maven, MySQL 5.7+, and Redis are installed locally; component installation is covered in System Deployment.
- **Operation Path**: Clone the source → Import into IDEA → Initialize the database → Edit the configuration → Start and verify.
- **Acceptance Criteria**: The service starts without errors, API 8080 and MQTT 1883/8083 ports are listening, and /swagger-ui.html opens the API documentation.

## Environment Setup

Before you start, confirm that JDK 8, Maven, MySQL 5.7+, and Redis are installed locally; see [System Deployment](https://www.fastbee.cn/doc/en/open/v2/install/system.md) for component installation.

### 1. Clone the Source Code

```bash
git clone https://gitee.com/beecue/fastbee.git
```

### 2. Import into IDEA

Open the `springboot` directory with IntelliJ IDEA and wait for the Maven dependencies to finish downloading.

### 3. Initialize the Database

```bash
mysql -uroot -p -e "CREATE DATABASE fastbee DEFAULT CHARSET utf8 COLLATE utf8_unicode_ci;"
mysql -uroot -p fastbee < sql/fastbee.sql
```

### 4. Edit the Configuration

Edit `springboot/fastbee-admin/src/main/resources/application-dev.yml`; both the datasource and Redis configuration live in this file.

```yaml
spring:
  datasource:
    dynamic:
      datasource:
        master:
          url: jdbc:mysql://localhost:3306/fastbee?...
          username: root
          password: your_password
  redis:
    host: localhost
    port: 6379
    password: your_password
```

### 5. Start

Run the `FastBeeApplication.main()` method.

### 6. Verify the Startup

After a successful start, confirm:

* No exceptions in the console output
* The backend API port 8080, MQTT TCP port 1883, and MQTT WebSocket port 8083 are all listening
* Visiting `http://localhost:8080/swagger-ui.html` in a browser opens the Swagger API documentation

## Module Overview

| Module | Role |
|--------|------|
| fastbee-admin | Startup entry point, configuration files |
| fastbee-common | Utilities, constants, exceptions, entity base classes |
| fastbee-framework | Spring Security integration, JWT authentication, RuoYi framework |
| fastbee-open-api | 37 REST Controllers |
| fastbee-service | IoT and system business Service layer |
| fastbee-server | Netty MQTT Broker + SIP Server |
| fastbee-protocol | Protocol encoding/decoding |
| fastbee-plugs | Extension plugins |

## API Controller Layer

The `fastbee-open-api` module contains the following API controllers:

| Category | Controller | Description |
|----------|-----------|-------------|
| IoT | ProductController | Product management |
| IoT | DeviceController | Device management |
| IoT | ThingsModelController | Thing model |
| IoT | SceneController | Scene linkage |
| IoT | ScriptController | Rule scripts |
| IoT | GroupController | Device grouping |
| Video | SipDeviceController | SIP devices |
| Video | PlayerController | Video playback |
| Video | PtzController | PTZ control |
| System | DashBoardController | Dashboard |
| System | DataCenterController | Data center |

## Extension Development

### Add a New Endpoint

1. Create a Controller in `fastbee-open-api`
2. Create the Service and Mapper in `fastbee-service`
3. Add the corresponding SQL and database tables

### Add a New Protocol

1. Implement the protocol codec in `fastbee-protocol`
2. Register the protocol handler in `fastbee-server`

## FAQ

### Database connection failure on startup?

Check that the `url`, `username`, and `password` in `application-dev.yml` are correct, and confirm that `sql/fastbee.sql` has been executed to initialize the database.

### Redis connection failure on startup?

Check the `host`, `port`, and `password` under `redis` in `application-dev.yml` and the Redis service status; if Redis has a password set, it must be filled in the configuration.

### Startup fails because a port is in use?

Confirm that ports 8080, 1883, and 8083 are not occupied by other processes; if there is a conflict, change the corresponding configuration and restart.
