Repository navigation
Expand file tree
/
Copy pathnotifications.ts
More file actions
121 lines (114 loc) · 6.1 KB
/
Copy pathnotifications.ts
File metadata and controls
121 lines (114 loc) · 6.1 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
import type { BrainStatus } from './status';
import type { AlertEvent, InfoEvent, ConnectionState } from './webSocketSource';
export type NotificationEvent = AlertEvent | InfoEvent;
export interface HistoryEntry { id: number; event: NotificationEvent; receivedAt: number }
export type WarningIndication = 'indicated' | 'unknown' | 'not-indicated';
export interface CurrentWarning {
entry: HistoryEntry;
indication: WarningIndication;
afterT: number | null;
}
export interface NotificationState {
history: HistoryEntry[];
warnings: Partial<Record<AlertEvent['reason'], CurrentWarning>>;
nextId: number;
connection: ConnectionState;
board: boolean | null;
lastStatusT: number | null;
ready: boolean;
}
export type NotificationAction =
| { type: 'event'; event: NotificationEvent; receivedAt: number }
| { type: 'status'; status: BrainStatus }
| { type: 'connection'; value: ConnectionState }
| { type: 'board'; connected: boolean };
// The phone notification's title and urgency (pc/ibuki/server.py), so both say the same.
export const notificationMessages: Record<NotificationEvent['reason'], string> = {
no_breath: '呼吸が確認できません',
left_bed: 'ベッドにいません',
woke_up: '起きました',
fast_breathing: '呼吸が速い状態が続いています',
};
export type NotificationLevel = '緊急' | '注意' | 'お知らせ';
export const notificationLevels: Record<NotificationEvent['reason'], NotificationLevel> = {
no_breath: '緊急', left_bed: '注意', fast_breathing: '注意', woke_up: 'お知らせ',
};
export function createNotificationState(): NotificationState {
return { history: [], warnings: {}, nextId: 1, connection: 'connecting', board: null, lastStatusT: null, ready: false };
}
function unknownWarnings(state: NotificationState, resetClock = false): NotificationState['warnings'] {
return Object.fromEntries(Object.entries(state.warnings).map(([reason, warning]) => [reason,
warning.indication === 'not-indicated' ? warning : {
...warning, indication: 'unknown', afterT: resetClock ? null : warning.afterT,
},
]));
}
// Pure presentation bookkeeping. It never changes Brain statuses, wave samples,
// transport timers, or the backend. "not-indicated" is not medical resolution.
export function reduceNotifications(state: NotificationState, action: NotificationAction): NotificationState {
if (action.type === 'event') {
const entry = { id: state.nextId, event: { ...action.event }, receivedAt: action.receivedAt };
const next = { ...state, nextId: state.nextId + 1, history: [entry, ...state.history].slice(0, 20) };
if (action.event.type === 'info') return next;
const afterT = action.event.t === undefined ? state.lastStatusT
: Math.max(action.event.t, state.lastStatusT ?? action.event.t);
return { ...next, warnings: { ...state.warnings, [action.event.reason]: {
entry, indication: state.ready ? 'indicated' : 'unknown', afterT,
} } };
}
if (action.type === 'connection') {
if (action.value === state.connection) return state;
return { ...state, connection: action.value,
board: action.value === 'connecting' ? null : state.board,
ready: false, lastStatusT: null, warnings: unknownWarnings(state, true) };
}
if (action.type === 'board') {
if (action.connected === state.board) return state;
return { ...state, board: action.connected, ready: false,
lastStatusT: null, warnings: unknownWarnings(state, true) };
}
const status = action.status;
if (state.connection !== 'connected' || state.board === false) {
return { ...state, ready: false, lastStatusT: null, warnings: unknownWarnings(state) };
}
const progresses = state.lastStatusT !== null && status.t > state.lastStatusT;
const resetClock = state.lastStatusT !== null && status.t < state.lastStatusT;
if (!progresses || status.shaking || status.source === 'placeholder') {
return { ...state, ready: false, lastStatusT: status.t, warnings: unknownWarnings(state, resetClock) };
}
const warnings = Object.fromEntries(Object.entries(state.warnings).map(([reason, warning]) => {
if (warning.indication === 'not-indicated') return [reason, warning];
// A status preceding an event (or the same t repeated) cannot dismiss it.
if (warning.afterT !== null && status.t <= warning.afterT) return [reason, { ...warning, indication: 'unknown' }];
let indication: WarningIndication = 'unknown';
if (reason === 'no_breath') {
if (status.present && (status.state === 'ALERT' || status.state === 'CHECK')) indication = 'indicated';
if (status.present && (status.state === 'OK' || status.state === 'STILL')) indication = 'not-indicated';
// EMPTY alone cannot establish the prior breathing-motion condition.
} else if (reason === 'left_bed') {
if (!status.present && status.state === 'EMPTY') indication = 'indicated';
if (status.present && status.state !== 'EMPTY') indication = 'not-indicated';
}
return [reason, { ...warning, indication }];
}));
return { ...state, ready: true, lastStatusT: status.t, warnings };
}
export function notificationPresentation(state: NotificationState) {
const current = Object.values(state.warnings)
.filter(warning => warning.indication !== 'not-indicated')
.sort((a, b) => b.entry.id - a.entry.id);
const unknown = !state.ready || state.connection !== 'connected' || state.board === false || current.some(warning => warning.indication === 'unknown');
const message = unknown ? '現在の状態は確認できません'
: current.length ? '受信イベントと、その後の解析状態に基づく表示です'
: Object.keys(state.warnings).length ? '現在の解析状態では継続していません'
: '警告イベントは受信していません';
return { current, unknown, message };
}
export function notificationDetails(event: NotificationEvent): string[] {
const details: string[] = [];
if (event.since_s > 0 && (event.reason === 'no_breath' || event.reason === 'fast_breathing')) {
details.push(`${event.since_s}秒続いたとき`);
}
if (event.bpm !== undefined) details.push(`呼吸数 ${event.bpm}回/分`);
return details;
}