Skip to content

Commit

Permalink
Support parsing nibepi style logs (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
yozik04 authored Dec 10, 2023
2 parents a797dde + e2c6222 commit b8cbdd9
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions nibe/console_scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from contextlib import AbstractAsyncContextManager
import io
import logging
import re
from typing import IO

import asyncclick as click
Expand Down Expand Up @@ -203,15 +204,34 @@ def read_bytes_socat(file: IO):
yield from bytes.fromhex(line)


RE_NIBEPI_LOG = re.compile(r"Serial: \[((?:[0-9]+,?)+)\]")


def read_bytes_nibepi(file: IO):
lines: list[str] = file.readlines()
for line in lines:
data = RE_NIBEPI_LOG.match(line)
if data:
yield from map(int, data.group(1).split(","))


def parse_stream(stream: io.RawIOBase):
while block := Block.parse_stream(stream):
yield block


@cli.command()
@click.argument("file", type=click.File())
def parse_file(file: IO):
with io.BytesIO(bytes(read_bytes_socat(file))) as stream:
@click.option("--type", type=click.Choice(["hex", "nibepi"]), default="hex")
def parse_file(file: IO, type: str):
if type == "hex":
data = read_bytes_socat(file)
elif type == "nibepi":
data = read_bytes_nibepi(file)
else:
raise ValueError("invalid argument")

with io.BytesIO(bytes(data)) as stream:
for packet in parse_stream(stream):
click.echo(packet.fields.value)

Expand Down

0 comments on commit b8cbdd9

Please sign in to comment.