From b2eb55ebd78f6899d4968eb428343df75e85cc7d Mon Sep 17 00:00:00 2001 From: Jonathan Roemer Date: Mon, 27 Apr 2015 15:56:55 -0400 Subject: [PATCH] More networking additions. Split up the information transmission methods. --- src/networking/ServerNetworking.java | 46 ----------------- src/networking/ThermostatNetworking.java | 6 +-- src/server/server.java | 63 ++++++++++++++++++++++-- src/thermostat/ThermostatDevice.java | 53 ++++++++++++++++++-- 4 files changed, 110 insertions(+), 58 deletions(-) diff --git a/src/networking/ServerNetworking.java b/src/networking/ServerNetworking.java index ba8c66e..cde7b50 100644 --- a/src/networking/ServerNetworking.java +++ b/src/networking/ServerNetworking.java @@ -10,52 +10,6 @@ import java.net.Socket; public class ServerNetworking { - - String thermostatIP; - - // Listens on receiveCurrentTempPORT and outputs the received int to console - public void receiveCurrentTemp() throws Exception { - // Open a receiver socket - ServerSocket listener = new ServerSocket(7000); - try { - // This stays running continuously once called - 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 current temperature to the console - } finally { - socket.close(); - } - } - } - finally { - listener.close(); - } - } - - // Sends the desired temperature on port sendDesiredTempPORT - public void sendDesiredTemp(int desiredTemperature, String thermostatIP) - throws Exception { - // Opens a sending port - Socket sender = new Socket(thermostatIP, 7001); - try { - while (true) { - // Instantiate a printwriter so we can write to the output stream - PrintWriter out = new PrintWriter(sender.getOutputStream(), true); - // Send out the value of desiredTemperature - out.println(desiredTemperature); - } - } finally { - // Immediately close the socket when finished - sender.close(); - } - } /* // Receive multicast messages from the thermostats diff --git a/src/networking/ThermostatNetworking.java b/src/networking/ThermostatNetworking.java index 2771650..44c32f4 100644 --- a/src/networking/ThermostatNetworking.java +++ b/src/networking/ThermostatNetworking.java @@ -12,8 +12,8 @@ public class ThermostatNetworking { // Sends the current temperature on port sendCurrentTempPORT - public void sendInfo(String currentTemperature, String serverIP) throws Exception { - int currentTempInt = Integer.parseInt(currentTemperature); + public void sendInfo(int currentTemperature, String serverIP) + throws Exception { // Open a sending port Socket sender = new Socket(serverIP, 7000); try { @@ -21,7 +21,7 @@ public void sendInfo(String currentTemperature, String serverIP) throws Exceptio // 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(currentTempInt); + out.println(currentTemperature); } } finally { // Immediately close the socket when finished. diff --git a/src/server/server.java b/src/server/server.java index be129f8..13b9bac 100644 --- a/src/server/server.java +++ b/src/server/server.java @@ -2,6 +2,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.PrintWriter; import java.io.Serializable; import java.net.ServerSocket; import java.net.Socket; @@ -41,7 +42,7 @@ public server() throws Exception { String serialnumber = "1.1"; Database dataStorage = new Database(this); dataStorage.createUser(email, adminpassword, ip, null); - this.startServerListenerThread(); + this.startServerListenerThreadCurrentTemp(); } public boolean createUser(String email, String password, String ip, String serialpermission) { @@ -82,14 +83,66 @@ public void setDesiredTempForSerialNumber(String serialnumber, String desiredtem public String getDevicesFromEmail(String email) { return null; } + + // Sends the desired temperature on port sendDesiredTempPORT + public void sendDesiredTemp(int desiredTemperature, String thermostatIP) throws Exception { + // Opens a sending port + Socket sender = new Socket(thermostatIP, 7002); + try { + while (true) { + // Instantiate a printwriter so we can write to the output stream + PrintWriter out = new PrintWriter(sender.getOutputStream(), true); + // Send out the value of desiredTemperature + out.println(desiredTemperature); + } + } finally { + // Immediately close the socket when finished + sender.close(); + } + } - public void sendDesiredTemp(int desiredTemperature, String thermostatIP) { - this.sendDesiredTemp(desiredTemperature, thermostatIP); + // Threaded receiver function + // Listens on 7000 and outputs the received int to console + public void startServerListenerThreadCurrentTemp() { + Runnable runnable = new Runnable() { + public void run() { + try { + ServerSocket listener = new ServerSocket(7000); + 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 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(); + } + } + } finally { + listener.close(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + finally { + + } + } + }; } - + // Threaded receiver function // Listens on 7000 and outputs the received int to console - public void startServerListenerThread() { + public void startServerListenerThreadSerialNumber() { Runnable runnable = new Runnable() { public void run() { try { diff --git a/src/thermostat/ThermostatDevice.java b/src/thermostat/ThermostatDevice.java index ecb4284..2818de1 100644 --- a/src/thermostat/ThermostatDevice.java +++ b/src/thermostat/ThermostatDevice.java @@ -5,6 +5,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; @@ -41,7 +42,7 @@ public ThermostatDevice(Integer updateIntervalInSeconds){ this.clock = new Timer(updateIntervalInSeconds*1000, this); this.clock.start(); - this.startThermostatListenerThread(); + this.startThermostatListenerThreadDesiredTemp(); } @@ -52,13 +53,57 @@ public void sensorUpdateEvent(Sensorable sensor) { } private void updateServer(DataObject dataReading){ - this.sendInfo(dataReading.getValue(), this.serialNumber); + //this.sendIP("192.168.1.2"); + this.sendCurrentTemp(dataReading.getValue()); + //this.sendSerialNumber(this.serialNumber); } public static void main(String[] args){ ThermostatDevice device = new ThermostatDevice(5); while(true){} //stay alive } + + /* + public void sendIP(String ipAddress) { + // Sends the current temperature on port sendCurrentTempPORT + // Open a sending port + Socket sender = new Socket(, 7000); + try { + while (true) { + // 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); + } + } finally { + // Immediately close the socket when finished. + sender.close(); + } + } + } + */ + + // CONVERT THIS TO INT + public void sendCurrentTemp(String currentTemp) throws IOException { + // Sends the current temperature on port sendCurrentTempPORT + // Open a sending port + Socket sender = new Socket("192.168.1.3", 7000); + try { + while (true) { + // 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(currentTemp); + } + } finally { + // Immediately close the socket when finished. + sender.close(); + } + } + + //public void sendSerialNumber(String serialNumber) { + + //} @Override public void actionPerformed(ActionEvent e) { @@ -69,11 +114,11 @@ public void actionPerformed(ActionEvent e) { // Threaded receiver function // Listens on 7001 and outputs the received int to console - public void startThermostatListenerThread() { + public void startThermostatListenerThreadDesiredTemp() { Runnable runnable = new Runnable() { public void run() { try { - ServerSocket listener = new ServerSocket(7001); + ServerSocket listener = new ServerSocket(7002); while (true) { try { while (true) {