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.log_file = "logs/log_lighting.txt"
- def setup(self):
- # 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)
- # log system startup
- self.log("SYSTEM START: LIGHTING")
- def log(self, msg):
- # get time
- cur_time = dt.datetime.now()
- cur_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, cur_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()
- #print "[ {:>22} ] : [ {} ] Status: {:1}".format(msg, cur_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")
- def display(self):
- 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"
- if self.status:
- display = "LIGHT: ON\n"
- else:
- display = "LIGHT: OFF\n"
- display += "1:{} 2:{} 3:{} 4:{}".format(pin_5, pin_6, pin_13, pin_19)
- return display
Add Comment
Please, Sign In to add comment