first commit
This commit is contained in:
214
src/main/java/Web/WebServer.java
Normal file
214
src/main/java/Web/WebServer.java
Normal file
@@ -0,0 +1,214 @@
|
||||
package Web;
|
||||
|
||||
import Other.SomeCodes;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.UploadedFile;
|
||||
import io.javalin.util.JavalinException;
|
||||
import io.javalin.websocket.*;
|
||||
import lombok.extern.java.Log;
|
||||
import org.tinylog.Logger;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static Other.SomeCodes.*;
|
||||
import static io.javalin.apibuilder.ApiBuilder.*;
|
||||
|
||||
|
||||
@SuppressWarnings({"unused"})
|
||||
public class WebServer {
|
||||
private final Javalin app;
|
||||
private final Set<WsContext> connectedWebsocketClients = ConcurrentHashMap.newKeySet();
|
||||
public WebServer(WebsocketEvent event, String webusername, String webpassword){
|
||||
app = Javalin.create(config -> {
|
||||
config.staticFiles.add("/html");
|
||||
config.router.apiBuilder(()-> path("setting", () ->{
|
||||
get(ctx -> ctx.json(SettingInfo.getInstance()));
|
||||
path("audiofile",()-> post(ctx -> {
|
||||
Logger.info("api /setting/audiofile");
|
||||
String audiofile1 = ctx.formParam("1");
|
||||
String audiofile2 = ctx.formParam("2");
|
||||
String audiofile3 = ctx.formParam("3");
|
||||
String audiofile4 = ctx.formParam("4");
|
||||
String audiofile5 = ctx.formParam("5");
|
||||
Logger.info("audiofile1: {}", audiofile1);
|
||||
Logger.info("audiofile2: {}", audiofile2);
|
||||
Logger.info("audiofile3: {}", audiofile3);
|
||||
Logger.info("audiofile4: {}", audiofile4);
|
||||
Logger.info("audiofile5: {}", audiofile5);
|
||||
|
||||
Properties prop = SomeCodes.LoadProperties("config.properties");
|
||||
prop.setProperty("audiofile1", audiofile1!=null?audiofile1:"");
|
||||
prop.setProperty("audiofile2", audiofile2!=null?audiofile2:"");
|
||||
prop.setProperty("audiofile3", audiofile3!=null?audiofile3:"");
|
||||
prop.setProperty("audiofile4", audiofile4!=null?audiofile4:"");
|
||||
prop.setProperty("audiofile5", audiofile5!=null?audiofile5:"");
|
||||
if (SaveProperties(prop, "config.properties")){
|
||||
Logger.info("audiofile saved");
|
||||
ctx.status(200);
|
||||
} else {
|
||||
Logger.error("Failed to save audiofile");
|
||||
ctx.status(400);
|
||||
}
|
||||
}));
|
||||
path("uploadaudiofile", ()-> post(ctx -> {
|
||||
UploadedFile file = ctx.uploadedFile("file");
|
||||
if (file!=null){
|
||||
try {
|
||||
Path targetsave = audioPath.resolve(file.filename());
|
||||
Files.copy(file.content(), targetsave);
|
||||
Logger.info("Uploaded file: {}, size: {} saved at {}", file.filename(),file.size(), targetsave);
|
||||
} catch (Exception e){
|
||||
Logger.error("Failed to save uploaded file: {}, Message: {}", file.filename(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}));
|
||||
path("weblogin", ()-> post(ctx -> {
|
||||
String username = ctx.formParam("username");
|
||||
String password = ctx.formParam("password");
|
||||
Properties prop = SomeCodes.LoadProperties("config.properties");
|
||||
prop.setProperty("WebUsername", ValidString(username)?username:"admin");
|
||||
prop.setProperty("WebPassword", ValidString(password)?password:"bandara");
|
||||
if (SaveProperties(prop, "config.properties")){
|
||||
ctx.status(200);
|
||||
} else {
|
||||
ctx.status(400);
|
||||
}
|
||||
}));
|
||||
path("camera",()-> post(ctx -> {
|
||||
String camera_ip = ctx.formParam("ip");
|
||||
String camera_port = ctx.formParam("port");
|
||||
String camera_username = ctx.formParam("username");
|
||||
String camera_password = ctx.formParam("password");
|
||||
|
||||
Properties prop = SomeCodes.LoadProperties("config.properties");
|
||||
prop.setProperty("Camera_ip", ValidString(camera_ip)?camera_ip:"192.168.0.4");
|
||||
prop.setProperty("Camera_port", ValidString(camera_port)?camera_port:"80");
|
||||
prop.setProperty("Camera_user", ValidString(camera_username)?camera_username:"root");
|
||||
prop.setProperty("Camera_password", ValidString(camera_password)?camera_password:"password");
|
||||
if (SaveProperties(prop, "config.properties")){
|
||||
ctx.status(200);
|
||||
} else {
|
||||
ctx.status(400);
|
||||
}
|
||||
}));
|
||||
|
||||
}));
|
||||
});
|
||||
|
||||
app.get("/", ctx-> {
|
||||
if (ctx.sessionAttribute("username")==null) {
|
||||
// belum login
|
||||
ctx.redirect("/login.html");
|
||||
} else if (Objects.equals(ctx.sessionAttribute("username"), webusername)){
|
||||
// sudah login
|
||||
ctx.redirect("/index.html");
|
||||
} else {
|
||||
// sudah login tapi bukan username yang benar
|
||||
ctx.redirect("/login.html");
|
||||
}
|
||||
});
|
||||
|
||||
app.before("/index.html", ctx ->{
|
||||
if (ctx.sessionAttribute("username")==null){
|
||||
ctx.redirect("/login.html");
|
||||
}
|
||||
});
|
||||
|
||||
app.before("/setting.html", ctx ->{
|
||||
if (ctx.sessionAttribute("username")==null){
|
||||
ctx.redirect("/login.html");
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/login", ctx ->{
|
||||
String username = ctx.formParam("username");
|
||||
String password = ctx.formParam("password");
|
||||
if (Objects.equals(username, webusername) && Objects.equals(password, webpassword)){
|
||||
ctx.sessionAttribute("username", username);
|
||||
ctx.redirect("/index.html");
|
||||
} else {
|
||||
ctx.redirect("/login.html?error=Invalid username or password");
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/logout", ctx ->{
|
||||
ctx.sessionAttribute("username", null);
|
||||
ctx.redirect("/login.html");
|
||||
});
|
||||
|
||||
app.ws("/ws", ws -> {
|
||||
ws.onConnect(connectws);
|
||||
ws.onClose(closews);
|
||||
//ws.onError(errorws);
|
||||
ws.onMessage(ctx -> {
|
||||
try{
|
||||
//Logger.info("WebSocket message {}", ctx.message());
|
||||
WebsocketCommand command = ctx.messageAsClass(WebsocketCommand.class);
|
||||
if (event!=null) {
|
||||
WebsocketReply reply = event.onWebsocketCommand(command);
|
||||
if (reply!=null) ctx.sendAsClass(reply, WebsocketReply.class);
|
||||
}
|
||||
} catch (Exception e){
|
||||
Logger.error("Failed to parse WebSocketCommand message: {}", e.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Send Object message to all connected websocket clients
|
||||
* @param obj Object to send
|
||||
*/
|
||||
public void SendtoAll(Object obj){
|
||||
connectedWebsocketClients.forEach(wsContext -> wsContext.send(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the web server
|
||||
* @param localip Local IP address to bind
|
||||
* @param port Port to bind
|
||||
*/
|
||||
public void Start(String localip, int port){
|
||||
try{
|
||||
connectedWebsocketClients.forEach(WsContext::closeSession);
|
||||
app.start(localip, port);
|
||||
Logger.info("Web server started at {}:{}", localip, port);
|
||||
} catch (JavalinException e){
|
||||
Logger.error("Web server failed to start: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the web server
|
||||
*/
|
||||
public void Stop(){
|
||||
try{
|
||||
app.stop();
|
||||
Logger.info("Web server stopped");
|
||||
} catch (JavalinException e){
|
||||
Logger.error("Web server failed to stop: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
WsConnectHandler connectws = ws ->{
|
||||
Logger.info("WebSocket connected from {}", ws.host());
|
||||
//ws.headerMap().forEach((key, value) -> Logger.info("HeaderMap {}: {}", key, value));
|
||||
connectedWebsocketClients.add(ws);
|
||||
};
|
||||
|
||||
WsCloseHandler closews = ws ->{
|
||||
Logger.info("WebSocket closed from {}, code {}, reason {}", ws.host(), ws.status(), ws.reason());
|
||||
connectedWebsocketClients.remove(ws);
|
||||
};
|
||||
|
||||
//WsErrorHandler errorws = ws -> Logger.error("WebSocket error from {}, error {}", ws.host(), ws.error());
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user