Java Embedded Challenge for Raspberry Pi(Part 2)

When I was waiting for my team's members at third day of the Raspberry Pi challenge in JavaOne2013, I just wanted to add some function in my previous little application.Java Embedded Challenge for Raspberry Pi
This code shows you how to use JavaFX8 ScheduleService, DateTime API as well as lambda expression.
(I just wanted to use ScheduleService which I learned in JavaOne2013)

The screen 1 shows temperature and time in second line.They shows up every 5 seconds.
And the temperature comes through the Raspberry Pi sensor.

  • screen 1

When you click the [start get temperature] button, the TemperatureService starts(see code 1,2).
Thanks to the information(Introduction to JavaFX on Raspberry Pi) which @ posted, it is easy to get the temperature.

  • code1(TemperatureService.java)
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);   // <--- get the temperature by JAX-RS
            }
        };
    }
  • code 2(RaspberryPi.java)
    private final TemperatureService service = new TemperatureService();
…
        service.setPeriod(Duration.seconds(5));   // <--- set the duration here(every 5 seconds)
        Button startBtn = new Button("start get temperature");
        startBtn.setOnAction(t -> {           // <--- the service(TemperatureService) starts here
            service.reset();
            service.start();
        });

The temperature and time are set after TemperatureService succeeded.(see code 3)
Thank to these blogs I have already learned about JSR310 before, so it was not hard.

  1. java8 Date and Time API
  2. 続・今日から始めるJava8 - JSR-310 Date and Time API - Taste of Tech Topics

You can get some ideas from above blogs even they are written in Japanese.

  • code 3(RaspberryPi.java)
        service.setOnSucceeded(t -> {
            temperatureText2.setText(service.getValue());    // <--- when the service succeeded, the temperature is set here 
            LocalTime time = LocalTime.now(ZoneId.of("America/Los_Angeles"));  // <--- This is the way how to get the local time
            timeText.setText(time.format(DateTimeFormatter.ofPattern("hh:mm:ss")));  
        });

When you click the [stop get temperature] button, the TemperatureService stops(see code 4).

  • code 4(RaspberryPi.java)
        Button stopBtn = new Button("stop get temperature");
        stopBtn.setOnAction(t -> {           // <--- the service(TemperatureService) stops here
            service.cancel();
            temperatureText2.setText(null);
            timeText.setText(null);
        });

Thanks to the JavaFX8 ScheduleService, it becomes easy to implement this kind of service.So in the nutshell I think we should use JavaFX8!
The whole code is here Temperature on JavaFX8

P.S.
I want to say "thank you" to those who attended this event again.I really appreciate it.
When the members came to the room, I was surprised that my team's project has already completed.
I felt terribly sorry, because I did nothing on the project.
But they said that you are one of our team. Then finally they put my name on the presentation contents.
I was deeply impressed by their kindness... :-)

I hope I can come to San Francisco for JavaOne next year!

Just keep coding.....