fix: 修改前端样式
This commit is contained in:
parent
86f731b76c
commit
ee338d2a09
193
db.go
193
db.go
@ -4,39 +4,40 @@ 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" // 请替换为你的实际密码
|
||||
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
|
||||
}
|
||||
|
||||
@ -52,7 +53,7 @@ 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
|
||||
@ -60,113 +61,113 @@ func SaveSensorData(sensorID int, x, y, z float64) error {
|
||||
|
||||
// 获取传感器数据 - 添加时间范围
|
||||
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,
|
||||
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
|
||||
|
||||
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,
|
||||
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
|
||||
|
||||
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
|
||||
@ -175,7 +176,7 @@ func GetAllSensorIDs() ([]int, error) {
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
|
||||
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
@ -32,7 +32,8 @@
|
||||
margin-bottom: 20px;
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 1px;
|
||||
border-radius: 4px;
|
||||
background-color: #fff;
|
||||
}
|
||||
|
||||
.control-group {
|
||||
@ -42,18 +43,30 @@
|
||||
}
|
||||
|
||||
select, input, button {
|
||||
padding: 5px;
|
||||
padding: 5px 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
button {
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-color: #0056b3;
|
||||
}
|
||||
|
||||
.chart-container {
|
||||
margin-bottom: 20px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 1px;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
padding: 15px;
|
||||
background-color: #fff;
|
||||
}
|
||||
|
||||
canvas {
|
||||
@ -63,13 +76,17 @@
|
||||
|
||||
.table-container {
|
||||
overflow-x: auto;
|
||||
margin-bottom: 20px;
|
||||
margin-top: 20px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 5px;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
background-color: #fff;
|
||||
padding: 15px;
|
||||
}
|
||||
h2{
|
||||
text-align:center;
|
||||
color: #333;
|
||||
margin-top: 0;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
table {
|
||||
@ -79,18 +96,23 @@
|
||||
|
||||
th, td {
|
||||
border: 1px solid #ddd;
|
||||
padding: 8px;
|
||||
padding: 12px 8px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
th {
|
||||
background-color: #f8f8f8;
|
||||
background-color: #f8f9fa;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
tr:nth-child(even) {
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
|
||||
tr:hover {
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
.footer {
|
||||
text-align: center;
|
||||
padding: 10px;
|
||||
@ -100,11 +122,14 @@
|
||||
@media (max-width: 768px) {
|
||||
.controls {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
}
|
||||
|
||||
.control-group {
|
||||
justify-content: space-between;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
select, input {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user