forked from jonnor/UnlockOslo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestdevices.py
More file actions
53 lines (44 loc) · 1.49 KB
/
Copy pathtestdevices.py
File metadata and controls
53 lines (44 loc) · 1.49 KB
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
import os
import os.path
import sys
firmware_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../firmware')
sys.path.insert(0, firmware_path)
import dlockoslo
import msgflo
import gevent
def set_fake_gpio(dev, number, state : bool):
p = os.path.join(dev, 'gpio'+str(number))
with open(p, 'w') as f:
f.write('1' if state else '0')
def create_virtual_lock(name):
os.environ['DLOCK_FAKE_GPIO'] = name
lockdir = os.path.dirname(name)
if not os.path.exists(lockdir):
os.mkdir(lockdir)
virtual = dlockoslo.LockParticipant(role=name)
del os.environ['DLOCK_FAKE_GPIO']
# turn door opener inputs off
virtual.recalculate_state() # ensure files exist
set_fake_gpio(name, 10, True)
set_fake_gpio(name, 24, True)
return virtual
def get_participants():
participants = [
create_virtual_lock('doors/virtual-1'),
create_virtual_lock('doors/virtual-2'),
create_virtual_lock('doors/dlock-2'),
dlockoslo.AlwaysErroringParticipant(role='doors/erroring-1'),
]
# for emulating timeout/device missing, send on MQTT topic which nothing uses
return participants
def run(done_cb=None):
participants = get_participants()
engine = msgflo.run(participants, done_cb=done_cb)
return participants, engine
def main():
participants = get_participants()
waiter = gevent.event.AsyncResult()
engine = msgflo.run(participants, done_cb=waiter.set)
waiter.wait()
if __name__ == '__main__':
main()