Skip to content
Snippets Groups Projects
Antifurto.java 4.86 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    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 {
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    	private boolean statoAntifurto = true;
    	private boolean statoAllarme = false;
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    
    	@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}
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    			if(Helper.compareText(requestMethod,"GET")) {
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    				String response = "{\"stato\":true,\"soglia\":43,\"attenzione\":21,\"allarme\":false,\"sensori\":[{\"in0\":8},{\"in2\":3}]}";
    				statoAntifurto = true;
    				statoAllarme = false;
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    				Helper.sendResponse(response,exchange);
    			} else {
    				Helper.methodNotAllowed(exchange);
    			}
    			return;
    		}
    		
    
    		if(Helper.compareText(lastfile,"stato")) {//get, put {se l'antifurto e' attivo + aggiornamento}
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    			if(Helper.compareText(requestMethod,"GET")) {
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    				String response = "{\"stato\":true}";
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    				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	{
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    					boolean exStato = statoAntifurto;
    
    					statoAntifurto = Boolean.parseBoolean(stato);
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    					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);
    				}
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    			} else {
    				Helper.methodNotAllowed(exchange);
    			}
    			return;
    		}
    		
    
    		if(Helper.compareText(lastfile,"allarme")) {//get, put {se l'allarme sta suonando + aggiornamento}
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    			if(Helper.compareText(requestMethod,"GET")) {
    
    				String response = "{\"dati\":\"ecco l'allarme\"}";
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    				Helper.sendResponse(response,exchange);
    			} else if(Helper.compareText(requestMethod,"PUT")) {
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    				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);
    				}
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    			} else {
    				Helper.methodNotAllowed(exchange);
    			}
    			return;
    		}
    		
    
    		if(Helper.compareText(lastfile,"attenzione")) {//put {valore della progress bar}
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    			if(Helper.compareText(requestMethod,"PUT")) {
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    				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);
    				}
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    			} else {
    				Helper.methodNotAllowed(exchange);
    			}
    			return;
    		}
    		
    
    		if(Helper.compareText(lastfile,"soglia")) {//put {valore scelto dall'utente per la soglia}
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    			if(Helper.compareText(requestMethod,"PUT")) {
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    				System.out.println(Helper.readBody(exchange.getRequestBody()));
    
    				String response = "{\"dati\":\"soglia aggiornata\"}";
    
    Alfredo Chissotti's avatar
    Alfredo Chissotti committed
    				Helper.sendResponse(response,exchange);
    			} else {
    				Helper.methodNotAllowed(exchange);
    			}
    			return;
    		}
    		
    		Helper.pageNotFound(exchange);
    	}
    
    }