feat: 优化多设备接口执行效果,尽量做到同时执行

This commit is contained in:
涵曦 2024-07-05 17:06:13 +00:00
parent 6a583119d0
commit cb0bae5ae7

View File

@ -289,13 +289,19 @@ class XiaoMusic:
self.log.info(f"do_tts ok. cur_music:{self.cur_music}") self.log.info(f"do_tts ok. cur_music:{self.cur_music}")
await self.check_replay() await self.check_replay()
async def text_to_speech(self, value): async def text_to_speech_one(self, device_id, value):
try: try:
for device_id in self.device2hardware: await self.mina_service.text_to_speech(device_id, value)
await self.mina_service.text_to_speech(device_id, value)
except Exception as e: except Exception as e:
self.log.error(f"Execption {e}") self.log.error(f"Execption {e}")
async def text_to_speech(self, value):
tasks = [
self.text_to_speech_one(device_id, value)
for device_id in self.device2hardware
]
await asyncio.gather(*tasks)
# 继续播放被打断的歌曲 # 继续播放被打断的歌曲
async def check_replay(self): async def check_replay(self):
if self.isplaying() and not self.isdownloading(): if self.isplaying() and not self.isdownloading():
@ -336,20 +342,25 @@ class XiaoMusic:
# stop it # stop it
ret = await self.mina_service.player_stop(device_id) ret = await self.mina_service.player_stop(device_id)
self.log.info( self.log.info(
f"force_stop_xiaoai player_stop device_id:{device_id} ret:{ret}" f"stop_if_xiaoai_is_playing player_stop device_id:{device_id} ret:{ret}"
) )
async def force_stop_xiaoai(self): async def force_stop_one_xiaoai(self, device_id):
try: try:
for device_id in self.device2hardware: ret = await self.mina_service.player_pause(device_id)
ret = await self.mina_service.player_pause(device_id) self.log.info(
self.log.info( f"force_stop_one_xiaoai player_pause device_id:{device_id} ret:{ret}"
f"force_stop_xiaoai player_pause device_id:{device_id} ret:{ret}" )
) await self.stop_if_xiaoai_is_playing(device_id)
await self.stop_if_xiaoai_is_playing(device_id)
except Exception as e: except Exception as e:
self.log.error(f"Execption {e}") self.log.error(f"Execption {e}")
async def force_stop_xiaoai(self):
tasks = [
self.force_stop_one_xiaoai(device_id) for device_id in self.device2hardware
]
await asyncio.gather(*tasks)
# 是否在下载中 # 是否在下载中
def isdownloading(self): def isdownloading(self):
if not self.download_proc: if not self.download_proc:
@ -720,21 +731,28 @@ class XiaoMusic:
url = kwargs.get("arg1", "") url = kwargs.get("arg1", "")
await self.all_player_play(url) await self.all_player_play(url)
async def all_player_play(self, url): async def play_one_url(self, device_id, url):
try: try:
for device_id in self.device2hardware: if self.config.use_music_api:
if self.config.use_music_api: ret = await self.play_by_music_url(device_id, url)
ret = await self.play_by_music_url(device_id, url) self.log.info(
self.log.info( f"play_one_url play_by_music_url device_id:{device_id} ret:{ret} url:{url}"
f"player_play play_by_music_url device_id:{device_id} ret:{ret} url:{url}" )
) else:
else: ret = await self.mina_service.play_by_url(device_id, url)
ret = await self.mina_service.play_by_url(device_id, url) self.log.info(
self.log.info( f"play_one_url play_by_url device_id:{device_id} ret:{ret} url:{url}"
f"player_play play_by_url device_id:{device_id} ret:{ret} url:{url}" )
)
except Exception as e: except Exception as e:
self.log.error(f"Execption {e}") self.log.error(f"Execption {e}")
return ret
async def all_player_play(self, url):
tasks = [
self.play_one_url(device_id, url) for device_id in self.device2hardware
]
results = await asyncio.gather(*tasks)
self.log.info(f"all_player_play {url} {results}")
def find_real_music_name(self, name): def find_real_music_name(self, name):
if not self.config.enable_fuzzy_match: if not self.config.enable_fuzzy_match: