Last weekend I've got the idea that it might make sense to add sections and areas to the Medusa Clock control. The visualization looks like follows...
Sections and Areas
The orange part is the area and the green part the section. Sections will be drawn on top of areas. The same principle exists in the Medusa Gauge control. The Gauge uses instances of the Section class and the Clock of the TimeSection class. Both of these classes have a method named checkForValue(). This method takes a double (Gauge) or a LocalTime (Clock) as a parameter and checks if this value is inside of the section that is defined by a start and a stop value.
The Section and TimeSection class will fire events for the following cases
- The current value entered the section
- The current value left the section
With this approach you can attach a listener to the section and enable the setCheckSectionForValue(true) in either the Gauge or Clock control to activate this functionality.
In a clock control that can be really useful to control things like switching lights for example. Let's take a look at a simple example.
We will switch on a light in our garden from 7pm to 10pm by using the TimeSection in the Clock control.
The first thing we need to do is creating the TimeSection like follows:
TimeSection gardenLightOn =
TimeSectionBuilder.create()
.start(LocalTime.of(19, 00, 00))
.stop(LocalTime.of(22, 00, 00))
.onTimeSectionEntered(event ->
System.out.println("Garden light on"))
.onTimeSectionLeft(event ->
System.out.println("Garden light off"))
.color(Color.rgb(255, 128, 0, 0.5))
.build();
As you can see we define the start and stop values by LocalTime instances and add an EventHandler with the onTimeSectionEntered() and onTimeSectionLeft() calls. Then we define a color for the section and that's it.
Now we have to create the Clock control and set the sections like follows:
Clock clock = ClockBuilder.create()
.sectionsVisible(true)
.sections(gardenLightOn)
.checkSectionsForValue(true)
.running(true)
.build();
The result of this will then look like this...
As soon as it will be 7pm you will see an output on the console with "Garden light on" and at 10pm it will say "Garden light off".
So in this case we only used a System.out.println command to show some action but you might want to send a MQTT message to your device that switches on or off the lights in the garden.
The area is in principle exactly the same just with a different visualization as you can see on the first image on this post. Oh and before I forget it...you can add multiple sections and areas to the Clock and also the Gauge control.
This was one way to use the Medusa Clock to control something but there is another one.
Alarms
You could also use Alarms to control things.
The Medusa Clock also takes a list of Alarm objects. You could either create a simple Alarm and react on the Alarm in the setOnAlarm() method with an EventHandler or you can create an Alarm that executes a so called Command. But let's take a look at the code to create a simple Alarm...
Alarm simpleAlarm =
AlarmBuilder.create()
.time(LocalDateTime.now().plusSeconds(2))
.repetition(Repetition.ONCE)
.text("Simple Alarm")
.build();
As you can see it's really simple, you just define the time and the repetition, add a text and now we can add the Alarm to the Clock as follows...
Clock clock = ClockBuilder.create()
.alarmsEnabled(true)
.alarms(simpleAlarm)
.onAlarm(alarmEvent ->
System.out.println(alarmEvent.ALARM.getText()))
.running(true)
.build();
Two seconds after starting our code we will see the "Simple Alarm" message on the console...very easy right :)
In case you would like to create specific Alarms you will find the @FunctionalInterface called Command that only has one method named execute(). This method will be called when an Alarm is triggered and a Command is provided. So with this you could create a Command for switching on/off lights like follows...
class LightOn implements Command {
@Override public void execute() {
System.out.println("Switch light on");
}
}
class LightOff implements Command {
@Override public void execute() {
System.out.println("Switch light off");
}
}
LightOn lightOn = new LightOn();
LightOff lightOff = new LightOff();
Alarm alarmLightOn =
AlarmBuilder.create()
.time(LocalDateTime.now().plusSeconds(5))
.repetition(Repetition.ONCE)
.text("Light On")
.command(lightOn)
.build();
Alarm alarmLightOff =
AlarmBuilder.create()
.time(LocalDateTime.now().plusSeconds(10))
.repetition(Repetition.ONCE)
.text("Light off")
.command(lightOff)
.build();
So we created two classes that implement the Command interface and in this case we only output something on the console but you could implement whatever you want in that classes. Then we create two Alarms that take the created command classes.
Now we just have to create the clock control and add the alarms like this...
Clock clock = ClockBuilder.create()
.alarmsEnabled(true)
.alarms(alarmLightOn, alarmLightOff)
.onAlarm(event ->
System.out.println(LocalTime.now() +
":" +
event.ALARM.getText()))
.running(true)
.build();
When you start that code you should see something like the following on your console...
So the first Alarm was triggered 5 sec after the program started and 10 sec after the program was started the second Alarm was triggered.
To be honest I was not sure if it makes sense to implement that functionality in a Clock control but this makes it very easy for me to use a JavaFX Clock control on e.g. a Raspberry Pi with a display and at the same time I'm able to control things with it without using extra code :)
The code can be found on github...
The code can be found on github...
So that's it for today...keep coding... :)
No comments:
Post a Comment