Skip to content

Commit aa6751e

Browse files
committed
changed from pygame to mpv
1 parent ac58fda commit aa6751e

6 files changed

+59
-62
lines changed

Diff for: config.py.sample

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#!/usr/bin/env python3
21
# -*- coding: utf-8 -*-
32

43
class LDAP:
5-
URI = 'ldap://10.0.1.7:389/'
6-
BASE = 'dc=c-base,dc=org'
7-
USERBASE = 'ou=crew'
8-
BINDDN ='cn=c-labpin,ou=bind,dc=c-base,dc=org'
9-
BINDPW ='ENTER PWD HERE'
10-
PINFIELD ='c-labPIN'
11-
UIDFIELD ='uid'
12-
ACCESS_FILTER ='(&(uidNumber={})(memberof=cn=cey-c-lab,ou=groups,dc=c-base,dc=org)(memberof=cn=crew,ou=groups,dc=c-base,dc=org))'
4+
URI = 'ldap://10.0.1.7:389/'
5+
BASE = 'dc=c-base,dc=org'
6+
USERBASE = 'ou=crew'
7+
BINDDN = 'cn=c-labpin,ou=bind,dc=c-base,dc=org'
8+
BINDPW = 'ENTER PWD HERE'
9+
PINFIELD = 'c-labPIN'
10+
UIDFIELD ='uid'
11+
ACCESS_FILTER ='(&(uidNumber={})(memberof=cn=cey-c-lab,ou=groups,dc=c-base,dc=org)(memberof=cn=crew,ou=groups,dc=c-base,dc=org))'
12+

Diff for: ldap_interface.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

44
import base64
55
import hashlib
66
from datetime import datetime
7+
78
from lmap import *
89

910
import config
@@ -14,6 +15,7 @@ def ldap_connect():
1415
ld.simple_bind(config.LDAP.BINDDN, config.LDAP.BINDPW)
1516
return lmap.lmap(dn=config.LDAP.BASE, ldap=ld)
1617

18+
1719
def pwcheck(record, pw):
1820
if not record.startswith('{SSHA}'):
1921
return record == pw
@@ -23,6 +25,7 @@ def pwcheck(record, pw):
2325
newhashv = hashlib.sha1(bytearray(pw, 'UTF-8')+salt).digest()
2426
return hashv == newhashv
2527

28+
2629
def authenticate(uid, pin):
2730
lm = ldap_connect()
2831
try:
@@ -35,8 +38,10 @@ def authenticate(uid, pin):
3538
print(datetime.now(), 'Invalid user/pin:', uid, '('+str(e)+')')
3639
return False
3740

41+
3842
numbuf = []
3943

44+
4045
if __name__ == "__main__":
4146
print(pwcheck('{SSHA}c8pLDYbSkF2jBAKxxa67nY7NYkdQXiPNFzzRso9FRZI=', '1234'))
4247

Diff for: requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RPi.GPIO==0.6.2
2+
lmap==0.0.2
3+
-e git+https://github.com/jaseg/python-mpv.git#egg=python-mpv

Diff for: run_schloss.sh

-6
This file was deleted.

Diff for: schloss.py

+39-45
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,45 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
import os
54
import time
6-
# import pifacedigitalio as pfio
7-
from RPi import GPIO
85
import queue
96
from threading import Thread, RLock
10-
import pygame
117

8+
import mpv
9+
from RPi import GPIO
1210
from ldap_interface import authenticate
1311

12+
1413
NUMERIC_KEYS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
1514

