improved data encapsulation
This commit is contained in:
lukas-heiligenbrunner 2020-01-23 18:32:29 +01:00
parent cdb03ada81
commit 10058b24a7
6 changed files with 113 additions and 21 deletions

View File

@ -8,37 +8,58 @@ import java.util.Date;
import java.util.Properties;
public class Info {
private static String version="not init";
private static String builddate="not init";
private static String starttime="not init";
private static String version = "not init";
private static String builddate = "not init";
private static String starttime = "not init";
/**
* get Software Version (defined in gradle build file)
*
* @return Version as string
*/
public static String getVersion() {
return version;
}
/**
* get Software build date
*
* @return Date as string
*/
public static String getBuilddate() {
return builddate;
}
/**
* get Server start time
*
* @return start time
*/
public static String getStarttime() {
return starttime;
}
public static void init(){
/**
* initialize the version and builddate variables
*/
public static void init() {
starttime = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date());
Properties prop = new Properties();
try {
URL url = Info.class.getResource("/version.properties");
prop.load(url.openStream());
version=(String)prop.get("version");
builddate=(String)prop.get("buildtime");
version = (String) prop.get("version");
builddate = (String) prop.get("buildtime");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void getMemoryUsage(){
/**
* print memory utilization
*/
public static void getMemoryUsage() {
Runtime runtime = Runtime.getRuntime();
NumberFormat format = NumberFormat.getInstance();

View File

@ -28,37 +28,72 @@ public class Log {
private static ArrayList<String> colors = new ArrayList<String>(Arrays.asList("", "DEBUG", "MESSAGE", "INFO", "WARNING", "ERROR", "CRITICAL_ERROR"));
/**
* Log critical Error
*
* @param msg message
*/
public static void criticalerror(Object msg) {
if (Loglevel <= CRITICAL_ERROR)
log(msg, CRITICAL_ERROR);
}
/**
* Log basic Error
*
* @param msg message
*/
public static void error(Object msg) {
if (Loglevel <= ERROR)
log(msg, ERROR);
}
/**
* Log warning
*
* @param msg message
*/
public static void warning(Object msg) {
if (Loglevel <= WARNING)
log(msg, WARNING);
}
/**
* Log info
*
* @param msg message
*/
public static void info(Object msg) {
if (Loglevel <= INFO)
log(msg, INFO);
}
/**
* Log basic message
*
* @param msg message
*/
public static void message(Object msg) {
if (Loglevel <= MESSAGE)
log(msg, MESSAGE);
}
/**
* Log debug Message
*
* @param msg message
*/
public static void debug(Object msg) {
if (Loglevel <= DEBUG)
log(msg, DEBUG);
}
/**
* Log as defined
*
* @param msg message
* @param level Loglevel --> static vals defined
*/
public static void log(Object msg, int level) {
boolean iswindows = System.getProperty("os.name").contains("Windows");
StringBuilder builder = new StringBuilder();
@ -116,6 +151,12 @@ public class Log {
return date_format.format(resultdate);
}
/**
* define Loglevel call on startup or at runtime
* default: 0[DEBUG] --> Max logging
*
* @param level Loglevel --> static vals defined
*/
public static void setLevel(int level) {
Loglevel = level;
}

View File

@ -9,8 +9,7 @@ import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
public abstract class
Database {
abstract class Database {
protected String user;
protected String password;

View File

@ -6,17 +6,28 @@ import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC {
static Connection conn;
private static Connection conn;
static JDBC JDBC;
static boolean loggedin = false;
private static JDBC JDBC;
private static boolean loggedin = false;
static String usernamec;
static String passwordc;
static String dbnamec;
static String ipc;
static int portc;
private static String usernamec;
private static String passwordc;
private static String dbnamec;
private static String ipc;
private static int portc;
/**
* initialize database values
* suggested on startup
*
* @param username db username
* @param password db password
* @param dbname Database name
* @param ip Server ip or hostname
* @param port Server port
* @throws IOException
*/
public static void init(String username, String password, String dbname, String ip, int port) throws IOException {
usernamec = username;
passwordc = password;
@ -28,6 +39,13 @@ public class JDBC {
logintodb(username, password, dbname, ip, port);
}
/**
* get instance of db object
* logindata has to be set before!
*
* @return JDBC object of this
* @throws IOException
*/
public static JDBC getInstance() throws IOException {
if (loggedin) {
return JDBC;
@ -55,6 +73,12 @@ public class JDBC {
}
/**
* execute basic query --> requests only
*
* @param sql query sql statement
* @return ResultSet representating the table
*/
public ResultSet executeQuery(String sql) {
try {
PreparedStatement stmt = conn.prepareStatement(sql);
@ -65,6 +89,13 @@ public class JDBC {
return null;
}
/**
* update db in some way
*
* @param sql sql insert/update/delete statement
* @return status
* @throws SQLException
*/
public int executeUpdate(String sql) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(sql);

View File

@ -4,7 +4,7 @@ import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySQLConnector extends Database {
class MySQLConnector extends Database {
static {
try {

View File

@ -14,7 +14,7 @@ import java.util.Date;
public class MqttService {
private MqttClient client = null;
private String serveruri;
JDBC db;
private JDBC db;
public MqttService(String serverurl, String port) {
serveruri = "tcp://" + serverurl + ":" + port;
@ -60,7 +60,7 @@ public class MqttService {
} else {
devicecities.first();
devicecities.previous();
// TODO: 23.01.20 Test this stuff
// TODO: 23.01.20 Test this stuff
while (devicecities.next()) {
int cityid = devicecities.getInt("CityID");
checkDatabase(cityid, Integer.parseInt(deviceid));