-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
43 lines (31 loc) · 998 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import click
import RPi.GPIO as GPIO
import time
import _cfg
import _ravendb
import datetime
from loguru import logger
config = _cfg.config
@click.command()
def capture():
logger.info("initializing")
switch = config.gpio_bcm_nr
GPIO.setmode(GPIO.BCM)
GPIO.setup(switch, GPIO.IN)
try:
while True:
if GPIO.input(switch):
_ravendb.store_result(0.01)
logger.info("reed contact triggered")
_delay_for_seconds(config.debounce_seconds)
_delay_for_seconds(config.timeout_seconds)
finally:
GPIO.cleanup()
def _delay_for_seconds(seconds: int):
next_run = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
wait_time = next_run - datetime.datetime.utcnow()
seconds = wait_time.total_seconds()
logger.info("Waiting until {next_run} -> {seconds} seconds", next_run=next_run, seconds=seconds)
time.sleep(seconds)
if __name__ == '__main__':
capture()