issue_9 #14
@@ -1,5 +1,11 @@
|
|||||||
# 猪场管理系统
|
# 猪场管理系统
|
||||||
|
|
||||||
|
## 安装说明
|
||||||
|
|
||||||
|
### 推荐使用 TimescaleDB
|
||||||
|
|
||||||
|
TimescaleDB 是基于 PostgreSQL 的开源数据库, 专门为处理时序数据而设计的。可以应对后续传海量传感器数据
|
||||||
|
|
||||||
## 功能介绍
|
## 功能介绍
|
||||||
|
|
||||||
### 一. 猪舍控制
|
### 一. 猪舍控制
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ database:
|
|||||||
password: "pig-farm-controller"
|
password: "pig-farm-controller"
|
||||||
dbname: "pig-farm-controller"
|
dbname: "pig-farm-controller"
|
||||||
sslmode: "disable" # 在生产环境中建议使用 "require"
|
sslmode: "disable" # 在生产环境中建议使用 "require"
|
||||||
|
is_timescaledb: false
|
||||||
max_open_conns: 25 # 最大开放连接数
|
max_open_conns: 25 # 最大开放连接数
|
||||||
max_idle_conns: 10 # 最大空闲连接数
|
max_idle_conns: 10 # 最大空闲连接数
|
||||||
conn_max_lifetime: 600 # 连接最大生命周期(秒)
|
conn_max_lifetime: 600 # 连接最大生命周期(秒)
|
||||||
|
|||||||
@@ -81,6 +81,9 @@ type DatabaseConfig struct {
|
|||||||
// SSLMode SSL模式
|
// SSLMode SSL模式
|
||||||
SSLMode string `yaml:"sslmode"`
|
SSLMode string `yaml:"sslmode"`
|
||||||
|
|
||||||
|
// IsTimescaleDB is timescaledb
|
||||||
|
IsTimescaleDB bool `yaml:"is_timescaledb"`
|
||||||
|
|
||||||
// MaxOpenConns 最大开放连接数
|
// MaxOpenConns 最大开放连接数
|
||||||
MaxOpenConns int `yaml:"max_open_conns"`
|
MaxOpenConns int `yaml:"max_open_conns"`
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
// 使用GORM作为ORM库
|
// 使用GORM作为ORM库
|
||||||
type PostgresStorage struct {
|
type PostgresStorage struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
|
isTimescaleDB bool
|
||||||
connectionString string
|
connectionString string
|
||||||
maxOpenConns int
|
maxOpenConns int
|
||||||
maxIdleConns int
|
maxIdleConns int
|
||||||
@@ -25,9 +26,10 @@ type PostgresStorage struct {
|
|||||||
|
|
||||||
// NewPostgresStorage 创建并返回一个新的PostgreSQL存储实例
|
// NewPostgresStorage 创建并返回一个新的PostgreSQL存储实例
|
||||||
// 它接收一个 logger 实例,而不是自己创建
|
// 它接收一个 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{
|
return &PostgresStorage{
|
||||||
connectionString: connectionString,
|
connectionString: connectionString,
|
||||||
|
isTimescaleDB: isTimescaleDB,
|
||||||
maxOpenConns: maxOpenConns,
|
maxOpenConns: maxOpenConns,
|
||||||
maxIdleConns: maxIdleConns,
|
maxIdleConns: maxIdleConns,
|
||||||
connMaxLifetime: connMaxLifetime,
|
connMaxLifetime: connMaxLifetime,
|
||||||
|
|||||||
@@ -42,9 +42,11 @@ func NewStorage(cfg config.DatabaseConfig, logger *logs.Logger) Storage {
|
|||||||
cfg.SSLMode,
|
cfg.SSLMode,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 当前默认返回PostgreSQL存储实现,并将 logger 注入
|
||||||
// 当前默认返回PostgreSQL存储实现,并将 logger 注入
|
// 当前默认返回PostgreSQL存储实现,并将 logger 注入
|
||||||
return NewPostgresStorage(
|
return NewPostgresStorage(
|
||||||
connectionString,
|
connectionString,
|
||||||
|
cfg.IsTimescaleDB, // <--- 添加 IsTimescaleDB
|
||||||
cfg.MaxOpenConns,
|
cfg.MaxOpenConns,
|
||||||
cfg.MaxIdleConns,
|
cfg.MaxIdleConns,
|
||||||
cfg.ConnMaxLifetime,
|
cfg.ConnMaxLifetime,
|
||||||
|
|||||||
30
internal/infra/models/SensorData.go
Normal file
30
internal/infra/models/SensorData.go
Normal file
@@ -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"
|
||||||
|
}
|
||||||
@@ -12,5 +12,6 @@ func GetAllModels() []interface{} {
|
|||||||
&PlanExecutionLog{},
|
&PlanExecutionLog{},
|
||||||
&TaskExecutionLog{},
|
&TaskExecutionLog{},
|
||||||
&PendingTask{},
|
&PendingTask{},
|
||||||
|
&SensorData{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user