Skip to content

Commit

Permalink
Merge branch 'graphs'
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Jan 26, 2017
2 parents 364078d + c8d6b73 commit bf35f94
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 11 deletions.
Binary file not shown.
94 changes: 93 additions & 1 deletion SolArduino-desktop/src/SolArduino/Controller.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package SolArduino;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
Expand All @@ -20,6 +21,7 @@
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.ResourceBundle;
Expand All @@ -39,16 +41,25 @@ public class Controller implements Initializable{

private Timer timerUp; // access is global to cancel it on button release
private Timer timerDown; // access is global to cancel it on button release
private Timer animationTimer; // access is global to be able to cancel it

private int timeout = 1000; // timeout for sending up/down requests
private String ip = "http://192.168.8.42/?"; // ip address from the Arduino
private String currentVersionString = "Version 1.1"; // current version of the desktop app
private String currentVersionString = "Version 1.2"; // current version of the desktop app
private String lastVersionString;
private String checkVersionLink = "https://raw.githubusercontent.com/PHPirates/SolArduino/" +
"master/SolArduino-desktop/version.txt";
private String jarLink = "https://github.com/PHPirates/SolArduino/raw/master/SolArduino-desktop/" +
"out/artifacts/SolArduino_desktop_jar/SolArduino-desktop.jar";

private boolean animationRunning = false; // boolean that keeps track of whether an animation is going on

// array with animation speeds.
// first half are for backwards, second half for forwards
private int[] animationSpeed = {100, 200, 300, 500, 750, 750, 500, 300, 200, 100};
private int middle = animationSpeed.length/2; // default for count
// int to keep track of where in the animationSpeed we are, and thus whether animation goes forwards or backwards
private int count = middle;

private long[][] data; // contains the times and angles from the csv files

Expand Down Expand Up @@ -227,9 +238,63 @@ public void run() {
}

@FXML protected void todayGraph() {
if(animationRunning){
stopAnimation();
}
graph.setData(getGraphData(getToday()));
}

@FXML protected void nextDayGraph() {
LocalDate nextDay = datePicker.getValue(); // get date that's on DatePicker
nextDay = nextDay.plusDays(1); // add one day

graph.setData(getGraphData(localDateToCalendar(nextDay))); // display new graph
}

@FXML protected void previousDayGraph() {
LocalDate previousDay = datePicker.getValue();
previousDay = previousDay.minusDays(1);

graph.setData(getGraphData(localDateToCalendar(previousDay)));
}

@FXML protected void playAnimationForward() {
if(count < animationSpeed.length-1) { // to avoid IndexOutOfBound exceptions
if(animationRunning) { // check if there is an animation going on
animationTimer.cancel();
}

// increase count before getting the speed, if we increase count afterwards,
// the animation would first go faster if we press back and only go slower/backwards the second time
count++;
int speed = animationSpeed[count];
playAnimation(speed);
}

}

@FXML protected void playAnimationBackward() {
if(count > 0){
if(animationRunning) {
animationTimer.cancel();
}

count--;
int speed = animationSpeed[count];
playAnimation(speed);

}

}

@FXML protected void stopAnimation() {
if(animationRunning) { // check if animation is running
animationTimer.cancel();
animationRunning = false;
count = middle; // set count to the default value
}
}

@FXML protected void checkVersion() {
System.out.println("checking version");
checkVersionOnline(checkVersionLink); // check what the last version is from the txt file online
Expand All @@ -252,6 +317,33 @@ public void run() {
}
}

public void playAnimation(int speed) {
animationTimer = new Timer(); // create new timer
animationRunning = true;
TimerTask animationTimerTask = new TimerTask() {

@Override
public void run() {
Platform.runLater(()->{ // runLater to avoid not being on fx-application thread
// if count is higher than the default, the animation goes forward. Backwards when it's lower.
if(count >= middle) {
nextDayGraph();
} else {
previousDayGraph();
}
});
}
};
animationTimer.schedule(animationTimerTask,10,speed);
}

public Calendar localDateToCalendar(LocalDate localDate) {
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); // first convert LocalDate to Date
Calendar cal = Calendar.getInstance();
cal.setTime(date); // convert Date to Calendar
return cal;
}

/**
* @param day for which data is returned
* @return list that contains data for graph
Expand Down
2 changes: 1 addition & 1 deletion SolArduino-desktop/src/SolArduino/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Main extends Application {
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("interface.fxml"));
primaryStage.setTitle("SolArduino");
Scene scene = new Scene(root, 650, 350);
Scene scene = new Scene(root, 750, 400);
// read the css this way so we don't get problems when creating an executable JAR
scene.getStylesheets().add(getClass().getResource("resources/stylesheet.css").toExternalForm());
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("ic_solarduino.png")));
Expand Down
53 changes: 46 additions & 7 deletions SolArduino-desktop/src/SolArduino/interface.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,56 @@
</yAxis>
</LineChart>

<Text text="Pick a day: "
GridPane.columnIndex="1"
GridPane.rowIndex="0"/>
<DatePicker fx:id="datePicker"
<GridPane GridPane.columnIndex="0"
GridPane.rowIndex="3"
alignment="CENTER"
hgap="10"
vgap="10">

<Button text="&lt;&lt;"
GridPane.columnIndex="0"
GridPane.rowIndex="0"
onAction="#playAnimationBackward"/>

<Button text="&gt;&gt;"
GridPane.columnIndex="1"
GridPane.rowIndex="0"
onAction="#playAnimationForward"/>

<Button text="stop"
GridPane.columnIndex="2"
GridPane.rowIndex="0"
onAction="#stopAnimation"/>
</GridPane>

<Text fx:id="text"
text="Pick a day: "
GridPane.columnIndex="1"
GridPane.rowIndex="1">
GridPane.rowIndex="0"
GridPane.columnSpan="3"/>

<Button fx:id="buttonPreviousDay"
text="&lt;"
GridPane.columnIndex="1"
GridPane.rowIndex="1"
minWidth="25"
onAction="#previousDayGraph"/>

<DatePicker fx:id="datePicker"
GridPane.columnIndex="2"
GridPane.rowIndex="1">
</DatePicker>

<Button fx:id="buttonNextDay"
text="&gt;"
GridPane.columnIndex="3"
GridPane.rowIndex="1"
minWidth="25"
onAction="#nextDayGraph"/>

<!-- today button -->
<Button
GridPane.columnIndex="2"
GridPane.columnIndex="4"
GridPane.rowIndex="1"
text="Today"
minWidth="80"
Expand All @@ -186,7 +224,8 @@
<TableView fx:id="table"
GridPane.columnIndex="1"
GridPane.rowIndex="2"
GridPane.columnSpan="2">
GridPane.columnSpan="4"
GridPane.rowSpan="2">
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
Expand Down
10 changes: 9 additions & 1 deletion SolArduino-desktop/src/SolArduino/resources/stylesheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
-fx-background-color: #00979C, white;
}

#buttonNextDay {
-fx-padding: 5px;
}

#buttonPreviousDay {
-fx-padding: 5px;
}

/* up and down button */
#buttonUp {
-fx-focus-color: transparent;
Expand Down Expand Up @@ -79,4 +87,4 @@

#buttonDown:pressed {
-fx-background-image: url("down_pressed.png");
}
}
2 changes: 1 addition & 1 deletion SolArduino-desktop/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Version 1.1
Version 1.2

0 comments on commit bf35f94

Please sign in to comment.