Files
esa-python-sdk/examples/example-multiple-devices.py
2026-04-12 16:06:44 +01:00

63 lines
1.7 KiB
Python

import asyncio
from esasdk import Machine
######## Custom command broadcast to multiple machines ########
async def broadcast_command(machines, command):
return await asyncio.gather(
*[m._send_command(command) for m in machines]
)
######## Different commands to different machines ########
async def mixed_operations():
m1 = Machine("ws://worker1")
m2 = Machine("ws://supervisor")
await asyncio.gather(
m1.connect(),
m2.connect()
)
results = await asyncio.gather(
m1.poweroff(),
m2.info()
)
await asyncio.gather(
m1.close(),
m2.close()
)
################### MAIN EXAMPLE ################
async def manage_machine(machine: Machine):
"""Handle connection and commands for a single machine"""
try:
await machine.connect()
print(f"Connected to {machine.uri}")
# Get machine info
info = await machine.info()
print(f"{machine.uri} info: {info}")
# Power off the machine
result = await machine.poweroff()
print(f"{machine.uri} poweroff result: {result}")
except Exception as e:
print(f"Error with {machine.uri}: {str(e)}")
finally:
await machine.close()
async def main():
# Create multiple machine connections
machines = [
Machine("ws://machine1.example.com:8765"),
Machine("ws://machine2.example.com:8765"),
Machine("ws://machine3.example.com:8765")
]
# Run all machine operations concurrently
await asyncio.gather(*[manage_machine(m) for m in machines])
if __name__ == "__main__":
asyncio.run(main())