MyTooliT / ICOc

ICOc is a tool to control the ICOtronic system, acquire data, and test stationary transceiver units and sensory tool holders.
https://mytoolit.github.io/ICOc/
2 stars 0 forks source link

Decrease Amount of Methods/Coroutines of Network Class #53

Open sanssecours opened 9 months ago

sanssecours commented 9 months ago

Description

Currently the Network class contains too many methods (coroutines), which makes finding the right method for a certain task unnecessary cumbersome. Some methods also only make sense, if certain preconditions are true. For example, reading the acceleration range (read_acceleration_sensor_range_in_g) only makes sense for a sensor device, but not for an STU.

Implementation

One option might be to return different objects (that only provide certain functionality) via a context manager or method. These objects could then store a reference to a Network object as attribute that provides only basic functionality. For example, in the case of EEPROM access we could add a method that creates an EEPROM object, which then uses the Network object for sending and receiving the correct command.

Class Hierarchy

The class hierarchy could look something like this:

classDiagram
    class Network {
        -connect_sensor_device() : SensorNode
        +connect_sth(identifier: [str, int, EUI]) 
        +stu()
    }

    class Node {
        +eeprom() : NodeEEPROM
        +reset()
        +get_name() : str
        +set_name(name: str)
        +get_state() : State
    }

    class SensorNode {
        +eeprom() : SensorNodeEEPROM
    }

    class STU {
        +eeprom() : STUEEPROM
    }

    class STH {
        +eeprom() : STHEEPROM
    }

    class SMH {
        +eeprom() : SMHEEPROM
    }

    Node <|-- SensorNode
    Node <|-- STU
    SensorNode <|-- STH
    SensorNode <|-- SMH

    NodeEEPROM <|-- SensorNodeEEPROM
    NodeEEPROM <|-- STUEEPROM
    SensorNodeEEPROM <|-- STHEEPROM
    SensorNodeEEPROM <|-- SMHEEPROM

Examples

Manually Clean Up Network Resources

network = Network()
network.shutdown()

Writing STU EEPROM Data

from datetime import date

async with Network() as network:
    stu = network.stu() # stu: `STU`
    stu_eeprom = stu.eeprom() # stu_eeprom: `STUEEPROM`
    stu_eeprom.write_production_date(date(year=2020, month=10, day=5))

Reading STH EEPROM Data

async with Network() as network:
    # network: `Network` 
    async with network.connect_sensor_device("Test-STH") as sensor_device:
        # sensor_device: `SensorDevice`
    sth_eeprom = sensor_device.eeprom()
    # sth_eeprom: `SensorNodeEEPROM`
    slope = await sth_eeprom.read_x_axis_acceleration_slope()

Reading STH Streaming Data

async with Network() as network:
    async with network.connect_sensor_device("Test-STH") as sth:
        async with sth.open_stream() as stream:
            async for data in stream:
                pass

Open Questions