Asset#
- class openfactory.assets.Asset(asset_uuid, ksqlClient, bootstrap_servers=None)[source]#
Bases:
BaseAssetRepresents an OpenFactory Asset using the ASSET_UUID as identifier.
This class encapsulates Asset metadata and a Kafka producer responsible for sending asset data. It uses the ksqlDB topology based on the ASSETS_STREAM stream to handle Asset data.
- ksql#
Client for interacting with ksqlDB.
- Type:
- producer#
Kafka producer instance for sending Asset messages.
- Type:
- Example usage:
import time from openfactory.assets import Asset from openfactory.kafka import KSQLDBClient ksql = KSQLDBClient('http://localhost:8088') cnc = Asset('PROVER3018', ksqlClient=ksql) # list samples print(cnc.samples()) print(cnc.Zact.value) print(cnc.Zact.type) print(cnc.Zact.timestamp) # redefine an attribute value cnc.Zact = 10.0 print(cnc.Zact.value) # callbacks for subscriptions def on_messages(msg_key, msg_value): print(f"[Message] [{msg_key}] {msg_value}") def on_sample(msg_key, msg_value): print(f"[Sample] [{msg_key}] {msg_value}") def on_event(msg_key, msg_value): print(f"[Event] [{msg_key}] {msg_value}") def on_condition(msg_key, msg_value): print(f"[Condition] [{msg_key}] {msg_value}") cnc.subscribe_to_messages(on_messages, 'demo_messages_group') cnc.subscribe_to_samples(on_sample, 'demo_samples_group') cnc.subscribe_to_events(on_event, 'demo_events_group') cnc.subscribe_to_conditions(on_condition, 'demo_conditions_group') # run a main loop while subscriptions remain active try: while True: time.sleep(1) except KeyboardInterrupt: print("Stopping consumer threads ...") cnc.stop_messages_subscription() cnc.stop_samples_subscription() cnc.stop_events_subscription() cnc.stop_conditions_subscription() print("Consumers stopped") finally: ksql.close()
- ASSET_CONSUMER_CLASS#
alias of
KafkaAssetConsumer
- __init__(asset_uuid, ksqlClient, bootstrap_servers=None)[source]#
Initializes the Asset with metadata and a Kafka producer.
- Parameters:
asset_uuid (str) – UUID identifier of the asset.
ksqlClient (KSQLDBClient) – Client for interacting with ksqlDB.
bootstrap_servers (str) – Kafka bootstrap server address. Defaults to config setting.
- add_attribute(asset_attribute)#
Adds a new attribute to the asset.
- Return type:
- Parameters:
asset_attribute (AssetAttribute) – The attribute to be added.
- add_reference_above(above_asset_reference)#
Adds a reference to an asset above the current asset.
- add_reference_below(below_asset_reference)#
Adds a reference to an asset below the current asset.
- attributes()#
Returns all non-‘Method’ attribute IDs associated with this asset.
Queries KSQL_ASSET_TABLE for all attribute IDs of the asset where the type is not ‘Method’.
- conditions()#
Returns all condition-type attributes for this asset.
- events()#
Returns all event-type attributes for this asset.
- method(method, args='')#
Requests the execution of a method for the asset by sending a command to the Kafka stream.
Constructs a message with the provided method name and optional arguments, and sends it to the CMDS_STREAM Kafka topic for processing.
- methods()#
Returns method-type attributes for this asset.
Queries KSQL_ASSET_TABLE for entries where TYPE = ‘Method’ for the asset.
- references_above()#
Retrieves a list of assets above the current asset.
- Return type:
List[Self]- Returns:
List[Self] – A list of asset objects that are above the current asset.
- references_above_uuid()#
Retrieves a list of asset-references of assets above the current asset.
- references_below()#
Retrieves a list of assets below the current asset.
- Return type:
List[Self]- Returns:
List[Self] – A list of asset objects that are below the current asset.
- references_below_uuid()#
Retrieves a list of asset-references below the current asset.
- samples()#
Returns all sample-type attributes for this asset.
- stop_conditions_subscription()#
Stops the NATS consumer and gracefully shuts down the subscription for conditions.
- Return type:
- stop_events_subscription()#
Stops the NATS consumer and gracefully shuts down the subscription for events.
- Return type:
- stop_messages_subscription()#
Stops the NATS consumer and gracefully shuts down the subscription.
- Return type:
- stop_samples_subscription()#
Stops the NATS consumer and gracefully shuts down the subscription for samples.
- Return type:
- subscribe_to_conditions(on_condition)#
Subscribes to asset conditions using a NATS consumer. Only messages with TYPE == ‘Condition’ are forwarded to the callback.
- Return type:
- Parameters:
on_condition (AssetNATSCallback) – Callable that takes (msg_subject: str, msg_value: dict).
- subscribe_to_events(on_event)#
Subscribes to asset events using a NATS consumer. Only messages with TYPE == ‘Events’ are forwarded to the callback.
- Return type:
- Parameters:
on_event (AssetNATSCallback) – Callable that takes (msg_subject: str, msg_value: dict).
- subscribe_to_messages(on_message)#
Subscribes to asset messages using a NATS consumer.
- Return type:
- Parameters:
on_message (AssetNATSCallback) – Callable that takes (msg_subject: str, msg_value: dict) and handles messages.
- subscribe_to_samples(on_sample)#
Subscribes to asset samples using a NATS consumer. Only messages with TYPE == ‘Samples’ are forwarded to the callback.
- Return type:
- Parameters:
on_meon_samplessage (AssetNATSCallback) – Callable that takes (msg_subject: str, msg_value: dict).
- property type: Literal['Samples', 'Condition', 'Events', 'Method', 'OpenFactory', 'UNAVAILABLE']#
Retrieves the type of the asset from ksqlDB.
Executes a SQL query to fetch the asset type. If the query returns no result, the method defaults to ‘UNAVAILABLE’.
- Returns:
Literal[‘Samples’, ‘Condition’, ‘Events’, ‘Method’, ‘OpenFactory’, ‘UNAVAILABLE’] – The asset type as stored in the assets_type table, or ‘UNAVAILABLE’ if not found.
- wait_until(attribute_id, value, timeout=30, use_ksqlDB=False)#
Waits until the asset attribute has a specific value or times out.
Monitors either the NATS cluster or ksqlDB to check if the attribute value matches the expected value. Returns True if the value is found within the given timeout, False otherwise.
- Return type:
- Parameters:
attribute_id (str) – The attribute ID of the asset to monitor.
value (Any) – The value to wait for the attribute to match.
timeout (int) – The maximum time to wait, in seconds. Default is 30 seconds.
use_ksqlDB (bool) – If True, uses ksqlDB instead of NATS to check the attribute value. Default is False.
- Returns:
bool – True if the attribute value matches the expected value within the timeout, False otherwise.