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>
|
||||
<version>1.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mariadb.jdbc</groupId>
|
||||
<artifactId>mariadb-java-client</artifactId>
|
||||
<version>3.5.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>OebbApi</finalName>
|
||||
|
|
|
@ -11,12 +11,14 @@ import org.json.JSONObject;
|
|||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
public class Main {
|
||||
private static final String version = "0.7";
|
||||
private static final HashMap<String, Integer> data = new HashMap<>();
|
||||
private static final String version = "0.8";
|
||||
private static final HashMap<String, HashMap<String, Integer>> udata = new HashMap<>();
|
||||
public static Settings conf;
|
||||
static OebbCheck check = new OebbCheck();
|
||||
static long time;
|
||||
|
@ -45,7 +47,7 @@ public class Main {
|
|||
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) {
|
||||
server.createContext("/oebbapi", new OebbHandler());
|
||||
server.setExecutor(null); // creates a default executor
|
||||
|
@ -64,17 +66,35 @@ public class Main {
|
|||
}
|
||||
|
||||
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")));
|
||||
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")));
|
||||
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")));
|
||||
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")));
|
||||
HashMap<String, Integer> data = new HashMap<>();
|
||||
try{
|
||||
ResultSet rs = MariaDB.load_users();
|
||||
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();
|
||||
}
|
||||
|
||||
static class OebbHandler implements HttpHandler {
|
||||
static class OebbHandler implements HttpHandler {
|
||||
@Override
|
||||
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"));
|
||||
try {
|
||||
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();
|
||||
Date updatetime = new Date(time);
|
||||
dat.put("interval",conf.getInt("refreshTime")*1000);
|
||||
|
@ -133,7 +153,7 @@ public class Main {
|
|||
monitor2.put("label",conf.getString("monitors.monitor1.Title"));
|
||||
monitor2.put("labelColor",conf.getString("labelcolor"));
|
||||
monitor2.put("fullDraw",true);
|
||||
int zug1 = data.get(conf.getString("monitors.monitor1.DepartureTime"));
|
||||
int zug1 = udata.get(user).get("1");
|
||||
makeLabel(monitor2, zug1);
|
||||
display.put(monitor2);
|
||||
|
||||
|
@ -141,7 +161,7 @@ public class Main {
|
|||
monitor3.put("label",conf.getString("monitors.monitor2.Title"));
|
||||
monitor3.put("labelColor",conf.getString("labelcolor"));
|
||||
monitor3.put("fullDraw",true);
|
||||
int zug2 = data.get(conf.getString("monitors.monitor2.DepartureTime"));
|
||||
int zug2 = udata.get(user).get("2");
|
||||
makeLabel(monitor3, zug2);
|
||||
display.put(monitor3);
|
||||
|
||||
|
@ -149,7 +169,7 @@ public class Main {
|
|||
monitor4.put("label",conf.getString("monitors.monitor3.Title"));
|
||||
monitor4.put("labelColor",conf.getString("labelcolor"));
|
||||
monitor4.put("fullDraw",true);
|
||||
int zug3 = data.get(conf.getString("monitors.monitor3.DepartureTime"));
|
||||
int zug3 = udata.get(user).get("3");
|
||||
makeLabel(monitor4, zug3);
|
||||
display.put(monitor4);
|
||||
|
||||
|
@ -157,10 +177,11 @@ public class Main {
|
|||
monitor5.put("label",conf.getString("monitors.monitor4.Title"));
|
||||
monitor5.put("labelColor",conf.getString("labelcolor"));
|
||||
monitor5.put("fullDraw",true);
|
||||
int zug4 = data.get(conf.getString("monitors.monitor4.DepartureTime"));
|
||||
int zug4 = udata.get(user).get("4");
|
||||
makeLabel(monitor5, zug4);
|
||||
display.put(monitor5);
|
||||
dat.put("displays",display);
|
||||
System.out.println(display);
|
||||
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 {
|
||||
port = 8111 //Port to listen to
|
||||
refreshTime = 60 //Refresh Time in Seconds
|
||||
|
||||
db{
|
||||
server =
|
||||
user =
|
||||
password =
|
||||
database =
|
||||
table =
|
||||
}
|
||||
monitors {
|
||||
monitor1 {
|
||||
Title = ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue