angle_dtu/db.go
2025-05-18 14:11:45 +08:00

202 lines
4.1 KiB
Go

package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
const SCALING_FACTOR = 1000 // 浮点数到整数的转换因子
// 初始化数据库连接
func InitDB() error {
username := "root"
password := "root"
host := "localhost"
port := "3306"
dbName := "probe_db"
// 连接字符串
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", username, password, host, port, dbName)
var err error
db, err = sql.Open("mysql", dsn)
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(5)
db.SetConnMaxLifetime(time.Minute * 5)
return nil
}
// 关闭数据库连接
func CloseDB() {
if db != nil {
db.Close()
}
}
// 保存传感器数据 - 将浮点值转换为整数存储
func SaveSensorData(sensorID int, x, y, z float64) error {
xInt := int(x * SCALING_FACTOR)
yInt := int(y * SCALING_FACTOR)
zInt := int(z * SCALING_FACTOR)
query := `INSERT INTO sensor_data (sensor_id, x_value, y_value, z_value) VALUES (?, ?, ?, ?)`
_, err := db.Exec(query, sensorID, xInt, yInt, zInt)
return err
}
// 获取传感器数据 - 添加时间范围
func GetSensorData(sensorID int, limit int, startDate time.Time, endDate time.Time) ([]SensorData, error) {
query := `SELECT id, sensor_id, x_value, y_value, z_value,
timestamp as timestamp
FROM sensor_data
WHERE sensor_id = ?`
var args []interface{}
args = append(args, sensorID)
if !startDate.IsZero() {
query += " AND timestamp >= ?"
args = append(args, startDate)
}
if !endDate.IsZero() {
query += " AND timestamp <= ?"
args = append(args, endDate)
}
query += " ORDER BY timestamp DESC"
// 只有当limit > 0时才添加LIMIT子句
if limit > 0 {
query += " LIMIT ?"
args = append(args, limit)
}
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var result []SensorData
for rows.Next() {
var data SensorData
var xInt, yInt, zInt int
err := rows.Scan(&data.ID, &data.SensorID, &xInt, &yInt, &zInt, &data.Timestamp)
if err != nil {
return nil, err
}
data.X = float64(xInt) / SCALING_FACTOR
data.Y = float64(yInt) / SCALING_FACTOR
data.Z = float64(zInt) / SCALING_FACTOR
result = append(result, data)
}
return result, nil
}
// 获取所有传感器数据
func GetAllSensorData(limit int, startDate time.Time, endDate time.Time) ([]SensorData, error) {
query := `SELECT id, sensor_id, x_value, y_value, z_value,
timestamp as timestamp
FROM sensor_data
WHERE 1=1`
var args []interface{}
if !startDate.IsZero() {
query += " AND timestamp >= ?"
args = append(args, startDate)
}
if !endDate.IsZero() {
query += " AND timestamp <= ?"
args = append(args, endDate)
}
query += " ORDER BY timestamp DESC"
// 只有当limit > 0时才添加LIMIT子句
if limit > 0 {
query += " LIMIT ?"
args = append(args, limit)
}
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var result []SensorData
for rows.Next() {
var data SensorData
var xInt, yInt, zInt int
err := rows.Scan(&data.ID, &data.SensorID, &xInt, &yInt, &zInt, &data.Timestamp)
if err != nil {
return nil, err
}
data.X = float64(xInt) / SCALING_FACTOR
data.Y = float64(yInt) / SCALING_FACTOR
data.Z = float64(zInt) / SCALING_FACTOR
result = append(result, data)
}
return result, nil
}
// 获取所有传感器ID
func GetAllSensorIDs() ([]int, error) {
query := `SELECT DISTINCT sensor_id FROM sensor_data ORDER BY sensor_id`
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
var ids []int
for rows.Next() {
var id int
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, nil
}
// SensorData 结构用于存储传感器数据
type SensorData struct {
ID int `json:"id"`
SensorID int `json:"sensor_id"`
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
Timestamp time.Time `json:"timestamp"`
}