Example #1
0
    def handle_read(self):
        if _debug: MSTPDirector._debug("handle_read")

        try:
            msg, addr = self.socket.recvfrom(512)
            if _debug: MSTPDirector._debug("    - received %d octets ", len(msg))
            pdu = PDU(msg,destination=int(str(self.address)))
            mstp_src = pdu.get()
            pdu.pduSource = Address(mstp_src)

            if _debug: MSTPDirector._debug("Received MSTP PDU={}".format(str(pdu)))

            # send the PDU up to the client
            deferred(self._response, pdu)

        except socket.timeout as err:
            if _debug: MSTPDirector._debug("    - socket timeout: %s", err)

        except socket.error as err:
            if err.args[0] == 11:
                pass
            else:
                if _debug: MSTPDirector._debug("    - socket error: %s", err)

                # pass along to a handler
                self.handle_error(err)

        except Exception as e:
            MSTPDirector._error('Exception in handle_read: {}'.format(e))
pdu = PDU(b'hello')  # Data portion of PDU only
pdu.debug_contents()
# Add source and destination information
pdu = PDU(b'hello', source=1, destination=2)
pdu.pduSource = 1
pdu.pduDestination = 2
pdu.debug_contents()

# Encoding and decoding a PDU
"""THis process consists of consuming content from a PDU (data, source, destination)
and generating content in the destination
Think if Decoding a PDU like taking characters out of an array
Encoding a PDU is like stacking characters into an array"""
pdu = PDU(b'hello!!!', source=1, destination=2)
pdu.debug_contents()
pdu.get()  # 104
pdu.get_short()  # 25964
pdu.get_long()  # 1819222305

pdu.put_long(1819222305)
pdu.put_short(25964)
pdu.put(104)
pdu.debug_contents()
print(str(pdu.pduData.decode('utf-8')))

#%% Addresses

# Local stations hold address data
from bacpypes.pdu import LocalStation, LocalBroadcast
from bacpypes.pdu import RemoteStation, RemoteBroadcast, GlobalBroadcast
addr1 = LocalStation(b'123456')  # 6 octet long address