Setup.py – Configure Switches Using Crontab

Overview

Setup is both a utility command and a library implemented in python. It is initialized by the json config file defining the switches and their schemas (described on the previous blog post).

 json                   registers    +---------------------+
config +----> setup.py +-----------> |                     |
                    +                |   Crontab daemon    |
                    |                |                     |
                    |                +---------+-----------+
                    |                          |            
                    |                          |  calls     
                    |                          |  at XX:YY  
                    |                          |            
                    |    direct                v            
                    +--------------------->  turn.py        
                         call                               
The program reads each chunk of definitions in the config file translating the schemas into ‘turn on’ and ‘turn off’ commands which are registered in the crontab daemon of linux. In case the actual time is within an interval, the ‘turn on’ command is called directly. Registered on/off commands are run by crontab at the specified time e.g. sunset. The turn.py command is used to turn on and off, it is also a simple python program – presented in “Using python to control telldus”. The setup command might be run from the command line by supplying the json config file or if using it as a library interfacing it with other programs or a webserver.


Design

The setup.py program is divided into three classes SetupSchema, SetupCron and SetupTurn. The class SetupSchema is called from main to parse the configuration json file (described in former post). For each new definition of a switch a callback is executed to interested instances. In this case there are two instances interested, one from the class SetupCron and the other from the class SetupTurn – see image below. The class SetupCron registers ‘turn on’/’turn off’ commands in the crontab daemon, the class SetupTurn on the other side checks if the switch shall be on/off right now and executes turn.py directly.

setup.py                                 
+---------------------------------------+
|                subscribes             |
|                                       |
|                    +--> SetupCron.py  |
|                    |                  |
|  SetupSchema.py    |                  |
|                    |                  |
|                    +--> SetupTurn.py  |
|                                       |
+---------------------------------------+
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s