|
8 | 8 | # EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
|
9 | 9 | # WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
|
10 | 10 |
|
| 11 | +from ... import SBP |
| 12 | +from .base_logger import BaseLogger, LogIterator |
11 | 13 | import json
|
12 | 14 |
|
13 |
| -from base_logger import BaseLogger |
14 |
| - |
15 | 15 | class JSONLogger(BaseLogger):
|
16 | 16 | """
|
17 | 17 | JSONLogger
|
18 |
| - |
| 18 | +
|
19 | 19 | The :class:`JSONLogger` logs JSON records.
|
20 | 20 | """
|
21 | 21 | def __call__(self, msg):
|
22 | 22 | self.call(msg)
|
23 | 23 |
|
24 | 24 | def fmt_msg(self, msg):
|
25 |
| - return {"delta": self.delta(), "timestamp": self.timestamp(), "data": msg.to_json_dict()} |
26 |
| - |
| 25 | + return {"delta": self.delta(), |
| 26 | + "timestamp": self.timestamp(), |
| 27 | + "data": msg.to_json_dict()} |
| 28 | + |
27 | 29 | def call(self, msg):
|
28 | 30 | self.handle.write(json.dumps(self.fmt_msg(msg)) + "\n")
|
| 31 | + |
| 32 | + |
| 33 | +class JSONLogIterator(LogIterator): |
| 34 | + """ |
| 35 | + JSONLogIterator |
| 36 | +
|
| 37 | + The :class:`JSONLogIterator` is an iterator for reading JSON logs |
| 38 | + of SBP data. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + handle : File-like handle |
| 43 | + Any file-like handle providing SBP messages. |
| 44 | +
|
| 45 | + """ |
| 46 | + |
| 47 | + def next(self): |
| 48 | + """ |
| 49 | + Return the next record tuple from log file containing |
| 50 | + JSON-serialized SBP. If an unknown SBP message type is found, |
| 51 | + it'll return the raw SBP. If EOF, throws exception and then |
| 52 | + returns to start of file. |
| 53 | +
|
| 54 | + Returns |
| 55 | + ------- |
| 56 | + (float, float, object) |
| 57 | + (elapsed msec since beginning of log, UTC timestamp, msg) |
| 58 | +
|
| 59 | + """ |
| 60 | + for line in self.handle: |
| 61 | + data = json.loads(line) |
| 62 | + delta = data['delta'] |
| 63 | + timestamp = data['timestamp'] |
| 64 | + item = SBP.from_json_dict(data['data']) |
| 65 | + try: |
| 66 | + yield (delta, timestamp, self.dispatcher(item)) |
| 67 | + except KeyError: |
| 68 | + yield (delta, timestamp, item) |
| 69 | + self.handle.seek(0, 0) |
| 70 | + raise StopIteration |
0 commit comments