forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring-chime.ts
152 lines (126 loc) · 3.09 KB
/
ring-chime.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
import {
ChimeData,
ChimeUpdate,
ChimeSoundKind,
RingtoneOptions,
ChimeHealth,
ChimeModel,
} from './ring-types'
import { clientApi, RingRestClient } from './rest-client'
import { BehaviorSubject, Subject } from 'rxjs'
const settingsWhichRequireReboot = [
'ding_audio_id',
'ding_audio_user_id',
'motion_audio_id',
'motion_audio_user_id',
]
export class RingChime {
id
deviceType
model
onData
onRequestUpdate = new Subject()
constructor(
private initialData: ChimeData,
private restClient: RingRestClient,
) {
this.id = this.initialData.id
this.deviceType = this.initialData.kind
this.model = ChimeModel[this.deviceType] || 'Chime'
this.onData = new BehaviorSubject<ChimeData>(this.initialData)
}
updateData(update: ChimeData) {
this.onData.next(update)
}
requestUpdate() {
this.onRequestUpdate.next(null)
}
get data() {
return this.onData.getValue()
}
get name() {
return this.data.description
}
get description() {
return this.data.description
}
get volume() {
return this.data.settings.volume
}
getRingtones() {
return this.restClient.request<RingtoneOptions>({
url: clientApi('ringtones'),
})
}
async getRingtoneByDescription(description: string, kind: ChimeSoundKind) {
const ringtones = await this.getRingtones(),
requestedRingtone = ringtones.audios.find(
(audio) =>
audio.available &&
audio.description === description &&
audio.kind === kind,
)
if (!requestedRingtone) {
throw new Error('Requested ringtone not found')
}
return requestedRingtone
}
chimeUrl(path = '') {
return clientApi(`chimes/${this.id}/${path}`)
}
playSound(kind: ChimeSoundKind) {
return this.restClient.request({
url: this.chimeUrl('play_sound'),
method: 'POST',
json: { kind },
})
}
async snooze(time: number) {
// time is in minutes, max 24 * 60 (1440)
await this.restClient.request({
url: this.chimeUrl('do_not_disturb'),
method: 'POST',
json: { time },
})
this.requestUpdate()
}
async clearSnooze() {
await this.restClient.request({
url: this.chimeUrl('do_not_disturb'),
method: 'POST',
})
this.requestUpdate()
}
async updateChime(update: ChimeUpdate) {
await this.restClient.request({
url: this.chimeUrl(),
method: 'PUT',
json: { chime: update },
})
this.requestUpdate()
// inform caller if this change requires a reboot
return Object.keys(update.settings || {}).some((key) =>
settingsWhichRequireReboot.includes(key),
)
}
setVolume(volume: number) {
if (volume < 0 || volume > 11) {
throw new Error(
`Volume for ${this.name} must be between 0 and 11, got ${volume}`,
)
}
return this.updateChime({
settings: {
volume,
},
})
}
async getHealth() {
const response = await this.restClient.request<{
device_health: ChimeHealth
}>({
url: clientApi(`chimes/${this.id}/health`),
})
return response.device_health
}
}