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; import java.util.Properties;
public class Info { public class Info {
private static String version="not init"; private static String version = "not init";
private static String builddate="not init"; private static String builddate = "not init";
private static String starttime="not init"; private static String starttime = "not init";
/**
* get Software Version (defined in gradle build file)
*
* @return Version as string
*/
public static String getVersion() { public static String getVersion() {
return version; return version;
} }
/**
* get Software build date
*
* @return Date as string
*/
public static String getBuilddate() { public static String getBuilddate() {
return builddate; return builddate;
} }
/**
* get Server start time
*
* @return start time
*/
public static String getStarttime() { public static String getStarttime() {
return starttime; 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()); starttime = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date());
Properties prop = new Properties(); Properties prop = new Properties();
try { try {
URL url = Info.class.getResource("/version.properties"); URL url = Info.class.getResource("/version.properties");
prop.load(url.openStream()); prop.load(url.openStream());
version=(String)prop.get("version"); version = (String) prop.get("version");
builddate=(String)prop.get("buildtime"); builddate = (String) prop.get("buildtime");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void getMemoryUsage(){ /**
* print memory utilization
*/
public static void getMemoryUsage() {
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
NumberFormat format = NumberFormat.getInstance(); 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")); 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) { public static void criticalerror(Object msg) {
if (Loglevel <= CRITICAL_ERROR) if (Loglevel <= CRITICAL_ERROR)
log(msg, CRITICAL_ERROR); log(msg, CRITICAL_ERROR);
} }
/**
* Log basic Error
*
* @param msg message
*/
public static void error(Object msg) { public static void error(Object msg) {
if (Loglevel <= ERROR) if (Loglevel <= ERROR)
log(msg, ERROR); log(msg, ERROR);
} }
/**
* Log warning
*
* @param msg message
*/
public static void warning(Object msg) { public static void warning(Object msg) {
if (Loglevel <= WARNING) if (Loglevel <= WARNING)
log(msg, WARNING); log(msg, WARNING);
} }
/**
* Log info
*
* @param msg message
*/
public static void info(Object msg) { public static void info(Object msg) {
if (Loglevel <= INFO) if (Loglevel <= INFO)
log(msg, INFO); log(msg, INFO);
} }
/**
* Log basic message
*
* @param msg message
*/
public static void message(Object msg) { public static void message(Object msg) {
if (Loglevel <= MESSAGE) if (Loglevel <= MESSAGE)
log(msg, MESSAGE); log(msg, MESSAGE);
} }
/**
* Log debug Message
*
* @param msg message
*/
public static void debug(Object msg) { public static void debug(Object msg) {
if (Loglevel <= DEBUG) if (Loglevel <= DEBUG)
log(msg, DEBUG); log(msg, DEBUG);
} }
/**
* Log as defined
*
* @param msg message
* @param level Loglevel --> static vals defined
*/
public static void log(Object msg, int level) { public static void log(Object msg, int level) {
boolean iswindows = System.getProperty("os.name").contains("Windows"); boolean iswindows = System.getProperty("os.name").contains("Windows");
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
@ -116,6 +151,12 @@ public class Log {
return date_format.format(resultdate); 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) { public static void setLevel(int level) {
Loglevel = level; Loglevel = level;
} }

View File

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

View File

@ -6,17 +6,28 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
public class JDBC { public class JDBC {
static Connection conn; private static Connection conn;
static JDBC JDBC; private static JDBC JDBC;
static boolean loggedin = false; private static boolean loggedin = false;
static String usernamec; private static String usernamec;
static String passwordc; private static String passwordc;
static String dbnamec; private static String dbnamec;
static String ipc; private static String ipc;
static int portc; 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 { public static void init(String username, String password, String dbname, String ip, int port) throws IOException {
usernamec = username; usernamec = username;
passwordc = password; passwordc = password;
@ -28,6 +39,13 @@ public class JDBC {
logintodb(username, password, dbname, ip, port); 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 { public static JDBC getInstance() throws IOException {
if (loggedin) { if (loggedin) {
return JDBC; 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) { public ResultSet executeQuery(String sql) {
try { try {
PreparedStatement stmt = conn.prepareStatement(sql); PreparedStatement stmt = conn.prepareStatement(sql);
@ -65,6 +89,13 @@ public class JDBC {
return null; 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 { public int executeUpdate(String sql) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(sql); PreparedStatement stmt = conn.prepareStatement(sql);

View File

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

View File

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