Skip to content
Snippets Groups Projects
Commit 543d29db authored by jkerdreu's avatar jkerdreu
Browse files

Added asyncio example



git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2655 b32b6428-25c9-4566-ad07-03861ab6144f
parent ab6cd1f7
No related branches found
No related tags found
No related merge requests found
txt = """================= xAAL dummy devices =================
This package contains some fake lamps, power_relay, and bot:
This package contains some fake lamps, power_relay, and bots:
- lamp: a simple lamp implementation
- lamp_minimal: a simple lamp using schema devices
- power_relay: a power relay
- temperature: a fake temperature sensor
- autobot: a bot than send turn_on/off and set_brightness
- asyncbot: same as autobot with asyncio
To run a module simply call: python -m xaal.dummy.module
Example: python -m xaal.dummy.lamp 8e1495cc-b98b-11eb-8432-d6bd5fe18736
......
from xaal.lib.asyncio import Engine
from xaal.lib import Device,tools,helpers
import time,random,sys,asyncio
def usage():
print("Usage:")
print(" %s target_address" % sys.argv[0])
def main():
helpers.setup_console_logger()
target = tools.get_uuid(sys.argv[-1])
if not target:
usage()
return
dev = Device("switch.basic",tools.get_random_uuid())
dev.vendor_id = "RAMBo"
dev.product_id = "Fake Auto Switch"
dev.info = 'Autobot switch for %s' % target
eng = Engine()
eng.add_device(dev)
async def run():
while 1:
eng.send_request(dev,[target,],'turn_on')
print('turn_on')
await asyncio.sleep(3)
eng.send_request(dev,[target,],'turn_off')
print('turn_off')
await asyncio.sleep(3)
eng.send_request(dev,[target,],'set_brightness',{'brightness':random.randint(0,100)})
print('set_brightness')
await asyncio.sleep(3)
tasks = [ asyncio.ensure_future(eng.run()),
asyncio.ensure_future(run()), ]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("Bye ..")
\ No newline at end of file
......@@ -2,25 +2,33 @@
import time,random,sys
from xaal.lib import Engine,Device,tools
eng = None
def usage():
print("Usage:")
print(" %s target_address" % sys.argv[0])
def wait():
t0 = time.time()
while time.time() < (t0+3):
eng.loop()
def main():
global eng
target = tools.get_uuid(sys.argv[-1])
if not target:
usage()
return
dev = Device("switch.basic",tools.get_random_uuid())
dev.vendor_id = "RAMBo"
dev.product_id = "Fake Auto Switch"
dev.info = 'Autobot switch for %s' % target
eng = Engine()
eng.start()
eng.add_device(dev)
def wait():
t0 = time.time()
while time.time() < (t0+3):
eng.loop()
def main():
while 1:
eng.send_request(dev,[target,],'turn_on')
print('turn_on')
wait()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment