Exemplo n.º 1
0
    def read(self, obj_type, obj_inst, prop_id, address):
        datatype = get_datatype(obj_type, prop_id)
        if not datatype:
            print("invalid property %s for object type %s" %
                  (prop_id, object_type))
            return "error invalid property %s for object type %s" % (
                prop_id, object_type)

        # build a request
        request = ReadPropertyRequest(objectIdentifier=(obj_type, obj_inst),
                                      propertyIdentifier=prop_id)
        request.pduDestination = Address(address)

        # TODO: How do we handle an index? This is a 'parameterized get' case that uses b
        # request.propertyArrayIndex = <handle me>

        # build an IOCB, save the request
        iocb = IOCB()
        iocb.ioRequest = request

        # give it to the application to send
        _ss.this_application.do_request(request, iocb)

        # wait for the response
        iocb.ioComplete.wait()

        # filter out errors and aborts
        if isinstance(iocb.ioResponse, Error) or isinstance(
                iocb.ioResponse, AbortPDU):
            print("get operation failed %s %s %s" %
                  (obj_type, obj_inst, str(iocb.ioResponse)))
            n = "error get operation failed %s %s %s" % (obj_type, obj_inst,
                                                         str(iocb.ioResponse))
        else:
            n = self._value_format(prop_id, iocb.ioResponse)

        print("read %s %s %s %s" % (address, obj_inst, prop_id, n))

        return n
Exemplo n.º 2
0
    def write(self, obj_type, obj_inst, prop_id, address, value):
        n = ""
        datatype = get_datatype(obj_type, prop_id)
        if not datatype:
            return "error invalid property %s for object type %s" % (
                prop_id, object_type)

        # set a priority
        priority = 1  #o.get('priority') if o.get('priority') else 1

        # TODO: How do we handle an index? This is a 'parameterized set' case
        indx = None

        # change atomic values into something encodeable, null is a special case
        if (value == 'null'):
            value = Null()
        elif issubclass(datatype, Atomic):
            if datatype is Integer:
                value = int(value)
            elif datatype is Real:
                value = float(value)
            elif datatype is Unsigned:
                value = int(value)
            value = datatype(value)
        elif issubclass(datatype, Array) and (indx is not None):
            if indx == 0:
                value = Integer(value)
            elif issubclass(datatype.subtype, Atomic):
                value = datatype.subtype(value)
            elif not isinstance(value, datatype.subtype):
                return "error invalid result datatype, expecting %s" % (
                    datatype.subtype.__name__, )
        elif not isinstance(value, datatype):
            return "error invalid result datatype, expecting %s" % (
                datatype.subtype.__name__, )

        # build a request
        request = WritePropertyRequest(objectIdentifier=(obj_type, obj_inst),
                                       propertyIdentifier=prop_id)
        request.pduDestination = Address(address)

        # save the value
        request.propertyValue = Any()
        try:
            request.propertyValue.cast_in(value)
        except e as Exception:
            return "error write property cast error %r" % e

        # optional array index
        if indx is not None:
            request.propertyArrayIndex = indx

        # optional priority
        if priority is not None:
            request.priority = priority

        # build an IOCB, save the request
        iocb = IOCB()
        iocb.ioRequest = request

        # give it to the application to send
        _ss.this_application.do_request(request, iocb)

        # wait for the response
        iocb.ioComplete.wait()

        # filter out errors and aborts
        if isinstance(iocb.ioResponse, Error) or isinstance(
                iocb.ioResponse, AbortPDU):
            return "error set operation failed %s %s %s" % (
                obj_inst, prop_id, str(iocb.ioResponse))

        print("wrote %s %s %s %s" % (address, obj_inst, prop_id, value))

        return n