Thus far, we have explored the mechanisms for sensing inputs from the home, reasoning based on these inputs and actuating devices in the home.
To create context sensitive home automation apps we also need to understand the time at which particular behaviours occur within the home. It also must be possible to quickly test this time-based reasoning over the entire range of time. To this end, we've included a clock object in the CHAOS platform which allows the developer to query the current time, and step through time to test the performance of their apps over time with a variety of home inputs.
This tutorial will introduce the usage of the CHAOS clock object to enable temporal reasoning in home automation apps.
The upper-right of the CHAOS Demonstrator shows the clock representing the current time. When you click one of the '+' buttons on the clock it will advance time by either an hour or a minute and an indicator will appear to confirm that we are in simulated time. You can click the 'x' on the indicator to return to normal time. These time values can be accessed via the self.clock object which is available by default to all apps.
|Regular Time:||Simulated Time|
Every time the time value changes, one or more signals are emitted. Upon viewing the clock's tooltip we can see what signals are emitted and when:
The minuteChanged signal is emitted once per minute, the hourChanged signal is emitted once per hour and the yearChanged signal is emitted once per year. Hence, the only time all 5 signals are emitted at the same time is when we transition from one year to the next.
All 5 of these signals contain the same attributes. Each one contains 5 integers, representing; year, month, day of month, hour and minute, in that order. As in previous lessons, any of these time change signals can be connected to methods:
As you can imagine, now that we can query the current time and/or trigger methods on time changes, our apps can evaluate the current time to decide what action to take.
If we wish to have a time-based trigger, we could use the code:
Alternatively, if we had code that triggered on some other stimulus, like a door opening, we can access the clock object and evaluate the current time:
Try this functionality for yourself. The code will work in both real and simulated time. So if you're feeling a little impatient and don't want to wait for hours for the correct time to arise, use the time '+' buttons to advance to the correct time while the apps are running.
Let's solidify our understanding of clock usage by developing a simple app.
We want to write an app that encourages the user to go to bed on time by deactivating the TV at a certain time. To this end, we want to write an app that:
When you want to compare with your solution, click Expand below.
The highlighted lines are the lines we've added to the skeleton app to achieve this functionality.
Be aware that testing this code will require you to hit the 23.30 mark exactly. If you advance the hour value you are likely to jump past the 23.30 time. Advance the time by one minute at a time to ensure 23.30 occurs.
Also be aware that to turn the TV back on to retest the code, you need to turn on the "tvSwitchActuator" device icon first, then press standby on the TV icon.
This code merely provides a simple example, a more complex example could check what room the user is in before taking any action. We could also use the smart TV's powerOff() method to make sure the TV is turned off, even if the user doesn't have a plug actuator connected to their TV. We will learn about this extra functionality in later tutorials. Turning off the TV in two different ways allows redundancy which enables an app to work in a wider range of homes with different device setups.
This tutorial has presented how we can incorporate temporal information into an app by using the self.clock object. The next tutorial will advance onto the generation of rapid timed events by using timers in our code.<< - <Prev - Next>