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

- Added lastalexa virtual device.

- Fix asyncio sleep


git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2704 b32b6428-25c9-4566-ad07-03861ab6144f
parent f5e94f2e
Branches
No related tags found
No related merge requests found
......@@ -44,3 +44,10 @@ class Echo(object):
if _lang or _voice:
logger.warning("Unable to change lang or voice right now")
self.gw.run_script(f'-d {self.name} -e speak:"{_msg}"')
class LastEcho(Echo):
def say(self,_msg,_lang=None,_voice=None):
if _lang or _voice:
logger.warning("Unable to change lang or voice right now")
self.gw.run_script_lastalexa(f'-e speak:"{_msg}"')
import platform
import logging
from xaal.lib import tools
from xaal.lib import tools,helpers
from xaal.schemas.devices import gateway
import time
import shlex
import atexit
import asyncio
from .devices import Echo
from .devices import Echo, LastEcho
PACKAGE_NAME = "xaal.alexa"
logger = logging.getLogger(PACKAGE_NAME)
......@@ -20,7 +22,7 @@ class GW(object):
self.engine = engine
self.config()
atexit.register(self._exit)
self.setup()
self.setup_echo()
self.setup_gw()
def config(self):
......@@ -30,25 +32,41 @@ class GW(object):
logger.info('Missing config file, building a new one')
cfg = tools.new_cfg(PACKAGE_NAME)
cfg['config']['remote_script'] = 'alexa_remote_control.sh'
cfg['devices'] = {}
cfg['devices'] = {"last_alexa":{}}
cfg.write()
self.cfg = cfg
self.remote_script = cfg['config']['remote_script']
def run_script(self,cmd):
asyncio.ensure_future(self.async_run(cmd))
async def async_run(self,cmd):
t0 = time.time()
cmd = self.remote_script + " " + cmd
proc = await asyncio.create_subprocess_shell(cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)
args = shlex.split(cmd)
proc = await asyncio.create_subprocess_exec(*args,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
logger.info(f"Run {cmd}: {proc.returncode}")
logger.debug(f"{stdout.decode()}")
output = stdout.decode().strip()
logger.debug(f"{output}")
logger.warning(f'{cmd} done=> {time.time()-t0}')
return output
async def async_run_lastalexa(self,cmd):
self.last_alexa = await self.async_run('-lastalexa')
cmd = f'-d "{self.last_alexa}" ' + cmd
await self.async_run(cmd)
def run_script(self,cmd):
asyncio.ensure_future(self.async_run(cmd))
def run_script_lastalexa(self,cmd):
asyncio.ensure_future(self.async_run_lastalexa(cmd))
def setup(self):
def setup_echo(self):
cfg_list = self.cfg.get('devices',[])
for k in cfg_list:
dev = Echo(k,cfg_list[k],self)
if k=='last_alexa':
dev = LastEcho(k,cfg_list[k],self)
else:
dev = Echo(k,cfg_list[k],self)
self.engine.add_devices(dev.embedded())
def setup_gw(self):
......
......@@ -21,6 +21,6 @@ class Engine(CoreEngine):
self.running = True
while self.running:
self.loop()
await asyncio.sleep(0)
await asyncio.sleep(0.02)
AsyncEngine = Engine
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment