Skip to content

Commit

Permalink
Merge pull request #12 from ueckoken/redis-dependency-injection
Browse files Browse the repository at this point in the history
dependency injection for Redis
  • Loading branch information
otariidae authored Aug 16, 2023
2 parents 2c41881 + 229b15f commit f7732a8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
22 changes: 11 additions & 11 deletions KM4K.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
suica = nfc.clf.RemoteTarget("212F")
suica.sensf_req = bytearray.fromhex("0000030000")

# Redisに接続
conn = redis.StrictRedis(
host=os.environ["REDIS_HOST"],
port=os.environ["REDIS_PORT"],
db=os.environ["REDIS_DB"],
)


# 有効期間1週間
CACHE_EXPIRES_SECONDS = 60 * 60 * 24 * 7
Expand All @@ -37,21 +30,21 @@ def read_nfc():
return binascii.hexlify(tag.idm)


def start_system(isopen, okled_pin, ngled_pin, card: CardSDK):
def start_system(isopen, okled_pin, ngled_pin, cache: redis.Redis, card: CardSDK):
while True:
idm = read_nfc()
if idm:
verified = False
# Redisに登録されているか確認
if conn.get(idm.decode()) is not None:
if cache.get(idm.decode()) is not None:
verified = True
else:
# Card Managerで登録されているか確認
is_registered_sso = card.verify(idm.decode())
if is_registered_sso:
# 有効期限付きでRedisに保存
# 値は今のところ使わないので適当に1にしておいた
conn.set(idm.decode(), 1, ex=CACHE_EXPIRES_SECONDS)
cache.set(idm.decode(), 1, ex=CACHE_EXPIRES_SECONDS)
verified = True
if verified:
print("Registered (idm:" + idm.decode() + ")")
Expand Down Expand Up @@ -91,6 +84,13 @@ def main(_):
okled_pin = 19
ngled_pin = 26

# Redisに接続
conn = redis.StrictRedis(
host=os.environ["REDIS_HOST"],
port=os.environ["REDIS_PORT"],
db=os.environ["REDIS_DB"],
)

servo.reset()
GPIO.setmode(GPIO.BCM)
GPIO.setup(okled_pin, GPIO.OUT)
Expand All @@ -100,7 +100,7 @@ def main(_):

try:
print("Welcome to Koken Kagi System")
start_system(isopen, okled_pin, ngled_pin, card)
start_system(isopen, okled_pin, ngled_pin, conn, card)
except Exception as e: # noqa: BLE001
print("An error has occured!")
print(e)
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ services:
km4k:
build:
dockerfile: Dockerfile.dev
environment:
REDIS_HOST: redis
REDIS_PORT: 6379
REDIS_DB: 0
56 changes: 49 additions & 7 deletions test_KM4K.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ def test_start_system_with_closed_door_and_unregistered_card(
from KM4K import start_system

with contextlib.suppress(InterruptedError):
start_system(isopen=False, okled_pin=19, ngled_pin=26, card=card)
start_system(
isopen=False,
okled_pin=19,
ngled_pin=26,
cache=self.conn,
card=card,
)

mocked_servo.unlock.assert_not_called()
mocked_servo.lock.assert_not_called()
Expand All @@ -65,7 +71,13 @@ def test_start_system_with_closed_door_and_registered_card(
from KM4K import start_system

with contextlib.suppress(InterruptedError):
start_system(isopen=False, okled_pin=19, ngled_pin=26, card=card)
start_system(
isopen=False,
okled_pin=19,
ngled_pin=26,
cache=self.conn,
card=card,
)

mocked_servo.unlock.assert_called_once()
mocked_servo.lock.assert_not_called()
Expand All @@ -83,7 +95,13 @@ def test_start_system_with_open_door_and_unregistered_card(
from KM4K import start_system

with contextlib.suppress(InterruptedError):
start_system(isopen=True, okled_pin=19, ngled_pin=26, card=card)
start_system(
isopen=True,
okled_pin=19,
ngled_pin=26,
cache=self.conn,
card=card,
)

mocked_servo.unlock.assert_not_called()
mocked_servo.lock.assert_not_called()
Expand All @@ -101,7 +119,13 @@ def test_start_system_with_open_door_and_registered_card(
from KM4K import start_system

with contextlib.suppress(InterruptedError):
start_system(isopen=True, okled_pin=19, ngled_pin=26, card=card)
start_system(
isopen=True,
okled_pin=19,
ngled_pin=26,
cache=self.conn,
card=card,
)

mocked_servo.unlock.assert_not_called()
mocked_servo.lock.assert_called_once()
Expand All @@ -119,7 +143,13 @@ def test_start_system_with_redis_cache(
from KM4K import start_system

with contextlib.suppress(InterruptedError):
start_system(isopen=True, okled_pin=19, ngled_pin=26, card=card)
start_system(
isopen=True,
okled_pin=19,
ngled_pin=26,
cache=self.conn,
card=card,
)

mocked_servo.unlock.assert_called_once()
mocked_servo.lock.assert_called_once()
Expand All @@ -138,7 +168,13 @@ def test_start_system_unregistered_card_with_redis_cache(
from KM4K import start_system

with contextlib.suppress(InterruptedError):
start_system(isopen=True, okled_pin=19, ngled_pin=26, card=card)
start_system(
isopen=True,
okled_pin=19,
ngled_pin=26,
cache=self.conn,
card=card,
)

mocked_servo.unlock.assert_called_once()
mocked_servo.lock.assert_called_once()
Expand All @@ -158,7 +194,13 @@ def test_start_system_with_redis_cache_expires(
from KM4K import start_system

with contextlib.suppress(InterruptedError):
start_system(isopen=True, okled_pin=19, ngled_pin=26, card=card)
start_system(
isopen=True,
okled_pin=19,
ngled_pin=26,
cache=self.conn,
card=card,
)

mocked_servo.unlock.assert_called_once()
mocked_servo.lock.assert_called_once()
Expand Down

0 comments on commit f7732a8

Please sign in to comment.