|
| 1 | +import threading |
| 2 | +import time |
| 3 | +from datetime import datetime |
| 4 | +from uuid import uuid4 |
| 5 | + |
| 6 | +from nisystemlink.clients.assetmanagement import AssetManagementClient |
| 7 | +from nisystemlink.clients.assetmanagement.models import ( |
| 8 | + AssetBusType, |
| 9 | + AssetIdentification, |
| 10 | + AssetLocationForCreate, |
| 11 | + AssetPresence, |
| 12 | + AssetPresenceStatus, |
| 13 | + CreateAssetRequest, |
| 14 | + StartUtilizationRequest, |
| 15 | +) |
| 16 | +from nisystemlink.clients.core import HttpConfiguration |
| 17 | +from nisystemlink.clients.core.helpers import read_minion_id |
| 18 | + |
| 19 | +# Configure connection to SystemLink server |
| 20 | +server_configuration = HttpConfiguration( |
| 21 | + server_uri="https://yourserver.yourcompany.com", |
| 22 | + api_key="YourAPIKeyGeneratedFromSystemLink", |
| 23 | +) |
| 24 | + |
| 25 | +client = AssetManagementClient(configuration=server_configuration) |
| 26 | + |
| 27 | +# Generate a unique identifier for this utilization session |
| 28 | +utilization_id = str(uuid4()) |
| 29 | + |
| 30 | +# Create the assets first |
| 31 | +# Define the assets to be created and used in the test |
| 32 | +create_assets_request = [ |
| 33 | + CreateAssetRequest( |
| 34 | + model_name="NI PXIe-6368", |
| 35 | + model_number=4000, |
| 36 | + serial_number="01BB877A", |
| 37 | + vendor_name="NI", |
| 38 | + vendor_number=4244, |
| 39 | + bus_type=AssetBusType.ACCESSORY, |
| 40 | + name="DAQ Device - 01BB877A", |
| 41 | + location=AssetLocationForCreate( |
| 42 | + state=AssetPresence(asset_presence=AssetPresenceStatus.PRESENT) |
| 43 | + ), |
| 44 | + ), |
| 45 | + CreateAssetRequest( |
| 46 | + model_name="NI PXIe-5163", |
| 47 | + model_number=5000, |
| 48 | + serial_number="02CC988B", |
| 49 | + vendor_name="NI", |
| 50 | + vendor_number=4244, |
| 51 | + bus_type=AssetBusType.ACCESSORY, |
| 52 | + name="Oscilloscope - 02CC988B", |
| 53 | + location=AssetLocationForCreate( |
| 54 | + state=AssetPresence(asset_presence=AssetPresenceStatus.PRESENT) |
| 55 | + ), |
| 56 | + ), |
| 57 | +] |
| 58 | + |
| 59 | +# Create the assets in SystemLink |
| 60 | +create_assets_response = client.create_assets(assets=create_assets_request) |
| 61 | + |
| 62 | +if create_assets_response.assets: |
| 63 | + print(f"Created {len(create_assets_response.assets)} asset(s)") |
| 64 | + created_asset_ids = [ |
| 65 | + asset.id for asset in create_assets_response.assets if asset.id |
| 66 | + ] |
| 67 | +else: |
| 68 | + print("Failed to create assets") |
| 69 | + exit(1) |
| 70 | + |
| 71 | +# Define the asset identifications for utilization tracking |
| 72 | +test_assets = [ |
| 73 | + AssetIdentification( |
| 74 | + model_name="NI PXIe-6368", |
| 75 | + model_number=4000, |
| 76 | + serial_number="01BB877A", |
| 77 | + vendor_name="NI", |
| 78 | + vendor_number=4244, |
| 79 | + bus_type=AssetBusType.ACCESSORY, |
| 80 | + ), |
| 81 | + AssetIdentification( |
| 82 | + model_name="NI PXIe-5163", |
| 83 | + model_number=5000, |
| 84 | + serial_number="02CC988B", |
| 85 | + vendor_name="NI", |
| 86 | + vendor_number=4244, |
| 87 | + bus_type=AssetBusType.ACCESSORY, |
| 88 | + ), |
| 89 | +] |
| 90 | + |
| 91 | +# Start asset utilization tracking |
| 92 | +# This marks the assets as "in use" in the SystemLink UI |
| 93 | +# Read the minion ID from the Salt configuration |
| 94 | +minion_id = read_minion_id() or "test-station-01" # Fallback minion ID if not found |
| 95 | + |
| 96 | +start_utilization_request = StartUtilizationRequest( |
| 97 | + utilization_identifier=utilization_id, |
| 98 | + minion_id=minion_id, |
| 99 | + asset_identifications=test_assets, |
| 100 | + utilization_category="Automated Testing", |
| 101 | + task_name="DUT Validation Suite", |
| 102 | + user_name="automation_user", |
| 103 | + utilization_timestamp=datetime.now(), |
| 104 | +) |
| 105 | + |
| 106 | +start_utilization_response = client.start_utilization(request=start_utilization_request) |
| 107 | + |
| 108 | +print(start_utilization_response) |
| 109 | + |
| 110 | +# Verify utilization started successfully |
| 111 | +if start_utilization_response.assets_with_started_utilization: |
| 112 | + print( |
| 113 | + f"Utilization started for {len(start_utilization_response.assets_with_started_utilization)} asset(s)" |
| 114 | + ) |
| 115 | +else: |
| 116 | + print("Failed to start utilization") |
| 117 | + |
| 118 | + |
| 119 | +# Heartbeat mechanism using a background thread |
| 120 | +# IMPORTANT: Heartbeats are for UI purposes only, to keep assets visually |
| 121 | +# marked as "in use" in the SystemLink UI. This applies only to utilizations |
| 122 | +# that have not been ended. While the standard heartbeat interval is 5 minutes, |
| 123 | +# the UI requires heartbeats at least every 10 minutes to continue showing |
| 124 | +# assets as actively utilized. If heartbeats stop, the assets will no longer |
| 125 | +# appear as "in use" in the UI. |
| 126 | +heartbeat_interval = 300 # 5 minutes in seconds |
| 127 | +stop_event = threading.Event() |
| 128 | + |
| 129 | + |
| 130 | +def heartbeat_loop(): |
| 131 | + """Background thread that sends periodic heartbeats. |
| 132 | +
|
| 133 | + This keeps the asset visually marked as "in use" in the SystemLink UI |
| 134 | + (for UI purposes only). Applies only to utilizations that have not been ended. |
| 135 | + The UI requires heartbeats at least every 10 minutes to continue displaying |
| 136 | + the asset as actively utilized. |
| 137 | + """ |
| 138 | + while not stop_event.wait(heartbeat_interval): |
| 139 | + heartbeat_response = client.utilization_heartbeat( |
| 140 | + ids=[utilization_id], |
| 141 | + timestamp=datetime.now(), |
| 142 | + ) |
| 143 | + |
| 144 | + if heartbeat_response.updated_utilization_ids: |
| 145 | + print( |
| 146 | + f"Heartbeat sent at {datetime.now().strftime('%H:%M:%S')} - asset remains 'in use'" |
| 147 | + ) |
| 148 | + |
| 149 | + |
| 150 | +# Start the heartbeat thread |
| 151 | +heartbeat_thread = threading.Thread(target=heartbeat_loop, daemon=True) |
| 152 | +heartbeat_thread.start() |
| 153 | + |
| 154 | +# Simulate a long-running operation where assets are in use |
| 155 | +# In a real scenario, this would be your actual test or operation |
| 156 | +print("\nAssets are now in use. Heartbeats will be sent every 5 minutes...") |
| 157 | +print("Waiting for 10 minutes to demonstrate heartbeats...\n") |
| 158 | + |
| 159 | +time.sleep(600) # Wait 10 minutes |
| 160 | + |
| 161 | +# Stop the heartbeat thread |
| 162 | +stop_event.set() |
| 163 | +heartbeat_thread.join() |
| 164 | + |
| 165 | +# End asset utilization tracking |
| 166 | +end_utilization_response = client.end_utilization( |
| 167 | + ids=[utilization_id], |
| 168 | + timestamp=datetime.now(), |
| 169 | +) |
| 170 | + |
| 171 | +if end_utilization_response.updated_utilization_ids: |
| 172 | + print("\nUtilization ended - asset(s) released") |
| 173 | + |
| 174 | +# Clean up: delete the created assets |
| 175 | +if created_asset_ids: |
| 176 | + client.delete_assets(ids=created_asset_ids) |
| 177 | + print(f"Deleted {len(created_asset_ids)} asset(s)") |
0 commit comments