--
-- PostgreSQL database dump
--

-- Dumped from database version 14.18 (Ubuntu 14.18-0ubuntu0.22.04.1)
-- Dumped by pg_dump version 17.5

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--

-- *not* creating schema, since initdb creates it


SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: rs485_weather_data; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.rs485_weather_data (
    id integer NOT NULL,
    station_id character varying(50) NOT NULL,
    "timestamp" timestamp with time zone NOT NULL,
    temperature double precision,
    humidity double precision,
    wind_speed double precision,
    wind_direction double precision,
    rainfall double precision,
    light double precision,
    uv double precision,
    pressure double precision,
    raw_data text
);


--
-- Name: rs485_weather_data_bak; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.rs485_weather_data_bak (
    id integer,
    station_id character varying(50),
    "timestamp" timestamp without time zone,
    temperature numeric(5,2),
    humidity numeric(5,2),
    wind_speed numeric(5,2),
    wind_direction numeric(5,2),
    rainfall numeric(5,2),
    light numeric(15,2),
    uv numeric(8,2),
    pressure numeric(7,2),
    raw_data text
);


--
-- Name: rs485_weather_data_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.rs485_weather_data_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: rs485_weather_data_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.rs485_weather_data_id_seq OWNED BY public.rs485_weather_data.id;


--
-- Name: stations; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.stations (
    station_id character varying(50) NOT NULL,
    device_id character varying(50),
    password character varying(50) NOT NULL,
    name character varying(100),
    location character varying(100),
    latitude numeric(10,6),
    longitude numeric(10,6),
    altitude numeric(8,3),
    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
    last_update timestamp with time zone,
    software_type character varying(100),
    device_type character varying(20) DEFAULT 'UNKNOWN'::character varying NOT NULL,
    CONSTRAINT check_device_type CHECK (((device_type)::text = ANY ((ARRAY['ECOWITT'::character varying, 'WH65LP'::character varying, 'UNKNOWN'::character varying])::text[])))
);


--
-- Name: TABLE stations; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON TABLE public.stations IS '气象站设备信息表，存储设备的基本信息和认证信息';


--
-- Name: COLUMN stations.device_type; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.stations.device_type IS 'ECOWITT: WIFI型, WH65LP: 485型';


--
-- Name: weather_data; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.weather_data (
    id integer NOT NULL,
    station_id character varying(50) NOT NULL,
    "timestamp" timestamp with time zone NOT NULL,
    temp_f integer,
    humidity integer,
    dewpoint_f integer,
    windchill_f integer,
    wind_dir integer,
    wind_speed_mph integer,
    wind_gust_mph integer,
    rain_in integer,
    daily_rain_in integer,
    weekly_rain_in integer,
    monthly_rain_in integer,
    yearly_rain_in integer,
    total_rain_in integer,
    solar_radiation integer,
    uv integer,
    indoor_temp_f integer,
    indoor_humidity integer,
    abs_barometer_in integer,
    barometer_in integer,
    low_battery boolean,
    raw_data text
);


--
-- Name: TABLE weather_data; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON TABLE public.weather_data IS '气象站数据表，存储所有气象观测数据，数值型数据以整数形式存储，查询时需进行转换';


--
-- Name: COLUMN weather_data.id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.id IS '自增主键ID';


--
-- Name: COLUMN weather_data.station_id; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.station_id IS '气象站ID，外键关联stations表';


--
-- Name: COLUMN weather_data."timestamp"; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data."timestamp" IS '数据记录时间，使用UTC+8时区(中国标准时间)';


--
-- Name: COLUMN weather_data.temp_f; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.temp_f IS '室外温度，存储值=实际值×10，单位：华氏度，查询时需除以10，如768表示76.8°F';


--
-- Name: COLUMN weather_data.humidity; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.humidity IS '室外湿度，单位：百分比，如53表示53%';


--
-- Name: COLUMN weather_data.dewpoint_f; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.dewpoint_f IS '露点温度，存储值=实际值×10，单位：华氏度，查询时需除以10，如585表示58.5°F';


--
-- Name: COLUMN weather_data.windchill_f; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.windchill_f IS '风寒指数，存储值=实际值×10，单位：华氏度，查询时需除以10，如768表示76.8°F';


--
-- Name: COLUMN weather_data.wind_dir; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.wind_dir IS '风向，单位：角度(0-359)，如44表示东北风(44°)';


--
-- Name: COLUMN weather_data.wind_speed_mph; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.wind_speed_mph IS '风速，存储值=实际值×100，单位：英里/小时，查询时需除以100，如100表示1.00mph';


--
-- Name: COLUMN weather_data.wind_gust_mph; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.wind_gust_mph IS '阵风速度，存储值=实际值×100，单位：英里/小时，查询时需除以100，如100表示1.00mph';


--
-- Name: COLUMN weather_data.rain_in; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.rain_in IS '当前降雨速率，存储值=实际值×1000，单位：英寸/小时，查询时需除以1000，如500表示0.500英寸/小时';