16-
BOUNCE_TIME = 300 # in milliseconds
17-
STALE_TIMEOUT = 30 # in seconds
18-
timeouts = {'1': BOUNCE_TIME, '2': BOUNCE_TIME, '3': BOUNCE_TIME, '4': BOUNCE_TIME,
15+
BOUNCE_TIME = 300 # in milliseconds
16+
STALE_TIMEOUT = 30 # in seconds
17+
timeouts = {'1': BOUNCE_TIME, '2': BOUNCE_TIME, '3': BOUNCE_TIME, '4': BOUNCE_TIME,
1918
'5': BOUNCE_TIME, '6': BOUNCE_TIME, '7': BOUNCE_TIME, '8': BOUNCE_TIME,
20-
'9': BOUNCE_TIME, '0': BOUNCE_TIME, 'A': BOUNCE_TIME, 'B': BOUNCE_TIME,
19+
'9': BOUNCE_TIME, '0': BOUNCE_TIME, 'A': BOUNCE_TIME, 'B': BOUNCE_TIME,
2120
'C': BOUNCE_TIME, 'D': BOUNCE_TIME, 'E': BOUNCE_TIME, 'F': BOUNCE_TIME}
2221

2322

2423
q = queue.Queue()
2524
lock = RLock()
2625

27-
# for piface
28-
# ROWS = [2, 3, 4, 5]
29-
# COLS = [0, 1, 2, 3]
30-
3126
COLS = [15, 13, 11, 7]
3227
ROWS = [12, 16, 18, 22]
3328

3429
OPEN_PIN = 26
3530

3631
# preinit to avoid sound lag
37-
pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag
38-
pygame.mixer.init()
39-
beep = pygame.mixer.Sound('/home/pi/raspberrylock/beep.wav')
40-
fail = pygame.mixer.Sound('/home/pi/raspberrylock/fail.wav')
41-
success = pygame.mixer.Sound('/home/pi/raspberrylock/success.wav')
42-
43-
def init_piface():
44-
pfio.init()
45-
pfio.digital_write_pullup(0, 1)
46-
pfio.digital_write_pullup(1, 1)
47-
pfio.digital_write_pullup(2, 1)
48-
pfio.digital_write_pullup(3, 1)
49-
32+
MPV = mpv.MPV()
33+
beep = '/opt/raspberrylock/beep.wav'
34+
fail = '/opt/raspberrylock/fail.wav'
35+
success = '/opt/raspberrylock/success.wav'
36+
37+
state = 0
38+
uid = ''
39+
pin = ''
40+
reset_timer = STALE_TIMEOUT
41+
42+
5043
def init_gpios():
5144
GPIO.setmode(GPIO.BOARD)
5245
GPIO.setup(OPEN_PIN, GPIO.OUT)
@@ -56,80 +49,83 @@ def init_gpios():
5649
for pin in COLS:
5750
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
5851

52+
5953
def decode_keypad(measurements):
6054
"""
6155
x1 = (0,0,1,0)
6256
"""
63-
layout = [['C', 'D', 'E', 'F'], ['B', '9', '6', '3'],
57+
layout = [['C', 'D', 'E', 'F'], ['B', '9', '6', '3'],
6458
['0', '8', '5', '2'], ['A', '7', '4', '1']]
6559
for y_index, y_row in enumerate(measurements):
6660
for x_index, x_state in enumerate(y_row):
6761
if x_state > 0:
6862
return layout[y_index][x_index]
6963

64+
7065
def decrease_timeouts(timeouts):
7166
for k, v in timeouts.items():
7267
if v > 0:
7368
timeouts[k] = v - 1
7469
return timeouts
7570

71+
7672
def collect_measurements():
7773
"""
7874
"""
7975
pin_state = []
8076
for y_pin in ROWS:
81-
# pfio.digital_write(y_pin, 1)
8277
GPIO.output(y_pin, 1)
8378
x_pin_states = []
8479
for x_pin in COLS:
85-
#pin_in = pfio.digital_read(x_pin)
8680
pin_in = GPIO.input(x_pin)
8781
# print("{}x{} = {}".format(y_pin, x_pin, pin_in))
8882
x_pin_states.append(pin_in)
89-
# pfio.digital_write(y_pin, 0)
9083
GPIO.output(y_pin, 0)
9184
pin_state.append(x_pin_states)
9285
return pin_state
9386

87+
9488
def read_keypad():
9589
decrease_timeouts(timeouts)
9690
key = decode_keypad(collect_measurements())
9791
if key:
9892
if timeouts[key] > 0:
9993
return None
10094
else:
101-
beep.play()
95+
play(beep)
10296
timeouts[key] = BOUNCE_TIME
10397
return key
10498
else:
10599
return None
106100

107101

108-
state = 0
109-
uid = ''
110-
pin = ''
111-
reset_timer = STALE_TIMEOUT
112102

113103
def reset_state():
114-
print("reset state")
115104
global state, uid, pin
105+
106+
print("reset state")
116107
state = 0
117108
uid = ''
118109
pin = ''
119110

111+
120112
def timeout_reset_state():
121113
global reset_timer
114+
122115
while True:
123116
reset_timer -= 1
124117
if reset_timer <= 0:
125118
reset_state()
126119
reset_timer = STALE_TIMEOUT
127120
time.sleep(1.0)
128121

122+
129123
def control_loop():
130124
global reset_timer
131125
global state, uid, pin
126+
132127
reset_state()
128+
133129
# Main state machine.
134130
# Expects the user to enter her UID, then PIN like this:
135131
# [A] 2903 [A] 123456 A
@@ -172,32 +168,28 @@ def control_loop():
172168
pin += key
173169
continue
174170
elif key == 'C':
175-
reset_state()
171+
reset_state()
176172
continue
177173
elif key == 'A':
178174
t = Thread(target=open_if_correct, args=(uid, pin))
179175
t.start()
180176
reset_state()
181177
continue
182178

179+
183180
def open_if_correct(uid, pin):
184181
print('checking ldap ...')
185182
if authenticate(uid, pin):
186-
success.play()
183+
play(success)
187184
with lock:
188185
GPIO.output(OPEN_PIN, 1)
189-
#pfio.digital_write(6, 1)
190-
# pfio.digital_write(1, 1)
191186
time.sleep(10)
192187
GPIO.output(OPEN_PIN, 0)
193-
# pfio.digital_write(6, 0)
194-
# pfio.digital_write(1, 0)
195188
else:
196-
fail.play()
189+
play(fail)
197190
with lock:
198-
#pfio.digital_write(7, 1)
199191
time.sleep(2)
200-
# pfio.digital_write(7, 0)
192+
201193

202194
def keypad_loop():
203195
while True:
@@ -206,8 +198,8 @@ def keypad_loop():
206198
if key:
207199
q.put(key)
208200

201+
209202
def main():
210-
# pfio.init()
211203
#os.nice(10)
212204
init_gpios()
213205
control_thread = Thread(target=control_loop)
@@ -217,8 +209,10 @@ def main():
217209
timeout_thread = Thread(target=timeout_reset_state)
218210
timeout_thread.start()
219211

212+
220213
if __name__ == '__main__':
221214
try:
222215
main()
223216
except KeyboardInterrupt:
224217
GPIO.cleanup()
218+

Diff for: schloss_tests.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#!/usr/bin/env python
21
# -*- coding: utf-8 -*-
32

43
import unittest
54

65
from schloss import *
76

7+
88
class SchlossTest(unittest.TestCase):
99
def test_decode_keypad(self):
1010
key_1 = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]]
@@ -17,3 +17,4 @@ def test_decrease_timeouts(self):
1717
self.assertEquals(timeout_expected, result)
1818
result = decrease_timeouts(result)
1919
self.assertEquals(timeout_expected, result)
20+

0 commit comments

Comments
 (0)