def query_object(sock, parameter): # query information about an object ID: object_info = R.get_by_name(parameter) # construct a byte stream that will send a read command for the object ID we want, and send it send_frame = make_frame(command=Command.READ, id=object_info.object_id) sock.send(send_frame) # loop until we got the entire response frame frame = ReceiveFrame() while True: ready_read, _, _ = select.select([sock], [], [], 2.0) if sock in ready_read: # receive content of the input buffer buf = sock.recv(256) # if there is content, let the frame consume it if len(buf) > 0: frame.consume(buf) # if the frame is complete, we're done if frame.complete(): break else: # the socket was closed by the device, exit sys.exit(1) # decode the frames payload value = decode_value(object_info.response_data_type, frame.data) return value
def add_ids(self, ids: Union[str, List[str]], interval: int = 60, inventory: bool = True, is_inventory: bool = False, handler: OidHandler = None) -> None: ''' Adds managed frames to the list. :param ids: List of names or an individual name. :param interval: Interval at which the OID should be checked. :param inventory: Whether the OID is considered to be for building the inventory. :param cb_fun: Optional callback, if set it is registered to be called when the OID is received. ''' if isinstance(ids, List): for oid in ids: self.add_ids(oid, interval, inventory, is_inventory, handler) else: try: tmp_oinfo = R.get_by_name(ids) except KeyError: log.error('Failed to add OID %s: Not found in registry', ids) else: self._frames[tmp_oinfo.object_id] = ManagedFrame(oinfo=tmp_oinfo, interval=interval, is_inventory=is_inventory) if inventory: self._inventory_ids.append(tmp_oinfo.object_id) if handler is not None: self.add_callback(tmp_oinfo.object_id, handler)
def __post_init__(self): if not self.object_names: self.object_names = [self.key] self.object_infos = [ REGISTRY.get_by_name(object_name) for object_name in self.object_names ]
def get_api_response_by_name(self, object_name: str, default: Optional[ApiResponse] = None): return self.get_api_response_by_id( REGISTRY.get_by_name(object_name).object_id, default)
def __post_init__(self): self.object_infos = [ REGISTRY.get_by_name(object_name) for object_name in self.object_names ]