--
-- Name: COLUMN weather_data.daily_rain_in; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.daily_rain_in IS '日降雨量，存储值=实际值×1000，单位：英寸，查询时需除以1000，如500表示0.500英寸';


--
-- Name: COLUMN weather_data.weekly_rain_in; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.weekly_rain_in IS '周降雨量，存储值=实际值×1000，单位：英寸，查询时需除以1000，如500表示0.500英寸';


--
-- Name: COLUMN weather_data.monthly_rain_in; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.monthly_rain_in IS '月降雨量，存储值=实际值×1000，单位：英寸，查询时需除以1000，如79表示0.079英寸';


--
-- Name: COLUMN weather_data.yearly_rain_in; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.yearly_rain_in IS '年降雨量，存储值=实际值×1000，单位：英寸，查询时需除以1000，如79表示0.079英寸';


--
-- Name: COLUMN weather_data.total_rain_in; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.total_rain_in IS '总降雨量，存储值=实际值×1000，单位：英寸，查询时需除以1000，如79表示0.079英寸';


--
-- Name: COLUMN weather_data.solar_radiation; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.solar_radiation IS '太阳辐射，存储值=实际值×100，单位：W/m²，查询时需除以100，如172表示1.72W/m²';


--
-- Name: COLUMN weather_data.uv; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.uv IS '紫外线指数，整数值，如0表示无紫外线';


--
-- Name: COLUMN weather_data.indoor_temp_f; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.indoor_temp_f IS '室内温度，存储值=实际值×10，单位：华氏度，查询时需除以10，如837表示83.7°F';


--
-- Name: COLUMN weather_data.indoor_humidity; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.indoor_humidity IS '室内湿度，单位：百分比，如48表示48%';


--
-- Name: COLUMN weather_data.abs_barometer_in; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.abs_barometer_in IS '绝对气压，存储值=实际值×1000，单位：英寸汞柱，查询时需除以1000，如29320表示29.320英寸汞柱';


--
-- Name: COLUMN weather_data.barometer_in; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.barometer_in IS '相对气压，存储值=实际值×1000，单位：英寸汞柱，查询时需除以1000，如29805表示29.805英寸汞柱';


--
-- Name: COLUMN weather_data.low_battery; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.low_battery IS '低电量标志，布尔值，true表示电量低';


--
-- Name: COLUMN weather_data.raw_data; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON COLUMN public.weather_data.raw_data IS '原始数据字符串';


--
-- Name: weather_data_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.weather_data_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: weather_data_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.weather_data_id_seq OWNED BY public.weather_data.id;


--
-- Name: rs485_weather_data id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.rs485_weather_data ALTER COLUMN id SET DEFAULT nextval('public.rs485_weather_data_id_seq'::regclass);


--
-- Name: weather_data id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.weather_data ALTER COLUMN id SET DEFAULT nextval('public.weather_data_id_seq'::regclass);


--
-- Name: rs485_weather_data rs485_udx; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.rs485_weather_data
    ADD CONSTRAINT rs485_udx UNIQUE (station_id, "timestamp");


--
-- Name: rs485_weather_data rs485_weather_data_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.rs485_weather_data
    ADD CONSTRAINT rs485_weather_data_pkey PRIMARY KEY (id);


--
-- Name: stations stations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.stations
    ADD CONSTRAINT stations_pkey PRIMARY KEY (station_id);


--
-- Name: weather_data weather_data_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.weather_data
    ADD CONSTRAINT weather_data_pkey PRIMARY KEY (id);


--
-- Name: idx_rwd_station_time; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_rwd_station_time ON public.rs485_weather_data USING btree (station_id, "timestamp");


--
-- Name: idx_rwd_time; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_rwd_time ON public.rs485_weather_data USING btree ("timestamp");


--
-- Name: idx_weather_data_station_timestamp; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_weather_data_station_timestamp ON public.weather_data USING btree (station_id, "timestamp");


--
-- Name: INDEX idx_weather_data_station_timestamp; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON INDEX public.idx_weather_data_station_timestamp IS '气象站ID和时间戳的复合索引';


--
-- Name: idx_weather_data_timestamp; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_weather_data_timestamp ON public.weather_data USING btree ("timestamp");


--
-- Name: INDEX idx_weather_data_timestamp; Type: COMMENT; Schema: public; Owner: -
--

COMMENT ON INDEX public.idx_weather_data_timestamp IS '时间戳索引';


--
-- Name: rs485_weather_data rs485_weather_data_station_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.rs485_weather_data
    ADD CONSTRAINT rs485_weather_data_station_id_fkey FOREIGN KEY (station_id) REFERENCES public.stations(station_id);


--
-- Name: weather_data weather_data_station_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.weather_data
    ADD CONSTRAINT weather_data_station_id_fkey FOREIGN KEY (station_id) REFERENCES public.stations(station_id);


--
-- PostgreSQL database dump complete
--


