added views per sensor
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 30 Jun 2023 15:47:24 +0000 (17:47 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 30 Jun 2023 15:47:24 +0000 (17:47 +0200)
queries.sql [new file with mode: 0644]
views.sql [new file with mode: 0644]

diff --git a/queries.sql b/queries.sql
new file mode 100644 (file)
index 0000000..221cbbe
--- /dev/null
@@ -0,0 +1,2 @@
+select count(*),min(time),max(time),sensor_type,json->'sensor_name' as sensor_name from eg5120 group by sensor_type,sensor_name;
+
diff --git a/views.sql b/views.sql
new file mode 100644 (file)
index 0000000..8bf643a
--- /dev/null
+++ b/views.sql
@@ -0,0 +1,100 @@
+-- 1 Temperature/Humidity
+drop view s_1;
+create view s_1 as
+select time,
+       addr,
+       (json->'sensor_type')::int  as sensor_type,
+       (json->'sensor_name')::text as sensor_name,
+       (json->'sensor_data'->'temperature')::float as temperature,
+       (json->'sensor_data'->'humidity')::float as humidity,
+       (json->>'battery')::float as bettery,
+       (json->>'battery_percent')::float as battery_percent,
+       (json->'counter')::int as counter
+from eg5120
+where sensor_type = 1;
+;
+grant select on s_1 to gread ;
+drop table sm_1;
+create table sm_1 as select * from s_1;
+grant select on sm_1 to gread ;
+
+-- 2 2 Channel Push Notification
+drop view s_2;
+create view s_2 as
+select time,
+       addr,
+       (json->'sensor_type')::int  as sensor_type,
+       (json->'sensor_name')::text as sensor_name,
+       (json->'sensor_data'->'input_1')::int as input_1,
+       (json->'sensor_data'->'input_2')::int as input_2,
+       (json->>'battery')::float as bettery,
+       (json->>'battery_percent')::float as battery_percent,
+       (json->'counter')::int as counter
+from eg5120
+where sensor_type = 2;
+;
+grant select on s_2 to gread ;
+drop table sm_2;
+create table sm_2 as select * from s_2;
+grant select on sm_2 to gread ;
+
+-- 29 Linear Displacement
+drop view s_29;
+create view s_29 as
+select time,
+       addr,
+       (json->'sensor_type')::int  as sensor_type,
+       (json->'sensor_name')::text as sensor_name,
+       (json->'sensor_data'->'adc')::int as adc,
+       (json->'sensor_data'->'position')::float as position,
+       (json->>'battery')::float as bettery,
+       (json->>'battery_percent')::float as battery_percent,
+       (json->'counter')::int as counter
+from eg5120
+where sensor_type = 29;
+;
+grant select on s_29 to gread ;
+drop table sm_29;
+create table sm_29 as select * from s_29;
+grant select on sm_29 to gread ;
+
+-- 30 Structural Monitoring
+drop view s_30;
+create view s_30 as
+select time,
+       addr,
+       (json->'sensor_type')::int  as sensor_type,
+       (json->'sensor_name')::text as sensor_name,
+       (json->'sensor_data'->'adc')::int as adc,
+       (json->'sensor_data'->'position')::float as position,
+       (json->>'battery')::float as bettery,
+       (json->>'battery_percent')::float as battery_percent,
+       (json->'counter')::int as counter
+from eg5120
+where sensor_type = 30;
+;
+grant select on s_30 to gread ;
+drop table sm_30;
+create table sm_30 as select * from s_30;
+grant select on sm_30 to gread ;
+
+-- 47 Wireless Tilt Sensor
+drop view s_47;
+create view s_47 as
+select time,
+       addr,
+       (json->'sensor_type')::int  as sensor_type,
+       (json->'sensor_name')::text as sensor_name,
+       (json->'sensor_data'->'Roll')::float as Roll,
+       (json->'sensor_data'->'Pitch')::float as Pitch,
+       (json->>'battery')::float as bettery,
+       (json->>'battery_percent')::float as battery_percent,
+       (json->'counter')::int as counter
+from eg5120
+where sensor_type = 29;
+;
+grant select on s_47 to gread ;
+drop table sm_47;
+create table sm_47 as select * from s_47;
+grant select on sm_47 to gread ;
+