-
-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathdeltas.ts
More file actions
225 lines (202 loc) · 5.59 KB
/
Copy pathdeltas.ts
File metadata and controls
225 lines (202 loc) · 5.59 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
import { Position } from '.'
import { Brand } from './brand'
/** @hidden */
export interface WithContext {
context: Context
}
/** @inline - Not exported as part of the public API */
type NormalizedBaseDelta = {
context: Context
$source: SourceRef
/** @deprecated Use $source instead */
source?: Source
path: Path
timestamp: Timestamp
}
/** @hidden */
export type NormalizedMetaDelta = NormalizedBaseDelta & {
value: MetaValue
isMeta: true
}
/** @hidden */
export type NormalizedValueDelta = NormalizedBaseDelta & {
value: Value
/**
* Optional staleness-state container preserved through the cache so
* replayed deltas (and the HTTP API snapshot built from the cache)
* surface the server's `state.timedOut` flag, matching what a live WS
* client received in the original delta.
*/
state?: PathValueState
isMeta: false
}
/** @hidden */
export type NormalizedDelta = NormalizedValueDelta | NormalizedMetaDelta
/** @category Server API */
export type SourceRef = Brand<string, 'sourceRef'>
/** @category Server API */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Source = any
/** @category Server API */
export type Path = Brand<string, 'path'>
/** @category Server API */
export type Timestamp = Brand<string, 'timestamp'>
/** @category Server API */
export type Context = Brand<string, 'context'>
/** @category Server API */
export type NotificationId = Brand<string, 'notificationId'>
/** @category Server API */
export type Value = object | number | string | null | Notification | boolean
/** @category Server API */
export interface Delta {
context?: Context
updates: Update[]
}
/**
* @deprecated earlier mistake assumed ValuesDelta and MetaDelta were separate
* @hidden
*/
export type ValuesDelta = Delta
/**
* @deprecated earlier mistake assumed ValuesDelta and MetaDelta were separate
* @hidden
*/
export type MetaDelta = Delta
/** @category Server API */
export type Update = {
timestamp?: Timestamp
/** @deprecated Use $source (SourceRef) instead for more practical string-based referencing */
source?: Source
$source?: SourceRef
notificationId?: NotificationId
} & ({ values: PathValue[] } | { meta: Meta[] }) // require either values or meta or both
/** @category Server API */
export function hasValues(u: Update): u is Update & { values: PathValue[] } {
return 'values' in u && Array.isArray(u.values)
}
/** @category Server API */
export function hasMeta(u: Update): u is Update & { meta: Meta[] } {
return 'meta' in u && Array.isArray(u.meta)
}
// Update delta
/** @category Server API */
export interface PathValue {
path: Path
value: Value
state?: PathValueState
}
/**
* Out-of-band metadata about a particular delta value, set by the server when
* a value is anything other than a fresh reading from its source. Distinct
* from `Meta` (which describes the path itself) and from `value` (which is
* what the source reported). Clients that don't recognise `state` ignore it.
*
* @category Server API
*/
export interface PathValueState {
/**
* Set to `true` on the synthetic delta the server emits when a source's
* `meta.timeout` has elapsed since its last update. A sensor that
* legitimately publishes `value: null` (e.g. an echosounder with no
* bottom return) does not carry this flag.
*/
readonly timedOut?: boolean
/**
* The last good (non-null) value seen from the timed-out source and its
* original timestamp. Absent if no good value was ever received.
*/
readonly lastValue?: {
readonly timestamp: Timestamp
readonly value: Value
}
}
// Notification payload
/** @category Server API */
export interface Notification {
state: ALARM_STATE
method: ALARM_METHOD[]
message: string
status?: AlarmStatus
position?: Position
createdAt?: Timestamp
id?: string
data?: Record<string, Value>
}
// MetaMessage
/** @category Server API */
export interface Meta {
path: Path
value: MetaValue
}
/**
* Update contract for a path. Determines whether the server's staleness
* enforcer treats silence as a failure (`periodic`) or as unchanged state
* (`event`).
*
* @category Server API
*/
export type UpdateContract = 'periodic' | 'event'
// Meta payload
/** @category Server API */
export interface MetaValue {
description?: string
units?: string
example?: string
/**
* Time in seconds after which a path+source's last value is considered
* stale. `'auto'` lets the server derive the timeout from observed delta
* arrival rate. `0` disables enforcement for the path.
*/
timeout?: number | 'auto'
/**
* Declares the path's update contract for staleness enforcement.
* Defaults to `'periodic'` when absent.
*/
updateContract?: UpdateContract
displayName?: string
displayScale?: {
lower: number
upper: number
}
zones?: Zone[]
supportsPut?: boolean
displayUnits?: {
category: string
targetUnit: string
displayFormat?: string
formula: string
inverseFormula: string
symbol: string
}
}
// Notification attribute types
/** @category Server API */
export enum ALARM_STATE {
nominal = 'nominal',
normal = 'normal',
alert = 'alert',
warn = 'warn',
alarm = 'alarm',
emergency = 'emergency'
}
/** @category Server API */
export enum ALARM_METHOD {
visual = 'visual',
sound = 'sound'
}
/** @category Server API */
export interface AlarmStatus {
silenced: boolean
acknowledged: boolean
acknowledgedAt?: Timestamp
canSilence: boolean
canAcknowledge: boolean
canClear: boolean
}
/** @category Server API */
export interface Zone {
lower: number | undefined
upper: number | undefined
state: ALARM_STATE
message: string
}