Java tutorial
/* * 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 bizlogic; /** * * @author Teemu */ import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.Date; import java.sql.SQLException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; import org.json.simple.parser.ParseException; public class Command { public static int Process(String _command, Connection con, PrintWriter out) throws SQLException { try { System.out.print("Processing command: "); System.out.println(_command); String string = _command; String[] _array = string.split(","); String command = _array[0]; switch (command) { case "addSensor": bizlogic.Sensors.add(con, _array[1], _array[2], _array[3], _array[4], _array[5], _array[6]); bizlogic.Sensors.list(con); break; case "delSensor": bizlogic.Sensors.del(con, _array[1]); bizlogic.Sensors.list(con); break; case "listSensors": bizlogic.Sensors.list(con); out.println("sensors.json ok"); break; case "addRecord": bizlogic.Records.add(con, _array[1], _array[2], _array[3], _array[4]); bizlogic.Records.list(con); break; case "delRecord": bizlogic.Records.del(con, _array[1]); bizlogic.Records.list(con); break; case "startRecords": bizlogic.Records.setColumn(con, _array[1], "USERCONF.LOG_LIST", "RUNNING", "true"); // (conDB, records, column, value) //bizlogic.Records.list(con); out.println("startRecords " + _array[1]); break; case "stopRecords": bizlogic.Records.setColumn(con, _array[1], "USERCONF.LOG_LIST", "RUNNING", "false"); // (conDB, records, column, value) //bizlogic.Records.list(con); out.println("stopRecords " + _array[1]); break; case "listRecords": bizlogic.Records.list(con); out.println("listRecords ok"); break; case "writeCSV": bizlogic.Records.writeCSV(con, _array[1]); out.println("writeCSV " + _array[1] + " Value" + " Time"); break; case "setConf": String[] confString = new String[] { "date", "+%D", "--set", _array[3] + " " + _array[2] }; System.out.println("confString=" + Arrays.toString(confString)); Process p = java.lang.Runtime.getRuntime().exec(confString); int setConf_status = p.waitFor(); System.out.println("confStatus = " + Integer.toString(setConf_status)); if (setConf_status != -1) { out.println("setConf " + "ok"); } else { out.println("ERROR Date not set"); } break; case "serverTime": DateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); Date date = new Date(System.currentTimeMillis()); out.println("Server time: " + sdf.format(date)); break; case "changeIP": if (bizlogic.modSettings.validateIP(_array[1])) { System.out.println("changeIP to " + _array[1]); bizlogic.modSettings.resetIP(); if (!_array[1].equals("0.0.0.0")) { bizlogic.modSettings.setIP(_array[1]); } out.println("Server rebooting to set the IP"); out.close(); con.close(); java.lang.Runtime.getRuntime().exec("sudo reboot"); } else { out.println("ERROR: Bad IP address." + " Enter a valid IPv4 addess"); System.out.println("Bad IP: " + _array[1]); java.lang.Runtime.getRuntime().exec( "pid2=`ps aux | " + "grep \"[d]l_hwlogic\" | awk '{print $2}'`\n" + "kill -9 $pid2"); } break; default: break; } return 1; } catch (IOException | ParseException | InterruptedException ex) { Logger.getLogger(Command.class.getName()).log(Level.WARNING, null, ex); return -1; } } }