Merge from old Gitea
This commit is contained in:
62
examples/example-multiple-devices.py
Normal file
62
examples/example-multiple-devices.py
Normal file
@@ -0,0 +1,62 @@
|
||||
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())
|
||||
Reference in New Issue
Block a user