
import data.DataBaseHelper;
import data.Log;
import data.SerialConnector;
import server.RBLSocketServer;
import server.RBLWebSocketServer;
import util.Config;
import util.NetworkUtil;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by Peter Mösenthin.
 *
 * Main class for the RaspberryLife server application.
 */
public class RaspberryLife {

    public static final String DEBUG_TAG = RaspberryLife.class.getSimpleName();
    public static Thread serverThread = null;
    public static Thread webServerThread = null;


    public static void main(String[] args){
        Log.printLogHeader();
        Config.readConfig();
        //Config.dumpConfig();
        NetworkUtil.listIPAddresses();
        // RaspberryHome WebSocketServer
        webServerThread = new Thread(new Runnable() {
            public void run() {
                Log.add(DEBUG_TAG,
                        "Starting WebSocketServer");
                RBLWebSocketServer webServer = new RBLWebSocketServer();
                webServer.start();
            }
        });

        // RaspberryHome Application Server
        serverThread = new Thread(new Runnable() {
                public void run() {
                Log.add(DEBUG_TAG, "Starting Java socket server");
                RBLSocketServer server = new RBLSocketServer();
                server.start(Config.get().getInt("socket.java_port"));
            }
        });

        // Start threads
        webServerThread.start();
        serverThread.start();

        // Initialize the serial connector for module communication
        SerialConnector.init();

        // Initialize database
        DataBaseHelper.setUpInitial();

    }




}

@@ -79,266 +79,175 @@
 ort 
-server.RBLSocketServer;%0Aimport server.RBLWebSocketServer;%0Aimport util.Config;%0Aimport util.NetworkUtil;%0A%0Aimport java.net.InetAddress;%0Aimport java.net.NetworkInterface;%0Aimport java.net.SocketException;%0Aimport java.security.Timestamp;%0Aimport java.util.ArrayList
+org.quartz.Scheduler;%0Aimport org.quartz.SchedulerException;%0Aimport org.quartz.impl.StdSchedulerFactory;%0Aimport server.RBLSocketServer;%0Aimport server.RBLWebSocketServer
 ;%0Aim
@@ -255,26 +255,19 @@
 ort 
-java.
 util.C
-alendar
+onfig
 ;%0Aim
@@ -275,52 +275,24 @@
 ort 
-java.util.Enumeration;%0Aimport java.util.List
+util.NetworkUtil
 ;%0A%0A/
@@ -594,16 +594,317 @@
  null;%0A%0A
+    public static boolean runDebugSetup = false;%0A%0A    //----------------------------------------------------------------------------------------------%0A    //                                      MAIN%0A    //----------------------------------------------------------------------------------------------%0A
 %0A    pub
@@ -1081,28 +1081,13 @@
     
-// RaspberryHome Web
+start
 Sock
@@ -1090,24 +1090,27 @@
 SocketServer
+();
 %0A        web
@@ -1110,85 +1110,63 @@
     
-webServerThread = new Thread(new Runnable() %7B%0A            public void run() %7B
+startWebSocketServer();%0A        initSerialConnection();
 %0A   
@@ -1174,85 +1174,77 @@
     
-        Log.add(DEBUG_TAG,%0A                        %22Starting WebSocketServer%22
+initDatabase();%0A        if(runDebugSetup)%7B%0A            initScheduler(
 );%0A 
@@ -1254,108 +1254,140 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-RBLWebSocketServer webServer = new RBLWebSocketServer();%0A                webServer.start();%0A
+//----------------------------------------------------------------------------------------------%0A    //                
     
@@ -1390,34 +1390,32 @@
             
-%7D%0A
         
 %7D);%0A%0A       
@@ -1406,56 +1406,163 @@
     
-%7D);%0A%0A        // RaspberryHome Application 
+  STARTUP%0A    //----------------------------------------------------------------------------------------------%0A%0A    public static void startSocket
 Server
+()%7B
 %0A   
@@ -1621,20 +1621,16 @@
         
-    
 public v
@@ -1873,76 +1873,412 @@
 %7D);%0A
+        serverThread.start();
 %0A    
+%7D%0A%0A
     
-// Start threads%0A        webServerThread.start();%0A        s
+public static void startWebSocketServer()%7B%0A        webServerThread = new Thread(new Runnable() %7B%0A            public void run() %7B%0A                Log.add(DEBUG_TAG,%0A                        %22Starting WebSocketServer%22);%0A                RBLWebSocketServer webServer = new RBLWebSocketServer();%0A                webServer.start();%0A            %7D%0A        %7D);%0A        webS
 erve
@@ -2290,29 +2290,31 @@
 ad.start();%0A
-%0A
     
+%7D%0A%0A
     // Initi
@@ -2373,48 +2373,157 @@
     
-    SerialConnector.init();%0A%0A        // 
+public static void initSerialConnection()%7B%0A        SerialConnector.init();%0A    %7D%0A%0A    public static void initDatabase()%7B%0A        Log.add(DEBUG_TAG, %22
 Init
@@ -2527,17 +2527,19 @@
 nitializ
-e
+ing
  databas
@@ -2539,16 +2539,19 @@
 database
+%22);
 %0A       
@@ -2581,16 +2581,565 @@
 itial();
+%0A    %7D%0A%0A    public static void initScheduler()%7B%0A        Log.add(DEBUG_TAG, %22Initializing scheduler%22);%0A        Scheduler scheduler = null;%0A        try %7B%0A            scheduler = StdSchedulerFactory.getDefaultScheduler();%0A        %7D catch (SchedulerException e) %7B%0A            Log.add(DEBUG_TAG, %22Unable to create scheduler%22);%0A        %7D%0A        if(scheduler != null)%7B%0A            try %7B%0A                scheduler.start();%0A            %7D catch (SchedulerException e) %7B%0A                Log.add(DEBUG_TAG, %22Unable to start scheduler%22);%0A            %7D%0A        %7D
 %0A%0A    %7D%0A

split main initilization and add debug setup