Пример #1
0
    def do_dcc(self, args):
        """dcc <addr> [ <duration> ] <enable> [ <password> ]"""
        args = args.split()
        if _debug: DCCConsoleCmd._debug("do_dcc %r", args)

        try:
            addr = args[0]

            if len(args) == 2:
                enable_disable = args[1]
                time_duration = password = None

            elif len(args) == 3:
                if args[1].isdigit():
                    time_duration = int(args[1])
                    enable_disable = args[2]
                    password = None
                else:
                    time_duration = None
                    enable_disable = args[1]
                    password = args[2]
            else:
                time_duration = int(args[1])
                enable_disable = args[2]
                password = args[3]

            # build a request
            request = DeviceCommunicationControlRequest(
                enableDisable=enable_disable,
                )
            request.pduDestination = Address(addr)

            if time_duration is not None:
                request.timeDuration = time_duration
            if password is not None:
                request.password = password
            if _debug: DCCConsoleCmd._debug("    - request: %r", request)

            # make an IOCB
            iocb = IOCB(request)
            if _debug: DCCConsoleCmd._debug("    - iocb: %r", iocb)

            # give it to the application
            this_application.request_io(iocb)

            # wait for it to complete
            iocb.wait()

            # do something for error/reject/abort
            if iocb.ioError:
                sys.stdout.write(str(iocb.ioError) + '\n')
            else:
                if _debug: DCCConsoleCmd._debug("    - ack")

        except Exception as error:
            DCCConsoleCmd._exception("exception: %r", error)
    def dcc(self, address=None, duration=None, password=None, state=None):
        """
        Will send DeviceCommunicationControl request
        """
        if not self._started:
            raise ApplicationNotStarted(
                "BACnet stack not running - use startApp()")

        if not address:
            raise ValueError("Provide address for request")

        if not state:
            raise ValueError(
                "Provide state ('enable', 'disable', 'disableInitiation'")

        # build a request
        request = DeviceCommunicationControlRequest()
        request.enableDisable = DeviceCommunicationControlRequestEnableDisable.enumerations[
            state]
        request.pduDestination = Address(address)
        if duration:
            request.duration = Unsigned16(duration)

        request.password = CharacterString(password)

        self._log.debug("{:>12} {}".format("- request:", request))

        iocb = IOCB(request)  # make an IOCB

        # pass to the BACnet stack
        deferred(self.this_application.request_io, iocb)

        # Unconfirmed request...so wait until complete
        iocb.wait()  # Wait for BACnet response

        if iocb.ioResponse:  # successful response
            apdu = iocb.ioResponse

            if not isinstance(apdu, SimpleAckPDU):  # expect an ACK
                self._log.warning("Not an ack, see debug for more infos.")
                self._log.debug("Not an ack. | APDU : {} / {}".format(
                    (apdu, type(apdu))))
                return

        if iocb.ioError:  # unsuccessful: error/reject/abort
            apdu = iocb.ioError
            reason = find_reason(apdu)
            raise NoResponseFromController(
                "APDU Abort Reason : {}".format(reason))

        self._log.info(
            "DeviceCommunicationControl request sent to device : {}".format(
                address))