1
0

changed deviceId from Integer to String/CHAR(6)

This commit is contained in:
Jose
2025-05-08 18:23:12 +02:00
parent 5665cb3e5e
commit b6f13cfff8
29 changed files with 445 additions and 248 deletions

102
README.md
View File

@@ -26,8 +26,9 @@ La encontraréis en `dist/`
```sql ```sql
USE dad; USE dad;
DROP TABLE IF EXISTS co_values;
DROP TABLE IF EXISTS weather_values;
DROP TABLE IF EXISTS gps_values; DROP TABLE IF EXISTS gps_values;
DROP TABLE IF EXISTS air_values;
DROP TABLE IF EXISTS actuators; DROP TABLE IF EXISTS actuators;
DROP TABLE IF EXISTS sensors; DROP TABLE IF EXISTS sensors;
DROP TABLE IF EXISTS devices; DROP TABLE IF EXISTS devices;
@@ -39,54 +40,60 @@ CREATE TABLE IF NOT EXISTS groups(
); );
CREATE TABLE IF NOT EXISTS devices( CREATE TABLE IF NOT EXISTS devices(
deviceId INT PRIMARY KEY AUTO_INCREMENT NOT NULL, deviceId CHAR(6) PRIMARY KEY NOT NULL,
groupId INT NOT NULL, groupId INT NOT NULL,
deviceName VARCHAR(64) DEFAULT NULL, deviceName VARCHAR(64) DEFAULT NULL,
FOREIGN KEY (groupId) REFERENCES groups(groupId) FOREIGN KEY (groupId) REFERENCES groups(groupId) ON DELETE CASCADE
); );
CREATE TABLE IF NOT EXISTS sensors( CREATE TABLE IF NOT EXISTS sensors(
sensorId INT PRIMARY KEY AUTO_INCREMENT NOT NULL, sensorId INT NOT NULL,
deviceId INT NOT NULL, deviceId CHAR(6) NOT NULL,
sensorType VARCHAR(64) NOT NULL, sensorType VARCHAR(64) NOT NULL,
unit VARCHAR(8) NOT NULL, unit VARCHAR(8) NOT NULL,
status INT NOT NULL, status INT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP(),
FOREIGN KEY (deviceId) REFERENCES devices(deviceId) PRIMARY KEY (deviceId, sensorId),
FOREIGN KEY (deviceId) REFERENCES devices(deviceId) ON DELETE CASCADE
); );
CREATE TABLE IF NOT EXISTS actuators ( CREATE TABLE IF NOT EXISTS actuators (
actuatorId INT PRIMARY KEY AUTO_INCREMENT NOT NULL, actuatorId INT NOT NULL,
deviceId INT NOT NULL, deviceId CHAR(6) NOT NULL,
status INT NOT NULL, status INT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP(),
FOREIGN KEY (deviceId) REFERENCES devices(deviceId) PRIMARY KEY (deviceId, actuatorId),
FOREIGN KEY (deviceId) REFERENCES devices(deviceId) ON DELETE CASCADE
); );
CREATE TABLE IF NOT EXISTS gps_values( CREATE TABLE IF NOT EXISTS gps_values(
valueId INT PRIMARY KEY AUTO_INCREMENT NOT NULL, valueId INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
deviceId CHAR(6) NOT NULL,
sensorId INT NOT NULL, sensorId INT NOT NULL,
lat FLOAT NOT NULL, lat FLOAT NOT NULL,
lon FLOAT NOT NULL, lon FLOAT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP(),
FOREIGN KEY (sensorId) REFERENCES sensors(sensorId) FOREIGN KEY (deviceId, sensorId) REFERENCES sensors(deviceId, sensorId) ON DELETE CASCADE
); );
CREATE TABLE IF NOT EXISTS weather_values ( CREATE TABLE IF NOT EXISTS weather_values (
valueId INT PRIMARY KEY AUTO_INCREMENT NOT NULL , valueId INT PRIMARY KEY AUTO_INCREMENT NOT NULL ,
deviceId CHAR(6) NOT NULL,
sensorId INT NOT NULL, sensorId INT NOT NULL,
temperature FLOAT NOT NULL, temperature FLOAT NOT NULL,
humidity FLOAT NOT NULL, humidity FLOAT NOT NULL,
pressure FLOAT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP(),
FOREIGN KEY (sensorId) REFERENCES sensors(sensorId) FOREIGN KEY (deviceId, sensorId) REFERENCES sensors(deviceId, sensorId) ON DELETE CASCADE
); );
CREATE TABLE IF NOT EXISTS co_values ( CREATE TABLE IF NOT EXISTS co_values (
valueId INT PRIMARY KEY AUTO_INCREMENT NOT NULL , valueId INT PRIMARY KEY AUTO_INCREMENT NOT NULL ,
deviceId CHAR(6) NOT NULL,
sensorId INT NOT NULL, sensorId INT NOT NULL,
value FLOAT NOT NULL, value FLOAT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP(),
FOREIGN KEY (sensorId) REFERENCES sensors(sensorId) FOREIGN KEY (deviceId, sensorId) REFERENCES sensors(deviceId, sensorId) ON DELETE CASCADE
); );
CREATE OR REPLACE VIEW v_sensor_values AS CREATE OR REPLACE VIEW v_sensor_values AS
@@ -98,14 +105,15 @@ SELECT
s.status AS sensorStatus, s.status AS sensorStatus,
wv.temperature, wv.temperature,
wv.humidity, wv.humidity,
wv.pressure,
cv.value AS carbonMonoxide, cv.value AS carbonMonoxide,
gv.lat, gv.lat,
gv.lon, gv.lon,
COALESCE(gv.timestamp, wv.timestamp, cv.timestamp) AS timestamp -- el primero no nulo COALESCE(gv.timestamp, wv.timestamp, cv.timestamp) AS timestamp -- el primero no nulo
FROM sensors s FROM sensors s
LEFT JOIN weather_values wv ON s.sensorId = wv.sensorId LEFT JOIN weather_values wv ON s.deviceId = wv.deviceId AND s.sensorId = wv.sensorId
LEFT JOIN co_values cv ON s.sensorId = cv.sensorId LEFT JOIN co_values cv ON s.deviceId = cv.deviceId AND s.sensorId = cv.sensorId
LEFT JOIN gps_values gv ON s.sensorId = gv.sensorId; LEFT JOIN gps_values gv ON s.deviceId = gv.deviceId AND s.sensorId = gv.sensorId;
CREATE OR REPLACE VIEW v_latest_values AS CREATE OR REPLACE VIEW v_latest_values AS
@@ -118,14 +126,15 @@ SELECT
s.timestamp AS sensorTimestamp, s.timestamp AS sensorTimestamp,
wv.temperature, wv.temperature,
wv.humidity, wv.humidity,
wv.pressure,
cv.value AS carbonMonoxide, cv.value AS carbonMonoxide,
gv.lat, gv.lat,
gv.lon, gv.lon,
COALESCE(gv.timestamp, wv.timestamp, cv.timestamp) AS airValuesTimestamp -- el primero no nulo COALESCE(gv.timestamp, wv.timestamp, cv.timestamp) AS airValuesTimestamp -- el primero no nulo
FROM sensors s FROM sensors s
LEFT JOIN weather_values wv ON s.sensorId = wv.sensorId LEFT JOIN weather_values wv ON s.deviceId = wv.deviceId AND s.sensorId = wv.sensorId
LEFT JOIN co_values cv ON s.sensorId = cv.sensorId LEFT JOIN co_values cv ON s.deviceId = cv.deviceId AND s.sensorId = cv.sensorId
LEFT JOIN gps_values gv ON s.sensorId = gv.sensorId LEFT JOIN gps_values gv ON s.deviceId = gv.deviceId AND s.sensorId = gv.sensorId
WHERE (wv.timestamp = (SELECT MAX(timestamp) FROM weather_values WHERE sensorId = s.sensorId) WHERE (wv.timestamp = (SELECT MAX(timestamp) FROM weather_values WHERE sensorId = s.sensorId)
OR cv.timestamp = (SELECT MAX(timestamp) FROM co_values WHERE sensorId = s.sensorId) OR cv.timestamp = (SELECT MAX(timestamp) FROM co_values WHERE sensorId = s.sensorId)
OR gv.timestamp = (SELECT MAX(timestamp) FROM gps_values WHERE sensorId = s.sensorId)); OR gv.timestamp = (SELECT MAX(timestamp) FROM gps_values WHERE sensorId = s.sensorId));
@@ -138,7 +147,7 @@ SELECT
c.value AS carbonMonoxide, c.value AS carbonMonoxide,
c.timestamp c.timestamp
FROM sensors s FROM sensors s
JOIN co_values c ON s.sensorId = c.sensorId JOIN co_values c ON s.deviceId = c.deviceId AND s.sensorId = c.sensorId
WHERE s.sensorType = 'CO'; WHERE s.sensorType = 'CO';
CREATE OR REPLACE VIEW v_gps_by_device AS CREATE OR REPLACE VIEW v_gps_by_device AS
@@ -148,7 +157,7 @@ SELECT
g.lon, g.lon,
g.timestamp g.timestamp
FROM sensors s FROM sensors s
JOIN gps_values g ON s.sensorId = g.sensorId JOIN gps_values g ON s.deviceId = g.deviceId AND s.sensorId = g.sensorId
WHERE s.sensorType = 'GPS'; WHERE s.sensorType = 'GPS';
CREATE OR REPLACE VIEW v_weather_by_device AS CREATE OR REPLACE VIEW v_weather_by_device AS
@@ -156,9 +165,10 @@ SELECT
s.deviceId, s.deviceId,
w.temperature AS temperature, w.temperature AS temperature,
w.humidity AS humidity, w.humidity AS humidity,
w.pressure AS pressure,
w.timestamp w.timestamp
FROM sensors s FROM sensors s
JOIN weather_values w ON s.sensorId = w.sensorId JOIN weather_values w ON s.deviceId = w.deviceId AND s.sensorId = w.sensorId
WHERE s.sensorType = 'Temperature & Humidity'; WHERE s.sensorType = 'Temperature & Humidity';
-- VISTAS AUXILIARES -- VISTAS AUXILIARES
@@ -210,46 +220,16 @@ ORDER BY deviceId, timestamp;
INSERT INTO groups (groupName) VALUES ('Grupo 1'); INSERT INTO groups (groupName) VALUES ('Grupo 1');
-- Insertar dispositivos -- Insertar dispositivos
INSERT INTO devices (groupId, deviceName) VALUES INSERT INTO devices (deviceId, groupId, deviceName) VALUES
(1, 'Dispositivo 1'), ('6A6098', 1, 'Dispositivo 1');
(1, 'Dispositivo 2'),
(1, 'Dispositivo 3');
-- Sensores para el Dispositivo 1 -- Sensores para el Dispositivo 6A6098
-- Cada dispositivo tiene un único sensor de cada tipo (GPS, Temperature & Humidity, CO) INSERT INTO sensors (sensorId, deviceId, sensorType, unit, status) VALUES
INSERT INTO sensors (deviceId, sensorType, unit, status) VALUES (1, '6A6098', 'GPS', 'N/A', 1),
(1, 'GPS', 'N/A', 1), -- Sensor de GPS para Dispositivo 1 (2, '6A6098', 'Temperature & Humidity', '°C/%', 1),
(1, 'Temperature & Humidity', '°C/%', 1), -- Sensor de Temp/Humidity para Dispositivo 1 (3, '6A6098', 'CO', 'ppm', 1);
(1, 'CO', 'ppm', 1); -- Sensor de CO para Dispositivo 1
-- Sensores para el Dispositivo 2 -- ACtuadores para el Dispositivo 6A6098
INSERT INTO sensors (deviceId, sensorType, unit, status) VALUES INSERT INTO actuators (actuatorId, deviceId, status, timestamp) VALUES
(2, 'GPS', 'N/A', 1), -- Sensor de GPS para Dispositivo 2 (1, '6A6098', 1, CURRENT_TIMESTAMP());
(2, 'Temperature & Humidity', '°C/%', 1), -- Sensor de Temp/Humidity para Dispositivo 2
(2, 'CO', 'ppm', 1); -- Sensor de CO para Dispositivo 2
-- Sensores para el Dispositivo 3
INSERT INTO sensors (deviceId, sensorType, unit, status) VALUES
(3, 'GPS', 'N/A', 1), -- Sensor de GPS para Dispositivo 3
(3, 'Temperature & Humidity', '°C/%', 1), -- Sensor de Temp/Humidity para Dispositivo 3
(3, 'CO', 'ppm', 1); -- Sensor de CO para Dispositivo 3
-- Valores de GPS para los Dispositivos
-- Cada dispositivo tiene un único sensor de GPS con latitud y longitud asociada
INSERT INTO gps_values (sensorId, lat, lon) VALUES
(1, 37.3861, -5.9921), -- GPS para Dispositivo 1
(4, 37.3850, -5.9910), -- GPS para Dispositivo 2
(7, 37.3860, -5.9920); -- GPS para Dispositivo 3
-- Valores de Temperatura, Humedad y CO para los Dispositivos
-- Cada dispositivo tiene un único sensor de aire (temperatura, humedad, CO) con valores asociados
INSERT INTO weather_values (sensorId, temperature, humidity) VALUES
(2, 22.5, 45.0), -- Temperatura, Humedad para Dispositivo 1
(5, 24.5, 50.0), -- Temperatura, Humedad para Dispositivo 2
(8, 21.0, 44.0); -- Temperatura, Humedad para Dispositivo 3
INSERT INTO co_values (sensorId, value) VALUES
(3, 0.02), -- CO para Dispositivo 1
(6, 0.04), -- CO para Dispositivo 2
(9, 0.01); -- CO para Dispositivo 3
``` ```

