You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/env python2
  2. import os
  3. import stat
  4. import time
  5. import pifacedigitalio
  6. import urllib2
  7. import signal
  8. import sys
  9. import grp
  10. from ConfigParser import SafeConfigParser
  11. #Read config
  12. parser = SafeConfigParser()
  13. parser.read('/etc/foodoord.conf')
  14. url = parser.get('door_firstlevel', 'status_url')
  15. old_api = parser.get('door_firstlevel_old', 'status_url')
  16. #Definitions for output
  17. LED_RED=6
  18. LED_GREEN=7
  19. RELAYS_LOCK=0
  20. RELAYS_UNLOCK=1
  21. #Definitions for input
  22. DOOR_BELL=0
  23. REED_RELAYS=1 #not implementet yet
  24. #Definitions for LEDcolor
  25. RED=1
  26. GREEN=2
  27. ORANGE=3
  28. if __name__ == "__main__":
  29. def doorbell(event):
  30. if (STATUS):
  31. pifacedigital.relays[RELAYS_UNLOCK].toggle()
  32. time.sleep(2)
  33. pifacedigital.relays[RELAYS_UNLOCK].toggle()
  34. #print 'got doorbell'
  35. def close_button(event):
  36. global STATUS
  37. STATUS = False
  38. try:
  39. urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
  40. urllib2.urlopen(old_api+'&status=closed', timeout=2)
  41. except:
  42. pass
  43. set_led(RED)
  44. listener = pifacedigitalio.InputEventListener()
  45. listener.register(0, pifacedigitalio.IODIR_RISING_EDGE, doorbell, settle_time=10)
  46. listener.register(1, pifacedigitalio.IODIR_RISING_EDGE, close_button, settle_time=5)
  47. listener.activate()
  48. def signal_handler(signal, frame):
  49. listener.deactivate()
  50. os.remove("/var/run/foodoord.pipe")
  51. try:
  52. urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
  53. urllib2.urlopen(old_api+'&status=closed', timeout=2)
  54. except:
  55. pass
  56. set_led(RED)
  57. sys.exit(0)
  58. def set_led(color):
  59. if (color==RED):
  60. pifacedigital.leds[LED_RED].turn_on()
  61. pifacedigital.leds[LED_GREEN].turn_off()
  62. elif (color==GREEN):
  63. pifacedigital.leds[LED_GREEN].turn_on()
  64. pifacedigital.leds[LED_RED].turn_off()
  65. elif (color==ORANGE):
  66. pifacedigital.leds[LED_RED].turn_on()
  67. pifacedigital.leds[LED_GREEN].turn_on()
  68. pifacedigital = pifacedigitalio.PiFaceDigital()
  69. signal.signal(signal.SIGTERM, signal_handler)
  70. #Startsettings
  71. STATUS = False
  72. pifacedigital.leds[LED_RED].turn_on()
  73. #Setting up FiFo to get sshd-output
  74. try:
  75. os.mkfifo("/var/run/foodoord.pipe")
  76. os.chown("/var/run/foodoord.pipe", -1, grp.getgrnam('foodoor')[2])
  77. os.chmod("/var/run/foodoord.pipe", stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
  78. except OSError:
  79. pass
  80. with open("/var/run/foodoord.pipe", "r") as ssh_input:
  81. while 1:
  82. #Read sshd-output from pipe
  83. Pipe = ssh_input.readline()[:-1]
  84. if (Pipe == "close" and STATUS):
  85. pifacedigital.relays[RELAYS_LOCK].toggle()
  86. time.sleep(1)
  87. pifacedigital.relays[RELAYS_LOCK].toggle()
  88. STATUS = False
  89. try:
  90. urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
  91. urllib2.urlopen(old_api+'&status=closed', timeout=2)
  92. except:
  93. pass
  94. set_led(RED)
  95. elif (Pipe == "open"):
  96. pifacedigital.relays[RELAYS_UNLOCK].toggle()
  97. time.sleep(2)
  98. pifacedigital.relays[RELAYS_UNLOCK].toggle()
  99. if (STATUS==False):
  100. try:
  101. urllib2.urlopen(url+'&door=aerie&locked=0', timeout=2)
  102. urllib2.urlopen(old_api+'&status=open', timeout=2)
  103. except:
  104. pass
  105. STATUS = True
  106. set_led(GREEN)
  107. time.sleep(0.1)