Skip to content

Commit

Permalink
More networking additions. Split up the information transmission meth…
Browse files Browse the repository at this point in the history
…ods.
  • Loading branch information
pid1 committed Apr 27, 2015
1 parent 3347507 commit b2eb55e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 58 deletions.
46 changes: 0 additions & 46 deletions src/networking/ServerNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/networking/ThermostatNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
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 {
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(currentTempInt);
out.println(currentTemperature);
}
} finally {
// Immediately close the socket when finished.
Expand Down
63 changes: 58 additions & 5 deletions src/server/server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
53 changes: 49 additions & 4 deletions src/thermostat/ThermostatDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,7 +42,7 @@ public ThermostatDevice(Integer updateIntervalInSeconds){

this.clock = new Timer(updateIntervalInSeconds*1000, this);
this.clock.start();
this.startThermostatListenerThread();
this.startThermostatListenerThreadDesiredTemp();

}

Expand All @@ -52,13 +53,57 @@ public void sensorUpdateEvent(Sensorable sensor) {
}

private void updateServer(DataObject<Integer> 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) {
Expand All @@ -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) {
Expand Down

0 comments on commit b2eb55e

Please sign in to comment.