Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime as dt
- import RPi.GPIO as GPIO
- class LightSchedule:
- def __init__(self):
- self.status = False
- self.lcd = None
- self.log_file = "logs/log_lighting.txt"
- def setup(self, lcd_display):
- # lighting
- GPIO.setup(5, GPIO.OUT)
- GPIO.setup(6, GPIO.OUT)
- GPIO.setup(13, GPIO.OUT)
- GPIO.setup(19, GPIO.OUT)
- # initilize to 0
- GPIO.output(5, GPIO.HIGH)
- GPIO.output(6, GPIO.HIGH)
- GPIO.output(13, GPIO.HIGH)
- GPIO.output(19, GPIO.HIGH)
- # initialize lcd for lighting
- self.lcd = lcd_display
- # log system startup
- self.log("SYS START: LIGHTING")
- # display startup on lcd
- cur_time = dt.datetime.now()
- dsp_time = cur_time.strftime("%H:%M:%S")
- msg_top = "SYS START: LIGHT\n"
- msg_btm = "{:8}".format(dsp_time)
- def teardown(self):
- # clear GPIO on program failure
- GPIO.output(5, GPIO.HIGH)
- GPIO.output(6, GPIO.HIGH)
- GPIO.output(13, GPIO.HIGH)
- GPIO.output(19, GPIO.HIGH)
- def log(self, msg):
- # get time
- cur_time = dt.datetime.now()
- date_time = cur_time.strftime("%Y/%m/%d %H:%M:%S")
- # get status
- status = self.status
- # log
- pin_5 = "0" if GPIO.input(5) else "1"
- pin_6 = "0" if GPIO.input(6) else "1"
- pin_13 = "0" if GPIO.input(13) else "1"
- pin_19 = "0" if GPIO.input(19) else "1"
- log_msg = "[ {:>22} ] : [ {} ] Status: {:1}\n".format(msg, date_time, status)
- log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("PIN", 5, pin_5)
- log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("PIN", 6, pin_6)
- log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("PIN", 13, pin_13)
- log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("PIN", 19, pin_19)
- log_file = open(self.log_file, "a")
- log_file.write(log_msg)
- log_file.close()
- # display on LCD
- dsp_status = "ON" if status else "OFF"
- msg_top = "LIGHT: {:3}\n".format(dsp_status);
- msg_btm = "1:{:1} 2:{:1} 3:{:1} 4:{:1}".format(pin_5, pin_6, pin_13, pin_19)
- self.lcd.showMessage(msg_top, msg_btm)
- #print "[ {:>22} ] : [ {} ] Status: {:1}".format(msg, date_time, status)
- #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 5, pin_5)
- #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 6, pin_6)
- #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 13, pin_13)
- #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 19, pin_19)
- def start(self):
- # set status
- self.status = True
- # set relay on
- GPIO.output(5, GPIO.LOW)
- GPIO.output(6, GPIO.LOW)
- GPIO.output(13, GPIO.LOW)
- GPIO.output(19, GPIO.LOW)
- # call log
- self.log("LIGHTING ON")
- def stop(self):
- # set status
- self.status = False
- # set relay off
- GPIO.output(5, GPIO.HIGH)
- GPIO.output(6, GPIO.HIGH)
- GPIO.output(13, GPIO.HIGH)
- GPIO.output(19, GPIO.HIGH)
- # call log
- self.log("LIGHTING OFF")
- def update(self):
- cur_time = dt.datetime.now()
- cur_hour = cur_time.hour
- # check status
- if not self.status:
- # check time range: 20-00/24
- if cur_hour >= 20 and cur_hour < 24:
- #turn on
- self.start()
- # check time range: 00-08
- elif cur_hour >= 0 and cur_hour < 8:
- #turn on
- self.start()
- else:
- # log
- self.log("LIGHTING LOG")
- else:
- # check time range: 8-20
- if cur_hour >= 8 and cur_hour < 20:
- # turn off
- self.stop()
- else:
- # log
- self.log("LIGHTING LOG")
Add Comment
Please, Sign In to add comment