电脑实现小爱语音关机

tsvico Lv5

目前使用的是神秘鸭来实现关机的,免费版本足够我的日常使用,不过我在几次失灵后,就决定自己实现一个,一般是在电脑断网后再重新连接网络就会失灵,而且神秘鸭接入的是巴法云,巴法云在 APP 没找到设备在线状态,不利于远程查看是否开机(偶尔会使用 tailscale 远程组网控制电脑),网上没有找到满意的版本,决定基于 TypeScript 实现一个 nodejs 版本的关机程序,以下是开发过程

开发过程

  1. 新建开发目录
1
mkdir blinker-js-shoutdown
  1. 引入 blinker-js
1
2
3
cd blinker-js-shoutdown
git clone add https://github.com/blinker-iot/blinker-js.git
mkdir src

我们的程序主要放在 src 目录中,主程序文件为 index.ts

  1. 参考点灯文档,获取 key
    文档

我准备以灯的方式接入,这样可以将控制亮度映射被调整音量

通过调试发现,在 windows 上点灯连接服务器有时会触发超时,在 index.ts 中设置全局超时时间

1
2
3
4
5
6
7
8
import axios from "axios";
import https from "https";
import http from "http";

axios.defaults.timeout = 30 * 1000; // 30s
axios.defaults.httpsAgent = new https.Agent({ keepAlive: true });
axios.defaults.httpAgent = new http.Agent({ keepAlive: true });

  1. 关机实现
    关机我们可以借助 shutdown 系统命令
    写一个函数
1
2
3
4
5
6
7
8
9
10
11
function shutdownWindow() {
let command = exec("shutdown -s -t 00", function (err, stdout, stderr) {
if (err || stderr) {
console.log("shutdown failed" + err + stderr);
}
});
command.stdin.end();
command.on("close", function (code) {
console.log("shutdown", code);
});
}
  1. 音量调节实现
    音量调节我们使用 loudness,安装 npm i loudness --save

  2. 完整代码
    目前发现已知问题,点灯的官方库不能及时响应小爱音箱,总是提示 “要操作的设备出问题了”,但是实际上又是可以控制的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { BlinkerDevice } from "../lib/blinker";
import { ButtonWidget, NumberWidget, RangeWidget } from "../lib/widget";
import { Miot, VA_TYPE, MI_LIGHT_MODE } from "../lib/voice-assistant";
import axios from "axios";
import https from "https";
import http from "http";
import { exec } from "child_process";
import loudness from "loudness";

axios.defaults.timeout = 60 * 1000; // 30s
axios.defaults.httpsAgent = new https.Agent({
keepAlive: true,
maxSockets: 100,
maxFreeSockets: 100,
});
axios.defaults.httpAgent = new http.Agent({
keepAlive: true,
maxSockets: 100,
maxFreeSockets: 100,
});

const authkey = process.env.AUTHKEY;
if (!authkey) {
throw new Error("AUTHKEY 未配置,请检查环境变量");
}
const device = new BlinkerDevice(authkey, {
protocol: "mqtts", // 可选协议mqtt/mqtts/ws/wss
webSocket: false, // 是否开启本地webSocket,默认开启
sourceCheck: true, // 是否开启来源检查,默认开启
});

const miot = device.addVoiceAssistant(new Miot(VA_TYPE.LIGHT));

// 注册组件
// 关机按钮
const shoutdownButton: ButtonWidget = device.addWidget(
new ButtonWidget("btn-abc")
);
// 关机次数
const number1: NumberWidget = device.addWidget(new NumberWidget("num-abc"));
// 音量
const sound: RangeWidget = device.addWidget(new RangeWidget("ran-xax"));

let num = 0;
let soundValue = 0;

function rgb2int(r: number, g: number, b: number) {
return (0xff << 24) | (r << 16) | (g << 8) | b;
}

function int2rgb(value: number) {
let r = (value & 0xff0000) >> 16;
let g = (value & 0xff00) >> 8;
let b = value & 0xff;
return [r, g, b];
}

function shutdownWindow() {
let command = exec("shutdown -s -t 00", function (err, stdout, stderr) {
if (err || stderr) {
console.log("shutdown failed" + err + stderr);
}
});
command.stdin.end();
command.on("close", function (code) {
console.log("shutdown", code);
});
}

function onShoutdownClick() {
console.log("click shoutdownButton:");
num++;
number1.value(num).update();
shoutdownButton.color("#DCDCDC").text("已关机").update();
shutdownWindow();
}

function changeSound(value: number) {
if (soundValue == value) {
return;
}
loudness.setVolume(value).then(() => {
console.log("setVolume:", value);
});
soundValue = value;
if (soundValue > 0) {
loudness.getMuted().then((isMuted) => {
if (isMuted) {
loudness.setMuted(false).then();
}
});
}
}

