CREATE TABLE temperature_reading (
    c_time  int  NOT NULL DEFAULT current_timestamp,
    sensor  text NOT NULL,
    value   int  NOT NULL,
    PRIMARY KEY (c_time, sensor_id)
);

--

CREATE TABLE state_t (
    id   int  PRIMARY KEY,
    name text NOT NULL UNIQUE
);

INSERT INTO state_t (id, name) VALUES
    (0, 'idle'),
    (1, 'cooling'),
    (2, 'heating');

--

CREATE TABLE state_log (
    c_time   int PRIMARY KEY DEFAULT current_timestamp,
    state    int NOT NULL REFERENCES state_t (id)
);

--

CREATE TABLE pid_t (
    id   int  PRIMARY KEY,
    name text NOT NULL UNIQUE
);

INSERT INTO pid_t (id, name) VALUES
    (1, 'main'),
    (2, 'heat');

--

CREATE TABLE pid (
    c_time int  NOT NULL DEFAULT current_timestamp,
    ident  int  NOT NULL REFERENCES pid_ident_t (id),
    p      real NOT NULL,
    i      real NOT NULL,
    d      real NOT NULL,
    PRIMARY KEY (c_time, ident)
);
