-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial networking setup is complete. At the moment, received current…
… and desired temperatures are just spat out to the console.
- Loading branch information
Showing
6 changed files
with
100 additions
and
34 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package networking; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
public interface networking { | ||
// Listen for the current temperature on port 7000 | ||
static final int receiveCurrentTempPORT = 7000; | ||
// Send the current temperature on port 7000 | ||
static final int sendCurrentTempPORT = 7000; | ||
// Listen for the desired temperature on port 7001 | ||
static final int receiveDesiredTempPORT = 7001; | ||
// Send the desired temperature on port 7001 | ||
static final int sendDesiredTempPORT = 7001; | ||
|
||
// Listens on receiveCurrentTempPORT and outputs the received string to console. | ||
public static void receiveCurrentTemp() throws Exception { | ||
ServerSocket listener = new ServerSocket(receiveCurrentTempPORT); | ||
try { | ||
// This stays running continuously once called. | ||
while (true) { | ||
Socket socket = listener.accept(); | ||
try { | ||
BufferedReader input = | ||
new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
int currentTemperature = Integer.parseInt(input.readLine()); | ||
System.out.printf("The current temperature is:", currentTemperature); | ||
} finally { | ||
socket.close(); | ||
} | ||
} | ||
} | ||
finally { | ||
listener.close(); | ||
} | ||
} | ||
|
||
// Listens on receiveDesiredTempPORT and outputs the received string to console. | ||
public static void receiveDesiredTemp() throws Exception { | ||
ServerSocket listener = new ServerSocket(receiveDesiredTempPORT); | ||
try { | ||
while (true) { | ||
Socket socket = listener.accept(); | ||
try { | ||
BufferedReader input = | ||
new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
int currentTemperature = Integer.parseInt(input.readLine()); | ||
System.out.printf("The desired temperature is:", currentTemperature); | ||
} finally { | ||
socket.close(); | ||
} | ||
} | ||
} | ||
finally { | ||
listener.close(); | ||
} | ||
} | ||
|
||
// Sends the current temperature on port sendCurrentTempPORT. | ||
public static void sendCurrentTemp(int currentTemperature, String thermostatIP) throws Exception { | ||
Socket sender = new Socket(thermostatIP, sendCurrentTempPORT); | ||
try { | ||
while (true) { | ||
PrintWriter out = new PrintWriter(sender.getOutputStream(), true); | ||
out.println(currentTemperature); | ||
} | ||
} finally { | ||
sender.close(); | ||
} | ||
} | ||
|
||
// Sends the desired temperature on port sendCurrentTempPORT. | ||
public static void sendDesiredTemp(int desiredTemperature, String thermostatIP) throws Exception { | ||
Socket sender = new Socket(thermostatIP, sendDesiredTempPORT); | ||
try { | ||
while (true) { | ||
PrintWriter out = new PrintWriter(sender.getOutputStream(), true); | ||
out.println(desiredTemperature); | ||
} | ||
} finally { | ||
sender.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package testing; | ||
|
||
import networking.networking; | ||
|
||
public class Networking_Test implements networking { | ||
public void Networking_test() { | ||
this.serverCurrentTemperatureListener(); | ||
} | ||
|
||
public void serverCurrentTemperatureListener() { | ||
this.serverCurrentTemperatureListener(); | ||
} | ||
} |