207 lines
4.9 KiB
Go
207 lines
4.9 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 {
|
||
// 调整这些参数以匹配你的MySQL配置
|
||
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 {
|
||
// 转换为整数,乘以1000并四舍五入
|
||
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 ?"
|
||
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 ?"
|
||
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"`
|
||
}
|