-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibhangups.py
50 lines (39 loc) · 1.59 KB
/
libhangups.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
import datetime
import json
import ctypes
_libhangups = ctypes.CDLL('target/debug/libhangups.so')
_libhangups.libhangups_client_create.restype = ctypes.c_void_p
_libhangups.libhangups_client_create.argtypes = []
_libhangups.libhangups_client_receive.restype = ctypes.POINTER(ctypes.c_char_p)
_libhangups.libhangups_client_receive.argtypes = [ctypes.c_void_p, ctypes.c_uint64]
_libhangups.libhangups_client_destroy.restype = ctypes.c_void_p
_libhangups.libhangups_client_destroy.argtypes = [ctypes.c_void_p]
_libhangups.libhangups_destroy_received.restype = ctypes.c_void_p
_libhangups.libhangups_destroy_received.argtypes = [ctypes.POINTER(ctypes.c_char_p)]
class Client():
def __init__(self):
self._client = _libhangups.libhangups_client_create()
assert self._client
def __del__(self):
_libhangups.libhangups_client_destroy(self._client)
def receive(self, timeout):
assert isinstance(timeout, datetime.timedelta)
res_ptr = _libhangups.libhangups_client_receive(
self._client, int(timeout.total_seconds() * 1000)
)
assert res_ptr
res_str = ctypes.cast(res_ptr, ctypes.c_char_p).value
_libhangups.libhangups_destroy_received(res_ptr)
return json.loads(res_str)
def main():
try:
client = Client()
while True:
state_update = client.receive(datetime.timedelta(seconds=1))
assert state_update is not None
if state_update:
print(json.dumps(state_update, indent=4))
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()