## Page Guide

- **Use Cases**: For starting the FastBee backend service during local development, source-code deployment, or secondary development.
- **Prerequisites**: MySQL, Redis, and Maven/IDEA are ready, and the project database script has been imported.
- **Key Configuration**: Focus on checking application.yml, environment configuration, file upload path, MQTT service selection, and data source connections.
- **Acceptance Criteria**: The backend service starts without exceptions, APIs are accessible, and the database, Redis, and MQTT connection logs are normal.

## 1. Create The Database

Create the FastBee database with the utf8 or utf8mb4 character set, and import the `springboot/sql/fastbee.sql` database script from the project.

::: tip
When installing MySQL on Linux, configure case-insensitive table names by adding `lower_case_table_names=1` to `/etc/my.cnf`, then restart the MySQL service.
:::

## 2. Open The Project

Open the `springboot` folder of the project with the ==development tool IDEA==. Maven dependencies will be installed automatically; wait for the installation to complete. The speed depends on the network.

## 3. Modify The Main Configuration File

The file is located at: ` springboot/fastbee-admin/src/main/resources/application.yml`. Refer to the comments in the file for specific configuration items.

```yml
fastbee:
  name: fastbee             # Name
  profile: /uploadPath      # File path, ending with uploadPath. Example (Windows: D:/uploadPath, Linux: /uploadPath)
  addressEnabled: true      # Switch for obtaining IP addresses
  captchaType: math         # Captcha type. math: arithmetic captcha, char: character captcha

server:
  port: 8080                # Server HTTP port, default 8080
  servlet:
    context-path: /         # Application access path
  tomcat:
    uri-encoding: UTF-8     # Tomcat URI encoding
    accept-count: 1000      # Queue size after all connections are used, default 100
    threads:
      max: 800              # Maximum Tomcat threads, default 200
      min-spare: 100        # Initial Tomcat threads at startup, default 10
  # Netty-based server
  broker:
    enabled: false          # mqttBroker type selection. true: Netty-based mqttBroker and webSocket; false: EMQ mqttBroker
    broker-node: node1      # Server cluster node
    port: 1883
    openws: false           # Controls whether webSocket is enabled
    websocket-port: 8083
    websocket-path: /mqtt
    keep-alive: 70          # Default keep-alive upload time for all clients
  #TCP service port
  tcp:
    enabled: true           # Controls whether the TCP port is enabled
    port: 8888
    keep-alive: 70
    delimiter: 0x7e

spring:
  profiles:
    active: prod            # Environment configuration. dev = development environment, prod = production environment
```

::: tip
Pay attention to the configuration file format; indentation must be aligned. Focus on the following:

* File upload path. Windows and Linux use different path formats.
* MQTT message server selection. The backend has a built-in netty-mqtt service; you can also choose EMQX as the MQTT message server.
* Environment configuration. Development and production environments correspond to different sub-configuration files; prod uses the application-prod.yml configuration file.
  :::

::: danger Common Errors
**The backend has a built-in MQTT service. The professional edition can also choose EMQX as the MQTT message server. Only one of the two can be used.**<br />
Pay attention to the following configuration:
::: code-group

```yml [Select the MQTT server in application.yml]
  # Content located in the application.yml file
  broker:
    enabled: false           # MQTT server selection. true: Netty-based mqttBroker and webSocket; false: EMQ mqttBroker
    port: 1883
    websocket-port: 8083
    websocket-path: /mqtt
    keep-alive: 70           # System keep-alive time, which must be more than 10 seconds longer than the device-side keep-alive time. If the system is configured with 70 seconds, the device side must be within 60 seconds.
```

```yml [Configure the MQTT address in application-dev.yml]
  # Content located in the application-dev.yml and application-prod.yml files
  mqtt:
    username: fastbee                       
    password: fastbee                      
    host-url: tcp://localhost:1883    # MQTT connection TCP address (set to the EMQX installation address if EMQX is used)
    client-id: ${random.int}                
    default-topic: test                     
    timeout: 30                             
    keepalive: 30                           
    clearSession: true                      
```

