diff --git a/src/networking/ServerNetworking.java b/src/networking/ServerNetworking.java index 242b112..ba8c66e 100644 --- a/src/networking/ServerNetworking.java +++ b/src/networking/ServerNetworking.java @@ -57,6 +57,7 @@ public void sendDesiredTemp(int desiredTemperature, String thermostatIP) } } + /* // Receive multicast messages from the thermostats public void receiveMutlticast() throws Exception { // Opens the receiving port @@ -80,4 +81,5 @@ public void receiveMutlticast() throws Exception { multicastReceiver.close(); } } + */ } diff --git a/src/networking/ThermostatNetworking.java b/src/networking/ThermostatNetworking.java index b390815..2771650 100644 --- a/src/networking/ThermostatNetworking.java +++ b/src/networking/ThermostatNetworking.java @@ -10,38 +10,10 @@ import java.net.Socket; public class ThermostatNetworking { - - String serverIP; - - // Listens on receiveDesiredTempPORT and outputs the received int to console - public void receiveDesiredTemp() throws Exception { - // Open a receiver socket - ServerSocket listener = new ServerSocket(7001); - try { - while (true) { - // Sets the socket to an accept state - Socket socket = listener.accept(); - try { - // Creates a new input stream reader - BufferedReader input = new BufferedReader(new InputStreamReader - (socket.getInputStream())); - // Parses the input stream - int currentTemperature = Integer.parseInt(input.readLine()); - // Outputs the desired temperature to the console - System.out.printf("The desired temperature is: %d \n", currentTemperature); - } finally { - socket.close(); - } - } - } - finally { - listener.close(); - } - } // Sends the current temperature on port sendCurrentTempPORT - public void sendInfo(int currentTemperature, String serverIP) - throws Exception { + public void sendInfo(String currentTemperature, String serverIP) throws Exception { + int currentTempInt = Integer.parseInt(currentTemperature); // Open a sending port Socket sender = new Socket(serverIP, 7000); try { @@ -49,7 +21,7 @@ public void sendInfo(int currentTemperature, String serverIP) // Instantiate a printwriter so we can write to the output stream PrintWriter out = new PrintWriter(sender.getOutputStream(), true); // Send out the value of currentTemperature - out.println(currentTemperature); + out.println(currentTempInt); } } finally { // Immediately close the socket when finished. diff --git a/src/server/server.java b/src/server/server.java index f775f69..be129f8 100644 --- a/src/server/server.java +++ b/src/server/server.java @@ -1,5 +1,6 @@ package server; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStreamReader; import java.io.Serializable; import java.net.ServerSocket; @@ -40,7 +41,7 @@ public server() throws Exception { String serialnumber = "1.1"; Database dataStorage = new Database(this); dataStorage.createUser(email, adminpassword, ip, null); - // this.startServerListenerThread(); + this.startServerListenerThread(); } public boolean createUser(String email, String password, String ip, String serialpermission) { @@ -85,8 +86,9 @@ public String getDevicesFromEmail(String email) { public void sendDesiredTemp(int desiredTemperature, String thermostatIP) { this.sendDesiredTemp(desiredTemperature, thermostatIP); } - // Threading - /* + + // Threaded receiver function + // Listens on 7000 and outputs the received int to console public void startServerListenerThread() { Runnable runnable = new Runnable() { public void run() { @@ -102,8 +104,10 @@ public void run() { BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); // Parses the input stream int currentTemperature = Integer.parseInt(input.readLine()); - // Outputs the desired temperature to the console and writes to disk - System.out.printf("The desired temperature is: %d \n", currentTemperature); + // Outputs the current temperature to the console and writes to disk + System.out.printf("The current temperature is: %d \n", currentTemperature); + String currentTempString = Integer.toString(currentTemperature); + //this.setCurrentTempForSerialNumber(); } finally { socket.close(); } @@ -112,13 +116,16 @@ public void run() { listener.close(); } } - } finally { + } catch (IOException e) { + e.printStackTrace(); + } + finally { } } }; } - */ + public void datastructureLogEvent(String query) { } diff --git a/src/thermostat/ThermostatDevice.java b/src/thermostat/ThermostatDevice.java index 7b63e36..d42b87c 100644 --- a/src/thermostat/ThermostatDevice.java +++ b/src/thermostat/ThermostatDevice.java @@ -2,7 +2,11 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.net.ServerSocket; +import java.net.Socket; import java.util.ArrayList; import java.util.List; @@ -37,6 +41,7 @@ public ThermostatDevice(Integer updateIntervalInSeconds){ this.clock = new Timer(updateIntervalInSeconds*1000, this); this.clock.start(); + this.startThermostatListenerThread(); } @@ -61,5 +66,44 @@ public void actionPerformed(ActionEvent e) { sensor.requestNewDataPointReading(); } } + + // Threaded receiver function + // Listens on 7001 and outputs the received int to console + public void startThermostatListenerThread() { + Runnable runnable = new Runnable() { + public void run() { + try { + ServerSocket listener = new ServerSocket(7001); + while (true) { + try { + while (true) { + //sets the socket to an accept state + Socket socket = listener.accept(); + try { + // Creates a new input stream reader + BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + // Parses the input stream + int currentTemperature = Integer.parseInt(input.readLine()); + // Outputs the desired temperature to the console and writes to disk + System.out.printf("The desired temperature is: %d \n", currentTemperature); + String currentTempString = Integer.toString(currentTemperature); + //this.setCurrentTempForSerialNumber(); + } finally { + socket.close(); + } + } + } finally { + listener.close(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + finally { + + } + } + }; + } }