Created
September 29, 2013 12:40
-
-
Save tomoTaka01/6752192 to your computer and use it in GitHub Desktop.
get the temperature from RaspberryPi and show it on the JavaFX8 screen.
The temperature and time show up every 5 seconds.
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
import java.time.LocalTime; | |
import java.time.ZoneId; | |
import java.time.format.DateTimeFormatter; | |
import javafx.application.Application; | |
import javafx.concurrent.ScheduledService; | |
import javafx.concurrent.Task; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.text.Text; | |
import javafx.stage.Stage; | |
import javafx.util.Duration; | |
import javax.ws.rs.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
/** | |
* | |
* @author tomo | |
*/ | |
public class RaspberryPi extends Application { | |
private final TemperatureService service = new TemperatureService(); | |
@Override | |
public void start(Stage primaryStage) { | |
VBox root = new VBox(); | |
// line1 | |
HBox line1 = new HBox(); | |
line1.setPadding(new Insets(10)); | |
line1.setSpacing(10); | |
Label temperatureLable1 = new Label("Temperature:"); | |
final Text temperatureText1 = new Text(); | |
Button button = new Button("get temperature"); | |
button.setOnAction(t -> | |
temperatureText1.setText(getTemperature()) | |
); | |
primaryStage.setTitle("Hello Raspberry Pi!"); | |
line1.getChildren().addAll(temperatureLable1,temperatureText1, button); | |
// line2 | |
HBox line2 = new HBox(); | |
line2.setPadding(new Insets(10)); | |
line2.setSpacing(10); | |
Label temperatureLable2 = new Label("Temperature:"); | |
final Text temperatureText2 = new Text(); | |
Label timeLabel = new Label("Time:"); | |
final Text timeText = new Text(); | |
line2.getChildren().addAll(temperatureLable2, temperatureText2, timeLabel, timeText); | |
// line3 | |
HBox line3 = new HBox(); | |
line3.setPadding(new Insets(10)); | |
line3.setSpacing(10); | |
Button startBtn = new Button("start get temperature"); | |
startBtn.setOnAction(t -> { | |
service.reset(); | |
service.start(); | |
}); | |
Button stopBtn = new Button("stop get temperature"); | |
stopBtn.setOnAction(t -> { | |
service.cancel(); | |
temperatureText2.setText(null); | |
timeText.setText(null); | |
}); | |
service.setPeriod(Duration.seconds(5)); | |
service.setOnSucceeded(t -> { | |
temperatureText2.setText(service.getValue()); | |
LocalTime time = LocalTime.now(ZoneId.of("America/Los_Angeles")); | |
timeText.setText(time.format(DateTimeFormatter.ofPattern("hh:mm:ss"))); | |
}); | |
line3.getChildren().addAll(startBtn, stopBtn); | |
root.getChildren().addAll(line1, line2, line3); | |
primaryStage.setScene(new Scene(root, 500, 200)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
private String getTemperature() { | |
Client client = ClientBuilder.newClient(); | |
String uriTemplate = "http://192.168.1.11:8080/things"; | |
return client.target(uriTemplate).path("/humidity").request().get(String.class); | |
} | |
} | |
class TemperatureService extends ScheduledService<String>{ | |
Client client = ClientBuilder.newClient(); | |
static final String uriTemperature = "http://192.168.1.11:8080/things"; | |
@Override | |
protected Task<String> createTask() { | |
return new Task<String>() { | |
@Override | |
protected String call() throws Exception { | |
return client.target(uriTemperature).path("/humidity").request().get(String.class); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment