-- extend the schema monitoring from badesee application


create table level_sensor_t (
    id serial not null primary key,
    sensor_type varchar(16) not null,
    ground_level numeric(10, 0) not null,
    device integer not null references device_id(id),
    unique(device)
);

create table level_measurement_t (
    time timestamp without time zone not null,
    device_name varchar(16) not null,
    raw_level numeric(10, 0),
    level numeric(10, 0),
    status varchar(16),
    battery float
);

select create_hypertable('level_measurement_t', 'time');

insert into device_t (device_id, label) values('eui-a84041a2c18341d6', 'deilbach');
insert into level_sensor_t (sensor_type, ground_level, device) 
    values('LDDS75', 300, (select id from device_t where device_id='eui-a84041a2c18341d6'));

create user level_preprocessor password 'geheim';
grant select on device_t, level_sensor_t to level_preprocessor;
grant insert on level_measurement_t to level_preprocessor;

