Guest User

Untitled

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