-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vincent Bisserié <[email protected]>
- Loading branch information
Showing
12 changed files
with
877 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,12 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
#ide | ||
.idea | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
#python | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
__pycache__ | ||
requirements.txt | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
#project | ||
config.yaml | ||
synchro-gh.json | ||
porte.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.4.0 | ||
hooks: | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace | ||
- repo: https://github.com/psf/black | ||
rev: 23.1.0 | ||
hooks: | ||
- id: black |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
# porte | ||
Ouverture de la porte via la scanette | ||
|
||
### Utilisation | ||
Créer un fichier config.yaml : | ||
```yaml | ||
default_gh_list: # Liste des codes barres par default | ||
- "123" | ||
- "456" | ||
|
||
sentry: # Configuration du DSN sentry | ||
dsn: "https://foo.bar" | ||
``` | ||
`python main.py` pour lancer la boucle de lecture/ouverture | ||
|
||
`python main.py refresh` pour mettre à jour la liste des GHs | ||
|
||
### Installation | ||
```shell | ||
pip install poetry | ||
poetry export --without-hashes --format=requirements.txt > requirements.txt | ||
pip install -r requirements.txt | ||
``` | ||
Puis declarer deux services et un timer dans systemd. Ils sont dans le répertoire `./systemd`. | ||
|
||
### Développement | ||
```shell | ||
pyenv install 3.7.16 | ||
pyenv global 3.7.16 | ||
pip install poetry | ||
poetry install | ||
pre-commit install-hooks | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import logging | ||
import sys | ||
import time | ||
from logging.handlers import RotatingFileHandler | ||
|
||
import requests | ||
import sentry_sdk | ||
from tinydb import TinyDB, where | ||
|
||
from porte import CONFIG, HID, UnknownDevice | ||
|
||
ADMIN_CHOUETTOS_URL = "https://adminchouettos.lachouettecoop.fr/api/export/gh" | ||
CHANNEL_NB = 21 | ||
DB = TinyDB("synchro-gh.json") | ||
SCANNETTES = [ | ||
"0003:0C2E:0200", | ||
"0003:05E0:1200", | ||
] | ||
|
||
logger = logging.getLogger() | ||
logger.setLevel(logging.INFO) | ||
formatter = logging.Formatter("%(asctime)s [%(levelname)s]: %(message)s") | ||
s_handler = logging.StreamHandler() | ||
s_handler.setFormatter(formatter) | ||
logger.addHandler(s_handler) | ||
f_handler = RotatingFileHandler( | ||
filename="porte.log", | ||
maxBytes=10 * 1024 * 1024, | ||
backupCount=10, | ||
encoding="utf-8", | ||
) | ||
f_handler.setFormatter(formatter) | ||
logger.addHandler(f_handler) | ||
|
||
sentry_config = CONFIG.get("sentry") | ||
if sentry_config: | ||
sentry_sdk.init(dsn=sentry_config.dsn, traces_sample_rate=1.0) | ||
|
||
|
||
def print_device_id(d): | ||
logging.info(d.device_path) | ||
|
||
|
||
def check_device(d): | ||
for part in d.device_path.split("/"): | ||
for s in SCANNETTES: | ||
if part.startswith(s): | ||
return True | ||
return False | ||
|
||
|
||
def is_gh(code_barre): | ||
if code_barre in CONFIG.get("default_gh_list", []): | ||
logging.info(f"{code_barre} in default list") | ||
return True | ||
gh = DB.get(where("code_barre") == code_barre) | ||
if gh: | ||
logging.info(f"{gh.get('name')} ({code_barre}) is a GH") | ||
return True | ||
logging.warning(f"{code_barre} is not a GH") | ||
return False | ||
|
||
|
||
# http://www.piddlerintheroot.com/5v-relay/ | ||
# https://www.piddlerintheroot.com/barcode-scanner/ | ||
def scanette(): | ||
import pyudev as pd | ||
import RPi.GPIO as GPIO | ||
|
||
# La scanette ne se mettait pas toujours sur le /dev/hidraw0 (parfois hidraw1, 2 etc...) | ||
# ce petit script permet de determiner le bon hidraw dynamiquement ! | ||
# @thone https://www.instructables.com/id/USB-Barcode-Scanner-Raspberry-Pi/ | ||
context = pd.Context() | ||
location = None | ||
for device in context.list_devices(subsystem="hidraw"): | ||
# print_device_id(device) | ||
if check_device(device): | ||
location = device.device_node | ||
|
||
logging.info("--- Démarrage du serveur de porte ---") | ||
if not location: | ||
logging.error("Scannette introuvable !") | ||
raise UnknownDevice(context) | ||
|
||
try: | ||
# attention le CHANNEL_NB peut changer a certains moments | ||
fp = open(location, "rb") | ||
while True: | ||
v = "" | ||
done = False | ||
while not done: | ||
buffer = fp.read(8) | ||
for c in buffer: | ||
if int(c) == 40: | ||
done = True | ||
break | ||
if int(c) > 28: | ||
if int(HID[int(c)]) >= 0: | ||
v = v + HID[int(c)] | ||
|
||
if is_gh(v): | ||
GPIO.setmode(GPIO.BCM) | ||
GPIO.setup(CHANNEL_NB, GPIO.OUT) | ||
logging.info("Nouvelle ouverture " + str(v)) | ||
GPIO.output(CHANNEL_NB, GPIO.HIGH) # Turn motor on | ||
time.sleep(1) | ||
GPIO.output(CHANNEL_NB, GPIO.LOW) # Turn motor off | ||
time.sleep(1) | ||
GPIO.cleanup() | ||
except KeyboardInterrupt: | ||
GPIO.cleanup() | ||
|
||
|
||
def refresh(): | ||
r = requests.get(ADMIN_CHOUETTOS_URL) | ||
|
||
if r.status_code == 200: | ||
DB.drop_tables() | ||
for code_barre in r.json(): | ||
DB.insert({"code_barre": code_barre}) | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) <= 1 or sys.argv[1] == "scan": | ||
scanette() | ||
elif sys.argv[1] == "refresh": | ||
refresh() |
Oops, something went wrong.