Exemple #1
0
            async def run(*args, **kwargs):
                called.append("run")

                if len(called) > 0:
                    s1 = DiscoveryMessages.StateService(
                        service=Services.UDP, port=56, target="d073d5000001"
                    )
                    s1.Information.update(
                        remote_addr=("192.168.0.3", 56700),
                        sender_message=DiscoveryMessages.GetService(),
                    )
                    yield s1

                if len(called) > 1:
                    s2 = DiscoveryMessages.StateService(
                        service=Services.UDP, port=58, target="d073d5000002"
                    )
                    s2.Information.update(
                        remote_addr=("192.168.0.4", 56700),
                        sender_message=DiscoveryMessages.GetService(),
                    )
                    yield s2

                if len(called) > 2:
                    s3 = DiscoveryMessages.StateService(
                        service=Services.UDP, port=59, target="d073d5000003"
                    )
                    s3.Information.update(
                        remote_addr=("192.168.0.5", 56700),
                        sender_message=DiscoveryMessages.GetService(),
                    )
                    yield s3
Exemple #2
0
 async def respond(s, event):
     if event | DeviceMessages.GetPower:
         event.set_replies(DeviceMessages.StatePower(level=s.device_attrs.power))
     if event | DeviceMessages.GetLabel:
         event.ignore_request()
     elif event | DeviceMessages.SetPower:
         event.set_replies(DeviceMessages.StatePower(level=s.device_attrs.power))
         await s.device_attrs.attrs_apply(
             s.device_attrs.attrs_path("power").changer_to(event.pkt.level),
             event=None,
         )
     elif event | DiscoveryMessages.GetService:
         event.set_replies(
             DiscoveryMessages.StateService(service=Services.UDP, port=56700),
             DiscoveryMessages.StateService(service=Services.UDP, port=76500),
         )
Exemple #3
0
 async def respond(self, event):
     if event | DiscoveryMessages.GetService and event.io is self:
         port = self.options.get("state_service_port", self.options.port)
         state_service_port = 0 if port is None else port
         event.add_replies(
             DiscoveryMessages.StateService(
                 service=self.options.state_service,
                 port=state_service_port))
Exemple #4
0
 async def run(*args, **kwargs):
     s1 = DiscoveryMessages.StateService(
         service=Services.UDP, port=56, target="d073d5000001"
     )
     s1.Information.update(
         remote_addr=("192.168.0.3", 56700),
         sender_message=DiscoveryMessages.GetService(),
     )
     yield s1
Exemple #5
0
    async def ensure_udp_service(self):
        await self.stop_service(Services.UDP)

        class ServerProtocol(asyncio.Protocol):
            def connection_made(sp, transport):
                sp.udp_transport = transport

            def datagram_received(sp, data, addr):
                if not self.attrs.online:
                    return

                async def received_data(bts, a):
                    sp.udp_transport.sendto(bts, addr)

                self.sync_write("udp", received_data, data)

            def error_received(sp, exc):
                log.error(hp.lc("Error on udp transport", error=exc))

        remote = None

        for i in range(3):
            port = self.port
            if port is None:
                port = self.make_port()

            try:
                remote, _ = await asyncio.get_event_loop(
                ).create_datagram_endpoint(ServerProtocol,
                                           local_addr=("0.0.0.0", port))
                break
            except OSError:
                log.exception("Couldn't make datagram server")
                await asyncio.sleep(0.1)

        if remote is None:
            raise Exception("Failed to bind to a udp socket for fake device")

        async def closer():
            remote.close()

        async def add_service(adder):
            await adder(self.serial, Services.UDP, host="127.0.0.1", port=port)

        def address(source):
            if source == "udp":
                return ("127.0.0.1", port)

        state_service = DiscoveryMessages.StateService(service=Services.UDP,
                                                       port=port)
        self.services.append(
            Service(Services.UDP, closer, add_service, state_service, address))
Exemple #6
0
    async def ensure_memory_service(self):
        await self.stop_service(MemoryService)

        async def closer():
            pass

        async def add_service(adder):
            await adder(self.serial,
                        MemoryService,
                        writer=partial(self.write, "memory"))

        def address(source):
            if source == "memory":
                return (f"fake://{self.serial}/memory", 56700)

        state_service = DiscoveryMessages.StateService(service=Services.UDP,
                                                       port=56700)
        self.services.append(
            Service(MemoryService, closer, add_service, state_service,
                    address))
Exemple #7
0
 def make_response(self, pkt, protocol):
     if pkt | DiscoveryMessages.GetService:
         msgs = []
         for service, (_, port) in self.services[0]:
             msgs.append(DiscoveryMessages.StateService(service=service, port=port))
         return msgs
Exemple #8
0
            def datagram_received(sp, data, addr):
                pkt = DiscoveryMessages.unpack(data, protocol_register=protocol_register)
                assert pkt | DiscoveryMessages.GetService

                res = DiscoveryMessages.StateService(target=target, source=pkt.source, sequence=pkt.sequence, service=Services.UDP, port=port)
                self.transport.sendto(res.tobytes(None), addr)
Exemple #9
0
                            sent_pkt_type=502,
                            sequence=1,
                            source=2,
                            serial=device.serial,
                        ):
                            await send_single(original, timeout=3, no_retry=True)

            assert t.time == 3

            assertSent(sender, (0, device.serial, original.Payload.__name__, original.payload))

        async it "doesn't wait beyond timeout for unlimited reply messages that get no reply", send_single, sender, device, FakeTime, MockedCallLater:
            original = DiscoveryMessages.GetService(ack_required=False)
            io = device.io["MEMORY"]

            reply1 = DiscoveryMessages.StateService(service=1, port=56700)
            reply2 = DiscoveryMessages.StateService(service=2, port=56700)

            info = {"m": None}

            @io.packet_filter.intercept_process_request
            async def process_request(event, Cont):
                if event | DiscoveryMessages.GetService:
                    event.set_replies(Cont)
                    event.handled = False
                    event._viewers_only = True
                    return True
                else:
                    raise Cont()

            @io.packet_filter.intercept_process_outgoing