-- Table: accelerometer_data

-- DROP TABLE accelerometer_data;

CREATE TABLE accelerometer_data
(
  databaseid integer NOT NULL,
  "timestamp" bigint NOT NULL,
  value_x double precision NOT NULL,
  value_y double precision NOT NULL,
  value_z double precision NOT NULL,
  CONSTRAINT accelerometer_data_pkey PRIMARY KEY ("timestamp")
)
WITH (
  OIDS=FALSE
);
ALTER TABLE accelerometer_data
  OWNER TO postgres;


 -- Table: ap_init

-- DROP TABLE ap_init;

CREATE TABLE ap_init
(
  id serial NOT NULL,
  mac_address macaddr,
  ip_address inet,
  x_poz double precision,
  y_poz double precision,
  z_poz double precision,
  antena_gain integer,
  tx_power integer,
  note text,
  CONSTRAINT ap_init_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE ap_init
  OWNER TO postgres;

-- Trigger: calculate_real_distance on ap_init

-- DROP TRIGGER calculate_real_distance ON ap_init;

CREATE TRIGGER calculate_real_distance
  AFTER INSERT OR UPDATE
  ON ap_init
  FOR EACH ROW
  EXECUTE PROCEDURE calculate_real_distance();
  
  -- Table: ap_rssi_current_copy

-- DROP TABLE ap_rssi_current_copy;

CREATE TABLE ap_rssi_current_copy
(
  id serial NOT NULL,
  "timestamp" bigint NOT NULL,
  source_address macaddr,
  destination_address macaddr,
  rssi_avg double precision,
  eta real,
  distance_real real,
  CONSTRAINT ap_rssi_current_copy_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE ap_rssi_current_copy
  OWNER TO postgres;

  -- Table: ap_rssi_data_copy

-- DROP TABLE ap_rssi_data_copy;

CREATE TABLE ap_rssi_data_copy
(
  id serial NOT NULL,
  "timestamp" bigint NOT NULL,
  source_address macaddr NOT NULL,
  destination_address macaddr NOT NULL,
  essid text,
  rssi double precision NOT NULL,
  CONSTRAINT ap_rssi_data_copy_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE ap_rssi_data_copy
  OWNER TO postgres;

-- Trigger: update_ap_current on ap_rssi_data_copy

-- DROP TRIGGER update_ap_current ON ap_rssi_data_copy;

CREATE TRIGGER update_ap_current
  AFTER INSERT OR UPDATE
  ON ap_rssi_data_copy
  FOR EACH ROW
  EXECUTE PROCEDURE update_ap_current();

   -- Table: mobile_connected_dev

-- DROP TABLE mobile_connected_dev;

CREATE TABLE mobile_connected_dev
(
  id serial NOT NULL,
  "timestamp" bigint NOT NULL,
  device_address_ip inet NOT NULL,
  device_address_mac macaddr NOT NULL,
  hash integer NOT NULL,
  x_real double precision,
  y_real double precision,
  z_real double precision,
  x_0 double precision,
  y_0 double precision,
  z_0 double precision,
  x_1 double precision,
  y_1 double precision,
  z_1 double precision,
  CONSTRAINT mobile_connected_dev_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE mobile_connected_dev
  OWNER TO postgres;

-- Trigger: update_mobile_rssi_table on mobile_connected_dev

-- DROP TRIGGER update_mobile_rssi_table ON mobile_connected_dev;

CREATE TRIGGER update_mobile_rssi_table
  AFTER INSERT OR UPDATE OF device_address_mac, x_real, y_real, z_real
  ON mobile_connected_dev
  FOR EACH ROW
  EXECUTE PROCEDURE update_mobile_rssi_table();

    -- Table: mobile_measure

-- DROP TABLE mobile_measure;

CREATE TABLE mobile_measure
(
  id serial NOT NULL,
  "timestamp" bigint,
  source_address macaddr,
  destination_address macaddr,
  route_rssi double precision,
  route_eta real,
  route_distance real,
  mobile_rssi double precision,
  distance_calc_0 real,
  distance_error_0 real,
  distance_calc_1 real,
  distance_error_1 real,
  CONSTRAINT mobile_measure_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE mobile_measure
  OWNER TO postgres;


  -- Table: mobile_pos_0

-- DROP TABLE mobile_pos_0;

CREATE TABLE mobile_pos_0
(
  id serial NOT NULL,
  "timestamp" bigint,
  mac_address macaddr,
  x_pos double precision,
  y_pos double precision,
  CONSTRAINT mobile_pos_0_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE mobile_pos_0
  OWNER TO postgres;


  -- Table: mobile_pos_1

-- DROP TABLE mobile_pos_1;

CREATE TABLE mobile_pos_1
(
  id serial NOT NULL,
  "timestamp" bigint,
  mac_address macaddr,
  x_pos double precision,
  y_pos double precision,
  CONSTRAINT mobile_pos_1_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE mobile_pos_1
  OWNER TO postgres;



  -- Table: mobile_pos_step

-- DROP TABLE mobile_pos_step;

CREATE TABLE mobile_pos_step
(
  id serial NOT NULL,
  databaseid integer,
  "timestamp" bigint,
  x_pos_step double precision,
  y_pos_step double precision,
  x_pos_int double precision,
  y_pos_int double precision,
  type text,
  CONSTRAINT mobile_pos_step_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE mobile_pos_step
  OWNER TO postgres;



  -- Table: mobile_rssi_current_copy

-- DROP TABLE mobile_rssi_current_copy;

CREATE TABLE mobile_rssi_current_copy
(
  id serial NOT NULL,
  "timestamp" bigint,
  source_address macaddr,
  destination_address macaddr,
  rssi_avg double precision,
  eta real,
  distance_real real,
  distance_calc_0 real,
  distance_error_0 real,
  distance_calc_1 real,
  distance_error_1 real,
  distance_calc_imu_0 real,
  distance_error_imu_0 real,
  distance_calc_imu_1 real,
  distance_error_imu_1 real,
  CONSTRAINT mobile_rssi_current_copy_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE mobile_rssi_current_copy
  OWNER TO postgres;

-- Trigger: calculate_distance_imu_mobile on mobile_rssi_current_copy

-- DROP TRIGGER calculate_distance_imu_mobile ON mobile_rssi_current_copy;

CREATE TRIGGER calculate_distance_imu_mobile
  AFTER UPDATE OF rssi_avg
  ON mobile_rssi_current_copy
  FOR EACH ROW
  EXECUTE PROCEDURE calculate_distance_imu_mobile();

-- Trigger: calculate_distance_mobile on mobile_rssi_current_copy

-- DROP TRIGGER calculate_distance_mobile ON mobile_rssi_current_copy;

CREATE TRIGGER calculate_distance_mobile
  AFTER UPDATE OF rssi_avg
  ON mobile_rssi_current_copy
  FOR EACH ROW
  EXECUTE PROCEDURE calculate_distance_mobile();

-- Trigger: calculate_pos_0_mobile on mobile_rssi_current_copy

-- DROP TRIGGER calculate_pos_0_mobile ON mobile_rssi_current_copy;

CREATE TRIGGER calculate_pos_0_mobile
  AFTER UPDATE OF distance_calc_0
  ON mobile_rssi_current_copy
  FOR EACH STATEMENT
  EXECUTE PROCEDURE calculate_pos_0_mobile();

-- Trigger: calculate_pos_1_mobile on mobile_rssi_current_copy

-- DROP TRIGGER calculate_pos_1_mobile ON mobile_rssi_current_copy;

CREATE TRIGGER calculate_pos_1_mobile
  AFTER UPDATE OF distance_calc_1
  ON mobile_rssi_current_copy
  FOR EACH STATEMENT
  EXECUTE PROCEDURE calculate_pos_1_mobile();

-- Trigger: calculate_pos_imu_1_mobile on mobile_rssi_current_copy

-- DROP TRIGGER calculate_pos_imu_1_mobile ON mobile_rssi_current_copy;

CREATE TRIGGER calculate_pos_imu_1_mobile
  AFTER UPDATE OF distance_calc_imu_1
  ON mobile_rssi_current_copy
  FOR EACH STATEMENT
  EXECUTE PROCEDURE calculate_pos_imu_1_mobile();


  -- Table: mobile_rssi_data_copy

-- DROP TABLE mobile_rssi_data_copy;

CREATE TABLE mobile_rssi_data_copy
(
  id serial NOT NULL,
  "timestamp" bigint NOT NULL,
  databaseid integer NOT NULL,
  source_address macaddr,
  destination_address macaddr NOT NULL,
  rssi double precision NOT NULL,
  essid text,
  CONSTRAINT mobile_rssi_data_copy_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE mobile_rssi_data_copy
  OWNER TO postgres;

-- Trigger: calculate_rssi_eta_mobile on mobile_rssi_data_copy

-- DROP TRIGGER calculate_rssi_eta_mobile ON mobile_rssi_data_copy;

CREATE TRIGGER calculate_rssi_eta_mobile
  AFTER INSERT OR UPDATE
  ON mobile_rssi_data_copy
  FOR EACH ROW
  EXECUTE PROCEDURE calculate_rssi_eta_mobile();

      -- Table: orientation_data

-- DROP TABLE orientation_data;

CREATE TABLE orientation_data
(
  databaseid integer NOT NULL,
  "timestamp" bigint NOT NULL,
  azimuth double precision NOT NULL,
  pitch double precision NOT NULL,
  roll double precision NOT NULL,
  CONSTRAINT orientation_data_pkey PRIMARY KEY ("timestamp")
)
WITH (
  OIDS=FALSE
);
ALTER TABLE orientation_data
  OWNER TO postgres;




