br.com.arduinoweb.controller.ArduinoController.java Source code

Java tutorial

Introduction

Here is the source code for br.com.arduinoweb.controller.ArduinoController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.arduinoweb.controller;

import br.com.arduinoweb.model.Command;
import br.com.arduinoweb.model.Log;
import br.com.arduinoweb.model.Output;
import br.com.arduinoweb.model.Status;
import br.com.arduinoweb.model.User;
import br.com.arduinoweb.service.ArduinoService;
import br.com.arduinoweb.service.CommandService;
import br.com.arduinoweb.service.LogService;
import br.com.arduinoweb.service.OutputService;
import br.com.arduinoweb.service.StatusService;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Rodolfo
 */
@Controller
public class ArduinoController {

    //<editor-fold defaultstate="collapsed" desc="Comandos">
    @RequestMapping("/OutputArduino")
    public ModelAndView OutputArduino(HttpSession session) {
        ModelAndView mv = new ModelAndView("Admin/OutputArduino");
        mv.addObject("listOutput", new OutputService().getOutputs());
        mv.addObject("listCommand", new CommandService().getCommands());
        mv.addObject("listStatus", new StatusService().getStatus());
        return mv;
    }

    @RequestMapping("actions/saveCommand")
    public ModelAndView saveCommand(Command command, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("redirect:/OutputArduino");
        Status status = new StatusService().getStatusById(Integer.parseInt(request.getParameter("statusId")));
        Output output = new OutputService().getOutputById(Integer.parseInt(request.getParameter("outputId")));
        command.setStatus(status);
        command.setOutput(output);
        mv.addObject("Message", new CommandService().SaveCommand(command));
        return mv;
    }

    @RequestMapping("actions/deleteCommand")
    public ModelAndView deleteCommand(Command command) {
        ModelAndView mv = new ModelAndView("redirect:/OutputArduino");
        new CommandService().DeleteCommand(command);
        mv.addObject("Message", "Comando exclu&iacute;do com sucesso!");
        return mv;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Log">
    @RequestMapping("/Log")
    public ModelAndView Log(HttpSession session) {
        ModelAndView mv = new ModelAndView("Admin/Log");
        mv.addObject("listLog", new LogService().getLogs());
        return mv;
    }

    @RequestMapping("actions/CleanLog")
    public String CleanLog() {
        new LogService().CleanLog();
        return "redirect:/Log";
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Arduino">
    @RequestMapping("actions/ArduinoExecuta")
    public String ArduinoExecuta(Command command, HttpSession session) throws IOException {
        if (session.getAttribute("conexao").equals("btn-success")) {
            command = new CommandService().getCommandById(command.getCommandId());
            new ArduinoService(session).ArduinoExecuta(command.getCommand());
            User user = (User) session.getAttribute("usuarioLogado");
            new LogService().SaveLog(new Log(command, user));
        }
        return "redirect:/Home";
    }
    /*
     public void ArduinoLog(String parametros[]) throws ParseException, SQLException {
     int acao = Integer.parseInt(parametros[1]);
     int sensor = Integer.parseInt(parametros[0]);
     saidaService.changeSaida(acao, sensor);
     if (acao > 3) {
     Comando comando = controleService.getComandoByStatusAndPosicao(acao, sensor);
     String descricao = comando.getStatus().getDescricao();
     String local = comando.getDescricao();
     Log log = new Log(descricao, new SystemProperties().getDate(), "Arduino", local);
     logService.saveLog(log);
     }
     }*/
    //</editor-fold>

}