olivebot/gui/flask_server.py

133 lines
3.6 KiB
Python
Raw Normal View History

import imp
2022-06-20 11:05:10 +08:00
import json
import time
import pyaudio
from flask import Flask, render_template, request
from flask_cors import CORS
import fay_booter
2022-06-20 11:05:10 +08:00
from core.tts_voice import EnumVoice
from gevent import pywsgi
2022-06-20 11:05:10 +08:00
from scheduler.thread_manager import MyThread
from utils import config_util, util
from core import wsa_server
from core import fay_core
from core.content_db import Content_Db
from ai_module import yolov8
2022-06-20 11:05:10 +08:00
__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 list(set(device_list))
2022-06-20 11:05:10 +08:00
@__app.route('/api/submit', methods=['post'])
def api_submit():
data = request.values.get('data')
# print(data)
config_data = json.loads(data)
if(config_data['config']['source']['record']['enabled']):
config_data['config']['source']['record']['channels'] = 0
audio = pyaudio.PyAudio()
for i in range(audio.get_device_count()):
devInfo = audio.get_device_info_by_index(i)
if devInfo['name'].find(config_data['config']['source']['record']['device']) >= 0 and devInfo['hostApi'] == 0:
config_data['config']['source']['record']['channels'] = devInfo['maxInputChannels']
2022-06-20 11:05:10 +08:00
config_util.save_config(config_data['config'])
return '{"result":"successful"}'
@__app.route('/api/control-eyes', methods=['post'])
def control_eyes():
eyes = yolov8.new_instance()
if(not eyes.get_status()):
eyes.start()
util.log(1, "YOLO v8正在启动...")
else:
eyes.stop()
util.log(1, "YOLO v8正在关闭...")
2022-06-20 11:05:10 +08:00
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('/api/send', methods=['post'])
def api_send():
data = request.values.get('data')
info = json.loads(data)
text = fay_core.send_for_answer(info['msg'],info['sendto'])
return '{"result":"successful","msg":"'+text+'"}'
@__app.route('/api/get-msg', methods=['post'])
def api_get_Msg():
contentdb = Content_Db()
list = contentdb.get_list('all','desc',1000)
relist = []
i = len(list)-1
while i >= 0:
relist.append(dict(type=list[i][0],way=list[i][1],content=list[i][2],createtime=list[i][3],timetext=list[i][4]))
i -= 1
return json.dumps({'list': relist})
2022-06-20 11:05:10 +08:00
@__app.route('/', methods=['get'])
def home_get():
return __get_template()
@__app.route('/', methods=['post'])
def home_post():
return __get_template()
def run():
server = pywsgi.WSGIServer(('0.0.0.0',5000), __app)
server.serve_forever()
2022-06-20 11:05:10 +08:00
def start():
MyThread(target=run).start()