/
voice-assistant.ts
340 lines (292 loc) · 9.28 KB
/
voice-assistant.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
export enum VA_TYPE {
LIGHT = 'light',
OUTLET = 'outlet',
MULTI_OUTLET = 'multi_outlet',
SENSOR = 'sensor',
FAN = 'fan',
AIRCONDITION = 'aircondition'
}
export enum MI_LIGHT_MODE {
DAY,
NIGHT,
COLOR,
WARMTH,
TV,
READING,
COMPUTER,
}
export enum ALI_LIGHT_MODE {
READING = 'reading',
MOVIE = 'movie',
SLEEP = 'sleep',
LIVE = 'live',
HOLIDAY = 'holiday',
MUSIC = 'music',
COMMON = 'common',
NIGHT = 'night'
}
export enum DUER_LIGHT_MODE {
READING = 'READING',
SLEEP = 'SLEEP',
ALARM = 'ALARM',
// DAYTIME = 'DAYTIME',
NIGHT_LIGHT = 'NIGHT_LIGHT',
ROMANTIC = 'ROMANTIC',
SUNDOWN = 'SUNDOWN',
SUNRISE = 'SUNRISE',
RELAX = 'RELAX',
LIGHTING = 'LIGHTING',
SUN = 'SUN',
STAR = 'STAR',
ENERGY_SAVING = 'ENERGY_SAVING',
MOON = 'MOON',
JUDI = 'JUDI',
// HEAT = 'HEAT',
// COOL = 'COOL',
// AUTO = 'AUTO'
}
import { Subject } from "rxjs";
import { BlinkerDevice } from "./blinker";
import { u8aToString } from "./fun"
import { Message } from "./message"
import { vaLog } from "./debug"
export class VoiceAssistant {
get subTopic() {
return `/sys/${this.device.config.productKey}/${this.device.config.deviceName}/rrpc/request/+`
}
get pubTopic() {
return `/sys/${this.device.config.productKey}/${this.device.config.deviceName}/rrpc/response/`
}
vaType;
vaName;
device: BlinkerDevice;
change = new Subject();
targetDevice;
powerChange = new Subject<powerMessage>();
modeChange = new Subject<modeMessage>();
colorChange = new Subject<colorMessage>();
colorTempChange = new Subject<colorTempMessage>();
brightnessChange = new Subject<brightnessMessage>();
stateQuery = new Subject<dataMessage>();
constructor(key) {
this.vaType = key
}
listen() {
this.device.mqttClient.on('message', (topic, message) => {
// console.log(topic);
// console.log(u8aToString(message));
if (topic.indexOf(this.subTopic.substr(0, this.subTopic.length - 1)) > -1) {
let data;
let fromDevice;
let messageId;
try {
let messageString = u8aToString(message)
let messageObject = JSON.parse(messageString)
fromDevice = messageObject.fromDevice
data = messageObject.data
this.targetDevice = fromDevice
messageId = topic.split('/')[6]
vaLog(data, `${this.vaName}>device`)
} catch (error) {
console.log(error);
}
if (fromDevice == this.vaName)
this.processData(messageId, data)
}
})
}
unlisten() {
// this.changeSubscription.unsubscribe();
}
processData(messageId, data) {
// console.log(data);
if (typeof data.set != 'undefined') {
if (typeof data.set.pState != 'undefined') {
this.powerChange.next(new powerMessage(this.device, this, messageId, data))
} else if (typeof data.set.col != 'undefined') {
this.colorChange.next(new colorMessage(this.device, this, messageId, data))
} else if (typeof data.set.colTemp != 'undefined') {
this.colorTempChange.next(new colorTempMessage(this.device, this, messageId, data))
} else if (typeof data.set.mode != 'undefined') {
this.modeChange.next(new modeMessage(this.device, this, messageId, data))
} else if (typeof data.set.bright != 'undefined' || typeof data.set.upBright != 'undefined' || typeof data.set.downBright != 'undefined') {
this.brightnessChange.next(new brightnessMessage(this.device, this, messageId, data))
}
} else if (typeof data.get != 'undefined') {
this.stateQuery.next(new dataMessage(this.device, this, messageId, data))
}
}
}
export class Miot extends VoiceAssistant {
constructor(key) {
super(key)
this.vaType = { miType: key }
this.vaName = 'MIOT'
}
mode(mode: MI_LIGHT_MODE) {
return this
}
}
export class AliGenie extends VoiceAssistant {
constructor(key) {
super(key)
this.vaType = { aliType: key }
this.vaName = 'AliGenie'
}
}
export class DuerOS extends VoiceAssistant {
constructor(key) {
super(key)
let newkey;
switch (key) {
case VA_TYPE.LIGHT:
newkey = 'LIGHT'
break;
case VA_TYPE.OUTLET:
newkey = 'SOCKET'
break;
case VA_TYPE.MULTI_OUTLET:
newkey = 'MULTI_SOCKET'
break;
case VA_TYPE.SENSOR:
newkey = 'AIR_MONITOR'
break;
}
this.vaType = { duerType: newkey }
this.vaName = 'DuerOS'
}
}
export class VaMessage extends Message {
id: number;
device: BlinkerDevice;
voiceAssistant: VoiceAssistant;
get data(): any {
return this.request
}
request = {};
response = {};
constructor(device, voiceAssistant, id, request) {
super(device)
this.device = device
this.id = id
this.request = request
this.voiceAssistant = voiceAssistant
}
update() {
let responseStr = JSON.stringify(this.response)
let data = `{ "fromDevice": "${this.device.config.deviceName}", "toDevice": "${this.voiceAssistant.vaName}_r", "data": ${responseStr}, "deviceType": "vAssistant"}`
let base64Data = Buffer.from(data).toString('base64')
this.device.mqttClient.publish(this.voiceAssistant.pubTopic + this.id, base64Data)
vaLog(responseStr, `device>${this.voiceAssistant.vaName}`)
}
}
class powerMessage extends VaMessage {
power(state: string) {
let data = { pState: state }
this.response = Object.assign(this.response, data)
return this
}
num(num: number) {
let data = { num: num }
this.response = Object.assign(this.response, data)
return this
}
}
class modeMessage extends VaMessage {
mode(state: string | number) {
let data = { mode: state }
this.response = Object.assign(this.response, data)
return this
}
}
class colorMessage extends VaMessage {
color(color: string | number[]) {
let data = { clr: color }
this.response = Object.assign(this.response, data)
return this
}
}
class colorTempMessage extends VaMessage {
colorTemp(val: number) {
let data = { colTemp: val }
this.response = Object.assign(this.response, data)
return this
}
}
class brightnessMessage extends VaMessage {
brightness(val: number | string) {
if (typeof val == 'number') val = val.toString()
let data = { bright: val }
this.response = Object.assign(this.response, data)
return this
}
}
class dataMessage extends VaMessage {
temp(val: number | string) {
if (typeof val == 'number') val = val.toString()
let data = { temp: val }
this.response = Object.assign(this.response, data)
return this
}
humi(val: number | string) {
if (typeof val == 'number') val = val.toString()
let data = { humi: val }
this.response = Object.assign(this.response, data)
return this
}
aqi(val: number | string) {
if (typeof val == 'number') val = val.toString()
let data = { aqi: val }
this.response = Object.assign(this.response, data)
return this
}
pm25(val: number | string) {
if (typeof val == 'number') val = val.toString()
let data = { pm25: val }
this.response = Object.assign(this.response, data)
return this
}
pm10(val: number | string) {
if (typeof val == 'number') val = val.toString()
let data = { pm10: val }
this.response = Object.assign(this.response, data)
return this
}
co2(val: number | string) {
if (typeof val == 'number') val = val.toString()
let data = { co2: val }
this.response = Object.assign(this.response, data)
return this
}
brightness(val: number | string) {
if (typeof val == 'number') val = val.toString()
let data = { bright: val }
this.response = Object.assign(this.response, data)
return this
}
color(color: string | number[]) {
let data = { clr: color }
this.response = Object.assign(this.response, data)
return this
}
colorTemp(val: number | string) {
if (typeof val == 'number') val = val.toString()
let data = { colorTemp: val }
this.response = Object.assign(this.response, data)
return this
}
mode(state: string | number) {
let data = { mode: state }
this.response = Object.assign(this.response, data)
return this
}
power(state: string) {
if (this.voiceAssistant.vaName == 'MIOT') {
if (state == 'on') state = 'true'
else state = 'false'
}
let data = { pState: state }
this.response = Object.assign(this.response, data)
return this
}
}