:::

## 4. Modify The Sub-Configuration File

The file is located at: `springboot/fastbee-admin/src/main/resources/application-dev.yml`, used for the development environment. For the production environment, use `application-prod`.

```yml
spring:
  datasource:
    dynamic:
      druid:
        initial-size: 5
        min-idle: 10
        max-wait: 60000
        max-active: 20
        timeBetweenEvictionRunsMillis: 60000  # Interval between eviction checks that close idle connections, in milliseconds
        minEvictableIdleTimeMillis: 300000    # Minimum time a connection can remain in the pool, in milliseconds
        maxEvictableIdleTimeMillis: 900000    # Maximum time a connection can remain in the pool, in milliseconds
        validation-query: 'SELECT 1'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
      datasource:
        master:                               # Master data source
          type: com.alibaba.druid.pool.DruidDataSource
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/fastbee?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
          username: root
          password: fastbee
          druid:
            filters: stat,wall
            stat:
              # Slow SQL recording
              log-slow-sql: true
              slow-sql-millis: 1000
              merge-sql: true
            wall:
              none-base-statement-allow: true
        taos:                                # Configure the taos data source
          enabled: false                     # TDengine is disabled by default. true = enabled, false = disabled
          type: com.alibaba.druid.pool.DruidDataSource
          driver-class-name: com.taosdata.jdbc.TSDBDriver
          url: jdbc:TAOS://fastbee:6030/fastbee_log?timezone=UTC-8&charset=utf-8
          username: root
          password: taosdata
          dbName: fastbee_log


  redis:                                    # redis configuration
    host: localhost                         # Address
    port: 6379                              # Port, default 6379
    database: 0                             # Database index
    password: fastbee                       # Password
    timeout: 10s                            # Connection timeout
    lettuce:
      pool:
        min-idle: 0                         # Minimum idle connections in the pool
        max-idle: 8                         # Maximum idle connections in the pool
        max-active: 8                       # Maximum database connections in the pool
        max-wait: -1ms                      # Maximum blocking wait time of the pool (a negative value means no limit)
  
  mqtt:                                     # mqtt configuration
    username: fastbee                       # Account
    password: fastbee                       # Password
    host-url: tcp://localhost:1883          # MQTT connection TCP address (points to the backend or EMQX address)
    client-id: ${random.int}                # Client ID, must be unique; a random number is used: ${random.value}
    default-topic: test                     # Default topic
    timeout: 30                             # Timeout
    keepalive: 30                           # Keep alive
    clearSession: true                      # Clear session (when set to false, after disconnecting and reconnecting, the previous session is used, subscribed topics are retained, and messages received during the offline period can be received)
  
```

::: tip
Focus on the following:

* MySQL, TDengine, Redis, and MQTT connection addresses, accounts, and passwords.
* Using TDengine requires correct installation and configuration; the server, client, and driver versions must be consistent.
* The MQTT connection address points to the backend or the EMQX deployment address according to the selection in the main configuration file.

:::

## 5. Modify The Log Configuration

Log path configuration. The file is located at: `springboot/fastbee-admin/src/main/resources/logback.xml`

```xml
# Example (Windows: D:/logs, Linux: /var/data/java/logs)
<property name="log.path" value="/var/data/java/logs" />
```

## 6. Run The Project

Run the project `FastBeeApplication.java`. The following output indicates a successful startup.

```sh
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *      
 *           _⚲_⚲_            ______        _     ____                   *       
 *     |   /       \   |     |  ____|      | |   |  _ \                  *     
 *     |  |  | ● |  |  |     | |__ __ _ ___| |_  | |_) | ___  ___        *       
 *     |   \       /   |     |  __/ _` / __| __| |  _ < / _ \/ _ \       *    
 *           \   /           | | | (_| \__ \ |_  | |_) |  __/  __/       *    
 *             V             |_|  \__,_|___/\__| |____/ \___|\___|       *   
 *                                                                       *       
 * * * * * * * * * * * * FastBee IoT Platform [Started Successfully] * * * * * * * 
```