-- Name: rs485_weather_10min; Type: TABLE; Schema: public; Owner: -
-- 用途：10分钟粒度聚合（长期保留，缩放整数存储）
--
CREATE TABLE IF NOT EXISTS public.rs485_weather_10min (
    id SERIAL PRIMARY KEY,
    station_id character varying(50) NOT NULL,
    "bucket_start" timestamp with time zone NOT NULL,
    temp_c_x100 integer,
    humidity_pct integer,
    wind_speed_ms_x1000 integer,
    wind_gust_ms_x1000 integer,
    wind_dir_deg integer,
    rain_10m_mm_x1000 integer,
    rain_total_mm_x1000 integer,
    solar_wm2_x100 integer,
    uv_index integer,
    pressure_hpa_x100 integer,
    sample_count integer DEFAULT 0 NOT NULL
);

-- 约束与索引
ALTER TABLE ONLY public.rs485_weather_10min
    ADD CONSTRAINT r10_udx UNIQUE (station_id, "bucket_start");

ALTER TABLE ONLY public.rs485_weather_10min
    ADD CONSTRAINT rs485_weather_10min_station_id_fkey FOREIGN KEY (station_id) REFERENCES public.stations(station_id);

CREATE INDEX idx_r10_station_time ON public.rs485_weather_10min USING btree (station_id, "bucket_start");

COMMENT ON TABLE public.rs485_weather_10min IS '10分钟聚合数据表，数值型以缩放整数存储（温度×100、风速×1000等）';
COMMENT ON COLUMN public.rs485_weather_10min."bucket_start" IS '10分钟桶开始时间（与CST对齐分桶，存储为timestamptz）';
COMMENT ON COLUMN public.rs485_weather_10min.temp_c_x100 IS '10分钟平均温度，单位℃×100';
COMMENT ON COLUMN public.rs485_weather_10min.humidity_pct IS '10分钟平均湿度，单位%';
COMMENT ON COLUMN public.rs485_weather_10min.wind_speed_ms_x1000 IS '10分钟平均风速，单位m/s×1000';
COMMENT ON COLUMN public.rs485_weather_10min.wind_gust_ms_x1000 IS '10分钟最大阵风，单位m/s×1000';
COMMENT ON COLUMN public.rs485_weather_10min.wind_dir_deg IS '10分钟风向向量平均，单位度(0-359)';
COMMENT ON COLUMN public.rs485_weather_10min.rain_10m_mm_x1000 IS '10分钟降雨量，按“带回绕正增量”计算，单位mm×1000';
COMMENT ON COLUMN public.rs485_weather_10min.rain_total_mm_x1000 IS '桶末设备累计降雨（自开机起累加，0..FFFF回绕），单位mm×1000';
COMMENT ON COLUMN public.rs485_weather_10min.solar_wm2_x100 IS '10分钟平均太阳辐射，单位W/m²×100';
COMMENT ON COLUMN public.rs485_weather_10min.uv_index IS '10分钟平均紫外线指数';
COMMENT ON COLUMN public.rs485_weather_10min.pressure_hpa_x100 IS '10分钟平均气压，单位hPa×100';
COMMENT ON COLUMN public.rs485_weather_10min.sample_count IS '10分钟样本数量';


--
-- Name: forecast_hourly; Type: TABLE; Schema: public; Owner: -
-- 用途：小时级预报（版本化：issued_at 为预报方案发布时间）
--
CREATE TABLE IF NOT EXISTS public.forecast_hourly (
    id SERIAL PRIMARY KEY,
    station_id character varying(50) NOT NULL,
    provider character varying(50) NOT NULL,
    issued_at timestamp with time zone NOT NULL,
    forecast_time timestamp with time zone NOT NULL,
    temp_c_x100 integer,
    humidity_pct integer,
    wind_speed_ms_x1000 integer,
    wind_gust_ms_x1000 integer,
    wind_dir_deg integer,
    rain_mm_x1000 integer,
    precip_prob_pct integer,
    uv_index integer,
    pressure_hpa_x100 integer
);

-- 约束与索引
ALTER TABLE ONLY public.forecast_hourly
    ADD CONSTRAINT forecast_hourly_udx UNIQUE (station_id, provider, issued_at, forecast_time);

ALTER TABLE ONLY public.forecast_hourly
    ADD CONSTRAINT forecast_hourly_station_id_fkey FOREIGN KEY (station_id) REFERENCES public.stations(station_id);

CREATE INDEX idx_fcast_station_time ON public.forecast_hourly USING btree (station_id, forecast_time);

-- 注释
COMMENT ON TABLE public.forecast_hourly IS '小时级预报表，按issued_at版本化；要素使用缩放整数存储';
COMMENT ON COLUMN public.forecast_hourly.issued_at IS '预报方案发布时间（版本时间）';
COMMENT ON COLUMN public.forecast_hourly.forecast_time IS '目标小时时间戳';
COMMENT ON COLUMN public.forecast_hourly.rain_mm_x1000 IS '该小时降雨量，单位mm×1000';