How to use the pyads.testserver_ex.structs.AmsHeader function in pyads

To help you get started, we’ve selected a few pyads examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stlehmann / pyads / pyads / testserver_ex / client.py View on Github external
def create_notification_packet(self, notification, length, handle):
        sample = AdsNotificationSample(
            handle=handle, sample_size=length, data=notification.value[:length]
        )

        stamp = AdsStampHeader(
            timestamp=dt_to_filetime(datetime.datetime.utcnow()), samples=[sample]
        )

        stream = AdsNotificationStream(stamps=[stamp])

        ams_header = AmsHeader(
            target_net_id=self.ams_net_id,
            target_port=self.ams_port,
            source_net_id=self.server.ams_addr._ams_addr.netId,
            source_port=self.server.port,
            command_id=constants.ADSCOMMAND_DEVICENOTE,
            state_flags=constants.ADSSTATEFLAG_REQRESP,
            data_length=stream.length,
            error_code=0,
            invoke_id=0,
        )

        return AmsPacket(
            amstcp_header=AmsTcpHeader(length=ams_header.length + stream.length),
            ams_header=ams_header,
            ads_data=stream.to_bytes(),
        )
github stlehmann / pyads / tests / test_testserver_ex_structs.py View on Github external
def test_ams_packet(self):
        source_addr = AmsAddr("127.0.0.1.1.1", 59558)
        target_addr = AmsAddr("172.38.1.1.1.1", 59559)

        header1 = structs.AmsHeader(
            target_net_id=target_addr.netIdStruct(),
            target_port=target_addr.port,
            source_net_id=source_addr.netIdStruct(),
            source_port=source_addr.port,
            command_id=pyads.constants.ADSCOMMAND_READDEVICEINFO,
            state_flags=pyads.constants.ADSSTATEFLAG_COMMAND,
            data_length=6,
            error_code=1,
            invoke_id=2,
        )
        ads_data = b"\x00" * 6
        amstcpheader = structs.AmsTcpHeader(header1.length + len(ads_data))
        packet = structs.AmsPacket(amstcpheader, header1, ads_data)

        # first six bytes should be amstcpheader
        self.assertEqual(packet.to_bytes()[:6], amstcpheader.to_bytes())
github stlehmann / pyads / pyads / testserver_ex / structs.py View on Github external
def from_bytes(data):
        return AmsHeader(
            target_net_id=SAmsNetId.from_buffer(bytearray(data[0:6])),
            target_port=struct.unpack("
github stlehmann / pyads / tests / test_testserver_ex_structs.py View on Github external
header1 = structs.AmsHeader(
            target_net_id=target_addr.netIdStruct(),
            target_port=target_addr.port,
            source_net_id=source_addr.netIdStruct(),
            source_port=source_addr.port,
            command_id=pyads.constants.ADSCOMMAND_READDEVICEINFO,
            state_flags=pyads.constants.ADSSTATEFLAG_COMMAND,
            data_length=6,
            error_code=1,
            invoke_id=2,
        )

        data_bytes = header1.to_bytes()
        self.assertEqual(len(data_bytes), header1.length)
        header2 = structs.AmsHeader.from_bytes(data_bytes)

        self.assertEqual(
            bytearray(header1.target_net_id.b), bytearray(header2.target_net_id.b)
        )
        self.assertEqual(header1.target_port, header2.target_port)
        self.assertEqual(
            bytearray(header1.source_net_id), bytearray(header2.source_net_id)
        )
        self.assertEqual(header1.source_port, header2.source_port)
        self.assertEqual(header1.command_id, header2.command_id)
        self.assertEqual(header1.state_flags, header2.state_flags)
        self.assertEqual(header1.data_length, header2.data_length)
        self.assertEqual(header1.error_code, header2.error_code)
        self.assertEqual(header1.invoke_id, header2.invoke_id)
github stlehmann / pyads / pyads / testserver_ex / handler.py View on Github external
constants.ADSCOMMAND_READDEVICEINFO: handle_read_device_info,
            constants.ADSCOMMAND_READ: handle_read,
            constants.ADSCOMMAND_WRITE: handle_write,
            constants.ADSCOMMAND_READWRITE: handle_read_write,
            constants.ADSCOMMAND_READSTATE: handle_read_state,
            constants.ADSCOMMAND_WRITECTRL: handle_writectrl,
            constants.ADSCOMMAND_ADDDEVICENOTE: handle_add_devicenote,
            constants.ADSCOMMAND_DELDEVICENOTE: handle_delete_devicenote,
            constants.ADSCOMMAND_DEVICENOTE: handle_devicenote,
        }

        response_content = function_map[request.ams_header.command_id]()

        return AmsPacket(
            amstcp_header=AmsTcpHeader(37 + len(response_content)),
            ams_header=AmsHeader(
                # swap target and source ams-id and port
                target_net_id=request.ams_header.source_net_id,
                target_port=request.ams_header.source_port,
                source_net_id=request.ams_header.target_net_id,
                source_port=request.ams_header.target_port,
                # mirror the command id from the request
                command_id=request.ams_header.command_id,
                # set response flag
                state_flags=request.ams_header.state_flags | 0x0001,
                data_length=len(response_content),
                error_code=0,
                invoke_id=request.ams_header.invoke_id,
            ),
            ads_data=response_content
        )
github stlehmann / pyads / tests / test_testserver_ex_structs.py View on Github external
def test_amsheader(self):
        source_addr = AmsAddr("127.0.0.1.1.1", 59558)
        target_addr = AmsAddr("172.38.1.1.1.1", 59559)

        header1 = structs.AmsHeader(
            target_net_id=target_addr.netIdStruct(),
            target_port=target_addr.port,
            source_net_id=source_addr.netIdStruct(),
            source_port=source_addr.port,
            command_id=pyads.constants.ADSCOMMAND_READDEVICEINFO,
            state_flags=pyads.constants.ADSSTATEFLAG_COMMAND,
            data_length=6,
            error_code=1,
            invoke_id=2,
        )

        data_bytes = header1.to_bytes()
        self.assertEqual(len(data_bytes), header1.length)
        header2 = structs.AmsHeader.from_bytes(data_bytes)

        self.assertEqual(