1. 实现配置文件解析
2. 实现数据库连接
This commit is contained in:
4
internal/api/api.go
Normal file
4
internal/api/api.go
Normal file
@@ -0,0 +1,4 @@
|
||||
// Package api 提供统一的API接口层
|
||||
// 负责处理所有外部请求,包括HTTP和WebSocket接口
|
||||
// 将请求路由到相应的服务层进行处理
|
||||
package api
|
||||
94
internal/config/config.go
Normal file
94
internal/config/config.go
Normal file
@@ -0,0 +1,94 @@
|
||||
// Package config 提供配置文件读取和解析功能
|
||||
// 支持YAML格式的配置文件解析
|
||||
// 包含服务器和数据库相关配置
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Config 代表应用的完整配置结构
|
||||
type Config struct {
|
||||
// Server 服务器配置
|
||||
Server ServerConfig `yaml:"server"`
|
||||
|
||||
// Database 数据库配置
|
||||
Database DatabaseConfig `yaml:"database"`
|
||||
}
|
||||
|
||||
// ServerConfig 代表服务器配置
|
||||
type ServerConfig struct {
|
||||
// Host 服务器监听IP
|
||||
Host string `yaml:"host"`
|
||||
|
||||
// Port 服务器监听端口
|
||||
Port int `yaml:"port"`
|
||||
}
|
||||
|
||||
// DatabaseConfig 代表数据库配置
|
||||
type DatabaseConfig struct {
|
||||
// Host 数据库主机地址
|
||||
Host string `yaml:"host"`
|
||||
|
||||
// Port 数据库端口
|
||||
Port int `yaml:"port"`
|
||||
|
||||
// Username 数据库用户名
|
||||
Username string `yaml:"username"`
|
||||
|
||||
// Password 数据库密码
|
||||
Password string `yaml:"password"`
|
||||
|
||||
// DBName 数据库名称
|
||||
DBName string `yaml:"dbname"`
|
||||
|
||||
// SSLMode SSL模式
|
||||
SSLMode string `yaml:"sslmode"`
|
||||
|
||||
// MaxOpenConns 最大开放连接数
|
||||
MaxOpenConns int `yaml:"max_open_conns"`
|
||||
|
||||
// MaxIdleConns 最大空闲连接数
|
||||
MaxIdleConns int `yaml:"max_idle_conns"`
|
||||
|
||||
// ConnMaxLifetime 连接最大生命周期(秒)
|
||||
ConnMaxLifetime int `yaml:"conn_max_lifetime"`
|
||||
}
|
||||
|
||||
// NewConfig 创建并返回一个新的配置实例
|
||||
func NewConfig() *Config {
|
||||
return &Config{}
|
||||
}
|
||||
|
||||
// Load 从指定路径加载配置文件
|
||||
func (c *Config) Load(path string) error {
|
||||
// 读取配置文件
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read config file: %v", err)
|
||||
}
|
||||
|
||||
// 解析YAML配置
|
||||
if err := yaml.Unmarshal(data, c); err != nil {
|
||||
return fmt.Errorf("failed to parse config file: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDatabaseConnectionString 获取数据库连接字符串
|
||||
func (c *Config) GetDatabaseConnectionString() string {
|
||||
// 构建PostgreSQL连接字符串
|
||||
return fmt.Sprintf(
|
||||
"user=%s password=%s dbname=%s host=%s port=%d sslmode=%s",
|
||||
c.Database.Username,
|
||||
c.Database.Password,
|
||||
c.Database.DBName,
|
||||
c.Database.Host,
|
||||
c.Database.Port,
|
||||
c.Database.SSLMode,
|
||||
)
|
||||
}
|
||||
16
internal/controller/biosecurity/biosecurity.go
Normal file
16
internal/controller/biosecurity/biosecurity.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package biosecurity 提供生物安全控制功能
|
||||
// 实现人员/车辆进出管理、消毒流程控制等
|
||||
// 通过任务执行器执行具体控制任务
|
||||
package biosecurity
|
||||
|
||||
// BiosecurityController 生物安全控制器
|
||||
// 管理生物安全相关控制逻辑
|
||||
type BiosecurityController struct {
|
||||
// TODO: 定义生物安全控制器结构
|
||||
}
|
||||
|
||||
// NewBiosecurityController 创建并返回一个新的生物安全控制器实例
|
||||
func NewBiosecurityController() *BiosecurityController {
|
||||
// TODO: 实现生物安全控制器初始化
|
||||
return nil
|
||||
}
|
||||
16
internal/controller/feed/feed.go
Normal file
16
internal/controller/feed/feed.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package feed 提供饲料控制功能
|
||||
// 实现饲料制备和分配的控制逻辑
|
||||
// 通过任务执行器执行具体控制任务
|
||||
package feed
|
||||
|
||||
// FeedController 饲料控制器
|
||||
// 管理饲料制备和分配设备的控制逻辑
|
||||
type FeedController struct {
|
||||
// TODO: 定义饲料控制器结构
|
||||
}
|
||||
|
||||
// NewFeedController 创建并返回一个新的饲料控制器实例
|
||||
func NewFeedController() *FeedController {
|
||||
// TODO: 实现饲料控制器初始化
|
||||
return nil
|
||||
}
|
||||
16
internal/controller/house/house.go
Normal file
16
internal/controller/house/house.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package house 提供猪舍控制功能
|
||||
// 实现对猪舍内设备的控制和环境监测
|
||||
// 通过任务执行器执行具体控制任务
|
||||
package house
|
||||
|
||||
// HouseController 猪舍控制器
|
||||
// 管理猪舍内所有设备的控制逻辑
|
||||
type HouseController struct {
|
||||
// TODO: 定义猪舍控制器结构
|
||||
}
|
||||
|
||||
// NewHouseController 创建并返回一个新的猪舍控制器实例
|
||||
func NewHouseController() *HouseController {
|
||||
// TODO: 实现猪舍控制器初始化
|
||||
return nil
|
||||
}
|
||||
16
internal/controller/remote/remote.go
Normal file
16
internal/controller/remote/remote.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package remote 提供远程管理功能
|
||||
// 实现手机端和Web端的远程监控和控制能力
|
||||
// 通过API接口层提供远程访问能力
|
||||
package remote
|
||||
|
||||
// RemoteController 远程控制器
|
||||
// 管理远程访问和控制的逻辑
|
||||
type RemoteController struct {
|
||||
// TODO: 定义远程控制器结构
|
||||
}
|
||||
|
||||
// NewRemoteController 创建并返回一个新的远程控制器实例
|
||||
func NewRemoteController() *RemoteController {
|
||||
// TODO: 实现远程控制器初始化
|
||||
return nil
|
||||
}
|
||||
68
internal/core/application.go
Normal file
68
internal/core/application.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Package core 提供核心应用协调功能
|
||||
// 负责初始化和协调API、任务执行器和存储等核心组件
|
||||
// 管理整个应用的生命周期
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/config"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/storage/db"
|
||||
)
|
||||
|
||||
// Application 代表核心应用结构
|
||||
// 协调API、任务执行器和存储组件的工作
|
||||
type Application struct {
|
||||
// Storage 存储组件实例
|
||||
Storage db.Storage
|
||||
|
||||
// Config 应用配置
|
||||
Config *config.Config
|
||||
}
|
||||
|
||||
// NewApplication 创建并返回一个新的核心应用实例
|
||||
// 初始化所有核心组件
|
||||
func NewApplication(cfg *config.Config) *Application {
|
||||
// 从配置中获取数据库连接字符串
|
||||
connectionString := cfg.GetDatabaseConnectionString()
|
||||
|
||||
// 从配置中获取连接池参数
|
||||
maxOpenConns := cfg.Database.MaxOpenConns
|
||||
maxIdleConns := cfg.Database.MaxIdleConns
|
||||
connMaxLifetime := cfg.Database.ConnMaxLifetime
|
||||
|
||||
// 初始化存储组件
|
||||
store := db.NewStorage(connectionString, maxOpenConns, maxIdleConns, connMaxLifetime)
|
||||
|
||||
return &Application{
|
||||
Storage: store,
|
||||
Config: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
// Start 启动核心应用
|
||||
// 按正确顺序启动所有核心组件
|
||||
func (app *Application) Start() error {
|
||||
// 启动存储组件
|
||||
if err := app.Storage.Connect(); err != nil {
|
||||
return fmt.Errorf("failed to connect to storage: %v", err)
|
||||
}
|
||||
log.Println("Storage connected successfully")
|
||||
|
||||
// TODO: 启动其他核心组件
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop 停止核心应用
|
||||
// 按正确顺序停止所有核心组件
|
||||
func (app *Application) Stop() error {
|
||||
// 停止存储组件
|
||||
if err := app.Storage.Disconnect(); err != nil {
|
||||
return fmt.Errorf("failed to disconnect from storage: %v", err)
|
||||
}
|
||||
log.Println("Storage disconnected successfully")
|
||||
|
||||
// TODO: 停止其他核心组件
|
||||
return nil
|
||||
}
|
||||
94
internal/storage/db/postgres.go
Normal file
94
internal/storage/db/postgres.go
Normal file
@@ -0,0 +1,94 @@
|
||||
// Package db 提供基于PostgreSQL的数据存储功能
|
||||
// 使用GORM作为ORM库来操作数据库
|
||||
// 实现与PostgreSQL数据库的连接和基本操作
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PostgresStorage 代表基于PostgreSQL的存储实现
|
||||
// 使用GORM作为ORM库
|
||||
type PostgresStorage struct {
|
||||
// db GORM数据库实例
|
||||
db *gorm.DB
|
||||
|
||||
// connectionString 数据库连接字符串
|
||||
connectionString string
|
||||
|
||||
// maxOpenConns 最大开放连接数
|
||||
maxOpenConns int
|
||||
|
||||
// maxIdleConns 最大空闲连接数
|
||||
maxIdleConns int
|
||||
|
||||
// connMaxLifetime 连接最大生命周期(秒)
|
||||
connMaxLifetime int
|
||||
}
|
||||
|
||||
// NewPostgresStorage 创建并返回一个新的PostgreSQL存储实例
|
||||
// 初始化数据库连接和相关配置
|
||||
func NewPostgresStorage(connectionString string, maxOpenConns, maxIdleConns, connMaxLifetime int) *PostgresStorage {
|
||||
return &PostgresStorage{
|
||||
connectionString: connectionString,
|
||||
maxOpenConns: maxOpenConns,
|
||||
maxIdleConns: maxIdleConns,
|
||||
connMaxLifetime: connMaxLifetime,
|
||||
}
|
||||
}
|
||||
|
||||
// Connect 建立与PostgreSQL数据库的连接
|
||||
// 使用GORM建立数据库连接
|
||||
func (ps *PostgresStorage) Connect() error {
|
||||
var err error
|
||||
ps.db, err = gorm.Open(postgres.Open(ps.connectionString), &gorm.Config{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
// 测试连接
|
||||
sqlDB, err := ps.db.DB()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get database instance: %v", err)
|
||||
}
|
||||
|
||||
if err = sqlDB.Ping(); err != nil {
|
||||
return fmt.Errorf("failed to ping database: %v", err)
|
||||
}
|
||||
|
||||
// 设置连接池参数
|
||||
sqlDB.SetMaxOpenConns(ps.maxOpenConns)
|
||||
sqlDB.SetMaxIdleConns(ps.maxIdleConns)
|
||||
sqlDB.SetConnMaxLifetime(time.Duration(ps.connMaxLifetime) * time.Second)
|
||||
|
||||
log.Println("Successfully connected to PostgreSQL database")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Disconnect 断开与PostgreSQL数据库的连接
|
||||
// 安全地关闭所有数据库连接
|
||||
func (ps *PostgresStorage) Disconnect() error {
|
||||
if ps.db != nil {
|
||||
sqlDB, err := ps.db.DB()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get database instance: %v", err)
|
||||
}
|
||||
|
||||
if err := sqlDB.Close(); err != nil {
|
||||
return fmt.Errorf("failed to close database connection: %v", err)
|
||||
}
|
||||
log.Println("Successfully disconnected from PostgreSQL database")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDB 获取GORM数据库实例
|
||||
// 用于执行具体的数据库操作
|
||||
func (ps *PostgresStorage) GetDB() *gorm.DB {
|
||||
return ps.db
|
||||
}
|
||||
28
internal/storage/db/storage.go
Normal file
28
internal/storage/db/storage.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Package db 提供统一的数据存储接口
|
||||
// 定义存储接口规范,支持多种存储后端实现
|
||||
// 当前支持PostgreSQL实现
|
||||
package db
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Storage 代表统一的存储接口
|
||||
// 所有存储实现都需要实现此接口定义的方法
|
||||
type Storage interface {
|
||||
// Connect 建立与存储后端的连接
|
||||
Connect() error
|
||||
|
||||
// Disconnect 断开与存储后端的连接
|
||||
Disconnect() error
|
||||
|
||||
// GetDB 获取数据库实例
|
||||
GetDB() *gorm.DB
|
||||
}
|
||||
|
||||
// NewStorage 创建并返回一个存储实例
|
||||
// 根据配置返回相应的存储实现
|
||||
func NewStorage(connectionString string, maxOpenConns, maxIdleConns, connMaxLifetime int) Storage {
|
||||
// 当前默认返回PostgreSQL存储实现
|
||||
return NewPostgresStorage(connectionString, maxOpenConns, maxIdleConns, connMaxLifetime)
|
||||
}
|
||||
16
internal/task/executor.go
Normal file
16
internal/task/executor.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package task 提供任务执行器功能
|
||||
// 负责执行各种控制任务,如环境调节、饲料投放等
|
||||
// 管理任务队列和并发执行
|
||||
package task
|
||||
|
||||
// Executor 代表任务执行器
|
||||
// 负责调度和执行所有控制任务
|
||||
type Executor struct {
|
||||
// TODO: 定义任务执行器结构
|
||||
}
|
||||
|
||||
// NewExecutor 创建并返回一个新的任务执行器实例
|
||||
func NewExecutor() *Executor {
|
||||
// TODO: 实现任务执行器初始化
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user