RasPi does the Home Automation (Part III): ‘Let’s Put It All Together’

Hi,
now it’s time to put it all together:
1st: a mini Custom Control for each gear, looking like this:

Gear

Its controller takes a Device to trigger DeviceControl to switch the radio controlled gears:

public class DevicePane extends AnchorPane {

    @FXML
    private Button offButton;
    @FXML
    private Button onButton;
    @FXML
    private Text deviceNameText;


    public DevicePane(Device device) {
        init(device);
    }

    private void init(final Device device) {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/DevicePane.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException ex) {
            Logger.getLogger(DevicePane.class.getName()).log(Level.SEVERE, null, ex);
        }

        deviceNameText.setText(device.getName());

        offButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                DeviceControl.get().turnOff(device);
            }
        });

        onButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                DeviceControl.get().turnOn(device);
            }
        });
    }
}

Finally the main controller HomeControlBoardController creates DevicePanes based on the configuration:

public class HomeControlBoardController {

    @FXML
    private Button exitButton;
    @FXML
    private AnchorPane root;
    @FXML
    private VBox devicesPane;

    public HomeControlBoardController() {
    }

    @FXML
    void initialize() {
        assert devicesPane != null : "fx:id=\"devicesPane\" was not injected: check your FXML file 'HomeControlBoard.fxml'.";
        assert exitButton != null : "fx:id=\"exitButton\" was not injected: check your FXML file 'HomeControlBoard.fxml'.";
        assert root != null : "fx:id=\"root\" was not injected: check your FXML file 'HomeControlBoard.fxml'.";

        // Running on RasPi always provide a way out instead of kill command
        final EventHandler<KeyEvent> exitOnCrtlCEventHandler =
                new EventHandler<KeyEvent>() {
            @Override
            public void handle(final KeyEvent keyEvent) {
                if (keyEvent.isControlDown() && KeyCode.C.equals(keyEvent.getCode())) {
                    Platform.exit();
                    keyEvent.consume();
                }
            }
        };
        root.setOnKeyPressed(exitOnCrtlCEventHandler);
        
        List<Device> devices = DeviceLoader.load();

        for (Device device : devices) {
            addDevice(device);
        }
    }

    private void addDevice(Device device) {
        DevicePane devicePane = new DevicePane(device);
        devicesPane.getChildren().add(devicePane);
    }

    @FXML
    public void exit() {
        Platform.exit();
    }
}

So finally (by now) it looks like this:

HomeControl2

Next: RasPi does the Home Automation (Part IV): Cut the ‘rope’

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>