Merge branch 'javadoc'

# Conflicts:
#	src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java
This commit is contained in:
Lukas Heiligenbrunner 2020-03-13 14:50:47 +01:00
commit 5605a813d9
8 changed files with 253 additions and 2 deletions

View File

@ -8,6 +8,11 @@ import com.wasteinformationserver.mqtt.MqttService
import com.wasteinformationserver.website.Webserver
import java.io.IOException
/**
* application entry point
*
* @author Lukas Heiligenbrunner
*/
fun main() {
Log.setLevel(Log.DEBUG)
Info.init()

View File

@ -7,6 +7,11 @@ import com.wasteinformationserver.basicutils.Log.Log.warning
import com.wasteinformationserver.website.datarequests.login.LoginState
import java.io.IOException
/**
* Http handler to deliver all the main pages (index.html ...)
*
* @author Lukas Heiligenbrunner
*/
class MainPage : HttpHandler {
@Throws(IOException::class)
override fun handle(t: HttpExchange) {

View File

@ -10,6 +10,11 @@ import java.io.IOException
import java.net.BindException
import java.net.InetSocketAddress
/**
* class to create the website nodes at specific paths
*
* @author Lukas Heiligenbrunner
*/
class Webserver {
fun startserver() {
info("starting Webserver")

View File

@ -9,14 +9,25 @@ import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
/**
* all requests for Admin page
*
* @author Lukas Heiligenbrunner
*/
public class AdminRequests extends PostRequest {
@Override
public String request(HashMap<String, String> params) {
String result = "";
switch (params.get("action")) {
/**
* shut down the whole application
*/
case "shutdownserver":
System.exit(0);
break;
/**
* restart the server application
*/
case "restartserver":
final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
File currentJar = null;

View File

@ -12,6 +12,11 @@ import java.sql.SQLIntegrityConstraintViolationException
import java.text.SimpleDateFormat
import java.util.*
/**
* General Datarequests for Dashboard
*
* @author Lukas Heiligenbrunner
*/
class DataRequest : PostRequest() {
override fun request(params: HashMap<String, String>): String {
val sb = StringBuilder()
@ -24,6 +29,9 @@ class DataRequest : PostRequest() {
}
when (params["action"]) {
/**
* create a new city entry in db
*/
"newCity" -> {
sb.append("{")
debug(params.toString())
@ -62,6 +70,9 @@ class DataRequest : PostRequest() {
sb.append(",\"query\":\"ok\"")
sb.append("}")
}
/**
* return all defined cities from db
*/
"getAllCities" -> {
set = jdbc.executeQuery("select * from cities")
debug(set.toString())
@ -83,6 +94,9 @@ class DataRequest : PostRequest() {
sb.append(",\"query\":\"ok\"")
sb.append("}")
}
/**
* delete a specific city
*/
"deletecity" -> {
//DELETE FROM `cities` WHERE `id`=0
sb.append("{")
@ -105,6 +119,9 @@ class DataRequest : PostRequest() {
sb.append(",\"query\":\"ok\"")
sb.append("}")
}
/**
* returns all configured dates with its city and zone
*/
"getAllDates" -> {
set = jdbc.executeQuery("SELECT pickupdates.id,pickupdates.pickupdate,cities.userid,cities.name,cities.wastetype,cities.zone " +
"FROM `pickupdates` INNER JOIN `cities` ON pickupdates.citywastezoneid = cities.id")
@ -127,6 +144,9 @@ class DataRequest : PostRequest() {
sb.append(",\"query\":\"ok\"")
sb.append("}")
}
/**
* delete a specific date
*/
"deletedate" -> {
sb.append("{")
try {
@ -147,6 +167,9 @@ class DataRequest : PostRequest() {
sb.append(",\"query\":\"ok\"")
sb.append("}")
}
/**
* return version foot data
*/
"getversionandbuildtime" -> {
sb.append("{")
sb.append("\"version\" : \"" + Info.getVersion() + "\"")
@ -154,6 +177,9 @@ class DataRequest : PostRequest() {
sb.append(",\"query\":\"ok\"")
sb.append("}")
}
/**
* return head data with basic collection infos
*/
"getStartHeaderData" -> {
sb.append("{")
try {

View File

@ -8,6 +8,11 @@ import java.sql.ResultSet
import java.sql.SQLException
import java.util.*
/**
* Class for all requests on device Page
*
* @author Lukas Heiligenbrunner
*/
class DeviceRequest : PostRequest() {
override fun request(params: HashMap<String, String>): String {
val jdbc = JDBC.getInstance()
@ -19,6 +24,9 @@ class DeviceRequest : PostRequest() {
val sb = StringBuilder()
val deviceset: ResultSet
when (params["action"]) {
/**
* return all available devices
*/
"getdevices" -> {
deviceset = jdbc!!.executeQuery("SELECT * FROM `devices")
sb.append("{\"data\":[")
@ -55,6 +63,9 @@ class DeviceRequest : PostRequest() {
e.printStackTrace()
}
}
/**
* returns all available city names
*/
"getCitynames" -> {
deviceset = jdbc!!.executeQuery("select * from cities")
debug(deviceset.toString())
@ -76,6 +87,9 @@ class DeviceRequest : PostRequest() {
sb.append("}")
debug(sb.toString())
}
/**
* returns all available zones for specified city
*/
"getzones" -> {
deviceset = jdbc!!.executeQuery("select * from cities WHERE `name`='" + params["cityname"] + "' ORDER BY zone ASC")
debug(deviceset.toString())
@ -96,6 +110,9 @@ class DeviceRequest : PostRequest() {
}
sb.append("}")
}
/**
* returns all available waste types for specified zone and city
*/
"gettypes" -> {
deviceset = jdbc!!.executeQuery("select * from cities WHERE `name`='" + params["cityname"] + "' AND `zone`='" + params["zonename"] + "' ORDER BY zone ASC")
debug(deviceset.toString())
@ -116,6 +133,9 @@ class DeviceRequest : PostRequest() {
}
sb.append("}")
}
/**
* configure device and save infos to db
*/
"savetodb" -> try {
val cityset = jdbc!!.executeQuery("SELECT id from cities WHERE `name`='" + params["cityname"] + "' AND `zone`='" + params["zonename"] + "' AND `wastetype`='" + params["wastetype"] + "'")
cityset.last()
@ -132,6 +152,9 @@ class DeviceRequest : PostRequest() {
} catch (e: SQLException) {
e.printStackTrace()
}
/**
* delete a configured device from db
*/
"deleteDevice" -> {
try {
jdbc.executeUpdate("DELETE FROM devices WHERE `DeviceID`='" + params["id"] + "'")
@ -141,6 +164,9 @@ class DeviceRequest : PostRequest() {
}
sb.append("{\"status\":\"success\"}")
}
/**
* add new city/zone/type to db to existing one
*/
"addtodb" -> {
try {
val device = jdbc.executeQuery("SELECT * FROM cities WHERE name='" + params["cityname"] + "' AND wastetype='" + params["wastetype"] + "' AND zone='" + params["zonename"] + "'")
@ -152,6 +178,9 @@ class DeviceRequest : PostRequest() {
}
sb.append("{\"success\":true}")
}
/**
* return header information such as devicenumber and number of unconfigured devices
*/
"getheader" -> {
try {
var numberset = jdbc.executeQuery("SELECT * FROM devices")

View File

@ -0,0 +1,132 @@
package com.wasteinformationserver.website.datarequests;
import com.wasteinformationserver.basicutils.Log;
import com.wasteinformationserver.db.JDBC;
import com.wasteinformationserver.website.basicrequest.PostRequest;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
/**
* todo
*
* @author Lukas Heiligenbrunner
*/
public class NewDateRequest extends PostRequest {
@Override
public String request(HashMap<String, String> params) {
StringBuilder sb = new StringBuilder();
JDBC jdbc;
ResultSet set;
try {
jdbc = JDBC.getInstance();
} catch (IOException e) {
Log.Log.error("no connection to db");
return "{\"query\" : \"nodbconn\"}";
}
switch (params.get("action")) {
case "getCitynames":
set = jdbc.executeQuery("select * from cities");
Log.Log.debug(set.toString());
sb.append("{\"data\":[");
try {
String prev = "";
while (set.next()) {
if (prev.equals(set.getString("name"))) {
} else {
if (!set.isFirst()) {
sb.append(",");
}
sb.append("{\"cityname\":\"" + set.getString("name") + "\"}");
}
prev = set.getString("name");
}
} catch (SQLException e) {
e.printStackTrace();
}
sb.append("]");
sb.append(",\"query\":\"ok\"");
sb.append("}");
Log.Log.debug(sb.toString());
break;
case "getzones":
set = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC");
Log.Log.debug(set.toString());
sb.append("{\"data\":[");
try {
int prev = 42;
while (set.next()) {
if (prev == set.getInt("zone")) {
} else {
sb.append("{\"zone\":\"" + set.getInt("zone") + "\"}");
if (!set.isLast()) {
sb.append(",");
}
}
prev = set.getInt("zone");
}
} catch (SQLException e) {
e.printStackTrace();
}
sb.append("]");
sb.append(",\"query\":\"ok\"");
sb.append("}");
break;
case "gettypes":
set = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='"+params.get("zonename")+"' ORDER BY zone ASC");
Log.Log.debug(set.toString());
sb.append("{\"data\":[");
try {
String prev = "42";
while (set.next()) {
if (prev == set.getString("wastetype")) {
} else {
sb.append("{\"wastetype\":\"" + set.getString("wastetype") + "\"}");
if (!set.isLast()) {
sb.append(",");
}
}
prev = set.getString("wastetype");
}
} catch (SQLException e) {
e.printStackTrace();
}
sb.append("]");
sb.append(",\"query\":\"ok\"");
sb.append("}");
break;
case "newdate":
sb.append("{");
Log.Log.debug(params);
set = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zone") + "' AND `wastetype`='" + params.get("wastetype") + "'");
try {
set.last();
if (set.getRow() == 1) {
Log.Log.debug(set.getInt("id"));
int status = jdbc.executeUpdate("INSERT INTO `pickupdates`(`citywastezoneid`, `pickupdate`) VALUES ('" + set.getInt("id") + "','" + params.get("date") + "')");
if (status == 1) {
sb.append("\"status\" : \"success\"");
} else {
sb.append("\"status\" : \"error\"");
}
} else {
Log.Log.warning("city doesnt exist!");
sb.append("\"status\" : \"citydoesntexist\"");
}
} catch (SQLException e) {
e.printStackTrace();
}
sb.append(",\"query\":\"ok\"");
sb.append("}");
break;
}
return sb.toString();
}
}

View File

@ -1,12 +1,16 @@
package com.wasteinformationserver.website.datarequests.login;
/**
*
* singleton representing the login state of the user
*/
public class LoginState {
private static LoginState mythis = new LoginState();
/**
* get object
* @return LoginState instance
*/
public static LoginState getObject() {
return mythis;
}
@ -17,16 +21,26 @@ public class LoginState {
private String email;
private int permission;
boolean loggedin = false;
private boolean loggedin = false;
/**
* login the user
*/
public void logIn() {
loggedin = true;
}
/**
* logout the user
*/
public void logOut() {
loggedin = false;
}
/**
* set the account infos
* username, firstname, lastname, email and permission level
*/
public void setAccountData(String username, String firstname, String lastname, String email, int permission) {
this.username = username;
this.firstname = firstname;
@ -35,26 +49,50 @@ public class LoginState {
this.permission = permission;
}
/**
* check if user is logged in
* @return loginstate
*/
public boolean isLoggedIn() {
return loggedin;
}
/**
* get username
* @return username
*/
public String getUsername() {
return username;
}
/**
* get firstname
* @return firstname
*/
public String getFirstname() {
return firstname;
}
/**
* get lastname
* @return lastname
*/
public String getLastname() {
return lastname;
}
/**
* get email address
* @return mail address
*/
public String getEmail() {
return email;
}
/**
* get permission level
* @return level as int
*/
public int getPermission() {
return permission;
}