View File

@@ -11,6 +11,8 @@ public class Constants {
/* API Endpoints */ /* API Endpoints */
public static final String POST_PAYLOAD = RAW_API_PREFIX + "/device-payload";
public static final String GET_GROUPS = RAW_API_PREFIX + "/groups"; public static final String GET_GROUPS = RAW_API_PREFIX + "/groups";
public static final String POST_GROUPS = RAW_API_PREFIX + "/groups"; public static final String POST_GROUPS = RAW_API_PREFIX + "/groups";
public static final String PUT_GROUP_BY_ID = RAW_API_PREFIX + "/groups/:groupId"; public static final String PUT_GROUP_BY_ID = RAW_API_PREFIX + "/groups/:groupId";

View File

@@ -10,7 +10,7 @@ import net.miarma.contaminus.util.DateParser;
public class Actuator { public class Actuator {
private Integer actuatorId; private Integer actuatorId;
private Integer deviceId; private String deviceId;
private Integer status; private Integer status;
private Long timestamp; private Long timestamp;
@@ -18,12 +18,12 @@ public class Actuator {
public Actuator(Row row) { public Actuator(Row row) {
this.actuatorId = row.getInteger("actuatorId"); this.actuatorId = row.getInteger("actuatorId");
this.deviceId = row.getInteger("deviceId"); this.deviceId = row.getString("deviceId");
this.status = row.getInteger("status"); this.status = row.getInteger("status");
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public Actuator(Integer actuatorId, Integer deviceId, Integer status, Long timestamp) { public Actuator(Integer actuatorId, String deviceId, Integer status, Long timestamp) {
super(); super();
this.actuatorId = actuatorId; this.actuatorId = actuatorId;
this.deviceId = deviceId; this.deviceId = deviceId;
@@ -39,11 +39,11 @@ public class Actuator {
this.actuatorId = actuatorId; this.actuatorId = actuatorId;
} }
public Integer getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
} }
public void setDeviceId(Integer deviceId) { public void setDeviceId(String deviceId) {
this.deviceId = deviceId; this.deviceId = deviceId;
} }

View File

@@ -10,6 +10,7 @@ import net.miarma.contaminus.util.DateParser;
public class COValue { public class COValue {
private Integer valueId; private Integer valueId;
private String deviceId;
private Integer sensorId; private Integer sensorId;
private Float value; private Float value;
private Long timestamp; private Long timestamp;
@@ -18,14 +19,16 @@ public class COValue {
public COValue(Row row) { public COValue(Row row) {
this.valueId = row.getInteger("valueId"); this.valueId = row.getInteger("valueId");
this.deviceId = row.getString("deviceId");
this.sensorId = row.getInteger("sensorId"); this.sensorId = row.getInteger("sensorId");
this.value = row.getFloat("value"); this.value = row.getFloat("value");
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public COValue(Integer valueId, Integer sensorId, Float value, Long timestamp) { public COValue(Integer valueId, String deviceId, Integer sensorId, Float value, Long timestamp) {
super(); super();
this.valueId = valueId; this.valueId = valueId;
this.deviceId = deviceId;
this.sensorId = sensorId; this.sensorId = sensorId;
this.value = value; this.value = value;
this.timestamp = timestamp; this.timestamp = timestamp;
@@ -39,6 +42,14 @@ public class COValue {
this.valueId = valueId; this.valueId = valueId;
} }
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public Integer getSensorId() { public Integer getSensorId() {
return sensorId; return sensorId;
} }
@@ -65,7 +76,7 @@ public class COValue {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(sensorId, timestamp, value, valueId); return Objects.hash(deviceId, sensorId, timestamp, value, valueId);
} }
@Override @Override
@@ -77,17 +88,20 @@ public class COValue {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
COValue other = (COValue) obj; COValue other = (COValue) obj;
return Objects.equals(sensorId, other.sensorId) && Objects.equals(timestamp, other.timestamp) return Objects.equals(deviceId, other.deviceId) && Objects.equals(sensorId, other.sensorId)
&& Objects.equals(value, other.value) && Objects.equals(valueId, other.valueId); && Objects.equals(timestamp, other.timestamp) && Objects.equals(value, other.value)
&& Objects.equals(valueId, other.valueId);
} }
@Override @Override
public String toString() { public String toString() {
return "COValue [valueId=" + valueId + ", sensorId=" + sensorId + ", value=" + value + ", timestamp=" return "COValue [valueId=" + valueId + ", deviceId=" + deviceId + ", sensorId=" + sensorId + ", value=" + value
+ timestamp + "]"; + ", timestamp=" + timestamp + "]";
}
public static COValue fromPayload(DevicePayload payload) {
return new COValue(null, payload.getDeviceId(), payload.getSensorId(), payload.getCarbonMonoxide(), payload.getTimestamp());
} }
} }

View File

@@ -6,30 +6,30 @@ import net.miarma.contaminus.common.Table;
@Table("devices") @Table("devices")
public class Device { public class Device {
private Integer deviceId; private String deviceId;
private Integer groupId; private Integer groupId;
private String deviceName; private String deviceName;
public Device() {} public Device() {}
public Device(Row row) { public Device(Row row) {
this.deviceId = row.getInteger("deviceId"); this.deviceId = row.getString("deviceId");
this.groupId = row.getInteger("groupId"); this.groupId = row.getInteger("groupId");
this.deviceName = row.getString("deviceName"); this.deviceName = row.getString("deviceName");
} }
public Device(Integer deviceId, Integer groupId, String deviceName) { public Device(String deviceId, Integer groupId, String deviceName) {
super(); super();
this.deviceId = deviceId; this.deviceId = deviceId;
this.groupId = groupId; this.groupId = groupId;
this.deviceName = deviceName; this.deviceName = deviceName;
} }
public Integer getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
} }
public void setDeviceId(Integer deviceId) { public void setDeviceId(String deviceId) {
this.deviceId = deviceId; this.deviceId = deviceId;
} }

View File

@@ -8,26 +8,26 @@ import net.miarma.contaminus.util.DateParser;
@Table("v_co_by_device") @Table("v_co_by_device")
public class DeviceCO { public class DeviceCO {
private Integer deviceId; private String deviceId;
private Float carbonMonoxide; private Float carbonMonoxide;
private Long timestamp; private Long timestamp;
public DeviceCO() {} public DeviceCO() {}
public DeviceCO(Row row) { public DeviceCO(Row row) {
this.deviceId = row.getInteger("deviceId"); this.deviceId = row.getString("deviceId");
this.carbonMonoxide = row.getFloat("carbonMonoxide"); this.carbonMonoxide = row.getFloat("carbonMonoxide");
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public DeviceCO(Integer deviceId, Float carbonMonoxide, Long timestamp) { public DeviceCO(String deviceId, Float carbonMonoxide, Long timestamp) {
super(); super();
this.deviceId = deviceId; this.deviceId = deviceId;
this.carbonMonoxide = carbonMonoxide; this.carbonMonoxide = carbonMonoxide;
this.timestamp = timestamp; this.timestamp = timestamp;
} }
public Integer getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
} }

View File

@@ -8,7 +8,7 @@ import net.miarma.contaminus.util.DateParser;
@Table("v_gps_by_device") @Table("v_gps_by_device")
public class DeviceGPS { public class DeviceGPS {
private Integer deviceId; private String deviceId;
private Float lat; private Float lat;
private Float lon; private Float lon;
private Long timestamp; private Long timestamp;
@@ -16,20 +16,20 @@ public class DeviceGPS {
public DeviceGPS() {} public DeviceGPS() {}
public DeviceGPS(Row row) { public DeviceGPS(Row row) {
this.deviceId = row.getInteger("deviceId"); this.deviceId = row.getString("deviceId");
this.lat = row.getFloat("lat"); this.lat = row.getFloat("lat");
this.lon = row.getFloat("lon"); this.lon = row.getFloat("lon");
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public DeviceGPS(Integer deviceId, Float lat, Float lon) { public DeviceGPS(String deviceId, Float lat, Float lon) {
super(); super();
this.deviceId = deviceId; this.deviceId = deviceId;
this.lat = lat; this.lat = lat;
this.lon = lon; this.lon = lon;
} }
public Integer getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
} }

View File

@@ -7,7 +7,7 @@ import net.miarma.contaminus.util.DateParser;
@Table("v_latest_values") @Table("v_latest_values")
public class DeviceLatestValuesView { public class DeviceLatestValuesView {
private Integer deviceId; private String deviceId;
private Integer sensorId; private Integer sensorId;
private String sensorType; private String sensorType;
private String unit; private String unit;
@@ -23,7 +23,7 @@ public class DeviceLatestValuesView {
public DeviceLatestValuesView() {} public DeviceLatestValuesView() {}
public DeviceLatestValuesView(Row row) { public DeviceLatestValuesView(Row row) {
this.deviceId = row.getInteger("deviceId"); this.deviceId = row.getString("deviceId");
this.sensorId = row.getInteger("sensorId"); this.sensorId = row.getInteger("sensorId");
this.sensorType = row.getString("sensorType"); this.sensorType = row.getString("sensorType");
this.unit = row.getString("unit"); this.unit = row.getString("unit");
@@ -37,7 +37,7 @@ public class DeviceLatestValuesView {
this.airValuesTimestamp = DateParser.parseDate(row.getLocalDateTime("airValuesTimestamp")); this.airValuesTimestamp = DateParser.parseDate(row.getLocalDateTime("airValuesTimestamp"));
} }
public DeviceLatestValuesView(Integer deviceId, Integer sensorId, String sensorType, String unit, Integer sensorStatus, public DeviceLatestValuesView(String deviceId, Integer sensorId, String sensorType, String unit, Integer sensorStatus,
Long sensorTimestamp, Float temperature, Float humidity, Float carbonMonoxide, Float lat, Float lon, Long sensorTimestamp, Float temperature, Float humidity, Float carbonMonoxide, Float lat, Float lon,
Long airValuesTimestamp) { Long airValuesTimestamp) {
super(); super();
@@ -55,7 +55,7 @@ public class DeviceLatestValuesView {
this.airValuesTimestamp = airValuesTimestamp; this.airValuesTimestamp = airValuesTimestamp;
} }
public Integer getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
} }

View File

@@ -0,0 +1,130 @@
package net.miarma.contaminus.database.entities;
import java.util.Objects;
public class DevicePayload {
private String deviceId;
private Integer sensorId;
private Float temperature;
private Float humidity;
private Float pressure;
private Float carbonMonoxide;
private Float lat;
private Float lon;
private Long timestamp;
public DevicePayload() {}
public DevicePayload(String deviceId, Integer sensorId, String sensorType, String unit, Integer sensorStatus,
Float temperature, Float humidity, Float pressure, Float carbonMonoxide, Float lat, Float lon, Long timestamp) {
super();
this.deviceId = deviceId;
this.sensorId = sensorId;
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
this.carbonMonoxide = carbonMonoxide;
this.lat = lat;
this.lon = lon;
this.timestamp = timestamp;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public Integer getSensorId() {
return sensorId;
}
public void setSensorId(Integer sensorId) {
this.sensorId = sensorId;
}
public Float getTemperature() {
return temperature;
}
public void setTemperature(Float temperature) {
this.temperature = temperature;
}
public Float getHumidity() {
return humidity;
}
public void setHumidity(Float humidity) {
this.humidity = humidity;
}
public Float getPressure() {
return pressure;
}
public Float getCarbonMonoxide() {
return carbonMonoxide;
}
public void setCarbonMonoxide(Float carbonMonoxide) {
this.carbonMonoxide = carbonMonoxide;
}
public Float getLat() {
return lat;
}
public void setLat(Float lat) {
this.lat = lat;
}
public Float getLon() {
return lon;
}
public void setLon(Float lon) {
this.lon = lon;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
@Override
public int hashCode() {
return Objects.hash(carbonMonoxide, deviceId, humidity, lat, lon, pressure, sensorId, temperature, timestamp);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DevicePayload other = (DevicePayload) obj;
return Objects.equals(carbonMonoxide, other.carbonMonoxide) && Objects.equals(deviceId, other.deviceId)
&& Objects.equals(humidity, other.humidity) && Objects.equals(lat, other.lat)
&& Objects.equals(lon, other.lon) && Objects.equals(pressure, other.pressure)
&& Objects.equals(sensorId, other.sensorId) && Objects.equals(temperature, other.temperature)
&& Objects.equals(timestamp, other.timestamp);
}
@Override
public String toString() {
return "DevicePayload [deviceId=" + deviceId + ", sensorId=" + sensorId + ", temperature=" + temperature
+ ", humidity=" + humidity + ", pressure=" + pressure + ", carbonMonoxide=" + carbonMonoxide + ", lat="
+ lat + ", lon=" + lon + ", timestamp=" + timestamp + "]";
}
}

View File

@@ -8,7 +8,7 @@ import net.miarma.contaminus.util.DateParser;
@Table("v_pollution_map") @Table("v_pollution_map")
public class DevicePollutionMap { public class DevicePollutionMap {
private Integer deviceId; private String deviceId;
private String deviceName; private String deviceName;
private Float lat; private Float lat;
private Float lon; private Float lon;
@@ -18,7 +18,7 @@ public class DevicePollutionMap {
public DevicePollutionMap() {} public DevicePollutionMap() {}
public DevicePollutionMap(Row row) { public DevicePollutionMap(Row row) {
this.deviceId = row.getInteger("deviceId"); this.deviceId = row.getString("deviceId");
this.deviceName = row.getString("deviceName"); this.deviceName = row.getString("deviceName");
this.lat = row.getFloat("lat"); this.lat = row.getFloat("lat");
this.lon = row.getFloat("lon"); this.lon = row.getFloat("lon");
@@ -26,7 +26,7 @@ public class DevicePollutionMap {
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public DevicePollutionMap(Integer deviceId, String deviceName, Float lat, Float lon, Float carbonMonoxide, public DevicePollutionMap(String deviceId, String deviceName, Float lat, Float lon, Float carbonMonoxide,
Long timestamp) { Long timestamp) {
super(); super();
this.deviceId = deviceId; this.deviceId = deviceId;
@@ -37,7 +37,7 @@ public class DevicePollutionMap {
this.timestamp = timestamp; this.timestamp = timestamp;
} }
public Integer getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
} }

View File

@@ -8,7 +8,7 @@ import net.miarma.contaminus.util.DateParser;
@Table("v_sensor_history_by_device") @Table("v_sensor_history_by_device")
public class DeviceSensorHistory { public class DeviceSensorHistory {
private Integer deviceId; private String deviceId;
private String deviceName; private String deviceName;
private Float value; private Float value;
private String valueType; private String valueType;
@@ -17,14 +17,14 @@ public class DeviceSensorHistory {
public DeviceSensorHistory() {} public DeviceSensorHistory() {}
public DeviceSensorHistory(Row row) { public DeviceSensorHistory(Row row) {
this.deviceId = row.getInteger("deviceId"); this.deviceId = row.getString("deviceId");
this.deviceName = row.getString("deviceName"); this.deviceName = row.getString("deviceName");
this.value = row.getFloat("value"); this.value = row.getFloat("value");
this.valueType = row.getString("valueType"); this.valueType = row.getString("valueType");
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public DeviceSensorHistory(Integer deviceId, String deviceName, Float value, String valueType, Long timestamp) { public DeviceSensorHistory(String deviceId, String deviceName, Float value, String valueType, Long timestamp) {
super(); super();
this.deviceId = deviceId; this.deviceId = deviceId;
this.deviceName = deviceName; this.deviceName = deviceName;
@@ -33,7 +33,7 @@ public class DeviceSensorHistory {
this.timestamp = timestamp; this.timestamp = timestamp;
} }
public Integer getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
} }

View File

@@ -9,7 +9,7 @@ import net.miarma.contaminus.util.DateParser;
@Table("v_sensor_values") @Table("v_sensor_values")
public class DeviceSensorValue { public class DeviceSensorValue {
private Integer sensorId; private Integer sensorId;
private Integer deviceId; private String deviceId;
private String sensorType; private String sensorType;
private String unit; private String unit;
private Integer sensorStatus; private Integer sensorStatus;
@@ -24,7 +24,7 @@ public class DeviceSensorValue {
public DeviceSensorValue(Row row) { public DeviceSensorValue(Row row) {
this.sensorId = row.getInteger("sensorId"); this.sensorId = row.getInteger("sensorId");
this.deviceId = row.getInteger("deviceId"); this.deviceId = row.getString("deviceId");
this.sensorType = row.getString("sensorType"); this.sensorType = row.getString("sensorType");
this.unit = row.getString("unit"); this.unit = row.getString("unit");
this.sensorStatus = row.getInteger("sensorStatus"); this.sensorStatus = row.getInteger("sensorStatus");
@@ -36,7 +36,7 @@ public class DeviceSensorValue {
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public DeviceSensorValue(Integer sensorId, Integer deviceId, String sensorType, String unit, Integer sensorStatus, public DeviceSensorValue(Integer sensorId, String deviceId, String sensorType, String unit, Integer sensorStatus,
Float temperature, Float humidity, Float carbonMonoxide, Float lat, Float lon, Long timestamp) { Float temperature, Float humidity, Float carbonMonoxide, Float lat, Float lon, Long timestamp) {
super(); super();
this.sensorId = sensorId; this.sensorId = sensorId;
@@ -56,7 +56,7 @@ public class DeviceSensorValue {
return sensorId; return sensorId;
} }
public Integer getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
} }

View File

@@ -8,7 +8,7 @@ import net.miarma.contaminus.util.DateParser;
@Table("v_weather_by_device") @Table("v_weather_by_device")
public class DeviceWeather { public class DeviceWeather {
private Integer deviceId; private String deviceId;
private Float temperature; private Float temperature;
private Float humidity; private Float humidity;
private Long timestamp; private Long timestamp;
@@ -16,13 +16,13 @@ public class DeviceWeather {
public DeviceWeather() {} public DeviceWeather() {}
public DeviceWeather(Row row) { public DeviceWeather(Row row) {
this.deviceId = row.getInteger("deviceId"); this.deviceId = row.getString("deviceId");
this.temperature = row.getFloat("temperature"); this.temperature = row.getFloat("temperature");
this.humidity = row.getFloat("humidity"); this.humidity = row.getFloat("humidity");
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public DeviceWeather(Integer deviceId, Float temperature, Float humidity, Long timestamp) { public DeviceWeather(String deviceId, Float temperature, Float humidity, Long timestamp) {
super(); super();
this.deviceId = deviceId; this.deviceId = deviceId;
this.temperature = temperature; this.temperature = temperature;
@@ -30,7 +30,7 @@ public class DeviceWeather {
this.timestamp = timestamp; this.timestamp = timestamp;
} }
public Integer getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
} }

View File

@@ -10,6 +10,7 @@ import net.miarma.contaminus.util.DateParser;
public class GpsValue { public class GpsValue {
private Integer valueId; private Integer valueId;
private String deviceId;
private Integer sensorId; private Integer sensorId;
private Float lat; private Float lat;
private Float lon; private Float lon;
@@ -19,15 +20,17 @@ public class GpsValue {
public GpsValue(Row row) { public GpsValue(Row row) {
this.valueId = row.getInteger("valueId"); this.valueId = row.getInteger("valueId");
this.deviceId = row.getString("deviceId");
this.sensorId = row.getInteger("sensorId"); this.sensorId = row.getInteger("sensorId");
this.lat = row.getFloat("lat"); this.lat = row.getFloat("lat");
this.lon = row.getFloat("lon"); this.lon = row.getFloat("lon");
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public GpsValue(Integer valueId, Integer sensorId, Float lat, Float lon, Long timestamp) { public GpsValue(Integer valueId, String deviceId, Integer sensorId, Float lat, Float lon, Long timestamp) {
super(); super();
this.valueId = valueId; this.valueId = valueId;
this.deviceId = deviceId;
this.sensorId = sensorId; this.sensorId = sensorId;
this.lat = lat; this.lat = lat;
this.lon = lon; this.lon = lon;
@@ -42,6 +45,14 @@ public class GpsValue {
this.valueId = valueId; this.valueId = valueId;
} }
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public Integer getSensorId() { public Integer getSensorId() {
return sensorId; return sensorId;
} }
@@ -76,7 +87,7 @@ public class GpsValue {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(lat, lon, sensorId, timestamp, valueId); return Objects.hash(deviceId, lat, lon, sensorId, timestamp, valueId);
} }
@Override @Override
@@ -88,17 +99,20 @@ public class GpsValue {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
GpsValue other = (GpsValue) obj; GpsValue other = (GpsValue) obj;
return Objects.equals(lat, other.lat) && Objects.equals(lon, other.lon) return Objects.equals(deviceId, other.deviceId) && Objects.equals(lat, other.lat)
&& Objects.equals(sensorId, other.sensorId) && Objects.equals(timestamp, other.timestamp) && Objects.equals(lon, other.lon) && Objects.equals(sensorId, other.sensorId)
&& Objects.equals(valueId, other.valueId); && Objects.equals(timestamp, other.timestamp) && Objects.equals(valueId, other.valueId);
} }
@Override @Override
public String toString() { public String toString() {
return "GpsValue [valueId=" + valueId + ", sensorId=" + sensorId + ", lat=" + lat + ", lon=" + lon return "GpsValue [valueId=" + valueId + ", deviceId=" + deviceId + ", sensorId=" + sensorId + ", lat=" + lat
+ ", timestamp=" + timestamp + "]"; + ", lon=" + lon + ", timestamp=" + timestamp + "]";
} }
public static GpsValue fromPayload(DevicePayload payload) {
return new GpsValue(null, payload.getDeviceId(), payload.getSensorId(), payload.getLat(), payload.getLon(),
payload.getTimestamp());
}
} }

View File

@@ -10,7 +10,7 @@ import net.miarma.contaminus.util.DateParser;
public class Sensor { public class Sensor {
private Integer sensorId; private Integer sensorId;
private Integer deviceId; private String deviceId;
private String sensorType; private String sensorType;
private String unit; private String unit;
private Integer status; private Integer status;
@@ -20,14 +20,14 @@ public class Sensor {
public Sensor(Row row) { public Sensor(Row row) {
this.sensorId = row.getInteger("sensorId"); this.sensorId = row.getInteger("sensorId");
this.deviceId = row.getInteger("deviceId"); this.deviceId = row.getString("deviceId");
this.sensorType = row.getString("sensorType"); this.sensorType = row.getString("sensorType");
this.unit = row.getString("unit"); this.unit = row.getString("unit");
this.status = row.getInteger("status"); this.status = row.getInteger("status");
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public Sensor(Integer sensorId, Integer deviceId, String sensorType, String unit, Integer status, Long timestamp) { public Sensor(Integer sensorId, String deviceId, String sensorType, String unit, Integer status, Long timestamp) {
super(); super();
this.sensorId = sensorId; this.sensorId = sensorId;
this.deviceId = deviceId; this.deviceId = deviceId;
@@ -45,11 +45,11 @@ public class Sensor {
this.sensorId = sensorId; this.sensorId = sensorId;
} }
public Integer getDeviceId() { public String getDeviceId() {
return deviceId; return deviceId;
} }
public void setDeviceId(Integer deviceId) { public void setDeviceId(String deviceId) {
this.deviceId = deviceId; this.deviceId = deviceId;
} }

View File

@@ -10,27 +10,33 @@ import net.miarma.contaminus.util.DateParser;
public class WeatherValue { public class WeatherValue {
private Integer valueId; private Integer valueId;
private String deviceId;
private Integer sensorId; private Integer sensorId;
private Float temperature; private Float temperature;
private Float humidity; private Float humidity;
private Float pressure;
private Long timestamp; private Long timestamp;
public WeatherValue() {} public WeatherValue() {}
public WeatherValue(Row row) { public WeatherValue(Row row) {
this.valueId = row.getInteger("valueId"); this.valueId = row.getInteger("valueId");
this.deviceId = row.getString("deviceId");
this.sensorId = row.getInteger("sensorId"); this.sensorId = row.getInteger("sensorId");
this.temperature = row.getFloat("temperature"); this.temperature = row.getFloat("temperature");
this.humidity = row.getFloat("humidity"); this.humidity = row.getFloat("humidity");
this.pressure = row.getFloat("pressure");
this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp")); this.timestamp = DateParser.parseDate(row.getLocalDateTime("timestamp"));
} }
public WeatherValue(Integer valueId, Integer sensorId, Float temperature, Float humidity, Long timestamp) { public WeatherValue(Integer valueId, String deviceId, Integer sensorId, Float temperature, Float humidity, Float pressure, Long timestamp) {
super(); super();
this.valueId = valueId; this.valueId = valueId;
this.deviceId = deviceId;
this.sensorId = sensorId; this.sensorId = sensorId;
this.temperature = temperature; this.temperature = temperature;
this.humidity = humidity; this.humidity = humidity;
this.pressure = pressure;
this.timestamp = timestamp; this.timestamp = timestamp;
} }
@@ -42,6 +48,14 @@ public class WeatherValue {
this.valueId = valueId; this.valueId = valueId;
} }
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public Integer getSensorId() { public Integer getSensorId() {
return sensorId; return sensorId;
} }
@@ -66,6 +80,14 @@ public class WeatherValue {
this.humidity = humidity; this.humidity = humidity;
} }
public Float getPressure() {
return pressure;
}
public void setPressure(Float pressure) {
this.pressure = pressure;
}
public Long getTimestamp() { public Long getTimestamp() {
return timestamp; return timestamp;
} }
@@ -76,7 +98,7 @@ public class WeatherValue {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(humidity, sensorId, temperature, timestamp, valueId); return Objects.hash(deviceId, humidity, pressure, sensorId, temperature, timestamp, valueId);
} }
@Override @Override
@@ -88,17 +110,22 @@ public class WeatherValue {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
WeatherValue other = (WeatherValue) obj; WeatherValue other = (WeatherValue) obj;
return Objects.equals(humidity, other.humidity) && Objects.equals(sensorId, other.sensorId) return Objects.equals(deviceId, other.deviceId) && Objects.equals(humidity, other.humidity)
&& Objects.equals(pressure, other.pressure) && Objects.equals(sensorId, other.sensorId)
&& Objects.equals(temperature, other.temperature) && Objects.equals(timestamp, other.timestamp) && Objects.equals(temperature, other.temperature) && Objects.equals(timestamp, other.timestamp)
&& Objects.equals(valueId, other.valueId); && Objects.equals(valueId, other.valueId);
} }
@Override @Override
public String toString() { public String toString() {
return "WeatherValue [valueId=" + valueId + ", sensorId=" + sensorId + ", temperature=" + temperature return "WeatherValue [valueId=" + valueId + ", deviceId=" + deviceId + ", sensorId=" + sensorId
+ ", humidity=" + humidity + ", timestamp=" + timestamp + "]"; + ", temperature=" + temperature + ", humidity=" + humidity + ", pressure=" + pressure + ", timestamp="
+ timestamp + "]";
} }
public static WeatherValue fromPayload(DevicePayload payload) {
return new WeatherValue(null, payload.getDeviceId(), payload.getSensorId(), payload.getTemperature(),
payload.getHumidity(), payload.getPressure(), payload.getTimestamp());
}
} }

View File

@@ -23,13 +23,17 @@ import net.miarma.contaminus.common.SingleJsonResponse;
import net.miarma.contaminus.database.DatabaseManager; import net.miarma.contaminus.database.DatabaseManager;
import net.miarma.contaminus.database.QueryBuilder; import net.miarma.contaminus.database.QueryBuilder;
import net.miarma.contaminus.database.entities.Actuator; import net.miarma.contaminus.database.entities.Actuator;
import net.miarma.contaminus.database.entities.COValue;
import net.miarma.contaminus.database.entities.Device; import net.miarma.contaminus.database.entities.Device;
import net.miarma.contaminus.database.entities.DeviceLatestValuesView; import net.miarma.contaminus.database.entities.DeviceLatestValuesView;
import net.miarma.contaminus.database.entities.DevicePayload;
import net.miarma.contaminus.database.entities.DevicePollutionMap; import net.miarma.contaminus.database.entities.DevicePollutionMap;
import net.miarma.contaminus.database.entities.DeviceSensorHistory; import net.miarma.contaminus.database.entities.DeviceSensorHistory;
import net.miarma.contaminus.database.entities.DeviceSensorValue; import net.miarma.contaminus.database.entities.DeviceSensorValue;
import net.miarma.contaminus.database.entities.GpsValue;
import net.miarma.contaminus.database.entities.Group; import net.miarma.contaminus.database.entities.Group;
import net.miarma.contaminus.database.entities.Sensor; import net.miarma.contaminus.database.entities.Sensor;
import net.miarma.contaminus.database.entities.WeatherValue;
/* /*
* This class is a Verticle that will handle the Data Layer API. * This class is a Verticle that will handle the Data Layer API.
@@ -76,6 +80,9 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
.allowedMethods(allowedMethods)); .allowedMethods(allowedMethods));
router.route().handler(BodyHandler.create()); router.route().handler(BodyHandler.create());
// Payload
router.route(HttpMethod.POST, Constants.POST_PAYLOAD).handler(this::addDevicePayload);
// Group Routes // Group Routes
router.route(HttpMethod.GET, Constants.GET_GROUPS).handler(this::getAllGroups); router.route(HttpMethod.GET, Constants.GET_GROUPS).handler(this::getAllGroups);
router.route(HttpMethod.GET, Constants.GET_GROUP_BY_ID).handler(this::getGroupById); router.route(HttpMethod.GET, Constants.GET_GROUP_BY_ID).handler(this::getGroupById);
@@ -121,6 +128,48 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
}); });
} }
private void addDevicePayload(RoutingContext context) {
JsonObject body = context.body().asJsonObject();
DevicePayload devicePayload = gson.fromJson(body.toString(), DevicePayload.class);
COValue coValue = COValue.fromPayload(devicePayload);
GpsValue gpsValue = GpsValue.fromPayload(devicePayload);
WeatherValue weatherValue = WeatherValue.fromPayload(devicePayload);
String coQuery = QueryBuilder
.insert(coValue)
.build();
String gpsQuery = QueryBuilder
.insert(gpsValue)
.build();
String weatherQuery = QueryBuilder
.insert(weatherValue)
.build();
dbManager.execute(coQuery, COValue.class,
onSuccess -> {
dbManager.execute(gpsQuery, GpsValue.class,
onSuccess2 -> {
dbManager.execute(weatherQuery, WeatherValue.class,
onSuccess3 -> {
context.response()
.putHeader("content-type", "application/json; charset=utf-8")
.end(gson.toJson(SingleJsonResponse.of("Payload added successfully")));
},
onFailure3 -> {
context.fail(500, onFailure3);
});
},
onFailure2 -> {
context.fail(500, onFailure2);
});
},
onFailure -> {
context.fail(500, onFailure);
});
}
private void getAllGroups(RoutingContext context) { private void getAllGroups(RoutingContext context) {
String query = QueryBuilder String query = QueryBuilder
.select(Group.class) .select(Group.class)
@@ -211,7 +260,7 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
} }
private void getDeviceById(RoutingContext context) { private void getDeviceById(RoutingContext context) {
Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); String deviceId = context.request().getParam("deviceId");
Device device = new Device(deviceId, null, null); Device device = new Device(deviceId, null, null);
String query = QueryBuilder String query = QueryBuilder

View File

@@ -95,7 +95,7 @@ public class LogicLayerAPIVerticle extends AbstractVerticle {
} }
private void getDeviceSensors(RoutingContext context) { private void getDeviceSensors(RoutingContext context) {
Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); String deviceId = context.request().getParam("deviceId");
Promise<Sensor[]> resultList = Promise.promise(); Promise<Sensor[]> resultList = Promise.promise();
resultList.future().onComplete(result -> { resultList.future().onComplete(result -> {
if (result.succeeded()) { if (result.succeeded()) {
@@ -114,7 +114,7 @@ public class LogicLayerAPIVerticle extends AbstractVerticle {
} }
private void getDeviceActuators(RoutingContext context) { private void getDeviceActuators(RoutingContext context) {
Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); String deviceId = context.request().getParam("deviceId");
Promise<Actuator[]> resultList = Promise.promise(); Promise<Actuator[]> resultList = Promise.promise();
resultList.future().onComplete(result -> { resultList.future().onComplete(result -> {
if (result.succeeded()) { if (result.succeeded()) {
@@ -133,7 +133,7 @@ public class LogicLayerAPIVerticle extends AbstractVerticle {
} }
private void getDeviceLatestValues(RoutingContext context) { private void getDeviceLatestValues(RoutingContext context) {
Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); String deviceId = context.request().getParam("deviceId");
Promise<DeviceLatestValuesView[]> resultList = Promise.promise(); Promise<DeviceLatestValuesView[]> resultList = Promise.promise();
resultList.future().onComplete(complete -> { resultList.future().onComplete(complete -> {
@@ -155,7 +155,7 @@ public class LogicLayerAPIVerticle extends AbstractVerticle {
} }
private void getDevicePollutionMap(RoutingContext context) { private void getDevicePollutionMap(RoutingContext context) {
Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); String deviceId = context.request().getParam("deviceId");
Promise<DevicePollutionMap[]> resultList = Promise.promise(); Promise<DevicePollutionMap[]> resultList = Promise.promise();
@@ -178,7 +178,7 @@ public class LogicLayerAPIVerticle extends AbstractVerticle {
} }
private void getDeviceHistory(RoutingContext context) { private void getDeviceHistory(RoutingContext context) {
Integer deviceId = Integer.parseInt(context.request().getParam("deviceId")); String deviceId = context.request().getParam("deviceId");
Promise<DeviceSensorHistory[]> resultList = Promise.promise(); Promise<DeviceSensorHistory[]> resultList = Promise.promise();

View File

@@ -1,3 +1,5 @@
#pragma once
#include <Wire.h> #include <Wire.h>
#include <BME280I2C.h> #include <BME280I2C.h>

View File

@@ -1,3 +1,5 @@
#pragma once
#include "TinyGPSPlus.h" #include "TinyGPSPlus.h"
struct GPSData_t struct GPSData_t

View File

@@ -1,46 +1,27 @@
#pragma once
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <HTTPClient.h> #include <HTTPClient.h>
String serializeSensorValue ( String serializeSensorValue(
int sensorId, int sensorId,
int deviceId, const String &deviceId,
String sensorType, const String &sensorType,
String unit, const String &unit,
int sensorStatus, int sensorStatus,
float temperature, const BME280Data_t &bme,
float humidity, const MQ7Data_t &mq7,
float carbonMonoxide, const GPSData_t &gps,
float lat,
float lon,
long timestamp long timestamp
); );
String serializeActuatorStatus ( String serializeActuatorStatus(
int actuatorId, int actuatorId,
int deviceId, const String &deviceId,
int status, int status,
long timestamp long timestamp
); );
String serializeDevice ( void deserializeSensorValue(HTTPClient &http, int httpResponseCode);
int sensorId, void deserializeActuatorStatus(HTTPClient &http, int httpResponseCode);
int deviceId, void deserializeDevice(HTTPClient &http, int httpResponseCode);
String sensorType,
int status,
long timestamp
);
void deserializeSensorValue (
HTTPClient &http,
int httpResponseCode
);
void deserializeActuatorStatus (
HTTPClient &http,
int httpResponseCode
);
void deserializeDevice (
HTTPClient &http,
int httpResponseCode
);

View File

@@ -1,3 +1,5 @@
#pragma once
#include <MD_Parola.h> #include <MD_Parola.h>
#include <MD_MAX72xx.h> #include <MD_MAX72xx.h>
#include <SPI.h> #include <SPI.h>

View File

@@ -1,3 +1,5 @@
#pragma once
#include <Arduino.h> #include <Arduino.h>
#define MQ7_A0 34 #define MQ7_A0 34

View File

@@ -1,2 +1,4 @@
#pragma once
#include <WiFi.h> #include <WiFi.h>
#include <PubSubClient.h> #include <PubSubClient.h>

View File

@@ -1,3 +1,5 @@
#pragma once
#include <HTTPClient.h> #include <HTTPClient.h>
void getRequest(HTTPClient &httpClient, const String url, String &response); void getRequest(HTTPClient &httpClient, const String url, String &response);

View File

@@ -1,3 +1,5 @@
#pragma once
#include <WiFi.h> #include <WiFi.h>
#include <PubSubClient.h> #include <PubSubClient.h>

View File

@@ -20,7 +20,8 @@
#include "MAX7219.hpp" #include "MAX7219.hpp"
#include "MQ7v2.hpp" #include "MQ7v2.hpp"
struct TaskTimer { struct TaskTimer
{
uint32_t lastRun = 0; uint32_t lastRun = 0;
uint32_t interval = 1000; uint32_t interval = 1000;
@@ -30,7 +31,14 @@ struct TaskTimer {
: lastRun(last), interval(interval) {} : lastRun(last), interval(interval) {}
}; };
enum AirQualityStatus { struct SensorInfo
{
int id;
String type;
};
enum AirQualityStatus
{
GOOD, GOOD,
BAD BAD
}; };

View File

@@ -1,31 +1,39 @@
#include "JsonTools.hpp" #include "JsonTools.hpp"
String serializeSensorValue(int sensorId, int deviceId, String sensorType, String unit, int sensorStatus, float temperature, float humidity, float carbonMonoxide, float lat, float lon, long timestamp) String serializeSensorValue(
{ int sensorId,
DynamicJsonDocument doc(2048); const String &deviceId,
const String &sensorType,
const String &unit,
int sensorStatus,
const BME280Data_t &bme,
const MQ7Data_t &mq7,
const GPSData_t &gps,
long timestamp
) {
DynamicJsonDocument doc(1024);
doc["sensorId"] = sensorId; doc["sensorId"] = sensorId;
doc["deviceId"] = deviceId; doc["deviceId"] = deviceId;
doc["sensorType"] = sensorType; doc["sensorType"] = sensorType;
doc["unit"] = unit; doc["unit"] = unit;
doc["sesnsorStatuts"] = sensorStatus; doc["sensorStatus"] = sensorStatus;
doc["temperature"] = temperature; doc["temperature"] = bme.temperature;
doc["humidity"] = humidity; doc["humidity"] = bme.humidity;
doc["carbonMonoxide"] = carbonMonoxide; doc["pressure"] = bme.pressure;
doc["lat"] = lat; doc["carbonMonoxide"] = mq7.co;
doc["lon"] = lon; doc["lat"] = gps.lat;
doc["lon"] = gps.lon;
doc["timestamp"] = timestamp; doc["timestamp"] = timestamp;
String output; String output;
serializeJson(doc, output); serializeJson(doc, output);
Serial.println(output); Serial.println(output);
return output; return output;
} }
String serializeActuatorStatus (int actuatorId, int deviceId, int status, long timestamp) String serializeActuatorStatus(const int actuatorId, const String &deviceId, const int status, const long timestamp) {
{ DynamicJsonDocument doc(512);
DynamicJsonDocument doc(2048);
doc["actuatorId"] = actuatorId; doc["actuatorId"] = actuatorId;
doc["deviceId"] = deviceId; doc["deviceId"] = deviceId;
@@ -35,53 +43,43 @@ String serializeActuatorStatus (int actuatorId, int deviceId, int status, long t
String output; String output;
serializeJson(doc, output); serializeJson(doc, output);
Serial.println(output); Serial.println(output);
return output; return output;
} }
String serializeDevice(int sensorId, int deviceId, String sensorType, int status, long timestamp) String serializeDevice(const String &deviceId, int groupId, const String &deviceName) {
{ DynamicJsonDocument doc(512);
DynamicJsonDocument doc(2048);
doc["sensorId"] = sensorId;
doc["deviceId"] = deviceId; doc["deviceId"] = deviceId;
doc["sensorType"] = sensorType; doc["groupId"] = groupId;
doc["status"] = status; doc["deviceName"] = deviceName;
doc["timestamp"] = timestamp;
String output; String output;
serializeJson(doc, output); serializeJson(doc, output);
Serial.println(output); Serial.println(output);
return output; return output;
} }
void deserializeSensorValue (HTTPClient &http, int httpResponseCode) void deserializeSensorValue(HTTPClient &http, int httpResponseCode) {
{ if (httpResponseCode > 0) {
if (httpResponseCode > 0)
{
Serial.print("HTTP Response code: "); Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode); Serial.println(httpResponseCode);
String responseJson = http.getString(); String responseJson = http.getString();
DynamicJsonDocument doc(ESP.getMaxAllocHeap()); DynamicJsonDocument doc(ESP.getMaxAllocHeap());
DeserializationError error = deserializeJson(doc, responseJson); DeserializationError error = deserializeJson(doc, responseJson);
if (error) if (error) {
{
Serial.print(F("deserializeJson() failed: ")); Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str()); Serial.println(error.f_str());
return; return;
} }
JsonArray array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
for (JsonObject sensor : array) for (JsonObject sensor : array) {
{
int sensorId = sensor["sensorId"]; int sensorId = sensor["sensorId"];
int deviceId = sensor["deviceId"]; String deviceId = sensor["deviceId"];
String sensorType = sensor["sensorType"]; String sensorType = sensor["sensorType"];
String unit = sensor["unit"]; String unit = sensor["unit"];
int sesnsorStatuts = sensor["sesnsorStatuts"]; int sensorStatus = sensor["sensorStatus"];
float temperature = sensor["temperature"]; float temperature = sensor["temperature"];
float humidity = sensor["humidity"]; float humidity = sensor["humidity"];
float carbonMonoxide = sensor["carbonMonoxide"]; float carbonMonoxide = sensor["carbonMonoxide"];
@@ -89,90 +87,72 @@ void deserializeSensorValue (HTTPClient &http, int httpResponseCode)
float lon = sensor["lon"]; float lon = sensor["lon"];
long timestamp = sensor["timestamp"]; long timestamp = sensor["timestamp"];
Serial.println(("Sensor deserialized: [sensorId: " + String(sensorId) + ", deviceId: " + String(deviceId) + ", sensorType: " + sensorType + ", unit: " + unit +", sesnsorStatuts: " + String(sesnsorStatuts) +", temperature: " + String(temperature) +", humidity: " + String(humidity) +", carbonMonoxide: " + String(carbonMonoxide) +", lat: " + String(lat) +", lon: " + String(lon) +", timestamp: " + String(timestamp) + "]").c_str()); Serial.println("Sensor deserialized:");
Serial.printf(" ID: %d\n Device: %s\n Type: %s\n Unit: %s\n Status: %d\n Temp: %.2f\n Hum: %.2f\n CO: %.2f\n Lat: %.6f\n Lon: %.6f\n Time: %ld\n\n",
sensorId, deviceId.c_str(), sensorType.c_str(), unit.c_str(), sensorStatus,
temperature, humidity, carbonMonoxide, lat, lon, timestamp);
} }
} } else {
else
{
Serial.print("Error code: "); Serial.print("Error code: ");
Serial.println(httpResponseCode); Serial.println(httpResponseCode);
} }
} }
void deserializeActuatorStatus (HTTPClient &http, int httpResponseCode) void deserializeActuatorStatus(HTTPClient &http, int httpResponseCode) {
{ if (httpResponseCode > 0) {
if (httpResponseCode > 0)
{
Serial.print("HTTP Response code: "); Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode); Serial.println(httpResponseCode);
String responseJson = http.getString(); String responseJson = http.getString();
DynamicJsonDocument doc(ESP.getMaxAllocHeap()); DynamicJsonDocument doc(ESP.getMaxAllocHeap());
DeserializationError error = deserializeJson(doc, responseJson); DeserializationError error = deserializeJson(doc, responseJson);
if (error) if (error) {
{
Serial.print(F("deserializeJson() failed: ")); Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str()); Serial.println(error.f_str());
return; return;
} }
JsonArray array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
for (JsonObject actuator : array) for (JsonObject actuator : array) {
{ int actuatorId = actuator["actuatorId"];
int actuadorId = actuator["actuadorId"]; String deviceId = actuator["deviceId"];
int deviceId = actuator["deviceId"]; int status = actuator["status"];
int statuts = actuator["statuts"];
long timestamp = actuator["timestamp"]; long timestamp = actuator["timestamp"];
Serial.println(("Actuador deserialized: [actuadorId: " + String(actuadorId) + Serial.println("Actuator deserialized:");
", deviceId: " + String(deviceId) + Serial.printf(" ID: %d\n Device: %s\n Status: %d\n Time: %ld\n\n",
", statuts: " + String(statuts) + actuatorId, deviceId.c_str(), status, timestamp);
", timestamp: " + String(timestamp) + "]").c_str());
} }
} } else {
else
{
Serial.print("Error code: "); Serial.print("Error code: ");
Serial.println(httpResponseCode); Serial.println(httpResponseCode);
} }
} }
void deserializeDevice (HTTPClient &http, int httpResponseCode) void deserializeDevice(HTTPClient &http, int httpResponseCode) {
{ if (httpResponseCode > 0) {
if (httpResponseCode > 0)
{
Serial.print("HTTP Response code: "); Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode); Serial.println(httpResponseCode);
String responseJson = http.getString(); String responseJson = http.getString();
DynamicJsonDocument doc(ESP.getMaxAllocHeap()); DynamicJsonDocument doc(ESP.getMaxAllocHeap());
DeserializationError error = deserializeJson(doc, responseJson); DeserializationError error = deserializeJson(doc, responseJson);
if (error) if (error) {
{
Serial.print(F("deserializeJson() failed: ")); Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str()); Serial.println(error.f_str());
return; return;
} }
JsonArray array = doc.as<JsonArray>(); JsonArray array = doc.as<JsonArray>();
for (JsonObject device : array) for (JsonObject device : array) {
{ String deviceId = device["deviceId"];
int sensorId = device["sensorId"]; int groupId = device["groupId"];
int deviceId = device["deviceId"]; String deviceName = device["deviceName"];
String sensorType = device["sensorType"];
long timestamp = device["timestamp"];
Serial.println(("Sensor deserialized: [sensorId: " + String(sensorId) +
", deviceId: " + String(deviceId) +
", sensorType: " + sensorType +
", timestamp: " + String(timestamp) + "]").c_str());
Serial.println("Device deserialized:");
Serial.printf(" ID: %s\n Group: %d\n Name: %s\n\n", deviceId.c_str(), groupId, deviceName.c_str());
} }
} } else {
else
{
Serial.print("Error code: "); Serial.print("Error code: ");
Serial.println(httpResponseCode); Serial.println(httpResponseCode);
} }

View File

@@ -57,8 +57,6 @@ void loop()
printAllData(); printAllData();
#endif #endif
globalTimer.lastRun = now; globalTimer.lastRun = now;
} }
} }