-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
57 lines (54 loc) · 1.73 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import nfc
from dotenv import load_dotenv
import requests, re, os, time, logging
from pydub import AudioSegment
from pydub.playback import play
# logging
fmt = "%(asctime)s %(levelname)s %(name)s :%(message)s"
# logging.basicConfig(level=logging.INFO, format=fmt)
logging.basicConfig(filename='/home/pi/nyutaisitu.log', level=logging.INFO, format=fmt)
# sound
ASSETS_DIR = '/home/pi/busitu-nyutaisitu/assets/'
success_sound = AudioSegment.from_mp3("{}success.mp3".format(ASSETS_DIR))[:1500]
fail_sound = AudioSegment.from_mp3("{}fail.mp3".format(ASSETS_DIR))
# env for api
load_dotenv()
# get session
session = requests.Session()
clf = nfc.ContactlessFrontend("usb")
time.sleep(2)
while True:
logging.info("カードをかざしてください")
try:
tag = clf.connect(rdwr={'on-connect': lambda tag: False})
tagd = tag.dump()
except:
logging.warning("scan error")
play(fail_sound)
continue
try:
gakusekiStr = re.findall('\|.*\|', tagd[6])[0]
gakusekiStr = re.findall('[a-zA-Z0-9]*', gakusekiStr)[1][0:7]
except:
logging.info("\n".join(tagd))
logging.error("parse error")
play(fail_sound)
continue
logging.info(gakusekiStr)
logging.info("カードを検知")
try:
baseUrl = os.environ['API_URL']
except:
logging.error("環境変数が正しく設定されていません")
play(fail_sound)
break
url='{}?gakuseki_number={}'.format(baseUrl, gakusekiStr)
try:
data_response = session.get(url)
except:
logging.error("api error")
play(fail_sound)
continue
logging.info(data_response.text)
logging.info("正しく送信されましたa")
play(success_sound)