async function getSoundNumber() {
return await loudness.getVolume();
}

function init() {
number1.value(num).update();
getSoundNumber().then((value) => {
console.log("now SoundNumber:", value);
sound.value(value).update();
});
shoutdownButton.color("#ff0000").text("关机").update();
}

device.ready().then(() => {
device.dataRead.subscribe((message: any) => {
console.log("otherData:", message);
});

device.heartbeat.subscribe((message) => {
console.log("heartbeat:", message);
init();
});

shoutdownButton.listen().subscribe((message: any) => {
onShoutdownClick();
});

sound.listen().subscribe((message) => {
changeSound(message.data);
console.log("range:", message.data);
});

// 电源状态改变 适用于灯和插座
miot.powerChange.subscribe((message: any) => {
console.log("powerChange", message.data);
switch (message.data.set.pState) {
case "true":
message.power("on").update();
break;
case "false":
onShoutdownClick();
message.power("off").update();
break;
default:
break;
}
});

// 模式改变 适用于灯和插座
miot.modeChange.subscribe((message: any) => {
console.log("modeChange", message.data);
switch (message.data.set.mode) {
case MI_LIGHT_MODE.DAY:
break;
case MI_LIGHT_MODE.NIGHT:
break;
case MI_LIGHT_MODE.COLOR:
break;
case MI_LIGHT_MODE.WARMTH:
break;
case MI_LIGHT_MODE.TV:
break;
case MI_LIGHT_MODE.READING:
break;
case MI_LIGHT_MODE.COMPUTER:
break;
default:
break;
}
message.mode(message.data.set.mode).update();
});

// 颜色改变 适用于灯
miot.colorChange.subscribe((message: any) => {
// not do anything
console.log("RGB:", int2rgb(Number(message.data.set.col)));
});

// 色温改变 适用于灯
miot.colorTempChange.subscribe((message: any) => {
console.log("色温", message);
message.colorTemp(255).update();
});

// 亮度改变 适用于灯
let brightness = 50;
miot.brightnessChange.subscribe((message: any) => {
console.log("亮度", message.data.set);
if (typeof message.data.set.bright != "undefined") {
brightness = Number(message.data.set.bright);
changeSound(brightness);
} else if (typeof message.data.set.upBright != "undefined") {
brightness = brightness + Number(message.data.set.upBright);
} else if (typeof message.data.set.downBright != "undefined") {
brightness = brightness - Number(message.data.set.downBright);
}
message.brightness(brightness).update();
});

miot.stateQuery.subscribe((message: any) => {
console.log(message.data);
message.power("on").update();
});
});


  1. 界面样式
1
{¨version¨¨3.0.0¨¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨dark¨¨background¨{¨img¨¨assets/img/headerbg.jpg¨¨isFull¨«}}¨dashboard¨|{¨type¨¨btn¨¨ico¨¨fad fa-power-off¨¨mode¨É¨t0¨¨ 关机 ¨¨clr¨¨#389BEE¨¨t1¨¨ 文本 2¨¨bg¨É¨cols¨Ë¨rows¨Ë¨key¨¨btn-abc¨´x´Î´y´Ê¨speech¨|÷¨lstyle¨É}{ßC¨num¨ßH¨ 关机次数 ¨ßE¨fad fa-american-sign-language-interpreting¨ßJßK¨min¨É¨max¨¢1c¨uni¨´ 次 ´ßNÉßOÍßPËßQ¨num-abc¨´x´É´y´ÊßS|÷ßTÊ}{ßC¨deb¨ßGÉßNÉßOÑßPÌßQ¨debug¨´x´É´y´¤A}{ßC¨ran¨ßH¨ 电脑音量 ¨ßJßKßYº0ßXÉßNÉßOÑßPËßQ¨ran-xax¨´x´É´y´Í¨rt¨»}÷¨actions¨|¦¨cmd¨¦¨switch¨‡¨text¨‡¨on¨¨ 打开?name¨¨off¨¨ 关闭?name¨—÷¨triggers¨|{¨source¨ßj¨source_zh¨¨ 开关状态 ¨¨state¨|ßlßn÷¨state_zh¨|¨ 打开 ¨¨ 关闭 ¨÷}÷ßg|ßf÷}

… 待完善

  • 标题: 电脑实现小爱语音关机
  • 作者: tsvico
  • 创建于 : 2024-12-22 10:44:15
  • 更新于 : 2024-12-23 18:15:49
  • 链接: https://blog.tbox.fun/2024/1104496865.html
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
电脑实现小爱语音关机