-
Notifications
You must be signed in to change notification settings - Fork 63
/
utils.py
50 lines (43 loc) · 1.79 KB
/
utils.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
from connect_ffi import ffi, lib
def get_zeroconf_vars():
zeroconf_vars = ffi.new('SpZeroConfVars *')
lib.SpZeroConfGetVars(zeroconf_vars)
return convert_to_python(zeroconf_vars[0])
def print_zeroconf_vars():
zeroconf_vars = get_zeroconf_vars()
print "public key: {}".format(zeroconf_vars['publicKey'])
print "device id: {}".format(zeroconf_vars['deviceId'])
print "remote name: {}".format(zeroconf_vars['remoteName'])
print "account req: {}".format(zeroconf_vars['accountReq'])
print "device type: {}".format(zeroconf_vars['deviceType'])
def get_metadata():
metadata = ffi.new('SpMetadata *')
lib.SpGetMetadata(metadata, 0)
return convert_to_python(metadata[0])
def get_image_url(uri):
image_url = ffi.new('char[512]')
#TODO: Change back to 640
lib.SpGetMetadataImageURL(uri, lib.kSpImageSizeSmall, image_url, ffi.sizeof(image_url))
return ffi.string(image_url)
#From https://gist.github.com/inactivist/4ef7058c2132fa16759d
def __convert_struct_field( s, fields ):
for field,fieldtype in fields:
if fieldtype.type.kind == 'primitive':
yield (field,getattr( s, field ))
else:
yield (field, convert_to_python( getattr( s, field ) ))
#From https://gist.github.com/inactivist/4ef7058c2132fa16759d
def convert_to_python(s):
type=ffi.typeof(s)
if type.kind == 'struct':
return dict(__convert_struct_field( s, type.fields ) )
elif type.kind == 'array':
if type.item.kind == 'primitive':
if type.item.cname == 'char':
return ffi.string(s)
else:
return [ s[i] for i in range(type.length) ]
else:
return [ convert_to_python(s[i]) for i in range(type.length) ]
elif type.kind == 'primitive':
return int(s)