-
-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathalarm.ts
More file actions
252 lines (231 loc) · 6.52 KB
/
Copy pathalarm.ts
File metadata and controls
252 lines (231 loc) · 6.52 KB
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
import {
ALARM_METHOD,
ALARM_STATE,
AlarmStatus,
Context,
Delta,
hasValues,
Notification,
NotificationId,
Path,
SourceRef,
Timestamp,
Update,
AlarmProperties
} from '@signalk/server-api'
import { buildKey } from './notificationManager'
export class Alarm {
private external = false // true when alarm was created from delta
status: AlarmStatus = {
silenced: false,
acknowledged: false,
canSilence: true,
canAcknowledge: true,
canClear: false
}
private context: Context = '' as Context
private update: Update = {
values: [],
$source: 'notificationsApi' as SourceRef,
timestamp: undefined,
notificationId: undefined
}
private path: Path = '' as Path
public value: Notification = {
state: ALARM_STATE.normal,
method: [ALARM_METHOD.visual, ALARM_METHOD.sound],
message: '',
status: this.status
}
constructor(notificationId?: NotificationId) {
if (notificationId) {
this.timeStamp()
this.status.canClear = true
this.update.notificationId = notificationId
this.path = `notifications.${notificationId}` as Path
}
}
/**
* Extract and populate attributes from update and context
*/
private parseDelta(update: Update, context: Context) {
this.context = context
this.update = update
if (hasValues(this.update)) {
this.path = this.update.values[0].path
// ensure value is not empty
if (this.update.values[0].value) {
this.value = this.update.values[0].value as Notification
} else {
this.value.message = ''
this.value.method = []
this.value.state = ALARM_STATE.normal
}
}
}
/**
* Align notification alarm method with state and recorded user action
*/
private alignAlarmMethod() {
if (this.status.acknowledged) {
if (this.value.state === 'emergency') {
this.value.method = [ALARM_METHOD.visual]
} else {
this.value.method = []
}
} else if (this.status.silenced) {
if (this.value.state !== 'emergency') {
this.value.method = this.value.method?.filter((i) => i !== 'sound')
}
}
}
/** Update the timestamp to the current date / time */
private timeStamp() {
this.update.timestamp = new Date().toISOString() as Timestamp
}
/**
* Create / update alarm from incoming update and context
*/
public syncFromNotificationUpdate(update: Update, context: Context) {
this.external = true
this.status.canClear = false
const prevState = this.value?.state
this.parseDelta(update, context)
if (
prevState === ALARM_STATE.normal &&
this.value.state !== ALARM_STATE.normal
) {
this.status.acknowledged = false
delete this.status.acknowledgedAt
this.status.silenced = false
}
if (
!this.status.acknowledged &&
this.value &&
'acknowledgeStatus' in this.value
) {
if (this.value.acknowledgeStatus === 'Yes') {
this.status.acknowledgedAt = new Date().toISOString() as Timestamp
this.status.acknowledged = true
} else {
this.status.acknowledged = false
delete this.status.acknowledgedAt
}
}
if (
!this.status.silenced &&
this.value &&
'temporarySilenceStatus' in this.value
) {
this.status.silenced =
this.value.temporarySilenceStatus === 'Yes' ? true : false
}
if (this.value && 'temporarySilenceSupport' in this.value) {
this.status.canSilence =
this.value.temporarySilenceSupport === 'Yes' ? true : false
}
if (this.value && 'acknowledgeSupport' in this.value) {
this.status.canAcknowledge =
this.value.acknowledgeSupport === 'Yes' ? true : false
}
this.alignAlarmMethod()
}
public setContext(context: Context) {
this.context = context
}
/**
* Returns true if Alarm is external (generated from incoming Delta message)
*/
get isExternal(): boolean {
return this.external
}
/**
* Generates and returns the delta payload for use with `handleMessage()`
*/
get delta(): Delta {
if (hasValues(this.update)) {
this.update.values = [
{
path: this.path,
value: this.value
? Object.assign(
this.value,
{ id: this.update.notificationId },
{ status: this.status }
)
: this.value
}
]
}
// Strip the original source metadata so handleMessage does not
// create a phantom N2K device entry under the notificationApi label.
// The $source field is sufficient for delta routing.
const { source: _source, ...updateWithoutSource } = this.update
const d: Delta = { updates: [updateWithoutSource as Update] }
if (this.external) {
d.context = this.context
}
return d
}
/** Return Alarm properties */
get properties(): AlarmProperties {
return {
context: this.context,
path: this.path,
value: this.value
}
}
/**
* Return the external key (context/path/$source) of an alarm generated from incoming Delta.
*/
get extKey(): string {
return buildKey(this.context, this.path, this.update.$source as SourceRef)
}
/**
* Sets the path associated with the alarm.
*/
public setPath(path: Path, id?: string) {
if (path) {
path = path.startsWith('notifications.')
? path
: (`notifications.${path}` as Path)
this.path = id ? (`${path}.${id}` as Path) : path
}
}
public silence() {
if (!this.status.canSilence) {
throw new Error('Alarm cannot be silenced!')
}
if (this.status.silenced || this.status.acknowledged) {
throw new Error('Alarm already silenced or acknowledged!')
}
if (this.value.state === 'emergency') {
throw new Error('Cannot silence Emergency Alarm!')
}
this.status.silenced = true
this.alignAlarmMethod()
this.timeStamp()
}
public acknowledge() {
if (!this.status.canAcknowledge) {
throw new Error('Alarm cannot be acknowledged!')
}
if (this.status.acknowledged) {
throw new Error('Alarm already acknowledged!')
}
this.status.acknowledged = true
this.status.acknowledgedAt = new Date().toISOString() as Timestamp
this.alignAlarmMethod()
this.timeStamp()
}
public clear() {
if (!this.status.canClear) {
throw new Error('Alarm cannot be cleared!')
}
this.value.state = ALARM_STATE.normal
this.status.silenced = false
this.status.acknowledged = false
delete this.status.acknowledgedAt
this.timeStamp()
}
}