u9n / dlms-cosem

A Python library for DLMS/COSEM
Other
80 stars 41 forks source link

Improve OBIS string parsing #39

Closed Krolken closed 3 years ago

Krolken commented 3 years ago

As of now we can only parse an OBIS code from a dotted representation like 0.0.1.8.0.255

But the "normal" way of writing the obis would be 0-0:1.8.0.255 or even 0-0:1.8.0 as the last 255 usually can be omitted (but not in the byte representation)

What we would like is a from_string method that can handle any configuration.

0-0:1.8.0.255 can easily be parsed with regex. 0-0:1.8.0 should also be parsed with regex.

We might even be able to specify that we can have any delimiter. This could come in handy as we in some application have to use 0-0-1-8-0-255 as dot has special meaning in strings.

Some older meters using IEC62056-21 only uses a 3 char representation like 1.8.0 but this should no be allowed as we would not be able to transform back and forth with this.

We should also change the method to_string to have a standard format of the "normal" representation with a option to add a custom delimiter:


def to_string(delimiter: Optional[str] = None)
    if delimiter is not None:
        return f"{a}-{b}:{c}.{e}.{f}.{g}"
    else:
        return  f"{a}{delimiter}{b}{delimiter}{c}{delimiter}{e}{delimiter}{f}{delimiter}{g}"