Made settings configurable for multiple users
This commit is contained in:
parent
5a99f6b2d8
commit
fb0f87beb6
4 changed files with 173 additions and 15 deletions
5
pom.xml
5
pom.xml
|
@ -36,6 +36,11 @@
|
||||||
<artifactId>holidays</artifactId>
|
<artifactId>holidays</artifactId>
|
||||||
<version>1.0.3</version>
|
<version>1.0.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mariadb.jdbc</groupId>
|
||||||
|
<artifactId>mariadb-java-client</artifactId>
|
||||||
|
<version>3.5.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<finalName>OebbApi</finalName>
|
<finalName>OebbApi</finalName>
|
||||||
|
|
|
@ -11,12 +11,14 @@ import org.json.JSONObject;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
public class Main {
|
public class Main {
|
||||||
private static final String version = "0.7";
|
private static final String version = "0.8";
|
||||||
private static final HashMap<String, Integer> data = new HashMap<>();
|
private static final HashMap<String, HashMap<String, Integer>> udata = new HashMap<>();
|
||||||
public static Settings conf;
|
public static Settings conf;
|
||||||
static OebbCheck check = new OebbCheck();
|
static OebbCheck check = new OebbCheck();
|
||||||
static long time;
|
static long time;
|
||||||
|
@ -45,7 +47,7 @@ public class Main {
|
||||||
LOG.error("Error at Task: {}", e.toString());
|
LOG.error("Error at Task: {}", e.toString());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
service.scheduleAtFixedRate(task, 60, conf.getInt("refreshTime"), TimeUnit.SECONDS);
|
service.scheduleAtFixedRate(task, 10, conf.getInt("refreshTime"), TimeUnit.SECONDS);
|
||||||
if (server != null) {
|
if (server != null) {
|
||||||
server.createContext("/oebbapi", new OebbHandler());
|
server.createContext("/oebbapi", new OebbHandler());
|
||||||
server.setExecutor(null); // creates a default executor
|
server.setExecutor(null); // creates a default executor
|
||||||
|
@ -64,17 +66,35 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void refresh() throws IOException {
|
private static void refresh() throws IOException {
|
||||||
data.put(conf.getString("monitors.monitor1.DepartureTime"),check.getStatus(conf.getString("monitors.monitor1.Station"),conf.getString("monitors.monitor1.DepartureTime"), conf.getString("monitors.monitor1.FinalStop"),conf.getString("monitors.monitor1.TrainType"),String.valueOf(conf.getInt("monitors.monitor1.maxjourneys")), conf.getStringList("monitors.monitor1.notRunningDays")));
|
HashMap<String, Integer> data = new HashMap<>();
|
||||||
data.put(conf.getString("monitors.monitor2.DepartureTime"),check.getStatus(conf.getString("monitors.monitor2.Station"),conf.getString("monitors.monitor2.DepartureTime"), conf.getString("monitors.monitor2.FinalStop"),conf.getString("monitors.monitor2.TrainType"),String.valueOf(conf.getInt("monitors.monitor2.maxjourneys")), conf.getStringList("monitors.monitor2.notRunningDays")));
|
try{
|
||||||
data.put(conf.getString("monitors.monitor3.DepartureTime"),check.getStatus(conf.getString("monitors.monitor3.Station"),conf.getString("monitors.monitor3.DepartureTime"), conf.getString("monitors.monitor3.FinalStop"),conf.getString("monitors.monitor3.TrainType"),String.valueOf(conf.getInt("monitors.monitor3.maxjourneys")), conf.getStringList("monitors.monitor3.notRunningDays")));
|
ResultSet rs = MariaDB.load_users();
|
||||||
data.put(conf.getString("monitors.monitor4.DepartureTime"),check.getStatus(conf.getString("monitors.monitor4.Station"),conf.getString("monitors.monitor4.DepartureTime"), conf.getString("monitors.monitor4.FinalStop"),conf.getString("monitors.monitor4.TrainType"),String.valueOf(conf.getInt("monitors.monitor4.maxjourneys")), conf.getStringList("monitors.monitor4.notRunningDays")));
|
if (rs != null) {
|
||||||
|
while (rs.next()) {
|
||||||
|
String user = rs.getString("user");
|
||||||
|
ResultSet rsu = MariaDB.load_user(user);
|
||||||
|
if (rsu != null) {
|
||||||
|
while (rsu.next()) {
|
||||||
|
List<String> list = new ArrayList<>(Arrays.asList(rsu.getString("notRunningDays").split(",")));
|
||||||
|
data.put(rsu.getString("display"), check.getStatus(rsu.getString("Station"), rsu.getString("DepartureTime"), rsu.getString("FinalStop"), rsu.getString("TrainType"), String.valueOf(rsu.getInt("maxjourneys")), list));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
udata.put(user,data);
|
||||||
|
rs.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (SQLException e){
|
||||||
|
LOG.error("Update issue", e);
|
||||||
|
}
|
||||||
time = System.currentTimeMillis();
|
time = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
static class OebbHandler implements HttpHandler {
|
static class OebbHandler implements HttpHandler {
|
||||||
@Override
|
@Override
|
||||||
public void handle(HttpExchange t){
|
public void handle(HttpExchange t){
|
||||||
String response = createOutput();
|
String[] req = t.getRequestURI().toString().split("/");
|
||||||
|
String user = req[2];
|
||||||
|
String response = createOutput(user);
|
||||||
t.getResponseHeaders().put("Content-Type", Collections.singletonList("application/json"));
|
t.getResponseHeaders().put("Content-Type", Collections.singletonList("application/json"));
|
||||||
try {
|
try {
|
||||||
t.sendResponseHeaders(200, response.getBytes().length);
|
t.sendResponseHeaders(200, response.getBytes().length);
|
||||||
|
@ -87,7 +107,7 @@ public class Main {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String createOutput(){
|
private static String createOutput(String user){
|
||||||
JSONObject dat = new JSONObject();
|
JSONObject dat = new JSONObject();
|
||||||
Date updatetime = new Date(time);
|
Date updatetime = new Date(time);
|
||||||
dat.put("interval",conf.getInt("refreshTime")*1000);
|
dat.put("interval",conf.getInt("refreshTime")*1000);
|
||||||
|
@ -133,7 +153,7 @@ public class Main {
|
||||||
monitor2.put("label",conf.getString("monitors.monitor1.Title"));
|
monitor2.put("label",conf.getString("monitors.monitor1.Title"));
|
||||||
monitor2.put("labelColor",conf.getString("labelcolor"));
|
monitor2.put("labelColor",conf.getString("labelcolor"));
|
||||||
monitor2.put("fullDraw",true);
|
monitor2.put("fullDraw",true);
|
||||||
int zug1 = data.get(conf.getString("monitors.monitor1.DepartureTime"));
|
int zug1 = udata.get(user).get("1");
|
||||||
makeLabel(monitor2, zug1);
|
makeLabel(monitor2, zug1);
|
||||||
display.put(monitor2);
|
display.put(monitor2);
|
||||||
|
|
||||||
|
@ -141,7 +161,7 @@ public class Main {
|
||||||
monitor3.put("label",conf.getString("monitors.monitor2.Title"));
|
monitor3.put("label",conf.getString("monitors.monitor2.Title"));
|
||||||
monitor3.put("labelColor",conf.getString("labelcolor"));
|
monitor3.put("labelColor",conf.getString("labelcolor"));
|
||||||
monitor3.put("fullDraw",true);
|
monitor3.put("fullDraw",true);
|
||||||
int zug2 = data.get(conf.getString("monitors.monitor2.DepartureTime"));
|
int zug2 = udata.get(user).get("2");
|
||||||
makeLabel(monitor3, zug2);
|
makeLabel(monitor3, zug2);
|
||||||
display.put(monitor3);
|
display.put(monitor3);
|
||||||
|
|
||||||
|
@ -149,7 +169,7 @@ public class Main {
|
||||||
monitor4.put("label",conf.getString("monitors.monitor3.Title"));
|
monitor4.put("label",conf.getString("monitors.monitor3.Title"));
|
||||||
monitor4.put("labelColor",conf.getString("labelcolor"));
|
monitor4.put("labelColor",conf.getString("labelcolor"));
|
||||||
monitor4.put("fullDraw",true);
|
monitor4.put("fullDraw",true);
|
||||||
int zug3 = data.get(conf.getString("monitors.monitor3.DepartureTime"));
|
int zug3 = udata.get(user).get("3");
|
||||||
makeLabel(monitor4, zug3);
|
makeLabel(monitor4, zug3);
|
||||||
display.put(monitor4);
|
display.put(monitor4);
|
||||||
|
|
||||||
|
@ -157,10 +177,11 @@ public class Main {
|
||||||
monitor5.put("label",conf.getString("monitors.monitor4.Title"));
|
monitor5.put("label",conf.getString("monitors.monitor4.Title"));
|
||||||
monitor5.put("labelColor",conf.getString("labelcolor"));
|
monitor5.put("labelColor",conf.getString("labelcolor"));
|
||||||
monitor5.put("fullDraw",true);
|
monitor5.put("fullDraw",true);
|
||||||
int zug4 = data.get(conf.getString("monitors.monitor4.DepartureTime"));
|
int zug4 = udata.get(user).get("4");
|
||||||
makeLabel(monitor5, zug4);
|
makeLabel(monitor5, zug4);
|
||||||
display.put(monitor5);
|
display.put(monitor5);
|
||||||
dat.put("displays",display);
|
dat.put("displays",display);
|
||||||
|
System.out.println(display);
|
||||||
return dat.toString();
|
return dat.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
126
src/main/java/be/jaud/MariaDB.java
Normal file
126
src/main/java/be/jaud/MariaDB.java
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
package be.jaud;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
|
public class MariaDB {
|
||||||
|
|
||||||
|
private static Connection conn = null;
|
||||||
|
private static MariaDB self = null;
|
||||||
|
private static Settings conf = Settings.getInstance();
|
||||||
|
public static final Logger LOG = LoggerFactory.getLogger(MariaDB.class);
|
||||||
|
private MariaDB(String Host,String db,String User,String Password) {
|
||||||
|
try {
|
||||||
|
|
||||||
|
// Datenbanktreiber fuer ODBC Schnittstellen laden.
|
||||||
|
// F<>r verschiedene ODBC-Datenbanken muss dieser Treiber
|
||||||
|
// nur einmal geladen werden.
|
||||||
|
Class.forName("org.mariadb.jdbc.Driver").getDeclaredConstructor().newInstance();
|
||||||
|
|
||||||
|
// Verbindung zur ODBC-Datenbank herstellen.
|
||||||
|
// Es wird die JDBC-ODBC-Bruecke verwendet.
|
||||||
|
// Port -- Standard: 3306
|
||||||
|
String dbPort = "3306";
|
||||||
|
conn = DriverManager.getConnection("jdbc:mariadb://" + Host + ":"
|
||||||
|
+ dbPort + "/" + db + "?" + "user=" + URLEncoder.encode(User, StandardCharsets.UTF_8) + "&"
|
||||||
|
+ "password=" + Password + "&useSSL=false&autoReconnect=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC");
|
||||||
|
} catch (ClassNotFoundException | SQLException | IllegalAccessException | InstantiationException |
|
||||||
|
NoSuchMethodException | InvocationTargetException e) {
|
||||||
|
LOG.error("SQLException: {}", e.getMessage(), e);
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getInstance()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if(conn == null || !conn.isValid(0)){
|
||||||
|
self = new MariaDB(conf.getString("db.server") , conf.getString("db.database"), conf.getString("db.user"), conf.getString("db.password"));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOG.error("SQLException: {}", e.getMessage(), e);
|
||||||
|
}
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Connection
|
||||||
|
* @return MySQLConnection
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
public static MariaDB getDB()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if(conn == null || !conn.isValid(0)){
|
||||||
|
self = new MariaDB(conf.getString("db.server") , conf.getString("db.database"), conf.getString("db.user"), conf.getString("db.password"));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOG.error("SQLException: {}", e.getMessage(), e);
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void close()
|
||||||
|
{
|
||||||
|
if(conn != null){
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOG.error("SQLException: {}", e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
public static ResultSet load_users()
|
||||||
|
{
|
||||||
|
conn = getInstance();
|
||||||
|
|
||||||
|
if(conn != null)
|
||||||
|
{
|
||||||
|
// Anfrage-Statement erzeugen.
|
||||||
|
Statement query;
|
||||||
|
try {
|
||||||
|
query = conn.createStatement();
|
||||||
|
|
||||||
|
// Ergebnistabelle erzeugen und abholen.
|
||||||
|
String sql = "SELECT DISTINCT `user` FROM `"+ conf.getString("db.table")+"`";
|
||||||
|
return query.executeQuery(sql);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOG.error("SQLException: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the data of a user
|
||||||
|
* @param id Userid
|
||||||
|
* @return ResultSet
|
||||||
|
*/
|
||||||
|
public static ResultSet load_user(String id)
|
||||||
|
{
|
||||||
|
conn = getInstance();
|
||||||
|
|
||||||
|
if(conn != null)
|
||||||
|
{
|
||||||
|
// Anfrage-Statement erzeugen.
|
||||||
|
Statement query;
|
||||||
|
try {
|
||||||
|
query = conn.createStatement();
|
||||||
|
|
||||||
|
// Ergebnistabelle erzeugen und abholen.
|
||||||
|
String sql = "SELECT * FROM `"+ conf.getString("db.table")+"` WHERE `user`='"+id+"' ORDER BY `display`";
|
||||||
|
return query.executeQuery(sql);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOG.error("SQLException: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,7 +1,13 @@
|
||||||
apiserver {
|
apiserver {
|
||||||
port = 8111 //Port to listen to
|
port = 8111 //Port to listen to
|
||||||
refreshTime = 60 //Refresh Time in Seconds
|
refreshTime = 60 //Refresh Time in Seconds
|
||||||
|
db{
|
||||||
|
server =
|
||||||
|
user =
|
||||||
|
password =
|
||||||
|
database =
|
||||||
|
table =
|
||||||
|
}
|
||||||
monitors {
|
monitors {
|
||||||
monitor1 {
|
monitor1 {
|
||||||
Title = ""
|
Title = ""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue