olivebot/gui/flask_server.py
2022-06-20 11:05:10 +08:00

83 lines
2.0 KiB
Python

import json
import time
import pyaudio
from flask import Flask, render_template, request
from flask_cors import CORS
import fay_booter
from core import wsa_server
from core.tts_voice import EnumVoice
from scheduler.thread_manager import MyThread
from utils import config_util
__app = Flask(__name__)
CORS(__app, supports_credentials=True)
def __get_template():
return render_template('index.html')
def __get_device_list():
audio = pyaudio.PyAudio()
device_list = []
for i in range(audio.get_device_count()):
devInfo = audio.get_device_info_by_index(i)
if devInfo['hostApi'] == 0:
device_list.append(devInfo["name"])
return device_list
@__app.route('/api/submit', methods=['post'])
def api_submit():
data = request.values.get('data')
# print(data)
config_data = json.loads(data)
config_util.save_config(config_data['config'])
return '{"result":"successful"}'
@__app.route('/api/get-data', methods=['post'])
def api_get_data():
wsa_server.get_web_instance().add_cmd({
"voiceList": [
{"id": EnumVoice.XIAO_XIAO.name, "name": "晓晓"},
{"id": EnumVoice.YUN_XI.name, "name": "云溪"}
]
})
wsa_server.get_web_instance().add_cmd({"deviceList": __get_device_list()})
return json.dumps({'config': config_util.config})
@__app.route('/api/start-live', methods=['post'])
def api_start_live():
# time.sleep(5)
fay_booter.start()
time.sleep(1)
wsa_server.get_web_instance().add_cmd({"liveState": 1})
return '{"result":"successful"}'
@__app.route('/api/stop-live', methods=['post'])
def api_stop_live():
# time.sleep(1)
fay_booter.stop()
time.sleep(1)
wsa_server.get_web_instance().add_cmd({"liveState": 0})
return '{"result":"successful"}'
@__app.route('/', methods=['get'])
def home_get():
return __get_template()
@__app.route('/', methods=['post'])
def home_post():
return __get_template()
def start():
MyThread(target=__app.run).start()