1
0

changes (there are bugs, this is unstable!)

This commit is contained in:
Jose
2025-03-13 23:35:59 +01:00
parent 139e2d5961
commit db31753572
15 changed files with 753 additions and 134 deletions

View File

@@ -1,5 +1,3 @@
public class Main {
public static void main(String[] args) {

View File

@@ -11,22 +11,9 @@ public class ConfigManager {
private ConfigManager() {
this.configFile = new File(Constants.CONFIG_FILE);
this.config = new Properties();
if (!configFile.exists()) {
try {
createFiles();
} catch (IOException e) {
Constants.LOGGER.error("Error creating configuration files: ", e);
}
}
loadConfig();
}
public static void init() {
ConfigManager.getInstance();
}
public static synchronized ConfigManager getInstance() {
if (instance == null) {
instance = new ConfigManager();
@@ -34,25 +21,6 @@ public class ConfigManager {
return instance;
}
private void createFiles() throws IOException {
File baseDir = new File(Constants.BASE_DIR);
if (!baseDir.exists()) baseDir.mkdirs();
try (InputStream defaultConfigStream = getClass().getClassLoader().getResourceAsStream("default.properties");
FileOutputStream fos = new FileOutputStream(configFile)) {
if (defaultConfigStream != null) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = defaultConfigStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
} else {
Constants.LOGGER.error("File not found: default.properties");
}
}
}
private void loadConfig() {
try (FileInputStream fis = new FileInputStream(configFile)) {
config.load(fis);

View File

@@ -1,23 +1,18 @@
package net.miarma.contaminus.common;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import net.miarma.contaminus.util.SystemUtil;
import net.miarma.contaminus.database.DatabaseManager;
public class Constants {
public static final String APP_NAME = "ContaminUS";
public static final int API_VERSION = 1;
public static final String API_PREFIX = "/api/v" + API_VERSION;
public static final String HOME_DIR = SystemUtil.getOS() == OSType.WINDOWS ?
"C:/Users/" + System.getProperty("user.name") + "/" :
System.getProperty("user.home").contains("root") ? "/root/" :
"/home/" + System.getProperty("user.name") + "/";
public static final String BASE_DIR = HOME_DIR +
(SystemUtil.getOS() == OSType.WINDOWS ? ".contaminus" :
SystemUtil.getOS() == OSType.LINUX ? ".config/contaminus" :
".contaminus");
public static final String CONFIG_FILE = BASE_DIR + "/" + "config.properties";
public static final Logger LOGGER = LoggerFactory.getLogger(APP_NAME);
public static final String API_PREFIX = "/api/v" + Constants.API_VERSION;
public static String HOME_DIR;
public static String BASE_DIR;
public static String CONFIG_FILE;
public static ConfigManager CONFIG;
public static DatabaseManager DB;
public static Logger LOGGER;
/* API Endpoints */
public static final String GET_GROUPS = API_PREFIX + "/groups";

View File

@@ -8,37 +8,30 @@ import java.util.List;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.jdbcclient.JDBCConnectOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.jdbcclient.JDBCPool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import net.miarma.contaminus.common.ConfigManager;
import net.miarma.contaminus.common.Constants;
public class DatabaseManager {
private final JDBCPool pool;
private final Vertx vertx = Vertx.vertx();
private final JDBCPool pool;
private static DatabaseManager instance;
public static DatabaseManager getInstance() {
if (instance == null) {
instance = new DatabaseManager();
}
return instance;
}
private DatabaseManager() {
@SuppressWarnings("deprecation")
public DatabaseManager(Vertx vertx) {
ConfigManager config = ConfigManager.getInstance();
pool = JDBCPool.pool(vertx,
new JDBCConnectOptions()
.setJdbcUrl(config.getJdbcUrl())
.setUser(config.getStringProperty("db.user"))
.setPassword(config.getStringProperty("db.pwd")),
new PoolOptions()
.setMaxSize(5)
);
JsonObject dbConfig = new JsonObject()
.put("url", config.getStringProperty("db.protocol") + "//" +
config.getStringProperty("db.host") + ":" +
config.getStringProperty("db.port") + "/" +
config.getStringProperty("db.name"))
.put("user", config.getStringProperty("db.user"))
.put("password", config.getStringProperty("db.pwd"))
.put("max_pool_size", config.getStringProperty("db.poolSize"));
pool = JDBCPool.pool(vertx, dbConfig);
}
public Future<RowSet<Row>> testConnection() {

View File

@@ -1,5 +1,91 @@
package net.miarma.contaminus.database.entities;
import java.util.Objects;
import io.vertx.sqlclient.Row;
import net.miarma.contaminus.common.Table;
@Table("actuators")
public class Actuator {
private int actuatorId;
private int deviceId;
private int status;
private long timestamp;
public Actuator() {}
public Actuator(Row row) {
this.actuatorId = row.getInteger("actuatorId");
this.deviceId = row.getInteger("deviceId");
this.status = row.getInteger("status");
this.timestamp = row.getLong("timestamp");
}
public Actuator(int actuatorId, int deviceId, int status, long timestamp) {
super();
this.actuatorId = actuatorId;
this.deviceId = deviceId;
this.status = status;
this.timestamp = timestamp;
}
public int getActuatorId() {
return actuatorId;
}
public void setActuatorId(int actuatorId) {
this.actuatorId = actuatorId;
}
public int getDeviceId() {
return deviceId;
}
public void setDeviceId(int deviceId) {
this.deviceId = deviceId;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
@Override
public int hashCode() {
return Objects.hash(actuatorId, deviceId, status, timestamp);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Actuator other = (Actuator) obj;
return actuatorId == other.actuatorId && deviceId == other.deviceId
&& status == other.status && timestamp == other.timestamp;
}
@Override
public String toString() {
return "Actuator [actuatorId=" + actuatorId + ", deviceId=" + deviceId + ", status=" + status + ", timestamp="
+ timestamp + "]";
}
}

View File

@@ -1,5 +1,91 @@
package net.miarma.contaminus.database.entities;
import java.util.Objects;
import io.vertx.sqlclient.Row;
import net.miarma.contaminus.common.Table;
@Table("co_values")
public class COValue {
private int valueId;
private int sensorId;
private float value;
private long timestamp;
public COValue() {}
public COValue(Row row) {
this.valueId = row.getInteger("valueId");
this.sensorId = row.getInteger("sensorId");
this.value = row.getFloat("value");
this.timestamp = row.getLong("timestamp");
}
public COValue(int valueId, int sensorId, float value, long timestamp) {
super();
this.valueId = valueId;
this.sensorId = sensorId;
this.value = value;
this.timestamp = timestamp;
}
public int getValueId() {
return valueId;
}
public void setValueId(int valueId) {
this.valueId = valueId;
}
public int getSensorId() {
return sensorId;
}
public void setSensorId(int sensorId) {
this.sensorId = sensorId;
}
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
@Override
public int hashCode() {
return Objects.hash(sensorId, timestamp, value, valueId);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
COValue other = (COValue) obj;
return sensorId == other.sensorId
&& Objects.equals(timestamp, other.timestamp)
&& Float.floatToIntBits(value) == Float.floatToIntBits(other.value) && valueId == other.valueId;
}
@Override
public String toString() {
return "COValue [valueId=" + valueId + ", sensorId=" + sensorId + ", value=" + value + ", timestamp="
+ timestamp + "]";
}
}

View File

@@ -1,5 +1,81 @@
package net.miarma.contaminus.database.entities;
import java.util.Objects;
import io.vertx.sqlclient.Row;
import net.miarma.contaminus.common.Table;
@Table("devices")
public class Device {
private int deviceId;
private int groupId;
private String deviceName;
public Device() {}
public Device(Row row) {
this.deviceId = row.getInteger("deviceId");
this.groupId = row.getInteger("groupId");
this.deviceName = row.getString("deviceName");
}
public Device(int deviceId, int groupId, String deviceName) {
super();
this.deviceId = deviceId;
this.groupId = groupId;
this.deviceName = deviceName;
}
public int getDeviceId() {
return deviceId;
}
public void setDeviceId(int deviceId) {
this.deviceId = deviceId;
}
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
@Override
public int hashCode() {
return Objects.hash(deviceId, deviceName, groupId);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Device other = (Device) obj;
return deviceId == other.deviceId
&& Objects.equals(deviceName, other.deviceName)
&& groupId == other.groupId;
}
@Override
public String toString() {
return "Device [deviceId=" + deviceId + ", groupId=" + groupId + ", deviceName=" + deviceName + "]";
}
}

View File

@@ -1,5 +1,103 @@
package net.miarma.contaminus.database.entities;
import java.util.Objects;
import io.vertx.sqlclient.Row;
import net.miarma.contaminus.common.Table;
@Table("gps_values")
public class GpsValue {
private int valueId;
private int sensorId;
private float lat;
private float lon;
private long timestamp;
public GpsValue() {}
public GpsValue(Row row) {
this.valueId = row.getInteger("valueId");
this.sensorId = row.getInteger("sensorId");
this.lat = row.getFloat("lat");
this.lon = row.getFloat("lon");
this.timestamp = row.getLong("timestamp");
}
public GpsValue(int valueId, int sensorId, float lat, float lon, long timestamp) {
super();
this.valueId = valueId;
this.sensorId = sensorId;
this.lat = lat;
this.lon = lon;
this.timestamp = timestamp;
}
public int getValueId() {
return valueId;
}
public void setValueId(int valueId) {
this.valueId = valueId;
}
public int getSensorId() {
return sensorId;
}
public void setSensorId(int sensorId) {
this.sensorId = sensorId;
}
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(lat, lon, sensorId, timestamp, valueId);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
GpsValue other = (GpsValue) obj;
return Float.floatToIntBits(lat) == Float.floatToIntBits(other.lat)
&& Float.floatToIntBits(lon) == Float.floatToIntBits(other.lon)
&& sensorId == other.sensorId && timestamp == other.timestamp && valueId == other.valueId;
}
@Override
public String toString() {
return "GpsValue [valueId=" + valueId + ", sensorId=" + sensorId + ", lat=" + lat + ", lon=" + lon
+ ", timestamp=" + timestamp + "]";
}
}

View File

@@ -1,17 +1,67 @@
package net.miarma.contaminus.database.entities;
import java.util.Objects;
import io.vertx.sqlclient.Row;
import net.miarma.contaminus.common.Table;
@Table("groups")
@SuppressWarnings("unused")
public class Group {
private int groupId;
private String groupName;
public Group() {}
private int groupId;
private String groupName;
public Group() {}
public Group(Row row) {
this.groupId = row.getInteger("groupId");
this.groupName = row.getString("groupName");
}
public Group(int groupId, String groupName) {
super();
this.groupId = groupId;
this.groupName = groupName;
}
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
@Override
public int hashCode() {
return Objects.hash(groupId, groupName);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Group other = (Group) obj;
return groupId == other.groupId
&& Objects.equals(groupName, other.groupName);
}
@Override
public String toString() {
return "Group [groupId=" + groupId + ", groupName=" + groupName + "]";
}
}

View File

@@ -1,5 +1,112 @@
package net.miarma.contaminus.database.entities;
import java.util.Objects;
import io.vertx.sqlclient.Row;
import net.miarma.contaminus.common.Table;
@Table("sensors")
public class Sensor {
private int sensorId;
private int deviceId;
private String sensorType;
private String unit;
private int status;
private long timestamp;
public Sensor() {}
public Sensor(Row row) {
this.sensorId = row.getInteger("sensorId");
this.deviceId = row.getInteger("deviceId");
this.sensorType = row.getString("sensorType");
this.unit = row.getString("unit");
this.status = row.getInteger("status");
this.timestamp = row.getLong("timestamp");
}
public Sensor(int sensorId, int deviceId, String sensorType, String unit, int status, long timestamp) {
super();
this.sensorId = sensorId;
this.deviceId = deviceId;
this.sensorType = sensorType;
this.unit = unit;
this.status = status;
this.timestamp = timestamp;
}
public int getSensorId() {
return sensorId;
}
public void setSensorId(int sensorId) {
this.sensorId = sensorId;
}
public int getDeviceId() {
return deviceId;
}
public void setDeviceId(int deviceId) {
this.deviceId = deviceId;
}
public String getSensorType() {
return sensorType;
}
public void setSensorType(String sensorType) {
this.sensorType = sensorType;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
@Override
public int hashCode() {
return Objects.hash(deviceId, sensorId, sensorType, status, timestamp, unit);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Sensor other = (Sensor) obj;
return deviceId == other.deviceId && sensorId == other.sensorId && Objects.equals(sensorType, other.sensorType)
&& status == other.status && timestamp == other.timestamp && Objects.equals(unit, other.unit);
}
@Override
public String toString() {
return "Sensor [sensorId=" + sensorId + ", deviceId=" + deviceId + ", sensorType=" + sensorType + ", unit="
+ unit + ", status=" + status + ", timestamp=" + timestamp + "]";
}
}

View File

@@ -1,5 +1,103 @@
package net.miarma.contaminus.database.entities;
import java.util.Objects;
import io.vertx.sqlclient.Row;
import net.miarma.contaminus.common.Table;
@Table("weather_values")
public class WeatherValue {
private int valueId;
private int sensorId;
private float temperature;
private float humidity;
private long timestamp;
public WeatherValue() {}
public WeatherValue(Row row) {
this.valueId = row.getInteger("valueId");
this.sensorId = row.getInteger("sensorId");
this.temperature = row.getFloat("temperature");
this.humidity = row.getFloat("humidity");
this.timestamp = row.getLong("timestamp");
}
public WeatherValue(int valueId, int sensorId, float temperature, float humidity, long timestamp) {
super();
this.valueId = valueId;
this.sensorId = sensorId;
this.temperature = temperature;
this.humidity = humidity;
this.timestamp = timestamp;
}
public int getValueId() {
return valueId;
}
public void setValueId(int valueId) {
this.valueId = valueId;
}
public int getSensorId() {
return sensorId;
}
public void setSensorId(int 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 long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
@Override
public int hashCode() {
return Objects.hash(humidity, sensorId, temperature, timestamp, valueId);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WeatherValue other = (WeatherValue) obj;
return Float.floatToIntBits(humidity) == Float.floatToIntBits(other.humidity) && sensorId == other.sensorId
&& Float.floatToIntBits(temperature) == Float.floatToIntBits(other.temperature)
&& timestamp == other.timestamp && valueId == other.valueId;
}
@Override
public String toString() {
return "WeatherValue [valueId=" + valueId + ", sensorId=" + sensorId + ", temperature=" + temperature
+ ", humidity=" + humidity + ", timestamp=" + timestamp + "]";
}
}

View File

@@ -31,7 +31,7 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
configManager = ConfigManager.getInstance();
gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
dbManager = DatabaseManager.getInstance();
dbManager = new DatabaseManager(vertx);
Router router = Router.router(vertx);
Set<HttpMethod> allowedMethods = new HashSet<>(

View File

@@ -32,7 +32,7 @@ public class LogicLayerAPIVerticle extends AbstractVerticle {
configManager = ConfigManager.getInstance();
gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
dbManager = DatabaseManager.getInstance();
dbManager = new DatabaseManager(vertx);
Router router = Router.router(vertx);
Set<HttpMethod> allowedMethods = new HashSet<>(

View File

@@ -1,25 +1,81 @@
package net.miarma.contaminus.server;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Launcher;
import io.vertx.core.Promise;
import io.vertx.core.ThreadingModel;
import io.vertx.core.impl.logging.LoggerFactory;
import net.miarma.contaminus.common.Constants;
import net.miarma.contaminus.common.OSType;
import net.miarma.contaminus.util.SystemUtil;
public class MainVerticle extends AbstractVerticle {
@Override
public void start(Promise<Void> startPromise) {
final DeploymentOptions options = new DeploymentOptions();
options.setThreadingModel(ThreadingModel.WORKER);
@Override
public void start(Promise<Void> startPromise) {
final DeploymentOptions options = new DeploymentOptions();
options.setThreadingModel(ThreadingModel.WORKER);
getVertx().deployVerticle(new DataLayerAPIVerticle(), options);
//getVertx().deployVerticle(new LogicLayerAPIVerticle(), options);
//getVertx().deployVerticle(new HttpServerVerticle());
}
getVertx().deployVerticle(new DataLayerAPIVerticle(), options);
getVertx().deployVerticle(new LogicLayerAPIVerticle(), options);
getVertx().deployVerticle(new HttpServerVerticle());
}
@Override
public void stop(Promise<Void> stopPromise) throws Exception {
getVertx().deploymentIDs()
.forEach(v -> getVertx().undeploy(v));
}
@Override
public void stop(Promise<Void> stopPromise) throws Exception {
getVertx().deploymentIDs()
.forEach(v -> getVertx().undeploy(v));
}
public static void main(String[] args) {
initializeConstants();
initializeDirectories();
copyDefaultConfig();
Launcher.executeCommand("run", MainVerticle.class.getName());
}
private static void initializeConstants() {
Constants.HOME_DIR = SystemUtil.getOS() == OSType.WINDOWS ?
"C:/Users/" + System.getProperty("user.name") + "/" :
System.getProperty("user.home").contains("root") ? "/root/" :
"/home/" + System.getProperty("user.name") + "/";
Constants.BASE_DIR = Constants.HOME_DIR +
(SystemUtil.getOS() == OSType.WINDOWS ? ".contaminus" :
SystemUtil.getOS() == OSType.LINUX ? ".config/contaminus" :
".contaminus");
Constants.CONFIG_FILE = Constants.BASE_DIR + "/config.properties";
System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
Constants.LOGGER = LoggerFactory.getLogger(Constants.APP_NAME);
}
private static void initializeDirectories() {
File baseDir = new File(Constants.BASE_DIR);
if (!baseDir.exists()) {
baseDir.mkdirs();
}
}
private static void copyDefaultConfig() {
File configFile = new File(Constants.CONFIG_FILE);
if (!configFile.exists()) {
try (InputStream in = MainVerticle.class.getClassLoader().getResourceAsStream("default.properties")) {
if (in != null) {
Files.copy(in, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} else {
Constants.LOGGER.error("Default config file not found in resources");
}
} catch (IOException e) {
Constants.LOGGER.error("Failed to copy default config file", e);
}
}
}
}

View File

@@ -4,38 +4,46 @@ import net.miarma.contaminus.common.ConfigManager;
import net.miarma.contaminus.common.OSType;
public class SystemUtil {
static ConfigManager configManager = ConfigManager.getInstance();
static String host = configManager.getStringProperty("inet.host");
static String origin = configManager.getStringProperty("inet.origin");
static int dataApiPort = 8081;
static int logicApiPort = 8082;
static int webserverPort = 8080;
private static ConfigManager configManager;
public static String getHost() {
return host;
}
private static String host;
private static int dataApiPort;
private static int logicApiPort;
private static int webserverPort;
public static int getDataApiPort() {
return dataApiPort;
}
public static void init() {
configManager = ConfigManager.getInstance();
public static int getLogicApiPort() {
return logicApiPort;
}
host = configManager.getStringProperty("inet.host");
dataApiPort = configManager.getIntProperty("data-api.port");
logicApiPort = configManager.getIntProperty("logic-api.port");
webserverPort = configManager.getIntProperty("web.port");
}
public static int getWebserverPort() {
return webserverPort;
}
public static String getHost() {
return host;
}
public static OSType getOS() {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
return OSType.WINDOWS;
} else if (os.contains("nix") || os.contains("nux")) {
return OSType.LINUX;
} else {
return OSType.INVALID_OS;
}
}
public static int getDataApiPort() {
return dataApiPort;
}
public static int getLogicApiPort() {
return logicApiPort;
}
public static int getWebserverPort() {
return webserverPort;
}
public static OSType getOS() {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
return OSType.WINDOWS;
} else if (os.contains("nix") || os.contains("nux")) {
return OSType.LINUX;
} else {
return OSType.INVALID_OS;
}
}
}