Skip to content

Commit

Permalink
More adjustments to the threaded sections. They actually start thread…
Browse files Browse the repository at this point in the history
…s this time.
  • Loading branch information
pid1 committed Apr 27, 2015
1 parent 9e88ba7 commit 2e78879
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
21 changes: 14 additions & 7 deletions src/server/server.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ public static server getInstance() {
// Database object
Database dataStorage;
// Threaded network listener so we don't block other operations
Thread serverListenerThread;
Thread serverCurrentTempListener;
// String to hold the current temperature
String currentTempString;

public server() throws Exception {
// Default admin user
String adminpassword = "admin";
String adminPassword = "admin";
String ip = "127.0.0.1";
String email = "admin@uconn.edu";
String serialnumber = "1.1";
String serialNumber = "1.1";
Database dataStorage = new Database(this);
dataStorage.createUser(email, adminpassword, ip, null);
dataStorage.createUser(email, adminPassword, ip, serialNumber);
this.startServerListenerThreadCurrentTemp();
}

Expand Down Expand Up @@ -85,14 +87,15 @@ public String getDevicesFromEmail(String email) {
}

// Sends the desired temperature on port sendDesiredTempPORT
public void sendDesiredTemp(int desiredTemperature, String thermostatIP) throws Exception {
public void sendDesiredTemp(String 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
int desiredTempInt = Integer.parseInt(desiredTemperature);
out.println(desiredTemperature);
}
} finally {
Expand Down Expand Up @@ -138,8 +141,11 @@ public void run() {
}
}
};
serverCurrentTempListener = new Thread(runnable);
serverCurrentTempListener.start();
}


/*
// Threaded receiver function
// Listens on 7000 and outputs the received int to console
public void startServerListenerThreadSerialNumber() {
Expand All @@ -158,7 +164,7 @@ public void run() {
// 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);
System.out.printf("This Raspberry Pi's serial number is: %d \n", currentTemperature);
String currentTempString = Integer.toString(currentTemperature);
//this.setCurrentTempForSerialNumber();
} finally {
Expand All @@ -178,6 +184,7 @@ public void run() {
}
};
}
*/

public void datastructureLogEvent(String query) {
}
Expand Down
14 changes: 10 additions & 4 deletions src/thermostat/ThermostatDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class ThermostatDevice extends ThermostatNetworking implements Deviceable
List<Actuatorable> actuatorList;
private Timer clock;
private String serialNumber;
String desiredTempString;
// Threaded listener thread
Thread thermostatListener;

public ThermostatDevice(Integer updateIntervalInSeconds){
try {
Expand Down Expand Up @@ -97,7 +100,8 @@ public void sendCurrentTemp(String currentTemp) throws IOException {
// 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);
int currentTempInt = Integer.parseInt(currentTemp);
out.println(currentTempInt);
}
} finally {
// Immediately close the socket when finished.
Expand Down Expand Up @@ -132,10 +136,10 @@ public void run() {
// Creates a new input stream reader
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Parses the input stream
int currentTemperature = Integer.parseInt(input.readLine());
int desiredTemperature = 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);
System.out.printf("The desired temperature is: %d \n", desiredTemperature);
desiredTempString = Integer.toString(desiredTemperature);
//this.setCurrentTempForSerialNumber();
} finally {
socket.close();
Expand All @@ -153,6 +157,8 @@ public void run() {
}
}
};
thermostatListener = new Thread(runnable);
thermostatListener.start();
}

}

0 comments on commit 2e78879

Please sign in to comment.