Skip to content
Snippets Groups Projects
Commit 3cb44b5a authored by Alfredo Chissotti's avatar Alfredo Chissotti
Browse files

rimosse classi inutilizzate dal server

parent 5550c8c1
No related branches found
No related tags found
No related merge requests found
package code;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
public class Antifurto implements HttpHandler {
private boolean statoAntifurto = true;
private boolean statoAllarme = false;
@Override
public void handle(HttpExchange exchange) throws IOException {
URI requestURI = exchange.getRequestURI();
String path[] = requestURI.toString().split("/");
String lastfile = path[path.length - 1];
String requestMethod = exchange.getRequestMethod();
List<String> strlist = new ArrayList<>(); //serve List<String> a causa di .put("content-type", strlist);
strlist.add("text/json");
exchange.getResponseHeaders().put("content-type", strlist);//opzionale, per dire cosa c'e' nel body
if(Helper.compareText(lastfile,"antifurto")) {//get {stato, allarme, attenzione, soglia, sensori}
if(Helper.compareText(requestMethod,"GET")) {
String response = "{\"stato\":true,\"soglia\":43,\"attenzione\":21,\"allarme\":false,\"sensori\":[{\"in0\":8},{\"in2\":3}]}";
statoAntifurto = true;
statoAllarme = false;
Helper.sendResponse(response,exchange);
} else {
Helper.methodNotAllowed(exchange);
}
return;
}
if(Helper.compareText(lastfile,"stato")) {//get, put {se l'antifurto e' attivo + aggiornamento}
if(Helper.compareText(requestMethod,"GET")) {
String response = "{\"stato\":true}";
Helper.sendResponse(response,exchange);
} else if(Helper.compareText(requestMethod,"PUT")) {
String body = Helper.readBody(exchange.getRequestBody());
if(!Helper.checkJSON(body) || !body.contains("\"stato\"")) {
Helper.badRequest(exchange);
return;
}
String stato = body.split("\"stato\"",2)[1].split(":",2)[1].split(",",2)[0].split("}",2)[0].trim();
try {
boolean exStato = statoAntifurto;
statoAntifurto = Boolean.parseBoolean(stato);
if(exStato == statoAntifurto)
System.out.println("\tRicevuto stato antifurto gia' assegnato");
if(statoAllarme && !statoAntifurto) {
Helper.badRequest(exchange);
return;
}
String response = "{\"antifurto\":\"ok\",\"stato\":"+statoAntifurto+"}";
Helper.sendResponse(response,exchange);
} catch (Exception e) {
System.out.println("Conversion error.\t"+stato);
e.printStackTrace();
Helper.badRequest(exchange);
}
} else {
Helper.methodNotAllowed(exchange);
}
return;
}
if(Helper.compareText(lastfile,"allarme")) {//get, put {se l'allarme sta suonando + aggiornamento}
if(Helper.compareText(requestMethod,"GET")) {
String response = "{\"dati\":\"ecco l'allarme\"}";
Helper.sendResponse(response,exchange);
} else if(Helper.compareText(requestMethod,"PUT")) {
String body = Helper.readBody(exchange.getRequestBody());
if(!Helper.checkJSON(body) || !body.contains("\"stato\"")) {
Helper.badRequest(exchange);
return;
}
String stato = body.split("\"stato\"",2)[1].split(":",2)[1].split(",",2)[0].split("}",2)[0].trim();
try {
boolean exStato = statoAllarme;
statoAllarme = Boolean.parseBoolean(stato);
if(exStato == statoAllarme)
System.out.println("\tRicevuto stato allarme gia' assegnato");
if(statoAllarme && !statoAntifurto) {
Helper.badRequest(exchange);
return;
}
String response = "{\"allarme\":\"ok\",\"stato\":"+statoAllarme+"}";
Helper.sendResponse(response,exchange);
} catch (Exception e) {
System.out.println("Conversion error.\t"+stato);
e.printStackTrace();
Helper.badRequest(exchange);
}
} else {
Helper.methodNotAllowed(exchange);
}
return;
}
if(Helper.compareText(lastfile,"attenzione")) {//put {valore della progress bar}
if(Helper.compareText(requestMethod,"PUT")) {
String body = Helper.readBody(exchange.getRequestBody());
if(!Helper.checkJSON(body) || !body.contains("\"attenzione\"")) {
Helper.badRequest(exchange);
return;
}
String valore = body.split("\"attenzione\"",2)[1].split(":",2)[1].split(",",2)[0].split("}",2)[0].trim();
try {
int val = Integer.parseInt(valore);
String response = "{\"attenzione\":"+val+"}";
Helper.sendResponse(response,exchange);
} catch (Exception e) {
System.out.println("Conversion error.\t"+valore);
e.printStackTrace();
Helper.badRequest(exchange);
}
} else {
Helper.methodNotAllowed(exchange);
}
return;
}
if(Helper.compareText(lastfile,"soglia")) {//put {valore scelto dall'utente per la soglia}
if(Helper.compareText(requestMethod,"PUT")) {
System.out.println(Helper.readBody(exchange.getRequestBody()));
String response = "{\"dati\":\"soglia aggiornata\"}";
Helper.sendResponse(response,exchange);
} else {
Helper.methodNotAllowed(exchange);
}
return;
}
Helper.pageNotFound(exchange);
}
}
package code;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
public class Luci implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
URI requestURI = exchange.getRequestURI();
String path[] = requestURI.getPath().split("/");
String lastfile = path[path.length - 1];
String requestMethod = exchange.getRequestMethod();
List<String> strlist = new ArrayList<>(); //serve List<String> a causa di .put("content-type", strlist);
strlist.add("text/json");
exchange.getResponseHeaders().put("content-type", strlist);//opzionale, per dire cosa c'e' nel body
if(Helper.compareText(lastfile,"luci")) {//post, get [put, delete] {luogo e stato di tutte luci}
if(Helper.compareText(requestMethod,"GET")) {
String response = "[{\"luogo\":\"ingresso\",\"stato\":true,\"in\":\"in2\",\"out\":\"out2\"},{\"luogo\":\"cucina\",\"stato\":false,\"in\":\"in3\",\"out\":\"out3\"}]";
Helper.sendResponse(response,exchange);
// Helper.methodNotAllowed(exchange);
} else if(Helper.compareText(requestMethod,"POST")) {
String body = Helper.readBody(exchange.getRequestBody());
//check body is json or xml
if(!Helper.checkJSON(body) || !body.contains("\"luogo\"")) {
Helper.badRequest(exchange);
return;
}
String luogo = body.split("\"luogo\"",2)[1].split("\"",3)[1].trim();
String response = "{\"luogo\":\""+luogo+"\"}";
Helper.sendResponse(response,exchange);
} else {
Helper.methodNotAllowed(exchange);
}
return;
}
if(Helper.compareText(lastfile,"stato")) {//put {aggiorna lo stato di una luce}
if(Helper.compareText(requestMethod,"PUT")) {
String body = Helper.readBody(exchange.getRequestBody());
//check body is json or xml
if(!Helper.checkJSON(body) || !body.contains("\"luogo\"")) {
Helper.badRequest(exchange);
return;
}
String stato = body.split("\"stato\"",2)[1].split(":",3)[1].split(",",2)[0].split("}",2)[0].trim();
try {
boolean st = Boolean.parseBoolean(stato);
String response = "{\"luce\":\"ok\",\"stato\":"+st+"}";
Helper.sendResponse(response,exchange);
} catch (Exception e) {
System.out.println("Conversion error.\t"+stato);
e.printStackTrace();
Helper.badRequest(exchange);
}
} else {
Helper.methodNotAllowed(exchange);
}
return;
}
Helper.pageNotFound(exchange);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment