Exemplo n.º 1
0
 def melkweg_observer(self, observer: Observer, _) -> Observer:
     parser = MelkwegParser()
     data = fetch(self.scrape_url)
     observer.on_next(
         parser.parse(ParsingContext(venue=self.venue, content=data)))
     observer.on_completed()
     return observer
Exemplo n.º 2
0
def test_create_on_next_throw_close2():
    ex = 'ex'
    next = [True]
    error = [False]
    completed = [False]

    def on_next(x):
        assert 42 == x
        next[0] = True

    def on_error(e):
        assert ex == e
        error[0] = True

    def on_completed():
        completed[0] = True

    res = Observer(on_next, on_error, on_completed)

    res.on_next(42)

    assert next[0]
    assert not error[0]
    assert not completed[0]

    res.on_error(ex)

    assert not completed[0]
    assert error[0]
Exemplo n.º 3
0
    def __scan_range(self, observer: Observer, scan_range: range) -> None:

        with self._context.lock:
            try:
                for unit in scan_range:

                    if unit in self.__found_units:
                        continue

                    device: Optional[ModbusDevice] = self.__scan_unit(unit)

                    if device is not None:

                        self.__found_units.add(unit)

                        # TODO
                        # device.on_close().subscribe(
                        #     lambda _: self.__found_units.remove(device.unit)
                        # )

                        observer.on_next(device)

            except SerialException:
                pass

            except Exception as e:
                observer.on_error(e)
Exemplo n.º 4
0
def test_create_on_next_close_has_error():
    e_ = None
    ex = 'ex'
    next = [False]
    completed = [False]

    def on_next(x):
        assert 42 == x
        next[0] = True

    def on_completed():
        completed[0] = True

    res = Observer(on_next, None, on_completed)

    res.on_next(42)
    assert next[0]
    assert not completed[0]
    try:
        res.on_error(ex)
        assert False
    except Exception as e:
        e_ = e.args[0]

    assert ex == e_
    assert not completed[0]
Exemplo n.º 5
0
    def simplon_observer(self, observer: Observer, _) -> Disposable:
        parser = SimplonParser()
        data = fetch(self.scrape_url)
        events = parser.parse(ParsingContext(venue=self.venue, content=data))

        observer.on_next(events)
        observer.on_completed()
        return observer
Exemplo n.º 6
0
 def observe(observer: Observer, _: Optional[Scheduler]) -> None:
     assert device is not None
     self._should_monitor_evdev_events = True
     for event in device.read_loop():
         if not self._should_monitor_evdev_events:
             break
         if event.type == ecodes.EV_KEY or \
                 (event.type == ecodes.EV_ABS and self.should_send_ev_abs_events):
             observer.on_next(event)
     device.close()
     observer.on_completed()
Exemplo n.º 7
0
def test_create_on_next():
    next = [False]

    def on_next(x):
        assert 42 == x
        next[0] = True

    res = Observer(on_next)

    res.on_next(42)
    assert next[0]
    return res.on_completed()
Exemplo n.º 8
0
    def vera_observer(self, observer: Observer, _) -> Disposable:
        vera_parser = VeraParser()

        items_per_page = 20
        page_index = 0
        done = False
        while not done:
            page_index += 1
            url = self.scrape_url.format(page_index, items_per_page)
            data = fetch(url)
            new_events = vera_parser.parse(ParsingContext(venue=self.venue, content=data))
            observer.on_next(new_events)
            done = len(new_events) < items_per_page
        observer.on_completed()
        return observer
Exemplo n.º 9
0
def test_create_on_next_has_error():
    ex = 'ex'
    next = [False]
    _e = None

    def on_next(x):
        assert 42 == x
        next[0] = True

    res = Observer(on_next)

    res.on_next(42)
    assert next[0]

    try:
        res.on_error(ex)
        assert False
    except Exception as e:
        e_ = e.args[0]

    assert ex == e_
Exemplo n.º 10
0
def test_create_on_next_on_error():
    ex = 'ex'
    next = [True]
    error = [False]

    def on_next(x):
        assert 42 == x
        next[0] = True

    def on_error(e):
        assert ex == e
        error[0] = True

    res = Observer(on_next, on_error)

    res.on_next(42)

    assert next[0]
    assert not error[0]

    res.on_error(ex)
    assert error[0]
Exemplo n.º 11
0
def test_create_on_next_on_completed():
    next = [False]
    completed = [False]

    def on_next(x):
        assert 42 == x
        next[0] = True
        return next[0]

    def on_completed():
        completed[0] = True
        return completed[0]

    res = Observer(on_next, None, on_completed)

    res.on_next(42)

    assert next[0]
    assert not completed[0]

    res.on_completed()

    assert completed[0]
Exemplo n.º 12
0
    def __read_all_values(self, observer: Observer):

        message1 = modbus.read_holding_registers(slave_id=self.__unit,
                                                 starting_address=0,
                                                 quantity=50)

        message2 = modbus.read_holding_registers(slave_id=self.__unit,
                                                 starting_address=50,
                                                 quantity=50)

        message3 = modbus.read_holding_registers(slave_id=self.__unit,
                                                 starting_address=100,
                                                 quantity=2)

        first_batch = self.__context.send(message1, 0.5)
        second_batch = self.__context.send(message2, 0.5)
        message3 = self.__context.send(message3, 0.5)

        all_values = first_batch + second_batch

        values = struct.unpack("23i6h25i", struct.pack("102H", *all_values))

        for k, v in VALUE_MAP.items():
            observer.on_next(DeviceValue(self, v, values[k]))
Exemplo n.º 13
0
def make_TM(observer: Observer, _: Scheduler):
    observer.on_next(Write(1, "x"))
    observer.on_next(Read(2, "y"))
    observer.on_next(Read(1, "x"))
    observer.on_next(Commit(1))
    observer.on_next(Read(2, "x"))
    observer.on_next(Write(2, "y"))
    observer.on_next(Commit(2))
    return observer