Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 36391ba187 | |||
| aa1e424e8b | |||
| 789f4f38d5 |
+3
-2
@@ -11,7 +11,7 @@ from fuzzywuzzy import fuzz
|
|||||||
from pvrecorder import PvRecorder
|
from pvrecorder import PvRecorder
|
||||||
|
|
||||||
from data import config
|
from data import config
|
||||||
from modules import HomeAssistant
|
from modules import HomeAssistant, MediaPlayerController
|
||||||
from utils import download_models, execute_cmd, play
|
from utils import download_models, execute_cmd, play
|
||||||
|
|
||||||
|
|
||||||
@@ -25,6 +25,7 @@ class Jarvis:
|
|||||||
self.CDIR = os.getcwd()
|
self.CDIR = os.getcwd()
|
||||||
self.VA_CMD_LIST = yaml.safe_load(open('data/commands.yaml', encoding='utf8'))
|
self.VA_CMD_LIST = yaml.safe_load(open('data/commands.yaml', encoding='utf8'))
|
||||||
self.home_assistant = HomeAssistant.HomeAssistant()
|
self.home_assistant = HomeAssistant.HomeAssistant()
|
||||||
|
self.media_player_controller = MediaPlayerController.MediaPlayerController()
|
||||||
self.porcupine = pvporcupine.create(
|
self.porcupine = pvporcupine.create(
|
||||||
access_key=config.PICOVOICE_TOKEN,
|
access_key=config.PICOVOICE_TOKEN,
|
||||||
keywords=['jarvis'],
|
keywords=['jarvis'],
|
||||||
@@ -73,7 +74,7 @@ class Jarvis:
|
|||||||
:return: bool - распознана или нет команда
|
:return: bool - распознана или нет команда
|
||||||
"""
|
"""
|
||||||
print(f"Распознано: {voice}")
|
print(f"Распознано: {voice}")
|
||||||
for x in config.VA_ALIAS + config.VA_TBR:
|
for x in config.VA_ALIAS:
|
||||||
voice = voice.replace(x, "").strip()
|
voice = voice.replace(x, "").strip()
|
||||||
rc = {'cmd': '', 'percent': 0}
|
rc = {'cmd': '', 'percent': 0}
|
||||||
for c, v in self.VA_CMD_LIST.items():
|
for c, v in self.VA_CMD_LIST.items():
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
import platform
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
class MediaPlayerController:
|
||||||
|
"""
|
||||||
|
Модуль для манипуляции музыкой
|
||||||
|
"""
|
||||||
|
def __init__(self):
|
||||||
|
self.os_type = platform.system()
|
||||||
|
|
||||||
|
def play_pause(self) -> None:
|
||||||
|
"""
|
||||||
|
Запуск/остановка музыки
|
||||||
|
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
if self.os_type == 'Windows':
|
||||||
|
self._windows_play_pause()
|
||||||
|
elif self.os_type == 'Linux':
|
||||||
|
self._linux_control("play-pause")
|
||||||
|
|
||||||
|
def next_track(self) -> None:
|
||||||
|
"""
|
||||||
|
Включает следующею композицию
|
||||||
|
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
if self.os_type == 'Windows':
|
||||||
|
self._windows_control("next")
|
||||||
|
elif self.os_type == 'Linux':
|
||||||
|
self._linux_control("next")
|
||||||
|
|
||||||
|
def previous_track(self) -> None:
|
||||||
|
"""
|
||||||
|
Включает предыдущею композицию
|
||||||
|
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
if self.os_type == 'Windows':
|
||||||
|
self._windows_control("previous")
|
||||||
|
elif self.os_type == 'Linux':
|
||||||
|
self._linux_control("previous")
|
||||||
|
|
||||||
|
def _windows_play_pause(self) -> None:
|
||||||
|
"""
|
||||||
|
Запуск/остановка музыки в windows
|
||||||
|
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
import win32con
|
||||||
|
self.key_press(win32con.VK_MEDIA_PLAY_PAUSE)
|
||||||
|
|
||||||
|
def _windows_control(self, action: str) -> None:
|
||||||
|
"""
|
||||||
|
Включает предыдущею или следующею композицию в windows
|
||||||
|
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
import win32con
|
||||||
|
if action == "next":
|
||||||
|
self.key_press(win32con.VK_MEDIA_NEXT_TRACK)
|
||||||
|
elif action == "previous":
|
||||||
|
self.key_press(win32con.VK_MEDIA_PREV_TRACK)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def key_press(key_code: str) -> None:
|
||||||
|
"""
|
||||||
|
Симуляция нажатия и отпускания клавиши
|
||||||
|
|
||||||
|
:param key_code: str - какую кнопку нажать
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
import win32api
|
||||||
|
import win32con
|
||||||
|
win32api.keybd_event(key_code, 0, 0, 0)
|
||||||
|
win32api.keybd_event(key_code, 0, win32con.KEYEVENTF_KEYUP, 0)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _linux_control(command: str) -> None:
|
||||||
|
"""
|
||||||
|
Запускает команду для linux систем
|
||||||
|
|
||||||
|
:param command: str - команда для запуска
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
subprocess.run(["playerctl", command], check=True)
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print(f"Failed to {command}: {e}")
|
||||||
@@ -16,6 +16,14 @@ def execute_cmd(self, cmd: str, recognized_phrase: str, voice: str) -> None:
|
|||||||
self.play("off", True)
|
self.play("off", True)
|
||||||
self.porcupine.delete()
|
self.porcupine.delete()
|
||||||
exit(0)
|
exit(0)
|
||||||
|
elif cmd == 'music_on':
|
||||||
|
self.media_player_controller.play_pause()
|
||||||
|
elif cmd == 'music_off':
|
||||||
|
self.media_player_controller.play_pause()
|
||||||
|
elif cmd == 'music_next':
|
||||||
|
self.media_player_controller.next_track()
|
||||||
|
elif cmd == 'music_previous':
|
||||||
|
self.media_player_controller.previous_track()
|
||||||
elif cmd == 'home_assistant_execute':
|
elif cmd == 'home_assistant_execute':
|
||||||
self.home_assistant.send_process(recognized_phrase)
|
self.home_assistant.send_process(recognized_phrase)
|
||||||
elif cmd == 'home_assistant_get':
|
elif cmd == 'home_assistant_get':
|
||||||
|
|||||||
Reference in New Issue
Block a user