Guest User

Untitled

a guest
Aug 11th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import datetime as dt
  2. import RPi.GPIO as GPIO
  3.  
  4. class LightSchedule:
  5.  
  6. def __init__(self):
  7.  
  8. self.status = False
  9.  
  10. self.log_file = "logs/log_lighting.txt"
  11.  
  12. def setup(self):
  13.  
  14. # lighting
  15. GPIO.setup(5, GPIO.OUT)
  16. GPIO.setup(6, GPIO.OUT)
  17. GPIO.setup(13, GPIO.OUT)
  18. GPIO.setup(19, GPIO.OUT)
  19.  
  20. # initilize to 0
  21. GPIO.output(5, GPIO.HIGH)
  22. GPIO.output(6, GPIO.HIGH)
  23. GPIO.output(13, GPIO.HIGH)
  24. GPIO.output(19, GPIO.HIGH)
  25.  
  26. # log system startup
  27. self.log("SYSTEM START: LIGHTING")
  28.  
  29. def log(self, msg):
  30.  
  31. # get time
  32. cur_time = dt.datetime.now()
  33. cur_time = cur_time.strftime("%Y/%m/%d %H:%M:%S")
  34.  
  35. # get status
  36. status = self.status
  37.  
  38. # log
  39. pin_5 = "0" if GPIO.input(5) else "1"
  40. pin_6 = "0" if GPIO.input(6) else "1"
  41. pin_13 = "0" if GPIO.input(13) else "1"
  42. pin_19 = "0" if GPIO.input(19) else "1"
  43.  
  44. log_msg = "[ {:>22} ] : [ {} ] Status: {:1}\n".format(msg, cur_time, status)
  45. log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("PIN", 5, pin_5)
  46. log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("PIN", 6, pin_6)
  47. log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("PIN", 13, pin_13)
  48. log_msg += "[ {:>22} ] : [ {:2} {:1} ]\n".format("PIN", 19, pin_19)
  49.  
  50. log_file = open(self.log_file, "a")
  51. log_file.write(log_msg)
  52. log_file.close()
  53.  
  54. #print "[ {:>22} ] : [ {} ] Status: {:1}".format(msg, cur_time, status)
  55. #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 5, pin_5)
  56. #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 6, pin_6)
  57. #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 13, pin_13)
  58. #print "[ {:>22} ] : [ {:2} {:1} ]".format("PIN", 19, pin_19)
  59.  
  60.  
  61. def start(self):
  62.  
  63. # set status
  64. self.status = True
  65.  
  66. # set relay on
  67. GPIO.output(5, GPIO.LOW)
  68. GPIO.output(6, GPIO.LOW)
  69. GPIO.output(13, GPIO.LOW)
  70. GPIO.output(19, GPIO.LOW)
  71.  
  72. # call log
  73. self.log("LIGHTING ON")
  74.  
  75. def stop(self):
  76.  
  77. # set status
  78. self.status = False
  79.  
  80. # set relay off
  81. GPIO.output(5, GPIO.HIGH)
  82. GPIO.output(6, GPIO.HIGH)
  83. GPIO.output(13, GPIO.HIGH)
  84. GPIO.output(19, GPIO.HIGH)
  85.  
  86. # call log
  87. self.log("LIGHTING OFF")
  88.  
  89. def update(self):
  90. cur_time = dt.datetime.now()
  91. cur_hour = cur_time.hour
  92.  
  93. # check status
  94. if not self.status:
  95.  
  96. # check time range: 20-00/24
  97. if cur_hour >= 20 and cur_hour < 24:
  98.  
  99. #turn on
  100. self.start()
  101.  
  102. # check time range: 00-08
  103. elif cur_hour >= 0 and cur_hour < 8:
  104.  
  105. #turn on
  106. self.start()
  107.  
  108. else:
  109.  
  110. # log
  111. self.log("LIGHTING LOG")
  112.  
  113. else:
  114.  
  115. # check time range: 8-20
  116. if cur_hour >= 8 and cur_hour < 20:
  117.  
  118. # turn off
  119. self.stop()
  120.  
  121. else:
  122.  
  123. # log
  124. self.log("LIGHTING LOG")
  125.  
  126. def display(self):
  127. pin_5 = "0" if GPIO.input(5) else "1"
  128. pin_6 = "0" if GPIO.input(6) else "1"
  129. pin_13 = "0" if GPIO.input(13) else "1"
  130. pin_19 = "0" if GPIO.input(19) else "1"
  131.  
  132. if self.status:
  133. display = "LIGHT: ON\n"
  134. else:
  135. display = "LIGHT: OFF\n"
  136.  
  137. display += "1:{} 2:{} 3:{} 4:{}".format(pin_5, pin_6, pin_13, pin_19)
  138.  
  139. return display
Add Comment
Please, Sign In to add comment