Exemple #1
0
def test_listen(ubusd_test, event_sender, disconnect_after):
    listen_test = {"passed": False, "passed2": False}

    def set_result(event, data):
        assert event == "event_sender"
        assert data == dict(a="b", c=3, d=False)
        listen_test["passed"] = True

    def test1(event, data):
        assert event == "event_sender"
        assert data == dict(a="b", c=3, d=False)
        listen_test["passed2"] = True

    path = UBUSD_TEST_SOCKET_PATH
    timeout = 300
    event_name = "event_sender"

    with CheckRefCount(path, time, event_name, test1):

        ubus.connect(socket_path=path)
        ubus.listen((event_name, test1), (event_name, set_result))
        ubus.listen((event_name, test1))

        del set_result

        ubus.loop(timeout)
        assert listen_test["passed"]
        assert listen_test["passed2"]

        listen_test = {"passed": False, "passed2": False}
        ubus.loop(timeout)
        assert listen_test["passed"]
        assert listen_test["passed2"]

        ubus.disconnect()
        def process_function():
            def handler1(*arg):
                pass

            import ubus
            ubus.connect(UBUSD_TEST_SOCKET_PATH)
            ubus.add(
                "registered_object1",
                {
                    "method1": {
                        "method": handler1,
                        "signature": {}
                    },
                    "method2": {
                        "method": handler1,
                        "signature": {
                            "first": ubus.BLOBMSG_TYPE_STRING,
                            "second": ubus.BLOBMSG_TYPE_BOOL,
                            "third": ubus.BLOBMSG_TYPE_INT32,
                        }
                    },
                },
            )
            ubus.add(
                "registered_object2",
                {},
            )
            ubus.add("registered_object3", {
                "method1": {
                    "method": handler1,
                    "signature": {}
                },
            })
            guard.touch()
            ubus.loop()
def main():
    try:
        ubus.connect()

        init()

        while True:
            ubus.loop(1)

    except KeyboardInterrupt:
        ubus.disconnect()
Exemple #4
0
def test_multi_objects_listeners(ubusd_test, event_sender, calls_extensive,
                                 disconnect_after):
    counts = 20
    listen_test = {"pass%d" % e: False for e in range(counts)}
    object_test = {"pass%d" % e: False for e in range(counts)}
    event_name = "event_sender"
    timeout = 200

    path = UBUSD_TEST_SOCKET_PATH

    def passed_listen_gen(index):
        def passed(*args):
            listen_test["pass%d" % index] = True

        return passed

    def passed_object_gen(index):
        def passed(*args):
            object_test["pass%d" % index] = True

        return passed

    with CheckRefCount(path, time):

        for _ in range(5):
            ubus.connect(socket_path=path)

            for i in range(counts):
                ubus.listen((event_name, passed_listen_gen(i)))
                ubus.add("extensive_object_%d" % i, {
                    "method": {
                        "method": passed_object_gen(i),
                        "signature": {}
                    }
                })

            stored_counter = calls_extensive.counter.value
            while calls_extensive.counter.value - stored_counter < counts:
                ubus.loop(timeout)
            ubus.disconnect()

            for i in range(counts):
                current = "pass%d" % i
                assert listen_test[current]
                assert object_test[current]

            listen_test = {"pass%d" % e: False for e in range(counts)}
            object_test = {"pass%d" % e: False for e in range(counts)}
        def process_function():
            def handler1(handler, data):
                data["passed"] = True
                handler.reply(data)

            def handler2(handler, data):
                data["passed1"] = True
                handler.reply(data)
                data["passed2"] = True
                handler.reply(data)
                data["passed3"] = True
                handler.reply(data)

            def handler_fail(handler, data):
                raise Exception("Handler Fails")

            import ubus
            ubus.connect(UBUSD_TEST_SOCKET_PATH)
            ubus.add(
                "responsive_object",
                {
                    "respond": {
                        "method": handler1,
                        "signature": {
                            "first": ubus.BLOBMSG_TYPE_STRING,
                            "second": ubus.BLOBMSG_TYPE_BOOL,
                            "third": ubus.BLOBMSG_TYPE_INT32,
                        }
                    },
                    "fail": {
                        "method": handler_fail,
                        "signature": {}
                    },
                    "multi_respond": {
                        "method": handler2,
                        "signature": {}
                    },
                    "number": {
                        "method": handler1,
                        "signature": {
                            "number": ubus.BLOBMSG_TYPE_INT32,
                        }
                    },
                },
            )
            guard.touch()
            ubus.loop()
