Skip to content

Commit

Permalink
新增设置声音按钮
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jan 29, 2024
1 parent 714cae9 commit cad7b53
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 31 deletions.
38 changes: 22 additions & 16 deletions xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
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
}

Expand All @@ -43,6 +43,7 @@
"关机": "stop",
"停止播放": "stop",
"分钟后关机": "stop_after_minute",
"set_volume#": "set_volume",
}

# 命令参数在前面
Expand All @@ -52,6 +53,7 @@

# 匹配优先级
KEY_MATCH_ORDER = [
"set_volume#",
"分钟后关机",
"播放歌曲",
"放歌曲",
Expand Down Expand Up @@ -96,6 +98,10 @@ def tts_command(self) -> str:
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 = {}
Expand Down
33 changes: 25 additions & 8 deletions xiaomusic/static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -30,7 +47,7 @@ $(function(){
type: "POST",
url: "/cmd",
contentType: "application/json",
data: JSON.stringify({cmd: name}),
data: JSON.stringify({"cmd": cmd}),
success: () => {
// 请求成功时执行的操作
},
Expand Down
9 changes: 9 additions & 0 deletions xiaomusic/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
34 changes: 27 additions & 7 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ async def init_all_data(self, session):
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(
Expand Down Expand Up @@ -214,6 +213,13 @@ def set_last_record(self, query):

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)
Expand All @@ -230,6 +236,19 @@ async def do_tts(self, value, wait_for_finish=False):
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():
Expand Down Expand Up @@ -363,6 +382,7 @@ async def run_forever(self):
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(
Expand All @@ -384,12 +404,6 @@ async def run_forever(self):
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)
Expand All @@ -403,6 +417,7 @@ def match_cmd(self, query):
# 匹配参数
matcharg = re.match(patternarg, query)
if not matcharg:
# self.log.debug(patternarg)
continue

argpre = matcharg.groups()[0]
Expand Down Expand Up @@ -493,3 +508,8 @@ async def _do_stop():

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}")

0 comments on commit cad7b53

Please sign in to comment.