You've already forked STARK
mirror of
https://github.com/MarkParker5/STARK.git
synced 2025-12-03 21:44:58 +02:00
infinite pool for telebot, sleep mode for voice assistant
This commit is contained in:
@@ -9,10 +9,19 @@ listener = SpeechRecognition.SpeechToText()
|
||||
voice = Text2Speech.Engine()
|
||||
threads = []
|
||||
memory = []
|
||||
online = True
|
||||
voids = 0
|
||||
|
||||
listener.listen_noise()
|
||||
|
||||
if config.double_clap_activation:
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
lastClapTime = 0
|
||||
doubleClap = False
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(12, GPIO.IN)
|
||||
GPIO.add_event_detect(12, GPIO.RISING, callback=checkClap)
|
||||
|
||||
def reply(responce):
|
||||
if responce['text']: # print answer
|
||||
print('Archie: '+responce['text'])
|
||||
@@ -29,6 +38,25 @@ def check_threads():
|
||||
thread['finish_event'].clear()
|
||||
del thread
|
||||
|
||||
# check double clap from arduino microphone module
|
||||
def checkClap(channel):
|
||||
global lastClapTime
|
||||
now = time.time()
|
||||
delta = now - lastClapTime
|
||||
if 0.1 < delta < 0.6:
|
||||
doubleClap = True
|
||||
else:
|
||||
lastClapTime = now
|
||||
|
||||
# waiting for double clap
|
||||
def sleep():
|
||||
global doubleClap
|
||||
while not doubleClap:
|
||||
check_threads()
|
||||
time.sleep(1)
|
||||
else:
|
||||
doubleClap = False
|
||||
|
||||
while True: # main loop
|
||||
check_threads()
|
||||
print('\nYou: ', end='')
|
||||
@@ -38,44 +66,37 @@ while True: # main loop
|
||||
if speech['status'] == 'ok':
|
||||
print(text)
|
||||
voids = 0
|
||||
# set online add clean input if name in text
|
||||
if name := list(set(config.names) & set(text.split(' '))):
|
||||
online = True
|
||||
text = text.replace(name[0], '').strip()
|
||||
# recognize and execute command
|
||||
if online:
|
||||
# repeat last answer
|
||||
if Command.isRepeat(text):
|
||||
reply(memory[0]['responce']);
|
||||
continue
|
||||
# recognize command with context
|
||||
try:
|
||||
cmd, params = memory[0]['cmd'].checkContext(text).values()
|
||||
if memory[0].get('params'): params = {**memory[0].get('params'), **params}
|
||||
except:
|
||||
cmd, params = Command.reg_find(text).values()
|
||||
# execute command
|
||||
responce = cmd.start(params)
|
||||
# say answer
|
||||
reply(responce)
|
||||
# waiting answer on question
|
||||
if responce['type'] == 'question':
|
||||
print('\nYou: ', end='')
|
||||
speech = listener.listen()
|
||||
if speech['status'] == 'ok':
|
||||
text = speech['text']
|
||||
print(text)
|
||||
if responce := responce['callback'].answer(text): reply(responce)
|
||||
# remember the command
|
||||
memory.insert(0, {
|
||||
'text': text,
|
||||
'cmd': cmd,
|
||||
'responce': responce,
|
||||
})
|
||||
# repeat last answer
|
||||
if Command.isRepeat(text):
|
||||
reply(memory[0]['responce']);
|
||||
continue
|
||||
# recognize command with context
|
||||
try:
|
||||
cmd, params = memory[0]['cmd'].checkContext(text).values()
|
||||
if memory[0].get('params'): params = {**memory[0].get('params'), **params}
|
||||
except:
|
||||
cmd, params = Command.reg_find(text).values()
|
||||
# execute command
|
||||
responce = cmd.start(params)
|
||||
# say answer
|
||||
reply(responce)
|
||||
# waiting answer on question
|
||||
if responce['type'] == 'question':
|
||||
print('\nYou: ', end='')
|
||||
speech = listener.listen()
|
||||
if speech['status'] == 'ok':
|
||||
text = speech['text']
|
||||
print(text)
|
||||
if responce := responce['callback'].answer(text): reply(responce)
|
||||
# remember the command
|
||||
memory.insert(0, {
|
||||
'text': text,
|
||||
'cmd': cmd,
|
||||
'responce': responce,
|
||||
})
|
||||
else:
|
||||
if speech['status'] == 'error': print('Отсутсвует подключение к интернету');
|
||||
elif online and speech['status'] == 'void': voids += 1;
|
||||
if online and voids >= 3: online = False; voids = 0
|
||||
if not online:
|
||||
if config.double_clap_activation: break
|
||||
else: listener.listen_noise()
|
||||
elif speech['status'] == 'void': voids += 1;
|
||||
if voids >= 3:
|
||||
voids = 0
|
||||
if config.double_clap_activation: sleep()
|
||||
|
||||
Reference in New Issue
Block a user