Exemple #6
0
def test_reply(ubusd_test, call_for_object, disconnect_after):
    path = UBUSD_TEST_SOCKET_PATH

    results = {e: {'data': None, 'exits': False} for e in range(1, 4)}

    def handler1(handler, data):
        results[1]['data'] = data
        handler.reply(data)
        results[1]['exits'] = True

    def handler2(handler, data):
        results[2]['data'] = data
        handler.reply(data)
        results[2]['exits'] = True

    def handler3(handler, data):
        results[3]['data'] = data
        handler.reply(data)
        results[3]['exits'] = True

    with CheckRefCount(path, results, handler1, handler2, handler3):

        ubus.connect(path)
        ubus.add(
            "callee_object",
            {
                "method1": {
                    "method": handler1,
                    "signature": {
                        "first": ubus.BLOBMSG_TYPE_INT32,
                    }
                },
                "method2": {
                    "method": handler2,
                    "signature": {
                        "second": ubus.BLOBMSG_TYPE_INT32,
                    }
                },
                "method3": {
                    "method": handler3,
                    "signature": {}
                },
            },
        )
        ubus.loop(500)

        assert results == {
            1: {
                'data': {
                    'first': 1
                },
                'exits': True
            },
            2: {
                'data': {
                    'second': 2
                },
                'exits': True
            },
            3: {
                'data': {},
                'exits': True
            },
        }

        ubus.disconnect()
Exemple #7
0
def test_loop(ubusd_test, disconnect_after):
    path = UBUSD_TEST_SOCKET_PATH

    time1 = 200
    time2 = 1
    time3 = 50
    time4 = 2**65
    time5 = None
    time6 = "5"
    time7 = 0

    with CheckRefCount(path, time1, time2, time3, time4, time6, time7):

        with pytest.raises(RuntimeError):
            ubus.loop(time1)

        ubus.connect(socket_path=path)
        assert ubus.loop(time1) is None
        assert ubus.loop(time2) is None
        assert ubus.loop(time3) is None

        with pytest.raises(OverflowError):
            ubus.loop(time4)

        with pytest.raises(TypeError):
            ubus.loop(time5)

        with pytest.raises(TypeError):
            ubus.loop(time6)

        assert ubus.loop(time7) is None

        ubus.disconnect()
Exemple #8
0
    for k in data:
        if k == "voltage":
            if data['voltage'] > power_supply_reaquare and notified_power_supply:
                commands.getoutput('create_notification -s news "Sitove napajeni bylo obnoveno." "Power supply was reaquired."')
                notified_power_supply = False
            elif data['voltage'] <= power_supply_cut_off and not notified_power_supply: # 12.8V
                commands.getoutput('create_notification -s error "Vypadlo sitove napajeni!" "Power supply was interrupted!"')
                notified_power_supply = True
            if data['voltage'] > battery_not_low and notified_low_battery:
                notified_low_battery = False
            elif data['voltage'] <= battery_low and not notified_low_battery: # 12V
                volts = float(data['voltage']) * 0.0593
                commands.getoutput('create_notification -s error "Nizke napetu akumulatoru! %.2f V" "Low voltage of the acumulator! %.2f V"' % (volts, volts))
                notified_low_battery = True
            if data['voltage'] <= battery_not_high and notified_high_battery:
                notified_high_battery = False
            elif data['voltage'] > battery_high and not notified_high_battery:
                commands.getoutput('create_notification -s error "Vysoke napetu akumulatoru!" "High voltage of the acumulator!"')
                notified_high_battery = True

# TODO add to exit handler
# i2c.close()

while True:
    ubus.loop(60000)
    data = load_data()
    #print(data)
    db_data(data)
    ubus_data(data)
    check_data(data)