-
Notifications
You must be signed in to change notification settings - Fork 0
/
protobuf.py
37 lines (28 loc) · 904 Bytes
/
protobuf.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
#!/usr/bin/python2
import advertisement_pb2 as Ad
import json
"""
https://github.com/berezovskyi/protobuf-embedded-c
https://github.com/nanopb/nanopb
"""
eventList = Ad.ScanList()
js = open('input.json').read()
jlist = json.loads(js)
eventList.sourceID = jlist['sourceId']
eventList.timestamp = jlist['timestamp']
eventList.version = 3
eventList.sourceType = "GATEWAY"
for scan in jlist['events']:
ad = eventList.ads.add()
ad.connectable = scan['conn']
ad.rssi = scan['rssi']
ad.mac = scan['deviceAddress'].replace(':', '').decode('hex')
if scan['srData']:
ad.scan_response = scan['srData'].decode('base64')
ad.data = scan['data'].decode('base64')
ad.timestamp = scan['timestamp']
ad.type = 0
length = len(eventList.SerializeToString())
print length, length / (len(jlist['events']) + 0.0)
f = open('proto.bin', 'w')
f.write(eventList.SerializeToString())