Light control daemon

Let's review another example where we implement a simple daemon using OOP that turns on/off lights at specified times of the day. In order to be able to perform tasks at scheduled times, we will make use of the schedule library (https://github.com/dbader/schedule). It could be installed as follows:

    sudo pip3 install schedule

Let's call our class, LightScheduler. It should be capable of accepting start and top times to turn on/off lights at given times. It should also provide override capabilities to let the user turn on/off lights as necessary. Let's assume that the light is controlled using PowerSwitch Tail II (http://www.powerswitchtail.com/Pages/default.aspx). It is interfaced as follows:

Raspberry Pi Zero interfaced to the PowerSwitch Tail II

The following is the LightSchedular class created:

class LightScheduler(object): 
"""A Python class to turn on/off lights"""

def __init__(self, start_time, stop_time):
self.start_time = start_time
self.stop_time = stop_time
# lamp is connected to GPIO pin2.
self.lights = OutputDevice(2)

Whenever an instance of LightScheduler is created, the GPIO pin is initialized to control the PowerSwitch Tail II. Now, let's add methods to turn on/off lights:

def init_schedule(self): 
# set the schedule
schedule.every().day.at(self.start_time).do(self.on)
schedule.every().day.at(self.stop_time).do(self.off)

def on(self):
"""turn on lights"""
self.lights.on()

def off(self):
"""turn off lights"""
self.lights.off()

In the init_schedule() method, the start and stop times that were passed as arguments are used to initialize schedule to turn on/off the lights at the specified times.

Put it together, we have:

import schedule 
import time
from gpiozero import OutputDevice

class LightScheduler(object):
"""A Python class to turn on/off lights"""

def __init__(self, start_time, stop_time):
self.start_time = start_time
self.stop_time = stop_time
# lamp is connected to GPIO pin2.
self.lights = OutputDevice(2)

def init_schedule(self):
# set the schedule
schedule.every().day.at(self.start_time).do(self.on)
schedule.every().day.at(self.stop_time).do(self.off)

def on(self):
"""turn on lights"""
self.lights.on()

def off(self):
"""turn off lights"""
self.lights.off()


if __name__ == "__main__":
lamp = LightScheduler("18:30", "9:30")
lamp.on()
time.sleep(50)
lamp.off()
lamp.init_schedule()
while True:
schedule.run_pending()
time.sleep(1)

In the preceding example, the lights are scheduled to be turned on at 6:30 p.m. and turned off at 9:30 a.m. Once the jobs are scheduled, the program enters an infinite loop where it awaits task execution. This example could be run as a daemon by executing the file at start-up (add a line called light_scheduler.py to /etc/rc.local). After scheduling the job, it will continue to run as a daemon in the background.

This is just a basic introduction to OOP and its applications (keeping the beginner in mind). Refer to this book's website for more examples on OOP.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.118.99