188 lines
6.8 KiB
Go
188 lines
6.8 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// WeatherData 气象站数据结构
|
|
type WeatherData struct {
|
|
ID int64 `json:"id" db:"id"`
|
|
Timestamp time.Time `json:"timestamp" db:"timestamp"`
|
|
WindSpeed float64 `json:"wind_speed" db:"wind_speed"`
|
|
WindForce int `json:"wind_force" db:"wind_force"`
|
|
WindDirection8 int `json:"wind_direction_8" db:"wind_direction_8"`
|
|
WindDirection360 int `json:"wind_direction_360" db:"wind_direction_360"`
|
|
Humidity float64 `json:"humidity" db:"humidity"`
|
|
Temperature float64 `json:"temperature" db:"temperature"`
|
|
Noise float64 `json:"noise" db:"noise"`
|
|
PM25 int `json:"pm25" db:"pm25"`
|
|
PM10 int `json:"pm10" db:"pm10"`
|
|
AtmPressure float64 `json:"atm_pressure" db:"atm_pressure"`
|
|
LuxHigh int `json:"lux_high" db:"lux_high"`
|
|
LuxLow int `json:"lux_low" db:"lux_low"`
|
|
LightIntensity int `json:"light_intensity" db:"light_intensity"`
|
|
Rainfall float64 `json:"rainfall" db:"rainfall"`
|
|
CompassAngle float64 `json:"compass_angle" db:"compass_angle"`
|
|
SolarRadiation int `json:"solar_radiation" db:"solar_radiation"`
|
|
}
|
|
|
|
// RainGaugeData 雨量计数据结构
|
|
type RainGaugeData struct {
|
|
ID int64 `json:"id" db:"id"`
|
|
Timestamp time.Time `json:"timestamp" db:"timestamp"`
|
|
DailyRainfall float64 `json:"daily_rainfall" db:"daily_rainfall"`
|
|
InstantRainfall float64 `json:"instant_rainfall" db:"instant_rainfall"`
|
|
YesterdayRainfall float64 `json:"yesterday_rainfall" db:"yesterday_rainfall"`
|
|
TotalRainfall float64 `json:"total_rainfall" db:"total_rainfall"`
|
|
HourlyRainfall float64 `json:"hourly_rainfall" db:"hourly_rainfall"`
|
|
LastHourRainfall float64 `json:"last_hour_rainfall" db:"last_hour_rainfall"`
|
|
Max24hRainfall float64 `json:"max_24h_rainfall" db:"max_24h_rainfall"`
|
|
Max24hRainfallPeriod int `json:"max_24h_rainfall_period" db:"max_24h_rainfall_period"`
|
|
Min24hRainfall float64 `json:"min_24h_rainfall" db:"min_24h_rainfall"`
|
|
Min24hRainfallPeriod int `json:"min_24h_rainfall_period" db:"min_24h_rainfall_period"`
|
|
}
|
|
|
|
// AggregatedData 聚合数据结构,用于前端展示
|
|
type AggregatedData struct {
|
|
Timestamp time.Time `json:"timestamp" db:"timestamp"`
|
|
FormattedTime string `json:"formatted_time,omitempty"`
|
|
Rainfall float64 `json:"rainfall" db:"rainfall"`
|
|
AvgTemperature float64 `json:"avg_temperature" db:"avg_temperature"`
|
|
AvgHumidity float64 `json:"avg_humidity" db:"avg_humidity"`
|
|
AvgWindSpeed float64 `json:"avg_wind_speed" db:"avg_wind_speed"`
|
|
AtmPressure float64 `json:"atm_pressure" db:"atm_pressure"`
|
|
SolarRadiation float64 `json:"solar_radiation" db:"solar_radiation"`
|
|
}
|
|
|
|
// ConnectionStatus 连接状态
|
|
type ConnectionStatus struct {
|
|
Connected bool `json:"connected"`
|
|
IP string `json:"ip,omitempty"`
|
|
Port int `json:"port,omitempty"`
|
|
Count int `json:"count,omitempty"`
|
|
}
|
|
|
|
// CreateWeatherDataTable 创建气象站数据表SQL
|
|
const CreateWeatherDataTable = `
|
|
CREATE TABLE IF NOT EXISTS weather_data (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
timestamp DATETIME NOT NULL,
|
|
wind_speed FLOAT NOT NULL,
|
|
wind_force INT NOT NULL,
|
|
wind_direction_8 INT NOT NULL,
|
|
wind_direction_360 INT NOT NULL,
|
|
humidity FLOAT NOT NULL,
|
|
temperature FLOAT NOT NULL,
|
|
noise FLOAT NOT NULL,
|
|
pm25 INT NOT NULL,
|
|
pm10 INT NOT NULL,
|
|
atm_pressure FLOAT NOT NULL,
|
|
lux_high INT NOT NULL,
|
|
lux_low INT NOT NULL,
|
|
light_intensity INT NOT NULL,
|
|
rainfall FLOAT NOT NULL,
|
|
compass_angle FLOAT NOT NULL,
|
|
solar_radiation INT NOT NULL,
|
|
INDEX idx_timestamp (timestamp)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
`
|
|
|
|
// CreateRainGaugeDataTable 创建雨量计数据表SQL
|
|
const CreateRainGaugeDataTable = `
|
|
CREATE TABLE IF NOT EXISTS rain_gauge_data (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
timestamp DATETIME NOT NULL,
|
|
daily_rainfall FLOAT NOT NULL,
|
|
instant_rainfall FLOAT NOT NULL,
|
|
yesterday_rainfall FLOAT NOT NULL,
|
|
total_rainfall FLOAT NOT NULL,
|
|
hourly_rainfall FLOAT NOT NULL,
|
|
last_hour_rainfall FLOAT NOT NULL,
|
|
max_24h_rainfall FLOAT NOT NULL,
|
|
max_24h_rainfall_period INT NOT NULL,
|
|
min_24h_rainfall FLOAT NOT NULL,
|
|
min_24h_rainfall_period INT NOT NULL,
|
|
INDEX idx_timestamp (timestamp)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
`
|
|
|
|
// InsertWeatherDataSQL 插入气象站数据SQL
|
|
const InsertWeatherDataSQL = `
|
|
INSERT INTO weather_data (
|
|
timestamp, wind_speed, wind_force, wind_direction_8, wind_direction_360,
|
|
humidity, temperature, noise, pm25, pm10, atm_pressure, lux_high, lux_low,
|
|
light_intensity, rainfall, compass_angle, solar_radiation
|
|
) VALUES (
|
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
|
)
|
|
`
|
|
|
|
// InsertRainGaugeDataSQL 插入雨量计数据SQL
|
|
const InsertRainGaugeDataSQL = `
|
|
INSERT INTO rain_gauge_data (
|
|
timestamp, daily_rainfall, instant_rainfall, yesterday_rainfall,
|
|
total_rainfall, hourly_rainfall, last_hour_rainfall, max_24h_rainfall,
|
|
max_24h_rainfall_period, min_24h_rainfall, min_24h_rainfall_period
|
|
) VALUES (
|
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
|
)
|
|
`
|
|
|
|
// QueryWeatherDataByTimeRangeSQL 按时间范围查询气象站数据SQL
|
|
const QueryWeatherDataByTimeRangeSQL = `
|
|
SELECT * FROM weather_data
|
|
WHERE timestamp BETWEEN ? AND ?
|
|
ORDER BY timestamp DESC
|
|
`
|
|
|
|
// QueryRainGaugeDataByTimeRangeSQL 按时间范围查询雨量计数据SQL
|
|
const QueryRainGaugeDataByTimeRangeSQL = `
|
|
SELECT * FROM rain_gauge_data
|
|
WHERE timestamp BETWEEN ? AND ?
|
|
ORDER BY timestamp DESC
|
|
`
|
|
|
|
// QueryLatestWeatherDataSQL 查询最新气象站数据SQL
|
|
const QueryLatestWeatherDataSQL = `
|
|
SELECT * FROM weather_data
|
|
ORDER BY timestamp DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
// QueryLatestRainGaugeDataSQL 查询最新雨量计数据SQL
|
|
const QueryLatestRainGaugeDataSQL = `
|
|
SELECT * FROM rain_gauge_data
|
|
ORDER BY timestamp DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
// QueryAggregatedDataSQL 查询聚合数据SQL (小时级别)
|
|
const QueryAggregatedDataSQL = `
|
|
SELECT
|
|
w.time_hour as timestamp,
|
|
IFNULL(MAX(r.hourly_rainfall), 0) as rainfall,
|
|
AVG(w.temperature) as avg_temperature,
|
|
AVG(w.humidity) as avg_humidity,
|
|
AVG(w.wind_speed) as avg_wind_speed,
|
|
AVG(w.atm_pressure) as atm_pressure,
|
|
AVG(w.solar_radiation) as solar_radiation
|
|
FROM
|
|
(SELECT
|
|
DATE_FORMAT(timestamp, '%Y-%m-%d %H:00:00') as time_hour,
|
|
temperature, humidity, wind_speed, atm_pressure, solar_radiation
|
|
FROM weather_data
|
|
WHERE timestamp BETWEEN ? AND ?
|
|
) w
|
|
LEFT JOIN
|
|
(SELECT
|
|
DATE_FORMAT(timestamp, '%Y-%m-%d %H:00:00') as time_hour,
|
|
hourly_rainfall
|
|
FROM rain_gauge_data
|
|
WHERE timestamp BETWEEN ? AND ?
|
|
) r ON w.time_hour = r.time_hour
|
|
GROUP BY
|
|
w.time_hour
|
|
ORDER BY
|
|
w.time_hour DESC
|
|
`
|