新增设置声音按钮

This commit is contained in:
涵曦 2024-01-29 23:10:16 +08:00
parent 714cae9bd1
commit cad7b53aa4
4 changed files with 83 additions and 31 deletions

View File

@ -12,22 +12,22 @@ LATEST_ASK_API = "https://userprofile.mina.mi.com/device_profile/v2/conversation
COOKIE_TEMPLATE = "deviceId={device_id}; serviceToken={service_token}; userId={user_id}"
HARDWARE_COMMAND_DICT = {
# hardware: (tts_command, wakeup_command)
"LX06": ("5-1", "5-5"),
"L05B": ("5-3", "5-4"),
"S12A": ("5-1", "5-5"),
"LX01": ("5-1", "5-5"),
"L06A": ("5-1", "5-5"),
"LX04": ("5-1", "5-4"),
"L05C": ("5-3", "5-4"),
"L17A": ("7-3", "7-4"),
"X08E": ("7-3", "7-4"),
"LX05A": ("5-1", "5-5"), # 小爱红外版
"LX5A": ("5-1", "5-5"), # 小爱红外版
"L07A": ("5-1", "5-5"), # Redmi小爱音箱Play(l7a)
"L15A": ("7-3", "7-4"),
"X6A": ("7-3", "7-4"), # 小米智能家庭屏6
"X10A": ("7-3", "7-4"), # 小米智能家庭屏10
# hardware: (tts_command, wakeup_command, volume_command)
"LX06": ("5-1", "5-5", "2-1"),
"L05B": ("5-3", "5-4", "2-1"),
"S12A": ("5-1", "5-5", "2-1"),
"LX01": ("5-1", "5-5", "2-1"),
"L06A": ("5-1", "5-5", "2-1"),
"LX04": ("5-1", "5-4", "2-1"),
"L05C": ("5-3", "5-4", "2-1"),
"L17A": ("7-3", "7-4", "2-1"),
"X08E": ("7-3", "7-4", "2-1"),
"LX05A": ("5-1", "5-5", "2-1"), # 小爱红外版
"LX5A": ("5-1", "5-5", "2-1"), # 小爱红外版
"L07A": ("5-1", "5-5", "2-1"), # Redmi小爱音箱Play(l7a)
"L15A": ("7-3", "7-4", "2-1"),
"X6A": ("7-3", "7-4", "2-1"), # 小米智能家庭屏6
"X10A": ("7-3", "7-4", "2-1"), # 小米智能家庭屏10
# add more here
}
@ -43,6 +43,7 @@ KEY_WORD_DICT = {
"关机": "stop",
"停止播放": "stop",
"分钟后关机": "stop_after_minute",
"set_volume#": "set_volume",
}
# 命令参数在前面
@ -52,6 +53,7 @@ KEY_WORD_ARG_BEFORE_DICT = {
# 匹配优先级
KEY_MATCH_ORDER = [
"set_volume#",
"分钟后关机",
"播放歌曲",
"放歌曲",
@ -96,6 +98,10 @@ class Config:
def wakeup_command(self) -> str:
return HARDWARE_COMMAND_DICT.get(self.hardware, DEFAULT_COMMAND)[1]
@property
def volume_command(self) -> str:
return HARDWARE_COMMAND_DICT.get(self.hardware, DEFAULT_COMMAND)[2]
@classmethod
def from_options(cls, options: argparse.Namespace) -> Config:
config = {}

View File

@ -6,18 +6,35 @@ $(function(){
$container=$("#cmds");
// 遍历数据
for (const [key, value] of Object.entries(data)) {
if (key != "分钟后关机" && key != "放歌曲") {
append_op_button(key);
if (key != "分钟后关机"
&& key != "放歌曲"
&& key != "停止播放"
&& !key.includes("#")) {
append_op_button_name(key);
}
}
append_op_button("5分钟后关机");
append_op_button("10分钟后关机");
append_op_button("30分钟后关机");
append_op_button("60分钟后关机");
$container.append($("<hr>"));
append_op_button_name("10分钟后关机");
append_op_button_name("30分钟后关机");
append_op_button_name("60分钟后关机");
$container.append($("<hr>"));
append_op_button_volume("声音设为5", 5);
append_op_button_volume("声音设为10", 10);
append_op_button_volume("声音设为30", 30);
append_op_button_volume("声音设为50", 50);
append_op_button_volume("声音设为80", 80);
append_op_button_volume("声音设为100", 100);
});
function append_op_button(name) {
function append_op_button_volume(name, value) {
append_op_button(name, "set_volume#"+value);
}
function append_op_button_name(name) {
append_op_button(name, name);
}
function append_op_button(name, cmd) {
// 创建按钮
const $button = $("<button>");
$button.text(name);
@ -30,7 +47,7 @@ $(function(){
type: "POST",
url: "/cmd",
contentType: "application/json",
data: JSON.stringify({cmd: name}),
data: JSON.stringify({"cmd": cmd}),
success: () => {
// 请求成功时执行的操作
},

View File

@ -10,8 +10,17 @@
margin: 10px;
width: 100px;
height: 50px;
border: none; /* 无边框 */
color: white; /* 白色文字 */
text-align: center; /* 文字居中 */
text-decoration: none; /* 无下划线 */
display: inline-block; /* 行内块元素 */
border-radius: 10px; /* 圆角 */
background-color: #008CBA; /* 蓝色 */
}
input {
margin: 10px;
width: 200px;
height: 40px;
}
</style>

View File

@ -95,7 +95,6 @@ class XiaoMusic:
await self._init_data_hardware()
session.cookie_jar.update_cookies(self.get_cookie())
self.cookie_jar = session.cookie_jar
StartHTTPServer(self.port, self.music_path, self)
async def login_miboy(self, session):
account = MiAccount(
@ -214,6 +213,13 @@ class XiaoMusic:
async def do_tts(self, value, wait_for_finish=False):
self.log.info("do_tts: %s", value)
if self.config.mute_xiaoai:
await self.stop_if_xiaoai_is_playing()
else:
# waiting for xiaoai speaker done
await asyncio.sleep(8)
if not self.config.use_command:
try:
await self.mina_service.text_to_speech(self.device_id, value)
@ -230,6 +236,19 @@ class XiaoMusic:
await asyncio.sleep(elapse)
await self.wait_for_tts_finish()
async def do_set_volume(self, value):
if not self.config.use_command:
try:
await self.mina_service.player_set_volume(self.device_id, value)
except Exception:
pass
else:
await miio_command(
self.miio_service,
self.config.mi_did,
f"{self.config.volume_command} {value}",
)
async def wait_for_tts_finish(self):
while True:
if not await self.get_if_xiaoai_is_playing():
@ -363,6 +382,7 @@ class XiaoMusic:
async with ClientSession() as session:
self.session = session
await self.init_all_data(session)
StartHTTPServer(self.port, self.music_path, self)
task = asyncio.create_task(self.poll_latest_ask())
assert task is not None # to keep the reference to task, do not remove this
self.log.info(
@ -384,12 +404,6 @@ class XiaoMusic:
await asyncio.sleep(1)
continue
if self.config.mute_xiaoai:
await self.stop_if_xiaoai_is_playing()
else:
# waiting for xiaoai speaker done
await asyncio.sleep(8)
try:
func = getattr(self, opvalue)
await func(arg1=oparg)
@ -403,6 +417,7 @@ class XiaoMusic:
# 匹配参数
matcharg = re.match(patternarg, query)
if not matcharg:
# self.log.debug(patternarg)
continue
argpre = matcharg.groups()[0]
@ -493,3 +508,8 @@ class XiaoMusic:
self._stop_timer = asyncio.ensure_future(_do_stop())
self.log.info(f"{minute}分钟后将关机")
async def set_volume(self, **kwargs):
value = kwargs["arg1"]
await self.do_set_volume(value)
self.log.info(f"声音设置为{value}")