diff --git a/README.md b/README.md index bba3f97..fbe6388 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ # 猪场管理系统 +## 安装说明 + +### 推荐使用 TimescaleDB + +TimescaleDB 是基于 PostgreSQL 的开源数据库, 专门为处理时序数据而设计的。可以应对后续传海量传感器数据 + ## 功能介绍 ### 一. 猪舍控制 diff --git a/config.yml b/config.yml index 79bff63..33d0652 100644 --- a/config.yml +++ b/config.yml @@ -29,6 +29,7 @@ database: password: "pig-farm-controller" dbname: "pig-farm-controller" sslmode: "disable" # 在生产环境中建议使用 "require" + is_timescaledb: false max_open_conns: 25 # 最大开放连接数 max_idle_conns: 10 # 最大空闲连接数 conn_max_lifetime: 600 # 连接最大生命周期(秒) diff --git a/internal/infra/config/config.go b/internal/infra/config/config.go index 273e37c..4944f99 100644 --- a/internal/infra/config/config.go +++ b/internal/infra/config/config.go @@ -81,6 +81,9 @@ type DatabaseConfig struct { // SSLMode SSL模式 SSLMode string `yaml:"sslmode"` + // IsTimescaleDB is timescaledb + IsTimescaleDB bool `yaml:"is_timescaledb"` + // MaxOpenConns 最大开放连接数 MaxOpenConns int `yaml:"max_open_conns"` diff --git a/internal/infra/database/postgres.go b/internal/infra/database/postgres.go index c5df1b3..5d77cf9 100644 --- a/internal/infra/database/postgres.go +++ b/internal/infra/database/postgres.go @@ -16,6 +16,7 @@ import ( // 使用GORM作为ORM库 type PostgresStorage struct { db *gorm.DB + isTimescaleDB bool connectionString string maxOpenConns int maxIdleConns int @@ -25,9 +26,10 @@ type PostgresStorage struct { // NewPostgresStorage 创建并返回一个新的PostgreSQL存储实例 // 它接收一个 logger 实例,而不是自己创建 -func NewPostgresStorage(connectionString string, maxOpenConns, maxIdleConns, connMaxLifetime int, logger *logs.Logger) *PostgresStorage { +func NewPostgresStorage(connectionString string, isTimescaleDB bool, maxOpenConns, maxIdleConns, connMaxLifetime int, logger *logs.Logger) *PostgresStorage { return &PostgresStorage{ connectionString: connectionString, + isTimescaleDB: isTimescaleDB, maxOpenConns: maxOpenConns, maxIdleConns: maxIdleConns, connMaxLifetime: connMaxLifetime, diff --git a/internal/infra/database/storage.go b/internal/infra/database/storage.go index 72302d7..ceeb84c 100644 --- a/internal/infra/database/storage.go +++ b/internal/infra/database/storage.go @@ -42,9 +42,11 @@ func NewStorage(cfg config.DatabaseConfig, logger *logs.Logger) Storage { cfg.SSLMode, ) + // 当前默认返回PostgreSQL存储实现,并将 logger 注入 // 当前默认返回PostgreSQL存储实现,并将 logger 注入 return NewPostgresStorage( connectionString, + cfg.IsTimescaleDB, // <--- 添加 IsTimescaleDB cfg.MaxOpenConns, cfg.MaxIdleConns, cfg.ConnMaxLifetime, diff --git a/internal/infra/models/SensorData.go b/internal/infra/models/SensorData.go new file mode 100644 index 0000000..1fdb548 --- /dev/null +++ b/internal/infra/models/SensorData.go @@ -0,0 +1,30 @@ +package models + +import ( + "time" + + "gorm.io/datatypes" +) + +// SensorData 存储所有类型的传感器数据,对应数据库中的 'sensor_data' 表。 +type SensorData struct { + // Time 是数据记录的时间戳,作为复合主键的一部分。 + // GORM 会将其映射到 'time' TIMESTAMPTZ 列。 + Time time.Time `gorm:"primaryKey"` + + // DeviceID 是传感器的唯一标识符,作为复合主键的另一部分。 + // GORM 会将其映射到 'device_id' VARCHAR(50) 列。 + DeviceID string `gorm:"primaryKey;size:50"` + + // RegionalControllerID 是上报此数据的区域主控的ID。 + // 我们为其添加了数据库索引以优化按区域查询的性能。 + RegionalControllerID string `gorm:"size:50;index"` + + // Data 存储一个或多个传感器读数,格式为 JSON。 + // GORM 会使用 'jsonb' 类型来创建此列。 + Data datatypes.JSON `gorm:"type:jsonb"` +} + +func (SensorData) TableName() string { + return "sensor_data" +} diff --git a/internal/infra/models/models.go b/internal/infra/models/models.go index 4d542da..812be1a 100644 --- a/internal/infra/models/models.go +++ b/internal/infra/models/models.go @@ -12,5 +12,6 @@ func GetAllModels() []interface{} { &PlanExecutionLog{}, &TaskExecutionLog{}, &PendingTask{}, + &SensorData{}, } }