2024-06-07 20:17:06 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-11-13 13:27:33 +00:00
|
|
|
# vim: ts=2 sw=2 et
|
2021-01-19 17:47:19 +00:00
|
|
|
|
2024-06-07 20:17:06 +00:00
|
|
|
import grp
|
|
|
|
import json
|
2021-01-19 17:47:19 +00:00
|
|
|
import os
|
|
|
|
import stat
|
2024-06-07 20:17:06 +00:00
|
|
|
import subprocess
|
2021-01-19 17:47:19 +00:00
|
|
|
import time
|
2021-11-13 16:23:46 +00:00
|
|
|
from configparser import ConfigParser
|
2024-07-04 20:57:17 +00:00
|
|
|
from dataclasses import dataclass
|
2021-01-19 17:47:19 +00:00
|
|
|
|
2024-06-07 20:17:06 +00:00
|
|
|
import RPi.GPIO as gpio
|
|
|
|
|
|
|
|
# Definitions for output
|
|
|
|
LED_RED = 6
|
|
|
|
LED_GREEN = 7
|
|
|
|
RELAYS_LOCK = 0
|
|
|
|
RELAYS_UNLOCK = 1
|
|
|
|
PIN_OPEN = 24
|
|
|
|
PIN_CLOSE = 27
|
|
|
|
# Definitions for input
|
|
|
|
DOOR_BELL = 0
|
|
|
|
REED_RELAYS = 1 # not implemented yet
|
|
|
|
|
|
|
|
# Definitions for LED color
|
|
|
|
RED = 1
|
|
|
|
GREEN = 2
|
|
|
|
ORANGE = 3
|
|
|
|
|
|
|
|
# Read config
|
2021-11-13 16:23:46 +00:00
|
|
|
parser = ConfigParser()
|
2021-01-19 17:47:19 +00:00
|
|
|
parser.read('/etc/foodoord.conf')
|
|
|
|
|
2024-07-04 20:57:17 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class API:
|
|
|
|
api_url: str
|
|
|
|
consumer_key: str
|
|
|
|
consumer_secret: str
|
|
|
|
|
|
|
|
|
|
|
|
APIv1 = API(
|
|
|
|
parser.get('doorstatus', 'status_url'),
|
|
|
|
parser.get('doorstatus', 'key'),
|
|
|
|
parser.get('doorstatus', 'secret'),
|
|
|
|
)
|
|
|
|
APIv2 = API(
|
|
|
|
parser.get('doorstatusv2', 'status_url'),
|
|
|
|
parser.get('doorstatusv2', 'key'),
|
|
|
|
parser.get('doorstatusv2', 'secret'),
|
|
|
|
)
|
2021-01-19 17:47:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def write_state(state):
|
2024-06-07 20:17:06 +00:00
|
|
|
try:
|
|
|
|
with open("/tmp/door_state", "w") as handle:
|
|
|
|
handle.write(state)
|
|
|
|
except:
|
|
|
|
pass
|
2021-01-19 17:47:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_api(locked):
|
2024-06-07 20:17:06 +00:00
|
|
|
try:
|
2024-07-04 20:57:17 +00:00
|
|
|
# API v1
|
|
|
|
subprocess.check_call([
|
|
|
|
"/usr/bin/curl", "-XPOST",
|
|
|
|
"--header", "Content-Type: application/json",
|
|
|
|
"--data",
|
|
|
|
json.dumps({"consumer_key": APIv1.consumer_key, "consumer_secret": APIv1.consumer_secret, "cellar": locked}),
|
|
|
|
APIv1.api_url
|
|
|
|
])
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
# API v2
|
2024-06-07 20:17:06 +00:00
|
|
|
subprocess.check_call([
|
|
|
|
"/usr/bin/curl", "-XPOST",
|
|
|
|
"--header", "Content-Type: application/json",
|
|
|
|
"--data",
|
2024-07-04 20:57:17 +00:00
|
|
|
json.dumps({"consumer_key": APIv2.consumer_key, "consumer_secret": APIv2.consumer_secret, "cellar": locked}),
|
|
|
|
APIv2.api_url
|
2024-06-07 20:17:06 +00:00
|
|
|
])
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# Start settings
|
|
|
|
gpio.setmode(gpio.BCM)
|
|
|
|
gpio.setup(PIN_OPEN, gpio.OUT)
|
|
|
|
gpio.setup(PIN_CLOSE, gpio.OUT)
|
|
|
|
|
|
|
|
# Setting up FiFo to get sshd-output
|
|
|
|
try:
|
|
|
|
os.mkfifo("/var/run/foodoord.pipe")
|
|
|
|
os.chown("/var/run/foodoord.pipe", -1, grp.getgrnam('foodoor')[2])
|
|
|
|
os.chmod("/var/run/foodoord.pipe", stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
ssh_input = open("/var/run/foodoord.pipe", "r")
|
|
|
|
while True:
|
|
|
|
# Read sshd output from pipe
|
|
|
|
pipe_cmd = ssh_input.readline().strip()
|
|
|
|
|
|
|
|
if pipe_cmd == "close":
|
|
|
|
gpio.output(PIN_CLOSE, 1)
|
|
|
|
time.sleep(1)
|
|
|
|
gpio.output(PIN_CLOSE, 0)
|
|
|
|
|
|
|
|
write_state("closed")
|
|
|
|
update_api(True)
|
|
|
|
|
|
|
|
elif pipe_cmd == "open":
|
|
|
|
# Locking
|
|
|
|
gpio.output(PIN_OPEN, 1)
|
|
|
|
time.sleep(1)
|
|
|
|
gpio.output(PIN_OPEN, 0)
|
|
|
|
|
|
|
|
write_state("open") # Save State
|
|
|
|
update_api(False) # Status Update
|
|
|
|
|
|
|
|
time.sleep(0.2)
|
2021-01-19 17:47:19 +00:00
|
|
|
|
|
|
|
|
2024-06-07 20:17:06 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|