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.

97 lines
2.0 KiB

  1. #! /usr/bin/python3
  2. # vim: ts=2 sw=2 et
  3. import os
  4. import stat
  5. import time
  6. import signal
  7. import sys
  8. import RPi.GPIO as gpio
  9. import grp
  10. from configparser import ConfigParser
  11. #Read config
  12. parser = ConfigParser()
  13. parser.read('/etc/foodoord.conf')
  14. doorapi = parser.get('doorstatus', 'status_url')
  15. consumerkey = parser.get('doorstatus', 'key')
  16. consumersecret = parser.get('doorstatus', 'secret')
  17. #Definitions for output
  18. LED_RED=6
  19. LED_GREEN=7
  20. RELAYS_LOCK=0
  21. RELAYS_UNLOCK=1
  22. PIN_OPEN=24
  23. PIN_CLOSE=27
  24. #Definitions for input
  25. DOOR_BELL=0
  26. REED_RELAYS=1 #not implementet yet
  27. #Definitions for LEDcolor
  28. RED=1
  29. GREEN=2
  30. ORANGE=3
  31. def write_state(state):
  32. try:
  33. handle = open("/tmp/door_state", "w")
  34. handle.write(state)
  35. handle.close()
  36. except:
  37. pass
  38. def update_api(locked):
  39. try:
  40. os.system("/usr/bin/curl -XPOST --header 'Content-Type: application/json' --data '{ \"consumer_key\": \"" + consumerkey + "\", \"consumer_secret\": \"" + consumersecret + "\", \"cellar\": " + str(locked).lower() + " }' '" + doorapi + "' ")
  41. except:
  42. pass
  43. if __name__ == "__main__":
  44. #Startsettings
  45. STATUS=False
  46. gpio.setmode(gpio.BCM)
  47. gpio.setup(PIN_OPEN, gpio.OUT)
  48. gpio.setup(PIN_CLOSE, gpio.OUT)
  49. #Setting up FiFo to get sshd-output
  50. try:
  51. os.mkfifo("/var/run/foodoord.pipe")
  52. os.chown("/var/run/foodoord.pipe", -1, grp.getgrnam('foodoor')[2])
  53. os.chmod("/var/run/foodoord.pipe", stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
  54. except OSError:
  55. pass
  56. with open("/var/run/foodoord.pipe", "r") as ssh_input:
  57. while 1:
  58. #Read sshd-output from pipe
  59. Pipe = ssh_input.readline()[:-1]
  60. if (Pipe == "close"):
  61. gpio.output(PIN_CLOSE,1)
  62. time.sleep(1)
  63. gpio.output(PIN_CLOSE,0)
  64. write_state("closed")
  65. update_api(True)
  66. elif (Pipe == "open"):
  67. #Locking
  68. gpio.output(PIN_OPEN,1)
  69. time.sleep(1)
  70. gpio.output(PIN_OPEN,0)
  71. #Save State
  72. write_state("open")
  73. #Status Update
  74. update_api(False)
  75. time.sleep(0.2)